diff options
| author | Moriz82 <72836413+Moriz82@users.noreply.github.com> | 2026-11-24 11:42:12 -0600 |
|---|---|---|
| committer | Moriz82 <72836413+Moriz82@users.noreply.github.com> | 2026-11-24 11:42:12 -0600 |
| commit | 273b3bdf9d6e736442ad584ad464df6e86ee928b (patch) | |
| tree | 3886a0af7c74ffa7d9415d662d8dc0fb708f1c08 /runtime/shims/d3d12_com.cpp | |
| parent | ece4ba5b31ebc6f345c8e29eae1261f2f780057a (diff) | |
[integration] Linux/Proton and native Windows: boot through the story load (RT-PROTON-STORY-LOAD)
One squashed commit of a fork that was developed on a Linux/Proton machine and
tested on two native Windows machines. On all of them the retail 1.0.2.1
package now launches, shows its launch movie, first-run pages and landing
prompt, runs the story load to its end with the loading slideshow drawing its
artwork, and enters the world phase. A first world frame is not verified yet.
docs/linux_proton.md has the setup, a healthy boot's timeline, the machines
and where it stops.
Runtime: frames in the title's own window; DirectStorage pinned reads,
page-list scatter/gather, BCPack and unshuffle with a locally generated
predictor file; pooled, device-local and deferred image backing for streamed
and placed textures; enhanced barriers into LAYOUT_UNDEFINED; a bindless table
that degrades instead of refusing a submission; a descriptor hash finalizer
(off until a library carries it); directly indexed heap offsets for compute
dispatches and VS/PS/GS draws; title buffer windows outside a fragmented
arena; opt-in device-local copies of streamed buffer views; write-fault
retries under the tracker lock, shared trackers and write-watched title memory
for native Windows; device-loss, compute-input and memory probes.
Translator: loop bounds at structured exits, the eight FidelityFX programs
that use a packed-dot form the disassembler skips, buffer bounds modes per
view kind, a compaction option, the hash finalizer option.
Tools and scripts: Linux boot scripts (Wine/Proton, no network), a Windows
boot script, the paired compute translation script.
No game data. AI-assisted (Claude Code and Codex agents), reviewed and booted
by people on the machines named in docs/linux_proton.md.
Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 410 |
1 files changed, 369 insertions, 41 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 4f4904e96..cfd2d4b6e 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp | |||
| @@ -6,20 +6,26 @@ | |||
| 6 | #include "d3d12_trace.h" | 6 | #include "d3d12_trace.h" |
| 7 | #include "d3d12_xs_device.h" | 7 | #include "d3d12_xs_device.h" |
| 8 | #include "../src/gfx10_descriptors.h" | 8 | #include "../src/gfx10_descriptors.h" |
| 9 | #include "../src/title_va_alias.h" | ||
| 10 | #include "../src/fsdraw_probe.h" // LOCAL DIAGNOSTIC | ||
| 9 | 11 | ||
| 10 | #include <algorithm> | 12 | #include <algorithm> |
| 11 | #include <array> | 13 | #include <array> |
| 12 | #include <atomic> | 14 | #include <atomic> |
| 13 | #include <bit> | 15 | #include <bit> |
| 16 | #include <chrono> | ||
| 14 | #include <condition_variable> | 17 | #include <condition_variable> |
| 15 | #include <functional> | 18 | #include <functional> |
| 19 | #include <map> | ||
| 16 | #include <memory> | 20 | #include <memory> |
| 17 | #include <optional> | 21 | #include <optional> |
| 18 | #include <cstdio> | 22 | #include <cstdio> |
| 19 | #include <cstring> | 23 | #include <cstring> |
| 20 | #include <mutex> | 24 | #include <mutex> |
| 21 | #include <set> | 25 | #include <set> |
| 26 | #include <source_location> | ||
| 22 | #include <string> | 27 | #include <string> |
| 28 | #include <string_view> | ||
| 23 | #include <thread> | 29 | #include <thread> |
| 24 | #include <utility> | 30 | #include <utility> |
| 25 | #include <vector> | 31 | #include <vector> |
| @@ -198,6 +204,8 @@ public: | |||
| 198 | } | 204 | } |
| 199 | 205 | ||
| 200 | const leonida::CommandList& commands() const { return list_; } | 206 | const leonida::CommandList& commands() const { return list_; } |
| 207 | // The descriptor tables this recording binds (console VAs), for the scan before a submission. | ||
| 208 | const std::vector<std::pair<std::uint64_t, std::uint32_t>>& tables() const { return tables_; } | ||
| 201 | // The pipeline states and root signatures the recording names (it holds a reference to each until Reset). | 209 | // The pipeline states and root signatures the recording names (it holds a reference to each until Reset). |
| 202 | const std::vector<IUnknown*>& pinned() const { return held_; } | 210 | const std::vector<IUnknown*>& pinned() const { return held_; } |
| 203 | UINT front_stencil_ref() const { return front_stencil_ref_; } | 211 | UINT front_stencil_ref() const { return front_stencil_ref_; } |
| @@ -234,6 +242,7 @@ public: | |||
| 234 | record_into(command_allocator); | 242 | record_into(command_allocator); |
| 235 | invalid_ = false; | 243 | invalid_ = false; |
| 236 | calls_.clear(); | 244 | calls_.clear(); |
| 245 | tables_.clear(); | ||
| 237 | release_held(); // what the old recording used: it was replayed at ExecuteCommandLists | 246 | release_held(); // what the old recording used: it was replayed at ExecuteCommandLists |
| 238 | reset_state(state); // Reset returns every list state to its default | 247 | reset_state(state); // Reset returns every list state to its default |
| 239 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) set_initial_pipeline(state); // reset_state holds it | 248 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) set_initial_pipeline(state); // reset_state holds it |
| @@ -487,7 +496,7 @@ public: | |||
| 487 | if (x > 65535 || y > 65535 || z > 65535 || std::uint64_t(x) * y * z > (1u << 22)) return check(false); | 496 | if (x > 65535 || y > 65535 || z > 65535 || std::uint64_t(x) * y * z > (1u << 22)) return check(false); |
| 488 | if (!graphics_pipeline_ || !graphics_.layout) return check(false); | 497 | if (!graphics_pipeline_ || !graphics_.layout) return check(false); |
| 489 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; // as record_draw: nothing is rasterized | 498 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; // as record_draw: nothing is rasterized |
| 490 | leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, {}, 0, viewport_, | 499 | leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, {}, 0, viewport_, |
| 491 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; | 500 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; |
| 492 | draw.mesh = true; | 501 | draw.mesh = true; |
| 493 | check(list_.draw(std::move(draw))); | 502 | check(list_.draw(std::move(draw))); |
| @@ -526,29 +535,49 @@ public: | |||
| 526 | switch (group[g].Type) { | 535 | switch (group[g].Type) { |
| 527 | case D3D12_BARRIER_TYPE_GLOBAL: { | 536 | case D3D12_BARRIER_TYPE_GLOBAL: { |
| 528 | const auto& b = group[g].pGlobalBarriers[i]; | 537 | const auto& b = group[g].pGlobalBarriers[i]; |
| 529 | check(list_.barrier(leonida::GlobalBarrier{u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), | 538 | if (!list_.barrier(leonida::GlobalBarrier{u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), |
| 530 | u(b.AccessAfter)})); | 539 | u(b.AccessAfter)}) && |
| 540 | reject()) | ||
| 541 | std::fprintf(stderr, "[rt] d3d12: global barrier sync 0x%x->0x%x access 0x%x->0x%x\n", | ||
| 542 | u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), u(b.AccessAfter)); | ||
| 531 | break; | 543 | break; |
| 532 | } | 544 | } |
| 533 | case D3D12_BARRIER_TYPE_BUFFER: { | 545 | case D3D12_BARRIER_TYPE_BUFFER: { |
| 534 | const auto& b = group[g].pBufferBarriers[i]; | 546 | const auto& b = group[g].pBufferBarriers[i]; |
| 535 | check(list_.barrier(leonida::BufferBarrier{u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), | 547 | if (!list_.barrier(leonida::BufferBarrier{u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), |
| 536 | u(b.AccessAfter), resource_id(b.pResource)})); | 548 | u(b.AccessAfter), resource_id(b.pResource)}) && |
| 549 | reject()) { | ||
| 550 | std::fprintf(stderr, "[rt] d3d12: buffer barrier sync 0x%x->0x%x access 0x%x->0x%x offset " | ||
| 551 | "0x%llx size 0x%llx\n", | ||
| 552 | u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), u(b.AccessAfter), | ||
| 553 | static_cast<unsigned long long>(b.Offset), static_cast<unsigned long long>(b.Size)); | ||
| 554 | describe_barrier_resource(b.pResource); | ||
| 555 | } | ||
| 537 | break; | 556 | break; |
| 538 | } | 557 | } |
| 539 | case D3D12_BARRIER_TYPE_TEXTURE: { | 558 | case D3D12_BARRIER_TYPE_TEXTURE: { |
| 540 | const auto& b = group[g].pTextureBarriers[i]; | 559 | const auto& b = group[g].pTextureBarriers[i]; |
| 541 | const auto& r = b.Subresources; | 560 | const auto& r = b.Subresources; |
| 542 | check(list_.barrier(leonida::TextureBarrier{ | 561 | if (!list_.barrier(leonida::TextureBarrier{ |
| 543 | u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), u(b.AccessAfter), u(b.LayoutBefore), | 562 | u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), u(b.AccessAfter), u(b.LayoutBefore), |
| 544 | u(b.LayoutAfter), resource_id(b.pResource), | 563 | u(b.LayoutAfter), resource_id(b.pResource), |
| 545 | {r.IndexOrFirstMipLevel, r.NumMipLevels, r.FirstArraySlice, r.NumArraySlices, r.FirstPlane, | 564 | {r.IndexOrFirstMipLevel, r.NumMipLevels, r.FirstArraySlice, r.NumArraySlices, |
| 546 | r.NumPlanes}, | 565 | r.FirstPlane, r.NumPlanes}, |
| 547 | u(b.Flags)})); | 566 | u(b.Flags)}) && |
| 567 | reject()) { | ||
| 568 | std::fprintf(stderr, | ||
| 569 | "[rt] d3d12: texture barrier sync 0x%x->0x%x access 0x%x->0x%x layout %u->%u " | ||
| 570 | "mips %u+%u layers %u+%u planes %u+%u flags 0x%x\n", | ||
| 571 | u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), u(b.AccessAfter), | ||
| 572 | u(b.LayoutBefore), u(b.LayoutAfter), r.IndexOrFirstMipLevel, r.NumMipLevels, | ||
| 573 | r.FirstArraySlice, r.NumArraySlices, r.FirstPlane, r.NumPlanes, u(b.Flags)); | ||
| 574 | describe_barrier_resource(b.pResource); | ||
| 575 | } | ||
| 548 | break; | 576 | break; |
| 549 | } | 577 | } |
| 550 | default: | 578 | default: |
| 551 | check(false); | 579 | if (reject()) |
| 580 | std::fprintf(stderr, "[rt] d3d12: barrier group type %d\n", int(group[g].Type)); | ||
| 552 | } | 581 | } |
| 553 | } | 582 | } |
| 554 | } | 583 | } |
| @@ -616,7 +645,24 @@ public: | |||
| 616 | if (!dst || !src) return check(false); | 645 | if (!dst || !src) return check(false); |
| 617 | leonida::CopyTexture copy{location(*dst), x, y, z, location(*src), box != nullptr, {}}; | 646 | leonida::CopyTexture copy{location(*dst), x, y, z, location(*src), box != nullptr, {}}; |
| 618 | if (box) copy.box = {box->left, box->top, box->front, box->right, box->bottom, box->back}; | 647 | if (box) copy.box = {box->left, box->top, box->front, box->right, box->bottom, box->back}; |
| 619 | check(list_.copy_texture(copy)); | 648 | if (!list_.copy_texture(copy) && reject()) { |
| 649 | for (const auto* l : {dst, src}) { | ||
| 650 | const char* side = l == dst ? "dst" : "src"; | ||
| 651 | if (l->Type != D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT) { | ||
| 652 | std::fprintf(stderr, "[rt] d3d12: %s type %d resource %p subresource %u\n", side, int(l->Type), | ||
| 653 | static_cast<void*>(l->pResource), l->SubresourceIndex); | ||
| 654 | continue; | ||
| 655 | } | ||
| 656 | const auto& f = l->PlacedFootprint; | ||
| 657 | std::fprintf(stderr, "[rt] d3d12: %s footprint resource %p offset 0x%llx format %d %ux%ux%u pitch %u\n", | ||
| 658 | side, static_cast<void*>(l->pResource), static_cast<unsigned long long>(f.Offset), | ||
| 659 | int(f.Footprint.Format), f.Footprint.Width, f.Footprint.Height, f.Footprint.Depth, | ||
| 660 | f.Footprint.RowPitch); | ||
| 661 | } | ||
| 662 | if (box) | ||
| 663 | std::fprintf(stderr, "[rt] d3d12: at %u,%u,%u box %u,%u,%u-%u,%u,%u\n", x, y, z, box->left, box->top, | ||
| 664 | box->front, box->right, box->bottom, box->back); | ||
| 665 | } | ||
| 620 | } | 666 | } |
| 621 | 667 | ||
| 622 | void STDMETHODCALLTYPE CopyResource(ID3D12Resource* dst, ID3D12Resource* src) override { | 668 | void STDMETHODCALLTYPE CopyResource(ID3D12Resource* dst, ID3D12Resource* src) override { |
| @@ -698,6 +744,17 @@ public: | |||
| 698 | d3d12_trace::record("ID3D12GraphicsCommandList::ClearRenderTargetView"); | 744 | d3d12_trace::record("ID3D12GraphicsCommandList::ClearRenderTargetView"); |
| 699 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); | 745 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); |
| 700 | if (!view.ptr || !color) return check(false); | 746 | if (!view.ptr || !color) return check(false); |
| 747 | { // LOCAL DIAGNOSTIC: what the title clears, and with which colour | ||
| 748 | static std::atomic<std::uint64_t> clears{0}; | ||
| 749 | const std::uint64_t n = ++clears; | ||
| 750 | if (n <= 6 || n % 600 == 0) { | ||
| 751 | const auto& t = *reinterpret_cast<const leonida::TargetView*>(view.ptr); | ||
| 752 | std::fprintf(stderr, "[rt] present-probe: ClearRenderTargetView #%llu resource 0x%llx mip %u layers %u+%u " | ||
| 753 | "colour %.3f %.3f %.3f %.3f rects %u\n", | ||
| 754 | static_cast<unsigned long long>(n), static_cast<unsigned long long>(t.resource), t.mip, | ||
| 755 | t.first_layer, t.layer_count, color[0], color[1], color[2], color[3], count); | ||
| 756 | } | ||
| 757 | } | ||
| 701 | check(list_.clear_render_target(*reinterpret_cast<const leonida::TargetView*>(view.ptr), | 758 | check(list_.clear_render_target(*reinterpret_cast<const leonida::TargetView*>(view.ptr), |
| 702 | {color[0], color[1], color[2], color[3]}, to_rects(count, rects))); | 759 | {color[0], color[1], color[2], color[3]}, to_rects(count, rects))); |
| 703 | } | 760 | } |
| @@ -834,19 +891,27 @@ public: | |||
| 834 | } else if (count_buffer_offset != 0) { | 891 | } else if (count_buffer_offset != 0) { |
| 835 | return check(false); | 892 | return check(false); |
| 836 | } | 893 | } |
| 837 | // The record size is checked for every call: D3D12 rejects a signature whose stride cannot hold a record | 894 | if (kind == leonida::IndirectKind::dispatch_mesh) { |
| 838 | // even when no command runs. The argument span is what the signature can walk, max_command_count * stride: | 895 | // Not replayed yet (#3777 replays it as vkCmdDrawMeshTasksIndirectEXT): nothing is recorded, and the |
| 839 | // the host refuses a range that ends past the buffer (`240` with stride `20` in a `256`-byte buffer), and | 896 | // first such call says so. The list stays valid. |
| 840 | // it checks the offset itself when max_command_count is 0, where the required bytes are none. | 897 | static std::atomic<bool> logged{false}; |
| 898 | if (!logged.exchange(true, std::memory_order_relaxed)) | ||
| 899 | std::fprintf(stderr, "[rt] d3d12: ExecuteIndirect with a DISPATCH_MESH signature is not replayed yet; " | ||
| 900 | "nothing was recorded\n"); | ||
| 901 | return; | ||
| 902 | } | ||
| 841 | const UINT64 record_size = | 903 | const UINT64 record_size = |
| 842 | kind == leonida::IndirectKind::draw ? sizeof(D3D12_DRAW_ARGUMENTS) | 904 | kind == leonida::IndirectKind::draw ? sizeof(D3D12_DRAW_ARGUMENTS) |
| 843 | : kind == leonida::IndirectKind::draw_indexed ? sizeof(D3D12_DRAW_INDEXED_ARGUMENTS) | 905 | : kind == leonida::IndirectKind::draw_indexed ? sizeof(D3D12_DRAW_INDEXED_ARGUMENTS) |
| 844 | : kind == leonida::IndirectKind::dispatch ? sizeof(D3D12_DISPATCH_ARGUMENTS) | 906 | : kind == leonida::IndirectKind::dispatch ? sizeof(D3D12_DISPATCH_ARGUMENTS) |
| 845 | : 0; | 907 | : 0; |
| 846 | if (!record_size || stride < record_size || stride % 4 != 0) return check(false); | 908 | if (!record_size || stride < record_size || stride % 4 != 0) return check(false); |
| 847 | if (argument_buffer_offset > arg_desc.Width || | 909 | // Native D3D12 checks through the last record, with no argument range to check at max_command_count == 0. |
| 848 | UINT64(max_command_count) > (arg_desc.Width - argument_buffer_offset) / stride) | 910 | if (max_command_count) { |
| 849 | return check(false); | 911 | const UINT64 required = UINT64(max_command_count - 1) * stride + record_size; |
| 912 | if (argument_buffer_offset > arg_desc.Width || required > arg_desc.Width - argument_buffer_offset) | ||
| 913 | return check(false); | ||
| 914 | } | ||
| 850 | leonida::ExecuteIndirect cmd{}; | 915 | leonida::ExecuteIndirect cmd{}; |
| 851 | cmd.kind = kind; | 916 | cmd.kind = kind; |
| 852 | cmd.stride = stride; | 917 | cmd.stride = stride; |
| @@ -858,7 +923,7 @@ public: | |||
| 858 | if (kind == leonida::IndirectKind::dispatch && max_command_count) { // the pipeline and root arguments a Dispatch would record | 923 | if (kind == leonida::IndirectKind::dispatch && max_command_count) { // the pipeline and root arguments a Dispatch would record |
| 859 | if (!compute_pipeline_ || !compute_.layout) return check(false); | 924 | if (!compute_pipeline_ || !compute_.layout) return check(false); |
| 860 | cmd.pipeline = compute_pipeline_; | 925 | cmd.pipeline = compute_pipeline_; |
| 861 | cmd.user_data = compute_.data; | 926 | cmd.user_data = compute_user_data(); |
| 862 | } | 927 | } |
| 863 | check(list_.execute_indirect(cmd)); | 928 | check(list_.execute_indirect(cmd)); |
| 864 | } | 929 | } |
| @@ -1016,8 +1081,35 @@ private: | |||
| 1016 | return out; | 1081 | return out; |
| 1017 | } | 1082 | } |
| 1018 | 1083 | ||
| 1019 | void check(bool ok) { | 1084 | // Records an invalid call: Close then returns E_INVALIDARG and ExecuteCommandLists refuses the list. The first |
| 1020 | if (!ok) invalid_ = true; | 1085 | // rejection at each call site is logged with the method and line, so a failed Close names its cause; reject() |
| 1086 | // returns true for that first one, when a caller may print the arguments too. | ||
| 1087 | bool reject(std::source_location where = std::source_location::current()) { | ||
| 1088 | invalid_ = true; | ||
| 1089 | static std::array<std::atomic<bool>, 4096> seen{}; | ||
| 1090 | const std::uint_least32_t line = where.line(); | ||
| 1091 | if (line < seen.size() && seen[line].exchange(true, std::memory_order_relaxed)) return false; | ||
| 1092 | std::string_view method = where.function_name(); | ||
| 1093 | if (const auto at = method.find("GraphicsCommandList::"); at != std::string_view::npos) | ||
| 1094 | method.remove_prefix(at); | ||
| 1095 | if (const auto paren = method.find('('); paren != std::string_view::npos) method = method.substr(0, paren); | ||
| 1096 | std::fprintf(stderr, "[rt] d3d12: %.*s rejected a call (d3d12_com.cpp:%u): the list's Close returns " | ||
| 1097 | "E_INVALIDARG and ExecuteCommandLists refuses it\n", | ||
| 1098 | int(method.size()), method.data(), unsigned(line)); | ||
| 1099 | return true; | ||
| 1100 | } | ||
| 1101 | void check(bool ok, std::source_location where = std::source_location::current()) { | ||
| 1102 | if (!ok) reject(where); | ||
| 1103 | } | ||
| 1104 | static void describe_barrier_resource(ID3D12Resource* resource) { | ||
| 1105 | if (!resource) return void(std::fprintf(stderr, "[rt] d3d12: resource null\n")); | ||
| 1106 | const D3D12_RESOURCE_DESC desc = resource->GetDesc(); | ||
| 1107 | std::fprintf(stderr, | ||
| 1108 | "[rt] d3d12: resource %p dimension %d format %d %llux%ux%u mips %u samples %u layout %d " | ||
| 1109 | "flags 0x%x\n", | ||
| 1110 | static_cast<void*>(resource), int(desc.Dimension), int(desc.Format), | ||
| 1111 | static_cast<unsigned long long>(desc.Width), desc.Height, desc.DepthOrArraySize, desc.MipLevels, | ||
| 1112 | desc.SampleDesc.Count, int(desc.Layout), unsigned(desc.Flags)); | ||
| 1021 | } | 1113 | } |
| 1022 | 1114 | ||
| 1023 | void stop_recording() { | 1115 | void stop_recording() { |
| @@ -1240,11 +1332,23 @@ private: | |||
| 1240 | return; | 1332 | return; |
| 1241 | const RootArgumentLayout::Parameter* p = parameter(state, index, type); | 1333 | const RootArgumentLayout::Parameter* p = parameter(state, index, type); |
| 1242 | if (!p) return; | 1334 | if (!p) return; |
| 1335 | // A root view into the title's own memory (a slot-133 buffer, a constant ring) gets the console VA of a GPU | ||
| 1336 | // copy of it, so the shader's heap_base + VA reaches it (src/title_va_alias.h). A descriptor table is in one | ||
| 1337 | // of the layer's heaps: kept, and its buffer V#s are rewritten the same way before each submission. | ||
| 1338 | // A table in a console descriptor heap is read from the heap's arena copy (console_descriptor_table_va). | ||
| 1339 | if (type == rt::rootsig::ParamType::Descriptor) | ||
| 1340 | va = leonida::title_va_aliases().resolve(va, kRootViewBytes, leonida::TitleVaAliases::root_view); | ||
| 1341 | else | ||
| 1342 | va = console_descriptor_table_va(va); | ||
| 1343 | if (type != rt::rootsig::ParamType::Descriptor && va && p->table_descriptors && | ||
| 1344 | (tables_.empty() || tables_.back().first != va)) | ||
| 1345 | tables_.push_back({va, p->table_descriptors}); | ||
| 1243 | const std::uint32_t first = first_dword(state, *p); | 1346 | const std::uint32_t first = first_dword(state, *p); |
| 1244 | write(state, first, std::uint32_t(va)); | 1347 | write(state, first, std::uint32_t(va)); |
| 1245 | // In the compute layout a descriptor table is one dword, the 32-bit VA of its first descriptor (the heaps | 1348 | // In the console layouts a descriptor table is one dword, the 32-bit VA of its first descriptor (the heaps |
| 1246 | // live below 4 GiB); a root view keeps both halves. The graphics layout gives every address two dwords. | 1349 | // live below 4 GiB); a root view keeps both halves. pack()'s graphics layout gives every address two dwords. |
| 1247 | if (!(is_compute(state) && type == rt::rootsig::ParamType::Table)) write(state, first + 1, std::uint32_t(va >> 32)); | 1350 | const bool console = is_compute(state) || state.layout->graphics_console; |
| 1351 | if (!(console && type == rt::rootsig::ParamType::Table)) write(state, first + 1, std::uint32_t(va >> 32)); | ||
| 1248 | } | 1352 | } |
| 1249 | 1353 | ||
| 1250 | D3D12_COMMAND_LIST_TYPE type_; | 1354 | D3D12_COMMAND_LIST_TYPE type_; |
| @@ -1256,6 +1360,7 @@ private: | |||
| 1256 | RootState compute_; | 1360 | RootState compute_; |
| 1257 | RootState graphics_; | 1361 | RootState graphics_; |
| 1258 | std::uintptr_t compute_pipeline_ = 0; | 1362 | std::uintptr_t compute_pipeline_ = 0; |
| 1363 | bool unregistered_pipeline_ = false; // the bound PSO has no pipeline here (a stub): see set_pipeline | ||
| 1259 | std::uintptr_t graphics_pipeline_ = 0; | 1364 | std::uintptr_t graphics_pipeline_ = 0; |
| 1260 | ID3D12DescriptorHeap* cbv_srv_uav_heap_ = nullptr; | 1365 | ID3D12DescriptorHeap* cbv_srv_uav_heap_ = nullptr; |
| 1261 | ID3D12DescriptorHeap* sampler_heap_ = nullptr; | 1366 | ID3D12DescriptorHeap* sampler_heap_ = nullptr; |
| @@ -1264,6 +1369,10 @@ private: | |||
| 1264 | std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED | 1369 | std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED |
| 1265 | std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{}; | 1370 | std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{}; |
| 1266 | leonida::IndexBufferView index_{}; | 1371 | leonida::IndexBufferView index_{}; |
| 1372 | // SetGraphicsRootDescriptorTable / SetComputeRootDescriptorTable: VA, CBV/SRV/UAV slots the signature declares | ||
| 1373 | std::vector<std::pair<std::uint64_t, std::uint32_t>> tables_; | ||
| 1374 | // The span a root view may read: D3D12's largest constant buffer (4096 x 16 bytes). | ||
| 1375 | static constexpr std::uint64_t kRootViewBytes = 65536; | ||
| 1267 | leonida::Viewport viewport_{}; | 1376 | leonida::Viewport viewport_{}; |
| 1268 | leonida::Rect scissor_{}; | 1377 | leonida::Rect scissor_{}; |
| 1269 | std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT> render_targets_{}; | 1378 | std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT> render_targets_{}; |
| @@ -1321,7 +1430,11 @@ private: | |||
| 1321 | hold(state); | 1430 | hold(state); |
| 1322 | compute_pipeline_ = compute_pipeline_id(state); | 1431 | compute_pipeline_ = compute_pipeline_id(state); |
| 1323 | graphics_pipeline_ = graphics_pipeline_id(state); | 1432 | graphics_pipeline_ = graphics_pipeline_id(state); |
| 1324 | if (state && !compute_pipeline_ && !graphics_pipeline_) | 1433 | // A pipeline this device never registered: a LEONIDA_STUB_PIPELINES stub (its translated shader is missing), |
| 1434 | // or one from another device. Its draws resolve to nothing in replay; its dispatches are no-ops too | ||
| 1435 | // (record_dispatch), so the list still closes. | ||
| 1436 | unregistered_pipeline_ = state && !compute_pipeline_ && !graphics_pipeline_; | ||
| 1437 | if (unregistered_pipeline_) | ||
| 1325 | stub("ID3D12GraphicsCommandList::SetPipelineState(pipeline from another device)"); | 1438 | stub("ID3D12GraphicsCommandList::SetPipelineState(pipeline from another device)"); |
| 1326 | } | 1439 | } |
| 1327 | 1440 | ||
| @@ -1345,13 +1458,13 @@ private: | |||
| 1345 | if (!cbv_srv_uav_heap_) | 1458 | if (!cbv_srv_uav_heap_) |
| 1346 | note_once("draw/dispatch uses CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED with no CBV/SRV/UAV heap bound"); | 1459 | note_once("draw/dispatch uses CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED with no CBV/SRV/UAV heap bound"); |
| 1347 | else | 1460 | else |
| 1348 | note_once("CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED heap base tracked on list (SGPR slot open, section 3.4)"); | 1461 | note_once("CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED heap base tracked on list"); |
| 1349 | } | 1462 | } |
| 1350 | if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) { | 1463 | if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) { |
| 1351 | if (!sampler_heap_) | 1464 | if (!sampler_heap_) |
| 1352 | note_once("draw/dispatch uses SAMPLER_HEAP_DIRECTLY_INDEXED with no sampler heap bound"); | 1465 | note_once("draw/dispatch uses SAMPLER_HEAP_DIRECTLY_INDEXED with no sampler heap bound"); |
| 1353 | else | 1466 | else |
| 1354 | note_once("SAMPLER_HEAP_DIRECTLY_INDEXED heap base tracked on list (SGPR slot open, section 3.4)"); | 1467 | note_once("SAMPLER_HEAP_DIRECTLY_INDEXED heap base tracked on list"); |
| 1355 | } | 1468 | } |
| 1356 | } | 1469 | } |
| 1357 | 1470 | ||
| @@ -1378,21 +1491,114 @@ private: | |||
| 1378 | 1491 | ||
| 1379 | void set_vertex_buffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) { | 1492 | void set_vertex_buffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) { |
| 1380 | if (count) draw_views_stale_ = true; | 1493 | if (count) draw_views_stale_ = true; |
| 1381 | for (UINT i = 0; i < count; ++i) | 1494 | for (UINT i = 0; i < count; ++i) { |
| 1495 | if (views) leonida::title_va_aliases().note(views[i].BufferLocation, leonida::TitleVaAliases::vertex_view); | ||
| 1496 | if (views) leonida::title_va_aliases().want_buffer(views[i].BufferLocation, views[i].SizeInBytes); | ||
| 1382 | vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes, | 1497 | vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes, |
| 1383 | views[i].StrideInBytes} | 1498 | views[i].StrideInBytes} |
| 1384 | : leonida::VertexBufferView{}; | 1499 | : leonida::VertexBufferView{}; |
| 1500 | } | ||
| 1385 | } | 1501 | } |
| 1386 | 1502 | ||
| 1387 | void set_index_buffer(const D3D12_INDEX_BUFFER_VIEW* view) { | 1503 | void set_index_buffer(const D3D12_INDEX_BUFFER_VIEW* view) { |
| 1504 | if (view) leonida::title_va_aliases().note(view->BufferLocation, leonida::TitleVaAliases::index_view); | ||
| 1505 | if (view) leonida::title_va_aliases().want_buffer(view->BufferLocation, view->SizeInBytes); | ||
| 1388 | index_ = view ? leonida::IndexBufferView{view->BufferLocation, view->SizeInBytes, u(view->Format)} | 1506 | index_ = view ? leonida::IndexBufferView{view->BufferLocation, view->SizeInBytes, u(view->Format)} |
| 1389 | : leonida::IndexBufferView{}; | 1507 | : leonida::IndexBufferView{}; |
| 1390 | } | 1508 | } |
| 1391 | 1509 | ||
| 1510 | // The compiled console ABI appends directly indexed heaps after the explicit root arguments: the resource heap's | ||
| 1511 | // 32-bit shader VA, then the sampler heap's, each only when the signature has its flag. `slot` is the first | ||
| 1512 | // dword after the arguments (RootArgumentLayout::*_arguments_end). The heaps are snapshotted for each command, | ||
| 1513 | // so rebinding a heap cannot change a recorded draw or dispatch. | ||
| 1514 | std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> with_direct_heaps(const RootState& state, | ||
| 1515 | std::uint32_t slot) const { | ||
| 1516 | auto data = state.data; | ||
| 1517 | const auto flags = state.layout->flags; | ||
| 1518 | if ((flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) && slot < data.size()) | ||
| 1519 | data[slot++] = static_cast<std::uint32_t>(cbv_srv_uav_base_); | ||
| 1520 | if ((flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) && slot < data.size()) | ||
| 1521 | data[slot] = static_cast<std::uint32_t>(sampler_base_); | ||
| 1522 | return data; | ||
| 1523 | } | ||
| 1524 | |||
| 1525 | static bool direct_heaps_enabled(const char* variable) { | ||
| 1526 | const char* v = std::getenv(variable); | ||
| 1527 | return !(v && std::string_view(v) == "0"); | ||
| 1528 | } | ||
| 1529 | |||
| 1530 | std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> compute_user_data() const { | ||
| 1531 | static const bool enabled = direct_heaps_enabled("LEONIDA_COMPUTE_DIRECT_HEAPS"); | ||
| 1532 | if (!enabled) return compute_.data; | ||
| 1533 | return with_direct_heaps(compute_, compute_.layout->compute_arguments_end); | ||
| 1534 | } | ||
| 1535 | |||
| 1536 | // Graphics: vertex, pixel and geometry shaders share one user-data block and the s8 layout, so the heaps after | ||
| 1537 | // the explicit arguments serve every one of those stages (a pixel shader of a signature whose arguments end at | ||
| 1538 | // s11 reads the resource heap from s11). A mesh shader reads its arguments from s16, which is not modelled | ||
| 1539 | // (RootArgumentLayout, D3D-448): its pixel shader gets the heaps, the mesh stage does not. | ||
| 1540 | // LEONIDA_GRAPHICS_DIRECT_HEAPS=0 restores the previous arguments. | ||
| 1541 | std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> graphics_user_data() const { | ||
| 1542 | constexpr UINT direct = D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED | | ||
| 1543 | D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED; | ||
| 1544 | const RootArgumentLayout& layout = *graphics_.layout; | ||
| 1545 | if (!(layout.flags & direct)) return graphics_.data; | ||
| 1546 | static const bool enabled = direct_heaps_enabled("LEONIDA_GRAPHICS_DIRECT_HEAPS"); | ||
| 1547 | if (!enabled) { | ||
| 1548 | note_once("graphics direct heaps: off (LEONIDA_GRAPHICS_DIRECT_HEAPS=0), draws keep the explicit " | ||
| 1549 | "arguments only"); | ||
| 1550 | return graphics_.data; | ||
| 1551 | } | ||
| 1552 | const std::uint32_t end = layout.graphics_arguments_end; | ||
| 1553 | if (end >= rt::rootsig::kMaxUserSgprs) { | ||
| 1554 | note_once(layout.graphics_console | ||
| 1555 | ? "graphics direct heaps: not supplied, the explicit arguments fill s8..s31" | ||
| 1556 | : "graphics direct heaps: not supplied, the signature keeps the s0 graphics layout"); | ||
| 1557 | return graphics_.data; | ||
| 1558 | } | ||
| 1559 | static std::atomic<bool> logged{false}; | ||
| 1560 | if (!logged.exchange(true, std::memory_order_relaxed)) { | ||
| 1561 | const bool resource = layout.flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED; | ||
| 1562 | const bool sampler = layout.flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED; | ||
| 1563 | char resource_at[48] = "none", sampler_at[48] = "none"; | ||
| 1564 | if (resource) | ||
| 1565 | std::snprintf(resource_at, sizeof resource_at, "0x%llx at s%u", | ||
| 1566 | static_cast<unsigned long long>(std::uint32_t(cbv_srv_uav_base_)), end); | ||
| 1567 | if (sampler && end + (resource ? 1u : 0u) < rt::rootsig::kMaxUserSgprs) | ||
| 1568 | std::snprintf(sampler_at, sizeof sampler_at, "0x%llx at s%u", | ||
| 1569 | static_cast<unsigned long long>(std::uint32_t(sampler_base_)), | ||
| 1570 | end + (resource ? 1u : 0u)); | ||
| 1571 | std::fprintf(stderr, | ||
| 1572 | "[rt] d3d12: graphics direct heaps: resource heap %s, sampler heap %s, after %u dwords of " | ||
| 1573 | "explicit arguments from s8 (first draw; LEONIDA_GRAPHICS_DIRECT_HEAPS=0 restores the " | ||
| 1574 | "old arguments)\n", | ||
| 1575 | resource_at, sampler_at, end - 8); | ||
| 1576 | } | ||
| 1577 | return with_direct_heaps(graphics_, end); | ||
| 1578 | } | ||
| 1579 | |||
| 1392 | void record_dispatch(UINT x, UINT y, UINT z) { | 1580 | void record_dispatch(UINT x, UINT y, UINT z) { |
| 1393 | if (!compute_pipeline_ || !compute_.layout) return check(false); | 1581 | if (!compute_pipeline_ && unregistered_pipeline_) return; // a stub pipeline's dispatch: nothing to run |
| 1582 | if (!compute_pipeline_) return check(false); // separate lines: the rejection log names which | ||
| 1583 | if (!compute_.layout) return check(false); | ||
| 1584 | static const bool probe = std::getenv("LEONIDA_COMPUTE_INPUTS") != nullptr; | ||
| 1585 | if (probe) { | ||
| 1586 | static std::mutex mutex; | ||
| 1587 | static std::set<std::tuple<std::uintptr_t, const RootArgumentLayout*, UINT64>> seen; | ||
| 1588 | std::lock_guard lock(mutex); | ||
| 1589 | if (seen.emplace(compute_pipeline_, compute_.layout, cbv_srv_uav_base_).second) { | ||
| 1590 | std::fprintf(stderr, "[rt] compute-layout: pso=%p flags=%x bound-heap=0x%llx shader-heap=0x%llx", | ||
| 1591 | reinterpret_cast<void*>(compute_pipeline_), compute_.layout->flags, | ||
| 1592 | (unsigned long long)cbv_srv_uav_base_, | ||
| 1593 | (unsigned long long)console_descriptor_table_va(cbv_srv_uav_base_)); | ||
| 1594 | for (const auto& p : compute_.layout->parameters) | ||
| 1595 | std::fprintf(stderr, " (s%u type=%u const=%u slots=%u)", p.compute_offset, | ||
| 1596 | unsigned(p.type), p.constant_count, p.table_descriptors); | ||
| 1597 | std::fputc('\n', stderr); | ||
| 1598 | } | ||
| 1599 | } | ||
| 1394 | note_direct_heap_indexing(compute_.layout); | 1600 | note_direct_heap_indexing(compute_.layout); |
| 1395 | check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); | 1601 | check(list_.dispatch({compute_pipeline_, compute_user_data(), x, y, z})); |
| 1396 | } | 1602 | } |
| 1397 | 1603 | ||
| 1398 | // The views block a draw records. Rebuilt only when the render targets or the bound vertex buffers change. | 1604 | // The views block a draw records. Rebuilt only when the render targets or the bound vertex buffers change. |
| @@ -1414,8 +1620,87 @@ private: | |||
| 1414 | note_direct_heap_indexing(graphics_.layout); | 1620 | note_direct_heap_indexing(graphics_.layout); |
| 1415 | // No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing, | 1621 | // No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing, |
| 1416 | // and Vulkan forbids a 0x0 viewport, so the draw is not recorded. | 1622 | // and Vulkan forbids a 0x0 viewport, so the draw is not recorded. |
| 1623 | { // LOCAL DIAGNOSTIC: the state each draw is recorded with | ||
| 1624 | static std::atomic<std::uint64_t> draws{0}, skipped{0}; | ||
| 1625 | const bool skip = !(viewport_.width > 0) || !(viewport_.height > 0); | ||
| 1626 | const std::uint64_t n = ++draws, k = skip ? ++skipped : skipped.load(); | ||
| 1627 | if (n <= 8 || n % 1200 == 0) | ||
| 1628 | std::fprintf(stderr, "[rt] present-probe: draw #%llu (%s; %llu skipped so far) pso %p rts %u rt0 0x%llx " | ||
| 1629 | "mip %u viewport %.0fx%.0f at %.0f,%.0f scissor %d,%d-%d,%d count %u x%u\n", | ||
| 1630 | static_cast<unsigned long long>(n), skip ? "SKIPPED: no viewport" : "recorded", | ||
| 1631 | static_cast<unsigned long long>(k), reinterpret_cast<const void*>(graphics_pipeline_), | ||
| 1632 | render_target_count_, static_cast<unsigned long long>(render_targets_[0].resource), | ||
| 1633 | render_targets_[0].mip, viewport_.width, viewport_.height, viewport_.x, viewport_.y, | ||
| 1634 | scissor_.left, scissor_.top, scissor_.right, scissor_.bottom, count, instances); | ||
| 1635 | // LOCAL DIAGNOSTIC (fsdraw): the first draw of each pipeline, and one frame's draws every ~300 frames, | ||
| 1636 | // with the pipeline's description, the root data and what the bound descriptor tables hold. | ||
| 1637 | static std::mutex seen_mutex; | ||
| 1638 | static std::set<std::uintptr_t> seen; | ||
| 1639 | bool first_seen; | ||
| 1640 | { | ||
| 1641 | std::lock_guard lock(seen_mutex); | ||
| 1642 | first_seen = seen.insert(graphics_pipeline_).second; | ||
| 1643 | } | ||
| 1644 | const bool fullscreen = render_target_count_ >= 1 && count == 3 && viewport_.width >= 1000; | ||
| 1645 | if (fullscreen) leonida::fsdraw::note_target(render_targets_[0].resource); | ||
| 1646 | if (first_seen || (n - 1) % 600 < 2 || (n > 2 && n < 9)) { | ||
| 1647 | std::fprintf(stderr, "[rt] fsdraw: draw #%llu pso %p%s rt0 0x%llx rts %u depth 0x%llx count %u x%u: %s\n", | ||
| 1648 | static_cast<unsigned long long>(n), reinterpret_cast<const void*>(graphics_pipeline_), | ||
| 1649 | first_seen ? " (first use)" : "", static_cast<unsigned long long>(render_targets_[0].resource), | ||
| 1650 | render_target_count_, static_cast<unsigned long long>(depth_.resource), count, instances, | ||
| 1651 | leonida::fsdraw::pipeline(graphics_pipeline_).c_str()); | ||
| 1652 | std::fprintf(stderr, "[rt] fsdraw: draw #%llu layout %p user data [%s] cbv heap %p base 0x%llx sampler " | ||
| 1653 | "base 0x%llx tables %zu\n", | ||
| 1654 | static_cast<unsigned long long>(n), static_cast<const void*>(graphics_.layout), | ||
| 1655 | leonida::fsdraw::hex_words(graphics_.data.data(), graphics_.data.size()).c_str(), | ||
| 1656 | static_cast<const void*>(cbv_srv_uav_heap_), | ||
| 1657 | static_cast<unsigned long long>(cbv_srv_uav_base_), | ||
| 1658 | static_cast<unsigned long long>(sampler_base_), tables_.size()); | ||
| 1659 | for (std::size_t slot = 0; slot < 2; ++slot) { // the vertex buffers the blit's VS reads | ||
| 1660 | const auto& vb = vertex_buffers_[slot]; | ||
| 1661 | if (!vb.va) continue; | ||
| 1662 | std::uint32_t words[12]{}; | ||
| 1663 | bool read = false; | ||
| 1664 | MEMORY_BASIC_INFORMATION mbi{}; | ||
| 1665 | if (vb.va < leonida::fsdraw::arena_size()) { | ||
| 1666 | read = leonida::fsdraw::arena_words(vb.va, words, 12); | ||
| 1667 | } else if (VirtualQuery(reinterpret_cast<const void*>(static_cast<std::uintptr_t>(vb.va)), &mbi, sizeof mbi) && | ||
| 1668 | mbi.State == MEM_COMMIT && !(mbi.Protect & (PAGE_NOACCESS | PAGE_GUARD)) && mbi.Protect) { | ||
| 1669 | std::memcpy(words, reinterpret_cast<const void*>(static_cast<std::uintptr_t>(vb.va)), sizeof words); | ||
| 1670 | read = true; | ||
| 1671 | } | ||
| 1672 | std::fprintf(stderr, "[rt] fsdraw: draw #%llu vb%zu va 0x%llx size %u stride %u: %s [%s]\n", | ||
| 1673 | static_cast<unsigned long long>(n), slot, static_cast<unsigned long long>(vb.va), | ||
| 1674 | unsigned(vb.size), unsigned(vb.stride), read ? "read" : "unreadable", | ||
| 1675 | read ? leonida::fsdraw::hex_words(words, 12).c_str() : ""); | ||
| 1676 | } | ||
| 1677 | D3D12_DESCRIPTOR_HEAP_DESC heap_desc{}; | ||
| 1678 | std::uintptr_t heap_cpu = 0; | ||
| 1679 | if (cbv_srv_uav_heap_) { | ||
| 1680 | heap_desc = cbv_srv_uav_heap_->GetDesc(); | ||
| 1681 | heap_cpu = cbv_srv_uav_heap_->GetCPUDescriptorHandleForHeapStart().ptr; | ||
| 1682 | } | ||
| 1683 | char tag[48]; | ||
| 1684 | std::snprintf(tag, sizeof tag, "draw #%llu", static_cast<unsigned long long>(n)); | ||
| 1685 | for (const auto& [shader_va, slots] : tables_) { | ||
| 1686 | const std::uint64_t va = leonida::fsdraw::table_origin(shader_va); | ||
| 1687 | const std::uint64_t end = cbv_srv_uav_base_ + std::uint64_t(heap_desc.NumDescriptors) * 32; | ||
| 1688 | const bool in_heap = heap_cpu && va >= cbv_srv_uav_base_ && va + 32 <= end; | ||
| 1689 | const auto* cpu = in_heap ? reinterpret_cast<const std::uint8_t*>(heap_cpu + (va - cbv_srv_uav_base_)) | ||
| 1690 | : nullptr; | ||
| 1691 | const bool console = heap_cpu == std::uintptr_t(cbv_srv_uav_base_); | ||
| 1692 | if (shader_va != va) | ||
| 1693 | std::fprintf(stderr, "[rt] fsdraw: %s table title va 0x%llx is shader va 0x%llx\n", tag, | ||
| 1694 | static_cast<unsigned long long>(va), static_cast<unsigned long long>(shader_va)); | ||
| 1695 | leonida::fsdraw::describe_table(tag, shader_va, slots, cpu, | ||
| 1696 | !in_heap ? "none (not in the bound CBV/SRV/UAV heap)" | ||
| 1697 | : console ? "console (CPU address = GPU VA, title memory)" | ||
| 1698 | : "layer heap (arena)"); | ||
| 1699 | } | ||
| 1700 | } | ||
| 1701 | } | ||
| 1417 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; | 1702 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; |
| 1418 | leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, index_, topology_, viewport_, | 1703 | leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, index_, topology_, viewport_, |
| 1419 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, | 1704 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, |
| 1420 | first, base_vertex, first_instance}; | 1705 | first, base_vertex, first_instance}; |
| 1421 | check(list_.draw(std::move(draw))); | 1706 | check(list_.draw(std::move(draw))); |
| @@ -1469,12 +1754,14 @@ public: | |||
| 1469 | context_.enqueue_fence_callback(semaphore, queued, [context, semaphore, queued, value, held = held_] { | 1754 | context_.enqueue_fence_callback(semaphore, queued, [context, semaphore, queued, value, held = held_] { |
| 1470 | std::uint64_t now = 0; | 1755 | std::uint64_t now = 0; |
| 1471 | bool signaled = false; | 1756 | bool signaled = false; |
| 1472 | if (context->vk.vkGetSemaphoreCounterValue(context->device(), semaphore, &now) == VK_SUCCESS && | 1757 | if (context->observe_result(context->vk.vkGetSemaphoreCounterValue(context->device(), semaphore, &now), |
| 1758 | "vkGetSemaphoreCounterValue (fence callback)") == VK_SUCCESS && | ||
| 1473 | now >= queued && now < value) { | 1759 | now >= queued && now < value) { |
| 1474 | VkSemaphoreSignalInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO}; | 1760 | VkSemaphoreSignalInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO}; |
| 1475 | info.semaphore = semaphore; | 1761 | info.semaphore = semaphore; |
| 1476 | info.value = value; | 1762 | info.value = value; |
| 1477 | signaled = context->vk.vkSignalSemaphore(context->device(), &info) == VK_SUCCESS; | 1763 | signaled = context->observe_result(context->vk.vkSignalSemaphore(context->device(), &info), |
| 1764 | "vkSignalSemaphore (fence callback)") == VK_SUCCESS; | ||
| 1478 | } | 1765 | } |
| 1479 | held->fetch_sub(1); | 1766 | held->fetch_sub(1); |
| 1480 | if (signaled) { | 1767 | if (signaled) { |
| @@ -1644,6 +1931,42 @@ public: | |||
| 1644 | recorded.push_back(&list->commands()); | 1931 | recorded.push_back(&list->commands()); |
| 1645 | graphics.push_back(list); | 1932 | graphics.push_back(list); |
| 1646 | } | 1933 | } |
| 1934 | // Buffer V#s the title wrote into a bound table itself may name its own memory: rewritten to a GPU copy's | ||
| 1935 | // console VA before the copies are refreshed for this submission (src/title_va_alias.h). | ||
| 1936 | { | ||
| 1937 | std::map<std::uint64_t, std::uint32_t> tables; // each table once, with the most slots it was bound with | ||
| 1938 | for (const GraphicsCommandList* list : graphics) | ||
| 1939 | for (const auto& [table, descriptors] : list->tables()) | ||
| 1940 | tables[table] = std::max(tables[table], descriptors); | ||
| 1941 | for (const auto& [table, descriptors] : tables) { // LOCAL DIAGNOSTIC (fsdraw): poke experiment | ||
| 1942 | const std::uint64_t origin = leonida::fsdraw::table_origin(table); | ||
| 1943 | if (origin != table) | ||
| 1944 | leonida::fsdraw::poke_cb(reinterpret_cast<const std::uint8_t*>(static_cast<std::uintptr_t>(origin))); | ||
| 1945 | } | ||
| 1946 | { // LOCAL DIAGNOSTIC (fsdraw): what the bound tables hold when the lists are submitted | ||
| 1947 | static std::atomic<std::uint64_t> submits{0}; | ||
| 1948 | const std::uint64_t n = tables.empty() ? 0 : ++submits; | ||
| 1949 | if (n && (n <= 6 || n % 300 == 0)) { | ||
| 1950 | char tag[48]; | ||
| 1951 | std::snprintf(tag, sizeof tag, "submit #%llu", static_cast<unsigned long long>(n)); | ||
| 1952 | for (const auto& [table, descriptors] : tables) { | ||
| 1953 | const std::uint64_t origin = leonida::fsdraw::table_origin(table); | ||
| 1954 | const bool title = origin != table; | ||
| 1955 | leonida::fsdraw::describe_table( | ||
| 1956 | tag, table, descriptors, | ||
| 1957 | title ? reinterpret_cast<const std::uint8_t*>(static_cast<std::uintptr_t>(origin)) : nullptr, | ||
| 1958 | title ? "title memory (CPU bytes read at the title VA the table was set with)" : "arena or unknown"); | ||
| 1959 | } | ||
| 1960 | } | ||
| 1961 | } | ||
| 1962 | if (!tables.empty()) { | ||
| 1963 | const auto start = std::chrono::steady_clock::now(); | ||
| 1964 | for (const auto& [table, descriptors] : tables) leonida::title_va_aliases().scan_table(table, descriptors); | ||
| 1965 | leonida::title_va_aliases().note_scan(std::uint64_t( | ||
| 1966 | std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - start) | ||
| 1967 | .count())); | ||
| 1968 | } | ||
| 1969 | } | ||
| 1647 | last_result_ = queue_.execute(recorded, [&graphics] { return pin_lists(graphics); }); | 1970 | last_result_ = queue_.execute(recorded, [&graphics] { return pin_lists(graphics); }); |
| 1648 | d3d12_trace::record(method, last_result_ == leonida::ReplayResult::success ? "ok" : "failed"); | 1971 | d3d12_trace::record(method, last_result_ == leonida::ReplayResult::success ? "ok" : "failed"); |
| 1649 | if (last_result_ != leonida::ReplayResult::success) | 1972 | if (last_result_ != leonida::ReplayResult::success) |
| @@ -1792,16 +2115,20 @@ private: | |||
| 1792 | LARGE_INTEGER before{}, after{}; | 2115 | LARGE_INTEGER before{}, after{}; |
| 1793 | { | 2116 | { |
| 1794 | std::lock_guard submit_lock(context_.submit_mutex()); | 2117 | std::lock_guard submit_lock(context_.submit_mutex()); |
| 1795 | if (vk.vkQueueWaitIdle(vk_queue_) != VK_SUCCESS) return E_FAIL; | 2118 | if (context_.observe_result(vk.vkQueueWaitIdle(vk_queue_), "vkQueueWaitIdle (calibration)") != VK_SUCCESS) |
| 2119 | return E_FAIL; | ||
| 1796 | QueryPerformanceCounter(&before); | 2120 | QueryPerformanceCounter(&before); |
| 1797 | if (vk.vkQueueSubmit(vk_queue_, 1, &submit, VK_NULL_HANDLE) != VK_SUCCESS || | 2121 | if (context_.observe_result(vk.vkQueueSubmit(vk_queue_, 1, &submit, VK_NULL_HANDLE), |
| 1798 | vk.vkQueueWaitIdle(vk_queue_) != VK_SUCCESS) | 2122 | "vkQueueSubmit (calibration)") != VK_SUCCESS || |
| 2123 | context_.observe_result(vk.vkQueueWaitIdle(vk_queue_), "vkQueueWaitIdle (calibration)") != VK_SUCCESS) | ||
| 1799 | return E_FAIL; | 2124 | return E_FAIL; |
| 1800 | QueryPerformanceCounter(&after); | 2125 | QueryPerformanceCounter(&after); |
| 1801 | } | 2126 | } |
| 1802 | std::uint64_t stamp = 0; | 2127 | std::uint64_t stamp = 0; |
| 1803 | if (stamp_.get_query_pool_results(context_.device(), stamp_.queries, 0, 1, sizeof(stamp), &stamp, | 2128 | if (context_.observe_result(stamp_.get_query_pool_results(context_.device(), stamp_.queries, 0, 1, |
| 1804 | sizeof(stamp), VK_QUERY_RESULT_64_BIT) != VK_SUCCESS) | 2129 | sizeof(stamp), &stamp, sizeof(stamp), |
| 2130 | VK_QUERY_RESULT_64_BIT), | ||
| 2131 | "vkGetQueryPoolResults (calibration)") != VK_SUCCESS) | ||
| 1805 | return E_FAIL; | 2132 | return E_FAIL; |
| 1806 | if (clock_.timestamp_valid_bits < 64) stamp &= (std::uint64_t(1) << clock_.timestamp_valid_bits) - 1; | 2133 | if (clock_.timestamp_valid_bits < 64) stamp &= (std::uint64_t(1) << clock_.timestamp_valid_bits) - 1; |
| 1807 | gpu = stamp; | 2134 | gpu = stamp; |
| @@ -2021,7 +2348,8 @@ HRESULT set_event_on_multiple_fence_completion(leonida::VulkanContext& context, | |||
| 2021 | wait.semaphoreCount = count; | 2348 | wait.semaphoreCount = count; |
| 2022 | wait.pSemaphores = semaphores.data(); | 2349 | wait.pSemaphores = semaphores.data(); |
| 2023 | wait.pValues = values; | 2350 | wait.pValues = values; |
| 2024 | const VkResult result = context.vk.vkWaitSemaphores(context.device(), &wait, UINT64_MAX); | 2351 | const VkResult result = context.observe_result( |
| 2352 | context.vk.vkWaitSemaphores(context.device(), &wait, UINT64_MAX), "vkWaitSemaphores (multiple fences)"); | ||
| 2025 | // Unlike a single fence's blocking SetEventOnCompletion, this path invokes Vulkan directly. | 2353 | // Unlike a single fence's blocking SetEventOnCompletion, this path invokes Vulkan directly. |
| 2026 | // A lost device must wake its other registered fence waiters and reject later submissions. | 2354 | // A lost device must wake its other registered fence waiters and reject later submissions. |
| 2027 | if (result == VK_ERROR_DEVICE_LOST) { | 2355 | if (result == VK_ERROR_DEVICE_LOST) { |