Shaders: honor clamp on unsigned VOP3 add and subtract (port of upstream VIctory #1398) #948

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

@⁠Moon0755 on GitHub · 2026-11-25 13:29 UTC · original
Moon0755:fix/vop3-unsigned-add-sub-clamp → main · +133 −16 · 3 files · 1 commits · closed without merging

Ports upstream Marvisak/VIctory#1398 (EatCodeSleep2), plus additional clamp guards in victory-shader's descriptor tracer and wave-prefix analysis.

Problem

On RDNA2 / Xbox Series X, VOP3 integer instructions can carry the clamp modifier (bit 15) to perform unsigned saturating arithmetic (u32::MAX on unsigned add overflow, 0 on unsigned subtract borrow) while still writing the true unclamped carry/borrow mask to sdst / VCC.

Previously, Lower::write_i rejected every integer VALU instruction with v.clamp (Err("integer output modifiers")).

What changed

  • crates/victory-shader/src/emit/lower.rs (Lower::write_i & Lower::valu_op):
    • Keep rejecting v.clamp in Lower::valu_op's int helper by default so unsupported integer ops still fail translation loudly, while allowing v.clamp through Lower::write_i.
    • Saturate v_add_co_u32, v_sub_co_u32, v_subrev_co_u32, v_add_co_ci_u32, v_sub_co_ci_u32, and v_subrev_co_ci_u32 at u32::MAX / 0 when v.clamp is set, while preserving the unclamped carry / borrow mask written to lane_bits.
    • Support v.clamp on v_add_nc_u32, v_sub_nc_u32, v_subrev_nc_u32, and v_add3_u32 (ORing both carry stages for v_add3_u32).
    • Also guard v_add_nc_u32 and v_sub_nc_u32 in wave-prefix analysis so saturating operations are not treated as modular wave sums/prefixes.
  • crates/victory-shader/src/resource.rs (Tracer::vadd & Tracer::carried):
    • Reject f.clamp when matching v_add_co_u32, v_add_nc_u32, and v_add_co_ci_u32 during descriptor address tracing so saturating arithmetic is not mistaken for a linear base+offset or 64-bit high-dword carry chain.
  • crates/victory-shader/src/emit/valu_tests.rs (vop3_unsigned_add_sub_clamp):
    • Add an end-to-end synthetic compute shader test covering all 10 instructions with clamp = false and clamp = true across edge-case inputs, independently reading back the carry/borrow mask for the 6 carry-out ops.

Verification

Verified on Windows (windows-latest + dxil.dll + D3D12 WARP) via a temporary scoped CI branch on the fork:

  • CI run: https://github.com/Moon0755/victory-fork/actions/runs/45623437448
  • 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: victory-shader 216 passed; 0 failed; 3 ignored (test emit::valu_tests::vop3_unsigned_add_sub_clamp ... ok, validated via dxil.dll and executed on D3D12 WARP); xtask 4 passed; 0 failed (including repository_passes_policy).

Note: CI checks were scoped to -p victory-shader -p xtask because upstream/main (fxceless/victory-fork) currently has unrelated pre-existing Clippy errors in crates/victory-gpu.

— Neon

**@⁠Moon0755** on GitHub · 2026-11-25 13:29 UTC · [original](https://github.com/fxceless/victory-fork/pull/948) `Moon0755:fix/vop3-unsigned-add-sub-clamp` → `main` · +133 −16 · 3 files · 1 commits · closed without merging Ports upstream [Marvisak/VIctory#1398](https://github.com/Marvisak/VIctory/pull/1398) (`EatCodeSleep2`), plus additional `clamp` guards in `victory-shader`'s descriptor tracer and wave-prefix analysis. ## Problem On RDNA2 / Xbox Series X, VOP3 integer instructions can carry the `clamp` modifier (bit 15) to perform unsigned saturating arithmetic (`u32::MAX` on unsigned add overflow, `0` on unsigned subtract borrow) while still writing the true unclamped carry/borrow mask to `sdst` / `VCC`. Previously, `Lower::write_i` rejected every integer VALU instruction with `v.clamp` (`Err("integer output modifiers")`). ## What changed - **`crates/victory-shader/src/emit/lower.rs` (`Lower::write_i` & `Lower::valu_op`)**: - Keep rejecting `v.clamp` in `Lower::valu_op`'s `int` helper by default so unsupported integer ops still fail translation loudly, while allowing `v.clamp` through `Lower::write_i`. - Saturate `v_add_co_u32`, `v_sub_co_u32`, `v_subrev_co_u32`, `v_add_co_ci_u32`, `v_sub_co_ci_u32`, and `v_subrev_co_ci_u32` at `u32::MAX` / `0` when `v.clamp` is set, while preserving the unclamped `carry` / borrow mask written to `lane_bits`. - Support `v.clamp` on `v_add_nc_u32`, `v_sub_nc_u32`, `v_subrev_nc_u32`, and `v_add3_u32` (ORing both carry stages for `v_add3_u32`). - Also guard `v_add_nc_u32` and `v_sub_nc_u32` in wave-prefix analysis so saturating operations are not treated as modular wave sums/prefixes. - **`crates/victory-shader/src/resource.rs` (`Tracer::vadd` & `Tracer::carried`)**: - Reject `f.clamp` when matching `v_add_co_u32`, `v_add_nc_u32`, and `v_add_co_ci_u32` during descriptor address tracing so saturating arithmetic is not mistaken for a linear base+offset or 64-bit high-dword carry chain. - **`crates/victory-shader/src/emit/valu_tests.rs` (`vop3_unsigned_add_sub_clamp`)**: - Add an end-to-end synthetic compute shader test covering all 10 instructions with `clamp = false` and `clamp = true` across edge-case inputs, independently reading back the carry/borrow mask for the 6 carry-out ops. ## Verification Verified on Windows (`windows-latest` + `dxil.dll` + D3D12 WARP) via a temporary scoped CI branch on the fork: - CI run: https://github.com/Moon0755/victory-fork/actions/runs/45623437448 - `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`: `victory-shader` `216 passed; 0 failed; 3 ignored` (`test emit::valu_tests::vop3_unsigned_add_sub_clamp ... ok`, validated via `dxil.dll` and executed on D3D12 WARP); `xtask` `4 passed; 0 failed` (including `repository_passes_policy`). Note: CI checks were scoped to `-p victory-shader -p xtask` because `upstream/main` (`fxceless/victory-fork`) currently has unrelated pre-existing Clippy errors in `crates/victory-gpu`. — Neon
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:54:27 +00:00
Author
Owner

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

A hardware run of this PR's new test: vop3_unsigned_add_sub_clamp 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:00 UTC · [original](https://github.com/fxceless/victory-fork/pull/948#issuecomment-7170316509) A hardware run of this PR's new test: `vop3_unsigned_add_sub_clamp` 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:50 +00:00

Pull request closed

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