Shaders: support saturating clamp on packed 16-bit integer ops (port of upstream VIctory #1142) #1040

Closed
Moon0755 wants to merge 1 commit from upstream-pr/1042 into main
Moon0755 commented 2026-11-25 17:13:39 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Moon0755 on 2026-11-25T17:13:39Z.


Port of upstream Marvisak/VIctory#1142 (originally authored by @ESAT586).

Summary

Adds saturating clamp support for RDNA2 packed 16-bit integer VOP3P operations: 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.

Previously, Lower::packed16 rejected v.clamp on all packed integer operations (Err("modifiers on a packed integer operation")), preventing shaders using saturating 16-bit packed integer arithmetic from lowering.

Changes

  • crates/victory-shader/src/emit/lower.rs:
    • Allowed v.clamp in Lower::packed16 specifically for "add_u16" | "sub_u16" | "mad_u16" | "add_i16" | "sub_i16" | "mad_i16" (while continuing to reject omod, neg/neg_hi, and clamp on non-saturating packed integer ops).
    • Applied per-lane saturation before masking to 16 bits:
      • add_u16 / mad_u16: clamp unsigned values > 0xFFFF to 0xFFFF (since 65535 * 65535 + 65535 <= u32::MAX, the 32-bit intermediate never wraps).
      • sub_u16: clamp signed < 0 underflow to 0.
      • add_i16 / sub_i16 / mad_i16: clamp sign-extended 32-bit intermediates to [i16::MIN, i16::MAX] (|r| <= 32768 * 32768 + 32768 < 2^31, so i32 never wraps).
  • crates/victory-shader/src/emit/valu_tests.rs: added vop3p encoding helper and D3D12 WARP regression test v_pk_int16_clamp_saturates_as_rdna2 testing overflow, underflow, and in-range lanes across all 6 instructions (including u16::MAX * u16::MAX + u16::MAX and i16::MIN * i16::MIN + i16::MAX).

Verification

Verified on windows-latest via GitHub Actions (run 45661896173):

  • cargo clippy -p victory-shader -p xtask --all-targets --locked -- -D warnings — passed
  • cargo clippy -p victory-shader -p xtask --all-targets --all-features --locked -- -D warnings — passed
  • cargo test -p victory-shader -p xtask --locked -- --nocapture — 216 passed; 0 failed; 3 ignored (including v_pk_int16_clamp_saturates_as_rdna2 executed on D3D12 WARP, plus xtask 4 passed, repository_passes_policy passed)

— Neon

<!-- greyfinch-sync:fxceless/victory-fork:issue:6609750571 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1042). Originally posted by [Moon0755](https://github.com/Moon0755) on 2026-11-25T17:13:39Z. --- Port of upstream `Marvisak/VIctory#1142` (originally authored by `@ESAT586`). ### Summary Adds saturating `clamp` support for RDNA2 packed 16-bit integer VOP3P operations: `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`. Previously, `Lower::packed16` rejected `v.clamp` on all packed integer operations (`Err("modifiers on a packed integer operation")`), preventing shaders using saturating 16-bit packed integer arithmetic from lowering. ### Changes - `crates/victory-shader/src/emit/lower.rs`: - Allowed `v.clamp` in `Lower::packed16` specifically for `"add_u16" | "sub_u16" | "mad_u16" | "add_i16" | "sub_i16" | "mad_i16"` (while continuing to reject `omod`, `neg`/`neg_hi`, and `clamp` on non-saturating packed integer ops). - Applied per-lane saturation before masking to 16 bits: - `add_u16` / `mad_u16`: clamp unsigned values `> 0xFFFF` to `0xFFFF` (since `65535 * 65535 + 65535 <= u32::MAX`, the 32-bit intermediate never wraps). - `sub_u16`: clamp signed `< 0` underflow to `0`. - `add_i16` / `sub_i16` / `mad_i16`: clamp sign-extended 32-bit intermediates to `[i16::MIN, i16::MAX]` (`|r| <= 32768 * 32768 + 32768 < 2^31`, so `i32` never wraps). - `crates/victory-shader/src/emit/valu_tests.rs`: added `vop3p` encoding helper and D3D12 WARP regression test `v_pk_int16_clamp_saturates_as_rdna2` testing overflow, underflow, and in-range lanes across all 6 instructions (including `u16::MAX * u16::MAX + u16::MAX` and `i16::MIN * i16::MIN + i16::MAX`). ### Verification Verified on `windows-latest` via GitHub Actions ([run 45661896173](https://github.com/Moon0755/victory-fork/actions/runs/45661896173)): - `cargo clippy -p victory-shader -p xtask --all-targets --locked -- -D warnings` — passed - `cargo clippy -p victory-shader -p xtask --all-targets --all-features --locked -- -D warnings` — passed - `cargo test -p victory-shader -p xtask --locked -- --nocapture` — `216 passed; 0 failed; 3 ignored` (including `v_pk_int16_clamp_saturates_as_rdna2` executed on D3D12 WARP, plus `xtask` `4 passed`, `repository_passes_policy` passed) — Neon
avt0m4t48 commented 2026-11-25 18:05:11 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by avt0m4t48 on 2026-11-25T17:20:26Z.


Independent synthetic shader check on base ca8829f and the GitHub test merge aca45c3 for head e0e993d (head and merge have identical source trees).

  • The base refused all 24 clamped shaders. The test merge emitted all 24, covering signed and unsigned packed add, sub and MAD with conventional, swapped and mixed operand half selectors.
  • I evaluated the integer instructions feeding each emitted UAV store on the CPU and compared the packed result against an independent mathematical reference. All 72,768 comparisons matched across the clamped and unclamped variants, using boundary and seeded random inputs. Both output halves covered every applicable saturation direction and in-range results.
  • All 26 shared DXBC outputs were byte-identical, including two packed-f16 controls. Both revisions still refused clamp on packed multiply, signed max and unsigned min and refused integer negation on either half.
  • Both Linux shader library suites passed (171 passed, 2 ignored each). The Windows MSVC cross-target shader check passed with --all-targets --locked, including compilation of the WARP test.

The CPU evaluation covered the emitted integer dataflow. I did not independently run WARP, physical GPU readback, or the Microsoft DXIL validator.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7169361393 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1042#issuecomment-7169361393). Originally posted by [avt0m4t48](https://github.com/avt0m4t48) on 2026-11-25T17:20:26Z. --- Independent synthetic shader check on base `ca8829f` and the GitHub test merge `aca45c3` for head `e0e993d` (head and merge have identical source trees). - The base refused all 24 clamped shaders. The test merge emitted all 24, covering signed and unsigned packed add, sub and MAD with conventional, swapped and mixed operand half selectors. - I evaluated the integer instructions feeding each emitted UAV store on the CPU and compared the packed result against an independent mathematical reference. All **72,768** comparisons matched across the clamped and unclamped variants, using boundary and seeded random inputs. Both output halves covered every applicable saturation direction and in-range results. - All 26 shared DXBC outputs were byte-identical, including two packed-f16 controls. Both revisions still refused clamp on packed multiply, signed max and unsigned min and refused integer negation on either half. - Both Linux shader library suites passed (171 passed, 2 ignored each). The Windows MSVC cross-target shader check passed with `--all-targets --locked`, including compilation of the WARP test. The CPU evaluation covered the emitted integer dataflow. I did not independently run WARP, physical GPU readback, or the Microsoft DXIL validator.
HAL9000RELOADED commented 2026-11-25 18:05:15 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by HAL9000RELOADED on 2026-11-25T18:01:01Z.


A hardware run of this PR's new test: v_pk_int16_clamp_saturates_as_rdna2 passes on a Radeon RX 7900 XTX (RDNA3), Windows 10 22H2, with #402's Agility SDK runtime. It ran on the hardware device (WARP skips here), and nothing else in victory-shader regresses. The setup, and the same run for eight related ports, are in one table on #1030: https://github.com/fxceless/victory-fork/pull/1030#issuecomment-7170315986

<!-- greyfinch-sync:fxceless/victory-fork:comment:7170320340 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1042#issuecomment-7170320340). Originally posted by [HAL9000RELOADED](https://github.com/HAL9000RELOADED) on 2026-11-25T18:01:01Z. --- A hardware run of this PR's new test: `v_pk_int16_clamp_saturates_as_rdna2` passes on a Radeon RX 7900 XTX (RDNA3), Windows 10 22H2, with #402's Agility SDK runtime. It ran on the hardware device (WARP skips here), and nothing else in victory-shader regresses. The setup, and the same run for eight related ports, are in one table on #1030: https://github.com/fxceless/victory-fork/pull/1030#issuecomment-7170315986
upstream-sync closed this pull request 2026-11-26 12:07:13 +00:00

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
fxceless/victory-fork!1040
No description provided.