Skip to content

Shaders: wave32 lane masks on 64-lane host waves take their half's dword - #3556

Open
kilolimaVI wants to merge 5 commits into
Marvisak:mainfrom
kilolimaVI:shader/wave32-masks-64-lane-host
Open

kilolimaVI wants to merge 5 commits into
Marvisak:mainfrom
kilolimaVI:shader/wave32-masks-64-lane-host

Conversation

@kilolimaVI

Copy link
Copy Markdown

On hosts that run pixel waves of 64 lanes (AMD: RDNA pixel shaders run wave64; measured on a Radeon 780M, RDNA3), a wave32 pixel or vertex program's 32-bit lane masks were the ballot's low dword in every lane. Lanes 32-63 tested the bits of lanes 0-31, so the console compiler's loop that serves one value per pass (s_ff1 of EXEC, v_readlane at that lane, v_cmpx_eq) never served them: those pixels kept whatever they held before the loop.

NVIDIA (32-lane pixel waves) and WARP never show it, which is likely why it went unnoticed.

Fix (emit/lower.rs)

With MaskData::Host and a wave32 program, a 64-lane host wave is treated as two console waves:

  • mask_dword: each lane takes its own half's dword of the ballot (Lower::host_half, waveGetLaneIndex >= 32).
  • v_readlane at a computed (host) lane adds the half's base (Lower::host_lane).
  • v_mbcnt counts within the half (lane & 31).

On a 32-lane host the upper half never exists, so nothing changes there; wave64 programs and compute (MaskData::Console) are untouched (wave64 DXIL is byte-identical).

Test (emit/lane_mask_tests.rs)

wave32_lane_mask_loops_serve_every_lane_of_a_64_lane_host_wave: a hand-assembled pixel program runs that loop over a per-pixel key, drawn over a 16x16 target (no helper lanes), wave32 and wave64, on the hardware adapter and WARP. Pixels served a wrong key:

before after
wave32, Radeon 780M 88 / 256 0
wave64, Radeon 780M 0 0
wave32 / wave64, WARP 0 / 0 0 / 0

0/5 failures over 5 runs. cargo test -p victory-shader on the 780M: 303 passed, 0 failed. cargo clippy -p victory-shader --all-targets [--all-features] -- -D warnings clean. cargo fmt --check reports no new sites (main already has 33 in lower.rs and 2 in mod.rs).

Related: helper lanes at triangle edges are a separate issue (#2373); this test draws a full-coverage triangle so it measures only the mask width.

AI-assisted (Claude).

On a host that runs pixel waves of 64 lanes (AMD; a Radeon 780M here), a
wave32 pixel or vertex program's 32-bit lane masks were the ballot's low
dword in every lane: lanes 32-63 tested the bits of lanes 0-31, so a loop
that serves one value per pass (s_ff1 of EXEC, v_readlane, v_cmpx) never
served them. Each 32-lane half now takes its own dword, as two console
waves would, and lane numbers from masks (v_readlane at a computed lane,
v_mbcnt) count within the half. Hosts running 32 lanes (NVIDIA, WARP) are
unchanged; wave64 programs emit the same DXIL.

Test: lane_mask_tests draws such a loop over a 16x16 target in wave32 and
wave64. Before, the 780M served 88 of 256 pixels a wrong key in wave32
(0 in wave64, 0 on WARP); now 0 everywhere.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I checked the merge order for the Wave32 mask fix alongside #3421. The exact heads of #3556 (48f1646) and #3421 (c2698c3) merge cleanly, but the resulting lower.rs is 524,618 bytes, 330 bytes over the 512 KiB asset-policy limit.

Adding the barycentric lowering split from #3335 (e495d2d) also merges cleanly. On the combined commit, lower.rs is 508,704 bytes and the asset policy passes for all 194 files. Both merge commits preserve the original parent commits.

  • The 9 wave lowering tests and 9 barycentric tests pass.
  • App-local D3D12 and WARP ran the pixel comparison. Both guest wave widths returned the expected keys for all 256 pixels. The WARP DPP row-mask and quad-swizzle plus first-lane readbacks passed as well.
  • Clippy passes for all shader targets with both default and all features.

These are local software-rendering results. The Radeon 780M measurement above is the hardware evidence for #3556. Its Windows workflow is still awaiting approval.

The next step is for the maintainers to choose a merge order that puts the split in place before both wave changes are in main. If you reuse the integration work, please retain Git authorship, or add Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com> if folding it into another commit.

@ColorMeBoom

Copy link
Copy Markdown

RDNA1 run, and an overlap with #125 / #145. RX 5700 XT (RDNA1), driver 32.0.21045.29003, Windows 11 26200, no game files. All trees are merged onto main 63bcca4.

wave32_lane_mask_loops_serve_every_lane_of_a_64_lane_host_wave (your test):

tree wave32, RX 5700 XT wave64, RX 5700 XT WARP (both)
main + this test only 88 / 256 pixels wrong, first (3, 4): 0 instead of 30 0 0
this PR (48f1646) 0 0 0
#145 rebased on main (ColorMeBoom:pr145-rebase, dec8a5b) + this test 0 0 0

So RDNA1 shows the same 88/256 as your 780M, and this PR fixes it. Full victory-shader --lib --features host on this PR: 303 passed, 0 failed.

Overlap: this is the bug #125 tracks: a wave32 pixel/vertex program's lane masks only use the low ballot dword on a 64-lane host wave, which I confirmed on this card earlier today. #145 (TheHoracio's) fixes it a different way: a 64-bit host ballot for mask ops (emit/lower/host_masks.rs). Its head conflicts with main. The rebase above merges cleanly, and its own pixel_lane_masks_count_every_lane_of_the_host_wave fails on main here ("the host wave is wider than 32 lanes and its high half is lost") and passes with it. The two PRs conflict in lower.rs, and as the table shows, both pass your test on this card. So they look like two fixes for one bug, and it's worth settling which one lands.

Not run: #145's test on this PR. Windows Application Control on this machine blocked that test binary on every rebuild.

AI use: Claude Code (Claude Opus 5.5).

host_half and host_lane move to lower/lanes.rs, as lower/bvh.rs, so lower.rs
stays under the asset policy's 512 KiB with Marvisak#3421 merged beside this.
No change in behaviour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kilolimaVI

Copy link
Copy Markdown
Author

Thanks for checking the merge order. Rather than depend on #3335 landing first, I moved the two helpers (host_half, host_lane) into lower/lanes.rs, as lower/bvh.rs already is. This PR now adds about 0.8 KB to lower.rs instead of 2.2 KB, so with #3421 beside it the file is about 523.3 KB, under the 512 KiB limit in either merge order. No change in behaviour: the hardware test still gives 0 of 256 on the 780M, and clippy is clean.

AI-assisted (Claude).

@kilolimaVI

Copy link
Copy Markdown
Author

Thanks for the RDNA1 run and for spotting #125 / #145. That overlap is my miss: my check of open PRs didn't go back that far, and #145 was first.

The two fixes differ in shape. #145 widens a wave32 program's masks to the host's 64-bit ballot (lane numbers, counts and compares over the whole host wave). This PR treats a 64-lane host wave as two console waves: each lane reads its own half's dword, and lane numbers from masks count within the half (mask_dword, v_readlane at a computed lane, v_mbcnt; about 60 lines plus the test), so every 32-bit mask operation keeps its console meaning without tracking a second dword through the program. Either way your table shows the 88/256 gone on RDNA1 and RDNA3.

I'm happy for the maintainers to take whichever they prefer. If it's #145, I'll close this one, and the hardware test (emit/lane_mask_tests.rs) can go there with it.

AI-assisted (Claude).

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I retested the split on the exact new #3556 head dcc8ca4 with #3421 c2698c3. Git reports one conflict where both branches add a module declaration. Keeping mod lanes; and #[cfg(test)] mod wave_tests; resolves it. The rest merged automatically.

The combined commit keeps both exact parents. lower.rs is 523,263 bytes, 1,025 bytes below the 512 KiB limit. The asset policy passes for 194 files. This combination passes with the split in #3556 itself.

App-local WARP gave the expected keys for all 256 pixels at each guest wave width, and the DPP row-mask and quad-swizzle/first-lane readbacks passed. All nine wave lowering tests and both strict shader Clippy configurations passed. The author reported the same pixel result on the Radeon 780M after the split. The earlier RX 5700 XT result is for 48f1646. The new Windows workflow awaits approval.

The merge-order question is resolved for these two changes. The maintainers can now choose between the approaches in #3556 and #145. If reusing my merge resolution, please retain Git authorship, or add Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com> if folding it into another commit.

@ColorMeBoom

Copy link
Copy Markdown

RX 5700 XT at the new head dcc8ca4 (merged onto main 63bcca4), and the cross-check that was blocked last time.

  • wave32_lane_mask_loops_serve_every_lane_of_a_64_lane_host_wave: 0 of 256 pixels wrong, wave32 and wave64, hardware and WARP. Full victory-shader --lib --features host: 303 passed, 0 failed.
  • Shaders: read both ballot halves for wave32 host-wave mask ops #145's test on this PR (pixel_lane_masks_count_every_lane_of_the_host_wave, taken from the #145 rebase): passes. What it prints shows how the two fixes differ, on the same card:
tree probe line test
main up to 32 active lanes, lane indices up to 32 fails: "the host wave is wider than 32 lanes and its high half is lost"
#145 rebase up to 64 active lanes, lane indices up to 63 passes
this PR up to 32 active lanes, lane indices up to 31 passes

So the two agree on the bug, but disagree on what a wave32 program sees on a 64-lane host wave:

A wave32 program on the console never sees a lane number above 31, so where its code depends on that (a lane number used as an index or a shift, a count compared with 32), this PR's form keeps the console's values. That could help the maintainers choose between the two.

AI use: Claude Code (Claude Opus 5.5).

…64-lane host wave

A wave32 pixel or vertex program on a host that runs 64 lanes per wave
has two console waves in each host wave (MaskData::Host). Lane masks are
already taken per half; v_readfirstlane still went to waveReadLaneFirst
over the whole host wave, so lanes 32-63 read the value of the first
active lane in 0-31, another console wave's. A value the console keeps
uniform per wave (a flat attribute, a scalarised index) then came from
the wrong primitive in the upper half.

With masks split by halves, v_readfirstlane now reads the first active
lane of this lane's half: FirstbitLo of the half's ballot dword, offset
by 32 in the upper half, through waveReadLaneAt. Elsewhere it is
waveReadLaneFirst as before. The helper lives in lower/lanes.rs.

lane_mask_tests: the draw loop is shared, and a new test checks that the
pixels reading one value are at most a console wave and include the
pixel whose value it is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kilolimaVI

Copy link
Copy Markdown
Author

Added one commit (2445a0c): the same split for v_readfirstlane_b32.

A wave32 pixel program on a 64-lane host wave has two console waves in it. Lane masks were already taken per half; v_readfirstlane still went to waveReadLaneFirst over the whole host wave, so lanes 32-63 read the first value of lanes 0-31, another console wave's. A value the console keeps uniform per wave (a flat attribute, a scalarised index) then comes from the wrong primitive in the upper half.

With masks split, it now reads the first active lane of its own half (FirstbitLo of the half's ballot dword, +32 in the upper half, waveReadLaneAt). Elsewhere it is still waveReadLaneFirst. The helper is in lower/lanes.rs; lower.rs stays at 521,136 bytes.

New test wave32_first_lane_reads_stay_in_the_console_wave_on_a_64_lane_host_wave: every pixel reads the first lane's unique key; the pixels reading one value must be at most a console wave and include the pixel whose key it is. Radeon 780M (RDNA3) + WARP:

code wave32, 780M WARP
main 63bcca4 4 values, groups of 64 72 values, groups of 4
this PR before (dcc8ca4) 4 values, groups of 64 72 values, groups of 4
this PR now (2445a0c) 8 values, groups of 32 72 values, groups of 4

WARP runs 4-lane waves, so only a 64-lane hardware wave shows it. The lane-mask loop test is unchanged (88/256 wrong on main, 0 here). 5 of 5 runs, full suite 553 passed, clippy clean.

AI-assisted (Claude).

…64-lane host wave

A whole-wave section (the compiler's expansion of a wave sum, minimum,
bit operation or prefix sum) lowers to DXIL's waveActiveOp, waveActiveBit
or wavePrefixOp over the host's active lanes. For a wave32 pixel or
vertex program on a host running 64 lanes per wave (MaskData::Host),
that reduced both console waves of the host wave together: each lane
got the sum over 64 lanes, not over its own wave's 32.

With masks split by halves, the operation now runs twice, each half's
lanes giving the identity in the other run, and each lane takes its own
half's result. A prefix sum works the same way: the other half adds the
identity. Elsewhere one operation as before. The DXIL emission moves to
lower/lanes.rs (wave_reduce), so lower.rs shrinks to 519,957 bytes.

lane_mask_tests: a wave32 wave sum of (2^20 + key): the lane count read
is at most a console wave, and the pixels reading one (count, sum) have
keys adding up to it. draws takes the wave sizes to draw.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kilolimaVI

Copy link
Copy Markdown
Author

Added a third commit (4f4e98d): whole-wave reductions split by halves too.

A whole-wave section (the compiler's expansion of a wave sum, min/max, bit operation or prefix sum) lowers to waveActiveOp / waveActiveBit / wavePrefixOp over the host's active lanes. For a wave32 program on a 64-lane host wave that reduced both console waves together.

With masks split, the operation now runs twice, each half's lanes giving the identity in the other run, and each lane takes its own half's result (a prefix the same way). Elsewhere it is one operation as before. The DXIL emission moved to lower/lanes.rs (wave_reduce), so lower.rs shrinks to 519,957 bytes.

New test wave32_wave_sums_stay_in_the_console_wave_on_a_64_lane_host_wave: each pixel adds 2^20 + its key through the compiler's DPP / v_permlanex16 / v_readlane sequence, so the result carries the lane count and the key sum. Radeon 780M (RDNA3) + WARP:

code wave32, 780M WARP
this PR before (2445a0c) 4 sums, lane count 64, all 4 inconsistent 72 sums, lane count 4
this PR now (4f4e98d) 8 sums, lane count 32, 0 inconsistent 72 sums, lane count 4

The other two lane-mask tests are unchanged and pass. 5 of 5 runs, full suite 554 passed, clippy clean.

So the PR now covers the three ways a wave32 program read the other console wave in a 64-lane host wave: lane-mask loops, v_readfirstlane, and wave reductions.

AI-assisted (Claude).

@kilolimaVI

Copy link
Copy Markdown
Author

Merge note for whoever lands these: all five of my open PRs together (#3556 4f4e98d, #3571, #3583, #3613 4fb5462, #3749 152ebaa) on main 63bcca4, Radeon 780M (RDNA3) + WARP:

  • the lane-mask, helper-lane, quad-read, LOD and gather tests together: 12/12, 3 of 3 runs; the shader-cache test passes;
  • full suite (victory-shader + victory-gpu --features diagnostics): 562 passed, 0 failed; clippy clean;
  • lower.rs is 520,854 bytes combined (limit 524,288).

The only conflicts are module-order lines: the test modules in emit/mod.rs (#3583, #3613, #3749), and in emit/lower.rs this PR's mod lanes; against #3613's mod gather; right after mod bvh; (keep both, gather first).

AI-assisted (Claude).

A whole-wave section summing f16 halves (Shape::Half: the sum in the low
half, console lane `hi`'s high half on top) was refused outside compute
("a 16-bit wave reduction outside compute"), because the high half names
a console lane. With host lanes that lane is known too: lane `hi` of a
wave64 program, or of a wave32 program in its own half of a 64-lane host
wave (lane hi + 32 in the upper half). Mesh programs still refuse it.

The f16 sum goes through wave_reduce_halves, so each console wave sums
its own lanes there; console_lane gives the ballot dword and host lane
of a console lane. The general reductions use the same helper, and
lower.rs shrinks to 519,102 bytes.

lane_mask_tests: a wave32 pixel program sums 1.0 per lane in f16 with
each lane's key in the high half; the sum read is at most a console
wave's lane count and lane 31's key is a pixel among those reading it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kilolimaVI

Copy link
Copy Markdown
Author

Added a fourth commit (82813e4): 16-bit wave sums now translate in pixel and vertex programs.

A whole-wave section summing f16 halves (Shape::Half: the sum in the low half, console lane hi's high half on top) was refused outside compute ("a 16-bit wave reduction outside compute"), because the high half names a console lane. With host lanes that lane is known: lane hi of a wave64 program, or of a wave32 program within its own half of a 64-lane host wave. Mesh programs still refuse it. Where the host wave is narrower than the console's (WARP's 4 lanes) there is no lane hi, and the high half is the identity's, as in compute.

The f16 sum goes through the same per-half split as the other reductions (wave_reduce_halves), and console_lane gives a console lane's ballot dword and host lane. lower.rs shrinks to 519,102 bytes.

New test wave32_half_wave_sums_stay_in_the_console_wave_on_a_64_lane_host_wave, Radeon 780M (RDNA3) + WARP:

code result
this PR before (4f4e98d) refused at translation
this PR now (82813e4) 780M: 8 f16 sums, at most 32 lanes, 0 inconsistent; WARP: 0 inconsistent; 5 of 5 runs

Full suite 555 passed, clippy clean.

AI-assisted (Claude).

@ColorMeBoom

Copy link
Copy Markdown

@kilolimaVI RDNA1 row for 4f4e98d: RX 5700 XT (Navi 10, runs both wave sizes, driver 32.0.21045.29003), Windows 11 26200.

code wave32_wave_sums_stay_in_the_console_wave_on_a_64_lane_host_wave, hardware WARP
before (2445a0c + this test) fails: 4 sums, lane count 64, 4 inconsistent 72 sums, lane count 4, 0 inconsistent
after (4f4e98d), 3 of 3 runs passes: 8 sums, lane count 32, 0 inconsistent the same

The other two tests at 4f4e98d, 3 of 3 runs on hardware:

  • lane-mask loops: 0 of 256 pixels served a wrong key;
  • v_readfirstlane: 8 values read, largest group 32 pixels, 0 groups over 32, 0 not their own pixel's.

Full victory-shader --lib --features host: 305 passed, 0 failed, 3 ignored.

The same counts as the 780M, so the reduction fix covers RDNA1 as well as RDNA3.

AI use: Claude Code (Claude Opus 5.5).

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.

3 participants