GPU: serialize PresentX texture state reads and barriers under Host::submitted #3204

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

@⁠fuzeofficlal on GitHub · 2026-11-26 12:30 UTC · original
fuzeofficlal:fix/gpu-present-state-sync → main · +414 −47 · 3 files · 1 commits · open

Problem

All regular GPU submissions read and update HostTexture::state under Host::submitted inside HostList::prelude. queue::present and Host::present violated that invariant:

  1. queue::present called resource::host_transition(resource, COPY_SOURCE) before entering Host::present and without holding Host::submitted, publishing a phantom COPY_SOURCE (0x800) state while the host resource was still in its prior GPU state (e.g. RENDER_TARGET = 0x4). Any concurrent Host::execute on another queue observed that phantom state in HostList::prelude and emitted a mismatched StateBefore transition barrier.
  2. Host::present_with_compositor recorded p.copy's before/after transition barriers using that pre-call state snapshot before Host::execute acquired Host::submitted. If a concurrent submission transitioned the texture before p.copy ran, p.copy executed with a stale StateBefore barrier.
  3. After Host::present returned (having already released Host::submitted before IDXGISwapChain::Present and Meter::frame), queue::present unconditionally called resource::host_transition(resource, state) outside Host::submitted, overwriting any state change made by an intervening submission and permanently desynchronizing HostTexture::state from the GPU.

Fix

  • Introduce PresentPlane::Tracked(&HostTexture) and Host::present_planes so queue::present passes live Arc<HostTexture> handles directly instead of mutating HostTexture::state before and after presentation.
  • Acquire Host::submitted inside Host::present_planes_with_compositor (preserving the existing presenter -> submitted lock order) before reading each plane's live HostTexture::state, recording p.copy's barriers, and executing p.copy via Host::execute_locked. Because p.copy transitions each plane back to its observed state before Host::submitted is released, HostTexture::state stays in lockstep with the GPU without unsynchronized mutations.
  • Scope resource::host_transition to #[cfg(all(windows, test))], as queue::present was its only non-test caller.

Tests

  • queue::tests::present_does_not_expose_copy_source_or_clobber_concurrent_state: fails on main (exposes phantom COPY_SOURCE = 2048 instead of RENDER_TARGET = 4, causes barrier replay mismatches on concurrent/present submissions, and clobbers HostTexture::state afterward); passes with this fix.
  • host::present_state_sync_tests::present_planes_reads_live_state_under_submission_lock: deterministically runs an intervening Host::execute during compositor preparation and verifies via host::tests::replay that both single-plane fallback and multi-plane composite paths read the live GPU state under Host::submitted and emit matching transition barriers.
  • Verified with rustfmt --edition 2024, RTX 5090 + WARP with VICTORY_D3D12_DEBUG=1, full WARP victory-gpu suite (248 passed, 0 failed, 1 ignored), cargo clippy --workspace --all-targets --locked -- -D warnings (default and --all-features), and xtask policy check.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 12:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/3204) `fuzeofficlal:fix/gpu-present-state-sync` → `main` · +414 −47 · 3 files · 1 commits · open ### Problem All regular GPU submissions read and update `HostTexture::state` under `Host::submitted` inside `HostList::prelude`. `queue::present` and `Host::present` violated that invariant: 1. `queue::present` called `resource::host_transition(resource, COPY_SOURCE)` before entering `Host::present` and without holding `Host::submitted`, publishing a phantom `COPY_SOURCE` (`0x800`) state while the host resource was still in its prior GPU state (e.g. `RENDER_TARGET = 0x4`). Any concurrent `Host::execute` on another queue observed that phantom state in `HostList::prelude` and emitted a mismatched `StateBefore` transition barrier. 2. `Host::present_with_compositor` recorded `p.copy`'s before/after transition barriers using that pre-call state snapshot before `Host::execute` acquired `Host::submitted`. If a concurrent submission transitioned the texture before `p.copy` ran, `p.copy` executed with a stale `StateBefore` barrier. 3. After `Host::present` returned (having already released `Host::submitted` before `IDXGISwapChain::Present` and `Meter::frame`), `queue::present` unconditionally called `resource::host_transition(resource, state)` outside `Host::submitted`, overwriting any state change made by an intervening submission and permanently desynchronizing `HostTexture::state` from the GPU. ### Fix - Introduce `PresentPlane::Tracked(&HostTexture)` and `Host::present_planes` so `queue::present` passes live `Arc<HostTexture>` handles directly instead of mutating `HostTexture::state` before and after presentation. - Acquire `Host::submitted` inside `Host::present_planes_with_compositor` (preserving the existing `presenter -> submitted` lock order) before reading each plane's live `HostTexture::state`, recording `p.copy`'s barriers, and executing `p.copy` via `Host::execute_locked`. Because `p.copy` transitions each plane back to its observed state before `Host::submitted` is released, `HostTexture::state` stays in lockstep with the GPU without unsynchronized mutations. - Scope `resource::host_transition` to `#[cfg(all(windows, test))]`, as `queue::present` was its only non-test caller. ### Tests - `queue::tests::present_does_not_expose_copy_source_or_clobber_concurrent_state`: fails on `main` (exposes phantom `COPY_SOURCE = 2048` instead of `RENDER_TARGET = 4`, causes barrier replay mismatches on concurrent/present submissions, and clobbers `HostTexture::state` afterward); passes with this fix. - `host::present_state_sync_tests::present_planes_reads_live_state_under_submission_lock`: deterministically runs an intervening `Host::execute` during compositor preparation and verifies via `host::tests::replay` that both single-plane fallback and multi-plane composite paths read the live GPU state under `Host::submitted` and emit matching transition barriers. - Verified with `rustfmt --edition 2024`, RTX 5090 + WARP with `VICTORY_D3D12_DEBUG=1`, full WARP `victory-gpu` suite (248 passed, 0 failed, 1 ignored), `cargo clippy --workspace --all-targets --locked -- -D warnings` (default and `--all-features`), and `xtask` policy check.
queue::present previously swapped each presented texture's HostTexture::state to COPY_SOURCE before calling Host::present and swapped it back afterward, all outside Host::submitted. Host::present also recorded p.copy using that pre-call state snapshot before Host::execute acquired Host::submitted.

Any concurrent Host::execute running between the pre-present swap and present completion observed a phantom COPY_SOURCE state (emitting D3D12 resource barrier mismatches), raced with p.copy's stale before-state transitions, and had its resulting HostTexture::state overwritten back to the pre-present snapshot once PresentX returned.

Pass tracked HostTexture references directly to Host::present_planes, acquire Host::submitted before reading their live states and recording p.copy's transition barriers, and execute p.copy under that same submission lock via Host::execute_locked. Because p.copy restores every presented texture to its original GPU state before releasing Host::submitted, HostTexture::state never needs unsynchronized mutation in queue::present.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 12:32:59 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 12:48 UTC · original · approved these changes

Verified on RTX 5090 against origin/main (63bcca4):

  • cargo test -p victory-gpu --lib queue:: -- --test-threads=1: 6/6 passed (including present_does_not_expose_copy_source_or_clobber_concurrent_state).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly serializes PresentX texture state reads and barriers under Host::submitted.

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

**@⁠Dukestar69** on GitHub · 2026-11-26 12:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/3204#pullrequestreview-6074390380) · **approved these changes** Verified on RTX 5090 against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib queue:: -- --test-threads=1`: 6/6 passed (including `present_does_not_expose_copy_source_or_clobber_concurrent_state`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly serializes `PresentX` texture state reads and barriers under `Host::submitted`. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 14:05 UTC · original

WARP test report: head f76d3f47 (parent and control base: main 63bcca43)

  • Adapter: Microsoft Basic Render Driver (WARP). A local, uncommitted probe asserted the host device's adapter name before each run; it passed and was reverted.
  • Environment: VICTORY_WARP=1, VICTORY_D3D12_WARP=1, cargo test -j 4 -p victory-gpu --features diagnostics --lib, rustc 1.98.1, Windows 11, dxil.dll from the Windows SDK 10.0.26100.0.
  • Head: host::present_state_sync_tests::present_planes_reads_live_state_under_submission_lock and queue::tests::present_does_not_expose_copy_source_or_clobber_concurrent_state both pass. The whole victory-gpu lib suite passes: 249 passed (248 plus the probe), 0 failed, 1 ignored.
  • Control 1: main's queue.rs code with this PR's test module (and this PR's host.rs). present_does_not_expose_copy_source_or_clobber_concurrent_state fails: present must not mutate tracked state before acquiring the submission lock (left 2048, right 4).
  • Control 2: the host test uses present_planes_with_compositor, which main doesn't have, so it can't be built on main. I used a mutation of this head instead: the tracked states are read at the start of present_planes_with_compositor, before the compositor callback and before Host::submitted is locked. With that mutation, both tests fail: present_planes_reads_live_state_under_submission_lock (left 4, right 2048) and present_does_not_expose_copy_source_or_clobber_concurrent_state (present copy barriers: before 0x4, but the resource is in 0x80). Moving only the lock, with the state read still after the callback, did not fail the host test, so that test doesn't check lock order on its own.
  • Not tested: synthetic textures only, with the intervening submission injected from the compositor callback, so the concurrency is simulated and not a real two-thread race. No physical GPU and no title.

AI-assisted: verification by Claude Code (Claude Sonnet 5.5).

**@⁠oni-swr** on GitHub · 2026-11-26 14:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/3204#issuecomment-7194253000) **WARP test report: head `f76d3f47`** (parent and control base: main `63bcca43`) - Adapter: Microsoft Basic Render Driver (WARP). A local, uncommitted probe asserted the host device's adapter name before each run; it passed and was reverted. - Environment: `VICTORY_WARP=1`, `VICTORY_D3D12_WARP=1`, `cargo test -j 4 -p victory-gpu --features diagnostics --lib`, rustc 1.98.1, Windows 11, `dxil.dll` from the Windows SDK 10.0.26100.0. - Head: `host::present_state_sync_tests::present_planes_reads_live_state_under_submission_lock` and `queue::tests::present_does_not_expose_copy_source_or_clobber_concurrent_state` both pass. The whole `victory-gpu` lib suite passes: 249 passed (248 plus the probe), 0 failed, 1 ignored. - Control 1: main's `queue.rs` code with this PR's test module (and this PR's `host.rs`). `present_does_not_expose_copy_source_or_clobber_concurrent_state` fails: `present must not mutate tracked state before acquiring the submission lock` (left 2048, right 4). - Control 2: the host test uses `present_planes_with_compositor`, which main doesn't have, so it can't be built on main. I used a mutation of this head instead: the tracked states are read at the start of `present_planes_with_compositor`, before the compositor callback and before `Host::submitted` is locked. With that mutation, both tests fail: `present_planes_reads_live_state_under_submission_lock` (left 4, right 2048) and `present_does_not_expose_copy_source_or_clobber_concurrent_state` (`present copy barriers`: `before 0x4, but the resource is in 0x80`). Moving only the lock, with the state read still after the callback, did not fail the host test, so that test doesn't check lock order on its own. - Not tested: synthetic textures only, with the intervening submission injected from the compositor callback, so the concurrency is simulated and not a real two-thread race. No physical GPU and no title. AI-assisted: verification by Claude Code (Claude Sonnet 5.5).
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/3204/head:github-bot/gh-3204'
git switch 'github-bot/gh-3204'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3204'
git switch 'github-bot/gh-3204'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3204'
git switch 'github-bot/gh-3204'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3204'
git switch 'main'
git merge --squash 'github-bot/gh-3204'
git switch 'main'
git merge --ff-only 'github-bot/gh-3204'
git switch 'main'
git merge 'github-bot/gh-3204'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3204
No description provided.