Skip to content

Commit

Permalink
Merge #1224
Browse files Browse the repository at this point in the history
1224: Update the blend API to upstream r=kvark a=kvark

**Connections**
Matches gpuweb/gpuweb#1134

**Description**
Makes blending state ON/OFF explicit.

**Testing**
Simple enough!

Co-authored-by: Dzmitry Malyshau <kvarkus@gmail.com>
  • Loading branch information
bors[bot] and kvark committed Feb 19, 2021
2 parents 5afe832 + ff31911 commit 53bab9d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 35 deletions.
32 changes: 13 additions & 19 deletions wgpu-core/src/conv.rs
Expand Up @@ -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,
}
}

Expand All @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions wgpu-core/src/device/mod.rs
Expand Up @@ -2386,8 +2386,10 @@ impl<B: GfxBackend> Device<B> {

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() {
Expand Down
36 changes: 22 additions & 14 deletions wgpu-types/src/lib.rs
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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)]
Expand All @@ -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.
#[cfg_attr(any(feature = "trace", feature = "replay"), serde(default))]
pub alpha_blend: BlendState,
/// The color 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 color_blend: BlendState,
pub blend: Option<BlendState>,
/// Mask which enables/disables writes to different color/alpha channel.
#[cfg_attr(any(feature = "trace", feature = "replay"), serde(default))]
pub write_mask: ColorWrite,
Expand All @@ -758,8 +767,7 @@ impl From<TextureFormat> for ColorTargetState {
fn from(format: TextureFormat) -> Self {
Self {
format,
alpha_blend: BlendState::REPLACE,
color_blend: BlendState::REPLACE,
blend: None,
write_mask: ColorWrite::ALL,
}
}
Expand Down

0 comments on commit 53bab9d

Please sign in to comment.