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:
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
-rw-r--r--runtime/shims/d3d12_com.cpp243
1 files changed, 212 insertions, 31 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp
index 961514bcf..4a61ffadb 100644
--- a/runtime/shims/d3d12_com.cpp
+++ b/runtime/shims/d3d12_com.cpp
@@ -222,8 +222,11 @@ public:
222 const leonida::CommandList& commands() const { return list_; } 222 const leonida::CommandList& commands() const { return list_; }
223 // The descriptor tables this recording binds (console VAs), for the scan before a submission. 223 // The descriptor tables this recording binds (console VAs), for the scan before a submission.
224 const std::vector<std::pair<std::uint64_t, std::uint32_t>>& tables() const { return tables_; } 224 const std::vector<std::pair<std::uint64_t, std::uint32_t>>& tables() const { return tables_; }
225 // The pipeline states and root signatures the recording names (it holds a reference to each until Reset). 225 // The pipeline states and root signatures the recording names (it holds a reference to each until Reset), and
226 // the objects a shim handed it with hold_until_reset.
226 const std::vector<IUnknown*>& pinned() const { return held_; } 227 const std::vector<IUnknown*>& pinned() const { return held_; }
228 // rt::d3d12::hold_until_reset: an object the recording uses that its caller does not keep (a shim's own scratch).
229 void hold_for_recording(IUnknown* object) { hold(object); }
227 bool needs_all_sampled_views() const { 230 bool needs_all_sampled_views() const {
228 if (directly_indexed_) return true; 231 if (directly_indexed_) return true;
229 for (const auto& command : list_.commands()) { 232 for (const auto& command : list_.commands()) {
@@ -512,6 +515,15 @@ public:
512 blend_factor_ = value; 515 blend_factor_ = value;
513 } 516 }
514 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
515 void STDMETHODCALLTYPE OMSetStencilRef(UINT reference) override { 527 void STDMETHODCALLTYPE OMSetStencilRef(UINT reference) override {
516 if (list_.closed()) return; 528 if (list_.closed()) return;
517 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;
@@ -569,6 +581,8 @@ public:
569 note_direct_heap_indexing(graphics_.layout); 581 note_direct_heap_indexing(graphics_.layout);
570 draw.mesh = true; 582 draw.mesh = true;
571 draw.viewports = draw_viewports(); 583 draw.viewports = draw_viewports();
584 draw.depth_bounds = depth_bounds_;
585 request_draw_pipeline();
572 check(list_.draw(std::move(draw))); 586 check(list_.draw(std::move(draw)));
573 } 587 }
574 588
@@ -837,8 +851,7 @@ public:
837 d3d12_trace::record("ID3D12GraphicsCommandList::ClearRenderTargetView"); 851 d3d12_trace::record("ID3D12GraphicsCommandList::ClearRenderTargetView");
838 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); 852 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
839 if (!view.ptr || !color) return check(false); 853 if (!view.ptr || !color) return check(false);
840 // 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
841 if (leonida::local_diagnostics()) {
842 static std::atomic<std::uint64_t> clears{0}; 855 static std::atomic<std::uint64_t> clears{0};
843 const std::uint64_t n = ++clears; 856 const std::uint64_t n = ++clears;
844 if (n <= 6 || n % 600 == 0) { 857 if (n <= 6 || n % 600 == 0) {
@@ -1014,10 +1027,11 @@ public:
1014 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,
1015 stride, sig->packed_size())) 1028 stride, sig->packed_size()))
1016 return check(false); 1029 return check(false);
1017 if (kind == leonida::IndirectKind::dispatch_mesh) { 1030 if (kind == leonida::IndirectKind::dispatch_mesh && !terminal_only) {
1018 // 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
1019 // 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
1020 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);
1021 return; 1035 return;
1022 } 1036 }
1023 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)) {
@@ -1543,8 +1557,10 @@ private:
1543 // 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
1544 // of storing at heap_base + a raw title address. 1558 // of storing at heap_base + a raw title address.
1545 if (!va && raw) leonida::note_unbacked_view("root view", raw, kRootViewBytes); 1559 if (!va && raw) leonida::note_unbacked_view("root view", raw, kRootViewBytes);
1546 } else 1560 } else {
1561 if (descriptor_ring_diagnostics()) note_table_slots(va, p->table_descriptors);
1547 va = console_descriptor_table_va(va); 1562 va = console_descriptor_table_va(va);
1563 }
1548 if (type != rt::rootsig::ParamType::Descriptor && va && p->table_descriptors) 1564 if (type != rt::rootsig::ParamType::Descriptor && va && p->table_descriptors)
1549 remember_table(va, p->table_descriptors); 1565 remember_table(va, p->table_descriptors);
1550 const std::uint32_t first = first_dword(state, *p); 1566 const std::uint32_t first = first_dword(state, *p);
@@ -1571,6 +1587,9 @@ private:
1571 ID3D12DescriptorHeap* sampler_heap_ = nullptr; 1587 ID3D12DescriptorHeap* sampler_heap_ = nullptr;
1572 D3D12_GPU_VIRTUAL_ADDRESS cbv_srv_uav_base_ = 0; 1588 D3D12_GPU_VIRTUAL_ADDRESS cbv_srv_uav_base_ = 0;
1573 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
1574 std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED 1593 std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED
1575 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_{};
1576 leonida::IndexBufferView index_{}; 1595 leonida::IndexBufferView index_{};
@@ -1595,6 +1614,7 @@ private:
1595 bool draw_views_stale_ = true; 1614 bool draw_views_stale_ = true;
1596 leonida::TargetView depth_{}; 1615 leonida::TargetView depth_{};
1597 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};
1598 1618
1599 // 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
1600 // 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
@@ -1642,6 +1662,7 @@ private:
1642 hold(state); 1662 hold(state);
1643 compute_pipeline_ = compute_pipeline_id(state); 1663 compute_pipeline_ = compute_pipeline_id(state);
1644 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
1645 // 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),
1646 // 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
1647 // (record_dispatch), so the list still closes. 1668 // (record_dispatch), so the list still closes.
@@ -1656,6 +1677,81 @@ private:
1656 cbv_srv_uav_base_ = cbv_base; 1677 cbv_srv_uav_base_ = cbv_base;
1657 sampler_heap_ = smp; 1678 sampler_heap_ = smp;
1658 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 }
1659 } 1755 }
1660 1756
1661 // 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
@@ -1673,19 +1769,26 @@ private:
1673 1769
1674 void note_direct_heap_indexing(const RootArgumentLayout* layout) { 1770 void note_direct_heap_indexing(const RootArgumentLayout* layout) {
1675 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 };
1676 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) {
1677 if (!cbv_srv_uav_heap_) 1779 if (!cbv_srv_uav_heap_)
1678 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");
1679 else { 1781 else {
1680 remember_table(cbv_srv_uav_base_, cbv_srv_uav_heap_->GetDesc().NumDescriptors); 1782 const auto shader_va = console_descriptor_table_va(cbv_srv_uav_base_);
1681 note_once("CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED heap base tracked on list"); 1783 remember_table(shader_va, cbv_srv_uav_heap_->GetDesc().NumDescriptors);
1784 once(1, "CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED heap base tracked on list");
1682 } 1785 }
1683 } 1786 }
1684 if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) { 1787 if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) {
1685 if (!sampler_heap_) 1788 if (!sampler_heap_)
1686 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");
1687 else 1790 else
1688 note_once("SAMPLER_HEAP_DIRECTLY_INDEXED heap base tracked on list"); 1791 once(3, "SAMPLER_HEAP_DIRECTLY_INDEXED heap base tracked on list");
1689 } 1792 }
1690 } 1793 }
1691 1794
@@ -1713,6 +1816,7 @@ private:
1713 draw_views_stale_ = true; 1816 draw_views_stale_ = true;
1714 depth_ = {}; 1817 depth_ = {};
1715 blend_factor_ = {1, 1, 1, 1}; 1818 blend_factor_ = {1, 1, 1, 1};
1819 depth_bounds_ = {0, 1};
1716 set_pipeline(state); 1820 set_pipeline(state);
1717 } 1821 }
1718 1822
@@ -1781,10 +1885,14 @@ private:
1781 void write_direct_heaps(const RootState& state, std::uint32_t slot, 1885 void write_direct_heaps(const RootState& state, std::uint32_t slot,
1782 std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs>& data) const { 1886 std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs>& data) const {
1783 const auto flags = state.layout->flags; 1887 const auto flags = state.layout->flags;
1784 if ((flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) && slot < data.size()) 1888 if ((flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) && slot < data.size()) {
1785 data[slot++] = static_cast<std::uint32_t>(cbv_srv_uav_base_); 1889 const D3D12_GPU_VIRTUAL_ADDRESS va = console_descriptor_table_va(cbv_srv_uav_base_);
1786 if ((flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) && slot < data.size()) 1890 data[slot++] = static_cast<std::uint32_t>(va);
1787 data[slot] = static_cast<std::uint32_t>(sampler_base_); 1891 }
1892 if ((flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) && slot < data.size()) {
1893 const D3D12_GPU_VIRTUAL_ADDRESS va = console_descriptor_table_va(sampler_base_);
1894 data[slot] = static_cast<std::uint32_t>(va);
1895 }
1788 } 1896 }
1789 1897
1790 std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> with_direct_heaps(const RootState& state, 1898 std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> with_direct_heaps(const RootState& state,
@@ -1840,9 +1948,12 @@ private:
1840 } 1948 }
1841 const std::uint32_t end = layout.graphics_arguments_end; 1949 const std::uint32_t end = layout.graphics_arguments_end;
1842 if (end >= rt::rootsig::kMaxUserSgprs) { 1950 if (end >= rt::rootsig::kMaxUserSgprs) {
1843 note_once(layout.graphics_console 1951 static std::atomic<bool> told[2]; // per draw: the site's own flag before note_once's mutex
1844 ? "graphics direct heaps: not supplied, the explicit arguments fill s8..s31" 1952 const int site = layout.graphics_console ? 0 : 1;
1845 : "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");
1846 return graphics_.data; 1957 return graphics_.data;
1847 } 1958 }
1848 static std::atomic<bool> logged{false}; 1959 static std::atomic<bool> logged{false};
@@ -1850,13 +1961,17 @@ private:
1850 const bool resource = layout.flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED; 1961 const bool resource = layout.flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED;
1851 const bool sampler = layout.flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED; 1962 const bool sampler = layout.flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED;
1852 char resource_at[48] = "none", sampler_at[48] = "none"; 1963 char resource_at[48] = "none", sampler_at[48] = "none";
1853 if (resource) 1964 if (resource) {
1965 const auto va = console_descriptor_table_va(cbv_srv_uav_base_);
1854 std::snprintf(resource_at, sizeof resource_at, "0x%llx at s%u", 1966 std::snprintf(resource_at, sizeof resource_at, "0x%llx at s%u",
1855 static_cast<unsigned long long>(std::uint32_t(cbv_srv_uav_base_)), end); 1967 static_cast<unsigned long long>(std::uint32_t(va)), end);
1856 if (sampler && end + (resource ? 1u : 0u) < rt::rootsig::kMaxUserSgprs) 1968 }
1969 if (sampler && end + (resource ? 1u : 0u) < rt::rootsig::kMaxUserSgprs) {
1970 const auto va = console_descriptor_table_va(sampler_base_);
1857 std::snprintf(sampler_at, sizeof sampler_at, "0x%llx at s%u", 1971 std::snprintf(sampler_at, sizeof sampler_at, "0x%llx at s%u",
1858 static_cast<unsigned long long>(std::uint32_t(sampler_base_)), 1972 static_cast<unsigned long long>(std::uint32_t(va)),
1859 end + (resource ? 1u : 0u)); 1973 end + (resource ? 1u : 0u));
1974 }
1860 std::fprintf(stderr, 1975 std::fprintf(stderr,
1861 "[rt] d3d12: graphics direct heaps: resource heap %s, sampler heap %s, after %u dwords of " 1976 "[rt] d3d12: graphics direct heaps: resource heap %s, sampler heap %s, after %u dwords of "
1862 "explicit arguments from s8 (first draw; LEONIDA_GRAPHICS_DIRECT_HEAPS=0 " 1977 "explicit arguments from s8 (first draw; LEONIDA_GRAPHICS_DIRECT_HEAPS=0 "
@@ -1922,7 +2037,41 @@ private:
1922 return draw_viewports_; 2037 return draw_viewports_;
1923 } 2038 }
1924 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.
1925 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 }
1926 if (kind == leonida::IndirectKind::draw || kind == leonida::IndirectKind::draw_indexed) { 2075 if (kind == leonida::IndirectKind::draw || kind == leonida::IndirectKind::draw_indexed) {
1927 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false); 2076 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false);
1928 // 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.
@@ -1973,8 +2122,12 @@ private:
1973 note_direct_heap_indexing(graphics_.layout); 2122 note_direct_heap_indexing(graphics_.layout);
1974 // 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
1975 // 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.
1976 // LOCAL DIAGNOSTIC: the state each draw is recorded with 2125 static const bool draw_diag = [] {
1977 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) {
1978 static std::atomic<std::uint64_t> draws{0}, skipped{0}; 2131 static std::atomic<std::uint64_t> draws{0}, skipped{0};
1979 const bool skip = !any_nonempty_viewport(); 2132 const bool skip = !any_nonempty_viewport();
1980 const std::uint64_t n = ++draws, k = skip ? ++skipped : skipped.load(); 2133 const std::uint64_t n = ++draws, k = skip ? ++skipped : skipped.load();
@@ -1988,16 +2141,18 @@ private:
1988 scissor_.left, scissor_.top, scissor_.right, scissor_.bottom, count, instances); 2141 scissor_.left, scissor_.top, scissor_.right, scissor_.bottom, count, instances);
1989 // 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,
1990 // 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.
1991 static std::mutex seen_mutex; 2145 static std::mutex seen_mutex;
1992 static std::set<std::uintptr_t> seen; 2146 static std::set<std::uintptr_t> seen;
1993 bool first_seen; 2147 const bool fsdraw = leonida::fsdraw::enabled();
1994 { 2148 bool first_seen = false;
2149 if (fsdraw) {
1995 std::lock_guard lock(seen_mutex); 2150 std::lock_guard lock(seen_mutex);
1996 first_seen = seen.insert(graphics_pipeline_).second; 2151 first_seen = seen.insert(graphics_pipeline_).second;
1997 } 2152 }
1998 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;
1999 if (fullscreen) leonida::fsdraw::note_target(render_targets_[0].resource); 2154 if (fullscreen) leonida::fsdraw::note_target(render_targets_[0].resource);
2000 if (first_seen || (n - 1) % 600 < 2 || (n > 2 && n < 9)) { 2155 if (fsdraw && (first_seen || (n - 1) % 600 < 2 || (n > 2 && n < 9))) {
2001 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",
2002 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_),
2003 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),
@@ -2065,6 +2220,8 @@ private:
2065 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,
2066 first, base_vertex, first_instance}; 2221 first, base_vertex, first_instance};
2067 draw.viewports = draw_viewports(); 2222 draw.viewports = draw_viewports();
2223 draw.depth_bounds = depth_bounds_;
2224 request_draw_pipeline();
2068 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));
2069 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);
2070 check(recorded); 2227 check(recorded);
@@ -2147,6 +2304,11 @@ public:
2147 return S_OK; 2304 return S_OK;
2148 } 2305 }
2149 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
2150 // 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.
2151 void notify(UINT64 value, std::shared_ptr<FenceWaiter> waiter) { 2313 void notify(UINT64 value, std::shared_ptr<FenceWaiter> waiter) {
2152 if (context_.is_device_lost() || fence_.completed() >= value) { 2314 if (context_.is_device_lost() || fence_.completed() >= value) {
@@ -2188,7 +2350,8 @@ public:
2188 2350
2189 // What a held batch (queue_holds.h) keeps alive until it has run: the pipeline states and root signatures its 2351 // What a held batch (queue_holds.h) keeps alive until it has run: the pipeline states and root signatures its
2190 // lists name, which the application may release and Reset away as soon as ExecuteCommandLists returns. Resources 2352 // lists name, which the application may release and Reset away as soon as ExecuteCommandLists returns. Resources
2191 // are not held (a D3D12 application keeps them until the fence it signals; the docs, section 4.4, list this). 2353 // are not held (a D3D12 application keeps them until the fence it signals; the docs, section 4.4, list this),
2354 // except a shim's own the recording uses (hold_until_reset), which are pinned with them.
2192 static std::shared_ptr<void> pin_lists(const std::vector<const GraphicsCommandList*>& lists) { 2355 static std::shared_ptr<void> pin_lists(const std::vector<const GraphicsCommandList*>& lists) {
2193 auto objects = std::shared_ptr<std::vector<IUnknown*>>(new std::vector<IUnknown*>, [](auto* held) { 2356 auto objects = std::shared_ptr<std::vector<IUnknown*>>(new std::vector<IUnknown*>, [](auto* held) {
2194 for (IUnknown* object : *held) object->Release(); 2357 for (IUnknown* object : *held) object->Release();
@@ -2319,7 +2482,7 @@ public:
2319 // 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
2320 if (leonida::local_diagnostics()) { 2483 if (leonida::local_diagnostics()) {
2321 static std::atomic<std::uint64_t> submits{0}; 2484 static std::atomic<std::uint64_t> submits{0};
2322 const std::uint64_t n = tables.empty() ? 0 : ++submits; 2485 const std::uint64_t n = tables.empty() || !leonida::fsdraw::enabled() ? 0 : ++submits;
2323 if (n && (n <= 6 || n % 300 == 0)) { 2486 if (n && (n <= 6 || n % 300 == 0)) {
2324 char tag[48]; 2487 char tag[48];
2325 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));
@@ -2377,6 +2540,10 @@ public:
2377 if (last_result_ != leonida::ReplayResult::success) 2540 if (last_result_ != leonida::ReplayResult::success)
2378 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",
2379 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;
2380 } 2547 }
2381 2548
2382 // 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.
@@ -2803,6 +2970,13 @@ bool command_signature_info(ID3D12CommandSignature* signature, leonida::Indirect
2803 return true; 2970 return true;
2804} 2971}
2805 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
2806leonida::ReplayResult last_execute_result(ID3D12CommandQueue* queue) { 2980leonida::ReplayResult last_execute_result(ID3D12CommandQueue* queue) {
2807 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
2808 CommandQueue* q = CommandQueue::from(queue); 2982 CommandQueue* q = CommandQueue::from(queue);
@@ -2817,6 +2991,13 @@ bool front_back_stencil_ref(ID3D12GraphicsCommandList8* list, UINT& front, UINT&
2817 return true; 2991 return true;
2818} 2992}
2819 2993
2994bool hold_until_reset(ID3D12GraphicsCommandList* list, IUnknown* object) {
2995 GraphicsCommandList* l = GraphicsCommandList::from(list);
2996 if (!l || !object) return false;
2997 l->hold_for_recording(object);
2998 return true;
2999}
3000
2820bool recorded_commands(ID3D12GraphicsCommandList* list, std::vector<leonida::Command>& commands) { 3001bool recorded_commands(ID3D12GraphicsCommandList* list, std::vector<leonida::Command>& commands) {
2821 const GraphicsCommandList* l = GraphicsCommandList::from(list); 3002 const GraphicsCommandList* l = GraphicsCommandList::from(list);
2822 if (!l) return false; 3003 if (!l) return false;