From b7a0410e0930cba20b29332ad5762f2bfa2a5ce2 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 13 Apr 2021 22:19:44 -0400 Subject: [PATCH 1/2] Relax vertex attribute matching, also check interpolation --- wgpu-core/src/device/mod.rs | 12 +++----- wgpu-core/src/resource.rs | 4 ++- wgpu-core/src/validation.rs | 61 ++++++++++++++++++++++++++++++++----- 3 files changed, 61 insertions(+), 16 deletions(-) diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index d693409c5..215a53c77 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -27,8 +27,7 @@ use hal::{ use parking_lot::{Mutex, MutexGuard}; use thiserror::Error; use wgt::{ - BufferAddress, BufferSize, InputStepMode, TextureDimension, - TextureFormat, TextureViewDimension + BufferAddress, BufferSize, InputStepMode, TextureDimension, TextureFormat, TextureViewDimension, }; use std::{ @@ -2081,7 +2080,7 @@ impl Device { }); io.insert( attribute.shader_location, - validation::NumericType::from_vertex_format(attribute.format), + validation::InterfaceVar::vertex_attribute(attribute.format), ); } } @@ -2315,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 {:?}", @@ -4526,9 +4526,7 @@ impl Global { HostMap::Write => (wgt::BufferUsage::MAP_WRITE, resource::BufferUse::MAP_WRITE), }; - if range.start % wgt::MAP_ALIGNMENT != 0 - || range.end % wgt::COPY_BUFFER_ALIGNMENT != 0 - { + if range.start % wgt::MAP_ALIGNMENT != 0 || range.end % wgt::COPY_BUFFER_ALIGNMENT != 0 { return Err(resource::BufferAccessError::UnalignedRange); } diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index aa42169bc..6c71eea75 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -142,7 +142,9 @@ pub enum BufferAccessError { MissingBufferUsage(#[from] MissingBufferUsageError), #[error("buffer is not mapped")] NotMapped, - #[error("buffer map range must start aligned to `MAP_ALIGNMENT` and end to `COPY_BUFFER_ALIGNMENT`")] + #[error( + "buffer map range must start aligned to `MAP_ALIGNMENT` and end to `COPY_BUFFER_ALIGNMENT`" + )] UnalignedRange, #[error("buffer offset invalid: offset {offset} must be multiple of 8")] UnalignedOffset { offset: wgt::BufferAddress }, diff --git a/wgpu-core/src/validation.rs b/wgpu-core/src/validation.rs index e65d1eb09..4d52e7318 100644 --- a/wgpu-core/src/validation.rs +++ b/wgpu-core/src/validation.rs @@ -45,9 +45,26 @@ pub struct NumericType { width: naga::Bytes, } +#[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, + } + } +} + #[derive(Debug)] enum Varying { - Local { location: u32, ty: NumericType }, + Local { location: u32, iv: InterfaceVar }, BuiltIn(naga::BuiltIn), } @@ -461,7 +478,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 +642,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 +662,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 +704,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,13 +868,25 @@ 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 => { + iv.ty.is_subtype_of(&provided.ty) + && iv.interpolation == provided.interpolation + } + naga::ShaderStage::Compute => false, + }; + if compatible { Ok(()) } else { Err(InputError::WrongType) @@ -859,7 +904,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(); From 78652e93f429b51945d1d737af48ae293c1e7ee2 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 13 Apr 2021 22:48:54 -0400 Subject: [PATCH 2/2] Make input stage errors more detailed --- wgpu-core/src/validation.rs | 53 ++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/wgpu-core/src/validation.rs b/wgpu-core/src/validation.rs index 4d52e7318..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,6 +55,12 @@ 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, @@ -62,6 +78,12 @@ impl InterfaceVar { } } +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, iv: InterfaceVar }, @@ -166,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. @@ -175,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, }, } @@ -881,19 +908,27 @@ impl Interface { 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) - && iv.interpolation == provided.interpolation } 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(_) => {}