diff options
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 623 |
1 files changed, 554 insertions, 69 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 9da7240ef..de448ddc4 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | #include <cstdlib> | ||
| 1 | #include "d3d12_com.h" | 2 | #include "d3d12_com.h" |
| 2 | 3 | ||
| 3 | #include "d3d12_stubs.h" // generated | 4 | #include "d3d12_stubs.h" // generated |
| @@ -6,20 +7,27 @@ | |||
| 6 | #include "d3d12_trace.h" | 7 | #include "d3d12_trace.h" |
| 7 | #include "d3d12_xs_device.h" | 8 | #include "d3d12_xs_device.h" |
| 8 | #include "../src/gfx10_descriptors.h" | 9 | #include "../src/gfx10_descriptors.h" |
| 10 | #include "../src/title_va_alias.h" | ||
| 11 | #include "../src/fsdraw_probe.h" // LOCAL DIAGNOSTIC | ||
| 12 | #include "../src/local_diagnostics.h" | ||
| 9 | 13 | ||
| 10 | #include <algorithm> | 14 | #include <algorithm> |
| 11 | #include <array> | 15 | #include <array> |
| 12 | #include <atomic> | 16 | #include <atomic> |
| 13 | #include <bit> | 17 | #include <bit> |
| 18 | #include <chrono> | ||
| 14 | #include <condition_variable> | 19 | #include <condition_variable> |
| 15 | #include <functional> | 20 | #include <functional> |
| 21 | #include <map> | ||
| 16 | #include <memory> | 22 | #include <memory> |
| 17 | #include <optional> | 23 | #include <optional> |
| 18 | #include <cstdio> | 24 | #include <cstdio> |
| 19 | #include <cstring> | 25 | #include <cstring> |
| 20 | #include <mutex> | 26 | #include <mutex> |
| 21 | #include <set> | 27 | #include <set> |
| 28 | #include <source_location> | ||
| 22 | #include <string> | 29 | #include <string> |
| 30 | #include <string_view> | ||
| 23 | #include <thread> | 31 | #include <thread> |
| 24 | #include <utility> | 32 | #include <utility> |
| 25 | #include <vector> | 33 | #include <vector> |
| @@ -198,8 +206,19 @@ public: | |||
| 198 | } | 206 | } |
| 199 | 207 | ||
| 200 | const leonida::CommandList& commands() const { return list_; } | 208 | const leonida::CommandList& commands() const { return list_; } |
| 209 | // The descriptor tables this recording binds (console VAs), for the scan before a submission. | ||
| 210 | 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). | 211 | // 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_; } | 212 | const std::vector<IUnknown*>& pinned() const { return held_; } |
| 213 | bool needs_all_sampled_views() const { | ||
| 214 | if (directly_indexed_) return true; | ||
| 215 | for (const auto& command : list_.commands()) { | ||
| 216 | if (std::holds_alternative<leonida::ExecuteIndirect>(command)) return true; | ||
| 217 | if (tables_.empty() && (std::holds_alternative<leonida::Draw>(command) || | ||
| 218 | std::holds_alternative<leonida::Dispatch>(command))) return true; | ||
| 219 | } | ||
| 220 | return false; | ||
| 221 | } | ||
| 203 | UINT front_stencil_ref() const { return front_stencil_ref_; } | 222 | UINT front_stencil_ref() const { return front_stencil_ref_; } |
| 204 | UINT back_stencil_ref() const { return back_stencil_ref_; } | 223 | UINT back_stencil_ref() const { return back_stencil_ref_; } |
| 205 | D3D12_GPU_VIRTUAL_ADDRESS cbv_srv_uav_base() const { return cbv_srv_uav_base_; } | 224 | D3D12_GPU_VIRTUAL_ADDRESS cbv_srv_uav_base() const { return cbv_srv_uav_base_; } |
| @@ -234,6 +253,8 @@ public: | |||
| 234 | record_into(command_allocator); | 253 | record_into(command_allocator); |
| 235 | invalid_ = false; | 254 | invalid_ = false; |
| 236 | calls_.clear(); | 255 | calls_.clear(); |
| 256 | tables_.clear(); | ||
| 257 | directly_indexed_ = false; | ||
| 237 | release_held(); // what the old recording used: it was replayed at ExecuteCommandLists | 258 | release_held(); // what the old recording used: it was replayed at ExecuteCommandLists |
| 238 | reset_state(state); // Reset returns every list state to its default | 259 | 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 | 260 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) set_initial_pipeline(state); // reset_state holds it |
| @@ -439,7 +460,12 @@ public: | |||
| 439 | return; | 460 | return; |
| 440 | } | 461 | } |
| 441 | if (!rects) return; | 462 | if (!rects) return; |
| 442 | if (count > D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE) return check(false); | 463 | // WARP still closes a list after RSSetScissorRects(17) (d3d12_close_parity), so the call is not an error: |
| 464 | // the rectangles past the 16 a pipeline can have are dropped. | ||
| 465 | if (count > D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE) { | ||
| 466 | stub("ID3D12GraphicsCommandList::RSSetScissorRects(more than 16 rectangles; the first 16 are used)"); | ||
| 467 | count = D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; | ||
| 468 | } | ||
| 443 | scissors_.fill({}); | 469 | scissors_.fill({}); |
| 444 | for (UINT i = 0; i < count; ++i) { | 470 | for (UINT i = 0; i < count; ++i) { |
| 445 | scissors_[i] = {rects[i].left, rects[i].top, rects[i].right, rects[i].bottom}; | 471 | scissors_[i] = {rects[i].left, rects[i].top, rects[i].right, rects[i].bottom}; |
| @@ -503,8 +529,13 @@ public: | |||
| 503 | d3d12_trace::record("ID3D12GraphicsCommandList6::DispatchMesh"); | 529 | d3d12_trace::record("ID3D12GraphicsCommandList6::DispatchMesh"); |
| 504 | if (x > 65535 || y > 65535 || z > 65535 || std::uint64_t(x) * y * z > (1u << 22)) return check(false); | 530 | if (x > 65535 || y > 65535 || z > 65535 || std::uint64_t(x) * y * z > (1u << 22)) return check(false); |
| 505 | if (!graphics_pipeline_ || !graphics_.layout) return check(false); | 531 | if (!graphics_pipeline_ || !graphics_.layout) return check(false); |
| 532 | // A mesh shader reads the root arguments from s16 through a 16-dword window (RootArgumentLayout::mesh_ok). | ||
| 533 | if (!graphics_.layout->mesh_ok) { | ||
| 534 | stub("DispatchMesh(root signature larger than a mesh shader's root window)"); | ||
| 535 | return check(false); | ||
| 536 | } | ||
| 506 | if (!any_nonempty_viewport()) return; // as record_draw: nothing is rasterized | 537 | if (!any_nonempty_viewport()) return; // as record_draw: nothing is rasterized |
| 507 | leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, {}, 0, viewport_, | 538 | leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, {}, 0, viewport_, |
| 508 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; | 539 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; |
| 509 | draw.mesh = true; | 540 | draw.mesh = true; |
| 510 | draw.viewports = draw_viewports(); | 541 | draw.viewports = draw_viewports(); |
| @@ -517,19 +548,30 @@ public: | |||
| 517 | for (UINT i = 0; i < count; ++i) { | 548 | for (UINT i = 0; i < count; ++i) { |
| 518 | const auto& b = barriers[i]; | 549 | const auto& b = barriers[i]; |
| 519 | switch (b.Type) { | 550 | switch (b.Type) { |
| 520 | case D3D12_RESOURCE_BARRIER_TYPE_TRANSITION: | 551 | case D3D12_RESOURCE_BARRIER_TYPE_TRANSITION: { |
| 521 | check(list_.transition({resource_id(b.Transition.pResource), b.Transition.Subresource, | 552 | const auto& t = b.Transition; |
| 522 | static_cast<std::uint32_t>(b.Transition.StateBefore), | 553 | const auto logical = resource_id(t.pResource); |
| 523 | static_cast<std::uint32_t>(b.Transition.StateAfter)}, | 554 | const bool was_storage = (t.StateBefore & D3D12_RESOURCE_STATE_UNORDERED_ACCESS) != 0; |
| 524 | static_cast<std::uint32_t>(b.Flags))); | 555 | const bool storage = (t.StateAfter & D3D12_RESOURCE_STATE_UNORDERED_ACCESS) != 0; |
| 556 | const auto pair = (was_storage || storage) ? xbox_depth_storage(t.pResource) : XboxDepthStorage{}; | ||
| 557 | auto before = t.StateBefore; | ||
| 558 | if (pair.colour && was_storage != storage && !(b.Flags & D3D12_RESOURCE_BARRIER_FLAG_BEGIN_ONLY)) { | ||
| 559 | check(copy_depth_storage(logical, pair, storage, {t.Subresource})); | ||
| 560 | before = D3D12_RESOURCE_STATE_COPY_DEST; | ||
| 561 | } | ||
| 562 | check(list_.transition({pair.colour && storage ? pair.colour : logical, t.Subresource, | ||
| 563 | u(before), u(t.StateAfter)}, u(b.Flags))); | ||
| 525 | break; | 564 | break; |
| 565 | } | ||
| 526 | case D3D12_RESOURCE_BARRIER_TYPE_ALIASING: | 566 | case D3D12_RESOURCE_BARRIER_TYPE_ALIASING: |
| 527 | check(!b.Flags && list_.aliasing_barrier({resource_id(b.Aliasing.pResourceBefore), | 567 | check(!b.Flags && list_.aliasing_barrier({resource_id(b.Aliasing.pResourceBefore), |
| 528 | resource_id(b.Aliasing.pResourceAfter)})); | 568 | resource_id(b.Aliasing.pResourceAfter)})); |
| 529 | break; | 569 | break; |
| 530 | case D3D12_RESOURCE_BARRIER_TYPE_UAV: | 570 | case D3D12_RESOURCE_BARRIER_TYPE_UAV: { |
| 531 | check(!b.Flags && list_.uav_barrier({resource_id(b.UAV.pResource)})); | 571 | const auto pair = xbox_depth_storage(b.UAV.pResource); |
| 572 | check(!b.Flags && list_.uav_barrier({pair.colour ? pair.colour : resource_id(b.UAV.pResource)})); | ||
| 532 | break; | 573 | break; |
| 574 | } | ||
| 533 | default: | 575 | default: |
| 534 | check(false); | 576 | check(false); |
| 535 | } | 577 | } |
| @@ -544,29 +586,61 @@ public: | |||
| 544 | switch (group[g].Type) { | 586 | switch (group[g].Type) { |
| 545 | case D3D12_BARRIER_TYPE_GLOBAL: { | 587 | case D3D12_BARRIER_TYPE_GLOBAL: { |
| 546 | const auto& b = group[g].pGlobalBarriers[i]; | 588 | const auto& b = group[g].pGlobalBarriers[i]; |
| 547 | check(list_.barrier(leonida::GlobalBarrier{u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), | 589 | if (!list_.barrier(leonida::GlobalBarrier{u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), |
| 548 | u(b.AccessAfter)})); | 590 | u(b.AccessAfter)}) && |
| 591 | reject()) | ||
| 592 | std::fprintf(stderr, "[rt] d3d12: global barrier sync 0x%x->0x%x access 0x%x->0x%x\n", | ||
| 593 | u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), u(b.AccessAfter)); | ||
| 549 | break; | 594 | break; |
| 550 | } | 595 | } |
| 551 | case D3D12_BARRIER_TYPE_BUFFER: { | 596 | case D3D12_BARRIER_TYPE_BUFFER: { |
| 552 | const auto& b = group[g].pBufferBarriers[i]; | 597 | const auto& b = group[g].pBufferBarriers[i]; |
| 553 | check(list_.barrier(leonida::BufferBarrier{u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), | 598 | if (!list_.barrier(leonida::BufferBarrier{u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), |
| 554 | u(b.AccessAfter), resource_id(b.pResource)})); | 599 | u(b.AccessAfter), resource_id(b.pResource)}) && |
| 600 | reject()) { | ||
| 601 | std::fprintf(stderr, "[rt] d3d12: buffer barrier sync 0x%x->0x%x access 0x%x->0x%x offset " | ||
| 602 | "0x%llx size 0x%llx\n", | ||
| 603 | u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), u(b.AccessAfter), | ||
| 604 | static_cast<unsigned long long>(b.Offset), static_cast<unsigned long long>(b.Size)); | ||
| 605 | describe_barrier_resource(b.pResource); | ||
| 606 | } | ||
| 555 | break; | 607 | break; |
| 556 | } | 608 | } |
| 557 | case D3D12_BARRIER_TYPE_TEXTURE: { | 609 | case D3D12_BARRIER_TYPE_TEXTURE: { |
| 558 | const auto& b = group[g].pTextureBarriers[i]; | 610 | const auto& b = group[g].pTextureBarriers[i]; |
| 559 | const auto& r = b.Subresources; | 611 | const auto& r = b.Subresources; |
| 560 | check(list_.barrier(leonida::TextureBarrier{ | 612 | leonida::TextureBarrier barrier{ |
| 561 | u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), u(b.AccessAfter), u(b.LayoutBefore), | 613 | u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), u(b.AccessAfter), u(b.LayoutBefore), |
| 562 | u(b.LayoutAfter), resource_id(b.pResource), | 614 | u(b.LayoutAfter), resource_id(b.pResource), |
| 563 | {r.IndexOrFirstMipLevel, r.NumMipLevels, r.FirstArraySlice, r.NumArraySlices, r.FirstPlane, | 615 | {r.IndexOrFirstMipLevel, r.NumMipLevels, r.FirstArraySlice, r.NumArraySlices, r.FirstPlane, |
| 564 | r.NumPlanes}, | 616 | r.NumPlanes}, u(b.Flags)}; |
| 565 | u(b.Flags)})); | 617 | const bool was_storage = storage_access(b.LayoutBefore, b.AccessBefore); |
| 618 | const bool storage = storage_access(b.LayoutAfter, b.AccessAfter); | ||
| 619 | const auto pair = (was_storage || storage) ? xbox_depth_storage(b.pResource) : XboxDepthStorage{}; | ||
| 620 | if (pair.colour) { | ||
| 621 | if (was_storage != storage && b.SyncAfter != D3D12_BARRIER_SYNC_SPLIT && | ||
| 622 | b.LayoutBefore != D3D12_BARRIER_LAYOUT_UNDEFINED && !(b.Flags & D3D12_TEXTURE_BARRIER_FLAG_DISCARD)) { | ||
| 623 | check(copy_depth_storage(barrier.resource, pair, storage, barrier.range)); | ||
| 624 | barrier.sync_before = leonida::d3d12_sync::copy; | ||
| 625 | barrier.access_before = leonida::d3d12_access::copy_dest; | ||
| 626 | barrier.layout_before = leonida::d3d12_layout::copy_dest; | ||
| 627 | } | ||
| 628 | if (storage) barrier.resource = pair.colour; | ||
| 629 | } | ||
| 630 | if (!list_.barrier(barrier) && reject()) { | ||
| 631 | std::fprintf(stderr, | ||
| 632 | "[rt] d3d12: texture barrier sync 0x%x->0x%x access 0x%x->0x%x layout %u->%u " | ||
| 633 | "mips %u+%u layers %u+%u planes %u+%u flags 0x%x\n", | ||
| 634 | u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), u(b.AccessAfter), | ||
| 635 | u(b.LayoutBefore), u(b.LayoutAfter), r.IndexOrFirstMipLevel, r.NumMipLevels, | ||
| 636 | r.FirstArraySlice, r.NumArraySlices, r.FirstPlane, r.NumPlanes, u(b.Flags)); | ||
| 637 | describe_barrier_resource(b.pResource); | ||
| 638 | } | ||
| 566 | break; | 639 | break; |
| 567 | } | 640 | } |
| 568 | default: | 641 | default: |
| 569 | check(false); | 642 | if (reject()) |
| 643 | std::fprintf(stderr, "[rt] d3d12: barrier group type %d\n", int(group[g].Type)); | ||
| 570 | } | 644 | } |
| 571 | } | 645 | } |
| 572 | } | 646 | } |
| @@ -634,7 +708,24 @@ public: | |||
| 634 | if (!dst || !src) return check(false); | 708 | if (!dst || !src) return check(false); |
| 635 | leonida::CopyTexture copy{location(*dst), x, y, z, location(*src), box != nullptr, {}}; | 709 | leonida::CopyTexture copy{location(*dst), x, y, z, location(*src), box != nullptr, {}}; |
| 636 | if (box) copy.box = {box->left, box->top, box->front, box->right, box->bottom, box->back}; | 710 | if (box) copy.box = {box->left, box->top, box->front, box->right, box->bottom, box->back}; |
| 637 | check(list_.copy_texture(copy)); | 711 | if (!list_.copy_texture(copy) && reject()) { |
| 712 | for (const auto* l : {dst, src}) { | ||
| 713 | const char* side = l == dst ? "dst" : "src"; | ||
| 714 | if (l->Type != D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT) { | ||
| 715 | std::fprintf(stderr, "[rt] d3d12: %s type %d resource %p subresource %u\n", side, int(l->Type), | ||
| 716 | static_cast<void*>(l->pResource), l->SubresourceIndex); | ||
| 717 | continue; | ||
| 718 | } | ||
| 719 | const auto& f = l->PlacedFootprint; | ||
| 720 | std::fprintf(stderr, "[rt] d3d12: %s footprint resource %p offset 0x%llx format %d %ux%ux%u pitch %u\n", | ||
| 721 | side, static_cast<void*>(l->pResource), static_cast<unsigned long long>(f.Offset), | ||
| 722 | int(f.Footprint.Format), f.Footprint.Width, f.Footprint.Height, f.Footprint.Depth, | ||
| 723 | f.Footprint.RowPitch); | ||
| 724 | } | ||
| 725 | if (box) | ||
| 726 | std::fprintf(stderr, "[rt] d3d12: at %u,%u,%u box %u,%u,%u-%u,%u,%u\n", x, y, z, box->left, box->top, | ||
| 727 | box->front, box->right, box->bottom, box->back); | ||
| 728 | } | ||
| 638 | } | 729 | } |
| 639 | 730 | ||
| 640 | void STDMETHODCALLTYPE CopyResource(ID3D12Resource* dst, ID3D12Resource* src) override { | 731 | void STDMETHODCALLTYPE CopyResource(ID3D12Resource* dst, ID3D12Resource* src) override { |
| @@ -716,6 +807,18 @@ public: | |||
| 716 | d3d12_trace::record("ID3D12GraphicsCommandList::ClearRenderTargetView"); | 807 | d3d12_trace::record("ID3D12GraphicsCommandList::ClearRenderTargetView"); |
| 717 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); | 808 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); |
| 718 | if (!view.ptr || !color) return check(false); | 809 | if (!view.ptr || !color) return check(false); |
| 810 | // LOCAL DIAGNOSTIC: what the title clears, and with which colour | ||
| 811 | if (leonida::local_diagnostics()) { | ||
| 812 | static std::atomic<std::uint64_t> clears{0}; | ||
| 813 | const std::uint64_t n = ++clears; | ||
| 814 | if (n <= 6 || n % 600 == 0) { | ||
| 815 | const auto& t = *reinterpret_cast<const leonida::TargetView*>(view.ptr); | ||
| 816 | std::fprintf(stderr, "[rt] present-probe: ClearRenderTargetView #%llu resource 0x%llx mip %u layers %u+%u " | ||
| 817 | "colour %.3f %.3f %.3f %.3f rects %u\n", | ||
| 818 | static_cast<unsigned long long>(n), static_cast<unsigned long long>(t.resource), t.mip, | ||
| 819 | t.first_layer, t.layer_count, color[0], color[1], color[2], color[3], count); | ||
| 820 | } | ||
| 821 | } | ||
| 719 | check(list_.clear_render_target(*reinterpret_cast<const leonida::TargetView*>(view.ptr), | 822 | check(list_.clear_render_target(*reinterpret_cast<const leonida::TargetView*>(view.ptr), |
| 720 | {color[0], color[1], color[2], color[3]}, to_rects(count, rects))); | 823 | {color[0], color[1], color[2], color[3]}, to_rects(count, rects))); |
| 721 | } | 824 | } |
| @@ -858,6 +961,7 @@ public: | |||
| 858 | kind == leonida::IndirectKind::draw ? sizeof(D3D12_DRAW_ARGUMENTS) | 961 | kind == leonida::IndirectKind::draw ? sizeof(D3D12_DRAW_ARGUMENTS) |
| 859 | : kind == leonida::IndirectKind::draw_indexed ? sizeof(D3D12_DRAW_INDEXED_ARGUMENTS) | 962 | : kind == leonida::IndirectKind::draw_indexed ? sizeof(D3D12_DRAW_INDEXED_ARGUMENTS) |
| 860 | : kind == leonida::IndirectKind::dispatch ? sizeof(D3D12_DISPATCH_ARGUMENTS) | 963 | : kind == leonida::IndirectKind::dispatch ? sizeof(D3D12_DISPATCH_ARGUMENTS) |
| 964 | : kind == leonida::IndirectKind::dispatch_mesh ? sizeof(D3D12_DISPATCH_MESH_ARGUMENTS) | ||
| 861 | : 0; | 965 | : 0; |
| 862 | if (!record_size || stride < record_size || stride % 4 != 0) return check(false); | 966 | if (!record_size || stride < record_size || stride % 4 != 0) return check(false); |
| 863 | // The argument range is the bytes the commands read: (max_command_count - 1) * stride + record_size, and | 967 | // The argument range is the bytes the commands read: (max_command_count - 1) * stride + record_size, and |
| @@ -872,6 +976,15 @@ public: | |||
| 872 | if (argument_buffer_offset > arg_desc.Width || arg_desc.Width - argument_buffer_offset < required) | 976 | if (argument_buffer_offset > arg_desc.Width || arg_desc.Width - argument_buffer_offset < required) |
| 873 | return check(false); | 977 | return check(false); |
| 874 | } | 978 | } |
| 979 | if (kind == leonida::IndirectKind::dispatch_mesh) { | ||
| 980 | // Not replayed yet (#3777 replays it as vkCmdDrawMeshTasksIndirectEXT): nothing is recorded, and the | ||
| 981 | // first such call says so. The list stays valid. | ||
| 982 | static std::atomic<bool> logged{false}; | ||
| 983 | if (!logged.exchange(true, std::memory_order_relaxed)) | ||
| 984 | std::fprintf(stderr, "[rt] d3d12: ExecuteIndirect with a DISPATCH_MESH signature is not replayed yet; " | ||
| 985 | "nothing was recorded\n"); | ||
| 986 | return; | ||
| 987 | } | ||
| 875 | leonida::ExecuteIndirect cmd{}; | 988 | leonida::ExecuteIndirect cmd{}; |
| 876 | cmd.kind = kind; | 989 | cmd.kind = kind; |
| 877 | cmd.stride = stride; | 990 | cmd.stride = stride; |
| @@ -993,9 +1106,47 @@ public: | |||
| 993 | check(list_.copy_acceleration_structure({dest, source, static_cast<std::uint32_t>(mode)})); | 1106 | check(list_.copy_acceleration_structure({dest, source, static_cast<std::uint32_t>(mode)})); |
| 994 | } | 1107 | } |
| 995 | 1108 | ||
| 1109 | bool xbox_label(const leonida::label::Write& op) { return list_.write_gpu_label(op); } | ||
| 996 | static GraphicsCommandList* from(IUnknown* object) { return Child::from<GraphicsCommandList>(object); } | 1110 | static GraphicsCommandList* from(IUnknown* object) { return Child::from<GraphicsCommandList>(object); } |
| 997 | 1111 | ||
| 998 | private: | 1112 | private: |
| 1113 | static bool storage_access(D3D12_BARRIER_LAYOUT layout, D3D12_BARRIER_ACCESS access) { | ||
| 1114 | return (access & D3D12_BARRIER_ACCESS_UNORDERED_ACCESS) || | ||
| 1115 | layout == D3D12_BARRIER_LAYOUT_UNORDERED_ACCESS || | ||
| 1116 | layout == D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_UNORDERED_ACCESS || | ||
| 1117 | layout == D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_UNORDERED_ACCESS; | ||
| 1118 | } | ||
| 1119 | |||
| 1120 | bool copy_depth_storage(leonida::ResourceId depth, XboxDepthStorage pair, bool to_storage, | ||
| 1121 | const leonida::BarrierSubresourceRange& r) { | ||
| 1122 | namespace access = leonida::d3d12_access; | ||
| 1123 | namespace sync = leonida::d3d12_sync; | ||
| 1124 | const auto from = to_storage ? depth : pair.colour, to = to_storage ? pair.colour : depth; | ||
| 1125 | // The two images can be in GENERAL for bindless use. Order prior depth writes, unordered writes and | ||
| 1126 | // readers of the destination even when Vulkan does not need a layout transition. | ||
| 1127 | if (!list_.barrier(leonida::GlobalBarrier{sync::all, sync::copy, | ||
| 1128 | access::depth_stencil_write | access::depth_stencil_read | access::unordered_access | | ||
| 1129 | access::shader_resource | access::copy_source | access::copy_dest, | ||
| 1130 | access::copy_source | access::copy_dest})) return false; | ||
| 1131 | if (!r.mip_count && r.index_or_first_mip == D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES) | ||
| 1132 | return list_.copy_resource({to, from}); | ||
| 1133 | const auto one = [&](UINT subresource) { | ||
| 1134 | leonida::CopyTexture copy{}; | ||
| 1135 | copy.src.resource = from; | ||
| 1136 | copy.dst.resource = to; | ||
| 1137 | copy.src.subresource = copy.dst.subresource = subresource; | ||
| 1138 | return list_.copy_texture(copy); | ||
| 1139 | }; | ||
| 1140 | if (!r.mip_count) return r.index_or_first_mip < pair.mips * pair.layers && one(r.index_or_first_mip); | ||
| 1141 | if (r.first_plane || r.plane_count != 1 || !r.layer_count || | ||
| 1142 | r.first_layer >= pair.layers || r.layer_count > pair.layers - r.first_layer || | ||
| 1143 | r.index_or_first_mip >= pair.mips || r.mip_count > pair.mips - r.index_or_first_mip) return false; | ||
| 1144 | for (UINT layer = r.first_layer; layer < r.first_layer + r.layer_count; ++layer) | ||
| 1145 | for (UINT mip = r.index_or_first_mip; mip < r.index_or_first_mip + r.mip_count; ++mip) | ||
| 1146 | if (!one(layer * pair.mips + mip)) return false; | ||
| 1147 | return true; | ||
| 1148 | } | ||
| 1149 | |||
| 999 | static bool valid_postbuild_info_type(D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_TYPE type) { | 1150 | static bool valid_postbuild_info_type(D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_TYPE type) { |
| 1000 | switch (type) { | 1151 | switch (type) { |
| 1001 | case D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_COMPACTED_SIZE: | 1152 | case D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_COMPACTED_SIZE: |
| @@ -1041,8 +1192,35 @@ private: | |||
| 1041 | return out; | 1192 | return out; |
| 1042 | } | 1193 | } |
| 1043 | 1194 | ||
| 1044 | void check(bool ok) { | 1195 | // Records an invalid call: Close then returns E_INVALIDARG and ExecuteCommandLists refuses the list. The first |
| 1045 | if (!ok) invalid_ = true; | 1196 | // rejection at each call site is logged with the method and line, so a failed Close names its cause; reject() |
| 1197 | // returns true for that first one, when a caller may print the arguments too. | ||
| 1198 | bool reject(std::source_location where = std::source_location::current()) { | ||
| 1199 | invalid_ = true; | ||
| 1200 | static std::array<std::atomic<bool>, 4096> seen{}; | ||
| 1201 | const std::uint_least32_t line = where.line(); | ||
| 1202 | if (line < seen.size() && seen[line].exchange(true, std::memory_order_relaxed)) return false; | ||
| 1203 | std::string_view method = where.function_name(); | ||
| 1204 | if (const auto at = method.find("GraphicsCommandList::"); at != std::string_view::npos) | ||
| 1205 | method.remove_prefix(at); | ||
| 1206 | if (const auto paren = method.find('('); paren != std::string_view::npos) method = method.substr(0, paren); | ||
| 1207 | std::fprintf(stderr, "[rt] d3d12: %.*s rejected a call (d3d12_com.cpp:%u): the list's Close returns " | ||
| 1208 | "E_INVALIDARG and ExecuteCommandLists refuses it\n", | ||
| 1209 | int(method.size()), method.data(), unsigned(line)); | ||
| 1210 | return true; | ||
| 1211 | } | ||
| 1212 | void check(bool ok, std::source_location where = std::source_location::current()) { | ||
| 1213 | if (!ok) reject(where); | ||
| 1214 | } | ||
| 1215 | static void describe_barrier_resource(ID3D12Resource* resource) { | ||
| 1216 | if (!resource) return void(std::fprintf(stderr, "[rt] d3d12: resource null\n")); | ||
| 1217 | const D3D12_RESOURCE_DESC desc = resource->GetDesc(); | ||
| 1218 | std::fprintf(stderr, | ||
| 1219 | "[rt] d3d12: resource %p dimension %d format %d %llux%ux%u mips %u samples %u layout %d " | ||
| 1220 | "flags 0x%x\n", | ||
| 1221 | static_cast<void*>(resource), int(desc.Dimension), int(desc.Format), | ||
| 1222 | static_cast<unsigned long long>(desc.Width), desc.Height, desc.DepthOrArraySize, desc.MipLevels, | ||
| 1223 | desc.SampleDesc.Count, int(desc.Layout), unsigned(desc.Flags)); | ||
| 1046 | } | 1224 | } |
| 1047 | 1225 | ||
| 1048 | void stop_recording() { | 1226 | void stop_recording() { |
| @@ -1145,12 +1323,24 @@ private: | |||
| 1145 | if (g::is_image(words)) { | 1323 | if (g::is_image(words)) { |
| 1146 | g::Image image{}; | 1324 | g::Image image{}; |
| 1147 | if (!g::decode(words, image)) return false; | 1325 | if (!g::decode(words, image)) return false; |
| 1326 | out.resource = uav_image_resource(resource, image.address); | ||
| 1327 | if (!out.resource) return false; | ||
| 1148 | const bool volume = image.type == g::ImageType::image_3d; | 1328 | const bool volume = image.type == g::ImageType::image_3d; |
| 1329 | const auto pair = xbox_depth_storage(resource); | ||
| 1330 | if (pair.colour) out.resource = pair.colour; | ||
| 1331 | if (g::partial_volume_uav(words, image)) { | ||
| 1332 | // Transfer clears operate on a whole 3D mip, not a storage image view. Refuse instead of | ||
| 1333 | // overwriting slices outside the UAV; a sliced clear needs a separate replay path. | ||
| 1334 | stub("ClearUnorderedAccessView of a partial 3D mip"); | ||
| 1335 | return false; | ||
| 1336 | } | ||
| 1149 | out.buffer = false; | 1337 | out.buffer = false; |
| 1150 | out.format = std::uint32_t(descriptor_format(image.format, image.swizzle)); | 1338 | out.format = std::uint32_t(descriptor_format(image.format, image.swizzle)); |
| 1151 | out.mip = image.first_mip; | 1339 | out.mip = image.first_mip; |
| 1152 | out.first_layer = volume ? 0 : image.first_layer; // a 3D view clears every slice of its mip | 1340 | out.first_layer = volume ? 0 : image.first_layer; // a 3D view clears every slice of its mip |
| 1153 | out.layer_count = volume ? 1 : image.layer_count; | 1341 | out.layer_count = volume ? 1 : image.layer_count; |
| 1342 | out.exact_clear_format = volume || image.type == g::ImageType::image_1d || | ||
| 1343 | image.type == g::ImageType::image_1d_array; | ||
| 1154 | return out.format != VK_FORMAT_UNDEFINED; | 1344 | return out.format != VK_FORMAT_UNDEFINED; |
| 1155 | } | 1345 | } |
| 1156 | g::BufferWords buffer_words{}; | 1346 | g::BufferWords buffer_words{}; |
| @@ -1200,6 +1390,7 @@ private: | |||
| 1200 | if (layout && layout == state.layout) return; | 1390 | if (layout && layout == state.layout) return; |
| 1201 | hold(signature); // state.layout points into it | 1391 | hold(signature); // state.layout points into it |
| 1202 | state.layout = layout; | 1392 | state.layout = layout; |
| 1393 | if (layout && (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED)) directly_indexed_ = true; | ||
| 1203 | state.data = {}; | 1394 | state.data = {}; |
| 1204 | // CreateRootSignature accepted it for the other bind point only (its log line says why). | 1395 | // CreateRootSignature accepted it for the other bind point only (its log line says why). |
| 1205 | if (layout && !(is_compute(state) ? layout->compute_ok : layout->graphics_ok)) { | 1396 | if (layout && !(is_compute(state) ? layout->compute_ok : layout->graphics_ok)) { |
| @@ -1254,9 +1445,20 @@ private: | |||
| 1254 | if ((count && !data) || std::uint64_t(offset) + count > p->constant_count) return check(false); | 1445 | if ((count && !data) || std::uint64_t(offset) + count > p->constant_count) return check(false); |
| 1255 | const auto* values = static_cast<const std::uint32_t*>(data); | 1446 | const auto* values = static_cast<const std::uint32_t*>(data); |
| 1256 | const std::uint32_t first = first_dword(state, *p); | 1447 | const std::uint32_t first = first_dword(state, *p); |
| 1448 | if (first == rt::rootsig::kNoSgpr) return spilled(); | ||
| 1257 | for (UINT i = 0; i < count; ++i) write(state, first + offset + i, values[i]); | 1449 | for (UINT i = 0; i < count; ++i) write(state, first + offset + i, values[i]); |
| 1258 | } | 1450 | } |
| 1259 | 1451 | ||
| 1452 | // A parameter in the spill array behind s[0:1] (a compute signature past s31: pack_stage gives it no SGPR, only a | ||
| 1453 | // spill_dword): that array is not written yet, so its value is not stored; kNoSgpr plus an offset must never | ||
| 1454 | // reach write(), where it would wrap round to s0. | ||
| 1455 | static void spilled() { | ||
| 1456 | static std::atomic<bool> said{false}; | ||
| 1457 | if (!said.exchange(true)) | ||
| 1458 | std::fprintf(stderr, "[rt] d3d12: root arguments in the spill array behind s[0:1] are not stored (the " | ||
| 1459 | "array is not written yet)\n"); | ||
| 1460 | } | ||
| 1461 | |||
| 1260 | void address(RootState& state, UINT index, std::uint64_t va, rt::rootsig::ParamType type) { | 1462 | void address(RootState& state, UINT index, std::uint64_t va, rt::rootsig::ParamType type) { |
| 1261 | if (list_.closed()) return; | 1463 | if (list_.closed()) return; |
| 1262 | if (defer([=, compute = &state == &compute_](GraphicsCommandList& l) { | 1464 | if (defer([=, compute = &state == &compute_](GraphicsCommandList& l) { |
| @@ -1265,11 +1467,27 @@ private: | |||
| 1265 | return; | 1467 | return; |
| 1266 | const RootArgumentLayout::Parameter* p = parameter(state, index, type); | 1468 | const RootArgumentLayout::Parameter* p = parameter(state, index, type); |
| 1267 | if (!p) return; | 1469 | if (!p) return; |
| 1470 | // A root view into the title's own memory (a slot-133 buffer, a constant ring) gets the console VA of a GPU | ||
| 1471 | // copy of it, so the shader's heap_base + VA reaches it (src/title_va_alias.h). A descriptor table is in one | ||
| 1472 | // of the layer's heaps: kept, and its buffer V#s are rewritten the same way before each submission. | ||
| 1473 | // A table in a console descriptor heap is read from the heap's arena copy (console_descriptor_table_va). | ||
| 1474 | if (type == rt::rootsig::ParamType::Descriptor) | ||
| 1475 | va = leonida::title_va_aliases().resolve(va, kRootViewBytes, leonida::TitleVaAliases::root_view); | ||
| 1476 | else | ||
| 1477 | va = console_descriptor_table_va(va); | ||
| 1478 | if (type != rt::rootsig::ParamType::Descriptor && va && p->table_descriptors) { | ||
| 1479 | if (!tables_.empty() && tables_.back().first == va) | ||
| 1480 | tables_.back().second = std::max(tables_.back().second, p->table_descriptors); | ||
| 1481 | else | ||
| 1482 | tables_.emplace_back(va, p->table_descriptors); | ||
| 1483 | } | ||
| 1268 | const std::uint32_t first = first_dword(state, *p); | 1484 | const std::uint32_t first = first_dword(state, *p); |
| 1485 | if (first == rt::rootsig::kNoSgpr) return spilled(); | ||
| 1269 | write(state, first, std::uint32_t(va)); | 1486 | write(state, first, std::uint32_t(va)); |
| 1270 | // In the compute layout a descriptor table is one dword, the 32-bit VA of its first descriptor (the heaps | 1487 | // In the console layouts a descriptor table is one dword, the 32-bit VA of its first descriptor (the heaps |
| 1271 | // live below 4 GiB); a root view keeps both halves. The graphics layout gives every address two dwords. | 1488 | // live below 4 GiB); a root view keeps both halves. pack()'s graphics layout gives every address two dwords. |
| 1272 | if (!(is_compute(state) && type == rt::rootsig::ParamType::Table)) write(state, first + 1, std::uint32_t(va >> 32)); | 1489 | const bool console = is_compute(state) || state.layout->graphics_console; |
| 1490 | if (!(console && type == rt::rootsig::ParamType::Table)) write(state, first + 1, std::uint32_t(va >> 32)); | ||
| 1273 | } | 1491 | } |
| 1274 | 1492 | ||
| 1275 | D3D12_COMMAND_LIST_TYPE type_; | 1493 | D3D12_COMMAND_LIST_TYPE type_; |
| @@ -1281,6 +1499,7 @@ private: | |||
| 1281 | RootState compute_; | 1499 | RootState compute_; |
| 1282 | RootState graphics_; | 1500 | RootState graphics_; |
| 1283 | std::uintptr_t compute_pipeline_ = 0; | 1501 | std::uintptr_t compute_pipeline_ = 0; |
| 1502 | bool unregistered_pipeline_ = false; // the bound PSO has no pipeline here (a stub): see set_pipeline | ||
| 1284 | std::uintptr_t graphics_pipeline_ = 0; | 1503 | std::uintptr_t graphics_pipeline_ = 0; |
| 1285 | ID3D12DescriptorHeap* cbv_srv_uav_heap_ = nullptr; | 1504 | ID3D12DescriptorHeap* cbv_srv_uav_heap_ = nullptr; |
| 1286 | ID3D12DescriptorHeap* sampler_heap_ = nullptr; | 1505 | ID3D12DescriptorHeap* sampler_heap_ = nullptr; |
| @@ -1289,6 +1508,11 @@ private: | |||
| 1289 | std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED | 1508 | std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED |
| 1290 | std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{}; | 1509 | std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{}; |
| 1291 | leonida::IndexBufferView index_{}; | 1510 | leonida::IndexBufferView index_{}; |
| 1511 | // SetGraphicsRootDescriptorTable / SetComputeRootDescriptorTable: VA, CBV/SRV/UAV slots the signature declares | ||
| 1512 | std::vector<std::pair<std::uint64_t, std::uint32_t>> tables_; | ||
| 1513 | bool directly_indexed_ = false; | ||
| 1514 | // The span a root view may read: D3D12's largest constant buffer (4096 x 16 bytes). | ||
| 1515 | static constexpr std::uint64_t kRootViewBytes = 65536; | ||
| 1292 | leonida::Viewport viewport_{}; | 1516 | leonida::Viewport viewport_{}; |
| 1293 | leonida::Rect scissor_{}; | 1517 | leonida::Rect scissor_{}; |
| 1294 | std::array<leonida::Viewport, D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE> viewports_{}; | 1518 | std::array<leonida::Viewport, D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE> viewports_{}; |
| @@ -1352,7 +1576,11 @@ private: | |||
| 1352 | hold(state); | 1576 | hold(state); |
| 1353 | compute_pipeline_ = compute_pipeline_id(state); | 1577 | compute_pipeline_ = compute_pipeline_id(state); |
| 1354 | graphics_pipeline_ = graphics_pipeline_id(state); | 1578 | graphics_pipeline_ = graphics_pipeline_id(state); |
| 1355 | if (state && !compute_pipeline_ && !graphics_pipeline_) | 1579 | // A pipeline this device never registered: a LEONIDA_STUB_PIPELINES stub (its translated shader is missing), |
| 1580 | // or one from another device. Its draws resolve to nothing in replay; its dispatches are no-ops too | ||
| 1581 | // (record_dispatch), so the list still closes. | ||
| 1582 | unregistered_pipeline_ = state && !compute_pipeline_ && !graphics_pipeline_; | ||
| 1583 | if (unregistered_pipeline_) | ||
| 1356 | stub("ID3D12GraphicsCommandList::SetPipelineState(pipeline from another device)"); | 1584 | stub("ID3D12GraphicsCommandList::SetPipelineState(pipeline from another device)"); |
| 1357 | } | 1585 | } |
| 1358 | 1586 | ||
| @@ -1370,20 +1598,19 @@ private: | |||
| 1370 | if ((cbv && cbv != cbv_srv_uav_heap_) || (smp && smp != sampler_heap_)) check(false); | 1598 | if ((cbv && cbv != cbv_srv_uav_heap_) || (smp && smp != sampler_heap_)) check(false); |
| 1371 | } | 1599 | } |
| 1372 | 1600 | ||
| 1373 | void note_direct_heap_indexing(const RootArgumentLayout* layout, bool compute) const { | 1601 | void note_direct_heap_indexing(const RootArgumentLayout* layout) const { |
| 1602 | if (!layout) return; | ||
| 1374 | if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) { | 1603 | if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) { |
| 1375 | if (!cbv_srv_uav_heap_) | 1604 | if (!cbv_srv_uav_heap_) |
| 1376 | note_once(compute ? "dispatch uses direct resource indexing with no CBV/SRV/UAV heap bound" | 1605 | note_once("draw/dispatch uses CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED with no CBV/SRV/UAV heap bound"); |
| 1377 | : "draw uses direct resource indexing with no CBV/SRV/UAV heap bound"); | 1606 | else |
| 1378 | else if (!compute) | 1607 | note_once("CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED heap base tracked on list"); |
| 1379 | note_once("draw uses direct resource indexing; graphics heap-base SGPR is not supplied"); | ||
| 1380 | } | 1608 | } |
| 1381 | if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) { | 1609 | if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) { |
| 1382 | if (!sampler_heap_) | 1610 | if (!sampler_heap_) |
| 1383 | note_once(compute ? "dispatch uses direct sampler indexing with no sampler heap bound" | 1611 | note_once("draw/dispatch uses SAMPLER_HEAP_DIRECTLY_INDEXED with no sampler heap bound"); |
| 1384 | : "draw uses direct sampler indexing with no sampler heap bound"); | 1612 | else |
| 1385 | else if (!compute) | 1613 | note_once("SAMPLER_HEAP_DIRECTLY_INDEXED heap base tracked on list"); |
| 1386 | note_once("draw uses direct sampler indexing; graphics heap-base SGPR is not supplied"); | ||
| 1387 | } | 1614 | } |
| 1388 | } | 1615 | } |
| 1389 | 1616 | ||
| @@ -1416,43 +1643,128 @@ private: | |||
| 1416 | 1643 | ||
| 1417 | void set_vertex_buffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) { | 1644 | void set_vertex_buffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) { |
| 1418 | if (count) draw_views_stale_ = true; | 1645 | if (count) draw_views_stale_ = true; |
| 1419 | for (UINT i = 0; i < count; ++i) | 1646 | for (UINT i = 0; i < count; ++i) { |
| 1647 | if (views) leonida::title_va_aliases().note(views[i].BufferLocation, leonida::TitleVaAliases::vertex_view); | ||
| 1648 | if (views) leonida::title_va_aliases().want_buffer(views[i].BufferLocation, views[i].SizeInBytes); | ||
| 1420 | vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes, | 1649 | vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes, |
| 1421 | views[i].StrideInBytes} | 1650 | views[i].StrideInBytes} |
| 1422 | : leonida::VertexBufferView{}; | 1651 | : leonida::VertexBufferView{}; |
| 1652 | } | ||
| 1423 | } | 1653 | } |
| 1424 | 1654 | ||
| 1425 | void set_index_buffer(const D3D12_INDEX_BUFFER_VIEW* view) { | 1655 | void set_index_buffer(const D3D12_INDEX_BUFFER_VIEW* view) { |
| 1656 | if (view) leonida::title_va_aliases().note(view->BufferLocation, leonida::TitleVaAliases::index_view); | ||
| 1657 | if (view) leonida::title_va_aliases().want_buffer(view->BufferLocation, view->SizeInBytes); | ||
| 1426 | index_ = view ? leonida::IndexBufferView{view->BufferLocation, view->SizeInBytes, u(view->Format)} | 1658 | index_ = view ? leonida::IndexBufferView{view->BufferLocation, view->SizeInBytes, u(view->Format)} |
| 1427 | : leonida::IndexBufferView{}; | 1659 | : leonida::IndexBufferView{}; |
| 1428 | } | 1660 | } |
| 1429 | 1661 | ||
| 1430 | // Section 3.4 documents the observed resource-heap slot. Appending flagged heaps in this | 1662 | // The compiled console ABI appends directly indexed heaps after the explicit root arguments: the resource heap's |
| 1431 | // order is a working hypothesis for signatures without a matched program and layout. | 1663 | // 32-bit shader VA, then the sampler heap's, each only when the signature has its flag. `slot` is the first |
| 1432 | // Snapshot them for each command so rebinding a heap cannot change a recorded dispatch. | 1664 | // dword after the arguments (RootArgumentLayout::*_arguments_end). The heaps are snapshotted for each command, |
| 1665 | // so rebinding a heap cannot change a recorded draw or dispatch. | ||
| 1666 | std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> with_direct_heaps(const RootState& state, | ||
| 1667 | std::uint32_t slot) const { | ||
| 1668 | auto data = state.data; | ||
| 1669 | const auto flags = state.layout->flags; | ||
| 1670 | if ((flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) && slot < data.size()) | ||
| 1671 | data[slot++] = static_cast<std::uint32_t>(cbv_srv_uav_base_); | ||
| 1672 | if ((flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) && slot < data.size()) | ||
| 1673 | data[slot] = static_cast<std::uint32_t>(sampler_base_); | ||
| 1674 | return data; | ||
| 1675 | } | ||
| 1676 | |||
| 1677 | static bool direct_heaps_enabled(const char* variable) { | ||
| 1678 | const char* v = std::getenv(variable); | ||
| 1679 | return !(v && std::string_view(v) == "0"); | ||
| 1680 | } | ||
| 1681 | |||
| 1682 | // Direct and indirect dispatches. Putting the resource heap first and the sampler heap next is a hypothesis | ||
| 1683 | // with partial evidence (docs/d3d12_vulkan.md section 3.4, tools/compute_heap_layout_census.py). A heap with no | ||
| 1684 | // inline user-data dword left is reported once per heap kind and the explicit arguments are kept. | ||
| 1433 | std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> compute_user_data() const { | 1685 | std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> compute_user_data() const { |
| 1434 | note_direct_heap_indexing(compute_.layout, true); | 1686 | note_direct_heap_indexing(compute_.layout); |
| 1435 | auto data = compute_.data; | 1687 | static const bool enabled = direct_heaps_enabled("LEONIDA_COMPUTE_DIRECT_HEAPS"); |
| 1436 | static const bool enabled = [] { | 1688 | if (!enabled) return compute_.data; |
| 1437 | char value[2]{}; | 1689 | const RootArgumentLayout& layout = *compute_.layout; |
| 1438 | return GetEnvironmentVariableA("LEONIDA_COMPUTE_DIRECT_HEAPS", value, 2) != 1 || value[0] != '0'; | 1690 | const bool resource = layout.flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED; |
| 1439 | }(); | 1691 | const bool sampler = layout.flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED; |
| 1440 | if (!enabled) return data; | 1692 | const std::uint32_t end = layout.compute_arguments_end; |
| 1441 | const auto flags = compute_.layout->flags; | 1693 | if ((resource || sampler) && end == rt::rootsig::kNoSgpr) { |
| 1442 | auto slot = compute_.layout->compute_arguments_end; | 1694 | note_once("compute direct heaps: not supplied, the root arguments spill (no measured slot)"); |
| 1443 | if (flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) { | 1695 | } else { |
| 1444 | if (slot < data.size()) data[slot++] = static_cast<std::uint32_t>(cbv_srv_uav_base_); | 1696 | if (resource && end >= rt::rootsig::kMaxUserSgprs) |
| 1445 | else note_once("compute root arguments leave no inline SGPR for the resource heap"); | 1697 | note_once("compute root arguments leave no inline SGPR for the resource heap"); |
| 1446 | } | 1698 | if (sampler && end + (resource ? 1u : 0u) >= rt::rootsig::kMaxUserSgprs) |
| 1447 | if (flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) { | 1699 | note_once("compute root arguments leave no inline SGPR for the sampler heap"); |
| 1448 | if (slot < data.size()) data[slot] = static_cast<std::uint32_t>(sampler_base_); | ||
| 1449 | else note_once("compute root arguments leave no inline SGPR for the sampler heap"); | ||
| 1450 | } | 1700 | } |
| 1451 | return data; | 1701 | return with_direct_heaps(compute_, end); |
| 1702 | } | ||
| 1703 | |||
| 1704 | // Graphics: vertex, pixel and geometry shaders share one user-data block and the s8 layout, so the heaps after | ||
| 1705 | // the explicit arguments serve every one of those stages (a pixel shader of a signature whose arguments end at | ||
| 1706 | // s11 reads the resource heap from s11). A mesh shader reads its arguments from s16, which is not modelled | ||
| 1707 | // (RootArgumentLayout, D3D-448): its pixel shader gets the heaps, the mesh stage does not. | ||
| 1708 | // LEONIDA_GRAPHICS_DIRECT_HEAPS=0 restores the previous arguments. | ||
| 1709 | std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> graphics_user_data() const { | ||
| 1710 | constexpr UINT direct = D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED | | ||
| 1711 | D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED; | ||
| 1712 | const RootArgumentLayout& layout = *graphics_.layout; | ||
| 1713 | if (!(layout.flags & direct)) return graphics_.data; | ||
| 1714 | static const bool enabled = direct_heaps_enabled("LEONIDA_GRAPHICS_DIRECT_HEAPS"); | ||
| 1715 | if (!enabled) { | ||
| 1716 | note_once("graphics direct heaps: off (LEONIDA_GRAPHICS_DIRECT_HEAPS=0), draws keep the explicit " | ||
| 1717 | "arguments only"); | ||
| 1718 | return graphics_.data; | ||
| 1719 | } | ||
| 1720 | const std::uint32_t end = layout.graphics_arguments_end; | ||
| 1721 | if (end >= rt::rootsig::kMaxUserSgprs) { | ||
| 1722 | note_once(layout.graphics_console | ||
| 1723 | ? "graphics direct heaps: not supplied, the explicit arguments fill s8..s31" | ||
| 1724 | : "graphics direct heaps: not supplied, the signature keeps the s0 graphics layout"); | ||
| 1725 | return graphics_.data; | ||
| 1726 | } | ||
| 1727 | static std::atomic<bool> logged{false}; | ||
| 1728 | if (!logged.exchange(true, std::memory_order_relaxed)) { | ||
| 1729 | const bool resource = layout.flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED; | ||
| 1730 | const bool sampler = layout.flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED; | ||
| 1731 | char resource_at[48] = "none", sampler_at[48] = "none"; | ||
| 1732 | if (resource) | ||
| 1733 | std::snprintf(resource_at, sizeof resource_at, "0x%llx at s%u", | ||
| 1734 | static_cast<unsigned long long>(std::uint32_t(cbv_srv_uav_base_)), end); | ||
| 1735 | if (sampler && end + (resource ? 1u : 0u) < rt::rootsig::kMaxUserSgprs) | ||
| 1736 | std::snprintf(sampler_at, sizeof sampler_at, "0x%llx at s%u", | ||
| 1737 | static_cast<unsigned long long>(std::uint32_t(sampler_base_)), | ||
| 1738 | end + (resource ? 1u : 0u)); | ||
| 1739 | std::fprintf(stderr, | ||
| 1740 | "[rt] d3d12: graphics direct heaps: resource heap %s, sampler heap %s, after %u dwords of " | ||
| 1741 | "explicit arguments from s8 (first draw; LEONIDA_GRAPHICS_DIRECT_HEAPS=0 restores the " | ||
| 1742 | "old arguments)\n", | ||
| 1743 | resource_at, sampler_at, end - 8); | ||
| 1744 | } | ||
| 1745 | return with_direct_heaps(graphics_, end); | ||
| 1452 | } | 1746 | } |
| 1453 | 1747 | ||
| 1454 | void record_dispatch(UINT x, UINT y, UINT z) { | 1748 | void record_dispatch(UINT x, UINT y, UINT z) { |
| 1455 | if (!compute_pipeline_ || !compute_.layout) return check(false); | 1749 | if (!compute_pipeline_ && unregistered_pipeline_) return; // a stub pipeline's dispatch: nothing to run |
| 1750 | if (!compute_pipeline_) return check(false); // separate lines: the rejection log names which | ||
| 1751 | if (!compute_.layout) return check(false); | ||
| 1752 | static const bool probe = std::getenv("LEONIDA_COMPUTE_INPUTS") != nullptr; | ||
| 1753 | if (probe) { | ||
| 1754 | static std::mutex mutex; | ||
| 1755 | static std::set<std::tuple<std::uintptr_t, const RootArgumentLayout*, UINT64>> seen; | ||
| 1756 | std::lock_guard lock(mutex); | ||
| 1757 | if (seen.emplace(compute_pipeline_, compute_.layout, cbv_srv_uav_base_).second) { | ||
| 1758 | std::fprintf(stderr, "[rt] compute-layout: pso=%p flags=%x bound-heap=0x%llx shader-heap=0x%llx", | ||
| 1759 | reinterpret_cast<void*>(compute_pipeline_), compute_.layout->flags, | ||
| 1760 | (unsigned long long)cbv_srv_uav_base_, | ||
| 1761 | (unsigned long long)console_descriptor_table_va(cbv_srv_uav_base_)); | ||
| 1762 | for (const auto& p : compute_.layout->parameters) | ||
| 1763 | std::fprintf(stderr, " (s%u type=%u const=%u slots=%u)", p.compute_offset, | ||
| 1764 | unsigned(p.type), p.constant_count, p.table_descriptors); | ||
| 1765 | std::fputc('\n', stderr); | ||
| 1766 | } | ||
| 1767 | } | ||
| 1456 | check(list_.dispatch({compute_pipeline_, compute_user_data(), x, y, z})); | 1768 | check(list_.dispatch({compute_pipeline_, compute_user_data(), x, y, z})); |
| 1457 | } | 1769 | } |
| 1458 | 1770 | ||
| @@ -1492,11 +1804,91 @@ private: | |||
| 1492 | // A draw needs a graphics pipeline, a graphics root signature and a topology. | 1804 | // A draw needs a graphics pipeline, a graphics root signature and a topology. |
| 1493 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { | 1805 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { |
| 1494 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); | 1806 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); |
| 1495 | note_direct_heap_indexing(graphics_.layout, false); | 1807 | note_direct_heap_indexing(graphics_.layout); |
| 1496 | // No viewport (never set, or unbound with RSSetViewports(0)) or every bound viewport empty: D3D12 rasterizes | 1808 | // No viewport (never set, or unbound with RSSetViewports(0)) or every bound viewport empty: D3D12 rasterizes |
| 1497 | // nothing, and Vulkan forbids a 0x0 viewport, so the draw is not recorded. | 1809 | // nothing, and Vulkan forbids a 0x0 viewport, so the draw is not recorded. |
| 1810 | // LOCAL DIAGNOSTIC: the state each draw is recorded with | ||
| 1811 | if (leonida::local_diagnostics()) { | ||
| 1812 | static std::atomic<std::uint64_t> draws{0}, skipped{0}; | ||
| 1813 | const bool skip = !any_nonempty_viewport(); | ||
| 1814 | const std::uint64_t n = ++draws, k = skip ? ++skipped : skipped.load(); | ||
| 1815 | if (n <= 8 || n % 1200 == 0) | ||
| 1816 | std::fprintf(stderr, "[rt] present-probe: draw #%llu (%s; %llu skipped so far) pso %p rts %u rt0 0x%llx " | ||
| 1817 | "mip %u viewport %.0fx%.0f at %.0f,%.0f scissor %d,%d-%d,%d count %u x%u\n", | ||
| 1818 | static_cast<unsigned long long>(n), skip ? "SKIPPED: no viewport" : "recorded", | ||
| 1819 | static_cast<unsigned long long>(k), reinterpret_cast<const void*>(graphics_pipeline_), | ||
| 1820 | render_target_count_, static_cast<unsigned long long>(render_targets_[0].resource), | ||
| 1821 | render_targets_[0].mip, viewport_.width, viewport_.height, viewport_.x, viewport_.y, | ||
| 1822 | scissor_.left, scissor_.top, scissor_.right, scissor_.bottom, count, instances); | ||
| 1823 | // LOCAL DIAGNOSTIC (fsdraw): the first draw of each pipeline, and one frame's draws every ~300 frames, | ||
| 1824 | // with the pipeline's description, the root data and what the bound descriptor tables hold. | ||
| 1825 | static std::mutex seen_mutex; | ||
| 1826 | static std::set<std::uintptr_t> seen; | ||
| 1827 | bool first_seen; | ||
| 1828 | { | ||
| 1829 | std::lock_guard lock(seen_mutex); | ||
| 1830 | first_seen = seen.insert(graphics_pipeline_).second; | ||
| 1831 | } | ||
| 1832 | const bool fullscreen = render_target_count_ >= 1 && count == 3 && viewport_.width >= 1000; | ||
| 1833 | if (fullscreen) leonida::fsdraw::note_target(render_targets_[0].resource); | ||
| 1834 | if (first_seen || (n - 1) % 600 < 2 || (n > 2 && n < 9)) { | ||
| 1835 | std::fprintf(stderr, "[rt] fsdraw: draw #%llu pso %p%s rt0 0x%llx rts %u depth 0x%llx count %u x%u: %s\n", | ||
| 1836 | static_cast<unsigned long long>(n), reinterpret_cast<const void*>(graphics_pipeline_), | ||
| 1837 | first_seen ? " (first use)" : "", static_cast<unsigned long long>(render_targets_[0].resource), | ||
| 1838 | render_target_count_, static_cast<unsigned long long>(depth_.resource), count, instances, | ||
| 1839 | leonida::fsdraw::pipeline(graphics_pipeline_).c_str()); | ||
| 1840 | std::fprintf(stderr, "[rt] fsdraw: draw #%llu layout %p user data [%s] cbv heap %p base 0x%llx sampler " | ||
| 1841 | "base 0x%llx tables %zu\n", | ||
| 1842 | static_cast<unsigned long long>(n), static_cast<const void*>(graphics_.layout), | ||
| 1843 | leonida::fsdraw::hex_words(graphics_.data.data(), graphics_.data.size()).c_str(), | ||
| 1844 | static_cast<const void*>(cbv_srv_uav_heap_), | ||
| 1845 | static_cast<unsigned long long>(cbv_srv_uav_base_), | ||
| 1846 | static_cast<unsigned long long>(sampler_base_), tables_.size()); | ||
| 1847 | for (std::size_t slot = 0; slot < 2; ++slot) { // the vertex buffers the blit's VS reads | ||
| 1848 | const auto& vb = vertex_buffers_[slot]; | ||
| 1849 | if (!vb.va) continue; | ||
| 1850 | std::uint32_t words[12]{}; | ||
| 1851 | bool read = false; | ||
| 1852 | MEMORY_BASIC_INFORMATION mbi{}; | ||
| 1853 | if (vb.va < leonida::fsdraw::arena_size()) { | ||
| 1854 | read = leonida::fsdraw::arena_words(vb.va, words, 12); | ||
| 1855 | } else if (VirtualQuery(reinterpret_cast<const void*>(static_cast<std::uintptr_t>(vb.va)), &mbi, sizeof mbi) && | ||
| 1856 | mbi.State == MEM_COMMIT && !(mbi.Protect & (PAGE_NOACCESS | PAGE_GUARD)) && mbi.Protect) { | ||
| 1857 | std::memcpy(words, reinterpret_cast<const void*>(static_cast<std::uintptr_t>(vb.va)), sizeof words); | ||
| 1858 | read = true; | ||
| 1859 | } | ||
| 1860 | std::fprintf(stderr, "[rt] fsdraw: draw #%llu vb%zu va 0x%llx size %u stride %u: %s [%s]\n", | ||
| 1861 | static_cast<unsigned long long>(n), slot, static_cast<unsigned long long>(vb.va), | ||
| 1862 | unsigned(vb.size), unsigned(vb.stride), read ? "read" : "unreadable", | ||
| 1863 | read ? leonida::fsdraw::hex_words(words, 12).c_str() : ""); | ||
| 1864 | } | ||
| 1865 | D3D12_DESCRIPTOR_HEAP_DESC heap_desc{}; | ||
| 1866 | std::uintptr_t heap_cpu = 0; | ||
| 1867 | if (cbv_srv_uav_heap_) { | ||
| 1868 | heap_desc = cbv_srv_uav_heap_->GetDesc(); | ||
| 1869 | heap_cpu = cbv_srv_uav_heap_->GetCPUDescriptorHandleForHeapStart().ptr; | ||
| 1870 | } | ||
| 1871 | char tag[48]; | ||
| 1872 | std::snprintf(tag, sizeof tag, "draw #%llu", static_cast<unsigned long long>(n)); | ||
| 1873 | for (const auto& [shader_va, slots] : tables_) { | ||
| 1874 | const std::uint64_t va = leonida::fsdraw::table_origin(shader_va); | ||
| 1875 | const std::uint64_t end = cbv_srv_uav_base_ + std::uint64_t(heap_desc.NumDescriptors) * 32; | ||
| 1876 | const bool in_heap = heap_cpu && va >= cbv_srv_uav_base_ && va + 32 <= end; | ||
| 1877 | const auto* cpu = in_heap ? reinterpret_cast<const std::uint8_t*>(heap_cpu + (va - cbv_srv_uav_base_)) | ||
| 1878 | : nullptr; | ||
| 1879 | const bool console = heap_cpu == std::uintptr_t(cbv_srv_uav_base_); | ||
| 1880 | if (shader_va != va) | ||
| 1881 | std::fprintf(stderr, "[rt] fsdraw: %s table title va 0x%llx is shader va 0x%llx\n", tag, | ||
| 1882 | static_cast<unsigned long long>(va), static_cast<unsigned long long>(shader_va)); | ||
| 1883 | leonida::fsdraw::describe_table(tag, shader_va, slots, cpu, | ||
| 1884 | !in_heap ? "none (not in the bound CBV/SRV/UAV heap)" | ||
| 1885 | : console ? "console (CPU address = GPU VA, title memory)" | ||
| 1886 | : "layer heap (arena)"); | ||
| 1887 | } | ||
| 1888 | } | ||
| 1889 | } | ||
| 1498 | if (!any_nonempty_viewport()) return; | 1890 | if (!any_nonempty_viewport()) return; |
| 1499 | leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, index_, topology_, viewport_, | 1891 | leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, index_, topology_, viewport_, |
| 1500 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, | 1892 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, |
| 1501 | first, base_vertex, first_instance}; | 1893 | first, base_vertex, first_instance}; |
| 1502 | draw.viewports = draw_viewports(); | 1894 | draw.viewports = draw_viewports(); |
| @@ -1551,12 +1943,14 @@ public: | |||
| 1551 | context_.enqueue_fence_callback(semaphore, queued, [context, semaphore, queued, value, held = held_] { | 1943 | context_.enqueue_fence_callback(semaphore, queued, [context, semaphore, queued, value, held = held_] { |
| 1552 | std::uint64_t now = 0; | 1944 | std::uint64_t now = 0; |
| 1553 | bool signaled = false; | 1945 | bool signaled = false; |
| 1554 | if (context->vk.vkGetSemaphoreCounterValue(context->device(), semaphore, &now) == VK_SUCCESS && | 1946 | if (context->observe_result(context->vk.vkGetSemaphoreCounterValue(context->device(), semaphore, &now), |
| 1947 | "vkGetSemaphoreCounterValue (fence callback)") == VK_SUCCESS && | ||
| 1555 | now >= queued && now < value) { | 1948 | now >= queued && now < value) { |
| 1556 | VkSemaphoreSignalInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO}; | 1949 | VkSemaphoreSignalInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO}; |
| 1557 | info.semaphore = semaphore; | 1950 | info.semaphore = semaphore; |
| 1558 | info.value = value; | 1951 | info.value = value; |
| 1559 | signaled = context->vk.vkSignalSemaphore(context->device(), &info) == VK_SUCCESS; | 1952 | signaled = context->observe_result(context->vk.vkSignalSemaphore(context->device(), &info), |
| 1953 | "vkSignalSemaphore (fence callback)") == VK_SUCCESS; | ||
| 1560 | } | 1954 | } |
| 1561 | held->fetch_sub(1); | 1955 | held->fetch_sub(1); |
| 1562 | if (signaled) { | 1956 | if (signaled) { |
| @@ -1726,6 +2120,83 @@ public: | |||
| 1726 | recorded.push_back(&list->commands()); | 2120 | recorded.push_back(&list->commands()); |
| 1727 | graphics.push_back(list); | 2121 | graphics.push_back(list); |
| 1728 | } | 2122 | } |
| 2123 | if (!context_.publish_cpu_textures()) { | ||
| 2124 | last_result_=leonida::ReplayResult::unresolved_resource; | ||
| 2125 | d3d12_trace::record(method,"failed"); | ||
| 2126 | return; | ||
| 2127 | } | ||
| 2128 | // Buffer V#s the title wrote into a bound table itself may name its own memory: rewritten to a GPU copy's | ||
| 2129 | // console VA before the copies are refreshed for this submission (src/title_va_alias.h). | ||
| 2130 | { | ||
| 2131 | std::map<std::uint64_t, std::uint32_t> tables; // each table once, with the most slots it was bound with | ||
| 2132 | for (const GraphicsCommandList* list : graphics) | ||
| 2133 | for (const auto& [table, descriptors] : list->tables()) | ||
| 2134 | tables[table] = std::max(tables[table], descriptors); | ||
| 2135 | // LOCAL DIAGNOSTIC (fsdraw): poke experiment, LEONIDA_FSDRAW_POKE_CB=1 | ||
| 2136 | if (leonida::fsdraw::poke_enabled()) { | ||
| 2137 | for (const auto& [table, descriptors] : tables) { | ||
| 2138 | const std::uint64_t origin = leonida::fsdraw::table_origin(table); | ||
| 2139 | if (origin != table) | ||
| 2140 | leonida::fsdraw::poke_cb( | ||
| 2141 | reinterpret_cast<const std::uint8_t*>(static_cast<std::uintptr_t>(origin))); | ||
| 2142 | } | ||
| 2143 | } | ||
| 2144 | // LOCAL DIAGNOSTIC (fsdraw): what the bound tables hold when the lists are submitted | ||
| 2145 | if (leonida::local_diagnostics()) { | ||
| 2146 | static std::atomic<std::uint64_t> submits{0}; | ||
| 2147 | const std::uint64_t n = tables.empty() ? 0 : ++submits; | ||
| 2148 | if (n && (n <= 6 || n % 300 == 0)) { | ||
| 2149 | char tag[48]; | ||
| 2150 | std::snprintf(tag, sizeof tag, "submit #%llu", static_cast<unsigned long long>(n)); | ||
| 2151 | for (const auto& [table, descriptors] : tables) { | ||
| 2152 | const std::uint64_t origin = leonida::fsdraw::table_origin(table); | ||
| 2153 | const bool title = origin != table; | ||
| 2154 | leonida::fsdraw::describe_table( | ||
| 2155 | tag, table, descriptors, | ||
| 2156 | title ? reinterpret_cast<const std::uint8_t*>(static_cast<std::uintptr_t>(origin)) : nullptr, | ||
| 2157 | title ? "title memory (CPU bytes read at the title VA the table was set with)" : "arena or unknown"); | ||
| 2158 | } | ||
| 2159 | } | ||
| 2160 | } | ||
| 2161 | if (!tables.empty()) { | ||
| 2162 | const auto start = std::chrono::steady_clock::now(); | ||
| 2163 | for (const auto& [table, descriptors] : tables) { | ||
| 2164 | // A table VA is an offset in this device's arena, so its own scanner reads it; the process-wide | ||
| 2165 | // hook belongs to the first device that registered and serves a context without a scanner. | ||
| 2166 | if (!context_.scan_table(table, descriptors)) | ||
| 2167 | leonida::title_va_aliases().scan_table(table, descriptors); | ||
| 2168 | if (!context_.prepare_sample_table(table, descriptors)) { | ||
| 2169 | last_result_ = leonida::ReplayResult::unresolved_resource; | ||
| 2170 | d3d12_trace::record(method, "failed"); | ||
| 2171 | return; | ||
| 2172 | } | ||
| 2173 | } | ||
| 2174 | leonida::title_va_aliases().note_scan(std::uint64_t( | ||
| 2175 | std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - start) | ||
| 2176 | .count())); | ||
| 2177 | } | ||
| 2178 | } | ||
| 2179 | for (const auto* list : graphics) { | ||
| 2180 | for (const auto& command : list->commands().commands()) { | ||
| 2181 | leonida::ResourceId resource = 0; | ||
| 2182 | if (const auto* b=std::get_if<leonida::TransitionBarrier>(&command)) { | ||
| 2183 | if (b->after & (D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE|D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE)) | ||
| 2184 | resource=b->resource; | ||
| 2185 | } else if (const auto* b=std::get_if<leonida::TextureBarrier>(&command)) { | ||
| 2186 | if (b->access_after & D3D12_BARRIER_ACCESS_SHADER_RESOURCE) resource=b->resource; | ||
| 2187 | } | ||
| 2188 | if (resource && !context_.prepare_sample_table(resource,0)) { | ||
| 2189 | last_result_=leonida::ReplayResult::unresolved_resource; | ||
| 2190 | d3d12_trace::record(method,"failed"); | ||
| 2191 | return; | ||
| 2192 | } | ||
| 2193 | } | ||
| 2194 | if (list->needs_all_sampled_views() && !context_.prepare_sample_table(0,0)) { | ||
| 2195 | last_result_ = leonida::ReplayResult::unresolved_resource; | ||
| 2196 | d3d12_trace::record(method,"failed"); | ||
| 2197 | return; | ||
| 2198 | } | ||
| 2199 | } | ||
| 1729 | last_result_ = queue_.execute(recorded, [&graphics] { return pin_lists(graphics); }); | 2200 | last_result_ = queue_.execute(recorded, [&graphics] { return pin_lists(graphics); }); |
| 1730 | d3d12_trace::record(method, last_result_ == leonida::ReplayResult::success ? "ok" : "failed"); | 2201 | d3d12_trace::record(method, last_result_ == leonida::ReplayResult::success ? "ok" : "failed"); |
| 1731 | if (last_result_ != leonida::ReplayResult::success) | 2202 | if (last_result_ != leonida::ReplayResult::success) |
| @@ -1740,7 +2211,7 @@ public: | |||
| 1740 | for (const auto& command : list.commands().commands()) { | 2211 | for (const auto& command : list.commands().commands()) { |
| 1741 | const auto range = leonida::query_range(command); | 2212 | const auto range = leonida::query_range(command); |
| 1742 | if (range.count && range.type == leonida::d3d12_query::timestamp && | 2213 | if (range.count && range.type == leonida::d3d12_query::timestamp && |
| 1743 | context_.query_heap_type(range.heap) == other) | 2214 | (!timestamp_family_supported() || context_.query_heap_type(range.heap) == other)) |
| 1744 | return false; | 2215 | return false; |
| 1745 | } | 2216 | } |
| 1746 | return true; | 2217 | return true; |
| @@ -1799,16 +2270,20 @@ public: | |||
| 1799 | } | 2270 | } |
| 1800 | 2271 | ||
| 1801 | // Ticks per second of the timestamps EndQuery writes: one tick is timestampPeriod nanoseconds. Every D3D12 | 2272 | // Ticks per second of the timestamps EndQuery writes: one tick is timestampPeriod nanoseconds. Every D3D12 |
| 1802 | // queue of the device shares one VkQueue, so they share one frequency. E_FAIL when that queue has no | 2273 | // device has one timestamp period, but individual queue families may not write timestamps. |
| 1803 | // timestamps (timestampValidBits 0), as D3D12 reports for a queue that does not support them. | ||
| 1804 | HRESULT STDMETHODCALLTYPE GetTimestampFrequency(UINT64* frequency) override { | 2274 | HRESULT STDMETHODCALLTYPE GetTimestampFrequency(UINT64* frequency) override { |
| 1805 | if (!frequency) return E_INVALIDARG; | 2275 | if (!frequency) return E_INVALIDARG; |
| 1806 | const double period = context_.timestamp_period(); | 2276 | const double period = context_.timestamp_period(); |
| 1807 | if (!(period > 0)) return E_FAIL; | 2277 | if (!(period > 0) || !timestamp_family_supported()) return E_FAIL; |
| 1808 | *frequency = static_cast<UINT64>(1e9 / period + 0.5); | 2278 | *frequency = static_cast<UINT64>(1e9 / period + 0.5); |
| 1809 | return S_OK; | 2279 | return S_OK; |
| 1810 | } | 2280 | } |
| 1811 | 2281 | ||
| 2282 | bool timestamp_family_supported() const { | ||
| 2283 | // The low-level queue factory also accepts caller-owned queues without a QueueClock. | ||
| 2284 | return !clock_.get_device_proc || clock_.timestamp_valid_bits != 0; | ||
| 2285 | } | ||
| 2286 | |||
| 1812 | // Queue labels, in order with the submissions around them. | 2287 | // Queue labels, in order with the submissions around them. |
| 1813 | void STDMETHODCALLTYPE SetMarker(UINT metadata, const void* data, UINT size) override { | 2288 | void STDMETHODCALLTYPE SetMarker(UINT metadata, const void* data, UINT size) override { |
| 1814 | d3d12_trace::record("ID3D12CommandQueue::SetMarker"); | 2289 | d3d12_trace::record("ID3D12CommandQueue::SetMarker"); |
| @@ -1874,16 +2349,20 @@ private: | |||
| 1874 | LARGE_INTEGER before{}, after{}; | 2349 | LARGE_INTEGER before{}, after{}; |
| 1875 | { | 2350 | { |
| 1876 | std::lock_guard submit_lock(context_.submit_mutex()); | 2351 | std::lock_guard submit_lock(context_.submit_mutex()); |
| 1877 | if (vk.vkQueueWaitIdle(vk_queue_) != VK_SUCCESS) return E_FAIL; | 2352 | if (context_.observe_result(vk.vkQueueWaitIdle(vk_queue_), "vkQueueWaitIdle (calibration)") != VK_SUCCESS) |
| 2353 | return E_FAIL; | ||
| 1878 | QueryPerformanceCounter(&before); | 2354 | QueryPerformanceCounter(&before); |
| 1879 | if (vk.vkQueueSubmit(vk_queue_, 1, &submit, VK_NULL_HANDLE) != VK_SUCCESS || | 2355 | if (context_.observe_result(vk.vkQueueSubmit(vk_queue_, 1, &submit, VK_NULL_HANDLE), |
| 1880 | vk.vkQueueWaitIdle(vk_queue_) != VK_SUCCESS) | 2356 | "vkQueueSubmit (calibration)") != VK_SUCCESS || |
| 2357 | context_.observe_result(vk.vkQueueWaitIdle(vk_queue_), "vkQueueWaitIdle (calibration)") != VK_SUCCESS) | ||
| 1881 | return E_FAIL; | 2358 | return E_FAIL; |
| 1882 | QueryPerformanceCounter(&after); | 2359 | QueryPerformanceCounter(&after); |
| 1883 | } | 2360 | } |
| 1884 | std::uint64_t stamp = 0; | 2361 | std::uint64_t stamp = 0; |
| 1885 | if (stamp_.get_query_pool_results(context_.device(), stamp_.queries, 0, 1, sizeof(stamp), &stamp, | 2362 | if (context_.observe_result(stamp_.get_query_pool_results(context_.device(), stamp_.queries, 0, 1, |
| 1886 | sizeof(stamp), VK_QUERY_RESULT_64_BIT) != VK_SUCCESS) | 2363 | sizeof(stamp), &stamp, sizeof(stamp), |
| 2364 | VK_QUERY_RESULT_64_BIT), | ||
| 2365 | "vkGetQueryPoolResults (calibration)") != VK_SUCCESS) | ||
| 1887 | return E_FAIL; | 2366 | return E_FAIL; |
| 1888 | if (clock_.timestamp_valid_bits < 64) stamp &= (std::uint64_t(1) << clock_.timestamp_valid_bits) - 1; | 2367 | if (clock_.timestamp_valid_bits < 64) stamp &= (std::uint64_t(1) << clock_.timestamp_valid_bits) - 1; |
| 1889 | gpu = stamp; | 2368 | gpu = stamp; |
| @@ -2016,6 +2495,11 @@ private: | |||
| 2016 | 2495 | ||
| 2017 | } // namespace | 2496 | } // namespace |
| 2018 | 2497 | ||
| 2498 | bool record_gpu_label(ID3D12GraphicsCommandList* list,const leonida::label::Write& write) { | ||
| 2499 | auto* own=GraphicsCommandList::from(list); | ||
| 2500 | return own && own->xbox_label(write); | ||
| 2501 | } | ||
| 2502 | |||
| 2019 | HRESULT create_command_queue(leonida::VulkanContext& context, VkQueue queue, std::uint32_t queue_family, | 2503 | HRESULT create_command_queue(leonida::VulkanContext& context, VkQueue queue, std::uint32_t queue_family, |
| 2020 | const D3D12_COMMAND_QUEUE_DESC& desc, REFIID riid, void** out, IUnknown* device, | 2504 | const D3D12_COMMAND_QUEUE_DESC& desc, REFIID riid, void** out, IUnknown* device, |
| 2021 | const QueueClock& clock, std::shared_ptr<void> queue_lease) { | 2505 | const QueueClock& clock, std::shared_ptr<void> queue_lease) { |
| @@ -2103,7 +2587,8 @@ HRESULT set_event_on_multiple_fence_completion(leonida::VulkanContext& context, | |||
| 2103 | wait.semaphoreCount = count; | 2587 | wait.semaphoreCount = count; |
| 2104 | wait.pSemaphores = semaphores.data(); | 2588 | wait.pSemaphores = semaphores.data(); |
| 2105 | wait.pValues = values; | 2589 | wait.pValues = values; |
| 2106 | const VkResult result = context.vk.vkWaitSemaphores(context.device(), &wait, UINT64_MAX); | 2590 | const VkResult result = context.observe_result( |
| 2591 | context.vk.vkWaitSemaphores(context.device(), &wait, UINT64_MAX), "vkWaitSemaphores (multiple fences)"); | ||
| 2107 | // Unlike a single fence's blocking SetEventOnCompletion, this path invokes Vulkan directly. | 2592 | // Unlike a single fence's blocking SetEventOnCompletion, this path invokes Vulkan directly. |
| 2108 | // A lost device must wake its other registered fence waiters and reject later submissions. | 2593 | // A lost device must wake its other registered fence waiters and reject later submissions. |
| 2109 | if (result == VK_ERROR_DEVICE_LOST) { | 2594 | if (result == VK_ERROR_DEVICE_LOST) { |