Patch wave32 per-lane tile-list allocations in compute shaders #2

Closed
github-bot wants to merge 1 commit from gh-pr/2/head into gh-pr/2/base
Owner

@⁠Logard0 on GitHub · 2026-12-05 11:57 UTC · original
Logard0:fix/water-tile-alloc → main · +547 −0 · 3 files · 1 commits · closed without merging

Fixes dark/missing water light tiles on 32-lane hosts (TV_GenerateTiles) by patching wave64 compute shaders that allocate tile-list slots via v_cmpx leader election, based on xss3009's idea from the SmXme fork.

Background

On 32-lane hosts, the two halves of a 64-lane console wave can reach the tile-list allocation on different loop passes. A half without the elected lane then takes slots reserved by another pass or wave, overwriting earlier tiles and causing them to lose lighting.

Summary of changes

  • Exact-match GFX10 patch (crates/larp-shader/src/tile_alloc.rs): Called from NativeProgram::patched_code after by_hash::replace_code on 32-lane hosts (host_uses_wave32(), disableable via LARP_TILE_ALLOC_PATCH=0). Fires only when all criteria match:
    • HwStage::Cs and wave64 == true;
    • exactly two buffer_atomic_add glc allocations on the same rsrc at byte offsets 0 and 32;
    • the four instructions preceding each add (skipping s_nop and s_waitcnt*) are v_mov_b32 v<result>, s<count>, s_bcnt1_i32_b64, s_cbranch_execz, and v_cmpx_*;
    • within 10 instructions after each add, the full v_mbcnt_lo_u32_b32, v_readfirstlane_b32, v_mbcnt_hi_u32_b32, v_add_nc_u32 chain with matching registers and no intervening writes to v<result>.
  • Rewrite per allocation: Rewrites v_cmpx_* to s_nop 0, s_bcnt1_i32_b64 to s_mov_b32 s<count>, 1, v_mbcnt_lo to v_mov_b32 v<lo>, 0, v_readfirstlane_b32 to s_mov_b32 s<base>, 0, and v_mbcnt_hi to v_mov_b32 v<hi>, v<result>, so every active lane allocates 1 slot and uses its own atomic return value.
  • Shader cache integration (crates/larp-shader/src/lib.rs): Added tile_alloc.rs to sources() and "LARP_TILE_ALLOC_PATCH" to SWITCHES.
  • Unit tests: Added tests verifying exact rewriting of matching wave64 compute code and untouched pass-through for non-matching cases (wrong stage/wave32, wrong offsets, mismatched rsrc, clobbered result register).

Verification

  • cargo test --locked -p larp-shader tile_alloc: 2 passed, 0 failed
  • cargo clippy --locked --tests -p larp-shader: 0 warnings in tile_alloc.rs, native.rs, and lib.rs

Made with the help of Gemini 4 Neon.

**@⁠Logard0** on GitHub · 2026-12-05 11:57 UTC · [original](https://github.com/gamingwithhomie/larp/pull/2) `Logard0:fix/water-tile-alloc` → `main` · +547 −0 · 3 files · 1 commits · closed without merging Fixes dark/missing water light tiles on 32-lane hosts (`TV_GenerateTiles`) by patching wave64 compute shaders that allocate tile-list slots via `v_cmpx` leader election, based on xss3009's idea from the SmXme fork. ### Background On 32-lane hosts, the two halves of a 64-lane console wave can reach the tile-list allocation on different loop passes. A half without the elected lane then takes slots reserved by another pass or wave, overwriting earlier tiles and causing them to lose lighting. ### Summary of changes - **Exact-match GFX10 patch (`crates/larp-shader/src/tile_alloc.rs`):** Called from `NativeProgram::patched_code` after `by_hash::replace_code` on 32-lane hosts (`host_uses_wave32()`, disableable via `LARP_TILE_ALLOC_PATCH=0`). Fires only when all criteria match: - `HwStage::Cs` and `wave64 == true`; - exactly two `buffer_atomic_add` `glc` allocations on the same `rsrc` at byte offsets `0` and `32`; - the four instructions preceding each add (skipping `s_nop` and `s_waitcnt*`) are `v_mov_b32 v<result>, s<count>`, `s_bcnt1_i32_b64`, `s_cbranch_execz`, and `v_cmpx_*`; - within 10 instructions after each add, the full `v_mbcnt_lo_u32_b32`, `v_readfirstlane_b32`, `v_mbcnt_hi_u32_b32`, `v_add_nc_u32` chain with matching registers and no intervening writes to `v<result>`. - **Rewrite per allocation:** Rewrites `v_cmpx_*` to `s_nop 0`, `s_bcnt1_i32_b64` to `s_mov_b32 s<count>, 1`, `v_mbcnt_lo` to `v_mov_b32 v<lo>, 0`, `v_readfirstlane_b32` to `s_mov_b32 s<base>, 0`, and `v_mbcnt_hi` to `v_mov_b32 v<hi>, v<result>`, so every active lane allocates 1 slot and uses its own atomic return value. - **Shader cache integration (`crates/larp-shader/src/lib.rs`):** Added `tile_alloc.rs` to `sources()` and `"LARP_TILE_ALLOC_PATCH"` to `SWITCHES`. - **Unit tests:** Added tests verifying exact rewriting of matching wave64 compute code and untouched pass-through for non-matching cases (wrong stage/wave32, wrong offsets, mismatched `rsrc`, clobbered result register). ### Verification - `cargo test --locked -p larp-shader tile_alloc`: 2 passed, 0 failed - `cargo clippy --locked --tests -p larp-shader`: 0 warnings in `tile_alloc.rs`, `native.rs`, and `lib.rs` Made with the help of Gemini 4 Neon.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-05 17:21:26 +00:00
github-bot closed this pull request 2026-12-05 17:21:26 +00:00
Author
Owner

@⁠gamingwithhomie on GitHub · 2026-12-05 14:46 UTC · original · edited 2026-12-05 15:04 UTC

Thanks for the clean PR! After checking crates/larp-shader/src/emit/by_hash/whole.rs (lines 214-225), stock larp already handles TV_GenerateTiles (0x1aa247ed3653b616) natively via Rule::LocalWaves. Closing this so we don't invalidate users' shader caches unnecessarily — appreciate your other PRs (#1 and #3 are merged!).

**@⁠gamingwithhomie** on GitHub · 2026-12-05 14:46 UTC · [original](https://github.com/gamingwithhomie/larp/pull/2#issuecomment-7439847265) · edited 2026-12-05 15:04 UTC Thanks for the clean PR! After checking `crates/larp-shader/src/emit/by_hash/whole.rs` (lines 214-225), stock `larp` already handles `TV_GenerateTiles` (`0x1aa247ed3653b616`) natively via `Rule::LocalWaves`. Closing this so we don't invalidate users' shader caches unnecessarily — appreciate your other PRs (#1 and #3 are merged!).

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.