diff --git a/CHANGELOG.md b/CHANGELOG.md index 301d9a254..7563f540a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -242,6 +242,7 @@ By @sagudev in [#10307](https://github.com/gfx-rs/wgpu/pull/10307). - Panic when a SPIR-V's instruction's word count is greater than `u16::MAX`, instead of emitting a malformed instruction. By @ErichDonGubler in [#10537](https://github.com/gfx-rs/wgpu/pull/10537). - Accept a `ViewIndex` input declared as a signed integer in the SPIR-V frontend, as glslang and Slang emit it, by reading it as a `u32`. By @VoxelBoy in [#10568](https://github.com/gfx-rs/wgpu/pull/10568). - Fix the MSL backend generating wrong code for a loop's `continuing` block when it refers to a value computed by the loop body, such as `loop { let cmp = a < 8; ... continuing { break if cmp; } }`. The value was re-evaluated in place, reading whatever the body had left behind, and for values produced by statements (such as a function call result) the backend panicked instead. By @teoxoy in [#10459](https://github.com/gfx-rs/wgpu/pull/10459). +- Fix invalid GLSL for `textureLoad` with a `u32` level or sample index under the `Restrict` and `ReadZeroSkipWrite` bounds check policies. By @ilyas-mallah in [#XXXXX](https://github.com/gfx-rs/wgpu/pull/XXXXX). #### Validation diff --git a/naga/src/back/glsl/writer.rs b/naga/src/back/glsl/writer.rs index ae78d48b7..fe77c5d4b 100644 --- a/naga/src/back/glsl/writer.rs +++ b/naga/src/back/glsl/writer.rs @@ -2776,25 +2776,8 @@ impl<'a, W: Write> Writer<'a, W> { write!(self.out, "textureSize(")?; self.write_expr(image, ctx)?; if let Some(expr) = level { - let cast_to_int = matches!( - *ctx.resolve_type(expr, &self.module.types), - TypeInner::Scalar(crate::Scalar { - kind: crate::ScalarKind::Uint, - .. - }) - ); - write!(self.out, ", ")?; - - if cast_to_int { - write!(self.out, "int(")?; - } - - self.write_expr(expr, ctx)?; - - if cast_to_int { - write!(self.out, ")")?; - } + self.write_expr_as_int(expr, ctx)?; } else if !multi { // All textureSize calls requires an lod argument // except for multisampled samplers @@ -3868,6 +3851,36 @@ impl<'a, W: Write> Writer<'a, W> { Ok(()) } + /// Writes `expr`, wrapped in `int(...)` if it is an unsigned scalar. + /// + /// GLSL image functions only take `int` levels and sample indices, while WGSL also + /// allows `u32`. + fn write_expr_as_int( + &mut self, + expr: Handle, + ctx: &back::FunctionCtx, + ) -> BackendResult { + let cast_to_int = matches!( + *ctx.resolve_type(expr, &self.module.types), + TypeInner::Scalar(crate::Scalar { + kind: crate::ScalarKind::Uint, + .. + }) + ); + + if cast_to_int { + write!(self.out, "int(")?; + } + + self.write_expr(expr, ctx)?; + + if cast_to_int { + write!(self.out, ")")?; + } + + Ok(()) + } + /// Helper function to write the local holding the clamped lod fn write_clamped_lod( &mut self, @@ -3884,7 +3897,7 @@ impl<'a, W: Write> Writer<'a, W> { CLAMPED_LOD_SUFFIX )?; // Write the lod that will be clamped - self.write_expr(level_expr, ctx)?; + self.write_expr_as_int(level_expr, ctx)?; // Set the min value to 0 and start a call to `textureQueryLevels` to get // the maximum value write!(self.out, ", 0, textureQueryLevels(")?; @@ -4224,7 +4237,7 @@ impl<'a, W: Write> Writer<'a, W> { if let Some(level_expr) = level { // Separate the image from the lod write!(self.out, ", ")?; - self.write_expr(level_expr, ctx)?; + self.write_expr_as_int(level_expr, ctx)?; } // Close the `textureSize` call write!(self.out, ")")?; @@ -4294,7 +4307,7 @@ impl<'a, W: Write> Writer<'a, W> { // the number of samples the image has. if let Some(sample_expr) = sample { write!(self.out, ", clamp(")?; - self.write_expr(sample_expr, ctx)?; + self.write_expr_as_int(sample_expr, ctx)?; // Set the min value to 0 and start the call to `textureSamples` write!(self.out, ", 0, textureSamples(")?; self.write_expr(image, ctx)?; @@ -4303,28 +4316,10 @@ impl<'a, W: Write> Writer<'a, W> { writeln!(self.out, ") - 1)")?; } } else if let Some(sample_or_level) = sample.or(level) { - // GLSL only support SInt on this field while WGSL support also UInt - let cast_to_int = matches!( - *ctx.resolve_type(sample_or_level, &self.module.types), - TypeInner::Scalar(crate::Scalar { - kind: crate::ScalarKind::Uint, - .. - }) - ); - // If no bounds checking is need just add the sample or level argument // after the coordinates write!(self.out, ", ")?; - - if cast_to_int { - write!(self.out, "int(")?; - } - - self.write_expr(sample_or_level, ctx)?; - - if cast_to_int { - write!(self.out, ")")?; - } + self.write_expr_as_int(sample_or_level, ctx)?; } // Close the image load function. diff --git a/naga/tests/in/wgsl/bounds-check-image-restrict-unsigned.toml b/naga/tests/in/wgsl/bounds-check-image-restrict-unsigned.toml new file mode 100644 index 000000000..e6027deaa --- /dev/null +++ b/naga/tests/in/wgsl/bounds-check-image-restrict-unsigned.toml @@ -0,0 +1,8 @@ +targets = "GLSL" + +[bounds_check_policies] +image_load = "Restrict" + +[glsl] +version.Desktop = 430 +zero_initialize_workgroup_memory = true diff --git a/naga/tests/in/wgsl/bounds-check-image-restrict-unsigned.wgsl b/naga/tests/in/wgsl/bounds-check-image-restrict-unsigned.wgsl new file mode 100644 index 000000000..f826522d3 --- /dev/null +++ b/naga/tests/in/wgsl/bounds-check-image-restrict-unsigned.wgsl @@ -0,0 +1,33 @@ +// Unsigned levels and sample indices, which GLSL image functions take as `int`. + +@group(0) @binding(0) +var image_2d_array_u: texture_2d_array; + +fn test_textureLoad_2d_array_u32_level(coords: vec2, index: i32, level: u32) -> vec4 { + return textureLoad(image_2d_array_u, coords, index, level); +} + +@group(0) @binding(1) +var image_3d: texture_3d; + +fn test_textureLoad_3d_u32_level(coords: vec3, level: u32) -> vec4 { + return textureLoad(image_3d, coords, level); +} + +@group(0) @binding(2) +var image_multisampled_2d: texture_multisampled_2d; + +fn test_textureLoad_multisampled_2d_u32_sample(coords: vec2, _sample: u32) -> vec4 { + return textureLoad(image_multisampled_2d, coords, _sample); +} + +// GLSL output requires that we identify an entry point, so +// that it can tell what "in" and "out" globals to write. +@fragment +fn fragment_shader() -> @location(0) vec4 { + test_textureLoad_2d_array_u32_level(vec2(), 0, 0u); + test_textureLoad_3d_u32_level(vec3(), 0u); + test_textureLoad_multisampled_2d_u32_sample(vec2(), 0u); + + return vec4(0.,0.,0.,0.); +} diff --git a/naga/tests/in/wgsl/bounds-check-image-rzsw-unsigned.toml b/naga/tests/in/wgsl/bounds-check-image-rzsw-unsigned.toml new file mode 100644 index 000000000..47422ac99 --- /dev/null +++ b/naga/tests/in/wgsl/bounds-check-image-rzsw-unsigned.toml @@ -0,0 +1,8 @@ +targets = "GLSL" + +[bounds_check_policies] +image_load = "ReadZeroSkipWrite" + +[glsl] +version.Desktop = 430 +zero_initialize_workgroup_memory = true diff --git a/naga/tests/in/wgsl/bounds-check-image-rzsw-unsigned.wgsl b/naga/tests/in/wgsl/bounds-check-image-rzsw-unsigned.wgsl new file mode 100644 index 000000000..f826522d3 --- /dev/null +++ b/naga/tests/in/wgsl/bounds-check-image-rzsw-unsigned.wgsl @@ -0,0 +1,33 @@ +// Unsigned levels and sample indices, which GLSL image functions take as `int`. + +@group(0) @binding(0) +var image_2d_array_u: texture_2d_array; + +fn test_textureLoad_2d_array_u32_level(coords: vec2, index: i32, level: u32) -> vec4 { + return textureLoad(image_2d_array_u, coords, index, level); +} + +@group(0) @binding(1) +var image_3d: texture_3d; + +fn test_textureLoad_3d_u32_level(coords: vec3, level: u32) -> vec4 { + return textureLoad(image_3d, coords, level); +} + +@group(0) @binding(2) +var image_multisampled_2d: texture_multisampled_2d; + +fn test_textureLoad_multisampled_2d_u32_sample(coords: vec2, _sample: u32) -> vec4 { + return textureLoad(image_multisampled_2d, coords, _sample); +} + +// GLSL output requires that we identify an entry point, so +// that it can tell what "in" and "out" globals to write. +@fragment +fn fragment_shader() -> @location(0) vec4 { + test_textureLoad_2d_array_u32_level(vec2(), 0, 0u); + test_textureLoad_3d_u32_level(vec3(), 0u); + test_textureLoad_multisampled_2d_u32_sample(vec2(), 0u); + + return vec4(0.,0.,0.,0.); +} diff --git a/naga/tests/out/glsl/wgsl-bounds-check-image-restrict-unsigned.fragment_shader.Fragment.glsl b/naga/tests/out/glsl/wgsl-bounds-check-image-restrict-unsigned.fragment_shader.Fragment.glsl new file mode 100644 index 000000000..01e9172e4 --- /dev/null +++ b/naga/tests/out/glsl/wgsl-bounds-check-image-restrict-unsigned.fragment_shader.Fragment.glsl @@ -0,0 +1,36 @@ +#version 430 core +#extension GL_ARB_shader_texture_image_samples : require +uniform usampler2DArray _group_0_binding_0_fs; + +uniform sampler3D _group_0_binding_1_fs; + +uniform sampler2DMS _group_0_binding_2_fs; + +layout(location = 0) out vec4 _fs2p_location0; + +uvec4 test_textureLoad_2d_array_u32_level(ivec2 coords, int index, uint level) { + int _e4_clamped_lod = clamp(int(level), 0, textureQueryLevels(_group_0_binding_0_fs) - 1); + uvec4 _e4 = texelFetch(_group_0_binding_0_fs, clamp(ivec3(coords, index), ivec3(0), textureSize(_group_0_binding_0_fs, _e4_clamped_lod) - ivec3(1)), _e4_clamped_lod); + return _e4; +} + +vec4 test_textureLoad_3d_u32_level(ivec3 coords_1, uint level_1) { + int _e3_clamped_lod = clamp(int(level_1), 0, textureQueryLevels(_group_0_binding_1_fs) - 1); + vec4 _e3 = texelFetch(_group_0_binding_1_fs, clamp(coords_1, ivec3(0), textureSize(_group_0_binding_1_fs, _e3_clamped_lod) - ivec3(1)), _e3_clamped_lod); + return _e3; +} + +vec4 test_textureLoad_multisampled_2d_u32_sample(ivec2 coords_2, uint _sample) { + vec4 _e3 = texelFetch(_group_0_binding_2_fs, clamp(coords_2, ivec2(0), textureSize(_group_0_binding_2_fs) - ivec2(1)), clamp(int(_sample), 0, textureSamples(_group_0_binding_2_fs) - 1) +); + return _e3; +} + +void main() { + uvec4 _e3 = test_textureLoad_2d_array_u32_level(ivec2(0), 0, 0u); + vec4 _e6 = test_textureLoad_3d_u32_level(ivec3(0), 0u); + vec4 _e9 = test_textureLoad_multisampled_2d_u32_sample(ivec2(0), 0u); + _fs2p_location0 = vec4(0.0, 0.0, 0.0, 0.0); + return; +} + diff --git a/naga/tests/out/glsl/wgsl-bounds-check-image-rzsw-unsigned.fragment_shader.Fragment.glsl b/naga/tests/out/glsl/wgsl-bounds-check-image-rzsw-unsigned.fragment_shader.Fragment.glsl new file mode 100644 index 000000000..e84aee54a --- /dev/null +++ b/naga/tests/out/glsl/wgsl-bounds-check-image-rzsw-unsigned.fragment_shader.Fragment.glsl @@ -0,0 +1,33 @@ +#version 430 core +#extension GL_ARB_shader_texture_image_samples : require +uniform usampler2DArray _group_0_binding_0_fs; + +uniform sampler3D _group_0_binding_1_fs; + +uniform sampler2DMS _group_0_binding_2_fs; + +layout(location = 0) out vec4 _fs2p_location0; + +uvec4 test_textureLoad_2d_array_u32_level(ivec2 coords, int index, uint level) { + uvec4 _e4 = (level < textureQueryLevels(_group_0_binding_0_fs) && all(lessThan(ivec3(coords, index), textureSize(_group_0_binding_0_fs, int(level)))) ? texelFetch(_group_0_binding_0_fs, ivec3(coords, index), int(level)) : uvec4(0u)); + return _e4; +} + +vec4 test_textureLoad_3d_u32_level(ivec3 coords_1, uint level_1) { + vec4 _e3 = (level_1 < textureQueryLevels(_group_0_binding_1_fs) && all(lessThan(coords_1, textureSize(_group_0_binding_1_fs, int(level_1)))) ? texelFetch(_group_0_binding_1_fs, coords_1, int(level_1)) : vec4(0.0)); + return _e3; +} + +vec4 test_textureLoad_multisampled_2d_u32_sample(ivec2 coords_2, uint _sample) { + vec4 _e3 = (_sample < textureSamples(_group_0_binding_2_fs) && all(lessThan(coords_2, textureSize(_group_0_binding_2_fs))) ? texelFetch(_group_0_binding_2_fs, coords_2, int(_sample)) : vec4(0.0)); + return _e3; +} + +void main() { + uvec4 _e3 = test_textureLoad_2d_array_u32_level(ivec2(0), 0, 0u); + vec4 _e6 = test_textureLoad_3d_u32_level(ivec3(0), 0u); + vec4 _e9 = test_textureLoad_multisampled_2d_u32_sample(ivec2(0), 0u); + _fs2p_location0 = vec4(0.0, 0.0, 0.0, 0.0); + return; +} +