diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 638431ec2..1513b69e9 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -2080,7 +2080,7 @@ impl Device { }); io.insert( attribute.shader_location, - validation::NumericType::from_vertex_format(attribute.format), + validation::InterfaceVar::vertex_attribute(attribute.format), ); } } @@ -2314,7 +2314,8 @@ 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(output) if validation::check_texture_format(state.format, output) => {} + Some(ref output) + if validation::check_texture_format(state.format, &output.ty) => {} Some(output) => { log::warn!( "Incompatible fragment output[{}] from shader: {:?}, expected {:?}", diff --git a/wgpu-core/src/validation.rs b/wgpu-core/src/validation.rs index e65d1eb09..613be5aac 100644 --- a/wgpu-core/src/validation.rs +++ b/wgpu-core/src/validation.rs @@ -4,7 +4,7 @@ use crate::{binding_model::BindEntryMap, FastHashMap}; use naga::valid::GlobalUse; -use std::collections::hash_map::Entry; +use std::{collections::hash_map::Entry, fmt}; use thiserror::Error; use wgt::{BindGroupLayoutEntry, BindingType}; @@ -38,6 +38,16 @@ enum NumericDimension { Matrix(naga::VectorSize, naga::VectorSize), } +impl fmt::Display for NumericDimension { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Self::Scalar => write!(f, ""), + Self::Vector(size) => write!(f, "x{}", size as u8), + Self::Matrix(columns, rows) => write!(f, "x{}{}", columns as u8, rows as u8), + } + } +} + #[derive(Clone, Copy, Debug)] pub struct NumericType { dim: NumericDimension, @@ -45,9 +55,38 @@ pub struct NumericType { width: naga::Bytes, } +impl fmt::Display for NumericType { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:?}{}{}", self.kind, self.width * 8, self.dim) + } +} + +#[derive(Clone, Debug)] +pub struct InterfaceVar { + pub ty: NumericType, + interpolation: Option, + //TODO: https://github.com/gfx-rs/naga/pull/689 + //sampling: Option, +} + +impl InterfaceVar { + pub fn vertex_attribute(format: wgt::VertexFormat) -> Self { + InterfaceVar { + ty: NumericType::from_vertex_format(format), + interpolation: None, + } + } +} + +impl fmt::Display for InterfaceVar { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{} interpolated as {:?}", self.ty, self.interpolation) + } +} + #[derive(Debug)] enum Varying { - Local { location: u32, ty: NumericType }, + Local { location: u32, iv: InterfaceVar }, BuiltIn(naga::BuiltIn), } @@ -149,8 +188,10 @@ pub enum BindingError { pub enum InputError { #[error("input is not provided by the earlier stage in the pipeline")] Missing, - #[error("input type is not compatible with the provided")] - WrongType, + #[error("input type is not compatible with the provided {0}")] + WrongType(NumericType), + #[error("input interpolation doesn't match provided {0:?}")] + InterpolationMismatch(Option), } /// Errors produced when validating a programmable stage of a pipeline. @@ -158,19 +199,22 @@ pub enum InputError { pub enum StageError { #[error("shader module is invalid")] InvalidModule, - #[error("unable to find an entry point at {0:?} stage")] + #[error("unable to find entry point '{0:?}'")] MissingEntryPoint(String), - #[error("error matching global binding at index {binding} in group {group} against the pipeline layout: {error}")] + #[error("error matching global binding at index {binding} in group {group} against the pipeline layout")] Binding { group: u32, binding: u32, + #[source] error: BindingError, }, #[error( - "error matching the stage input at {location} against the previous stage outputs: {error}" + "error matching the stage input at {location} ({var}) against the previous stage outputs" )] Input { location: wgt::ShaderLocation, + var: InterfaceVar, + #[source] error: InputError, }, } @@ -461,7 +505,7 @@ impl Resource { } impl NumericType { - pub fn from_vertex_format(format: wgt::VertexFormat) -> Self { + fn from_vertex_format(format: wgt::VertexFormat) -> Self { use naga::{ScalarKind as Sk, VectorSize as Vs}; use wgt::VertexFormat as Vf; @@ -625,6 +669,19 @@ impl NumericType { _ => false, } } + + fn is_compatible_with(&self, other: &NumericType) -> bool { + if self.kind != other.kind { + return false; + } + match (self.dim, other.dim) { + (NumericDimension::Scalar, NumericDimension::Scalar) => true, + (NumericDimension::Scalar, NumericDimension::Vector(_)) => true, + (NumericDimension::Vector(_), NumericDimension::Vector(_)) => true, + (NumericDimension::Matrix(..), NumericDimension::Matrix(..)) => true, + _ => false, + } + } } /// Return true if the fragment `format` is covered by the provided `output`. @@ -632,7 +689,7 @@ pub fn check_texture_format(format: wgt::TextureFormat, output: &NumericType) -> NumericType::from_texture_format(format).is_subtype_of(output) } -pub type StageIo = FastHashMap; +pub type StageIo = FastHashMap; impl Interface { fn populate( @@ -674,9 +731,12 @@ impl Interface { }; let varying = match binding { - Some(&naga::Binding::Location(location, _)) => Varying::Local { + Some(&naga::Binding::Location(location, interpolation)) => Varying::Local { location, - ty: numeric_ty, + iv: InterfaceVar { + ty: numeric_ty, + interpolation, + }, }, Some(&naga::Binding::BuiltIn(built_in)) => Varying::BuiltIn(built_in), None => { @@ -835,20 +895,40 @@ impl Interface { for input in entry_point.inputs.iter() { match *input { - Varying::Local { location, ty } => { + Varying::Local { location, ref iv } => { let result = inputs .get(&location) .ok_or(InputError::Missing) .and_then(|provided| { - if ty.is_subtype_of(provided) { + let compatible = match shader_stage { + // For vertex attributes, there are defaults filled out + // by the driver if data is not provided. + naga::ShaderStage::Vertex => { + iv.ty.is_compatible_with(&provided.ty) + } + naga::ShaderStage::Fragment => { + if iv.interpolation != provided.interpolation { + return Err(InputError::InterpolationMismatch( + provided.interpolation, + )); + } + iv.ty.is_subtype_of(&provided.ty) + } + naga::ShaderStage::Compute => false, + }; + if compatible { Ok(()) } else { - Err(InputError::WrongType) + Err(InputError::WrongType(provided.ty)) } }); if let Err(error) = result { - return Err(StageError::Input { location, error }); + return Err(StageError::Input { + location, + var: iv.clone(), + error, + }); } } Varying::BuiltIn(_) => {} @@ -859,7 +939,7 @@ impl Interface { .outputs .iter() .filter_map(|output| match *output { - Varying::Local { location, ty } => Some((location, ty)), + Varying::Local { location, ref iv } => Some((location, iv.clone())), Varying::BuiltIn(_) => None, }) .collect();