GPU: a draw ensures its targets once per states generation, and clones a texture only when a barrier is owed #3249

Open
github-bot wants to merge 4 commits from github-bot/gh-3249 into main AGit
Owner

@⁠borosf on GitHub · 2026-11-26 14:31 UTC · original
borosf:vibe/ensure-state-once-842ca5 → main · +84 −9 · 2 files · 4 commits · open

Every draw called list::ensure_state for each of its render targets and its depth target, and each call paid for the answer "nothing changed" in full.

What one call cost

HostList::transition did, unconditionally: host_copy (a lock and an Arc clone), copy.texture.clone(), a twin check, legal_state (two GetDescs on some hosts), and a states lock plus hash entry — then returned None because the state was already right. A world frame makes ~8,000 draws, most binding the same targets.

What changes

  • HostList carries a states_generation: AtomicU64, bumped whenever states gains or changes an entry, and when it is cleared at a reset. State::bind remembers the targets and depth it last ensured along with that generation, and skips the whole loop while the generation, the depth and the target list are all unchanged.
  • transition clones the texture only when track reports a barrier is owed, instead of on every call.

The generation is the soundness argument: any transition the list itself records (targets, or a texture view in readable) bumps it, so a draw re-ensures exactly when something it named could have moved. A texture released and re-made is a new Arc pointer, hence a new states entry, hence a bump.

Verification

  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings — clean; --all-features too.
  • cargo test -p victory-gpu --lib --locked --no-fail-fast — 230 passed, 2 failed, both the known Intel baseline failures pristine main also fails (descriptor::host::tests::dynamic_slots_are_held_until_released, memory::tests::lists_share_a_table_and_copies_keep_their_views, #1744/#1829). The draw tests that drive barriers through ensure_state pass.
  • rustfmt --edition 2024 --check hunk counts against main: draw.rs 84/84, host.rs 85/85.

Per-call cost is arithmetic from the code (one lock + one Arc clone + two GetDescs + one hash entry ≈ 145–200 ns per call, 40k–130k calls a frame), not a title measurement; the frame line's existing counters are the way to see the delta in a run.

Synthetic tests only; no game data.

Part of the fifteen gaps claimed for this sweep on #1633. AI-assisted.

**@⁠borosf** on GitHub · 2026-11-26 14:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/3249) `borosf:vibe/ensure-state-once-842ca5` → `main` · +84 −9 · 2 files · 4 commits · open Every draw called `list::ensure_state` for each of its render targets and its depth target, and each call paid for the answer "nothing changed" in full. ## What one call cost `HostList::transition` did, unconditionally: `host_copy` (a lock and an `Arc` clone), **`copy.texture.clone()`**, a twin check, `legal_state` (two `GetDesc`s on some hosts), and a `states` lock plus hash entry — then returned `None` because the state was already right. A world frame makes ~8,000 draws, most binding the same targets. ## What changes - `HostList` carries a `states_generation: AtomicU64`, bumped whenever `states` gains or changes an entry, and when it is cleared at a reset. `State::bind` remembers the targets and depth it last ensured along with that generation, and skips the whole loop while the generation, the depth and the target list are all unchanged. - `transition` clones the texture **only when `track` reports a barrier is owed**, instead of on every call. The generation is the soundness argument: any transition the list itself records (targets, or a texture view in `readable`) bumps it, so a draw re-ensures exactly when something it named could have moved. A texture released and re-made is a new `Arc` pointer, hence a new `states` entry, hence a bump. ## Verification - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` — clean; `--all-features` too. - `cargo test -p victory-gpu --lib --locked --no-fail-fast` — 230 passed, 2 failed, both the known Intel baseline failures pristine `main` also fails (`descriptor::host::tests::dynamic_slots_are_held_until_released`, `memory::tests::lists_share_a_table_and_copies_keep_their_views`, #1744/#1829). The draw tests that drive barriers through `ensure_state` pass. - `rustfmt --edition 2024 --check` hunk counts against `main`: `draw.rs` 84/84, `host.rs` 85/85. Per-call cost is arithmetic from the code (one lock + one Arc clone + two `GetDesc`s + one hash entry ≈ 145–200 ns per call, 40k–130k calls a frame), not a title measurement; the frame line's existing counters are the way to see the delta in a run. Synthetic tests only; no game data. Part of the fifteen gaps claimed for this sweep on #1633. AI-assisted.
Every draw called `list::ensure_state` for each render target and the
depth target, and each call took the host copy, cloned its texture, checked
the twin, legalised the wanted state and hashed an entry in `states` before
finding out the state was already right. A world frame makes ~8,000 draws,
most binding the same targets.

`HostList` now carries a `states_generation`, bumped whenever `states` gains
or changes an entry (and when it is cleared at a reset), and `State::bind`
remembers the targets and depth it last ensured together with that
generation: while nothing the list named has moved, the loop is skipped.
`HostList::transition` also clones the texture only when `track` says a
barrier is actually owed, instead of on every call.

Verification: `cargo clippy -p victory-gpu --all-targets --locked -- -D
warnings` and `--all-features` both clean; `cargo test -p victory-gpu --lib
--locked --no-fail-fast` 230 passed, 2 failed — both the known Intel
baseline failures pristine main also fails (#1744/#1829), including the draw
tests that exercise barriers through `ensure_state`. `rustfmt --edition
2024 --check` hunks against main: draw.rs 84/84, host.rs 85/85.

Synthetic tests only; no game data.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:33:20 +00:00
Author
Owner

@⁠Bj0rnis on GitHub · 2026-11-26 14:42 UTC · original

Verified head 8b09e2fd on Windows 11 (x64, Rust 1.98.1, NVIDIA GeForce RTX 5070 Ti + WARP):

  • Merge check vs origin/main (63bcca43): clean
  • RTX 5070 Ti (cargo test -p victory-gpu --lib --features diagnostics): 244 passed, 2 failed, 1 ignored (2.95s) — the 2 failures are the pre-existing #1744 NVIDIA 1M view-heap cap failures on main (descriptor::host::tests::dynamic_slots_are_held_until_released, memory::tests::lists_share_a_table_and_copies_keep_their_views); no regressions
  • WARP (VICTORY_D3D12_WARP=1): 246 passed, 0 failed, 1 ignored (1.97s)
  • Clippy (-p victory-gpu --lib --tests --features diagnostics -- -D warnings): clean (0 warnings)
  • cargo xtask policy: 191 files ok

Not tested: live title workload / draw-loop performance under gameplay.

AI-assisted test run and summary.

**@⁠Bj0rnis** on GitHub · 2026-11-26 14:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/3249#issuecomment-7195210166) Verified head `8b09e2fd` on Windows 11 (x64, Rust 1.98.1, NVIDIA GeForce RTX 5070 Ti + WARP): - **Merge check vs `origin/main` (`63bcca43`):** clean - **RTX 5070 Ti (`cargo test -p victory-gpu --lib --features diagnostics`):** **244 passed**, 2 failed, 1 ignored (2.95s) — the 2 failures are the pre-existing #1744 NVIDIA 1M view-heap cap failures on `main` (`descriptor::host::tests::dynamic_slots_are_held_until_released`, `memory::tests::lists_share_a_table_and_copies_keep_their_views`); no regressions - **WARP (`VICTORY_D3D12_WARP=1`):** **246 passed**, 0 failed, 1 ignored (1.97s) - **Clippy (`-p victory-gpu --lib --tests --features diagnostics -- -D warnings`):** clean (0 warnings) - **`cargo xtask policy`:** 191 files ok Not tested: live title workload / draw-loop performance under gameplay. *AI-assisted test run and summary.*
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 14:50 UTC · original · reviewed

Reviewed the diff and ran local verification on Windows 11 x64 (cargo test -p victory-gpu --lib draw:: -- --test-threads=1: 20/20 passed; cargo clippy -p victory-gpu --all-targets -- -D warnings: clean; cargo xtask policy: 191 files ok).

Deferring copy.texture.clone() in HostList::transition until self.track() actually returns a barrier looks great, but the self.ensured fast path in draw.rs currently never hits whenever at least one target/depth is bound:

  1. let generation = list.states_generation(); is read before the ensure_state loop, and self.ensured = Some((self.targets.clone(), self.depth, generation)); stores that pre-loop generation.
  2. Inside HostList::track, self.states_generation.fetch_add(1, Ordering::Relaxed); runs unconditionally on every call (even when the entry already exists and current == wanted).
  3. As a result, draw 1 reads gen = 0, calls ensure_state (which bumps states_generation to >= 1), and stores t = 0. Draw 2 then sees list.states_generation() != 0, treats ensured as stale, calls ensure_state again (bumping states_generation again), and clones self.targets on every single draw.

To make the cache actually hit, self.ensured should store list.states_generation() after the ensure_state calls (and HostList::track should only bump states_generation when an entry is inserted or its state actually changes, matching the doc comment). Also consider whether a HostList cut/replacement in State::bind or a depth win::route switch can occur without bumping states_generation.

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

**@⁠Dukestar69** on GitHub · 2026-11-26 14:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/3249#pullrequestreview-6076184145) · **reviewed** Reviewed the diff and ran local verification on Windows 11 x64 (cargo test -p victory-gpu --lib draw:: -- --test-threads=1: 20/20 passed; cargo clippy -p victory-gpu --all-targets -- -D warnings: clean; cargo xtask policy: 191 files ok). Deferring copy.texture.clone() in HostList::transition until self.track() actually returns a barrier looks great, but the self.ensured fast path in draw.rs currently **never hits** whenever at least one target/depth is bound: 1. let generation = list.states_generation(); is read **before** the ensure_state loop, and self.ensured = Some((self.targets.clone(), self.depth, generation)); stores that pre-loop generation. 2. Inside HostList::track, self.states_generation.fetch_add(1, Ordering::Relaxed); runs unconditionally on every call (even when the entry already exists and current == wanted). 3. As a result, draw 1 reads gen = 0, calls ensure_state (which bumps states_generation to >= 1), and stores t = 0. Draw 2 then sees list.states_generation() != 0, treats ensured as stale, calls ensure_state again (bumping states_generation again), and clones self.targets on every single draw. To make the cache actually hit, self.ensured should store list.states_generation() **after** the ensure_state calls (and HostList::track should only bump states_generation when an entry is inserted or its state actually changes, matching the doc comment). Also consider whether a HostList cut/replacement in State::bind or a depth win::route switch can occur without bumping states_generation. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Reviewer on #3249: two things kept the fast path from ever firing. `track`
bumped `states_generation` on every call, even when the entry already held
the wanted state, and `State::bind` stored the generation read *before* the
ensure loop, so the loop's own bump made the next draw see it as stale.

`track` now bumps only when an entry is inserted or its state actually
changes, `transition` bumps when a twin route moves the texture's side, and
`bind` stores the generation read after the loop. A draw that binds the same
targets with nothing moved now skips the loop entirely.

Synthetic tests only; no game data.
Author
Owner

@⁠borosf on GitHub · 2026-11-26 14:59 UTC · original

Fixed at d4903faff4be45b0d597357ebebfe904d0d7d7c: both halves of the never-hits bug. HostList::track now bumps states_generation only when an entry is inserted or its state actually changes (not on every call), transition bumps when a twin route moves the texture's side, and State::bind stores the generation read after the ensure loop — so a draw that rebinds the same targets with nothing moved skips the loop. On your cut/replacement question: the only mutators of states are track and the reset-clear, both of which bump; a HostList cut goes through reset. Clippy clean both feature sets, 229/2 (known baseline), draw:: 20/20 locally. Synthetic tests only; no game data.

**@⁠borosf** on GitHub · 2026-11-26 14:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/3249#issuecomment-7195616252) Fixed at `d4903faff4be45b0d597357ebebfe904d0d7d7c`: both halves of the never-hits bug. `HostList::track` now bumps `states_generation` only when an entry is inserted or its state actually changes (not on every call), `transition` bumps when a twin route moves the texture's side, and `State::bind` stores the generation read *after* the ensure loop — so a draw that rebinds the same targets with nothing moved skips the loop. On your cut/replacement question: the only mutators of `states` are `track` and the reset-clear, both of which bump; a HostList cut goes through reset. Clippy clean both feature sets, 229/2 (known baseline), draw:: 20/20 locally. Synthetic tests only; no game data.
Author
Owner

@⁠TheGrayOrange on GitHub · 2026-11-26 18:22 UTC · original

I reproduced a remaining segment-cut cache issue at 6654fb7.

Two host lists can have identical state-generation counters despite having different state maps. After State::cut replays the target binding, prepare_for can incorrectly skip enrolling the target in the new segment's state map.

A focused WARP test reproduced this. The subsequent transition to COPY_SOURCE reported no previous state.

Adding self.ensured = None; at the start of State::set_targets restored the expected RENDER_TARGET state. Both versions still performed only one target check across 1,023 same-segment preparations.

I have a focused reproduction and can provide the correction and regression test if useful.

**@⁠TheGrayOrange** on GitHub · 2026-11-26 18:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/3249#issuecomment-7200379207) I reproduced a remaining segment-cut cache issue at `6654fb7`. Two host lists can have identical state-generation counters despite having different state maps. After `State::cut` replays the target binding, `prepare_for` can incorrectly skip enrolling the target in the new segment's state map. A focused WARP test reproduced this. The subsequent transition to COPY_SOURCE reported no previous state. Adding `self.ensured = None;` at the start of `State::set_targets` restored the expected RENDER_TARGET state. Both versions still performed only one target check across 1,023 same-segment preparations. I have a focused reproduction and can provide the correction and regression test if useful.
The memo stored the old host list's states generation; a fresh list after
State::cut can read the same counter with different state maps, so a draw
could skip ensure_state and the new segment's map never enrolled the
targets (a later transition then reported no previous state). Clear it in
cut, with a regression test for the equal-counters case. Reported by
@TheGrayOrange on #3249. Synthetic test only; no game data.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <[email protected]>
Author
Owner

@⁠borosf on GitHub · 2026-11-26 18:39 UTC · original

Thanks @⁠TheGrayOrange — reproduced and fixed at fd2eb57: State::cut now clears self.ensured, since a fresh host list can read the same states_generation counter with a different state map. cut (list.rs:213) is the only place a State moves to a fresh list, so this covers the case exactly; regression test draw::tests::a_cut_to_a_fresh_list_forgets_the_ensured_targets encodes the equal-counters scenario (two fresh lists both read 0).

Local verification on Windows 11 x64, Intel Arc B580, head fd2eb57:

  • cargo test -p victory-gpu --lib --locked -- --test-threads=1 draw:: — 21/21 passed
  • cargo test -p victory-gpu --lib --locked --no-fail-fast — 230 passed, only the 2 known B580 baseline failures (#1744/#1829)
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings and --all-features — exit 0
  • fmt gate: 84 hunks on draw.rs, same as the origin/main baseline

Synthetic tests only; no game data used.

**@⁠borosf** on GitHub · 2026-11-26 18:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/3249#issuecomment-7200708230) Thanks @⁠TheGrayOrange — reproduced and fixed at `fd2eb57`: `State::cut` now clears `self.ensured`, since a fresh host list can read the same `states_generation` counter with a different state map. `cut` (list.rs:213) is the only place a `State` moves to a fresh list, so this covers the case exactly; regression test `draw::tests::a_cut_to_a_fresh_list_forgets_the_ensured_targets` encodes the equal-counters scenario (two fresh lists both read 0). Local verification on Windows 11 x64, Intel Arc B580, head `fd2eb57`: - `cargo test -p victory-gpu --lib --locked -- --test-threads=1 draw::` — 21/21 passed - `cargo test -p victory-gpu --lib --locked --no-fail-fast` — 230 passed, only the 2 known B580 baseline failures (#1744/#1829) - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` and `--all-features` — exit 0 - fmt gate: 84 hunks on draw.rs, same as the origin/main baseline Synthetic tests only; no game data used.
Author
Owner

@⁠TheGrayOrange on GitHub · 2026-11-26 19:33 UTC · original

@⁠borosf confirmed the fix at fd2eb57 independently on WARP. Both your regression test and my original reproduction passed. The new segment correctly tracks the render target, and the same-segment fast path remains intact.

**@⁠TheGrayOrange** on GitHub · 2026-11-26 19:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/3249#issuecomment-7201715797) @⁠borosf confirmed the fix at `fd2eb57` independently on WARP. Both your regression test and my original reproduction passed. The new segment correctly tracks the render target, and the same-segment fast path remains intact.
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/3249/head:github-bot/gh-3249'
git switch 'github-bot/gh-3249'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3249'
git switch 'github-bot/gh-3249'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3249'
git switch 'github-bot/gh-3249'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3249'
git switch 'main'
git merge --squash 'github-bot/gh-3249'
git switch 'main'
git merge --ff-only 'github-bot/gh-3249'
git switch 'main'
git merge 'github-bot/gh-3249'
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!3249
No description provided.