Skip to content

Runtime: O(1) LayoutTracker rollback and unlock tracker_mutex before vkQueuePresentKHR in VulkanPresenter - #1744

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
emirhan-karaca:perf/presenter-o1-tracker-rollback
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
emirhan-karaca:perf/presenter-o1-tracker-rollback

Conversation

@emirhan-karaca

Copy link
Copy Markdown
Contributor

Claimed on #1548 (comment) (follow-up to merged #1619 addressing @dracbat's review note).

Changes (runtime/src/vulkan_present.h)

  1. O(1) single-subresource rollback instead of copying LayoutTracker on every Present():
    • VulkanPresenter::record() only touches subresource 0 ({VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}) of source via detail::initialize().
    • Instead of copying the entire context_.tracker_ (std::unordered_map<ResourceId, Entry> + per-image layout vectors) under tracker_mutex_ on every frame, submit() now records context_.tracker_.layout(source, 0) (when tracked), records directly into context_.tracker_, and restores subresource 0 in O(1) if vkResetFences or vkQueueSubmit fails.
    • Removes #include <optional> and the extra LayoutTracker& parameter on record().
  2. Release tracker_mutex_ immediately after vkQueueSubmit (before vkQueuePresentKHR):
    • Commit/rollback and tracker_lock.unlock() now happen right after vkQueueSubmit returns inside the submit_mutex() block, preserving strict submission order without blocking other threads' emit() / layout() calls while vkQueuePresentKHR runs.

Tests (runtime/test/vk_presenter_sync_test.cpp)

  • Populates 512 live tracked resources (1000..1511) during the failed-submit rollback check and verifies source rolls back to UNDEFINED while other tracked layouts stay intact.
  • Hooks vkQueuePresentKHR and spawns a reader thread calling context.layout(1, 0) inside vkQueuePresentKHR, verifying tracker_mutex_ is already released before presentation and sees the committed VK_IMAGE_LAYOUT_GENERAL.

Verification

  • Negative control (unmodified main 89b047cd): Failed on Intel UHD at line 237: unblocked_during_present (tracker_mutex_ remained locked during vkQueuePresentKHR).
  • After fix (MinGW-w64 GCC 16.2.0 -std=c++20 -O2 -Wall -Wextra -Wpedantic -Wno-missing-field-initializers, 0 warnings in touched files):
    • Intel(R) UHD Graphics (0x8086:0xa7a8, driver 101.7092, Vulkan 1.4.323): vk_presenter_sync_test 3/3 passed.
    • NVIDIA GeForce RTX 5050 Laptop GPU (0x10de:0x2d98, driver 621.24, Vulkan 1.4.357): vk_presenter_sync_test 3/3 passed.

AI-assisted: written and verified with an AI coding assistant; all code, negative control, and multi-GPU runs reviewed and executed locally. No game data.

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revu le head db9b090 : la sauvegarde/restauration ciblée couvre l’unique transition tracker du subresource 0, et le verrou est relâché après vkQueueSubmit tout en préservant l’ordre de soumission. Cela retire la copie de la map complète à chaque frame et évite de tenir tracker_mutex_ pendant vkQueuePresentKHR. Le test couvre l’échec submit, l’état d’une autre ressource et la lecture concurrente pendant present. Petite réserve non bloquante : la vérification concurrente dépend d’une attente fixe de 30 ms; une condition_variable avec un délai borné limiterait les faux échecs sur runner chargé. Checks CI encore en attente.

@Vonsek Vonsek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tracker commit/rollback ordering looks consistent with the submit lock at db9b0904. There are two failure modes in the new test's reader:

  • A 30 ms sleep can expire before the reader is scheduled on a loaded runner, giving a false regression. Have the worker signal that it has started, then wait for completion with a bounded timeout inside the present hook.
  • If the present hook starts the reader and the real vkQueuePresentKHR returns an error, EXPECT(presenter.present(...) == VK_SUCCESS) returns before the join. The thread destructor then terminates the test. Capture the present result, join the reader, then check the result.

Reviewed by source. AI-assisted.

@emirhan-karaca

Copy link
Copy Markdown
Contributor Author

Thanks @Flyander and @Vonsek for catching both points! Updated in 6933e0c9:

  • Replaced the fixed 30 ms sleep_for in vk_presenter_sync_test.cpp with a std::condition_variable handshake: the present hook waits for the reader thread to start (bounded 2 s), then waits for context.layout(1, 0) to complete (bounded 500 ms).
  • Captured present_rc = presenter.present(1, true) and joined reader unconditionally before EXPECT(present_rc == VK_SUCCESS), so a failing vkQueuePresentKHR cannot trigger std::terminate on an unjoined std::thread.

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

  • Intel(R) UHD Graphics (101.7092, Vulkan 1.4.323): 3/3 PASS
  • NVIDIA GeForce RTX 5050 Laptop GPU (621.24): 3/3 PASS
  • Negative control (keeping tracker_lock held across vkQueuePresentKHR): fails cleanly at line 255: unblocked_during_present (exit=1, clean join, no hang).

AI-assisted.

@itsonlysimbo

Copy link
Copy Markdown
Contributor

Tested 6933e0c9 on RTX 4070, NVIDIA 621.14, Windows 11 (10.0.26300), MSVC 19.44, Vulkan SDK 1.4.363: pass.

AI-assisted (Claude Code).

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 6933e0c on Windows 11 (26200), MSVC 19.4x (VS 2022 Build Tools), Ninja RelWithDebInfo: pass on both GPUs of a two-GPU PC.

  • NVIDIA GeForce RTX 5080, driver 616.92 (LEONIDA_VK_DEVICE=nvidia): ctest 94/94 passed, including vk_presenter_sync.
  • AMD Radeon RDNA2 iGPU, driver 32.0.21045.16010 (LEONIDA_VK_DEVICE=radeon, LEONIDA_HOST_ADAPTER=radeon so the host comparisons run on the same GPU): ctest 94/94 passed.

No game files. AI-assisted check (Claude Code), Vonsek3.

@ItsNotRexed
ItsNotRexed merged commit 5e53273 into ItsNotRexed:main Nov 22, 2026
4 checks passed
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.

5 participants