From 0f37909cd9303641036bdb49fc1b9b916fb90329 Mon Sep 17 00:00:00 2001 From: atlas dostal Date: Wed, 2 Oct 2024 17:15:08 -0400 Subject: [PATCH 1/5] refactor texture format to scalar conversion --- naga/src/back/glsl/mod.rs | 18 +++++++++++------- naga/src/back/msl/writer.rs | 13 +++++++++---- naga/src/back/spv/mod.rs | 11 +++++++---- naga/src/back/spv/writer.rs | 5 +---- naga/src/front/glsl/functions.rs | 2 +- naga/src/proc/mod.rs | 10 +++++++--- naga/src/proc/typifier.rs | 5 +---- naga/src/valid/function.rs | 5 +---- 8 files changed, 38 insertions(+), 31 deletions(-) diff --git a/naga/src/back/glsl/mod.rs b/naga/src/back/glsl/mod.rs index ed373d00b0..de4a31b74c 100644 --- a/naga/src/back/glsl/mod.rs +++ b/naga/src/back/glsl/mod.rs @@ -1095,12 +1095,16 @@ impl<'a, W: Write> Writer<'a, W> { // - Array - used if it's an image array // - Shadow - used if it's a depth image use crate::ImageClass as Ic; - - let (base, kind, ms, comparison) = match class { - Ic::Sampled { kind, multi: true } => ("sampler", kind, "MS", ""), - Ic::Sampled { kind, multi: false } => ("sampler", kind, "", ""), - Ic::Depth { multi: true } => ("sampler", crate::ScalarKind::Float, "MS", ""), - Ic::Depth { multi: false } => ("sampler", crate::ScalarKind::Float, "", "Shadow"), + use crate::Scalar as S; + let float = S { + kind: crate::ScalarKind::Float, + width: 4, + }; + let (base, scalar, ms, comparison) = match class { + Ic::Sampled { kind, multi: true } => ("sampler", S { kind, width: 4 }, "MS", ""), + Ic::Sampled { kind, multi: false } => ("sampler", S { kind, width: 4 }, "", ""), + Ic::Depth { multi: true } => ("sampler", float, "MS", ""), + Ic::Depth { multi: false } => ("sampler", float, "", "Shadow"), Ic::Storage { format, .. } => ("image", format.into(), "", ""), }; @@ -1114,7 +1118,7 @@ impl<'a, W: Write> Writer<'a, W> { self.out, "{}{}{}{}{}{}{}", precision, - glsl_scalar(crate::Scalar { kind, width: 4 })?.prefix, + glsl_scalar(scalar)?.prefix, base, glsl_dimension(dim), ms, diff --git a/naga/src/back/msl/writer.rs b/naga/src/back/msl/writer.rs index 4ba478c851..83d937d486 100644 --- a/naga/src/back/msl/writer.rs +++ b/naga/src/back/msl/writer.rs @@ -215,14 +215,15 @@ impl<'a> Display for TypeContext<'a> { crate::ImageDimension::D3 => "3d", crate::ImageDimension::Cube => "cube", }; - let (texture_str, msaa_str, kind, access) = match class { + let (texture_str, msaa_str, scalar, access) = match class { crate::ImageClass::Sampled { kind, multi } => { let (msaa_str, access) = if multi { ("_ms", "read") } else { ("", "sample") }; - ("texture", msaa_str, kind, access) + let scalar = crate::Scalar { kind, width: 4 }; + ("texture", msaa_str, scalar, access) } crate::ImageClass::Depth { multi } => { let (msaa_str, access) = if multi { @@ -230,7 +231,11 @@ impl<'a> Display for TypeContext<'a> { } else { ("", "sample") }; - ("depth", msaa_str, crate::ScalarKind::Float, access) + let scalar = crate::Scalar { + kind: crate::ScalarKind::Float, + width: 4, + }; + ("depth", msaa_str, scalar, access) } crate::ImageClass::Storage { format, .. } => { let access = if self @@ -254,7 +259,7 @@ impl<'a> Display for TypeContext<'a> { ("texture", "", format.into(), access) } }; - let base_name = crate::Scalar { kind, width: 4 }.to_msl_name(); + let base_name = scalar.to_msl_name(); let array_str = if arrayed { "_array" } else { "" }; write!( out, diff --git a/naga/src/back/spv/mod.rs b/naga/src/back/spv/mod.rs index 8a254a3a91..f02958d899 100644 --- a/naga/src/back/spv/mod.rs +++ b/naga/src/back/spv/mod.rs @@ -213,7 +213,7 @@ impl Function { /// where practical. #[derive(Debug, PartialEq, Hash, Eq, Copy, Clone)] struct LocalImageType { - sampled_type: crate::ScalarKind, + sampled_type: crate::Scalar, dim: spirv::Dim, flags: ImageTypeFlags, image_format: spirv::ImageFormat, @@ -244,19 +244,22 @@ impl LocalImageType { match class { crate::ImageClass::Sampled { kind, multi } => LocalImageType { - sampled_type: kind, + sampled_type: crate::Scalar { kind, width: 4 }, dim, flags: make_flags(multi, ImageTypeFlags::SAMPLED), image_format: spirv::ImageFormat::Unknown, }, crate::ImageClass::Depth { multi } => LocalImageType { - sampled_type: crate::ScalarKind::Float, + sampled_type: crate::Scalar { + kind: crate::ScalarKind::Float, + width: 4, + }, dim, flags: make_flags(multi, ImageTypeFlags::DEPTH | ImageTypeFlags::SAMPLED), image_format: spirv::ImageFormat::Unknown, }, crate::ImageClass::Storage { format, access: _ } => LocalImageType { - sampled_type: crate::ScalarKind::from(format), + sampled_type: format.into(), dim, flags: make_flags(false, ImageTypeFlags::empty()), image_format: format.into(), diff --git a/naga/src/back/spv/writer.rs b/naga/src/back/spv/writer.rs index 3e7ef7d376..5cc37de786 100644 --- a/naga/src/back/spv/writer.rs +++ b/naga/src/back/spv/writer.rs @@ -902,10 +902,7 @@ impl Writer { Instruction::type_pointer(id, class, type_id) } LocalType::Image(image) => { - let local_type = LocalType::Numeric(NumericType::Scalar(crate::Scalar { - kind: image.sampled_type, - width: 4, - })); + let local_type = LocalType::Numeric(NumericType::Scalar(image.sampled_type)); let type_id = self.get_type_id(LookupType::Local(local_type)); Instruction::type_image(id, type_id, image.dim, image.flags, image.image_format) } diff --git a/naga/src/front/glsl/functions.rs b/naga/src/front/glsl/functions.rs index 2a63c7a028..2ebc8ccfd5 100644 --- a/naga/src/front/glsl/functions.rs +++ b/naga/src/front/glsl/functions.rs @@ -622,7 +622,7 @@ impl Frontend { // check that the format scalar kind matches let good_format = overload_format == call_format || (overload.internal - && ScalarKind::from(overload_format) == ScalarKind::from(call_format)); + && Scalar::from(overload_format) == Scalar::from(call_format)); if !(good_size && good_format) { continue 'outer; } diff --git a/naga/src/proc/mod.rs b/naga/src/proc/mod.rs index b7a08bc8ad..1090631429 100644 --- a/naga/src/proc/mod.rs +++ b/naga/src/proc/mod.rs @@ -20,10 +20,10 @@ pub use namer::{EntryPointIndex, NameKey, Namer}; pub use terminator::ensure_block_returns; pub use typifier::{ResolveContext, ResolveError, TypeResolution}; -impl From for super::ScalarKind { +impl From for super::Scalar { fn from(format: super::StorageFormat) -> Self { use super::{ScalarKind as Sk, StorageFormat as Sf}; - match format { + let kind = match format { Sf::R8Unorm => Sk::Float, Sf::R8Snorm => Sk::Float, Sf::R8Uint => Sk::Uint, @@ -64,7 +64,11 @@ impl From for super::ScalarKind { Sf::Rg16Snorm => Sk::Float, Sf::Rgba16Unorm => Sk::Float, Sf::Rgba16Snorm => Sk::Float, - } + }; + let width = match format { + _ => 4, + }; + super::Scalar { kind, width } } } diff --git a/naga/src/proc/typifier.rs b/naga/src/proc/typifier.rs index 04c776365f..a94546fbce 100644 --- a/naga/src/proc/typifier.rs +++ b/naga/src/proc/typifier.rs @@ -494,10 +494,7 @@ impl<'a> ResolveContext<'a> { size: crate::VectorSize::Quad, }, crate::ImageClass::Storage { format, .. } => Ti::Vector { - scalar: crate::Scalar { - kind: format.into(), - width: 4, - }, + scalar: format.into(), size: crate::VectorSize::Quad, }, }), diff --git a/naga/src/valid/function.rs b/naga/src/valid/function.rs index 23e6204ccb..c695d65144 100644 --- a/naga/src/valid/function.rs +++ b/naga/src/valid/function.rs @@ -1089,10 +1089,7 @@ impl super::Validator { crate::ImageClass::Storage { format, .. } => { crate::TypeInner::Vector { size: crate::VectorSize::Quad, - scalar: crate::Scalar { - kind: format.into(), - width: 4, - }, + scalar: format.into(), } } _ => { From 9d062447ed6fc046693f63f6debd97311ce18e6b Mon Sep 17 00:00:00 2001 From: atlas dostal Date: Wed, 23 Oct 2024 12:28:27 -0400 Subject: [PATCH 2/5] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 148564d6a2..b0ba82dccb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -164,6 +164,7 @@ By @bradwerth [#6216](https://github.com/gfx-rs/wgpu/pull/6216). - Invalidate the device when we encounter driver-induced device loss or on unexpected errors. By @teoxoy in [#6229](https://github.com/gfx-rs/wgpu/pull/6229). - Make Vulkan error handling more robust. By @teoxoy in [#6119](https://github.com/gfx-rs/wgpu/pull/6119). - Add bounds checking to Buffer slice method. By @beholdnec in [#6432](https://github.com/gfx-rs/wgpu/pull/6432). +- ScalarKind from StorageFormat replaced with Scalar from StorageFormat implementation so byte width is included in return type. By @atlv24 in [#6451](https://github.com/gfx-rs/wgpu/pull/6451) #### Internal From 86a023d05b66c30618f51fec0948975049a86e5a Mon Sep 17 00:00:00 2001 From: atlas dostal Date: Wed, 23 Oct 2024 12:30:14 -0400 Subject: [PATCH 3/5] clippy --- naga/src/proc/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/naga/src/proc/mod.rs b/naga/src/proc/mod.rs index 1090631429..5a7f12bac5 100644 --- a/naga/src/proc/mod.rs +++ b/naga/src/proc/mod.rs @@ -65,9 +65,7 @@ impl From for super::Scalar { Sf::Rgba16Unorm => Sk::Float, Sf::Rgba16Snorm => Sk::Float, }; - let width = match format { - _ => 4, - }; + let width = 4; super::Scalar { kind, width } } } From 1b51a8ff28f2030b59dbb2b06e8bf1b4f6486dac Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Wed, 23 Oct 2024 15:38:43 -0400 Subject: [PATCH 4/5] fixup! add changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0ba82dccb..9cbd47860e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -164,7 +164,7 @@ By @bradwerth [#6216](https://github.com/gfx-rs/wgpu/pull/6216). - Invalidate the device when we encounter driver-induced device loss or on unexpected errors. By @teoxoy in [#6229](https://github.com/gfx-rs/wgpu/pull/6229). - Make Vulkan error handling more robust. By @teoxoy in [#6119](https://github.com/gfx-rs/wgpu/pull/6119). - Add bounds checking to Buffer slice method. By @beholdnec in [#6432](https://github.com/gfx-rs/wgpu/pull/6432). -- ScalarKind from StorageFormat replaced with Scalar from StorageFormat implementation so byte width is included in return type. By @atlv24 in [#6451](https://github.com/gfx-rs/wgpu/pull/6451) +- Replace `impl From for ScalarKind` with `impl From for Scalar` so that byte width is included. By @atlv24 in [#6451](https://github.com/gfx-rs/wgpu/pull/6451). #### Internal From 005565ada4998c7d34a424358f4eed2f1264eb16 Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Wed, 23 Oct 2024 15:55:10 -0400 Subject: [PATCH 5/5] fixup! refactor texture format to scalar conversion --- naga/src/proc/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/naga/src/proc/mod.rs b/naga/src/proc/mod.rs index 5a7f12bac5..5661e13d51 100644 --- a/naga/src/proc/mod.rs +++ b/naga/src/proc/mod.rs @@ -65,8 +65,7 @@ impl From for super::Scalar { Sf::Rgba16Unorm => Sk::Float, Sf::Rgba16Snorm => Sk::Float, }; - let width = 4; - super::Scalar { kind, width } + super::Scalar { kind, width: 4 } } }