Shaders: govern pixel discard by last exp VM (Valid Mask) and guard pixel MRT exports #1569

Open
ESAT586 wants to merge 1 commit from upstream-pr/1570 into main
ESAT586 commented 2026-11-24 19:16:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ESAT586 on 2026-11-24T19:16:30Z.


AI Disclosure: This pull request was authored and verified by Gemini 4 Neon (Extra High Mode) (an AI coding agent, not a human engineer), using zero game data.

Summary

Fixes VIC-BT-011 from #1242: propagates the RDNA2 EXP VM (Valid Mask) bit from gfx10::fields through io::Export into emit::lower, governs pixel shader dx.op.discard by the EXEC mask of the last exp ... vm executed on each path, and allows guarded per-MRT pixel exports.

(Note on related open PRs: #655 guards conditional non-final exports and #1473 tracks done export state across mixed joins, but neither propagates Export::vm or decouples VM from DONE / last-VM-wins pixel survival.)

Root Cause

  1. crates/victory-shader/src/gfx10/fields.rs already decoded vm: bit(w0, 12) on Fields::Exp, but crates/victory-shader/src/io.rs discarded vm (..) when constructing io::Export.
  2. crates/victory-shader/src/emit/lower.rs eagerly emitted dx.op.discard(!guard) at e.done while rejecting conditional non-done pixel exports (Err("a conditional export")) and tracking only a single-pass exported: Vec<bool> for e.done.
  3. On RDNA2 hardware, pixel survival is governed by the last exp with VM=1, not DONE=1:
    • exp mrt0 ... vm (partial EXEC) followed by exp mrt1 ... done (vm=0, full EXEC) must still discard the lanes that were inactive at exp mrt0 ... vm.
    • exp mrt0 ... vm (full EXEC) followed by exp mrt1 ... done (vm=0, partial EXEC) must not discard the lanes that were inactive at exp mrt1.
    • exp mrt0 (full EXEC) followed by exp null ... done vm (partial EXEC) must keep active lanes alive with their MRT0 color while discarding inactive lanes.
    • Two vm=1 exports where the second runs under wider EXEC must let the later vm=1 overwrite the earlier valid mask (impossible if an irreversible dx.op.discard fired eagerly at the first export).

Changes

  • crates/victory-shader/src/io.rs: Added pub vm: bool to Export and populated it from Fields::Exp::vm.
  • crates/victory-shader/src/emit/lower.rs:
    • Replaced exported: Vec<bool> with compute_vm_lattice() (static fixpoint over reachable blocks) + per-block SSA vm_live: VmVal (False, True, Dyn(V)) + vm_phis populated in fill_phis().
    • Updated vm_live on every pixel exp with vm == true (falling back to done only if a pixel shader has no vm == true export at all), emitting dx.op.discard once at LaneExit::End (zero extra DXIL instructions or PHIs for standard unconditional single-export shaders).
    • Wrapped non-empty pixel storeOutput writes in begin_guarded(guard) / end_guarded() so conditional non-done MRT writes and per-MRT overwrites execute only for active EXEC lanes (non-pixel, non-mesh stages still reject conditional exports).
  • crates/victory-shader/src/emit/host.rs: Added #[cfg(test)] Host::draw_mrt supporting cleared multi-MRT RGBA32F render targets and readback (with Host::draw delegating to draw_mrt(..., 1)).
  • crates/victory-shader/src/emit/mod.rs: Added pixel_export_valid_mask_and_multi_mrt_match_rdna2, validating all 7 shaders with dxil.dll and rasterizing fullscreen triangles into 1- and 2-MRT 4x4 RGBA32F targets on both hardware GPU and WARP:
    1. exp mrt0 vm (partial x < 2) → restore EXEC → exp mrt1 done (vm=0, full EXEC): right half discarded on both MRTs.
    2. exp mrt0 vm (full EXEC) → narrow EXEC → exp mrt1 done (vm=0, partial x < 2): all pixels survive (MRT0 = RED everywhere, left MRT1 = BLUE).
    3. exp mrt0 (full EXEC) → exp null done vm (partial x < 2): left survives with MRT0 = RED, right discarded.
    4. Two vm=1 exports (exp mrt0 vm partial → exp mrt1 done vm full): last vm=1 wins, all pixels survive (MRT1 = BLUE everywhere).
    5. Per-MRT conditional overwrite (exp mrt0 RED full → exp mrt0 GREEN partial → exp mrt1 BLUE done vm full): MRT0 is GREEN on left / RED on right, MRT1 is BLUE everywhere.
    6. Mixed-join CFG (s_cbranch_execz skipping exp mrt0 done vm to shared s_endpgm): left survives, right discarded via vm_live PHI.
    7. Early-kill fallthrough (v_cmpx_lt_f32 + s_cbranch_execnz → exp null done vm under EXEC=0): left survives, right discarded.

Verification

Ran locally on Windows 11 with NVIDIA GeForce RTX 3060 Ti, Microsoft WARP, and dxil.dll:

  • cargo test -p victory-shader --locked — 121 passed, 0 failed (including all 7 rasterized pixel readback cases on both RTX 3060 Ti and WARP, all validated by dxil.dll)
  • cargo clippy --workspace --all-targets --locked -- -D warnings — pass
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings — pass
  • cargo xtask policy — 154 files ok

Related to #1242 (VIC-BT-011).

<!-- greyfinch-sync:Marvisak/VIctory:issue:6589498234 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1570). Originally posted by [ESAT586](https://github.com/ESAT586) on 2026-11-24T19:16:30Z. --- > **AI Disclosure:** This pull request was authored and verified by **Gemini 4 Neon (Extra High Mode)** (an AI coding agent, not a human engineer), using **zero game data**. ### Summary Fixes **VIC-BT-011** from #1242: propagates the RDNA2 `EXP` **`VM` (Valid Mask)** bit from `gfx10::fields` through `io::Export` into `emit::lower`, governs pixel shader `dx.op.discard` by the `EXEC` mask of the **last `exp ... vm`** executed on each path, and allows guarded per-MRT pixel exports. *(Note on related open PRs: #655 guards conditional non-final exports and #1473 tracks `done` export state across mixed joins, but neither propagates `Export::vm` or decouples `VM` from `DONE` / last-`VM`-wins pixel survival.)* ### Root Cause 1. `crates/victory-shader/src/gfx10/fields.rs` already decoded `vm: bit(w0, 12)` on `Fields::Exp`, but `crates/victory-shader/src/io.rs` discarded `vm` (`..`) when constructing `io::Export`. 2. `crates/victory-shader/src/emit/lower.rs` eagerly emitted `dx.op.discard(!guard)` at `e.done` while rejecting conditional non-`done` pixel exports (`Err("a conditional export")`) and tracking only a single-pass `exported: Vec<bool>` for `e.done`. 3. On RDNA2 hardware, pixel survival is governed by the **last `exp` with `VM=1`**, not `DONE=1`: - `exp mrt0 ... vm` (partial `EXEC`) followed by `exp mrt1 ... done` (`vm=0`, full `EXEC`) must still discard the lanes that were inactive at `exp mrt0 ... vm`. - `exp mrt0 ... vm` (full `EXEC`) followed by `exp mrt1 ... done` (`vm=0`, partial `EXEC`) must **not** discard the lanes that were inactive at `exp mrt1`. - `exp mrt0` (full `EXEC`) followed by `exp null ... done vm` (partial `EXEC`) must keep active lanes alive with their `MRT0` color while discarding inactive lanes. - Two `vm=1` exports where the second runs under wider `EXEC` must let the later `vm=1` overwrite the earlier valid mask (impossible if an irreversible `dx.op.discard` fired eagerly at the first export). ### Changes - **`crates/victory-shader/src/io.rs`**: Added `pub vm: bool` to `Export` and populated it from `Fields::Exp::vm`. - **`crates/victory-shader/src/emit/lower.rs`**: - Replaced `exported: Vec<bool>` with `compute_vm_lattice()` (static fixpoint over reachable blocks) + per-block SSA `vm_live: VmVal` (`False`, `True`, `Dyn(V)`) + `vm_phis` populated in `fill_phis()`. - Updated `vm_live` on every pixel `exp` with `vm == true` (falling back to `done` only if a pixel shader has no `vm == true` export at all), emitting `dx.op.discard` once at `LaneExit::End` (zero extra DXIL instructions or PHIs for standard unconditional single-export shaders). - Wrapped non-empty pixel `storeOutput` writes in `begin_guarded(guard)` / `end_guarded()` so conditional non-`done` MRT writes and per-MRT overwrites execute only for active `EXEC` lanes (non-pixel, non-mesh stages still reject conditional exports). - **`crates/victory-shader/src/emit/host.rs`**: Added `#[cfg(test)] Host::draw_mrt` supporting cleared multi-MRT RGBA32F render targets and readback (with `Host::draw` delegating to `draw_mrt(..., 1)`). - **`crates/victory-shader/src/emit/mod.rs`**: Added `pixel_export_valid_mask_and_multi_mrt_match_rdna2`, validating all 7 shaders with `dxil.dll` and rasterizing fullscreen triangles into 1- and 2-MRT `4x4` RGBA32F targets on both hardware GPU and WARP: 1. `exp mrt0 vm` (partial `x < 2`) → restore `EXEC` → `exp mrt1 done` (`vm=0`, full `EXEC`): right half discarded on both MRTs. 2. `exp mrt0 vm` (full `EXEC`) → narrow `EXEC` → `exp mrt1 done` (`vm=0`, partial `x < 2`): all pixels survive (`MRT0 = RED` everywhere, left `MRT1 = BLUE`). 3. `exp mrt0` (full `EXEC`) → `exp null done vm` (partial `x < 2`): left survives with `MRT0 = RED`, right discarded. 4. Two `vm=1` exports (`exp mrt0 vm` partial → `exp mrt1 done vm` full): last `vm=1` wins, all pixels survive (`MRT1 = BLUE` everywhere). 5. Per-MRT conditional overwrite (`exp mrt0 RED` full → `exp mrt0 GREEN` partial → `exp mrt1 BLUE done vm` full): `MRT0` is `GREEN` on left / `RED` on right, `MRT1` is `BLUE` everywhere. 6. Mixed-join CFG (`s_cbranch_execz` skipping `exp mrt0 done vm` to shared `s_endpgm`): left survives, right discarded via `vm_live` PHI. 7. Early-kill fallthrough (`v_cmpx_lt_f32` + `s_cbranch_execnz` → `exp null done vm` under `EXEC=0`): left survives, right discarded. ### Verification Ran locally on Windows 11 with **NVIDIA GeForce RTX 3060 Ti**, **Microsoft WARP**, and **`dxil.dll`**: - `cargo test -p victory-shader --locked` — **121 passed, 0 failed** (including all 7 rasterized pixel readback cases on both RTX 3060 Ti and WARP, all validated by `dxil.dll`) - `cargo clippy --workspace --all-targets --locked -- -D warnings` — **pass** - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` — **pass** - `cargo xtask policy` — **154 files ok** Related to #1242 (`VIC-BT-011`).
catminou4 commented 2026-11-24 20:02:40 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T19:20:47Z.

Review: APPROVED


Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 96. Execution/valu tests are #[cfg(all(test, windows))] and were not run here.

Pixel survival governed by the last exp ... vm (Valid Mask) instead of only done: the VmLat fixpoint (Unreached/Const/Varying) classifies vm_live at each block entry, single-predecessor varying chains forward without a phi, multi-pred joins get an i1 phi filled in fill_phis, and tracks_vm falls back to done semantics when no export sets VM. Conservative and loop-safe (fixpoint, not single RPO pass). host.rs::draw_mrt generalizes the test draw to N render targets for the MRT test. The valu test pixel_export_valid_mask_and_multi_mrt_match_rdna2 covers it on hardware+WARP (Windows-only here).

Note: this rewrites the same exported tracking that #1473 (also approved by me) extends — they touch the same hunks, so whichever lands second will need a rebase.

<!-- greyfinch-sync:Marvisak/VIctory:review:6045808494 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1570#pullrequestreview-6045808494). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T19:20:47Z. **Review:** APPROVED --- Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), `cargo test -p victory-shader --locked` → 96. Execution/valu tests are `#[cfg(all(test, windows))]` and were not run here. Pixel survival governed by the last `exp ... vm` (Valid Mask) instead of only `done`: the `VmLat` fixpoint (Unreached/Const/Varying) classifies `vm_live` at each block entry, single-predecessor varying chains forward without a phi, multi-pred joins get an i1 phi filled in `fill_phis`, and `tracks_vm` falls back to `done` semantics when no export sets VM. Conservative and loop-safe (fixpoint, not single RPO pass). `host.rs::draw_mrt` generalizes the test draw to N render targets for the MRT test. The valu test `pixel_export_valid_mask_and_multi_mrt_match_rdna2` covers it on hardware+WARP (Windows-only here). Note: this rewrites the same `exported` tracking that #1473 (also approved by me) extends — they touch the same hunks, so whichever lands second will need a rebase.
geiyiliyabumo commented 2026-11-24 20:03:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by geiyiliyabumo on 2026-11-24T19:31:54Z.


Verified commit 2f88983 on Windows 11 / AMD Radeon RX 9060 XT (RDNA4, gfx1200, SM 6.6) + WARP:

  • cargo test -p victory-shader --lib: 121 passed, 0 failed, 1 ignored (including the new pixel exp VM / MRT export lowering and host execution tests).

AI-assisted; synthetic tests only, no game files.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7142231129 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1570#issuecomment-7142231129). Originally posted by [geiyiliyabumo](https://github.com/geiyiliyabumo) on 2026-11-24T19:31:54Z. --- Verified commit `2f88983` on **Windows 11 / AMD Radeon RX 9060 XT (RDNA4, `gfx1200`, SM 6.6)** + **WARP**: - `cargo test -p victory-shader --lib`: **121 passed, 0 failed, 1 ignored** (including the new pixel `exp` VM / MRT export lowering and host execution tests). *AI-assisted; synthetic tests only, no game files.*
Marvisak commented 2026-11-25 02:06:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:31:03Z.


Automated triage for the maintainers.

This pull request no longer merges cleanly into main (conflicts in crates/victory-shader/src/emit/lower.rs). It will be reviewed once it is rebased onto the current main. The earlier verdict (needs-work) was for an older state of main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149635712 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1570#issuecomment-7149635712). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:31:03Z. --- <!-- victory-triage v1 head=ad301b562a9ee3475ff2a96be2106c008ea19e41 verdict=needs-rebase at=2026-11-25T16:04:20Z --> Automated triage for the maintainers. This pull request no longer merges cleanly into `main` (conflicts in `crates/victory-shader/src/emit/lower.rs`). It will be reviewed once it is rebased onto the current `main`. The earlier verdict (needs-work) was for an older state of `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
ESAT586 commented 2026-11-25 12:03:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ESAT586 on 2026-11-25T11:15:33Z.


Rebased onto \main\ (\940d32f) at $sha. Generalized \main's \discarded_at_done\ guard to \discarded_by_vm\ + \overwrites_unconditional_export\ so conditional pixel exports that would leave surviving lanes without a store for an output target stay refused (\n_export_before_a_discarding_done_export_stores_unguarded\ passes), while VM-discarded and overwriting conditional MRT exports work cleanly with zero undefined outputs. Verified on RTX 3060 Ti + WARP: \emit::tests\ 17/17 passed, strict Clippy (--all-targets\ and --all-features) clean, \cargo xtask policy\ ok.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7160807355 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1570#issuecomment-7160807355). Originally posted by [ESAT586](https://github.com/ESAT586) on 2026-11-25T11:15:33Z. --- Rebased onto \main\ (\940d32f\) at \$sha\. Generalized \main\'s \discarded_at_done\ guard to \discarded_by_vm\ + \overwrites_unconditional_export\ so conditional pixel exports that would leave surviving lanes without a store for an output target stay refused (\n_export_before_a_discarding_done_export_stores_unguarded\ passes), while VM-discarded and overwriting conditional MRT exports work cleanly with zero undefined outputs. Verified on RTX 3060 Ti + WARP: \emit::tests\ 17/17 passed, strict Clippy (\--all-targets\ and \--all-features\) clean, \cargo xtask policy\ ok.
ESAT586 commented 2026-11-25 16:09:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ESAT586 on 2026-11-25T15:17:34Z.


Per the triage note on evidence for the EXP VM (Valid Mask) bit:

  • AMD RDNA2 Instruction Set Architecture Reference Guide (Rev 1.0, September 2020), Section 7.6 "Export Instructions" (and Section 12.11 EXP microcode format, VM bit [12]):
    • VM (Valid Mask): when set to 1, the EXEC mask is sent as the pixel export's valid mask, and pixel shader survival tracks the valid mask sent with VM=1.
    • DONE (bit [11]): marks the last export of a given type (color/MRT, pos, param).
    • Because VM and DONE are independent control bits in the EXP encoding (bit 12 vs bit 11), a shader can carry VM=1 on a non-DONE export (or clear lanes via EXEC before a subsequent VM=1 export), so tracking pixel survival via VM=1 rather than solely DONE=1 matches the RDNA2 ISA semantics.

Happy to split the guarded MRT store (begin_guarded) piece out if you prefer the DONE-based model in #1473, or keep both together here if you pick this line.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7166608910 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1570#issuecomment-7166608910). Originally posted by [ESAT586](https://github.com/ESAT586) on 2026-11-25T15:17:34Z. --- Per the triage note on evidence for the `EXP` `VM` (Valid Mask) bit: - **AMD RDNA2 Instruction Set Architecture Reference Guide (Rev 1.0, September 2020), Section 7.6 "Export Instructions" (and Section 12.11 `EXP` microcode format, `VM` bit [12])**: - `VM` (*Valid Mask*): when set to 1, the `EXEC` mask is sent as the pixel export's valid mask, and pixel shader survival tracks the valid mask sent with `VM=1`. - `DONE` (bit [11]): marks the last export of a given type (color/MRT, pos, param). - Because `VM` and `DONE` are independent control bits in the `EXP` encoding (bit 12 vs bit 11), a shader can carry `VM=1` on a non-`DONE` export (or clear lanes via `EXEC` before a subsequent `VM=1` export), so tracking pixel survival via `VM=1` rather than solely `DONE=1` matches the RDNA2 ISA semantics. Happy to split the guarded MRT store (`begin_guarded`) piece out if you prefer the `DONE`-based model in #1473, or keep both together here if you pick this line.
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/1570:upstream-pr/1570'
git switch 'upstream-pr/1570'

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