xshader: v_pk_add/sub/mad_i16 with CLAMP use sat_int (wrong high lane on an NVIDIA RTX 5090) #267
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
library
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/omonkeyz--xbox-emulator!267
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/267/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 mergingLibrary / area:
tombox-xshader,pk_int16insrc/hlsl.rs,pk_int16_constants_and_clampintests/exec.rsExports changed (exports.txt rows): none
Source of the behaviour: no change of semantics. The saturation of
v_pk_add_i16,v_pk_sub_i16andv_pk_mad_i16with CLAMP is the same; only its HLSL spelling changes, to thesat_inthelper that6afb54fintroduced for the same driver defect.What fails today
TOMBOX_EXEC_HW=1 cargo test -p tombox-xshader --release --test execon an NVIDIA RTX 5090, driver 617.42, on6e81ffc: 128 passed, 1 failed.WARP passes the same DXIL.
Reduced to a pixel shader (
zis 0 at run time, the driver cannot know it):ffff80007fff8000^ zor+ zinstead of| z7fff80007fff8000clamp7fff80007fff8000(asuint(clamp(x, -32768, 32767) + 32768) ^ 0x8000u)for the low lane7fff80007fff8000So 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 defect6afb54fdescribes ("the NVIDIA driver turns clamp(x, -32768, 32767) & 0xffff into a saturating convert and drops the mask") and fixed withsat_intfor the unpacked 16-bit clamps,v_cvt_pk_i16_i32and MTBUF packing. The packed add, sub and mad still usedclamp().The change
Three lines:
v_pk_add_i16,v_pk_sub_i16andv_pk_mad_i16with CLAMP now emitsat_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_i16with CLAMP, low lane negative and in range, high lane positive: on the RTX 5090 it givesffff8001for7ffe8001before the change.v_pk_mad_i16with 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
--test exec pk_on the RTX 5090pk_int16_constants_and_clampfails on #13 (sub) and #16 (the new add case)--test exec pk_on WARPAlso after the change:
--lib116 passed, 0 failed, 3 ignored; every integration test exceptexecpassed.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-xshaderpasses for what is listed above (WARP and RTX 5090)missingwith a note🤖 Generated with Claude Code
@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.@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.
v_pk_add_i16,v_pk_sub_i16andv_pk_mad_i16, with and without CLAMP. The inputs mix random values with 0x7fff/0x8000/0xffff edge halves.All cases match the rule this PR implements.
mad, the 32-bita*b + c) and saturates it to [-32768, 32767]. For the u16 forms the range is [0, 65535].madintermediate width, is exercised.(Measured with an AI assistant, Claude.)
@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.Pull request closed