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

Manually merged
github merged 1 commit from github-bot/gh-366 into main 2026-11-27 16:13:15 +00:00 AGit
Owner

@⁠tiagonogueiraa-bit on GitHub · 2026-11-27 14:16 UTC · original
tiagonogueiraa-bit:shader-integer-clamp → main · +62 −2 · 3 files · 1 commits · merged 2026-11-27 16:04 UTC

Refs #368

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.

**@⁠tiagonogueiraa-bit** on GitHub · 2026-11-27 14:16 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/366) `tiagonogueiraa-bit:shader-integer-clamp` → `main` · +62 −2 · 3 files · 1 commits · merged 2026-11-27 16:04 UTC Refs #368 **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.
The VOP3 clamp bit on v_add_nc_u32, v_sub_nc_u32 and v_subrev_nc_u32
saturates as an unsigned number. LLVM's AMDGPU backend selects uaddsat
and usubsat to these with clamp. Every clamped integer operation was
refused as "integer output modifiers". Lower::integer now selects
0xffffffff for a sum that wraps and 0 for a difference below zero. Clamp
on other integer operations, and omod, stays refused.

Corpus sweep (12,020 shaders, main 905e7aa): 8 compute shaders gained,
no regressions, no changed outputs.

Refs #365
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 14:22:15 +00:00
github-bot referenced this pull request from a commit 2026-11-27 16:13:14 +00:00
github manually merged commit f45a05dc54 into main 2026-11-27 16:13:15 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-27 16:05 UTC · original

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.

**@⁠LakshanSivakumar** on GitHub · 2026-11-27 16:05 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/366#issuecomment-7222839133) 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.
This discussion has been locked. Commenting is limited to contributors.
No description provided.