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

Open
github-bot wants to merge 1 commit from github-bot/gh-1570 into main AGit
Owner

@⁠ESAT586 on GitHub · 2026-11-24 19:16 UTC · original
ESAT586:shader/pixel-export-valid-mask → main · +706 −98 · 4 files · 1 commits · open

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).

**@⁠ESAT586** on GitHub · 2026-11-24 19:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/1570) `ESAT586:shader/pixel-export-valid-mask` → `main` · +706 −98 · 4 files · 1 commits · open > **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`).
Authored and verified by Gemini 4 Neon (Extra High Mode, AI coding agent)
with zero game data.

On RDNA2 hardware, pixel survival is governed by the `EXEC` mask of the
last `exp` instruction with `VM=1` (Valid Mask), not `DONE=1`:
- `io::Export` previously discarded the `vm` bit decoded by `gfx10::fields`
  and `emit::lower` eagerly emitted `dx.op.discard(!guard)` on `e.done`
  while refusing non-`done` conditional pixel exports.
- Decoupled multi-MRT/depth exports (e.g., `exp mrt0 ... vm` followed by
  `exp mrt1 ... done` with `vm=0`, or `exp mrt0 ...` followed by
  `exp null ... done vm` under partial `EXEC`, or two `vm=1` exports where
  the later `vm=1` runs under wider `EXEC`) either failed translation,
  failed to discard inactive lanes, or prematurely/falsely discarded
  surviving pixels.

Changes:
- `crates/victory-shader/src/io.rs`: Preserve `vm: bool` on `io::Export`.
- `crates/victory-shader/src/emit/lower.rs`:
  - Replace the single-pass `exported: Vec<bool>` tracking with a static
    fixpoint lattice (`compute_vm_lattice`) and per-block SSA `vm_live`
    state (`VmVal::{False, True, Dyn(V)}`) with PHIs at multi-predecessor
    joins (`vm_phis` populated in `fill_phis`).
  - Update `vm_live` on every pixel `exp` with `vm == true` (falling back
    to `done` only for synthetic pixel shaders with no `vm == true` export
    at all), overwriting any earlier `vm` mask, and emit `dx.op.discard`
    once at `LaneExit::End` (zero extra instructions for standard
    unconditional exports).
  - Guard non-empty pixel `storeOutput` writes with `begin_guarded(guard)`
    so conditional non-`done` MRT exports and per-MRT overwrites execute
    only for active `EXEC` lanes (non-pixel, non-mesh stages continue to
    reject conditional exports).
- `crates/victory-shader/src/emit/host.rs`: Add `#[cfg(test)] Host::draw_mrt`
  supporting multiple cleared RGBA32F render targets (with `Host::draw`
  delegating to `draw_mrt(..., 1)`).
- `crates/victory-shader/src/emit/mod.rs`: Add
  `pixel_export_valid_mask_and_multi_mrt_match_rdna2`, rendering 7
  `dxil.dll`-validated pixel shaders across 1- and 2-MRT 4x4 targets on
  both NVIDIA GeForce RTX 3060 Ti and WARP.

Fixes VIC-BT-011 (tracking issue #1242).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:36:45 +00:00
Author
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-24 19:31 UTC · original

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.

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 19:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/1570#issuecomment-7142231129) 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.*
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:31 UTC · original · edited 2026-11-25 16:07 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 01:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/1570#issuecomment-7149635712) · edited 2026-11-25 16:07 UTC <!-- 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)_
Author
Owner

@⁠ESAT586 on GitHub · 2026-11-25 11:15 UTC · original

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** on GitHub · 2026-11-25 11:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/1570#issuecomment-7160807355) 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.
Author
Owner

@⁠ESAT586 on GitHub · 2026-11-25 15:17 UTC · original

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.

**@⁠ESAT586** on GitHub · 2026-11-25 15:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/1570#issuecomment-7166608910) 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 has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/host.rs
  • crates/victory-shader/src/emit/lower.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1570/head:github-bot/gh-1570'
git switch 'github-bot/gh-1570'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1570'
git switch 'github-bot/gh-1570'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1570'
git switch 'github-bot/gh-1570'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1570'
git switch 'main'
git merge --squash 'github-bot/gh-1570'
git switch 'main'
git merge --ff-only 'github-bot/gh-1570'
git switch 'main'
git merge 'github-bot/gh-1570'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1570
No description provided.