shader: lower v_add_nc_i16, v_sub_nc_i16, v_mad_i16, and v_mad_u16 with clamp #1698

Closed
geiyiliyabumo wants to merge 1 commit from upstream-pr/1699 into main
geiyiliyabumo commented 2026-11-24 20:52:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by geiyiliyabumo on 2026-11-24T20:52:11Z.


Summary

  • Lowers four RDNA2 16-bit integer VOP3 arithmetic instructions in crates/victory-shader/src/emit/lower.rs (valu16_op), honoring op_sel[0..3] and preserving the untouched VGPR half via write_half with pure 32-bit DXIL arithmetic:
    • v_add_nc_i16 (0x30d), v_sub_nc_i16 (0x30e), v_mad_i16 (0x35e), and v_mad_u16 (0x340), with both wraparound and CLAMP saturation ([-32768, 32767] signed / [0, 65535] unsigned).
  • Scoped cleanly to avoid overlapping with #1300 (v_{min3,max3,med3}_{i16,u16}), #1688 (v_mad_{u32_u16,i32_i16}), and #1668 (half_src inline float handling).

Verification

  • cargo test -p victory-shader --lib valu_tests: PASS on both AMD Radeon RX 9060 XT (D3D12 hardware) and Microsoft Basic Render Driver (WARP), including vop3_16bit_int_add_sub_mad_and_triads_match_rdna2 testing all 4 instructions across wrap vs CLAMP saturation, op_sel high/low half selection, and untouched VGPR half preservation.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: pass (154 files).

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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6591169152 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1699). Originally posted by [geiyiliyabumo](https://github.com/geiyiliyabumo) on 2026-11-24T20:52:11Z. --- ## Summary - Lowers four RDNA2 16-bit integer VOP3 arithmetic instructions in `crates/victory-shader/src/emit/lower.rs` (`valu16_op`), honoring `op_sel[0..3]` and preserving the untouched VGPR half via `write_half` with pure 32-bit DXIL arithmetic: - `v_add_nc_i16` (`0x30d`), `v_sub_nc_i16` (`0x30e`), `v_mad_i16` (`0x35e`), and `v_mad_u16` (`0x340`), with both wraparound and `CLAMP` saturation (`[-32768, 32767]` signed / `[0, 65535]` unsigned). - Scoped cleanly to avoid overlapping with #1300 (`v_{min3,max3,med3}_{i16,u16}`), #1688 (`v_mad_{u32_u16,i32_i16}`), and #1668 (`half_src` inline float handling). ## Verification - `cargo test -p victory-shader --lib valu_tests`: PASS on both **AMD Radeon RX 9060 XT (D3D12 hardware)** and **Microsoft Basic Render Driver (WARP)**, including `vop3_16bit_int_add_sub_mad_and_triads_match_rdna2` testing all 4 instructions across wrap vs `CLAMP` saturation, `op_sel` high/low half selection, and untouched VGPR half preservation. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: pass (154 files). *AI-assisted; synthetic tests only, no game files.*
DenRakEiw commented 2026-11-24 21:03:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-24T20:56:54Z.

Review: APPROVED


Verified locally merged onto origin/main (Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC, with dxil.dll validator):

  • cargo test -p victory-shader --lib vop3_16bit_int_add_sub_mad_and_triads_match_rdna2 — passed on RTX 5090 hardware and under VICTORY_D3D12_WARP=1
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok

Clean lowering of the 10 RDNA2 16-bit integer VOP3 ops (add/sub/mad/min3/max3/med3) with end-to-end GPU+WARP verification. Looks good.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6047271445 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1699#pullrequestreview-6047271445). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-24T20:56:54Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC, with `dxil.dll` validator): - `cargo test -p victory-shader --lib vop3_16bit_int_add_sub_mad_and_triads_match_rdna2` — passed on RTX 5090 hardware and under `VICTORY_D3D12_WARP=1` - `cargo clippy -p victory-shader --all-targets` — 0 warnings - `cargo xtask policy` — ok Clean lowering of the 10 RDNA2 16-bit integer VOP3 ops (add/sub/mad/min3/max3/med3) with end-to-end GPU+WARP verification. Looks good. AI-assisted (Gemini 4 Neon).
adamdotpy commented 2026-11-24 21:28:29 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by adamdotpy on 2026-11-24T21:09:43Z.


Reviewed head 6fd640a locally (isolated worktree, fresh CARGO_TARGET_DIR, real GPU dispatch).

What it does: lowers v_add_nc_i16, v_sub_nc_i16, v_mad_i16, v_mad_u16 in the shared 16-bit VOP3 arm. Signed forms sext all three sources to i32, compute in 32-bit, and when the clamp bit is set saturate to [i16::MIN, i16::MAX] (SLT/SGT + select); v_mad_u16 saturates to u16::MAX with an unsigned compare. Clamp is now accepted for exactly these four names and still refused for the other 16-bit ops (v_add_nc_u16 etc.), keeping the conservative refusal where hardware behavior wasn't pinned.

Verified locally:

  • cargo test -p victory-shader --lib vop3_16bit_int_add_sub_mad_and_triads_match_rdna2 -- --nocapture — passes on hardware (2.2s, no skip): 4 ops x clamp on/off x 4 cases, checking wrap vs clamp, exact i32/u32 reference, and high-half preservation through op_sel writes
  • cargo test -p victory-shader --lib --locked — 121 passed / 0 failed / 1 ignored
  • cargo clippy -p victory-shader --all-targets --all-features --locked — clean
  • cargo build --release -p victory --locked — ok
  • rustfmt vs main@ffe310e: lower.rs has the same 3 pre-existing hunks (7099/7118/7137 -> 7142/7161/7180, shifted by the added lines), no new ones; valu_tests.rs has 2 new cosmetic hunks (940, 980, long lines in the new test)

Findings, ranked:

  1. Sound: the clamp math is exact in i32/u32 (no intermediate overflow: (-32768)*(-32768)+32767 fits i32), the wrap path keeps the low 16 bits, and the hardware test pins all of it including the untouched high half. The clamp-allowlist is the right conservative shape.
  2. Interaction with #1668 (same rule we verified there): the mad arm reads src2 via half_src (f16-packed inline constants), but 16-bit integer ops read inline floats as raw f32 bits — so when #1668 lands, let mut d = self.half_src(inst, 2)? must become half_int_src(inst, 2), and the shared (a, b) pair flips to half_int_src too (textual conflict at the same lines, same behavior). Worth a one-line note here so the second merger adjusts the mad operand as well.
  3. Relationship to #1300: complementary — #1300 covers min3/max3/med3 and the mad32_16 forms, this covers the plain 16-bit add/sub/mad dst forms; no overlap in opcodes.
  4. Minor: 2 new rustfmt hunks in the test file; not blocking (no fmt CI job).

LGTM, with the #1668 note above.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7144558891 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1699#issuecomment-7144558891). Originally posted by [adamdotpy](https://github.com/adamdotpy) on 2026-11-24T21:09:43Z. --- Reviewed head 6fd640a locally (isolated worktree, fresh CARGO_TARGET_DIR, real GPU dispatch). **What it does:** lowers `v_add_nc_i16`, `v_sub_nc_i16`, `v_mad_i16`, `v_mad_u16` in the shared 16-bit VOP3 arm. Signed forms sext all three sources to i32, compute in 32-bit, and when the clamp bit is set saturate to [i16::MIN, i16::MAX] (SLT/SGT + select); `v_mad_u16` saturates to u16::MAX with an unsigned compare. Clamp is now accepted for exactly these four names and still refused for the other 16-bit ops (`v_add_nc_u16` etc.), keeping the conservative refusal where hardware behavior wasn't pinned. **Verified locally:** - `cargo test -p victory-shader --lib vop3_16bit_int_add_sub_mad_and_triads_match_rdna2 -- --nocapture` — passes on hardware (2.2s, no skip): 4 ops x clamp on/off x 4 cases, checking wrap vs clamp, exact i32/u32 reference, and high-half preservation through op_sel writes - `cargo test -p victory-shader --lib --locked` — 121 passed / 0 failed / 1 ignored - `cargo clippy -p victory-shader --all-targets --all-features --locked` — clean - `cargo build --release -p victory --locked` — ok - rustfmt vs `main`@ffe310e: `lower.rs` has the same 3 pre-existing hunks (7099/7118/7137 -> 7142/7161/7180, shifted by the added lines), no new ones; `valu_tests.rs` has 2 new cosmetic hunks (940, 980, long lines in the new test) **Findings, ranked:** 1. Sound: the clamp math is exact in i32/u32 (no intermediate overflow: (-32768)*(-32768)+32767 fits i32), the wrap path keeps the low 16 bits, and the hardware test pins all of it including the untouched high half. The clamp-allowlist is the right conservative shape. 2. Interaction with #1668 (same rule we verified there): the mad arm reads src2 via `half_src` (f16-packed inline constants), but 16-bit *integer* ops read inline floats as raw f32 bits — so when #1668 lands, `let mut d = self.half_src(inst, 2)?` must become `half_int_src(inst, 2)`, and the shared `(a, b)` pair flips to `half_int_src` too (textual conflict at the same lines, same behavior). Worth a one-line note here so the second merger adjusts the mad operand as well. 3. Relationship to #1300: complementary — #1300 covers min3/max3/med3 and the mad32_16 forms, this covers the plain 16-bit add/sub/mad dst forms; no overlap in opcodes. 4. Minor: 2 new rustfmt hunks in the test file; not blocking (no fmt CI job). LGTM, with the #1668 note above.
catminou4 commented 2026-11-24 22:01:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T21:40:04Z.

Review: APPROVED


Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 96 passed, 0 failed. Hardware/valu/waterfall execution tests are #[cfg(all(test, windows))] and run only on Windows; macOS covers lowering/structure unit tests.

v_add_nc_i16/v_sub_nc_i16/v_mad_i16/v_mad_u16 lowered with clamp support (computed on selected 16-bit halves, low 16 bits kept); clamp_ok gate rejects clamp/omod/abs/neg for the rest. Hardware tests on RDNA2/WARP.

<!-- greyfinch-sync:Marvisak/VIctory:review:6047848074 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1699#pullrequestreview-6047848074). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T21:40:04Z. **Review:** APPROVED --- Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 96 passed, 0 failed. Hardware/valu/waterfall execution tests are #[cfg(all(test, windows))] and run only on Windows; macOS covers lowering/structure unit tests. v_add_nc_i16/v_sub_nc_i16/v_mad_i16/v_mad_u16 lowered with clamp support (computed on selected 16-bit halves, low 16 bits kept); clamp_ok gate rejects clamp/omod/abs/neg for the rest. Hardware tests on RDNA2/WARP.
upipibi commented 2026-11-24 23:06:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-24T22:46:09Z.


Measurement-backed extensions to the clamp list, from reading the diff (not run). AnyPS5 measured the VOP3 clamp bit on 16-bit integer ops on RDNA2 hardware: gfx1036, wave32 and wave64; boykopovar/AnyPS5's docs/dev/debt/shader.md "VOP3 16-bit integer binary clamp" and boykopovar/AnyPS5#22615.

  • On v_mul_lo_u16, v_lshlrev_b16, v_lshrrev_b16, v_ashrrev_i16, v_min/max_u16 and v_min/max_i16, the clamp bit changed none of the results. v_mul_lo_u16 keeps the low 16 bits of an overflowing product. Those eight could accept clamp as a no-op instead of refusing it. The one to keep wrapping is v_mul_lo_u16; XCL's #202 currently saturates it.
  • v_add_nc_u16 with clamp saturates on the same part. It is still refused by clamp_ok here and would be the unsigned counterpart of your v_add_nc_i16 path. v_sub_nc_u16 is presumably the same, but wasn't in that measurement.

AI-assisted (Claude Code). No game files.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7146622254 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1699#issuecomment-7146622254). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-24T22:46:09Z. --- Measurement-backed extensions to the clamp list, from reading the diff (not run). AnyPS5 measured the VOP3 clamp bit on 16-bit integer ops on RDNA2 hardware: gfx1036, wave32 and wave64; boykopovar/AnyPS5's `docs/dev/debt/shader.md` "VOP3 16-bit integer binary clamp" and boykopovar/AnyPS5#22615. - On `v_mul_lo_u16`, `v_lshlrev_b16`, `v_lshrrev_b16`, `v_ashrrev_i16`, `v_min/max_u16` and `v_min/max_i16`, the clamp bit changed none of the results. `v_mul_lo_u16` keeps the low 16 bits of an overflowing product. Those eight could accept `clamp` as a no-op instead of refusing it. The one to keep wrapping is `v_mul_lo_u16`; XCL's #202 currently saturates it. - `v_add_nc_u16` with clamp saturates on the same part. It is still refused by `clamp_ok` here and would be the unsigned counterpart of your `v_add_nc_i16` path. `v_sub_nc_u16` is presumably the same, but wasn't in that measurement. AI-assisted (Claude Code). No game files.
upstream-sync closed this pull request 2026-11-25 02:04:56 +00:00
Marvisak commented 2026-11-25 02:06:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:24:50Z.


Automated triage for the maintainers. Closing because another open pull request covers the same change.

See #1211. v_mad_u16/v_mad_i16 with clamp (the same code as #1211) plus v_add_nc_i16/v_sub_nc_i16 (also in #92). None is used by the title.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149519741 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1699#issuecomment-7149519741). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:24:50Z. --- Automated triage for the maintainers. Closing because another open pull request covers the same change. See #1211. v_mad_u16/v_mad_i16 with clamp (the same code as #1211) plus v_add_nc_i16/v_sub_nc_i16 (also in #92). None is used by the title. If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.
Marvisak commented 2026-11-25 02:06:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:34:45Z.


Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-shader/src/emit/valu_tests.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149709314 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1699#issuecomment-7149709314). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:34:45Z. --- <!-- victory-triage v1 head=6fd640a2ae53fb964cf41d924362a9392cc873f9 verdict=needs-rebase at=2026-11-25T01:34:44Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-shader/src/emit/valu_tests.rs`). It will be reviewed once it is rebased onto the current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_

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
Marvisak/VIctory!1698
No description provided.