diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 8468db618..3a610381e 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -4238,6 +4238,7 @@ impl Device { vertex_stage = { let stage_desc = &vertex.stage; let stage = validation::ShaderStageForValidation::Vertex { + topology: desc.primitive.topology, compare_function: desc.depth_stencil.as_ref().map(|d| d.depth_compare), }; let stage_bit = stage.to_wgt_bit(); diff --git a/wgpu-core/src/validation.rs b/wgpu-core/src/validation.rs index 1b7e9c4c6..f1702fa89 100644 --- a/wgpu-core/src/validation.rs +++ b/wgpu-core/src/validation.rs @@ -7,6 +7,7 @@ use core::fmt; use arrayvec::ArrayVec; use hashbrown::hash_map::Entry; +use shader_io_deductions::{display_deductions_as_optional_list, MaxVertexShaderOutputDeduction}; use thiserror::Error; use wgt::{ error::{ErrorType, WebGpuError}, @@ -15,6 +16,8 @@ use wgt::{ use crate::{device::bgl, resource::InvalidResourceError, FastHashMap, FastHashSet}; +pub mod shader_io_deductions; + #[derive(Debug)] enum ResourceType { Buffer { @@ -328,6 +331,29 @@ pub enum StageError { MultipleEntryPointsFound, #[error(transparent)] InvalidResource(#[from] InvalidResourceError), + #[error( + "vertex shader output location Location[{location}] ({var}) exceeds the \ + `max_inter_stage_shader_variables` limit ({}, 0-based){}", + // NOTE: Remember: the limit is 0-based for indices. + limit - 1, + display_deductions_as_optional_list(deductions, |d| d.for_location()) + )] + VertexOutputLocationTooLarge { + location: u32, + var: InterfaceVar, + limit: u32, + deductions: Vec, + }, + #[error( + "found {num_found} user-defined vertex shader output variables, which exceeds the \ + `max_inter_stage_shader_variables` limit ({limit}){}", + display_deductions_as_optional_list(deductions, |d| d.for_variables()) + )] + TooManyUserDefinedVertexOutputs { + num_found: u32, + limit: u32, + deductions: Vec, + }, #[error( "Location[{location}] {var}'s index exceeds the `max_color_attachments` limit ({limit})" )] @@ -375,6 +401,8 @@ impl WebGpuError for StageError { | Self::MissingEntryPoint(..) | Self::NoEntryPointFound | Self::MultipleEntryPointsFound + | Self::VertexOutputLocationTooLarge { .. } + | Self::TooManyUserDefinedVertexOutputs { .. } | Self::ColorAttachmentLocationTooLarge { .. } | Self::TooManyMeshVertices { .. } | Self::TooManyMeshPrimitives { .. } @@ -1444,12 +1472,50 @@ impl Interface { } match shader_stage { - ShaderStageForValidation::Vertex { compare_function } => { + ShaderStageForValidation::Vertex { + topology, + compare_function, + } => { + let mut max_vertex_shader_output_variables = + self.limits.max_inter_stage_shader_variables; + let mut max_vertex_shader_output_location = max_vertex_shader_output_variables - 1; + + let point_list_deduction = if topology == wgt::PrimitiveTopology::PointList { + Some(MaxVertexShaderOutputDeduction::PointListPrimitiveTopology) + } else { + None + }; + + let deductions = point_list_deduction.into_iter(); + + for deduction in deductions.clone() { + // NOTE: Deductions, in the current version of the spec. we implement, do not + // ever exceed the minimum variables available. + max_vertex_shader_output_variables = max_vertex_shader_output_variables + .checked_sub(deduction.for_variables()) + .unwrap(); + max_vertex_shader_output_location = max_vertex_shader_output_location + .checked_sub(deduction.for_location()) + .unwrap(); + } + + let mut num_user_defined_outputs = 0; + for output in entry_point.outputs.iter() { - //TODO: count builtins towards the limit? - inter_stage_components += match *output { - Varying::Local { ref iv, .. } => iv.ty.dim.num_components(), - Varying::BuiltIn(_) => 0, + match *output { + Varying::Local { ref iv, location } => { + if location > max_vertex_shader_output_location { + return Err(StageError::VertexOutputLocationTooLarge { + location, + var: iv.clone(), + limit: self.limits.max_inter_stage_shader_variables, + deductions: deductions.collect(), + }); + } + num_user_defined_outputs += 1; + inter_stage_components += iv.ty.dim.num_components() + } + Varying::BuiltIn(_) => {} }; if let Some( @@ -1476,6 +1542,14 @@ impl Interface { } } } + + if num_user_defined_outputs > max_vertex_shader_output_variables { + return Err(StageError::TooManyUserDefinedVertexOutputs { + num_found: num_user_defined_outputs, + limit: self.limits.max_inter_stage_shader_variables, + deductions: deductions.collect(), + }); + } } ShaderStageForValidation::Fragment => { for output in &entry_point.outputs { @@ -1615,6 +1689,7 @@ pub fn validate_color_attachment_bytes_per_sample( pub enum ShaderStageForValidation { Vertex { + topology: wgt::PrimitiveTopology, compare_function: Option, }, Mesh, diff --git a/wgpu-core/src/validation/shader_io_deductions.rs b/wgpu-core/src/validation/shader_io_deductions.rs new file mode 100644 index 000000000..dc14d446a --- /dev/null +++ b/wgpu-core/src/validation/shader_io_deductions.rs @@ -0,0 +1,63 @@ +use core::fmt::{self, Debug, Display, Formatter}; + +#[cfg(doc)] +#[expect(unused_imports)] +use crate::validation::StageError; + +/// Max shader I/O variable deductions for vertex shader output. Used by +/// [`StageError::TooManyUserDefinedVertexOutputs`] and +/// [`StageError::VertexOutputLocationTooLarge`]. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum MaxVertexShaderOutputDeduction { + /// When a pipeline's [`crate::pipeline::RenderPipelineDescriptor::primitive`] is set to + /// [`wgt::PrimitiveTopology::PointList`]. + PointListPrimitiveTopology, +} + +impl MaxVertexShaderOutputDeduction { + pub fn for_variables(self) -> u32 { + match self { + Self::PointListPrimitiveTopology => 1, + } + } + + pub fn for_location(self) -> u32 { + match self { + Self::PointListPrimitiveTopology => 0, + } + } +} + +pub(in crate::validation) fn display_deductions_as_optional_list( + deductions: &[T], + accessor: fn(&T) -> u32, +) -> impl Display + '_ +where + T: Debug, +{ + struct DisplayFromFn(F); + + impl Display for DisplayFromFn + where + F: Fn(&mut Formatter<'_>) -> fmt::Result, + { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let Self(inner) = self; + inner(f) + } + } + + DisplayFromFn(move |f: &mut Formatter<'_>| { + let relevant_deductions = deductions + .iter() + .map(|deduction| (deduction, accessor(deduction))) + .filter(|(_, effective_deduction)| *effective_deduction > 0); + if relevant_deductions.clone().next().is_some() { + writeln!(f, "; note that some deductions apply during validation:")?; + for deduction in deductions { + writeln!(f, "\n- {deduction:?}: {}", accessor(deduction))?; + } + } + Ok(()) + }) +}