perf(formats): add AVX2/SSE2 256B micro-tile texture detiler and DetilePool #42

Open
github-bot wants to merge 2 commits from github-bot/gh-42 into beta AGit
Owner

@⁠Vladimir-Human on GitHub · 2026-11-25 21:01 UTC · original
Vladimir-Human:perf/simd-detile-avx2 → beta · +3447 −12 · 3 files · 2 commits · open

AI-assisted clean-room contribution (victory-formats, unit tests + synthetic throughput benchmark only — no title files used).

Summary

Adds an AVX2/SSE2 SIMD + portable 256-byte micro-tile texture detiler/tiler and persistent worker pool (DetilePool) in crates/victory-formats/src/{detile.rs, lib.rs, texture.rs} (victory-gpu untouched):

  • All non-XOR and thin XOR micro-tile modes accelerated: validates micro-tile and macro/slice/pipe_bank_xor alignment from flips and xor directly (SW_256B_*, SW_4KB_*, SW_64KB_*, *_T, *_X, SW_64KB_R_X, and SW_64KB_Z_X 4B/16B via R8x32Z4 Morton kernel), with copy_fallback handling thick 3D and remaining modes cleanly while preserving existing Layout::{new, for_gpu, detile, tile, bytes_from} APIs.
  • Hardened pub Layout soundness: prepare_thin_geometry -> Option<ThinGeometry> falls back safely to copy_fallback on any inconsistent mutated pub Layout fields, with checked u64/usize/u32 offset math, pitch_blocks >= blocks_x macro-row disjointness, tail bounds, GF(2) injectivity check on layout.block flips, and zero-dim / element_bytes POT <= 16 checks.
  • Safe SIMD & pool discipline: runtime is_x86_feature_detected!("avx2") dispatch with #[target_feature(enable = "avx2")], unaligned loads/stores, separate ConstSendPtr/MutSendPtr, worker/caller panic propagation and mutex poison recovery (DetilePool), and zero overlap with existing klbf-lab crates (git merge-tree clean against beta and sibling shader work).

Verification (beta e328c88)

  • cargo clippy -p victory-formats --all-targets -- -D warnings: 0 warnings.
  • cargo test -p victory-formats --all-targets: 37 passed, 0 failed (7 ignored), including exhaustive scalar vs AVX2 vs portable parity across all 15 addressable swizzle modes (Series X & S, non-zero pipe_bank_xor, BPE 1..16, NPOT dims, odd pitches, mip tails, 1D/arrays/3D), 4-thread try_lock fallback, DetilePool panic recovery, and release-mode mutated_public_layout_fields_never_enter_unsound_fast_path.
  • Release detile_speedup_benchmark (during parallel build load; uncontested benchmark follow-up planned): 2.21x–8.35x vs baseline (19.4x–90.7x vs scalar), e.g., 4K BC7 SW_64KB_D 25.8 GB/s (3.07x), 4K BC1 SW_64KB_D 45.4 GB/s (4.56x), 4K RGBA8 SW_64KB_Z_X 17.0 GB/s (8.35x).
**@⁠Vladimir-Human** on GitHub · 2026-11-25 21:01 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/42) `Vladimir-Human:perf/simd-detile-avx2` → `beta` · +3447 −12 · 3 files · 2 commits · open AI-assisted clean-room contribution (`victory-formats`, unit tests + synthetic throughput benchmark only — no title files used). ### Summary Adds an AVX2/SSE2 SIMD + portable 256-byte micro-tile texture detiler/tiler and persistent worker pool (`DetilePool`) in `crates/victory-formats/src/{detile.rs, lib.rs, texture.rs}` (`victory-gpu` untouched): - **All non-XOR and thin XOR micro-tile modes accelerated**: validates micro-tile and macro/slice/`pipe_bank_xor` alignment from `flips` and `xor` directly (`SW_256B_*`, `SW_4KB_*`, `SW_64KB_*`, `*_T`, `*_X`, `SW_64KB_R_X`, and `SW_64KB_Z_X` 4B/16B via `R8x32Z4` Morton kernel), with `copy_fallback` handling thick 3D and remaining modes cleanly while preserving existing `Layout::{new, for_gpu, detile, tile, bytes_from}` APIs. - **Hardened `pub Layout` soundness**: `prepare_thin_geometry -> Option<ThinGeometry>` falls back safely to `copy_fallback` on any inconsistent mutated `pub` `Layout` fields, with checked `u64`/`usize`/`u32` offset math, `pitch_blocks >= blocks_x` macro-row disjointness, tail bounds, GF(2) injectivity check on `layout.block` flips, and zero-dim / `element_bytes` POT `<= 16` checks. - **Safe SIMD & pool discipline**: runtime `is_x86_feature_detected!("avx2")` dispatch with `#[target_feature(enable = "avx2")]`, unaligned loads/stores, separate `ConstSendPtr`/`MutSendPtr`, worker/caller panic propagation and mutex poison recovery (`DetilePool`), and zero overlap with existing `klbf-lab` crates (`git merge-tree` clean against `beta` and sibling shader work). ### Verification (`beta` `e328c88`) - `cargo clippy -p victory-formats --all-targets -- -D warnings`: **0 warnings**. - `cargo test -p victory-formats --all-targets`: **37 passed, 0 failed** (7 ignored), including exhaustive scalar vs AVX2 vs portable parity across all 15 addressable swizzle modes (Series X & S, non-zero `pipe_bank_xor`, BPE 1..16, NPOT dims, odd pitches, mip tails, 1D/arrays/3D), 4-thread `try_lock` fallback, `DetilePool` panic recovery, and release-mode `mutated_public_layout_fields_never_enter_unsound_fast_path`. - Release `detile_speedup_benchmark` (during parallel build load; uncontested benchmark follow-up planned): **2.21x–8.35x** vs baseline (**19.4x–90.7x** vs scalar), e.g., 4K BC7 `SW_64KB_D` `25.8 GB/s` (`3.07x`), 4K BC1 `SW_64KB_D` `45.4 GB/s` (`4.56x`), 4K RGBA8 `SW_64KB_Z_X` `17.0 GB/s` (`8.35x`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:53:21 +00:00
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-25 21:07 UTC · original

Thanks a lot @⁠Vladimir-Human! We test one change at a time on the RX 9070 XT (same save, same 7-minute run, fps and picture compared with our reference build), so this one goes into that queue on its own. Results will be posted here, then we merge or report back what we saw.

**@⁠PastiiJay** on GitHub · 2026-11-25 21:07 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/42#issuecomment-7174654757) Thanks a lot @⁠Vladimir-Human! We test one change at a time on the RX 9070 XT (same save, same 7-minute run, fps and picture compared with our reference build), so this one goes into that queue on its own. Results will be posted here, then we merge or report back what we saw.
Author
Owner

@⁠HandshakeHackin on GitHub · 2026-11-25 21:17 UTC · original

I checked 853852e on Linux x86-64 (AMD EPYC 9V74, 9 logical CPUs available). The formats suite passed all 37 active tests, including the scalar-reference, odd-pitch, partial-mip and concurrent-caller comparisons. Seven tests were ignored. The combined Windows suite with #41, #43 and the queued beta fixes also passed (608 passed, 16 ignored). Windows CI

One size is worth a look before tuning the worker pool. I ran the PR’s release detile_speedup_benchmark five times per build, alternating the order. The experimental build changed only PARALLEL_MIN_BYTES from 512 KiB to 2 MiB. Median milliseconds per BC7 SW_64KB_D texture on this machine:

Texture Existing walker PR PR with 2 MiB threshold
1K / 1 MiB 0.048 0.049 0.029
2K / 4 MiB 0.202 0.087 0.090
4K / 16 MiB 3.541 0.583 0.582

The 1 MiB case benefited from staying on the calling thread here. The 4K case retained the same speedup with either threshold. Each benchmark checked the output against the old walker before timing. These are CPU texture-conversion timings.

**@⁠HandshakeHackin** on GitHub · 2026-11-25 21:17 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/42#issuecomment-7174870465) I checked `853852e` on Linux x86-64 (AMD EPYC 9V74, 9 logical CPUs available). The formats suite passed all 37 active tests, including the scalar-reference, odd-pitch, partial-mip and concurrent-caller comparisons. Seven tests were ignored. The combined Windows suite with #41, #43 and the queued beta fixes also passed (608 passed, 16 ignored). [Windows CI](https://github.com/HandshakeHackin/klbf-lab/actions/runs/45701360796/job/139512003165) One size is worth a look before tuning the worker pool. I ran the PR’s release `detile_speedup_benchmark` five times per build, alternating the order. The experimental build changed only `PARALLEL_MIN_BYTES` from 512 KiB to 2 MiB. Median milliseconds per BC7 `SW_64KB_D` texture on this machine: | Texture | Existing walker | PR | PR with 2 MiB threshold | |---|---:|---:|---:| | 1K / 1 MiB | 0.048 | 0.049 | 0.029 | | 2K / 4 MiB | 0.202 | 0.087 | 0.090 | | 4K / 16 MiB | 3.541 | 0.583 | 0.582 | The 1 MiB case benefited from staying on the calling thread here. The 4K case retained the same speedup with either threshold. Each benchmark checked the output against the old walker before timing. These are CPU texture-conversion timings.
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-25 21:18 UTC · original

Thanks @⁠HandshakeHackin, that's a very useful check. What we keep: all 37 formats tests pass, the combined Windows suite with #41 and #43 is green (608 passed), the output matches the old walker byte for byte, and the 4K speedup (~6x on BC7 SW_64KB_D) holds with either threshold, while 1 MiB textures do better staying on the calling thread with a 2 MiB PARALLEL_MIN_BYTES.

When this PR's turn comes in our RX 9070 XT queue, we'll run it as-is first, then with the 2 MiB threshold as a separate run, and post both results here. @⁠Vladimir-Human, feel free to weigh in on the threshold if you had a reason for 512 KiB.

**@⁠PastiiJay** on GitHub · 2026-11-25 21:18 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/42#issuecomment-7174893839) Thanks @⁠HandshakeHackin, that's a very useful check. What we keep: all 37 formats tests pass, the combined Windows suite with #41 and #43 is green (608 passed), the output matches the old walker byte for byte, and the 4K speedup (~6x on BC7 `SW_64KB_D`) holds with either threshold, while 1 MiB textures do better staying on the calling thread with a 2 MiB `PARALLEL_MIN_BYTES`. When this PR's turn comes in our RX 9070 XT queue, we'll run it as-is first, then with the 2 MiB threshold as a separate run, and post both results here. @⁠Vladimir-Human, feel free to weigh in on the threshold if you had a reason for 512 KiB.
Author
Owner

@⁠Vladimir-Human on GitHub · 2026-11-26 00:05 UTC · original

@⁠PastiiJay @⁠HandshakeHackin Thanks for running those cross-CPU EPYC 9V74 numbers! 512 KiB was an initial conservative threshold before seeing virtualized/multi-CCX pool-wake latency across CPUs.

I tested raising PARALLEL_MIN_BYTES from 512 KiB to 2 MiB on idle i5-13400F (--release detile_speedup_benchmark):

Subresource Old walker 512 KiB threshold 2 MiB threshold Notes
1K BC7 SW_64KB_D (1 MiB)
**@⁠Vladimir-Human** on GitHub · 2026-11-26 00:05 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/42#issuecomment-7178375552) @⁠PastiiJay @⁠HandshakeHackin Thanks for running those cross-CPU EPYC 9V74 numbers! 512 KiB was an initial conservative threshold before seeing virtualized/multi-CCX pool-wake latency across CPUs. I tested raising PARALLEL_MIN_BYTES from 512 KiB to 2 MiB on idle i5-13400F (--release detile_speedup_benchmark): | Subresource | Old walker | 512 KiB threshold | 2 MiB threshold | Notes | | --- | --- | --- | --- | --- | | 1K BC7 SW_64KB_D (1 MiB) |
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-26 00:05 UTC · original

Thanks for checking it on your side too @⁠Vladimir-Human. We'll keep the plan: one run with the PR as-is and one with the 2 MiB threshold, and post both here.

**@⁠PastiiJay** on GitHub · 2026-11-26 00:05 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/42#issuecomment-7178380829) Thanks for checking it on your side too @⁠Vladimir-Human. We'll keep the plan: one run with the PR as-is and one with the 2 MiB threshold, and post both here.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/42/head:github-bot/gh-42'
git switch 'github-bot/gh-42'

Merge

Merge the changes and update on Forgejo.
git switch 'beta'
git merge --no-ff 'github-bot/gh-42'
git switch 'github-bot/gh-42'
git rebase 'beta'
git switch 'beta'
git merge --ff-only 'github-bot/gh-42'
git switch 'github-bot/gh-42'
git rebase 'beta'
git switch 'beta'
git merge --no-ff 'github-bot/gh-42'
git switch 'beta'
git merge --squash 'github-bot/gh-42'
git switch 'beta'
git merge --ff-only 'github-bot/gh-42'
git switch 'beta'
git merge 'github-bot/gh-42'
git push origin 'beta'
This discussion has been locked. Commenting is limited to contributors.
No description provided.