Skip to content

runtime: track queued and CPU fence rewinds via monotonic queue order fence - #3771

Open
R0D4X wants to merge 1 commit into
R0Y00:mainfrom
R0D4X:runtime/queued-fence-rewind
Open

R0D4X wants to merge 1 commit into
R0Y00:mainfrom
R0D4X:runtime/queued-fence-rewind

Conversation

@R0D4X

@R0D4X R0D4X commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Fixes #3280
Refs #3279

Summary

Fixes queued and CPU fence rewinds in src/startup_stubs/d3d12_xs_device.cpp so the shim's GetCompletedValue() (slot 8) and polled completion word (slot 12) stay synchronized with the desktop ID3D12Fence without adding any lock to the read path:

  • Monotonic per-queue order fence (queue_signal_and_track): On async-tracked CommandQueue::Signal(fence, value) calls, pairs the guest queue->Signal(desktop_fence, value) under a per-queue SRWLOCK (queue_order_lock) with a signal on a strictly monotonic private queue_order_fence. The threadpool callback waits on queue_order_fence->SetEventOnCompletion(order_seq, event), so a queued rewind below the fence's current value never fires early while the queue is blocked.
  • Desktop-value publication under fence_lock (publish_fence_completed / fence_signal): Both CPU fence_signal and async signal_reached callbacks acquire the per-fence SRWLOCK (fence_lock), read the actual ID3D12Fence::GetCompletedValue(), and publish it via InterlockedExchange64(&fence->own().completed, ...). This handles arbitrary rewinds (including 64-bit values and device removal UINT64_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.
  • Read path unchanged: fence_completed_value (slot 8) and fence_completed_address (slot 12) remain lock-free reads of WrapperOwn::completed.
  • Module lifetime & cleanup: Async threadpool callbacks pin d3d12_xs.dll via GetModuleHandleExW / FreeLibraryWhenCallbackReturns and arm the threadpool wait before SetEventOnCompletion.

Tests

Added tests/startup_fence_rewind.cpp and tests/cmake/startup_fence_rewind.cmake (registered in CMakePresets.json pr preset as startup_fence_rewind_warp), covering:

  1. CPU Signal() rewinds including 64-bit values (0x100000009 -> 9 -> 2 -> 0 -> 7) on both slot 8 and slot 12.
  2. Queued rewind while blocked on a separate gate fence (7, Wait(gate, 1), Signal(target, 3), Signal(marker, 1)): verifies target stays 7 while blocked, then reaches 3 on both the polled completion word and GetCompletedValue() after unblock.
  3. Queued advance after rewind (Signal(target, 11)).
  4. Mixed queue-then-CPU rewind (queued Signal(20), then CPU Signal(4)).
  5. Concurrent CPU Signal() calls from two threads.

Verification

Environment: Windows 11 x64 (10.0.26300.9974), MSVC 19.44.35230, Windows SDK / DXC 10.0.26100.0, NVIDIA GeForce RTX 5080 (10de:2c02, driver 32.0.16.2140), Microsoft Basic Render Driver (10.0.26100.9818), D3D12 debug layer enabled (X2W_D3D12_DEBUG=1).

Commit under test: 6668d4c9.

ctest --test-dir out/build/dev --no-tests=error -j 1 -V -R "^startup_(fence_rewind|queue_wait)_(warp|hardware)$"
ctest --test-dir out/build/release --no-tests=error -j 1 -V -R "^startup_(fence_rewind|queue_wait)_(warp|hardware)$"
  • dev: 4/4 passed (0 skipped), WARP + RTX 5080.
  • release: 4/4 passed (0 skipped), WARP + RTX 5080.
  • Mutation check: Temporarily replacing order_fence->SetEventOnCompletion(order_seq, event) with desktop_fence->SetEventOnCompletion(value, event) caused startup_fence_rewind_warp to fail (queue Signal(3) after unblock: method=7 address=7); after reverting src/, 4/4 passed again.

No game files, dumps, or private paths included.

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.

Queued fence rewind leaves the completion word at the previous higher value

1 participant