Skip to content

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

Closed
geiyiliyabumo wants to merge 1 commit into
Marvisak:mainfrom
geiyiliyabumo:fix/shader-16bit-int-vop3
Closed

geiyiliyabumo wants to merge 1 commit into
Marvisak:mainfrom
geiyiliyabumo:fix/shader-16bit-int-vop3

Conversation

@geiyiliyabumo

@geiyiliyabumo geiyiliyabumo commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

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 DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@geiyiliyabumo
geiyiliyabumo force-pushed the fix/shader-16bit-int-vop3 branch from 8512978 to 6fd640a Compare November 24, 2026 20:59
@geiyiliyabumo geiyiliyabumo changed the title shader: lower 10 RDNA2 16-bit integer VOP3 ops (add/sub/mad/min3/max3/med3) shader: lower v_add_nc_i16, v_sub_nc_i16, v_mad_i16, and v_mad_u16 with clamp Nov 24, 2026
@adamdotpy

Copy link
Copy Markdown

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 Shaders: read inline floats as f32 bits in 16-bit integer ops #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 Shaders: read inline floats as f32 bits in 16-bit integer ops #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 Shaders: lower v_{min3,max3,med3}_{i16,u16} and v_mad_{u32_u16,i32_i16} #1300: complementary — Shaders: lower v_{min3,max3,med3}_{i16,u16} and v_mad_{u32_u16,i32_i16} #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 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

upipibi commented Nov 24, 2026

Copy link
Copy Markdown

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 GPU: skip unmapped regions after a failed bulk read into host memory #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.

@Marvisak

Copy link
Copy Markdown
Owner

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 Marvisak closed this Nov 25, 2026

Copy link
Copy Markdown
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants