Repository navigation
Runtime: serialize VulkanContext::remove_resource with tracker_mutex and use lower_bound for view retirement - #1920
Conversation
|
Tested Built with
Not covered: I did not measure lock contention or frame-time impact of holding No game data involved. AI-assisted (Claude Code): it built the tree and ran everything above on this machine. |
Serrambi
left a comment
There was a problem hiding this comment.
Reviewed 48773c78 and tested on NVIDIA GeForce RTX 5070 Ti (driver 610.88, Windows 11 Pro, MSVC 19.50.35719.0, Vulkan SDK 1.4.363.0): approve.
Code review (runtime/src/vulkan_backend.h, runtime/test/vk_remove_resource_sync_test.cpp, runtime/CMakeLists.txt)
VulkanContext::remove_resource()maintains theresource_mutex_ -> tracker_mutex_ -> submit_mutex_hierarchy (defer_destroy()acquires onlysubmit_mutex_afterviews_mutex_is released) and matchesadd_resource()'sresource_mutex_ -> tracker_mutex_lock order, closing both theLayoutTracker::forget()ABA gap and prematuredefer_destroy()ticket stamping acrossVulkanQueue::execute()submissions.views_.lower_bound()starts cleanly from(img, 0, 0, 0, 0, VK_FORMAT_UNDEFINED)(0) and erases only that image's cached views.vk_remove_resource_sync_test.cppcovers thevkQueueSubmitserialization window (probingquery_heap_type()shared lock behavior), concurrentadd_resource/remove_resource/execute()stress, and targeted 2-of-32lower_bound()view retirement cleanly under/W4.
Checks on 48773c78
vk_remove_resource_sync_test.exe(default): passed (EXIT=0)vk_remove_resource_sync_test.exe(LEONIDA_VK_VALIDATION=1): passed (EXIT=0, 0 Vulkan validation errors)ctest --test-dir runtime/build --output-on-failure -R "^vk_remove_resource_sync$": passed1/1
|
Tested
|
|
Tested 48773c7 on Windows 11 (26200), MSVC 19.4x (VS 2022 Build Tools), Ninja RelWithDebInfo: pass on both GPUs of a two-GPU PC.
No game files. AI-assisted check (Claude Code), Vonsek3. |
|
Tested 48773c7 natively on Windows with NVIDIA and AMD: pass, no regressions. Setup: Windows 11 Pro 10.0.26300, MSVC 19.44.35230 (VS 2022 Build Tools), Vulkan SDK 1.4.363.0,
Code read:
AI-assisted (Claude Code). |
|
Tested 48773c7 on an NVIDIA GeForce RTX 3050 Laptop GPU, driver 32.0.16.2101, Windows 11, native MSVC (VS 2022 Build Tools, Ninja, RelWithDebInfo): pass.
AI-assisted (Claude Code). |
|
Tested 48773c7, merged onto
Code read:
AI-assisted (Claude Code). |
48773c7 to
68c55e5
Compare
|
Tested 68c55e5 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 AI-assisted. |
|
Tested 68c55e5 on Intel UHD 770 (driver 32.0.101.7088), Windows 11, MSVC 19.44, Vulkan SDK 1.4.357: pass. |
…and use lower_bound for view retirement
|
Rebased onto Re-verified on Windows 11 (
AI-assisted. |
68c55e5 to
13bebf1
Compare
|
Tested on NVIDIA GeForce RTX 5080, driver 621.24, Windows 11 (10.0.26200), Vulkan device API 1.4.357: pass Checked out PR head
|
|
Tested 13bebf1 on NVIDIA RTX 4070 SUPER (driver 616.92), Windows 11 26200, MSVC 14.44, Vulkan SDK 1.4.363, RelWithDebInfo: pass, no regression against main 561b65a.
|
Claimed in #1745 (claim).
What and why
VulkanContext::remove_resource()(runtime/src/vulkan_backend.h:820-842) previously releasedresource_mutex_after erasingresources_[id], then lockedviews_mutex_(scanning the entireviews_map and callingdefer_destroy(), which acquiressubmit_mutex_), and only lockedtracker_mutex_to calltracker_.forget(id)at the very end. That left three bugs:LayoutTracker::forget()race: Afterremove_resource(id)unlockedresource_mutex_and before it acquiredtracker_mutex_, a concurrentadd_resource(id, ...)on a reused COM pointer (ResourceId) could insert the new resource into bothresources_andtracker_, only for the earlierremove_resource(id)to calltracker_.forget(id)and silently drop layout tracking for the live resource.VkImageViewretirement window +views_mutex_held acrosssubmit_mutex_: BecauseVulkanQueue::execute()releasesresource_mutex_beforesubmit()while keepingtracker_mutex_held,remove_resource(id)did not wait for in-flight submissions usingid's cachedVkImageViews before callingdefer_destroy(), and heldviews_mutex_while blocking onsubmit_mutex_(stalling other queues'prepare()calls incontext_.view()).views_is already astd::mapkeyed withVkImageas tuple element 0, so scanning fromviews_.begin()toviews_.end()touched every cached view in the device.Changes
runtime/src/vulkan_backend.h(VulkanContext::remove_resource()):resource_mutex_(unique) andtracker_mutex_together (matchingadd_resource(),remove_query_pool(), and theresource_mutex_ -> tracker_mutex_ -> submit_mutex_hierarchy) acrossresources_.erase(it),tracker_.forget(id), view collection, anddefer_destroy().views_.lower_bound(...)underviews_mutex_to collect and erase onlyimg's views in O(log V + k), then releasesviews_mutex_before callingdefer_destroy()soviews_mutex_is never held acrosssubmit_mutex_.runtime/test/vk_remove_resource_sync_test.cpp+runtime/CMakeLists.txt(vk_remove_resource_sync,SKIP_RETURN_CODE 77):vkQueueSubmit(whenexecute()has unlockedresource_mutex_and holdstracker_mutex_) to triggerremove_resource(1)on a second thread and verifyresource_mutex_is NOT prematurely released beforetracker_.forget(1)and view retirement complete.add_resource()/remove_resource()+VulkanQueue::execute()stress loop.0newvkCreateImageViewcalls).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-Headers1.4.363.0, 0 warnings in touched/new code):0x8086:0xa7a8, driver101.7092, Vulkan1.4.323):vk_remove_resource_sync_test3/3 PASS (0 validation errors).621.24):vk_remove_resource_sync_test3/3 PASS (0 validation errors).origin/main6a6704dfremove_resource()): deterministically failsvk_remove_resource_sync_testatline 215: !probe_passed_during_submit(exit=1) on both Intel UHD and RTX 5050.AI-assisted.