Shaders: run wave32 pixel and vertex wave operations per 32-lane half #318

Open
github-bot wants to merge 1 commit from github-bot/gh-318 into main AGit
Owner

@⁠mynzjerko on GitHub · 2026-11-24 23:30 UTC · original
mynzjerko:fix/amd-wave32-host-halves → main · +291 −0 · 2 files · 1 commits · open

Problem

In SM 6.6, WaveSize can only be declared on compute shaders. On AMD RDNA GPUs (native 32/64-lane waves), the driver can launch console wave32 pixel (and vertex) shaders as 64-lane waves (observed on RDNA 4 / RX 9070).

In crates/victory-shader/src/emit/lower.rs, MaskData::Host wave operations on console wave32 shaders assumed a 32-lane host wave:

  • mask_dword (waveActiveBallot) always returned dword 0 (lanes 0..31), so upper-half lanes 32..63 read the lower half's ballot mask.
  • v_mbcnt_lo_u32_b32 used the raw host lane index (32..63 in the upper half), saturating the mask to 0xFFFFFFFF and returning 32 instead of 0..31.
  • v_readlane_b32 read from lane (0..31) instead of offsetting into the caller's 32-lane half (32..63).
  • first_lane (v_readfirstlane_b32) picked from all 64 lanes instead of the caller's 32-lane half.

When a driver schedules a wave32 pixel/vertex shader in 64-lane waves, this breaks wave-based loops and cross-lane reads. Note: this PR specifically fixes the wave32-on-wave64 lowering semantics; as confirmed by testing on RX 9070 XT, remaining magenta/purple NaN patches in #3 have an additional independent NaN source beyond this bug.

Fix

For self.mask_data == MaskData::Host && !self.x.p.wave64 in crates/victory-shader/src/emit/lower.rs, keep each 32-lane half independent:

  • mask_dword: select ballot dword 1 when lane >= 32, else dword 0.
  • v_mbcnt_lo_u32_b32: mask lane with 31.
  • v_readlane_b32: target (lane & 31) | (me & !31).
  • first_lane: ballot within the caller's 32-lane half and read (me & !31) | firstbitlow(half).

On 32-lane hosts (NVIDIA, WARP, or RDNA3 when the driver picks Wave32) lane < 32 is always true, so behavior is unchanged.

Verification

  • Added hardware GPU pixel-shader test wave32_pixel_shader_runs_as_32_lane_halves_on_wave64_hosts in crates/victory-shader/src/emit/wave64_tests.rs (renders a 16x16 full-screen triangle verifying v_mbcnt_lo, waveActiveBallot + s_ff1 + v_readlane_b32, and guarded v_readfirstlane_b32 across all 256 pixels):
    • Before fix on AMD Radeon RX 9070: failed immediately at pixel 64: v_mbcnt_lo gave lane 32 >= 32.
    • After fix on AMD Radeon RX 9070: passes (8 distinct 32-lane halves across 256 pixels).
  • Confirmed no regression on RX 7800 XT (RDNA 3, driver 32.0.32029.2010): 211 passed, 0 failed.
  • cargo test -p victory-shader (211 passed, 0 failed), cargo clippy -p victory-shader --all-targets -- -D warnings, and cargo xtask policy all pass.

AI-assisted (Antigravity); verified locally on an AMD Radeon RX 9070. No game data.

**@⁠mynzjerko** on GitHub · 2026-11-24 23:30 UTC · [original](https://github.com/fxceless/victory-fork/pull/318) `mynzjerko:fix/amd-wave32-host-halves` → `main` · +291 −0 · 2 files · 1 commits · open ## Problem In SM 6.6, `WaveSize` can only be declared on compute shaders. On AMD RDNA GPUs (native 32/64-lane waves), the driver can launch console wave32 pixel (and vertex) shaders as **64-lane waves** (observed on RDNA 4 / RX 9070). In `crates/victory-shader/src/emit/lower.rs`, `MaskData::Host` wave operations on console wave32 shaders assumed a 32-lane host wave: - `mask_dword` (`waveActiveBallot`) always returned dword 0 (`lanes 0..31`), so upper-half lanes `32..63` read the lower half's ballot mask. - `v_mbcnt_lo_u32_b32` used the raw host lane index (`32..63` in the upper half), saturating the mask to `0xFFFFFFFF` and returning `32` instead of `0..31`. - `v_readlane_b32` read from `lane` (`0..31`) instead of offsetting into the caller's 32-lane half (`32..63`). - `first_lane` (`v_readfirstlane_b32`) picked from all 64 lanes instead of the caller's 32-lane half. When a driver schedules a wave32 pixel/vertex shader in 64-lane waves, this breaks wave-based loops and cross-lane reads. Note: this PR specifically fixes the wave32-on-wave64 lowering semantics; as confirmed by testing on RX 9070 XT, remaining magenta/purple NaN patches in #3 have an additional independent NaN source beyond this bug. ## Fix For `self.mask_data == MaskData::Host && !self.x.p.wave64` in `crates/victory-shader/src/emit/lower.rs`, keep each 32-lane half independent: - `mask_dword`: select ballot dword 1 when `lane >= 32`, else dword 0. - `v_mbcnt_lo_u32_b32`: mask `lane` with `31`. - `v_readlane_b32`: target `(lane & 31) | (me & !31)`. - `first_lane`: ballot within the caller's 32-lane half and read `(me & !31) | firstbitlow(half)`. On 32-lane hosts (NVIDIA, WARP, or RDNA3 when the driver picks Wave32) `lane < 32` is always true, so behavior is unchanged. ## Verification - Added hardware GPU pixel-shader test `wave32_pixel_shader_runs_as_32_lane_halves_on_wave64_hosts` in `crates/victory-shader/src/emit/wave64_tests.rs` (renders a 16x16 full-screen triangle verifying `v_mbcnt_lo`, `waveActiveBallot` + `s_ff1` + `v_readlane_b32`, and guarded `v_readfirstlane_b32` across all 256 pixels): - Before fix on AMD Radeon RX 9070: failed immediately at `pixel 64: v_mbcnt_lo gave lane 32 >= 32`. - After fix on AMD Radeon RX 9070: passes (`8 distinct 32-lane halves across 256 pixels`). - Confirmed no regression on RX 7800 XT (RDNA 3, driver 32.0.32029.2010): 211 passed, 0 failed. - `cargo test -p victory-shader` (211 passed, 0 failed), `cargo clippy -p victory-shader --all-targets -- -D warnings`, and `cargo xtask policy` all pass. _AI-assisted (Antigravity); verified locally on an AMD Radeon RX 9070. No game data._
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:40:37 +00:00
Author
Owner

@⁠Frtuchinno on GitHub · 2026-11-25 01:38 UTC · original

An RDNA3 data point for this PR, since the description covers RDNA 3/4 and the test was run on an RX 9070.

Setup: AMD Radeon RX 7800 XT (RDNA3), driver 32.0.32029.2010, Windows 11 Pro build 26300, Rust 1.98.1. I tested this PR's head 507c307 and the same tree with crates/victory-shader/src/emit/lower.rs reset to 2119b0e, in separate target directories.

Tree wave32_pixel_shader_runs_as_32_lane_halves_on_wave64_hosts
without the fix (lower.rs from 2119b0e) passes: ran on the hardware device: 8 distinct 32-lane halves across 256 pixels
with the fix (507c307) passes, same line

Without the fix, no pixel gets v_mbcnt_lo ≥ 32. On this card and driver, the test's pixel shader therefore runs in 32-lane waves and the bug doesn't show up, unlike the RX 9070's pixel 64: v_mbcnt_lo gave lane 32 >= 32. That doesn't rule out the driver picking 64-lane waves for other pixel shaders on RDNA3. It only means this test doesn't exercise the fix here.

With the change, cargo test -p victory-shader --locked on this branch gives 211 passed, 0 failed, 2 ignored. On a host that already runs 32-lane pixel waves the change causes no regression.

AI-assisted (Claude Code): it ran both trees on my machine and drafted this.

**@⁠Frtuchinno** on GitHub · 2026-11-25 01:38 UTC · [original](https://github.com/fxceless/victory-fork/pull/318#issuecomment-7149777369) An RDNA3 data point for this PR, since the description covers RDNA 3/4 and the test was run on an RX 9070. **Setup:** AMD Radeon RX 7800 XT (RDNA3), driver 32.0.32029.2010, Windows 11 Pro build 26300, Rust 1.98.1. I tested this PR's head `507c307` and the same tree with `crates/victory-shader/src/emit/lower.rs` reset to `2119b0e`, in separate target directories. | Tree | `wave32_pixel_shader_runs_as_32_lane_halves_on_wave64_hosts` | |---|---| | without the fix (`lower.rs` from `2119b0e`) | **passes**: `ran on the hardware device: 8 distinct 32-lane halves across 256 pixels` | | with the fix (`507c307`) | passes, same line | Without the fix, no pixel gets `v_mbcnt_lo` ≥ 32. On this card and driver, the test's pixel shader therefore runs in 32-lane waves and the bug doesn't show up, unlike the RX 9070's `pixel 64: v_mbcnt_lo gave lane 32 >= 32`. That doesn't rule out the driver picking 64-lane waves for other pixel shaders on RDNA3. It only means this test doesn't exercise the fix here. With the change, `cargo test -p victory-shader --locked` on this branch gives 211 passed, 0 failed, 2 ignored. On a host that already runs 32-lane pixel waves the change causes no regression. AI-assisted (Claude Code): it ran both trees on my machine and drafted this.
Author
Owner

@⁠IxMxAMAR on GitHub · 2026-11-25 02:37 UTC · original

Three runs on an RX 9070 XT (driver 32.0.32029.2010):

  • fd021bf + this PR, no NaN guard: purple and magenta blocks still on the bedding, posters and a floor strip in the dormitory cutscene.
  • 19feca8 + this PR + PastiiJay's NaN guard on colour exports and typed stores (klbf-amd 0e5f4b2): small purple spots left, one appearing while standing still.
  • c96b4f2 + this PR + the same NaN guard (and #195): no purple in 14 minutes, through the dormitory and the yard view.

So on this card this PR alone was not enough; with the NaN guard on current main it was.

**@⁠IxMxAMAR** on GitHub · 2026-11-25 02:37 UTC · [original](https://github.com/fxceless/victory-fork/pull/318#issuecomment-7150959824) Three runs on an RX 9070 XT (driver 32.0.32029.2010): - `fd021bf` + this PR, no NaN guard: purple and magenta blocks still on the bedding, posters and a floor strip in the dormitory cutscene. - `19feca8` + this PR + PastiiJay's NaN guard on colour exports and typed stores (`klbf-amd` `0e5f4b2`): small purple spots left, one appearing while standing still. - `c96b4f2` + this PR + the same NaN guard (and #195): no purple in 14 minutes, through the dormitory and the yard view. So on this card this PR alone was not enough; with the NaN guard on current main it was.
Author
Owner

@⁠mynzjerko on GitHub · 2026-11-25 03:16 UTC · original

Thanks @⁠Frtuchinno and @⁠IxMxAMAR for the data points on RX 7800 XT and RX 9070 XT!

I've updated the PR description to clarify the scope:

  • This specifically fixes the wave32-on-wave64 lowering semantics when the driver launches a wave32 pixel/vertex shader in 64-lane waves (observed on RDNA 4 / RX 9070; no-op / zero regression when the driver picks Wave32, as on RX 7800 XT).
  • Remaining magenta/purple NaN patches in #3 have a separate NaN source on top of this, matching @⁠IxMxAMAR's runs with the NaN guard on main.
**@⁠mynzjerko** on GitHub · 2026-11-25 03:16 UTC · [original](https://github.com/fxceless/victory-fork/pull/318#issuecomment-7151751687) Thanks @⁠Frtuchinno and @⁠IxMxAMAR for the data points on RX 7800 XT and RX 9070 XT! I've updated the PR description to clarify the scope: - This specifically fixes the wave32-on-wave64 lowering semantics when the driver launches a wave32 pixel/vertex shader in 64-lane waves (observed on RDNA 4 / RX 9070; no-op / zero regression when the driver picks Wave32, as on RX 7800 XT). - Remaining magenta/purple NaN patches in #3 have a separate NaN source on top of this, matching @⁠IxMxAMAR's runs with the NaN guard on `main`.
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/wave64_tests.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/318/head:github-bot/gh-318'
git switch 'github-bot/gh-318'

Merge

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