From 3e0e3591b5b3130bf6d24e3e0e400e2bc3606b8d Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Thu, 18 Dec 2025 23:52:29 -0500 Subject: [PATCH] fix(core): cover `clip_distances` bindings in `max_inter_stage_shader_variables` limit check --- CHANGELOG.md | 4 ++ wgpu-core/src/validation.rs | 49 +++++++++++++++++-- .../src/validation/shader_io_deductions.rs | 14 ++++++ 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6daab1aa..47305a823 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,10 @@ Bottom level categories: - `Features::CLIP_DISTANCE`, `naga::Capabilities::CLIP_DISTANCE`, and `naga::BuiltIn::ClipDistance` have been renamed to `CLIP_DISTANCES` and `ClipDistances` (viz., pluralized) as appropriate, to match the WebGPU spec. By @ErichDonGubler in [#9267](https://github.com/gfx-rs/wgpu/pull/9267). +#### Validation + +- Add clip distances validation for `maxInterStageShaderVariables`. By @ErichDonGubler in [8762](https://github.com/gfx-rs/wgpu/pull/8762). This may break some existing programs, but it compiles with the WebGPU spec. + ### Bug Fixes #### General diff --git a/wgpu-core/src/validation.rs b/wgpu-core/src/validation.rs index 1566535ea..d32804d40 100644 --- a/wgpu-core/src/validation.rs +++ b/wgpu-core/src/validation.rs @@ -162,7 +162,7 @@ enum BuiltIn { ViewIndex, BaseInstance, BaseVertex, - ClipDistances, + ClipDistances { array_size: u32 }, CullDistance, InstanceIndex, PointSize, @@ -216,7 +216,7 @@ impl BuiltIn { Self::ViewIndex => naga::BuiltIn::ViewIndex, Self::BaseInstance => naga::BuiltIn::BaseInstance, Self::BaseVertex => naga::BuiltIn::BaseVertex, - Self::ClipDistances => naga::BuiltIn::ClipDistances, + Self::ClipDistances { .. } => naga::BuiltIn::ClipDistances, Self::CullDistance => naga::BuiltIn::CullDistance, Self::InstanceIndex => naga::BuiltIn::InstanceIndex, Self::PointSize => naga::BuiltIn::PointSize, @@ -1078,6 +1078,33 @@ impl Interface { } return; } + naga::TypeInner::Array { base, size, stride } + if matches!( + binding, + Some(naga::Binding::BuiltIn(naga::BuiltIn::ClipDistances)), + ) => + { + // NOTE: We should already have validated these in `naga`. + debug_assert_eq!( + &arena[base].inner, + &naga::TypeInner::Scalar(naga::Scalar::F32) + ); + debug_assert_eq!(stride, 4); + + let naga::ArraySize::Constant(array_size) = size else { + // NOTE: Based on the + // [spec](https://gpuweb.github.io/gpuweb/wgsl/#fixed-footprint-types): + // + // > The only valid use of a fixed-size array with an element count that is an + // > override-expression that is not a const-expression is as a memory view in + // > the workgroup address space. + unreachable!("non-constant array size for `clip_distances`") + }; + let array_size = array_size.get(); + + list.push(Varying::BuiltIn(BuiltIn::ClipDistances { array_size })); + return; + } ref other => { //Note: technically this should be at least `log::error`, but // the reality is - every shader coming from `glslc` outputs an array @@ -1110,7 +1137,7 @@ impl Interface { naga::BuiltIn::ViewIndex => BuiltIn::ViewIndex, naga::BuiltIn::BaseInstance => BuiltIn::BaseInstance, naga::BuiltIn::BaseVertex => BuiltIn::BaseVertex, - naga::BuiltIn::ClipDistances => BuiltIn::ClipDistances, + naga::BuiltIn::ClipDistances => unreachable!(), naga::BuiltIn::CullDistance => BuiltIn::CullDistance, naga::BuiltIn::InstanceIndex => BuiltIn::InstanceIndex, naga::BuiltIn::PointSize => BuiltIn::PointSize, @@ -1598,7 +1625,21 @@ impl Interface { None }; - let deductions = point_list_deduction.into_iter(); + let clip_distance_deductions = entry_point.outputs.iter().filter_map(|output| { + if let &Varying::BuiltIn(BuiltIn::ClipDistances { array_size }) = output { + Some(MaxVertexShaderOutputDeduction::ClipDistances { array_size }) + } else { + None + } + }); + debug_assert!( + clip_distance_deductions.clone().count() <= 1, + "multiple `clip_distances` outputs found" + ); + + let deductions = point_list_deduction + .into_iter() + .chain(clip_distance_deductions); for deduction in deductions.clone() { // NOTE: Deductions, in the current version of the spec. we implement, do not diff --git a/wgpu-core/src/validation/shader_io_deductions.rs b/wgpu-core/src/validation/shader_io_deductions.rs index 70652f293..140ba42bc 100644 --- a/wgpu-core/src/validation/shader_io_deductions.rs +++ b/wgpu-core/src/validation/shader_io_deductions.rs @@ -12,18 +12,32 @@ pub enum MaxVertexShaderOutputDeduction { /// When a pipeline's [`crate::pipeline::RenderPipelineDescriptor::primitive`] is set to /// [`wgt::PrimitiveTopology::PointList`]. PointListPrimitiveTopology, + /// When a clip distances array primitive is used in an output. + ClipDistances { array_size: u32 }, +} + +impl MaxVertexShaderOutputDeduction { + fn variables_from_clip_distance_slot(num_slots: u32) -> u32 { + num_slots.div_ceil(4) + } } impl MaxVertexShaderOutputDeduction { pub fn for_variables(self) -> u32 { match self { Self::PointListPrimitiveTopology => 1, + Self::ClipDistances { array_size } => { + Self::variables_from_clip_distance_slot(array_size) + } } } pub fn for_location(self) -> u32 { match self { Self::PointListPrimitiveTopology => 0, + Self::ClipDistances { array_size } => { + Self::variables_from_clip_distance_slot(array_size) + } } } }