From a3c28bdd3b16d2c9d7b39dc36b498dd0b98b7533 Mon Sep 17 00:00:00 2001 From: Pavel Kotek <334189328+kotekompotek@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:58:29 +0500 Subject: [PATCH] d3d12: support separate front/back stencil masks in DEPTH_STENCIL2 streams --- docs/d3d12_vulkan.md | 5 +- runtime/shims/d3d12_vulkan_device.cpp | 32 ++++++++---- runtime/test/d3d12_bundle_test.cpp | 70 +++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 12 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 8bf2bd38ea..a0feb7c1e7 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2107,8 +2107,9 @@ The trace will enumerate them. a VS stream builds the same PSO as the classic call. - `E_INVALIDARG`, as on the PC runtime: a type given twice, an unknown type, a truncated stream, MS with VS, tessellation or GS, CS with a graphics stage, or no stage at all. - - `E_NOTIMPL` with a log, for what is not built yet: an AS, a depth-bounds test, separate front and back stencil - masks, view instancing. + - `E_NOTIMPL` with a log, for what is not built yet: an AS, a depth-bounds test, view instancing + (`DEPTH_STENCIL2`'s separate front and back stencil read/write masks map to `depth.front` / `depth.back` + `compareMask` and `writeMask`). - `RASTERIZER1`/`2`'s float depth bias is truncated to the integer the PSO path takes, with a log. - **Mesh PSO:** the MS (`ms` index entry by FSSM hash) and PS on the mesh stage. It has no vertex input or input assembly; the input layout, topology type and strip cut are ignored, as D3D12 ignores them for mesh PSOs. Topology diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 2445302ccb..df494d6f03 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -3104,11 +3104,18 @@ class XboxPipelineStateStubs : public ID3D12PipelineStateStubs { // subobject that is absent takes the runtime's default (the CD3DX12 defaults: depth test on with LESS, back-face // culling, no blending, sample mask 0xFFFFFFFF, one sample). A type given twice, a truncated stream or an unknown // type is E_INVALIDARG, as on the PC runtime; state this layer cannot honour yet is E_NOTIMPL with a log. +struct GraphicsStreamExtras { + bool separate_stencil_masks = false; + UINT8 back_stencil_read_mask = D3D12_DEFAULT_STENCIL_READ_MASK; + UINT8 back_stencil_write_mask = D3D12_DEFAULT_STENCIL_WRITE_MASK; +}; + struct PipelineStream { D3D12_GRAPHICS_PIPELINE_STATE_DESC graphics{}; D3D12_COMPUTE_PIPELINE_STATE_DESC compute{}; D3D12_SHADER_BYTECODE ms{}; D3D12_SHADER_BYTECODE as{}; + GraphicsStreamExtras extras{}; bool has_cs = false; bool has_graphics_stage = false; // VS, PS, DS, HS, GS, AS or MS }; @@ -3236,18 +3243,17 @@ HRESULT parse_pipeline_stream(const D3D12_PIPELINE_STATE_STREAM_DESC& desc, Pipe log_once("depth-bounds tests are not supported yet; stream subobject", int(type)); return E_NOTIMPL; } - // Separate front and back stencil masks are not modelled (one read and one write mask per PSO). - if (d.StencilEnable && (d.FrontFace.StencilReadMask != d.BackFace.StencilReadMask || - d.FrontFace.StencilWriteMask != d.BackFace.StencilWriteMask)) { - log_once("separate front and back stencil masks are not supported yet; stream subobject", int(type)); - return E_NOTIMPL; - } const auto face = [](const D3D12_DEPTH_STENCILOP_DESC1& f) { return D3D12_DEPTH_STENCILOP_DESC{f.StencilFailOp, f.StencilDepthFailOp, f.StencilPassOp, f.StencilFunc}; }; g.DepthStencilState = {d.DepthEnable, d.DepthWriteMask, d.DepthFunc, d.StencilEnable, UINT8(d.FrontFace.StencilReadMask), UINT8(d.FrontFace.StencilWriteMask), face(d.FrontFace), face(d.BackFace)}; + out.extras.separate_stencil_masks = + d.StencilEnable && (d.FrontFace.StencilReadMask != d.BackFace.StencilReadMask || + d.FrontFace.StencilWriteMask != d.BackFace.StencilWriteMask); + out.extras.back_stencil_read_mask = UINT8(d.BackFace.StencilReadMask); + out.extras.back_stencil_write_mask = UINT8(d.BackFace.StencilWriteMask); break; } case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RASTERIZER1: @@ -3389,7 +3395,8 @@ class PipelineState final // no VS, no vertex input or input assembly (the input layout, topology type and strip cut are ignored, as D3D12 // does for mesh PSOs), drawn by DispatchMesh. An AS (amplification shader) is refused: the game has none. HRESULT create_graphics(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& desc, const D3D12_SHADER_BYTECODE* ms = nullptr, - const D3D12_SHADER_BYTECODE* as = nullptr) { + const D3D12_SHADER_BYTECODE* as = nullptr, + const GraphicsStreamExtras* extras = nullptr) { auto& vk = device_.context->vk; const VkDevice dev = device_.info.device; const bool mesh = ms && ms->pShaderBytecode && ms->BytecodeLength; @@ -3777,8 +3784,12 @@ class PipelineState final depth.depthWriteEnable = ds.DepthEnable && ds.DepthWriteMask == D3D12_DEPTH_WRITE_MASK_ALL; depth.depthCompareOp = compare_op(ds.DepthFunc); depth.stencilTestEnable = ds.StencilEnable; + const UINT8 back_read_mask = + extras && extras->separate_stencil_masks ? extras->back_stencil_read_mask : ds.StencilReadMask; + const UINT8 back_write_mask = + extras && extras->separate_stencil_masks ? extras->back_stencil_write_mask : ds.StencilWriteMask; depth.front = stencil_face(ds.FrontFace, ds.StencilReadMask, ds.StencilWriteMask); - depth.back = stencil_face(ds.BackFace, ds.StencilReadMask, ds.StencilWriteMask); + depth.back = stencil_face(ds.BackFace, back_read_mask, back_write_mask); std::vector blends(desc.NumRenderTargets); for (UINT i = 0; i < desc.NumRenderTargets; ++i) { @@ -6184,8 +6195,9 @@ class Device final if (const HRESULT hr = parse_pipeline_stream(*desc, stream); FAILED(hr)) return d3d12_trace::result(method, hr); auto* pipeline = new PipelineState(state_); pipeline->attach_device(identity()); - const HRESULT hr = stream.has_cs ? pipeline->create_compute(stream.compute) - : pipeline->create_graphics(stream.graphics, &stream.ms, &stream.as); + const HRESULT hr = stream.has_cs + ? pipeline->create_compute(stream.compute) + : pipeline->create_graphics(stream.graphics, &stream.ms, &stream.as, &stream.extras); if (FAILED(hr)) { pipeline->Release(); return d3d12_trace::result(method, hr); diff --git a/runtime/test/d3d12_bundle_test.cpp b/runtime/test/d3d12_bundle_test.cpp index 115a7d96f7..edf2c10fb4 100644 --- a/runtime/test/d3d12_bundle_test.cpp +++ b/runtime/test/d3d12_bundle_test.cpp @@ -748,6 +748,76 @@ bool layer_checks(ID3D12Device* device, const std::vector& vs, con return false; } } + + // DEPTH_STENCIL2 stream subobject with distinct FrontFace and BackFace stencil masks succeeds and draws. + { + Rig r; + CHECK(r.init(device, vs, ps)); + Ref dev2; + CHECK(SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&dev2.p)))); + struct alignas(void*) SubRootSig { + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE t = D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE; + ID3D12RootSignature* v = nullptr; + }; + struct alignas(void*) SubShader { + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE t{}; + D3D12_SHADER_BYTECODE v{}; + }; + struct alignas(void*) SubInput { + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE t = D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_INPUT_LAYOUT; + D3D12_INPUT_LAYOUT_DESC v{}; + }; + struct alignas(void*) SubRtv { + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE t = D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RENDER_TARGET_FORMATS; + D3D12_RT_FORMAT_ARRAY v{}; + }; + struct alignas(void*) SubDs2 { + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE t = D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL2; + D3D12_DEPTH_STENCIL_DESC2 v{}; + }; + const D3D12_INPUT_ELEMENT_DESC elems[2] = { + {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}, + {"POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 16, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}, + }; + struct Stream { + SubRootSig rs; + SubShader vs; + SubShader ps; + SubInput ia; + SubRtv rtv; + SubDs2 ds2; + } s{}; + s.rs.v = r.root1.p; + s.vs = {D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VS, {vs.data(), vs.size()}}; + s.ps = {D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PS, {ps.data(), ps.size()}}; + s.ia.v = {elems, 2}; + s.rtv.v.NumRenderTargets = 1; + s.rtv.v.RTFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM; + s.ds2.v.DepthEnable = FALSE; + s.ds2.v.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO; + s.ds2.v.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS; + s.ds2.v.StencilEnable = TRUE; + s.ds2.v.FrontFace = {D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, + D3D12_COMPARISON_FUNC_ALWAYS, 0x55, 0x0f}; + s.ds2.v.BackFace = {D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, + D3D12_COMPARISON_FUNC_ALWAYS, 0xaa, 0xf0}; + const D3D12_PIPELINE_STATE_STREAM_DESC stream_desc{sizeof(s), &s}; + Ref ds2_pso; + CHECK(SUCCEEDED(dev2->CreatePipelineState(&stream_desc, IID_PPV_ARGS(&ds2_pso.p)))); + std::string grid_text; + HRESULT close = S_OK; + CHECK(r.render([&](Rig& rig, ID3D12GraphicsCommandList* l) { + rig.targets(l); + l->SetPipelineState(ds2_pso.p); + l->SetGraphicsRootSignature(rig.root1.p); + rig.vertices(l, vb_quad); + rig.offset(l, 0, 0); + rig.tint(l, tint_red); + l->IASetPrimitiveTopology(strip); + l->DrawInstanced(4, 1, 0, 0); + }, grid_text, close)); + CHECK(close == S_OK && grid_text == ROWS4("RRRRBBBB") " " BLANK4); + } return true; }