From 344eeb4bc87ae7fe21aeae2a9ae72864639ba83f Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Fri, 12 Dec 2025 11:58:09 -0500 Subject: [PATCH] refactor(core): migrate from `naga::ShaderStage` to new `ShaderStageForValidation` In order to prepare for spec.-compliant validation of shader I/O, we need to create a new `enum` that will have the same set of _variants_ as `naga::ShaderStage`, but will carry additional information relevant to validation. As a preparatory step, create the new `enum` (called `ShaderStageForValidation`), and use it as the source of truth for details of the stage being validated. This yields several immediate benefits, even before making adjustments for the new validation: - Stage-specific stage is now properly encapsulated. `compare_function`, which was previously passed as a `Option`al sibling argument, is now properly associated with only the vertex shader stage. - This led to an odd discovery that `compare_function` was being provided to `check_stages` with other stages, but not actually actually checked. This seems like a rightfully squashed bug. - Instead of passing a `wgpu_types::ShaderStages` (with a single bit) and `naga::ShaderStage` side-by-side, encapsulate both into conversion methods for an instance of `ShaderStageForValidation`, so errors are impossible to make. --- wgpu-core/src/device/resource.rs | 37 +++++++++---------- wgpu-core/src/validation.rs | 63 +++++++++++++++++++++----------- 2 files changed, 59 insertions(+), 41 deletions(-) diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index a530e9863..8468db618 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -52,7 +52,7 @@ use crate::{ snatch::{SnatchGuard, SnatchLock, Snatchable}, timestamp_normalization::TIMESTAMP_NORMALIZATION_BUFFER_USES, track::{BindGroupStates, DeviceTracker, TrackerIndexAllocators, UsageScope, UsageScopePool}, - validation::{self, stage_bit_from_shader_stage}, + validation, weak_vec::WeakVec, FastHashMap, LabelHelpers, OnceCellOrLock, }; @@ -3743,10 +3743,10 @@ impl Device { let final_entry_point_name; { - let stage = naga::ShaderStage::Compute; + let stage = validation::ShaderStageForValidation::Compute; final_entry_point_name = shader_module.finalize_entry_point_name( - stage, + stage.to_naga(), desc.stage.entry_point.as_ref().map(|ep| ep.as_ref()), )?; @@ -3757,7 +3757,6 @@ impl Device { &final_entry_point_name, stage, io, - None, )?; } } @@ -4238,8 +4237,10 @@ impl Device { pipeline::RenderPipelineVertexProcessor::Vertex(ref vertex) => { vertex_stage = { let stage_desc = &vertex.stage; - let stage = naga::ShaderStage::Vertex; - let stage_bit = stage_bit_from_shader_stage(stage); + let stage = validation::ShaderStageForValidation::Vertex { + compare_function: desc.depth_stencil.as_ref().map(|d| d.depth_compare), + }; + let stage_bit = stage.to_wgt_bit(); let vertex_shader_module = &stage_desc.module; vertex_shader_module.same_device(self)?; @@ -4251,7 +4252,7 @@ impl Device { _vertex_entry_point_name = vertex_shader_module .finalize_entry_point_name( - stage, + stage.to_naga(), stage_desc.entry_point.as_ref().map(|ep| ep.as_ref()), ) .map_err(stage_err)?; @@ -4264,7 +4265,6 @@ impl Device { &_vertex_entry_point_name, stage, io, - desc.depth_stencil.as_ref().map(|d| d.depth_compare), ) .map_err(stage_err)?; validated_stages |= stage_bit; @@ -4283,8 +4283,8 @@ impl Device { task_stage = if let Some(task) = task { let stage_desc = &task.stage; - let stage = naga::ShaderStage::Task; - let stage_bit = stage_bit_from_shader_stage(stage); + let stage = validation::ShaderStageForValidation::Task; + let stage_bit = stage.to_wgt_bit(); let task_shader_module = &stage_desc.module; task_shader_module.same_device(self)?; @@ -4295,7 +4295,7 @@ impl Device { _task_entry_point_name = task_shader_module .finalize_entry_point_name( - stage, + stage.to_naga(), stage_desc.entry_point.as_ref().map(|ep| ep.as_ref()), ) .map_err(stage_err)?; @@ -4308,7 +4308,6 @@ impl Device { &_task_entry_point_name, stage, io, - desc.depth_stencil.as_ref().map(|d| d.depth_compare), ) .map_err(stage_err)?; validated_stages |= stage_bit; @@ -4325,8 +4324,8 @@ impl Device { }; mesh_stage = { let stage_desc = &mesh.stage; - let stage = naga::ShaderStage::Mesh; - let stage_bit = stage_bit_from_shader_stage(stage); + let stage = validation::ShaderStageForValidation::Mesh; + let stage_bit = stage.to_wgt_bit(); let mesh_shader_module = &stage_desc.module; mesh_shader_module.same_device(self)?; @@ -4337,7 +4336,7 @@ impl Device { _mesh_entry_point_name = mesh_shader_module .finalize_entry_point_name( - stage, + stage.to_naga(), stage_desc.entry_point.as_ref().map(|ep| ep.as_ref()), ) .map_err(stage_err)?; @@ -4350,7 +4349,6 @@ impl Device { &_mesh_entry_point_name, stage, io, - desc.depth_stencil.as_ref().map(|d| d.depth_compare), ) .map_err(stage_err)?; validated_stages |= stage_bit; @@ -4369,8 +4367,8 @@ impl Device { let fragment_entry_point_name; let fragment_stage = match desc.fragment { Some(ref fragment_state) => { - let stage = naga::ShaderStage::Fragment; - let stage_bit = stage_bit_from_shader_stage(stage); + let stage = validation::ShaderStageForValidation::Fragment; + let stage_bit = stage.to_wgt_bit(); let shader_module = &fragment_state.stage.module; shader_module.same_device(self)?; @@ -4382,7 +4380,7 @@ impl Device { fragment_entry_point_name = shader_module .finalize_entry_point_name( - stage, + stage.to_naga(), fragment_state .stage .entry_point @@ -4399,7 +4397,6 @@ impl Device { &fragment_entry_point_name, stage, io, - desc.depth_stencil.as_ref().map(|d| d.depth_compare), ) .map_err(stage_err)?; validated_stages |= stage_bit; diff --git a/wgpu-core/src/validation.rs b/wgpu-core/src/validation.rs index 62b8bc1b1..1b7e9c4c6 100644 --- a/wgpu-core/src/validation.rs +++ b/wgpu-core/src/validation.rs @@ -1172,20 +1172,19 @@ impl Interface { layouts: &mut BindingLayoutSource<'_>, shader_binding_sizes: &mut FastHashMap, entry_point_name: &str, - shader_stage: naga::ShaderStage, + shader_stage: ShaderStageForValidation, inputs: StageIo, - compare_function: Option, ) -> Result { // Since a shader module can have multiple entry points with the same name, // we need to look for one with the right execution model. - let pair = (shader_stage, entry_point_name.to_string()); + let pair = (shader_stage.to_naga(), entry_point_name.to_string()); let entry_point = match self.entry_points.get(&pair) { Some(some) => some, None => return Err(StageError::MissingEntryPoint(pair.1)), }; let (_, entry_point_name) = pair; - let stage_bit = stage_bit_from_shader_stage(shader_stage); + let stage_bit = shader_stage.to_wgt_bit(); // check resources visibility for &handle in entry_point.resources.iter() { @@ -1308,13 +1307,13 @@ impl Interface { } // check workgroup size limits - if shader_stage.compute_like() { + if shader_stage.to_naga().compute_like() { let ( max_workgroup_size_limits, max_workgroup_size_total, per_dimension_limit, total_limit, - ) = match shader_stage { + ) = match shader_stage.to_naga() { naga::ShaderStage::Compute => ( [ self.limits.max_compute_workgroup_size_x, @@ -1380,7 +1379,7 @@ impl Interface { .ok_or(InputError::Missing) .and_then(|provided| { let (compatible, num_components, per_primitive_correct) = - match shader_stage { + match shader_stage.to_naga() { // For vertex attributes, there are defaults filled out // by the driver if data is not provided. naga::ShaderStage::Vertex => { @@ -1445,7 +1444,7 @@ impl Interface { } match shader_stage { - naga::ShaderStage::Vertex => { + ShaderStageForValidation::Vertex { compare_function } => { for output in entry_point.outputs.iter() { //TODO: count builtins towards the limit? inter_stage_components += match *output { @@ -1478,7 +1477,7 @@ impl Interface { } } } - naga::ShaderStage::Fragment => { + ShaderStageForValidation::Fragment => { for output in &entry_point.outputs { let &Varying::Local { location, ref iv } = output else { continue; @@ -1524,7 +1523,7 @@ impl Interface { }); } } - if shader_stage == naga::ShaderStage::Mesh + if shader_stage.to_naga() == naga::ShaderStage::Mesh && entry_point.task_payload_size != inputs.task_payload_size { return Err(StageError::TaskPayloadMustMatch { @@ -1534,18 +1533,18 @@ impl Interface { } // Fragment shader primitive index is treated like a varying - if shader_stage == naga::ShaderStage::Fragment + if shader_stage.to_naga() == naga::ShaderStage::Fragment && this_stage_primitive_index && inputs.primitive_index == Some(false) { return Err(StageError::InvalidPrimitiveIndex); - } else if shader_stage == naga::ShaderStage::Fragment + } else if shader_stage.to_naga() == naga::ShaderStage::Fragment && !this_stage_primitive_index && inputs.primitive_index == Some(true) { return Err(StageError::MissingPrimitiveIndex); } - if shader_stage == naga::ShaderStage::Mesh + if shader_stage.to_naga() == naga::ShaderStage::Mesh && inputs.task_payload_size.is_some() && has_draw_id { @@ -1564,7 +1563,7 @@ impl Interface { Ok(StageIo { task_payload_size: entry_point.task_payload_size, varyings: outputs, - primitive_index: if shader_stage == naga::ShaderStage::Mesh { + primitive_index: if shader_stage.to_naga() == naga::ShaderStage::Mesh { Some(this_stage_primitive_index) } else { None @@ -1614,12 +1613,34 @@ pub fn validate_color_attachment_bytes_per_sample( Ok(()) } -pub(crate) fn stage_bit_from_shader_stage(shader_stage: naga::ShaderStage) -> wgt::ShaderStages { - match shader_stage { - naga::ShaderStage::Vertex => wgt::ShaderStages::VERTEX, - naga::ShaderStage::Fragment => wgt::ShaderStages::FRAGMENT, - naga::ShaderStage::Compute => wgt::ShaderStages::COMPUTE, - naga::ShaderStage::Mesh => wgt::ShaderStages::MESH, - naga::ShaderStage::Task => wgt::ShaderStages::TASK, +pub enum ShaderStageForValidation { + Vertex { + compare_function: Option, + }, + Mesh, + Fragment, + Compute, + Task, +} + +impl ShaderStageForValidation { + pub fn to_naga(&self) -> naga::ShaderStage { + match self { + Self::Vertex { .. } => naga::ShaderStage::Vertex, + Self::Mesh => naga::ShaderStage::Mesh, + Self::Fragment => naga::ShaderStage::Fragment, + Self::Compute => naga::ShaderStage::Compute, + Self::Task => naga::ShaderStage::Task, + } + } + + pub fn to_wgt_bit(&self) -> wgt::ShaderStages { + match self { + Self::Vertex { .. } => wgt::ShaderStages::VERTEX, + Self::Mesh { .. } => wgt::ShaderStages::MESH, + Self::Fragment { .. } => wgt::ShaderStages::FRAGMENT, + Self::Compute => wgt::ShaderStages::COMPUTE, + Self::Task => wgt::ShaderStages::TASK, + } } }