Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
77 changes: 72 additions & 5 deletions runtime/shims/d3d12_com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ class CommandAllocator final
};

class GraphicsCommandList final
: public Child<GraphicsCommandList, ID3D12GraphicsCommandList8Stubs, ID3D12Object, ID3D12DeviceChild,
: public Child<GraphicsCommandList, ID3D12GraphicsCommandList9Stubs, ID3D12Object, ID3D12DeviceChild,
ID3D12CommandList, ID3D12GraphicsCommandList, ID3D12GraphicsCommandList1,
ID3D12GraphicsCommandList2, ID3D12GraphicsCommandList3, ID3D12GraphicsCommandList4,
ID3D12GraphicsCommandList5, ID3D12GraphicsCommandList6, ID3D12GraphicsCommandList7,
ID3D12GraphicsCommandList8> {
ID3D12GraphicsCommandList8, ID3D12GraphicsCommandList9> {
public:
// {2f8a7c33-91d4-4b6e-8e05-6d3f1a9b2c47}
static constexpr GUID kTag{0x2f8a7c33, 0x91d4, 0x4b6e, {0x8e, 0x05, 0x6d, 0x3f, 0x1a, 0x9b, 0x2c, 0x47}};
Expand Down Expand Up @@ -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_; }

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<leonida::Command>& commands) {
const GraphicsCommandList* l = GraphicsCommandList::from(list);
if (!l) return false;
Expand Down
16 changes: 16 additions & 0 deletions runtime/shims/d3d12_com.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<leonida::SubgroupRequest>* 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.
Expand Down Expand Up @@ -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<leonida::Command>& commands);
Expand Down
Loading
Loading