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

Open
github-bot wants to merge 2 commits from github-bot/gh-2026 into main AGit
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-25 03:28 UTC · original
0xkn0wh3r3:formats/detile-rows-checked-once → main · +196 −34 · 1 files · 2 commits · open

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 #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 (#1835).
  • cargo clippy -p victory-formats --all-targets, with and without --all-features: clean.

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.

**@⁠0xkn0wh3r3** on GitHub · 2026-11-25 03:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/2026) `0xkn0wh3r3:formats/detile-rows-checked-once` → `main` · +196 −34 · 1 files · 2 commits · open ## 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 #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 (#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 #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.
Review (#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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:51:58 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-25 03:31 UTC · original

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

Evidence

  • The patch keeps #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.

**@⁠jeremygold02** on GitHub · 2026-11-25 03:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/2026#issuecomment-7151995198) **Triage verdict — Promising follow-up to #603; verify the linear path and row boundaries** (`376946a`) **Evidence** - The patch keeps #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.
Author
Owner

@⁠Calvix2k on GitHub · 2026-11-25 03:57 UTC · original

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.

**@⁠Calvix2k** on GitHub · 2026-11-25 03:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/2026#issuecomment-7152391157) 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.
Author
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-25 06:31 UTC · original

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** on GitHub · 2026-11-25 06:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/2026#issuecomment-7154873453) 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.
Author
Owner

@⁠Calvix2k on GitHub · 2026-11-25 08:14 UTC · original

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.

**@⁠Calvix2k** on GitHub · 2026-11-25 08:14 UTC · [original](https://github.com/Marvisak/VIctory/pull/2026#issuecomment-7156789500) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 09:18 UTC · original · edited 2026-11-26 01:24 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 09:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/2026#issuecomment-7158156888) · edited 2026-11-26 01:24 UTC <!-- victory-triage v1 head=1ec3877c526ccbb8decafeb57a2c3722b2c7ff3a verdict=merge-candidate at=2026-11-26T01:24:14Z --> 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](https://claude.ai/code)_
Author
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-26 00:17 UTC · original

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** on GitHub · 2026-11-26 00:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/2026#issuecomment-7178609378) 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.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2026/head:github-bot/gh-2026'
git switch 'github-bot/gh-2026'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2026'
git switch 'github-bot/gh-2026'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2026'
git switch 'github-bot/gh-2026'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2026'
git switch 'main'
git merge --squash 'github-bot/gh-2026'
git switch 'main'
git merge --ff-only 'github-bot/gh-2026'
git switch 'main'
git merge 'github-bot/gh-2026'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2026
No description provided.