From 9eddc296c34f932cbe6efdf7d6558118306353d0 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Thu, 23 May 2019 10:03:11 -0400 Subject: [PATCH] Fix clear value filtering and integer support --- ffi/wgpu-remote.h | 2 +- ffi/wgpu.h | 2 +- wgpu-native/src/command/mod.rs | 44 +++++++++++++++++++++++++------ wgpu-native/src/command/render.rs | 2 +- wgpu-native/src/conv.rs | 10 ++++++- 5 files changed, 48 insertions(+), 12 deletions(-) diff --git a/ffi/wgpu-remote.h b/ffi/wgpu-remote.h index e41ecdc9e0..65336655dc 100644 --- a/ffi/wgpu-remote.h +++ b/ffi/wgpu-remote.h @@ -1,6 +1,6 @@ -/* Generated with cbindgen:0.8.6 */ +/* Generated with cbindgen:0.8.7 */ #include #include diff --git a/ffi/wgpu.h b/ffi/wgpu.h index 12e4d9f0b6..abadc3ed4c 100644 --- a/ffi/wgpu.h +++ b/ffi/wgpu.h @@ -1,6 +1,6 @@ #define WGPU_LOCAL -/* Generated with cbindgen:0.8.6 */ +/* Generated with cbindgen:0.8.7 */ #include #include diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index 3159812e08..2a69b58ec6 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -263,7 +263,7 @@ pub fn command_encoder_begin_render_pass( }; let mut render_pass_cache = device.render_passes.lock(); - let render_pass = match render_pass_cache.entry(rp_key) { + let render_pass = match render_pass_cache.entry(rp_key.clone()) { Entry::Occupied(e) => e.into_mut(), Entry::Vacant(e) => { let color_ids = [ @@ -329,14 +329,42 @@ pub fn command_encoder_begin_render_pass( let clear_values = color_attachments .iter() - .map(|at| { - //TODO: integer types? - let value = hal::command::ClearColor::Float(conv::map_color(&at.clear_color)); - hal::command::ClearValueRaw::from(hal::command::ClearValue::Color(value)) + .zip(&rp_key.colors) + .flat_map(|(at, key)| { + match at.load_op { + LoadOp::Load => None, + LoadOp::Clear => { + use hal::format::ChannelType; + //TODO: validate sign/unsign and normalized ranges of the color values + let value = match key.format.unwrap().base_format().1 { + ChannelType::Unorm | + ChannelType::Snorm | + ChannelType::Ufloat | + ChannelType::Sfloat | + ChannelType::Uscaled | + ChannelType::Sscaled | + ChannelType::Srgb => { + hal::command::ClearColor::Float(conv::map_color_f32(&at.clear_color)) + } + ChannelType::Sint => { + hal::command::ClearColor::Int(conv::map_color_i32(&at.clear_color)) + } + ChannelType::Uint => { + hal::command::ClearColor::Uint(conv::map_color_u32(&at.clear_color)) + } + }; + Some(hal::command::ClearValueRaw::from(hal::command::ClearValue::Color(value))) + } + } }) - .chain(depth_stencil_attachment.map(|at| { - let value = hal::command::ClearDepthStencil(at.clear_depth, at.clear_stencil); - hal::command::ClearValueRaw::from(hal::command::ClearValue::DepthStencil(value)) + .chain(depth_stencil_attachment.and_then(|at| { + match (at.depth_load_op, at.stencil_load_op) { + (LoadOp::Load, LoadOp::Load) => None, + (LoadOp::Clear, _) | (_, LoadOp::Clear) => { + let value = hal::command::ClearDepthStencil(at.clear_depth, at.clear_stencil); + Some(hal::command::ClearValueRaw::from(hal::command::ClearValue::DepthStencil(value))) + } + } })); unsafe { diff --git a/wgpu-native/src/command/render.rs b/wgpu-native/src/command/render.rs index 14b2390afb..11fc6d9c4b 100644 --- a/wgpu-native/src/command/render.rs +++ b/wgpu-native/src/command/render.rs @@ -382,7 +382,7 @@ pub extern "C" fn wgpu_render_pass_set_blend_color(pass_id: RenderPassId, color: pass.blend_color_status = OptionalState::Set; unsafe { - pass.raw.set_blend_constants(conv::map_color(color)); + pass.raw.set_blend_constants(conv::map_color_f32(color)); } } diff --git a/wgpu-native/src/conv.rs b/wgpu-native/src/conv.rs index 5ba3245bb1..3e86f0d887 100644 --- a/wgpu-native/src/conv.rs +++ b/wgpu-native/src/conv.rs @@ -553,10 +553,18 @@ pub fn map_load_store_ops( } } -pub fn map_color(color: &Color) -> hal::pso::ColorValue { +pub fn map_color_f32(color: &Color) -> hal::pso::ColorValue { [color.r, color.g, color.b, color.a] } +pub fn map_color_i32(color: &Color) -> [i32; 4] { + [color.r as i32, color.g as i32, color.b as i32, color.a as i32] +} + +pub fn map_color_u32(color: &Color) -> [u32; 4] { + [color.r as u32, color.g as u32, color.b as u32, color.a as u32] +} + pub fn map_filter(filter: resource::FilterMode) -> hal::image::Filter { match filter { resource::FilterMode::Nearest => hal::image::Filter::Nearest,