perf(gpu): vectorize texture::digest with VAES/AES-NI and fused zero check #507

Manually merged
github merged 1 commit from github-bot/gh-507 into main 2026-12-05 17:38:45 +00:00 AGit
Owner

@⁠Not4Eric on GitHub · 2026-12-04 20:24 UTC · original
Not4Eric:perf/texture-vaes-digest → main · +281 −22 · 1 files · 1 commits · merged 2026-12-05 17:30 UTC

What this does

Adds runtime-dispatched x86_64 VAES (avx512f,vaes,aes) and AES-NI (aes,sse4.1) implementations of victory_gpu::texture::digest_and_zero with scalar fallback, fusing the all-zero check into the same single pass over texture memory. This avoids scanning every staged/fingerprinted texture twice (once for hashing, once for all_zero) and accelerates hashing on CPUs with AES-NI or AVX-512 VAES.

Checks

  • N/A: pure performance optimization (digest is an internal non-cryptographic in-memory change detector, never persisted on disk).
  • N/A: no switches added, changed, or removed.
  • No unrelated cleanup: diff is limited to digest/digest_and_zero, its callers (Staging, fingerprint_blank, print_of), and its unit test in crates/victory-gpu/src/texture.rs.
  • Added unit test texture::allocation_costs::digest_and_zero_across_tiers exercising all available runtime tiers (dispatch, scalar, AES-NI, VAES-512) for zero detection, single-bit flips, length sensitivity, and byte-swap sensitivity (cannot exist before this change).
  • Checked open PR queue: independent of #489 (which touches watched-memory detiling in texture.rs, not digest).
  • cargo xtask policy passed (359 files ok); no fork-only files, configs, or build outputs.

Verified

  • cargo xtask policy — 359 files ok
  • PKG=victory-gpu ./build.sh test --lib -- texture:: --test-threads=1 (Proton Wine on Linux, Ryzen 5 7600X with AVX-512/VAES + AES-NI) — 12 passed; 0 failed; 3 ignored, 0 warnings (includes digest_and_zero_across_tiers testing scalar, AES-NI, and VAES-512 tiers)
  • Also exercised in gameplay on GTA 6 Series S (Ryzen 5 7600X, RX 9070 XT, CachyOS + Proton). Non-x86_64 builds were not compiled.

Cost

None (runtime CPU feature detection selects VAES-512 / AES-NI / scalar safely on any x86_64 CPU; 0 new warnings).

AI-assisted: yes

**@⁠Not4Eric** on GitHub · 2026-12-04 20:24 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/507) `Not4Eric:perf/texture-vaes-digest` → `main` · +281 −22 · 1 files · 1 commits · merged 2026-12-05 17:30 UTC ## What this does Adds runtime-dispatched x86_64 VAES (`avx512f,vaes,aes`) and AES-NI (`aes,sse4.1`) implementations of `victory_gpu::texture::digest_and_zero` with scalar fallback, fusing the all-zero check into the same single pass over texture memory. This avoids scanning every staged/fingerprinted texture twice (once for hashing, once for `all_zero`) and accelerates hashing on CPUs with AES-NI or AVX-512 VAES. ## Checks - N/A: pure performance optimization (`digest` is an internal non-cryptographic in-memory change detector, never persisted on disk). - N/A: no switches added, changed, or removed. - [x] No unrelated cleanup: diff is limited to `digest`/`digest_and_zero`, its callers (`Staging`, `fingerprint_blank`, `print_of`), and its unit test in `crates/victory-gpu/src/texture.rs`. - [x] Added unit test `texture::allocation_costs::digest_and_zero_across_tiers` exercising all available runtime tiers (dispatch, scalar, AES-NI, VAES-512) for zero detection, single-bit flips, length sensitivity, and byte-swap sensitivity (cannot exist before this change). - [x] Checked open PR queue: independent of #489 (which touches watched-memory detiling in `texture.rs`, not `digest`). - [x] `cargo xtask policy` passed (`359 files ok`); no fork-only files, configs, or build outputs. ## Verified - `cargo xtask policy` — `359 files ok` - `PKG=victory-gpu ./build.sh test --lib -- texture:: --test-threads=1` (Proton Wine on Linux, Ryzen 5 7600X with AVX-512/VAES + AES-NI) — `12 passed; 0 failed; 3 ignored`, 0 warnings (includes `digest_and_zero_across_tiers` testing scalar, AES-NI, and VAES-512 tiers) - Also exercised in gameplay on GTA 6 Series S (Ryzen 5 7600X, RX 9070 XT, CachyOS + Proton). Non-x86_64 builds were not compiled. ## Cost None (runtime CPU feature detection selects VAES-512 / AES-NI / scalar safely on any x86_64 CPU; 0 new warnings). AI-assisted: yes
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-04 20:27:24 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-04 21:23 UTC · original

Checked 9b9f78e on native Windows with MSVC (didn't run the game).

Debug and release builds of victory-gpu are fine with the pinned toolchain, and the intrinsics and target_feature attributes compile cleanly. digest_and_zero_across_tiers passes and texture:: is 12 passed, 3 ignored. On the full lib suite the only failures are the ones that already fail natively on main (the two indirect host_list tests, the two pipeline::host library loads, and a watch test that passes when rerun alone). All three tiers ran here, and the dispatch picks VAES-512.

I also ran a throwaway test of my own, which I didn't push.

  • The zero flag matched all_zero on every tier for lengths 0 to about 8 KiB, start offsets 0..63, and a single non-zero byte at each position including the last partial lane. Non-zero canary bytes just outside the slice never leaked in, which fits the tail being copied into a zeroed stack array (texture.rs:1216, 1280).
  • Each tier gives the same value for the same bytes whatever the alignment, and the scalar tier still gives exactly main's value. The tiers don't agree with each other, though. Every buffer gave three different digests. That looks harmless today, because the dispatch is fixed per process and every comparison I could find stays in memory (last.hash, level.hash, Held in resource.rs:1191, blank_print against fingerprint_blank in draw.rs:3911, the replay page marks). It would bite once a digest gets written to disk or compared between machines, so maybe say in the doc comment that the value depends on the CPU.
  • No collisions on any tier with random 4 KiB and 64 KiB buffers. I tried every single bit flip on 4 KiB, swapping two 16-byte blocks and two 64-byte blocks, sliding a 256-byte zero run around, mostly-zero buffers with one random 16-byte block, and the same bytes with 0..2000 zeros appended. That's about 450k digests per tier. From reading the code rather than testing, one AES round per block is a weak mixer against a deliberate two-block edit in the same lane, since those can cancel. For change detection on real textures I'd still expect it to hold up as well as the old fold.

Release throughput on this machine, best of 5. The main row is main's digest + all_zero, measured in a separate run.

64 KiB 1 MiB 16 MiB
main, random data 32 GB/s 32 GB/s 31 GB/s
main, zero data 17 GB/s 17 GB/s 16 GB/s
PR scalar 24-26 26 26
PR AES-NI 81 80 68-71
PR VAES-512 215-218 188-204 88-106

So the speedup comes from AES, while the fused zero check alone only helps on blank buffers. all_zero stops at the first non-zero word, so main already scans non-blank data only once. The scalar tier looked a bit slower than main, possibly the extra or_acc at texture.rs:1326, but I didn't isolate that and it hardly matters on x86_64.

A few smaller things.

  • In Staging (texture.rs:2101-2107) the blank flag now comes from the fused pass, but print_of(read, Some(hash)) still calls all_zero on the Some path (texture.rs:2291), and the callers throw .1 away. So a blank texture of 48 KiB or less is still scanned twice there.
  • Clippy gets 38 new incompatible_msrv warnings, all on the AVX-512 intrinsics in texture.rs:1184-1238. They're stable since 1.89 and the workspace says rust-version 1.88. CI doesn't run clippy and the pinned toolchain is newer, so nothing fails, but it doesn't match the "0 new warnings". Bumping rust-version or an allow on the two functions would fix it. It's the same with --features diagnostics.
  • Detection uses is_x86_feature_detected! (texture.rs:1155-1162), which std caches and which already checks XCR0 for the AVX-512 state. I didn't build for aarch64, but everything new sits behind cfg(target_arch = "x86_64") with the old scalar path as the fallback.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-04 21:23 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/507#issuecomment-7424619463) Checked 9b9f78e on native Windows with MSVC (didn't run the game). Debug and release builds of victory-gpu are fine with the pinned toolchain, and the intrinsics and target_feature attributes compile cleanly. `digest_and_zero_across_tiers` passes and `texture::` is 12 passed, 3 ignored. On the full lib suite the only failures are the ones that already fail natively on main (the two indirect host_list tests, the two pipeline::host library loads, and a watch test that passes when rerun alone). All three tiers ran here, and the dispatch picks VAES-512. I also ran a throwaway test of my own, which I didn't push. - The zero flag matched `all_zero` on every tier for lengths 0 to about 8 KiB, start offsets 0..63, and a single non-zero byte at each position including the last partial lane. Non-zero canary bytes just outside the slice never leaked in, which fits the tail being copied into a zeroed stack array (texture.rs:1216, 1280). - Each tier gives the same value for the same bytes whatever the alignment, and the scalar tier still gives exactly main's value. The tiers don't agree with each other, though. Every buffer gave three different digests. That looks harmless today, because the dispatch is fixed per process and every comparison I could find stays in memory (`last.hash`, `level.hash`, `Held` in resource.rs:1191, `blank_print` against `fingerprint_blank` in draw.rs:3911, the replay page marks). It would bite once a digest gets written to disk or compared between machines, so maybe say in the doc comment that the value depends on the CPU. - No collisions on any tier with random 4 KiB and 64 KiB buffers. I tried every single bit flip on 4 KiB, swapping two 16-byte blocks and two 64-byte blocks, sliding a 256-byte zero run around, mostly-zero buffers with one random 16-byte block, and the same bytes with 0..2000 zeros appended. That's about 450k digests per tier. From reading the code rather than testing, one AES round per block is a weak mixer against a deliberate two-block edit in the same lane, since those can cancel. For change detection on real textures I'd still expect it to hold up as well as the old fold. Release throughput on this machine, best of 5. The main row is main's `digest` + `all_zero`, measured in a separate run. | | 64 KiB | 1 MiB | 16 MiB | |---|---|---|---| | main, random data | 32 GB/s | 32 GB/s | 31 GB/s | | main, zero data | 17 GB/s | 17 GB/s | 16 GB/s | | PR scalar | 24-26 | 26 | 26 | | PR AES-NI | 81 | 80 | 68-71 | | PR VAES-512 | 215-218 | 188-204 | 88-106 | So the speedup comes from AES, while the fused zero check alone only helps on blank buffers. `all_zero` stops at the first non-zero word, so main already scans non-blank data only once. The scalar tier looked a bit slower than main, possibly the extra `or_acc` at texture.rs:1326, but I didn't isolate that and it hardly matters on x86_64. A few smaller things. - In `Staging` (texture.rs:2101-2107) the blank flag now comes from the fused pass, but `print_of(read, Some(hash))` still calls `all_zero` on the `Some` path (texture.rs:2291), and the callers throw `.1` away. So a blank texture of 48 KiB or less is still scanned twice there. - Clippy gets 38 new `incompatible_msrv` warnings, all on the AVX-512 intrinsics in texture.rs:1184-1238. They're stable since 1.89 and the workspace says rust-version 1.88. CI doesn't run clippy and the pinned toolchain is newer, so nothing fails, but it doesn't match the "0 new warnings". Bumping rust-version or an allow on the two functions would fix it. It's the same with `--features diagnostics`. - Detection uses `is_x86_feature_detected!` (texture.rs:1155-1162), which std caches and which already checks XCR0 for the AVX-512 state. I didn't build for aarch64, but everything new sits behind `cfg(target_arch = "x86_64")` with the old scalar path as the fallback. AI-assisted: yes
github-bot force-pushed github-bot/gh-507 from 9b9f78e807 to f0f595220f 2026-12-05 00:05:39 +00:00 Compare
Author
Owner

@⁠Not4Eric on GitHub · 2026-12-05 00:00 UTC · original

Thank you so much for helping, I'm not the best at this and I'm heavily relying on AI to even do this

Pushed updates addressing your review:

  • Added #[allow(clippy::incompatible_msrv)] on digest_and_zero_vaes512 (0 incompatible_msrv clippy warnings across --all-targets).
  • Changed print_of to take Option<(Digest, bool)> and updated all 4 callers so blank textures on the <= 48 KiB path reuse the fused all-zero check instead of scanning twice (all_zero kept for texture::ledger).
  • Noted in digest()'s doc comment that the hash depends on the runtime CPU feature tier.
**@⁠Not4Eric** on GitHub · 2026-12-05 00:00 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/507#issuecomment-7427748400) Thank you so much for helping, I'm not the best at this and I'm heavily relying on AI to even do this Pushed updates addressing your review: - Added `#[allow(clippy::incompatible_msrv)]` on `digest_and_zero_vaes512` (0 `incompatible_msrv` clippy warnings across `--all-targets`). - Changed `print_of` to take `Option<(Digest, bool)>` and updated all 4 callers so blank textures on the `<= 48 KiB` path reuse the fused all-zero check instead of scanning twice (`all_zero` kept for `texture::ledger`). - Noted in `digest()`'s doc comment that the hash depends on the runtime CPU feature tier.
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-05 00:59 UTC · original

Re-checked f0f5952 on native Windows (didn't run the game).

All three points are in. Clippy has no incompatible_msrv warnings left with --all-targets, with and without diagnostics, thanks to the allow at texture.rs:1180. print_of now takes the fused flag (texture.rs:2288-2293) and all four callers pass it (2108, 2135, 2158, 2414), so a blank texture of 48 KiB or less is scanned once. The CPU-tier note is on digest() at 1147. The digest code itself didn't change, so what I found last time about the tiers and the zero flag still holds. The full lib run shows only the four failures main already has natively, and it still merges cleanly onto current main.

One small thing I missed last time. rustfmt with the repo's max_width = 100 wants six changes in texture.rs, the DIGEST_SEEDS rows around 1173-1176, a few long lines in the AES functions (around 1235, 1242, 1298, 1348) and one in the test near 3250. main is fmt-clean for that file. A quick cargo fmt would fix it.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-05 00:59 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/507#issuecomment-7428825753) Re-checked f0f5952 on native Windows (didn't run the game). All three points are in. Clippy has no incompatible_msrv warnings left with --all-targets, with and without diagnostics, thanks to the allow at texture.rs:1180. print_of now takes the fused flag (texture.rs:2288-2293) and all four callers pass it (2108, 2135, 2158, 2414), so a blank texture of 48 KiB or less is scanned once. The CPU-tier note is on digest() at 1147. The digest code itself didn't change, so what I found last time about the tiers and the zero flag still holds. The full lib run shows only the four failures main already has natively, and it still merges cleanly onto current main. One small thing I missed last time. rustfmt with the repo's max_width = 100 wants six changes in texture.rs, the DIGEST_SEEDS rows around 1173-1176, a few long lines in the AES functions (around 1235, 1242, 1298, 1348) and one in the test near 3250. main is fmt-clean for that file. A quick cargo fmt would fix it. AI-assisted: yes
github-bot force-pushed github-bot/gh-507 from f0f595220f to f3dcf68e54 2026-12-05 11:46:12 +00:00 Compare
github manually merged commit 4d8317a279 into main 2026-12-05 17:38:44 +00:00
github-bot changed target branch from main to gh-pr/507/base 2026-12-05 17:38:46 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.