GPU/present: transition presented texture via HostList prelude under submitted lock #564

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

@⁠AscilCH on GitHub · 2026-11-25 02:17 UTC · original
AscilCH:fix/gpu-present-copy-source-barrier-and-state-race → main · +115 −33 · 3 files · 1 commits · open

Problem

Two bugs in the presentation state-tracking path across crates/victory-gpu/src/queue.rs and crates/victory-gpu/src/host.rs:

  1. Illegal COPY_SOURCE -> COPY_SOURCE D3D12 barriers: When the presented texture was already in D3D12_RESOURCE_STATE_COPY_SOURCE (0x800), resource::host_transition(resource, COPY_SOURCE) returned None, defaulting state = COPY_SOURCE. Host::present() then unconditionally recorded two COPY_SOURCE -> COPY_SOURCE barriers (StateBefore == StateAfter, violating D3D12 barrier rules).
  2. Unlocked HostTexture::state race: queue.rs mutated HostTexture::state to COPY_SOURCE before Host::present() acquired host.submitted, and unconditionally swapped it back afterwards. A concurrent Host::submit() running list.prelude() in that window saw the bogus COPY_SOURCE state, emitted a mismatched barrier, and then had its resulting HostTexture::state clobbered by queue.rs. (Passing &ID3D12Resource instead of Arc<HostTexture> also did not retain the texture on async late_free_present copy lists.)

Fix

  • Pass an owned Arc<HostTexture> (resource::host_copy) into Host::present() and remove the unlocked host_transition swaps from queue.rs (scoping resource::host_transition to #[cfg(all(windows, test))]).
  • Call copy.settle(texture, D3D12_RESOURCE_STATE_COPY_SOURCE.0 as u32) on the present copy list so copy.prelude() transitions texture from its true state to COPY_SOURCE atomically under self.submitted, skips the barrier completely when already in COPY_SOURCE, and retains Arc<HostTexture> until copy.reset().
  • Add synthetic D3D12 test present_settle_skips_redundant_copy_source_barrier_and_tracks_state_at_prelude.

Verification

  • cargo test -p victory-gpu --locked host::batch -- --test-threads=1: 5 passed, 0 failed (1 pre-existing ignored benchmark)
  • cargo test -p victory-gpu --locked queue::tests -- --test-threads=1: 4 passed, 0 failed
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings: clean (default-features build on c96b4f2 has only the pre-existing dead_code warning on placed_filler_twins, tracked in #491)

(AI-assisted; synthetic unit tests only, no game data.)

**@⁠AscilCH** on GitHub · 2026-11-25 02:17 UTC · [original](https://github.com/fxceless/victory-fork/pull/564) `AscilCH:fix/gpu-present-copy-source-barrier-and-state-race` → `main` · +115 −33 · 3 files · 1 commits · open ## Problem Two bugs in the presentation state-tracking path across `crates/victory-gpu/src/queue.rs` and `crates/victory-gpu/src/host.rs`: 1. **Illegal `COPY_SOURCE -> COPY_SOURCE` D3D12 barriers**: When the presented texture was already in `D3D12_RESOURCE_STATE_COPY_SOURCE` (`0x800`), `resource::host_transition(resource, COPY_SOURCE)` returned `None`, defaulting `state = COPY_SOURCE`. `Host::present()` then unconditionally recorded two `COPY_SOURCE -> COPY_SOURCE` barriers (`StateBefore == StateAfter`, violating D3D12 barrier rules). 2. **Unlocked `HostTexture::state` race**: `queue.rs` mutated `HostTexture::state` to `COPY_SOURCE` before `Host::present()` acquired `host.submitted`, and unconditionally swapped it back afterwards. A concurrent `Host::submit()` running `list.prelude()` in that window saw the bogus `COPY_SOURCE` state, emitted a mismatched barrier, and then had its resulting `HostTexture::state` clobbered by `queue.rs`. (Passing `&ID3D12Resource` instead of `Arc<HostTexture>` also did not retain the texture on async `late_free_present` copy lists.) ## Fix - Pass an owned `Arc<HostTexture>` (`resource::host_copy`) into `Host::present()` and remove the unlocked `host_transition` swaps from `queue.rs` (scoping `resource::host_transition` to `#[cfg(all(windows, test))]`). - Call `copy.settle(texture, D3D12_RESOURCE_STATE_COPY_SOURCE.0 as u32)` on the present copy list so `copy.prelude()` transitions `texture` from its true state to `COPY_SOURCE` atomically under `self.submitted`, skips the barrier completely when already in `COPY_SOURCE`, and retains `Arc<HostTexture>` until `copy.reset()`. - Add synthetic D3D12 test `present_settle_skips_redundant_copy_source_barrier_and_tracks_state_at_prelude`. ## Verification - `cargo test -p victory-gpu --locked host::batch -- --test-threads=1`: 5 passed, 0 failed (1 pre-existing ignored benchmark) - `cargo test -p victory-gpu --locked queue::tests -- --test-threads=1`: 4 passed, 0 failed - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings`: clean (default-features build on `c96b4f2` has only the pre-existing `dead_code` warning on `placed_filler_twins`, tracked in #491) *(AI-assisted; synthetic unit tests only, no game data.)*
- Replace unlocked `resource::host_transition` swaps around `host.present()` in
  `queue.rs` with passing an owned `Arc<HostTexture>` (`resource::host_copy`)
  into `Host::present()`.
- Record `copy.settle(texture, D3D12_RESOURCE_STATE_COPY_SOURCE)` on the
  present copy list so `copy.prelude()` runs atomically under `self.submitted`:
  - Eliminates illegal `COPY_SOURCE -> COPY_SOURCE` D3D12 barriers when the
    presented texture is already in `COPY_SOURCE`.
  - Eliminates the race where a concurrent `Host::submit()` observed the
    unlocked `COPY_SOURCE` swap and had its resulting `HostTexture::state`
    overwritten on `present()`'s return.
  - Retains the `Arc<HostTexture>` on the present copy list until reset after
    GPU completion (including `late_free_present` flights).
- Add synthetic D3D12 unit test verifying `settle` + `prelude` emits no
  no-op barrier for `COPY_SOURCE` and updates `HostTexture::state` only at
  prelude/submission time.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:46:27 +00:00
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/host.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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