Skip to content

Shaders: lower VOP3 integer clamp saturation for add/sub ops - #1478

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

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

Conversation

@geiyiliyabumo

Copy link
Copy Markdown
Contributor

Summary

Implements VOP3 integer \clamp\ saturation for RDNA2 / GFX10.3 integer add/sub instructions in \�ictory-shader\ (tracking issue #1242, \VIC-BT-010).

Previously, \Lower::write_i\ rejected any integer VALU instruction with \�.clamp, refusing valid clamped integer add/sub instructions emitted by console shaders.

Changes

  • \crates/victory-shader/src/emit/lower.rs:
    • Allow \write_i\ to accept instructions whose \�.clamp\ has been explicitly handled while still rejecting unsupported \�.clamp\ or non-zero \�.omod.
    • Saturate unsigned overflow to \

@AnoHunted

Copy link
Copy Markdown
Contributor

Independent NVIDIA hardware run. No game files were used.

Machine: Windows 11, NVIDIA GeForce RTX 5080, Rust 1.98.1, DXC dxil.dll 1.9.2609 (SDK 10.0.26100) validating.

Applied on main (ffe310e), cargo test -p victory-shader --features host -- --test-threads=1:

  • 124 passed, 0 failed, 1 ignored (main: 120 / 0 / 1).
  • The four added tests (vop3_unsigned_clamp_preserves_carry_and_borrow_out, vop3_unsigned_no_carry_add_sub_respect_clamp, vop3_signed_no_carry_add_sub_respect_clamp, v_add3_u32_wraps_on_double_carry) execute through run_cases/run_cases_with_output on the RTX 5080 (and WARP) and all pass.

Not tested: the in-game effect; AMD.

@marquezinii

Copy link
Copy Markdown
Contributor

Ran on an RTX 5070 (desktop, driver 621.40, the only adapter), Windows 11 26300, Rust 1.98.1 at 1905213. cargo test -p victory-shader --lib -- --test-threads=1 --nocapture gives 124 passed, 0 failed, 1 ignored.

#1398 covers the same VIC-BT-010 item, so I ran each PR's tests against the other PR's code (tests copied in, everything else unchanged):

tests from on #1478 on #1398
#1478: vop3_unsigned_clamp_preserves_carry_and_borrow_out, vop3_unsigned_no_carry_add_sub_respect_clamp, v_add3_u32_wraps_on_double_carry pass pass
#1478: vop3_signed_no_carry_add_sub_respect_clamp pass refused: v_add_nc_i32 isn't lowered there
#1398: vop3_unsigned_add_sub_clamp refused (integer output modifiers) only for v_add3_u32 with clamp; the other 9 ops pass pass

So the two agree on every value both produce on this hardware. Each one covers something the other doesn't: this PR adds signed v_add/sub_nc_i32 with clamp, and #1398 adds clamped v_add3_u32 (saturating when either add carries). I also checked the signed saturation here: overflow when the sign of b disagrees with whether the result moved below a, and the limit follows the sign of a. That looks right.

@wwwwwwww3333333

Copy link
Copy Markdown
Contributor

Native Windows run on AMD RDNA2: RX 6750 XT (RDNA2, gfx1031), AMD driver 32.0.21045.29003, i5-13400F, Windows 11, Rust 1.98.1 MSVC. Head 1905213 on main ffe310e (clean merge).

cargo test -p victory-shader --lib -- --test-threads=1: 124 passed, 0 failed, 1 ignored (main alone: 120 passed). v_add3_u32_wraps_on_double_carry, vop3_signed_no_carry_add_sub_respect_clamp, vop3_unsigned_clamp_preserves_carry_and_borrow_out, vop3_unsigned_no_carry_add_sub_respect_clamp ran on the hardware adapter and then on WARP, and passed on both. This adds an AMD result to the NVIDIA runs already posted.

Synthetic tests only, no game files. AI-assisted (Claude picked the PRs and wrote the batch script; I ran it on my machine).

@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. Execution/valu tests are #[cfg(all(test, windows))] and were not run here.

This subsumes the unsigned half of #1398 (already approved) and goes further — carry-out variants (v_add_co_u32 etc.) clamp via select(carry, limit, r) with the carry-out itself preserved, and the _i32 clamp path attempts signed overflow detection. Three things before merge:

  1. Overlap: #1398 touches the same write_i/nc-op hunks plus a resource.rs change (|| f.clamp in the plain-move bail-out) that this PR does not have. If both are needed, whichever lands second must reconcile; if #1398 lands first this will conflict textually.
  2. Signed clamp is untested: both new valu tests are vop3_unsigned_*; the _i32 overflow formula (decreases XOR decreased, limit by sign of a) has no hardware case. The unsigned saturation semantics are well covered (carry/borrow preservation across 6 opcodes × clamp on/off), but please add at least one signed clamp case — the formula is subtle enough that I'd want RDNA2 evidence.
  3. v_add3_u32 correctly noted as having no clamp modifier on GFX10.3.

macOS: 96 passed, merge clean.

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump, and the shader tests on Ada

Result: no count change against main.

Offline. I merged head 1905213 onto main ffe310e (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 21,724 on main, 21,724 with this PR.

No program changes its failure reason either. That is the expected result for a fix whose instructions sit behind the earlier failures on main. The count shows that the change rejects nothing. It does not show whether the output is right.

Tests: with the same merge, cargo test --locked -p victory-shader --lib passed 124, failed 0. The bitcode benchmark is ignored, as on main, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with VICTORY_WARP unset.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

@geiyiliyabumo

Copy link
Copy Markdown
Contributor Author

Thanks @catminou4 for the macOS verification and review! Just a quick note on point #2: commit 1905213 already includes vop3_signed_no_carry_add_sub_respect_clamp testing v_add_nc_i32 and v_sub_nc_i32 with clamp on/off across positive/negative overflow and boundary cases — verified passing above by @AnoHunted (RTX 5080), @marquezinii (RTX 5070), @wwwwwwww3333333 (RX 6750 XT), @sinan-cmyk (RTX 4070 SUPER), and on our RX 9060 XT + WARP. Happy to rebase onto #1398 if #1398 lands first.

AI-assisted; synthetic tests only, 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 #1398. clamp on VOP3 integer add/sub. The unsigned carry and no-carry code is the same as in #1398 (identical lines), which also guards descriptor tracing; the extra signed v_add_nc_i32/v_sub_nc_i32 are also in #92. Neither 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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants