Shaders: support saturating clamp on packed 16-bit integer ops (v_pk_{add,sub,mad}_{u16,i16}) #1142

Open
github-bot wants to merge 1 commit from github-bot/gh-1142 into main AGit
Owner

@⁠ESAT586 on GitHub · 2026-11-24 16:10 UTC · original
ESAT586:shader/pk-int16-clamp → main · +149 −2 · 2 files · 1 commits · open

AI Disclosure — Generated & Verified by Gemini 4 Neon (High Mode) (Not Written by a Human Engineer)
This diagnosis, RDNA2 ISA implementation, and synthetic GPU/WARP test suite were authored and verified end-to-end by Gemini 4 Neon (High Mode), submitted transparently on behalf of @ESAT586. All builds, clippy checks, DXIL validation, and GPU/WARP tests below were executed locally on real hardware (NVIDIA GeForce RTX 3060 Ti, Windows 11, Rust 1.98.1). No game data was used.

What's wrong

In crates/victory-shader/src/emit/lower.rs (packed16, line 4788), any packed 16-bit integer instruction with v.clamp == true fails lowering with Unsupported { what: "modifiers on a packed integer operation" }.

Under the AMD RDNA2 ISA (VOP3P), clamp = 1 is the saturating integer modifier for v_pk_add_u16, v_pk_sub_u16, v_pk_mad_u16, v_pk_add_i16, v_pk_sub_i16, and v_pk_mad_i16, clamping each 16-bit lane to [0, 65535] (unsigned) or [-32768, 32767] (signed).

Fix

  • Allow v.clamp in Lower::packed16 specifically for the 6 saturating packed integer ops (add_u16, sub_u16, mad_u16, add_i16, sub_i16, mad_i16); non-saturating packed integer ops (mul_lo_u16, shifts, min/max) continue to reject v.clamp.
  • Clamp the 32-bit lane result before the final & 0xFFFF mask:
    • add_u16 / mad_u16: u16::MAX * u16::MAX + u16::MAX = 4,294,901,760 <= u32::MAX (never wraps u32), so r >u 0xFFFF ? 0xFFFF : r is exact.
    • sub_u16: operands are in [0, 65535], so r is in [-65535, 65535] and r <s 0 ? 0 : r catches underflow exactly.
    • add_i16 / sub_i16 / mad_i16: operands are sign-extended to i32 (|r| <= 32768 * 32768 + 32768 < 2^31, never wraps i32), so signed clamp to [-32768, 32767] is exact.
  • Add emit::valu_tests::v_pk_int16_clamp_saturates_as_rdna2 in crates/victory-shader/src/emit/valu_tests.rs testing all 6 instructions with clamp = 1 across overflow, underflow, max-product, and in-range lanes.

Verification (Real GPU + WARP + CI Gates)

Tested on Windows 11, NVIDIA GeForce RTX 3060 Ti (driver 32.0.16.1656), Rust 1.98.1:

  • cargo test -p victory-shader --lib v_pk_int16_clamp_saturates_as_rdna2: ok (dxil.dll validator + RTX 3060 Ti hardware + Microsoft WARP)
  • cargo test -p victory-shader --locked: 121 passed, 0 failed, 1 ignored
  • cargo clippy --workspace --all-targets --locked -- -D warnings: pass (0 warnings)
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: pass (0 warnings)
  • cargo xtask policy: asset policy: 154 files ok

Scope & Non-Overlap

  • Checked open PRs and issues before writing: no open PR or issue covers v_pk_{add,sub,mad}_{u16,i16} saturating clamp.
  • Not tested: against retail title shader packages (synthetic fixtures only, no game data).
**@⁠ESAT586** on GitHub · 2026-11-24 16:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/1142) `ESAT586:shader/pk-int16-clamp` → `main` · +149 −2 · 2 files · 1 commits · open > **AI Disclosure — Generated & Verified by Gemini 4 Neon (High Mode) (Not Written by a Human Engineer)** > This diagnosis, RDNA2 ISA implementation, and synthetic GPU/WARP test suite were authored and verified end-to-end by **Gemini 4 Neon (High Mode)**, submitted transparently on behalf of `@ESAT586`. All builds, clippy checks, DXIL validation, and GPU/WARP tests below were executed locally on real hardware (`NVIDIA GeForce RTX 3060 Ti`, Windows 11, Rust `1.98.1`). **No game data was used.** ## What's wrong In `crates/victory-shader/src/emit/lower.rs` (`packed16`, line 4788), any packed 16-bit integer instruction with `v.clamp == true` fails lowering with `Unsupported { what: "modifiers on a packed integer operation" }`. Under the AMD RDNA2 ISA (VOP3P), `clamp = 1` is the saturating integer modifier for `v_pk_add_u16`, `v_pk_sub_u16`, `v_pk_mad_u16`, `v_pk_add_i16`, `v_pk_sub_i16`, and `v_pk_mad_i16`, clamping each 16-bit lane to `[0, 65535]` (unsigned) or `[-32768, 32767]` (signed). ## Fix - Allow `v.clamp` in `Lower::packed16` specifically for the 6 saturating packed integer ops (`add_u16`, `sub_u16`, `mad_u16`, `add_i16`, `sub_i16`, `mad_i16`); non-saturating packed integer ops (`mul_lo_u16`, shifts, `min`/`max`) continue to reject `v.clamp`. - Clamp the 32-bit lane result before the final `& 0xFFFF` mask: - `add_u16` / `mad_u16`: `u16::MAX * u16::MAX + u16::MAX = 4,294,901,760 <= u32::MAX` (never wraps `u32`), so `r >u 0xFFFF ? 0xFFFF : r` is exact. - `sub_u16`: operands are in `[0, 65535]`, so `r` is in `[-65535, 65535]` and `r <s 0 ? 0 : r` catches underflow exactly. - `add_i16` / `sub_i16` / `mad_i16`: operands are sign-extended to `i32` (`|r| <= 32768 * 32768 + 32768 < 2^31`, never wraps `i32`), so signed clamp to `[-32768, 32767]` is exact. - Add `emit::valu_tests::v_pk_int16_clamp_saturates_as_rdna2` in `crates/victory-shader/src/emit/valu_tests.rs` testing all 6 instructions with `clamp = 1` across overflow, underflow, max-product, and in-range lanes. ## Verification (Real GPU + WARP + CI Gates) Tested on Windows 11, **NVIDIA GeForce RTX 3060 Ti** (driver `32.0.16.1656`), Rust `1.98.1`: - `cargo test -p victory-shader --lib v_pk_int16_clamp_saturates_as_rdna2`: **ok** (`dxil.dll` validator + RTX 3060 Ti hardware + Microsoft WARP) - `cargo test -p victory-shader --locked`: **121 passed, 0 failed, 1 ignored** - `cargo clippy --workspace --all-targets --locked -- -D warnings`: **pass (0 warnings)** - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: **pass (0 warnings)** - `cargo xtask policy`: **`asset policy: 154 files ok`** ## Scope & Non-Overlap - Checked open PRs and issues before writing: no open PR or issue covers `v_pk_{add,sub,mad}_{u16,i16}` saturating `clamp`. - **Not tested:** against retail title shader packages (synthetic fixtures only, no game data).
AI-generated & verified with Gemini 4 Neon (High Mode) — not written by a human engineer.

`packed16` in `lower.rs` previously rejected `v.clamp` on all packed 16-bit
integer instructions with `Err("modifiers on a packed integer operation")`.

Under the AMD RDNA2 ISA (VOP3P), `clamp` on `v_pk_add_u16`, `v_pk_sub_u16`,
`v_pk_mad_u16`, `v_pk_add_i16`, `v_pk_sub_i16`, and `v_pk_mad_i16` saturates
each 16-bit lane to `[0, 65535]` (unsigned) or `[-32768, 32767]` (signed).
Because `packed16` computes each lane in 32 bits (`u16*u16+u16 <= u32::MAX`,
`i16*i16+i16` fits in `i32`), saturation is exact in 32-bit integer compares
before masking to 16 bits. Non-saturating packed integer ops keep rejecting
`clamp`.

Verified with synthetic GPU + WARP + dxil.dll tests in `valu_tests.rs` on
NVIDIA GeForce RTX 3060 Ti (Windows 11, Rust 1.98.1). No game data used.

Co-Authored-By: Gemini 4 Neon (High Mode) <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:20:59 +00:00
github-bot force-pushed github-bot/gh-1142 from f76cf2bbce to de111550b7 2026-11-26 06:29:54 +00:00 Compare
Author
Owner

@⁠ghost on GitHub · 2026-11-24 16:35 UTC · original

Ran on RDNA2 hardware through vkd3d-proton on a Steam Deck (not native Windows): AMD Van Gogh, RADV 26.1.99, SteamOS 3.8.28, Proton 11.0-3 (wine-11.0) with its vkd3d-proton 212991fc. Windows MSVC test binary (rustc 1.100.0) built from head bf873ff on main ffe310e. Your report lists NVIDIA and WARP, so this adds AMD hardware.

  • v_pk_int16_clamp_saturates_as_rdna2 passes: v_pk_{add,sub,mad}_{u16,i16} with clamp, per half, including the 65535*65535+65535 case.
  • victory-shader lib tests: 120 passed, 1 failed, 1 ignored (main on the same host: 119/1/1). The one failure, stub_pipeline_is_created, fails on main too (likely vkd3d-proton accepting the zero DXIL digest the test expects refused; environment, not this PR).
  • No hangs. A pass means the translated DXIL gives these values on RDNA2 through vkd3d-proton/RADV; it isn't a console measurement.

Not tested: native Windows AMD drivers; WARP (this setup has none, so the WARP leg ran on RADV too); whether the new test fails without the fix on this host.

AI-assisted (Claude Code, Claude Opus 5.5); no game data.

**@⁠ghost** on GitHub · 2026-11-24 16:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/1142#issuecomment-7137893732) Ran on RDNA2 hardware through vkd3d-proton on a Steam Deck (not native Windows): AMD Van Gogh, RADV 26.1.99, SteamOS 3.8.28, Proton 11.0-3 (wine-11.0) with its vkd3d-proton 212991fc. Windows MSVC test binary (rustc 1.100.0) built from head `bf873ff` on main `ffe310e`. Your report lists NVIDIA and WARP, so this adds AMD hardware. - `v_pk_int16_clamp_saturates_as_rdna2` passes: v_pk_{add,sub,mad}_{u16,i16} with clamp, per half, including the 65535*65535+65535 case. - victory-shader lib tests: 120 passed, 1 failed, 1 ignored (main on the same host: 119/1/1). The one failure, `stub_pipeline_is_created`, fails on main too (likely vkd3d-proton accepting the zero DXIL digest the test expects refused; environment, not this PR). - No hangs. A pass means the translated DXIL gives these values on RDNA2 through vkd3d-proton/RADV; it isn't a console measurement. Not tested: native Windows AMD drivers; WARP (this setup has none, so the WARP leg ran on RADV too); whether the new test fails without the fix on this host. AI-assisted (Claude Code, Claude Opus 5.5); no game data.
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 18:01 UTC · original

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at bf873ff: cargo test -p victory-shader passes, 121 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

**@⁠Zipettte** on GitHub · 2026-11-24 18:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/1142#issuecomment-7140043723) Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at bf873ff: `cargo test -p victory-shader` passes, 121 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:37 UTC · original · edited 2026-11-26 05:24 UTC

Automated triage for the maintainers.

Merge candidate (recommending a look, not approving). Thanks for the rebase: the branch now merges cleanly onto main. In crates/victory-shader/src/emit/lower.rs Lower::packed16, the clamp bit is now accepted only for v_pk_{add,sub,mad}_{u16,i16} and saturates each half before the 16-bit mask; the other packed integer ops still refuse it. I checked the range arguments (unsigned mad peaks at 4,294,901,760, below u32::MAX; signed results stay inside i32) and the VOP3P encoding and opcodes in the new valu_tests.rs test against the RDNA2 ISA. Saturation via clamp on these ops matches the ISA document the PR cites. CI check passes on de11155.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/1142#issuecomment-7148704687) · edited 2026-11-26 05:24 UTC <!-- victory-triage v1 head=de111550b740d5d81c2fb4f6231ab7935c3c5919 verdict=merge-candidate at=2026-11-26T05:25:30Z --> Automated triage for the maintainers. **Merge candidate** (recommending a look, not approving). Thanks for the rebase: the branch now merges cleanly onto `main`. In `crates/victory-shader/src/emit/lower.rs` `Lower::packed16`, the `clamp` bit is now accepted only for `v_pk_{add,sub,mad}_{u16,i16}` and saturates each half before the 16-bit mask; the other packed integer ops still refuse it. I checked the range arguments (unsigned mad peaks at 4,294,901,760, below `u32::MAX`; signed results stay inside `i32`) and the VOP3P encoding and opcodes in the new `valu_tests.rs` test against the RDNA2 ISA. Saturation via `clamp` on these ops matches the ISA document the PR cites. CI `check` passes on `de11155`. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠ESAT586 on GitHub · 2026-11-25 13:01 UTC · original

Rebased onto \main\ (\940d32f) at $sha. Verified on RTX 3060 Ti + WARP: \ _pk_int16_clamp_saturates_as_rdna2\ passed, strict Clippy (--all-targets\ and --all-features) clean, \cargo xtask policy\ ok.

**@⁠ESAT586** on GitHub · 2026-11-25 13:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/1142#issuecomment-7163172674) Rebased onto \main\ (\940d32f\) at \$sha\. Verified on RTX 3060 Ti + WARP: \ _pk_int16_clamp_saturates_as_rdna2\ passed, strict Clippy (\--all-targets\ and \--all-features\) clean, \cargo xtask policy\ ok.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 16:02 UTC · original · approved these changes

The saturation ranges and signedness checks look right for all six packed integer ops (v_pk_{add,sub,mad}_{u16,i16}): mad_u16 peaks at 0xffff_0000 (fitting in u32 so IUGT catches overflow without wrap), sub_u16 spans [-65535, 65535] (so SLT against 0 catches underflow), and all three signed ops stay strictly within i32 before clamping to [i16::MIN, i16::MAX]. Source OPSEL/OPSEL_HI selection and wrapping when clamp is clear remain intact, and mul_lo_u16/min/max/shifts still reject clamp.

As optional follow-up coverage, a non-default OPSEL/OPSEL_HI test and a CLAMP=0 wrap-around case would pin those paths as well.

Local verification at de111550 (and merged cleanly onto origin/main 63bcca43, emit/lower.rs = 522,255 B, 2,033 B under the 512 KiB cap): xtask policy (191 files ok), cargo clippy -p victory-shader --all-targets (default and --all-features), emit::valu_tests on both RTX 5090 and WARP (36/36), and v_pk_int16_clamp_saturates_as_rdna2 on RTX 5090 and WARP all passed.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 16:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/1142#pullrequestreview-6077206003) · **approved these changes** The saturation ranges and signedness checks look right for all six packed integer ops (`v_pk_{add,sub,mad}_{u16,i16}`): `mad_u16` peaks at `0xffff_0000` (fitting in `u32` so `IUGT` catches overflow without wrap), `sub_u16` spans `[-65535, 65535]` (so `SLT` against `0` catches underflow), and all three signed ops stay strictly within `i32` before clamping to `[i16::MIN, i16::MAX]`. Source `OPSEL`/`OPSEL_HI` selection and wrapping when `clamp` is clear remain intact, and `mul_lo_u16`/min/max/shifts still reject `clamp`. As optional follow-up coverage, a non-default `OPSEL`/`OPSEL_HI` test and a `CLAMP=0` wrap-around case would pin those paths as well. Local verification at `de111550` (and merged cleanly onto `origin/main` `63bcca43`, `emit/lower.rs` = `522,255` B, `2,033` B under the 512 KiB cap): `xtask policy` (`191 files ok`), `cargo clippy -p victory-shader --all-targets` (default and `--all-features`), `emit::valu_tests` on both RTX 5090 and WARP (`36/36`), and `v_pk_int16_clamp_saturates_as_rdna2` on RTX 5090 and WARP all passed.
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 19:50 UTC · original · approved these changes

LGTM — verified on RTX 5090 and VICTORY_WARP=1 (Windows / D3D12) against origin/main (63bcca4).

  • Supporting saturating CLAMP on packed 16-bit integer ops (v_pk_{add,sub,mad}_{u16,i16}) matches RDNA2 semantics cleanly on both discrete NVIDIA hardware and WARP.
  • cargo test -p victory-shader --lib emit::valu_tests::v_pk_int16_clamp_saturates_as_rdna2 -- --test-threads=1: passed on RTX 5090 and VICTORY_WARP=1.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 19:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/1142#pullrequestreview-6079770721) · **approved these changes** LGTM — verified on RTX 5090 and `VICTORY_WARP=1` (Windows / D3D12) against `origin/main` (`63bcca4`). - Supporting saturating `CLAMP` on packed 16-bit integer ops (`v_pk_{add,sub,mad}_{u16,i16}`) matches RDNA2 semantics cleanly on both discrete NVIDIA hardware and WARP. - `cargo test -p victory-shader --lib emit::valu_tests::v_pk_int16_clamp_saturates_as_rdna2 -- --test-threads=1`: passed on RTX 5090 and `VICTORY_WARP=1`. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1142/head:github-bot/gh-1142'
git switch 'github-bot/gh-1142'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1142'
git switch 'github-bot/gh-1142'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1142'
git switch 'github-bot/gh-1142'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1142'
git switch 'main'
git merge --squash 'github-bot/gh-1142'
git switch 'main'
git merge --ff-only 'github-bot/gh-1142'
git switch 'main'
git merge 'github-bot/gh-1142'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1142
No description provided.