Shaders: fuse and flush sources in legacy f32 MAD and MAC (port of upstream VIctory #1554) #1026

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

@⁠Moon0755 on GitHub · 2026-11-25 16:37 UTC · original
Moon0755:fix/legacy-mad-mac-fused-ftz → main · +75 −8 · 2 files · 1 commits · open

Ports upstream Marvisak/VIctory#1554 (Calvix2k).

Problem

In crates/victory-shader/src/emit/lower.rs, v_mad_legacy_f32 and v_mac_legacy_f32 were lowered as unfused legacy_mul(a, b) + c without flushing subnormal operands.

On RDNA2 / Xbox Series X, the legacy multiply-add encoding decodes as fused (v_fma_legacy_f32), flushes subnormal f32 source operands to signed zero, and keeps the legacy zero-factor rule (0 * inf/NaN = +0, so +0 + c).

What changed

  • crates/victory-shader/src/emit/lower.rs:
    • Add Lower::ftz to flush f32 subnormals ((bits & 0x7F80_0000) == 0) to signed zero while preserving the sign bit and all normal/inf/NaN values.
    • Add Lower::legacy_fmad using self.fmad(a, b, c) with the legacy zero-factor override (+0 + c when a == 0 or b == 0).
    • Route v_mad_legacy_f32 and v_mac_legacy_f32 through ftz on all three sources followed by legacy_fmad (leaving non-legacy v_mad_f32 / v_mac_f32 unfused and v_fmac_f32 unchanged).
  • crates/victory-shader/src/emit/valu_tests.rs (legacy_mad_fuses_its_product_and_flushes_its_sources):
    • Add an end-to-end synthetic v_mad_legacy_f32 test covering single-rounding fusion (on hardware adapters), subnormal * 1.0 + 0.0 == 0.0, subnormal * +inf + 1.0 == 1.0 (proving source FTZ happens before the zero-factor check), and 0 * +inf + 1.0 == 1.0.

Verification

Verified on Windows (windows-latest + dxil.dll + D3D12 WARP) via a temporary scoped CI branch on the fork:

  • CI run: https://github.com/Moon0755/victory-fork/actions/runs/45655273501
  • cargo clippy -p victory-shader -p xtask --all-targets --locked -- -D warnings: passed
  • cargo clippy -p victory-shader -p xtask --all-targets --all-features --locked -- -D warnings: passed
  • cargo test -p victory-shader -p xtask --locked -- --nocapture: victory-shader 216 passed; 0 failed; 3 ignored (test emit::valu_tests::legacy_mad_fuses_its_product_and_flushes_its_sources ... ok, validated via dxil.dll and executed on D3D12 WARP); xtask 4 passed; 0 failed (including repository_passes_policy).

Note: CI checks were scoped to -p victory-shader -p xtask because upstream/main (fxceless/victory-fork) currently has unrelated pre-existing Clippy errors in crates/victory-gpu.

— Neon

**@⁠Moon0755** on GitHub · 2026-11-25 16:37 UTC · [original](https://github.com/fxceless/victory-fork/pull/1026) `Moon0755:fix/legacy-mad-mac-fused-ftz` → `main` · +75 −8 · 2 files · 1 commits · open Ports upstream [Marvisak/VIctory#1554](https://github.com/Marvisak/VIctory/pull/1554) (`Calvix2k`). ## Problem In `crates/victory-shader/src/emit/lower.rs`, `v_mad_legacy_f32` and `v_mac_legacy_f32` were lowered as unfused `legacy_mul(a, b) + c` without flushing subnormal operands. On RDNA2 / Xbox Series X, the legacy multiply-add encoding decodes as fused (`v_fma_legacy_f32`), flushes subnormal f32 source operands to signed zero, and keeps the legacy zero-factor rule (`0 * inf/NaN = +0`, so `+0 + c`). ## What changed - **`crates/victory-shader/src/emit/lower.rs`**: - Add `Lower::ftz` to flush f32 subnormals (`(bits & 0x7F80_0000) == 0`) to signed zero while preserving the sign bit and all normal/inf/NaN values. - Add `Lower::legacy_fmad` using `self.fmad(a, b, c)` with the legacy zero-factor override (`+0 + c` when `a == 0` or `b == 0`). - Route `v_mad_legacy_f32` and `v_mac_legacy_f32` through `ftz` on all three sources followed by `legacy_fmad` (leaving non-legacy `v_mad_f32` / `v_mac_f32` unfused and `v_fmac_f32` unchanged). - **`crates/victory-shader/src/emit/valu_tests.rs` (`legacy_mad_fuses_its_product_and_flushes_its_sources`)**: - Add an end-to-end synthetic `v_mad_legacy_f32` test covering single-rounding fusion (on hardware adapters), `subnormal * 1.0 + 0.0 == 0.0`, `subnormal * +inf + 1.0 == 1.0` (proving source FTZ happens before the zero-factor check), and `0 * +inf + 1.0 == 1.0`. ## Verification Verified on Windows (`windows-latest` + `dxil.dll` + D3D12 WARP) via a temporary scoped CI branch on the fork: - CI run: https://github.com/Moon0755/victory-fork/actions/runs/45655273501 - `cargo clippy -p victory-shader -p xtask --all-targets --locked -- -D warnings`: passed - `cargo clippy -p victory-shader -p xtask --all-targets --all-features --locked -- -D warnings`: passed - `cargo test -p victory-shader -p xtask --locked -- --nocapture`: `victory-shader` `216 passed; 0 failed; 3 ignored` (`test emit::valu_tests::legacy_mad_fuses_its_product_and_flushes_its_sources ... ok`, validated via `dxil.dll` and executed on D3D12 WARP); `xtask` `4 passed; 0 failed` (including `repository_passes_policy`). Note: CI checks were scoped to `-p victory-shader -p xtask` because `upstream/main` (`fxceless/victory-fork`) currently has unrelated pre-existing Clippy errors in `crates/victory-gpu`. — Neon
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:56:12 +00:00
Author
Owner

@⁠HAL9000RELOADED on GitHub · 2026-11-25 18:00 UTC · original

A hardware run of this PR's new test: legacy_mad_fuses_its_product_and_flushes_its_sources passes on a Radeon RX 7900 XTX (RDNA3), Windows 10 22H2, with #402's Agility SDK runtime. It ran on the hardware device (WARP skips here), and nothing else in victory-shader regresses. The setup, and the same run for eight related ports, are in one table on #1030: #1030

**@⁠HAL9000RELOADED** on GitHub · 2026-11-25 18:00 UTC · [original](https://github.com/fxceless/victory-fork/pull/1026#issuecomment-7170318703) A hardware run of this PR's new test: `legacy_mad_fuses_its_product_and_flushes_its_sources` passes on a Radeon RX 7900 XTX (RDNA3), Windows 10 22H2, with #402's Agility SDK runtime. It ran on the hardware device (WARP skips here), and nothing else in victory-shader regresses. The setup, and the same run for eight related ports, are in one table on #1030: https://git.gtahub.st/github/fxceless--victory-fork/pulls/1030
Author
Owner

@⁠SkillIssueXO on GitHub · 2026-11-25 23:56 UTC · original

I ran legacy_mad_fuses_its_product_and_flushes_its_sources from 66e9d884 on an NVIDIA RTX 5070 Ti (driver 621.40, Windows 11 build 26300). The hardware path passed all four cases, including the single-rounding result 0xa8800000 and the denormal and zero-factor cases. WARP passed the three applicable cases and skipped fused rounding as expected.

This was the PR applied to the 6d20eed6 renderer with local capture and input changes. An in-game comparison is pending. AI-assisted testing with synthetic inputs.

**@⁠SkillIssueXO** on GitHub · 2026-11-25 23:56 UTC · [original](https://github.com/fxceless/victory-fork/pull/1026#issuecomment-7178229102) I ran `legacy_mad_fuses_its_product_and_flushes_its_sources` from `66e9d884` on an NVIDIA RTX 5070 Ti (driver 621.40, Windows 11 build 26300). The hardware path passed all four cases, including the single-rounding result `0xa8800000` and the denormal and zero-factor cases. WARP passed the three applicable cases and skipped fused rounding as expected. This was the PR applied to the `6d20eed6` renderer with local capture and input changes. An in-game comparison is pending. AI-assisted testing with synthetic inputs.
Author
Owner

@⁠SkillIssueXO on GitHub · 2026-11-26 05:39 UTC · original · edited 2026-11-26 06:19 UTC

In-game follow-up on RTX 5070 Ti (621.40). I ported the PR’s lowering to Neptune and retranslated only the final composite pixel shader. Both runs used the same runtime executable, Series X checkpoint, 4K output, and water compute shader. A hash comparison of all 2,922 warm-cache files found exactly one changed entry. The revised DXIL validated, pipeline creation succeeded, and the shader’s bindings were identical.

At the held boat view, the stock composite showed black cores in the brightest water reflections. The revised composite showed bright cores in two captures over a minute apart. The sun disk was outside the frame, so this verifies the water reflection symptom in this scene.

Over about 60 seconds each, stock and revised logged 6.47 and 6.38 DXGI-accepted presents/s respectively. Median intervals were 165.9 and 157.9 ms. This was one stationary A/B pair. AI-assisted testing and analysis.

In a later Neptune replay with the revised composite, tilting the camera up showed a bright sun center in two captures. A readback immediately before a revised composite draw captured all three 33³ RGBA16F color lookup volumes. Every sample was finite and the upper corner of each volume was bright. That gives a clean reference for comparing the stock draw’s sampled inputs.

**@⁠SkillIssueXO** on GitHub · 2026-11-26 05:39 UTC · [original](https://github.com/fxceless/victory-fork/pull/1026#issuecomment-7183682950) · edited 2026-11-26 06:19 UTC In-game follow-up on RTX 5070 Ti (621.40). I ported the PR’s lowering to Neptune and retranslated only the final composite pixel shader. Both runs used the same runtime executable, Series X checkpoint, 4K output, and water compute shader. A hash comparison of all 2,922 warm-cache files found exactly one changed entry. The revised DXIL validated, pipeline creation succeeded, and the shader’s bindings were identical. At the held boat view, the stock composite showed black cores in the brightest water reflections. The revised composite showed bright cores in two captures over a minute apart. The sun disk was outside the frame, so this verifies the water reflection symptom in this scene. Over about 60 seconds each, stock and revised logged 6.47 and 6.38 DXGI-accepted presents/s respectively. Median intervals were 165.9 and 157.9 ms. This was one stationary A/B pair. AI-assisted testing and analysis. In a later Neptune replay with the revised composite, tilting the camera up showed a bright sun center in two captures. A readback immediately before a revised composite draw captured all three 33³ RGBA16F color lookup volumes. Every sample was finite and the upper corner of each volume was bright. That gives a clean reference for comparing the stock draw’s sampled inputs.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
The changes on this branch are already on the target branch. This will be an empty commit.
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1026'
git switch 'github-bot/gh-1026'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1026'
git switch 'github-bot/gh-1026'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1026'
git switch 'main'
git merge --squash 'github-bot/gh-1026'
git switch 'main'
git merge --ff-only 'github-bot/gh-1026'
git switch 'main'
git merge 'github-bot/gh-1026'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.