feat(shader): accept clamp on v_cube*_f32 and match their zero and subnormal results #508
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/508"
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:23:14Z.
What
VOP3
clamponv_cubeid_f32,v_cubesc_f32,v_cubetc_f32andv_cubema_f32used to throw. It is now accepted and clamps the result to [0, 1] with the f32 clamp rule from #500 (NaN and -0 give +0).omodon these ops still throws (see below).The test for this also showed where the cube ops differed from hardware even without modifiers. This PR fixes those cases too:
v_cubema_f32returned -0 for a -0 major axis. The hardware returns +0.v_cubesc_f32/v_cubetc_f32still return the subnormal bits unchanged, andv_cubema_f32returns +0 for a subnormal major axis.x/y/zpassed throughOpFNegate, which on RADV dropped the sign of the NaN. Negation now works on the bits. Sourceneg/abson these ops are now read throughreadU32, so they also work on the bits.v_cubema_f32no longer multiplies a NaN major axis by 2.Behaviour measured on HW
I measured these on an RDNA2 GPU (gfx1035), in the mode the recompiler assumes for every other f32 op: MODE.IEEE = 0 and f32 output denormals flushed (
.amdhsa_ieee_mode 0,.amdhsa_float_denorm_mode_32 0). This is LLVM's default for graphics calling conventions. llvm-mc gives the same VOP3 opcodes 0x144-0x147 for gfx1013.clamp,mul:2,mul:4,div:2andclamp mul:2forms.clamp= the plain result clamped by the #500 rule, with no mismatches.omodis applied in this mode, the same as onv_mul_f32. I ran av_mul_f32 ... mul:2/div:2/mul:4control in the same dispatch. In this mode the cube ops and the control follow the same rule: NaN unchanged, and a zero or subnormal result gives +0. Main'sapplyF32ResultModifiersis a plainFMul, so it returns -0 there. Acceptingomodon the cube ops would add a result that is known to be wrong, so it keeps throwing. Theomodrule for all f32 ops will come in a separate PR.omodon both the cube ops andv_mul_f32, as the ISA guide says. The earlier version of this PR measured in that mode by mistake. With IEEE = 0 and output denormals kept (float_denorm_mode_32 2),v_mul_f32ignoresomodbut the cube ops still apply it. Since the recompiler doesn't model that mode, it doesn't change anything here.To stay exact on any driver, the cube logic works on the bits:
FMulby 2 only sees non-NaN, non-zero, non-subnormal values.Tested
Linux, RADV + lavapipe (local Mesa and CI's Ubuntu Mesa 25.2.8):
agc_driver_vop3_cube_clamp_tests: 32 lanes × 16 forms (plain,clamp, mixedneg/abswith and withoutclamp). Expected values come from the hardware in the mode above. The model matched the hardware on these rows and on a 2048-row sweep (32768 checks, 0 mismatches). It also checks thatomodon each of the four opcodes is refused.agc_driver_pixel_helper_lanes. That test already fails on main with local RADV and isn't touched here.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:02Z.
Thanks, the bit-level cube logic reads right to me (negation on the sign bit, the subnormal flush for the face and sign picks, +0 for a zero or subnormal major axis). Two things about the modes before it can go in:
v_mul_f32 ... mul:2in the same dispatch as a control that does change? Main applies omod on every other f32 op, which assumes exactly that mode.SIModeRegisterDefaults::getDefaultForCallingConv), and the recompiler doesn't read the mode from PGM_RSRC1. I'd keep the sNaN unquieted like main does, which is consistent with the omod assumption above, or record the IEEE=1 assumption in TechnicalDebt.Since this changes cube results without modifiers too, I'll run it through my PPSA21564 check once it's updated.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T21:20:52Z.
Thanks, you were right on both points. Rebased on current main and force-pushed.
v_mul_f32 ... mul:2/div:2/mul:4in the same dispatch. In that modeomodis applied to the cube ops as well as to the control. My earlier "omod is ignored" came from measuring with IEEE=1. Both follow the same rule: NaN unchanged, and a zero or subnormal result gives +0. Main'sapplyF32ResultModifiersreturns -0 there, so instead of acceptingomodwith that gap, it keeps throwing on the cube ops. A test now checks that refusal for all four opcodes. The +0 rule for all f32 ops will come in a separate PR.Title and description are updated.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T22:38:12Z.
Thanks for rerunning it in the IEEE=0, denormals-flushed mode. That settles both points: with omod applied there and main's
applyF32ResultModifiersgiving -0 where the hardware gives +0, keeping omod refused on the cube ops is the exact choice, andCheckOmodRefusedcovers all four opcodes (the second dword sets omod to mul:2 with v4, v5, v6 as sources). NaN results now go through unchanged, as on main, including cubema returning the selected major axis without the multiply. The +0 rule for every f32 op as a separate PR sounds good.The first commit is #500's commit, patch-identical to #500's head, so this lands after #500.
agc_driver_vop3_cube_clamppasses on my NVIDIA machine on current main, and fails on main's code.Since it changes cube results without modifiers too, I'll run it through my PPSA21564 check with #500 and merge both if it's clean.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T07:26:33Z.
#610 landed today and added
isF32DivisionStepOpcode(opcode)to thesupportsNativeVop3Clampreturn line, the same line where this PR addsisCubeOpcode(opcode), so it conflicts now. Keeping both terms fixes it:Could you rebase? I tried it on current main together with #500 and #507, and the full ctest passes. #507 renames
usesSignedSaturateClamptousesIntegerSaturateClampon this line, so whichever of the two lands second needs to keep the rename and all the terms.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T08:31:18Z.
Gated on PPSA21564 on top of #500: no visual or FPS change.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T08:57:53Z.
Rebased on current
mainafter #610 and #500:supportsNativeVop3Clamp: keptisF32DivisionStepOpcodeand addedisCubeOpcode, as suggested. The #500 commit dropped out as already merged.agc_driver_vop3*andagc_shader*tests pass on RADV.