xshader: v_pk_add/sub/mad_i16 with CLAMP use sat_int (wrong high lane on an NVIDIA RTX 5090) #267

Closed
github-bot wants to merge 1 commit from gh-pr/267/head into gh-pr/267/base
Owner

@⁠QuentinDanblon on GitHub · 2026-11-23 08:56 UTC · original
QuentinDanblon:fix/pk-i16-sat-hw → main · +6 −4 · 2 files · 1 commits · closed without merging

Library / area: tombox-xshader, pk_int16 in src/hlsl.rs, pk_int16_constants_and_clamp in tests/exec.rs

Exports changed (exports.txt rows): none

Source of the behaviour: no change of semantics. The saturation of v_pk_add_i16, v_pk_sub_i16 and v_pk_mad_i16 with CLAMP is the same; only its HLSL spelling changes, to the sat_int helper that 6afb54f introduced for the same driver defect.

What fails today

TOMBOX_EXEC_HW=1 cargo test -p tombox-xshader --release --test exec on an NVIDIA RTX 5090, driver 617.42, on 6e81ffc: 128 passed, 1 failed.

pk_int16_constants_and_clamp (runtime inputs): 5 mnemonics verified, 1 failing, 16 cases
#13 v_pk_sub_i16: got [ffff8000] want [7fff8000]

WARP passes the same DXIL.

Reduced to a pixel shader (z is 0 at run time, the driver cannot know it):

uint4 main(float4 p : SV_Position) : SV_Target {
    uint z = (uint)p.x;
    uint v1 = 0x7fff8000u | z, v2 = 0xffff0001u | z;
    int la = asint(v1 << 16) >> 16, lb = asint(v2 << 16) >> 16;
    int a = asint(v1) >> 16, b = asint(v2) >> 16;
    uint r = (asuint(clamp(la - lb, -32768, 32767)) & 0xffffu) | ((asuint(clamp(a - b, -32768, 32767)) & 0xffffu) << 16);
    return uint4(r, 0, 0, 0);
}
RTX 5090 WARP
as written ffff8000 7fff8000
operands mixed with ^ z or + z instead of | z 7fff8000 7fff8000
each lane returned on its own right right
saturation written with two comparisons instead of clamp 7fff8000 7fff8000
(asuint(clamp(x, -32768, 32767) + 32768) ^ 0x8000u) for the low lane 7fff8000 7fff8000
no saturation: sub, add, max, min, shift, multiply right right

So the mask after clamp(x, -32768, 32767) is dropped on the low lane and its sign bits land in the high lane, when the driver knows part of the operands. It is the defect 6afb54f describes ("the NVIDIA driver turns clamp(x, -32768, 32767) & 0xffff into a saturating convert and drops the mask") and fixed with sat_int for the unpacked 16-bit clamps, v_cvt_pk_i16_i32 and MTBUF packing. The packed add, sub and mad still used clamp().

The change

Three lines: v_pk_add_i16, v_pk_sub_i16 and v_pk_mad_i16 with CLAMP now emit sat_int(expr, 16). Shaders without these three instructions with CLAMP are emitted as before.

Two cases added to pk_int16_constants_and_clamp:

  • v_pk_add_i16 with CLAMP, low lane negative and in range, high lane positive: on the RTX 5090 it gives ffff8001 for 7ffe8001 before the change.
  • v_pk_mad_i16 with CLAMP, same shape. It passes before and after: the multiply keeps the driver from the pattern. It is there so the three instructions have a negative low lane covered.

Tests

before after
--test exec pk_ on the RTX 5090 pk_int16_constants_and_clamp fails on #13 (sub) and #16 (the new add case) 3 passed
--test exec pk_ on WARP passed 3 passed

Also after the change: --lib 116 passed, 0 failed, 3 ignored; every integration test except exec passed.

Not run after the change: the rest of tests/exec.rs. The whole of it on the RTX 5090 before the change gave the one failure above.

Not known: when exactly the driver applies the transformation. I only have the cases above. A title shader with a constant operand in a clamped packed add or sub could meet it, so the result was silently wrong on that hardware, not only in the test.

  • cargo test -p tombox-xshader passes for what is listed above (WARP and RTX 5090)
  • No game or console files and no binaries included
  • No fake success; anything not possible is left missing with a note

🤖 Generated with Claude Code

**@⁠QuentinDanblon** on GitHub · 2026-11-23 08:56 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/267) `QuentinDanblon:fix/pk-i16-sat-hw` → `main` · +6 −4 · 2 files · 1 commits · closed without merging **Library / area:** `tombox-xshader`, `pk_int16` in `src/hlsl.rs`, `pk_int16_constants_and_clamp` in `tests/exec.rs` **Exports changed (exports.txt rows):** none **Source of the behaviour:** no change of semantics. The saturation of `v_pk_add_i16`, `v_pk_sub_i16` and `v_pk_mad_i16` with CLAMP is the same; only its HLSL spelling changes, to the `sat_int` helper that `6afb54f` introduced for the same driver defect. ### What fails today `TOMBOX_EXEC_HW=1 cargo test -p tombox-xshader --release --test exec` on an NVIDIA RTX 5090, driver 617.42, on `6e81ffc`: 128 passed, 1 failed. ``` pk_int16_constants_and_clamp (runtime inputs): 5 mnemonics verified, 1 failing, 16 cases #13 v_pk_sub_i16: got [ffff8000] want [7fff8000] ``` WARP passes the same DXIL. Reduced to a pixel shader (`z` is 0 at run time, the driver cannot know it): ```hlsl uint4 main(float4 p : SV_Position) : SV_Target { uint z = (uint)p.x; uint v1 = 0x7fff8000u | z, v2 = 0xffff0001u | z; int la = asint(v1 << 16) >> 16, lb = asint(v2 << 16) >> 16; int a = asint(v1) >> 16, b = asint(v2) >> 16; uint r = (asuint(clamp(la - lb, -32768, 32767)) & 0xffffu) | ((asuint(clamp(a - b, -32768, 32767)) & 0xffffu) << 16); return uint4(r, 0, 0, 0); } ``` | | RTX 5090 | WARP | |---|---|---| | as written | `ffff8000` | `7fff8000` | | operands mixed with `^ z` or `+ z` instead of `\| z` | `7fff8000` | `7fff8000` | | each lane returned on its own | right | right | | saturation written with two comparisons instead of `clamp` | `7fff8000` | `7fff8000` | | `(asuint(clamp(x, -32768, 32767) + 32768) ^ 0x8000u)` for the low lane | `7fff8000` | `7fff8000` | | no saturation: sub, add, max, min, shift, multiply | right | right | So the mask after `clamp(x, -32768, 32767)` is dropped on the low lane and its sign bits land in the high lane, when the driver knows part of the operands. It is the defect `6afb54f` describes ("the NVIDIA driver turns clamp(x, -32768, 32767) & 0xffff into a saturating convert and drops the mask") and fixed with `sat_int` for the unpacked 16-bit clamps, `v_cvt_pk_i16_i32` and MTBUF packing. The packed add, sub and mad still used `clamp()`. ### The change Three lines: `v_pk_add_i16`, `v_pk_sub_i16` and `v_pk_mad_i16` with CLAMP now emit `sat_int(expr, 16)`. Shaders without these three instructions with CLAMP are emitted as before. Two cases added to `pk_int16_constants_and_clamp`: - `v_pk_add_i16` with CLAMP, low lane negative and in range, high lane positive: on the RTX 5090 it gives `ffff8001` for `7ffe8001` before the change. - `v_pk_mad_i16` with CLAMP, same shape. It passes before and after: the multiply keeps the driver from the pattern. It is there so the three instructions have a negative low lane covered. ### Tests | | before | after | |---|---|---| | `--test exec pk_` on the RTX 5090 | `pk_int16_constants_and_clamp` fails on #13 (sub) and #16 (the new add case) | 3 passed | | `--test exec pk_` on WARP | passed | 3 passed | Also after the change: `--lib` 116 passed, 0 failed, 3 ignored; every integration test except `exec` passed. Not run after the change: the rest of `tests/exec.rs`. The whole of it on the RTX 5090 before the change gave the one failure above. Not known: when exactly the driver applies the transformation. I only have the cases above. A title shader with a constant operand in a clamped packed add or sub could meet it, so the result was silently wrong on that hardware, not only in the test. - [x] `cargo test -p tombox-xshader` passes for what is listed above (WARP and RTX 5090) - [x] No game or console files and no binaries included - [x] No fake success; anything not possible is left `missing` with a note 🤖 Generated with [Claude Code](https://claude.com/claude-code)
On an NVIDIA RTX 5090 (driver 617.42) v_pk_sub_i16 and v_pk_add_i16 with
CLAMP returned the sign of the low lane in the high lane when part of
the operands is known at compile time: pk_int16_constants_and_clamp
#13 gave 0xffff8000 for 0x7fff8000. The driver turns
clamp(x, -32768, 32767) & 0xffff into a saturating convert and drops
the mask. 6afb54f already replaced that spelling by sat_int for the
unpacked 16-bit clamps, v_cvt_pk_i16_i32 and MTBUF packing; the packed
add, sub and mad were left on clamp().

Two cases added: v_pk_add_i16 with CLAMP and a negative low lane (fails
on the RTX 5090 before this change), and the same for v_pk_mad_i16
(passes before and after).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:16:34 +00:00
github-bot closed this pull request 2026-11-26 06:16:34 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-23 14:08 UTC · original

Applied locally on main with your authorship kept (cherry-picked, e4c1b20); the tombox-xshader suite passes in a sandbox (exec 155/155 on WARP). It ships in the next release; this PR will be closed when that is pushed. Thanks.

**@⁠omonkeyz** on GitHub · 2026-11-23 14:08 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/267#issuecomment-7103530616) Applied locally on main with your authorship kept (cherry-picked, e4c1b20); the tombox-xshader suite passes in a sandbox (exec 155/155 on WARP). It ships in the next release; this PR will be closed when that is pushed. Thanks.
Author
Owner

@⁠SchmashinPrunes on GitHub · 2026-11-24 13:20 UTC · original

I checked this on RDNA2 silicon: a gfx1036 iGPU (Ryzen 5 9600X), ROCm/HIP 5.7.1, raw wave32 kernels, so RDNA2, not gfx1013.

  • I recorded 65,536 inputs for each of v_pk_add_i16, v_pk_sub_i16 and v_pk_mad_i16, with and without CLAMP. The inputs mix random values with 0x7fff/0x8000/0xffff edge halves.
  • I also recorded the u16 forms with CLAMP.
  • Each run was done twice, with identical results.

All cases match the rule this PR implements.

  • With CLAMP, each lane computes the full-width result (for mad, the 32-bit a*b + c) and saturates it to [-32768, 32767]. For the u16 forms the range is [0, 65535].
  • Without CLAMP, the result is truncated to 16 bits.
  • The clamp changes the result in 24,399 (add), 24,640 (sub) and 55,336 (mad) of the i16 cases, so the saturation, including the mad intermediate width, is exercised.

(Measured with an AI assistant, Claude.)

**@⁠SchmashinPrunes** on GitHub · 2026-11-24 13:20 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/267#issuecomment-7132682570) I checked this on RDNA2 silicon: a gfx1036 iGPU (Ryzen 5 9600X), ROCm/HIP 5.7.1, raw wave32 kernels, so RDNA2, not gfx1013. - I recorded 65,536 inputs for each of `v_pk_add_i16`, `v_pk_sub_i16` and `v_pk_mad_i16`, with and without CLAMP. The inputs mix random values with 0x7fff/0x8000/0xffff edge halves. - I also recorded the u16 forms with CLAMP. - Each run was done twice, with identical results. All cases match the rule this PR implements. - With CLAMP, each lane computes the full-width result (for `mad`, the 32-bit `a*b + c`) and saturates it to [-32768, 32767]. For the u16 forms the range is [0, 65535]. - Without CLAMP, the result is truncated to 16 bits. - The clamp changes the result in 24,399 (add), 24,640 (sub) and 55,336 (mad) of the i16 cases, so the saturation, including the `mad` intermediate width, is exercised. (Measured with an AI assistant, Claude.)
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-25 13:07 UTC · original

Applied on main in e4c1b20 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

**@⁠omonkeyz** on GitHub · 2026-11-25 13:07 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/267#issuecomment-7163335168) Applied on main in e4c1b20 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

Pull request closed

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