Skip to content

Runtime: replace full LayoutTracker clone in VulkanQueue::execute with O(touched) rollback journal (D3D-244) - #1872

Open
emirhan-karaca wants to merge 2 commits into
ItsNotRexed:mainfrom
emirhan-karaca:perf/queue-execute-tracker-rollback
Open

emirhan-karaca wants to merge 2 commits into
ItsNotRexed:mainfrom
emirhan-karaca:perf/queue-execute-tracker-rollback

Conversation

@emirhan-karaca

Copy link
Copy Markdown
Contributor

Claimed in #1745 as D3D-244 (claim). Follow-up to #1744 (D3D-225, which removed the full LayoutTracker clone from VulkanPresenter::submit()); independent of #901 (per-command barrier/copy vectors in prepare/emit).

What and why

VulkanQueue::execute() (runtime/src/vulkan_backend.h) previously cloned the entire device-wide LayoutTracker (auto next_tracker = context_.tracker_;) on every ExecuteCommandLists call while holding the global tracker_mutex_, so failed submissions could discard uncommitted layout transitions.

Because LayoutTracker stores an std::unordered_map<ResourceId, Entry> with a std::vector<VkImageLayout> per tracked texture, cloning it copies every live texture in the device even when a batch touches only a handful of subresources — thousands of heap allocations per submission under tracker_mutex_.

Changes

  • runtime/src/vulkan_replay.h (LayoutTracker::Transaction):
    • Added a scoped RAII LayoutTracker::Transaction guard that mutates LayoutTracker in place and appends (ResourceId, subresource_index, previous_layout) to a reusable undo_log_ only when set() actually changes a subresource's layout.
    • commit() runs in O(1) without touching or cloning unmodified resources (reusing undo_log_ capacity up to 4,096 entries and releasing oversized spikes).
    • rollback() (and ~Transaction() if uncommitted) walks undo_log_ in reverse order with a per-resource lookup cache, restoring multi-step transitions on the same subresource, partial mip/layer ranges, and separate depth/stencil planes to their exact initial states.
    • Supports nested transactions cleanly; explicit copy/move constructors leave copies/moves with an empty, inactive undo log so callers copying LayoutTracker (e.g., VulkanPresenter prior to Runtime: O(1) LayoutTracker rollback and unlock tracker_mutex before vkQueuePresentKHR in VulkanPresenter #1744) stay unaffected.
  • runtime/src/vulkan_backend.h (VulkanQueue::execute()):
    • Replaced auto next_tracker = context_.tracker_; + context_.tracker_ = std::move(next_tracker); with LayoutTracker::Transaction tracker_tx(context_.tracker_); under tracker_lock, calling tracker_tx.commit() on successful submit and tracker_tx.rollback() on failed vkEndCommandBuffer or vkQueueSubmit.
    • Lock hierarchy (resource_mutex_ -> tracker_mutex_ -> submit_mutex_) and lifetime ordering (tracker_tx destroyed before tracker_lock) are unchanged.
  • runtime/test/layout_tracker_rollback_test.cpp + runtime/CMakeLists.txt (layout_tracker_rollback):
    • Pure-CPU test (no GPU required) covering commit, explicit rollback, RAII rollback, multi-step same-subresource rollback, partial mip/array ranges, separate vs merged depth/stencil planes, nested transactions, forgotten resources, copy independence, oversized-spike capacity release, and a global operator new check proving 0 heap allocations across 400 steady-state commit/rollback transactions with 2,048 live tracked textures.
  • runtime/test/vk_queue_tracker_rollback_test.cpp + runtime/CMakeLists.txt (vk_queue_tracker_rollback, SKIP_RETURN_CODE 77):
    • Real-GPU regression test covering:
      1. Multi-list ExecuteCommandLists failing at vkQueueSubmit: rolls back both layers of a GPU-poisoned (0x5A) UNDEFINED committed texture while preserving 1,024 untouched textures; retry commits, clears layer 0, and runs deferred zero-fill on layer 1 (0x00 readback).
      2. Failed vkEndCommandBuffer: rolls back multi-step layout transitions without invoking vkQueueSubmit.
      3. Separate depth/stencil planes: depth-only transitions roll back cleanly on failed submit while leaving the stencil plane untouched.
      4. O(1) allocation scaling: compares steady-state host allocations across 10 VulkanQueue::execute() calls with 16 vs 2,048 live tracked textures (allocs_2048 <= allocs_16 + 64).

Verification (synthetic tests only, no game data)

Built on Windows 11 with MinGW-w64 g++ 16.2.0 (-std=c++20 -O2 -Wall -Wextra, Vulkan-Headers 1.4.363.0, 0 warnings in touched/new code):

  • CPU tests:
    • layout_tracker_rollback_test: PASS (0 heap allocations across 400 steady-state commit/rollback rounds with 2,048 tracked textures)
    • d3d12_state_map_test, replay_clear_gate_test, replay_copy_offset_test, replay_readback_stages_test, replay_cpu_bench --self-test: all PASS
  • GPU tests (3/3 runs each, 0 validation errors):
    • Intel(R) UHD Graphics (0x8086:0xa7a8, driver 101.7092, Vulkan 1.4.323):
      • vk_queue_tracker_rollback_test: 3/3 PASS — 10 executes: 16 textures = 41 allocs (2.3–2.4 us/exec), 2,048 textures = 40 allocs (2.0 us/exec)
      • vk_queue_query_sync_test, vk_command_replay_test, depth24_convert_test, vk_presenter_sync_test: all PASS
    • NVIDIA GeForce RTX 5050 Laptop GPU (driver 621.24):
      • vk_queue_tracker_rollback_test: 3/3 PASS — 10 executes: 16 textures = 41–43 allocs (13.6–16.4 us/exec), 2,048 textures = 40–41 allocs (10.6–18.0 us/exec)
      • vk_queue_query_sync_test, vk_command_replay_test, depth24_convert_test, vk_presenter_sync_test: all PASS
  • Negative controls (before-vs-after measurement on origin/main 003e7812):
    1. Reverting VulkanQueue::execute() to auto next_tracker = context_.tracker_; fails vk_queue_tracker_rollback_test at line 333: allocs_2048 <= allocs_16 + 64 (exit=1) on both GPUs:
      • Intel UHD: 16 textures = 431 allocs (3.8 us/exec) → 2,048 textures = 41,070 allocs (147.9 us/exec) (~1,027× more allocations, ~74× slower per execute than this PR)
      • RTX 5050: 16 textures = 433 allocs (18.3 us/exec) → 2,048 textures = 41,070 allocs (168.5 us/exec) (~1,000× more allocations, ~10–13× slower per execute than this PR)
    2. Skipping tracker_tx.rollback() on failed submit fails vk_queue_tracker_rollback_test at line 193: context.layout(1, 0) == VK_IMAGE_LAYOUT_UNDEFINED (exit=1).

AI-assisted.

@Thatters

Copy link
Copy Markdown
Contributor

Tested 32c48197 on an RTX 5070 Ti (621.24), Windows 11 Pro 25H2, MSVC 14.51 + Ninja, Vulkan SDK 1.4.363.0. The runtime change looks right, but the new vk_queue_tracker_rollback_test fails with the validation layer on, 3/3 runs, because of its own setup:

validation: vkQueueSubmit(): pSubmits[0] command buffer ... expects VkImage ... (aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, mipLevel = 0, arrayLayer = 0)
to be in layout VK_IMAGE_LAYOUT_GENERAL--instead, current layout is VK_IMAGE_LAYOUT_UNDEFINED.  (VUID-vkCmdDraw-None-09600)
line 339: d.validation_errors() == 0

Without LEONIDA_VK_VALIDATION=1 it passes, and the three rollback scenarios and the scaling check print ok either way. So ctest with validation on reports it as failed.

Cause: side_img is created fresh, so it really is UNDEFINED. vk_queue_tracker_rollback_test.cpp:169 then registers it as context.add_resource(2, side_rb, VK_IMAGE_LAYOUT_GENERAL) without moving the image there. The tracker trusts that, so the later barriers on resource 2 (scenario 4's ping-pong) start from GENERAL while the image is still UNDEFINED.

Checked: I added a one-shot d.run barrier from UNDEFINED to GENERAL on side_img just before line 169, built the same way as poison_image. With that, the test passes 3/3 with validation and 0 VUIDs, and the scaling check still holds (54 vs 50 allocs over 10 executes). Registering resource 2 as UNDEFINED would also work, but scenario 2's layout(2, 0) == GENERAL expectation would need to change with it. I only made the edit locally; nothing pushed.

Full ctest -j 4 with validation: 107/109. The two failures are this test and d3d12_draw (main's bundle-blend fence issue, fixed by #1807).

AI-assisted.

@emirhan-karaca

Copy link
Copy Markdown
Contributor Author

Thanks @Thatters for catching that under LEONIDA_VK_VALIDATION=1! Fixed in 6907e705: added a one-shot UNDEFINED -> GENERAL barrier on side_img (transition_image_to_general()) before context.add_resource(2, side_rb, VK_IMAGE_LAYOUT_GENERAL) so the real Vulkan image layout matches what the tracker is told.

Re-verified on Windows 11 (MinGW-w64 g++ 16.2.0 -std=c++20 -O2 -Wall -Wextra, Vulkan-Headers 1.4.363.0; VK_LAYER_KHRONOS_validation not installed locally):

  • Intel(R) UHD Graphics (101.7092, Vulkan 1.4.323): vk_queue_tracker_rollback_test 3/3 PASS (16 textures = 41 allocs, 2,048 textures = 40 allocs / 2.0 us/exec).
  • NVIDIA GeForce RTX 5050 Laptop GPU (621.24): vk_queue_tracker_rollback_test 3/3 PASS (16 textures = 41–42 allocs, 2,048 textures = 40 allocs / 10.2–12.9 us/exec).

AI-assisted.

@Thatters

Copy link
Copy Markdown
Contributor

Confirmed with the validation layer on: 6907e705 fixes it. Same RTX 5070 Ti / 621.24, Windows 11 Pro 25H2, MSVC 14.51 + Ninja, Vulkan SDK 1.4.363.0 with VK_LAYER_KHRONOS_validation.

AI-assisted.

@Gabzinnn12

Copy link
Copy Markdown

Tested 6907e70 on AMD Radeon RX 7600 (RDNA3), AMD 26.11.2 (LLPC), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363.0: rollback retest passed.

  • vk_queue_tracker_rollback_test: ok with LEONIDA_VK_VALIDATION=1 (+ sync validation) and without, no Vulkan validation errors reported. At 32c4819 it failed here under validation with the side_img layout message @Thatters reported, so the fix holds on AMD too.
  • d3d12_com_test, d3d12_queue_wait_test, vk_command_replay_test: exit 0 with and without validation, no Vulkan validation errors reported.
  • layout_tracker_rollback_test (CPU only): ok.
  • ctest not run at this head.

AI-assisted (Claude Code, reviewed with Codex). No game files.

@sanakontorov-sys

Copy link
Copy Markdown

Tested 6907e70 on NVIDIA GeForce RTX 5060 (Blackwell, driver 617.42, Vulkan 1.4.351), Windows 11 Pro build 26300, MSVC (VS 2026), LLVM_MC set, scripts\build_runtime.ps1, then ctest -j 4: pass for this change. layout_tracker_rollback and vk_queue_tracker_rollback pass; 108 of 109 run tests pass. The one failure is d3d12_draw ("fence signal 4 is not above 30"), which fails the same way on main 003e781 here and is fixed by #1807. GitHub now shows a conflict with main, so this result is for the current head before a rebase.

(Run with an AI coding agent.)

@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 20006d6 on Windows 11 (MSVC BuildTools 2022, Ninja, RelWithDebInfo, -j 4; no Vulkan validation layer installed), one GPU at a time, device line confirmed: AMD Radeon RDNA2 iGPU (driver 26.10.1) and NVIDIA RTX 5080 (driver 616.92). Ran ctest -R "^(layout_tracker_rollback|vk_queue_tracker_rollback)$" (the tests this PR adds/changes): pass on both GPUs, 2/2 each. No game files.

AI-assisted.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 20006d6 on Intel UHD 770 (driver 32.0.101.7088), Windows 11, MSVC 19.44, Vulkan SDK 1.4.357: pass. layout_tracker_rollback_test and vk_queue_tracker_rollback_test both report ok with LEONIDA_VK_VALIDATION=1, including the failed-submit, failed-vkEndCommandBuffer and separate depth/stencil plane rollbacks, and the scaling check (16 textures = 10 allocs, 2048 textures = 10 allocs over 10 executes). AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants