fix(shader): saturate VOP3 integer mad/sad results on clamp #507
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/507"
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?
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T11:03:55Z.
What
clampon the VOP3 integer mad/sad ops and on the 16-bit max3/min3/med3 and align ops threwVOP3 source modifiers are not implemented. It is now decoded and applied as the hardware does:v_mad_u32_u24,v_mad_u32_u16,v_sad_u8,v_sad_hi_u8,v_sad_u16,v_sad_u32,v_msad_u8,v_mqsad_u32_u8(each dword): the exact sum saturates to0xffffffff.v_mad_i32_i24,v_mad_i32_i16: the exact sum saturates to[0x80000000, 0x7fffffff].v_mad_u16: saturates to0xffff;v_mad_i16: to[-32768, 32767].op_selstill applies (high destination half keeps the low half).v_qsad_pk_u16_u8,v_mqsad_pk_u16_u8: each 16-bit lane (accumulator half + SAD) saturates to0xffff.v_max3/min3/med3_{i16,u16},v_alignbit_b32,v_alignbyte_b32:clamphas no effect on hardware, so it is accepted and ignored.The 32-bit cases go through one helper that adds the addend to a (high:low) 64-bit value (the full 24x24 product via
SMulHi/UMulHi, the sign of the 16x16 product, or 0 for SAD) and saturates only whenclampis set; withoutclampthe result is computed as before.#502'ssaturateInteger16treats the value as signed (it needs that forv_pk_sub_u16), which is wrong forv_mad_u16sums above0x7fffffff, so the 16-bit case has its own unsignedUMin.Vop3Sad.cppchecked thatclampon the SAD ops was refused; that check is removed, the new tests cover it.v_mad_u64_u32/v_mad_i64_i32(VOP3b)clampstill throws.Behaviour measured on HW (RDNA2, gfx1035)
4096 rows (edge words + random) for each of the 22 ops, run with and without
clamp: the Python model (exact sum, then saturate to the destination width and signedness) matches 57344 of 57344 clamped and unclamped results for the 14 saturating ops. For max3/min3/med3_{i16,u16} and alignbit/alignbyte, the clamped and unclamped results are identical on all 4096 rows.Tested
Two new execution tests, 32 rows each, expected values from the hardware (the model matches every cell):
agc_driver_vop3_integer_clamp_tests: the 11 mad/sad ops plus 5op_sel+clampforms of the 16-bit mads (high destination half included). Rows are picked so each op has saturating and non-saturating rows, at both bounds for the signed ones.agc_driver_vop3_integer_clamp_wide_tests: qsad/mqsad (every lane saturating and not) and the 8 ops whereclampis ignored.Both pass on RADV, local lavapipe and CI's lavapipe (Mesa 25.2.8). The tests fail without each part of the fix: unsigned or signed wrap instead of saturation, dropping the high product or the addend sign, no upper or lower 16-bit bound, a zero high word for
v_mad_i32_i16, the qsad lane not masked or its high lane wrapping,v_mqsad_u32_u8wrapping, andclampstill refused by the decoder all fail (14 mutations). The full Release ctest passes exceptagc_driver_pixel_helper_lanes, which also fails onmainon local RADV.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T21:08:01Z.
Checked the saturation by hand:
wideAddbuilds the high word of the exact 64-bit sum (high + carry + sext(addend)), so both the signed overflow test and the0x80000000/0x7fffffffselect are right.saturateU16Result.clampevery path computes the same value as before.The clamp bit matches LLVM's
VOP3_CLAMPprofiles on these ops. On my machine both new tests fail on main and pass with the change.This and #508 both edit
supportsNativeVop3Clamp, so whichever lands second needs a one-line rebase. Once #504 is in and after my PPSA21564 check, it's ready to merge from my side.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T07:26:31Z.
#610 landed today and added
isF32DivisionStepOpcode(opcode)to thesupportsNativeVop3Clampreturn line, which this PR also changes, so it conflicts now. Keeping main's new term with your rename fixes it:Could you rebase? I tried this on current main together with #504 and #508, and the full ctest passes. #508 changes the same line, so whichever of the two lands second will need the same one-line fix again (all terms kept).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T08:58:22Z.
Rebased on current
mainafter #610 and #504:supportsNativeVop3Clamp: keptisF32DivisionStepOpcodenext to theusesIntegerSaturateClamprename, as suggested. #504 dropped out as already merged.agc_driver_vop3*andagc_shader*tests pass on RADV.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T17:05:54Z.
Rebased on main (conflict in
supportsNativeVop3Clampwith the cube clamp: both kept). Vop3IntegerClamp, Vop3IntegerClampWide and Vop3Sad pass on RADV and lavapipe.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T18:13:04Z.
Thanks, the rebase is right: the
supportsNativeVop3Clampline now hasusesIntegerSaturateClamptogether with #508'sisCubeOpcode, and the rest of the commit is unchanged. Ready to merge from my side.