Skip to content

Formats: bounds-check a detiled row once, not each run in it - #2026

Open
0xkn0wh3r3 wants to merge 2 commits into
Marvisak:mainfrom
0xkn0wh3r3:formats/detile-rows-checked-once
Open

0xkn0wh3r3 wants to merge 2 commits into
Marvisak:mainfrom
0xkn0wh3r3:formats/detile-rows-checked-once

Conversation

@0xkn0wh3r3

@0xkn0wh3r3 0xkn0wh3r3 commented Nov 25, 2026 •

Copy link
Copy Markdown

The problem

Detiling and tiling a texture spent much of its time on bounds checks.

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, so about two million pairs of checks.

The fix

  • 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. Its runs are then copied from them without further checks (at the fixed widths Formats: inline repeated micro-tile copies when detiling textures #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.

Testing

  • The existing layout tests run through the new path: 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 except descriptor::host::tests::dynamic_slots_are_held_until_released and memory::tests::lists_share_a_table_and_copies_keep_their_views, which fail the same way on main on 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.

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

Copy link
Copy Markdown
Contributor

Triage verdict — Promising follow-up to #603; verify the linear path and row boundaries (376946a)

Evidence

  • The patch keeps Formats: inline repeated micro-tile copies when detiling textures #603’s fixed-width runs and checks the tiled and linear spans once per row before the raw copies. It also changes the linear layout path from an empty Vec with 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

  • The reported formats suite includes existing round trips and pitch checks. I reviewed the row-copy contract and the posted timings without rerunning the benchmark. At review I found no completed exact-head check.

Next step

  • Add a linear-layout timing, including a small texture or mip where the new run-list allocation may matter. For the raw-copy change, a focused tile/detile fixture with a partial final run, exact-sized buffers, and untouched pitch padding would make the boundary guarantee explicit alongside the existing round trips.

This verification was AI-assisted.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@Calvix2k

Copy link
Copy Markdown
Contributor

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 wrong span/bytes in the new row contract — the unsafe copy trusts exactly those) and layouts_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/bytes and keeping the per-run offsets relative to it is the only way to hoist the check, and the SAFETY comment names the invariant. If you want belt and braces for the unsafe block, a debug-only assert!(runs.span <= … && runs.bytes <= …) inside Runs::copy would document it at the point of use; the round-trip test is what covers it today.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
…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.
@0xkn0wh3r3

Copy link
Copy Markdown
Author

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

Case main PR before PR now
linear 16×16, all mips 219–227 ns 393 ns 221–222 ns
linear 4×4, one mip 35–41 ns 68 ns 35 ns
linear 1024×1024, all mips 768–843 µs 851 µs 760–821 µs
64 KiB D 1024×1024, mip 0 667–673 µs 710 µs 649–652 µs

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::copy now takes the row's two slices and checks once per row that they are exactly its span tiled and bytes linear bytes. The runs are then copied without checks as before, and the call sites no longer need unsafe.

New test rows_are_copied_exactly_at_their_bounds:

  • the linear buffer ends exactly at its last row, and the tiled buffer is exactly the layout's size;
  • some rows end in a shorter run, and the test asserts that this actually happens;
  • pitch padding between rows is left untouched;
  • tile leaves every tiled byte outside its subresource untouched.

It runs on both tiled and linear layouts, across all mips. All 39 formats tests pass.

@Calvix2k

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.
Merge candidate.

  • In crates/victory-formats/src/texture.rs, Layout::walk now passes each row to detile/tile as a Runs, and Runs::copy checks the row's tiled span and linear bytes once before copying each run with ptr::copy_nonoverlapping. I checked the safety argument and it holds. span is the furthest end of any run by construction. Runs::new asserts that every run lies within bytes. The two slices come from a shared borrow and an exclusive one, so they cannot overlap. Because the last run ends at bytes, the new row checks cover exactly what the old per-run checks covered.
  • The linear regression found in review is fixed by keeping the single run inline (Runs::one). The new rows_are_copied_exactly_at_their_bounds test uses exact-sized buffers and a partial last run, and checks that pitch padding and the tiled bytes outside the subresource are left untouched. This adds unsafe code to a hot path. The measured gain is 1.1-1.45x.
  • Nit: the patch adds a blank line at the end of texture.rs, which rustfmt would remove. No other open PR changes this function, apart from bundles that carry the code already merged from Formats: inline repeated micro-tile copies when detiling textures #603.

CI: not run on this head (no check runs; the workflow may need approval). Reviewers report victory-formats passing on Windows at 8bd4270.

Recommending a look, not approving.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants