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

Closed
Vladimir-Human wants to merge 86 commits from upstream-pr/42 into main
Vladimir-Human commented 2026-11-25 21:01:18 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by Vladimir-Human on 2026-11-25T21:01:18Z.


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).
<!-- greyfinch-sync:PastiiJay/klbf-lab:issue:5499106681 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/42). Originally posted by [Vladimir-Human](https://github.com/Vladimir-Human) on 2026-11-25T21:01:18Z. --- 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`).
PastiiJay commented 2026-11-25 21:07:58 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-25T21:07:58Z.


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.

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7174654757 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/42#issuecomment-7174654757). Originally posted by [PastiiJay](https://github.com/PastiiJay) on 2026-11-25T21:07:58Z. --- 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.
HandshakeHackin commented 2026-11-25 21:17:13 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by HandshakeHackin on 2026-11-25T21:17:13Z.


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.

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7174870465 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/42#issuecomment-7174870465). Originally posted by [HandshakeHackin](https://github.com/HandshakeHackin) on 2026-11-25T21:17:13Z. --- 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.
PastiiJay commented 2026-11-25 21:18:13 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-25T21:18:13Z.


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.

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7174893839 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/42#issuecomment-7174893839). Originally posted by [PastiiJay](https://github.com/PastiiJay) on 2026-11-25T21:18:13Z. --- 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.
Vladimir-Human commented 2026-11-26 00:05:00 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by Vladimir-Human on 2026-11-26T00:05:00Z.


@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)
<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7178375552 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/42#issuecomment-7178375552). Originally posted by [Vladimir-Human](https://github.com/Vladimir-Human) on 2026-11-26T00:05:00Z. --- @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) |
PastiiJay commented 2026-11-26 00:05:18 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-26T00:05:18Z.


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.

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7178380829 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/42#issuecomment-7178380829). Originally posted by [PastiiJay](https://github.com/PastiiJay) on 2026-11-26T00:05:18Z. --- 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.
Vladimir-Human commented 2026-11-27 07:04:44 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by Vladimir-Human on 2026-11-27T07:04:44Z.


Follow-up test coverage update in 276d0b3: after raising PARALLEL_MIN_BYTES to 2 MiB, the existing 1 MiB cases ran inline and only the exact 2 MiB cases hit the worker pool. Added three strictly > 2 MiB cases to parallel_threshold_and_concurrent_callers_match_scalar_reference covering both non-XOR and XOR pool paths (2048x2048 BC7 SW_64KB_D 4 MiB POT, 2056x1544 BC7 SW_64KB_R_X 3 MiB NPOT + remainders, and 2048x2048 BC7 SW_64KB_Z_X 4 MiB POT) and updated the stale >= 512 KiB comments.

Re-verified on 276d0b3: cargo clippy -p victory-formats --all-targets (0 warnings), cargo test -p victory-formats --release -- --test-threads=1 (37 passed, 0 failed, 7 ignored), and debug parallel_threshold_and_concurrent_callers_match_scalar_reference (passed).

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7212120449 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/42#issuecomment-7212120449). Originally posted by [Vladimir-Human](https://github.com/Vladimir-Human) on 2026-11-27T07:04:44Z. --- Follow-up test coverage update in `276d0b3`: after raising `PARALLEL_MIN_BYTES` to `2 MiB`, the existing `1 MiB` cases ran inline and only the exact `2 MiB` cases hit the worker pool. Added three strictly `> 2 MiB` cases to `parallel_threshold_and_concurrent_callers_match_scalar_reference` covering both non-XOR and XOR pool paths (`2048x2048 BC7 SW_64KB_D` 4 MiB POT, `2056x1544 BC7 SW_64KB_R_X` 3 MiB NPOT + remainders, and `2048x2048 BC7 SW_64KB_Z_X` 4 MiB POT) and updated the stale `>= 512 KiB` comments. Re-verified on `276d0b3`: `cargo clippy -p victory-formats --all-targets` (**0 warnings**), `cargo test -p victory-formats --release -- --test-threads=1` (**37 passed, 0 failed, 7 ignored**), and debug `parallel_threshold_and_concurrent_callers_match_scalar_reference` (**passed**).
PastiiJay commented 2026-11-27 09:52:34 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-27T09:52:34Z.


Thanks @Vladimir-Human, the extra cases above 2 MiB cover the pool path properly now. We'll review it with the next beta batch. Could you rebase on the current head if it no longer merges cleanly by then?

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7215200792 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/42#issuecomment-7215200792). Originally posted by [PastiiJay](https://github.com/PastiiJay) on 2026-11-27T09:52:34Z. --- Thanks @Vladimir-Human, the extra cases above 2 MiB cover the pool path properly now. We'll review it with the next beta batch. Could you rebase on the current head if it no longer merges cleanly by then?
Vladimir-Human commented 2026-11-27 10:44:16 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by Vladimir-Human on 2026-11-27T10:44:16Z.


Thanks @PastiiJay! Verified 276d0b3 still merges cleanly against current beta (6cce57e), and I will rebase promptly if the next beta batch introduces any conflict.

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7216244361 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/42#issuecomment-7216244361). Originally posted by [Vladimir-Human](https://github.com/Vladimir-Human) on 2026-11-27T10:44:16Z. --- Thanks @PastiiJay! Verified `276d0b3` still merges cleanly against current `beta` (`6cce57e`), and I will rebase promptly if the next beta batch introduces any conflict.
PastiiJay commented 2026-11-27 10:50:20 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-27T10:50:20Z.


Thanks @Vladimir-Human for keeping this rebased-ready and for checking it against beta. We're reviewing the detiler diff now; no merge timing yet. Could you tell us which CPU and whether you saw any texture change on your side?

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7216365468 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/42#issuecomment-7216365468). Originally posted by [PastiiJay](https://github.com/PastiiJay) on 2026-11-27T10:50:20Z. --- Thanks @Vladimir-Human for keeping this rebased-ready and for checking it against `beta`. We're reviewing the detiler diff now; no merge timing yet. Could you tell us which CPU and whether you saw any texture change on your side?
PastiiJay commented 2026-11-27 10:52:00 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-27T10:52:00Z.


We'll take them onto the AMD line one at a time with an A/B bench. Thanks, the tests you added make that much easier.

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7216399475 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/42#issuecomment-7216399475). Originally posted by [PastiiJay](https://github.com/PastiiJay) on 2026-11-27T10:52:00Z. --- We'll take them onto the AMD line one at a time with an A/B bench. Thanks, the tests you added make that much easier.
Vladimir-Human commented 2026-11-28 18:07:27 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by Vladimir-Human on 2026-11-28T18:07:27Z.


Superseded by #178 targeting stable-amd (v26.11.6): https://github.com/PastiiJay/klbf-lab/pull/178

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7243548230 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/42#issuecomment-7243548230). Originally posted by [Vladimir-Human](https://github.com/Vladimir-Human) on 2026-11-28T18:07:27Z. --- Superseded by #178 targeting `stable-amd` (`v26.11.6`): https://github.com/PastiiJay/klbf-lab/pull/178

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
PastiiJay/klbf-lab!42
No description provided.