Skip to content

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

Open
borosf wants to merge 5 commits into
Marvisak:mainfrom
borosf:vibe/ensure-state-once-842ca5
Open

borosf wants to merge 5 commits into
Marvisak:mainfrom
borosf:vibe/ensure-state-once-842ca5

Conversation

@borosf

@borosf borosf commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

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

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.

…s a texture only when a barrier is owed

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 (Marvisak#1744/Marvisak#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.
@Bj0rnis

Bj0rnis commented Nov 26, 2026

Copy link
Copy Markdown

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 Test report: RTX 5070 (Blackwell), Windows 11, 70564ae: 471 passed, 5 failed, 7 ignored (4 from the 1,000,000 view heap cap) #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.

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

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

…loop, bumped only on a real move

Reviewer on Marvisak#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.
@borosf

borosf commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

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.

…:tests::marked_pages_go_back_by_the_bytes_the_gpu_changed under a parallel suite (FIND-20); it passes single-threaded locally on the B580

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
@TheGrayOrange

Copy link
Copy Markdown

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 Marvisak#3249. Synthetic test only; no game data.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
@borosf

borosf commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

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:

Synthetic tests only; no game data used.

@TheGrayOrange

Copy link
Copy Markdown

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

@borosf

borosf commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Confirmed independently on WARP at fd2eb57 — thank you. That closes the State::cut segment-memo hole you reproduced at 6654fb7: set_targets clears ensured, so a new segment's state map is enrolled again, and the same-segment fast path still holds (one target check across 1,023 same-segment preparations).

One cross-reference worth having on this PR: @KainanP's stack in #2586 names #3249 as the place that answers the target transitions' texture clones on the 5-thread path, which is what ensured was added for.

Also noting @Bj0rnis's RTX 5070 Ti run above (244 passed / 2 failed = the #1744 heap-cap baseline, WARP 246/0, clippy clean) so the three reports sit together. Head unchanged at fd2eb57; synthetic only, no game data.

101Leafy pushed a commit to 101Leafy/victory-leafy that referenced this pull request Nov 27, 2026
… epoch or the depth state it wants moved since the last draw (VIctory Marvisak#3249 by borosf)
@KainanP

KainanP commented Nov 27, 2026 •

Copy link
Copy Markdown

List-call numbers for this PR (head fd2eb57), ns per draw:

main main + #3249 stack stack + #3249
1 thread 2506 / 2557 2421 / 2430 (−3.4 % / −5.0 %) 1110 / 1114 1081 / 1116
5 threads 8959 / 9047 9148 / 9291 (noise) 1488 / 1460 1491 / 1475

Method. pipeline::list_bench from #2586 (fork branch perf/list-call-bench) with VICTORY_LIST_BENCH_SCALING=1: the draw sequence (root CBV, 4 root constants, index buffer, indexed draw that reads 6 textures through a table and 3 root CBVs). Each thread records its own list, best of 5 × 20,000 draws. Each row is the median of 7 interleaved runs, and there were two separate batches of 7 in opposite config order (shown as batch 1 / batch 2). Release --lib test build, RTX 4060 Laptop, Windows 11. Nothing else heavy was running (an editor and a browser, CPU 7–41 % between rounds, no game). The 5-thread row on main is noisy (single runs from 7.4 to 12.8 µs), so deltas under ~5 % there are not resolvable.

Bases. main = 63bcca4 + bench commits. Stack = main + #1978 + #2821 + #3258 + #3274 + #3275 + rebased #1356 + #3405 (the stack in #2586).

Reading it. This bench binds no render or depth targets, so it does not exercise the ensured memo. What it does measure is the other half, the texture clone that transition no longer makes, because every texture a draw reads goes through ensure_state → transition. On main that is worth 85–127 ns a draw on one thread. On the stack it is worth nothing, because #3258 already moves draw reads to transition_held, which clones only when a barrier is owed. Merging onto the stack conflicts in host.rs with #3258 (transition / transition_held). The resolution keeps #3258's split and adds this PR's twin-route bump and states_generation() there. The 5-thread row on main does not move, because it is bound by shared locks that this PR doesn't touch.

Correctness. cargo test -p victory-gpu --lib (release) on the RTX: 230 passed, 2 failed. The 2 failures are the #1744 heap-cap pair that main also fails here (229 + this PR's new test). On the stack: 238 passed, 3 failed, the same 3 as the stack without this PR (the stack itself fails 3: the #1744 pair, plus a_list_lends_a_read_copy_only_while_it_is_the_textures in the stack's port of #3405. That test passes on #3405's own head, 232/2 here). I read the memo against main. states changes only through track (which now bumps on insert or a real move) and reset (which bumps). A State moves to another host list only through cut (which clears ensured) or a title Reset (State::reset → default). A depth target with a twin goes back to track for the DEPTH side, and the UAV side bumps. I found no path that skips a needed transition.

Not measured. The targets memo itself (needs a bench row that binds targets), and any game frame.

AI use: Claude Code (Claude Opus 5.5).

The skip rested on claims nothing checked: that a list's states only move in
`track`/`reset`, that the memo holds the generation read *after* the ensure
loop, and that a bump brings the transitions back. The one test near it set
`State::ensured` by hand and returned early without a host GPU, so on CI it
asserted nothing at all.

Take the ensure loop out of `State::prepare_for` as `State::ensure_targets`,
with the transition passed in, and give the generation a real mutator:
`host::Generation::bump`, the one `track`, `reset` and the twin route call.
The transitions a draw asks for are then counted off the loop itself, headless:
two draws with the same targets and depth ask for none the second time, a third
target or another depth asks for them all again, and a bump asks for them again
until the next pass records the new generation. Reverting either half of the
predicate, or storing the generation read before the loop, fails these tests.

`Generation` also records why Relaxed carries it: every bump is made by the
code recording into the list, and a draw reads it holding the title list's
state mutex.

Synthetic tests only; no game data.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
101Leafy pushed a commit to 101Leafy/victory-leafy that referenced this pull request Nov 27, 2026
… epoch or the depth state it wants moved since the last draw (VIctory Marvisak#3249 by borosf)
101Leafy pushed a commit to 101Leafy/victory-leafy that referenced this pull request Nov 28, 2026
… epoch or the depth state it wants moved since the last draw (VIctory Marvisak#3249 by borosf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants