Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
adriadam10:fix/f32-clamp-bits
Oct 5, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
adriadam10:fix/f32-clamp-bits

Conversation

@adriadam10

Copy link
Copy Markdown
Collaborator

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

Co-Authored-By: Claude <noreply@anthropic.com>
@oneandonlydean

Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Collaborator

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

@oneandonlydean
oneandonlydean merged commit bbedad3 into boykopovar:main Oct 5, 2026
4 of 5 checks passed
oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Oct 5, 2026
Takes upstream's merged versions of our boykopovar#586 boykopovar#611 boykopovar#612 boykopovar#505 boykopovar#490 boykopovar#619
boykopovar#503 boykopovar#627 boykopovar#628 boykopovar#618 and drops astrobot's local copies, plus gate-batch-3
(boykopovar#469 boykopovar#500 boykopovar#504 boykopovar#509 boykopovar#510 boykopovar#514 boykopovar#525 boykopovar#526 boykopovar#528), round o/p (boykopovar#617 boykopovar#620
boykopovar#621 boykopovar#622 boykopovar#623 boykopovar#624 boykopovar#501 boykopovar#508 boykopovar#629 boykopovar#630 boykopovar#631).

Conflicts:
- SpirvAnalysis.hpp/.cpp, SpirvBufferAccess.cpp: upstream's boykopovar#586/boykopovar#612.
  The 64-bit LDS atomics keep the full array (f27d6e5 no longer needed).
- SpirvMemoryInstructions.cpp: upstream's constant slot plus offset;
  astrobot's split-wave append/consume kept.
- GuestTextureResource.cpp, TextureTiling.cpp and their tests: upstream's
  boykopovar#490/boykopovar#503 (astrobot had no other change there).
- MaskedSelect.cpp test, ShaderMemory.cpp LDS test: upstream's.
- State.cpp: upstream's boykopovar#505 export count and boykopovar#469 null pixel program;
  astrobot's depth export rejection and depth target model kept.
- Pipeline.cpp: upstream's pipeline key; astrobot's depth attachment and
  blend check (a trailing unused attachment stays valid, as
  colorGapPipelineTests expects).
- Draw.cpp, Recorder.cpp, ShaderResources.cpp: astrobot's (renderPassKey
  with depth, moved sample functions, residency maintenance), which
  already hold the merged boykopovar#505/boykopovar#628/boykopovar#490 changes.
- ShaderInputState.cpp: both register sets.
- State.hpp: the duplicate exportIndex member from the auto-merge removed.
- CMakeLists.txt: astrobot's test list (a superset of upstream's).
- tests/Graphics.cpp: upstream's CompactedExportTests replaces astrobot's
  inline copy; tests/Recorder.cpp: upstream's viewPastLastMipTests added.
@adriadam10
adriadam10 deleted the fix/f32-clamp-bits branch October 6, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants