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
Contributor

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.

@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
@0xkn0wh3r3

Copy link
Copy Markdown
Contributor 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.

0xkn0wh3r3 added a commit to 0xkn0wh3r3/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.
@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.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. The new head 1ec3877 is the same change rebased onto current main, plus the nit from the last triage: the trailing blank line at the end of crates/victory-formats/src/texture.rs is gone. It merges cleanly.

I re-read Runs and its copy against the rebased file. copy still asserts that each row's slices are exactly span tiled and bytes linear bytes before the unchecked copy_nonoverlapping runs, and Runs::new asserts every run lies in bytes. Linear rows still take the inline Runs::one path with a single slice copy. rows_are_copied_exactly_at_their_bounds still covers exact-sized buffers, a partial last run and untouched padding.

CI: passing (check). Recommending a look, not approving.


Generated by Claude Code

0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 25, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
0xkn0wh3r3 added a commit to 0xkn0wh3r3/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
0xkn0wh3r3 force-pushed the formats/detile-rows-checked-once branch from 8bd4270 to 7967ef6 Compare November 25, 2026 22:59
`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.
…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
0xkn0wh3r3 force-pushed the formats/detile-rows-checked-once branch from 7967ef6 to 1ec3877 Compare November 26, 2026 00:17
@0xkn0wh3r3

Copy link
Copy Markdown
Contributor Author

Thanks for the review. The nit is fixed in 1ec3877: the branch no longer adds a blank line at the end of texture.rs. It's also rebased onto current main. victory-formats tests (47 passed), clippy and policy pass. @Calvix2k thanks for re-verifying on the RX 7800 XT.

0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 26, 2026
0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 26, 2026
… staging test with Marvisak#2149's levels

Marvisak#2879's render-target XOR path calls the row visitor with a run per 16-byte piece (Marvisak#2026 made rows runs), and its test's staging reads every level (Marvisak#2149).
0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 26, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026

@fuzeofficlal fuzeofficlal 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.

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 (including rows_are_copied_exactly_at_their_bounds)
  • cargo clippy --locked -p victory-formats --all-targets -- -D warnings (default and --all-features) — 0 warnings
  • cargo xtask policy — ok (191 files ok)

Review notes

  • The upfront row slice checks (&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, while Runs::one keeps the linear path free of heap allocations and raw pointer copies.
  • Non-blocking soundness hardening suggestion: because Layout, Mip, Tail, and Desc expose pub fields, safe code that mutates them could overflow t + len or l + len in Runs::new (texture.rs:755-756) under release builds (or 32-bit usize), causing span/bytes checks to pass before copy_runs's unsafe copy_nonoverlapping. Switching both endpoint sums in Runs::new to checked_add closes that hole cleanly.
  • Minor non-blocking formatting nit: detile and tile (texture.rs:612, 630) exceed rustfmt line width.

@ThomasHFWright

Copy link
Copy Markdown

We tested a downstream port of 1ec3877 on 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 notes

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.

7 participants