From ff319117608d4ae07e3c23d8592199fd054b4bab Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Fri, 19 Feb 2021 12:07:47 -0500 Subject: [PATCH] Update the blend API to upstream --- wgpu-core/src/conv.rs | 32 +++++++++++++------------------- wgpu-core/src/device/mod.rs | 6 ++++-- wgpu-types/src/lib.rs | 36 ++++++++++++++++++++++-------------- 3 files changed, 39 insertions(+), 35 deletions(-) diff --git a/wgpu-core/src/conv.rs b/wgpu-core/src/conv.rs index 3ffbd5de70..49ef67d260 100644 --- a/wgpu-core/src/conv.rs +++ b/wgpu-core/src/conv.rs @@ -179,19 +179,13 @@ pub fn map_primitive_topology(primitive_topology: wgt::PrimitiveTopology) -> hal pub fn map_color_target_state(desc: &wgt::ColorTargetState) -> hal::pso::ColorBlendDesc { let color_mask = desc.write_mask; - let blend_state = if desc.color_blend != wgt::BlendState::REPLACE - || desc.alpha_blend != wgt::BlendState::REPLACE - { - Some(hal::pso::BlendState { - color: map_blend_state(&desc.color_blend), - alpha: map_blend_state(&desc.alpha_blend), - }) - } else { - None - }; + let blend = desc.blend.as_ref().map(|bs| hal::pso::BlendState { + color: map_blend_component(&bs.color), + alpha: map_blend_component(&bs.alpha), + }); hal::pso::ColorBlendDesc { mask: map_color_write_flags(color_mask), - blend: blend_state, + blend, } } @@ -215,21 +209,21 @@ fn map_color_write_flags(flags: wgt::ColorWrite) -> hal::pso::ColorMask { value } -fn map_blend_state(blend_desc: &wgt::BlendState) -> hal::pso::BlendOp { +fn map_blend_component(component: &wgt::BlendComponent) -> hal::pso::BlendOp { use hal::pso::BlendOp as H; use wgt::BlendOperation as Bo; - match blend_desc.operation { + match component.operation { Bo::Add => H::Add { - src: map_blend_factor(blend_desc.src_factor), - dst: map_blend_factor(blend_desc.dst_factor), + src: map_blend_factor(component.src_factor), + dst: map_blend_factor(component.dst_factor), }, Bo::Subtract => H::Sub { - src: map_blend_factor(blend_desc.src_factor), - dst: map_blend_factor(blend_desc.dst_factor), + src: map_blend_factor(component.src_factor), + dst: map_blend_factor(component.dst_factor), }, Bo::ReverseSubtract => H::RevSub { - src: map_blend_factor(blend_desc.src_factor), - dst: map_blend_factor(blend_desc.dst_factor), + src: map_blend_factor(component.src_factor), + dst: map_blend_factor(component.dst_factor), }, Bo::Min => H::Min, Bo::Max => H::Max, diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index cb8aefe20a..5bc3269b53 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -2386,8 +2386,10 @@ impl Device { let mut flags = pipeline::PipelineFlags::empty(); for state in color_states.iter() { - if state.color_blend.uses_color() | state.alpha_blend.uses_color() { - flags |= pipeline::PipelineFlags::BLEND_COLOR; + if let Some(ref bs) = state.blend { + if bs.color.uses_color() | bs.alpha.uses_color() { + flags |= pipeline::PipelineFlags::BLEND_COLOR; + } } } if let Some(ds) = depth_stencil_state.as_ref() { diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index 4cf5db4dbc..84061706eb 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -690,14 +690,12 @@ impl Default for BlendOperation { } } -/// Describes the blend state of a pipeline. -/// -/// Alpha blending is very complicated: see the OpenGL or Vulkan spec for more information. +/// Describes the blend component of a pipeline. #[repr(C)] #[derive(Clone, Debug, PartialEq, Eq, Hash)] #[cfg_attr(feature = "trace", derive(Serialize))] #[cfg_attr(feature = "replay", derive(Deserialize))] -pub struct BlendState { +pub struct BlendComponent { /// Multiplier for the source, which is produced by the fragment shader. pub src_factor: BlendFactor, /// Multiplier for the destination, which is stored in the target. @@ -707,9 +705,9 @@ pub struct BlendState { pub operation: BlendOperation, } -impl BlendState { +impl BlendComponent { /// Default blending state that replaces destination with the source. - pub const REPLACE: Self = BlendState { + pub const REPLACE: Self = BlendComponent { src_factor: BlendFactor::One, dst_factor: BlendFactor::Zero, operation: BlendOperation::Add, @@ -728,12 +726,26 @@ impl BlendState { } } -impl Default for BlendState { +impl Default for BlendComponent { fn default() -> Self { Self::REPLACE } } +/// Describe the blend state of a render pipeline. +/// +/// See the OpenGL or Vulkan spec for more information. +#[repr(C)] +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "trace", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct BlendState { + /// Color equation. + pub color: BlendComponent, + /// Alpha equation. + pub alpha: BlendComponent, +} + /// Describes the color state of a render pipeline. #[repr(C)] #[derive(Clone, Debug, PartialEq, Eq, Hash)] @@ -743,12 +755,9 @@ pub struct ColorTargetState { /// The [`TextureFormat`] of the image that this pipeline will render to. Must match the the format /// of the corresponding color attachment in [`CommandEncoder::begin_render_pass`]. pub format: TextureFormat, - /// The alpha blending that is used for this pipeline. + /// The blending that is used for this pipeline. #[cfg_attr(any(feature = "trace", feature = "replay"), serde(default))] - pub alpha_blend: BlendState, - /// The color blending that is used for this pipeline. - #[cfg_attr(any(feature = "trace", feature = "replay"), serde(default))] - pub color_blend: BlendState, + pub blend: Option, /// Mask which enables/disables writes to different color/alpha channel. #[cfg_attr(any(feature = "trace", feature = "replay"), serde(default))] pub write_mask: ColorWrite, @@ -758,8 +767,7 @@ impl From for ColorTargetState { fn from(format: TextureFormat) -> Self { Self { format, - alpha_blend: BlendState::REPLACE, - color_blend: BlendState::REPLACE, + blend: None, write_mask: ColorWrite::ALL, } }