Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions runtime/src/vulkan_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned> device_lost_reports{0};
inline bool set_device_lost(std::atomic<bool>& 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.
Expand Down Expand Up @@ -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<Entry> drained = std::move(pending_);
pending_.clear();
dispatch_outside_lock(lock, drained, signal_event_);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down
18 changes: 16 additions & 2 deletions runtime/test/vk_fence_dispatcher_sync_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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());
Expand All @@ -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.
Expand All @@ -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);
Expand All @@ -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());
});

Expand All @@ -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);
}

Expand Down
Loading