Skip to content

GPU: a draw reads the swizzle it already has, and the release frame line costs nothing - #3238

Open
borosf wants to merge 2 commits into
Marvisak:mainfrom
borosf:vibe/frame-waste-cluster-842ca5
Open

borosf wants to merge 2 commits into
Marvisak:mainfrom
borosf:vibe/frame-waste-cluster-842ca5

Conversation

@borosf

@borosf borosf commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Four pieces of per-draw waste found by a sweep of the world frame, all of them work that is computed and then thrown away. No behaviour change: the same values, read instead of rebuilt.

What each one cost

A placed texture's swizzle was derived by building the whole console layout. texture_layout builds a texture::Texture — three heap allocations plus one Mip per level — to answer with a single u32. Two per-draw paths did it:

  • memory_key, which current() reaches for every placed texture a heap slot reads (texture_at → current → memory_key) and shared_texture reaches for every title texture of one memory;
  • alias_source's bytes closure, reached from draw.rs's readable for every texture view of every draw, and again for every candidate it compares against.

footprint already called texture_layout once at creation and threw the result away. The resource now keeps the swizzle from that point and both paths read the word.

Deferred's two dedup sets were hash sets. A list asks defer_refresh a few dozen times; SipHash over a 24-byte tuple plus a set probe is ~20 ns where a linear scan of three words is under 5. refreshed and textures are now lists, with first_use and both defer_refresh/defer_heap keeping their answers.

memory::note_use filled a diagnostics-only map in release builds. Stats::uses (a BTreeMap<String, u64>) is read by exactly one place: the once-a-frame line that names the busiest shaders. Filling it cost a global mutex, a String allocation and a BTreeMap descent per draw per memory-reading stage, in every build. It is now #[cfg(feature = "diagnostics")], and an AtomicU64 (USES) carries the count the release line prints, so the line reads the same.

memory::log_frame built its output before asking whether to write it. It drained the stats, sorted the map and formatted up to 32 Strings, then called tracing::info!. It now checks tracing::enabled!(Level::INFO) first.

Measured

Intel Arc B580, Windows 11, --release:

cargo test --release -p victory-gpu --lib --locked -- --exact --ignored --nocapture \
  resource::tests::a_swizzle_costs_no_layout_per_placed_texture

2048x2048, 12 mips: 290 ns to derive it, 0.00 ns to read the kept one
1920x1080 render target: 263 ns to derive it, 0.00 ns to read the kept one

Synthetic descriptions only. The per-frame total is that cost times the number of placed-texture reads and texture views a world frame makes, which this PR does not attempt to count — the counters for it belong to the follow-ups claimed for the same sweep.

Verification

Synthetic tests only; no game data was used, and none enters the repo.

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

A placed-texture read asked a texture for its swizzle by building the
whole console layout: three allocations and the full mip chain, under a
lock, to answer with one `u32`. Two paths did it per draw — `memory_key`,
which `current()` and `texture_at` reach for every placed texture a heap
slot reads, and `alias_source`'s row geometry, which every texture view of
every draw reaches. The resource now works the swizzle out once at
creation, where `footprint` already called the same function, and both
paths read the word.

`Deferred`'s two dedup sets are lists. A list asks a few dozen times, and
a linear scan over three words beats SipHash over a tuple plus a set
probe at that length; `first_use` and both `defer_refresh`/`defer_heap`
keep their answers.

`memory::note_use` becomes an atomic count, and the per-pipeline map that
names the busiest shaders is diagnostics only — nothing but the frame line
reads it, and filling it cost a global lock, a `String` allocation and a
`BTreeMap` descent per draw per memory-reading stage. The release line
still prints the same count, from `USES`.

`memory::log_frame` asks whether the info level is enabled before it sorts
the map and formats up to 32 `String`s, so a run logging above it no
longer builds them for nothing.

Measured on an Intel Arc B580, `cargo test --release -p victory-gpu --lib
-- --exact --ignored --nocapture
resource::tests::a_swizzle_costs_no_layout_per_placed_texture`:
deriving a 2048x2048 12-mip texture's swizzle costs 290 ns, a 1920x1080
render target's 263 ns; reading the kept word costs nothing measurable.
Synthetic descriptions only, no game data.

Synthetic tests only; no game data.

@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 verified locally on Windows 11 x64 (RTX 5090):

  • Caching layout_swizzle on Resource at creation avoids rebuilding the full console mip layout per memory_key / �lias_source lookup; gating Stats::uses behind diagnostics with an atomic USES counter and racing::enabled!(INFO) guard removes per-draw global lock/allocation overhead in non-diagnostics builds; small Vec lookups for Deferred::{refreshed, textures} are clean.
  • cargo test -p victory-gpu --lib resource:: -- --test-threads=1 (RTX 5090): 18 passed, 0 failed, 1 ignored (�_swizzle_costs_no_layout_per_placed_texture).
  • cargo test -p victory-gpu --lib memory::tests:: -- --test-threads=1 --skip lists_share_a_table_and_copies_keep_their_views (RTX 5090): 7 passed, 0 failed (pre-existing #1744 test skipped).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo clippy -p victory-gpu --features diagnostics --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

@KainanP

KainanP commented Nov 26, 2026

Copy link
Copy Markdown

Measured on main 63bcca4 with this PR merged (RTX 4060 Laptop, release, median of 7 interleaved runs; method in #2586 (comment)):

  • alias_source: 120 → 14 ns, and an indexed draw reading six textures: 2713 → 1966 ns. Good.
  • But Deferred::refreshed as a Vec with contains grows with the list. In a sequence where each draw binds a new constant-ring position (root CBV, 4 root constants, index buffer, draw; lists reset every 2,000 calls), the sequence costs 2388 ns per draw, against 1966 for the bare draw. On main and on every other build measured, the setters add ~70 ns. A world frame's lists refresh thousands of distinct ranges, so the scan is O(n) per refresh and O(n²) per list. A set with a cheap hash (GPU: per-list and per-submission sets hash with FxHash, not SipHash #2102's FxHash) keeps the gain without that cost. GPU: no texture layouts or name copies per draw for unchanged answers #1978 has the same alias_source change alone.

AI use: Claude Code (Claude Opus 5.5) ran the measurements and drafted this comment.

Reviewer measurement on Marvisak#3238 (RTX 4060 Laptop): the Vec scan kept the
alias_source win but a draw sequence that binds a new constant-ring
position each draw cost 2388 ns against 1966 ns bare, because the scan is
O(n) per refresh and O(n^2) per list, while a world frame's lists refresh
thousands of distinct ranges. SipHash over the tuple was the original cost.

`Deferred::refreshed` and `Deferred::textures` are now `FastSet` — a
`HashSet` with an in-tree FxHash (the same multiply-rotate step
victory-shader's structure.rs uses; no new dependency), keeping insert at
O(1) regardless of list length.

Synthetic tests only; no game data.
@borosf

borosf commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Fixed at b5e79ab9b7999caef232e4025e17397f0919080a: Deferred::refreshed and Deferred::textures are now FastSet — a HashSet with an in-tree FxHash (the same multiply-rotate step as victory-shader's structure.rs; no new dependency) — so insert is O(1) at any list length instead of the O(n) Vec scan your sequence measured (2388 ns vs 1966 ns bare draw). The alias_source change is untouched and keeps your 120 → 14 ns. Clippy clean on both feature sets, 229 passed / 2 failed (the known #1744/#1829 baseline), rustfmt hunks host.rs 85/85. Synthetic tests only; no game data.

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.

3 participants