Shaders: a discarded pixel stays a helper of its quad's derivatives (restores part of upstream 77053a0) #238
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/238"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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, execremoves them, and a branch on that SCC leads to an exit that exportsnullwhen no lane is left. The code then runss_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), which570a941removed with the rest of that commit. If that removal was deliberate, this PR is not wanted.structure.rsandemit/mod.rshave changed since, so the port is adapted, and it takes fewer programs than upstream's, as listed below.Change:
structure.rs:Lanes::newfinds a pixel's discards (discard_at). That is ans_cbranch_scc0to, or ans_cbranch_scc1past, an exit that does only scalar work and adoneexport tonull(kill_exit), on the SCC ofs_andn2 X, X, execat the program's wave width, in a program that turns whole quads on with ans_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, ands_wqm exec, exec. The mask thats_andn2writes keeps every lane, and the branch never goes to the exit.Structured::discardsgives, per discard, the lanes that were in EXEC there.killingbuilds the per-lane form without this, as before, andfinds_discardssays whether a program has such a discard.emit/lower.rs(host_discard): before such ans_andn2, a DXILdiscardof 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" (ans_wqmof another mask) is refused, sincebooleantakes 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_tracedgives 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.emitlowers 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.translateand the offline listing (apps/victory/src/translate.rs) both usestructured_and_traced.Quad reads in pixel shaders already use
QuadReadLaneAt/QuadOphere (use_quadincludes pixel shaders), and those see helper lanes, so upstream'shelpers_readhas 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 bys_wqm exec, liveor, as the console's code does, bys_and exec, exec, liveands_wqm exec, exec, then exportsddx_fine(by DPPquad_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 hostdiscardfails it: the 64 discarded pixels are written. With the first commit's check (ans_wqmreading 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_bcnt1of 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:structurefinds the discard, with the compare's lanes, and never takes the exit;killingtakes it. No discard is found in a program without anys_wqm, or with vector code in the exit.a_pixel_discard_branching_past_its_exit_always_branches: the same withs_cbranch_scc1past 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 --libon 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_wholeandthe_rectangle_stage_is_a_geometry_shader) fail on main too.-D warnings, with and without--all-features(and forvictory), andcargo xtask policypass. rustfmt reports the same 269 differences as on main. It merges with my open PRs.Limits:
s_andn2 live, live, vcc) keeps the old behaviour.boolean): that test's image would be the same either way, so only the refusal is checked there.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'sv_readfirstlane_b32reads its own value in a pixel loop) merged, and gets 64 of 192 pixels wrong without it.Generated with Claude Code
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.