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:
authorAslan2142 <jan.trencansky@protonmail.com>2026-11-26 01:01:15 +0100
committerGitHub <noreply@github.com>2026-11-26 01:01:15 +0100
commitc50ed94b35b37b697ab9c5fd176f3738480b4db5 (patch)
treefbc257236021f3e4e41b3a7e7bf062aac898a1bf /runtime/shims/d3d12_com.cpp
parent33bd07cf86bb42236635c27341359b3fe841d1d5 (diff)
parentfc71534883424112a7ea08fefe5b820fc4ee58aa (diff)
Merge pull request #2098 from Moriz82/pr/upstream-1125
Improve post-load GPU memory and pipeline handling (RT-POST-LOAD-GPU-MEMORY)
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
-rw-r--r--runtime/shims/d3d12_com.cpp201
1 files changed, 181 insertions, 20 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp
index 4a4381277..4a61ffadb 100644
--- a/runtime/shims/d3d12_com.cpp
+++ b/runtime/shims/d3d12_com.cpp
@@ -515,6 +515,15 @@ public:
515 blend_factor_ = value; 515 blend_factor_ = value;
516 } 516 }
517 517
518 // ID3D12GraphicsCommandList1: the depth-bounds test's range for the draws after it, read by pipelines that enable
519 // the test (DEPTH_STENCIL1/2). D3D12's default is [0, 1].
520 void STDMETHODCALLTYPE OMSetDepthBounds(FLOAT min, FLOAT max) override {
521 if (list_.closed()) return;
522 const std::array<float, 2> value{min, max};
523 if (defer([=](GraphicsCommandList& l) { l.depth_bounds_ = value; })) return;
524 depth_bounds_ = value;
525 }
526
518 void STDMETHODCALLTYPE OMSetStencilRef(UINT reference) override { 527 void STDMETHODCALLTYPE OMSetStencilRef(UINT reference) override {
519 if (list_.closed()) return; 528 if (list_.closed()) return;
520 if (defer([=](GraphicsCommandList& l) { l.front_stencil_ref_ = l.back_stencil_ref_ = reference; })) return; 529 if (defer([=](GraphicsCommandList& l) { l.front_stencil_ref_ = l.back_stencil_ref_ = reference; })) return;
@@ -572,6 +581,8 @@ public:
572 note_direct_heap_indexing(graphics_.layout); 581 note_direct_heap_indexing(graphics_.layout);
573 draw.mesh = true; 582 draw.mesh = true;
574 draw.viewports = draw_viewports(); 583 draw.viewports = draw_viewports();
584 draw.depth_bounds = depth_bounds_;
585 request_draw_pipeline();
575 check(list_.draw(std::move(draw))); 586 check(list_.draw(std::move(draw)));
576 } 587 }
577 588
@@ -840,8 +851,7 @@ public:
840 d3d12_trace::record("ID3D12GraphicsCommandList::ClearRenderTargetView"); 851 d3d12_trace::record("ID3D12GraphicsCommandList::ClearRenderTargetView");
841 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); 852 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
842 if (!view.ptr || !color) return check(false); 853 if (!view.ptr || !color) return check(false);
843 // LOCAL DIAGNOSTIC: what the title clears, and with which colour 854 if (leonida::fsdraw::enabled()) { // LOCAL DIAGNOSTIC: what the title clears, and with which colour
844 if (leonida::local_diagnostics()) {
845 static std::atomic<std::uint64_t> clears{0}; 855 static std::atomic<std::uint64_t> clears{0};
846 const std::uint64_t n = ++clears; 856 const std::uint64_t n = ++clears;
847 if (n <= 6 || n % 600 == 0) { 857 if (n <= 6 || n % 600 == 0) {
@@ -1017,10 +1027,11 @@ public:
1017 if (!leonida::indirect_records_fit(arg_desc.Width, argument_buffer_offset, max_command_count, 1027 if (!leonida::indirect_records_fit(arg_desc.Width, argument_buffer_offset, max_command_count,
1018 stride, sig->packed_size())) 1028 stride, sig->packed_size()))
1019 return check(false); 1029 return check(false);
1020 if (kind == leonida::IndirectKind::dispatch_mesh) { 1030 if (kind == leonida::IndirectKind::dispatch_mesh && !terminal_only) {
1021 // Not replayed yet (#3777 replays it as vkCmdDrawMeshTasksIndirectEXT): nothing is recorded, and the 1031 // A DISPATCH_MESH signature with root arguments before it is not replayed: nothing is recorded, and the
1022 // first such call says so. The list stays valid. 1032 // first such call says so. The list stays valid. A terminal-only one is recorded below as a mesh draw
1023 leonida::indirect_stats::skip(leonida::indirect_stats::Event::record_skip, leonida::indirect_stats::mesh); 1033 // (vkCmdDrawMeshTasksIndirect(Count)EXT).
1034 leonida::indirect_stats::skip(leonida::indirect_stats::Event::record_skip, leonida::indirect_stats::compound);
1024 return; 1035 return;
1025 } 1036 }
1026 if (!terminal_only && (kind == leonida::IndirectKind::draw || kind == leonida::IndirectKind::draw_indexed)) { 1037 if (!terminal_only && (kind == leonida::IndirectKind::draw || kind == leonida::IndirectKind::draw_indexed)) {
@@ -1546,8 +1557,10 @@ private:
1546 // No copy for the title's memory: the root view is 0, so the shader reads the arena's zero page instead 1557 // No copy for the title's memory: the root view is 0, so the shader reads the arena's zero page instead
1547 // of storing at heap_base + a raw title address. 1558 // of storing at heap_base + a raw title address.
1548 if (!va && raw) leonida::note_unbacked_view("root view", raw, kRootViewBytes); 1559 if (!va && raw) leonida::note_unbacked_view("root view", raw, kRootViewBytes);
1549 } else 1560 } else {
1561 if (descriptor_ring_diagnostics()) note_table_slots(va, p->table_descriptors);
1550 va = console_descriptor_table_va(va); 1562 va = console_descriptor_table_va(va);
1563 }
1551 if (type != rt::rootsig::ParamType::Descriptor && va && p->table_descriptors) 1564 if (type != rt::rootsig::ParamType::Descriptor && va && p->table_descriptors)
1552 remember_table(va, p->table_descriptors); 1565 remember_table(va, p->table_descriptors);
1553 const std::uint32_t first = first_dword(state, *p); 1566 const std::uint32_t first = first_dword(state, *p);
@@ -1574,6 +1587,9 @@ private:
1574 ID3D12DescriptorHeap* sampler_heap_ = nullptr; 1587 ID3D12DescriptorHeap* sampler_heap_ = nullptr;
1575 D3D12_GPU_VIRTUAL_ADDRESS cbv_srv_uav_base_ = 0; 1588 D3D12_GPU_VIRTUAL_ADDRESS cbv_srv_uav_base_ = 0;
1576 D3D12_GPU_VIRTUAL_ADDRESS sampler_base_ = 0; 1589 D3D12_GPU_VIRTUAL_ADDRESS sampler_base_ = 0;
1590 UINT heap_slots_[2]{}; // NumDescriptors of the bound view and sampler heaps
1591 std::uint64_t table_high_[3]{}; // this list's descriptor-ring marks: view, sampler, console heap (note_table_slots)
1592 std::uint64_t console_high_base_ = 0; // the console heap table_high_[2] is for
1577 std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED 1593 std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED
1578 std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{}; 1594 std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{};
1579 leonida::IndexBufferView index_{}; 1595 leonida::IndexBufferView index_{};
@@ -1598,6 +1614,7 @@ private:
1598 bool draw_views_stale_ = true; 1614 bool draw_views_stale_ = true;
1599 leonida::TargetView depth_{}; 1615 leonida::TargetView depth_{};
1600 std::array<float, 4> blend_factor_{1, 1, 1, 1}; 1616 std::array<float, 4> blend_factor_{1, 1, 1, 1};
1617 std::array<float, 2> depth_bounds_{0, 1};
1601 1618
1602 // The root signatures and pipeline states this recording uses. D3D12 lets an application release them right 1619 // The root signatures and pipeline states this recording uses. D3D12 lets an application release them right
1603 // after Set*RootSignature / SetPipelineState; the list keeps them until Reset or its own release, so the root 1620 // after Set*RootSignature / SetPipelineState; the list keeps them until Reset or its own release, so the root
@@ -1645,6 +1662,7 @@ private:
1645 hold(state); 1662 hold(state);
1646 compute_pipeline_ = compute_pipeline_id(state); 1663 compute_pipeline_ = compute_pipeline_id(state);
1647 graphics_pipeline_ = graphics_pipeline_id(state); 1664 graphics_pipeline_ = graphics_pipeline_id(state);
1665 if (compute_pipeline_) request_pipeline_build(compute_pipeline_, 0, false); // starts its build, if asynchronous
1648 // A pipeline this device never registered: a LEONIDA_STUB_PIPELINES stub (its translated shader is missing), 1666 // A pipeline this device never registered: a LEONIDA_STUB_PIPELINES stub (its translated shader is missing),
1649 // or one from another device. Its draws resolve to nothing in replay; its dispatches are no-ops too 1667 // or one from another device. Its draws resolve to nothing in replay; its dispatches are no-ops too
1650 // (record_dispatch), so the list still closes. 1668 // (record_dispatch), so the list still closes.
@@ -1659,6 +1677,81 @@ private:
1659 cbv_srv_uav_base_ = cbv_base; 1677 cbv_srv_uav_base_ = cbv_base;
1660 sampler_heap_ = smp; 1678 sampler_heap_ = smp;
1661 sampler_base_ = smp_base; 1679 sampler_base_ = smp_base;
1680 if (!descriptor_ring_diagnostics()) return;
1681 for (int h = 0; h < 2; ++h) {
1682 D3D12_DESCRIPTOR_HEAP_DESC desc{};
1683 D3D12_GPU_VIRTUAL_ADDRESS base = 0;
1684 heap_slots_[h] = descriptor_heap_info(h ? smp : cbv, desc, base) ? desc.NumDescriptors : 0;
1685 table_high_[h] = 0;
1686 }
1687 }
1688
1689 // note_table_slots runs on every descriptor-table bind of the title's recording threads, and takes a global lock
1690 // whenever a list's mark rises (almost every bind of a ring): only with LEONIDA_DEVICE_FAULT=1 or
1691 // LEONIDA_DESCRIPTOR_RING=1.
1692 static bool descriptor_ring_diagnostics() {
1693 static const bool on = [] {
1694 char v[2]{};
1695 return (GetEnvironmentVariableA("LEONIDA_DEVICE_FAULT", v, 2) == 1 && (v[0] == '1' || v[0] == '2')) ||
1696 (GetEnvironmentVariableA("LEONIDA_DESCRIPTOR_RING", v, 2) == 1 && v[0] == '1');
1697 }();
1698 return on;
1699 }
1700
1701 // LOCAL DIAGNOSTIC (early device loss; VIctory 3827767, a title table ring past the slots the layer backs hung
1702 // NVIDIA ~45 s in): the highest slot a descriptor table reaches in each bound heap, against the heap's slots. Logged
1703 // when a heap's high-water mark passes another eighth of the heap, every new mark past its end, and the first
1704 // tables that lie in no bound heap. A list only takes the shared lock when its own mark rises.
1705 void note_table_slots(std::uint64_t va, std::uint32_t declared) {
1706 if (!va) return;
1707 D3D12_GPU_VIRTUAL_ADDRESS bases[3]{cbv_srv_uav_base_, sampler_base_, 0};
1708 std::uint64_t counts[3]{heap_slots_[0], heap_slots_[1], 0};
1709 int heap = -1;
1710 for (int h = 0; h < 2; ++h)
1711 if (bases[h] && counts[h] && va >= bases[h] && va < bases[h] + counts[h] * descriptor_size)
1712 heap = h;
1713 std::uint64_t console_bytes = 0;
1714 if (heap < 0 && console_descriptor_heap_range(va, bases[2], console_bytes)) {
1715 heap = 2; // a console descriptor heap (Xbox slot 14) in title memory, read from its arena copy
1716 counts[2] = console_bytes / descriptor_size;
1717 }
1718 if (heap < 0) {
1719 static std::atomic<int> outside{0};
1720 if (outside.fetch_add(1) < 8)
1721 std::fprintf(stderr, "[rt] descriptor-ring: table 0x%llx in no bound heap (view heap 0x%llx, %u slots; "
1722 "sampler heap 0x%llx, %u slots)\n",
1723 (unsigned long long)va, (unsigned long long)bases[0], heap_slots_[0],
1724 (unsigned long long)bases[1], heap_slots_[1]);
1725 return;
1726 }
1727 const std::uint64_t first = (va - bases[heap]) / descriptor_size;
1728 const std::uint64_t end = first + (declared ? declared : 1);
1729 if (heap == 2 && console_high_base_ != bases[2]) {
1730 console_high_base_ = bases[2];
1731 table_high_[2] = 0;
1732 }
1733 if (end <= table_high_[heap]) return;
1734 table_high_[heap] = end;
1735 struct Mark {
1736 std::uint64_t high = 0;
1737 std::uint64_t logged = 0;
1738 };
1739 static std::mutex mutex;
1740 static std::map<std::uint64_t, Mark> marks; // by heap base
1741 std::lock_guard lock(mutex);
1742 Mark& mark = marks[bases[heap]];
1743 if (end <= mark.high) return;
1744 mark.high = end;
1745 const std::uint64_t slots = counts[heap];
1746 const std::uint64_t step = (std::max)(slots / 8, std::uint64_t{1});
1747 if (end > slots || end >= mark.logged + step) {
1748 mark.logged = end;
1749 std::fprintf(stderr, "[rt] descriptor-ring: %s heap 0x%llx: highest slot %llu (table at %llu, %u declared) "
1750 "of %llu backed%s\n",
1751 heap == 2 ? "console" : heap ? "sampler" : "view", (unsigned long long)bases[heap], (unsigned long long)(end - 1),
1752 (unsigned long long)first, declared, (unsigned long long)slots,
1753 end > slots ? " -- PAST THE HEAP" : "");
1754 }
1662 } 1755 }
1663 1756
1664 // D3D12 bundle rule: every non-null heap bound by a bundle's SetDescriptorHeaps must match the parent list's 1757 // D3D12 bundle rule: every non-null heap bound by a bundle's SetDescriptorHeaps must match the parent list's
@@ -1676,20 +1769,26 @@ private:
1676 1769
1677 void note_direct_heap_indexing(const RootArgumentLayout* layout) { 1770 void note_direct_heap_indexing(const RootArgumentLayout* layout) {
1678 if (!layout) return; 1771 if (!layout) return;
1772 // Called at every draw and dispatch: each note has its own flag, so after its first time a site skips
1773 // note_once's process-wide mutex, std::string and set lookup.
1774 static std::atomic<bool> told[4];
1775 const auto once = [](int site, const char* what) {
1776 if (!told[site].load(std::memory_order_relaxed) && !told[site].exchange(true)) note_once(what);
1777 };
1679 if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) { 1778 if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) {
1680 if (!cbv_srv_uav_heap_) 1779 if (!cbv_srv_uav_heap_)
1681 note_once("draw/dispatch uses CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED with no CBV/SRV/UAV heap bound"); 1780 once(0, "draw/dispatch uses CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED with no CBV/SRV/UAV heap bound");
1682 else { 1781 else {
1683 const auto shader_va = console_descriptor_table_va(cbv_srv_uav_base_); 1782 const auto shader_va = console_descriptor_table_va(cbv_srv_uav_base_);
1684 remember_table(shader_va, cbv_srv_uav_heap_->GetDesc().NumDescriptors); 1783 remember_table(shader_va, cbv_srv_uav_heap_->GetDesc().NumDescriptors);
1685 note_once("CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED heap base tracked on list"); 1784 once(1, "CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED heap base tracked on list");
1686 } 1785 }
1687 } 1786 }
1688 if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) { 1787 if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) {
1689 if (!sampler_heap_) 1788 if (!sampler_heap_)
1690 note_once("draw/dispatch uses SAMPLER_HEAP_DIRECTLY_INDEXED with no sampler heap bound"); 1789 once(2, "draw/dispatch uses SAMPLER_HEAP_DIRECTLY_INDEXED with no sampler heap bound");
1691 else 1790 else
1692 note_once("SAMPLER_HEAP_DIRECTLY_INDEXED heap base tracked on list"); 1791 once(3, "SAMPLER_HEAP_DIRECTLY_INDEXED heap base tracked on list");
1693 } 1792 }
1694 } 1793 }
1695 1794
@@ -1717,6 +1816,7 @@ private:
1717 draw_views_stale_ = true; 1816 draw_views_stale_ = true;
1718 depth_ = {}; 1817 depth_ = {};
1719 blend_factor_ = {1, 1, 1, 1}; 1818 blend_factor_ = {1, 1, 1, 1};
1819 depth_bounds_ = {0, 1};
1720 set_pipeline(state); 1820 set_pipeline(state);
1721 } 1821 }
1722 1822
@@ -1848,9 +1948,12 @@ private:
1848 } 1948 }
1849 const std::uint32_t end = layout.graphics_arguments_end; 1949 const std::uint32_t end = layout.graphics_arguments_end;
1850 if (end >= rt::rootsig::kMaxUserSgprs) { 1950 if (end >= rt::rootsig::kMaxUserSgprs) {
1851 note_once(layout.graphics_console 1951 static std::atomic<bool> told[2]; // per draw: the site's own flag before note_once's mutex
1852 ? "graphics direct heaps: not supplied, the explicit arguments fill s8..s31" 1952 const int site = layout.graphics_console ? 0 : 1;
1853 : "graphics direct heaps: not supplied, the signature keeps the s0 graphics layout"); 1953 if (!told[site].load(std::memory_order_relaxed) && !told[site].exchange(true))
1954 note_once(layout.graphics_console
1955 ? "graphics direct heaps: not supplied, the explicit arguments fill s8..s31"
1956 : "graphics direct heaps: not supplied, the signature keeps the s0 graphics layout");
1854 return graphics_.data; 1957 return graphics_.data;
1855 } 1958 }
1856 static std::atomic<bool> logged{false}; 1959 static std::atomic<bool> logged{false};
@@ -1934,7 +2037,41 @@ private:
1934 return draw_viewports_; 2037 return draw_viewports_;
1935 } 2038 }
1936 2039
2040 // LEONIDA_ASYNC_PIPELINES=1: the variant this draw needs (by the targets it binds) starts building on the compile
2041 // worker now, before the list is submitted (request_pipeline_build).
2042 void request_draw_pipeline() const {
2043 std::uint32_t bound = 0;
2044 for (UINT slot = 0; slot < render_target_count_ && slot < render_targets_.size(); ++slot)
2045 if (render_targets_[slot].resource) bound |= 1u << slot;
2046 request_pipeline_build(graphics_pipeline_, bound, depth_.resource != 0);
2047 }
2048
2049 // One recorder for both entry points: the direct ExecuteIndirect call and a bundle call deferred into its
2050 // parent list. A simple DRAW or DRAW_INDEXED signature becomes a stateful Draw (IndirectRasterDraw) carrying the
2051 // graphics state frozen at the call; anything else stays a legacy ExecuteIndirect command.
1937 void record_indirect(leonida::IndirectKind kind, bool terminal_only, const leonida::IndirectDrawArgs& args) { 2052 void record_indirect(leonida::IndirectKind kind, bool terminal_only, const leonida::IndirectDrawArgs& args) {
2053 if (kind == leonida::IndirectKind::dispatch_mesh) {
2054 // As DispatchMesh, with the group counts in the argument buffer: the replay draws the records with
2055 // vkCmdDrawMeshTasksIndirect(Count)EXT after its clamp pass bounds them.
2056 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false);
2057 if (!args.max_count) return;
2058 namespace stats = leonida::indirect_stats;
2059 const auto skip = [](stats::Reason reason) { stats::skip(stats::Event::record_skip, reason); };
2060 if (indirect_draws_disabled()) return skip(stats::disabled);
2061 if (!terminal_only) return skip(stats::compound);
2062 if (!graphics_pipeline_) return skip(stats::pipeline);
2063 if (!graphics_.layout) return skip(stats::root_layout);
2064 if (!(viewport_.width > 0) || !(viewport_.height > 0)) return skip(stats::viewport);
2065 leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, {}, 0, viewport_,
2066 scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, 0, 0, 0, 0, 0};
2067 note_direct_heap_indexing(graphics_.layout);
2068 draw.mesh = true;
2069 draw.depth_bounds = depth_bounds_;
2070 request_draw_pipeline();
2071 const bool recorded = list_.draw_indirect(std::move(draw), args);
2072 if (recorded) stats::note(stats::Event::recorded);
2073 return check(recorded);
2074 }
1938 if (kind == leonida::IndirectKind::draw || kind == leonida::IndirectKind::draw_indexed) { 2075 if (kind == leonida::IndirectKind::draw || kind == leonida::IndirectKind::draw_indexed) {
1939 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false); 2076 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false);
1940 // A valid zero-count call records nothing, even if graphics state has not been set. 2077 // A valid zero-count call records nothing, even if graphics state has not been set.
@@ -1985,8 +2122,12 @@ private:
1985 note_direct_heap_indexing(graphics_.layout); 2122 note_direct_heap_indexing(graphics_.layout);
1986 // No viewport (never set, or unbound with RSSetViewports(0)) or every bound viewport empty: D3D12 rasterizes 2123 // No viewport (never set, or unbound with RSSetViewports(0)) or every bound viewport empty: D3D12 rasterizes
1987 // nothing, and Vulkan forbids a 0x0 viewport, so the draw is not recorded. 2124 // nothing, and Vulkan forbids a 0x0 viewport, so the draw is not recorded.
1988 // LOCAL DIAGNOSTIC: the state each draw is recorded with 2125 static const bool draw_diag = [] {
1989 if (leonida::local_diagnostics()) { 2126 const char* value = std::getenv("LEONIDA_DRAW_DIAG");
2127 if (value) return std::strcmp(value, "0") != 0;
2128 return leonida::local_diagnostics() || leonida::fsdraw::enabled();
2129 }();
2130 if (draw_diag) {
1990 static std::atomic<std::uint64_t> draws{0}, skipped{0}; 2131 static std::atomic<std::uint64_t> draws{0}, skipped{0};
1991 const bool skip = !any_nonempty_viewport(); 2132 const bool skip = !any_nonempty_viewport();
1992 const std::uint64_t n = ++draws, k = skip ? ++skipped : skipped.load(); 2133 const std::uint64_t n = ++draws, k = skip ? ++skipped : skipped.load();
@@ -2000,16 +2141,18 @@ private:
2000 scissor_.left, scissor_.top, scissor_.right, scissor_.bottom, count, instances); 2141 scissor_.left, scissor_.top, scissor_.right, scissor_.bottom, count, instances);
2001 // LOCAL DIAGNOSTIC (fsdraw): the first draw of each pipeline, and one frame's draws every ~300 frames, 2142 // LOCAL DIAGNOSTIC (fsdraw): the first draw of each pipeline, and one frame's draws every ~300 frames,
2002 // with the pipeline's description, the root data and what the bound descriptor tables hold. 2143 // with the pipeline's description, the root data and what the bound descriptor tables hold.
2144 // Only with LEONIDA_FSDRAW_PROBE=1.
2003 static std::mutex seen_mutex; 2145 static std::mutex seen_mutex;
2004 static std::set<std::uintptr_t> seen; 2146 static std::set<std::uintptr_t> seen;
2005 bool first_seen; 2147 const bool fsdraw = leonida::fsdraw::enabled();
2006 { 2148 bool first_seen = false;
2149 if (fsdraw) {
2007 std::lock_guard lock(seen_mutex); 2150 std::lock_guard lock(seen_mutex);
2008 first_seen = seen.insert(graphics_pipeline_).second; 2151 first_seen = seen.insert(graphics_pipeline_).second;
2009 } 2152 }
2010 const bool fullscreen = render_target_count_ >= 1 && count == 3 && viewport_.width >= 1000; 2153 const bool fullscreen = render_target_count_ >= 1 && count == 3 && viewport_.width >= 1000;
2011 if (fullscreen) leonida::fsdraw::note_target(render_targets_[0].resource); 2154 if (fullscreen) leonida::fsdraw::note_target(render_targets_[0].resource);
2012 if (first_seen || (n - 1) % 600 < 2 || (n > 2 && n < 9)) { 2155 if (fsdraw && (first_seen || (n - 1) % 600 < 2 || (n > 2 && n < 9))) {
2013 std::fprintf(stderr, "[rt] fsdraw: draw #%llu pso %p%s rt0 0x%llx rts %u depth 0x%llx count %u x%u: %s\n", 2156 std::fprintf(stderr, "[rt] fsdraw: draw #%llu pso %p%s rt0 0x%llx rts %u depth 0x%llx count %u x%u: %s\n",
2014 static_cast<unsigned long long>(n), reinterpret_cast<const void*>(graphics_pipeline_), 2157 static_cast<unsigned long long>(n), reinterpret_cast<const void*>(graphics_pipeline_),
2015 first_seen ? " (first use)" : "", static_cast<unsigned long long>(render_targets_[0].resource), 2158 first_seen ? " (first use)" : "", static_cast<unsigned long long>(render_targets_[0].resource),
@@ -2077,6 +2220,8 @@ private:
2077 scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, 2220 scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances,
2078 first, base_vertex, first_instance}; 2221 first, base_vertex, first_instance};
2079 draw.viewports = draw_viewports(); 2222 draw.viewports = draw_viewports();
2223 draw.depth_bounds = depth_bounds_;
2224 request_draw_pipeline();
2080 const bool recorded = indirect ? list_.draw_indirect(std::move(draw), *indirect) : list_.draw(std::move(draw)); 2225 const bool recorded = indirect ? list_.draw_indirect(std::move(draw), *indirect) : list_.draw(std::move(draw));
2081 if (indirect && recorded) leonida::indirect_stats::note(leonida::indirect_stats::Event::recorded); 2226 if (indirect && recorded) leonida::indirect_stats::note(leonida::indirect_stats::Event::recorded);
2082 check(recorded); 2227 check(recorded);
@@ -2159,6 +2304,11 @@ public:
2159 return S_OK; 2304 return S_OK;
2160 } 2305 }
2161 2306
2307 // `fn` on the dispatcher's thread once the fence reaches `value` (rt::d3d12::fence_callback_at).
2308 void callback_at(UINT64 value, std::function<void()> fn) {
2309 context_.enqueue_fence_callback(fence_.semaphore(), value, std::move(fn));
2310 }
2311
2162 // Counts `waiter` down once this fence reaches `value`: now if it has, else via the centralized dispatcher. 2312 // Counts `waiter` down once this fence reaches `value`: now if it has, else via the centralized dispatcher.
2163 void notify(UINT64 value, std::shared_ptr<FenceWaiter> waiter) { 2313 void notify(UINT64 value, std::shared_ptr<FenceWaiter> waiter) {
2164 if (context_.is_device_lost() || fence_.completed() >= value) { 2314 if (context_.is_device_lost() || fence_.completed() >= value) {
@@ -2332,7 +2482,7 @@ public:
2332 // LOCAL DIAGNOSTIC (fsdraw): what the bound tables hold when the lists are submitted 2482 // LOCAL DIAGNOSTIC (fsdraw): what the bound tables hold when the lists are submitted
2333 if (leonida::local_diagnostics()) { 2483 if (leonida::local_diagnostics()) {
2334 static std::atomic<std::uint64_t> submits{0}; 2484 static std::atomic<std::uint64_t> submits{0};
2335 const std::uint64_t n = tables.empty() ? 0 : ++submits; 2485 const std::uint64_t n = tables.empty() || !leonida::fsdraw::enabled() ? 0 : ++submits;
2336 if (n && (n <= 6 || n % 300 == 0)) { 2486 if (n && (n <= 6 || n % 300 == 0)) {
2337 char tag[48]; 2487 char tag[48];
2338 std::snprintf(tag, sizeof tag, "submit #%llu", static_cast<unsigned long long>(n)); 2488 std::snprintf(tag, sizeof tag, "submit #%llu", static_cast<unsigned long long>(n));
@@ -2390,6 +2540,10 @@ public:
2390 if (last_result_ != leonida::ReplayResult::success) 2540 if (last_result_ != leonida::ReplayResult::success)
2391 std::fprintf(stderr, "[rt] d3d12: ExecuteCommandLists failed: %s; nothing was submitted\n", 2541 std::fprintf(stderr, "[rt] d3d12: ExecuteCommandLists failed: %s; nothing was submitted\n",
2392 name(last_result_)); 2542 name(last_result_));
2543 // A batch or Signal that failed after its call returned (held, or on the queue's worker with
2544 // LEONIDA_SUBMIT_ASYNC; reported on stderr there) is this call's result, one call late.
2545 if (last_result_ == leonida::ReplayResult::success)
2546 if (const auto held = queue_.take_held_failure(); held != leonida::ReplayResult::success) last_result_ = held;
2393 } 2547 }
2394 2548
2395 // A copy list's timestamps go to COPY_QUEUE_TIMESTAMP heaps and every other list's to TIMESTAMP heaps. 2549 // A copy list's timestamps go to COPY_QUEUE_TIMESTAMP heaps and every other list's to TIMESTAMP heaps.
@@ -2816,6 +2970,13 @@ bool command_signature_info(ID3D12CommandSignature* signature, leonida::Indirect
2816 return true; 2970 return true;
2817} 2971}
2818 2972
2973bool fence_callback_at(ID3D12Fence* fence, std::uint64_t value, std::function<void()> fn) {
2974 Fence* f = Fence::from(fence);
2975 if (!f) return false;
2976 f->callback_at(value, std::move(fn));
2977 return true;
2978}
2979
2819leonida::ReplayResult last_execute_result(ID3D12CommandQueue* queue) { 2980leonida::ReplayResult last_execute_result(ID3D12CommandQueue* queue) {
2820 if (ID3D12CommandQueue* real = d3d12_xs::queue_proxy_real_queue(queue)) queue = real; // the Xbox-ABI proxy 2981 if (ID3D12CommandQueue* real = d3d12_xs::queue_proxy_real_queue(queue)) queue = real; // the Xbox-ABI proxy
2821 CommandQueue* q = CommandQueue::from(queue); 2982 CommandQueue* q = CommandQueue::from(queue);