Skip to content
Merged
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
28 changes: 23 additions & 5 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -1744,18 +1744,36 @@ The game can use D3D12 descriptors in three ways, and the layer must handle all
include PS_NoStripImmutableTable at s11 and VS_TransformD at s12. Their shader inputs establish the
destination dwords. The explicit graphics layouts and ordering for signatures with spills still need
independent root-signature evidence.
**Inline compute layout (integration #984).** Run
`python -I tools/compute_heap_layout_census.py <carve>/disasm` on each compute carve. Of 5,450 indexed compute
entries, 2,959 advertise direct indexing and none of those embeds its root signature. Adding
`--group fspx_compute --dispatch-log <dispatch-log>` joined seven resource-indexing entries to independently
recorded layouts. In all seven, a dynamically indexed resource descriptor used the first SGPR after the explicit
arguments. There were zero mismatches and zero cases without room **among those seven**. For the remaining
2,952 entries the layout is unknown. These are entries across carves, which can contain the same program.
No sampler-indexing entry had a matched layout. Putting the resource heap first when both flags are set, then the
sampler in the next dword, remains a hypothesis. D3D-329's adjacent SGPR examples alone do not establish it.
The runtime snapshots the bound resource and sampler heap pointers in that order after the explicit
arguments for direct and indirect compute dispatches with an inline signature. Set
arguments for direct and indirect compute dispatches with an inline signature. An unbound heap contributes
zero, and rebinding affects later commands. A heap that cannot fit in the inline user data is reported once,
and a signature whose compute arguments spill gets nothing written (logged once). Set
`LEONIDA_COMPUTE_DIRECT_HEAPS=0` to restore the previous compute arguments.
Draws do the same in the graphics layout (`pack_stage(Stage::Pixel)`, from s8, one dword per table): the
resource heap's 32-bit shader VA goes in the dword after the explicit arguments, then the sampler heap's, each
only when the signature has its flag, snapshotted per draw. Vertex, pixel and geometry shaders share one
user-data block and that layout, so one write serves all three; a signature whose explicit arguments take
s8..s10 gives s11, one whose arguments take s8..s11 gives s12 (the two program reads above). A mesh shader reads
its arguments from s16, which is not modelled, so in a mesh pipeline only the pixel shader gets the heaps. A
signature that keeps the s0 graphics layout, or whose arguments fill s31, gets none (logged once).
`d3d12_graphics_direct_heaps_test` checks the recorded draws and what a vertex and a pixel shader read, with
and without the flags. Set `LEONIDA_GRAPHICS_DIRECT_HEAPS=0` to restore the previous draw arguments.
push dword d as s(d + 8) through its root window (section 3.5), so `DispatchMesh` records the same dwords and
the mesh stage finds the resource heap at s(16 + end − 8), right after its arguments from s16, where DadaCap
reports every directly indexed program of the retail library reads it, for compute from s4, VS/PS/GS from s8
and mesh from s16 (issue #14, comment 7143020922, on a `boot-prototype` run; not reproduced in this
repository). A mesh signature that fills the window leaves no room there: a heap past s31 of the mesh shader
(push dword 24 or later) is logged once. A signature that keeps the s0 graphics layout, or whose arguments fill
s31, gets none (logged once). `d3d12_graphics_direct_heaps_test` checks the recorded draws and what a vertex
and a pixel shader read, with and without the flags; `d3d12_mesh_test` checks that a mesh draw records the
vertex draw's dwords, a resource-only rebind, a signature ending at s30 and one on the s0 layout (recorded
arguments only; no GPU read through a mesh shader). Set `LEONIDA_GRAPHICS_DIRECT_HEAPS=0` to restore the
previous draw arguments.
3. **Game-built descriptor memory.** The game writes raw T#/S# bytes into a heap itself, without calling
`CreateShaderResourceView`. The shaders then read those bytes exactly as in case 1.

Expand Down
41 changes: 36 additions & 5 deletions runtime/shims/d3d12_com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,21 @@ class GraphicsCommandList final
stub("DispatchMesh(root signature larger than a mesh shader's root window)");
return check(false);
}
// The directly indexed heaps follow the arguments (graphics_user_data). The window shows a mesh shader push
// dwords 8..23 only (s16..s31), so a signature that fills it leaves a heap where the mesh stage cannot read.
{
constexpr std::uint32_t window_end = rt::rootsig::stage_base(rt::rootsig::Stage::Pixel) +
rt::rootsig::kMaxUserSgprs -
rt::rootsig::stage_base(rt::rootsig::Stage::Mesh);
const auto flags = graphics_.layout->flags;
const bool resource = flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED;
const bool sampler = flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED;
const std::uint32_t end = graphics_.layout->graphics_arguments_end;
if (resource && end >= window_end)
note_once("mesh direct heaps: no SGPR left in the mesh window (s16..s31) for the resource heap");
if (sampler && end + (resource ? 1u : 0u) >= window_end)
note_once("mesh direct heaps: no SGPR left in the mesh window (s16..s31) for the sampler heap");
}
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};
Expand Down Expand Up @@ -1679,17 +1694,34 @@ class GraphicsCommandList final
return !(v && std::string_view(v) == "0");
}

// Direct and indirect dispatches. Putting the resource heap first and the sampler heap next is a hypothesis
// with partial evidence (docs/d3d12_vulkan.md section 3.4, tools/compute_heap_layout_census.py). A heap with no
// inline user-data dword left is reported once per heap kind and the explicit arguments are kept.
std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> compute_user_data() const {
note_direct_heap_indexing(compute_.layout);
static const bool enabled = direct_heaps_enabled("LEONIDA_COMPUTE_DIRECT_HEAPS");
if (!enabled) return compute_.data;
return with_direct_heaps(compute_, compute_.layout->compute_arguments_end);
const RootArgumentLayout& layout = *compute_.layout;
const bool resource = layout.flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED;
const bool sampler = layout.flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED;
const std::uint32_t end = layout.compute_arguments_end;
if ((resource || sampler) && end == rt::rootsig::kNoSgpr) {
note_once("compute direct heaps: not supplied, the root arguments spill (no measured slot)");
} else {
if (resource && end >= rt::rootsig::kMaxUserSgprs)
note_once("compute root arguments leave no inline SGPR for the resource heap");
if (sampler && end + (resource ? 1u : 0u) >= rt::rootsig::kMaxUserSgprs)
note_once("compute root arguments leave no inline SGPR for the sampler heap");
}
return with_direct_heaps(compute_, end);
}

// Graphics: vertex, pixel and geometry shaders share one user-data block and the s8 layout, so the heaps after
// the explicit arguments serve every one of those stages (a pixel shader of a signature whose arguments end at
// s11 reads the resource heap from s11). A mesh shader reads its arguments from s16, which is not modelled
// (RootArgumentLayout, D3D-448): its pixel shader gets the heaps, the mesh stage does not.
// LEONIDA_GRAPHICS_DIRECT_HEAPS=0 restores the previous arguments.
// s11 reads the resource heap from s11). A mesh shader reads push dword d as s(d + 8) through its root window
// (RootArgumentLayout::mesh_ok), so the same dwords give it the heaps after its arguments from s16, where the
// console puts them (issue #14 comment 7143020922, RT-DIRECT-HEAP-GRAPHICS); DispatchMesh reports a heap that
// falls past the window. LEONIDA_GRAPHICS_DIRECT_HEAPS=0 restores the previous arguments.
std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> graphics_user_data() const {
constexpr UINT direct = D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED |
D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED;
Expand Down Expand Up @@ -1749,7 +1781,6 @@ class GraphicsCommandList final
std::fputc('\n', stderr);
}
}
note_direct_heap_indexing(compute_.layout);
check(list_.dispatch({compute_pipeline_, compute_user_data(), x, y, z}));
}

Expand Down
51 changes: 45 additions & 6 deletions runtime/test/d3d12_compute_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,15 +588,53 @@ int main() {
list->SetDescriptorHeaps(2, both);
list->SetComputeRootDescriptorTable(2, {gpu + 32});
list->Dispatch(1, 1, 1);
Ref<ID3D12CommandAllocator> bundle_allocator;
Ref<ID3D12GraphicsCommandList> bundle;
EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_BUNDLE, IID_PPV_ARGS(&bundle_allocator.p))));
EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_BUNDLE, bundle_allocator.p, nullptr,
IID_PPV_ARGS(&bundle.p))));
bundle->Dispatch(1, 1, 1);
EXPECT(SUCCEEDED(bundle->Close()));
list->ExecuteBundle(bundle.p);
list->SetDescriptorHeaps(1, &heap.p);
list->ExecuteIndirect(signature.p, 1, out_a.p, 0, nullptr, 0);
list->SetDescriptorHeaps(1, &sampler.p);
list->ExecuteIndirect(signature.p, 1, out_a.p, 0, nullptr, 0);
// 27 constants starting at s4 leave room for one heap. At 28, s31 is explicit too.
for (UINT count : {27u, 28u}) {
auto constants_param = params[0];
constants_param.Constants.Num32BitValues = count;
auto full_desc = direct_desc;
full_desc.Desc_1_0.NumParameters = 1;
full_desc.Desc_1_0.pParameters = &constants_param;
const auto blob = serialize(full_desc);
Ref<ID3D12RootSignature> full;
EXPECT(SUCCEEDED(device->CreateRootSignature(0, blob.data(), blob.size(), IID_PPV_ARGS(&full.p))));
pipeline_desc.pRootSignature = full.p;
Ref<ID3D12PipelineState> full_pso;
EXPECT(SUCCEEDED(device->CreateComputePipelineState(&pipeline_desc, IID_PPV_ARGS(&full_pso.p))));
list->SetPipelineState(full_pso.p);
list->SetComputeRootSignature(full.p);
list->SetComputeRoot32BitConstant(0, 123, count - 1);
list->SetDescriptorHeaps(2, both);
list->Dispatch(1, 1, 1);
}
EXPECT(SUCCEEDED(list->Close()));
std::vector<leonida::Command> commands;
EXPECT(rt::d3d12::recorded_commands(list.p, commands) && commands.size() == 2);
EXPECT(rt::d3d12::recorded_commands(list.p, commands) && commands.size() == 6);
const auto& before = std::get<leonida::Dispatch>(commands[0]).user_data;
const auto& after = std::get<leonida::ExecuteIndirect>(commands[1]).user_data;
EXPECT(before[8] == gpu + 32 && before[9] == gpu && before[10] == smp);
EXPECT(after[8] == gpu + 32 && after[9] == gpu && after[10] == 0);
const auto& after = std::get<leonida::ExecuteIndirect>(commands[2]).user_data;
char setting[2]{};
const bool enabled = GetEnvironmentVariableA("LEONIDA_COMPUTE_DIRECT_HEAPS", setting, 2) != 1 ||
setting[0] != '0';
EXPECT(before[8] == gpu + 32 && before[9] == (enabled ? gpu : 0) && before[10] == (enabled ? smp : 0));
EXPECT(after[8] == gpu + 32 && after[9] == (enabled ? gpu : 0) && after[10] == 0);
EXPECT(std::get<leonida::Dispatch>(commands[1]).user_data == before);
const auto& sampler_only = std::get<leonida::ExecuteIndirect>(commands[3]).user_data;
EXPECT(sampler_only[9] == 0 && sampler_only[10] == (enabled ? smp : 0));
const auto& one_slot = std::get<leonida::Dispatch>(commands[4]).user_data;
EXPECT(one_slot[30] == 123 && one_slot[31] == (enabled ? gpu : 0));
EXPECT(std::get<leonida::Dispatch>(commands[5]).user_data[31] == 123);
EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr)));
// The shader follows the V# at the heap index in s4. Slot 1 and the unrelated explicit table are null.
D3D12_SHADER_RESOURCE_VIEW_DESC srv{};
Expand Down Expand Up @@ -635,10 +673,11 @@ int main() {
const auto* values = static_cast<const UINT*>(results);
for (UINT i = 0; i < elements; ++i) {
EXPECT(values[i] == 0);
EXPECT(values[elements + i] == table_values[i % 4]);
EXPECT(values[elements + i] == (enabled ? table_values[i % 4] : 0));
}
readback->Unmap(0, nullptr);
std::puts("directly indexed compute heap: 64 buffer reads and 64 null controls matched");
std::puts(enabled ? "directly indexed compute heap: 64 buffer reads and 64 null controls matched"
: "directly indexed compute heap disabled: 128 zero controls matched");
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
}

Expand Down
67 changes: 65 additions & 2 deletions runtime/test/d3d12_mesh_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,14 @@ int main(int argc, char** argv) {
};
// Twenty constants: s8..s27 for a pixel shader, but past a mesh shader's 16-dword window (s16..s31). Thirty:
// past the graphics window (24 from s8) and the compute one (28 from s4) too, as a retail start-up signature is.
const auto wide_root_signature = [&](ID3D12Device* device, Ref<ID3D12RootSignature>& root, UINT values = 20) {
const auto wide_root_signature = [&](ID3D12Device* device, Ref<ID3D12RootSignature>& root, UINT values = 20,
D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE) {
D3D12_ROOT_PARAMETER param{};
param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
param.Constants = {0, 0, values};
D3D12_VERSIONED_ROOT_SIGNATURE_DESC root_desc{};
root_desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_0;
root_desc.Desc_1_0 = {1, &param, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE};
root_desc.Desc_1_0 = {1, &param, 0, nullptr, flags};
const auto blob = serialize_root_signature(root_desc);
return !blob.empty() &&
SUCCEEDED(device->CreateRootSignature(0, blob.data(), blob.size(), IID_PPV_ARGS(&root.p)));
Expand Down Expand Up @@ -526,6 +527,68 @@ int main(int argc, char** argv) {
list->RSSetViewports(1, &viewport);
list6->DispatchMesh(1, 1, 1);
EXPECT(SUCCEEDED(list->Close()));

// RT-DIRECT-HEAP-GRAPHICS: with the directly-indexed flags, the dword after the graphics root arguments
// holds the resource heap's 32-bit address and the next one the sampler heap's, for vertex and mesh draws
// alike (a mesh shader reads them through its window). 4 constants take s8..s11, so the heaps are at 12
// and 13; 23 constants leave room for the resource heap only (s31); a signature past the graphics window
// (30 constants) keeps pack()'s s0 layout, which has no measured slot, so nothing is written.
const auto direct = static_cast<D3D12_ROOT_SIGNATURE_FLAGS>(
D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED |
D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED);
Ref<ID3D12RootSignature> direct4, direct23, direct30;
EXPECT(wide_root_signature(device.p, direct4, 4, direct));
EXPECT(wide_root_signature(device.p, direct23, 23, direct));
EXPECT(wide_root_signature(device.p, direct30, 30, direct));
D3D12_DESCRIPTOR_HEAP_DESC resources_desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 8,
D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0};
auto samplers_desc = resources_desc;
samplers_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER;
Ref<ID3D12DescriptorHeap> resources, samplers;
EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&resources_desc, IID_PPV_ARGS(&resources.p))));
EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&samplers_desc, IID_PPV_ARGS(&samplers.p))));
const std::uint64_t res_va = reinterpret_cast<XboxHeapHandleFn>(slot(resources.p, 10))(resources.p);
const std::uint64_t smp_va = reinterpret_cast<XboxHeapHandleFn>(slot(samplers.p, 10))(samplers.p);
EXPECT(res_va && res_va < (1ull << 32) && smp_va && smp_va < (1ull << 32));
const std::array<std::uint32_t, 4> four{1, 2, 3, 4};
ID3D12DescriptorHeap* both[] = {resources.p, samplers.p};
EXPECT(SUCCEEDED(list->Reset(allocator.p, vs_pso.p)));
list->SetDescriptorHeaps(2, both);
list->SetGraphicsRootSignature(direct4.p);
list->SetGraphicsRoot32BitConstants(0, 4, four.data(), 0);
list->RSSetViewports(1, &viewport);
list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
list->DrawInstanced(3, 1, 0, 0);
list->SetPipelineState(mesh_pso.p);
list6->DispatchMesh(1, 1, 1);
list->SetPipelineState(vs_pso.p);
list->SetDescriptorHeaps(1, &resources.p); // rebinding only the resource heap unbinds the sampler heap
list->DrawInstanced(3, 1, 0, 0);
list->SetDescriptorHeaps(2, both);
list->SetGraphicsRootSignature(direct23.p);
list->SetGraphicsRoot32BitConstant(0, 77, 22);
list->DrawInstanced(3, 1, 0, 0);
list->SetGraphicsRootSignature(direct30.p);
list->DrawInstanced(3, 1, 0, 0);
EXPECT(SUCCEEDED(list->Close()));
std::vector<leonida::Command> commands;
EXPECT(rt::d3d12::recorded_commands(list.p, commands));
std::vector<const leonida::Draw*> draws;
for (const auto& command : commands)
if (const auto* recorded = std::get_if<leonida::Draw>(&command)) draws.push_back(recorded);
EXPECT(draws.size() == 5);
char setting[2]{};
const bool enabled = GetEnvironmentVariableA("LEONIDA_GRAPHICS_DIRECT_HEAPS", setting, 2) != 1 ||
setting[0] != '0';
const auto res = enabled ? std::uint32_t(res_va) : 0u, smp = enabled ? std::uint32_t(smp_va) : 0u;
const auto& vertex = draws[0]->user_data;
EXPECT(vertex[8] == 1 && vertex[11] == 4 && vertex[12] == res && vertex[13] == smp);
EXPECT(draws[1]->mesh && draws[1]->user_data == vertex); // the mesh draw: the same dwords
EXPECT(draws[2]->user_data[12] == res && draws[2]->user_data[13] == 0);
EXPECT(draws[3]->user_data[30] == 77 && draws[3]->user_data[31] == res);
for (std::uint32_t k = 0; k < 32; ++k) EXPECT(draws[4]->user_data[k] == 0);
std::printf("direct heaps on graphics draws: resource heap at s%u, sampler heap at s%u%s\n", 12u, 13u,
enabled ? "" : " (LEONIDA_GRAPHICS_DIRECT_HEAPS=0: not written)");
}
// A DISPATCH_MESH command signature (D3D12_DISPATCH_MESH_ARGUMENTS, 12 bytes) is created; a stride below its
// 12 bytes is refused as the PC runtime refuses it. ExecuteIndirect with it records nothing yet (#3777 replays
Expand Down
Loading
Loading