Shaders: lower v_mad_u32_u16, v_mad_i32_i16, and v_lerp_u8 #1068

Open
opened 2026-11-26 04:18:51 +00:00 by github-bot · 3 comments
Owner

@⁠kilvynsilveira-tech on GitHub · 2026-11-24 15:27 UTC · original

Summary

Lowers three RDNA2 integer VALU instructions in \crates/victory-shader/src/emit/lower.rs\ that previously returned \Unsupported:

  • \ _mad_u32_u16\ and \ _mad_i32_i16\ (16x16->32 multiply-add, supporting \opsel\ low/high 16-bit operand selection for \src0\ and \src1)
  • \ _lerp_u8\ (packed 4x8-bit unsigned average with per-byte rounding bit from \src2: a[i] + b[i] + (c[i] & 1)) >> 1

Because external PR creation is currently restricted (\collaborators_only), the branch is ready on my fork for cherry-pick / review:

  • Branch: \kilvynsilveira-tech/VIctory@shader/lerp-and-mad16-u32\
  • Commit: \kilvynsilveira-tech/VIctory@5319a34\ (based on \main\ \ fe310e)

Changes

  • \crates/victory-shader/src/emit/lower.rs:
    • Added lowering for \ _mad_u32_u16, \ _mad_i32_i16, and \ _lerp_u8\ in \Lower::integer.
    • Added synthetic unit test \mad_32_16_and_lerp_u8_lower_and_validate\ (including \opsel\ high-half selection) with \dxil.dll\ validation.

Verification (Windows x64 MSVC)

  • \cargo clippy -p victory-shader --all-targets: PASS
  • \cargo xtask policy: PASS (\154 files ok)
  • \cargo test -p victory-shader mad_32_16_and_lerp_u8_lower_and_validate: PASS (validated with \dxil.dll)

Synthetic tests only; no game files.

**@⁠kilvynsilveira-tech** on GitHub · 2026-11-24 15:27 UTC · [original](https://github.com/Marvisak/VIctory/issues/1068) ### Summary Lowers three RDNA2 integer VALU instructions in \crates/victory-shader/src/emit/lower.rs\ that previously returned \Unsupported\: - \ _mad_u32_u16\ and \ _mad_i32_i16\ (16x16->32 multiply-add, supporting \opsel\ low/high 16-bit operand selection for \src0\ and \src1\) - \ _lerp_u8\ (packed 4x8-bit unsigned average with per-byte rounding bit from \src2\: \(a[i] + b[i] + (c[i] & 1)) >> 1\) Because external PR creation is currently restricted (\collaborators_only\), the branch is ready on my fork for cherry-pick / review: - **Branch**: \kilvynsilveira-tech/VIctory@shader/lerp-and-mad16-u32\ - **Commit**: \kilvynsilveira-tech/VIctory@5319a34\ (based on \main\ \ fe310e\) ### Changes - \crates/victory-shader/src/emit/lower.rs\: - Added lowering for \ _mad_u32_u16\, \ _mad_i32_i16\, and \ _lerp_u8\ in \Lower::integer\. - Added synthetic unit test \mad_32_16_and_lerp_u8_lower_and_validate\ (including \opsel\ high-half selection) with \dxil.dll\ validation. ### Verification (Windows x64 MSVC) - \cargo clippy -p victory-shader --all-targets\: **PASS** - \cargo xtask policy\: **PASS** (\154 files ok\) - \cargo test -p victory-shader mad_32_16_and_lerp_u8_lower_and_validate\: **PASS** (validated with \dxil.dll\) *Synthetic tests only; no game files.*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:18:51 +00:00
Author
Owner

@⁠DrDarkDK on GitHub · 2026-11-24 15:37 UTC · original

Tested 5319a34 on native Windows with an RTX 4070 Ti (621.01) and WARP. Both backends returned the expected values in all eight synthetic DXIL readbacks, covering independent op_sel choices, signed and unsigned MAD wraparound, and per-byte v_lerp_u8 rounding.

Both MAD opcodes with clamp set still return Unsupported: integer output modifiers during lowering.

I also scanned 26,096 decoded programs from the local 1.0.2.1 executable and shader archive. None contained these three opcodes. No gameplay test.

AI-assisted with Codex.

**@⁠DrDarkDK** on GitHub · 2026-11-24 15:37 UTC · [original](https://github.com/Marvisak/VIctory/issues/1068#issuecomment-7136335788) Tested `5319a34` on native Windows with an RTX 4070 Ti (621.01) and WARP. Both backends returned the expected values in all eight synthetic DXIL readbacks, covering independent `op_sel` choices, signed and unsigned MAD wraparound, and per-byte `v_lerp_u8` rounding. Both MAD opcodes with `clamp` set still return `Unsupported: integer output modifiers` during lowering. I also scanned 26,096 decoded programs from the local 1.0.2.1 executable and shader archive. None contained these three opcodes. No gameplay test. *AI-assisted with Codex.*
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 10:00 UTC · original

Automated triage for the maintainers.

main decodes v_mad_u32_u16, v_mad_i32_i16 and v_lerp_u8 (crates/victory-shader/src/gfx10/opcodes.rs), but crates/victory-shader/src/emit/lower.rs has no lowering for them, so a program using them is refused today. The change fits the translator's scope.

Open PR #1688 has the same title, refers to this issue and appears to carry the same three lowerings, so review can happen there. Pull requests are open to everyone now, so you can also open yours from shader/lerp-and-mad16-u32 (5319a34) if it differs.

From the test comment: the two MAD forms with clamp set are still refused (integer output modifiers). None of the three opcodes turned up in the 1.0.2.1 programs scanned, so this adds coverage but doesn't fix a known failure in the title.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 10:00 UTC · [original](https://github.com/Marvisak/VIctory/issues/1068#issuecomment-7159098393) <!-- victory-issue-triage v1 verdict=tracked seen=2026-11-24T15:37:20Z at=2026-11-25T09:50:38Z --> Automated triage for the maintainers. `main` decodes `v_mad_u32_u16`, `v_mad_i32_i16` and `v_lerp_u8` (`crates/victory-shader/src/gfx10/opcodes.rs`), but `crates/victory-shader/src/emit/lower.rs` has no lowering for them, so a program using them is refused today. The change fits the translator's scope. Open PR #1688 has the same title, refers to this issue and appears to carry the same three lowerings, so review can happen there. Pull requests are open to everyone now, so you can also open yours from `shader/lerp-and-mad16-u32` (`5319a34`) if it differs. From the test comment: the two MAD forms with `clamp` set are still refused (integer output modifiers). None of the three opcodes turned up in the 1.0.2.1 programs scanned, so this adds coverage but doesn't fix a known failure in the title. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-26 18:52 UTC · original

RDNA2 hardware results for these three opcodes, from gfx1036. Two op_sel bits do something here that #1688 doesn't model, and clamp saturates.

Setup.

  • Hardware: the Radeon iGPU of a Ryzen 7 9800X3D, gfx1036, AMD driver 32.0.21042.62, Windows 11. A HIP kernel (assembled by the driver's amd_comgr) runs the raw VOP3 words, one input row per lane.
  • Inputs: 76 rows. The 16-bit halves are drawn from 0, 1, 2, 0x7fff, 0x8000, 0x8001, 0xffff, 0x1234, 0xabcd, 0xff and random values; the addend is 0, -1, 0x80000000, 0x7fffffff or random. 12 more rows run with a preset destination.
  • Translator: the same words through build_valu_cs, on that Radeon and on an RTX 5080 (driver 610.88), one adapter at a time with a local switch.

PC RDNA2 silicon, not the console's GPU. No game data.

What the hardware does.

  • v_mad_u32_u16: S0[15:0] * S1[15:0] + S2, the addend all 32 bits, wrapping. v_mad_i32_i16: the same with signed 16-bit factors. Both match that on all 76 rows.
  • op_sel[0] / op_sel[1]: pick the high half of src0 / src1. 76/76 rows for op_sel 1, 2 and 3, both opcodes.
  • op_sel[2]: the addend becomes S2[31:16], zero-extended, for v_mad_i32_i16 too:
    • v_mad_u32_u16 op_sel:[0,0,1,0] of 0xffffffff, 0xffffffff, 0xffffffff gives 0xffff0000 (0xfffe0001 + 0xffff).
    • v_mad_i32_i16 of the same gives 0x00010000 (1 + 0xffff, not 1 - 1).
    • v_mad_i32_i16 of 0x8000ffff, 0x80000001, 0x7fff8000 gives 0x00007ffe.
  • op_sel[3]: the low 16 bits of the result go to D[31:16], and D[15:0] becomes 0, whatever it held before. 10 * 10 + 0 gives 0x00640000; 3 * 2 + 0x80017fff over a destination of 0xa5a5a5a5 gives 0x80050000.
  • clamp: saturates. v_mad_u32_u16 goes to 0xffffffff (0xffff * 0xffff + 0xffffffff → 0xffffffff), v_mad_i32_i16 to [-2^31, 2^31 - 1]. 76/76 rows each.
  • v_lerp_u8: (a + b + (c & 1)) >> 1 per byte, 76/76 rows. op_sel[0] changes nothing.

Translator, rows that differ from the hardware (of 76), the same on the Radeon and the RTX 5080:

form main 63bcca4 #1688 (8c6ff38)
v_mad_u32_u16 / v_mad_i32_i16, op_sel 0..3 refused 0 / 0
op_sel[2] set refused 62 / 62
op_sel[3] set refused 74 / 73
clamp refused refused ("integer output modifiers")
v_lerp_u8, with and without op_sel[0] refused 0

So #1688 is right where it reads op_sel[0..1]. It silently ignores op_sel[2] and op_sel[3], where main refuses the program. #1688 could model those two bits from the rows above, or refuse them. The saturating clamp is a pair of selects, if it is wanted. DrDarkDK found none of the three opcodes in the 1.0.2.1 programs, so this is about not getting a wrong answer later.

AI use: Claude Code (Claude Opus 5.5) wrote the probe and ran it on the GPUs above; the numbers come from those runs.

**@⁠Lyrnc** on GitHub · 2026-11-26 18:52 UTC · [original](https://github.com/Marvisak/VIctory/issues/1068#issuecomment-7200948270) RDNA2 hardware results for these three opcodes, from gfx1036. Two `op_sel` bits do something here that #1688 doesn't model, and `clamp` saturates. **Setup.** - **Hardware:** the Radeon iGPU of a Ryzen 7 9800X3D, **gfx1036**, AMD driver 32.0.21042.62, Windows 11. A HIP kernel (assembled by the driver's amd_comgr) runs the raw VOP3 words, one input row per lane. - **Inputs:** 76 rows. The 16-bit halves are drawn from `0`, `1`, `2`, `0x7fff`, `0x8000`, `0x8001`, `0xffff`, `0x1234`, `0xabcd`, `0xff` and random values; the addend is `0`, `-1`, `0x80000000`, `0x7fffffff` or random. 12 more rows run with a preset destination. - **Translator:** the same words through `build_valu_cs`, on that Radeon and on an RTX 5080 (driver 610.88), one adapter at a time with a local switch. PC RDNA2 silicon, not the console's GPU. No game data. **What the hardware does.** - **`v_mad_u32_u16`:** `S0[15:0] * S1[15:0] + S2`, the addend all 32 bits, wrapping. **`v_mad_i32_i16`:** the same with signed 16-bit factors. Both match that on all 76 rows. - **`op_sel[0]` / `op_sel[1]`:** pick the high half of `src0` / `src1`. 76/76 rows for `op_sel` 1, 2 and 3, both opcodes. - **`op_sel[2]`:** the addend becomes `S2[31:16]`, **zero-extended**, for `v_mad_i32_i16` too: - `v_mad_u32_u16 op_sel:[0,0,1,0]` of `0xffffffff, 0xffffffff, 0xffffffff` gives `0xffff0000` (`0xfffe0001 + 0xffff`). - `v_mad_i32_i16` of the same gives `0x00010000` (`1 + 0xffff`, not `1 - 1`). - `v_mad_i32_i16` of `0x8000ffff, 0x80000001, 0x7fff8000` gives `0x00007ffe`. - **`op_sel[3]`:** the low 16 bits of the result go to `D[31:16]`, and `D[15:0]` becomes 0, whatever it held before. `10 * 10 + 0` gives `0x00640000`; `3 * 2 + 0x80017fff` over a destination of `0xa5a5a5a5` gives `0x80050000`. - **`clamp`:** saturates. `v_mad_u32_u16` goes to `0xffffffff` (`0xffff * 0xffff + 0xffffffff → 0xffffffff`), `v_mad_i32_i16` to `[-2^31, 2^31 - 1]`. 76/76 rows each. - **`v_lerp_u8`:** `(a + b + (c & 1)) >> 1` per byte, 76/76 rows. `op_sel[0]` changes nothing. **Translator, rows that differ from the hardware (of 76), the same on the Radeon and the RTX 5080:** | form | `main` `63bcca4` | #1688 (`8c6ff38`) | |---|---|---| | `v_mad_u32_u16` / `v_mad_i32_i16`, `op_sel` 0..3 | refused | 0 / 0 | | `op_sel[2]` set | refused | **62 / 62** | | `op_sel[3]` set | refused | **74 / 73** | | `clamp` | refused | refused ("integer output modifiers") | | `v_lerp_u8`, with and without `op_sel[0]` | refused | 0 | So #1688 is right where it reads `op_sel[0..1]`. It silently ignores `op_sel[2]` and `op_sel[3]`, where `main` refuses the program. #1688 could model those two bits from the rows above, or refuse them. The saturating `clamp` is a pair of selects, if it is wanted. DrDarkDK found none of the three opcodes in the 1.0.2.1 programs, so this is about not getting a wrong answer later. AI use: Claude Code (Claude Opus 5.5) wrote the probe and ran it on the GPUs above; the numbers come from those runs.
This discussion has been locked. Commenting is limited to contributors.
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#1068
No description provided.