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

Open
mynzjerko wants to merge 1 commit from upstream-pr/318 into main
mynzjerko commented 2026-11-24 23:30:56 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by mynzjerko on 2026-11-24T23:30:56Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:issue:6593545169 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/318). Originally posted by [mynzjerko](https://github.com/mynzjerko) on 2026-11-24T23:30:56Z. --- ## 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._
Frtuchinno commented 2026-11-25 02:10:09 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Frtuchinno on 2026-11-25T01:38:08Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7149777369 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/318#issuecomment-7149777369). Originally posted by [Frtuchinno](https://github.com/Frtuchinno) on 2026-11-25T01:38:08Z. --- 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.
IxMxAMAR commented 2026-11-25 03:06:35 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by IxMxAMAR on 2026-11-25T02:37:15Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7150959824 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/318#issuecomment-7150959824). Originally posted by [IxMxAMAR](https://github.com/IxMxAMAR) on 2026-11-25T02:37:15Z. --- 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.
mynzjerko commented 2026-11-25 04:11:20 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by mynzjerko on 2026-11-25T03:16:03Z.


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.
<!-- greyfinch-sync:fxceless/victory-fork:comment:7151751687 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/318#issuecomment-7151751687). Originally posted by [mynzjerko](https://github.com/mynzjerko) on 2026-11-25T03:16:03Z. --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/318:upstream-pr/318'
git switch 'upstream-pr/318'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/318'
git switch 'upstream-pr/318'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/318'
git switch 'upstream-pr/318'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/318'
git switch 'main'
git merge --squash 'upstream-pr/318'
git switch 'main'
git merge --ff-only 'upstream-pr/318'
git switch 'main'
git merge 'upstream-pr/318'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
fxceless/victory-fork!318
No description provided.