From 4c1ea47ea255d4531b3a34d863a19b1f68e11f3f Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Wed, 13 May 2026 08:51:15 -0700 Subject: [PATCH] fix(dx12): Fix textureNum{Levels,Layers,Samples} functions (#9542) --- CHANGELOG.md | 1 + cts_runner/fail.lst | 1 + cts_runner/test.lst | 4 +++ naga/src/back/hlsl/help.rs | 26 ++++++++++++++------ naga/tests/out/hlsl/wgsl-binding-arrays.hlsl | 2 +- naga/tests/out/hlsl/wgsl-image.hlsl | 4 +-- 6 files changed, 27 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d22dbcbc6..4539db5b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -225,6 +225,7 @@ By @beholdnec in [#8505](https://github.com/gfx-rs/wgpu/pull/8505). - Fixed use of a texture view without `TextureUsage::TEXTURE_BINDING` as a read-only depth attachment. By @andyleiserson in [#9346](https://github.com/gfx-rs/wgpu/pull/9346). - Fixed a `debug_assert` during stride validation for indirect multi draw. By @kristoff3r in [#9332](https://github.com/gfx-rs/wgpu/pull/9332) - Fixed stencil values read with `textureLoad` appearing in G instead of R. By @andyleiserson in [#9520](https://github.com/gfx-rs/wgpu/pull/9520). +- Fixed some cases where the `textureNum{Layers,Levels,Samples}` functions returned incorrect results. By @andyleiserson in [#9542](https://github.com/gfx-rs/wgpu/pull/9542). #### Metal diff --git a/cts_runner/fail.lst b/cts_runner/fail.lst index ce5897d3a..b52e8d678 100644 --- a/cts_runner/fail.lst +++ b/cts_runner/fail.lst @@ -65,6 +65,7 @@ webgpu:api,validation,state,device_lost,destroy:* // crash webgpu:api,validation,texture,destroy:submit_a_destroyed_texture_as_attachment:* // 44%, https://github.com/gfx-rs/wgpu/issues/8714 webgpu:shader,execution,expression,call,builtin,atan2:f16:* // dx12, fails with dxc, passes with fxc, https://github.com/gfx-rs/wgpu/issues/9179 +webgpu:shader,execution,expression,call,builtin,textureDimensions:* // dx12, https://github.com/gfx-rs/wgpu/issues/9541 webgpu:shader,validation,decl,let:* // texture/sampler let webgpu:shader,validation,decl,override:* // 93%, unrestricted_pointer_parameters not implemented, https://github.com/gfx-rs/wgpu/issues/5158 diff --git a/cts_runner/test.lst b/cts_runner/test.lst index e9151c024..727883f41 100644 --- a/cts_runner/test.lst +++ b/cts_runner/test.lst @@ -359,6 +359,10 @@ webgpu:shader,execution,expression,call,builtin,firstLeadingBit:* //FAIL: webgpu:shader,execution,expression,call,builtin,select:* // - Fails with `const`/abstract int cases on all platforms because of . // - Fails with `vec3` & `f16` cases on macOS because of . +fails-if(dx12) webgpu:shader,execution,expression,call,builtin,textureDimensions:* +webgpu:shader,execution,expression,call,builtin,textureNumLayers:* +webgpu:shader,execution,expression,call,builtin,textureNumLevels:* +webgpu:shader,execution,expression,call,builtin,textureNumSamples:* webgpu:shader,execution,expression,call,builtin,textureSample:sampled_1d_coords:* webgpu:shader,execution,expression,call,builtin,textureSampleBaseClampToEdge:2d_coords:stage="c";textureType="texture_2d";* // NOTE: This is supposed to be an exhaustive listing underneath diff --git a/naga/src/back/hlsl/help.rs b/naga/src/back/hlsl/help.rs index d8e60f1d8..10631331a 100644 --- a/naga/src/back/hlsl/help.rs +++ b/naga/src/back/hlsl/help.rs @@ -667,7 +667,8 @@ impl super::Writer<'_, W> { // GetDimensions Overloaded Methods // https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-to-getdimensions#overloaded-methods - let (ret_swizzle, number_of_params) = match wiq.query { + // We rely on the validator to reject invalid queries. + let (ret_swizzle, number_of_out_params) = match wiq.query { ImageQuery::Size | ImageQuery::SizeLevel => { let ret = match wiq.dim { IDim::D1 => "x", @@ -677,13 +678,22 @@ impl super::Writer<'_, W> { }; (ret, ret.len() + array_coords + extra_coords) } - ImageQuery::NumLevels | ImageQuery::NumSamples | ImageQuery::NumLayers => { - if wiq.arrayed || wiq.dim == IDim::D3 { - ("w", 4) - } else { - ("z", 3) + ImageQuery::NumLevels | ImageQuery::NumSamples => { + // We want `NumberOfLevels` or `Samples` + match wiq.dim { + IDim::D1 => ("y", 2), + IDim::D3 => ("w", 4), + IDim::D2 | IDim::Cube => { + if wiq.arrayed { + ("w", 4) + } else { + ("z", 3) + } + } } } + // We want `Elements` + ImageQuery::NumLayers => ("z", 3 + extra_coords), }; // Write `GetDimensions` function. @@ -704,7 +714,7 @@ impl super::Writer<'_, W> { }, } - for component in COMPONENTS[..number_of_params - 1].iter() { + for component in COMPONENTS[..number_of_out_params - 1].iter() { write!(self.out, "{RETURN_VARIABLE_NAME}.{component}, ")?; } @@ -713,7 +723,7 @@ impl super::Writer<'_, W> { self.out, "{}.{}", RETURN_VARIABLE_NAME, - COMPONENTS[number_of_params - 1] + COMPONENTS[number_of_out_params - 1] )?; writeln!(self.out, ");")?; diff --git a/naga/tests/out/hlsl/wgsl-binding-arrays.hlsl b/naga/tests/out/hlsl/wgsl-binding-arrays.hlsl index 653380f05..664c04a85 100644 --- a/naga/tests/out/hlsl/wgsl-binding-arrays.hlsl +++ b/naga/tests/out/hlsl/wgsl-binding-arrays.hlsl @@ -34,7 +34,7 @@ uint NagaNumLayers2DArray(Texture2DArray tex) { uint4 ret; tex.GetDimensions(0, ret.x, ret.y, ret.z, ret.w); - return ret.w; + return ret.z; } uint NagaNumLevels2D(Texture2D tex) diff --git a/naga/tests/out/hlsl/wgsl-image.hlsl b/naga/tests/out/hlsl/wgsl-image.hlsl index e53eb9e39..b3b381469 100644 --- a/naga/tests/out/hlsl/wgsl-image.hlsl +++ b/naga/tests/out/hlsl/wgsl-image.hlsl @@ -198,7 +198,7 @@ uint NagaNumLayers2DArray(Texture2DArray tex) { uint4 ret; tex.GetDimensions(0, ret.x, ret.y, ret.z, ret.w); - return ret.w; + return ret.z; } uint NagaNumLevels2DArray(Texture2DArray tex) @@ -226,7 +226,7 @@ uint NagaNumLayersCubeArray(TextureCubeArray tex) { uint4 ret; tex.GetDimensions(0, ret.x, ret.y, ret.z, ret.w); - return ret.w; + return ret.z; } uint NagaNumLevels3D(Texture3D tex)