diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 21:55:03 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 21:55:03 -0500 |
| commit | 50740a1bc541b2d007f87cad86f7a776f994f3e7 (patch) | |
| tree | 8a0baed3231cd9ccc85987ba5e2ee7d4173dd0f8 /runtime/shims/d3d12_com.cpp | |
| parent | e39adc942cfe473f3febbf9ff30a6787baa705bb (diff) | |
| parent | f78385646c5483d8bcd7d4161ee5dcd48d2b7fb3 (diff) | |
Merge main into carry/old-9921-ms-compressed-exports
The "Mesh shaders" row of docs/design.md changed on main (#802); the
compressed-export sentence is added to main's version of the row.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 649 |
1 files changed, 592 insertions, 57 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 4f4904e96..b6ec111c7 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 |
| @@ -411,12 +432,20 @@ public: | |||
| 411 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); | 432 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); |
| 412 | if (!count) { | 433 | if (!count) { |
| 413 | viewport_ = {}; | 434 | viewport_ = {}; |
| 435 | viewports_.fill({}); | ||
| 436 | viewport_count_ = 0; | ||
| 437 | draw_viewports_stale_ = true; | ||
| 414 | return; | 438 | return; |
| 415 | } | 439 | } |
| 416 | if (!viewports || count > D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE) return check(false); | 440 | if (!viewports || count > D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE) return check(false); |
| 417 | if (count > 1) stub("ID3D12GraphicsCommandList::RSSetViewports (more than one viewport; the first is used)"); | 441 | viewports_.fill({}); |
| 418 | const D3D12_VIEWPORT& v = viewports[0]; | 442 | for (UINT i = 0; i < count; ++i) { |
| 419 | viewport_ = {v.TopLeftX, v.TopLeftY, v.Width, v.Height, v.MinDepth, v.MaxDepth}; | 443 | const D3D12_VIEWPORT& v = viewports[i]; |
| 444 | viewports_[i] = {v.TopLeftX, v.TopLeftY, v.Width, v.Height, v.MinDepth, v.MaxDepth}; | ||
| 445 | } | ||
| 446 | viewport_count_ = count; | ||
| 447 | viewport_ = viewports_[0]; | ||
| 448 | draw_viewports_stale_ = true; | ||
| 420 | } | 449 | } |
| 421 | 450 | ||
| 422 | void STDMETHODCALLTYPE RSSetScissorRects(UINT count, const D3D12_RECT* rects) override { | 451 | void STDMETHODCALLTYPE RSSetScissorRects(UINT count, const D3D12_RECT* rects) override { |
| @@ -425,11 +454,25 @@ public: | |||
| 425 | // With no scissor bound, the rasterizer clips every fragment, as on native D3D12. | 454 | // With no scissor bound, the rasterizer clips every fragment, as on native D3D12. |
| 426 | if (!count) { | 455 | if (!count) { |
| 427 | scissor_ = {}; | 456 | scissor_ = {}; |
| 457 | scissors_.fill({}); | ||
| 458 | scissor_count_ = 0; | ||
| 459 | draw_viewports_stale_ = true; | ||
| 428 | return; | 460 | return; |
| 429 | } | 461 | } |
| 430 | if (!rects) return; | 462 | if (!rects) return; |
| 431 | if (count > 1) stub("ID3D12GraphicsCommandList::RSSetScissorRects (more than one rectangle; the first is used)"); | 463 | // WARP still closes a list after RSSetScissorRects(17) (d3d12_close_parity), so the call is not an error: |
| 432 | scissor_ = {rects[0].left, rects[0].top, rects[0].right, rects[0].bottom}; | 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 | } | ||
| 469 | scissors_.fill({}); | ||
| 470 | for (UINT i = 0; i < count; ++i) { | ||
| 471 | scissors_[i] = {rects[i].left, rects[i].top, rects[i].right, rects[i].bottom}; | ||
| 472 | } | ||
| 473 | scissor_count_ = count; | ||
| 474 | scissor_ = scissors_[0]; | ||
| 475 | draw_viewports_stale_ = true; | ||
| 433 | } | 476 | } |
| 434 | 477 | ||
| 435 | // D3D12 reads render-target and depth descriptors here, so their views are copied now. | 478 | // D3D12 reads render-target and depth descriptors here, so their views are copied now. |
| @@ -486,10 +529,11 @@ public: | |||
| 486 | d3d12_trace::record("ID3D12GraphicsCommandList6::DispatchMesh"); | 529 | d3d12_trace::record("ID3D12GraphicsCommandList6::DispatchMesh"); |
| 487 | 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); |
| 488 | if (!graphics_pipeline_ || !graphics_.layout) return check(false); | 531 | if (!graphics_pipeline_ || !graphics_.layout) return check(false); |
| 489 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; // as record_draw: nothing is rasterized | 532 | if (!any_nonempty_viewport()) return; // as record_draw: nothing is rasterized |
| 490 | leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, {}, 0, viewport_, | 533 | 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}; | 534 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; |
| 492 | draw.mesh = true; | 535 | draw.mesh = true; |
| 536 | draw.viewports = draw_viewports(); | ||
| 493 | check(list_.draw(std::move(draw))); | 537 | check(list_.draw(std::move(draw))); |
| 494 | } | 538 | } |
| 495 | 539 | ||
| @@ -499,19 +543,30 @@ public: | |||
| 499 | for (UINT i = 0; i < count; ++i) { | 543 | for (UINT i = 0; i < count; ++i) { |
| 500 | const auto& b = barriers[i]; | 544 | const auto& b = barriers[i]; |
| 501 | switch (b.Type) { | 545 | switch (b.Type) { |
| 502 | case D3D12_RESOURCE_BARRIER_TYPE_TRANSITION: | 546 | case D3D12_RESOURCE_BARRIER_TYPE_TRANSITION: { |
| 503 | check(list_.transition({resource_id(b.Transition.pResource), b.Transition.Subresource, | 547 | const auto& t = b.Transition; |
| 504 | static_cast<std::uint32_t>(b.Transition.StateBefore), | 548 | const auto logical = resource_id(t.pResource); |
| 505 | static_cast<std::uint32_t>(b.Transition.StateAfter)}, | 549 | const bool was_storage = (t.StateBefore & D3D12_RESOURCE_STATE_UNORDERED_ACCESS) != 0; |
| 506 | static_cast<std::uint32_t>(b.Flags))); | 550 | const bool storage = (t.StateAfter & D3D12_RESOURCE_STATE_UNORDERED_ACCESS) != 0; |
| 551 | const auto pair = (was_storage || storage) ? xbox_depth_storage(t.pResource) : XboxDepthStorage{}; | ||
| 552 | auto before = t.StateBefore; | ||
| 553 | if (pair.colour && was_storage != storage && !(b.Flags & D3D12_RESOURCE_BARRIER_FLAG_BEGIN_ONLY)) { | ||
| 554 | check(copy_depth_storage(logical, pair, storage, {t.Subresource})); | ||
| 555 | before = D3D12_RESOURCE_STATE_COPY_DEST; | ||
| 556 | } | ||
| 557 | check(list_.transition({pair.colour && storage ? pair.colour : logical, t.Subresource, | ||
| 558 | u(before), u(t.StateAfter)}, u(b.Flags))); | ||
| 507 | break; | 559 | break; |
| 560 | } | ||
| 508 | case D3D12_RESOURCE_BARRIER_TYPE_ALIASING: | 561 | case D3D12_RESOURCE_BARRIER_TYPE_ALIASING: |
| 509 | check(!b.Flags && list_.aliasing_barrier({resource_id(b.Aliasing.pResourceBefore), | 562 | check(!b.Flags && list_.aliasing_barrier({resource_id(b.Aliasing.pResourceBefore), |
| 510 | resource_id(b.Aliasing.pResourceAfter)})); | 563 | resource_id(b.Aliasing.pResourceAfter)})); |
| 511 | break; | 564 | break; |
| 512 | case D3D12_RESOURCE_BARRIER_TYPE_UAV: | 565 | case D3D12_RESOURCE_BARRIER_TYPE_UAV: { |
| 513 | check(!b.Flags && list_.uav_barrier({resource_id(b.UAV.pResource)})); | 566 | const auto pair = xbox_depth_storage(b.UAV.pResource); |
| 567 | check(!b.Flags && list_.uav_barrier({pair.colour ? pair.colour : resource_id(b.UAV.pResource)})); | ||
| 514 | break; | 568 | break; |
| 569 | } | ||
| 515 | default: | 570 | default: |
| 516 | check(false); | 571 | check(false); |
| 517 | } | 572 | } |
| @@ -526,29 +581,61 @@ public: | |||
| 526 | switch (group[g].Type) { | 581 | switch (group[g].Type) { |
| 527 | case D3D12_BARRIER_TYPE_GLOBAL: { | 582 | case D3D12_BARRIER_TYPE_GLOBAL: { |
| 528 | const auto& b = group[g].pGlobalBarriers[i]; | 583 | const auto& b = group[g].pGlobalBarriers[i]; |
| 529 | check(list_.barrier(leonida::GlobalBarrier{u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), | 584 | if (!list_.barrier(leonida::GlobalBarrier{u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), |
| 530 | u(b.AccessAfter)})); | 585 | u(b.AccessAfter)}) && |
| 586 | reject()) | ||
| 587 | std::fprintf(stderr, "[rt] d3d12: global barrier sync 0x%x->0x%x access 0x%x->0x%x\n", | ||
| 588 | u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), u(b.AccessAfter)); | ||
| 531 | break; | 589 | break; |
| 532 | } | 590 | } |
| 533 | case D3D12_BARRIER_TYPE_BUFFER: { | 591 | case D3D12_BARRIER_TYPE_BUFFER: { |
| 534 | const auto& b = group[g].pBufferBarriers[i]; | 592 | const auto& b = group[g].pBufferBarriers[i]; |
| 535 | check(list_.barrier(leonida::BufferBarrier{u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), | 593 | if (!list_.barrier(leonida::BufferBarrier{u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), |
| 536 | u(b.AccessAfter), resource_id(b.pResource)})); | 594 | u(b.AccessAfter), resource_id(b.pResource)}) && |
| 595 | reject()) { | ||
| 596 | std::fprintf(stderr, "[rt] d3d12: buffer barrier sync 0x%x->0x%x access 0x%x->0x%x offset " | ||
| 597 | "0x%llx size 0x%llx\n", | ||
| 598 | u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), u(b.AccessAfter), | ||
| 599 | static_cast<unsigned long long>(b.Offset), static_cast<unsigned long long>(b.Size)); | ||
| 600 | describe_barrier_resource(b.pResource); | ||
| 601 | } | ||
| 537 | break; | 602 | break; |
| 538 | } | 603 | } |
| 539 | case D3D12_BARRIER_TYPE_TEXTURE: { | 604 | case D3D12_BARRIER_TYPE_TEXTURE: { |
| 540 | const auto& b = group[g].pTextureBarriers[i]; | 605 | const auto& b = group[g].pTextureBarriers[i]; |
| 541 | const auto& r = b.Subresources; | 606 | const auto& r = b.Subresources; |
| 542 | check(list_.barrier(leonida::TextureBarrier{ | 607 | leonida::TextureBarrier barrier{ |
| 543 | u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), u(b.AccessAfter), u(b.LayoutBefore), | 608 | u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), u(b.AccessAfter), u(b.LayoutBefore), |
| 544 | u(b.LayoutAfter), resource_id(b.pResource), | 609 | u(b.LayoutAfter), resource_id(b.pResource), |
| 545 | {r.IndexOrFirstMipLevel, r.NumMipLevels, r.FirstArraySlice, r.NumArraySlices, r.FirstPlane, | 610 | {r.IndexOrFirstMipLevel, r.NumMipLevels, r.FirstArraySlice, r.NumArraySlices, r.FirstPlane, |
| 546 | r.NumPlanes}, | 611 | r.NumPlanes}, u(b.Flags)}; |
| 547 | u(b.Flags)})); | 612 | const bool was_storage = storage_access(b.LayoutBefore, b.AccessBefore); |
| 613 | const bool storage = storage_access(b.LayoutAfter, b.AccessAfter); | ||
| 614 | const auto pair = (was_storage || storage) ? xbox_depth_storage(b.pResource) : XboxDepthStorage{}; | ||
| 615 | if (pair.colour) { | ||
| 616 | if (was_storage != storage && b.SyncAfter != D3D12_BARRIER_SYNC_SPLIT && | ||
| 617 | b.LayoutBefore != D3D12_BARRIER_LAYOUT_UNDEFINED && !(b.Flags & D3D12_TEXTURE_BARRIER_FLAG_DISCARD)) { | ||
| 618 | check(copy_depth_storage(barrier.resource, pair, storage, barrier.range)); | ||
| 619 | barrier.sync_before = leonida::d3d12_sync::copy; | ||
| 620 | barrier.access_before = leonida::d3d12_access::copy_dest; | ||
| 621 | barrier.layout_before = leonida::d3d12_layout::copy_dest; | ||
| 622 | } | ||
| 623 | if (storage) barrier.resource = pair.colour; | ||
| 624 | } | ||
| 625 | if (!list_.barrier(barrier) && reject()) { | ||
| 626 | std::fprintf(stderr, | ||
| 627 | "[rt] d3d12: texture barrier sync 0x%x->0x%x access 0x%x->0x%x layout %u->%u " | ||
| 628 | "mips %u+%u layers %u+%u planes %u+%u flags 0x%x\n", | ||
| 629 | u(b.SyncBefore), u(b.SyncAfter), u(b.AccessBefore), u(b.AccessAfter), | ||
| 630 | u(b.LayoutBefore), u(b.LayoutAfter), r.IndexOrFirstMipLevel, r.NumMipLevels, | ||
| 631 | r.FirstArraySlice, r.NumArraySlices, r.FirstPlane, r.NumPlanes, u(b.Flags)); | ||
| 632 | describe_barrier_resource(b.pResource); | ||
| 633 | } | ||
| 548 | break; | 634 | break; |
| 549 | } | 635 | } |
| 550 | default: | 636 | default: |
| 551 | check(false); | 637 | if (reject()) |
| 638 | std::fprintf(stderr, "[rt] d3d12: barrier group type %d\n", int(group[g].Type)); | ||
| 552 | } | 639 | } |
| 553 | } | 640 | } |
| 554 | } | 641 | } |
| @@ -616,7 +703,24 @@ public: | |||
| 616 | if (!dst || !src) return check(false); | 703 | if (!dst || !src) return check(false); |
| 617 | leonida::CopyTexture copy{location(*dst), x, y, z, location(*src), box != nullptr, {}}; | 704 | 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}; | 705 | if (box) copy.box = {box->left, box->top, box->front, box->right, box->bottom, box->back}; |
| 619 | check(list_.copy_texture(copy)); | 706 | if (!list_.copy_texture(copy) && reject()) { |
| 707 | for (const auto* l : {dst, src}) { | ||
| 708 | const char* side = l == dst ? "dst" : "src"; | ||
| 709 | if (l->Type != D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT) { | ||
| 710 | std::fprintf(stderr, "[rt] d3d12: %s type %d resource %p subresource %u\n", side, int(l->Type), | ||
| 711 | static_cast<void*>(l->pResource), l->SubresourceIndex); | ||
| 712 | continue; | ||
| 713 | } | ||
| 714 | const auto& f = l->PlacedFootprint; | ||
| 715 | std::fprintf(stderr, "[rt] d3d12: %s footprint resource %p offset 0x%llx format %d %ux%ux%u pitch %u\n", | ||
| 716 | side, static_cast<void*>(l->pResource), static_cast<unsigned long long>(f.Offset), | ||
| 717 | int(f.Footprint.Format), f.Footprint.Width, f.Footprint.Height, f.Footprint.Depth, | ||
| 718 | f.Footprint.RowPitch); | ||
| 719 | } | ||
| 720 | if (box) | ||
| 721 | std::fprintf(stderr, "[rt] d3d12: at %u,%u,%u box %u,%u,%u-%u,%u,%u\n", x, y, z, box->left, box->top, | ||
| 722 | box->front, box->right, box->bottom, box->back); | ||
| 723 | } | ||
| 620 | } | 724 | } |
| 621 | 725 | ||
| 622 | void STDMETHODCALLTYPE CopyResource(ID3D12Resource* dst, ID3D12Resource* src) override { | 726 | void STDMETHODCALLTYPE CopyResource(ID3D12Resource* dst, ID3D12Resource* src) override { |
| @@ -698,6 +802,18 @@ public: | |||
| 698 | d3d12_trace::record("ID3D12GraphicsCommandList::ClearRenderTargetView"); | 802 | d3d12_trace::record("ID3D12GraphicsCommandList::ClearRenderTargetView"); |
| 699 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); | 803 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); |
| 700 | if (!view.ptr || !color) return check(false); | 804 | if (!view.ptr || !color) return check(false); |
| 805 | // LOCAL DIAGNOSTIC: what the title clears, and with which colour | ||
| 806 | if (leonida::local_diagnostics()) { | ||
| 807 | static std::atomic<std::uint64_t> clears{0}; | ||
| 808 | const std::uint64_t n = ++clears; | ||
| 809 | if (n <= 6 || n % 600 == 0) { | ||
| 810 | const auto& t = *reinterpret_cast<const leonida::TargetView*>(view.ptr); | ||
| 811 | std::fprintf(stderr, "[rt] present-probe: ClearRenderTargetView #%llu resource 0x%llx mip %u layers %u+%u " | ||
| 812 | "colour %.3f %.3f %.3f %.3f rects %u\n", | ||
| 813 | static_cast<unsigned long long>(n), static_cast<unsigned long long>(t.resource), t.mip, | ||
| 814 | t.first_layer, t.layer_count, color[0], color[1], color[2], color[3], count); | ||
| 815 | } | ||
| 816 | } | ||
| 701 | check(list_.clear_render_target(*reinterpret_cast<const leonida::TargetView*>(view.ptr), | 817 | 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))); | 818 | {color[0], color[1], color[2], color[3]}, to_rects(count, rects))); |
| 703 | } | 819 | } |
| @@ -835,18 +951,35 @@ public: | |||
| 835 | return check(false); | 951 | return check(false); |
| 836 | } | 952 | } |
| 837 | // The record size is checked for every call: D3D12 rejects a signature whose stride cannot hold a record | 953 | // The record size is checked for every call: D3D12 rejects a signature whose stride cannot hold a record |
| 838 | // even when no command runs. The argument span is what the signature can walk, max_command_count * stride: | 954 | // even when no command runs. |
| 839 | // the host refuses a range that ends past the buffer (`240` with stride `20` in a `256`-byte buffer), and | ||
| 840 | // it checks the offset itself when max_command_count is 0, where the required bytes are none. | ||
| 841 | const UINT64 record_size = | 955 | const UINT64 record_size = |
| 842 | kind == leonida::IndirectKind::draw ? sizeof(D3D12_DRAW_ARGUMENTS) | 956 | kind == leonida::IndirectKind::draw ? sizeof(D3D12_DRAW_ARGUMENTS) |
| 843 | : kind == leonida::IndirectKind::draw_indexed ? sizeof(D3D12_DRAW_INDEXED_ARGUMENTS) | 957 | : kind == leonida::IndirectKind::draw_indexed ? sizeof(D3D12_DRAW_INDEXED_ARGUMENTS) |
| 844 | : kind == leonida::IndirectKind::dispatch ? sizeof(D3D12_DISPATCH_ARGUMENTS) | 958 | : kind == leonida::IndirectKind::dispatch ? sizeof(D3D12_DISPATCH_ARGUMENTS) |
| 959 | : kind == leonida::IndirectKind::dispatch_mesh ? sizeof(D3D12_DISPATCH_MESH_ARGUMENTS) | ||
| 845 | : 0; | 960 | : 0; |
| 846 | if (!record_size || stride < record_size || stride % 4 != 0) return check(false); | 961 | if (!record_size || stride < record_size || stride % 4 != 0) return check(false); |
| 847 | if (argument_buffer_offset > arg_desc.Width || | 962 | // The argument range is the bytes the commands read: (max_command_count - 1) * stride + record_size, and |
| 848 | UINT64(max_command_count) > (arg_desc.Width - argument_buffer_offset) / stride) | 963 | // nothing when max_command_count is 0. PC D3D12 runtimes do not agree here. Some bound max_command_count * |
| 849 | return check(false); | 964 | // stride and check the offset at a zero count; others (D3D12Core 10.0.26100.9818, WARP and NVIDIA alike) |
| 965 | // accept a last record that fits while its stride runs past the buffer, and any aligned offset at a zero | ||
| 966 | // count. The layer takes the wider rule: it is the one Vulkan's own indirect draws use, no byte outside the | ||
| 967 | // buffer is read, and refusing here drops the whole command list for a call a real runtime runs. | ||
| 968 | // d3d12_execute_indirect_validation_test lists the five cases the runtimes split on. | ||
| 969 | if (max_command_count) { | ||
| 970 | const UINT64 required = UINT64(max_command_count - 1) * stride + record_size; | ||
| 971 | if (argument_buffer_offset > arg_desc.Width || arg_desc.Width - argument_buffer_offset < required) | ||
| 972 | return check(false); | ||
| 973 | } | ||
| 974 | if (kind == leonida::IndirectKind::dispatch_mesh) { | ||
| 975 | // Not replayed yet (#3777 replays it as vkCmdDrawMeshTasksIndirectEXT): nothing is recorded, and the | ||
| 976 | // first such call says so. The list stays valid. | ||
| 977 | static std::atomic<bool> logged{false}; | ||
| 978 | if (!logged.exchange(true, std::memory_order_relaxed)) | ||
| 979 | std::fprintf(stderr, "[rt] d3d12: ExecuteIndirect with a DISPATCH_MESH signature is not replayed yet; " | ||
| 980 | "nothing was recorded\n"); | ||
| 981 | return; | ||
| 982 | } | ||
| 850 | leonida::ExecuteIndirect cmd{}; | 983 | leonida::ExecuteIndirect cmd{}; |
| 851 | cmd.kind = kind; | 984 | cmd.kind = kind; |
| 852 | cmd.stride = stride; | 985 | cmd.stride = stride; |
| @@ -858,7 +991,7 @@ public: | |||
| 858 | if (kind == leonida::IndirectKind::dispatch && max_command_count) { // the pipeline and root arguments a Dispatch would record | 991 | 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); | 992 | if (!compute_pipeline_ || !compute_.layout) return check(false); |
| 860 | cmd.pipeline = compute_pipeline_; | 993 | cmd.pipeline = compute_pipeline_; |
| 861 | cmd.user_data = compute_.data; | 994 | cmd.user_data = compute_user_data(); |
| 862 | } | 995 | } |
| 863 | check(list_.execute_indirect(cmd)); | 996 | check(list_.execute_indirect(cmd)); |
| 864 | } | 997 | } |
| @@ -968,9 +1101,47 @@ public: | |||
| 968 | check(list_.copy_acceleration_structure({dest, source, static_cast<std::uint32_t>(mode)})); | 1101 | check(list_.copy_acceleration_structure({dest, source, static_cast<std::uint32_t>(mode)})); |
| 969 | } | 1102 | } |
| 970 | 1103 | ||
| 1104 | bool xbox_label(const leonida::label::Write& op) { return list_.write_gpu_label(op); } | ||
| 971 | static GraphicsCommandList* from(IUnknown* object) { return Child::from<GraphicsCommandList>(object); } | 1105 | static GraphicsCommandList* from(IUnknown* object) { return Child::from<GraphicsCommandList>(object); } |
| 972 | 1106 | ||
| 973 | private: | 1107 | private: |
| 1108 | static bool storage_access(D3D12_BARRIER_LAYOUT layout, D3D12_BARRIER_ACCESS access) { | ||
| 1109 | return (access & D3D12_BARRIER_ACCESS_UNORDERED_ACCESS) || | ||
| 1110 | layout == D3D12_BARRIER_LAYOUT_UNORDERED_ACCESS || | ||
| 1111 | layout == D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_UNORDERED_ACCESS || | ||
| 1112 | layout == D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_UNORDERED_ACCESS; | ||
| 1113 | } | ||
| 1114 | |||
| 1115 | bool copy_depth_storage(leonida::ResourceId depth, XboxDepthStorage pair, bool to_storage, | ||
| 1116 | const leonida::BarrierSubresourceRange& r) { | ||
| 1117 | namespace access = leonida::d3d12_access; | ||
| 1118 | namespace sync = leonida::d3d12_sync; | ||
| 1119 | const auto from = to_storage ? depth : pair.colour, to = to_storage ? pair.colour : depth; | ||
| 1120 | // The two images can be in GENERAL for bindless use. Order prior depth writes, unordered writes and | ||
| 1121 | // readers of the destination even when Vulkan does not need a layout transition. | ||
| 1122 | if (!list_.barrier(leonida::GlobalBarrier{sync::all, sync::copy, | ||
| 1123 | access::depth_stencil_write | access::depth_stencil_read | access::unordered_access | | ||
| 1124 | access::shader_resource | access::copy_source | access::copy_dest, | ||
| 1125 | access::copy_source | access::copy_dest})) return false; | ||
| 1126 | if (!r.mip_count && r.index_or_first_mip == D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES) | ||
| 1127 | return list_.copy_resource({to, from}); | ||
| 1128 | const auto one = [&](UINT subresource) { | ||
| 1129 | leonida::CopyTexture copy{}; | ||
| 1130 | copy.src.resource = from; | ||
| 1131 | copy.dst.resource = to; | ||
| 1132 | copy.src.subresource = copy.dst.subresource = subresource; | ||
| 1133 | return list_.copy_texture(copy); | ||
| 1134 | }; | ||
| 1135 | if (!r.mip_count) return r.index_or_first_mip < pair.mips * pair.layers && one(r.index_or_first_mip); | ||
| 1136 | if (r.first_plane || r.plane_count != 1 || !r.layer_count || | ||
| 1137 | r.first_layer >= pair.layers || r.layer_count > pair.layers - r.first_layer || | ||
| 1138 | r.index_or_first_mip >= pair.mips || r.mip_count > pair.mips - r.index_or_first_mip) return false; | ||
| 1139 | for (UINT layer = r.first_layer; layer < r.first_layer + r.layer_count; ++layer) | ||
| 1140 | for (UINT mip = r.index_or_first_mip; mip < r.index_or_first_mip + r.mip_count; ++mip) | ||
| 1141 | if (!one(layer * pair.mips + mip)) return false; | ||
| 1142 | return true; | ||
| 1143 | } | ||
| 1144 | |||
| 974 | static bool valid_postbuild_info_type(D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_TYPE type) { | 1145 | static bool valid_postbuild_info_type(D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_TYPE type) { |
| 975 | switch (type) { | 1146 | switch (type) { |
| 976 | case D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_COMPACTED_SIZE: | 1147 | case D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_COMPACTED_SIZE: |
| @@ -1016,8 +1187,35 @@ private: | |||
| 1016 | return out; | 1187 | return out; |
| 1017 | } | 1188 | } |
| 1018 | 1189 | ||
| 1019 | void check(bool ok) { | 1190 | // Records an invalid call: Close then returns E_INVALIDARG and ExecuteCommandLists refuses the list. The first |
| 1020 | if (!ok) invalid_ = true; | 1191 | // rejection at each call site is logged with the method and line, so a failed Close names its cause; reject() |
| 1192 | // returns true for that first one, when a caller may print the arguments too. | ||
| 1193 | bool reject(std::source_location where = std::source_location::current()) { | ||
| 1194 | invalid_ = true; | ||
| 1195 | static std::array<std::atomic<bool>, 4096> seen{}; | ||
| 1196 | const std::uint_least32_t line = where.line(); | ||
| 1197 | if (line < seen.size() && seen[line].exchange(true, std::memory_order_relaxed)) return false; | ||
| 1198 | std::string_view method = where.function_name(); | ||
| 1199 | if (const auto at = method.find("GraphicsCommandList::"); at != std::string_view::npos) | ||
| 1200 | method.remove_prefix(at); | ||
| 1201 | if (const auto paren = method.find('('); paren != std::string_view::npos) method = method.substr(0, paren); | ||
| 1202 | std::fprintf(stderr, "[rt] d3d12: %.*s rejected a call (d3d12_com.cpp:%u): the list's Close returns " | ||
| 1203 | "E_INVALIDARG and ExecuteCommandLists refuses it\n", | ||
| 1204 | int(method.size()), method.data(), unsigned(line)); | ||
| 1205 | return true; | ||
| 1206 | } | ||
| 1207 | void check(bool ok, std::source_location where = std::source_location::current()) { | ||
| 1208 | if (!ok) reject(where); | ||
| 1209 | } | ||
| 1210 | static void describe_barrier_resource(ID3D12Resource* resource) { | ||
| 1211 | if (!resource) return void(std::fprintf(stderr, "[rt] d3d12: resource null\n")); | ||
| 1212 | const D3D12_RESOURCE_DESC desc = resource->GetDesc(); | ||
| 1213 | std::fprintf(stderr, | ||
| 1214 | "[rt] d3d12: resource %p dimension %d format %d %llux%ux%u mips %u samples %u layout %d " | ||
| 1215 | "flags 0x%x\n", | ||
| 1216 | static_cast<void*>(resource), int(desc.Dimension), int(desc.Format), | ||
| 1217 | static_cast<unsigned long long>(desc.Width), desc.Height, desc.DepthOrArraySize, desc.MipLevels, | ||
| 1218 | desc.SampleDesc.Count, int(desc.Layout), unsigned(desc.Flags)); | ||
| 1021 | } | 1219 | } |
| 1022 | 1220 | ||
| 1023 | void stop_recording() { | 1221 | void stop_recording() { |
| @@ -1120,12 +1318,24 @@ private: | |||
| 1120 | if (g::is_image(words)) { | 1318 | if (g::is_image(words)) { |
| 1121 | g::Image image{}; | 1319 | g::Image image{}; |
| 1122 | if (!g::decode(words, image)) return false; | 1320 | if (!g::decode(words, image)) return false; |
| 1321 | out.resource = uav_image_resource(resource, image.address); | ||
| 1322 | if (!out.resource) return false; | ||
| 1123 | const bool volume = image.type == g::ImageType::image_3d; | 1323 | const bool volume = image.type == g::ImageType::image_3d; |
| 1324 | const auto pair = xbox_depth_storage(resource); | ||
| 1325 | if (pair.colour) out.resource = pair.colour; | ||
| 1326 | if (g::partial_volume_uav(words, image)) { | ||
| 1327 | // Transfer clears operate on a whole 3D mip, not a storage image view. Refuse instead of | ||
| 1328 | // overwriting slices outside the UAV; a sliced clear needs a separate replay path. | ||
| 1329 | stub("ClearUnorderedAccessView of a partial 3D mip"); | ||
| 1330 | return false; | ||
| 1331 | } | ||
| 1124 | out.buffer = false; | 1332 | out.buffer = false; |
| 1125 | out.format = std::uint32_t(descriptor_format(image.format, image.swizzle)); | 1333 | out.format = std::uint32_t(descriptor_format(image.format, image.swizzle)); |
| 1126 | out.mip = image.first_mip; | 1334 | out.mip = image.first_mip; |
| 1127 | out.first_layer = volume ? 0 : image.first_layer; // a 3D view clears every slice of its mip | 1335 | out.first_layer = volume ? 0 : image.first_layer; // a 3D view clears every slice of its mip |
| 1128 | out.layer_count = volume ? 1 : image.layer_count; | 1336 | out.layer_count = volume ? 1 : image.layer_count; |
| 1337 | out.exact_clear_format = volume || image.type == g::ImageType::image_1d || | ||
| 1338 | image.type == g::ImageType::image_1d_array; | ||
| 1129 | return out.format != VK_FORMAT_UNDEFINED; | 1339 | return out.format != VK_FORMAT_UNDEFINED; |
| 1130 | } | 1340 | } |
| 1131 | g::BufferWords buffer_words{}; | 1341 | g::BufferWords buffer_words{}; |
| @@ -1175,6 +1385,7 @@ private: | |||
| 1175 | if (layout && layout == state.layout) return; | 1385 | if (layout && layout == state.layout) return; |
| 1176 | hold(signature); // state.layout points into it | 1386 | hold(signature); // state.layout points into it |
| 1177 | state.layout = layout; | 1387 | state.layout = layout; |
| 1388 | if (layout && (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED)) directly_indexed_ = true; | ||
| 1178 | state.data = {}; | 1389 | state.data = {}; |
| 1179 | // CreateRootSignature accepted it for the other bind point only (its log line says why). | 1390 | // CreateRootSignature accepted it for the other bind point only (its log line says why). |
| 1180 | if (layout && !(is_compute(state) ? layout->compute_ok : layout->graphics_ok)) { | 1391 | if (layout && !(is_compute(state) ? layout->compute_ok : layout->graphics_ok)) { |
| @@ -1240,11 +1451,26 @@ private: | |||
| 1240 | return; | 1451 | return; |
| 1241 | const RootArgumentLayout::Parameter* p = parameter(state, index, type); | 1452 | const RootArgumentLayout::Parameter* p = parameter(state, index, type); |
| 1242 | if (!p) return; | 1453 | if (!p) return; |
| 1454 | // A root view into the title's own memory (a slot-133 buffer, a constant ring) gets the console VA of a GPU | ||
| 1455 | // copy of it, so the shader's heap_base + VA reaches it (src/title_va_alias.h). A descriptor table is in one | ||
| 1456 | // of the layer's heaps: kept, and its buffer V#s are rewritten the same way before each submission. | ||
| 1457 | // A table in a console descriptor heap is read from the heap's arena copy (console_descriptor_table_va). | ||
| 1458 | if (type == rt::rootsig::ParamType::Descriptor) | ||
| 1459 | va = leonida::title_va_aliases().resolve(va, kRootViewBytes, leonida::TitleVaAliases::root_view); | ||
| 1460 | else | ||
| 1461 | va = console_descriptor_table_va(va); | ||
| 1462 | if (type != rt::rootsig::ParamType::Descriptor && va && p->table_descriptors) { | ||
| 1463 | if (!tables_.empty() && tables_.back().first == va) | ||
| 1464 | tables_.back().second = std::max(tables_.back().second, p->table_descriptors); | ||
| 1465 | else | ||
| 1466 | tables_.emplace_back(va, p->table_descriptors); | ||
| 1467 | } | ||
| 1243 | const std::uint32_t first = first_dword(state, *p); | 1468 | const std::uint32_t first = first_dword(state, *p); |
| 1244 | write(state, first, std::uint32_t(va)); | 1469 | 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 | 1470 | // 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. | 1471 | // 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)); | 1472 | const bool console = is_compute(state) || state.layout->graphics_console; |
| 1473 | if (!(console && type == rt::rootsig::ParamType::Table)) write(state, first + 1, std::uint32_t(va >> 32)); | ||
| 1248 | } | 1474 | } |
| 1249 | 1475 | ||
| 1250 | D3D12_COMMAND_LIST_TYPE type_; | 1476 | D3D12_COMMAND_LIST_TYPE type_; |
| @@ -1256,6 +1482,7 @@ private: | |||
| 1256 | RootState compute_; | 1482 | RootState compute_; |
| 1257 | RootState graphics_; | 1483 | RootState graphics_; |
| 1258 | std::uintptr_t compute_pipeline_ = 0; | 1484 | std::uintptr_t compute_pipeline_ = 0; |
| 1485 | bool unregistered_pipeline_ = false; // the bound PSO has no pipeline here (a stub): see set_pipeline | ||
| 1259 | std::uintptr_t graphics_pipeline_ = 0; | 1486 | std::uintptr_t graphics_pipeline_ = 0; |
| 1260 | ID3D12DescriptorHeap* cbv_srv_uav_heap_ = nullptr; | 1487 | ID3D12DescriptorHeap* cbv_srv_uav_heap_ = nullptr; |
| 1261 | ID3D12DescriptorHeap* sampler_heap_ = nullptr; | 1488 | ID3D12DescriptorHeap* sampler_heap_ = nullptr; |
| @@ -1264,8 +1491,19 @@ private: | |||
| 1264 | std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED | 1491 | std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED |
| 1265 | std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{}; | 1492 | std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{}; |
| 1266 | leonida::IndexBufferView index_{}; | 1493 | leonida::IndexBufferView index_{}; |
| 1494 | // SetGraphicsRootDescriptorTable / SetComputeRootDescriptorTable: VA, CBV/SRV/UAV slots the signature declares | ||
| 1495 | std::vector<std::pair<std::uint64_t, std::uint32_t>> tables_; | ||
| 1496 | bool directly_indexed_ = false; | ||
| 1497 | // The span a root view may read: D3D12's largest constant buffer (4096 x 16 bytes). | ||
| 1498 | static constexpr std::uint64_t kRootViewBytes = 65536; | ||
| 1267 | leonida::Viewport viewport_{}; | 1499 | leonida::Viewport viewport_{}; |
| 1268 | leonida::Rect scissor_{}; | 1500 | leonida::Rect scissor_{}; |
| 1501 | std::array<leonida::Viewport, D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE> viewports_{}; | ||
| 1502 | UINT viewport_count_ = 0; | ||
| 1503 | std::array<leonida::Rect, D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE> scissors_{}; | ||
| 1504 | UINT scissor_count_ = 0; | ||
| 1505 | std::shared_ptr<const leonida::DrawViewports> draw_viewports_; | ||
| 1506 | bool draw_viewports_stale_ = true; | ||
| 1269 | std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT> render_targets_{}; | 1507 | std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT> render_targets_{}; |
| 1270 | UINT render_target_count_ = 0; | 1508 | UINT render_target_count_ = 0; |
| 1271 | // One block shared by every draw that keeps the same targets and buffers, rebuilt when either changes, so a | 1509 | // One block shared by every draw that keeps the same targets and buffers, rebuilt when either changes, so a |
| @@ -1321,7 +1559,11 @@ private: | |||
| 1321 | hold(state); | 1559 | hold(state); |
| 1322 | compute_pipeline_ = compute_pipeline_id(state); | 1560 | compute_pipeline_ = compute_pipeline_id(state); |
| 1323 | graphics_pipeline_ = graphics_pipeline_id(state); | 1561 | graphics_pipeline_ = graphics_pipeline_id(state); |
| 1324 | if (state && !compute_pipeline_ && !graphics_pipeline_) | 1562 | // A pipeline this device never registered: a LEONIDA_STUB_PIPELINES stub (its translated shader is missing), |
| 1563 | // or one from another device. Its draws resolve to nothing in replay; its dispatches are no-ops too | ||
| 1564 | // (record_dispatch), so the list still closes. | ||
| 1565 | unregistered_pipeline_ = state && !compute_pipeline_ && !graphics_pipeline_; | ||
| 1566 | if (unregistered_pipeline_) | ||
| 1325 | stub("ID3D12GraphicsCommandList::SetPipelineState(pipeline from another device)"); | 1567 | stub("ID3D12GraphicsCommandList::SetPipelineState(pipeline from another device)"); |
| 1326 | } | 1568 | } |
| 1327 | 1569 | ||
| @@ -1345,13 +1587,13 @@ private: | |||
| 1345 | if (!cbv_srv_uav_heap_) | 1587 | if (!cbv_srv_uav_heap_) |
| 1346 | note_once("draw/dispatch uses CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED with no CBV/SRV/UAV heap bound"); | 1588 | note_once("draw/dispatch uses CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED with no CBV/SRV/UAV heap bound"); |
| 1347 | else | 1589 | else |
| 1348 | note_once("CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED heap base tracked on list (SGPR slot open, section 3.4)"); | 1590 | note_once("CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED heap base tracked on list"); |
| 1349 | } | 1591 | } |
| 1350 | if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) { | 1592 | if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) { |
| 1351 | if (!sampler_heap_) | 1593 | if (!sampler_heap_) |
| 1352 | note_once("draw/dispatch uses SAMPLER_HEAP_DIRECTLY_INDEXED with no sampler heap bound"); | 1594 | note_once("draw/dispatch uses SAMPLER_HEAP_DIRECTLY_INDEXED with no sampler heap bound"); |
| 1353 | else | 1595 | else |
| 1354 | note_once("SAMPLER_HEAP_DIRECTLY_INDEXED heap base tracked on list (SGPR slot open, section 3.4)"); | 1596 | note_once("SAMPLER_HEAP_DIRECTLY_INDEXED heap base tracked on list"); |
| 1355 | } | 1597 | } |
| 1356 | } | 1598 | } |
| 1357 | 1599 | ||
| @@ -1368,6 +1610,12 @@ private: | |||
| 1368 | index_ = {}; | 1610 | index_ = {}; |
| 1369 | viewport_ = {}; | 1611 | viewport_ = {}; |
| 1370 | scissor_ = {}; | 1612 | scissor_ = {}; |
| 1613 | viewports_.fill({}); | ||
| 1614 | viewport_count_ = 0; | ||
| 1615 | scissors_.fill({}); | ||
| 1616 | scissor_count_ = 0; | ||
| 1617 | draw_viewports_.reset(); | ||
| 1618 | draw_viewports_stale_ = true; | ||
| 1371 | render_targets_.fill({}); | 1619 | render_targets_.fill({}); |
| 1372 | render_target_count_ = 0; | 1620 | render_target_count_ = 0; |
| 1373 | draw_views_stale_ = true; | 1621 | draw_views_stale_ = true; |
| @@ -1378,21 +1626,114 @@ private: | |||
| 1378 | 1626 | ||
| 1379 | void set_vertex_buffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) { | 1627 | void set_vertex_buffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) { |
| 1380 | if (count) draw_views_stale_ = true; | 1628 | if (count) draw_views_stale_ = true; |
| 1381 | for (UINT i = 0; i < count; ++i) | 1629 | for (UINT i = 0; i < count; ++i) { |
| 1630 | if (views) leonida::title_va_aliases().note(views[i].BufferLocation, leonida::TitleVaAliases::vertex_view); | ||
| 1631 | 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, | 1632 | vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes, |
| 1383 | views[i].StrideInBytes} | 1633 | views[i].StrideInBytes} |
| 1384 | : leonida::VertexBufferView{}; | 1634 | : leonida::VertexBufferView{}; |
| 1635 | } | ||
| 1385 | } | 1636 | } |
| 1386 | 1637 | ||
| 1387 | void set_index_buffer(const D3D12_INDEX_BUFFER_VIEW* view) { | 1638 | void set_index_buffer(const D3D12_INDEX_BUFFER_VIEW* view) { |
| 1639 | if (view) leonida::title_va_aliases().note(view->BufferLocation, leonida::TitleVaAliases::index_view); | ||
| 1640 | if (view) leonida::title_va_aliases().want_buffer(view->BufferLocation, view->SizeInBytes); | ||
| 1388 | index_ = view ? leonida::IndexBufferView{view->BufferLocation, view->SizeInBytes, u(view->Format)} | 1641 | index_ = view ? leonida::IndexBufferView{view->BufferLocation, view->SizeInBytes, u(view->Format)} |
| 1389 | : leonida::IndexBufferView{}; | 1642 | : leonida::IndexBufferView{}; |
| 1390 | } | 1643 | } |
| 1391 | 1644 | ||
| 1645 | // The compiled console ABI appends directly indexed heaps after the explicit root arguments: the resource heap's | ||
| 1646 | // 32-bit shader VA, then the sampler heap's, each only when the signature has its flag. `slot` is the first | ||
| 1647 | // dword after the arguments (RootArgumentLayout::*_arguments_end). The heaps are snapshotted for each command, | ||
| 1648 | // so rebinding a heap cannot change a recorded draw or dispatch. | ||
| 1649 | std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> with_direct_heaps(const RootState& state, | ||
| 1650 | std::uint32_t slot) const { | ||
| 1651 | auto data = state.data; | ||
| 1652 | const auto flags = state.layout->flags; | ||
| 1653 | if ((flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) && slot < data.size()) | ||
| 1654 | data[slot++] = static_cast<std::uint32_t>(cbv_srv_uav_base_); | ||
| 1655 | if ((flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) && slot < data.size()) | ||
| 1656 | data[slot] = static_cast<std::uint32_t>(sampler_base_); | ||
| 1657 | return data; | ||
| 1658 | } | ||
| 1659 | |||
| 1660 | static bool direct_heaps_enabled(const char* variable) { | ||
| 1661 | const char* v = std::getenv(variable); | ||
| 1662 | return !(v && std::string_view(v) == "0"); | ||
| 1663 | } | ||
| 1664 | |||
| 1665 | std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> compute_user_data() const { | ||
| 1666 | static const bool enabled = direct_heaps_enabled("LEONIDA_COMPUTE_DIRECT_HEAPS"); | ||
| 1667 | if (!enabled) return compute_.data; | ||
| 1668 | return with_direct_heaps(compute_, compute_.layout->compute_arguments_end); | ||
| 1669 | } | ||
| 1670 | |||
| 1671 | // Graphics: vertex, pixel and geometry shaders share one user-data block and the s8 layout, so the heaps after | ||
| 1672 | // the explicit arguments serve every one of those stages (a pixel shader of a signature whose arguments end at | ||
| 1673 | // s11 reads the resource heap from s11). A mesh shader reads its arguments from s16, which is not modelled | ||
| 1674 | // (RootArgumentLayout, D3D-448): its pixel shader gets the heaps, the mesh stage does not. | ||
| 1675 | // LEONIDA_GRAPHICS_DIRECT_HEAPS=0 restores the previous arguments. | ||
| 1676 | std::array<std::uint32_t, rt::rootsig::kMaxUserSgprs> graphics_user_data() const { | ||
| 1677 | constexpr UINT direct = D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED | | ||
| 1678 | D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED; | ||
| 1679 | const RootArgumentLayout& layout = *graphics_.layout; | ||
| 1680 | if (!(layout.flags & direct)) return graphics_.data; | ||
| 1681 | static const bool enabled = direct_heaps_enabled("LEONIDA_GRAPHICS_DIRECT_HEAPS"); | ||
| 1682 | if (!enabled) { | ||
| 1683 | note_once("graphics direct heaps: off (LEONIDA_GRAPHICS_DIRECT_HEAPS=0), draws keep the explicit " | ||
| 1684 | "arguments only"); | ||
| 1685 | return graphics_.data; | ||
| 1686 | } | ||
| 1687 | const std::uint32_t end = layout.graphics_arguments_end; | ||
| 1688 | if (end >= rt::rootsig::kMaxUserSgprs) { | ||
| 1689 | note_once(layout.graphics_console | ||
| 1690 | ? "graphics direct heaps: not supplied, the explicit arguments fill s8..s31" | ||
| 1691 | : "graphics direct heaps: not supplied, the signature keeps the s0 graphics layout"); | ||
| 1692 | return graphics_.data; | ||
| 1693 | } | ||
| 1694 | static std::atomic<bool> logged{false}; | ||
| 1695 | if (!logged.exchange(true, std::memory_order_relaxed)) { | ||
| 1696 | const bool resource = layout.flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED; | ||
| 1697 | const bool sampler = layout.flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED; | ||
| 1698 | char resource_at[48] = "none", sampler_at[48] = "none"; | ||
| 1699 | if (resource) | ||
| 1700 | std::snprintf(resource_at, sizeof resource_at, "0x%llx at s%u", | ||
| 1701 | static_cast<unsigned long long>(std::uint32_t(cbv_srv_uav_base_)), end); | ||
| 1702 | if (sampler && end + (resource ? 1u : 0u) < rt::rootsig::kMaxUserSgprs) | ||
| 1703 | std::snprintf(sampler_at, sizeof sampler_at, "0x%llx at s%u", | ||
| 1704 | static_cast<unsigned long long>(std::uint32_t(sampler_base_)), | ||
| 1705 | end + (resource ? 1u : 0u)); | ||
| 1706 | std::fprintf(stderr, | ||
| 1707 | "[rt] d3d12: graphics direct heaps: resource heap %s, sampler heap %s, after %u dwords of " | ||
| 1708 | "explicit arguments from s8 (first draw; LEONIDA_GRAPHICS_DIRECT_HEAPS=0 restores the " | ||
| 1709 | "old arguments)\n", | ||
| 1710 | resource_at, sampler_at, end - 8); | ||
| 1711 | } | ||
| 1712 | return with_direct_heaps(graphics_, end); | ||
| 1713 | } | ||
| 1714 | |||
| 1392 | void record_dispatch(UINT x, UINT y, UINT z) { | 1715 | void record_dispatch(UINT x, UINT y, UINT z) { |
| 1393 | if (!compute_pipeline_ || !compute_.layout) return check(false); | 1716 | if (!compute_pipeline_ && unregistered_pipeline_) return; // a stub pipeline's dispatch: nothing to run |
| 1717 | if (!compute_pipeline_) return check(false); // separate lines: the rejection log names which | ||
| 1718 | if (!compute_.layout) return check(false); | ||
| 1719 | static const bool probe = std::getenv("LEONIDA_COMPUTE_INPUTS") != nullptr; | ||
| 1720 | if (probe) { | ||
| 1721 | static std::mutex mutex; | ||
| 1722 | static std::set<std::tuple<std::uintptr_t, const RootArgumentLayout*, UINT64>> seen; | ||
| 1723 | std::lock_guard lock(mutex); | ||
| 1724 | if (seen.emplace(compute_pipeline_, compute_.layout, cbv_srv_uav_base_).second) { | ||
| 1725 | std::fprintf(stderr, "[rt] compute-layout: pso=%p flags=%x bound-heap=0x%llx shader-heap=0x%llx", | ||
| 1726 | reinterpret_cast<void*>(compute_pipeline_), compute_.layout->flags, | ||
| 1727 | (unsigned long long)cbv_srv_uav_base_, | ||
| 1728 | (unsigned long long)console_descriptor_table_va(cbv_srv_uav_base_)); | ||
| 1729 | for (const auto& p : compute_.layout->parameters) | ||
| 1730 | std::fprintf(stderr, " (s%u type=%u const=%u slots=%u)", p.compute_offset, | ||
| 1731 | unsigned(p.type), p.constant_count, p.table_descriptors); | ||
| 1732 | std::fputc('\n', stderr); | ||
| 1733 | } | ||
| 1734 | } | ||
| 1394 | note_direct_heap_indexing(compute_.layout); | 1735 | note_direct_heap_indexing(compute_.layout); |
| 1395 | check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); | 1736 | check(list_.dispatch({compute_pipeline_, compute_user_data(), x, y, z})); |
| 1396 | } | 1737 | } |
| 1397 | 1738 | ||
| 1398 | // The views block a draw records. Rebuilt only when the render targets or the bound vertex buffers change. | 1739 | // The views block a draw records. Rebuilt only when the render targets or the bound vertex buffers change. |
| @@ -1408,16 +1749,117 @@ private: | |||
| 1408 | return draw_views_; | 1749 | return draw_views_; |
| 1409 | } | 1750 | } |
| 1410 | 1751 | ||
| 1752 | bool any_nonempty_viewport() const { | ||
| 1753 | for (UINT i = 0; i < viewport_count_; ++i) | ||
| 1754 | if (viewports_[i].width > 0.0f && viewports_[i].height > 0.0f) return true; | ||
| 1755 | return false; | ||
| 1756 | } | ||
| 1757 | |||
| 1758 | std::shared_ptr<const leonida::DrawViewports> draw_viewports() { | ||
| 1759 | if (viewport_count_ <= 1 && scissor_count_ <= 1) return nullptr; | ||
| 1760 | if (draw_viewports_stale_) { | ||
| 1761 | auto block = std::make_shared<leonida::DrawViewports>(); | ||
| 1762 | block->viewports = viewports_; | ||
| 1763 | block->viewport_count = viewport_count_; | ||
| 1764 | block->scissors = scissors_; | ||
| 1765 | block->scissor_count = scissor_count_; | ||
| 1766 | draw_viewports_ = std::move(block); | ||
| 1767 | draw_viewports_stale_ = false; | ||
| 1768 | } | ||
| 1769 | return draw_viewports_; | ||
| 1770 | } | ||
| 1771 | |||
| 1411 | // A draw needs a graphics pipeline, a graphics root signature and a topology. | 1772 | // A draw needs a graphics pipeline, a graphics root signature and a topology. |
| 1412 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { | 1773 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { |
| 1413 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); | 1774 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); |
| 1414 | note_direct_heap_indexing(graphics_.layout); | 1775 | note_direct_heap_indexing(graphics_.layout); |
| 1415 | // No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing, | 1776 | // No viewport (never set, or unbound with RSSetViewports(0)) or every bound viewport empty: D3D12 rasterizes |
| 1416 | // and Vulkan forbids a 0x0 viewport, so the draw is not recorded. | 1777 | // nothing, and Vulkan forbids a 0x0 viewport, so the draw is not recorded. |
| 1417 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; | 1778 | // LOCAL DIAGNOSTIC: the state each draw is recorded with |
| 1418 | leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, index_, topology_, viewport_, | 1779 | if (leonida::local_diagnostics()) { |
| 1780 | static std::atomic<std::uint64_t> draws{0}, skipped{0}; | ||
| 1781 | const bool skip = !any_nonempty_viewport(); | ||
| 1782 | const std::uint64_t n = ++draws, k = skip ? ++skipped : skipped.load(); | ||
| 1783 | if (n <= 8 || n % 1200 == 0) | ||
| 1784 | std::fprintf(stderr, "[rt] present-probe: draw #%llu (%s; %llu skipped so far) pso %p rts %u rt0 0x%llx " | ||
| 1785 | "mip %u viewport %.0fx%.0f at %.0f,%.0f scissor %d,%d-%d,%d count %u x%u\n", | ||
| 1786 | static_cast<unsigned long long>(n), skip ? "SKIPPED: no viewport" : "recorded", | ||
| 1787 | static_cast<unsigned long long>(k), reinterpret_cast<const void*>(graphics_pipeline_), | ||
| 1788 | render_target_count_, static_cast<unsigned long long>(render_targets_[0].resource), | ||
| 1789 | render_targets_[0].mip, viewport_.width, viewport_.height, viewport_.x, viewport_.y, | ||
| 1790 | scissor_.left, scissor_.top, scissor_.right, scissor_.bottom, count, instances); | ||
| 1791 | // LOCAL DIAGNOSTIC (fsdraw): the first draw of each pipeline, and one frame's draws every ~300 frames, | ||
| 1792 | // with the pipeline's description, the root data and what the bound descriptor tables hold. | ||
| 1793 | static std::mutex seen_mutex; | ||
| 1794 | static std::set<std::uintptr_t> seen; | ||
| 1795 | bool first_seen; | ||
| 1796 | { | ||
| 1797 | std::lock_guard lock(seen_mutex); | ||
| 1798 | first_seen = seen.insert(graphics_pipeline_).second; | ||
| 1799 | } | ||
| 1800 | const bool fullscreen = render_target_count_ >= 1 && count == 3 && viewport_.width >= 1000; | ||
| 1801 | if (fullscreen) leonida::fsdraw::note_target(render_targets_[0].resource); | ||
| 1802 | if (first_seen || (n - 1) % 600 < 2 || (n > 2 && n < 9)) { | ||
| 1803 | std::fprintf(stderr, "[rt] fsdraw: draw #%llu pso %p%s rt0 0x%llx rts %u depth 0x%llx count %u x%u: %s\n", | ||
| 1804 | static_cast<unsigned long long>(n), reinterpret_cast<const void*>(graphics_pipeline_), | ||
| 1805 | first_seen ? " (first use)" : "", static_cast<unsigned long long>(render_targets_[0].resource), | ||
| 1806 | render_target_count_, static_cast<unsigned long long>(depth_.resource), count, instances, | ||
| 1807 | leonida::fsdraw::pipeline(graphics_pipeline_).c_str()); | ||
| 1808 | std::fprintf(stderr, "[rt] fsdraw: draw #%llu layout %p user data [%s] cbv heap %p base 0x%llx sampler " | ||
| 1809 | "base 0x%llx tables %zu\n", | ||
| 1810 | static_cast<unsigned long long>(n), static_cast<const void*>(graphics_.layout), | ||
| 1811 | leonida::fsdraw::hex_words(graphics_.data.data(), graphics_.data.size()).c_str(), | ||
| 1812 | static_cast<const void*>(cbv_srv_uav_heap_), | ||
| 1813 | static_cast<unsigned long long>(cbv_srv_uav_base_), | ||
| 1814 | static_cast<unsigned long long>(sampler_base_), tables_.size()); | ||
| 1815 | for (std::size_t slot = 0; slot < 2; ++slot) { // the vertex buffers the blit's VS reads | ||
| 1816 | const auto& vb = vertex_buffers_[slot]; | ||
| 1817 | if (!vb.va) continue; | ||
| 1818 | std::uint32_t words[12]{}; | ||
| 1819 | bool read = false; | ||
| 1820 | MEMORY_BASIC_INFORMATION mbi{}; | ||
| 1821 | if (vb.va < leonida::fsdraw::arena_size()) { | ||
| 1822 | read = leonida::fsdraw::arena_words(vb.va, words, 12); | ||
| 1823 | } else if (VirtualQuery(reinterpret_cast<const void*>(static_cast<std::uintptr_t>(vb.va)), &mbi, sizeof mbi) && | ||
| 1824 | mbi.State == MEM_COMMIT && !(mbi.Protect & (PAGE_NOACCESS | PAGE_GUARD)) && mbi.Protect) { | ||
| 1825 | std::memcpy(words, reinterpret_cast<const void*>(static_cast<std::uintptr_t>(vb.va)), sizeof words); | ||
| 1826 | read = true; | ||
| 1827 | } | ||
| 1828 | std::fprintf(stderr, "[rt] fsdraw: draw #%llu vb%zu va 0x%llx size %u stride %u: %s [%s]\n", | ||
| 1829 | static_cast<unsigned long long>(n), slot, static_cast<unsigned long long>(vb.va), | ||
| 1830 | unsigned(vb.size), unsigned(vb.stride), read ? "read" : "unreadable", | ||
| 1831 | read ? leonida::fsdraw::hex_words(words, 12).c_str() : ""); | ||
| 1832 | } | ||
| 1833 | D3D12_DESCRIPTOR_HEAP_DESC heap_desc{}; | ||
| 1834 | std::uintptr_t heap_cpu = 0; | ||
| 1835 | if (cbv_srv_uav_heap_) { | ||
| 1836 | heap_desc = cbv_srv_uav_heap_->GetDesc(); | ||
| 1837 | heap_cpu = cbv_srv_uav_heap_->GetCPUDescriptorHandleForHeapStart().ptr; | ||
| 1838 | } | ||
| 1839 | char tag[48]; | ||
| 1840 | std::snprintf(tag, sizeof tag, "draw #%llu", static_cast<unsigned long long>(n)); | ||
| 1841 | for (const auto& [shader_va, slots] : tables_) { | ||
| 1842 | const std::uint64_t va = leonida::fsdraw::table_origin(shader_va); | ||
| 1843 | const std::uint64_t end = cbv_srv_uav_base_ + std::uint64_t(heap_desc.NumDescriptors) * 32; | ||
| 1844 | const bool in_heap = heap_cpu && va >= cbv_srv_uav_base_ && va + 32 <= end; | ||
| 1845 | const auto* cpu = in_heap ? reinterpret_cast<const std::uint8_t*>(heap_cpu + (va - cbv_srv_uav_base_)) | ||
| 1846 | : nullptr; | ||
| 1847 | const bool console = heap_cpu == std::uintptr_t(cbv_srv_uav_base_); | ||
| 1848 | if (shader_va != va) | ||
| 1849 | std::fprintf(stderr, "[rt] fsdraw: %s table title va 0x%llx is shader va 0x%llx\n", tag, | ||
| 1850 | static_cast<unsigned long long>(va), static_cast<unsigned long long>(shader_va)); | ||
| 1851 | leonida::fsdraw::describe_table(tag, shader_va, slots, cpu, | ||
| 1852 | !in_heap ? "none (not in the bound CBV/SRV/UAV heap)" | ||
| 1853 | : console ? "console (CPU address = GPU VA, title memory)" | ||
| 1854 | : "layer heap (arena)"); | ||
| 1855 | } | ||
| 1856 | } | ||
| 1857 | } | ||
| 1858 | if (!any_nonempty_viewport()) return; | ||
| 1859 | 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, | 1860 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, |
| 1420 | first, base_vertex, first_instance}; | 1861 | first, base_vertex, first_instance}; |
| 1862 | draw.viewports = draw_viewports(); | ||
| 1421 | check(list_.draw(std::move(draw))); | 1863 | check(list_.draw(std::move(draw))); |
| 1422 | } | 1864 | } |
| 1423 | }; | 1865 | }; |
| @@ -1469,12 +1911,14 @@ public: | |||
| 1469 | context_.enqueue_fence_callback(semaphore, queued, [context, semaphore, queued, value, held = held_] { | 1911 | context_.enqueue_fence_callback(semaphore, queued, [context, semaphore, queued, value, held = held_] { |
| 1470 | std::uint64_t now = 0; | 1912 | std::uint64_t now = 0; |
| 1471 | bool signaled = false; | 1913 | bool signaled = false; |
| 1472 | if (context->vk.vkGetSemaphoreCounterValue(context->device(), semaphore, &now) == VK_SUCCESS && | 1914 | if (context->observe_result(context->vk.vkGetSemaphoreCounterValue(context->device(), semaphore, &now), |
| 1915 | "vkGetSemaphoreCounterValue (fence callback)") == VK_SUCCESS && | ||
| 1473 | now >= queued && now < value) { | 1916 | now >= queued && now < value) { |
| 1474 | VkSemaphoreSignalInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO}; | 1917 | VkSemaphoreSignalInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO}; |
| 1475 | info.semaphore = semaphore; | 1918 | info.semaphore = semaphore; |
| 1476 | info.value = value; | 1919 | info.value = value; |
| 1477 | signaled = context->vk.vkSignalSemaphore(context->device(), &info) == VK_SUCCESS; | 1920 | signaled = context->observe_result(context->vk.vkSignalSemaphore(context->device(), &info), |
| 1921 | "vkSignalSemaphore (fence callback)") == VK_SUCCESS; | ||
| 1478 | } | 1922 | } |
| 1479 | held->fetch_sub(1); | 1923 | held->fetch_sub(1); |
| 1480 | if (signaled) { | 1924 | if (signaled) { |
| @@ -1644,6 +2088,83 @@ public: | |||
| 1644 | recorded.push_back(&list->commands()); | 2088 | recorded.push_back(&list->commands()); |
| 1645 | graphics.push_back(list); | 2089 | graphics.push_back(list); |
| 1646 | } | 2090 | } |
| 2091 | if (!context_.publish_cpu_textures()) { | ||
| 2092 | last_result_=leonida::ReplayResult::unresolved_resource; | ||
| 2093 | d3d12_trace::record(method,"failed"); | ||
| 2094 | return; | ||
| 2095 | } | ||
| 2096 | // Buffer V#s the title wrote into a bound table itself may name its own memory: rewritten to a GPU copy's | ||
| 2097 | // console VA before the copies are refreshed for this submission (src/title_va_alias.h). | ||
| 2098 | { | ||
| 2099 | std::map<std::uint64_t, std::uint32_t> tables; // each table once, with the most slots it was bound with | ||
| 2100 | for (const GraphicsCommandList* list : graphics) | ||
| 2101 | for (const auto& [table, descriptors] : list->tables()) | ||
| 2102 | tables[table] = std::max(tables[table], descriptors); | ||
| 2103 | // LOCAL DIAGNOSTIC (fsdraw): poke experiment, LEONIDA_FSDRAW_POKE_CB=1 | ||
| 2104 | if (leonida::fsdraw::poke_enabled()) { | ||
| 2105 | for (const auto& [table, descriptors] : tables) { | ||
| 2106 | const std::uint64_t origin = leonida::fsdraw::table_origin(table); | ||
| 2107 | if (origin != table) | ||
| 2108 | leonida::fsdraw::poke_cb( | ||
| 2109 | reinterpret_cast<const std::uint8_t*>(static_cast<std::uintptr_t>(origin))); | ||
| 2110 | } | ||
| 2111 | } | ||
| 2112 | // LOCAL DIAGNOSTIC (fsdraw): what the bound tables hold when the lists are submitted | ||
| 2113 | if (leonida::local_diagnostics()) { | ||
| 2114 | static std::atomic<std::uint64_t> submits{0}; | ||
| 2115 | const std::uint64_t n = tables.empty() ? 0 : ++submits; | ||
| 2116 | if (n && (n <= 6 || n % 300 == 0)) { | ||
| 2117 | char tag[48]; | ||
| 2118 | std::snprintf(tag, sizeof tag, "submit #%llu", static_cast<unsigned long long>(n)); | ||
| 2119 | for (const auto& [table, descriptors] : tables) { | ||
| 2120 | const std::uint64_t origin = leonida::fsdraw::table_origin(table); | ||
| 2121 | const bool title = origin != table; | ||
| 2122 | leonida::fsdraw::describe_table( | ||
| 2123 | tag, table, descriptors, | ||
| 2124 | title ? reinterpret_cast<const std::uint8_t*>(static_cast<std::uintptr_t>(origin)) : nullptr, | ||
| 2125 | title ? "title memory (CPU bytes read at the title VA the table was set with)" : "arena or unknown"); | ||
| 2126 | } | ||
| 2127 | } | ||
| 2128 | } | ||
| 2129 | if (!tables.empty()) { | ||
| 2130 | const auto start = std::chrono::steady_clock::now(); | ||
| 2131 | for (const auto& [table, descriptors] : tables) { | ||
| 2132 | // A table VA is an offset in this device's arena, so its own scanner reads it; the process-wide | ||
| 2133 | // hook belongs to the first device that registered and serves a context without a scanner. | ||
| 2134 | if (!context_.scan_table(table, descriptors)) | ||
| 2135 | leonida::title_va_aliases().scan_table(table, descriptors); | ||
| 2136 | if (!context_.prepare_sample_table(table, descriptors)) { | ||
| 2137 | last_result_ = leonida::ReplayResult::unresolved_resource; | ||
| 2138 | d3d12_trace::record(method, "failed"); | ||
| 2139 | return; | ||
| 2140 | } | ||
| 2141 | } | ||
| 2142 | leonida::title_va_aliases().note_scan(std::uint64_t( | ||
| 2143 | std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - start) | ||
| 2144 | .count())); | ||
| 2145 | } | ||
| 2146 | } | ||
| 2147 | for (const auto* list : graphics) { | ||
| 2148 | for (const auto& command : list->commands().commands()) { | ||
| 2149 | leonida::ResourceId resource = 0; | ||
| 2150 | if (const auto* b=std::get_if<leonida::TransitionBarrier>(&command)) { | ||
| 2151 | if (b->after & (D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE|D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE)) | ||
| 2152 | resource=b->resource; | ||
| 2153 | } else if (const auto* b=std::get_if<leonida::TextureBarrier>(&command)) { | ||
| 2154 | if (b->access_after & D3D12_BARRIER_ACCESS_SHADER_RESOURCE) resource=b->resource; | ||
| 2155 | } | ||
| 2156 | if (resource && !context_.prepare_sample_table(resource,0)) { | ||
| 2157 | last_result_=leonida::ReplayResult::unresolved_resource; | ||
| 2158 | d3d12_trace::record(method,"failed"); | ||
| 2159 | return; | ||
| 2160 | } | ||
| 2161 | } | ||
| 2162 | if (list->needs_all_sampled_views() && !context_.prepare_sample_table(0,0)) { | ||
| 2163 | last_result_ = leonida::ReplayResult::unresolved_resource; | ||
| 2164 | d3d12_trace::record(method,"failed"); | ||
| 2165 | return; | ||
| 2166 | } | ||
| 2167 | } | ||
| 1647 | last_result_ = queue_.execute(recorded, [&graphics] { return pin_lists(graphics); }); | 2168 | last_result_ = queue_.execute(recorded, [&graphics] { return pin_lists(graphics); }); |
| 1648 | d3d12_trace::record(method, last_result_ == leonida::ReplayResult::success ? "ok" : "failed"); | 2169 | d3d12_trace::record(method, last_result_ == leonida::ReplayResult::success ? "ok" : "failed"); |
| 1649 | if (last_result_ != leonida::ReplayResult::success) | 2170 | if (last_result_ != leonida::ReplayResult::success) |
| @@ -1658,7 +2179,7 @@ public: | |||
| 1658 | for (const auto& command : list.commands().commands()) { | 2179 | for (const auto& command : list.commands().commands()) { |
| 1659 | const auto range = leonida::query_range(command); | 2180 | const auto range = leonida::query_range(command); |
| 1660 | if (range.count && range.type == leonida::d3d12_query::timestamp && | 2181 | if (range.count && range.type == leonida::d3d12_query::timestamp && |
| 1661 | context_.query_heap_type(range.heap) == other) | 2182 | (!timestamp_family_supported() || context_.query_heap_type(range.heap) == other)) |
| 1662 | return false; | 2183 | return false; |
| 1663 | } | 2184 | } |
| 1664 | return true; | 2185 | return true; |
| @@ -1717,16 +2238,20 @@ public: | |||
| 1717 | } | 2238 | } |
| 1718 | 2239 | ||
| 1719 | // Ticks per second of the timestamps EndQuery writes: one tick is timestampPeriod nanoseconds. Every D3D12 | 2240 | // Ticks per second of the timestamps EndQuery writes: one tick is timestampPeriod nanoseconds. Every D3D12 |
| 1720 | // queue of the device shares one VkQueue, so they share one frequency. E_FAIL when that queue has no | 2241 | // device has one timestamp period, but individual queue families may not write timestamps. |
| 1721 | // timestamps (timestampValidBits 0), as D3D12 reports for a queue that does not support them. | ||
| 1722 | HRESULT STDMETHODCALLTYPE GetTimestampFrequency(UINT64* frequency) override { | 2242 | HRESULT STDMETHODCALLTYPE GetTimestampFrequency(UINT64* frequency) override { |
| 1723 | if (!frequency) return E_INVALIDARG; | 2243 | if (!frequency) return E_INVALIDARG; |
| 1724 | const double period = context_.timestamp_period(); | 2244 | const double period = context_.timestamp_period(); |
| 1725 | if (!(period > 0)) return E_FAIL; | 2245 | if (!(period > 0) || !timestamp_family_supported()) return E_FAIL; |
| 1726 | *frequency = static_cast<UINT64>(1e9 / period + 0.5); | 2246 | *frequency = static_cast<UINT64>(1e9 / period + 0.5); |
| 1727 | return S_OK; | 2247 | return S_OK; |
| 1728 | } | 2248 | } |
| 1729 | 2249 | ||
| 2250 | bool timestamp_family_supported() const { | ||
| 2251 | // The low-level queue factory also accepts caller-owned queues without a QueueClock. | ||
| 2252 | return !clock_.get_device_proc || clock_.timestamp_valid_bits != 0; | ||
| 2253 | } | ||
| 2254 | |||
| 1730 | // Queue labels, in order with the submissions around them. | 2255 | // Queue labels, in order with the submissions around them. |
| 1731 | void STDMETHODCALLTYPE SetMarker(UINT metadata, const void* data, UINT size) override { | 2256 | void STDMETHODCALLTYPE SetMarker(UINT metadata, const void* data, UINT size) override { |
| 1732 | d3d12_trace::record("ID3D12CommandQueue::SetMarker"); | 2257 | d3d12_trace::record("ID3D12CommandQueue::SetMarker"); |
| @@ -1792,16 +2317,20 @@ private: | |||
| 1792 | LARGE_INTEGER before{}, after{}; | 2317 | LARGE_INTEGER before{}, after{}; |
| 1793 | { | 2318 | { |
| 1794 | std::lock_guard submit_lock(context_.submit_mutex()); | 2319 | std::lock_guard submit_lock(context_.submit_mutex()); |
| 1795 | if (vk.vkQueueWaitIdle(vk_queue_) != VK_SUCCESS) return E_FAIL; | 2320 | if (context_.observe_result(vk.vkQueueWaitIdle(vk_queue_), "vkQueueWaitIdle (calibration)") != VK_SUCCESS) |
| 2321 | return E_FAIL; | ||
| 1796 | QueryPerformanceCounter(&before); | 2322 | QueryPerformanceCounter(&before); |
| 1797 | if (vk.vkQueueSubmit(vk_queue_, 1, &submit, VK_NULL_HANDLE) != VK_SUCCESS || | 2323 | if (context_.observe_result(vk.vkQueueSubmit(vk_queue_, 1, &submit, VK_NULL_HANDLE), |
| 1798 | vk.vkQueueWaitIdle(vk_queue_) != VK_SUCCESS) | 2324 | "vkQueueSubmit (calibration)") != VK_SUCCESS || |
| 2325 | context_.observe_result(vk.vkQueueWaitIdle(vk_queue_), "vkQueueWaitIdle (calibration)") != VK_SUCCESS) | ||
| 1799 | return E_FAIL; | 2326 | return E_FAIL; |
| 1800 | QueryPerformanceCounter(&after); | 2327 | QueryPerformanceCounter(&after); |
| 1801 | } | 2328 | } |
| 1802 | std::uint64_t stamp = 0; | 2329 | std::uint64_t stamp = 0; |
| 1803 | if (stamp_.get_query_pool_results(context_.device(), stamp_.queries, 0, 1, sizeof(stamp), &stamp, | 2330 | 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) | 2331 | sizeof(stamp), &stamp, sizeof(stamp), |
| 2332 | VK_QUERY_RESULT_64_BIT), | ||
| 2333 | "vkGetQueryPoolResults (calibration)") != VK_SUCCESS) | ||
| 1805 | return E_FAIL; | 2334 | return E_FAIL; |
| 1806 | if (clock_.timestamp_valid_bits < 64) stamp &= (std::uint64_t(1) << clock_.timestamp_valid_bits) - 1; | 2335 | if (clock_.timestamp_valid_bits < 64) stamp &= (std::uint64_t(1) << clock_.timestamp_valid_bits) - 1; |
| 1807 | gpu = stamp; | 2336 | gpu = stamp; |
| @@ -1934,6 +2463,11 @@ private: | |||
| 1934 | 2463 | ||
| 1935 | } // namespace | 2464 | } // namespace |
| 1936 | 2465 | ||
| 2466 | bool record_gpu_label(ID3D12GraphicsCommandList* list,const leonida::label::Write& write) { | ||
| 2467 | auto* own=GraphicsCommandList::from(list); | ||
| 2468 | return own && own->xbox_label(write); | ||
| 2469 | } | ||
| 2470 | |||
| 1937 | HRESULT create_command_queue(leonida::VulkanContext& context, VkQueue queue, std::uint32_t queue_family, | 2471 | HRESULT create_command_queue(leonida::VulkanContext& context, VkQueue queue, std::uint32_t queue_family, |
| 1938 | const D3D12_COMMAND_QUEUE_DESC& desc, REFIID riid, void** out, IUnknown* device, | 2472 | const D3D12_COMMAND_QUEUE_DESC& desc, REFIID riid, void** out, IUnknown* device, |
| 1939 | const QueueClock& clock, std::shared_ptr<void> queue_lease) { | 2473 | const QueueClock& clock, std::shared_ptr<void> queue_lease) { |
| @@ -2021,7 +2555,8 @@ HRESULT set_event_on_multiple_fence_completion(leonida::VulkanContext& context, | |||
| 2021 | wait.semaphoreCount = count; | 2555 | wait.semaphoreCount = count; |
| 2022 | wait.pSemaphores = semaphores.data(); | 2556 | wait.pSemaphores = semaphores.data(); |
| 2023 | wait.pValues = values; | 2557 | wait.pValues = values; |
| 2024 | const VkResult result = context.vk.vkWaitSemaphores(context.device(), &wait, UINT64_MAX); | 2558 | const VkResult result = context.observe_result( |
| 2559 | context.vk.vkWaitSemaphores(context.device(), &wait, UINT64_MAX), "vkWaitSemaphores (multiple fences)"); | ||
| 2025 | // Unlike a single fence's blocking SetEventOnCompletion, this path invokes Vulkan directly. | 2560 | // 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. | 2561 | // A lost device must wake its other registered fence waiters and reject later submissions. |
| 2027 | if (result == VK_ERROR_DEVICE_LOST) { | 2562 | if (result == VK_ERROR_DEVICE_LOST) { |