upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraslan2142 <jan.trencansky@protonmail.com>2026-11-25 04:26:54 +0100
committerGitHub <noreply@github.com>2026-11-25 04:26:54 +0100
commitbfef489cb6307fa4e1dc6ed2c40bfc0ce120d066 (patch)
tree19e2f01d50809de00ecba664b4044eed1aad2c36
parent98efc22ca3d2841b985eb36d4afbe7d434628670 (diff)
parent3ee0037fd3accba459d0d5ecd8acad831174d4d7 (diff)
Merge pull request #1232 from iFeyz/feat/direct-heap-graphics
d3d12: graphics draws supply the directly indexed heaps after their root arguments (RT-DIRECT-HEAP-GRAPHICS)
-rw-r--r--docs/d3d12_vulkan.md15
-rw-r--r--runtime/shims/d3d12_com.cpp22
-rw-r--r--runtime/test/d3d12_mesh_test.cpp67
3 files changed, 95 insertions, 9 deletions
diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md
index 161705e47..97d737c8e 100644
--- a/docs/d3d12_vulkan.md
+++ b/docs/d3d12_vulkan.md
@@ -1763,10 +1763,17 @@ The game can use D3D12 descriptors in three ways, and the layer must handle all
1763 only when the signature has its flag, snapshotted per draw. Vertex, pixel and geometry shaders share one 1763 only when the signature has its flag, snapshotted per draw. Vertex, pixel and geometry shaders share one
1764 user-data block and that layout, so one write serves all three; a signature whose explicit arguments take 1764 user-data block and that layout, so one write serves all three; a signature whose explicit arguments take
1765 s8..s10 gives s11, one whose arguments take s8..s11 gives s12 (the two program reads above). A mesh shader reads 1765 s8..s10 gives s11, one whose arguments take s8..s11 gives s12 (the two program reads above). A mesh shader reads
1766 its arguments from s16, which is not modelled, so in a mesh pipeline only the pixel shader gets the heaps. A 1766 push dword d as s(d + 8) through its root window (section 3.5), so `DispatchMesh` records the same dwords and
1767 signature that keeps the s0 graphics layout, or whose arguments fill s31, gets none (logged once). 1767 the mesh stage finds the resource heap at s(16 + end − 8), right after its arguments from s16, where DadaCap
1768 `d3d12_graphics_direct_heaps_test` checks the recorded draws and what a vertex and a pixel shader read, with 1768 reports every directly indexed program of the retail library reads it, for compute from s4, VS/PS/GS from s8
1769 and without the flags. Set `LEONIDA_GRAPHICS_DIRECT_HEAPS=0` to restore the previous draw arguments. 1769 and mesh from s16 (issue #14, comment 7143020922, on a `boot-prototype` run; not reproduced in this
1770 repository). A mesh signature that fills the window leaves no room there: a heap past s31 of the mesh shader
1771 (push dword 24 or later) is logged once. A signature that keeps the s0 graphics layout, or whose arguments fill
1772 s31, gets none (logged once). `d3d12_graphics_direct_heaps_test` checks the recorded draws and what a vertex
1773 and a pixel shader read, with and without the flags; `d3d12_mesh_test` checks that a mesh draw records the
1774 vertex draw's dwords, a resource-only rebind, a signature ending at s30 and one on the s0 layout (recorded
1775 arguments only; no GPU read through a mesh shader). Set `LEONIDA_GRAPHICS_DIRECT_HEAPS=0` to restore the
1776 previous draw arguments.
17703. **Game-built descriptor memory.** The game writes raw T#/S# bytes into a heap itself, without calling 17773. **Game-built descriptor memory.** The game writes raw T#/S# bytes into a heap itself, without calling
1771 `CreateShaderResourceView`. The shaders then read those bytes exactly as in case 1. 1778 `CreateShaderResourceView`. The shaders then read those bytes exactly as in case 1.
1772 1779
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp
index de448ddc4..883cce32a 100644
--- a/runtime/shims/d3d12_com.cpp
+++ b/runtime/shims/d3d12_com.cpp
@@ -534,6 +534,21 @@ public:
534 stub("DispatchMesh(root signature larger than a mesh shader's root window)"); 534 stub("DispatchMesh(root signature larger than a mesh shader's root window)");
535 return check(false); 535 return check(false);
536 } 536 }
537 // The directly indexed heaps follow the arguments (graphics_user_data). The window shows a mesh shader push
538 // dwords 8..23 only (s16..s31), so a signature that fills it leaves a heap where the mesh stage cannot read.
539 {
540 constexpr std::uint32_t window_end = rt::rootsig::stage_base(rt::rootsig::Stage::Pixel) +
541 rt::rootsig::kMaxUserSgprs -
542 rt::rootsig::stage_base(rt::rootsig::Stage::Mesh);
543 const auto flags = graphics_.layout->flags;
544 const bool resource = flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED;
545 const bool sampler = flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED;
546 const std::uint32_t end = graphics_.layout->graphics_arguments_end;
547 if (resource && end >= window_end)
548 note_once("mesh direct heaps: no SGPR left in the mesh window (s16..s31) for the resource heap");
549 if (sampler && end + (resource ? 1u : 0u) >= window_end)
550 note_once("mesh direct heaps: no SGPR left in the mesh window (s16..s31) for the sampler heap");
551 }
537 if (!any_nonempty_viewport()) return; // as record_draw: nothing is rasterized 552 if (!any_nonempty_viewport()) return; // as record_draw: nothing is rasterized
538 leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, {}, 0, viewport_, 553 leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, {}, 0, viewport_,
539 scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; 554 scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0};
@@ -1703,9 +1718,10 @@ private:
1703 1718
1704 // Graphics: vertex, pixel and geometry shaders share one user-data block and the s8 layout, so the heaps after 1719 // Graphics: vertex, pixel and geometry shaders share one user-data block and the s8 layout, so the heaps after
1705 // the explicit arguments serve every one of those stages (a pixel shader of a signature whose arguments end at 1720 // the explicit arguments serve every one of those stages (a pixel shader of a signature whose arguments end at
1706 // s11 reads the resource heap from s11). A mesh shader reads its arguments from s16, which is not modelled 1721 // s11 reads the resource heap from s11). A mesh shader reads push dword d as s(d + 8) through its root window
1707 // (RootArgumentLayout, D3D-448): its pixel shader gets the heaps, the mesh stage does not. 1722 // (RootArgumentLayout::mesh_ok), so the same dwords give it the heaps after its arguments from s16, where the
1708 // LEONIDA_GRAPHICS_DIRECT_HEAPS=0 restores the previous arguments. 1723 // console puts them (issue #14 comment 7143020922, RT-DIRECT-HEAP-GRAPHICS); DispatchMesh reports a heap that
1724 // falls past the window. LEONIDA_GRAPHICS_DIRECT_HEAPS=0 restores the previous arguments.
1709 std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> graphics_user_data() const { 1725 std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> graphics_user_data() const {
1710 constexpr UINT direct = D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED | 1726 constexpr UINT direct = D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED |
1711 D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED; 1727 D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED;
diff --git a/runtime/test/d3d12_mesh_test.cpp b/runtime/test/d3d12_mesh_test.cpp
index e59414a01..2d9bc5212 100644
--- a/runtime/test/d3d12_mesh_test.cpp
+++ b/runtime/test/d3d12_mesh_test.cpp
@@ -188,13 +188,14 @@ int main(int argc, char** argv) {
188 }; 188 };
189 // Twenty constants: s8..s27 for a pixel shader, but past a mesh shader's 16-dword window (s16..s31). Thirty: 189 // Twenty constants: s8..s27 for a pixel shader, but past a mesh shader's 16-dword window (s16..s31). Thirty:
190 // past the graphics window (24 from s8) and the compute one (28 from s4) too, as a retail start-up signature is. 190 // past the graphics window (24 from s8) and the compute one (28 from s4) too, as a retail start-up signature is.
191 const auto wide_root_signature = [&](ID3D12Device* device, Ref<ID3D12RootSignature>& root, UINT values = 20) { 191 const auto wide_root_signature = [&](ID3D12Device* device, Ref<ID3D12RootSignature>& root, UINT values = 20,
192 D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE) {
192 D3D12_ROOT_PARAMETER param{}; 193 D3D12_ROOT_PARAMETER param{};
193 param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; 194 param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
194 param.Constants = {0, 0, values}; 195 param.Constants = {0, 0, values};
195 D3D12_VERSIONED_ROOT_SIGNATURE_DESC root_desc{}; 196 D3D12_VERSIONED_ROOT_SIGNATURE_DESC root_desc{};
196 root_desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_0; 197 root_desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_0;
197 root_desc.Desc_1_0 = {1, &param, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE}; 198 root_desc.Desc_1_0 = {1, &param, 0, nullptr, flags};
198 const auto blob = serialize_root_signature(root_desc); 199 const auto blob = serialize_root_signature(root_desc);
199 return !blob.empty() && 200 return !blob.empty() &&
200 SUCCEEDED(device->CreateRootSignature(0, blob.data(), blob.size(), IID_PPV_ARGS(&root.p))); 201 SUCCEEDED(device->CreateRootSignature(0, blob.data(), blob.size(), IID_PPV_ARGS(&root.p)));
@@ -526,6 +527,68 @@ int main(int argc, char** argv) {
526 list->RSSetViewports(1, &viewport); 527 list->RSSetViewports(1, &viewport);
527 list6->DispatchMesh(1, 1, 1); 528 list6->DispatchMesh(1, 1, 1);
528 EXPECT(SUCCEEDED(list->Close())); 529 EXPECT(SUCCEEDED(list->Close()));
530
531 // RT-DIRECT-HEAP-GRAPHICS: with the directly-indexed flags, the dword after the graphics root arguments
532 // holds the resource heap's 32-bit address and the next one the sampler heap's, for vertex and mesh draws
533 // alike (a mesh shader reads them through its window). 4 constants take s8..s11, so the heaps are at 12
534 // and 13; 23 constants leave room for the resource heap only (s31); a signature past the graphics window
535 // (30 constants) keeps pack()'s s0 layout, which has no measured slot, so nothing is written.
536 const auto direct = static_cast<D3D12_ROOT_SIGNATURE_FLAGS>(
537 D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED |
538 D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED);
539 Ref<ID3D12RootSignature> direct4, direct23, direct30;
540 EXPECT(wide_root_signature(device.p, direct4, 4, direct));
541 EXPECT(wide_root_signature(device.p, direct23, 23, direct));
542 EXPECT(wide_root_signature(device.p, direct30, 30, direct));
543 D3D12_DESCRIPTOR_HEAP_DESC resources_desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 8,
544 D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0};
545 auto samplers_desc = resources_desc;
546 samplers_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER;
547 Ref<ID3D12DescriptorHeap> resources, samplers;
548 EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&resources_desc, IID_PPV_ARGS(&resources.p))));
549 EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&samplers_desc, IID_PPV_ARGS(&samplers.p))));
550 const std::uint64_t res_va = reinterpret_cast<XboxHeapHandleFn>(slot(resources.p, 10))(resources.p);
551 const std::uint64_t smp_va = reinterpret_cast<XboxHeapHandleFn>(slot(samplers.p, 10))(samplers.p);
552 EXPECT(res_va && res_va < (1ull << 32) && smp_va && smp_va < (1ull << 32));
553 const std::array<std::uint32_t, 4> four{1, 2, 3, 4};
554 ID3D12DescriptorHeap* both[] = {resources.p, samplers.p};
555 EXPECT(SUCCEEDED(list->Reset(allocator.p, vs_pso.p)));
556 list->SetDescriptorHeaps(2, both);
557 list->SetGraphicsRootSignature(direct4.p);
558 list->SetGraphicsRoot32BitConstants(0, 4, four.data(), 0);
559 list->RSSetViewports(1, &viewport);
560 list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
561 list->DrawInstanced(3, 1, 0, 0);
562 list->SetPipelineState(mesh_pso.p);
563 list6->DispatchMesh(1, 1, 1);
564 list->SetPipelineState(vs_pso.p);
565 list->SetDescriptorHeaps(1, &resources.p); // rebinding only the resource heap unbinds the sampler heap
566 list->DrawInstanced(3, 1, 0, 0);
567 list->SetDescriptorHeaps(2, both);
568 list->SetGraphicsRootSignature(direct23.p);
569 list->SetGraphicsRoot32BitConstant(0, 77, 22);
570 list->DrawInstanced(3, 1, 0, 0);
571 list->SetGraphicsRootSignature(direct30.p);
572 list->DrawInstanced(3, 1, 0, 0);
573 EXPECT(SUCCEEDED(list->Close()));
574 std::vector<leonida::Command> commands;
575 EXPECT(rt::d3d12::recorded_commands(list.p, commands));
576 std::vector<const leonida::Draw*> draws;
577 for (const auto& command : commands)
578 if (const auto* recorded = std::get_if<leonida::Draw>(&command)) draws.push_back(recorded);
579 EXPECT(draws.size() == 5);
580 char setting[2]{};
581 const bool enabled = GetEnvironmentVariableA("LEONIDA_GRAPHICS_DIRECT_HEAPS", setting, 2) != 1 ||
582 setting[0] != '0';
583 const auto res = enabled ? std::uint32_t(res_va) : 0u, smp = enabled ? std::uint32_t(smp_va) : 0u;
584 const auto& vertex = draws[0]->user_data;
585 EXPECT(vertex[8] == 1 && vertex[11] == 4 && vertex[12] == res && vertex[13] == smp);
586 EXPECT(draws[1]->mesh && draws[1]->user_data == vertex); // the mesh draw: the same dwords
587 EXPECT(draws[2]->user_data[12] == res && draws[2]->user_data[13] == 0);
588 EXPECT(draws[3]->user_data[30] == 77 && draws[3]->user_data[31] == res);
589 for (std::uint32_t k = 0; k < 32; ++k) EXPECT(draws[4]->user_data[k] == 0);
590 std::printf("direct heaps on graphics draws: resource heap at s%u, sampler heap at s%u%s\n", 12u, 13u,
591 enabled ? "" : " (LEONIDA_GRAPHICS_DIRECT_HEAPS=0: not written)");
529 } 592 }
530 // A DISPATCH_MESH command signature (D3D12_DISPATCH_MESH_ARGUMENTS, 12 bytes) is created; a stride below its 593 // A DISPATCH_MESH command signature (D3D12_DISPATCH_MESH_ARGUMENTS, 12 bytes) is created; a stride below its
531 // 12 bytes is refused as the PC runtime refuses it. ExecuteIndirect with it records nothing yet (#3777 replays 594 // 12 bytes is refused as the PC runtime refuses it. ExecuteIndirect with it records nothing yet (#3777 replays