recomp: AVX-512 recompile of vic_SpatialSegmentCullAvx2 #135

Open
github-bot wants to merge 3 commits from github-bot/gh-134 into main AGit
Owner

@⁠SovietBearz0r on GitHub · 2026-12-05 16:34 UTC · original
SovietBearz0r:rdna3-avx512-spatial-cull → main · +7596 −510 · 58 files · 49 commits · draft

Recompile the title's spatial segment-cull kernel in Rust with an AVX-512 form: 16 segments tested at a time.

What

The title's vic_SpatialSegmentCullAvx2 (+0x2987e60, 632 bytes) culls scene segments against a spatial grid on the CPU. This adds a Rust replacement with three forms — AVX-512 (16-wide), AVX2 (8-wide) and scalar — chosen by CPU feature detection, wired through the existing surgical-recomp pool next to vic_SpatialRadiusCullAvx2:

  • entry RVA 0x298_7e60 and its 24-byte prologue are byte-verified before the detour is installed (a different build is a safe skip, as for every recompiled kernel);
  • the startup report now says "2 of 2 SIMD spatial culling kernels replaced in Rust";
  • the cull maths helpers and the scalar/AVX2/AVX-512 forms are in crates/victory-recomp/src/spatial.rs, with the equivalence test across edge cases (empty/zero-length segments, degenerate params, grid bounds, NaN/inf inputs, random cases) comparing all three against each other.

Verification

  • cargo test -p victory-recomp: 29 passed in a private target dir, including spatial_segment_cull_matches_scalar_across_edge_cases; the AVX-512 path actually runs on this machine's Ryzen 9 9900X (otherwise the test says it skipped).
  • No behaviour change beyond the recompiled kernel; no new dependencies.
  • Needs one in-game check before merging: the log should show the spatial recomp active with 2 of 2 kernels replaced on 1.0.2.1, and the picture/behaviour should be unchanged (culling is CPU-side; a wrong replacement would show as missing/extra geometry or a crash, which the byte-verified prologue prevents on other builds).

Source: ReVictory-RDNA3 commit cc22fb9 ("recompile vic_SpatialRadiusCullAvx2 (AVX-512 16-wide)"; the fork registers it as vic_SpatialSegmentCullAvx2), ported onto this tree's spatial recomp module.


Final verification (branch tip df0ef2d, folding in the real-binary disassembly check):

  • Re-derived the ABI and maths from the actual 1.0.2.1 binary at +0x2987e60 and its caller (capstone): grid layout count/xs/ys/zs/r_sq at r9+0/8/16/24/32; args (out_idx, out_dist, out_pos, grid, start, end, seg_start, seg_end); t = clamp(dot(p-A, dir)/|dir|^2, 0, 1); d2 <= r_sq inclusive; ascending 8-wide chunks; the 24 prologue bytes match the fork's constant.
  • cargo test -p victory-recomp (with VICTORY_TEST_GAME_EXE): 29 passed, including spatial_segment_cull_matches_scalar_across_edge_cases (scalar, AVX2, direct AVX-512 and dispatch) and real_title_prologues_match_when_game_exe_env_set (new RVA/prologue checked against the real exe).
  • Deliberate deviations, worth knowing: correctly-rounded 1/|dir|^2 where the title uses vrcpps + 1 NR step, and the folded error term; last-ulp differences (rarely an exact-boundary cull decision); garbage lanes past the returned count are not written (callers must not read past it); no measured perf delta published by the fork.
  • Still needs one in-game check: log shows the spatial recomp active with "2 of 2 kernels replaced", and geometry/picture unchanged on 1.0.2.1.
**@⁠SovietBearz0r** on GitHub · 2026-12-05 16:34 UTC · [original](https://github.com/ReTerryDavis/ReVIctory/pull/134) `SovietBearz0r:rdna3-avx512-spatial-cull` → `main` · +7596 −510 · 58 files · 49 commits · draft Recompile the title's spatial segment-cull kernel in Rust with an AVX-512 form: 16 segments tested at a time. ## What The title's `vic_SpatialSegmentCullAvx2` (`+0x2987e60`, 632 bytes) culls scene segments against a spatial grid on the CPU. This adds a Rust replacement with three forms — AVX-512 (16-wide), AVX2 (8-wide) and scalar — chosen by CPU feature detection, wired through the existing surgical-recomp pool next to `vic_SpatialRadiusCullAvx2`: - entry RVA `0x298_7e60` and its 24-byte prologue are byte-verified before the detour is installed (a different build is a safe skip, as for every recompiled kernel); - the startup report now says "2 of 2 SIMD spatial culling kernels replaced in Rust"; - the cull maths helpers and the scalar/AVX2/AVX-512 forms are in `crates/victory-recomp/src/spatial.rs`, with the equivalence test across edge cases (empty/zero-length segments, degenerate params, grid bounds, NaN/inf inputs, random cases) comparing all three against each other. ## Verification - `cargo test -p victory-recomp`: **29 passed** in a private target dir, including `spatial_segment_cull_matches_scalar_across_edge_cases`; the AVX-512 path actually runs on this machine's Ryzen 9 9900X (otherwise the test says it skipped). - No behaviour change beyond the recompiled kernel; no new dependencies. - **Needs one in-game check** before merging: the log should show the spatial recomp active with 2 of 2 kernels replaced on 1.0.2.1, and the picture/behaviour should be unchanged (culling is CPU-side; a wrong replacement would show as missing/extra geometry or a crash, which the byte-verified prologue prevents on other builds). Source: ReVictory-RDNA3 commit `cc22fb9` ("recompile vic_SpatialRadiusCullAvx2 (AVX-512 16-wide)"; the fork registers it as `vic_SpatialSegmentCullAvx2`), ported onto this tree's spatial recomp module. --- **Final verification (branch tip `df0ef2d`, folding in the real-binary disassembly check):** - Re-derived the ABI and maths from the actual 1.0.2.1 binary at `+0x2987e60` and its caller (capstone): grid layout `count/xs/ys/zs/r_sq` at r9+0/8/16/24/32; args `(out_idx, out_dist, out_pos, grid, start, end, seg_start, seg_end)`; `t = clamp(dot(p-A, dir)/|dir|^2, 0, 1)`; `d2 <= r_sq` inclusive; ascending 8-wide chunks; the 24 prologue bytes match the fork's constant. - `cargo test -p victory-recomp` (with `VICTORY_TEST_GAME_EXE`): **29 passed**, including `spatial_segment_cull_matches_scalar_across_edge_cases` (scalar, AVX2, direct AVX-512 and dispatch) and `real_title_prologues_match_when_game_exe_env_set` (new RVA/prologue checked against the real exe). - Deliberate deviations, worth knowing: correctly-rounded `1/|dir|^2` where the title uses `vrcpps` + 1 NR step, and the folded error term; last-ulp differences (rarely an exact-boundary cull decision); garbage lanes past the returned count are not written (callers must not read past it); no measured perf delta published by the fork. - **Still needs one in-game check:** log shows the spatial recomp active with "2 of 2 kernels replaced", and geometry/picture unchanged on 1.0.2.1.
The title's spatial segment-cull kernel (`+0x2987e60`, 632 bytes) is
recompiled in Rust: an AVX-512 form that tests 16 segments at a time, an
AVX2 form for CPUs without it, and the scalar form, chosen by CPU feature
detection. The entry RVA and its 24-byte prologue are byte-verified before
replacing; the report says 2 of 2 SIMD spatial kernels.

From ReVictory-RDNA3 commit cc22fb9 (16-wide AVX-512 spatial culling); the
scalar/AVX2/AVX-512 equivalence test across edge cases is included.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-05 16:35:44 +00:00
github-bot force-pushed github-bot/gh-134 from 80cea0ab22 to df0ef2d9ca 2026-12-05 16:40:59 +00:00 Compare
Author
Owner

@⁠ReTerryDavis on GitHub · 2026-12-05 17:39 UTC · original

Clean AVX-512 kernel and thorough edge-case equivalence tests! Marking as draft for two items before merge:

  1. target_feature soundness: recomp_spatial_segment_cull checks only is_x86_feature_detected!(avx512f) before entering spatial_segment_cull_avx512, which calls spatial_segment_cull_avx2 (#[target_feature(enable = avx2,fma)]) for odd trailing chunks — please check avx512f && avx2 && fma so the Rust target_feature contract is airtight.
  2. Upcoming spatial.rs overlap + in-game check: We have a large local victory-recomp batch about to land that also adds CULL_FilterSpatialSegmentAvx2 (+0x2987e60, AVX2+FMA) and ADVANCE_SPATIAL_ITER_RVA in spatial.rs. Once that hits main, rebasing your AVX-512 path on top and verifying in-game (replaced log + no missing geometry) will make it an easy merge!
**@⁠ReTerryDavis** on GitHub · 2026-12-05 17:39 UTC · [original](https://github.com/ReTerryDavis/ReVIctory/pull/134#issuecomment-7442375016) Clean AVX-512 kernel and thorough edge-case equivalence tests! Marking as draft for two items before merge: 1. **`target_feature` soundness:** `recomp_spatial_segment_cull` checks only `is_x86_feature_detected!(avx512f)` before entering `spatial_segment_cull_avx512`, which calls `spatial_segment_cull_avx2` (`#[target_feature(enable = avx2,fma)]`) for odd trailing chunks — please check `avx512f && avx2 && fma` so the Rust `target_feature` contract is airtight. 2. **Upcoming `spatial.rs` overlap + in-game check:** We have a large local `victory-recomp` batch about to land that also adds `CULL_FilterSpatialSegmentAvx2` (`+0x2987e60`, AVX2+FMA) and `ADVANCE_SPATIAL_ITER_RVA` in `spatial.rs`. Once that hits `main`, rebasing your AVX-512 path on top and verifying in-game (replaced log + no missing geometry) will make it an easy merge!
Both AVX-512 kernels hand an odd trailing 8-entity chunk to the
victory-recomp AVX2 kernel, whose `#[target_feature(enable = "avx2,fma")]`
contract is only met when those features are present: an AVX-512F-only CPU
does not imply them. The dispatch predicates now ask for
`avx512f && avx2 && fma` through one pure helper (unit-tested), and the
trailing-chunk call inside each AVX-512 kernel rechecks avx2/fma itself and
uses the scalar tail otherwise, so a direct caller cannot break the contract
either.

Rebased on current origin/main (8bff52be), which does not yet have the
CULL_FilterSpatialSegmentAvx2 / ADVANCE_SPATIAL_ITER_RVA victory-recomp batch,
so there was nothing of it to keep; the branch's second segment-cull kernel is
kept as it was. In-game verification remains owed until that batch lands.
Author
Owner

@⁠SovietBearz0r on GitHub · 2026-12-05 18:55 UTC · original

target_feature contract fixed: dispatch now requires avx512f && avx2 && fma (pure avx512_spatial_cull_allowed with a unit test), and inside each AVX-512 kernel the odd trailing-chunk AVX2 call rechecks avx2 && fma before entering it, else the scalar tail — applied to the segment kernel and the identical radius-kernel hazard. origin/main at 8bff52be does not yet contain CULL_FilterSpatialSegmentAvx2/ADVANCE_SPATIAL_ITER_RVA, so the branch is rebased on current main and ready to re-rebase on that batch when it lands. 31 recomp tests pass. Staying draft for the in-game check (replaced log + no missing geometry) after that batch.

**@⁠SovietBearz0r** on GitHub · 2026-12-05 18:55 UTC · [original](https://github.com/ReTerryDavis/ReVIctory/pull/134#issuecomment-7443555111) `target_feature` contract fixed: dispatch now requires `avx512f && avx2 && fma` (pure `avx512_spatial_cull_allowed` with a unit test), and inside each AVX-512 kernel the odd trailing-chunk AVX2 call rechecks `avx2 && fma` before entering it, else the scalar tail — applied to the segment kernel and the identical radius-kernel hazard. `origin/main` at 8bff52be does not yet contain `CULL_FilterSpatialSegmentAvx2`/`ADVANCE_SPATIAL_ITER_RVA`, so the branch is rebased on current main and ready to re-rebase on that batch when it lands. 31 recomp tests pass. Staying draft for the in-game check (replaced log + no missing geometry) after that batch.
This pull request has changes conflicting with the target branch.
  • crates/victory-recomp/src/lib.rs
  • crates/victory-recomp/src/spatial.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/135/head:github-bot/gh-134'
git switch 'github-bot/gh-134'

Merge

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