Skip to content

Runtime: propagate Vulkan fence counter device loss to D3D12 - #4445

Open
pmilewsskiedu-gif wants to merge 4 commits into
ItsNotRexed:mainfrom
pmilewsskiedu-gif:fix/d3d12-fence-device-loss-counter-20261122
Open

pmilewsskiedu-gif wants to merge 4 commits into
ItsNotRexed:mainfrom
pmilewsskiedu-gif:fix/d3d12-fence-device-loss-counter-20261122

Conversation

@pmilewsskiedu-gif

Copy link
Copy Markdown
Contributor

Claimed in coordination: #4421 (comment)

What and why

On unchanged upstream main 03b09a086592a197db64d91655c6e65c30764d96, VulkanFence::completed() ignored the VkResult of vkGetSemaphoreCounterValue. On VK_ERROR_DEVICE_LOST, ID3D12Fence::GetCompletedValue() reported stale 0 instead of UINT64_MAX, device removed reason remained S_OK, and an outstanding SetEventOnCompletion event was not released. This fix checks the query result, marks the shared Vulkan context lost and notifies its fence dispatcher. Non-device-loss errors cannot manufacture completed fence progress. No overlap with #3969 fence semaphore lifetime or transient signal retry.

Tested: identical original-main RED / patched GREEN

A new original synthetic Windows COM test intercepts the Vulkan proc resolver and injects one synthetic VK_ERROR_DEVICE_LOST only into vkGetSemaphoreCounterValue on the caller thread; it never forwards that error to the GPU driver. Native D3D12 WARP is the positive-control implementation for normal fence completion/event behavior.

  • Original production main 03b09a08, test-only scaffold: WARP signal/event normal, RTX 4060 Ti Vulkan validation enabled; injected loss yields GetCompletedValue=0, GetDeviceRemovedReason=S_OK, pending event not released, 1 injection and 0 validation errors: exit 1 (RED).
  • Patched 51f9555131f477444b807e4f2ff2b227506f9d69, same test: GetCompletedValue=UINT64_MAX, removed reason 0x887A0005, pending event signaled, 1 injected error, 0 validation errors: exit 0 (GREEN).
  • MSVC 19.44, Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0. Focused CTests vk_queue_query_sync, d3d12_com, d3d12_queue_wait, d3d12_fence_timing, d3d12_fence_device_lost_counter: 5/5 PASS, 5.53 seconds. New test 12/12 repeats PASS, 2.45 seconds. git diff --check clean.

Limits and checklist

  • No game data, keys, decryption, proprietary assets/shaders or other restricted content.
  • Only original source and synthetic test.
  • AI-assisted implementation, tests and review disclosed.

Injected device loss verifies error propagation, not actual physical driver loss. No full suite or other Vulkan GPU vendors tested. The missing authentic game executable precludes any GTA VI boot/performance claim. No self-merge.

@molodayamama

Copy link
Copy Markdown

Independent native check of 51f9555131f477444b807e4f2ff2b227506f9d69 on Windows 10 22H2 (19045.7058), RTX 4060 (32.0.16.1088), MSVC 19.40/Ninja RelWithDebInfo. The unmodified d3d12_queue_wait, timeout control, and d3d12_fence_device_lost_counter each passed six repeats. WARP completed the normal control; the injected loss returned UINT64_MAX, gave 0x887A0005, and released the pending event, with zero Vulkan validation errors.

I added a local test-only control before the existing device-loss injection: return VK_ERROR_OUT_OF_HOST_MEMORY with a synthetic returned counter value of 999 for a fence waiting on 7. Leonida kept the completed value at 0, kept device reason S_OK, and did not signal the pending event. The subsequent device-loss case still passed. Neither synthetic error was sent to the driver. No game data. AI-assisted (Codex).

@pmilewsskiedu-gif

Copy link
Copy Markdown
Contributor Author

Thanks for the independent RTX 4060 run and the additional VK_ERROR_OUT_OF_HOST_MEMORY negative control in #7089238221. I added that exact behavior as a durable test-only follow-up in commit 0e63ec7658945e3726de4d70ed1a9ab5e12d324b (pushed to this PR): the Vulkan interceptor now first returns a synthetic VK_ERROR_OUT_OF_HOST_MEMORY with bogus output count 999. COM correctly returns completed=0, device status S_OK, waiter not released. A subsequent injected VK_ERROR_DEVICE_LOST still returns UINT64_MAX, device removed 0x887A0005, waiter released. Both errors are injected and never sent to the physical driver. RTX 4060 Ti, LEONIDA_VK_VALIDATION=1, WARP normal control: zero Vulkan validation errors, executable exit0. MSVC focused vk_queue_query_sync|d3d12_com|d3d12_queue_wait|d3d12_fence_timing|d3d12_fence_device_lost_counter 5/5 PASS in 3.18s; modified regression 12/12 repeats PASS in 2.51s, git diff --check clean. Production code unchanged; no game data, AI-assisted.

@Exopus-0x65f7ad0

Copy link
Copy Markdown

Tested 51f95551 on Windows 11 (26300), RTX 5060 Ti / 621.14, MSVC 14.51 (VS 2026), Vulkan SDK 1.4.363.0, RelWithDebInfo, built from an empty folder with -DLEONIDA_LLVM_MC.

  • d3d12_fence_device_lost_counter_test: passes 10/10 (value=ffffffffffffffff, device_removed=887a0005, pending_event=1, 0 validation errors).
  • Regression: vk_fence_dispatcher_sync fails on this branch, serial and under -j4; it passes on main. It stops in step 4 ("vkGetSemaphoreCounterValue DEVICE_LOST drains without spin"): the test arms the counter-query failure and then calls sem.signal(1) to wake the dispatcher, which must notice the loss itself. VulkanFence::signal() starts with if (value <= completed()) return false;, and with this PR completed() now marks the context lost and returns UINT64_MAX on that injected VK_ERROR_DEVICE_LOST, so the CPU signal is refused (1 <= UINT64_MAX) and the test's std::_Exit(1) after signal fires.

A fix that keeps both tests (and this PR's behaviour) is to have signal() not go through completed()'s side effect just to reject a non-increasing value:

diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index a168d9a4..44205676 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -1411,7 +1411,14 @@ public:
     // ID3D12Fence::Signal from the CPU. Vulkan timelines only increase: a value at or below the current one is
     // ignored (D3D12 would set it), and the caller is told.
     bool signal(std::uint64_t value) {
-        if (value <= completed()) return false;
+        // A lost device takes no signal. Otherwise the counter is read without completed()'s device-loss marking:
+        // a value that doesn't increase is refused, and a device lost since then is reported by vkSignalSemaphore
+        // itself, so the fence dispatcher's own counter query stays the place that notices the loss.
+        if (context_.is_device_lost()) return false;
+        std::uint64_t current = 0;
+        if (context_.vk.vkGetSemaphoreCounterValue(context_.device(), semaphore_, &current) == VK_SUCCESS &&
+            value <= current)
+            return false;
         VkSemaphoreSignalInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO};
         info.semaphore = semaphore_;
         info.value = value;

A device already lost still refuses the signal, and a loss that happens later is reported by vkSignalSemaphore itself or by the dispatcher's own counter query. With it: vk_fence_dispatcher_sync + d3d12_fence_device_lost_counter 10/10, full LEONIDA_VK_VALIDATION=1 ctest -j4 146/146. Not pushed to your branch; take it if it fits.

(One note in case you re-run locally: MSBuild's parallel build didn't recompile vk_fence_dispatcher_sync_test after a change to vulkan_backend.h here, which is header-only for it; touching the test source or a single-target build was needed to see the change.)

AI-assisted (Claude Code).

@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.

P2: The new counter path marks the context lost, but the blocking host-fence path still discards vkWaitSemaphores results. In SetEventOnCompletion(target, nullptr), if the counter query succeeds with a value below target and the following wait returns VK_ERROR_DEVICE_LOST, VulkanFence::wait returns false and the wrapper returns E_FAIL without marking the context lost. GetDeviceRemovedReason can still report S_OK until a later counter query. Please propagate VK_ERROR_DEVICE_LOST from wait and add an injected wait-failure regression.

@pmilewsskiedu-gif

Copy link
Copy Markdown
Contributor Author

Review follow-up to the independently reproduced vk_fence_dispatcher_sync regression (#7089396934) and P2 blocking-wait device-loss review (#6015721506): pushed commit 8728339f9e8e7fb46ac4d965d4d4e03276f62378 to this same PR.

Production corrections: VulkanFence::signal now reads the timeline counter directly rather than through completed()'s device-loss side effect, guarding already-lost contexts and marking loss if vkSignalSemaphore itself returns VK_ERROR_DEVICE_LOST; VulkanFence::wait marks shared context lost when vkWaitSemaphores returns VK_ERROR_DEVICE_LOST. The previous counter-query fix remains unchanged.

Proof: On the prior PR head 0e63ec76, actual RTX 4060 Ti validation-enabled vk_fence_dispatcher_sync_test exits 1 after its first three scenarios; additionally, the identical new COM synthetic blocking-wait test returns E_FAIL but leaves device reason S_OK (exit 1). Patched head passes dispatcher scenario four, and injected blocking-wait loss returns E_FAIL with GetDeviceRemovedReason = DXGI_ERROR_DEVICE_REMOVED 0x887a0005 (exit 0), exactly one wait fault, zero Vulkan validation errors. Identical baseline/patch test SHA256 A4706EA31DD0CAF054A4518F47A87C0FAC391569B81975F5E5A6C25AD874227C in a detached old-head test-only worktree. Native D3D12 WARP normal fence signal/event still passes.

MSVC/Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0, RTX 4060 Ti with LEONIDA_VK_VALIDATION=1: focused vk_fence_dispatcher_sync, d3d12_fence_device_lost_counter, vk_queue_query_sync, d3d12_queue_wait, d3d12_fence_timing, d3d12_com 6/6 PASS, 3.39s. Both affected CTests pass 10/10 repeats each; git diff --check clean. Injected Vulkan failures are synthetic, not real driver loss; no actual game boot, non-NVIDIA fault coverage or whole-suite claim. Original code/tests, AI-assisted; no proprietary assets. Thank you for spotting both bugs.

@rafaelpssz

Copy link
Copy Markdown
Contributor

Tested 51f95551 on Windows 11, RTX 3060 (Ampere, driver 616.64), clang-cl 23 + xwin, Ninja RelWithDebInfo. Full ctest: 154/155. The new d3d12_fence_device_lost_counter passes, and so do d3d12_queue_wait and d3d12_queue_wait_timeout. But vk_fence_dispatcher_sync fails 6/6. On #4443's build (main plus an unrelated change) it passes, so this PR causes it.

Cause (a code path, not the GPU): part 4 of runtime/test/vk_fence_dispatcher_sync_test.cpp (around line 230) makes the hooked vkGetSemaphoreCounterValue return VK_ERROR_DEVICE_LOST. Then it calls sem.signal(1) to wake the blocked wait. VulkanFence::signal starts with if (value <= completed()) return false;. With this PR, completed() sees DEVICE_LOST, marks the device lost and returns UINT64_MAX. So signal(1) returns false, and the test thread takes if (!sem.signal(1)) std::_Exit(1);. The output stops after "mark_device_lost() wakes blocked wait and teardown completes ok", with exit 1 and no line N: message. On main, completed() ignored the result, signal() went through, and the dispatcher hit the error on its own poll.

Two possible fixes:

  • Keep completed() as is, and have signal() skip the early-out on a lost device, or query the counter itself and only return false on VK_SUCCESS with value >= target.
  • Or, if a signal on a lost device should fail, part 4 needs another way to reach the dispatcher's poll than sem.signal(1) after the flag is set. That test is what pins "drains without spinning".

Either way, it would be worth running the whole vk_fence_*/d3d12_queue_* group, not only the new test.

@Kam471 Kam471 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.

Built and ran d3d12_fence_device_lost_counter_test at 8728339f on Windows 11, RTX 5070 Ti: exit 0, 0 Vulkan validation errors.

The injected cases report:

  • VK_ERROR_OUT_OF_HOST_MEMORY with a bogus counter 999 -> value=0, device_removed=0, pending_event=0;
  • VK_ERROR_DEVICE_LOST on the first GetCompletedValue -> value=0xffffffffffffffff, device_removed=0x887a0005, pending_event=1, interceptions=2;
  • VK_ERROR_DEVICE_LOST on a blocking fence wait -> wait=0x80004005, device_removed=0x887a0005, interceptions=1.

No blocking issue.

@Exopus-0x65f7ad0

Copy link
Copy Markdown

Follow-up: #4486 (@sds-228) also changes the fence dispatcher and extends vk_fence_dispatcher_sync. On the merge of #4486 and this PR the test fails 10/10. With the signal() change from my comment above applied on top of both, it's 10/10 pass, also for this PR's d3d12_fence_device_lost_counter, and the full suite is 146/146 (RTX 5060 Ti / 621.14, full rebuilds). Details on #4486.

@MKmajster

Copy link
Copy Markdown

Re-tested at 8728339 on NVIDIA GeForce RTX 3070, driver 617.42, Windows 11 24H2, MSVC 19.44: the regression is fixed here.

  • At 51f9555, vk_fence_dispatcher_sync failed 10/10 on this machine (case 4: signal(1) hit value <= completed() once the injected counter loss returned UINT64_MAX), while main passed 10/10.
  • At 8728339: vk_fence_dispatcher_sync 10/10 pass. All five fence tests (vk_fence_dispatcher_sync, d3d12_fence_timing, d3d12_fence_events, d3d12_fence_device_lost_counter, d3d12_fence_waiter) pass, and the full CTest is 156/156.

AI-assisted (Claude).

@mrbean56

Copy link
Copy Markdown

Reading review (no Windows here; the test target is d3d12_fence_device_lost_counter_test, so this is not a pass).

  • The D3D12 semantics the change relies on are documented: after device removal, ID3D12Fence::GetCompletedValue returns UINT64_MAX, so returning UINT64_MAX from VulkanFence::completed() when the context is lost is host parity, not a convenience.
  • The VK_ERROR_DEVICE_LOST arm marks the context, so every later completed() short-circuits without a second Vulkan call to a lost device; a non-success, non-lost result returns 0, which makes waits time out rather than report success — the safe direction.
  • completed() is const and now calls mark_device_lost(), a mutable-state side effect from a query. That is fine (the state is mutable) but worth a comment: a completed() that mutates is surprising, and the reason is exactly that the Vulkan call is the only place the loss surfaces.
  • Could not run the test (Windows target); the same caveat as Runtime: order repeated Vulkan buffer transfer copies #4475/vulkan: make fence cancel wait for in-flight callbacks #4486, both of which I could build and run on Linux because they use the native Vulkan headers. If d3d12_fence_device_lost_counter_test could be split so the fence part builds against vulkan_backend.h without D3D12, it would be runnable on a Linux GPU like vk_queue_query_sync_test (Runtime: propagate queue progress counter device loss #4501), which is how I verified the queue-side propagation.

No game data. AI-assisted.

@Exopus-0x65f7ad0

Copy link
Copy Markdown

Re-tested 8728339f (with the signal()/wait() follow-up) on Windows 11, RTX 5060 Ti / 621.14, MSVC 14.51, full rebuild with -DLEONIDA_LLVM_MC and LEONIDA_VK_VALIDATION=1:

  • -R "fence|queue_wait|d3d12_com$" --repeat until-fail:10: 8/8 each round, vk_fence_dispatcher_sync included.
  • Full ctest -j4: 146/146.
  • Merged with vulkan: make fence cancel wait for in-flight callbacks #4486 (clean text merge): the same 8 tests 10/10 and the full suite 146/146, so the two can land in either order now.

The regression I reported is gone. AI-assisted (Claude Code).

@pmilewsskiedu-gif

Copy link
Copy Markdown
Contributor Author

Additional distinct device-loss boundary found and fixed on this same already-claimed PR, commit a5412e0d736f71b273f299475c2d37710dfe2709, coordinator follow-up claim #4421 (comment).

ID3D12Device1::SetEventOnMultipleFenceCompletion with event=nullptr directly executes vkWaitSemaphores instead of the corrected single-fence VulkanFence::wait(). On previous PR head 8728339f, synthetic VK_ERROR_DEVICE_LOST in BOTH ALL and ANY multi-fence waits returned E_FAIL but incorrectly left GetDeviceRemovedReason = S_OK; identical extended test exited 1, with zero Vulkan validation errors. The patch in runtime/shims/d3d12_com.cpp now marks shared VulkanContext lost only for VK_ERROR_DEVICE_LOST, retaining E_FAIL result and keeping injected VK_ERROR_OUT_OF_HOST_MEMORY nonfatal. No mutex is held during notification.

Verification on Windows RTX 4060 Ti (Vulkan SDK 1.4.363.0 + LEONIDA_VK_VALIDATION=1): patched ALL and ANY both return E_FAIL with GetDeviceRemovedReason=DXGI_ERROR_DEVICE_REMOVED (0x887a0005); nonfatal controls still leave S_OK, exactly two wait injections per mode, 0 validation errors; executable exit 0. Native WARP normal two-fence ALL and ANY blocking waits S_OK. Identical old-head vs patched test source SHA256 CCF76770ACA41B91923E18F59937677CCBAEB789824ADC96FDE9D79268A3433F. Six relevant CTests vk_queue_query_sync, vk_fence_dispatcher_sync, d3d12_queue_wait, d3d12_fence_timing, d3d12_fence_events, d3d12_fence_device_lost_counter 6/6 pass. Dispatcher, fence events and extended device-loss test each 10 consecutive repetitions passed (30/30). MSVC19.44/Ninja, git diff --check clean.

This is deliberately a follow-up in #4445, not a redundant new PR. Faults are one-shot synthetic VkResult injection, NOT physical driver device loss; no real gameplay, proprietary game assets or DRM changes. Original code/test, AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Tested a5412e0 merged with main d0835dd (merged tree 029567dde1) on Windows 11 with two GPUs (MSVC 14.51): built d3d12_fence_device_lost_counter_test and ran it directly.

  • NVIDIA RTX 4080 SUPER: exit 0, Vulkan validation errors: 0, multi-fence blocking ANY: transient=80004005 ... lost_wait=80004005, removed=887a0005, interceptions=2. Intel UHD 770 (driver 32.0.101.6129): exit 0, the same line, 0 validation errors.

Negative control: runtime/shims/d3d12_com.cpp and runtime/src/vulkan_backend.h restored from main with the PR's test kept: exit 1 on both GPUs, and the same line shows removed=00000000 instead of 887a0005 (the device loss is not propagated to the D3D12 side).

@Exopus-0x65f7ad0

Copy link
Copy Markdown

Re-tested a5412e0d (RTX 5060 Ti / 621.14, full rebuild with -DLEONIDA_LLVM_MC, LEONIDA_VK_VALIDATION=1):

AI-assisted (Claude Code).

@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested a5412e0 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 ctest -R "^d3d12_fence_device_lost_counter$" (the test this PR adds): pass on both GPUs, 1/1 each. No game files.

AI-assisted.

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.

10 participants