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

Closed
github-bot wants to merge 1 commit from gh-pr/1699/head into gh-pr/1699/base
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-24 20:52 UTC · original
geiyiliyabumo:fix/shader-16bit-int-vop3 → main · +126 −3 · 2 files · 1 commits · closed without merging

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.

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 20:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/1699) `geiyiliyabumo:fix/shader-16bit-int-vop3` → `main` · +126 −3 · 2 files · 1 commits · closed without merging ## 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.*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:42:03 +00:00
github-bot closed this pull request 2026-11-26 04:42:03 +00:00
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 21:09 UTC · original

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.

**@⁠adamdotpy** on GitHub · 2026-11-24 21:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/1699#issuecomment-7144558891) 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.
Author
Owner

@⁠upipibi on GitHub · 2026-11-24 22:46 UTC · original

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.

**@⁠upipibi** on GitHub · 2026-11-24 22:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/1699#issuecomment-7146622254) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:24 UTC · original

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** on GitHub · 2026-11-25 01:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/1699#issuecomment-7149519741) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:34 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 01:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/1699#issuecomment-7149709314) <!-- 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

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!1699
No description provided.