Skip to content

Runtime: keep a released fence's semaphore until the GPU is done with it (D3D-486) - #4663

Open
requiemsnkrp wants to merge 1 commit into
ItsNotRexed:mainfrom
requiemsnkrp:d3d-486-fence-semaphore-retire
Open

requiemsnkrp wants to merge 1 commit into
ItsNotRexed:mainfrom
requiemsnkrp:d3d-486-fence-semaphore-retire

Conversation

@requiemsnkrp

Copy link
Copy Markdown

What and why

D3D-486 (claim, my finding). VulkanFence destroyed its timeline semaphore at release even while queued Signal/Wait submissions still referenced it — legal in D3D12 (an app may release a fence right after queue->Signal; vkd3d-proton keeps a private reference).

Change

  • vulkan_backend.h: new deletion-queue kind semaphore. ~VulkanFence calls remove_fence, then hands the semaphore to the deletion queue: destroyed once every submission made before the release has completed. VulkanQueue::discard_waiting_on also forgets the fence's semaphore: a Wait not yet attached to a batch counts as reached, a held Signal of it signals nothing; held batches waiting on it are still dropped and reported as before.
  • queue_holds.h: remove_fence asks every queue, not only when something is held (a pending Wait also names the fence).
  • Docs: docs/d3d12_vulkan.md §4.4.

Tested (NVIDIA GeForce RTX 3060, Windows 11, MSVC 19.51, Vulkan SDK 1.4.363.0, LEONIDA_VK_VALIDATION=1 + sync validation)

New case in d3d12_queue_handover_test, on the layer and on host D3D12. A COPY queue runs a ~300 ms GPU gate before each Signal; each fence is released right away. A: a Signal and a DIRECT Wait in flight; B: a Wait not yet attached to a batch; C: a Signal held behind a CPU-gated batch.

fences released while in use (gate of 80 copies): A Signal and Wait in flight, B Wait not yet attached, C held Signal: every readback exact
Vulkan layer: 374 cases passed, every one exact
Host D3D12 (NVIDIA GeForce RTX 3060): 372 cases passed, every one exact
  • 0 validation errors. Negative control (main's behaviour restored): 4× VUID-vkDestroySemaphore-semaphore-05149 in A and B, 2× VUID-VkSubmitInfo-pWaitSemaphores-parameter in B, then an access violation (exit -1073741819) before C.
  • Full ctest (no window tests): 141/141, same as main (the case lives inside d3d12_queue_handover). 67 queue/fence/vk_/d3d12_ tests under validation: 0 errors.
  • Difference from the host: in B the layer runs the batch right away; the host still waits for the (released) fence. Same data.

Merging with neighbours

No game files. AI-assisted (Claude).

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

Copy link
Copy Markdown
Contributor

Tested at 0cc958d0 on Windows 11 with AMD Radeon RX 6700 XT (RDNA2, driver 26.11.2, Vulkan validation enabled via LEONIDA_VK_VALIDATION=1).

Summary

  • Build ([27/27] across 9 targets: retire_tickets_test, vk_queue_query_sync_test, vk_fence_dispatcher_sync_test, vk_queue_test, d3d12_queue_wait_test, d3d12_queue_handover_test, d3d12_fence_timing_test, d3d12_fence_events_test, d3d12_fence_waiter_test): exit 0.
  • ctest -R "^(retire_tickets|vk_queue_query_sync|vk_fence_dispatcher_sync|d3d12_queue_wait|d3d12_queue_handover|d3d12_fence_timing|d3d12_fence_events|d3d12_fence_waiter)$":
    • Standard: 8/8 passed (13.32 sec real).
    • LEONIDA_VK_VALIDATION=1: 8/8 passed (11.74 sec real).
  • Direct d3d12_queue_handover_test.exe (LEONIDA_VK_VALIDATION=1): exit 0 with 0 validation errors:
    • Vulkan layer: fences released while in use (gate of 47 copies): A Signal and Wait in flight, B Wait not yet attached, C held Signal: every readback exact (374 cases passed, every one exact).
    • Host D3D12 (AMD Radeon RX 6700 XT): fences released while in use (gate of 110 copies): ... every readback exact (372 cases passed, every one exact).
  • Direct vk_queue_test.exe (LEONIDA_VK_VALIDATION=1): exit 0.
  • Audited runtime/src/queue_holds.h & runtime/src/vulkan_backend.h: remove_fence drops the held_ == 0 short-circuit so discard_waiting_on always runs under pump_mutex_ + state_mutex_ before defer_destroy(Kind::semaphore, semaphore_), clearing unattached waits_/wait_values_ and nulling op.fence on held signals (submit() skips VK_NULL_HANDLE semaphore signals while still recording the queue's progress_ ticket).
  • tests/test_ctest_registration.py (4 tests OK, 0.499s), tests/test_docs_*.py (20 tests OK, 19.039s), scripts/check_no_game_data.py (exit 0), git diff --check (exit 0).

AI-assisted (Gemini, Antigravity).

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

Reviewed head 0cc958d. The semaphore now retires behind the global submission tickets after queue-held references are removed; the change covers pending waits, held signals, and already-submitted work. The three-case handover regression and reported validation runs exercise the relevant lifetime paths. I found no additional defect in this diff.

@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_queue_handover_test at 0cc958d0f on Windows 11, RTX 5070 Ti: exit 0.

  • fences released while in use (gate of 199 copies): A Signal and Wait in flight, B Wait not yet attached, C held Signal: every readback exact
  • never-signaled fence: held, others unaffected, destroyed without a hang; ...
  • Vulkan layer 374 cases, every one exact; Host D3D12 372 cases, every one exact.

The semaphore now goes through DeferredDeletionQueue::Kind::semaphore after remove_fence, remove_fence asks every queue even when held_ == 0 (a detached Wait still names the fence), and discard_waiting_on erases attached waits and neutralises held Signals of it. The new scenario covers all three uses. git diff --check clean.

Overlap: #4681 makes nearly the same deferred-semaphore change in vulkan_backend.h with a smaller test; the two conflict (git merge-tree refs/remotes/pr/4663 refs/remotes/pr/4681 -> conflict in runtime/src/vulkan_backend.h). This PR is earlier and also covers the queue-holds side, so it is the better base. Merge matrix: clean with #4557 and #4607; conflicts with #4570 (queue_holds.h), #4577 (docs/d3d12_vulkan.md, d3d12_queue_handover_test.cpp) and #4681 (vulkan_backend.h).

@gut0to

gut0to commented Nov 23, 2026

Copy link
Copy Markdown

Tested 0cc958d0f728ce9b093e65cb5ae6c43727bdf1b8 on Windows 11, AMD Radeon RX 6600, driver 26.11.2 (Vulkan 1.4.315): pass.

  • Built d3d12_queue_handover_test with MSVC 19.44 / RelWithDebInfo.
  • ctest -R ^d3d12_queue_handover$: 1/1 passed.
  • Direct run: the new “fences released while in use” case passed on both the Vulkan layer (374 cases, every readback exact) and host D3D12 (372 cases, every readback exact); the new case used gates of 22 and 29 copies respectively.
  • VK_LOADER_LAYERS_DISABLE=~implicit~ used to avoid injected overlay layers. VK_LAYER_KHRONOS_validation is not installed here, so synchronization validation was not run.

No game files used.

@Exopus-0x65f7ad0

Copy link
Copy Markdown

Tested 0cc958d0 with a local release-race probe (in d3d12_fence_timing_test, not pushed; details on #4282), RTX 5060 Ti / 621.14, Windows 11, MSVC 14.51, LEONIDA_VK_VALIDATION=1:

  • Pre-existing case on main: queue B Wait(F, 2), CPU F->Signal(2), F->Release() right away, 200 iterations. main 7b7d73f8 gives VUID-vkDestroySemaphore-semaphore-05149 in 3/3 runs. With this PR: 200/200 batches run, 0 VUIDs in 3/3 runs.
  • With runtime: notify QueueHolds when a held CPU Fence::Signal fires #4282 merged on top (its postponed CPU signal now pumps the holds, which on its own gives 05149 and 2/3 crashing runs): 0 VUIDs and no crash in 10/10 runs (5 gated + 5 immediate).
  • Host D3D12 (same GPU, debug layer) agrees that this release order is legal: the consumer runs, there are 0 debug-layer messages, and the queue takes no COM reference on the fence. So retiring the semaphore after the GPU, as here, is the behaviour to match.

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

@Exopus-0x65f7ad0

Copy link
Copy Markdown

Tested 0cc958d0 on Windows 11 (26300), RTX 5060 Ti / 621.14, MSVC 14.51 (VS 2026), Vulkan SDK 1.4.363.0, RelWithDebInfo, clean build with -DLEONIDA_LLVM_MC (translated-shader D3D12 gate tests included), full LEONIDA_VK_VALIDATION=1 ctest -j4: 145/145 passed, 0 VUIDs, 0 build errors. AI-assisted (Claude Code).

0klavair added a commit to 0klavair/Leonida that referenced this pull request Nov 23, 2026
…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.
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.

6 participants