diff options
| author | pmilewsskiedu-gif <266377087+pmilewsskiedu-gif@users.noreply.github.com> | 2026-11-22 15:53:23 -0500 |
|---|---|---|
| committer | pmilewsskiedu-gif <266377087+pmilewsskiedu-gif@users.noreply.github.com> | 2026-11-22 15:53:23 -0500 |
| commit | 3ff15669c2f221d1e310b888b0574f73bcaecbdf (patch) | |
| tree | 022f4f24f49edae10e859a7e24a8c34c76eaaa51 /runtime | |
| parent | f847748cc76bd3777f9c070c4034eb2fc1c04e64 (diff) | |
fix: serialize presenter swapchain destruction with Vulkan queue submissions
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/src/vulkan_present.h | 16 | ||||
| -rw-r--r-- | runtime/test/vk_presenter_sync_test.cpp | 86 |
2 files changed, 99 insertions, 3 deletions
diff --git a/runtime/src/vulkan_present.h b/runtime/src/vulkan_present.h index a2874b4b3..53cc34df3 100644 --- a/runtime/src/vulkan_present.h +++ b/runtime/src/vulkan_present.h | |||
| @@ -160,8 +160,10 @@ public: | |||
| 160 | { | 160 | { |
| 161 | std::lock_guard lock(context_.submit_mutex()); | 161 | std::lock_guard lock(context_.submit_mutex()); |
| 162 | vk.vkQueueWaitIdle(queue_); | 162 | vk.vkQueueWaitIdle(queue_); |
| 163 | // A WSI layer may perform a device-wide idle while destroying the swapchain. | ||
| 164 | // Keep that implicit idle serialized with submits from every context queue. | ||
| 165 | release(swapchain_); | ||
| 163 | } | 166 | } |
| 164 | release(swapchain_); | ||
| 165 | swapchain_ = VK_NULL_HANDLE; | 167 | swapchain_ = VK_NULL_HANDLE; |
| 166 | release_capture(); | 168 | release_capture(); |
| 167 | composer_.reset(); | 169 | composer_.reset(); |
| @@ -863,7 +865,10 @@ private: | |||
| 863 | } | 865 | } |
| 864 | } | 866 | } |
| 865 | if (extent.width == 0 || extent.height == 0) { | 867 | if (extent.width == 0 || extent.height == 0) { |
| 866 | release(swapchain_); | 868 | { |
| 869 | std::lock_guard lock(context_.submit_mutex()); | ||
| 870 | release(swapchain_); | ||
| 871 | } | ||
| 867 | swapchain_ = VK_NULL_HANDLE; | 872 | swapchain_ = VK_NULL_HANDLE; |
| 868 | vsync_ = vsync; | 873 | vsync_ = vsync; |
| 869 | swapchain_srgb_ = srgb; | 874 | swapchain_srgb_ = srgb; |
| @@ -975,13 +980,18 @@ private: | |||
| 975 | info.oldSwapchain = old; | 980 | info.oldSwapchain = old; |
| 976 | VkSwapchainKHR fresh = VK_NULL_HANDLE; | 981 | VkSwapchainKHR fresh = VK_NULL_HANDLE; |
| 977 | const VkResult created = vkCreateSwapchainKHR_(device, &info, nullptr, &fresh); | 982 | const VkResult created = vkCreateSwapchainKHR_(device, &info, nullptr, &fresh); |
| 978 | release(old); // retired by the creation attempt, whether or not it succeeded | 983 | { |
| 984 | std::lock_guard lock(context_.submit_mutex()); | ||
| 985 | release(old); // retired by the creation attempt, whether or not it succeeded | ||
| 986 | } | ||
| 979 | if (created != VK_SUCCESS) return false; | 987 | if (created != VK_SUCCESS) return false; |
| 980 | 988 | ||
| 981 | std::vector<VkImage> images; | 989 | std::vector<VkImage> images; |
| 982 | std::vector<VkSemaphore> ready; | 990 | std::vector<VkSemaphore> ready; |
| 983 | const auto discard = [&] { | 991 | const auto discard = [&] { |
| 984 | for (VkSemaphore semaphore : ready) vk.vkDestroySemaphore(device, semaphore, nullptr); | 992 | for (VkSemaphore semaphore : ready) vk.vkDestroySemaphore(device, semaphore, nullptr); |
| 993 | // The WSI implementation may block on the device here too. | ||
| 994 | std::lock_guard lock(context_.submit_mutex()); | ||
| 985 | vkDestroySwapchainKHR_(device, fresh, nullptr); | 995 | vkDestroySwapchainKHR_(device, fresh, nullptr); |
| 986 | return false; | 996 | return false; |
| 987 | }; | 997 | }; |
diff --git a/runtime/test/vk_presenter_sync_test.cpp b/runtime/test/vk_presenter_sync_test.cpp index b4585ce20..844e01654 100644 --- a/runtime/test/vk_presenter_sync_test.cpp +++ b/runtime/test/vk_presenter_sync_test.cpp | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | // A failed capture fence wait also leaves the caller's outputs untouched. | 6 | // A failed capture fence wait also leaves the caller's outputs untouched. |
| 7 | // 3. Concurrent present() + VulkanQueue::execute() + add_resource()/remove_resource() follows VulkanContext's | 7 | // 3. Concurrent present() + VulkanQueue::execute() + add_resource()/remove_resource() follows VulkanContext's |
| 8 | // resource_mutex_ -> tracker_mutex_ -> submit_mutex_ lock hierarchy without data races or deadlocks. | 8 | // resource_mutex_ -> tracker_mutex_ -> submit_mutex_ lock hierarchy without data races or deadlocks. |
| 9 | // 4. A swapchain-destroy WSI layer which calls vkDeviceWaitIdle cannot overlap vkQueueSubmit on another thread. | ||
| 9 | // Skips with exit code 77 when no Vulkan 1.3 device with Win32 presentation is available. No game data. | 10 | // Skips with exit code 77 when no Vulkan 1.3 device with Win32 presentation is available. No game data. |
| 10 | #ifndef WIN32_LEAN_AND_MEAN | 11 | #ifndef WIN32_LEAN_AND_MEAN |
| 11 | #define WIN32_LEAN_AND_MEAN | 12 | #define WIN32_LEAN_AND_MEAN |
| @@ -58,6 +59,8 @@ PFN_vkQueuePresentKHR real_present = nullptr; | |||
| 58 | PFN_vkWaitForFences real_wait = nullptr; | 59 | PFN_vkWaitForFences real_wait = nullptr; |
| 59 | PFN_vkCmdPipelineBarrier real_barrier = nullptr; | 60 | PFN_vkCmdPipelineBarrier real_barrier = nullptr; |
| 60 | PFN_vkCmdCopyImageToBuffer real_copy_i2b = nullptr; | 61 | PFN_vkCmdCopyImageToBuffer real_copy_i2b = nullptr; |
| 62 | PFN_vkDestroySwapchainKHR real_destroy_swapchain = nullptr; | ||
| 63 | PFN_vkDeviceWaitIdle real_device_wait_idle = nullptr; | ||
| 61 | 64 | ||
| 62 | std::atomic<VkResult> fail_next_submit{VK_SUCCESS}; | 65 | std::atomic<VkResult> fail_next_submit{VK_SUCCESS}; |
| 63 | std::atomic<VkResult> fail_next_wait{VK_SUCCESS}; | 66 | std::atomic<VkResult> fail_next_wait{VK_SUCCESS}; |
| @@ -66,9 +69,36 @@ std::atomic<bool> observe_capture{false}; | |||
| 66 | std::atomic<bool> saw_capture_waw{false}; | 69 | std::atomic<bool> saw_capture_waw{false}; |
| 67 | std::atomic<std::uint32_t> capture_copies{0}; | 70 | std::atomic<std::uint32_t> capture_copies{0}; |
| 68 | std::atomic<std::uint32_t> capture_waw_barriers{0}; | 71 | std::atomic<std::uint32_t> capture_waw_barriers{0}; |
| 72 | std::atomic<bool> check_swapchain_destroy{false}; | ||
| 73 | std::atomic<bool> destroy_entered{false}; | ||
| 74 | std::atomic<bool> submit_entered{false}; | ||
| 75 | std::atomic<bool> overlap_detected{false}; | ||
| 76 | std::atomic<bool> unexpected_submit_error{false}; | ||
| 77 | std::mutex destroy_mutex; | ||
| 78 | std::condition_variable destroy_cv; | ||
| 79 | bool allow_destroy_finish = false; | ||
| 80 | |||
| 81 | // Synthetic WSI layer: a destroy hook initiates vkDeviceWaitIdle, as a capture layer may do internally. | ||
| 82 | // Holding the hook behind a condition variable makes the concurrency window deterministic rather than dependent | ||
| 83 | // on the installed capture software or validation logging overhead. Only the test hook waits on this condition. | ||
| 84 | VKAPI_ATTR void VKAPI_CALL destroy_swapchain(VkDevice device, VkSwapchainKHR swapchain, | ||
| 85 | const VkAllocationCallbacks* alloc) { | ||
| 86 | if (check_swapchain_destroy.load(std::memory_order_relaxed)) { | ||
| 87 | destroy_entered.store(true, std::memory_order_release); | ||
| 88 | destroy_cv.notify_all(); | ||
| 89 | { | ||
| 90 | std::unique_lock lock(destroy_mutex); | ||
| 91 | destroy_cv.wait(lock, [] { return allow_destroy_finish; }); | ||
| 92 | } | ||
| 93 | overlap_detected.store(submit_entered.load(std::memory_order_acquire), std::memory_order_release); | ||
| 94 | if (real_device_wait_idle(device) != VK_SUCCESS) unexpected_submit_error.store(true); | ||
| 95 | } | ||
| 96 | real_destroy_swapchain(device, swapchain, alloc); | ||
| 97 | } | ||
| 69 | 98 | ||
| 70 | VKAPI_ATTR VkResult VKAPI_CALL queue_submit(VkQueue queue, std::uint32_t count, const VkSubmitInfo* submits, | 99 | VKAPI_ATTR VkResult VKAPI_CALL queue_submit(VkQueue queue, std::uint32_t count, const VkSubmitInfo* submits, |
| 71 | VkFence fence) { | 100 | VkFence fence) { |
| 101 | submit_entered.store(true, std::memory_order_release); | ||
| 72 | const VkResult armed = fail_next_submit.exchange(VK_SUCCESS); | 102 | const VkResult armed = fail_next_submit.exchange(VK_SUCCESS); |
| 73 | if (armed != VK_SUCCESS) return armed; | 103 | if (armed != VK_SUCCESS) return armed; |
| 74 | return real_submit(queue, count, submits, fence); | 104 | return real_submit(queue, count, submits, fence); |
| @@ -124,6 +154,7 @@ VKAPI_ATTR void VKAPI_CALL cmd_copy_image_to_buffer(VkCommandBuffer cb, VkImage | |||
| 124 | 154 | ||
| 125 | VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_device(VkDevice device, const char* name) { | 155 | VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_device(VkDevice device, const char* name) { |
| 126 | const std::string_view n = name; | 156 | const std::string_view n = name; |
| 157 | if (n == "vkDestroySwapchainKHR") return reinterpret_cast<PFN_vkVoidFunction>(destroy_swapchain); | ||
| 127 | if (n == "vkQueueSubmit") return reinterpret_cast<PFN_vkVoidFunction>(queue_submit); | 158 | if (n == "vkQueueSubmit") return reinterpret_cast<PFN_vkVoidFunction>(queue_submit); |
| 128 | if (n == "vkQueuePresentKHR") return reinterpret_cast<PFN_vkVoidFunction>(queue_present); | 159 | if (n == "vkQueuePresentKHR") return reinterpret_cast<PFN_vkVoidFunction>(queue_present); |
| 129 | if (n == "vkWaitForFences") return reinterpret_cast<PFN_vkVoidFunction>(wait_for_fences); | 160 | if (n == "vkWaitForFences") return reinterpret_cast<PFN_vkVoidFunction>(wait_for_fences); |
| @@ -140,6 +171,8 @@ VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_instance(VkInstance instance, const | |||
| 140 | void install(PFN_vkGetInstanceProcAddr instance_proc, VkInstance instance, VkDevice device) { | 171 | void install(PFN_vkGetInstanceProcAddr instance_proc, VkInstance instance, VkDevice device) { |
| 141 | real_get_instance = instance_proc; | 172 | real_get_instance = instance_proc; |
| 142 | real_get_device = reinterpret_cast<PFN_vkGetDeviceProcAddr>(instance_proc(instance, "vkGetDeviceProcAddr")); | 173 | real_get_device = reinterpret_cast<PFN_vkGetDeviceProcAddr>(instance_proc(instance, "vkGetDeviceProcAddr")); |
| 174 | real_destroy_swapchain = reinterpret_cast<PFN_vkDestroySwapchainKHR>(real_get_device(device, "vkDestroySwapchainKHR")); | ||
| 175 | real_device_wait_idle = reinterpret_cast<PFN_vkDeviceWaitIdle>(real_get_device(device, "vkDeviceWaitIdle")); | ||
| 143 | real_submit = reinterpret_cast<PFN_vkQueueSubmit>(real_get_device(device, "vkQueueSubmit")); | 176 | real_submit = reinterpret_cast<PFN_vkQueueSubmit>(real_get_device(device, "vkQueueSubmit")); |
| 144 | real_present = reinterpret_cast<PFN_vkQueuePresentKHR>(real_get_device(device, "vkQueuePresentKHR")); | 177 | real_present = reinterpret_cast<PFN_vkQueuePresentKHR>(real_get_device(device, "vkQueuePresentKHR")); |
| 145 | real_wait = reinterpret_cast<PFN_vkWaitForFences>(real_get_device(device, "vkWaitForFences")); | 178 | real_wait = reinterpret_cast<PFN_vkWaitForFences>(real_get_device(device, "vkWaitForFences")); |
| @@ -350,6 +383,59 @@ int main() { | |||
| 350 | EXPECT(!worker_failed.load()); | 383 | EXPECT(!worker_failed.load()); |
| 351 | EXPECT(queue.idle() && worker_queue.idle()); | 384 | EXPECT(queue.idle() && worker_queue.idle()); |
| 352 | std::printf("presenter sync: concurrent present + queue.execute + tracker mutations ok\n"); | 385 | std::printf("presenter sync: concurrent present + queue.execute + tracker mutations ok\n"); |
| 386 | |||
| 387 | // 4. Emulate a WSI capture layer that performs device-idle inside vkDestroySwapchainKHR. | ||
| 388 | // The presenter first enters the destroy hook; a second thread then attempts a real queue submission. | ||
| 389 | // Once the hook is released, that submission must not have entered the Vulkan driver while the hook ran. | ||
| 390 | driver::destroy_entered.store(false); | ||
| 391 | driver::submit_entered.store(false); | ||
| 392 | driver::overlap_detected.store(false); | ||
| 393 | driver::unexpected_submit_error.store(false); | ||
| 394 | { | ||
| 395 | std::lock_guard lock(driver::destroy_mutex); | ||
| 396 | driver::allow_destroy_finish = false; | ||
| 397 | } | ||
| 398 | driver::check_swapchain_destroy.store(true); | ||
| 399 | VkResult recreated = VK_ERROR_UNKNOWN; | ||
| 400 | std::thread destroy_thread([&] { recreated = presenter.present(1, true); }); | ||
| 401 | bool entered = false; | ||
| 402 | { | ||
| 403 | std::unique_lock lock(driver::destroy_mutex); | ||
| 404 | entered = driver::destroy_cv.wait_for(lock, std::chrono::seconds(5), | ||
| 405 | [] { return driver::destroy_entered.load(); }); | ||
| 406 | } | ||
| 407 | std::atomic<bool> submit_started{false}; | ||
| 408 | ReplayResult submit_result = ReplayResult::unsupported; | ||
| 409 | std::thread submit_thread; | ||
| 410 | if (entered) { | ||
| 411 | submit_thread = std::thread([&] { | ||
| 412 | submit_started.store(true); | ||
| 413 | CommandList list; | ||
| 414 | if (!list.clear_render_target({1, 0, 0, 1}, {0.3f, 0.2f, 0.1f, 1.0f}) || !list.close()) return; | ||
| 415 | const CommandList* ptr = &list; | ||
| 416 | submit_result = worker_queue.execute({&ptr, 1}); | ||
| 417 | }); | ||
| 418 | for (int attempt = 0; attempt < 100 && !submit_started.load(); ++attempt) | ||
| 419 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); | ||
| 420 | // Give the competing thread time to reach the submit point if no mutex is protecting destruction. | ||
| 421 | std::this_thread::sleep_for(std::chrono::milliseconds(50)); | ||
| 422 | } | ||
| 423 | { | ||
| 424 | std::lock_guard lock(driver::destroy_mutex); | ||
| 425 | driver::allow_destroy_finish = true; | ||
| 426 | } | ||
| 427 | driver::destroy_cv.notify_all(); | ||
| 428 | destroy_thread.join(); | ||
| 429 | if (submit_thread.joinable()) submit_thread.join(); | ||
| 430 | driver::check_swapchain_destroy.store(false); | ||
| 431 | EXPECT(entered); | ||
| 432 | EXPECT(recreated == VK_SUCCESS); | ||
| 433 | EXPECT(submit_started.load()); | ||
| 434 | EXPECT(submit_result == ReplayResult::success); | ||
| 435 | EXPECT(!driver::overlap_detected.load()); | ||
| 436 | EXPECT(!driver::unexpected_submit_error.load()); | ||
| 437 | EXPECT(queue.idle() && worker_queue.idle()); | ||
| 438 | std::printf("presenter sync: synthetic WSI device-idle during swapchain destruction serialized ok\n"); | ||
| 353 | } | 439 | } |
| 354 | 440 | ||
| 355 | DestroyWindow(window); | 441 | DestroyWindow(window); |