fix(shader): apply clamp but not omod to f16 results, as RDNA does #323
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/323"
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-02T05:51:44Z.
What
RDNA f16 VALU results take the
clampbit but, in the default float mode, notomod. The recompiler had it the other way around in several places:writeF16(every f16 result written through it: VOP3v_rcp/floor/fract/…_f16,v_cvt_f16_f32,v_cvt_f16_u16/i16,v_fma_f16, SDWAv_add/mul_f16) ignored clamp:v_rcp_f16 0.25 clampgave 4.0 instead of 1.0.applyF32ResultModifiers, so they applied omod.v_cvt_pkrtz_f16_f32applied clamp and omod to its inputs. The hardware ignores both on this opcode.Measured on an RDNA2 GPU (ROCm HSA, raw kernels; same VALU as gfx1013 for these opcodes):
applyF16ResultModifiersdoes the clamp.writeF16,float16Unaryandfloat16Trignow use it.vCvtPkrtzF16F32no longer modifies its inputs.Test
agc_driver_float16_result_modifiers: 16 ops × 32 lanes with values captured on the RDNA2 GPU: clamp and omod on VOP1/VOP3/SDWA f16 results,v_fma_f16,v_cvt_f16_f32/u16andv_cvt_pkrtz, including NaN, ±inf, −0 and SDWAWORD_1withUNUSED_PRESERVE. f16 inputs are kept normal and f32 inputs away from denormals because those depend on the driver's float controls.Tested
Linux, full ctest (100) passes.
rcp_clamp is 0x4400, expected 0x3c00(both drivers). Reverting only the pkrtz part:pkrtz_div2 is 0x30cc, expected 0x34cc. Reverting only the sqrt/sin part:sqrt_clamp_mul2 is 0x3c00, expected 0x3800.Follow-up: in VOP3, the decoder still rejects clamp on the f16 binaries and on
mad/min3/max3/med3_f16. That will come in a separate PR after #252, which touches the same checks.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-04T05:40:45Z.
The fix itself looks right to me. It matches what ACO does: no omod on 16-bit results unless f16 denormals are flushed, and no output modifiers on
v_cvt_pkrtz_f16_f32. Butagc_driver_float16_result_modifiersfails on my NVIDIA machine (on main with this branch merged in). Lane 4 feeds the quiet NaN 0x7e00 throughrcp,sin,fmaand the SDWAmuland expects 0x7e00 back. NVIDIA returns its canonical NaN 0x7fffffff for f32 arithmetic on a NaN, so after the f16 conversion the result is 0x7fff. RDNA and lavapipe keep the payload, which is why CI is green. #317's Float16Rounding test accepts any NaN (IsNan16) for this reason. Could this test do the same for the NaN rows, or drop NaN inputs from the arithmetic columns? Then it is ready from my side.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T06:26:53Z.
Thanks for running it on NVIDIA. The test now accepts any NaN in each 16-bit half where a NaN is expected (
IsNan16, as in #317's Float16Rounding); all other values are still compared bit for bit. Rebased onto main. It passes on RADV and lavapipe, and with main's translation files it still fails on the first clamp row (rcp_clamp is 0x00004400, expected 0x00003c00).Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T21:19:05Z.
Thanks for the IsNan16 change, that fixes the NaN payload rows on NVIDIA. With those out of the way the test now gets further and stops at "lane 7 sin_mul2 is 0x00000003, expected 0x00000006" on my NVIDIA machine. Input 0x0001 gives sin(2pi * 2^-24), about 6 f16 denormal ulps. That value comes straight from the driver's sine, and NVIDIA's sine has an absolute error bound larger than the value itself (Vulkan only asks for 2^-11 absolute for sin), so 0x0003 is a valid result there and RDNA, RADV and lavapipe just happen to be exact. Could the sin rows use an input whose sine is well above that error (around 0.25 to 0.5, where mul:2 applied or ignored still differs clearly), with the sin columns compared within a tolerance? Then it's ready from my side, and since it changes f16 results that carry clamp, I'll run it through my PPSA21564 check before merging.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T21:35:24Z.
Thanks, done (new last commit, rebased on current main):
sin_mul2,sin_clamp) are now compared within 2^-10 absolute, inside the Vulkan bound for sin, so 0x0003 for the 0x0001 row passes. Results the code picks on the bits stay exact: a ±0 the cardinal pin chooses must match bit for bit, NaN/inf rows go throughSameHalf, and the clamped column must also be non-negative.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T22:37:49Z.
Thanks, the new comparison looks right to me.
NearHalfkeeps everything the code picks on the bits exact (an unclamped ±0, NaN and inf throughSameHalf), checks the clamped column for a negative sign, and only the two sine columns go through the tolerance. 2^-10 is a fair bound here: the recompiler evaluatesv_sin_f16as an f32 sine of the widened input, so the driver error is within Vulkan's 2^-11 absolute bound for 32-bit sin, and rounding to f16 adds at most half an f16 ulp below 1, 2^-12. Lane 0 (sin = 1) still separates mul:2 applied from ignored by a full 1.0, so the omod check keeps its teeth.agc_driver_float16_result_modifiersnow passes on my NVIDIA machine (on current main, with #537 and the other open shader PRs), where it stopped at lane 7sin_mul2before.Since this changes f16 results that carry clamp, I'll run it through my PPSA21564 check next and merge it after that if it's clean.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T05:03:57Z.
Gated on PPSA21564 together with #537 and #567: no visual or FPS change.