Skip to content

Commit 928ddb4

Browse files
fix(dx12): Map components properly for textureLoad from stencil (#9520)
1 parent b2adc9c commit 928ddb4

5 files changed

Lines changed: 55 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ By @beholdnec in [#8505](https://github.com/gfx-rs/wgpu/pull/8505).
215215

216216
- 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).
217217
- Fixed a `debug_assert` during stride validation for indirect multi draw. By @kristoff3r in [#9332](https://github.com/gfx-rs/wgpu/pull/9332)
218+
- Fixed stencil values read with `textureLoad` appearing in G instead of R. By @andyleiserson in [#9520](https://github.com/gfx-rs/wgpu/pull/9520).
218219

219220
#### Metal
220221

cts_runner/fail.lst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
//
1212
// The `cts_runner` integration test `lst_files_are_sorted` verifies that test selectors
1313
// appear in this file in order -- including ones in comments.
14+
15+
webgpu:api,operation,memory_sync,texture,readonly_depth_stencil:sampling_while_testing:format="depth24plus-stencil8";depthReadOnly=false;stencilReadOnly=true // https://github.com/gfx-rs/wgpu/issues/8705
16+
webgpu:api,operation,memory_sync,texture,readonly_depth_stencil:sampling_while_testing:format="depth24plus-stencil8";depthReadOnly=true;stencilReadOnly=false // https://github.com/gfx-rs/wgpu/issues/8705
17+
webgpu:api,operation,memory_sync,texture,readonly_depth_stencil:sampling_while_testing:format="depth32float-stencil8";depthReadOnly=false;stencilReadOnly=true // https://github.com/gfx-rs/wgpu/issues/8705
18+
webgpu:api,operation,memory_sync,texture,readonly_depth_stencil:sampling_while_testing:format="depth32float-stencil8";depthReadOnly=true;stencilReadOnly=false // https://github.com/gfx-rs/wgpu/issues/8705
1419
webgpu:api,operation,rendering,3d_texture_slices:* // https://github.com/gfx-rs/wgpu/issues/9455
1520

1621
webgpu:api,validation,buffer,mapping:* // crash

cts_runner/test.lst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ webgpu:api,operation,compute_pipeline,overrides:*
3636
//FAIL: webgpu:api,operation,compute,basic:large_dispatch:*
3737
webgpu:api,operation,compute,basic:memcpy:*
3838
webgpu:api,operation,device,lost:*
39+
webgpu:api,operation,memory_sync,texture,readonly_depth_stencil:sampling_while_testing:format="depth16unorm";*
40+
webgpu:api,operation,memory_sync,texture,readonly_depth_stencil:sampling_while_testing:format="depth24plus";*
41+
webgpu:api,operation,memory_sync,texture,readonly_depth_stencil:sampling_while_testing:format="depth24plus-stencil8";depthReadOnly=false;stencilReadOnly=false
42+
webgpu:api,operation,memory_sync,texture,readonly_depth_stencil:sampling_while_testing:format="depth24plus-stencil8";depthReadOnly=true;stencilReadOnly=true
43+
webgpu:api,operation,memory_sync,texture,readonly_depth_stencil:sampling_while_testing:format="depth32float";*
44+
webgpu:api,operation,memory_sync,texture,readonly_depth_stencil:sampling_while_testing:format="depth32float-stencil8";depthReadOnly=false;stencilReadOnly=false
45+
webgpu:api,operation,memory_sync,texture,readonly_depth_stencil:sampling_while_testing:format="depth32float-stencil8";depthReadOnly=true;stencilReadOnly=true
46+
webgpu:api,operation,memory_sync,texture,readonly_depth_stencil:sampling_while_testing:format="stencil8";*
47+
3948
webgpu:api,operation,render_pass,storeOp:*
4049
webgpu:api,operation,render_pipeline,overrides:*
4150
webgpu:api,operation,render_pipeline,pipeline_output_targets:color,component_count,blend:*

wgpu-hal/src/dx12/conv.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,21 @@ pub(crate) fn map_acceleration_structure_copy_mode(
447447
}
448448
}
449449
}
450+
451+
pub(crate) const fn make_shader_component_mapping(
452+
src0: Direct3D12::D3D12_SHADER_COMPONENT_MAPPING,
453+
src1: Direct3D12::D3D12_SHADER_COMPONENT_MAPPING,
454+
src2: Direct3D12::D3D12_SHADER_COMPONENT_MAPPING,
455+
src3: Direct3D12::D3D12_SHADER_COMPONENT_MAPPING,
456+
) -> Direct3D12::D3D12_SHADER_COMPONENT_MAPPING {
457+
const M: i32 = Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_MASK as i32;
458+
const S: i32 = Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_SHIFT as i32;
459+
Direct3D12::D3D12_SHADER_COMPONENT_MAPPING(
460+
(src0.0 & M)
461+
| (src1.0 & M) << S
462+
| (src2.0 & M) << (S * 2)
463+
| (src3.0 & M) << (S * 3)
464+
| Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_ALWAYS_SET_BIT_AVOIDING_ZEROMEM_MISTAKES
465+
as i32,
466+
)
467+
}

wgpu-hal/src/dx12/view.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,33 @@ fn aspects_to_plane(aspects: crate::FormatAspects) -> u32 {
4141
}
4242
}
4343

44+
/// Shader component mapping for stencil views
45+
///
46+
/// Stencil views use `DXGI_FORMAT_X24_TYPELESS_G8_UINT` or
47+
/// `DXGI_FORMAT_X32_TYPELESS_G8X24_UINT`, which have the stencil value in
48+
/// the green component. WebGPU specifies that the stencil value be in the
49+
/// red component. It also specifies that the remaining components _should_
50+
/// be (0, 0, 1), but may have an unspecified value.
51+
const STENCIL_COMPONENT_MAPPING: Direct3D12::D3D12_SHADER_COMPONENT_MAPPING =
52+
super::conv::make_shader_component_mapping(
53+
Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_FROM_MEMORY_COMPONENT_1,
54+
Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0,
55+
Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0,
56+
Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_1,
57+
);
58+
4459
impl ViewDescriptor {
4560
pub(crate) unsafe fn to_srv(&self) -> Option<Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC> {
61+
let swizzle = if self.aspects == crate::FormatAspects::STENCIL {
62+
STENCIL_COMPONENT_MAPPING.0 as u32
63+
} else {
64+
Direct3D12::D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING
65+
};
66+
4667
let mut desc = Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC {
4768
Format: self.srv_uav_format?,
4869
ViewDimension: Direct3D12::D3D12_SRV_DIMENSION_UNKNOWN,
49-
Shader4ComponentMapping: Direct3D12::D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
70+
Shader4ComponentMapping: swizzle,
5071
Anonymous: Default::default(),
5172
};
5273

0 commit comments

Comments
 (0)