Skip to content

Shaders: a host-wave lane mask uses both ballot dwords - #147

Closed
Luislariosss wants to merge 2 commits into
Marvisak:mainfrom
Luislariosss:verify/wave32-mask
Closed

Luislariosss wants to merge 2 commits into
Marvisak:mainfrom
Luislariosss:verify/wave32-mask

Conversation

@Luislariosss

Copy link
Copy Markdown
Contributor

Fixes #125.

For a pixel or vertex program the lowering keeps MaskData::Host and no WaveSize, so the host picks the wave size — and on AMD a vertex shader is not necessarily wave32. mask_dword returns only the low dword of the ballot when the console program is wave32 (nothing enters st.highs), and the mask uses read that dword through src32. On a 64-lane host wave the lanes above 31 were lost from every count, from v_mbcnt and from mask equality.

Changes

  • The ballot is computed once in ballot_pair, which mask_dword now uses too, so there is a single place that emits waveActiveBallot.
  • mask_pair resolves an instruction's source to its tracked mask and returns both dwords. It reads the source list from Fields::Valu or Fields::Sop — the scalar forms are SOP1 and the vector ones VALU — and maps the listing's exec/vcc inline constants (Const(252), Const(251)) to Reg::S(EXEC)/Reg::S(VCC).
  • s_bcnt1_i32_b32 and s_ff1_i32_b32 count over both dwords exactly as the _b64 forms already did.
  • v_mbcnt_lo_u32_b32 counts the low dword below min(lane, 32) plus the high dword below the lanes past 31.

Every path that is not a tracked host-wave mask — including all of MaskData::Console and mesh — is byte for byte what it was: the scalar fallback still calls src32 with the same unaryBits op, and v_mbcnt_hi keeps its old body.

Testing

  • cargo test -p victory-shader: 70 passed, 0 failed — the 69 the crate had plus host_wave32_mask_ops_use_both_ballot_halves, which fails on main with got 0 and passes here.
  • cargo clippy -p victory-shader --all-targets: clean.

The reproduction is a synthetic wave32 pixel program built from hand-written GFX10 words, so it needs no game data. I have no copy of the game, so I could not measure whether this path is hot in the title's shaders; the change is justified by the code and by the test only.

AI-assisted.

…el/vertex mask ops

Test-only reproduction for the issue: for Ps/Vs the lowering keeps MaskData::Host and no WaveSize, mask_dword returns only the low dword of the ballot, and s_bcnt1_i32_b32 / s_ff1_i32_b32 / v_mbcnt_lo_u32_b32 read it through src32. On a 64-lane host wave the high half is never referenced. Adds uses_of() under #[cfg(test)] and one test over a hand-written wave32 pixel program. No production code changes.
…k#125)

For a pixel or vertex program the lowering keeps MaskData::Host and no
WaveSize, so the host picks the wave size, and on AMD a vertex shader is not
necessarily wave32. mask_dword returns only the low dword of the ballot when
the console program is wave32 (nothing enters st.highs), and the mask uses
read that dword through src32, so on a 64-lane host wave the lanes above 31
were lost from every count, from �_mbcnt and from mask equality.

The ballot is now computed once in �allot_pair, which mask_dword also uses,
and a new mask_pair resolves an instruction's source to its tracked mask and
returns both dwords. It reads the source list from either Fields::Valu or
Fields::Sop, because the scalar forms are SOP1 and the vector ones VALU, and
it maps the listing's exec/�cc inline constants to Reg::S(EXEC)/VCC.

s_bcnt1_i32_b32 and s_ff1_i32_b32 then count over both dwords exactly as
the _b64 forms already did, and �_mbcnt_lo_u32_b32 counts the low dword
below min(lane, 32) plus the high dword below the lanes past 31. Every path
that is not a tracked host-wave mask, including all of MaskData::Console, is
byte for byte what it was: the scalar fallback still calls src32 with the
same unaryBits op.

cargo test -p victory-shader: 70 passed, 0 failed (the 69 before plus the
reproduction). cargo clippy -p victory-shader --all-targets: clean.
@DaviTostes

Copy link
Copy Markdown

Tested 35caba4 on Arch Linux x86_64, rustc 1.98.1: pass.

No game data used. AI-assisted (DeepSeek V4.1 Flash coding agent).

@Luislariosss

Copy link
Copy Markdown
Contributor Author

Closing this as a duplicate of #145, which does the same fix and was opened 12 seconds earlier (00:57:40 vs 00:57:52 — two agents reached it at once, nothing more interesting than that).

I read #145 before deciding: it has the same shape as this one. mask_ballot is the ballot-pair helper this PR calls ballot_pair, host_mask_pair is what this PR calls mask_pair, and both route the three SOP1/VALU sites through it. Keeping one of them is the right call, and the earlier one is the one to keep.

What is worth carrying over from here if it is not in #145:

  • the reproduction is on the fork branch verify/wave32-mask (commit 7004dfe3b), test-only, and it fails on main 9aef3d9c0 with expected high ballot half to be used by s_bcnt1_i32_b32, s_ff1_i32_b32, and v_mbcnt_lo_u32_b32 (>= 3 uses), got 0. It is the red-first half of the work and it is independent of how the fix is written;
  • the one thing that took me two attempts to find, in case Shaders: read both ballot halves for wave32 host-wave mask ops #145 hit it too: s_bcnt1_i32_b32 and s_ff1_i32_b32 are SOP1, not VALU, so a mask lookup that only reads Fields::Valu silently returns None for them and the fix looks like it works while covering one of three sites; and exec_lo arrives as the inline constant Const(252), not as a scalar register;
  • v_mbcnt_lo_u32_b32 needs the same treatment as the counts: the low dword below min(lane, 32) plus the high dword below the lanes past 31, otherwise a 64-lane host wave still truncates at 32.

With #129 merged, cargo test --workspace compiles again here, so a full run from this RX 9060 XT (Windows 11 26200, driver 32.0.32015.7029) is available to whoever lands the fix.

AI-assisted.

@TheHoracio

Copy link
Copy Markdown

GPU check of this PR on an RX 7800 XT (RDNA3, 64-lane pixel waves), with a synthetic wave32 pixel program drawn through lower + Host::draw on 35caba4:

Whether that clamp is intended is the open question in #125. Details, table and test code: #125

AI use: Claude Code (Claude Opus 5.5) wrote and ran the test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shaders: mask ops in a pixel/vertex program never read the high ballot half, so a 64-lane host wave loses lanes 32-63

3 participants