Skip to content

d3d12_fence_timing_test: a fence released after a postponed CPU Signal (D3D-496 case 1) - #4756

Open
0klavair wants to merge 5 commits into
ItsNotRexed:mainfrom
0klavair:runtime/released-fence-cpu-signal
Open

0klavair wants to merge 5 commits into
ItsNotRexed:mainfrom
0klavair:runtime/released-fence-cpu-signal

Conversation

@0klavair

@0klavair 0klavair commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

D3D-496, case 1. Claimed in the coordination issue as: #4683 (comment) (from @Exopus-0x65f7ad0's finding and repro)

Depends on: #4663
Depends on: #4740 (D3D-496 case 2, itself on #4663). Only the last commit is this PR's. (It no longer depends on #4282: the case passes 200/200 on #4663 + #4740 alone, 3 runs out of 3.)

What and why

Case 1: queue A Wait(gate) + Signal(F, 1), CPU F->Signal(2) (postponed behind A's Signal), queue B Wait(F, 2) + Signal(done), then F->Release(). If the release comes before the GPU reaches 1, ~Fence → cancel_fence_waits runs the postponed callback with now < queued, the CPU Signal is lost, and remove_fence drops B's held batch. The host ran B every time (50/50 in the finding). With #4663 + #4282, 16-24 of 200 were still dropped.

No new runtime change is needed: #4740 makes a released fence's wait count as reached, so B runs whether or not the postponed Signal landed. That is what the host does. This PR adds the regression test, so the case stays covered.

The change (test only)

check_released_postponed_signal in d3d12_fence_timing_test: the scenario above, 200 iterations, a fresh fence each time.

  • Even iterations release F before opening the gate, so the GPU cannot have reached 1 and the lost-Signal path is always taken. On a fast GPU the original 0-1.9 ms race never fired here: 200/200 even without the fix, so the race alone was no regression test.
  • Odd iterations keep the original timing: release 0-1.9 ms after opening the gate.
  • B's batch must run every time. The same function runs on the host runtime as the reference.

Tested

On NVIDIA GeForce RTX 5060, driver 621.01, Windows 11 23H2 22631.6199, MSVC 19.44, Vulkan SDK 1.4.363:

No game files. AI-assisted.

requiemsnkrp and others added 2 commits November 23, 2026 04:50
…kets (D3D-486)

~VulkanFence destroyed its timeline semaphore at once, though submissions made before (a queue Signal behind
other work, a GPU Wait for it) may still signal or wait for it: VUID-vkDestroySemaphore-semaphore-05149. A
queue Wait not yet attached to a batch, or a held Signal of the fence, would also be submitted later with the
destroyed handle.

- vulkan_backend.h: DeferredDeletionQueue::Kind::semaphore; ~VulkanFence calls remove_fence, then defers the
  semaphore (retired once every submission made before the release has completed).
  VulkanQueue::discard_waiting_on also forgets the semaphore: a Wait not yet attached counts as reached (as a
  released fence does for the CPU), a held Signal of it signals nothing; held batches waiting on it are still
  dropped and reported.
- queue_holds.h: remove_fence asks every queue, not only when something is held (a pending Wait names the
  fence too).
- d3d12_queue_handover_test: case on the layer and the host runtime, behind a ~300 ms GPU gate on the COPY
  queue: A a Signal and a DIRECT Wait in flight, B a Wait not yet attached, C a held Signal; each fence is
  released right away, readbacks exact. With the semaphore destroyed at once (origin/main behaviour) the layer
  gave VUID-vkDestroySemaphore-semaphore-05149 (A, B), VUID-VkSubmitInfo-pWaitSemaphores-parameter (B) and an
  access violation.
- docs/d3d12_vulkan.md 4.4.

RTX 3060 / 617.14, LEONIDA_VK_VALIDATION=1 with sync validation: 374 layer cases, 372 host, 0 messages.

AI-assisted.
…(D3D-496)

When an ID3D12Fence was released while a queue held a batch waiting on it,
VulkanQueue::discard_waiting_on dropped the batch and printed "that work is
dropped". The host runtime lets the queue go on: the batch runs and a Signal
after it is reached (measured on an RTX 5060 host, and by @Exopus-0x65f7ad0
on an RTX 5060 Ti with the debug layer on).

discard_waiting_on now takes only the released fence's wait off each held
operation, as ItsNotRexed#4663 already does for a Wait not yet attached to a
submission; remove_fence's pump_ready then releases the operation in queue
order once its other waits are met. No batch is dropped.

d3d12_queue_handover_test's "fence released while a batch waits" now checks
that behaviour on the layer and on the host runtime: the held batch copies
data to stage[1] and signals 101, and after the release the Signal is
reached and stage[1] reads back exact. Without the change the layer drops
the batch and the case fails.

@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 both tests at e96ad918a on Windows 11, RTX 5070 Ti:

  • d3d12_fence_timing_test -> exit 0: the new D3D-496 case 1 (a fence released after a postponed CPU Signal), plus held CPU signals, calibration and LUID checks.
  • d3d12_queue_handover_test -> exit 0: fence released while a batch waits: the batch ran and its Signal was reached; layer 374 / host 373 exact.

Stacking note: the branch contains #4663 (0cc958d0f, D3D-486 semaphore retirement) and #4740 (d7e534067, D3D-496 case 2) under its case-1 commit, so it merges cleanly with those two by construction and would land them too. It conflicts with #4681 (vulkan_backend.h), #4557 (vulkan_backend.h, handover test), #4570 (queue_holds.h) and #4577 (docs, d3d12_com.cpp, fence timing test). If this is the intended combined branch, the three component PRs should be closed in its favor; otherwise it needs a rebase onto #4663/#4740 and a note of what is carried. git diff --check clean.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested e96ad918 on Windows 11 with the NVIDIA GeForce RTX 4090 and the AMD Radeon iGPU. Both adapters pass the new fence test.

I built d3d12_fence_timing_test and d3d12_queue_handover_test in RelWithDebInfo. I ran both executables with LEONIDA_VK_DEVICE=4090 and then with LEONIDA_VK_DEVICE=Radeon. The Vulkan device log confirms each adapter.

Queue B completes 200/200 iterations on each Vulkan adapter. The native D3D12 reference also completes 200/200 in each run. The queue handover test passes all 374 layer cases on each adapter. Its native D3D12 reference uses the RTX 4090 and passes all 373 cases.

For a negative control, I reversed only the vulkan_backend.h change from dependency commit d7e53406. I shortened the new loop to two iterations to cover one release before the gate opens and one release after it opens. On the RTX 4090, queue B completes only 1/2 iterations. The new assertion fails.

NVIDIA driver 32.0.16.2101. AMD driver 32.0.21043.5001.

AI-assisted.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested e96ad918 on Windows 11, NVIDIA GeForce RTX 5090 (driver 617.42), MSVC 19.44, Vulkan SDK 1.4.363, RelWithDebInfo.

Stacking: confirmed as declared — git log origin/main..HEAD shows 8dfe9bce (#4282), 0cc958d0 (#4663), d7e53406 (#4740), merge 13215ed2, then this PR's own commit e96ad918, which touches only runtime/test/d3d12_fence_timing_test.cpp. The gh pr diff file list (d3d12_com.cpp, queue_holds.h, vulkan_backend.h, handover test, docs) is entirely carried by the stacked branches, not this commit.

  • d3d12_fence_timing_test -> exit 0, 4/4 runs: fence released after a postponed CPU Signal (Vulkan layer): B's batch ran 200/200 and (host runtime): 200/200 every run. One run with LEONIDA_VK_VALIDATION=1 -> exit 0, no VUIDs.
  • d3d12_queue_handover_test (carries Runtime: a queue waiting on a released fence goes on, as on the host (D3D-496, case 2) #4740's case on this branch) -> exit 0, 4/4 runs: layer 374 / host 373 cases exact, fence released while a batch waits reached on both.
  • Related tests: d3d12_queue_wait_test, d3d12_fence_waiter_test, d3d12_fence_events_test -> all exit 0.
  • git diff --check clean.

No blocking issue. AI-assisted.

@justr4mzi

Copy link
Copy Markdown
Contributor

Verified commit e96ad918 locally on Windows 11 x64 (AMD Radeon RX 6700 XT, driver 26.11.2, RelWithDebInfo):

  • Built d3d12_fence_timing_test.exe and d3d12_queue_handover_test.exe ([13/13]).
  • ctest --test-dir build --output-on-failure -R "^(d3d12_fence_timing|d3d12_queue_handover)$" -V:
    • Standard: 2/2 passed (13.23 sec real: d3d12_queue_handover 10.55 sec — layer 374, host AMD Radeon RX 6700 XT 373; d3d12_fence_timing 2.67 sec — 200/200 layer and host).
    • LEONIDA_VK_VALIDATION=1: 2/2 passed (12.49 sec real: 10.26 sec + 2.22 sec, AMD Radeon RX 6700 XT (validation on), 0 validation errors).
  • 3/3 consecutive direct .\build\d3d12_fence_timing_test.exe runs under LEONIDA_VK_VALIDATION=1: exit 0 (200/200 layer and host every run).
  • Repo checks: tests/test_ctest_registration.py (4/4 in 0.956s), tests/test_docs_*.py (20/20 in 27.943s), scripts/check_no_game_data.py exit 0, and git diff --check exit 0.

Non-blocking review note: when combining with #4725 (TEST-1017), keep #4725's callback_lifetime_ synchronization in runtime/shims/d3d12_com.cpp's ~Fence() / deferred signal callback alongside the signaled/pump_ready() call here so semaphore destruction stays serialized with an already-ready callback.

AI-assisted (Gemini, Antigravity).

…l (D3D-496 case 1)

check_released_postponed_signal runs @Exopus-0x65f7ad0's scenario 200 times
on a fresh fence each time: queue A Wait(gate, i) + Signal(F, 1), CPU
F->Signal(2) (held behind A's Signal), queue B Wait(F, 2) + Signal(done, i),
then F->Release(). Even iterations release F before opening the gate, so the
GPU cannot have reached 1 and the postponed Signal is lost; odd ones release
it 0-1.9 ms after. B's batch must run every time, as on the host runtime,
which the test also runs as the reference.

With ItsNotRexed#4663 + ItsNotRexed#4282 alone the layer drops B's batch on every early release
(100/200 here). With D3D-496 case 2 (a released fence's wait counts as
reached) it runs 200/200, on the host too.
@kapitche

Copy link
Copy Markdown
Contributor

Tested c905eb0 merged with main 98fa503 on Windows (MSVC 14.51, RelWithDebInfo, Ninja): pass (build only; GPU runs skipped)

  • Full runtime build (.git\pr_rt_full.bat flags) succeeds on the merged tree; d3d12_fence_timing_test.exe and d3d12_queue_handover_test.exe link. No new warnings in the changed files (the warnings printed are in xgameruntime_test.cpp and d3d12_vulkan_device_test.cpp, untouched here).
  • Not run: d3d12_fence_timing_test and d3d12_queue_handover_test need a GPU (and the layer vs host runtime comparison), so the new cases and the negative control (old fence-release behavior) were not exercised. Linux: these are D3D12 tests, not buildable there.
  • Read the diff: remove_fence no longer returns early when nothing is held, discard_waiting_on strips the released semaphore from pending waits, held waits and held Signals (null fence is accepted by submit), and the semaphore goes through defer_destroy. No problem found by reading.

@Etr0p

Etr0p commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested c905eb0 (the current head; the GPU runs above are on e96ad918) on NVIDIA GeForce RTX 5070 Ti Laptop GPU (621.24) and AMD Radeon 610M (RDNA2 iGPU, AMD 26.11.2 / 32.0.21045.29003), Windows 11 Home 26200, MSVC 19.44 (Build Tools 17.14, Windows SDK 10.0.26100), Vulkan SDK 1.4.363.0: pass.

Built with scripts\build_runtime.ps1 from this head (no SDL3, so gameinput is disabled), then the whole ctest --test-dir runtime/build -C RelWithDebInfo, one GPU at a time through LEONIDA_VK_DEVICE:

Whole suite d3d12_fence_timing d3d12_queue_handover
RTX 5070 Ti Laptop 146 tests, 0 failed (9 skipped, 1 disabled) Passed, 2.55 s Passed, 7.34 s
Radeon 610M 146 tests, 0 failed (8 skipped, 1 disabled) Passed, 1.97 s Passed, 8.15 s

The skips are the tests that need game files or an optional component. The build has one compiler warning, C4456 in xgameruntime_test.cpp, which main has too. I ran the two tests once each inside the suite, not in a repeat loop, and did not review the code. No game files. AI-assisted (Claude Code).

@Exopus-0x65f7ad0

Copy link
Copy Markdown
Contributor

Independent check for D3D-496 (I posted the original finding). RTX 5060 Ti / 621.14, Windows 11 (26300), MSVC 14.51, Vulkan SDK 1.4.363.0, RelWithDebInfo:

tree gated (postponed CPU signal) immediate CPU signal
main 7b7d73f8 0/200 run, 0 VUIDs 200/200, VUID-vkDestroySemaphore-05149 every run
#4663 + #4282 176–184/200, 0 VUIDs 200/200, 0 VUIDs
#4756 (#4663 + #4740) 200/200, 0 VUIDs 200/200, 0 VUIDs

This now matches the host D3D12 runtime (50/50 in both orders, debug layer silent), and it no longer needs #4282 for this case. Thanks @0klavair.

AI-assisted (Claude Code). No game files.

@ray3242

ray3242 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Independent Pascal verification of PR head c905eb0d (built as-is; its base is d0835dd9, I did not merge current main), adding the GPU family missing from the runs already posted here.

Environment (AI-assisted, OpenCode): Windows 11 Pro build 26300, MSVC 19.44.35207 (cl 19.44.35230), CMake 4.4.4 + Ninja, RelWithDebInfo, Vulkan headers fetched by CMake. GPU: NVIDIA GeForce GTX 1060 6GB, driver 582.78, Vulkan subgroup sizes 32..32 (default 32), maxPushConstantsSize 256 bytes, no Resizable BAR. No game data, keys or decryption.

Build: cmake -S runtime -B <build> -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLEONIDA_VULKAN_HEADERS_FETCH=ON -DLEONIDA_SDL3_FETCH=ON, then build of d3d12_fence_timing_test and d3d12_queue_handover_test — both linked cleanly, no warnings from the changed files.

Results:

  • d3d12_fence_timing_test.exe -> exit 0. The new case passes on both paths: "fence released after a postponed CPU Signal (Vulkan layer): B's batch ran 200/200" and "(host runtime): B's batch ran 200/200". Multiple-fence waits, held CPU signals, clock calibration, adapter LUID and custom heap properties all checked; no mismatch.
  • d3d12_queue_handover_test.exe -> exit 0. Vulkan layer: 374 cases passed, every one exact; host D3D12: 373 cases passed, every one exact, including a released fence with A Signal/Wait in flight, B Wait not yet attached and C held Signal, plus a never-signaled fence destroyed without a hang.

Not run (environment limitation, not a PR defect): with LEONIDA_VK_VALIDATION=1, the fence test reports "VK_LAYER_KHRONOS_validation is not installed" (exit 1) and the queue test returns 77 (the documented skip code) after "Vulkan standalone device unavailable". I did not change any validation config.

Depends on: none. No conflict with my open PRs; this is verification only, not a competing fix.

@memorypressure

Copy link
Copy Markdown

Merge-and-read review on main (no runnable checks for runtime files on a CPU-only box): the branch merges cleanly, drops the held_==0 early-out for the right reason — a Wait not yet attached to a submission still names the semaphore, so the early-out could skip live waiters — routes the released fence's semaphore through the deferred-deletion tickets like the other kinds, and both behaviour changes are pinned by the named GPU-gated cases. The host-comparison claims (RTX 3060) are unverifiable here. Worth flagging: no CI checks are reported on this branch at all. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only review).

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested c905eb0 on Intel UHD 770, driver 32.0.101.7088, Windows 11, MSVC, with LEONIDA_VK_VALIDATION=1 and the Khronos validation layer loaded, at the stack head (#4740's head d7e5340 is an ancestor of c905eb0, and #4663's head 0cc958d is an ancestor of d7e5340): d3d12_fence_timing_test, rebuilt from scratch, exits 0. Both new lines print: "fence released after a postponed CPU Signal (Vulkan layer): B's batch ran 200/200" and "(host runtime): B's batch ran 200/200". There were no validation error lines. The negative control without #4740 was not run.

AI-assisted.

@memorypressure

Copy link
Copy Markdown

Merge-order addendum to the review above, found while verifying a sibling PR and confirmed on the graph: this branch contains 0cc958d, which is the head of still-open #4663 and is not on main — so #4663 must land before this PR (open #4740 carries the same commit and sits in the same chain). AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

main now pins the fences a queue waits on (the queue-holds review fixes) and its
fence_released case, layer only, expects a batch waiting on a released fence to stay
held. The host runtime runs it: this branch's fence_released, on the layer and the
host, shows the batch runs and its Signal is reached (RTX 5060, WARP and NVIDIA).
Resolved for the host behaviour:

- ID3D12CommandQueue::Wait no longer pins the fence; Signal keeps its pin, so a held
  Signal of a released fence still signals (held_signal_survives_release).
- QueueHolds::remove_fence keeps main's re-entrancy guard (in_pump) but not the
  held_ == 0 early-out: a Wait not yet attached to a submission names the fence too.
- discard_waiting_on takes the released fence's wait off held work instead of
  dropping it.
- d3d12_queue_handover_test keeps this branch's fence_released (layer and host) and
  main's never_signaled, held_signal_survives_release, monotonic_shared_fence,
  teardown_wakes_cpu_waiters and concurrent_signalling.
- docs/d3d12_vulkan.md: main's text, with the keep-alive sentence corrected.
@0klavair

Copy link
Copy Markdown
Contributor Author

Updated: merged the refreshed #4740 (which now has current main merged in) into this branch, giving 55a0f523. The PR merges cleanly again. This PR's own change is still only d3d12_fence_timing_test.cpp.

Retested on RTX 5060, NVIDIA 621.01, Windows 11 23H2, with LEONIDA_VK_VALIDATION=1:

  • d3d12_fence_timing_test: exit 0. fence released after a postponed CPU Signal: B's batch ran 200/200 on the Vulkan layer and 200/200 on the host runtime.
  • d3d12_queue_handover_test: exit 0. Layer 595/595 and host 591/591, both exact, with no validation messages.

#4740's update explains the merge with main: main had started pinning waited-on fences, against the host's D3D-496 behaviour.

AI-assisted (Claude Code).

@DeSunShine

Copy link
Copy Markdown

Tested 55a0f523b merged into main 1cc6e3c12 on NVIDIA GeForce RTX 4090 (driver 621.24), Windows 11 Pro 26200, MSVC (VS Build Tools 18) + Ninja, Vulkan SDK 1.4.363: pass.

  • ctest -R "fence|queue_handover" --output-on-failure: 8/8 passed (d3d12_fence_timing, d3d12_fence_events, d3d12_fence_device_lost_counter, d3d12_fence_waiter, vk_fence_dispatcher_sync, d3d12_queue_handover, _two_queues, _shared_queue).

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.