diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h index 571043c8d..7e9659120 100644 --- a/wgpu-bindings/wgpu.h +++ b/wgpu-bindings/wgpu.h @@ -617,6 +617,8 @@ typedef struct { #define WGPUColor_WHITE (WGPUColor){ .r = 1, .g = 1, .b = 1, .a = 1 } +#define WGPUPipelineFlags_BLEND_COLOR 1 + #define WGPUShaderStageFlags_COMPUTE 4 #define WGPUShaderStageFlags_FRAGMENT 2 @@ -792,6 +794,8 @@ void wgpu_render_pass_set_bind_group(WGPURenderPassId pass_id, uint32_t index, WGPUBindGroupId bind_group_id); +void wgpu_render_pass_set_blend_color(WGPURenderPassId pass_id, const WGPUColor *color); + void wgpu_render_pass_set_index_buffer(WGPURenderPassId pass_id, WGPUBufferId buffer_id, uint32_t offset); diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index ec4bb5ce6..f6c9981df 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -316,7 +316,7 @@ pub fn command_encoder_begin_render_pass( .iter() .map(|at| { //TODO: integer types? - let value = hal::command::ClearColor::Float(conv::map_color(at.clear_color)); + let value = hal::command::ClearColor::Float(conv::map_color(&at.clear_color)); hal::command::ClearValueRaw::from(hal::command::ClearValue::Color(value)) }) .chain(depth_stencil_attachment.map(|at| { diff --git a/wgpu-native/src/command/render.rs b/wgpu-native/src/command/render.rs index 3b7fa0bbe..a8ea4e983 100644 --- a/wgpu-native/src/command/render.rs +++ b/wgpu-native/src/command/render.rs @@ -1,22 +1,32 @@ +use crate::conv; use crate::command::bind::Binder; use crate::device::RenderPassContext; use crate::hub::HUB; +use crate::pipeline::PipelineFlags; use crate::resource::BufferUsageFlags; use crate::track::{Stitch, TrackerSet}; use crate::{ - BindGroupId, BufferId, CommandBuffer, CommandBufferId, RenderPassId, RenderPipelineId, Stored, + BindGroupId, BufferId, CommandBuffer, CommandBufferId, Color, + RenderPassId, RenderPipelineId, Stored, }; use hal::command::RawCommandBuffer; use std::{iter, slice}; + +#[derive(Debug, PartialEq)] +enum DrawError { + MissingBlendColor, +} + pub struct RenderPass { raw: B::CommandBuffer, cmb_id: Stored, context: RenderPassContext, binder: Binder, trackers: TrackerSet, + blend_color_set: i8, } impl RenderPass { @@ -31,8 +41,18 @@ impl RenderPass { context, binder: Binder::default(), trackers: TrackerSet::new(), + blend_color_set: -1, } } + + fn is_ready(&self) -> Result<(), DrawError> { + //TODO: vertex buffers + //TODO: bind groups + if self.blend_color_set == 0 { + return Err(DrawError::MissingBlendColor) + } + Ok(()) + } } #[no_mangle] @@ -131,8 +151,12 @@ pub extern "C" fn wgpu_render_pass_draw( first_vertex: u32, first_instance: u32, ) { + let mut pass_guard = HUB.render_passes.write(); + let pass = &mut pass_guard[pass_id]; + pass.is_ready().unwrap(); + unsafe { - HUB.render_passes.write()[pass_id].raw.draw( + pass.raw.draw( first_vertex..first_vertex + vertex_count, first_instance..first_instance + instance_count, ); @@ -148,8 +172,12 @@ pub extern "C" fn wgpu_render_pass_draw_indexed( base_vertex: i32, first_instance: u32, ) { + let mut pass_guard = HUB.render_passes.write(); + let pass = &mut pass_guard[pass_id]; + pass.is_ready().unwrap(); + unsafe { - HUB.render_passes.write()[pass_id].raw.draw_indexed( + pass.raw.draw_indexed( first_index..first_index + index_count, base_vertex, first_instance..first_instance + instance_count, @@ -202,6 +230,10 @@ pub extern "C" fn wgpu_render_pass_set_pipeline( "The render pipeline is not compatible with the pass!" ); + if pipeline.flags.contains(PipelineFlags::BLEND_COLOR) && pass.blend_color_set < 0 { + pass.blend_color_set = 0; + } + unsafe { pass.raw.bind_graphics_pipeline(&pipeline.raw); } @@ -238,3 +270,18 @@ pub extern "C" fn wgpu_render_pass_set_pipeline( } } } + +#[no_mangle] +pub extern "C" fn wgpu_render_pass_set_blend_color( + pass_id: RenderPassId, + color: &Color, +) { + let mut pass_guard = HUB.render_passes.write(); + let pass = &mut pass_guard[pass_id]; + + pass.blend_color_set = 1; + + unsafe { + pass.raw.set_blend_constants(conv::map_color(color)); + } +} diff --git a/wgpu-native/src/conv.rs b/wgpu-native/src/conv.rs index 25b80ab6b..8e7bd3a51 100644 --- a/wgpu-native/src/conv.rs +++ b/wgpu-native/src/conv.rs @@ -567,7 +567,7 @@ pub fn map_load_store_ops( } } -pub fn map_color(color: Color) -> hal::pso::ColorValue { +pub fn map_color(color: &Color) -> hal::pso::ColorValue { [color.r, color.g, color.b, color.a] } diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 1146c4ffe..7e6763837 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -1427,10 +1427,16 @@ pub fn device_create_render_pipeline( depth_stencil: depth_stencil_state.map(|state| state.format), }; + let mut flags = pipeline::PipelineFlags::empty(); + if color_states.iter().any(|state| state.color.uses_color() | state.alpha.uses_color()) { + flags |= pipeline::PipelineFlags::BLEND_COLOR; + } + pipeline::RenderPipeline { raw: pipeline, layout_id: desc.layout, pass_context, + flags, } } diff --git a/wgpu-native/src/pipeline.rs b/wgpu-native/src/pipeline.rs index 94048113f..d09cd179d 100644 --- a/wgpu-native/src/pipeline.rs +++ b/wgpu-native/src/pipeline.rs @@ -60,6 +60,16 @@ impl BlendDescriptor { dst_factor: BlendFactor::Zero, operation: BlendOperation::Add, }; + + pub fn uses_color(&self) -> bool { + match (self.src_factor, self.dst_factor) { + (BlendFactor::BlendColor, _) | + (BlendFactor::OneMinusBlendColor, _) | + (_, BlendFactor::BlendColor) | + (_, BlendFactor::OneMinusBlendColor) => true, + (_, _) => false, + } + } } #[repr(C)] @@ -276,8 +286,16 @@ pub struct RenderPipelineDescriptor { pub sample_count: u32, } +bitflags! { + #[repr(transparent)] + pub struct PipelineFlags: u32 { + const BLEND_COLOR = 1; + } +} + pub struct RenderPipeline { pub(crate) raw: B::GraphicsPipeline, pub(crate) layout_id: PipelineLayoutId, pub(crate) pass_context: RenderPassContext, + pub(crate) flags: PipelineFlags, } diff --git a/wgpu-rs/src/lib.rs b/wgpu-rs/src/lib.rs index d58cd37f3..f16cfd88a 100644 --- a/wgpu-rs/src/lib.rs +++ b/wgpu-rs/src/lib.rs @@ -784,6 +784,10 @@ impl<'a> RenderPass<'a> { wgn::wgpu_render_pass_set_pipeline(self.id, pipeline.id); } + pub fn set_blend_color(&mut self, color: Color) { + wgn::wgpu_render_pass_set_blend_color(self.id, &color); + } + pub fn set_index_buffer(&mut self, buffer: &Buffer, offset: u32) { wgn::wgpu_render_pass_set_index_buffer(self.id, buffer.id, offset); }