Formats: bounds-check a detiled row once, not each run in it #2024
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2026"
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 0xkn0wh3r3 on 2026-11-25T03:28:18Z.
The problem
Detiling and tiling a texture spent much of its time on bounds checks.
Layout::detileandLayout::tilecopy a subresource as the contiguous runs its columns make, the same runs on every row. Each run was a slice copy through a closure, bounds-checked on both sides. A 4K RGBA8 texture in a 64 KiB standard swizzle is about two million 16-byte runs, so about two million pairs of checks.The fix
Runs): the furthest tiled byte from the row's start, and the linear row's bytes, which they cover.Runs::newasserts that every run lies in the linear row. Every run lies in the tiled span by construction.Measured on a Ryzen 9 7900X (release; old and new alternating 40 times in one binary, best of each; same bytes out):
Copying rows in bands (each run for several rows at once, to stay in a micro-tile) was slower at every band size tried (4, 8, 16) and is not done.
Testing
addresses_are_a_bijection_and_copies_round_trip(detile and tile round trips over every addressable mode),layouts_match_addrlib, the mip-tail and deep-volume tests, and the depth-pitch tests.cargo test --release -p victory-formats: 38 passed. The 3 ignored tests that need reference data files (bcpack::tests::reference_chunks,xma::tests::matches_a_reference_decoder,xma::tests::walks_saved_streams) were not run.cargo test --release -p victory-gpu(its texture uploads detile through this): all pass exceptdescriptor::host::tests::dynamic_slots_are_held_until_releasedandmemory::tests::lists_share_a_table_and_copies_keep_their_views, which fail the same way onmainon this RTX 5070 Ti (#1835).cargo clippy -p victory-formats --all-targets, with and without--all-features: clean.Related
Complementary to #1135 (detile only the subresources written), #1873 (one scratch buffer) and #388 (detile into cached memory): those make fewer or cheaper detiles, this makes each one faster. Builds on #603's fixed-width runs.
Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T03:34:32Z.
Review: APPROVED
Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main
940d32f69b,cargo test -p victory-formats --locked.38 passed / 0 failed. Detiled row bounds-checked once, not per run — perf fix in the detile path; 38 passed / 0 failed / 5 ignored (formats baseline on current main).
Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-25T03:31:31Z.
Triage verdict — Promising follow-up to #603; verify the linear path and row boundaries (
376946a)Evidence
Vecwith a direct row copy to a one-entry run list. The posted speedups cover swizzled layouts, so they leave that separate cost unmeasured.Checks and limits
Next step
This verification was AI-assisted.
Imported from Marvisak/VIctory. Originally posted by Calvix2k on 2026-11-25T03:57:06Z.
Verified on Windows 10 19045, head
376946a:cargo test -p victory-formats --lib— 38 passed, 0 failed, 5 ignored.The two tests that matter for this change both pass:
addresses_are_a_bijection_and_copies_round_trip(the property that would catch a wrongspan/bytesin the new row contract — theunsafecopy trusts exactly those) andlayouts_match_addrlib. The ignored five are the pre-existing#[ignore]d ones, not skipped by this change.On the diff: making the bound the row's
span/bytesand keeping the per-run offsets relative to it is the only way to hoist the check, and theSAFETYcomment names the invariant. If you want belt and braces for the unsafe block, a debug-onlyassert!(runs.span <= … && runs.bytes <= …)insideRuns::copywould document it at the point of use; the round-trip test is what covers it today.Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-25T06:31:09Z.
Thanks @jeremygold02, the linear timing found a real regression. Fixed in
8bd4270.Timing (release build, synthetic RGBA8 textures, detiling every mip, best of five, two alternating runs each):
The one-entry run list for linear layouts was allocated on every call. A single run is now kept inline, and a row with one run is copied as a single slice.
Bounds: following @Calvix2k's suggestion,
Runs::copynow takes the row's two slices and checks once per row that they are exactly itsspantiled andbyteslinear bytes. The runs are then copied without checks as before, and the call sites no longer needunsafe.New test
rows_are_copied_exactly_at_their_bounds:tileleaves every tiled byte outside its subresource untouched.It runs on both tiled and linear layouts, across all mips. All 39 formats tests pass.
Imported from Marvisak/VIctory. Originally posted by Calvix2k on 2026-11-25T08:14:45Z.
Re-verified on the new head
8bd4270, Windows 10 19045 (RX 7800 XT):cargo test -p victory-formats --lib— 39 passed, 0 failed, 5 ignored (one more test than before, the new linear-row case).The linear-timing regression jeremygold02 found and the fix make sense together: the run list is only needed for tiled layouts, and linear rows copy as one span. Good fix, nothing else to add.
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T09:18:35Z.
Automated triage for the maintainers.
Merge candidate. The new head
1ec3877is the same change rebased onto currentmain, plus the nit from the last triage: the trailing blank line at the end ofcrates/victory-formats/src/texture.rsis gone. It merges cleanly.I re-read
Runsand itscopyagainst the rebased file.copystill asserts that each row's slices are exactlyspantiled andbyteslinear bytes before the uncheckedcopy_nonoverlappingruns, andRuns::newasserts every run lies inbytes. Linear rows still take the inlineRuns::onepath with a single slice copy.rows_are_copied_exactly_at_their_boundsstill covers exact-sized buffers, a partial last run and untouched padding.CI: passing (
check). Recommending a look, not approving.Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-26T00:17:53Z.
Thanks for the review. The nit is fixed in
1ec3877: the branch no longer adds a blank line at the end oftexture.rs. It's also rebased onto currentmain.victory-formatstests (47 passed), clippy and policy pass. @Calvix2k thanks for re-verifying on the RX 7800 XT.Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T19:06:50Z.
Review: APPROVED
Verified locally merged onto
origin/main(63bcca43, Windows 11 x64, Rust 1.98.1 MSVC):cargo test --locked -p victory-formats— 47 passed, 0 failed, 5 ignored (includingrows_are_copied_exactly_at_their_bounds)cargo clippy --locked -p victory-formats --all-targets -- -D warnings(default and--all-features) — 0 warningscargo xtask policy— ok (191 files ok)Review notes
&tiled[t..][..runs.span]/&linear[l..][..runs.bytes]) match the previous per-run touched range exactly across partial last runs, BC blocks, mip tails, arrays, 3D slices, and tight linear row pitches, whileRuns::onekeeps the linear path free of heap allocations and raw pointer copies.Layout,Mip,Tail, andDescexposepubfields, safe code that mutates them could overflowt + lenorl + leninRuns::new(texture.rs:755-756) under release builds (or 32-bitusize), causingspan/byteschecks to pass beforecopy_runs'sunsafe copy_nonoverlapping. Switching both endpoint sums inRuns::newtochecked_addcloses that hole cleanly.detileandtile(texture.rs:612,630) exceedrustfmtline width.Imported from Marvisak/VIctory. Originally posted by ThomasHFWright on 2026-11-27T09:53:34Z.
We tested a downstream port of
1ec3877on swizzled textures. Two runs of 16 alternating baseline/port pairs checked byte-for-byte output parity. Median paired speedups were 1.378× and 1.235× for BC1, and 1.106× and 1.252× for BC7. RGBA16F varied from 0.959× to 1.167×. Those three formats would be worth including in a repeat on the PR head. These timings cover CPU detiling in isolation. LinVxtory benchmark and regression notesView 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.