From a7d86d8c769f944de0e1c406e41795ca1753707d Mon Sep 17 00:00:00 2001 From: kawaii-shakal Date: Tue, 24 Nov 2026 19:49:09 +0500 Subject: [PATCH] d3d12: separate front and back stencil masks (D3D-STENCIL-FACE-MASKS) A DEPTH_STENCIL2 subobject whose FrontFace and BackFace masks differ was refused with E_NOTIMPL, and OPTIONS14 reported IndependentFrontAndBackStencilRefMaskSupported = FALSE although the per-face references of OMSetFrontAndBackStencilRef already reach the draw. Vulkan has a compare and a write mask per face in VkStencilOpState. parse_pipeline_stream keeps the front face's masks in the description and the back face's beside it (PipelineStream::back_stencil), create_graphics takes them as an optional argument for depth.back, and OPTIONS14 now reports the cap. The classic CreateGraphicsPipelineState path and its cached-PSO key are unchanged: D3D12_DEPTH_STENCIL_DESC has one mask pair. d3d12_state_sweep_test builds 16 more PSOs from a stream with DEPTH_STENCIL2 (same, split, front-only and back-only masks; one reference or two through OMSetFrontAndBackStencilRef; front- and back-facing) and compares the colour and stencil readback with the host D3D12 device: 498 cases, 0 differ on an RTX 5070 Ti. Giving the back face the front face's masks makes 6 of them differ. d3d12_vulkan_device_test expects the cap. Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 16 ++-- runtime/shims/d3d12_vulkan_device.cpp | 33 +++++-- runtime/test/d3d12_state_sweep_test.cpp | 107 +++++++++++++++++++++- runtime/test/d3d12_vulkan_device_test.cpp | 6 +- 4 files changed, 142 insertions(+), 20 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 8bf2bd38ea..827306eaf2 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -371,9 +371,8 @@ What is implemented (all of it testable without the game): SDK's `d3d12.h` by `tools/gen_d3d12_stubs.py`. The stub reports its name once, so the vtables follow the SDK order exactly and nothing derived from the SDK is checked in. Unknown IIDs are logged and refused. Resources are identified by their `ID3D12Resource` pointer, and RTV/DSV CPU handles point at a `TargetView`. - `OMSetFrontAndBackStencilRef` records its two `UINT`s on the list (and `Reset` clears them), but nothing applies - them yet: Vulkan can set the faces with `vkCmdSetStencilReference` and a face mask, but only a pipeline that - consumes the dynamic state can, and this layer has no pipeline or draw path. + `OMSetFrontAndBackStencilRef` records its two `UINT`s on the list (and `Reset` clears them); each draw sets them + with `vkCmdSetStencilReference` and a face mask. - **Device** (`runtime/shims/d3d12_vulkan_device.cpp`): an `ID3D12Device11` (PC ABI, 80 slots) that the Xbox-ABI proxy (`d3d12_xs_device.cpp`) forwards to, so slot 14's heap-handle conversion and slot 76's resource creation work unchanged. It creates the objects above, plus: @@ -831,7 +830,10 @@ The compute path, end to end: state, since nothing would set it (`VUID-vkCmdDraw-pStrides-04913`). `d3d12_state_sweep_test` checks the mapping against the host on the same adapter: 482 full-screen draws over every blend factor pair, blend operation, write mask, comparison function and stencil operation, with the stencil - plane read back, are byte-identical on an RTX 5080 and a Radeon iGPU. + plane read back, are byte-identical on an RTX 5080 and a Radeon iGPU. Sixteen more build the PSO from a stream + with `DEPTH_STENCIL2` and different masks per face, front- and back-facing, with one reference or two + (`OMSetFrontAndBackStencilRef`): 498 in all, byte-identical on an RTX 5070 Ti (driver 617.42), and 6 of the 16 + differ when the back face is given the front face's masks. - **Logic ops** come from `RenderTarget[0]`, as in D3D12, and apply to every target (Vulkan has one per pipeline; it skips float formats). **`DepthClipEnable = FALSE`** is depth clamping: D3D12 clamps output depth to the viewport's range anyway. @@ -2107,8 +2109,10 @@ 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 per-face read and write masks go to each face's `VkStencilOpState` (the front face's in the + description, the back face's beside it), so `OPTIONS14.IndependentFrontAndBackStencilRefMaskSupported` is TRUE: + with `OMSetFrontAndBackStencilRef`'s per-face references, both of that cap's parts. - `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..52a269d2e6 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -3104,8 +3104,17 @@ 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. +// The back face's stencil masks of a DEPTH_STENCIL2 subobject. D3D12_DEPTH_STENCIL_DESC has one read and one write +// mask for both faces, so the face masks travel beside the description. +struct StencilFaceMasks { + UINT8 read = D3D12_DEFAULT_STENCIL_READ_MASK; + UINT8 write = D3D12_DEFAULT_STENCIL_WRITE_MASK; +}; + struct PipelineStream { D3D12_GRAPHICS_PIPELINE_STATE_DESC graphics{}; + bool back_masks = false; // DEPTH_STENCIL2: back_stencil holds the back face's masks + StencilFaceMasks back_stencil; D3D12_COMPUTE_PIPELINE_STATE_DESC compute{}; D3D12_SHADER_BYTECODE ms{}; D3D12_SHADER_BYTECODE as{}; @@ -3236,12 +3245,10 @@ 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; - } + // Each face has its own read and write mask (OPTIONS14.IndependentFrontAndBackStencilRefMaskSupported): + // the description takes the front face's, back_stencil the back face's. + out.back_masks = true; + out.back_stencil = {UINT8(d.BackFace.StencilReadMask), UINT8(d.BackFace.StencilWriteMask)}; const auto face = [](const D3D12_DEPTH_STENCILOP_DESC1& f) { return D3D12_DEPTH_STENCILOP_DESC{f.StencilFailOp, f.StencilDepthFailOp, f.StencilPassOp, f.StencilFunc}; }; @@ -3388,8 +3395,10 @@ class PipelineState final // With `ms` (a pipeline state stream's MS subobject) it is an MS (+ PS) pipeline on VK_EXT_mesh_shader instead: // 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. + // `back_masks`, from a DEPTH_STENCIL2 subobject, are the back face's stencil masks; without it both faces take + // the description's. 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 StencilFaceMasks* back_masks = nullptr) { auto& vk = device_.context->vk; const VkDevice dev = device_.info.device; const bool mesh = ms && ms->pShaderBytecode && ms->BytecodeLength; @@ -3778,7 +3787,8 @@ class PipelineState final depth.depthCompareOp = compare_op(ds.DepthFunc); depth.stencilTestEnable = ds.StencilEnable; depth.front = stencil_face(ds.FrontFace, ds.StencilReadMask, ds.StencilWriteMask); - depth.back = stencil_face(ds.BackFace, ds.StencilReadMask, ds.StencilWriteMask); + depth.back = back_masks ? stencil_face(ds.BackFace, back_masks->read, back_masks->write) + : stencil_face(ds.BackFace, ds.StencilReadMask, ds.StencilWriteMask); std::vector blends(desc.NumRenderTargets); for (UINT i = 0; i < desc.NumRenderTargets; ++i) { @@ -6185,7 +6195,8 @@ class Device final 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); + : pipeline->create_graphics(stream.graphics, &stream.ms, &stream.as, + stream.back_masks ? &stream.back_stencil : nullptr); if (FAILED(hr)) { pipeline->Release(); return d3d12_trace::result(method, hr); @@ -7737,7 +7748,9 @@ class Device final *opts = {}; opts->AdvancedTextureOpsSupported = FALSE; opts->WriteableMSAATexturesSupported = FALSE; - opts->IndependentFrontAndBackStencilRefMaskSupported = FALSE; + // DEPTH_STENCIL2's per-face masks go to VkStencilOpState, and OMSetFrontAndBackStencilRef sets each + // face's reference (VK_DYNAMIC_STATE_STENCIL_REFERENCE with a face mask). + opts->IndependentFrontAndBackStencilRefMaskSupported = TRUE; return d3d12_trace::result(method, S_OK); } case D3D12_FEATURE_D3D12_OPTIONS15: { diff --git a/runtime/test/d3d12_state_sweep_test.cpp b/runtime/test/d3d12_state_sweep_test.cpp index 0ce033e99f..2c237910bf 100644 --- a/runtime/test/d3d12_state_sweep_test.cpp +++ b/runtime/test/d3d12_state_sweep_test.cpp @@ -51,8 +51,74 @@ struct Case { UINT8 clear_stencil = 0; UINT8 stencil_ref = 0; float blend_factor[4] = {0.3f, 0.6f, 0.9f, 0.1f}; + // A pipeline state stream with a DEPTH_STENCIL2 subobject (ID3D12Device2::CreatePipelineState) instead of + // CreateGraphicsPipelineState: `face_masks` gives each face its own read and write mask. With `back_ref` set + // the draw takes ID3D12GraphicsCommandList8::OMSetFrontAndBackStencilRef(stencil_ref, back_ref). + bool stream = false; + UINT8 front_read = 0xff, front_write = 0xff, back_read = 0xff, back_write = 0xff; + int back_ref = -1; }; +// One pipeline state stream subobject as d3dx12.h declares it (CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT). The +// alignment pads some of them, which is the point (C4324). +#pragma warning(push) +#pragma warning(disable : 4324) +template +struct alignas(void*) Subobject { + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE type = Type; + Inner inner; +}; +#pragma warning(pop) + +// The graphics description `pd` as a stream, its depth-stencil state as DEPTH_STENCIL2 with per-face masks. +HRESULT create_stream_pso(ID3D12Device* device, const D3D12_GRAPHICS_PIPELINE_STATE_DESC& pd, const Case& c, + ID3D12PipelineState** out) { + ID3D12Device2* device2 = nullptr; + if (FAILED(device->QueryInterface(IID_PPV_ARGS(&device2)))) return E_NOINTERFACE; + const D3D12_DEPTH_STENCIL_DESC& ds = pd.DepthStencilState; + const auto face = [](const D3D12_DEPTH_STENCILOP_DESC& f, UINT8 read, UINT8 write) { + return D3D12_DEPTH_STENCILOP_DESC1{f.StencilFailOp, f.StencilDepthFailOp, f.StencilPassOp, f.StencilFunc, read, + write}; + }; + D3D12_DEPTH_STENCIL_DESC2 ds2{}; + ds2.DepthEnable = ds.DepthEnable; + ds2.DepthWriteMask = ds.DepthWriteMask; + ds2.DepthFunc = ds.DepthFunc; + ds2.StencilEnable = ds.StencilEnable; + ds2.FrontFace = face(ds.FrontFace, c.front_read, c.front_write); + ds2.BackFace = face(ds.BackFace, c.back_read, c.back_write); + D3D12_RT_FORMAT_ARRAY formats{}; + formats.NumRenderTargets = pd.NumRenderTargets; + std::memcpy(formats.RTFormats, pd.RTVFormats, sizeof(formats.RTFormats)); + struct { + Subobject root; + Subobject vs; + Subobject ps; + Subobject blend; + Subobject sample_mask; + Subobject raster; + Subobject depth; + Subobject topology; + Subobject rtvs; + Subobject dsv; + Subobject samples; + } stream{{D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE, pd.pRootSignature}, + {D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VS, pd.VS}, + {D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PS, pd.PS}, + {D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_BLEND, pd.BlendState}, + {D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_MASK, pd.SampleMask}, + {D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RASTERIZER, pd.RasterizerState}, + {D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL2, ds2}, + {D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PRIMITIVE_TOPOLOGY, pd.PrimitiveTopologyType}, + {D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RENDER_TARGET_FORMATS, formats}, + {D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL_FORMAT, pd.DSVFormat}, + {D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_DESC, pd.SampleDesc}}; + const D3D12_PIPELINE_STATE_STREAM_DESC desc{sizeof(stream), &stream}; + const HRESULT hr = device2->CreatePipelineState(&desc, IID_PPV_ARGS(out)); + device2->Release(); + return hr; +} + constexpr DXGI_FORMAT kDepthFormat = DXGI_FORMAT_D32_FLOAT_S8X24_UINT; void base_state(D3D12_GRAPHICS_PIPELINE_STATE_DESC& pd, const Case& c, ID3D12RootSignature* root, const Programs& p) { @@ -100,7 +166,8 @@ Result run(ID3D12Device* device, const Case& c, const Programs& p) { D3D12_GRAPHICS_PIPELINE_STATE_DESC pd; base_state(pd, c, root.p, p); Ref pso; - r.pso = device->CreateGraphicsPipelineState(&pd, IID_PPV_ARGS(&pso.p)); + r.pso = c.stream ? create_stream_pso(device, pd, c, &pso.p) + : device->CreateGraphicsPipelineState(&pd, IID_PPV_ARGS(&pso.p)); if (FAILED(r.pso)) return r; D3D12_RESOURCE_DESC cd{}; @@ -172,7 +239,13 @@ Result run(ID3D12Device* device, const Case& c, const Programs& p) { list->SetGraphicsRootSignature(root.p); list->SetPipelineState(pso.p); list->OMSetRenderTargets(1, &rtv, FALSE, &dsv); - list->OMSetStencilRef(c.stencil_ref); + if (c.back_ref >= 0) { + Ref list8; + if (FAILED(list->QueryInterface(IID_PPV_ARGS(&list8.p)))) return r; + list8->OMSetFrontAndBackStencilRef(c.stencil_ref, UINT(c.back_ref)); + } else { + list->OMSetStencilRef(c.stencil_ref); + } list->OMSetBlendFactor(c.blend_factor); list->RSSetViewports(1, &viewport); list->RSSetScissorRects(1, &scissor); @@ -429,6 +502,36 @@ std::vector build_cases() { }; cases.push_back(std::move(c)); } + // DEPTH_STENCIL2: each face compares with its own read mask and writes through its own write mask, and with + // OMSetFrontAndBackStencilRef against its own reference. Stored 0xa5; a pass replaces, a fail inverts. + struct Masks { + const char* name; + UINT8 front_read, front_write, back_read, back_write; + }; + for (const Masks& m : {Masks{"same", 0x0f, 0x3c, 0x0f, 0x3c}, Masks{"split", 0x0f, 0x3c, 0xf0, 0xc3}, + Masks{"front only", 0xff, 0x00, 0x0f, 0xff}, Masks{"back only", 0x0f, 0xff, 0xff, 0x00}}) + for (int back_ref : {-1, 0xa3}) + for (bool flipped : {false, true}) { + Case c; + c.name = std::string("stencil per-face masks ") + m.name + (back_ref < 0 ? ", one ref" : ", two refs") + + (flipped ? ", back-facing" : ", front-facing"); + c.clear_stencil = 0xa5; + c.stencil_ref = 0x35; + c.back_ref = back_ref; + c.stream = true; + c.front_read = m.front_read; + c.front_write = m.front_write; + c.back_read = m.back_read; + c.back_write = m.back_write; + c.state = [=](D3D12_GRAPHICS_PIPELINE_STATE_DESC& pd) { + pd.RasterizerState.FrontCounterClockwise = flipped; + pd.DepthStencilState.StencilEnable = TRUE; + pd.DepthStencilState.FrontFace = pd.DepthStencilState.BackFace = { + D3D12_STENCIL_OP_INVERT, D3D12_STENCIL_OP_INVERT, D3D12_STENCIL_OP_REPLACE, + D3D12_COMPARISON_FUNC_EQUAL}; + }; + cases.push_back(std::move(c)); + } return cases; } diff --git a/runtime/test/d3d12_vulkan_device_test.cpp b/runtime/test/d3d12_vulkan_device_test.cpp index 99bb6d1a4f..4b686ffec7 100644 --- a/runtime/test/d3d12_vulkan_device_test.cpp +++ b/runtime/test/d3d12_vulkan_device_test.cpp @@ -2449,9 +2449,11 @@ int compare_feature_options(ID3D12Device* ours, const rt::d3d12::VulkanDeviceInf o13.InvertedViewportHeightFlipsYSupported || o13.InvertedViewportDepthFlipsZSupported || o13.TextureCopyBetweenDimensionsSupported || o13.AlphaBlendFactorSupported) fail("OPTIONS13 unexpected support"); - if (o14.AdvancedTextureOpsSupported || o14.WriteableMSAATexturesSupported || - o14.IndependentFrontAndBackStencilRefMaskSupported) + if (o14.AdvancedTextureOpsSupported || o14.WriteableMSAATexturesSupported) fail("OPTIONS14 unexpected support"); + // DEPTH_STENCIL2's per-face masks and OMSetFrontAndBackStencilRef (d3d12_state_sweep_test draws them). + if (!o14.IndependentFrontAndBackStencilRefMaskSupported) + fail("OPTIONS14.IndependentFrontAndBackStencilRefMaskSupported should be TRUE"); if (o15.TriangleFanSupported || o15.DynamicIndexBufferStripCutSupported) fail("OPTIONS15 unexpected support"); if (o16.DynamicDepthBiasSupported || o16.GPUUploadHeapSupported) fail("OPTIONS16 unexpected support"); if (o18.RenderPassesValid) fail("OPTIONS18 unexpected support");