Shaders: lower v_mad_f16 and v_mac_f16 with unfused binary16 product rounding #1598

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

@⁠geiyiliyabumo on GitHub · 2026-11-24 19:41 UTC · original
geiyiliyabumo:fix/shader-mad-f16-unfused → main · +159 −6 · 2 files · 1 commits · open

Summary

Separates v_mad_f16 / v_mac_f16 from fused v_fma_f16 / v_fmac_f16 so legacy unfused binary16 multiply-add rounds the intermediate product a * b to binary16 before adding the accumulator (tracking issue #1242, VIC-BT-042).

Previously, Lower::valu16 lowered v_fma_f16 | v_fmac_f16 | v_mad_f16 | v_mac_f16 identically to self.fmad(a, b, acc). Additionally, DXIL legacyF32ToF16 clamps overflowing finite products (|a * b| >= 65520) to ±65504 (0x7BFF) instead of IEEE-754 ±inf (0x7C00).

Changes

  • crates/victory-shader/src/emit/lower.rs:
    • Add a pure 32-bit nearest-even binary16 narrowing helper (no f64 or INT64 DXIL flags) that rounds ties-to-even and produces signed infinity at magnitude >= 65520.
    • Lower v_mad_f16 | v_mac_f16 as round_f16(a * b) + acc while preserving the existing fused v_fma_f16 | v_fmac_f16 path.
  • crates/victory-shader/src/emit/valu_tests.rs: Add GPU-executed regression test v_mad_f16_rounds_the_product_before_adding comparing unfused MAD against encoded v_fma_f16 across 12 cases (product cancellation, overflow threshold 65520, normal/subnormal ties, signed zero, and ordinary arithmetic). Note: GFX10.3 encodes only fused v_fma_f16/v_fmac_f16, so the test uses a #[cfg(test)]-gated mnemonic path to exercise the v_mad_f16 lowering directly on GPU hardware and WARP.

Verification

  • cargo test -p victory-shader --lib valu_tests: 14/14 passed on AMD Radeon RX 9060 XT (RDNA4, SM 6.6) and WARP.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 154 files OK.

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

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 19:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/1598) `geiyiliyabumo:fix/shader-mad-f16-unfused` → `main` · +159 −6 · 2 files · 1 commits · open ## Summary Separates `v_mad_f16` / `v_mac_f16` from fused `v_fma_f16` / `v_fmac_f16` so legacy unfused binary16 multiply-add rounds the intermediate product `a * b` to binary16 before adding the accumulator (tracking issue #1242, `VIC-BT-042`). Previously, `Lower::valu16` lowered `v_fma_f16 | v_fmac_f16 | v_mad_f16 | v_mac_f16` identically to `self.fmad(a, b, acc)`. Additionally, DXIL `legacyF32ToF16` clamps overflowing finite products (`|a * b| >= 65520`) to `±65504` (`0x7BFF`) instead of IEEE-754 `±inf` (`0x7C00`). ### Changes - `crates/victory-shader/src/emit/lower.rs`: - Add a pure 32-bit nearest-even binary16 narrowing helper (no `f64` or `INT64` DXIL flags) that rounds ties-to-even and produces signed infinity at magnitude `>= 65520`. - Lower `v_mad_f16 | v_mac_f16` as `round_f16(a * b) + acc` while preserving the existing fused `v_fma_f16 | v_fmac_f16` path. - `crates/victory-shader/src/emit/valu_tests.rs`: Add GPU-executed regression test `v_mad_f16_rounds_the_product_before_adding` comparing unfused MAD against encoded `v_fma_f16` across 12 cases (product cancellation, overflow threshold `65520`, normal/subnormal ties, signed zero, and ordinary arithmetic). Note: GFX10.3 encodes only fused `v_fma_f16`/`v_fmac_f16`, so the test uses a `#[cfg(test)]`-gated mnemonic path to exercise the `v_mad_f16` lowering directly on GPU hardware and WARP. ### Verification - `cargo test -p victory-shader --lib valu_tests`: **14/14 passed** on **AMD Radeon RX 9060 XT (RDNA4, SM 6.6)** and **WARP**. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 154 files OK. *AI-assisted; synthetic tests only, no game files.*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:37:48 +00:00
Author
Owner

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

Automated triage for the maintainers.

The new unfused lowering for v_mad_f16/v_mac_f16 cannot be reached by a title shader: the GFX10 decoder table in crates/victory-shader/src/gfx10/opcodes.rs has no v_mad_f16 or v_mac_f16 (RDNA2 has only the fused v_fma_f16/v_fmac_f16, as the PR description itself notes). The test therefore works by renaming v_fma_f16 to v_mad_f16 through a #[cfg(test)] flag added to Lower, so it exercises a path that never runs outside the test, and it adds a test-only field to production code. The names in Lower::valu16 are leftovers that could simply be removed instead. If a GFX10.3 encoding that decodes to these names exists, please point to it and this can be reconsidered.

This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the triage: proposed close label.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/1598#issuecomment-7149650113) · edited 2026-11-25 02:27 UTC <!-- victory-triage v1 head=a3c911608bb003aad4077de8dc59e378aa5b4029 verdict=proposed-close at=2026-11-25T02:20:29Z --> Automated triage for the maintainers. The new unfused lowering for `v_mad_f16`/`v_mac_f16` cannot be reached by a title shader: the GFX10 decoder table in `crates/victory-shader/src/gfx10/opcodes.rs` has no `v_mad_f16` or `v_mac_f16` (RDNA2 has only the fused `v_fma_f16`/`v_fmac_f16`, as the PR description itself notes). The test therefore works by renaming `v_fma_f16` to `v_mad_f16` through a `#[cfg(test)]` flag added to `Lower`, so it exercises a path that never runs outside the test, and it adds a test-only field to production code. The names in `Lower::valu16` are leftovers that could simply be removed instead. If a GFX10.3 encoding that decodes to these names exists, please point to it and this can be reconsidered. This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the `triage: proposed close` label. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-25 02:08 UTC · original

Rebased cleanly onto \main\ (\ccfb029). \cargo test -p victory-shader --locked mad_f16\ (verified on physical AMD RX 9060 XT + WARP + Microsoft \dxil.dll), \cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings, and \cargo xtask policy\ all pass.

**@⁠geiyiliyabumo** on GitHub · 2026-11-25 02:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/1598#issuecomment-7150365819) Rebased cleanly onto \main\ (\ccfb029\). \cargo test -p victory-shader --locked mad_f16\ (verified on physical AMD RX 9060 XT + WARP + Microsoft \dxil.dll\), \cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings\, and \cargo xtask policy\ all pass.
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/lower.rs
  • crates/victory-shader/src/emit/valu_tests.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/1598/head:github-bot/gh-1598'
git switch 'github-bot/gh-1598'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1598'
git switch 'github-bot/gh-1598'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1598'
git switch 'github-bot/gh-1598'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1598'
git switch 'main'
git merge --squash 'github-bot/gh-1598'
git switch 'main'
git merge --ff-only 'github-bot/gh-1598'
git switch 'main'
git merge 'github-bot/gh-1598'
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!1598
No description provided.