From b7a0410e0930cba20b29332ad5762f2bfa2a5ce2 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 13 Apr 2021 22:19:44 -0400 Subject: [PATCH] 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();