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

Closed
github-bot wants to merge 1 commit from github-bot/gh-1042 into main AGit
Owner

@⁠Moon0755 on GitHub · 2026-11-25 17:13 UTC · original
Moon0755:fix/packed16-saturating-clamp → main · +149 −2 · 2 files · 1 commits · closed without merging

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

**@⁠Moon0755** on GitHub · 2026-11-25 17:13 UTC · [original](https://github.com/fxceless/victory-fork/pull/1042) `Moon0755:fix/packed16-saturating-clamp` → `main` · +149 −2 · 2 files · 1 commits · closed without merging 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
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:56:31 +00:00
Author
Owner

@⁠avt0m4t48 on GitHub · 2026-11-25 17:20 UTC · original

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.

**@⁠avt0m4t48** on GitHub · 2026-11-25 17:20 UTC · [original](https://github.com/fxceless/victory-fork/pull/1042#issuecomment-7169361393) 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.
Author
Owner

@⁠HAL9000RELOADED on GitHub · 2026-11-25 18:01 UTC · original

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: #1030

**@⁠HAL9000RELOADED** on GitHub · 2026-11-25 18:01 UTC · [original](https://github.com/fxceless/victory-fork/pull/1042#issuecomment-7170320340) 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://git.gtahub.st/github/fxceless--victory-fork/pulls/1030
github-bot closed this pull request 2026-11-26 11:35:53 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.