shader: saturate clamped unsigned integer sums and differences (#365) #366

Merged
tiagonogueiraa-bit merged 1 commit from upstream-pr/366 into main 2026-11-27 16:04:48 +00:00
tiagonogueiraa-bit commented 2026-11-27 14:16:53 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by tiagonogueiraa-bit on 2026-11-27T14:16:53Z.


Refs #365

Shape. write_i refused every integer result with the clamp bit as "integer output modifiers". In the title, all 8 such compute shaders in my corpus use v_sub_nc_u32 vN, vA, vB clamp, with the result going to LDS.

Change. In Lower::integer, the clamp bit on v_add_nc_u32, v_sub_nc_u32 and v_subrev_nc_u32 (SATURATING) is unsigned saturation:

  • a sum that wraps (r <u a) gives 0xffffffff;
  • a difference below zero (a <u b) gives 0.

LLVM's AMDGPU backend selects uaddsat and usubsat to exactly these instructions with clamp. write_i lets the clamp through for those three only. Clamp on other integer operations (signed _i32, v_mad_u32_u24 and others) and omod stay refused, and unclamped instructions are unchanged.

By-output corpus sweep. 12,020 containers from my own boot-to-menus run (Series S), native host options, main 905e7aa against this head:

Stage Gained Lost Changed Other reason now
compute 8 0 0 0
pixel 0 0 0 0
vertex 0 0 0 0

Gained: 0752fed8675075be, 284a5f5796ba7abc, 3229462a630a0353, 35b422c6b9bd5693, b9dfec7f657e3196, bbe7ce7f482f36ab, f056aa31512ea1eb, f539346687541f48.

Test. split_wave_tests::clamped_unsigned_sums_and_differences_saturate (victory-gpu, on Metal with MSC 4.0 beta 3). Over 32 lanes:

  • lane i stores i - 5 clamped at 0;
  • in a second program, 0xfffffff0 + i clamped at 0xffffffff.

On main it fails at translation.

Checks.

  • cargo test -p victory-shader -p victory-hle -p victory -p victory-gpu -p victory-metal -p xtask: 955 passed, 0 failed, 10 ignored.
  • Windows clippy -D warnings: clean.
  • cargo xtask policy: ok.

docs/native-host.md has a short section ("Clamped integer sums and differences"). @Olima34, for the board.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5528124698 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/366). Originally posted by [tiagonogueiraa-bit](https://github.com/tiagonogueiraa-bit) on 2026-11-27T14:16:53Z. --- Refs #365 **Shape.** `write_i` refused every integer result with the clamp bit as "integer output modifiers". In the title, all 8 such compute shaders in my corpus use `v_sub_nc_u32 vN, vA, vB clamp`, with the result going to LDS. **Change.** In `Lower::integer`, the clamp bit on `v_add_nc_u32`, `v_sub_nc_u32` and `v_subrev_nc_u32` (`SATURATING`) is unsigned saturation: - a sum that wraps (`r <u a`) gives `0xffffffff`; - a difference below zero (`a <u b`) gives `0`. LLVM's AMDGPU backend selects `uaddsat` and `usubsat` to exactly these instructions with clamp. `write_i` lets the clamp through for those three only. Clamp on other integer operations (signed `_i32`, `v_mad_u32_u24` and others) and `omod` stay refused, and unclamped instructions are unchanged. **By-output corpus sweep.** 12,020 containers from my own boot-to-menus run (Series S), native host options, main `905e7aa` against this head: | Stage | Gained | Lost | Changed | Other reason now | |---|---|---|---|---| | compute | 8 | 0 | 0 | 0 | | pixel | 0 | 0 | 0 | 0 | | vertex | 0 | 0 | 0 | 0 | Gained: `0752fed8675075be`, `284a5f5796ba7abc`, `3229462a630a0353`, `35b422c6b9bd5693`, `b9dfec7f657e3196`, `bbe7ce7f482f36ab`, `f056aa31512ea1eb`, `f539346687541f48`. **Test.** `split_wave_tests::clamped_unsigned_sums_and_differences_saturate` (`victory-gpu`, on Metal with MSC 4.0 beta 3). Over 32 lanes: - lane `i` stores `i - 5` clamped at 0; - in a second program, `0xfffffff0 + i` clamped at `0xffffffff`. On main it fails at translation. **Checks.** - `cargo test -p victory-shader -p victory-hle -p victory -p victory-gpu -p victory-metal -p xtask`: 955 passed, 0 failed, 10 ignored. - Windows clippy `-D warnings`: clean. - `cargo xtask policy`: ok. `docs/native-host.md` has a short section ("Clamped integer sums and differences"). @Olima34, for the board.
LakshanSivakumar commented 2026-11-27 16:05:13 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T16:05:13Z.


Merged in be7dc1b5 with the other shader PRs from tonight. Ran the tests (native arm64), Windows clippy, the policy check and a release build on the merged tree, and swept my full corpus of 21,600 shaders before and after: 0 lost, 352 newly translated, every changed output accounted for. I have not run the game on it yet. Thanks.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7222839133 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/366#issuecomment-7222839133). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-27T16:05:13Z. --- Merged in be7dc1b5 with the other shader PRs from tonight. Ran the tests (native arm64), Windows clippy, the policy check and a release build on the merged tree, and swept my full corpus of 21,600 shaders before and after: 0 lost, 352 newly translated, every changed output accounted for. I have not run the game on it yet. Thanks.
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
LakshanSivakumar/Native-Victory-macOS!366
No description provided.