GPU: AVX2 fast-path and skip-unchanged writes in merge_gpu_writes* #123

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

@⁠altuux on GitHub · 2026-11-25 14:56 UTC · original
altuux:perf/avx2-merge-gpu-writes → main · +428 −6 · 1 files · 3 commits · open

Summary

Optimizes merge_gpu_writes, merge_gpu_writes_mut, and merge_gpu_writes_card in crates/victory-gpu/src/shadow.rs:

  1. Skip unnecessary writes on unchanged words/chunks: Previously, merge_gpu_writes and merge_gpu_writes_mut unconditionally copied title back into host (and old) whenever hw == ow, even when title == host, dirtying cachelines and mapped D3D12 buffers unnecessarily. Added the tw != hw check to the scalar fallback (matching merge_gpu_writes_card) and a zero-write fast path when host == old == title.
  2. AVX2 fast-path (32 bytes/iter) with runtime is_x86_feature_detected!("avx2") dispatch and the existing u64 scalar fallback preserved (zero new dependencies). Uses _mm256_cmpeq_epi8 + _mm256_blendv_epi8 and computes the tight [first, last) span via trailing_zeros / leading_zeros.
  3. Unit test: Added merge_gpu_writes_matches_scalar_across_lengths_and_patterns verifying bit-for-bit agreement between scalar and AVX2 paths across unaligned lengths (0..4096) and all CPU/GPU write combinations (including overlapping CPU+GPU writes to the same byte).

Synthetic CPU Benchmark (i5-12400, 64 MiB = 1024 × 64 KiB tiles, 5% GPU-modified)

  • merge_gpu_writes_mut: 15.61 ms → 9.16 ms (1.70× faster, 4.00 → 6.82 GB/s)
  • merge_gpu_writes_card: 13.23 ms → 9.29 ms (1.42× faster, 4.72 → 6.73 GB/s)

On mapped Write-Combined D3D12 memory, avoiding redundant writes on unchanged chunks should save additional bandwidth on top of the cached-RAM benchmark above.

**@⁠altuux** on GitHub · 2026-11-25 14:56 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/123) `altuux:perf/avx2-merge-gpu-writes` → `main` · +428 −6 · 1 files · 3 commits · open ### Summary Optimizes `merge_gpu_writes`, `merge_gpu_writes_mut`, and `merge_gpu_writes_card` in `crates/victory-gpu/src/shadow.rs`: 1. **Skip unnecessary writes on unchanged words/chunks**: Previously, `merge_gpu_writes` and `merge_gpu_writes_mut` unconditionally copied `title` back into `host` (and `old`) whenever `hw == ow`, even when `title == host`, dirtying cachelines and mapped D3D12 buffers unnecessarily. Added the `tw != hw` check to the scalar fallback (matching `merge_gpu_writes_card`) and a zero-write fast path when `host == old == title`. 2. **AVX2 fast-path (32 bytes/iter)** with runtime `is_x86_feature_detected!("avx2")` dispatch and the existing `u64` scalar fallback preserved (zero new dependencies). Uses `_mm256_cmpeq_epi8` + `_mm256_blendv_epi8` and computes the tight `[first, last)` span via `trailing_zeros` / `leading_zeros`. 3. **Unit test**: Added `merge_gpu_writes_matches_scalar_across_lengths_and_patterns` verifying bit-for-bit agreement between scalar and AVX2 paths across unaligned lengths (`0..4096`) and all CPU/GPU write combinations (including overlapping CPU+GPU writes to the same byte). ### Synthetic CPU Benchmark (i5-12400, 64 MiB = 1024 × 64 KiB tiles, 5% GPU-modified) - `merge_gpu_writes_mut`: **15.61 ms → 9.16 ms** (**1.70× faster**, 4.00 → 6.82 GB/s) - `merge_gpu_writes_card`: **13.23 ms → 9.29 ms** (**1.42× faster**, 4.72 → 6.73 GB/s) On mapped Write-Combined D3D12 memory, avoiding redundant writes on unchanged chunks should save additional bandwidth on top of the cached-RAM benchmark above.
legacy is merge_gpu_writes{,_mut} as on main (every unchanged word written
back to the copy), scalar skips unchanged words without AVX2, unset takes
AVX2 when the processor has it. The test now also compares with the code
from main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:36:11 +00:00
Author
Owner

@⁠sil-kong on GitHub · 2026-11-25 15:02 UTC · original

Tested on Linux (Arch, RX 7900 XT with RADV, Ryzen 9 5900X, GE-Proton10-34's Wine + vkd3d-proton), Belobug main at 01aadd1 with this PR applied on top, release build, same save and same spot, warm caches, mean of 8 world frames:

without with this PR
frame 624 ms (1.6 fps) 350 ms (~3 fps)
copying back ~100 ms ~54 ms

The whole frame gains more than the copy-back itself, so not writing unchanged words back seems to spare work in the next submission's copies too. No new rendering problem seen at that spot.

**@⁠sil-kong** on GitHub · 2026-11-25 15:02 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/123#issuecomment-7166211002) Tested on Linux (Arch, RX 7900 XT with RADV, Ryzen 9 5900X, GE-Proton10-34's Wine + vkd3d-proton), Belobug main at 01aadd1 with this PR applied on top, release build, same save and same spot, warm caches, mean of 8 world frames: | | without | with this PR | |---|---|---| | frame | 624 ms (1.6 fps) | 350 ms (~3 fps) | | copying back | ~100 ms | ~54 ms | The whole frame gains more than the copy-back itself, so not writing unchanged words back seems to spare work in the next submission's copies too. No new rendering problem seen at that spot.
Author
Owner

@⁠jmal423 on GitHub · 2026-11-25 16:41 UTC · original

Tested this PR on Windows 11 / AMD Radeon RX 9070 XT (Series X target):

With this PR applied, all prison/corridor walls disappear (likely due to a skipped/mis-merged GPU culling buffer write). Reverting this PR immediately restores all walls.

**@⁠jmal423** on GitHub · 2026-11-25 16:41 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/123#issuecomment-7168382480) Tested this PR on Windows 11 / AMD Radeon RX 9070 XT (Series X target): With this PR applied, all prison/corridor walls disappear (likely due to a skipped/mis-merged GPU culling buffer write). Reverting this PR immediately restores all walls.
Author
Owner

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

@⁠jmal423 Thanks for testing! I couldn't reproduce a difference in the merge logic itself: a 200k-case randomized comparison against the pre-PR functions from main gives byte-identical results, so if this PR causes it, it's the skipped (redundant) writes to the mapped copy rather than the AVX2 math.

I pushed a switch to narrow it down (cad3975) — could you try the same spot with each of these?

  • VICTORY_MERGE=legacy — exact pre-PR code
  • VICTORY_MERGE=scalar — skip-unchanged without AVX2
  • unset — AVX2 (current PR)

Also: which base commit did you apply it on, and do you have any other VICTORY_* set (especially VICTORY_ASYNC_SUBMIT)?

**@⁠altuux** on GitHub · 2026-11-25 18:25 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/123#issuecomment-7170890465) @⁠jmal423 Thanks for testing! I couldn't reproduce a difference in the merge logic itself: a 200k-case randomized comparison against the pre-PR functions from `main` gives byte-identical results, so if this PR causes it, it's the skipped (redundant) writes to the mapped copy rather than the AVX2 math. I pushed a switch to narrow it down (cad3975) — could you try the same spot with each of these? - `VICTORY_MERGE=legacy` — exact pre-PR code - `VICTORY_MERGE=scalar` — skip-unchanged without AVX2 - unset — AVX2 (current PR) Also: which base commit did you apply it on, and do you have any other `VICTORY_*` set (especially `VICTORY_ASYNC_SUBMIT`)?
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/123/head:github-bot/gh-123'
git switch 'github-bot/gh-123'

Merge

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