runtime: track queued and CPU fence rewinds via monotonic queue order fence #3773
No reviewers
Labels
No labels
accessibility
awaiting author
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
priority: P0
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/R0Y00--Xbox2Windows!3773
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3771"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@R0D4X on GitHub · 2026-12-01 17:53 UTC · original
R0D4X:runtime/queued-fence-rewind→main· +676 −55 · 4 files · 1 commits · openFixes #3282
Refs #3281
Summary
Fixes queued and CPU fence rewinds in
src/startup_stubs/d3d12_xs_device.cppso the shim'sGetCompletedValue()(slot 8) and polled completion word (slot 12) stay synchronized with the desktopID3D12Fencewithout adding any lock to the read path:queue_signal_and_track): On async-trackedCommandQueue::Signal(fence, value)calls, pairs the guestqueue->Signal(desktop_fence, value)under a per-queueSRWLOCK(queue_order_lock) with a signal on a strictly monotonic privatequeue_order_fence. The threadpool callback waits onqueue_order_fence->SetEventOnCompletion(order_seq, event), so a queued rewind below the fence's current value never fires early while the queue is blocked.fence_lock(publish_fence_completed/fence_signal): Both CPUfence_signaland asyncsignal_reachedcallbacks acquire the per-fenceSRWLOCK(fence_lock), read the actualID3D12Fence::GetCompletedValue(), and publish it viaInterlockedExchange64(&fence->own().completed, ...). This handles arbitrary rewinds (including 64-bit values and device removalUINT64_MAX), prevents two concurrent CPU callers from overwriting a newer value with a stale one, and prevents delayed queue callbacks from overwriting a newer CPU rewind.fence_completed_value(slot 8) andfence_completed_address(slot 12) remain lock-free reads ofWrapperOwn::completed.d3d12_xs.dllviaGetModuleHandleExW/FreeLibraryWhenCallbackReturnsand arm the threadpool wait beforeSetEventOnCompletion.Tests
Added
tests/startup_fence_rewind.cppandtests/cmake/startup_fence_rewind.cmake(registered inCMakePresets.jsonprpreset asstartup_fence_rewind_warp), covering:Signal()rewinds including 64-bit values (0x100000009 -> 9 -> 2 -> 0 -> 7) on both slot 8 and slot 12.7,Wait(gate, 1),Signal(target, 3),Signal(marker, 1)): verifiestargetstays7while blocked, then reaches3on both the polled completion word andGetCompletedValue()after unblock.Signal(target, 11)).queued Signal(20), thenCPU Signal(4)).Signal()calls from two threads.Verification
Environment: Windows 11 x64 (
10.0.26300.9974), MSVC19.44.35230, Windows SDK / DXC10.0.26100.0, NVIDIA GeForce RTX 5080 (10de:2c02, driver32.0.16.2140), Microsoft Basic Render Driver (10.0.26100.9818), D3D12 debug layer enabled (X2W_D3D12_DEBUG=1).Commit under test:
6668d4c9.dev: 4/4 passed (0 skipped), WARP + RTX 5080.release: 4/4 passed (0 skipped), WARP + RTX 5080.order_fence->SetEventOnCompletion(order_seq, event)withdesktop_fence->SetEventOnCompletion(value, event)causedstartup_fence_rewind_warpto fail (queue Signal(3) after unblock: method=7 address=7); after revertingsrc/, 4/4 passed again.No game files, dumps, or private paths included.
@R0Y00 on GitHub · 2026-12-03 01:08 UTC · original · requested changes
In queue_signal_and_track, the guest Signal is submitted before completion tracking is ready. If creating the private fence, signaling it, allocating the event, or registering SetEventOnCompletion fails, the fallback takes a one-time desktop sample and returns the original success. If the queue is behind a gate when that happens, the sample can be the old value and nothing later updates the slot-12 word after the signal actually runs. Please preserve completion tracking on a recoverable failure, or surface a failure rather than returning success with no pending publisher. Add a source-built control that forces a setup failure while the queue is blocked and checks the slot-12 value once the gate opens. This is from the code review. The trial merge also conflicts in CMakePresets.json.
@R0Y00 on GitHub · 2026-12-03 02:52 UTC · original
I reproduced the earlier completion-tracking issue on
6668d4c9. MSVC Debug on Windows 11 build 26300, debug layer enabled: the unmodified queue-wait and fence-rewind cases passed 4/4 on WARP and RTX 4060 Laptop.For the local fault injection, I changed only queue_signal_and_track's event creation to "value == 3 ? nullptr : CreateEventW(nullptr, FALSE, FALSE, nullptr)". It leaves the marker's own tracking active. On both adapters the guest queue Signal(3) returned S_OK and the later queue marker completed after the gate opened. The slot-12 value and GetCompletedValue both still reported 7 after the fixture's five-second poll. Restoring event creation made both rewind tests pass again. This pins the failure path without relying on memory pressure.
6668d4c975toaa235761dd@R0D4X on GitHub · 2026-12-03 14:24 UTC · original
Rebased onto
upstream/main(e7277aa4, resolvingCMakePresets.json) and fixedqueue_signal_and_trackinaa235761:Signal(src/startup_stubs/d3d12_xs_device.cpp):CreateEventW,PendingSignal,GetModuleHandleExW,CreateThreadpoolWait, lazyCreateFence(queue_order_fence),SetThreadpoolWait, andorder_fence->SetEventOnCompletion(order_seq, event)are all set up and armed before callingqueue->Signal(desktop_fence, value).Signalis never submitted andqueue_signal_and_trackreturns the failingHRESULT(E_OUTOFMEMORYor the failing D3D12hr) instead of returningS_OKwith no pending slot-12 publisher.queue->Signal(order_fence, order_seq)fails after arming, that failureHRESULTis also surfaced instead of returningS_OK.tests/startup_fence_rewind.cpp):X2W_TEST_FENCE_TRACK_FAIL_VALUE=3(which forcesCreateEventWto returnnullptrforvalue == 3inqueue_signal_and_track):gate3,queue->Signal(fault_target, 3)returnsE_OUTOFMEMORY(0x8007000e), notS_OK.gate3and waiting formarker3, both slot 12 andGetCompletedValue()remain7(consistent with the rejected signal).gate4,Signal(fault_target, 3),marker4) without the fault returnsS_OKand publishes3to both slot 12 andGetCompletedValue().Verification (
aa235761)10.0.26300.9974), MSVC19.44.35230, Windows SDK / DXC10.0.26100.0, NVIDIA GeForce RTX 5080 (10de:2c02, driver32.0.16.2140), Microsoft Basic Render Driver (10.0.26100.9818), D3D12 debug layer enabled.ctest --preset dev --no-tests=error -R '^startup_(fence_rewind|queue_wait)_(warp|hardware)$' -V: 4/4 passed (0 skips).ctest --preset release --no-tests=error -R '^startup_fence_rewind_(warp|hardware)$' -V: 2/2 passed (0 skips).tests/standin_exports_check.py: 480 cases, 6/6 mutations killed PASS.@R0Y00 on GitHub · 2026-12-03 14:43 UTC · original
A trial merge of
aa235761into mainadfc0d94finds one remaining conflict in CMakePresets.json. The queue/fence runtime source merges cleanly. Please retain main's startup_indirect_translated_indexed_snapshot_warp selector from #3937 and add the rewind controls on top. Checkctest --preset pr -Naccepts the resulting selector, then rerun the new fault-injection controls.aa235761ddto8aebb0f59f@R0D4X on GitHub · 2026-12-03 15:01 UTC · original
Rebased onto
upstream/main(b78e10df) at8aebb0f5, resolvingCMakePresets.jsonwhile keeping bothstartup_indirect_translated_indexed_snapshot_warp(#3937) andstartup_fence_rewind_warp.Verification (
8aebb0f5)10.0.26300.9974), MSVC19.44.35230(Debug), Windows SDK / DXC10.0.26100.0, NVIDIA GeForce RTX 5080 (10de:2c02, driver32.0.16.2140), Microsoft Basic Render Driver (10.0.26100.9818), D3D12 debug layer enabled.ctest --preset pr -N: accepted the selector with bothstartup_fence_rewind_warpandstartup_indirect_translated_indexed_snapshot_warp.ctest --preset dev --no-tests=error -R '^startup_(fence_rewind|queue_wait)_(warp|hardware)$' -V: 4/4 passed (0 skips; includes fault-injectedhr=0x8007000e method=7 address=7and retrymethod=3 address=3on both WARP and RTX 5080).8aebb0f59ftod9a1adb2f7@R0Y00 on GitHub · 2026-12-03 15:24 UTC · original · requested changes
Independent MSVC Debug run on
d9a1adb2: both WARP and RTX passed the fence rewind and queue wait cases (4/4). The injected setup error returned E_OUTOFMEMORY while the method and cached address stayed at 7, and the retry published 3. The PR preset also retained the index snapshot and draw report selectors.There is still a lifetime ordering issue in queue_signal_and_track. The registered fence event can dispatch signal_reached before initialization of pending->order_fence is complete. Put the callback-owned state and reference in place before enabling callbacks, and make registration failure clean up without using state the callback may already own. Also define the cleanup when the order-fence Signal fails after arming the wait. The current setup-failure test exercises the earlier allocation path.
@R0D4X on GitHub · 2026-12-03 16:00 UTC · original
Fixed the lifetime ordering and defined post-arm cleanup in
33230083:src/startup_stubs/d3d12_xs_device.cpp):order_fence->AddRef()andpending->order_fence = order_fenceare now set beforeSetEventOnCompletionand beforeSetThreadpoolWait.order_fence->SetEventOnCompletion(order_seq, event)is called beforeSetThreadpoolWait(wait, event, nullptr): if registration fails, no threadpool callback was ever enabled, so cleanup owns and releasespending->order_fence,wait,module,pending, andeventexclusively without touching callback-owned state.SetEventOnCompletionsucceeds andSetThreadpoolWait(wait, event, nullptr)is called, ownership ofwait/pending/order_fence/module/eventtransfers tosignal_reached. Ifqueue->Signal(order_fence, order_seq)fails after arming,queue_signal_and_tracknever touches callback-owned state and surfacesorder_hrto the caller;signal_reachedreleases everything whenorder_fenceadvances pastorder_seq(on a subsequent queue signal or device removal).tests/startup_fence_rewind.cpp):X2W_TEST_FENCE_TRACK_FAIL=event:3(event allocation failure): returnsE_OUTOFMEMORY, method and slot-12 stay7.X2W_TEST_FENCE_TRACK_FAIL=register:3(SetEventOnCompletionregistration failure): returnsE_FAIL, cleans up without enabling callback, method and slot-12 stay7.X2W_TEST_FENCE_TRACK_FAIL=order_signal:3(order-fenceSignalfailure after arming): returnsE_FAIL, and once the subsequentmarkersignal advances the order fence after unblock,signal_reachedpublishes3and cleans up cleanly.S_OKand publishes3.Verification (
33230083)10.0.26300.9974), MSVC19.44.35230, Windows SDK / DXC10.0.26100.0, NVIDIA GeForce RTX 5080 (10de:2c02, driver32.0.16.2140), Microsoft Basic Render Driver (10.0.26100.9818), D3D12 debug layer enabled.ctest --preset dev --no-tests=error -R '^startup_(fence_rewind|queue_wait)_(warp|hardware)$' -V: 4/4 passed (0 skips).ctest --preset release --no-tests=error -R '^startup_fence_rewind_(warp|hardware)$' -V: 2/2 passed (0 skips).@R0Y00 on GitHub · 2026-12-03 16:01 UTC · original · requested changes
Re-tested
33230083in MSVC Debug on Windows 11 build 26300, WARP and RTX 4060 Laptop. The rewind and queue-wait fixtures passed 4/4. Both pre-submission failures left method/address at 7, and the final-order-signal case returned E_FAIL and published 3 after the fixture submitted a later marker. The callback-owned fence reference is now initialized before registration and the threadpool wait, addressing the earlier ordering finding.One cleanup case is still open. After the final private Signal fails, the wait and retained references are left until a later signal advances the private fence or the device is removed. The existing fixture always submits another signal. Please define how pending ownership is retired when the failed signal is the last one on an otherwise healthy queue, including what happens at teardown. The latest main trial merge also needs to reconcile CMakePresets.json while retaining current main's test selectors.
3323008346to934c6db14d@R0Y00 on GitHub · 2026-12-03 16:40 UTC · original
Independent MSVC Debug retest on the rebased head
934c6db1passed all four rewind and queue-wait cases on WARP and RTX 4060 Laptop. The merge tree against current main is clean and required Windows CI passed. The final private-order failure case still submits a later marker, which advances the order fence and releases the pending wait. The earlier review request remains for the case with no later Signal on an otherwise healthy queue, including cleanup at teardown.@R0D4X on GitHub · 2026-12-03 16:56 UTC · original
Addressed in
6a97a527:order_signal/registerfailure (src/startup_stubs/d3d12_xs_device.cpp):queue_signal_and_tracknow runsqueue->Signal(desktop_fence, value)->queue->Signal(order_fence, order_seq)->order_fence->SetEventOnCompletion(order_seq, event)->SetThreadpoolWait(wait, event, nullptr)while still populatingpending->order_fencebefore any signal or registration. If eitherqueue->Signal(order_fence, order_seq)orSetEventOnCompletionfails, no event registration or threadpool wait is armed and cleanup runs synchronously inqueue_signal_and_track(pending->order_fence->Release(),CloseThreadpoolWait,FreeLibrary,delete pending,CloseHandle), even when the failed signal is the last one on an otherwise healthy queue.src/startup_stubs/d3d12_xs_device.cpp):release()now acquiresqueue_order_lock, releasesqueue_order_fence, and clears it tonullptrwhen aqueue_kindwrapper drops to refcount0.tests/startup_fence_rewind.cpp): steps6b(order_signal:3) and6c(register:3) each create a dedicatedisolated_queue, inject the fault on its finalSignal, unblock the gate, and releaseisolated_queuewith no subsequent signal submitted on that queue.Verification (commit
6a97a527/ pre-commit identical tree)Environment: Windows 11 x64 (
10.0.26300.9974), MSVC19.44.35230, Windows SDK / DXC10.0.26100.0, D3D12 debug layer enabled (X2W_D3D12_DEBUG=1), WARP (Microsoft Basic Render Driver,10.0.26100.9818), NVIDIA GeForce RTX 5080 (10de:2c02, driver32.0.16.2140).ctest --preset dev --no-tests=error -R '^startup_(fence_rewind|queue_wait)_(warp|hardware)$' -V: 4/4 passed, 0 skipped (startup_queue_wait_warp,startup_queue_wait_hardware,startup_fence_rewind_warp,startup_fence_rewind_hardware).ctest --preset release --no-tests=error -R '^startup_fence_rewind_(warp|hardware)$' -V: 2/2 passed, 0 skipped (startup_fence_rewind_warp,startup_fence_rewind_hardware).python -B tests/standin_exports_check.py: PASS (481 cases, 6/6 mutations killed PASS).@R0Y00 on GitHub · 2026-12-03 17:21 UTC · original · requested changes
On
6a97a527, MSVC Debug WARP and RTX passed 4/4 and printed both final-signal teardown cases. The new order fixes the abandoned-wait ownership case. It also submits queue->Signal(desktop_fence, value) before order signalling or event registration can fail. In either later failure it returns an error and frees the sole publisher while leaving the guest fence signal in the queue. The new teardown fixture no longer checks the method and cached slot-12 value after the gate opens for these two cases. Please define and test whether a failed guest signal remains unapplied or retains a completion publisher, and verify that the two reads agree after the queue executes. A later retry can otherwise hide the original result.@R0D4X on GitHub · 2026-12-03 17:50 UTC · original · edited 2026-12-03 17:59 UTC
Addressed in
a6beab9a(rebased ontomaind87f8455/#3942, preservingdrain_copied_uav_writebacks(),drain_list_timestamps(),trace_fence_progress(),ListMemoryQueueOrder,linear_queue_order(),fence_completed_address(Wrapper*), and all currentmainCMakePresets.jsontargets/selectors):Explicit failure contract (
src/startup_stubs/d3d12_xs_device.cpp)event/PendingSignal/module/waitallocation,order_fencecreation, andorder_fence->SetEventOnCompletionregistration):SetEventOnCompletion(order_seq, event)is registered beforequeue->Signal(desktop_fence, value). Any failure here leaves the guestSignalunapplied, arms no threadpool wait, and retires all resources synchronously.queue->Signal(order_fence, order_seq)fails afterqueue->Signal(desktop_fence, value)already succeeded): Because the guest signal is already queued on the desktop command queue,queue_signal_and_tracksurfaces the failingHRESULT, detachesqueue_order_fencefrom the queue (queue_order_fence = nullptr; queue_order_next = 0;), and retains a fallback completion publisher using a 1 ms timedSetThreadpoolWaiton the detachedorder_fence.refs == 0), the callback drains UAV writebacks and list timestamps, publishes the live desktop value to slot 12, and cleanly retires its detachedorder_fenceregistration (order_fence->Signal(order_seq)+WaitForSingleObject(event)once prior< order_seqsignals have completed) before releasingorder_fence, closingevent, deletingpending, closingwait, and freeingmodule.fence_completed_value(slot 8) now readsID3D12Fence::GetCompletedValue()directly in render mode or once a fence has been tracked asynchronously (fence_async_seen), so comparing slot 8 against slot 12 genuinely cross-checks live desktop GPU fence state against the cached slot-12 completion word.Fixture coverage (
tests/startup_fence_rewind.cpp)Each failure case uses its own dedicated fence so no later retry can mask an earlier result, and verifies both slot 8 (
method) and slot 12 (address) agree after execution and teardown:6a(event:3): guest signal unapplied -> after gate opens & marker drains:hr=0x8007000e method=7 address=7.6b(register:3, final signal on isolated queue, no later signal): guest signal unapplied -> after gate opens and across queue teardown:hr=0x80004005 method=7 address=7.6c(order_signal:3, final signal on isolated queue, no later signal): guest signal already queued -> stays7 / 7while blocked; after gate opens with no later signal, retained fallback publisher updates slot 12 to3agreeing with live slot 8 (hr=0x80004005 method=3 address=3), then tears down cleanly.6d: clean retry onfence_6bwithout fault injection ->method=3 address=3.6e: queue reuse afterorder_signal:3detachment lazily creates a freshqueue_order_fenceand tracksSignal(11)cleanly ->method=11 address=11.Verification (commit
a6beab9a)Environment: Windows 11 x64 (
10.0.26300.9974), MSVC19.44.35230, Windows SDK / DXC10.0.26100.0, D3D12 debug layer enabled (X2W_D3D12_DEBUG=1), WARP (Microsoft Basic Render Driver,10.0.26100.9818), NVIDIA GeForce RTX 5080 (10de:2c02, driver32.0.16.2140).ctest --preset dev --no-tests=error -R '^(startup_(fence_rewind|queue_wait)_(warp|hardware)|startup_list_resubmit_warp|startup_clear_(uav|depth_stencil)_warp|startup_list_copy_warp)$' -V: 8/8 passed, 0 skipped.ctest --preset release --no-tests=error -R '^startup_(fence_rewind|queue_wait)_(warp|hardware)$' -V: 4/4 passed, 0 skipped.python -B tests/standin_exports_check.py: PASS (513 cases, 6/6 mutations killed PASS).6a97a52739toa6beab9ad3View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.