specialize fixed-width runs in texture tiling and detiling #1097

Closed
github-bot wants to merge 1 commit from gh-pr/1097/head into gh-pr/1097/base
Owner

@⁠matheusfrta on GitHub · 2026-11-24 15:42 UTC · original
matheusfrta:patch-1795534971 → main · +185 −43 · 1 files · 1 commits · closed without merging

Specializes Layout::detile and Layout::tile for the fixed 4, 8, 16, and 32 byte runs from RDNA2 micro-tiles so the inner loop emits fixed-size copies instead of dynamic memcpy calls, while handling shorter trailing runs for odd widths. Also replaces power-of-two divisions in row, column, and split with shifts and masks.

**@⁠matheusfrta** on GitHub · 2026-11-24 15:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/1097) `matheusfrta:patch-1795534971` → `main` · +185 −43 · 1 files · 1 commits · closed without merging Specializes Layout::detile and Layout::tile for the fixed 4, 8, 16, and 32 byte runs from RDNA2 micro-tiles so the inner loop emits fixed-size copies instead of dynamic memcpy calls, while handling shorter trailing runs for odd widths. Also replaces power-of-two divisions in row, column, and split with shifts and masks.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:19:42 +00:00
github-bot closed this pull request 2026-11-26 04:19:42 +00:00
Author
Owner

@⁠MichaelSmithAI on GitHub · 2026-11-24 15:52 UTC · original

Independent CPU check on ceac35b: 900 subresources produced byte-identical tiled buffers against both main and our staged texture implementation. Both detilers also recovered the patterned input and preserved row padding. The cases included odd widths, block-compressed formats, arrays, volumes and mip tails. All ten existing texture tests passed.

For a 1919×1079 single-mip texture (4 bytes per texel, SW_64KB_D), we timed batches of 12 tile+detile pairs and alternated which implementation ran first over seven rounds. The median batch took 15.827 ms with the staged implementation, which already specializes fixed runs in 04f6244, and 12.994 ms with this PR. That is about 18% less time for this conversion loop. Title frame time remains unmeasured.

**@⁠MichaelSmithAI** on GitHub · 2026-11-24 15:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/1097#issuecomment-7136748424) Independent CPU check on `ceac35b`: 900 subresources produced byte-identical tiled buffers against both main and our staged texture implementation. Both detilers also recovered the patterned input and preserved row padding. The cases included odd widths, block-compressed formats, arrays, volumes and mip tails. All ten existing texture tests passed. For a 1919×1079 single-mip texture (4 bytes per texel, SW_64KB_D), we timed batches of 12 tile+detile pairs and alternated which implementation ran first over seven rounds. The median batch took 15.827 ms with the staged implementation, which already specializes fixed runs in `04f6244`, and 12.994 ms with this PR. That is about 18% less time for this conversion loop. Title frame time remains unmeasured.
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 16:29 UTC · original

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at ceac35b: cargo test -p victory-formats passes, 24 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

**@⁠Zipettte** on GitHub · 2026-11-24 16:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/1097#issuecomment-7137750046) Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at ceac35b: `cargo test -p victory-formats` passes, 24 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Author
Owner

@⁠necrobytes on GitHub · 2026-11-24 16:50 UTC · original

I compared #603 and #1097 independently against main on Fedora 45, Ryzen 5 9600X, with Rust 1.98.1 (x86_64-unknown-linux-gnu). I used ffe310e for all three builds. #603 (705a2f6) merged cleanly onto it, and #1097 (ceac35b) already had it as its parent.

#1097 took 5–68% less time than #603 to detile the six swizzled textures. For tiling the two RGBA8 inputs and the odd-width R8 input, #603 was faster in all 12 paired rounds for each input. Both patches improved on main for those three tiling cases. #1097 also had the lowest median in both directions for BC1, BC7, and the R8 volume.

Median milliseconds per complete texture

Synthetic texture Detile main Detile #603 Detile #1097 Tile main Tile #603 Tile #1097
RGBA8 1920×1080, 64KB_D 0.739 0.338 0.280 0.714 0.383 0.655
RGBA8 1919×1079, 64KB_D 0.748 0.344 0.286 0.718 0.394 0.627
R8 1919×1079, 4KB_D 0.369 0.167 0.129 0.366 0.165 0.289
BC7 4096×4096, 64KB_D 1.686 0.835 0.794 1.042 0.681 0.644
BC1 4096×4096, 64KB_S 0.740 0.401 0.364 0.726 0.353 0.324
R8 257×129×17, 4KB_S 0.222 0.216 0.069 0.222 0.221 0.185
RGBA8 1920×1080, linear 0.113 0.113 0.113 0.113 0.115 0.113

I repeated the three uncompressed 2D swizzled cases on a second physical core, varying the destination’s offset relative to the source modulo 4 KiB (0, 64, 512 and 2,048 bytes). #603 won all 72 paired tiling comparisons there. #1097 won all 72 detiling comparisons.

Correctness

On each revision, I checked 5,369 subresources across 679 synthetic layouts. An independent driver used unmodified main’s Layout::offset to construct the expected destination for each direction, with separate inputs for tile and detile. All 14,503,816 element offsets and all 10,738 pairs of full-buffer comparisons matched, including row/plane padding and untouched mips and slices. The cases included 1D and 2D arrays, 3D volumes, block compression, and 2,540 mip-tail subresources. All 20 invalid/unsupported-argument checks returned the expected error and preserved the destination. The victory-formats crate tests passed on each build (main and #603 each 23 passed, #1097 24 passed, four ignored on each).

Timing method

I used one pinned logical CPU and preallocated, tightly pitched buffers with one mip and one sample. I timed complete calls to Layout::tile and Layout::detile separately. Each process warmed up with 16 copies, then measured five batches of 16. Each table entry is the median of 12 process medians, with all six orders of the three revisions used twice. These measurements cover warm CPU texture copies on this Linux host. Gameplay frame time would need a separate measurement.

Reproduce an individual timing

Save the driver below as bench.rs outside the checkout. With benchdir set to that directory, run the following from each source tree. The driver prints five batch times in nanoseconds per copy and an output digest.

rustc +1.98.1 --edition=2024 -O --crate-type=rlib --crate-name=victory_formats \
  crates/victory-formats/src/lib.rs -o "$benchdir/libvictory_formats.rlib"
rustc +1.98.1 --edition=2024 -O "$benchdir/bench.rs" \
  --extern victory_formats="$benchdir/libvictory_formats.rlib" -o "$benchdir/bench"
taskset -c 4 "$benchdir/bench" rgba8 tile
taskset -c 4 "$benchdir/bench" rgba8 detile
use std::hint::black_box;
use std::time::Instant;
use victory_formats::texture::{Desc, Dimension, Layout, Swizzle};

fn main() {
    let mut args = std::env::args().skip(1);
    let case = args.next().expect("texture case");
    let direction = args.next().expect("tile or detile");
    let (width, height, depth, block, element_bytes, swizzle) = match case.as_str() {
        "rgba8" => (1920, 1080, 1, 1, 4, Swizzle::SW_64KB_D),
        "bc7" => (4096, 4096, 1, 4, 16, Swizzle::SW_64KB_D),
        "bc1" => (4096, 4096, 1, 4, 8, Swizzle::SW_64KB_S),
        "r8-odd" => (1919, 1079, 1, 1, 1, Swizzle::SW_4KB_D),
        "rgba8-odd" => (1919, 1079, 1, 1, 4, Swizzle::SW_64KB_D),
        "r8-volume" => (257, 129, 17, 1, 1, Swizzle::SW_4KB_S),
        "linear" => (1920, 1080, 1, 1, 4, Swizzle::LINEAR),
        _ => panic!("unknown case {case}"),
    };
    let layout = Layout::new(&Desc {
        dimension: if depth == 1 { Dimension::Tex2d } else { Dimension::Tex3d },
        width, height, depth, mips: 1, element_bytes, block, samples: 1, swizzle,
    }).unwrap();
    let row = (width.div_ceil(block) * element_bytes) as usize;
    let plane = row * height.div_ceil(block) as usize;
    let linear_len = plane * depth as usize;
    let detile = match direction.as_str() {
        "detile" => true, "tile" => false, _ => panic!("unknown direction"),
    };
    let (src_len, dst_len) = if detile {
        (layout.size as usize, linear_len)
    } else {
        (linear_len, layout.size as usize)
    };
    let src = (0..src_len).map(|i| ((i * 117 >> 7) ^ (i >> 13)) as u8).collect::<Vec<_>>();
    let mut dst = vec![0xa5; dst_len];
    for batch in 0..6 {
        let start = Instant::now();
        for _ in 0..16 {
            if detile {
                layout.detile(black_box(&src), 0, 0, black_box(&mut dst), row, plane).unwrap();
            } else {
                layout.tile(black_box(&mut dst), 0, 0, black_box(&src), row, plane).unwrap();
            }
            black_box(&dst);
        }
        let ns = start.elapsed().as_nanos() as f64 / 16.0;
        if batch > 0 { print!("{ns:.3} "); }
    }
    let digest = dst.iter().fold(0xcbf29ce484222325_u64, |h, &b| {
        (h ^ u64::from(b)).wrapping_mul(0x100000001b3)
    });
    println!("{digest:016x}");
}

Codex ran this comparison on my Linux machine.

**@⁠necrobytes** on GitHub · 2026-11-24 16:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/1097#issuecomment-7138284311) I compared #603 and #1097 independently against `main` on Fedora 45, Ryzen 5 9600X, with Rust 1.98.1 (`x86_64-unknown-linux-gnu`). I used `ffe310e` for all three builds. #603 (`705a2f6`) merged cleanly onto it, and #1097 (`ceac35b`) already had it as its parent. **#1097 took 5–68% less time than #603 to detile the six swizzled textures. For tiling the two RGBA8 inputs and the odd-width R8 input, #603 was faster in all 12 paired rounds for each input.** Both patches improved on main for those three tiling cases. #1097 also had the lowest median in both directions for BC1, BC7, and the R8 volume. **Median milliseconds per complete texture** | Synthetic texture | Detile main | Detile #603 | Detile #1097 | Tile main | Tile #603 | Tile #1097 | |---|---:|---:|---:|---:|---:|---:| | RGBA8 1920×1080, 64KB_D | 0.739 | 0.338 | 0.280 | 0.714 | 0.383 | 0.655 | | RGBA8 1919×1079, 64KB_D | 0.748 | 0.344 | 0.286 | 0.718 | 0.394 | 0.627 | | R8 1919×1079, 4KB_D | 0.369 | 0.167 | 0.129 | 0.366 | 0.165 | 0.289 | | BC7 4096×4096, 64KB_D | 1.686 | 0.835 | 0.794 | 1.042 | 0.681 | 0.644 | | BC1 4096×4096, 64KB_S | 0.740 | 0.401 | 0.364 | 0.726 | 0.353 | 0.324 | | R8 257×129×17, 4KB_S | 0.222 | 0.216 | 0.069 | 0.222 | 0.221 | 0.185 | | RGBA8 1920×1080, linear | 0.113 | 0.113 | 0.113 | 0.113 | 0.115 | 0.113 | I repeated the three uncompressed 2D swizzled cases on a second physical core, varying the destination’s offset relative to the source modulo 4 KiB (0, 64, 512 and 2,048 bytes). #603 won all 72 paired tiling comparisons there. #1097 won all 72 detiling comparisons. ### Correctness On each revision, I checked 5,369 subresources across 679 synthetic layouts. An independent driver used unmodified main’s `Layout::offset` to construct the expected destination for each direction, with separate inputs for tile and detile. All 14,503,816 element offsets and all 10,738 pairs of full-buffer comparisons matched, including row/plane padding and untouched mips and slices. The cases included 1D and 2D arrays, 3D volumes, block compression, and 2,540 mip-tail subresources. All 20 invalid/unsupported-argument checks returned the expected error and preserved the destination. The `victory-formats` crate tests passed on each build (main and #603 each 23 passed, #1097 24 passed, four ignored on each). ### Timing method I used one pinned logical CPU and preallocated, tightly pitched buffers with one mip and one sample. I timed complete calls to `Layout::tile` and `Layout::detile` separately. Each process warmed up with 16 copies, then measured five batches of 16. Each table entry is the median of 12 process medians, with all six orders of the three revisions used twice. These measurements cover warm CPU texture copies on this Linux host. Gameplay frame time would need a separate measurement. <details> <summary>Reproduce an individual timing</summary> Save the driver below as `bench.rs` outside the checkout. With `benchdir` set to that directory, run the following from each source tree. The driver prints five batch times in nanoseconds per copy and an output digest. ```bash rustc +1.98.1 --edition=2024 -O --crate-type=rlib --crate-name=victory_formats \ crates/victory-formats/src/lib.rs -o "$benchdir/libvictory_formats.rlib" rustc +1.98.1 --edition=2024 -O "$benchdir/bench.rs" \ --extern victory_formats="$benchdir/libvictory_formats.rlib" -o "$benchdir/bench" taskset -c 4 "$benchdir/bench" rgba8 tile taskset -c 4 "$benchdir/bench" rgba8 detile ``` ```rust use std::hint::black_box; use std::time::Instant; use victory_formats::texture::{Desc, Dimension, Layout, Swizzle}; fn main() { let mut args = std::env::args().skip(1); let case = args.next().expect("texture case"); let direction = args.next().expect("tile or detile"); let (width, height, depth, block, element_bytes, swizzle) = match case.as_str() { "rgba8" => (1920, 1080, 1, 1, 4, Swizzle::SW_64KB_D), "bc7" => (4096, 4096, 1, 4, 16, Swizzle::SW_64KB_D), "bc1" => (4096, 4096, 1, 4, 8, Swizzle::SW_64KB_S), "r8-odd" => (1919, 1079, 1, 1, 1, Swizzle::SW_4KB_D), "rgba8-odd" => (1919, 1079, 1, 1, 4, Swizzle::SW_64KB_D), "r8-volume" => (257, 129, 17, 1, 1, Swizzle::SW_4KB_S), "linear" => (1920, 1080, 1, 1, 4, Swizzle::LINEAR), _ => panic!("unknown case {case}"), }; let layout = Layout::new(&Desc { dimension: if depth == 1 { Dimension::Tex2d } else { Dimension::Tex3d }, width, height, depth, mips: 1, element_bytes, block, samples: 1, swizzle, }).unwrap(); let row = (width.div_ceil(block) * element_bytes) as usize; let plane = row * height.div_ceil(block) as usize; let linear_len = plane * depth as usize; let detile = match direction.as_str() { "detile" => true, "tile" => false, _ => panic!("unknown direction"), }; let (src_len, dst_len) = if detile { (layout.size as usize, linear_len) } else { (linear_len, layout.size as usize) }; let src = (0..src_len).map(|i| ((i * 117 >> 7) ^ (i >> 13)) as u8).collect::<Vec<_>>(); let mut dst = vec![0xa5; dst_len]; for batch in 0..6 { let start = Instant::now(); for _ in 0..16 { if detile { layout.detile(black_box(&src), 0, 0, black_box(&mut dst), row, plane).unwrap(); } else { layout.tile(black_box(&mut dst), 0, 0, black_box(&src), row, plane).unwrap(); } black_box(&dst); } let ns = start.elapsed().as_nanos() as f64 / 16.0; if batch > 0 { print!("{ns:.3} "); } } let digest = dst.iter().fold(0xcbf29ce484222325_u64, |h, &b| { (h ^ u64::from(b)).wrapping_mul(0x100000001b3) }); println!("{digest:016x}"); } ``` </details> Codex ran this comparison on my Linux machine.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:32 UTC · original

Automated triage for the maintainers. Closing because another open pull request covers the same change.

See #603. Same optimisation as #603 (fixed-size micro-tile copies) as a +185/-43 rewrite that drops the shared walk, with no measurements, unformatted code and no trailing newline.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 00:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/1097#issuecomment-7148607843) Automated triage for the maintainers. Closing because another open pull request covers the same change. See #603. Same optimisation as #603 (fixed-size micro-tile copies) as a +185/-43 rewrite that drops the shared walk, with no measurements, unformatted code and no trailing newline. If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

Pull request closed

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!1097
No description provided.