upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/d3d12_com.cpp
diff options
context:
space:
mode:
authorMoriz82 <72836413+Moriz82@users.noreply.github.com>2026-11-25 03:44:31 -0600
committerMoriz82 <72836413+Moriz82@users.noreply.github.com>2026-11-25 03:44:43 -0600
commitba626820ec54776fb0faffa2413727285befe85b (patch)
tree95a99f8029785bec5f3b07b7efc4277e5bc7510b /runtime/shims/d3d12_com.cpp
parent3de22a19047b168d28a7a11152bf05d18ed0062e (diff)
Integrate post-story-load memory, queue and Vulkan runtime changes
Preserve physical window aliases across remaps, order console list values, and replay bounded indirect graphics with captured draw state. Coordinate pipeline lifetime, texture refresh and queue teardown. Add synthetic CPU and GPU coverage for the integrated paths. Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com> Co-authored-by: ivanepsilondelta <264061546+ivanepsilondelta@users.noreply.github.com>
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
-rw-r--r--runtime/shims/d3d12_com.cpp253
1 files changed, 202 insertions, 51 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp
index 883cce32a..6dee464a8 100644
--- a/runtime/shims/d3d12_com.cpp
+++ b/runtime/shims/d3d12_com.cpp
@@ -4,10 +4,13 @@
4#include "d3d12_stubs.h" // generated 4#include "d3d12_stubs.h" // generated
5#include "d3d12_com_object.h" 5#include "d3d12_com_object.h"
6#include "d3d12_fence_waiter.h" 6#include "d3d12_fence_waiter.h"
7#include "d3d12_feature_compat.h"
8#include "d3d12_indirect_draw_policy.h"
7#include "d3d12_trace.h" 9#include "d3d12_trace.h"
8#include "d3d12_xs_device.h" 10#include "d3d12_xs_device.h"
9#include "../src/gfx10_descriptors.h" 11#include "../src/gfx10_descriptors.h"
10#include "../src/title_va_alias.h" 12#include "../src/title_va_alias.h"
13#include "../src/title_page_watch.h" // title_gpu_views::to_host_view
11#include "../src/fsdraw_probe.h" // LOCAL DIAGNOSTIC 14#include "../src/fsdraw_probe.h" // LOCAL DIAGNOSTIC
12#include "../src/local_diagnostics.h" 15#include "../src/local_diagnostics.h"
13 16
@@ -144,15 +147,26 @@ public:
144 // {9a4e1c2b-55d8-4f7a-b012-3c8e7a1f6d90} 147 // {9a4e1c2b-55d8-4f7a-b012-3c8e7a1f6d90}
145 static constexpr GUID kTag{0x9a4e1c2b, 0x55d8, 0x4f7a, {0xb0, 0x12, 0x3c, 0x8e, 0x7a, 0x1f, 0x6d, 0x90}}; 148 static constexpr GUID kTag{0x9a4e1c2b, 0x55d8, 0x4f7a, {0xb0, 0x12, 0x3c, 0x8e, 0x7a, 0x1f, 0x6d, 0x90}};
146 149
147 CommandSignature(leonida::IndirectKind kind, UINT byte_stride) : kind_(kind), stride_(byte_stride) {} 150 CommandSignature(leonida::IndirectKind kind, UINT byte_stride, UINT packed_size,
151 std::span<const D3D12_INDIRECT_ARGUMENT_DESC> prefix)
152 : kind_(kind), stride_(byte_stride), packed_size_(packed_size), terminal_only_(prefix.empty()) {
153 // The caller may free the description immediately. Single-operation signatures allocate nothing.
154 if (!prefix.empty() && (kind == leonida::IndirectKind::draw || kind == leonida::IndirectKind::draw_indexed))
155 graphics_writes_.assign(prefix.begin(), prefix.end());
156 }
148 157
149 leonida::IndirectKind kind() const { return kind_; } 158 leonida::IndirectKind kind() const { return kind_; }
150 UINT stride() const { return stride_; } 159 UINT stride() const { return stride_; }
160 UINT packed_size() const { return packed_size_; }
161 bool terminal_only() const { return terminal_only_; }
162 const auto& graphics_writes() const { return graphics_writes_; }
151 static CommandSignature* from(IUnknown* object) { return Child::from<CommandSignature>(object); } 163 static CommandSignature* from(IUnknown* object) { return Child::from<CommandSignature>(object); }
152 164
153private: 165private:
154 leonida::IndirectKind kind_; 166 leonida::IndirectKind kind_;
155 UINT stride_; 167 UINT stride_, packed_size_;
168 bool terminal_only_; // no root, vertex, or index updates before the terminal operation
169 std::vector<D3D12_INDIRECT_ARGUMENT_DESC> graphics_writes_;
156}; 170};
157 171
158class CommandAllocator final 172class CommandAllocator final
@@ -552,6 +566,7 @@ public:
552 if (!any_nonempty_viewport()) return; // as record_draw: nothing is rasterized 566 if (!any_nonempty_viewport()) return; // as record_draw: nothing is rasterized
553 leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, {}, 0, viewport_, 567 leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, {}, 0, viewport_,
554 scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; 568 scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0};
569 note_direct_heap_indexing(graphics_.layout);
555 draw.mesh = true; 570 draw.mesh = true;
556 draw.viewports = draw_viewports(); 571 draw.viewports = draw_viewports();
557 check(list_.draw(std::move(draw))); 572 check(list_.draw(std::move(draw)));
@@ -954,11 +969,16 @@ public:
954 void STDMETHODCALLTYPE ExecuteIndirect(ID3D12CommandSignature* signature, UINT max_command_count, 969 void STDMETHODCALLTYPE ExecuteIndirect(ID3D12CommandSignature* signature, UINT max_command_count,
955 ID3D12Resource* argument_buffer, UINT64 argument_buffer_offset, 970 ID3D12Resource* argument_buffer, UINT64 argument_buffer_offset,
956 ID3D12Resource* count_buffer, UINT64 count_buffer_offset) override { 971 ID3D12Resource* count_buffer, UINT64 count_buffer_offset) override {
957 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); 972 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT && type_ != D3D12_COMMAND_LIST_TYPE_COMPUTE &&
958 leonida::IndirectKind kind = leonida::IndirectKind::draw; 973 type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE)
959 UINT stride = 0;
960 if (!command_signature_info(signature, kind, stride) || !argument_buffer)
961 return check(false); 974 return check(false);
975 const auto* sig = CommandSignature::from(signature);
976 if (!sig || !argument_buffer) return check(false);
977 const auto kind = sig->kind();
978 const UINT stride = sig->stride();
979 const bool terminal_only = sig->terminal_only();
980 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE && (count_buffer || !terminal_only))
981 return check(false); // only a fixed count and one operation are legal in a bundle
962 const auto arg_desc = argument_buffer->GetDesc(); 982 const auto arg_desc = argument_buffer->GetDesc();
963 if (arg_desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || argument_buffer_offset % 4 != 0) 983 if (arg_desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || argument_buffer_offset % 4 != 0)
964 return check(false); 984 return check(false);
@@ -991,29 +1011,33 @@ public:
991 if (argument_buffer_offset > arg_desc.Width || arg_desc.Width - argument_buffer_offset < required) 1011 if (argument_buffer_offset > arg_desc.Width || arg_desc.Width - argument_buffer_offset < required)
992 return check(false); 1012 return check(false);
993 } 1013 }
1014 if (!leonida::indirect_records_fit(arg_desc.Width, argument_buffer_offset, max_command_count,
1015 stride, sig->packed_size()))
1016 return check(false);
994 if (kind == leonida::IndirectKind::dispatch_mesh) { 1017 if (kind == leonida::IndirectKind::dispatch_mesh) {
995 // Not replayed yet (#3777 replays it as vkCmdDrawMeshTasksIndirectEXT): nothing is recorded, and the 1018 // Not replayed yet (#3777 replays it as vkCmdDrawMeshTasksIndirectEXT): nothing is recorded, and the
996 // first such call says so. The list stays valid. 1019 // first such call says so. The list stays valid.
997 static std::atomic<bool> logged{false}; 1020 leonida::indirect_stats::skip(leonida::indirect_stats::Event::record_skip, leonida::indirect_stats::mesh);
998 if (!logged.exchange(true, std::memory_order_relaxed))
999 std::fprintf(stderr, "[rt] d3d12: ExecuteIndirect with a DISPATCH_MESH signature is not replayed yet; "
1000 "nothing was recorded\n");
1001 return; 1021 return;
1002 } 1022 }
1003 leonida::ExecuteIndirect cmd{}; 1023 if (!terminal_only && (kind == leonida::IndirectKind::draw || kind == leonida::IndirectKind::draw_indexed)) {
1004 cmd.kind = kind; 1024 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false);
1005 cmd.stride = stride; 1025 // Compound draw emission is not yet supported. Reset the written bindings even if no
1006 cmd.max_count = max_command_count; 1026 // records execute. Bundle nesting and prefixes were already rejected above for bundles.
1007 cmd.argument_buffer = resource_id(argument_buffer); 1027 leonida::indirect_stats::skip(leonida::indirect_stats::Event::record_skip, leonida::indirect_stats::compound);
1008 cmd.argument_offset = argument_buffer_offset; 1028 if (!list_.closed()) reset_indirect_graphics_bindings(sig->graphics_writes());
1009 cmd.count_buffer = count_buffer ? resource_id(count_buffer) : 0; 1029 return;
1010 cmd.count_offset = count_buffer ? count_buffer_offset : 0;
1011 if (kind == leonida::IndirectKind::dispatch && max_command_count) { // the pipeline and root arguments a Dispatch would record
1012 if (!compute_pipeline_ || !compute_.layout) return check(false);
1013 cmd.pipeline = compute_pipeline_;
1014 cmd.user_data = compute_user_data();
1015 } 1030 }
1016 check(list_.execute_indirect(cmd)); 1031 const leonida::IndirectDrawArgs args{resource_id(argument_buffer), argument_buffer_offset, stride,
1032 max_command_count, count_buffer ? resource_id(count_buffer) : 0,
1033 count_buffer_offset};
1034 // Preserve call position, not the bundle's incomplete draw state. No COM pointer is dereferenced
1035 // later: the signature is decoded now, and the app owns resource lifetimes through GPU completion.
1036 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) {
1037 defer([kind, args](GraphicsCommandList& parent) { parent.record_indirect(kind, true, args); }, true);
1038 return;
1039 }
1040 record_indirect(kind, terminal_only, args);
1017 } 1041 }
1018 1042
1019 void STDMETHODCALLTYPE BuildRaytracingAccelerationStructure( 1043 void STDMETHODCALLTYPE BuildRaytracingAccelerationStructure(
@@ -1086,6 +1110,14 @@ public:
1086 } 1110 }
1087 } 1111 }
1088 const std::uint64_t built = cmd.dst_va; 1112 const std::uint64_t built = cmd.dst_va;
1113 // The D3D12 rules (alignment, a source for an update) on the addresses as the title gave them.
1114 if (!cmd.dst_va || cmd.dst_va % 256 || !cmd.scratch_va || cmd.scratch_va % 256 ||
1115 (cmd.update && (!cmd.src_va || cmd.src_va % 256)))
1116 return check(false);
1117 // The addresses the builder reaches the memory by. One with no GPU copy (logged by the device) leaves the
1118 // build out, with its postbuild values: the list stays valid, nothing is written through heap_base + a title
1119 // address, and the destination keeps what it had.
1120 if (!resolve_acceleration_structure_build(attached_device(), cmd)) return;
1089 const bool recorded = list_.build_acceleration_structure(std::move(cmd)); 1121 const bool recorded = list_.build_acceleration_structure(std::move(cmd));
1090 check(recorded); 1122 check(recorded);
1091 if (!recorded) return; 1123 if (!recorded) return;
@@ -1095,6 +1127,8 @@ public:
1095 info.dest_va = postbuild[i].DestBuffer; 1127 info.dest_va = postbuild[i].DestBuffer;
1096 info.type = static_cast<std::uint32_t>(postbuild[i].InfoType); 1128 info.type = static_cast<std::uint32_t>(postbuild[i].InfoType);
1097 info.sources = {built}; 1129 info.sources = {built};
1130 if (!info.dest_va || info.dest_va % 8) return check(false);
1131 if (!resolve_acceleration_structure_postbuild(attached_device(), info)) continue;
1098 const bool ok = list_.acceleration_structure_postbuild(std::move(info)); 1132 const bool ok = list_.acceleration_structure_postbuild(std::move(info));
1099 check(ok); 1133 check(ok);
1100 if (!ok) return; 1134 if (!ok) return;
@@ -1110,6 +1144,10 @@ public:
1110 info.dest_va = desc->DestBuffer; 1144 info.dest_va = desc->DestBuffer;
1111 info.type = static_cast<std::uint32_t>(desc->InfoType); 1145 info.type = static_cast<std::uint32_t>(desc->InfoType);
1112 info.sources.assign(sources, sources + count); 1146 info.sources.assign(sources, sources + count);
1147 if (!info.dest_va || info.dest_va % 8) return check(false);
1148 for (const auto va : info.sources)
1149 if (!va || va % 256) return check(false);
1150 if (!resolve_acceleration_structure_postbuild(attached_device(), info)) return; // logged; left out
1113 check(list_.acceleration_structure_postbuild(std::move(info))); 1151 check(list_.acceleration_structure_postbuild(std::move(info)));
1114 } 1152 }
1115 1153
@@ -1118,7 +1156,10 @@ public:
1118 D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE mode) override { 1156 D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE mode) override {
1119 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); 1157 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
1120 if (!valid_copy_mode(mode)) return check(false); 1158 if (!valid_copy_mode(mode)) return check(false);
1121 check(list_.copy_acceleration_structure({dest, source, static_cast<std::uint32_t>(mode)})); 1159 leonida::CopyAccelerationStructure copy{dest, source, static_cast<std::uint32_t>(mode)};
1160 if (!copy.dst_va || !copy.src_va || copy.dst_va % 256 || copy.src_va % 256) return check(false);
1161 if (!resolve_acceleration_structure_copy(attached_device(), copy)) return; // logged; left out
1162 check(list_.copy_acceleration_structure(copy));
1122 } 1163 }
1123 1164
1124 bool xbox_label(const leonida::label::Write& op) { return list_.write_gpu_label(op); } 1165 bool xbox_label(const leonida::label::Write& op) { return list_.write_gpu_label(op); }
@@ -1486,16 +1527,16 @@ private:
1486 // copy of it, so the shader's heap_base + VA reaches it (src/title_va_alias.h). A descriptor table is in one 1527 // copy of it, so the shader's heap_base + VA reaches it (src/title_va_alias.h). A descriptor table is in one
1487 // of the layer's heaps: kept, and its buffer V#s are rewritten the same way before each submission. 1528 // of the layer's heaps: kept, and its buffer V#s are rewritten the same way before each submission.
1488 // A table in a console descriptor heap is read from the heap's arena copy (console_descriptor_table_va). 1529 // A table in a console descriptor heap is read from the heap's arena copy (console_descriptor_table_va).
1489 if (type == rt::rootsig::ParamType::Descriptor) 1530 if (type == rt::rootsig::ParamType::Descriptor) {
1531 const std::uint64_t raw = va;
1490 va = leonida::title_va_aliases().resolve(va, kRootViewBytes, leonida::TitleVaAliases::root_view); 1532 va = leonida::title_va_aliases().resolve(va, kRootViewBytes, leonida::TitleVaAliases::root_view);
1491 else 1533 // No copy for the title's memory: the root view is 0, so the shader reads the arena's zero page instead
1534 // of storing at heap_base + a raw title address.
1535 if (!va && raw) leonida::note_unbacked_view("root view", raw, kRootViewBytes);
1536 } else
1492 va = console_descriptor_table_va(va); 1537 va = console_descriptor_table_va(va);
1493 if (type != rt::rootsig::ParamType::Descriptor && va && p->table_descriptors) { 1538 if (type != rt::rootsig::ParamType::Descriptor && va && p->table_descriptors)
1494 if (!tables_.empty() && tables_.back().first == va) 1539 remember_table(va, p->table_descriptors);
1495 tables_.back().second = std::max(tables_.back().second, p->table_descriptors);
1496 else
1497 tables_.emplace_back(va, p->table_descriptors);
1498 }
1499 const std::uint32_t first = first_dword(state, *p); 1540 const std::uint32_t first = first_dword(state, *p);
1500 if (first == rt::rootsig::kNoSgpr) return spilled(); 1541 if (first == rt::rootsig::kNoSgpr) return spilled();
1501 write(state, first, std::uint32_t(va)); 1542 write(state, first, std::uint32_t(va));
@@ -1613,13 +1654,22 @@ private:
1613 if ((cbv && cbv != cbv_srv_uav_heap_) || (smp && smp != sampler_heap_)) check(false); 1654 if ((cbv && cbv != cbv_srv_uav_heap_) || (smp && smp != sampler_heap_)) check(false);
1614 } 1655 }
1615 1656
1616 void note_direct_heap_indexing(const RootArgumentLayout* layout) const { 1657 void remember_table(uint64_t base, uint32_t slots) {
1658 if (!tables_.empty() && tables_.back().first == base)
1659 tables_.back().second = std::max(tables_.back().second, slots);
1660 else
1661 tables_.emplace_back(base, slots);
1662 }
1663
1664 void note_direct_heap_indexing(const RootArgumentLayout* layout) {
1617 if (!layout) return; 1665 if (!layout) return;
1618 if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) { 1666 if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) {
1619 if (!cbv_srv_uav_heap_) 1667 if (!cbv_srv_uav_heap_)
1620 note_once("draw/dispatch uses CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED with no CBV/SRV/UAV heap bound"); 1668 note_once("draw/dispatch uses CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED with no CBV/SRV/UAV heap bound");
1621 else 1669 else {
1670 remember_table(cbv_srv_uav_base_, cbv_srv_uav_heap_->GetDesc().NumDescriptors);
1622 note_once("CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED heap base tracked on list"); 1671 note_once("CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED heap base tracked on list");
1672 }
1623 } 1673 }
1624 if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) { 1674 if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) {
1625 if (!sampler_heap_) 1675 if (!sampler_heap_)
@@ -1656,21 +1706,61 @@ private:
1656 set_pipeline(state); 1706 set_pipeline(state);
1657 } 1707 }
1658 1708
1709 void reset_indirect_graphics_bindings(std::span<const D3D12_INDIRECT_ARGUMENT_DESC> writes) {
1710 static constexpr std::array<UINT, D3D12_MAX_ROOT_COST> zeros{};
1711 for (const auto& arg : writes) {
1712 switch (arg.Type) {
1713 case D3D12_INDIRECT_ARGUMENT_TYPE_VERTEX_BUFFER_VIEW:
1714 if (arg.VertexBuffer.Slot >= vertex_buffers_.size()) return check(false);
1715 set_vertex_buffers(arg.VertexBuffer.Slot, 1, nullptr);
1716 break;
1717 case D3D12_INDIRECT_ARGUMENT_TYPE_INDEX_BUFFER_VIEW: set_index_buffer(nullptr); break;
1718 case D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT:
1719 if (arg.Constant.Num32BitValuesToSet > zeros.size()) return check(false);
1720 constants(graphics_, arg.Constant.RootParameterIndex, arg.Constant.Num32BitValuesToSet,
1721 zeros.data(), arg.Constant.DestOffsetIn32BitValues);
1722 break;
1723 case compat::kIndirectArgumentIncrementingConstant:
1724 // The two leading UINTs have the same ABI on SDKs predating IncrementingConstant.
1725 constants(graphics_, arg.Constant.RootParameterIndex, 1, zeros.data(), arg.Constant.DestOffsetIn32BitValues);
1726 break;
1727 case D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT_BUFFER_VIEW:
1728 address(graphics_, arg.ConstantBufferView.RootParameterIndex, 0, rt::rootsig::ParamType::Descriptor);
1729 break;
1730 case D3D12_INDIRECT_ARGUMENT_TYPE_SHADER_RESOURCE_VIEW:
1731 address(graphics_, arg.ShaderResourceView.RootParameterIndex, 0, rt::rootsig::ParamType::Descriptor);
1732 break;
1733 case D3D12_INDIRECT_ARGUMENT_TYPE_UNORDERED_ACCESS_VIEW:
1734 address(graphics_, arg.UnorderedAccessView.RootParameterIndex, 0, rt::rootsig::ParamType::Descriptor);
1735 break;
1736 default: return check(false); // signature creation excludes other non-terminal arguments
1737 }
1738 }
1739 }
1740
1659 void set_vertex_buffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) { 1741 void set_vertex_buffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) {
1660 if (count) draw_views_stale_ = true; 1742 if (count) draw_views_stale_ = true;
1661 for (UINT i = 0; i < count; ++i) { 1743 for (UINT i = 0; i < count; ++i) {
1662 if (views) leonida::title_va_aliases().note(views[i].BufferLocation, leonida::TitleVaAliases::vertex_view); 1744 // A stream pool's GPU-only alias becomes the CPU view of the same pages (rule B, gfxgeom section 6): the
1663 if (views) leonida::title_va_aliases().want_buffer(views[i].BufferLocation, views[i].SizeInBytes); 1745 // buffer made for it, and every other view naming that range, then use one host-readable address.
1664 vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes, 1746 const std::uint64_t va = views
1747 ? leonida::title_gpu_views::to_host_view(views[i].BufferLocation,
1748 views[i].SizeInBytes)
1749 : 0;
1750 if (views) leonida::title_va_aliases().note(va, leonida::TitleVaAliases::vertex_view);
1751 if (views) leonida::title_va_aliases().want_buffer(va, views[i].SizeInBytes);
1752 vertex_buffers_[start + i] = views ? leonida::VertexBufferView{va, views[i].SizeInBytes,
1665 views[i].StrideInBytes} 1753 views[i].StrideInBytes}
1666 : leonida::VertexBufferView{}; 1754 : leonida::VertexBufferView{};
1667 } 1755 }
1668 } 1756 }
1669 1757
1670 void set_index_buffer(const D3D12_INDEX_BUFFER_VIEW* view) { 1758 void set_index_buffer(const D3D12_INDEX_BUFFER_VIEW* view) {
1671 if (view) leonida::title_va_aliases().note(view->BufferLocation, leonida::TitleVaAliases::index_view); 1759 const std::uint64_t va =
1672 if (view) leonida::title_va_aliases().want_buffer(view->BufferLocation, view->SizeInBytes); 1760 view ? leonida::title_gpu_views::to_host_view(view->BufferLocation, view->SizeInBytes) : 0;
1673 index_ = view ? leonida::IndexBufferView{view->BufferLocation, view->SizeInBytes, u(view->Format)} 1761 if (view) leonida::title_va_aliases().note(va, leonida::TitleVaAliases::index_view);
1762 if (view) leonida::title_va_aliases().want_buffer(va, view->SizeInBytes);
1763 index_ = view ? leonida::IndexBufferView{va, view->SizeInBytes, u(view->Format)}
1674 : leonida::IndexBufferView{}; 1764 : leonida::IndexBufferView{};
1675 } 1765 }
1676 1766
@@ -1678,14 +1768,19 @@ private:
1678 // 32-bit shader VA, then the sampler heap's, each only when the signature has its flag. `slot` is the first 1768 // 32-bit shader VA, then the sampler heap's, each only when the signature has its flag. `slot` is the first
1679 // dword after the arguments (RootArgumentLayout::*_arguments_end). The heaps are snapshotted for each command, 1769 // dword after the arguments (RootArgumentLayout::*_arguments_end). The heaps are snapshotted for each command,
1680 // so rebinding a heap cannot change a recorded draw or dispatch. 1770 // so rebinding a heap cannot change a recorded draw or dispatch.
1681 std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> with_direct_heaps(const RootState& state, 1771 void write_direct_heaps(const RootState& state, std::uint32_t slot,
1682 std::uint32_t slot) const { 1772 std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs>& data) const {
1683 auto data = state.data;
1684 const auto flags = state.layout->flags; 1773 const auto flags = state.layout->flags;
1685 if ((flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) && slot < data.size()) 1774 if ((flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) && slot < data.size())
1686 data[slot++] = static_cast<std::uint32_t>(cbv_srv_uav_base_); 1775 data[slot++] = static_cast<std::uint32_t>(cbv_srv_uav_base_);
1687 if ((flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) && slot < data.size()) 1776 if ((flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) && slot < data.size())
1688 data[slot] = static_cast<std::uint32_t>(sampler_base_); 1777 data[slot] = static_cast<std::uint32_t>(sampler_base_);
1778 }
1779
1780 std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> with_direct_heaps(const RootState& state,
1781 std::uint32_t slot) const {
1782 auto data = state.data;
1783 write_direct_heaps(state, slot, data);
1689 return data; 1784 return data;
1690 } 1785 }
1691 1786
@@ -1697,7 +1792,7 @@ private:
1697 // Direct and indirect dispatches. Putting the resource heap first and the sampler heap next is a hypothesis 1792 // Direct and indirect dispatches. Putting the resource heap first and the sampler heap next is a hypothesis
1698 // with partial evidence (docs/d3d12_vulkan.md section 3.4, tools/compute_heap_layout_census.py). A heap with no 1793 // with partial evidence (docs/d3d12_vulkan.md section 3.4, tools/compute_heap_layout_census.py). A heap with no
1699 // inline user-data dword left is reported once per heap kind and the explicit arguments are kept. 1794 // inline user-data dword left is reported once per heap kind and the explicit arguments are kept.
1700 std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> compute_user_data() const { 1795 std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> compute_user_data() {
1701 note_direct_heap_indexing(compute_.layout); 1796 note_direct_heap_indexing(compute_.layout);
1702 static const bool enabled = direct_heaps_enabled("LEONIDA_COMPUTE_DIRECT_HEAPS"); 1797 static const bool enabled = direct_heaps_enabled("LEONIDA_COMPUTE_DIRECT_HEAPS");
1703 if (!enabled) return compute_.data; 1798 if (!enabled) return compute_.data;
@@ -1754,8 +1849,8 @@ private:
1754 end + (resource ? 1u : 0u)); 1849 end + (resource ? 1u : 0u));
1755 std::fprintf(stderr, 1850 std::fprintf(stderr,
1756 "[rt] d3d12: graphics direct heaps: resource heap %s, sampler heap %s, after %u dwords of " 1851 "[rt] d3d12: graphics direct heaps: resource heap %s, sampler heap %s, after %u dwords of "
1757 "explicit arguments from s8 (first draw; LEONIDA_GRAPHICS_DIRECT_HEAPS=0 restores the " 1852 "explicit arguments from s8 (first draw; LEONIDA_GRAPHICS_DIRECT_HEAPS=0 "
1758 "old arguments)\n", 1853 "restores the old arguments)\n",
1759 resource_at, sampler_at, end - 8); 1854 resource_at, sampler_at, end - 8);
1760 } 1855 }
1761 return with_direct_heaps(graphics_, end); 1856 return with_direct_heaps(graphics_, end);
@@ -1817,6 +1912,51 @@ private:
1817 return draw_viewports_; 1912 return draw_viewports_;
1818 } 1913 }
1819 1914
1915 void record_indirect(leonida::IndirectKind kind, bool terminal_only, const leonida::IndirectDrawArgs& args) {
1916 if (kind == leonida::IndirectKind::draw || kind == leonida::IndirectKind::draw_indexed) {
1917 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false);
1918 // A valid zero-count call records nothing, even if graphics state has not been set.
1919 if (!args.max_count) return;
1920 // Both direct-list calls and deferred bundle calls reach this point after validation.
1921 // Compound signatures already applied their root/IA postconditions in ExecuteIndirect.
1922 if (indirect_draws_disabled()) {
1923 leonida::indirect_stats::skip(leonida::indirect_stats::Event::record_skip, leonida::indirect_stats::disabled);
1924 return check(!list_.closed());
1925 }
1926 const bool indexed = kind == leonida::IndirectKind::draw_indexed;
1927 // Only single-operation signatures inherit all graphics state. A padded stride alone cannot
1928 // distinguish one of those from a signature with root or IA arguments before the DRAW.
1929 namespace stats = leonida::indirect_stats;
1930 const auto skip = [](stats::Reason reason) { stats::skip(stats::Event::record_skip, reason); };
1931 if (!terminal_only) return skip(stats::compound);
1932 if (!graphics_pipeline_) return skip(stats::pipeline);
1933 if (!graphics_.layout) return skip(stats::root_layout);
1934 if (topology_ != D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST) return skip(stats::topology);
1935 if (indexed && (!index_.va || !index_.size ||
1936 (index_.format != DXGI_FORMAT_R16_UINT && index_.format != DXGI_FORMAT_R32_UINT)))
1937 return skip(stats::index_view);
1938 if (!(viewport_.width > 0) || !(viewport_.height > 0)) return skip(stats::viewport);
1939 note_direct_heap_indexing(graphics_.layout);
1940 record_raster_draw(indexed, 0, 0, 0, 0, 0, &args);
1941 return;
1942 }
1943 leonida::ExecuteIndirect cmd{};
1944 cmd.kind = kind;
1945 cmd.stride = args.stride;
1946 cmd.max_count = args.max_count;
1947 cmd.argument_buffer = args.argument_buffer;
1948 cmd.argument_offset = args.argument_offset;
1949 cmd.count_buffer = args.count_buffer;
1950 cmd.count_offset = args.count_offset;
1951 if (kind == leonida::IndirectKind::dispatch && args.max_count) { // the pipeline and root arguments a Dispatch would record
1952 if (!compute_pipeline_ || !compute_.layout) return check(false);
1953 note_direct_heap_indexing(compute_.layout);
1954 cmd.pipeline = compute_pipeline_;
1955 cmd.user_data = compute_user_data();
1956 }
1957 check(list_.execute_indirect(cmd));
1958 }
1959
1820 // A draw needs a graphics pipeline, a graphics root signature and a topology. 1960 // A draw needs a graphics pipeline, a graphics root signature and a topology.
1821 void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { 1961 void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) {
1822 if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); 1962 if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false);
@@ -1904,11 +2044,20 @@ private:
1904 } 2044 }
1905 } 2045 }
1906 if (!any_nonempty_viewport()) return; 2046 if (!any_nonempty_viewport()) return;
2047 record_raster_draw(indexed, count, instances, first, base_vertex, first_instance);
2048 }
2049
2050 // The same bindings and dynamic state must be frozen at the call for both APIs. Only the source of the
2051 // draw arguments differs: values here for a direct draw, or a GPU buffer read later at execution.
2052 void record_raster_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance,
2053 const leonida::IndirectDrawArgs* indirect = nullptr) {
1907 leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, index_, topology_, viewport_, 2054 leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, index_, topology_, viewport_,
1908 scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, 2055 scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances,
1909 first, base_vertex, first_instance}; 2056 first, base_vertex, first_instance};
1910 draw.viewports = draw_viewports(); 2057 draw.viewports = draw_viewports();
1911 check(list_.draw(std::move(draw))); 2058 const bool recorded = indirect ? list_.draw_indirect(std::move(draw), *indirect) : list_.draw(std::move(draw));
2059 if (indirect && recorded) leonida::indirect_stats::note(leonida::indirect_stats::Event::recorded);
2060 check(recorded);
1912 } 2061 }
1913}; 2062};
1914 2063
@@ -2625,20 +2774,22 @@ bool queue_uses_calibrated_timestamps(ID3D12CommandQueue* queue) {
2625 return q && q->uses_calibrated_timestamps(); 2774 return q && q->uses_calibrated_timestamps();
2626} 2775}
2627 2776
2628HRESULT create_command_signature(leonida::IndirectKind kind, UINT byte_stride, REFIID riid, void** out, 2777HRESULT create_command_signature(leonida::IndirectKind kind, UINT byte_stride, UINT packed_size, REFIID riid, void** out,
2629 IUnknown* device) { 2778 IUnknown* device, std::span<const D3D12_INDIRECT_ARGUMENT_DESC> prefix) {
2630 if (!out) return E_POINTER; 2779 if (!out) return E_POINTER;
2631 *out = nullptr; 2780 *out = nullptr;
2632 auto* sig = new CommandSignature(kind, byte_stride); 2781 auto* sig = new CommandSignature(kind, byte_stride, packed_size, prefix);
2633 sig->attach_device(device); 2782 sig->attach_device(device);
2634 return hand_out(sig, riid, out); 2783 return hand_out(sig, riid, out);
2635} 2784}
2636 2785
2637bool command_signature_info(ID3D12CommandSignature* signature, leonida::IndirectKind& kind, UINT& byte_stride) { 2786bool command_signature_info(ID3D12CommandSignature* signature, leonida::IndirectKind& kind, UINT& byte_stride,
2787 bool& terminal_only) {
2638 const CommandSignature* s = CommandSignature::from(signature); 2788 const CommandSignature* s = CommandSignature::from(signature);
2639 if (!s) return false; 2789 if (!s) return false;
2640 kind = s->kind(); 2790 kind = s->kind();
2641 byte_stride = s->stride(); 2791 byte_stride = s->stride();
2792 terminal_only = s->terminal_only();
2642 return true; 2793 return true;
2643} 2794}
2644 2795