diff --git a/wgpu-core/src/conv.rs b/wgpu-core/src/conv.rs index 2d24df800..8ee4dc404 100644 --- a/wgpu-core/src/conv.rs +++ b/wgpu-core/src/conv.rs @@ -4,6 +4,7 @@ use crate::{ command::{LoadOp, PassChannel, StoreOp}, + pipeline::ColorStateError, resource, PrivateFeatures, }; @@ -196,16 +197,24 @@ pub fn map_primitive_topology(primitive_topology: wgt::PrimitiveTopology) -> hal } } -pub fn map_color_target_state(desc: &wgt::ColorTargetState) -> hal::pso::ColorBlendDesc { +pub fn map_color_target_state( + desc: &wgt::ColorTargetState, +) -> Result { let color_mask = desc.write_mask; - 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 { + let blend = desc + .blend + .as_ref() + .map(|bs| { + Ok(hal::pso::BlendState { + color: map_blend_component(&bs.color)?, + alpha: map_blend_component(&bs.alpha)?, + }) + }) + .transpose()?; + Ok(hal::pso::ColorBlendDesc { mask: map_color_write_flags(color_mask), blend, - } + }) } fn map_color_write_flags(flags: wgt::ColorWrite) -> hal::pso::ColorMask { @@ -228,25 +237,48 @@ fn map_color_write_flags(flags: wgt::ColorWrite) -> hal::pso::ColorMask { value } -fn map_blend_component(component: &wgt::BlendComponent) -> hal::pso::BlendOp { +fn map_blend_component( + component: &wgt::BlendComponent, +) -> Result { use hal::pso::BlendOp as H; use wgt::BlendOperation as Bo; - match component.operation { - Bo::Add => H::Add { - src: map_blend_factor(component.src_factor), - dst: map_blend_factor(component.dst_factor), + Ok(match *component { + wgt::BlendComponent { + operation: Bo::Add, + src_factor, + dst_factor, + } => H::Add { + src: map_blend_factor(src_factor), + dst: map_blend_factor(dst_factor), }, - Bo::Subtract => H::Sub { - src: map_blend_factor(component.src_factor), - dst: map_blend_factor(component.dst_factor), + wgt::BlendComponent { + operation: Bo::Subtract, + src_factor, + dst_factor, + } => H::Sub { + src: map_blend_factor(src_factor), + dst: map_blend_factor(dst_factor), }, - Bo::ReverseSubtract => H::RevSub { - src: map_blend_factor(component.src_factor), - dst: map_blend_factor(component.dst_factor), + wgt::BlendComponent { + operation: Bo::ReverseSubtract, + src_factor, + dst_factor, + } => H::RevSub { + src: map_blend_factor(src_factor), + dst: map_blend_factor(dst_factor), }, - Bo::Min => H::Min, - Bo::Max => H::Max, - } + wgt::BlendComponent { + operation: Bo::Min, + src_factor: wgt::BlendFactor::One, + dst_factor: wgt::BlendFactor::One, + } => H::Min, + wgt::BlendComponent { + operation: Bo::Max, + src_factor: wgt::BlendFactor::One, + dst_factor: wgt::BlendFactor::One, + } => H::Max, + _ => return Err(ColorStateError::InvalidMinMaxBlendFactors(*component)), + }) } fn map_blend_factor(blend_factor: wgt::BlendFactor) -> hal::pso::Factor { diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 5e1f943e9..409984ff6 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -2124,27 +2124,6 @@ impl Device { ); } - let input_assembler = conv::map_primitive_state_to_input_assembler(&desc.primitive); - - let blender = hal::pso::BlendDesc { - logic_op: None, // TODO - targets: color_states - .iter() - .map(conv::map_color_target_state) - .collect(), - }; - let depth_stencil = depth_stencil_state - .map(conv::map_depth_stencil_state) - .unwrap_or_default(); - - // TODO - let baked_states = hal::pso::BakedStates { - viewport: None, - scissor: None, - blend_constants: None, - depth_bounds: None, - }; - if desc.primitive.clamp_depth && !self.features.contains(wgt::Features::DEPTH_CLAMPING) { return Err(pipeline::CreateRenderPipelineError::MissingFeature( wgt::Features::DEPTH_CLAMPING, @@ -2174,6 +2153,29 @@ impl Device { ); } + let input_assembler = conv::map_primitive_state_to_input_assembler(&desc.primitive); + + let mut blender = hal::pso::BlendDesc { + logic_op: None, + targets: Vec::with_capacity(color_states.len()), + }; + for (i, cs) in color_states.iter().enumerate() { + let bt = conv::map_color_target_state(cs) + .map_err(|error| pipeline::CreateRenderPipelineError::ColorState(i as u8, error))?; + blender.targets.push(bt); + } + + let depth_stencil = depth_stencil_state + .map(conv::map_depth_stencil_state) + .unwrap_or_default(); + + let baked_states = hal::pso::BakedStates { + viewport: None, + scissor: None, + blend_constants: None, + depth_bounds: None, + }; + if desc.layout.is_none() { for _ in 0..self.limits.max_bind_groups { derived_group_layouts.push(binding_model::BindEntryMap::default()); @@ -2329,27 +2331,26 @@ impl Device { if validated_stages.contains(wgt::ShaderStage::FRAGMENT) { for (i, state) in color_states.iter().enumerate() { match io.get(&(i as wgt::ShaderLocation)) { - Some(ref output) - if validation::check_texture_format(state.format, &output.ty) => {} - Some(output) => { - log::warn!( - "Incompatible fragment output[{}] from shader: {:?}, expected {:?}", - i, - output, - state.format, - ); - return Err( - pipeline::CreateRenderPipelineError::IncompatibleOutputFormat { - index: i as u8, + Some(ref output) => { + validation::check_texture_format(state.format, &output.ty).map_err( + |pipeline| { + pipeline::CreateRenderPipelineError::ColorState( + i as u8, + pipeline::ColorStateError::IncompatibleFormat { + pipeline, + shader: output.ty, + }, + ) }, - ); + )?; } None if state.write_mask.is_empty() => {} None => { log::warn!("Missing fragment output[{}], expected {:?}", i, state,); - return Err(pipeline::CreateRenderPipelineError::MissingOutput { - index: i as u8, - }); + return Err(pipeline::CreateRenderPipelineError::ColorState( + i as u8, + pipeline::ColorStateError::Missing, + )); } } } diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index 3928ef5f8..ffab09b8c 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -196,6 +196,19 @@ pub struct RenderPipelineDescriptor<'a> { pub fragment: Option>, } +#[derive(Clone, Debug, Error)] +pub enum ColorStateError { + #[error("output is missing")] + Missing, + #[error("output format {pipeline} is incompatible with the shader {shader}")] + IncompatibleFormat { + pipeline: validation::NumericType, + shader: validation::NumericType, + }, + #[error("blend factors for {0:?} must be `One`")] + InvalidMinMaxBlendFactors(wgt::BlendComponent), +} + #[derive(Clone, Debug, Error)] pub enum CreateRenderPipelineError { #[error(transparent)] @@ -204,10 +217,8 @@ pub enum CreateRenderPipelineError { InvalidLayout, #[error("unable to derive an implicit layout")] Implicit(#[from] ImplicitLayoutError), - #[error("missing output at index {index}")] - MissingOutput { index: u8 }, - #[error("incompatible output format at index {index}")] - IncompatibleOutputFormat { index: u8 }, + #[error("color state [{0}] is invalid")] + ColorState(u8, #[source] ColorStateError), #[error("invalid sample count {0}")] InvalidSampleCount(u32), #[error("the number of vertex buffers {given} exceeds the limit {limit}")] diff --git a/wgpu-core/src/validation.rs b/wgpu-core/src/validation.rs index b030767ab..d36d555d8 100644 --- a/wgpu-core/src/validation.rs +++ b/wgpu-core/src/validation.rs @@ -702,8 +702,16 @@ impl NumericType { } /// Return true if the fragment `format` is covered by the provided `output`. -pub fn check_texture_format(format: wgt::TextureFormat, output: &NumericType) -> bool { - NumericType::from_texture_format(format).is_subtype_of(output) +pub fn check_texture_format( + format: wgt::TextureFormat, + output: &NumericType, +) -> Result<(), NumericType> { + let nt = NumericType::from_texture_format(format); + if nt.is_subtype_of(output) { + Ok(()) + } else { + Err(nt) + } } pub type StageIo = FastHashMap;