Skip to content

Formats: speed up GFX10 tiled texture copies up to 2.4x - #2736

Open
brainbudt wants to merge 1 commit into
Marvisak:mainfrom
brainbudt:perf/texture-detile
Open

brainbudt wants to merge 1 commit into
Marvisak:mainfrom
brainbudt:perf/texture-detile

Conversation

@brainbudt

Copy link
Copy Markdown

Layout::detile and Layout::tile previously inspected every column to build a run list and visited each row and depth slice independently, revisiting tiled blocks on adjacent rows and volume slices.

What changed

  • Derive the contiguous run width once from the shared swizzle pattern.
  • Pair short runs from adjacent rows and traverse depth slices while their tiled blocks stay in cache.
  • Add an ignored detile_throughput benchmark and extend copy tests against per-element Layout::offset across all addressable swizzles, element sizes, padding, odd dimensions, mip tails, array slices, 3D slabs, and error precedence.
  • Pure safe Rust, no new dependencies, public API and supported swizzle modes unchanged.

Benchmark (detile_throughput, median of 5 release runs on Apple M4, MB/s)

Layout Detile before Detile after Tile before Tile after
BC7 4096×4096 · 64KB_D 20,185.0 21,311.9 16,698.5 20,075.9
BC7 4096×4096 · 64KB_S 13,307.1 14,262.4 13,782.8 17,277.7
BC1 4096×4096 · 64KB_D 22,636.1 22,628.6 19,768.0 34,399.2
RGBA8 1920×1080 · 64KB_D 20,145.7 22,918.6 13,655.7 29,101.6
RGBA8 1920×1080 · 4KB_S 19,658.5 22,230.7 10,883.3 25,879.7
BC7 mip 5, 128×128 · 64KB_D 28,899.8 50,543.7 28,752.9 54,087.3
RGBA8 128×128×64 · 4KB_S 11,344.2 27,357.3 10,442.9 24,228.3

Verification

  • cargo test --release -p victory-formats texture (17 tests) and cargo clippy -p victory-formats --all-targets -- -D warnings passed.
  • Reproduce benchmark with cargo test --release -p victory-formats detile_throughput -- --ignored --nocapture.

Created with gpt-6.2-astra in Codex (orchestrated with Gemini 4 Neon in T3 Code).

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Reviewed and benchmarked d3310473 on Windows 11 against main (13f91ca).

Code review

  • Zero unsafe, and all bounds/overflow checks (mip/slice/3D slice == 0, addressable(), checked_mul/checked_add, row_pitch/depth_pitch, tiled_len/linear_len) run in walk before writing any output bytes (covered by copy_errors_precede_writes_in_both_directions).
  • Checked the walk_runs::<N> specializations against all 15 MICRO entries (Thin S / Thin D / Thick 3D × 1/2/4/8/16B): contiguous x-run widths always fall in {4, 8, 16, 32} bytes, every BLOCK_256_2D / BLOCK_256_3D entry has even micro[1] so tail.y is always even for the rows = 2 paired path (N < 32), and columns + trailing last / copy.span handle empty columns, single-row mips, odd widths, and unaligned pitches without out-of-bounds slicing.

Verification & same-machine --release benchmark (rustc 1.98.1 MSVC)

  • cargo test -p victory-formats --locked: 39 passed, 0 failed, 6 ignored (byte-exact round-trip vs Layout::offset across LINEAR/256B/4KB/64KB S/D, 1..16B bpp, odd 259x19, 8-mip tail chain, arrays, and 3D).
  • victory-gpu WARP (--features diagnostics): texture::tests 2/2, draw::tests --test-threads=1 11/11.
  • cargo clippy -p victory-formats --all-targets --locked -- -D warnings: 0 warnings; cargo xtask policy: 184 files ok; clean git merge-tree against 13f91ca and #2735.
  • detile_throughput (--release, same machine vs 13f91ca):
    • BC7 64KB_D: detile 9,317 -> 13,471 MB/s (1.45x) | tile 12,646 -> 15,430 MB/s (1.22x)
    • BC7 64KB_S: detile 9,354 -> 12,430 MB/s (1.33x) | tile 10,307 -> 14,856 MB/s (1.44x)
    • BC1 64KB_D: detile 19,254 -> 33,089 MB/s (1.72x) | tile 25,347 -> 44,560 MB/s (1.76x)
    • RGBA8 64KB_D: detile 15,925 -> 32,770 MB/s (2.06x) | tile 12,847 -> 40,079 MB/s (3.12x)
    • RGBA8 4KB_S: detile 15,445 -> 32,479 MB/s (2.10x) | tile 13,660 -> 35,579 MB/s (2.60x)
    • BC7 64KB_D tail: detile 29,747 -> 48,344 MB/s (1.63x) | tile 28,035 -> 49,428 MB/s (1.76x)
    • RGBA8 3D 4KB_S: detile 12,117 -> 29,495 MB/s (2.43x) | tile 6,690 -> 17,379 MB/s (2.60x)

@kotekompotek

Copy link
Copy Markdown

Solid rewrite: the contiguous-prefix detection (take_while on the x pattern) yields the compile-time run width N, columns are precomputed once in N-byte units, and the row-pair trick for narrow runs keeps writes to nearby tiled addresses together. The unreachable! on run width is safe per the GFX10 pattern shape. One coverage question: do the texture tests (or a fixture elsewhere) round-trip tile/detile across the swizzle modes and mip tails? The tail-run + second-row edge cases in walk_runs are exactly the spots where a copy could silently misalign.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

Independent WARP/CPU verification of d3310473 (#2736).

Review: Only victory-formats/src/texture.rs changes. walk now validates everything before any write (it was already doing that, and copy_errors_precede_writes_in_both_directions now pins it), and tiled copies go through a Buffers enum with monomorphised run widths (4/8/16/32). Linear layouts take a plain row copy. I found no unsafe, and slicing stays bounds-checked. Odd widths (a short last run) and odd heights (an unpaired row) are handled through last and second. I found no correctness problems.

Results (CPU):

  • origin/main: cargo test -p victory-formats --lib: 38 passed, 5 ignored.
  • PR: 39 passed, 0 failed, 6 ignored (the ignored extra is the detile_throughput benchmark; I did not re-measure the 2.4x claim).
  • cargo clippy --locked -p victory-formats --all-targets [--all-features] -- -D warnings: clean. cargo test -p xtask (policy): 6 passed.

Negative control (mutating the PR head):

  • Dropping the short tail run (last = None): addresses_are_a_bijection_and_copies_round_trip fails.
  • Dropping the row pairing (second = None): addresses_are_a_bijection_and_copies_round_trip and depth_pitch_accepts_exact_and_padded_slices fail.

So the new odd-size and cross-block cases catch breakage in the new fast path.

Method: no physical GPU. VICTORY_WARP=1, VICTORY_D3D12_WARP=1, Windows SDK 10.0.26100 dxil.dll on PATH, Windows 11, rustc 1.98.1 MSVC, --locked. Compared origin/main (13f91ca) with the PR head. The PR merges cleanly onto that main. No game files, dumps, shader names or retail hashes were used or are needed.

AI disclosure: this verification was done by Claude Code / Claude Sonnet 5.5.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

This reworks Layout::walk in crates/victory-formats/src/texture.rs for tile/detile: the contiguous run width is derived once from pattern[0], copies are monomorphised per width (walk_runs::<N>), narrow runs pair adjacent rows using pattern[1][1], and thick 3D slabs walk their depth slices together. By reading I checked the assumptions against MICRO: every addressable kind/element size gives a run of 4, 8, 16 or 32 bytes (so the unreachable! holds), y bits always sit above the run bits (so the paired-row offset is a whole number of chunks), micro tiles are at least 2 rows tall (tail origins stay on even y), and all validation still precedes the first write. The round-trip test now compares the whole tiled buffer against per-element Layout::offset over all addressable swizzles, element sizes, odd sizes, arrays and volumes. CI (check) passes.

This is a recommendation for a maintainer to look, not an approval.


Generated by Claude Code

pragyaangaur pushed a commit to pragyaangaur/VIctory-macOS that referenced this pull request Nov 26, 2026
Adapt Marvisak/VIctory#2736 (d33104738a) to the macOS branch. Includes the offset oracle and deterministic copy-error regression tests.

Co-authored-by: brainbudt <32467201+brainbudt@users.noreply.github.com>

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verified against origin/main (63bcca4):

  • GFX10 tiled texture copy speedup passes all 47 victory-formats unit tests (including addresses_are_a_bijection_and_copies_round_trip, layouts_match_addrlib, and layouts_share_identical_swizzle_tables).
  • cargo clippy -p victory-formats --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 192 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

@futurepro62

Copy link
Copy Markdown

NVIDIA full-suite run, to go with the reports above by fuzeofficlal, oni-swr, since none of them names a GPU run. Tested exact head d331047 (on its base 13f91ca).

Tested

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

BastienRodz added a commit to BastienRodz/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.

The contiguous run widths (4, 8, 16, and 32 bytes in crates/victory-formats/src/texture.rs:705-761) cover all currently addressable MICRO patterns and element sizes (1..=16 B), every full-run column() offset and pattern[1][1] second-row delta is an exact multiple of N, row pairs stay within a block height (both y and tail.y are even), 3D slab alignment holds since slice == 0 for Tex3d, narrow/odd tails fall back cleanly to span(), and all validation checks precede writes in both directions. Verified address equivalence across 60 layouts / 717 subresources / 999,986 element addresses (including 231 tail and 88 bytes < N subresources) with zero mismatches.

Local verification at d3310473 (and merged onto origin/main at 8a82b503): xtask policy (184 files ok), cargo clippy -p victory-formats --all-targets (default and --all-features, 0 warnings), and cargo test -p victory-formats (debug and --release: 39 passed, 0 failed, 6 ignored; merged onto origin/main: 47 passed, 0 failed, 6 ignored) all passed. Release detile_throughput on a Ryzen 9 9950X vs origin/main (63bcca43) improved across all 14 cases by 1.43x–4.35x (e.g., BC7 64KB_D detile 8,278.7 -> 16,452.3 MB/s, BC7 64KB_S tile 12,000.7 -> 21,391.2 MB/s, RGBA8 64KB_D detile 9,937.6 -> 43,266.9 MB/s, RGBA8 4KB_S tile 13,255.4 -> 52,662.9 MB/s, RGBA8 3D 4KB_S tile 7,311.3 -> 18,624.0 MB/s). Approving.

@rezioliver-collab

Copy link
Copy Markdown

I measured the odd-pitch case separately on Apple M1. Adding 37 bytes to each linear row and 19 bytes between depth slices still gave a speedup in both directions, including on a 3D volume with an odd depth.

Synthetic surface, +37-byte row padding Tile speedup Detile speedup
RGBA8, 257 × 129, array slice 1 1.89× 1.79×
BC7, 513 × 257, array slice 1 1.50× 1.95×
RGBA8, 65 × 33 × 9, 3D 2.06× 2.08×

I compared 13f91cae with d3310473 using coordinate-dependent bytes. Tight rows and rows rounded up to 256 bytes were additional controls. The PR had a faster median in all 18 combinations of surface, pitch and copy direction. I built a tiled reference one element at a time with Layout::offset, then compared whole buffers in both directions, including row padding, guard bytes and the untouched array slice. Every comparison matched.

The table uses median base/PR ratios from 12 alternating release pairs on macOS 14.7, Rust 1.98.1.

@Desamodeth Desamodeth left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I compared #2026 (1ec3877) and this PR (d331047) against main (63bcca4) on the same Ryzen 7 9800X3D. I would prioritize #2736 for its performance in both copy directions. One exception worth preserving in future benchmarks is detiling a rotating set of 1080p surfaces: #2026 was faster there in both processes.

Median µs per complete subresource copy on Windows x64. Columns are main / #2026 / #2736, first process:

Synthetic layout Detile Tile
1920×1080 RGBA8, 64KB_D 328 / 273 / 186 402 / 712 / 184
Same, rotating 8 input/output pairs 675 / 442 / 460 572 / 723 / 369
4096² BC7, 64KB_D 436 / 372 / 344 413 / 366 / 347
Same, rotating 4 pairs 1131 / 770 / 752 769 / 696 / 709
128×128×64 RGBA8 volume, 4KB_S 341 / 233 / 143 669 / 741 / 248

The second process reversed the version order. The 1080p tile slowdown on #2026 repeated (380 / 741 / 188 µs), as did its advantage over #2736 for rotating 1080p detile (696 / 448 / 484 µs). The rotating BC7 detile ranking between the two PRs flipped; that case does not give a dependable winner. The linear 1080p control was within 2% across versions in each process.

Each version's actual texture.rs was compiled as a separate optimized Rust 1.98.1 MSVC library. The module on #2736's older base is byte-identical to current main. Same buffers for all three versions, all six orders, two warm-up batches and twelve measured batches. Before timing I compared each direction independently against buffers built with main's per-element Layout::offset, including padding, an untouched array slice and a small mip. The module's active tests passed on all three. CPU copies only; no game frame or GPU measurement. Coordination. AI-assisted with Codex.

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.

9 participants