perf(formats): hoist XOR texture row/column terms and merge low-bit column runs #251

Manually merged
github merged 1 commit from github-bot/gh-251 into gh-pr/251/base 2026-11-27 10:37:45 +00:00 AGit
Owner

@⁠Sheffy007 on GitHub · 2026-11-27 05:04 UTC · original
Sheffy007:perf/xor-texture-detile → main · +244 −7 · 3 files · 1 commits · merged 2026-11-27 10:37 UTC

Summary

Speeds up victory_formats::texture::Layout::detile and Layout::tile on XOR swizzle modes (SW_4KB_*_X, SW_64KB_*_X, SW_64KB_*_T), reducing world texture-sync CPU work (#179, #15):

  • Hoist slice and row address/XOR terms out of the inner column loop: Layout::walk previously called xor_address per element, recomputing y / bh * (pitch / bw), xor.y, xor.z, and pipe_bank for every column x. It now precomputes each column's block byte offset and xor.x once per subresource and computes row_base and yz_xor once per (z, y).
  • Merge contiguous low-bit micro-runs and specialize copy widths (1..=32 B): Address bits below every bit touched by y, z, or pipe_bank are never flipped by yz_xor, so adjacent columns that advance monotonically inside those low bits (e.g. 16 x 1 B, 8 x 2 B, 4 x 4 B, or 2 x 8 B elements in a 16-byte micro-run) are merged into single compile-time-sized copies (copy_xor_runs::<N>), matching the non-XOR copy_runs::<N> path.
  • On an Apple Silicon host (cargo test --release -p victory-formats xor_detile_throughput -- --ignored --nocapture), a 2048x2048 SW_64KB_S_X detile runs in ~1.27 ms at 4 B/element and ~5.58 ms at 16 B/element.

Verification

  • Existing bijection and round-trip tests across all 8 XOR modes, mip chains, tails, array slices, 3D volumes, block compression, and pipe-bank XOR (modes_are_bijections_and_copies_round_trip, the_pipe_bank_xor_flips_the_pipe_and_bank_bits).
  • New unit test partial_xor_runs_with_pipe_bank_xor_round_trip_every_element_size (non-multiple widths + mip tails + non-zero pipe_bank across 1, 2, 4, 8, 16 B/element) and ignored benchmark xor_detile_throughput.
  • cargo test -p victory-formats -p victory-gpu -p victory-metal -p victory-shader -p victory-hle -p victory -p xtask
  • cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo xtask policy
**@⁠Sheffy007** on GitHub · 2026-11-27 05:04 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/251) `Sheffy007:perf/xor-texture-detile` → `main` · +244 −7 · 3 files · 1 commits · merged 2026-11-27 10:37 UTC ## Summary Speeds up `victory_formats::texture::Layout::detile` and `Layout::tile` on XOR swizzle modes (`SW_4KB_*_X`, `SW_64KB_*_X`, `SW_64KB_*_T`), reducing world texture-sync CPU work (#179, #15): - **Hoist slice and row address/XOR terms out of the inner column loop:** `Layout::walk` previously called `xor_address` per element, recomputing `y / bh * (pitch / bw)`, `xor.y`, `xor.z`, and `pipe_bank` for every column `x`. It now precomputes each column's block byte offset and `xor.x` once per subresource and computes `row_base` and `yz_xor` once per `(z, y)`. - **Merge contiguous low-bit micro-runs and specialize copy widths (`1..=32` B):** Address bits below every bit touched by `y`, `z`, or `pipe_bank` are never flipped by `yz_xor`, so adjacent columns that advance monotonically inside those low bits (e.g. 16 x 1 B, 8 x 2 B, 4 x 4 B, or 2 x 8 B elements in a 16-byte micro-run) are merged into single compile-time-sized copies (`copy_xor_runs::<N>`), matching the non-XOR `copy_runs::<N>` path. - On an Apple Silicon host (`cargo test --release -p victory-formats xor_detile_throughput -- --ignored --nocapture`), a `2048x2048` `SW_64KB_S_X` `detile` runs in ~1.27 ms at 4 B/element and ~5.58 ms at 16 B/element. ## Verification - Existing bijection and round-trip tests across all 8 XOR modes, mip chains, tails, array slices, 3D volumes, block compression, and pipe-bank XOR (`modes_are_bijections_and_copies_round_trip`, `the_pipe_bank_xor_flips_the_pipe_and_bank_bits`). - New unit test `partial_xor_runs_with_pipe_bank_xor_round_trip_every_element_size` (non-multiple widths + mip tails + non-zero `pipe_bank` across `1, 2, 4, 8, 16` B/element) and ignored benchmark `xor_detile_throughput`. - `cargo test -p victory-formats -p victory-gpu -p victory-metal -p victory-shader -p victory-hle -p victory -p xtask` - `cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo xtask policy`
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 05:08:12 +00:00
Author
Owner

@⁠evan-kolberg on GitHub · 2026-11-27 05:12 UTC · original

I ran a paired M4 Pro baseline with Codex using Sheffy007’s existing release benchmark. Both builds detiled the same generated 2048×2048 SW_64KB_S_X texture. Each result is the median of eight paired batches, with 20 detiles per batch and parent/head order alternating. Direct parent 775712ad · Tested head 6db66ea1 · Authored benchmark

Bytes per element Parent PR head Exact output bytes matched
4 14.785 ms 1.055 ms 16,777,216
16 16.735 ms 6.390 ms 67,108,864

The byte comparison used a separate nonconstant generated input on the same layout. The timings cover full CPU detile only.

**@⁠evan-kolberg** on GitHub · 2026-11-27 05:12 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/251#issuecomment-7210438213) I ran a paired M4 Pro baseline with Codex using Sheffy007’s existing release benchmark. Both builds detiled the same generated 2048×2048 `SW_64KB_S_X` texture. Each result is the median of eight paired batches, with 20 detiles per batch and parent/head order alternating. [Direct parent `775712ad`](https://github.com/LakshanSivakumar/Native-Victory-macOS/commit/775712ad0974940905aee612106ad67e1a449401) · [Tested head `6db66ea1`](https://github.com/LakshanSivakumar/Native-Victory-macOS/commit/6db66ea1f3b5ef17e9bc1713b4c7ed8aa345e290) · [Authored benchmark](https://github.com/LakshanSivakumar/Native-Victory-macOS/blob/6db66ea1f3b5ef17e9bc1713b4c7ed8aa345e290/crates/victory-formats/src/texture_xor_tests.rs#L273-L294) | Bytes per element | Parent | PR head | Exact output bytes matched | |---:|---:|---:|---:| | 4 | 14.785 ms | 1.055 ms | 16,777,216 | | 16 | 16.735 ms | 6.390 ms | 67,108,864 | The byte comparison used a separate nonconstant generated input on the same layout. The timings cover full CPU detile only.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-27 05:41 UTC · original

The measured speed-up and the byte-identical output in the comment above are convincing. One request before merge: the committed tests are round trips, and tile and detile share the new walk, so a symmetric mistake would still pass. Please add a test that compares walk against the per-element xor_address result across the modes, then rebase on main.

**@⁠LakshanSivakumar** on GitHub · 2026-11-27 05:41 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/251#issuecomment-7210850451) The measured speed-up and the byte-identical output in the comment above are convincing. One request before merge: the committed tests are round trips, and tile and detile share the new `walk`, so a symmetric mistake would still pass. Please add a test that compares `walk` against the per-element `xor_address` result across the modes, then rebase on main.
github-bot force-pushed github-bot/gh-251 from 6db66ea1f3 to 65a60fe177 2026-11-27 06:25:18 +00:00 Compare
Author
Owner

@⁠Sheffy007 on GitHub · 2026-11-27 06:22 UTC · original

Rebased onto main (2a3b03d) and added texture::xor_tests::walk_matches_per_element_xor_address_across_modes, which drives Layout::walk directly and verifies every visited linear byte against the per-element Layout::xor_address across all 8 addressable _X / _T modes, element byte widths 1..=16, 2D array slices and 3D depth layouts, mip chains (including tail mips), two pipe configurations, and non-zero pipe-bank XOR.

**@⁠Sheffy007** on GitHub · 2026-11-27 06:22 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/251#issuecomment-7211476643) Rebased onto `main` (`2a3b03d`) and added `texture::xor_tests::walk_matches_per_element_xor_address_across_modes`, which drives `Layout::walk` directly and verifies every visited linear byte against the per-element `Layout::xor_address` across all 8 addressable `_X` / `_T` modes, element byte widths `1..=16`, 2D array slices and 3D depth layouts, mip chains (including tail mips), two pipe configurations, and non-zero pipe-bank XOR.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-27 06:48 UTC · original

Thank you. This is the test I asked for: it compares walk against xor_address per element, so a symmetric mistake in tile and detile can no longer pass.

One difference between the text and the test. The text says element widths 1..=16, but the loop uses 1 << e, which is 1, 2, 4, 8 and 16 only. Can an _X or _T texture have an element width of 3, 6 or 12 bytes? If it can, please add those widths; if not, please change the text to say powers of two.

**@⁠LakshanSivakumar** on GitHub · 2026-11-27 06:48 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/251#issuecomment-7211865638) Thank you. This is the test I asked for: it compares `walk` against `xor_address` per element, so a symmetric mistake in tile and detile can no longer pass. One difference between the text and the test. The text says element widths `1..=16`, but the loop uses `1 << e`, which is 1, 2, 4, 8 and 16 only. Can an `_X` or `_T` texture have an element width of 3, 6 or 12 bytes? If it can, please add those widths; if not, please change the text to say powers of two.
github-bot force-pushed github-bot/gh-251 from 65a60fe177 to 87861b11ba 2026-11-27 10:37:43 +00:00 Compare
github-bot changed target branch from main to gh-pr/251/base 2026-11-27 10:37:44 +00:00
github manually merged commit 87861b11ba into gh-pr/251/base 2026-11-27 10:37:45 +00:00
Author
Owner

@⁠Sheffy007 on GitHub · 2026-11-27 09:39 UTC · original

Good catch — _X and _T textures cannot have element byte widths of 3, 6, or 12: RDNA2 Addr2 swizzle tables are indexed by element_log2 (0..=4), and Layout::new_for (crates/victory-formats/src/texture.rs:437-438) rejects any non-power-of-two element_bytes (!d.element_bytes.is_power_of_two() || d.element_bytes > 16, with 96-bit R32G32B32 formats restricted to linear layouts).

Updated the doc-comment on walk_matches_per_element_xor_address_across_modes to say power-of-two element byte widths (1, 2, 4, 8, 16), added explicit Err(Error::Invalid) assertions for [3, 6, 12] across XOR_MODES, and rebased onto main (c1b95875).

**@⁠Sheffy007** on GitHub · 2026-11-27 09:39 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/251#issuecomment-7214930350) Good catch — `_X` and `_T` textures cannot have element byte widths of `3`, `6`, or `12`: RDNA2 `Addr2` swizzle tables are indexed by `element_log2` (`0..=4`), and `Layout::new_for` (`crates/victory-formats/src/texture.rs:437-438`) rejects any non-power-of-two `element_bytes` (`!d.element_bytes.is_power_of_two() || d.element_bytes > 16`, with 96-bit `R32G32B32` formats restricted to linear layouts). Updated the doc-comment on `walk_matches_per_element_xor_address_across_modes` to say power-of-two element byte widths (`1, 2, 4, 8, 16`), added explicit `Err(Error::Invalid)` assertions for `[3, 6, 12]` across `XOR_MODES`, and rebased onto `main` (`c1b95875`).
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-27 10:37 UTC · original

Thank you. That answers my question: 3, 6 and 12 cannot occur, and the test now shows the error for them. CI is green and it is rebased.

I have no further points. It still needs my own merge. I have not run it; I rely on evan-kolberg's paired timings and the identical output he reported.

Merged.

**@⁠LakshanSivakumar** on GitHub · 2026-11-27 10:37 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/251#issuecomment-7216112063) Thank you. That answers my question: 3, 6 and 12 cannot occur, and the test now shows the error for them. CI is green and it is rebased. I have no further points. It still needs my own merge. I have not run it; I rely on evan-kolberg's paired timings and the identical output he reported. Merged.
This discussion has been locked. Commenting is limited to contributors.
No description provided.