GPU: a draw reads the swizzle it already has, and the release frame line costs nothing #3236
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3238"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-26T14:14:12Z.
Four pieces of work in the world frame that run per draw, per list or per frame. No behaviour change: the same swizzle, the same dedup, the same count in the frame line — computed once instead of repeatedly, or only when something is going to read it.
What the diff does
A texture keeps the swizzle its description implies.
memory_keyneeds the console layout's swizzle word, and it got it by callingtexture_layout(desc)— which builds a whole texture (three allocations and the full mip chain) to return oneu32.alias_source'sbytesclosure needed the same word. The expression is now thelayout_swizzle(desc)helper and is evaluated once per resource, inhand_out_resource, into a new#[cfg(windows)] swizzle: u32field onResource;memory_key(address, desc, swizzle)andshared_texturetake the word as a parameter, andcurrentandalias_source— for the placed texture and for every candidate it compares — pass that resource's own.layout_swizzlestill callstexture_layout, andfootprintstill builds its own layout at creation, so creation now pays for one more build; what the diff removes is the per-draw re-derivation.Deferred's two dedup sets hash with Fx.refreshed: HashSet<(usize, usize, usize)>andtextures: HashSet<usize>keep their type, their contents and their logic (insertbefore the work,clearwhen the list resets). What changes is the hasher:host.rsgains a privatestruct Fx(u64)implementing the FxHash multiply-rotate step, andtype FastSet<T> = HashSet<T, BuildHasherDefault<Fx>>, which both fields now use. The comment in the diff gives the reason — keys of a few words each, SipHash costing more than the lookup it guards, and aVecrejected because its scan is O(n) per refresh and O(n²) per list. There is no list and nofirst_usehere, anddefer_refresh/defer_heapare unchanged.Stats::usesexists only in diagnostics builds. The map (BTreeMap<String, u64>) has one reader, the once-a-frame line that names the busiest shaders, and filling it cost a lock, aStringand aBTreeMapdescent per draw per memory-reading stage in every build. It is now#[cfg(feature = "diagnostics")](diagnosticsis an empty, non-default feature). The count survives without it:note_usebumps a newstatic USES: AtomicU64(relaxed) in every build and fills the map only where it is compiled in, withlet _ = pipeline;where it is not.memory::log_frameasks before it builds. It still drains (stats(std::mem::take)), then returns early when nothing happened —USESswapped to zero and every counter zero — and returns again when!tracing::enabled!(Level::INFO), before the map is sorted and up to 32 pipeline strings are formatted. The line's{} draws/dispatches ({})count now comes fromUSESin every build; the parenthesised list is built from the diagnostics-only map, so outside the feature it prints empty.The numbers this diff can support
The diff adds one measurement,
resource::tests::a_swizzle_costs_no_layout_per_placed_texture, which is#[ignore = "timing"]d and asserts nothing — it prints and returns. Recorded when the branch was pushed (Intel Arc B580, Windows 11,--release,--exact --ignored --nocapture, synthetic descriptions):The derive side is
layout_swizzleper call: that is the cost the diff stops paying per draw. The read side times an index into a[u32; 8], so "0.00 ns" means "below this loop's resolution", not "a field read is free". Because the test asserts nothing, none of it fails if a later change makes the second side slower.The dedup sets and the
log_frameearly return are unmeasured here: no benchmark, no counter, no before/after. Per-frame totals are not claimed either — how many placed-texture reads and texture views a world frame makes is not counted in this PR.Verification
Recorded for this branch (not re-run for this body edit):
cargo clippy -p victory-gpu --all-targets --locked -- -D warnings— clean.cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings— clean.cargo test -p victory-gpu --lib --locked --no-fail-fast— 229 passed, 2 failed. Both failures are this card's known baseline failures that pristinemainalso fails on the same machine:descriptor::host::tests::dynamic_slots_are_held_until_releasedandmemory::tests::lists_share_a_table_and_copies_keep_their_views(#1744/#1829, the Intel view-heap cap).rustfmt --edition 2024 --checkper file, hunks againstmain(re-checked for this body edit with the same command on amaintree at the same paths):resource.rs64/64,host.rs85/85,memory.rs30/30 — no new formatting hunks (the tree is deliberately notrustfmt-clean).Synthetic descriptions and 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.
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T14:34:14Z.
Review: APPROVED
Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090):
Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-26T14:50:07Z.
Measured on main
63bcca4with this PR merged (RTX 4060 Laptop, release, median of 7 interleaved runs; method in https://github.com/Marvisak/VIctory/issues/2586#issuecomment-7195391455):alias_source: 120 → 14 ns, and an indexed draw reading six textures: 2713 → 1966 ns. Good.Deferred::refreshedas aVecwithcontainsgrows 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 (#2102's FxHash) keeps the gain without that cost. #1978 has the samealias_sourcechange alone.AI use: Claude Code (Claude Opus 5.5) ran the measurements and drafted this comment.
Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-26T14:59:09Z.
Fixed at
b5e79ab9b7999caef232e4025e17397f0919080a:Deferred::refreshedandDeferred::texturesare nowFastSet— aHashSetwith an in-tree FxHash (the same multiply-rotate step as victory-shader'sstructure.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.Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-27T11:43:21Z.
Built my open PRs as one stack (
main63bcca43+ 20 of mine) to check they hold together on the title, and this one conflicts with #3215-era code inmemory.rs— worth recording because the conflict resolution is better than either side alone.Both sides end up in
note_use:USES.fetch_add, puts thes.usesmap behind#[cfg(feature = "diagnostics")], and keepslet _ = pipeline;for release builds — so a release build pays one atomic and no mutex per draw.stats(...)unconditional but avoids aStringper call withget_mut+insertonly on the miss (to_ownedon every call was allocating and freeing aStringa draw).statsis not gated (fn stats<R>(f: impl FnOnce(&mut Stats) -> R) -> R, unconditionalSTATSmutex), so the two are not exclusive — the merged form keeps the atomic and the allocation-free map update:That is my whole point in writing it down: whichever of the two lands first, the other should not revert the atomic or the
get_mut— a release build should not takeSTATS's mutex for a counter it does not print, and a diagnostics build should not allocate a key that is almost always already there.I have not changed this PR; the stack is a local branch. With the resolution above the stack builds,
cargo test -p victory-gpu --libis 243 passed with the same two failures this host has onmain(dynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_views— both need a heap that reachesHEAP_VIEWS, which Arc caps), and the title boots to its menus exactly asmaindoes (30 fps, 13 draws, 0 device removals, 0 title crashes in 110 s).View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.