Skip to content

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

Open
ESAT586 wants to merge 1 commit into
Marvisak:mainfrom
ESAT586:shader/pk-int16-clamp
Open

ESAT586 wants to merge 1 commit into
Marvisak:mainfrom
ESAT586:shader/pk-int16-clamp

Conversation

@ESAT586

@ESAT586 ESAT586 commented Nov 24, 2026

Copy link
Copy Markdown

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).

@ghost

ghost commented Nov 24, 2026

Copy link
Copy Markdown

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.

@Zipettte

Copy link
Copy Markdown

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).

KainanP pushed a commit to KainanP/VIctory that referenced this pull request Nov 24, 2026
…fied together

Merges Marvisak#156 Marvisak#162 Marvisak#168 Marvisak#169 Marvisak#368 Marvisak#379 Marvisak#456 Marvisak#587 Marvisak#990 Marvisak#1021 Marvisak#1048 Marvisak#1052 Marvisak#1053
Marvisak#1095 Marvisak#1142 Marvisak#1190 Marvisak#1211 Marvisak#1223 Marvisak#1259 Marvisak#1293 on top of the bundle of Marvisak#1402
(which is on Marvisak#1319, on main ffe310e), each head unchanged. Hunks that
conflicted in emit/lower.rs, emit/valu_tests.rs and emit/diff_tests.rs were
merged by keeping both sides (additions next to each other); a PR was kept
only if cargo clippy -p victory-shader --all-targets -- -D warnings still
passed with it. No instruction name is handled in two places.

Marvisak#98 is not in this version: an offline measurement on a retail dump showed
it loses 31 programs that validate on Marvisak#1402.

cargo clippy --workspace --all-targets --locked -- -D warnings, with and
without --all-features, is clean and cargo test --workspace --locked passes
(381 passed, 6 ignored; main has 342 passed, 6 ignored).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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

@ESAT586

ESAT586 commented Nov 25, 2026

Copy link
Copy Markdown
Author

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.

…{add,sub,mad}_{u16,i16})

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) <noreply@google.com>

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants