Shaders: honor clamp on unsigned VOP3 add and subtract #1398

Open
EatCodeSleep2 wants to merge 1 commit from upstream-pr/1398 into main
EatCodeSleep2 commented 2026-11-24 17:45:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by EatCodeSleep2 on 2026-11-24T17:45:12Z.


Addresses VIC-BT-010 in #1242. Clamped unsigned addition saturates at UINT32_MAX on overflow, and clamped unsigned subtraction saturates at zero on borrow. The carry and borrow outputs retain their original values. This covers the carry, carry-in, no-carry and add3 forms. Descriptor tracing declines to treat a saturating addition as a plain address addition.

The synthetic Windows shader regression reads back the result and the carry/borrow independently, with and without clamp. It fails on main with "integer output modifiers." The updated regression passes alongside 119 other shader tests. The full suite still reproduces the existing host v_med3_f32 NaN failure tracked in #726. Shader Clippy with warnings as errors passes.

AI use: Codex wrote and tested the change.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6587905827 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1398). Originally posted by [EatCodeSleep2](https://github.com/EatCodeSleep2) on 2026-11-24T17:45:12Z. --- Addresses VIC-BT-010 in #1242. Clamped unsigned addition saturates at UINT32_MAX on overflow, and clamped unsigned subtraction saturates at zero on borrow. The carry and borrow outputs retain their original values. This covers the carry, carry-in, no-carry and add3 forms. Descriptor tracing declines to treat a saturating addition as a plain address addition. The synthetic Windows shader regression reads back the result and the carry/borrow independently, with and without clamp. It fails on main with "integer output modifiers." The updated regression passes alongside 119 other shader tests. The full suite still reproduces the existing host v_med3_f32 NaN failure tracked in #726. Shader Clippy with warnings as errors passes. AI use: Codex wrote and tested the change.
catminou4 commented 2026-11-24 18:05:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T17:52:33Z.

Review: APPROVED


Clamp on unsigned VOP3 add/sub: carry/borrow now saturates to u32::MAX/0 via select instead of rejecting the modifier. The dedicated arm covers v_add_nc/v_sub_nc/v_subrev_nc/v_add3 (overflow = OR of both carries for add3), the _co family reuses add_sub_carry with its carry-out, and every other integer op still errors on clamp through the int closure — conservative. resource.rs adds f.clamp to the plain-move bail-out so clamped ops aren't mistaken for moves. The 10-op x clamp x carry-in matrix is a solid hardware test. Local verification: macOS arm64, rustc 1.98.1, PR head merged into main ffe310e, cargo test -p victory-shader --locked. Shader execution tests (valu_tests/host/diff_tests) are #[cfg(all(test, windows))], so macOS covers lowering/decode/unit tests only. 96 passed, 0 failed on macOS; valu test Windows-gated.

<!-- greyfinch-sync:Marvisak/VIctory:review:6044527917 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1398#pullrequestreview-6044527917). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T17:52:33Z. **Review:** APPROVED --- Clamp on unsigned VOP3 add/sub: carry/borrow now saturates to u32::MAX/0 via select instead of rejecting the modifier. The dedicated arm covers v_add_nc/v_sub_nc/v_subrev_nc/v_add3 (overflow = OR of both carries for add3), the *_co* family reuses add_sub_carry with its carry-out, and every other integer op still errors on clamp through the int closure — conservative. resource.rs adds f.clamp to the plain-move bail-out so clamped ops aren't mistaken for moves. The 10-op x clamp x carry-in matrix is a solid hardware test. Local verification: macOS arm64, rustc 1.98.1, PR head merged into main ffe310e, `cargo test -p victory-shader --locked`. Shader execution tests (valu_tests/host/diff_tests) are `#[cfg(all(test, windows))]`, so macOS covers lowering/decode/unit tests only. 96 passed, 0 failed on macOS; valu test Windows-gated.
marquezinii commented 2026-11-24 19:04:05 +00:00 (Migrated from github.com)

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


Ran on an RTX 5070 (desktop, driver 621.40, the only adapter), Windows 11 26300, Rust 1.98.1 at 38ddebf. cargo test -p victory-shader --lib -- --test-threads=1 gives 121 passed, 0 failed, 1 ignored. vop3_unsigned_add_sub_clamp passes on the hardware adapter and WARP.

The saturation reads right to me:

  • A subtraction goes to 0 on borrow.
  • v_add3_u32 ORs the carries of both adds, so overflow in either step saturates.
  • The carry and borrow outputs keep their unclamped values.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7140272318 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1398#issuecomment-7140272318). Originally posted by [marquezinii](https://github.com/marquezinii) on 2026-11-24T18:10:47Z. --- Ran on an RTX 5070 (desktop, driver 621.40, the only adapter), Windows 11 26300, Rust 1.98.1 at `38ddebf`. `cargo test -p victory-shader --lib -- --test-threads=1` gives 121 passed, 0 failed, 1 ignored. `vop3_unsigned_add_sub_clamp` passes on the hardware adapter and WARP. The saturation reads right to me: - A subtraction goes to 0 on borrow. - `v_add3_u32` ORs the carries of both adds, so overflow in either step saturates. - The carry and borrow outputs keep their unclamped values.
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:14Z.


Cross-checked this against #1478, which covers the same VIC-BT-010 item: details in https://github.com/Marvisak/VIctory/pull/1478. On an RTX 5070 the two give identical results on the 9 ops they share. This PR adds clamped v_add3_u32, and #1478 adds signed v_add/sub_nc_i32 with clamp.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7141173653 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1398#issuecomment-7141173653). Originally posted by [marquezinii](https://github.com/marquezinii) on 2026-11-24T18:48:14Z. --- Cross-checked this against #1478, which covers the same VIC-BT-010 item: details in https://github.com/Marvisak/VIctory/pull/1478. On an RTX 5070 the two give identical results on the 9 ops they share. This PR adds clamped `v_add3_u32`, and #1478 adds signed `v_add/sub_nc_i32` with clamp.
sinan-cmyk commented 2026-11-24 21:03:38 +00:00 (Migrated from github.com)

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


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

Result: no count change against main.

Offline. I merged head 38ddebf 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 121, 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:7143987664 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1398#issuecomment-7143987664). Originally posted by [sinan-cmyk](https://github.com/sinan-cmyk) on 2026-11-24T20:44:12Z. --- ### Offline measurement on a retail dump, and the shader tests on Ada **Result: no count change against `main`.** **Offline.** I merged head 38ddebf 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 121, 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).
Marvisak commented 2026-11-25 02:06:02 +00:00 (Migrated from github.com)

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


Automated triage for the maintainers.

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


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149532086 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1398#issuecomment-7149532086). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:25:32Z. --- <!-- victory-triage v1 head=38ddebf954267e2ae46dce6f3f3dbfdb622219ee verdict=needs-rebase at=2026-11-25T01:25:30Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-shader/src/emit/lower.rs`). It will be reviewed once it is rebased onto the current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1398:upstream-pr/1398'
git switch 'upstream-pr/1398'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1398'
git switch 'upstream-pr/1398'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1398'
git switch 'upstream-pr/1398'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1398'
git switch 'main'
git merge --squash 'upstream-pr/1398'
git switch 'main'
git merge --ff-only 'upstream-pr/1398'
git switch 'main'
git merge 'upstream-pr/1398'
git push origin 'main'
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!1398
No description provided.