fix(shader): clamp f32 results to +0 for NaN and -0 #500

Merged
adriadam10 merged 1 commit from upstream-pr/500 into main 2026-10-05 08:31:32 +00:00
adriadam10 commented 2026-10-04 10:16:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T10:16:52Z.


What

The VOP3/SDWA clamp bit on f32 results (FPSaturate32) was emitted as GLSL FClamp(x, 0, 1). In SPIR-V that leaves NaN and −0 open: on RADV a NaN result stays NaN (0xffc00000) and lavapipe gives 1.0, but the hardware gives +0 for both. It is now done on the bits (1 <= bits <= 0x7f800000 ? umin(bits, 0x3f800000) : 0), so every driver gives the same result. FPSaturate64 already works this way.

Behaviour measured on HW (RDNA2, gfx1035, IEEE mode 0/1, f32 denormals on/off: all the same)

  • Pre-clamp NaN (quiet, signalling, either sign) → +0; −0 → +0; negatives and −inf → +0; > 1 and +inf → 1.0.
  • Positive denormal results are kept (when the op doesn't flush them).

Tested

New execution test agc_driver_f32_result_clamp_tests: 16 ops with clamp (add, sub, mul, mul with negated source, fma, fmac, min, max, med3, max3, floor, fract, trunc, ceil, rndne, cvt_f32_i32) over 32 rows covering each pre-clamp class (NaN, ±0, ±inf, negative, (0,1), 1, > 1). Expected values come from the hardware, and the Python model matches them in all 503 checked cells. Denormal rows and min/max rows with sNaN (where the result depends on IEEE mode) are left out. No row depends on whether the driver fuses fma.
Passes on RADV, local lavapipe and CI's lavapipe (Mesa 25.2.8). The test fails without the fix on both drivers (going back to FClamp, dropping the lower bound, or keeping NaN bits all fail). Full Release ctest is green except agc_driver_pixel_helper_lanes, which also fails on main on local RADV.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4733590044 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/500). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T10:16:52Z. --- ### What The VOP3/SDWA `clamp` bit on f32 results (`FPSaturate32`) was emitted as GLSL `FClamp(x, 0, 1)`. In SPIR-V that leaves NaN and −0 open: on RADV a NaN result stays NaN (`0xffc00000`) and lavapipe gives `1.0`, but the hardware gives `+0` for both. It is now done on the bits (`1 <= bits <= 0x7f800000 ? umin(bits, 0x3f800000) : 0`), so every driver gives the same result. `FPSaturate64` already works this way. #### Behaviour measured on HW (RDNA2, gfx1035, IEEE mode 0/1, f32 denormals on/off: all the same) - Pre-clamp NaN (quiet, signalling, either sign) → `+0`; −0 → `+0`; negatives and −inf → `+0`; > 1 and +inf → `1.0`. - Positive denormal results are kept (when the op doesn't flush them). ### Tested New execution test `agc_driver_f32_result_clamp_tests`: 16 ops with `clamp` (add, sub, mul, mul with negated source, fma, fmac, min, max, med3, max3, floor, fract, trunc, ceil, rndne, cvt_f32_i32) over 32 rows covering each pre-clamp class (NaN, ±0, ±inf, negative, (0,1), 1, > 1). Expected values come from the hardware, and the Python model matches them in all 503 checked cells. Denormal rows and min/max rows with sNaN (where the result depends on IEEE mode) are left out. No row depends on whether the driver fuses `fma`. Passes on RADV, local lavapipe and CI's lavapipe (Mesa 25.2.8). The test fails without the fix on both drivers (going back to `FClamp`, dropping the lower bound, or keeping NaN bits all fail). Full Release ctest is green except `agc_driver_pixel_helper_lanes`, which also fails on `main` on local RADV. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
oneandonlydean commented 2026-10-04 21:07:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T21:07:55Z.


Thanks, checked the bit rule (1 <= bits <= 0x7f800000, then umin to 1.0) against the RDNA2 MODE description: NaN to +0 is the DX10_CLAMP behaviour, which LLVM also assumes by default for every calling convention, and it now matches what FPSaturate64 already does. One note for the record: on NVIDIA the new test already passes on main, because the driver lowers FClamp(x, 0, 1) to a saturate that gives +0 for NaN and -0, so the failure without the fix only shows on RADV and lavapipe, as you wrote. This touches every f32 clamp, so I'll run it through my PPSA21564 check before merging; ready to merge from my side otherwise.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5984395436 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/500#issuecomment-5984395436). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T21:07:55Z. --- Thanks, checked the bit rule (`1 <= bits <= 0x7f800000`, then umin to 1.0) against the RDNA2 MODE description: NaN to +0 is the DX10_CLAMP behaviour, which LLVM also assumes by default for every calling convention, and it now matches what `FPSaturate64` already does. One note for the record: on NVIDIA the new test already passes on main, because the driver lowers `FClamp(x, 0, 1)` to a saturate that gives +0 for NaN and -0, so the failure without the fix only shows on RADV and lavapipe, as you wrote. This touches every f32 clamp, so I'll run it through my PPSA21564 check before merging; ready to merge from my side otherwise.
oneandonlydean commented 2026-10-05 08:31:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T08:31:16Z.


Gated on PPSA21564 with #508 on top: no visual or FPS change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5990920784 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/500#issuecomment-5990920784). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T08:31:16Z. --- Gated on PPSA21564 with #508 on top: no visual or FPS change.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!500
No description provided.