From f88117e3fd643ca4453b300b8a4173d0ac469958 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Fri, 27 Aug 2021 20:42:11 -0700 Subject: [PATCH] [spv-out] Request proper capabilities for storage image formats. --- src/back/spv/instructions.rs | 75 ++++++++------- src/back/spv/writer.rs | 116 ++++++++++++++++------ tests/out/spv/image.spvasm | 1 + tests/spirv-capabilities.rs | 180 +++++++++++++++++++++++++++++++++++ 4 files changed, 311 insertions(+), 61 deletions(-) create mode 100644 tests/spirv-capabilities.rs diff --git a/src/back/spv/instructions.rs b/src/back/spv/instructions.rs index 6530160e5..47a467d0c 100644 --- a/src/back/spv/instructions.rs +++ b/src/back/spv/instructions.rs @@ -242,40 +242,7 @@ impl super::Instruction { instruction.add_operand(if sampled { 1 } else { 2 }); let format = match image_class { - crate::ImageClass::Storage { format, .. } => match format { - crate::StorageFormat::R8Unorm => spirv::ImageFormat::R8, - crate::StorageFormat::R8Snorm => spirv::ImageFormat::R8Snorm, - crate::StorageFormat::R8Uint => spirv::ImageFormat::R8ui, - crate::StorageFormat::R8Sint => spirv::ImageFormat::R8i, - crate::StorageFormat::R16Uint => spirv::ImageFormat::R16ui, - crate::StorageFormat::R16Sint => spirv::ImageFormat::R16i, - crate::StorageFormat::R16Float => spirv::ImageFormat::R16f, - crate::StorageFormat::Rg8Unorm => spirv::ImageFormat::Rg8, - crate::StorageFormat::Rg8Snorm => spirv::ImageFormat::Rg8Snorm, - crate::StorageFormat::Rg8Uint => spirv::ImageFormat::Rg8ui, - crate::StorageFormat::Rg8Sint => spirv::ImageFormat::Rg8i, - crate::StorageFormat::R32Uint => spirv::ImageFormat::R32ui, - crate::StorageFormat::R32Sint => spirv::ImageFormat::R32i, - crate::StorageFormat::R32Float => spirv::ImageFormat::R32f, - crate::StorageFormat::Rg16Uint => spirv::ImageFormat::Rg16ui, - crate::StorageFormat::Rg16Sint => spirv::ImageFormat::Rg16i, - crate::StorageFormat::Rg16Float => spirv::ImageFormat::Rg16f, - crate::StorageFormat::Rgba8Unorm => spirv::ImageFormat::Rgba8, - crate::StorageFormat::Rgba8Snorm => spirv::ImageFormat::Rgba8Snorm, - crate::StorageFormat::Rgba8Uint => spirv::ImageFormat::Rgba8ui, - crate::StorageFormat::Rgba8Sint => spirv::ImageFormat::Rgba8i, - crate::StorageFormat::Rgb10a2Unorm => spirv::ImageFormat::Rgb10a2ui, - crate::StorageFormat::Rg11b10Float => spirv::ImageFormat::R11fG11fB10f, - crate::StorageFormat::Rg32Uint => spirv::ImageFormat::Rg32ui, - crate::StorageFormat::Rg32Sint => spirv::ImageFormat::Rg32i, - crate::StorageFormat::Rg32Float => spirv::ImageFormat::Rg32f, - crate::StorageFormat::Rgba16Uint => spirv::ImageFormat::Rgba16ui, - crate::StorageFormat::Rgba16Sint => spirv::ImageFormat::Rgba16i, - crate::StorageFormat::Rgba16Float => spirv::ImageFormat::Rgba16f, - crate::StorageFormat::Rgba32Uint => spirv::ImageFormat::Rgba32ui, - crate::StorageFormat::Rgba32Sint => spirv::ImageFormat::Rgba32i, - crate::StorageFormat::Rgba32Float => spirv::ImageFormat::Rgba32f, - }, + crate::ImageClass::Storage { format, .. } => format.into(), _ => spirv::ImageFormat::Unknown, }; @@ -932,3 +899,43 @@ impl super::Instruction { instruction } } + +impl From for spirv::ImageFormat { + fn from(format: crate::StorageFormat) -> Self { + use crate::StorageFormat as Sf; + match format { + Sf::R8Unorm => Self::R8, + Sf::R8Snorm => Self::R8Snorm, + Sf::R8Uint => Self::R8ui, + Sf::R8Sint => Self::R8i, + Sf::R16Uint => Self::R16ui, + Sf::R16Sint => Self::R16i, + Sf::R16Float => Self::R16f, + Sf::Rg8Unorm => Self::Rg8, + Sf::Rg8Snorm => Self::Rg8Snorm, + Sf::Rg8Uint => Self::Rg8ui, + Sf::Rg8Sint => Self::Rg8i, + Sf::R32Uint => Self::R32ui, + Sf::R32Sint => Self::R32i, + Sf::R32Float => Self::R32f, + Sf::Rg16Uint => Self::Rg16ui, + Sf::Rg16Sint => Self::Rg16i, + Sf::Rg16Float => Self::Rg16f, + Sf::Rgba8Unorm => Self::Rgba8, + Sf::Rgba8Snorm => Self::Rgba8Snorm, + Sf::Rgba8Uint => Self::Rgba8ui, + Sf::Rgba8Sint => Self::Rgba8i, + Sf::Rgb10a2Unorm => Self::Rgb10a2ui, + Sf::Rg11b10Float => Self::R11fG11fB10f, + Sf::Rg32Uint => Self::Rg32ui, + Sf::Rg32Sint => Self::Rg32i, + Sf::Rg32Float => Self::Rg32f, + Sf::Rgba16Uint => Self::Rgba16ui, + Sf::Rgba16Sint => Self::Rgba16i, + Sf::Rgba16Float => Self::Rgba16f, + Sf::Rgba32Uint => Self::Rgba32ui, + Sf::Rgba32Sint => Self::Rgba32i, + Sf::Rgba32Float => Self::Rgba32f, + } + } +} diff --git a/src/back/spv/writer.rs b/src/back/spv/writer.rs index 43cc22823..5fe07e388 100644 --- a/src/back/spv/writer.rs +++ b/src/back/spv/writer.rs @@ -717,34 +717,12 @@ impl Writer { arrayed, class, } => { - let kind = match class { - crate::ImageClass::Sampled { kind, multi: _ } => { - if dim == crate::ImageDimension::D1 { - self.require_any("1D sampled images", &[spirv::Capability::Sampled1D])?; - } - kind - } - crate::ImageClass::Depth { multi: _ } => crate::ScalarKind::Float, + let (kind, sampled) = match class { + crate::ImageClass::Sampled { kind, multi: _ } => (kind, true), + crate::ImageClass::Depth { multi: _ } => (crate::ScalarKind::Float, true), crate::ImageClass::Storage { format, .. } => { - match dim { - crate::ImageDimension::D1 => { - self.require_any( - "1D storage images", - &[spirv::Capability::Image1D], - )?; - } - crate::ImageDimension::Cube => { - if arrayed { - self.require_any( - "arrayed cube images", - &[spirv::Capability::ImageCubeArray], - )?; - } - } - _ => {} - }; - - format.into() + self.request_image_format_capabilities(spirv::ImageFormat::from(format))?; + (crate::ScalarKind::from(format), false) } }; let local_type = LocalType::Value { @@ -754,6 +732,29 @@ impl Writer { pointer_class: None, }; let dim = map_dim(dim); + match dim { + spirv::Dim::Dim1D => { + if sampled { + self.require_any("sampled 1D images", &[spirv::Capability::Sampled1D])?; + } else { + self.require_any("1D storage images", &[spirv::Capability::Image1D])?; + } + } + spirv::Dim::DimCube if arrayed => { + if sampled { + self.require_any( + "sampled cube array images", + &[spirv::Capability::SampledCubeArray], + )?; + } else { + self.require_any( + "cube array storage images", + &[spirv::Capability::ImageCubeArray], + )?; + } + } + _ => {} + } let type_id = self.get_type_id(LookupType::Local(local_type)); Instruction::type_image(id, type_id, dim, arrayed, class) } @@ -860,6 +861,62 @@ impl Writer { Ok(id) } + fn request_image_format_capabilities( + &mut self, + format: spirv::ImageFormat, + ) -> Result<(), Error> { + use spirv::ImageFormat as If; + match format { + If::Rg32f + | If::Rg16f + | If::R11fG11fB10f + | If::R16f + | If::Rgba16 + | If::Rgb10A2 + | If::Rg16 + | If::Rg8 + | If::R16 + | If::R8 + | If::Rgba16Snorm + | If::Rg16Snorm + | If::Rg8Snorm + | If::R16Snorm + | If::R8Snorm + | If::Rg32i + | If::Rg16i + | If::Rg8i + | If::R16i + | If::R8i + | If::Rgb10a2ui + | If::Rg32ui + | If::Rg16ui + | If::Rg8ui + | If::R16ui + | If::R8ui => self.require_any( + "storage image format", + &[spirv::Capability::StorageImageExtendedFormats], + ), + If::R64ui | If::R64i => self.require_any( + "64-bit integer storage image format", + &[spirv::Capability::Int64ImageEXT], + ), + If::Unknown + | If::Rgba32f + | If::Rgba16f + | If::R32f + | If::Rgba8 + | If::Rgba8Snorm + | If::Rgba32i + | If::Rgba16i + | If::Rgba8i + | If::R32i + | If::Rgba32ui + | If::Rgba16ui + | If::Rgba8ui + | If::R32ui => Ok(()), + } + } + pub(super) fn get_index_constant(&mut self, index: Word) -> Result { self.get_constant_scalar(crate::ScalarValue::Uint(index as _), 4) } @@ -1312,6 +1369,11 @@ impl Writer { self.logical_layout.in_words(words); Ok(()) } + + /// Return the set of capabilities the last module written used. + pub fn get_capabilities_used(&self) -> &crate::FastHashSet { + &self.capabilities_used + } } #[test] diff --git a/tests/out/spv/image.spvasm b/tests/out/spv/image.spvasm index 299dc344a..f2b104fc4 100644 --- a/tests/out/spv/image.spvasm +++ b/tests/out/spv/image.spvasm @@ -2,6 +2,7 @@ ; Version: 1.1 ; Generator: rspirv ; Bound: 213 +OpCapability SampledCubeArray OpCapability ImageQuery OpCapability Image1D OpCapability Shader diff --git a/tests/spirv-capabilities.rs b/tests/spirv-capabilities.rs new file mode 100644 index 000000000..be5fa62bf --- /dev/null +++ b/tests/spirv-capabilities.rs @@ -0,0 +1,180 @@ +//! Test SPIR-V backend capability checks. + +#![cfg(all(feature = "wgsl-in", feature = "spv-out"))] + +use spirv::Capability as Ca; + +fn capabilities_used(source: &str) -> naga::FastHashSet { + use naga::back::spv; + use naga::valid; + + let module = naga::front::wgsl::parse_str(source).unwrap_or_else(|e| { + panic!( + "expected WGSL to parse successfully:\n{}", + e.emit_to_string(source) + ); + }); + + let info = valid::Validator::new(valid::ValidationFlags::all(), valid::Capabilities::all()) + .validate(&module) + .expect("validation failed"); + + let mut words = vec![]; + let mut writer = spv::Writer::new(&spv::Options::default()).unwrap(); + writer.write(&module, &info, &mut words).unwrap(); + writer.get_capabilities_used().clone() +} + +fn require(capabilities: &[Ca], source: &str) { + require_and_forbid(capabilities, &[], source); +} + +fn require_and_forbid(required: &[Ca], forbidden: &[Ca], source: &str) { + let caps_used = capabilities_used(source); + + let missing_caps: Vec<_> = required + .iter() + .filter(|cap| !caps_used.contains(cap)) + .cloned() + .collect(); + if !missing_caps.is_empty() { + panic!( + "shader code should have requested these caps: {:?}\n\n{}", + missing_caps, source + ); + } + + let forbidden_caps: Vec<_> = forbidden + .iter() + .filter(|cap| caps_used.contains(cap)) + .cloned() + .collect(); + if !forbidden_caps.is_empty() { + panic!( + "shader code should not have requested these caps: {:?}\n\n{}", + forbidden_caps, source + ); + } +} + +#[test] +fn sampler1d() { + require( + &[Ca::Sampled1D], + r#" + [[group(0), binding(0)]] + var image_1d: texture_1d; + "#, + ); +} + +#[test] +fn storage1d() { + require( + &[Ca::Image1D], + r#" + [[group(0), binding(0)]] + var image_1d: texture_storage_1d; + "#, + ); +} + +#[test] +fn cube_array() { + // ImageCubeArray is only for storage cube array images, which WGSL doesn't + // support + require_and_forbid( + &[Ca::SampledCubeArray], + &[Ca::ImageCubeArray], + r#" + [[group(0), binding(0)]] + var image_cube: texture_cube_array; + "#, + ); +} + +#[test] +fn image_queries() { + require( + &[Ca::ImageQuery], + r#" + fn f(i: texture_2d) -> vec2 { + return textureDimensions(i); + } + "#, + ); + require( + &[Ca::ImageQuery], + r#" + fn f(i: texture_2d_array) -> i32 { + return textureNumLayers(i); + } + "#, + ); + require( + &[Ca::ImageQuery], + r#" + fn f(i: texture_2d) -> i32 { + return textureNumLevels(i); + } + "#, + ); + require( + &[Ca::ImageQuery], + r#" + fn f(i: texture_multisampled_2d) -> i32 { + return textureNumSamples(i); + } + "#, + ); +} + +#[test] +fn sample_rate_shading() { + require( + &[Ca::SampleRateShading], + r#" + [[stage(fragment)]] + fn f([[location(0), interpolate(perspective, sample)]] x: f32) { } + "#, + ); + + require( + &[Ca::SampleRateShading], + r#" + [[stage(fragment)]] + fn f([[builtin(sample_index)]] x: u32) { } + "#, + ); +} + +#[test] +fn geometry() { + require( + &[Ca::Geometry], + r#" + [[stage(fragment)]] + fn f([[builtin(primitive_index)]] x: u32) { } + "#, + ); +} + +#[test] +fn storage_image_formats() { + require_and_forbid( + &[Ca::Shader], + &[Ca::StorageImageExtendedFormats], + r#" + [[group(0), binding(0)]] + var image_rg32f: texture_storage_2d; + "#, + ); + + require( + &[Ca::StorageImageExtendedFormats], + r#" + [[group(0), binding(0)]] + var image_rg32f: texture_storage_2d; + "#, + ); +}