Repository navigation
Formats: bounds-check a detiled row once, not each run in it - #2026
0xkn0wh3r3 wants to merge 2 commits into
Conversation
`Layout::detile` and `Layout::tile` copy 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. The runs are now kept with what a row of them reaches (`Runs`): the furthest tiled byte from the row's start, and the linear row's bytes, which they cover. Each row's tiled and linear bytes are bounds-checked once as slices, and its runs copied from them without further checks (at the fixed widths Marvisak#603 added, as before). `Runs::new` asserts that every run lies in the linear row; every run lies in the tiled span by construction. Linear layouts are one run per row through the same path. Measured on a Ryzen 9 7900X (release; old and new alternating 40 times in one binary, best of each; same bytes out): | texture (64 KiB S unless noted) | before | after | |---|---|---| | RGBA8 3840x2160 | 5.71 ms | 3.93 ms (x1.45) | | RGBA8 3840x2160, 64 KiB D | 5.77 ms | 4.11 ms (x1.40) | | RGBA8 2048x2048, 256 B S | 1.50 ms | 1.31 ms (x1.14) | | R8 2048x2048 | 207 us | 157 us (x1.32) | | RGBA16F 3840x2160 | 10.17 ms | 8.23 ms (x1.24) | | BC1 4096x4096 | 506 us | 405 us (x1.25) | | BC7 4096x4096 | 1.96 ms | 1.66 ms (x1.18) | | RGBA32F 2048x2048 | 10.47 ms | 8.22 ms (x1.27) | 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.
|
Triage verdict — Promising follow-up to #603; verify the linear path and row boundaries ( Evidence
Checks and limits
Next step
This verification was AI-assisted. |
catminou4
left a comment
There was a problem hiding this comment.
Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, 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).
|
Verified on Windows 10 19045, head The two tests that matter for this change both pass: On the diff: making the bound the row's |
…ounds Review (Marvisak#2026) asked for the linear layout's cost and the row bounds. Timing (release, synthetic textures, detiling every mip, best of five): the linear path's one-entry run list, allocated per call, made small linear textures slower than main: 16x16 RGBA8 393 against 219 ns, 4x4 68 against 35 ns. A single run is now kept inline (`Runs::one`), and a row of one run is copied as one slice: 16x16 221-222 ns, 4x4 35 ns (main 219-227 and 35-41). 1024x1024 linear stays within the runs' spread; 64 KiB D 1024x1024 stays slightly faster than main. `Runs::copy` takes the row's two slices and checks, once per row, that they are the row's `span` tiled and `bytes` linear bytes; the runs are then copied unchecked as before, and the callers need no unsafe block. New test `rows_are_copied_exactly_at_their_bounds`: linear memory exactly as long as its last row (no slack), tiled memory exactly the layout's size, rows ending in a shorter run (asserted to occur), pitch padding between rows untouched, and tile leaving every tiled byte outside its subresource untouched; tiled and linear layouts, all mips.
|
Thanks @jeremygold02, the linear timing found a real regression. Fixed in 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, New test
It runs on both tiled and linear layouts, across all mips. All 39 formats tests pass. |
|
Re-verified on the new head 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. |
|
Automated triage for the maintainers.
CI: not run on this head (no check runs; the workflow may need approval). Reviewers report Recommending a look, not approving. Generated by Claude Code |
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 (NVIDIA D3D12 (70564ae): stride_tests & synthetic_tests fail due to HEAP_VIEWS (2M > 1M NVIDIA limit) and Stride::Structured with rawBufferLoad #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.