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

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

Imported from Marvisak/VIctory. Originally posted by geiyiliyabumo on 2026-11-24T18:18:28Z.


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 \
<!-- greyfinch-sync:Marvisak/VIctory:issue:6588495253 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1478). Originally posted by [geiyiliyabumo](https://github.com/geiyiliyabumo) on 2026-11-24T18:18:28Z. --- ## 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 commented 2026-11-24 19:04:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AnoHunted on 2026-11-24T18:38:07Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140921085 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1478#issuecomment-7140921085). Originally posted by [AnoHunted](https://github.com/AnoHunted) on 2026-11-24T18:38:07Z. --- 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 commented 2026-11-24 19:04:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by marquezinii on 2026-11-24T18:48:12Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7141173128 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1478#issuecomment-7141173128). Originally posted by [marquezinii](https://github.com/marquezinii) on 2026-11-24T18:48:12Z. --- 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.
catminou4 commented 2026-11-24 20:02:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T19:20:55Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6045811095 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1478#pullrequestreview-6045811095). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T19:20:55Z. **Review:** COMMENTED --- 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.
wwwwwwww3333333 commented 2026-11-24 20:03:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wwwwwwww3333333 on 2026-11-24T19:15:00Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7141821681 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1478#issuecomment-7141821681). Originally posted by [wwwwwwww3333333](https://github.com/wwwwwwww3333333) on 2026-11-24T19:15:00Z. --- 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).
sinan-cmyk commented 2026-11-24 21:03:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-24T20:13:30Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7143214917 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1478#issuecomment-7143214917). Originally posted by [sinan-cmyk](https://github.com/sinan-cmyk) on 2026-11-24T20:13:30Z. --- ### 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 commented 2026-11-24 21:28:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by geiyiliyabumo on 2026-11-24T21:07:05Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7144498682 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1478#issuecomment-7144498682). Originally posted by [geiyiliyabumo](https://github.com/geiyiliyabumo) on 2026-11-24T21:07:05Z. --- 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.*
upstream-sync closed this pull request 2026-11-25 01:06:39 +00:00
Marvisak commented 2026-11-25 01:08:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:40:56Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148766134 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1478#issuecomment-7148766134). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:40:56Z. --- 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.

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