diff options
| author | Moriz82 <72836413+Moriz82@users.noreply.github.com> | 2026-11-24 15:01:43 -0600 |
|---|---|---|
| committer | Moriz82 <72836413+Moriz82@users.noreply.github.com> | 2026-11-24 15:01:43 -0600 |
| commit | c8f7515e2e6fb2a2a61eb1e986ee8c6fd4431399 (patch) | |
| tree | 68fbac6e316de0e86b8cba404b6c1ab3058c99c6 /runtime/shims/d3d12_com.cpp | |
| parent | 66d4f5aa36bb6bcd006fa4b311c7a84d809fdbad (diff) | |
d3d12: supply directly indexed heaps to compute dispatches (RT-DIRECT-HEAP-COMPUTE)
Compute shaders using direct resource indexing received a zero heap
pointer. Snapshot each bound heap offset after the explicit inline root
arguments on direct and indirect dispatches, so each command retains the
bindings it had when recorded. When the explicit arguments leave no
SGPR, keep them and report the missing heap slot once.
The placement is a hypothesis with partial evidence. Of 2,959
direct-indexing compute entries, seven could be paired with recorded
layouts. All seven resource reads agree with the predicted slot. The
other 2,952 have no paired layout, and no paired sampler read
establishes the relative heap order. tools/compute_heap_layout_census.py
reproduces the count, with synthetic tests for agreement, disagreement
and missing evidence.
Built gta6rt and d3d12_compute_test with clang-cl. The Proton Vulkan
fixture passed 64 buffer reads and 64 null controls on an RTX 4070. With
the rollback switch, all 128 controls read zero. Both runs check the
recorded arguments for bundle inheritance, indirect heap rebinding and
the last inline SGPR. The synthetic SPIR-V fixture passed validation.
AI-assisted (Codex and Claude Code agents).
Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 48 |
1 files changed, 36 insertions, 12 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 42c8e5cea..9da7240ef 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp | |||
| @@ -883,7 +883,7 @@ public: | |||
| 883 | if (kind == leonida::IndirectKind::dispatch && max_command_count) { // the pipeline and root arguments a Dispatch would record | 883 | if (kind == leonida::IndirectKind::dispatch && max_command_count) { // the pipeline and root arguments a Dispatch would record |
| 884 | if (!compute_pipeline_ || !compute_.layout) return check(false); | 884 | if (!compute_pipeline_ || !compute_.layout) return check(false); |
| 885 | cmd.pipeline = compute_pipeline_; | 885 | cmd.pipeline = compute_pipeline_; |
| 886 | cmd.user_data = compute_.data; | 886 | cmd.user_data = compute_user_data(); |
| 887 | } | 887 | } |
| 888 | check(list_.execute_indirect(cmd)); | 888 | check(list_.execute_indirect(cmd)); |
| 889 | } | 889 | } |
| @@ -1370,19 +1370,20 @@ private: | |||
| 1370 | if ((cbv && cbv != cbv_srv_uav_heap_) || (smp && smp != sampler_heap_)) check(false); | 1370 | if ((cbv && cbv != cbv_srv_uav_heap_) || (smp && smp != sampler_heap_)) check(false); |
| 1371 | } | 1371 | } |
| 1372 | 1372 | ||
| 1373 | void note_direct_heap_indexing(const RootArgumentLayout* layout) const { | 1373 | void note_direct_heap_indexing(const RootArgumentLayout* layout, bool compute) const { |
| 1374 | if (!layout) return; | ||
| 1375 | if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) { | 1374 | if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) { |
| 1376 | if (!cbv_srv_uav_heap_) | 1375 | if (!cbv_srv_uav_heap_) |
| 1377 | note_once("draw/dispatch uses CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED with no CBV/SRV/UAV heap bound"); | 1376 | note_once(compute ? "dispatch uses direct resource indexing with no CBV/SRV/UAV heap bound" |
| 1378 | else | 1377 | : "draw uses direct resource indexing with no CBV/SRV/UAV heap bound"); |
| 1379 | note_once("CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED heap base tracked on list (SGPR slot open, section 3.4)"); | 1378 | else if (!compute) |
| 1379 | note_once("draw uses direct resource indexing; graphics heap-base SGPR is not supplied"); | ||
| 1380 | } | 1380 | } |
| 1381 | if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) { | 1381 | if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) { |
| 1382 | if (!sampler_heap_) | 1382 | if (!sampler_heap_) |
| 1383 | note_once("draw/dispatch uses SAMPLER_HEAP_DIRECTLY_INDEXED with no sampler heap bound"); | 1383 | note_once(compute ? "dispatch uses direct sampler indexing with no sampler heap bound" |
| 1384 | else | 1384 | : "draw uses direct sampler indexing with no sampler heap bound"); |
| 1385 | note_once("SAMPLER_HEAP_DIRECTLY_INDEXED heap base tracked on list (SGPR slot open, section 3.4)"); | 1385 | else if (!compute) |
| 1386 | note_once("draw uses direct sampler indexing; graphics heap-base SGPR is not supplied"); | ||
| 1386 | } | 1387 | } |
| 1387 | } | 1388 | } |
| 1388 | 1389 | ||
| @@ -1426,10 +1427,33 @@ private: | |||
| 1426 | : leonida::IndexBufferView{}; | 1427 | : leonida::IndexBufferView{}; |
| 1427 | } | 1428 | } |
| 1428 | 1429 | ||
| 1430 | // Section 3.4 documents the observed resource-heap slot. Appending flagged heaps in this | ||
| 1431 | // order is a working hypothesis for signatures without a matched program and layout. | ||
| 1432 | // Snapshot them for each command so rebinding a heap cannot change a recorded dispatch. | ||
| 1433 | std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> compute_user_data() const { | ||
| 1434 | note_direct_heap_indexing(compute_.layout, true); | ||
| 1435 | auto data = compute_.data; | ||
| 1436 | static const bool enabled = [] { | ||
| 1437 | char value[2]{}; | ||
| 1438 | return GetEnvironmentVariableA("LEONIDA_COMPUTE_DIRECT_HEAPS", value, 2) != 1 || value[0] != '0'; | ||
| 1439 | }(); | ||
| 1440 | if (!enabled) return data; | ||
| 1441 | const auto flags = compute_.layout->flags; | ||
| 1442 | auto slot = compute_.layout->compute_arguments_end; | ||
| 1443 | if (flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) { | ||
| 1444 | if (slot < data.size()) data[slot++] = static_cast<std::uint32_t>(cbv_srv_uav_base_); | ||
| 1445 | else note_once("compute root arguments leave no inline SGPR for the resource heap"); | ||
| 1446 | } | ||
| 1447 | if (flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) { | ||
| 1448 | if (slot < data.size()) data[slot] = static_cast<std::uint32_t>(sampler_base_); | ||
| 1449 | else note_once("compute root arguments leave no inline SGPR for the sampler heap"); | ||
| 1450 | } | ||
| 1451 | return data; | ||
| 1452 | } | ||
| 1453 | |||
| 1429 | void record_dispatch(UINT x, UINT y, UINT z) { | 1454 | void record_dispatch(UINT x, UINT y, UINT z) { |
| 1430 | if (!compute_pipeline_ || !compute_.layout) return check(false); | 1455 | if (!compute_pipeline_ || !compute_.layout) return check(false); |
| 1431 | note_direct_heap_indexing(compute_.layout); | 1456 | check(list_.dispatch({compute_pipeline_, compute_user_data(), x, y, z})); |
| 1432 | check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); | ||
| 1433 | } | 1457 | } |
| 1434 | 1458 | ||
| 1435 | // The views block a draw records. Rebuilt only when the render targets or the bound vertex buffers change. | 1459 | // The views block a draw records. Rebuilt only when the render targets or the bound vertex buffers change. |
| @@ -1468,7 +1492,7 @@ private: | |||
| 1468 | // A draw needs a graphics pipeline, a graphics root signature and a topology. | 1492 | // A draw needs a graphics pipeline, a graphics root signature and a topology. |
| 1469 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { | 1493 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { |
| 1470 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); | 1494 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); |
| 1471 | note_direct_heap_indexing(graphics_.layout); | 1495 | note_direct_heap_indexing(graphics_.layout, false); |
| 1472 | // No viewport (never set, or unbound with RSSetViewports(0)) or every bound viewport empty: D3D12 rasterizes | 1496 | // No viewport (never set, or unbound with RSSetViewports(0)) or every bound viewport empty: D3D12 rasterizes |
| 1473 | // nothing, and Vulkan forbids a 0x0 viewport, so the draw is not recorded. | 1497 | // nothing, and Vulkan forbids a 0x0 viewport, so the draw is not recorded. |
| 1474 | if (!any_nonempty_viewport()) return; | 1498 | if (!any_nonempty_viewport()) return; |