From 5c1f77e1d4bafb3cba8c3e2037de28cb376f633e Mon Sep 17 00:00:00 2001 From: CLHelper <359823989+CLHelper@users.noreply.github.com> Date: Tue, 24 Nov 2026 16:09:38 +0000 Subject: [PATCH] vulkan: say once when the device is lost (RT-DEVICE-LOST-LOG) VulkanContext::mark_device_lost() and the fence dispatcher's two VK_ERROR_DEVICE_LOST paths only set the device's lost flag. From then on every fence reads UINT64_MAX and device calls return DXGI_ERROR_DEVICE_REMOVED, so a title takes its own device-removed path (on the retail boot an int3 at game_scarlett_master.exe+0x2474606, reached from its frame wait reading a fence's completed value as ~0), and nothing in the log said the device had gone first. set_device_lost() sets the flag with an exchange and, the first time for a device, prints one stderr line saying where the loss was seen. mark_device_lost() takes that place as an optional argument; the dispatcher passes its own two. device_lost_reports counts the lines for tests. Tests: vk_fence_dispatcher_sync_test cases 3 and 4 check one line per device, from mark_device_lost() and from the dispatcher's vkGetSemaphoreCounterValue path, and none from a later call on the same device. AI-assisted. --- runtime/src/vulkan_backend.h | 25 ++++++++++++++++--- .../test/vk_fence_dispatcher_sync_test.cpp | 18 +++++++++++-- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index f90dd6a3ff..dcf89476cd 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -549,6 +549,22 @@ class DeferredDeletionQueue { void* bindless_release_user_ = nullptr; }; +// Sets a device's lost flag and says so once, on stderr, at the moment it is first seen. From then on every fence of +// the device reads UINT64_MAX and device calls return DXGI_ERROR_DEVICE_REMOVED, so a title takes its own +// device-removed path (on the retail boot an int3 at game_scarlett_master.exe+0x2474606, reached from its frame wait +// reading a fence's completed value as ~0); without this line that later stop is the first sign of the loss. +// True when this call set the flag. device_lost_reports counts the lines said in this process (for tests). +inline std::atomic device_lost_reports{0}; +inline bool set_device_lost(std::atomic& flag, const char* where) noexcept { + if (flag.exchange(true, std::memory_order_acq_rel)) return false; + device_lost_reports.fetch_add(1, std::memory_order_relaxed); + std::fprintf(stderr, + "[rt] vulkan: the device was lost (%s); its fences now read UINT64_MAX and device calls return " + "DXGI_ERROR_DEVICE_REMOVED\n", + where); + return true; +} + // Centralized single-thread fence dispatcher using Vulkan 1.2 vkWaitSemaphores(VK_SEMAPHORE_WAIT_ANY_BIT). // Eliminates per-fence OS threads and 10ms polling: a synthetic timeline semaphore (wakeup_sem_) at index 0 // wakes vkWaitSemaphores immediately whenever a new wait, a released fence, or shutdown arrives while waiting_ == true. @@ -821,7 +837,7 @@ class FenceWaitDispatcher { epoch_cv_.notify_all(); if (res == VK_ERROR_DEVICE_LOST || device_lost_.load(std::memory_order_acquire)) { - device_lost_.store(true, std::memory_order_release); + set_device_lost(device_lost_, "vkWaitSemaphores in the fence dispatcher returned VK_ERROR_DEVICE_LOST"); std::vector drained = std::move(pending_); pending_.clear(); dispatch_outside_lock(lock, drained, signal_event_); @@ -851,7 +867,8 @@ class FenceWaitDispatcher { pending_.resize(write); for (Entry& e : pending_) ready.push_back(std::move(e)); pending_.clear(); - device_lost_.store(true, std::memory_order_release); + set_device_lost(device_lost_, + "vkGetSemaphoreCounterValue in the fence dispatcher returned VK_ERROR_DEVICE_LOST"); dispatch_outside_lock(lock, ready, signal_event_); return; } @@ -1538,8 +1555,8 @@ class VulkanContext { // Vulkan requires external synchronisation of a VkQueue; every D3D12 queue of the device may share one. std::mutex& submit_mutex() { return submit_mutex_; } - void mark_device_lost() noexcept { - device_lost_.store(true, std::memory_order_release); + void mark_device_lost(const char* where = "a Vulkan call returned VK_ERROR_DEVICE_LOST") noexcept { + set_device_lost(device_lost_, where); if (fence_dispatcher_) { try { fence_dispatcher_->notify_device_lost(); diff --git a/runtime/test/vk_fence_dispatcher_sync_test.cpp b/runtime/test/vk_fence_dispatcher_sync_test.cpp index 56b1dd27ce..ebc184076d 100644 --- a/runtime/test/vk_fence_dispatcher_sync_test.cpp +++ b/runtime/test/vk_fence_dispatcher_sync_test.cpp @@ -6,6 +6,7 @@ // 3. mark_device_lost() wakes a blocked vkWaitSemaphores() so pending waits complete and cancel()/stop() never hang. // 4. vkGetSemaphoreCounterValue() returning VK_ERROR_DEVICE_LOST drains pending waits and marks device_lost_ instead // of spinning forever. +// 3 and 4 also check that a loss is said once per device (set_device_lost), whichever path sees it first. // 5. cancel() blocks until a callback already dispatched for that semaphore has stopped using it. // 6. A re-entrant cancel() for a later callback in the same ready batch skips that callback. // Skips with exit code 77 when no Vulkan 1.3 device is available. No game data. @@ -198,8 +199,13 @@ int main() { }); if (!wait_until_in_vk_wait()) std::_Exit(1); + const unsigned reports_before = leonida::device_lost_reports.load(); lost_ctx.mark_device_lost(); + lost_ctx.mark_device_lost("a second report of the same loss"); + // Said once per device: the second call and the dispatcher waking on the same flag add nothing. + if (leonida::device_lost_reports.load() != reports_before + 1) std::_Exit(1); if (!wait_flag(mu, cv, callback_fired)) std::_Exit(1); + if (leonida::device_lost_reports.load() != reports_before + 1) std::_Exit(1); // cancel() and ~VulkanContext() -> stop() must return promptly even after device_lost_ is set. lost_ctx.cancel_fence_waits(stuck.semaphore()); @@ -215,7 +221,8 @@ int main() { EXPECT(ok_lost); joiner.join(); EXPECT(callback_fired); - std::printf("fence dispatcher: mark_device_lost() wakes blocked wait and teardown completes ok\n"); + std::printf("fence dispatcher: mark_device_lost() wakes blocked wait, is reported once, and teardown completes " + "ok\n"); } // 4. vkGetSemaphoreCounterValue() returning VK_ERROR_DEVICE_LOST drains pending waits instead of spinning forever. @@ -230,6 +237,8 @@ int main() { bool done = false; bool marked_lost = false; std::uint32_t observed_calls = 0; + unsigned reports_by_dispatcher = 0, reports_after_mark = 0; + const unsigned reports_before = leonida::device_lost_reports.load(); std::thread err_test([&] { VulkanContext err_ctx(driver::get_device, d.device, true); @@ -250,6 +259,9 @@ int main() { marked_lost = err_ctx.is_device_lost(); observed_calls = driver::counter_calls.load(std::memory_order_acquire); + reports_by_dispatcher = leonida::device_lost_reports.load() - reports_before; + err_ctx.mark_device_lost(); // the device is already lost: nothing more is said + reports_after_mark = leonida::device_lost_reports.load() - reports_before; err_ctx.cancel_fence_waits(sem.semaphore()); }); @@ -266,7 +278,9 @@ int main() { EXPECT(drained); EXPECT(marked_lost); EXPECT(observed_calls >= 1 && observed_calls <= 4); - std::printf("fence dispatcher: vkGetSemaphoreCounterValue DEVICE_LOST drains without spin (%u calls) ok\n", + EXPECT(reports_by_dispatcher == 1 && reports_after_mark == 1); + std::printf("fence dispatcher: vkGetSemaphoreCounterValue DEVICE_LOST drains without spin (%u calls) and is " + "reported once ok\n", observed_calls); }