From 79ccfaa436eee7ce1bab61b47b56947301feccd7 Mon Sep 17 00:00:00 2001 From: robot00f Date: Tue, 24 Nov 2026 19:52:59 -0300 Subject: [PATCH 1/3] d3d12: GCL9 RSSetDepthBias and IASetIndexBufferStripCutValue, the DYNAMIC_* PSO flags --- docs/d3d12_vulkan.md | 20 ++++-- runtime/CMakeLists.txt | 2 +- runtime/shims/d3d12_com.cpp | 77 ++++++++++++++++++++++-- runtime/shims/d3d12_com.h | 16 +++++ runtime/shims/d3d12_vulkan_device.cpp | 59 +++++++++++++++--- runtime/src/command_list.h | 6 ++ runtime/src/vulkan_backend.h | 8 ++- runtime/src/vulkan_replay.h | 22 ++++++- runtime/test/d3d12_depth24_draw_test.cpp | 47 +++++++++++++-- runtime/test/d3d12_draw_test.cpp | 32 ++++++++-- runtime/test/d3d12_gcl8_test.cpp | 40 ++++++++++-- 11 files changed, 286 insertions(+), 43 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 45d4b4be47..6c21da1784 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -193,6 +193,7 @@ The command-list surface maps to the IR as follows: | `IASetVertexBuffers` / `IASetIndexBuffer` / `IASetPrimitiveTopology` | state; bind arena buffer + offset, dynamic topology. The console's rectangle list (17, PSO topology type 19) and quad list (19) are drawn as triangles, see *Console rectangle and quad lists* below | | `RSSetViewports` / `RSSetScissorRects` | state; dynamic viewport/scissor | | `OMSetRenderTargets` / `OMSetBlendFactor` / `OMSetStencilRef` | state; rendering scope and dynamic state | +| `RSSetDepthBias` / `IASetIndexBufferStripCutValue` (GCL9) | state, seeded from the pipeline at `SetPipelineState`; `vkCmdSetDepthBias` per draw and the cut value, where the PSO carries the matching `DYNAMIC_*` flag | | `DrawInstanced` / `DrawIndexedInstanced` | command with a state snapshot | | `Dispatch` / `DispatchMesh` | command with a state snapshot | | `Copy*` / `Resolve*` / `Clear*` | copy/clear command | @@ -389,14 +390,21 @@ What is implemented (all of it testable without the game): `VulkanQueue` (`ExecuteCommandLists` prepares every list before writing any, so a bad list submits nothing and leaves the tracker as it was; `Signal`; `Wait`, which is attached to the next submission because a Vulkan wait only holds back its own batch) and `VulkanFence` (a timeline semaphore). -- **COM** (`runtime/shims/d3d12_com.cpp`): `ID3D12GraphicsCommandList8` (the GCL7 chain plus - `OMSetFrontAndBackStencilRef`), `ID3D12CommandQueue`, `ID3D12CommandAllocator` and `ID3D12Fence1` (with +- **COM** (`runtime/shims/d3d12_com.cpp`): `ID3D12GraphicsCommandList9` (the GCL7/8 chain plus + `OMSetFrontAndBackStencilRef`, `RSSetDepthBias` and `IASetIndexBufferStripCutValue`), `ID3D12CommandQueue`, + `ID3D12CommandAllocator` and `ID3D12Fence1` (with `SetEventOnCompletion` events). Every method they do not implement is a stub generated at build time from the 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); each draw sets them with `vkCmdSetStencilReference` and a face mask. + `RSSetDepthBias` and `IASetIndexBufferStripCutValue` are list state seeded from the pipeline at + `SetPipelineState`, as D3D12 does, and only a pipeline carrying the matching `DYNAMIC_*` flag reads them: replay + then issues `vkCmdSetDepthBias` before the draw and takes the strip-cut value from the draw. The bias is applied + by the rasterizer, so a `D24_UNORM_S8_UINT` target stored as `D32_SFLOAT_S8_UINT` (whose pixel stage carries bias + itself) keeps the pipeline's bias instead, logged once. `CheckFeatureSupport` reports + `OPTIONS16.DynamicDepthBiasSupported` and `OPTIONS15.DynamicIndexBufferStripCutSupported` TRUE accordingly. - **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: @@ -1317,9 +1325,11 @@ and any validation error fails them. bit-identical, garbage records bounded, records and counts past `max_command_count` empty, and the per-batch work budget saturating; dispatch records bounded by the device's `maxComputeWorkGroupCount` (70 000 x 1 x 1 kept and run where the device allows it, cut and counted where it does not); clean SKIP without a Vulkan device. -- `d3d12_gcl8_test`: the public `ID3D12GraphicsCommandList8` surface: `QueryInterface` for its IID, - `OMSetFrontAndBackStencilRef(1, 2)` with the recorded values, and the reference count around the query. It - creates only an allocator and a list, so it needs no GPU or Vulkan loader. +- `d3d12_gcl8_test`: the public `ID3D12GraphicsCommandList8` and `9` surfaces: `QueryInterface` for both IIDs, + `OMSetFrontAndBackStencilRef(1, 2)`, `RSSetDepthBias` and `IASetIndexBufferStripCutValue` with the recorded values + (and their Reset defaults), and the reference count around the query. It creates only an allocator and a list, so + it needs no GPU or Vulkan loader. `d3d12_draw_test` and `d3d12_depth24_draw_test` apply the two GCL9 values to + real draws. - `d3d12_depth_resolve_test`: depth and stencil `ResolveSubresourceRegion` of a 4x D32 or D24S8 target cleared with three overlapping values, whole and by rectangle, compared with the PC runtime on the layer's own adapter and WARP; the refused calls must fail `Close` and leave the destination as cleared. diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index f8edc3508e..8db09ffc9b 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -2463,7 +2463,7 @@ if(TARGET Vulkan::Headers) OUTPUT ${LEONIDA_STUBS} COMMAND "${Python3_EXECUTABLE}" -I ${CMAKE_CURRENT_SOURCE_DIR}/../tools/gen_d3d12_stubs.py --out ${LEONIDA_STUBS} ${_stubs_header} - ID3D12GraphicsCommandList8 ID3D12CommandQueue ID3D12CommandAllocator ID3D12Fence1 + ID3D12GraphicsCommandList9 ID3D12CommandQueue ID3D12CommandAllocator ID3D12Fence1 ID3D12Device11 ID3D12DescriptorHeap ID3D12Heap ID3D12Resource ID3D12QueryHeap ID3D12RootSignature ID3D12PipelineState ID3D12CommandSignature ID3D12PipelineLibrary1 ID3D12ShaderCacheSession DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../tools/gen_d3d12_stubs.py ${LEONIDA_D3D12_HEADER} diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 4a61ffadbc..2b77828f69 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp @@ -198,11 +198,11 @@ class CommandAllocator final }; class GraphicsCommandList final - : public Child { + ID3D12GraphicsCommandList8, ID3D12GraphicsCommandList9> { public: // {2f8a7c33-91d4-4b6e-8e05-6d3f1a9b2c47} static constexpr GUID kTag{0x2f8a7c33, 0x91d4, 0x4b6e, {0x8e, 0x05, 0x6d, 0x3f, 0x1a, 0x9b, 0x2c, 0x47}}; @@ -238,6 +238,10 @@ class GraphicsCommandList final } UINT front_stencil_ref() const { return front_stencil_ref_; } UINT back_stencil_ref() const { return back_stencil_ref_; } + float depth_bias_constant() const { return depth_bias_constant_; } + float depth_bias_clamp() const { return depth_bias_clamp_; } + float depth_bias_slope() const { return depth_bias_slope_; } + std::uint32_t strip_cut() const { return strip_cut_; } D3D12_GPU_VIRTUAL_ADDRESS cbv_srv_uav_base() const { return cbv_srv_uav_base_; } D3D12_GPU_VIRTUAL_ADDRESS sampler_base() const { return sampler_base_; } @@ -577,7 +581,8 @@ class GraphicsCommandList final } if (!any_nonempty_viewport()) return; // as record_draw: nothing is rasterized leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, {}, 0, viewport_, - scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; + scissor_, blend_factor_, depth_bias_constant_, depth_bias_clamp_, depth_bias_slope_, + strip_cut_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; note_direct_heap_indexing(graphics_.layout); draw.mesh = true; draw.viewports = draw_viewports(); @@ -964,6 +969,35 @@ class GraphicsCommandList final back_stencil_ref_ = back; } + // GCL9. The depth bias a DYNAMIC_DEPTH_BIAS pipeline reads at draw time, replayed with vkCmdSetDepthBias. As in + // D3D12 the values are list state, seeded from the pipeline at SetPipelineState. + void STDMETHODCALLTYPE RSSetDepthBias(FLOAT depth_bias, FLOAT depth_bias_clamp, FLOAT slope) override { + d3d12_trace::record("ID3D12GraphicsCommandList9::RSSetDepthBias"); + if (list_.closed()) return; + if (defer([=](GraphicsCommandList& l) { + l.depth_bias_constant_ = depth_bias; + l.depth_bias_clamp_ = depth_bias_clamp; + l.depth_bias_slope_ = slope; + })) + return; + depth_bias_constant_ = depth_bias; + depth_bias_clamp_ = depth_bias_clamp; + depth_bias_slope_ = slope; + } + + // GCL9. Only the three defined values are taken; replay turns the value into primitive restart where the bound + // index width matches, since Vulkan's cut value is fixed by that width. + void STDMETHODCALLTYPE IASetIndexBufferStripCutValue(D3D12_INDEX_BUFFER_STRIP_CUT_VALUE value) override { + d3d12_trace::record("ID3D12GraphicsCommandList9::IASetIndexBufferStripCutValue"); + if (list_.closed()) return; + if (value != D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_DISABLED && + value != D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFF && + value != D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFFFFFF) + return check(false); + if (defer([=](GraphicsCommandList& l) { l.strip_cut_ = u(value); })) return; + strip_cut_ = u(value); + } + // Debug markers, recorded in order with the other commands and replayed as VK_EXT_debug_utils labels. The trace // names the call only, never the label. void STDMETHODCALLTYPE SetMarker(UINT metadata, const void* data, UINT size) override { @@ -1578,6 +1612,12 @@ class GraphicsCommandList final bool invalid_ = false; UINT front_stencil_ref_ = 0; UINT back_stencil_ref_ = 0; + // RSSetDepthBias / IASetIndexBufferStripCutValue state, restored to the pipeline's at SetPipelineState and read + // by the draws that pipeline records. + float depth_bias_constant_ = 0.0f; + float depth_bias_clamp_ = 0.0f; + float depth_bias_slope_ = 0.0f; + std::uint32_t strip_cut_ = 0; RootState compute_; RootState graphics_; std::uintptr_t compute_pipeline_ = 0; @@ -1669,6 +1709,14 @@ class GraphicsCommandList final unregistered_pipeline_ = state && !compute_pipeline_ && !graphics_pipeline_; if (unregistered_pipeline_) stub("ID3D12GraphicsCommandList::SetPipelineState(pipeline from another device)"); + // The dynamic-state values a PSO seeds the list with, as D3D12 does at SetPipelineState. + GraphicsPipelineDynamicState dynamic; + if (graphics_pipeline_dynamic_state(state, dynamic)) { + depth_bias_constant_ = dynamic.depth_bias_constant; + depth_bias_clamp_ = dynamic.depth_bias_clamp; + depth_bias_slope_ = dynamic.depth_bias_slope; + strip_cut_ = dynamic.strip_cut; + } } void set_descriptor_heaps(ID3D12DescriptorHeap* cbv, D3D12_GPU_VIRTUAL_ADDRESS cbv_base, @@ -1794,6 +1842,8 @@ class GraphicsCommandList final void reset_state(ID3D12PipelineState* state) { front_stencil_ref_ = back_stencil_ref_ = 0; + depth_bias_constant_ = depth_bias_clamp_ = depth_bias_slope_ = 0.0f; + strip_cut_ = 0; compute_ = {}; graphics_ = {}; cbv_srv_uav_heap_ = nullptr; @@ -2217,8 +2267,9 @@ class GraphicsCommandList final void record_raster_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance, const leonida::IndirectDrawArgs* indirect = nullptr) { leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, index_, topology_, viewport_, - scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, - first, base_vertex, first_instance}; + scissor_, blend_factor_, depth_bias_constant_, depth_bias_clamp_, depth_bias_slope_, + strip_cut_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, first, + base_vertex, first_instance}; draw.viewports = draw_viewports(); draw.depth_bounds = depth_bounds_; request_draw_pipeline(); @@ -2998,6 +3049,22 @@ bool hold_until_reset(ID3D12GraphicsCommandList* list, IUnknown* object) { return true; } +bool depth_bias_state(ID3D12GraphicsCommandList9* list, float& constant, float& clamp, float& slope) { + const GraphicsCommandList* l = GraphicsCommandList::from(list); + if (!l) return false; + constant = l->depth_bias_constant(); + clamp = l->depth_bias_clamp(); + slope = l->depth_bias_slope(); + return true; +} + +bool strip_cut_value(ID3D12GraphicsCommandList9* list, std::uint32_t& value) { + const GraphicsCommandList* l = GraphicsCommandList::from(list); + if (!l) return false; + value = l->strip_cut(); + return true; +} + bool recorded_commands(ID3D12GraphicsCommandList* list, std::vector& commands) { const GraphicsCommandList* l = GraphicsCommandList::from(list); if (!l) return false; diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index 3bad0b7671..d88534ea5e 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h @@ -593,6 +593,17 @@ std::uintptr_t graphics_pipeline_id(ID3D12PipelineState* pipeline); // nothing). Compute: at SetPipelineState; graphics: at each draw, with the render-target slots it binds (bit per slot) // and whether it binds a depth buffer. void request_pipeline_build(std::uintptr_t id, std::uint32_t bound_targets, bool depth_bound); +// The depth-bias and strip-cut state a graphics PSO seeds the list with at SetPipelineState, and whether it carries +// the matching DYNAMIC_* flag. False for anything but a graphics PSO of a Vulkan device. +struct GraphicsPipelineDynamicState { + float depth_bias_constant = 0.0f; + float depth_bias_clamp = 0.0f; + float depth_bias_slope = 0.0f; + std::uint32_t strip_cut = 0; // D3D12_INDEX_BUFFER_STRIP_CUT_VALUE + bool dynamic_depth_bias = false; + bool dynamic_strip_cut = false; +}; +bool graphics_pipeline_dynamic_state(ID3D12PipelineState* pipeline, GraphicsPipelineDynamicState& state); // What each stage of a Vulkan device's PSO asked for (src/subgroup_size.h), in creation order; null for another PSO. const std::vector* pipeline_subgroup_requests(ID3D12PipelineState* pipeline); // The linked SPIR-V stage modules for a lazy pipeline (in recipe stages order); false when not a lazy pipeline. @@ -666,6 +677,11 @@ bool front_back_stencil_ref(ID3D12GraphicsCommandList8* list, UINT& front, UINT& // lists or `object` is null. bool hold_until_reset(ID3D12GraphicsCommandList* list, IUnknown* object); +// The depth-bias values and strip-cut value last recorded by ID3D12GraphicsCommandList9::RSSetDepthBias and +// ::IASetIndexBufferStripCutValue. False when `list` is not one of this module's command lists. +bool depth_bias_state(ID3D12GraphicsCommandList9* list, float& constant, float& clamp, float& slope); +bool strip_cut_value(ID3D12GraphicsCommandList9* list, std::uint32_t& value); + // A copy of the commands `list` has recorded so far, for tests of what a bundle puts into its parent. False when // `list` is not one of this module's command lists. bool recorded_commands(ID3D12GraphicsCommandList* list, std::vector& commands); diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index f2fa3a45c2..c8fae0d8da 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -11476,6 +11476,9 @@ class PipelineState final } bool compute() const { return pending_stream_kind_==2 || (registered_ && !is_graphics_); } bool graphics() const { return native_owner_ ? native_owner_->graphics() : pending_stream_kind_==1 || (registered_ && is_graphics_); } + const leonida::GraphicsPipeline& graphics_pipeline() const { + return native_owner_ ? native_owner_->graphics_pipeline() : graphics_; + } const std::vector& subgroup_requests() const { return native_owner_ ? native_owner_->subgroup_requests() : subgroup_requests_; } @@ -11547,6 +11550,10 @@ class PipelineState final } if (desc.NodeMask & ~1u) return E_INVALIDARG; if (static_cast(desc.Flags) & ~kKnownPipelineStateFlags) return E_INVALIDARG; + // The two GCL9 dynamic-state flags: the bias and the strip-cut value are then list state, seeded from the + // values below at SetPipelineState (the draw carries them, see leonida::Draw). + graphics_.dynamic_depth_bias = (desc.Flags & D3D12_PIPELINE_STATE_FLAG_DYNAMIC_DEPTH_BIAS) != 0; + graphics_.dynamic_strip_cut = (desc.Flags & D3D12_PIPELINE_STATE_FLAG_DYNAMIC_INDEX_BUFFER_STRIP_CUT) != 0; if (!device_.arena.mapped) { return stream_unsupported("pipelines need the GPU address arena (bufferDeviceAddress); stage", 1); } @@ -11692,9 +11699,17 @@ class PipelineState final int(samples)); depth24_fragment = false; } - const leonida::Depth24Bias depth24_bias{desc.RasterizerState.DepthBias, - desc.RasterizerState.SlopeScaledDepthBias, - desc.RasterizerState.DepthBiasClamp,packed16_fallback?65535.0f:16777215.0f}; + // DYNAMIC_DEPTH_BIAS: the values arrive per draw, so the shader patch keeps no bias of its own and the + // rasterizer takes them from vkCmdSetDepthBias at replay. On a D24 target stored as D32 the pixel stage + // would have to carry them instead, so the bias there stays the PSO's (reported, not silent). + if (depth24_fragment && graphics_.dynamic_depth_bias) + log_once("D24 depth stored as D32 keeps the PSO's depth bias, not the dynamic one; flag", 4); + const leonida::Depth24Bias depth24_bias{graphics_.dynamic_depth_bias ? 0 : desc.RasterizerState.DepthBias, + graphics_.dynamic_depth_bias ? 0.0f + : desc.RasterizerState.SlopeScaledDepthBias, + graphics_.dynamic_depth_bias ? 0.0f + : desc.RasterizerState.DepthBiasClamp, + packed16_fallback ? 65535.0f : 16777215.0f}; for (Stage& stage : stages) { if (!stage.code.pShaderBytecode || !stage.code.BytecodeLength) continue; // no PS: depth only const std::span raw_bytes{ @@ -12018,13 +12033,19 @@ class PipelineState final // With the negative-height viewport, framebuffer winding matches D3D12's screen-space winding. raster.frontFace = r.FrontCounterClockwise ? VK_FRONT_FACE_COUNTER_CLOCKWISE : VK_FRONT_FACE_CLOCKWISE; // The D24-as-D32 pixel stage applies the bias itself, in D24 units (depth24_fragment above): the - // rasterizer's bias stays off and zero, so its clamp needs no depthBiasClamp feature either. - raster.depthBiasEnable = !depth24_fragment && (r.DepthBias || r.SlopeScaledDepthBias != 0.0f); - if (raster.depthBiasEnable) { + // rasterizer's bias stays off and zero, so its clamp needs no depthBiasClamp feature either. A dynamic + // pipeline with a D24 target therefore keeps the rasterizer path, with float depth bias on a D24 store. + raster.depthBiasEnable = !depth24_fragment && (graphics_.dynamic_depth_bias || r.DepthBias || + r.SlopeScaledDepthBias != 0.0f); + if (raster.depthBiasEnable && !graphics_.dynamic_depth_bias) { raster.depthBiasConstantFactor = float(r.DepthBias); raster.depthBiasClamp = r.DepthBiasClamp; raster.depthBiasSlopeFactor = r.SlopeScaledDepthBias; } + // The state the list starts with at SetPipelineState, and what replay hands to vkCmdSetDepthBias. + graphics_.depth_bias_constant = float(r.DepthBias); + graphics_.depth_bias_clamp = r.DepthBiasClamp; + graphics_.depth_bias_slope = r.SlopeScaledDepthBias; raster.lineWidth = 1.0f; // No depth clipping: fragments outside the viewport's depth range are kept and their depth clamped to it, as // D3D12 clamps output depth to MinDepth..MaxDepth anyway (needs depthClamp, checked below). @@ -12100,11 +12121,15 @@ class PipelineState final if ((n & (n - 1)) == 0) std::fprintf(stderr, "[rt] d3d12: depth-bounds PSO #%u\n", n); } + graphics_.depth_bias_clamp_supported = features.depthBiasClamp; if (raster.polygonMode != VK_POLYGON_MODE_FILL && !features.fillModeNonSolid) return stream_unsupported("wireframe fill needs fillModeNonSolid, which the device lacks; fill mode", r.FillMode); if (raster.depthBiasEnable && raster.depthBiasClamp != 0.0f && !features.depthBiasClamp) return stream_unsupported("a depth-bias clamp needs depthBiasClamp, which the device lacks; clamp x1000", int(raster.depthBiasClamp * 1000)); + if (graphics_.dynamic_depth_bias && r.DepthBiasClamp != 0.0f && !features.depthBiasClamp) + log_once("a dynamic depth-bias clamp needs depthBiasClamp, which the device lacks; the clamp is dropped", + int(r.DepthBiasClamp * 1000)); for (std::size_t i = 1; i < blends.size(); ++i) if (std::memcmp(&blends[i], &blends[0], sizeof(blends[0])) != 0 && !features.independentBlend) return stream_unsupported("per-render-target blend state needs independentBlend, which the device lacks; target", @@ -12231,6 +12256,7 @@ class PipelineState final recipe_.assembly = assembly; recipe_.raster = raster; recipe_.conservative = r.ConservativeRaster == D3D12_CONSERVATIVE_RASTERIZATION_MODE_ON; + recipe_.dynamic_depth_bias = graphics_.dynamic_depth_bias; recipe_.multisample = multisample; graphics_.samples = samples; recipe_.sample_mask = sample_mask; @@ -12695,6 +12721,7 @@ class PipelineState final VkPipelineInputAssemblyStateCreateInfo assembly{}; VkPipelineRasterizationStateCreateInfo raster{}; bool conservative = false; + bool dynamic_depth_bias = false; // VK_DYNAMIC_STATE_DEPTH_BIAS, as the draw supplies the values VkPipelineMultisampleStateCreateInfo multisample{}; VkSampleMask sample_mask = ~0u; VkPipelineDepthStencilStateCreateInfo depth{}; @@ -12753,7 +12780,7 @@ class PipelineState final // A mesh pipeline takes neither the topology nor the strides as dynamic state // (VUID-VkGraphicsPipelineCreateInfo-pDynamicStates-07065 and -07066). // With multiViewport enabled, VIEWPORT_WITH_COUNT / SCISSOR_WITH_COUNT allow 1..16 dynamic viewports/scissors. - VkDynamicState dyn_list[8]{}; + VkDynamicState dyn_list[10]{}; std::uint32_t dyn_count = 0; dyn_list[dyn_count++] = graphics_.viewport_with_count ? VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT : VK_DYNAMIC_STATE_VIEWPORT; @@ -12767,6 +12794,7 @@ class PipelineState final dyn_list[dyn_count++] = VK_DYNAMIC_STATE_BLEND_CONSTANTS; if (!r.mesh && !r.bindings.empty()) dyn_list[dyn_count++] = VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE; if (r.depth.depthBoundsTestEnable) dyn_list[dyn_count++] = VK_DYNAMIC_STATE_DEPTH_BOUNDS; + if (recipe_.dynamic_depth_bias) dyn_list[dyn_count++] = VK_DYNAMIC_STATE_DEPTH_BIAS; VkPipelineDynamicStateCreateInfo dynamic{VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO}; dynamic.dynamicStateCount = dyn_count; dynamic.pDynamicStates = dyn_list; @@ -19499,14 +19527,14 @@ class Device final auto* opts = static_cast(data); *opts = {}; opts->TriangleFanSupported = FALSE; - opts->DynamicIndexBufferStripCutSupported = FALSE; + opts->DynamicIndexBufferStripCutSupported = TRUE; // IASetIndexBufferStripCutValue (GCL9) return d3d12_trace::result(method, S_OK); } case D3D12_FEATURE_D3D12_OPTIONS16: { if (size != sizeof(D3D12_FEATURE_DATA_D3D12_OPTIONS16)) return d3d12_trace::result(method, E_INVALIDARG); auto* opts = static_cast(data); *opts = {}; - opts->DynamicDepthBiasSupported = FALSE; + opts->DynamicDepthBiasSupported = TRUE; // RSSetDepthBias (GCL9) opts->GPUUploadHeapSupported = FALSE; return d3d12_trace::result(method, S_OK); } @@ -20932,6 +20960,19 @@ void request_pipeline_build(std::uintptr_t id, std::uint32_t bound_targets, bool pso->request_early(bound_targets, depth_bound); } +bool graphics_pipeline_dynamic_state(ID3D12PipelineState* pipeline, GraphicsPipelineDynamicState& state) { + auto* p = PipelineState::from(pipeline); + if (!p || !p->resolve_type(true)) return false; + const leonida::GraphicsPipeline& graphics = p->graphics_pipeline(); + state.depth_bias_constant = graphics.depth_bias_constant; + state.depth_bias_clamp = graphics.depth_bias_clamp; + state.depth_bias_slope = graphics.depth_bias_slope; + state.strip_cut = graphics.strip_cut; + state.dynamic_depth_bias = graphics.dynamic_depth_bias; + state.dynamic_strip_cut = graphics.dynamic_strip_cut; + return true; +} + const std::vector* pipeline_subgroup_requests(ID3D12PipelineState* pipeline) { const PipelineState* p = PipelineState::from(pipeline); return p && (p->compute() || p->graphics()) ? &p->subgroup_requests() : nullptr; diff --git a/runtime/src/command_list.h b/runtime/src/command_list.h index d7d7396408..30bfcb49ae 100644 --- a/runtime/src/command_list.h +++ b/runtime/src/command_list.h @@ -452,6 +452,12 @@ struct Draw { Viewport viewport{}; // the first; more viewports are not modelled yet Rect scissor{}; // the first; D3D12 always scissors std::array blend_factor{1, 1, 1, 1}; + // The list's depth-bias state (RSSetDepthBias) and index-buffer strip-cut value, read at the draw when the + // pipeline carries the matching DYNAMIC_* flag. Seeded from the pipeline state at SetPipelineState. + float depth_bias_constant = 0.0f; + float depth_bias_clamp = 0.0f; + float depth_bias_slope = 0.0f; + std::uint32_t strip_cut = 0; // D3D12_INDEX_BUFFER_STRIP_CUT_VALUE std::uint32_t stencil_front = 0; std::uint32_t stencil_back = 0; bool indexed = false; diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index bf970aa282..57ee4c6343 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -125,8 +125,8 @@ struct VulkanDeviceFunctions { X(vkCmdPushConstants) X(vkCmdDispatch) \ X(vkCmdBindVertexBuffers2) X(vkCmdBindIndexBuffer) X(vkCmdSetViewport) X(vkCmdSetScissor) \ X(vkCmdSetViewportWithCount) X(vkCmdSetScissorWithCount) \ - X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) X(vkCmdSetDepthBounds) \ - X(vkCmdSetBlendConstants) X(vkCmdDraw) \ + X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) X(vkCmdSetDepthBounds) \ + X(vkCmdSetBlendConstants) X(vkCmdSetDepthBias) X(vkCmdDraw) \ X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) X(vkGetImageSparseMemoryRequirements) \ X(vkCreatePipelineCache) X(vkDestroyPipelineCache) X(vkGetPipelineCacheData) X(vkMergePipelineCaches) \ X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) X(vkCmdDrawMeshTasksEXT) X(vkCmdDrawMeshTasksIndirectEXT) X(vkCmdDrawMeshTasksIndirectCountEXT) \ @@ -158,7 +158,8 @@ struct VulkanDeviceFunctions { vkCreateShaderModule && vkDestroyShaderModule && vkCreatePipelineLayout && vkDestroyPipelineLayout && vkCreateComputePipelines && vkDestroyPipeline && vkGetBufferDeviceAddress && vkCmdBindIndexBuffer && vkCmdSetViewport && vkCmdSetScissor && - vkCmdSetStencilReference && vkCmdSetBlendConstants && vkCmdDraw && vkCmdDrawIndexed && + vkCmdSetStencilReference && vkCmdSetBlendConstants && vkCmdSetDepthBias && vkCmdDraw && + vkCmdDrawIndexed && vkCreateGraphicsPipelines; } @@ -1193,6 +1194,7 @@ class VulkanContext { replay_.set_stencil_reference = vk.vkCmdSetStencilReference; replay_.set_blend_constants = vk.vkCmdSetBlendConstants; replay_.set_depth_bounds = vk.vkCmdSetDepthBounds; + replay_.set_depth_bias = vk.vkCmdSetDepthBias; replay_.draw = vk.vkCmdDraw; replay_.draw_mesh_tasks = vk.vkCmdDrawMeshTasksEXT; // null unless VK_EXT_mesh_shader is enabled replay_.draw_mesh_tasks_indirect = vk.vkCmdDrawMeshTasksIndirectEXT; diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 548fb4c17a..b90169c094 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -896,6 +896,15 @@ struct GraphicsPipeline { std::uint32_t vertex_slots = 0; // bit per input slot the input layout uses std::uint32_t zero_step_slots = 0; // per-instance slots with InstanceDataStepRate 0: stride 0 at draw time std::uint32_t strip_cut = 0; // D3D12_INDEX_BUFFER_STRIP_CUT_VALUE (0: off, 1: 0xFFFF, 2: 0xFFFFFFFF) + // The two DYNAMIC_* PSO flags: the bias and the strip-cut value then come from the draw's list state + // (RSSetDepthBias, IASetIndexBufferStripCutValue) instead of this pipeline. The values below are the state the + // list starts with at SetPipelineState, taken from the rasterizer state and IBStripCutValue. + bool dynamic_depth_bias = false; + bool dynamic_strip_cut = false; + bool depth_bias_clamp_supported = false; // the device enables the clamp; a dynamic clamp is dropped without it + float depth_bias_constant = 0.0f; + float depth_bias_clamp = 0.0f; + float depth_bias_slope = 0.0f; bool mesh = false; // a mesh shader instead of a vertex shader: drawn by DispatchMesh only bool viewport_with_count = false; // pipeline uses VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT / SCISSOR_WITH_COUNT bool depth_bounds = false; // depthBoundsTestEnable: the bounds are dynamic (vkCmdSetDepthBounds) @@ -1218,6 +1227,7 @@ struct ReplayFunctions { PFN_vkCmdSetStencilReference set_stencil_reference = nullptr; PFN_vkCmdSetBlendConstants set_blend_constants = nullptr; PFN_vkCmdSetDepthBounds set_depth_bounds = nullptr; // for depth-bounds pipelines only + PFN_vkCmdSetDepthBias set_depth_bias = nullptr; PFN_vkCmdDraw draw = nullptr; unsigned* mesh_draw_count = nullptr; // opt-in count for this submission PFN_vkCmdDrawMeshTasksEXT draw_mesh_tasks = nullptr; // VK_EXT_mesh_shader; null: mesh draws are unsupported @@ -2766,7 +2776,7 @@ ReplayResult prepare_draw(const Draw& draw, const IndirectDrawArgs* arguments, R if (!vk.graphics_pipeline || !vk.buffer_at || !vk.image_view || !vk.begin_rendering || !vk.end_rendering || !vk.bind_vertex_buffers2 || !vk.bind_index_buffer || !vk.set_viewport || !vk.set_scissor || !vk.set_primitive_topology || !vk.set_primitive_restart_enable || !vk.set_stencil_reference || - !vk.set_blend_constants || !vk.draw || + !vk.set_blend_constants || !vk.set_depth_bias || !vk.draw || !vk.draw_indexed || !vk.bind_pipeline || !vk.push_constants) return unsupported_at(__LINE__); const GraphicsPipeline* pipeline = vk.graphics_pipeline(draw.pipeline); @@ -5610,11 +5620,17 @@ inline void emit_commands(const PreparedList& prepared, VkCommandBuffer command_ const VkPrimitiveTopology topology = draw->topology == topology_quad_list && !draw->indexed ? VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST : detail::vulkan_topology(draw->topology); vk.set_primitive_topology(command_buffer, topology); + // DYNAMIC_INDEX_BUFFER_STRIP_CUT: the cut value is list state instead of the pipeline's. + const std::uint32_t cut = graphics.dynamic_strip_cut ? draw->strip_cut : graphics.strip_cut; const bool restart = draw->indexed && detail::strip_topology(topology) && - ((graphics.strip_cut == 1 && d.index_type == VK_INDEX_TYPE_UINT16) || - (graphics.strip_cut == 2 && d.index_type == VK_INDEX_TYPE_UINT32)); + ((cut == 1 && d.index_type == VK_INDEX_TYPE_UINT16) || + (cut == 2 && d.index_type == VK_INDEX_TYPE_UINT32)); vk.set_primitive_restart_enable(command_buffer, restart ? VK_TRUE : VK_FALSE); } + if (graphics.dynamic_depth_bias) + vk.set_depth_bias(command_buffer, draw->depth_bias_constant, + graphics.depth_bias_clamp_supported ? draw->depth_bias_clamp : 0.0f, + draw->depth_bias_slope); vk.set_stencil_reference(command_buffer, VK_STENCIL_FACE_FRONT_BIT, draw->stencil_front); vk.set_stencil_reference(command_buffer, VK_STENCIL_FACE_BACK_BIT, draw->stencil_back); vk.set_blend_constants(command_buffer, draw->blend_factor.data()); diff --git a/runtime/test/d3d12_depth24_draw_test.cpp b/runtime/test/d3d12_depth24_draw_test.cpp index 9ff50b059d..6ae8bb0c6a 100644 --- a/runtime/test/d3d12_depth24_draw_test.cpp +++ b/runtime/test/d3d12_depth24_draw_test.cpp @@ -11,7 +11,8 @@ // slope-scaled bias (with and without DepthBiasClamp, within one 24-bit step: the slope is the GPU's), and a pixel // shader that writes its own depth (stored quantised, not biased). Each runs with and without a pixel shader. The // finding's cases run once more through a pass-through geometry shader without a pixel shader, which also gets the -// built-in depth stage. +// built-in depth stage. A DYNAMIC_DEPTH_BIAS pipeline (GCL9 RSSetDepthBias) then proves that the list's value, not +// the pipeline's, is the one applied. // // The shaders are GLSL written to the translator's contract (test/glsl/vs_depth.vert, ps_depth_empty.frag, // ps_depth_write.frag, gs_depth.geom). No game data. @@ -71,6 +72,10 @@ struct Pass { bool geometry_shader = false; // gs_depth between the VS and the (absent) pixel shader int bias = 0; float slope = 0.0f, clamp = 0.0f; + // DYNAMIC_DEPTH_BIAS: the bias, slope and clamp above go to RSSetDepthBias instead of the pipeline, which + // carries pso_bias (0 unless a case says otherwise), so the list value alone must produce the result. + bool dynamic_bias = false; + int pso_bias = 0; D3D12_COMPARISON_FUNC func = D3D12_COMPARISON_FUNC_ALWAYS; bool write = true; float clear = 1.0f; @@ -93,6 +98,7 @@ struct Harness { Ref queue; Ref allocator; Ref list; + Ref list9; Ref fence; UINT64 fence_value = 0; @@ -109,9 +115,10 @@ struct Harness { desc.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID; desc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE; desc.RasterizerState.DepthClipEnable = TRUE; - desc.RasterizerState.DepthBias = p.bias; - desc.RasterizerState.SlopeScaledDepthBias = p.slope; - desc.RasterizerState.DepthBiasClamp = p.clamp; + desc.RasterizerState.DepthBias = p.dynamic_bias ? p.pso_bias : p.bias; + desc.RasterizerState.SlopeScaledDepthBias = p.dynamic_bias ? 0.0f : p.slope; + desc.RasterizerState.DepthBiasClamp = p.dynamic_bias ? 0.0f : p.clamp; + if (p.dynamic_bias) desc.Flags = D3D12_PIPELINE_STATE_FLAG_DYNAMIC_DEPTH_BIAS; desc.DepthStencilState.DepthEnable = TRUE; desc.DepthStencilState.DepthWriteMask = p.write ? D3D12_DEPTH_WRITE_MASK_ALL : D3D12_DEPTH_WRITE_MASK_ZERO; desc.DepthStencilState.DepthFunc = p.func; @@ -138,6 +145,7 @@ struct Harness { list->ResourceBarrier(1, &to_write); list->ClearDepthStencilView(dsv, D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL, p.clear, 0, 0, nullptr); list->SetGraphicsRootSignature(root); + if (p.dynamic_bias) list9->RSSetDepthBias(float(p.bias), p.clamp, p.slope); const std::uint32_t constants[3] = {f2u(p.z), f2u(p.dz), f2u(p.shader_z)}; list->SetGraphicsRoot32BitConstants(0, 3, constants, 0); list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); @@ -200,7 +208,8 @@ std::string describe(const Pass& p) { : p.pixel_shader ? "with PS" : p.geometry_shader ? "GS, no PS" : "no PS"; - std::snprintf(text, sizeof(text), "%s, bias %d, slope %g, clamp %g", stages, p.bias, p.slope, p.clamp); + std::snprintf(text, sizeof(text), "%s, bias %d, slope %g, clamp %g%s", stages, p.bias, p.slope, p.clamp, + p.dynamic_bias ? " (dynamic)" : ""); return text; } @@ -317,6 +326,7 @@ int main() { EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&h.allocator.p)))); EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, h.allocator.p, nullptr, IID_PPV_ARGS(&h.list.p)))); + EXPECT(SUCCEEDED(h.list->QueryInterface(IID_PPV_ARGS(&h.list9.p)))); EXPECT(SUCCEEDED(h.list->Close())); EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&h.fence.p)))); @@ -391,6 +401,33 @@ int main() { " of " + std::to_string(all) + " differ from code " + std::to_string(expected) + " (first " + std::to_string(r.depth[0]) + ")"); } + // DYNAMIC_DEPTH_BIAS (GCL9 RSSetDepthBias): the list value replaces the pipeline's. First with a pipeline + // that carries bias 5 and a list bias of 0, then the other way round: each side alone would give a + // different code, so the pair proves whose value is used. + if (mode == 0) { + for (const auto& [pso_bias, list_bias] : {std::pair{5, 0}, {0, 3}, {0, -5}}) { + Pass p; + p.dynamic_bias = true; + p.pso_bias = pso_bias; + p.bias = list_bias; + p.z = leonida::depth24_to_float(5033000); + Result r; + std::string error; + EXPECT(h.run(p, r, error) || (std::fprintf(stderr, "%s\n", error.c_str()), false)); + const std::uint32_t expected = leonida::depth24_quantize(p.z + d3d_bias(list_bias, 0, 0, 0)); + int wrong = 0; + for (std::uint32_t c : r.depth) wrong += c != expected; + if (native && list_bias != 0 && wrong) { + std::printf("KNOWN stored depth, dynamic, %s: native D24 bias not exact (code %u, D3D gives " + "%u)\n", + describe(p).c_str(), r.depth[0], expected); + continue; + } + check(wrong == 0, "stored depth, dynamic bias (pipeline " + std::to_string(pso_bias) + ", list " + + std::to_string(list_bias) + "): " + std::to_string(wrong) + " of " + + std::to_string(all) + " differ from code " + std::to_string(expected)); + } + } // Stored depth, sloped: z = 0.5 + 0.25 * x_clip, so 1/128 per pixel in x. Within one 24-bit step without a // slope term; with one, within two: on the D32 path the slope comes from the derivatives of the // interpolated depth (a few float ulps), where D24 hardware uses the triangle's plane. diff --git a/runtime/test/d3d12_draw_test.cpp b/runtime/test/d3d12_draw_test.cpp index ffbfbf718d..f2fcac2370 100644 --- a/runtime/test/d3d12_draw_test.cpp +++ b/runtime/test/d3d12_draw_test.cpp @@ -165,6 +165,7 @@ int main(int argc, char** argv) { constexpr UINT size = 64; std::vector pixels(size * 256), second_pixels(size * 256), logic_pixels(size * 256); std::vector strip16_pixels(size * 256), strip32_pixels(size * 256), strip_nocut_pixels(size * 256); +std::vector strip_dyn16_pixels(size * 256), strip_dyn32_pixels(size * 256); std::vector msaa_full_pixels(size * 256), msaa_half_pixels(size * 256); bool ran_msaa = false; std::vector step2_pixels(size * 256), step1_pixels(size * 256); @@ -443,11 +444,13 @@ int main(int argc, char** argv) { Ref queue; Ref allocator; Ref list; + Ref list9; Ref fence; EXPECT(SUCCEEDED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p)))); EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p)))); EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, pso.p, IID_PPV_ARGS(&list.p)))); + EXPECT(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list9.p)))); EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))); // Upload the vertices, prepare the target. @@ -692,6 +695,11 @@ int main(int argc, char** argv) { EXPECT(SUCCEEDED(device->CreateGraphicsPipelineState(&strip_desc, IID_PPV_ARGS(&strip32_pso.p)))); strip_desc.IBStripCutValue = D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_DISABLED; EXPECT(SUCCEEDED(device->CreateGraphicsPipelineState(&strip_desc, IID_PPV_ARGS(&strip_off_pso.p)))); + // DYNAMIC_INDEX_BUFFER_STRIP_CUT: the pipeline keeps the value DISABLED and the list supplies it. + Ref strip_dyn_pso; + strip_desc.Flags = D3D12_PIPELINE_STATE_FLAG_DYNAMIC_INDEX_BUFFER_STRIP_CUT; + EXPECT(SUCCEEDED(device->CreateGraphicsPipelineState(&strip_desc, IID_PPV_ARGS(&strip_dyn_pso.p)))); + strip_desc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE; const D3D12_VERTEX_BUFFER_VIEW strip_vbv{upload_va + 1024, sizeof(strip_verts), sizeof(Vertex)}; struct StripPass { @@ -699,11 +707,20 @@ int main(int argc, char** argv) { D3D12_INDEX_BUFFER_VIEW ibv; UINT index_count; std::vector* out; + D3D12_INDEX_BUFFER_STRIP_CUT_VALUE dynamic_cut; // DISABLED: the pipeline's own value applies }; const StripPass strip_passes[] = { - {strip16_pso.p, {upload_va + 1280, sizeof(strip_idx16), DXGI_FORMAT_R16_UINT}, 9, &strip16_pixels}, - {strip32_pso.p, {upload_va + 1536, sizeof(strip_idx32), DXGI_FORMAT_R32_UINT}, 9, &strip32_pixels}, - {strip_off_pso.p, {upload_va + 1792, sizeof(strip_nocut), DXGI_FORMAT_R16_UINT}, 8, &strip_nocut_pixels}, + {strip16_pso.p, {upload_va + 1280, sizeof(strip_idx16), DXGI_FORMAT_R16_UINT}, 9, &strip16_pixels, + D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_DISABLED}, + {strip32_pso.p, {upload_va + 1536, sizeof(strip_idx32), DXGI_FORMAT_R32_UINT}, 9, &strip32_pixels, + D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_DISABLED}, + {strip_off_pso.p, {upload_va + 1792, sizeof(strip_nocut), DXGI_FORMAT_R16_UINT}, 8, &strip_nocut_pixels, + D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_DISABLED}, + // The dynamic pair must match the two PSO-driven ones pixel for pixel. + {strip_dyn_pso.p, {upload_va + 1280, sizeof(strip_idx16), DXGI_FORMAT_R16_UINT}, 9, + &strip_dyn16_pixels, D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFF}, + {strip_dyn_pso.p, {upload_va + 1536, sizeof(strip_idx32), DXGI_FORMAT_R32_UINT}, 9, + &strip_dyn32_pixels, D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFFFFFF}, }; UINT64 strip_fence = 10; for (const auto& sp : strip_passes) { @@ -714,6 +731,8 @@ int main(int argc, char** argv) { list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); list->IASetVertexBuffers(0, 1, &strip_vbv); list->IASetIndexBuffer(&sp.ibv); + if (sp.dynamic_cut != D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_DISABLED) + list9->IASetIndexBufferStripCutValue(sp.dynamic_cut); list->RSSetViewports(1, &viewport); list->RSSetScissorRects(1, &scissor); list->OMSetRenderTargets(1, &rtv, FALSE, nullptr); @@ -1284,6 +1303,7 @@ int main(int argc, char** argv) { root.reset(); rtv_heap.reset(); list.reset(); + list9.reset(); allocator.reset(); queue.reset(); fence.reset(); @@ -1356,7 +1376,7 @@ int main(int argc, char** argv) { const bool top = row < size / 2, left = col < size / 2; using Pixel = std::array; const Pixel want = (top && left) || (!top && !left) ? Pixel{255, 0, 0, 255} : Pixel{0, 0, 255, 255}; - for (const auto& buf : {strip16_pixels, strip32_pixels}) { + for (const auto& buf : {strip16_pixels, strip32_pixels, strip_dyn16_pixels, strip_dyn32_pixels}) { const std::uint8_t* got = buf.data() + row * 256 + col * 4; if (!std::equal(want.begin(), want.end(), got) && mismatches++ < 6) std::fprintf(stderr, "strip-cut pixel (%u, %u): got %u,%u,%u,%u want %u,%u,%u,%u\n", col, row, @@ -1370,8 +1390,8 @@ int main(int argc, char** argv) { } EXPECT(!mismatches); EXPECT(nocut_bl_red > 0); - std::printf("strip cut (0xFFFF & 0xFFFFFFFF): 4096/4096 pixels matched each (no-cut negative control painted %u " - "bridge pixels in bottom-left)\n", + std::printf("strip cut (0xFFFF & 0xFFFFFFFF, each also through IASetIndexBufferStripCutValue): 4096/4096 pixels " + "matched each (no-cut negative control painted %u bridge pixels in bottom-left)\n", nocut_bl_red); if (ran_msaa) { UINT half_changed = 0; diff --git a/runtime/test/d3d12_gcl8_test.cpp b/runtime/test/d3d12_gcl8_test.cpp index 722f46f54b..0ca43097a6 100644 --- a/runtime/test/d3d12_gcl8_test.cpp +++ b/runtime/test/d3d12_gcl8_test.cpp @@ -1,9 +1,11 @@ -// ID3D12GraphicsCommandList8 on the Vulkan-backed COM surface: QueryInterface for the public IID, its one method -// with two UINT parameters, the values the layer records, and the reference count. Creating an allocator and a -// command list needs neither a GPU nor the Vulkan loader, so this runs as a plain CPU test. +// ID3D12GraphicsCommandList8 and 9 on the Vulkan-backed COM surface: QueryInterface for the public IIDs, their +// methods with the values the layer records, and the reference count. Creating an allocator and a command list needs +// neither a GPU nor the Vulkan loader, so this runs as a plain CPU test. // -// The method is recorded but not applied: the backend has no pipeline or draw path that could consume per-face -// stencil references yet (docs/d3d12_vulkan.md section 3.2). +// The stencil references are recorded but not applied: the backend has no pipeline or draw path that could consume +// per-face stencil references yet (docs/d3d12_vulkan.md section 3.2). The GCL9 depth bias and strip-cut value are +// list state, applied only by a pipeline carrying the matching DYNAMIC_* flag (d3d12_depth24_draw_test and +// d3d12_draw_test draw with them). #include "../shims/d3d12_com.h" #include "../shims/d3d12_trace.h" @@ -82,6 +84,31 @@ int main(int argc, char** argv) { EXPECT(SUCCEEDED(list7.p->Reset(allocator.p, nullptr))); EXPECT(front_back_stencil_ref(list8.p, front, back)); EXPECT(front == 0 && back == 0); // Reset returns list state to its default + + // GCL9: the depth bias and the strip-cut value are list state, at slots 82 and 83 (after GCL8's 81). + Ref list9; + EXPECT(SUCCEEDED(list7.p->QueryInterface(IID_PPV_ARGS(&list9.p)))); + EXPECT(static_cast(list9.p) == static_cast(list7.p)); + using BiasFn = void(STDMETHODCALLTYPE*)(ID3D12GraphicsCommandList9*, FLOAT, FLOAT, FLOAT); + using CutFn = void(STDMETHODCALLTYPE*)(ID3D12GraphicsCommandList9*, D3D12_INDEX_BUFFER_STRIP_CUT_VALUE); + void** vtable9 = *reinterpret_cast(list9.p); + reinterpret_cast(vtable9[82])(list9.p, 2.5f, 0.25f, -1.5f); + reinterpret_cast(vtable9[83])(list9.p, D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFFFFFF); + float constant = 0.0f, clamp = 0.0f, slope = 0.0f; + std::uint32_t cut = 0; + EXPECT(depth_bias_state(list9.p, constant, clamp, slope)); + EXPECT(constant == 2.5f && clamp == 0.25f && slope == -1.5f); + EXPECT(strip_cut_value(list9.p, cut) && cut == D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFFFFFF); + EXPECT(list9.p->Close() == S_OK); // Reset takes only a closed list + EXPECT(SUCCEEDED(list7.p->Reset(allocator.p, nullptr))); + EXPECT(depth_bias_state(list9.p, constant, clamp, slope)); + EXPECT(constant == 0.0f && clamp == 0.0f && slope == 0.0f); + EXPECT(strip_cut_value(list9.p, cut) && cut == D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_DISABLED); + + // A value outside the enum makes Close report the invalid call, as D3D12's own validation does. + reinterpret_cast(vtable9[83])(list9.p, static_cast(7)); + EXPECT(list9.p->Close() == E_INVALIDARG); + EXPECT(SUCCEEDED(list7.p->Reset(allocator.p, nullptr))); // leave the list open for the calls below } EXPECT(list7.p->AddRef() == held); // QueryInterface released its reference list7.p->Release(); @@ -98,6 +125,7 @@ int main(int argc, char** argv) { UINT ignored_front = 0, ignored_back = 0; EXPECT(!front_back_stencil_ref(nullptr, ignored_front, ignored_back)); - std::printf("GCL8: QueryInterface, OMSetFrontAndBackStencilRef(1, 2), refcount balanced\n"); + std::printf("GCL8/9: QueryInterface, OMSetFrontAndBackStencilRef, RSSetDepthBias, IASetIndexBufferStripCutValue, " + "refcount balanced\n"); return 0; } From 030186216d530bd70b1628c5901909166e0ab17a Mon Sep 17 00:00:00 2001 From: Snooozs <0randale196@gmail.com> Date: Mon, 23 Nov 2026 19:57:49 +0100 Subject: [PATCH 2/3] d3d12_pso_validation_test: compare the DYNAMIC_* PSO flags with the host only where it reports them On Windows 10 22H2 (19045) the in-box D3D12 runtime refuses D3D12_PIPELINE_STATE_FLAG_DYNAMIC_DEPTH_BIAS and DYNAMIC_INDEX_BUFFER_STRIP_CUT with E_INVALIDARG and reports OPTIONS16.DynamicDepthBiasSupported and OPTIONS15.DynamicIndexBufferStripCutSupported as FALSE, so the test failed there: FAIL [gfx: Flags=DYNAMIC_DEPTH_BIAS]: layer 0x00000000 != host NVIDIA GeForce RTX 5060 Ti 0x80070057 Query both features on each host device and compare those four cases (gfx and compute) only on hosts that report the feature, with a SKIP line otherwise. The layer-side expectation still runs on every host. Co-Authored-By: Claude Opus 5.5 --- runtime/test/d3d12_pso_validation_test.cpp | 57 +++++++++++++++++----- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/runtime/test/d3d12_pso_validation_test.cpp b/runtime/test/d3d12_pso_validation_test.cpp index 89bee7bd26..1f3c4768e4 100644 --- a/runtime/test/d3d12_pso_validation_test.cpp +++ b/runtime/test/d3d12_pso_validation_test.cpp @@ -36,6 +36,9 @@ struct HostContext { std::string name; Ref device; Ref root; + // Feature answers on this host. Windows 10 reports both FALSE and refuses the matching PSO flags. + bool dynamic_depth_bias = false; // OPTIONS16.DynamicDepthBiasSupported + bool dynamic_index_buffer_strip_cut = false; // OPTIONS15.DynamicIndexBufferStripCutSupported D3D12_SHADER_BYTECODE vs_fullscreen{}; D3D12_SHADER_BYTECODE vs_pos{}; D3D12_SHADER_BYTECODE ps_unorm{}; @@ -172,6 +175,24 @@ int main() { hc->ps_unorm = {hlsl_ps_unorm->GetBufferPointer(), hlsl_ps_unorm->GetBufferSize()}; hc->ps_uint = {hlsl_ps_uint->GetBufferPointer(), hlsl_ps_uint->GetBufferSize()}; hc->cs = {hlsl_cs->GetBufferPointer(), hlsl_cs->GetBufferSize()}; + D3D12_FEATURE_DATA_D3D12_OPTIONS16 host16{}; + hc->dynamic_depth_bias = + SUCCEEDED(hc->device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS16, &host16, + sizeof(host16))) && + host16.DynamicDepthBiasSupported; + D3D12_FEATURE_DATA_D3D12_OPTIONS15 host15{}; + hc->dynamic_index_buffer_strip_cut = + SUCCEEDED(hc->device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS15, &host15, + sizeof(host15))) && + host15.DynamicIndexBufferStripCutSupported; + if (!hc->dynamic_depth_bias) + std::printf("SKIP host comparison of DYNAMIC_DEPTH_BIAS cases on %s: OPTIONS16." + "DynamicDepthBiasSupported is FALSE there (layer-side checks still run)\n", + name.c_str()); + if (!hc->dynamic_index_buffer_strip_cut) + std::printf("SKIP host comparison of DYNAMIC_INDEX_BUFFER_STRIP_CUT cases on %s: OPTIONS15." + "DynamicIndexBufferStripCutSupported is FALSE there (layer-side checks still run)\n", + name.c_str()); hosts.push_back(std::move(hc)); } } @@ -204,8 +225,10 @@ int main() { unsigned checked = 0, host_compared = 0; - auto check_gfx_parity = [&](const char* label, HRESULT expected, bool use_pos_vs, bool use_uint_ps, - auto mutator) -> bool { + // need: a HostContext feature answer the case depends on (nullptr for none). The host is compared only when + // it reports that feature; the layer-side expectation is checked on every run. + auto check_gfx_parity_gated = [&](const char* label, HRESULT expected, bool use_pos_vs, bool use_uint_ps, + bool HostContext::*need, auto mutator) -> bool { auto desc = make_gfx(layer_root.p, use_pos_vs ? layer_vs_pos : layer_vs_fs, use_uint_ps ? layer_ps_uint : layer_ps_unorm); if (use_uint_ps) desc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UINT; @@ -219,6 +242,7 @@ int main() { ++checked; for (const auto& hcp : hosts) { const HostContext& hc = *hcp; + if (need && !(hc.*need)) continue; auto hdesc = make_gfx(hc.root.p, use_pos_vs ? hc.vs_pos : hc.vs_fullscreen, use_uint_ps ? hc.ps_uint : hc.ps_unorm); if (use_uint_ps) hdesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UINT; @@ -233,6 +257,10 @@ int main() { } return true; }; + auto check_gfx_parity = [&](const char* label, HRESULT expected, bool use_pos_vs, bool use_uint_ps, + auto mutator) -> bool { + return check_gfx_parity_gated(label, expected, use_pos_vs, use_uint_ps, nullptr, mutator); + }; auto check_gfx_layer_only = [&](const char* label, HRESULT expected, auto mutator) -> bool { auto desc = make_gfx(layer_root.p, layer_vs_fs, layer_ps_unorm); @@ -247,7 +275,7 @@ int main() { return true; }; - auto check_cs_parity = [&](const char* label, HRESULT expected, auto mutator) -> bool { + auto check_cs_parity_gated = [&](const char* label, HRESULT expected, bool HostContext::*need, auto mutator) -> bool { D3D12_COMPUTE_PIPELINE_STATE_DESC desc{}; desc.pRootSignature = layer_root.p; desc.CS = layer_cs; @@ -261,6 +289,7 @@ int main() { ++checked; for (const auto& hcp : hosts) { const HostContext& hc = *hcp; + if (need && !(hc.*need)) continue; D3D12_COMPUTE_PIPELINE_STATE_DESC hdesc{}; hdesc.pRootSignature = hc.root.p; hdesc.CS = hc.cs; @@ -275,6 +304,9 @@ int main() { } return true; }; + auto check_cs_parity = [&](const char* label, HRESULT expected, auto mutator) -> bool { + return check_cs_parity_gated(label, expected, nullptr, mutator); + }; // 1. Baseline & null pInputElementDescs crash guards. EXPECT(check_gfx_parity("gfx: baseline", S_OK, false, false, [](auto&) {})); @@ -432,10 +464,12 @@ int main() { EXPECT(check_gfx_parity("gfx: NodeMask=3", E_INVALIDARG, false, false, [](auto& d) { d.NodeMask = 3; })); EXPECT(check_gfx_parity("gfx: NodeMask=0x80000000", E_INVALIDARG, false, false, [](auto& d) { d.NodeMask = 0x80000000u; })); - EXPECT(check_gfx_parity("gfx: Flags=DYNAMIC_DEPTH_BIAS", S_OK, false, false, - [](auto& d) { d.Flags = D3D12_PIPELINE_STATE_FLAG_DYNAMIC_DEPTH_BIAS; })); - EXPECT(check_gfx_parity("gfx: Flags=DYNAMIC_INDEX_BUFFER_STRIP_CUT", S_OK, false, false, - [](auto& d) { d.Flags = D3D12_PIPELINE_STATE_FLAG_DYNAMIC_INDEX_BUFFER_STRIP_CUT; })); + EXPECT(check_gfx_parity_gated("gfx: Flags=DYNAMIC_DEPTH_BIAS", S_OK, false, false, + &HostContext::dynamic_depth_bias, + [](auto& d) { d.Flags = D3D12_PIPELINE_STATE_FLAG_DYNAMIC_DEPTH_BIAS; })); + EXPECT(check_gfx_parity_gated("gfx: Flags=DYNAMIC_INDEX_BUFFER_STRIP_CUT", S_OK, false, false, + &HostContext::dynamic_index_buffer_strip_cut, + [](auto& d) { d.Flags = D3D12_PIPELINE_STATE_FLAG_DYNAMIC_INDEX_BUFFER_STRIP_CUT; })); EXPECT(check_gfx_parity("gfx: Flags=0x10 (unknown bit)", E_INVALIDARG, false, false, [](auto& d) { d.Flags = static_cast(0x10); })); EXPECT(check_gfx_parity("gfx: Flags=0x80000000 (unknown bit)", E_INVALIDARG, false, false, @@ -449,10 +483,11 @@ int main() { EXPECT(check_cs_parity("cs: NodeMask=2", E_INVALIDARG, [](auto& d) { d.NodeMask = 2; })); EXPECT(check_cs_parity("cs: NodeMask=3", E_INVALIDARG, [](auto& d) { d.NodeMask = 3; })); EXPECT(check_cs_parity("cs: NodeMask=0x80000000", E_INVALIDARG, [](auto& d) { d.NodeMask = 0x80000000u; })); - EXPECT(check_cs_parity("cs: Flags=DYNAMIC_DEPTH_BIAS", S_OK, - [](auto& d) { d.Flags = D3D12_PIPELINE_STATE_FLAG_DYNAMIC_DEPTH_BIAS; })); - EXPECT(check_cs_parity("cs: Flags=DYNAMIC_INDEX_BUFFER_STRIP_CUT", S_OK, - [](auto& d) { d.Flags = D3D12_PIPELINE_STATE_FLAG_DYNAMIC_INDEX_BUFFER_STRIP_CUT; })); + EXPECT(check_cs_parity_gated("cs: Flags=DYNAMIC_DEPTH_BIAS", S_OK, &HostContext::dynamic_depth_bias, + [](auto& d) { d.Flags = D3D12_PIPELINE_STATE_FLAG_DYNAMIC_DEPTH_BIAS; })); + EXPECT(check_cs_parity_gated("cs: Flags=DYNAMIC_INDEX_BUFFER_STRIP_CUT", S_OK, + &HostContext::dynamic_index_buffer_strip_cut, + [](auto& d) { d.Flags = D3D12_PIPELINE_STATE_FLAG_DYNAMIC_INDEX_BUFFER_STRIP_CUT; })); EXPECT(check_cs_parity("cs: Flags=0x10 (unknown bit)", E_INVALIDARG, [](auto& d) { d.Flags = static_cast(0x10); })); EXPECT(check_cs_parity("cs: Flags=0x80000000 (unknown bit)", E_INVALIDARG, From cc6597c958b70fb67d13ced3d65ce571ca903cfb Mon Sep 17 00:00:00 2001 From: robot00f Date: Tue, 24 Nov 2026 19:55:39 -0300 Subject: [PATCH 3/3] d3d12_pso_validation_test: the layer answers both dynamic-state features --- runtime/test/d3d12_pso_validation_test.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/runtime/test/d3d12_pso_validation_test.cpp b/runtime/test/d3d12_pso_validation_test.cpp index 1f3c4768e4..bbfef7b926 100644 --- a/runtime/test/d3d12_pso_validation_test.cpp +++ b/runtime/test/d3d12_pso_validation_test.cpp @@ -470,6 +470,15 @@ int main() { EXPECT(check_gfx_parity_gated("gfx: Flags=DYNAMIC_INDEX_BUFFER_STRIP_CUT", S_OK, false, false, &HostContext::dynamic_index_buffer_strip_cut, [](auto& d) { d.Flags = D3D12_PIPELINE_STATE_FLAG_DYNAMIC_INDEX_BUFFER_STRIP_CUT; })); + // The layer reports both features and implements the GCL9 setters, so it takes the two flags whatever the host + // does: the gated comparisons above are skipped on a host without them, this one never is. + { + D3D12_FEATURE_DATA_D3D12_OPTIONS16 layer16{}; + D3D12_FEATURE_DATA_D3D12_OPTIONS15 layer15{}; + EXPECT(SUCCEEDED(layer_dev->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS16, &layer16, sizeof(layer16)))); + EXPECT(SUCCEEDED(layer_dev->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS15, &layer15, sizeof(layer15)))); + EXPECT(layer16.DynamicDepthBiasSupported && layer15.DynamicIndexBufferStripCutSupported); + } EXPECT(check_gfx_parity("gfx: Flags=0x10 (unknown bit)", E_INVALIDARG, false, false, [](auto& d) { d.Flags = static_cast(0x10); })); EXPECT(check_gfx_parity("gfx: Flags=0x80000000 (unknown bit)", E_INVALIDARG, false, false,