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

Open
github-bot wants to merge 1 commit from github-bot/gh-3771 into main AGit
Owner

@⁠R0D4X on GitHub · 2026-12-01 17:53 UTC · original
R0D4X:runtime/queued-fence-rewind → main · +676 −55 · 4 files · 1 commits · open

Fixes #3282
Refs #3281

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.

**@⁠R0D4X** on GitHub · 2026-12-01 17:53 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3771) `R0D4X:runtime/queued-fence-rewind` → `main` · +676 −55 · 4 files · 1 commits · open Fixes #3282 Refs #3281 ## 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`. ```powershell 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 17:58:20 +00:00
Author
Owner

@⁠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 01:08 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3771#pullrequestreview-6161934015) · **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.
Author
Owner

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

**@⁠R0Y00** on GitHub · 2026-12-03 02:52 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3771#issuecomment-7365945721) 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.
github-bot force-pushed github-bot/gh-3771 from 6668d4c975 to aa235761dd 2026-12-03 14:32:41 +00:00 Compare
Author
Owner

@⁠R0D4X on GitHub · 2026-12-03 14:24 UTC · original

Rebased onto upstream/main (e7277aa4, resolving CMakePresets.json) and fixed queue_signal_and_track in aa235761:

  1. Arm completion tracking before submitting guest Signal (src/startup_stubs/d3d12_xs_device.cpp):
    • CreateEventW, PendingSignal, GetModuleHandleExW, CreateThreadpoolWait, lazy CreateFence(queue_order_fence), SetThreadpoolWait, and order_fence->SetEventOnCompletion(order_seq, event) are all set up and armed before calling queue->Signal(desktop_fence, value).
    • If any setup step fails, the guest Signal is never submitted and queue_signal_and_track returns the failing HRESULT (E_OUTOFMEMORY or the failing D3D12 hr) instead of returning S_OK with no pending slot-12 publisher.
    • If queue->Signal(order_fence, order_seq) fails after arming, that failure HRESULT is also surfaced instead of returning S_OK.
  2. Source-built blocked-queue fault-injection control (tests/startup_fence_rewind.cpp):
    • Added step 6 using X2W_TEST_FENCE_TRACK_FAIL_VALUE=3 (which forces CreateEventW to return nullptr for value == 3 in queue_signal_and_track):
      • With the queue blocked on gate3, queue->Signal(fault_target, 3) returns E_OUTOFMEMORY (0x8007000e), not S_OK.
      • After unblocking gate3 and waiting for marker3, both slot 12 and GetCompletedValue() remain 7 (consistent with the rejected signal).
      • Retrying the blocked rewind (gate4, Signal(fault_target, 3), marker4) without the fault returns S_OK and publishes 3 to both slot 12 and GetCompletedValue().

Verification (aa235761)

  • 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.
  • 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.
**@⁠R0D4X** on GitHub · 2026-12-03 14:24 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3771#issuecomment-7381403923) Rebased onto `upstream/main` (`e7277aa4`, resolving `CMakePresets.json`) and fixed `queue_signal_and_track` in `aa235761`: 1. **Arm completion tracking before submitting guest `Signal` (`src/startup_stubs/d3d12_xs_device.cpp`):** - `CreateEventW`, `PendingSignal`, `GetModuleHandleExW`, `CreateThreadpoolWait`, lazy `CreateFence(queue_order_fence)`, `SetThreadpoolWait`, and `order_fence->SetEventOnCompletion(order_seq, event)` are all set up and armed *before* calling `queue->Signal(desktop_fence, value)`. - If any setup step fails, the guest `Signal` is never submitted and `queue_signal_and_track` returns the failing `HRESULT` (`E_OUTOFMEMORY` or the failing D3D12 `hr`) instead of returning `S_OK` with no pending slot-12 publisher. - If `queue->Signal(order_fence, order_seq)` fails after arming, that failure `HRESULT` is also surfaced instead of returning `S_OK`. 2. **Source-built blocked-queue fault-injection control (`tests/startup_fence_rewind.cpp`):** - Added step 6 using `X2W_TEST_FENCE_TRACK_FAIL_VALUE=3` (which forces `CreateEventW` to return `nullptr` for `value == 3` in `queue_signal_and_track`): - With the queue blocked on `gate3`, `queue->Signal(fault_target, 3)` returns `E_OUTOFMEMORY` (`0x8007000e`), not `S_OK`. - After unblocking `gate3` and waiting for `marker3`, both slot 12 and `GetCompletedValue()` remain `7` (consistent with the rejected signal). - Retrying the blocked rewind (`gate4`, `Signal(fault_target, 3)`, `marker4`) without the fault returns `S_OK` and publishes `3` to both slot 12 and `GetCompletedValue()`. ### Verification (`aa235761`) - **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. - `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**.
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-03 14:43 UTC · original

A trial merge of aa235761 into main adfc0d94 finds 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. Check ctest --preset pr -N accepts the resulting selector, then rerun the new fault-injection controls.

**@⁠R0Y00** on GitHub · 2026-12-03 14:43 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3771#issuecomment-7381922875) A trial merge of aa235761 into main adfc0d94 finds 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. Check `ctest --preset pr -N` accepts the resulting selector, then rerun the new fault-injection controls.
github-bot force-pushed github-bot/gh-3771 from aa235761dd to 8aebb0f59f 2026-12-03 15:01:28 +00:00 Compare
Author
Owner

@⁠R0D4X on GitHub · 2026-12-03 15:01 UTC · original

Rebased onto upstream/main (b78e10df) at 8aebb0f5, resolving CMakePresets.json while keeping both startup_indirect_translated_indexed_snapshot_warp (#3937) and startup_fence_rewind_warp.

Verification (8aebb0f5)

  • Environment: Windows 11 x64 (10.0.26300.9974), MSVC 19.44.35230 (Debug), 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.
  • ctest --preset pr -N: accepted the selector with both startup_fence_rewind_warp and startup_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-injected hr=0x8007000e method=7 address=7 and retry method=3 address=3 on both WARP and RTX 5080).
**@⁠R0D4X** on GitHub · 2026-12-03 15:01 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3771#issuecomment-7382446349) Rebased onto `upstream/main` (`b78e10df`) at `8aebb0f5`, resolving `CMakePresets.json` while keeping both `startup_indirect_translated_indexed_snapshot_warp` (#3937) and `startup_fence_rewind_warp`. ### Verification (`8aebb0f5`) - **Environment:** Windows 11 x64 (`10.0.26300.9974`), MSVC `19.44.35230` (Debug), 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. - `ctest --preset pr -N`: accepted the selector with both `startup_fence_rewind_warp` and `startup_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-injected `hr=0x8007000e method=7 address=7` and retry `method=3 address=3` on both WARP and RTX 5080).
github-bot force-pushed github-bot/gh-3771 from 8aebb0f59f to d9a1adb2f7 2026-12-03 15:16:28 +00:00 Compare
Author
Owner

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

**@⁠R0Y00** on GitHub · 2026-12-03 15:24 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3771#pullrequestreview-6172806961) · **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.
Author
Owner

@⁠R0D4X on GitHub · 2026-12-03 16:00 UTC · original

Fixed the lifetime ordering and defined post-arm cleanup in 33230083:

  1. Initialize all callback-owned state before registration or enabling callbacks (src/startup_stubs/d3d12_xs_device.cpp):
    • order_fence->AddRef() and pending->order_fence = order_fence are now set before SetEventOnCompletion and before SetThreadpoolWait.
    • order_fence->SetEventOnCompletion(order_seq, event) is called before SetThreadpoolWait(wait, event, nullptr): if registration fails, no threadpool callback was ever enabled, so cleanup owns and releases pending->order_fence, wait, module, pending, and event exclusively without touching callback-owned state.
    • Once SetEventOnCompletion succeeds and SetThreadpoolWait(wait, event, nullptr) is called, ownership of wait/pending/order_fence/module/event transfers to signal_reached. If queue->Signal(order_fence, order_seq) fails after arming, queue_signal_and_track never touches callback-owned state and surfaces order_hr to the caller; signal_reached releases everything when order_fence advances past order_seq (on a subsequent queue signal or device removal).
  2. Expanded fault-injection controls (tests/startup_fence_rewind.cpp):
    • X2W_TEST_FENCE_TRACK_FAIL=event:3 (event allocation failure): returns E_OUTOFMEMORY, method and slot-12 stay 7.
    • X2W_TEST_FENCE_TRACK_FAIL=register:3 (SetEventOnCompletion registration failure): returns E_FAIL, cleans up without enabling callback, method and slot-12 stay 7.
    • X2W_TEST_FENCE_TRACK_FAIL=order_signal:3 (order-fence Signal failure after arming): returns E_FAIL, and once the subsequent marker signal advances the order fence after unblock, signal_reached publishes 3 and cleans up cleanly.
    • Clean retry without fault injection: returns S_OK and publishes 3.

Verification (33230083)

  • 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.
  • 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).
**@⁠R0D4X** on GitHub · 2026-12-03 16:00 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3771#issuecomment-7384101661) Fixed the lifetime ordering and defined post-arm cleanup in `33230083`: 1. **Initialize all callback-owned state before registration or enabling callbacks (`src/startup_stubs/d3d12_xs_device.cpp`):** - `order_fence->AddRef()` and `pending->order_fence = order_fence` are now set *before* `SetEventOnCompletion` and *before* `SetThreadpoolWait`. - `order_fence->SetEventOnCompletion(order_seq, event)` is called *before* `SetThreadpoolWait(wait, event, nullptr)`: if registration fails, no threadpool callback was ever enabled, so cleanup owns and releases `pending->order_fence`, `wait`, `module`, `pending`, and `event` exclusively without touching callback-owned state. - Once `SetEventOnCompletion` succeeds and `SetThreadpoolWait(wait, event, nullptr)` is called, ownership of `wait`/`pending`/`order_fence`/`module`/`event` transfers to `signal_reached`. If `queue->Signal(order_fence, order_seq)` fails after arming, `queue_signal_and_track` never touches callback-owned state and surfaces `order_hr` to the caller; `signal_reached` releases everything when `order_fence` advances past `order_seq` (on a subsequent queue signal or device removal). 2. **Expanded fault-injection controls (`tests/startup_fence_rewind.cpp`):** - `X2W_TEST_FENCE_TRACK_FAIL=event:3` (event allocation failure): returns `E_OUTOFMEMORY`, method and slot-12 stay `7`. - `X2W_TEST_FENCE_TRACK_FAIL=register:3` (`SetEventOnCompletion` registration failure): returns `E_FAIL`, cleans up without enabling callback, method and slot-12 stay `7`. - `X2W_TEST_FENCE_TRACK_FAIL=order_signal:3` (order-fence `Signal` failure after arming): returns `E_FAIL`, and once the subsequent `marker` signal advances the order fence after unblock, `signal_reached` publishes `3` and cleans up cleanly. - Clean retry without fault injection: returns `S_OK` and publishes `3`. ### Verification (`33230083`) - **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. - `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).
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-03 16:01 UTC · original · requested changes

Re-tested 33230083 in 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.

**@⁠R0Y00** on GitHub · 2026-12-03 16:01 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3771#pullrequestreview-6173447672) · **requested changes** Re-tested 33230083 in 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.
github-bot force-pushed github-bot/gh-3771 from 3323008346 to 934c6db14d 2026-12-03 16:25:52 +00:00 Compare
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-03 16:40 UTC · original

Independent MSVC Debug retest on the rebased head 934c6db1 passed 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.

**@⁠R0Y00** on GitHub · 2026-12-03 16:40 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3771#issuecomment-7385253328) Independent MSVC Debug retest on the rebased head 934c6db1 passed 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.
Author
Owner

@⁠R0D4X on GitHub · 2026-12-03 16:56 UTC · original

Addressed in 6a97a527:

  • Immediate retirement on order_signal / register failure (src/startup_stubs/d3d12_xs_device.cpp): queue_signal_and_track now runs queue->Signal(desktop_fence, value) -> queue->Signal(order_fence, order_seq) -> order_fence->SetEventOnCompletion(order_seq, event) -> SetThreadpoolWait(wait, event, nullptr) while still populating pending->order_fence before any signal or registration. If either queue->Signal(order_fence, order_seq) or SetEventOnCompletion fails, no event registration or threadpool wait is armed and cleanup runs synchronously in queue_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.
  • Queue teardown cleanup (src/startup_stubs/d3d12_xs_device.cpp): release() now acquires queue_order_lock, releases queue_order_fence, and clears it to nullptr when a queue_kind wrapper drops to refcount 0.
  • No-later-signal isolated queue test (tests/startup_fence_rewind.cpp): steps 6b (order_signal:3) and 6c (register:3) each create a dedicated isolated_queue, inject the fault on its final Signal, unblock the gate, and release isolated_queue with no subsequent signal submitted on that queue.

Verification (commit 6a97a527 / pre-commit identical tree)

Environment: Windows 11 x64 (10.0.26300.9974), MSVC 19.44.35230, Windows SDK / DXC 10.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, driver 32.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).
**@⁠R0D4X** on GitHub · 2026-12-03 16:56 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3771#issuecomment-7385698973) Addressed in `6a97a527`: - **Immediate retirement on `order_signal` / `register` failure (`src/startup_stubs/d3d12_xs_device.cpp`):** `queue_signal_and_track` now runs `queue->Signal(desktop_fence, value)` -> `queue->Signal(order_fence, order_seq)` -> `order_fence->SetEventOnCompletion(order_seq, event)` -> `SetThreadpoolWait(wait, event, nullptr)` while still populating `pending->order_fence` before any signal or registration. If either `queue->Signal(order_fence, order_seq)` or `SetEventOnCompletion` fails, no event registration or threadpool wait is armed and cleanup runs synchronously in `queue_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. - **Queue teardown cleanup (`src/startup_stubs/d3d12_xs_device.cpp`):** `release()` now acquires `queue_order_lock`, releases `queue_order_fence`, and clears it to `nullptr` when a `queue_kind` wrapper drops to refcount `0`. - **No-later-signal isolated queue test (`tests/startup_fence_rewind.cpp`):** steps `6b` (`order_signal:3`) and `6c` (`register:3`) each create a dedicated `isolated_queue`, inject the fault on its final `Signal`, unblock the gate, and release `isolated_queue` with no subsequent signal submitted on that queue. ### Verification (commit `6a97a527` / pre-commit identical tree) Environment: Windows 11 x64 (`10.0.26300.9974`), MSVC `19.44.35230`, Windows SDK / DXC `10.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`, driver `32.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`).
Author
Owner

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

**@⁠R0Y00** on GitHub · 2026-12-03 17:21 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3771#pullrequestreview-6174841558) · **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.
Author
Owner

@⁠R0D4X on GitHub · 2026-12-03 17:50 UTC · original · edited 2026-12-03 17:59 UTC

Addressed in a6beab9a (rebased onto main d87f8455 / #3942, preserving drain_copied_uav_writebacks(), drain_list_timestamps(), trace_fence_progress(), ListMemoryQueueOrder, linear_queue_order(), fence_completed_address(Wrapper*), and all current main CMakePresets.json targets/selectors):

Explicit failure contract (src/startup_stubs/d3d12_xs_device.cpp)

  • Pre-submission failures (event / PendingSignal / module / wait allocation, order_fence creation, and order_fence->SetEventOnCompletion registration): SetEventOnCompletion(order_seq, event) is registered before queue->Signal(desktop_fence, value). Any failure here leaves the guest Signal unapplied, arms no threadpool wait, and retires all resources synchronously.
  • Post-submission order-signal failure (queue->Signal(order_fence, order_seq) fails after queue->Signal(desktop_fence, value) already succeeded): Because the guest signal is already queued on the desktop command queue, queue_signal_and_track surfaces the failing HRESULT, detaches queue_order_fence from the queue (queue_order_fence = nullptr; queue_order_next = 0;), and retains a fallback completion publisher using a 1 ms timed SetThreadpoolWait on the detached order_fence.
    • While blocked behind a gate, the callback does not publish early.
    • Once the GPU executes the guest signal (even with no subsequent signal on that queue), or once a later normal signal on the same FIFO queue completes, or at queue teardown (refs == 0), the callback drains UAV writebacks and list timestamps, publishes the live desktop value to slot 12, and cleanly retires its detached order_fence registration (order_fence->Signal(order_seq) + WaitForSingleObject(event) once prior < order_seq signals have completed) before releasing order_fence, closing event, deleting pending, closing wait, and freeing module.
  • Live slot-8 cross-check: fence_completed_value (slot 8) now reads ID3D12Fence::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 -> stays 7 / 7 while blocked; after gate opens with no later signal, retained fallback publisher updates slot 12 to 3 agreeing with live slot 8 (hr=0x80004005 method=3 address=3), then tears down cleanly.
  • 6d: clean retry on fence_6b without fault injection -> method=3 address=3.
  • 6e: queue reuse after order_signal:3 detachment lazily creates a fresh queue_order_fence and tracks Signal(11) cleanly -> method=11 address=11.

Verification (commit a6beab9a)

Environment: Windows 11 x64 (10.0.26300.9974), MSVC 19.44.35230, Windows SDK / DXC 10.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, driver 32.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).
**@⁠R0D4X** on GitHub · 2026-12-03 17:50 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3771#issuecomment-7387159303) · edited 2026-12-03 17:59 UTC Addressed in `a6beab9a` (rebased onto `main` `d87f8455` / `#3942`, preserving `drain_copied_uav_writebacks()`, `drain_list_timestamps()`, `trace_fence_progress()`, `ListMemoryQueueOrder`, `linear_queue_order()`, `fence_completed_address(Wrapper*)`, and all current `main` `CMakePresets.json` targets/selectors): ### Explicit failure contract (`src/startup_stubs/d3d12_xs_device.cpp`) - **Pre-submission failures (`event` / `PendingSignal` / `module` / `wait` allocation, `order_fence` creation, and `order_fence->SetEventOnCompletion` registration):** `SetEventOnCompletion(order_seq, event)` is registered **before** `queue->Signal(desktop_fence, value)`. Any failure here leaves the guest `Signal` **unapplied**, arms no threadpool wait, and retires all resources synchronously. - **Post-submission order-signal failure (`queue->Signal(order_fence, order_seq)` fails after `queue->Signal(desktop_fence, value)` already succeeded):** Because the guest signal is already queued on the desktop command queue, `queue_signal_and_track` surfaces the failing `HRESULT`, **detaches** `queue_order_fence` from the queue (`queue_order_fence = nullptr; queue_order_next = 0;`), and **retains a fallback completion publisher** using a 1 ms timed `SetThreadpoolWait` on the detached `order_fence`. - While blocked behind a gate, the callback does not publish early. - Once the GPU executes the guest signal (**even with no subsequent signal on that queue**), or once a later normal signal on the same FIFO queue completes, or at queue teardown (`refs == 0`), the callback drains UAV writebacks and list timestamps, publishes the live desktop value to slot 12, and cleanly retires its detached `order_fence` registration (`order_fence->Signal(order_seq)` + `WaitForSingleObject(event)` once prior `< order_seq` signals have completed) before releasing `order_fence`, closing `event`, deleting `pending`, closing `wait`, and freeing `module`. - **Live slot-8 cross-check:** `fence_completed_value` (slot 8) now reads `ID3D12Fence::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 -> stays `7 / 7` while blocked; after gate opens with no later signal, retained fallback publisher updates slot 12 to `3` agreeing with live slot 8 (`hr=0x80004005 method=3 address=3`), then tears down cleanly. - `6d`: clean retry on `fence_6b` without fault injection -> `method=3 address=3`. - `6e`: queue reuse after `order_signal:3` detachment lazily creates a fresh `queue_order_fence` and tracks `Signal(11)` cleanly -> `method=11 address=11`. ### Verification (commit `a6beab9a`) Environment: Windows 11 x64 (`10.0.26300.9974`), MSVC `19.44.35230`, Windows SDK / DXC `10.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`, driver `32.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`).
github-bot force-pushed github-bot/gh-3771 from 6a97a52739 to a6beab9ad3 2026-12-03 18:02:30 +00:00 Compare
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3773/head:github-bot/gh-3771'
git switch 'github-bot/gh-3771'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3771'
git switch 'github-bot/gh-3771'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3771'
git switch 'github-bot/gh-3771'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3771'
git switch 'main'
git merge --squash 'github-bot/gh-3771'
git switch 'main'
git merge --ff-only 'github-bot/gh-3771'
git switch 'main'
git merge 'github-bot/gh-3771'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.