Skip to content

GPU: confirm the host fence before copying results back - #2899

Open
andrewpozdnakov7-jpg wants to merge 1 commit into
Marvisak:mainfrom
andrewpozdnakov7-jpg:codex1/confirm-host-fence-completion
Open

andrewpozdnakov7-jpg wants to merge 1 commit into
Marvisak:mainfrom
andrewpozdnakov7-jpg:codex1/confirm-host-fence-completion

Conversation

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

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 <andrewpozdnakov7-jpg@users.noreply.github.com>.

AI-assisted with Codex.

@kotekompotek kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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.

Copy link
Copy Markdown
Owner

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

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants