GPU: confirm the host fence before copying results back #2897

Open
andrewpozdnakov7-jpg wants to merge 1 commit from upstream-pr/2899 into main
andrewpozdnakov7-jpg commented 2026-11-25 23:25:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-25T23:25:05Z.


Change

The synchronous host submission path uses a shared auto-reset event. A previous notification can wake a later wait while that submission's fence value is still pending. The old path also continued on an event-registration error or a failed Win32 wait. The work that copies GPU data back to guest memory relies on confirmed completion.

Check the completed value after the event wakes. Continue waiting on the registered event if an earlier signal woke it. If registration or the native wait fails, poll the fence at 1 ms intervals. A failed queue signal or the device-removed value goes through the submission failure path before copy-back. This is the standalone fence change suggested in the closing note on #2158.

Verification

The WARP test gives a private D3D12 queue a fence signal behind a closed gate and supplies an already-signalled auto-reset event. On 842b0cb76f8f5fa9289bd779783c51ec24d75302, the test extracts the existing wait without changing its decision. It returned the still-pending value 0 in six runs. With fde6a92694cd5b61891e17ca4100a526384a6e4f, the same test stays blocked and sees 1 after the gate opens in all six runs.

Full native WARP GPU suite passed with 247 tests and one ignored under each D3D12 debug setting, and again with GPU timing enabled. Strict GPU Clippy in both configurations and asset policy passed. The paired test uses synthetic data and exercises the early wake. Device removal was not induced.

Coordination · Win32 event wait · D3D12 fence completion

Please retain the Git author if cherry-picking the fix or test. If folding this change into another commit, please retain Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.

AI-assisted with Codex.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5500940993 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2899). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-25T23:25:05Z. --- ## Change The synchronous host submission path uses a shared auto-reset event. A previous notification can wake a later wait while that submission's fence value is still pending. The old path also continued on an event-registration error or a failed Win32 wait. The work that copies GPU data back to guest memory relies on confirmed completion. Check the completed value after the event wakes. Continue waiting on the registered event if an earlier signal woke it. If registration or the native wait fails, poll the fence at 1 ms intervals. A failed queue signal or the device-removed value goes through the submission failure path before copy-back. This is the standalone fence change suggested in the closing note on #2158. ## Verification The WARP test gives a private D3D12 queue a fence signal behind a closed gate and supplies an already-signalled auto-reset event. On `842b0cb76f8f5fa9289bd779783c51ec24d75302`, the test extracts the existing wait without changing its decision. It returned the still-pending value `0` in six runs. With `fde6a92694cd5b61891e17ca4100a526384a6e4f`, the same test stays blocked and sees `1` after the gate opens in all six runs. Full native WARP GPU suite passed with 247 tests and one ignored under each D3D12 debug setting, and again with GPU timing enabled. Strict GPU Clippy in both configurations and asset policy passed. The paired test uses synthetic data and exercises the early wake. Device removal was not induced. [Coordination](https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7177343198) · [Win32 event wait](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitforsingleobject) · [D3D12 fence completion](https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12fence-getcompletedvalue) Please retain the Git author if cherry-picking the fix or test. If folding this change into another commit, please retain `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>`. AI-assisted with Codex.
kotekompotek commented 2026-11-25 23:35:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T23:35:07Z.

Review: COMMENTED


Robust fence handling: SetEventOnCompletion failure or a non-WAIT_OBJECT_0 wake now falls back to polling GetCompletedValue rather than hanging or proceeding early — and the loop re-checks the actual submitted value, so a stale event signal can't pass the wait. Copy-back correctness preserved. LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066478903 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2899#pullrequestreview-6066478903). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T23:35:07Z. **Review:** COMMENTED --- Robust fence handling: SetEventOnCompletion failure or a non-WAIT_OBJECT_0 wake now falls back to polling GetCompletedValue rather than hanging or proceeding early — and the loop re-checks the actual submitted value, so a stale event signal can't pass the wait. Copy-back correctness preserved. LGTM.
fuzeofficlal commented 2026-11-25 23:37:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T23:37:10Z.

Review: COMMENTED


Verified head fde6a926 cleanly merged onto origin/main (63bcca43) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + D3D12 WARP + VICTORY_D3D12_DEBUG=1).

Code & Safety Review

  • Signaling the host fence and failing immediately on Signal error rather than proceeding to copy-back closes a real data-race window, and confirming GetCompletedValue() >= value in wait_for_fence_value before Step::GpuWrite / copy_back_marks / defer_after ensures spurious or stale auto-reset event wakeups cannot allow premature reads.
  • No deadlock / device-removal hang: Host::execute serializes self.event under self.submitted; on DXGI_ERROR_DEVICE_REMOVED, D3D12 guarantees GetCompletedValue() returns u64::MAX (>= *submitted), so the loop exits immediately into the removed diagnostic path instead of hanging.

Local Verification (VICTORY_D3D12_DEBUG=1)

  • RTX 5090: host::tests 31/32 (including new submission_wait_confirms_fence_after_an_early_event; the 1 failure is the pre-existing dynamic_slots_are_held_until_released NVIDIA 1M view cap baseline), draw::tests 20/20, queue::tests 5/5, hold::tests 10/10.
  • WARP: host::tests 32/32, draw::tests 20/20, queue::tests 5/5, hold::tests 10/10.
  • cargo clippy --locked -p victory-gpu --all-targets -- -D warnings (default and --features diagnostics) & cargo xtask policy (191 files ok): clean.

Looks clean and ready to merge.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066498329 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2899#pullrequestreview-6066498329). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T23:37:10Z. **Review:** COMMENTED --- Verified head `fde6a926` cleanly merged onto `origin/main` (`63bcca43`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + D3D12 WARP + `VICTORY_D3D12_DEBUG=1`). ### Code & Safety Review - Signaling the host fence and failing immediately on `Signal` error rather than proceeding to copy-back closes a real data-race window, and confirming `GetCompletedValue() >= value` in `wait_for_fence_value` before `Step::GpuWrite` / `copy_back_marks` / `defer_after` ensures spurious or stale auto-reset event wakeups cannot allow premature reads. - **No deadlock / device-removal hang**: `Host::execute` serializes `self.event` under `self.submitted`; on `DXGI_ERROR_DEVICE_REMOVED`, D3D12 guarantees `GetCompletedValue()` returns `u64::MAX` (`>= *submitted`), so the loop exits immediately into the `removed` diagnostic path instead of hanging. ### Local Verification (`VICTORY_D3D12_DEBUG=1`) - **RTX 5090**: `host::tests` **31/32** (including new `submission_wait_confirms_fence_after_an_early_event`; the 1 failure is the pre-existing `dynamic_slots_are_held_until_released` NVIDIA 1M view cap baseline), `draw::tests` **20/20**, `queue::tests` **5/5**, `hold::tests` **10/10**. - **WARP**: `host::tests` **32/32**, `draw::tests` **20/20**, `queue::tests` **5/5**, `hold::tests` **10/10**. - `cargo clippy --locked -p victory-gpu --all-targets -- -D warnings` (default and `--features diagnostics`) & `cargo xtask policy` (`191 files ok`): clean. Looks clean and ready to merge.
Marvisak commented 2026-11-26 05:04:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T04:31:08Z.


Automated triage for the maintainers.

Merge candidate. The synchronous wait in Host::submit (host.rs) becomes wait_for_fence_value, which re-checks GetCompletedValue after every wake, falls back to 1 ms polling if event registration or the wait fails, and stops on the device-removed value; a failed Signal goes through fail before any copy-back. This closes a real gap on main, where a failed SetEventOnCompletion let copy-back run before the GPU finished. The WARP test with a gated queue checks that the wait does not return early.

CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7182664733 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2899#issuecomment-7182664733). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T04:31:08Z. --- <!-- victory-triage v1 head=fde6a92694cd5b61891e17ca4100a526384a6e4f verdict=merge-candidate at=2026-11-26T04:30:59Z --> Automated triage for the maintainers. **Merge candidate.** The synchronous wait in `Host::submit` (`host.rs`) becomes `wait_for_fence_value`, which re-checks `GetCompletedValue` after every wake, falls back to 1 ms polling if event registration or the wait fails, and stops on the device-removed value; a failed `Signal` goes through `fail` before any copy-back. This closes a real gap on main, where a failed `SetEventOnCompletion` let copy-back run before the GPU finished. The WARP test with a gated queue checks that the wait does not return early. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Dukestar69 commented 2026-11-26 05:47:58 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T05:47:58Z.

Review: APPROVED


Tested locally merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP):

  • Diff review: wait_for_fence_value confirms fence.GetCompletedValue() >= value in a loop (falling back to polling if SetEventOnCompletion or WaitForSingleObject fails), preventing premature result read-backs after spurious/early event wakes while preserving device-removed handling.
  • cargo test -p victory-gpu submission_wait_confirms_fence_after_an_early_event -- --test-threads=1 (RTX 5090 & WARP): 1 passed, 0 failed on both.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6069141245 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2899#pullrequestreview-6069141245). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T05:47:58Z. **Review:** APPROVED --- Tested locally merged onto `origin/main` (`63bcca4`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP): - Diff review: `wait_for_fence_value` confirms `fence.GetCompletedValue() >= value` in a loop (falling back to polling if `SetEventOnCompletion` or `WaitForSingleObject` fails), preventing premature result read-backs after spurious/early event wakes while preserving device-removed handling. - `cargo test -p victory-gpu submission_wait_confirms_fence_after_an_early_event -- --test-threads=1` (RTX 5090 & WARP): 1 passed, 0 failed on both. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
pickles976 commented 2026-11-28 14:18:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pickles976 on 2026-11-28T14:18:32Z.


Verified the completion invariant on a physical RTX 4060 Laptop GPU (8 GiB), Linux 6.17 / Wine 11, NVIDIA 590.48.1, diagnostic-relaxed vkd3d-proton 3.2.0 build 2576c0b4adfae4a+. This is a dirty local integration based on 3ed7d725f6, NOT an exact-head test of this PR.

The VIctory-NVIDIA fork's e205498c background-wait correction prompted a review of our synchronous path, which still ignored failed/premature event wakes. Your standalone fde6a92694 addresses that path without importing the fork's renderer.

A local ignored diagnostic regression exercises actual Host::execute (not only an extracted wait helper): close an empty native list, gate the native queue with a private CPU-signalled fence, pre-signal the existing auto-reset completion event, and execute from another thread. Release the private gate after a bounded 200 ms observation; require that execute has not already returned.

Before the port: 0/1 FAIL, early=Ok(()), completion before release=0. After porting your wait/Signal/removal handling while preserving our pending-submission observer: 1/1 PASS, early=Err(Timeout), before=0, after=1. Fresh intended-worktree builds 7.41/7.44 s; tests 0.65/0.85 s. The final fixture retains the empty list until completion; the initial failing fixture dropped its worker-owned list early and logged deferred native allocator release. That lifetime cleanup changed between legs, not the pre-release return assertion. An earlier fixture compilation error was our missing move capture, not a PR defect.

Command on both legs: VICTORY_LAB_SUBMISSIONS=1 VICTORY_WARP=0 DXVK_CONFIG='dxgi.hideNvidiaGpu = False' cargo test --locked --target x86_64-pc-windows-msvc -p victory-gpu --features diagnostics --lib lab_submission_does_not_retire_on_a_stale_event -- --ignored --nocapture --test-threads=1, through our guarded Wine wrapper. Actual adapter required, no WARP fallback or host skip. Scope 9 GiB/512 MiB; all global safety limits unchanged. Passing run GPU peak214 MiB, minimum available RAM10715 MiB.

Local evidence directories: verify-mixed-stale-event-control-fixed-fixture-20261128T141631Z/ and verify-mixed-pr2899-stale-event-20261128T141720Z/ (console and resource manifests). Adaptation credit: andrewpozdnakov7-jpg [email protected].

Limits: empty native work, no title shaders, no induced device removal/failed event-registration test, no native Windows debug-layer certification. This prevents premature copy-back/retirement; it does NOT establish a fix for our separate Xid109 CTX SWITCH TIMEOUT. In the saved title trace all 8,882 completed submissions reached their fence values; submission8,883 remained pending when the driver timed out. No new title run was performed.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7240572478 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2899#issuecomment-7240572478). Originally posted by [pickles976](https://github.com/pickles976) on 2026-11-28T14:18:32Z. --- Verified the completion invariant on a physical RTX 4060 Laptop GPU (8 GiB), Linux 6.17 / Wine 11, NVIDIA 590.48.1, diagnostic-relaxed vkd3d-proton 3.2.0 build 2576c0b4adfae4a+. This is a dirty local integration based on 3ed7d725f6895fe1a235eb6fa8ff2485261ca151, NOT an exact-head test of this PR. The VIctory-NVIDIA fork's e205498c background-wait correction prompted a review of our synchronous path, which still ignored failed/premature event wakes. Your standalone fde6a92694cd5b61891e17ca4100a526384a6e4f addresses that path without importing the fork's renderer. A local ignored diagnostic regression exercises actual Host::execute (not only an extracted wait helper): close an empty native list, gate the native queue with a private CPU-signalled fence, pre-signal the existing auto-reset completion event, and execute from another thread. Release the private gate after a bounded 200 ms observation; require that execute has not already returned. Before the port: 0/1 FAIL, early=Ok(()), completion before release=0. After porting your wait/Signal/removal handling while preserving our pending-submission observer: 1/1 PASS, early=Err(Timeout), before=0, after=1. Fresh intended-worktree builds 7.41/7.44 s; tests 0.65/0.85 s. The final fixture retains the empty list until completion; the initial failing fixture dropped its worker-owned list early and logged deferred native allocator release. That lifetime cleanup changed between legs, not the pre-release return assertion. An earlier fixture compilation error was our missing move capture, not a PR defect. Command on both legs: VICTORY_LAB_SUBMISSIONS=1 VICTORY_WARP=0 DXVK_CONFIG='dxgi.hideNvidiaGpu = False' cargo test --locked --target x86_64-pc-windows-msvc -p victory-gpu --features diagnostics --lib lab_submission_does_not_retire_on_a_stale_event -- --ignored --nocapture --test-threads=1, through our guarded Wine wrapper. Actual adapter required, no WARP fallback or host skip. Scope 9 GiB/512 MiB; all global safety limits unchanged. Passing run GPU peak214 MiB, minimum available RAM10715 MiB. Local evidence directories: verify-mixed-stale-event-control-fixed-fixture-20261128T141631Z/ and verify-mixed-pr2899-stale-event-20261128T141720Z/ (console and resource manifests). Adaptation credit: andrewpozdnakov7-jpg <[email protected]>. Limits: empty native work, no title shaders, no induced device removal/failed event-registration test, no native Windows debug-layer certification. This prevents premature copy-back/retirement; it does NOT establish a fix for our separate Xid109 CTX SWITCH TIMEOUT. In the saved title trace all 8,882 completed submissions reached their fence values; submission8,883 remained pending when the driver timed out. No new title run was performed.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2899:upstream-pr/2899'
git switch 'upstream-pr/2899'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2899'
git switch 'upstream-pr/2899'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2899'
git switch 'upstream-pr/2899'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2899'
git switch 'main'
git merge --squash 'upstream-pr/2899'
git switch 'main'
git merge --ff-only 'upstream-pr/2899'
git switch 'main'
git merge 'upstream-pr/2899'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Marvisak/VIctory!2897
No description provided.