mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-24 16:31:11 +01:00
[naga glsl-out] Cast u32 image levels and sample indices to int in bounds checks (#10704)
This commit is contained in:
1 parent
118b25a4c4
commit
14231a863b
8 files changed
+187
-40
No files matched your search
@@ -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
|
||||
|
||||
|
||||
@@ -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<crate::Expression>,
|
||||
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.
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
targets = "GLSL"
|
||||
|
||||
[bounds_check_policies]
|
||||
image_load = "Restrict"
|
||||
|
||||
[glsl]
|
||||
version.Desktop = 430
|
||||
zero_initialize_workgroup_memory = true
|
||||
@@ -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<u32>;
|
||||
|
||||
fn test_textureLoad_2d_array_u32_level(coords: vec2<i32>, index: i32, level: u32) -> vec4<u32> {
|
||||
return textureLoad(image_2d_array_u, coords, index, level);
|
||||
}
|
||||
|
||||
@group(0) @binding(1)
|
||||
var image_3d: texture_3d<f32>;
|
||||
|
||||
fn test_textureLoad_3d_u32_level(coords: vec3<i32>, level: u32) -> vec4<f32> {
|
||||
return textureLoad(image_3d, coords, level);
|
||||
}
|
||||
|
||||
@group(0) @binding(2)
|
||||
var image_multisampled_2d: texture_multisampled_2d<f32>;
|
||||
|
||||
fn test_textureLoad_multisampled_2d_u32_sample(coords: vec2<i32>, _sample: u32) -> vec4<f32> {
|
||||
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<f32> {
|
||||
test_textureLoad_2d_array_u32_level(vec2<i32>(), 0, 0u);
|
||||
test_textureLoad_3d_u32_level(vec3<i32>(), 0u);
|
||||
test_textureLoad_multisampled_2d_u32_sample(vec2<i32>(), 0u);
|
||||
|
||||
return vec4<f32>(0.,0.,0.,0.);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
targets = "GLSL"
|
||||
|
||||
[bounds_check_policies]
|
||||
image_load = "ReadZeroSkipWrite"
|
||||
|
||||
[glsl]
|
||||
version.Desktop = 430
|
||||
zero_initialize_workgroup_memory = true
|
||||
@@ -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<u32>;
|
||||
|
||||
fn test_textureLoad_2d_array_u32_level(coords: vec2<i32>, index: i32, level: u32) -> vec4<u32> {
|
||||
return textureLoad(image_2d_array_u, coords, index, level);
|
||||
}
|
||||
|
||||
@group(0) @binding(1)
|
||||
var image_3d: texture_3d<f32>;
|
||||
|
||||
fn test_textureLoad_3d_u32_level(coords: vec3<i32>, level: u32) -> vec4<f32> {
|
||||
return textureLoad(image_3d, coords, level);
|
||||
}
|
||||
|
||||
@group(0) @binding(2)
|
||||
var image_multisampled_2d: texture_multisampled_2d<f32>;
|
||||
|
||||
fn test_textureLoad_multisampled_2d_u32_sample(coords: vec2<i32>, _sample: u32) -> vec4<f32> {
|
||||
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<f32> {
|
||||
test_textureLoad_2d_array_u32_level(vec2<i32>(), 0, 0u);
|
||||
test_textureLoad_3d_u32_level(vec3<i32>(), 0u);
|
||||
test_textureLoad_multisampled_2d_u32_sample(vec2<i32>(), 0u);
|
||||
|
||||
return vec4<f32>(0.,0.,0.,0.);
|
||||
}
|
||||
+36
@@ -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;
|
||||
}
|
||||
|
||||
+33
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user