fix(core): add vertex-stage max_inter_stage_shader_variables validation (minus clip-distances)

Notable public API additions:

- New `StageError` variants:
	- `VertexOutputLocationTooLarge`
	- `TooManyVertexOutputs`
- Add new `shader_io_deductions` submodule of `wgpu_core::validation` with:
	- A `ShaderIoDeduction` trait
	- `enum MaxVertexShaderOutputDeduction`
	- `MaxInterStageShaderVariablesDeductions` for them.
This commit is contained in:
Erich Gubler committed 2025-12-23 16:09:17 -05:00
1 parent 0fbfbf8aa4
commit bfb153222b
3 files changed
+144 -5

No files matched your search

+1
View File
@@ -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();
+80 -5
View File
@@ -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<MaxVertexShaderOutputDeduction>,
},
#[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<MaxVertexShaderOutputDeduction>,
},
#[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<wgt::CompareFunction>,
},
Mesh,
@@ -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<T>(
deductions: &[T],
accessor: fn(&T) -> u32,
) -> impl Display + '_
where
T: Debug,
{
struct DisplayFromFn<F>(F);
impl<F> Display for DisplayFromFn<F>
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(())
})
}