Skip to content

d3d12: propagate explicit removal to fence notifications - #8454

Closed
kyaemma wants to merge 3 commits into
ProjectLeonida:mainfrom
kyaemma:fix/removedevice-fence-notifications
Closed

kyaemma wants to merge 3 commits into
ProjectLeonida:mainfrom
kyaemma:fix/removedevice-fence-notifications

Conversation

@kyaemma

@kyaemma kyaemma commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as: #8344 (comment)

Depends on: #8101, #3293, #4577 (integration/rebase coordination for overlapping code; no functional dependency for this fix).

What and why

Explicit ID3D12Device5::RemoveDevice originally updated the removal reason without reaching the fence/dispatcher context. Propagate removal through the existing context so completed values become UINT64_MAX and single/ALL/ANY HANDLE notifications fire, including registrations after removal. This follows the documented removal contract and the native WARP oracle.

Review follow-ups:

  • f2ee288a0: a first WARP CreateFence() failure is FAIL (1), rather than SKIP (77), after successful factory/adapter/device setup.
  • 89893c0a9bbdb7dccd69071874d122e72955252f: fixes the introduced CPU Fence::Signal regression after removal. Under the existing claim mutex, an already-lost context returns S_OK before bookkeeping or backend work. Signal(1/0/42) after RemoveDevice returns successfully and leaves the value at UINT64_MAX, matching WARP. The Vulkan backend is unchanged: an operation starting healthy still reports its first OOM/device-loss error as E_FAIL; no after-the-fact error-to-success mapping was added.

Three files are changed overall: d3d12_vulkan_device.cpp (removal propagation), d3d12_com.cpp (one-line Signal guard), and d3d12_fence_device_lost_counter_test.cpp (oracle/regressions).

Tested

Windows 11, MSVC 19.51, Vulkan SDK 1.4.363.0, RX 9070 XT / driver 32.0.32015.7029, LEONIDA_VK_VALIDATION=1; native Microsoft WARP. Synthetic inputs only; no physical GPU failure/reset induced.

Check Actual result
Exact final regression linked against the prior COM implementation from f2ee288a0 Expected FAIL, actual exit 1: WARP S_OK×3; layer E_FAIL/S_OK/E_FAIL for 1/0/42; validation errors 0
Final guarded implementation: five fence tests plus three queue-wait cases, after rebuild 8/8 Passed, exit 0; WARP/layer Signal matrix PASS; validation errors 0
Healthy Signal with test-intercepted OOM / first DEVICE_LOST Both remain E_FAIL; respectively healthy/value0 and removed/valueMAX, exactly one interception
Signal after already-known injected loss S_OK, MAX, no additional foreground Vulkan signal; this pins layer policy, not a native physical-loss measurement
Additional GPU controls: shared-queue handover / m4 compute / standalone vk_queue 2/3 passed: handover and compute PASS; vk_queue FAILS with 10 WRITE_AFTER_WRITE hazards under validation, reproduced once. Its test/backend inputs are unchanged from f2ee and it does not link the modified COM code; this separate validation failure is not hidden or attributed to the guard

The final test checks MAX after each post-removal Signal and no vkSignalSemaphore call on the calling thread. It retains pre/post HANDLE notifications, repeated removal, and the prior wait/counter error controls. Luna independently ran the final regression with validation: exit 0. Claude Opus 5.5 and Luna approve the exact final diff. No new warnings observed in the Signal builds. git diff --check and check_no_game_data.py on both newly modified files pass.

The five fence targets are vk_fence_dispatcher_sync_test, d3d12_fence_timing_test, d3d12_fence_events_test, d3d12_fence_device_lost_counter_test, d3d12_fence_waiter_test. The queue cases are d3d12_queue_wait, d3d12_queue_wait_timeout, d3d12_queue_wait_one_queue.

cmake --build <build> --config RelWithDebInfo --target vk_fence_dispatcher_sync_test d3d12_fence_timing_test d3d12_fence_events_test d3d12_fence_device_lost_counter_test d3d12_fence_waiter_test d3d12_queue_wait_test
ctest --test-dir <build> -C RelWithDebInfo --output-on-failure -R '^(vk_fence_dispatcher_sync|d3d12_fence_timing|d3d12_fence_events|d3d12_fence_device_lost_counter|d3d12_fence_waiter|d3d12_queue_wait|d3d12_queue_wait_timeout|d3d12_queue_wait_one_queue)$'

Earlier public validations by ECHOZwei, lohant27-netizen, gut0to and h0useadv apply to original head 2a87ecd63, not this Signal follow-up. No remote CI success or full-suite rerun is claimed.

Remaining scope / integration

Null-event blocking waits and the pre-existing CreateFence-after-removal HRESULT mismatch remain open. #4577 rewrites the waits but its current draft returns E_FAIL on logical loss, so it is not claimed as complete native S_OK conformance. Preserve this early Signal guard when integrating its rewritten Fence::Signal.

This regression proves ordered calls after RemoveDevice has returned. A Signal racing removal can still encounter the pre-existing check/use window; no concurrency guarantee for overlapping calls is added. The foreground counter does not cover older held callbacks on the dispatcher thread.

AI-assisted: yes — Codex (Sol orchestration/final review, Terra implementation, Luna independent validation) and Claude Opus 5.5 (independent root-cause analysis and final diff review).

Discord : kyaemma

Checklist

  • No game data, keys or decryption, including in the evidence above.
  • A new translator rule is in the contract table (not applicable: no translator changes).

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

The implementation change looks correct: mark_device_lost() sets the context lost and wakes the dispatcher, GetCompletedValue returns UINT64_MAX, and single/multiple SetEventOnCompletion complete immediately after removal; the test otherwise covers pre/post-removal single/ALL/ANY notifications, fence values, repeated removal and the native WARP oracle. One test defect:

runtime/test/d3d12_fence_device_lost_counter_test.cpp:127-131 groups device->CreateFence with the WARP setup checks:

if (FAILED(CreateDXGIFactory1(...)) ||
    FAILED(factory->EnumWarpAdapter(...)) ||
    FAILED(D3D12CreateDevice(...)) ||
    FAILED(device->CreateFence(...)))
    return 77;

main treats 77 as SKIP (CTest property SKIP_RETURN_CODE 77). If WARP and D3D12CreateDevice succeed but CreateFence fails, that is a fence-control failure, not WARP unavailability; the test skips before either remove_device_notifies call, so the explicit-removal/fence-notification behavior is never exercised. This contradicts the PR description's “Oracle failures are FAIL, not SKIP; only WARP setup unavailability skips.” The rest of the new control (event, signal, query, multiple-fence waits) correctly returns 1.

Fix: move CreateFence out of the return 77 block into the return 1 oracle-control block, or run the layer removal check independently when the oracle fails. Synthetic only; no game files. AI-assisted (OpenCode).

@ECHOZwei

Copy link
Copy Markdown

Tested 2a87ecd6383f639c6f58be92bebf9499a4c33220 merged onto origin/main 427a7ec71 (clean merge) on Windows 11 (MSVC 14.51 / Visual Studio Community 2026, RelWithDebInfo, Vulkan SDK 1.4.363.0, LEONIDA_VK_VALIDATION=1) on two GPUs (synthetic tests only; no game files): pass

  • Negative control (d3d12_vulkan_device.cpp at origin/main 427a7ec71, RTX 4080 SUPER, LEONIDA_VK_VALIDATION=1): d3d12_fence_device_lost_counter_test.exe fails as expected (exit 1: Leonida Vulkan RemoveDevice: reason=887a0005 fences=0/0 before=258 after=258 repeated=0: FAIL); with the PR merged, passes cleanly.
  • Built vk_fence_dispatcher_sync_test, d3d12_fence_timing_test, d3d12_fence_events_test, d3d12_fence_device_lost_counter_test, and d3d12_fence_waiter_test cleanly (0 new warnings).
  • NVIDIA GeForce RTX 4080 SUPER (driver 621.01, validation on):
    • ctest --test-dir runtime/build -C RelWithDebInfo --output-on-failure -R ^(vk_fence_dispatcher_sync|d3d12_fence_timing|d3d12_fence_events|d3d12_fence_device_lost_counter|d3d12_fence_waiter)$: 5/5 passed (4.24s), 0 validation errors.
  • AMD Radeon(TM) Graphics iGPU (driver 25.10, LEONIDA_VK_DEVICE=AMD, validation on):
    • Same 5 tests: 5/5 passed (3.18s), 0 validation errors.
  • git diff --check and python -I scripts/check_no_game_data.py runtime/shims/d3d12_vulkan_device.cpp runtime/test/d3d12_fence_device_lost_counter_test.cpp: clean (exit 0).
  • Code review: minimal and clean; GetDeviceRemovedReason() checks both removed_ and state_.context->is_device_lost(), so threads woken by mark_device_lost() before removed_.store(true) still observe DXGI_ERROR_DEVICE_REMOVED.

AI-assisted.

@rideinstead

Copy link
Copy Markdown

TEST validation — Needs changes

Reviewer-flagged test defect (significant):

In native_warp_control() in runtime/test/d3d12_fence_device_lost_counter_test.cpp, device->CreateFence(...) is grouped inside the return 77 (SKIP) block alongside WARP setup:

if (FAILED(CreateDXGIFactory1(...)) ||
    FAILED(factory->EnumWarpAdapter(...)) ||
    FAILED(D3D12CreateDevice(...)) ||
    FAILED(device->CreateFence(...)))
    return 77;

If WARP and D3D12CreateDevice succeed but CreateFence fails, the test returns 77 → CTest treats it as SKIP, and remove_device_notifies() is never called. The explicit-removal/fence-notification behavior is never exercised. This contradicts the PR's own stated contract: "Oracle failures are FAIL, not SKIP; only WARP setup unavailability skips."

Fix needed: Move CreateFence out of the return 77 block into the return 1 oracle-control block (or run the layer removal check independently when the oracle fails).

Other notes:

  • CI has not run (0 checks on the PR)
  • D3D12 half is Windows-only (not runnable on Linux CI)
  • scripts/check_no_game_data.py passed on both changed files (exit 0)
  • AI-assisted; one review by tupacfan04 exists, second reviewer needed

Recommendation: Needs changes — fix the SKIP/FAIL contract defect before merge.

@lohant27-netizen

Copy link
Copy Markdown

Tested 2a87ecd63 on Windows 11 (10.0.26300), NVIDIA GeForce RTX 5070 (driver 32.0.16.2101, subgroup 32), MSVC with SDK 10.0.26100, Release, merged onto main 2a3b3b554 together with #8457, #8454, #8373, #8364 and #8340 (no conflicts between them): pass.

@gut0to gut0to left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the removal and notification ordering, including repeated removal and notifications registered after removal. Built d3d12_fence_device_lost_counter_test with native MSVC and ran the WARP/Vulkan regression through CTest: d3d12_fence_device_lost_counter passed (1/1). The new test separately checks native WARP as the behavior oracle and the shim against the same expectations.

@Envyre

Envyre commented Nov 23, 2026

Copy link
Copy Markdown

The event-based paths look right: mark_device_lost() wakes the dispatcher, which drains every pending entry once, and enqueue checks device_lost_ under its mutex so late registrations fire immediately. But the blocking (null-event) waits are still uncovered:

  • runtime/shims/d3d12_com.cpp:1489: a SetEventOnCompletion(v, nullptr) already blocked in fence_.wait(value, UINT64_MAX) never returns, because after a logical RemoveDevice the Vulkan device is healthy and the semaphore is never signalled.
  • runtime/shims/d3d12_com.cpp:1994-2003: the null-event SetEventOnMultipleFenceCompletion has no is_device_lost() check, so it hangs even when called after removal.

Native result (RTX and WARP, ID3D12Device5::RemoveDevice): an in-flight blocking single wait and an in-flight blocking multi ALL wait both return S_OK, a post-removal null-event multi wait returns S_OK right away, and shared fences also read UINT64_MAX. Smaller differences: native Signal after removal returns S_OK (the shim returns E_FAIL), and CreateFence returns DXGI_ERROR_DEVICE_REMOVED (the shim succeeds).

Suggestion: bound the blocking waits in slices and re-check is_device_lost(), and add a threaded blocking-wait case to the test.

AI-assisted (Claude Code).

@h0useadv

Copy link
Copy Markdown

Tested 2a87ecd63 (base a86d7f950) on AMD Radeon RX 9070 XT (RDNA4), AMD Windows driver 32.0.32029.2010, Windows 11 Pro build 26300: pass, no new failures. Built with clang-cl 23.1.6 + lld-link against an xwin splat (MSVC 14.44, SDK 10.0.26100), RelWithDebInfo; synthetic, no game files.

  • d3d12_fence_device_lost_counter (with the new WARP-vs-layer removal checks): passes.
  • The other fence tests pass: d3d12_fence_events, d3d12_fence_timing, d3d12_fence_waiter, vk_fence_dispatcher_sync.
  • Full ctest -j8, failures rerun with -j1: 265/271; the failures are the ones main has on this GPU, plus d3d12_indirect_dispatch_count, which fails because this base predates its fix on main.

AI-assisted (Claude Code).

@kyaemma

kyaemma commented Nov 23, 2026

Copy link
Copy Markdown
Author

@tupacfan04 Thanks for catching this. You were right: the first CreateFence() failure was still grouped with WARP setup and could skip the oracle regression. Fixed in f2ee288: factory/adapter/device setup retains exit 77; the first fence creation now returns FAIL (1), like the later control operations.

Rebuilt all five fence targets and reran them on RX 9070 XT with Vulkan validation: 5/5 passed, no new warning lines or validation errors. Compiled branch-forcing probes also verify old fence-failure → 77, corrected fence-failure → 1, and setup-failure → 77. The follow-up changes only the test; Luna and a fresh Claude review approved it.

@kyaemma

kyaemma commented Nov 23, 2026

Copy link
Copy Markdown
Author

@rideinstead Thanks for independently identifying the same skip/fail defect. Addressed in f2ee288: CreateFence() now runs after successful WARP setup, and failure returns 1 instead of 77. I checked the remaining early returns: later oracle operations already fail with 1.

The five rebuilt targeted tests pass with validation on RX 9070 XT; an independent Luna run also passes with zero validation errors. The current corrected test, linked in an isolated executable against the original runtime, still fails as expected (actual exit 1, WARP PASS, layer counters 0/0 and notification timeouts). No runtime behavior was changed by this follow-up.

@kyaemma

kyaemma commented Nov 23, 2026

Copy link
Copy Markdown
Author

@ECHOZwei Thanks for the independent negative control and the RTX 4080 SUPER / AMD iGPU validation, including the clean five-target runs. I have retained those results against the exact commit you tested, 2a87ecd6383f639c6f58be92bebf9499a4c33220. Thanks also @lohant27-netizen and @gut0to for the additional RTX 5070 and native MSVC validation of that revision.

Follow-up f2ee288 addresses the reviewers' test classification feedback: initial WARP fence creation failure now fails instead of skipping. It preserves the production implementation. Our local RX 9070 XT validation of the follow-up is 5/5 targeted tests passed, with zero validation errors.

@kyaemma

kyaemma commented Nov 23, 2026

Copy link
Copy Markdown
Author

@Envyre Thanks for the native checks. I reproduced the two post-removal HRESULT differences with synthetic WARP/original-layer/PR-layer probes:

After RemoveDevice WARP Original layer PR runtime
Signal(1) S_OK, value stays UINT64_MAX S_OK, value becomes 1 E_FAIL, value stays UINT64_MAX
CreateFence DXGI_ERROR_DEVICE_REMOVED S_OK S_OK

So the Signal HRESULT divergence is introduced by the initial propagation change; creation-after-removal is pre-existing. I have highlighted both as open compatibility items in the description. They are not resolved by follow-up f2ee288, which only fixes the test's SKIP/FAIL classification.

The null-event cases were already explicitly excluded here because #4577 rewrites those same waits. Its current draft still maps logical loss to E_FAIL, so I do not claim it fully matches your native S_OK result either. I am keeping the reviewed test correction separate from that competing runtime work and leaving these concerns open for scope/integration review; the HANDLE regression alone does not establish complete removal conformance.

@kyaemma

kyaemma commented Nov 23, 2026

Copy link
Copy Markdown
Author

@Envyre The Signal regression is now fixed in 89893c0. A CPU fence Signal observing an already-lost context returns S_OK before claim bookkeeping or backend work; the completed value stays UINT64_MAX.

The same final WARP/layer test calls Signal(1/0/42) and checks MAX after each. With the prior COM implementation, it fails (actual exit 1, layer E_FAIL/S_OK/E_FAIL); with the guard, all three return S_OK, with no foreground Vulkan signal. Separate injections ensure a healthy-path OOM and a first device-loss error still return E_FAIL with the correct device state, rather than being hidden by the new guard.

Eight fence/queue-wait checks pass with Vulkan validation; Luna independently ran the final regression and Claude reviewed the exact diff. Additional handover/compute checks pass; a separate unchanged standalone vk_queue test still reports 10 validation hazards and is disclosed in the description.

Only the Signal part of your report is resolved here. CreateFence after removal and null-event blocking waits remain open; #4577's rewritten Signal must retain this early guard, and its current wait HRESULT policy still needs coordination. No physical GPU loss or reset was induced.

@Zipettte

Copy link
Copy Markdown

Tested on AMD Radeon RX 7700 XT (RDNA3), AMD Windows driver 32.0.32029.2010, Windows 11, MSVC 14.51 Release, at f2ee288. Full ctest -j 8, out of 239 tests:

No regression. The only failures are the same 5 as on main dde82f2 on this card (vk_queue_query_sync, d3d12_vulkan_device tiles == 1, d3d12_texture_tiles, d3d12_depth_resolve, d3d12_pipeline_library_host; tracked in #8426). Every test this PR adds or touches passes.

AI-assisted (Claude Code).

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