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
24 changes: 20 additions & 4 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -514,17 +514,29 @@ What is implemented (all of it testable without the game):
operation inherits the graphics state of its command list. The Vulkan replay snapshots the same pipeline, root
data, render targets, IA bindings and raster state as a direct draw, then reads the records on the GPU inside
that draw's rendering scope. A padded stride is supported; it is not evidence that a signature is simple.
A compound graphics signature still emits no draw, but its written VB/IB and root bindings are reset for the
next command, even if no record is interpreted. Untouched slots and previously recorded draws retain their state.
A compound graphics signature (root or IA arguments before its DRAW) is expanded on the CPU into the calls its
records stand for: each record's vertex buffer views, index buffer view, root constants and root CBVs are
applied in the signature's argument order, then its DRAW or DRAW_INDEXED is recorded, so the draw reads the
bindings the argument buffer names instead of the ones the list had bound. The expansion needs the record bytes
on the CPU, so only a buffer whose memory the CPU can map (an upload or readback heap in practice) is replayed,
for the argument bytes and for the count buffer's DWORD alike. Those bytes are read when the call is *recorded*,
not when the list executes, so an application that rewrites the argument buffer between recording and execution -
a ring buffer filled just before `ExecuteCommandLists`, or a list reused with a new buffer - replays the records
that were there at record time; a count the GPU writes (a `CopyBufferRegion` into the count buffer, say) is not
seen, and the call replays the count that was there when it was recorded. Keeping the buffer referenced and expanding at
replay would match the runtime's timing; until then, fill the buffer before recording. A device-local
buffer, which the GPU may have written, still draws nothing. In both cases every binding the signature writes is
reset after the call, even if no record is interpreted, as the D3D12 runtime does; untouched slots and previously
recorded draws retain their state.
See the [D3D12 state-leakage contract](https://microsoft.github.io/DirectX-Specs/d3d/IndirectDrawing.html#state-leakage).
Fixed- and GPU-counted batches use the device's enabled indirect features and limits. The current graphics
path covers triangle lists on direct command lists. A bundle can defer a single operation with no count
buffer: its argument-buffer identity and byte offset are recorded once; each `ExecuteBundle` captures the
direct parent's state at that call. Count buffers and compound signatures remain invalid in bundles.
`d3d12_bundle_indirect_warp` and `d3d12_bundle_indirect_layer` compare DRAW and DRAW_INDEXED against direct
pixels with two different parent bindings; the layer fixture also checks marker order and captured views.
Compound graphics signatures, non-unit instance divisors
and unsupported capabilities stay guarded. A GPU-counted draw currently requires
A compound graphics signature whose record bytes are not host-mappable stays guarded, as do
non-unit instance divisors and unsupported capabilities. A GPU-counted draw currently requires
`maxDrawIndirectCount == UINT32_MAX`: Vulkan also limits the raw counter value, not just the caller's maximum.
To omit indirect graphics without changing direct drawing or compute, set
`LEONIDA_INDIRECT_DRAWS=off` before starting the process. This applies to the Vulkan-backed D3D12
Expand Down Expand Up @@ -1304,6 +1316,9 @@ and any validation error fails them.
`d3d12_stateful_indirect_counted_pixels`: positive D3D12-to-Vulkan graphics tests. Compare every readback
pixel with both a direct-draw reference and a known pattern for each draw kind; the counted variant fills
its argument and count buffers by GPU copy before use.
- `d3d12_compound_indirect_pixels`: a compound signature whose two records carry two vertex buffer views, the
index buffer view, two root constants and a root CBV each. The picture must match the same calls made by hand
and must not match the state the list had bound before the indirect call.
- `d3d12_indirect_rt_test`: validates `ID3D12Device::CreateCommandSignature` + `ExecuteIndirect` with a GPU
`CountBuffer` (`vkCmdDrawIndirectCount`, `count = 0` skipped and `count = 1` rendered), a draw whose record asks
for four billion instances (clamped on the GPU, section 4.7), and a native Vulkan counted-draw reference over the
Expand Down Expand Up @@ -5340,3 +5355,4 @@ The public Xbox graphics index retrieved for this document lists interfaces up t
`ID3D12GraphicsCommandList7`; the shipped DLL names `Device11` and `CommandList8`, which the index does not cover.
This is why the layer targets the Windows SDK's newer public chain rather than assuming the Xbox public index is
complete.

5 changes: 4 additions & 1 deletion runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3999,13 +3999,16 @@ if(TARGET Vulkan::Headers)
add_test(NAME d3d12_stateful_indirect_pixels COMMAND d3d12_draw_test --stateful-indirect-pixels)
add_test(NAME d3d12_stateful_indirect_multi_pixels COMMAND d3d12_draw_test --stateful-indirect-multi-pixels)
add_test(NAME d3d12_stateful_indirect_counted_pixels COMMAND d3d12_draw_test --stateful-indirect-counted-pixels)
# A compound signature whose records carry their own state (two vertex buffer views, the index buffer, root
# constants and a root CBV): the picture must come from the argument buffer, not from what the list had bound.
add_test(NAME d3d12_compound_indirect_pixels COMMAND d3d12_draw_test --compound-indirect-pixels)
# A device without robust indexed views can still validate each ordinary DRAW pixel oracle.
add_test(NAME d3d12_stateful_indirect_nonindexed_pixels COMMAND d3d12_draw_test --stateful-indirect-pixels --nonindexed-only)
add_test(NAME d3d12_stateful_indirect_nonindexed_multi_pixels COMMAND d3d12_draw_test --stateful-indirect-multi-pixels --nonindexed-only)
add_test(NAME d3d12_stateful_indirect_nonindexed_counted_pixels COMMAND d3d12_draw_test --stateful-indirect-counted-pixels --nonindexed-only)
set_tests_properties(d3d12_stateful_indirect_pixels d3d12_stateful_indirect_multi_pixels d3d12_stateful_indirect_counted_pixels
d3d12_stateful_indirect_nonindexed_pixels d3d12_stateful_indirect_nonindexed_multi_pixels
d3d12_stateful_indirect_nonindexed_counted_pixels PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
d3d12_stateful_indirect_nonindexed_counted_pixels d3d12_compound_indirect_pixels PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
add_test(NAME d3d12_title_index_window COMMAND d3d12_draw_test --title-index-window)
add_test(NAME d3d12_title_vertex_window COMMAND d3d12_draw_test --title-vertex-window)
# LEONIDA_STUB_PIPELINES: a pipeline with no translated shader is a no-op stub when the switch is on.
Expand Down
133 changes: 130 additions & 3 deletions runtime/shims/d3d12_com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1036,9 +1036,17 @@ class GraphicsCommandList final
}
if (!terminal_only && (kind == leonida::IndirectKind::draw || kind == leonida::IndirectKind::draw_indexed)) {
if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false);
// Compound draw emission is not yet supported. Reset the written bindings even if no
// records execute. Bundle nesting and prefixes were already rejected above for bundles.
leonida::indirect_stats::skip(leonida::indirect_stats::Event::record_skip, leonida::indirect_stats::compound);
// A compound graphics signature binds state per record (vertex and index buffer views, root constants,
// root views) before its DRAW. Vulkan has no indirect call that rebinds state, so the record list is
// expanded into the calls it stands for (docs/d3d12_vulkan.md 4.7). Only a host-visible argument and
// count buffer can be expanded: a device-local one may be GPU-written, and that list keeps the
// reset-only path. Bundle nesting and prefixes were already rejected above for bundles.
if (!expand_compound_indirect(*sig, max_command_count, argument_buffer, argument_buffer_offset,
count_buffer, count_buffer_offset))
leonida::indirect_stats::skip(leonida::indirect_stats::Event::record_skip,
leonida::indirect_stats::compound);
// State leakage, as the D3D12 runtime does it: every binding the signature writes is reset after the
// call, even when no record ran.
if (!list_.closed()) reset_indirect_graphics_bindings(sig->graphics_writes());
return;
}
Expand Down Expand Up @@ -1820,6 +1828,125 @@ class GraphicsCommandList final
set_pipeline(state);
}

// A compound graphics signature writes state in every record and then draws with it. Neither Vulkan's indirect
// draws nor the replay's per-draw state snapshot can carry that, so each record is expanded here into the state
// calls the application could have made and the draw they precede; the record layout is packed in the signature's
// argument order (D3D12 Indirect Drawing, "Indirect Argument Buffer Structures"). Expansion needs the bytes on the
// CPU, which only a host-visible argument and count buffer give. False means no record was expanded: the caller
// keeps the reset-only path, which is all a device-local (potentially GPU-written) list can get.
bool expand_compound_indirect(const CommandSignature& sig, UINT max_command_count, ID3D12Resource* argument_buffer,
UINT64 argument_buffer_offset, ID3D12Resource* count_buffer,
UINT64 count_buffer_offset) {
const bool indexed = sig.kind() == leonida::IndirectKind::draw_indexed;
// The same prerequisites a direct draw needs; a record list that cannot draw records nothing.
if (!graphics_pipeline_ || !graphics_.layout || !topology_) return false;
void* base = nullptr;
if (FAILED(argument_buffer->Map(0, nullptr, &base)) || !base) return false;
// Every argument is checked before the first record is recorded, so a shape this code cannot decode leaves
// the list as it was instead of expanding part of it. A creation-valid signature passes all of these.
for (const auto& arg : sig.graphics_writes()) {
switch (arg.Type) {
case D3D12_INDIRECT_ARGUMENT_TYPE_VERTEX_BUFFER_VIEW:
if (arg.VertexBuffer.Slot >= vertex_buffers_.size()) return false;
break;
case D3D12_INDIRECT_ARGUMENT_TYPE_INDEX_BUFFER_VIEW:
case D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT_BUFFER_VIEW:
case D3D12_INDIRECT_ARGUMENT_TYPE_SHADER_RESOURCE_VIEW:
case D3D12_INDIRECT_ARGUMENT_TYPE_UNORDERED_ACCESS_VIEW:
case compat::kIndirectArgumentIncrementingConstant: break;
case D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT:
if (!arg.Constant.Num32BitValuesToSet || arg.Constant.Num32BitValuesToSet > D3D12_MAX_ROOT_COST)
return false;
break;
default: return false; // creation excludes every other non-terminal argument
}
}
UINT records = max_command_count;
if (count_buffer) {
void* counts = nullptr;
if (count_buffer_offset % sizeof(std::uint32_t) ||
count_buffer_offset + sizeof(std::uint32_t) > count_buffer->GetDesc().Width ||
FAILED(count_buffer->Map(0, nullptr, &counts)) || !counts)
return false;
std::uint32_t gpu_count = 0;
std::memcpy(&gpu_count, static_cast<const std::uint8_t*>(counts) + count_buffer_offset, sizeof gpu_count);
records = std::min(records, gpu_count); // D3D12Core: min(MaxCommandCount, the buffer's uint32)
}
const auto* base_bytes = static_cast<const std::uint8_t*>(base);
for (UINT record = 0; record < records; ++record) {
const std::uint8_t* cursor = base_bytes + argument_buffer_offset + std::uint64_t(record) * sig.stride();
const auto take = [&cursor](std::size_t bytes) {
const std::uint8_t* at = cursor;
cursor += bytes;
return at;
};
// Bounds are the caller's: packed_size <= stride and the last record's packed bytes fit the buffer.
for (const auto& arg : sig.graphics_writes()) {
switch (arg.Type) {
case D3D12_INDIRECT_ARGUMENT_TYPE_VERTEX_BUFFER_VIEW: {
D3D12_VERTEX_BUFFER_VIEW view{};
std::memcpy(&view, take(sizeof view), sizeof view);
set_vertex_buffers(arg.VertexBuffer.Slot, 1, &view);
break;
}
case D3D12_INDIRECT_ARGUMENT_TYPE_INDEX_BUFFER_VIEW: {
D3D12_INDEX_BUFFER_VIEW view{};
std::memcpy(&view, take(sizeof view), sizeof view);
set_index_buffer(&view);
break;
}
case D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT: {
const UINT count = arg.Constant.Num32BitValuesToSet;
std::array<std::uint32_t, D3D12_MAX_ROOT_COST> words{};
std::memcpy(words.data(), take(sizeof(std::uint32_t) * count), sizeof(std::uint32_t) * count);
constants(graphics_, arg.Constant.RootParameterIndex, count, words.data(),
arg.Constant.DestOffsetIn32BitValues);
break;
}
case compat::kIndirectArgumentIncrementingConstant: {
// Tier 1.1: the argument occupies no record bytes; the value is the record's index.
constants(graphics_, arg.Constant.RootParameterIndex, 1, &record,
arg.Constant.DestOffsetIn32BitValues);
break;
}
case D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT_BUFFER_VIEW:
case D3D12_INDIRECT_ARGUMENT_TYPE_SHADER_RESOURCE_VIEW:
case D3D12_INDIRECT_ARGUMENT_TYPE_UNORDERED_ACCESS_VIEW: {
D3D12_GPU_VIRTUAL_ADDRESS va = 0;
std::memcpy(&va, take(sizeof va), sizeof va);
const UINT index = arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT_BUFFER_VIEW
? arg.ConstantBufferView.RootParameterIndex
: arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_SHADER_RESOURCE_VIEW
? arg.ShaderResourceView.RootParameterIndex
: arg.UnorderedAccessView.RootParameterIndex;
address(graphics_, index, va, rt::rootsig::ParamType::Descriptor);
break;
}
default: return false; // creation excludes every other non-terminal argument
}
}
// The record's terminal operation, with the state above already in effect.
if (indexed) {
D3D12_DRAW_INDEXED_ARGUMENTS draw{};
std::memcpy(&draw, cursor, sizeof draw);
if (!(index_.va && index_.size &&
(index_.format == DXGI_FORMAT_R16_UINT || index_.format == DXGI_FORMAT_R32_UINT))) {
leonida::indirect_stats::skip(leonida::indirect_stats::Event::record_skip,
leonida::indirect_stats::index_view);
continue;
}
record_draw(true, draw.IndexCountPerInstance, draw.InstanceCount, draw.StartIndexLocation,
draw.BaseVertexLocation, draw.StartInstanceLocation);
} else {
D3D12_DRAW_ARGUMENTS draw{};
std::memcpy(&draw, cursor, sizeof draw);
record_draw(false, draw.VertexCountPerInstance, draw.InstanceCount, draw.StartVertexLocation, 0,
draw.StartInstanceLocation);
}
}
return true;
}

void reset_indirect_graphics_bindings(std::span<const D3D12_INDIRECT_ARGUMENT_DESC> writes) {
static constexpr std::array<UINT, D3D12_MAX_ROOT_COST> zeros{};
for (const auto& arg : writes) {
Expand Down
10 changes: 10 additions & 0 deletions runtime/test/d3d12_draw_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,17 @@ static_assert(offsetof(Vertex, wrong_color) == 16);
// Positive-only offscreen variant, sharing the translated-shader fixture, root signature and PSO description.
int check_stateful_indirect_pixels(ID3D12Device* device, ID3D12RootSignature* root, D3D12_GRAPHICS_PIPELINE_STATE_DESC desc,
bool multi, bool counted, bool nonindexed_only);
// The compound-signature variant: its records carry their own vertex and index buffer views and root arguments.
int check_compound_indirect_pixels(ID3D12Device* device, ID3D12RootSignature* root,
D3D12_GRAPHICS_PIPELINE_STATE_DESC desc);

int main(int argc, char** argv) {
const bool nonindexed_only = argc == 3 && std::strcmp(argv[2], "--nonindexed-only") == 0;
const bool indirect_mode = argc == 2 || nonindexed_only;
const bool multi_pixels = indirect_mode && std::strcmp(argv[1], "--stateful-indirect-multi-pixels") == 0;
const bool count_pixels = indirect_mode && std::strcmp(argv[1], "--stateful-indirect-counted-pixels") == 0;
// Compound signatures need no indirect draw feature: every record is expanded into ordinary draws.
const bool compound_pixels = indirect_mode && std::strcmp(argv[1], "--compound-indirect-pixels") == 0;
const bool indirect_pixels = multi_pixels || count_pixels ||
(indirect_mode && std::strcmp(argv[1], "--stateful-indirect-pixels") == 0);
if (argc > 2 && !(nonindexed_only && indirect_pixels)) return 1;
Expand Down Expand Up @@ -249,6 +254,11 @@ int main(int argc, char** argv) {
EXPECT(d.validation_errors() == 0);
return result;
}
if (compound_pixels) {
const int result = check_compound_indirect_pixels(device.p, root.p, pso_desc);
EXPECT(d.validation_errors() == 0);
return result;
}
Ref<ID3D12PipelineState> pso, unfed, mrt, instanced, refused, xor_pso, fullscreen;
EXPECT(SUCCEEDED(device->CreateGraphicsPipelineState(&pso_desc, IID_PPV_ARGS(&pso.p))));
pso_desc.InputLayout = {elements, 1}; // COLOR missing: D3D12 rejects a layout that leaves an input unfed
Expand Down
Loading
Loading