From bf0f2c18497768f322b12c800a7fc8c5faa1fe00 Mon Sep 17 00:00:00 2001 From: Brandon Jones Date: Tue, 29 Mar 2022 23:01:54 +0000 Subject: [PATCH] Stencil8 support fixups Addressing issues that showed up after the initial stencil8 CL landed. D3D12: - Selecting the `all` aspect of a `stencil8` texture was ASSERTing Metal: - Using the `stencil` aspect of a `stencil8` texture was hitting an UNREACHABLE(). Bug: dawn:666 Change-Id: Ic2931dbb915e109727ca24e3216a661fba84e508 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/85021 Reviewed-by: Austin Eng Commit-Queue: Brandon Jones --- src/dawn/native/d3d12/TextureD3D12.cpp | 16 +++++++++++++++- src/dawn/native/metal/TextureMTL.mm | 3 ++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/dawn/native/d3d12/TextureD3D12.cpp b/src/dawn/native/d3d12/TextureD3D12.cpp index 78b267600d..bc80c004dc 100644 --- a/src/dawn/native/d3d12/TextureD3D12.cpp +++ b/src/dawn/native/d3d12/TextureD3D12.cpp @@ -1184,7 +1184,21 @@ namespace dawn::native::d3d12 { case wgpu::TextureFormat::Depth16Unorm: mSrvDesc.Format = DXGI_FORMAT_R16_UNORM; break; - case wgpu::TextureFormat::Stencil8: + case wgpu::TextureFormat::Stencil8: { + // Stencil8 is always backed by a DXGI_FORMAT_R24G8_TYPELESS texture in D3D12, + // so always treat it as if the StencilOnly aspect of a Depth24UnormStencil8 was + // selected. + planeSlice = 1; + mSrvDesc.Format = DXGI_FORMAT_X24_TYPELESS_G8_UINT; + // Stencil is accessed using the .g component in the shader. + // Map it to the zeroth component to match other APIs. + mSrvDesc.Shader4ComponentMapping = D3D12_ENCODE_SHADER_4_COMPONENT_MAPPING( + D3D12_SHADER_COMPONENT_MAPPING_FROM_MEMORY_COMPONENT_1, + D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0, + D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0, + D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_1); + break; + } case wgpu::TextureFormat::Depth24UnormStencil8: switch (descriptor->aspect) { case wgpu::TextureAspect::DepthOnly: diff --git a/src/dawn/native/metal/TextureMTL.mm b/src/dawn/native/metal/TextureMTL.mm index d75510a892..20c56502a7 100644 --- a/src/dawn/native/metal/TextureMTL.mm +++ b/src/dawn/native/metal/TextureMTL.mm @@ -1060,7 +1060,8 @@ namespace dawn::native::metal { } } else { MTLPixelFormat format = MetalPixelFormat(descriptor->format); - if (descriptor->aspect == wgpu::TextureAspect::StencilOnly) { + if (descriptor->aspect == wgpu::TextureAspect::StencilOnly && + format != MTLPixelFormatStencil8) { if (@available(macOS 10.12, iOS 10.0, *)) { if (format == MTLPixelFormatDepth32Float_Stencil8) { format = MTLPixelFormatX32_Stencil8;