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

Closed
Moon0755 wants to merge 1 commit from upstream-pr/1026 into main
Moon0755 commented 2026-11-25 16:37:48 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Moon0755 on 2026-11-25T16:37:48Z.


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

<!-- greyfinch-sync:fxceless/victory-fork:issue:6609097798 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1026). Originally posted by [Moon0755](https://github.com/Moon0755) on 2026-11-25T16:37:48Z. --- 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
HAL9000RELOADED commented 2026-11-25 18:05:15 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by HAL9000RELOADED on 2026-11-25T18:00:57Z.


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://github.com/fxceless/victory-fork/pull/1030#issuecomment-7170315986

<!-- greyfinch-sync:fxceless/victory-fork:comment:7170318703 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1026#issuecomment-7170318703). Originally posted by [HAL9000RELOADED](https://github.com/HAL9000RELOADED) on 2026-11-25T18:00:57Z. --- 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://github.com/fxceless/victory-fork/pull/1030#issuecomment-7170315986
SkillIssueXO commented 2026-11-26 00:06:10 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by SkillIssueXO on 2026-11-25T23:56:43Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7178229102 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1026#issuecomment-7178229102). Originally posted by [SkillIssueXO](https://github.com/SkillIssueXO) on 2026-11-25T23:56:43Z. --- 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 commented 2026-11-26 06:07:03 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by SkillIssueXO on 2026-11-26T05:39:15Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7183682950 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1026#issuecomment-7183682950). Originally posted by [SkillIssueXO](https://github.com/SkillIssueXO) on 2026-11-26T05:39:15Z. --- 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.
UwUCammie commented 2026-11-26 10:06:45 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by UwUCammie on 2026-11-26T09:13:40Z.


The fused-rounding case in legacy_mad_fuses_its_product_and_flushes_its_sources still returns zero on my RX 7900 XT (RADV 26.3.1, vkd3d-proton 3.1, Wine). The expected bits are 0xa8800000. I used the synthetic shader from this PR.

I dumped that test's SPIR-V. It has a GLSL.std.450 Fma, with NoContraction on the separate zero-addend instruction only. Adding NoContraction to the Fma alone, validating the module and rerunning the same test through vkd3d's shader override made all four hardware cases pass. I then removed the override and ran the unmodified binary with vkd3d's FORCE_NOCONTRACT_MATH shader quirk. That also passes all four, and the emitted Fma has the decoration.

This reproduces a backend-specific rounding difference without the game. I haven't established a link to facial deformation. The WARP-labelled run here falls back to the same physical adapter under DXVK.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7187623405 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1026#issuecomment-7187623405). Originally posted by [UwUCammie](https://github.com/UwUCammie) on 2026-11-26T09:13:40Z. --- The fused-rounding case in `legacy_mad_fuses_its_product_and_flushes_its_sources` still returns zero on my RX 7900 XT (RADV 26.3.1, vkd3d-proton 3.1, Wine). The expected bits are `0xa8800000`. I used the synthetic shader from this PR. I dumped that test's SPIR-V. It has a GLSL.std.450 Fma, with NoContraction on the separate zero-addend instruction only. Adding NoContraction to the Fma alone, validating the module and rerunning the same test through vkd3d's shader override made all four hardware cases pass. I then removed the override and ran the unmodified binary with vkd3d's FORCE_NOCONTRACT_MATH shader quirk. That also passes all four, and the emitted Fma has the decoration. This reproduces a backend-specific rounding difference without the game. I haven't established a link to facial deformation. The WARP-labelled run here falls back to the same physical adapter under DXVK.
upstream-sync closed this pull request 2026-11-26 12:07:12 +00:00
Moon0755 commented 2026-11-26 12:07:19 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Moon0755 on 2026-11-26T11:22:46Z.


Closing: the owner took this change into main in f6a09d3, with a reference to this pull request, so the branch now conflicts with its own content there. Thanks to everyone who ran it, on hardware and in the game.

UwUCammie's finding still applies to main: on RADV through vkd3d-proton the fused case passes only with NoContraction on the Fma. That is a property of the layer under the test, not of this change, and it deserves an issue of its own so it is not lost with this pull request.

— Fable

<!-- greyfinch-sync:fxceless/victory-fork:comment:7190649818 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1026#issuecomment-7190649818). Originally posted by [Moon0755](https://github.com/Moon0755) on 2026-11-26T11:22:46Z. --- Closing: the owner took this change into `main` in f6a09d3, with a reference to this pull request, so the branch now conflicts with its own content there. Thanks to everyone who ran it, on hardware and in the game. UwUCammie's finding still applies to `main`: on RADV through vkd3d-proton the fused case passes only with `NoContraction` on the `Fma`. That is a property of the layer under the test, not of this change, and it deserves an issue of its own so it is not lost with this pull request. — Fable

Pull request closed

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
fxceless/victory-fork!1024
No description provided.