Shaders: a discarded pixel stays a helper of its quad's derivatives (restores part of upstream 77053a0) #238

Open
murillojndem wants to merge 2 commits from upstream-pr/238 into main
murillojndem commented 2026-11-30 00:51:55 +00:00 (Migrated from github.com)

Imported from DexNom/VIctory. Originally posted by murillojndem on 2026-11-30T00:51:55Z.


A pixel shader on the console discards a lane by taking it out of a live mask. The lanes to discard are put in EXEC, s_andn2 live, live, exec removes them, and a branch on that SCC leads to an exit that exports null when no lane is left. The code then runs s_wqm exec, live, which turns whole quads back on: a discarded lane whose quad still has a live lane keeps executing as a helper, so its neighbours' derivatives and quad reads see what it computes, before the discard and after it.

The structurisation here sent the discarded lane down the exit branch, so it stopped executing, and the survivors' quad reads then read a lane that had left the program. On an RX 7900 XT those reads give 0: in the test below every live pixel in the row of a discarded one gets a wrong derivative (64 of 256 pixels, in wave32 and in wave64). WARP keeps the old values, so the test passes there on main.

This restores the discard part of upstream Marvisak/VIctory 77053a0 (Marvis), which 570a941 removed with the rest of that commit. If that removal was deliberate, this PR is not wanted. structure.rs and emit/mod.rs have changed since, so the port is adapted, and it takes fewer programs than upstream's, as listed below.

Change:

  • structure.rs: Lanes::new finds a pixel's discards (discard_at). That is an s_cbranch_scc0 to, or an s_cbranch_scc1 past, an exit that does only scalar work and a done export to null (kill_exit), on the SCC of s_andn2 X, X, exec at the program's wave width, in a program that turns whole quads on with an s_wqm (whole_quads). The console's code turns them back on after a discard with the branch's EXEC restored, ANDed with the live mask, and s_wqm exec, exec. The mask that s_andn2 writes keeps every lane, and the branch never goes to the exit. Structured::discards gives, per discard, the lanes that were in EXEC there. killing builds the per-lane form without this, as before, and finds_discards says whether a program has such a discard.
  • emit/lower.rs (host_discard): before such an s_andn2, a DXIL discard of those lanes, in a DXIL block of its own. The host keeps a discarded lane as a helper, so quad reads and derivatives still read it, and drops its outputs and stores. Masks the IR block joined or wrote before the discard are balloted in the block ahead of it (before_discard), so read as data later they still count the discarded lanes, as the console's registers do. A discard whose lanes depend on "some lane of the quad" (an s_wqm of another mask) is refused, since boolean takes that as true and the discard would take live pixels out; so is one outside a pixel program or inside a whole-wave section.
  • emit/mod.rs: structured_and_traced gives the per-lane form and the resources, and takes the old form for a program with such a discard that cannot be structured or traced the new way. emit lowers a program whose discards fail the new way with the old form, threading included: the order is new form, new form threaded, old form, old form threaded. translate and the offline listing (apps/victory/src/translate.rs) both use structured_and_traced.

Quad reads in pixel shaders already use QuadReadLaneAt/QuadOp here (use_quad includes pixel shaders), and those see helper lanes, so upstream's helpers_read has no counterpart.

Tests, all in emit::helper_lane_tests:

  • a_discarded_pixel_stays_a_helper_of_its_quads_derivatives: a pixel program, in wave32 and in wave64, discards lane 3 of every quad with the sequence above, turns whole quads back on by s_wqm exec, live or, as the console's code does, by s_and exec, exec, live and s_wqm exec, exec, then exports ddx_fine (by DPP quad_perm) of a value computed before the discard and of one computed after it. It is lowered without the fallback. On main, on the RX 7900 XT, 64 of 256 pixels are wrong in both wave sizes: pixel (0, 1) gets ddx -9 instead of 3, its discarded neighbour read as 0. WARP passes on main. With this change it passes on both. A mutant without the host discard fails it: the 64 discarded pixels are written. With the first commit's check (an s_wqm reading the live mask's register) the console's shape still got 64 of 256 wrong on the card; the second commit takes it.
  • a_mask_written_before_a_discard_counts_the_lanes_it_discards: s_bcnt1 of the compare's VCC after the discard. On the RX 7900 XT every live pixel counts 8 (one discarded lane per quad, 8 quads in a wave of 32). A mutant that ballots after the host's discard counts 0 in all 192. On WARP the counts are only printed: WARP clips the triangle at the target into two, and the helpers along their shared edge take no part in a host ballot.
  • a_pixel_discard_keeps_its_lanes_live_until_the_host_discards_them: structure finds the discard, with the compare's lanes, and never takes the exit; killing takes it. No discard is found in a program without any s_wqm, or with vector code in the exit.
  • a_pixel_discard_branching_past_its_exit_always_branches: the same with s_cbranch_scc1 past the exit.
  • a_discard_of_lanes_found_by_their_quad_is_taken_as_the_console_kills_them: the lowering refuses such a discard and the program is translated the old way; on WARP and the RX 7900 XT the pixels of odd y are discarded and the others export their position.
  • victory-shader --lib on the RX 7900 XT and WARP with one test thread: 242 passed, 2 failed, 4 ignored (main: 237, 2, 4). The 2 failures (emit::rect::tests::rectangles_are_drawn_whole and the_rectangle_stage_is_a_geometry_shader) fail on main too.
  • Clippy with -D warnings, with and without --all-features (and for victory), and cargo xtask policy pass. rustfmt reports the same 269 differences as on main. It merges with my open PRs.

Limits:

  • Only this discard sequence is recognized. A discard written another way (for example s_andn2 live, live, vcc) keeps the old behaviour.
  • After a discard, in whole-quad mode, the console's wave operations count the discarded lanes, which are in EXEC there. Here they are host helpers, which D3D leaves out of wave operations unless a shader asks for them (SM 6.7). The masks the discard's block joined or wrote before it are the exception above.
  • A discard refused for its quad lanes goes the old way, which takes "some lane of the quad" as true too (the shortcut in boolean): that test's image would be the same either way, so only the refusal is checked there.
  • The fallback says nothing: a program translated the old way still reads 0 from discarded neighbours on AMD.
  • A waterfall loop after a discard (v_readfirstlane_b32): the discarded lane, a host helper, is never the host's first lane, so it never gets its turn, and a neighbour's quad read after the loop reads 0, as on main. A probe of that shape matches the console on WARP and the RX 7900 XT with #224 (a helper lane's v_readfirstlane_b32 reads its own value in a pixel loop) merged, and gets 64 of 192 pixels wrong without it.
  • Discarded lanes now run the rest of the program as host helpers, where before they left it at the discard.
  • Implicit derivatives of image samples after a discard are not tested; they read the same helper lanes.

Generated with Claude Code

<!-- greyfinch-sync:DexNom/VIctory:issue:5557038122 --> Imported from [DexNom/VIctory](https://github.com/DexNom/VIctory/pull/238). Originally posted by [murillojndem](https://github.com/murillojndem) on 2026-11-30T00:51:55Z. --- A pixel shader on the console discards a lane by taking it out of a live mask. The lanes to discard are put in EXEC, `s_andn2 live, live, exec` removes them, and a branch on that SCC leads to an exit that exports `null` when no lane is left. The code then runs `s_wqm exec, live`, which turns whole quads back on: a discarded lane whose quad still has a live lane keeps executing as a helper, so its neighbours' derivatives and quad reads see what it computes, before the discard and after it. The structurisation here sent the discarded lane down the exit branch, so it stopped executing, and the survivors' quad reads then read a lane that had left the program. On an RX 7900 XT those reads give 0: in the test below every live pixel in the row of a discarded one gets a wrong derivative (64 of 256 pixels, in wave32 and in wave64). WARP keeps the old values, so the test passes there on main. This restores the discard part of upstream Marvisak/VIctory 77053a0 (Marvis), which 570a941 removed with the rest of that commit. If that removal was deliberate, this PR is not wanted. `structure.rs` and `emit/mod.rs` have changed since, so the port is adapted, and it takes fewer programs than upstream's, as listed below. Change: - `structure.rs`: `Lanes::new` finds a pixel's discards (`discard_at`). That is an `s_cbranch_scc0` to, or an `s_cbranch_scc1` past, an exit that does only scalar work and a `done` export to `null` (`kill_exit`), on the SCC of `s_andn2 X, X, exec` at the program's wave width, in a program that turns whole quads on with an `s_wqm` (`whole_quads`). The console's code turns them back on after a discard with the branch's EXEC restored, ANDed with the live mask, and `s_wqm exec, exec`. The mask that `s_andn2` writes keeps every lane, and the branch never goes to the exit. `Structured::discards` gives, per discard, the lanes that were in EXEC there. `killing` builds the per-lane form without this, as before, and `finds_discards` says whether a program has such a discard. - `emit/lower.rs` (`host_discard`): before such an `s_andn2`, a DXIL `discard` of those lanes, in a DXIL block of its own. The host keeps a discarded lane as a helper, so quad reads and derivatives still read it, and drops its outputs and stores. Masks the IR block joined or wrote before the discard are balloted in the block ahead of it (`before_discard`), so read as data later they still count the discarded lanes, as the console's registers do. A discard whose lanes depend on "some lane of the quad" (an `s_wqm` of another mask) is refused, since `boolean` takes that as true and the discard would take live pixels out; so is one outside a pixel program or inside a whole-wave section. - `emit/mod.rs`: `structured_and_traced` gives the per-lane form and the resources, and takes the old form for a program with such a discard that cannot be structured or traced the new way. `emit` lowers a program whose discards fail the new way with the old form, threading included: the order is new form, new form threaded, old form, old form threaded. `translate` and the offline listing (`apps/victory/src/translate.rs`) both use `structured_and_traced`. Quad reads in pixel shaders already use `QuadReadLaneAt`/`QuadOp` here (`use_quad` includes pixel shaders), and those see helper lanes, so upstream's `helpers_read` has no counterpart. Tests, all in `emit::helper_lane_tests`: - `a_discarded_pixel_stays_a_helper_of_its_quads_derivatives`: a pixel program, in wave32 and in wave64, discards lane 3 of every quad with the sequence above, turns whole quads back on by `s_wqm exec, live` or, as the console's code does, by `s_and exec, exec, live` and `s_wqm exec, exec`, then exports `ddx_fine` (by DPP `quad_perm`) of a value computed before the discard and of one computed after it. It is lowered without the fallback. On main, on the RX 7900 XT, 64 of 256 pixels are wrong in both wave sizes: pixel (0, 1) gets ddx -9 instead of 3, its discarded neighbour read as 0. WARP passes on main. With this change it passes on both. A mutant without the host `discard` fails it: the 64 discarded pixels are written. With the first commit's check (an `s_wqm` reading the live mask's register) the console's shape still got 64 of 256 wrong on the card; the second commit takes it. - `a_mask_written_before_a_discard_counts_the_lanes_it_discards`: `s_bcnt1` of the compare's VCC after the discard. On the RX 7900 XT every live pixel counts 8 (one discarded lane per quad, 8 quads in a wave of 32). A mutant that ballots after the host's discard counts 0 in all 192. On WARP the counts are only printed: WARP clips the triangle at the target into two, and the helpers along their shared edge take no part in a host ballot. - `a_pixel_discard_keeps_its_lanes_live_until_the_host_discards_them`: `structure` finds the discard, with the compare's lanes, and never takes the exit; `killing` takes it. No discard is found in a program without any `s_wqm`, or with vector code in the exit. - `a_pixel_discard_branching_past_its_exit_always_branches`: the same with `s_cbranch_scc1` past the exit. - `a_discard_of_lanes_found_by_their_quad_is_taken_as_the_console_kills_them`: the lowering refuses such a discard and the program is translated the old way; on WARP and the RX 7900 XT the pixels of odd y are discarded and the others export their position. - `victory-shader --lib` on the RX 7900 XT and WARP with one test thread: 242 passed, 2 failed, 4 ignored (main: 237, 2, 4). The 2 failures (`emit::rect::tests::rectangles_are_drawn_whole` and `the_rectangle_stage_is_a_geometry_shader`) fail on main too. - Clippy with `-D warnings`, with and without `--all-features` (and for `victory`), and `cargo xtask policy` pass. rustfmt reports the same 269 differences as on main. It merges with my open PRs. Limits: - Only this discard sequence is recognized. A discard written another way (for example `s_andn2 live, live, vcc`) keeps the old behaviour. - After a discard, in whole-quad mode, the console's wave operations count the discarded lanes, which are in EXEC there. Here they are host helpers, which D3D leaves out of wave operations unless a shader asks for them (SM 6.7). The masks the discard's block joined or wrote before it are the exception above. - A discard refused for its quad lanes goes the old way, which takes "some lane of the quad" as true too (the shortcut in `boolean`): that test's image would be the same either way, so only the refusal is checked there. - The fallback says nothing: a program translated the old way still reads 0 from discarded neighbours on AMD. - A waterfall loop after a discard (`v_readfirstlane_b32`): the discarded lane, a host helper, is never the host's first lane, so it never gets its turn, and a neighbour's quad read after the loop reads 0, as on main. A probe of that shape matches the console on WARP and the RX 7900 XT with #224 (a helper lane's `v_readfirstlane_b32` reads its own value in a pixel loop) merged, and gets 64 of 192 pixels wrong without it. - Discarded lanes now run the rest of the program as host helpers, where before they left it at the discard. - Implicit derivatives of image samples after a discard are not tested; they read the same helper lanes. Generated with [Claude Code](https://claude.com/claude-code)
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/238:upstream-pr/238'
git switch 'upstream-pr/238'

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/238'
git switch 'upstream-pr/238'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/238'
git switch 'upstream-pr/238'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/238'
git switch 'main'
git merge --squash 'upstream-pr/238'
git switch 'main'
git merge --ff-only 'upstream-pr/238'
git switch 'main'
git merge 'upstream-pr/238'
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
DexNom/VIctory!238
No description provided.