feat(shader): follow DX10_CLAMP in the clamp modifier #1866

Merged
adriadam10 merged 1 commit from upstream-pr/1866 into main 2026-10-08 22:31:41 +00:00
adriadam10 commented 2026-10-08 22:25:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T22:25:32Z.


What

Part of #981. The clamp modifier always mapped a NaN result to +0. That is the hardware behaviour with DX10_CLAMP=1, the setting in every Astro's Playroom shader but ten. With DX10_CLAMP=0 the hardware writes the NaN unchanged. Translation now follows GuestContext::floatMode.dx10Clamp (#1816).

Measured on RDNA2 (gfx1035) with the HW oracle, over 196 rows (quiet and signaling NaNs of both signs, ±inf, ±0, values above 1 and below 0) at IEEE=0 and IEEE=1. The ops cover f32 arithmetic, min/max/med3, unary, rcp/sqrt/exp, mul_legacy, f16 (add/mul/max, cvt_f16_f32, cvt_f32_f16), packed f16 (pk_add/pk_max) and f64 (add/mul/max/fma_f64):

  • DX10_CLAMP=1: a NaN result clamps to +0, as before.
  • DX10_CLAMP=0: a NaN result is written unchanged. Every other value clamps to [0, 1] as before.
  • Using that rule, every cell matched the hardware (0 mismatches). IEEE_MODE doesn't change the clamp itself.

Changes:

  • applyF32ResultModifiers, applyF16ResultModifiers and clampF16Bits keep a NaN result when DX10_CLAMP=0. A shader without a known float mode keeps DX10_CLAMP=1.
  • Packed f16 with clamp and DX10_CLAMP=0 now goes through the existing NaN-on-the-bits selection, and the lane it picks is clamped. Without that, a min/max that returns the non-NaN operand would skip the clamp.
  • Unary f16 specials keep their NaN under DX10_CLAMP=0 instead of writing 0.
  • f64: the hardware follows the same rule, but there's no f64 execution test yet, so a clamped f64 result with DX10_CLAMP=0 throws.
  • TechnicalDebt: new clamp line; the float mode line lists DX10_CLAMP as read.

Tested

Linux (CachyOS, GCC), RADV (gfx1035) and CI's lavapipe (Mesa 25.2.8).

  • New agc_driver_dx10_clamp: 16 clamped instructions (f32, f16, packed f16) × 32 rows captured on hardware with DX10_CLAMP=1 and =0, IEEE=0, FLOAT_MODE 0xc0. 216 of the 512 cells differ between the two settings. The test runs with no mode and with DX10_CLAMP=1 (checked against the first table) and with DX10_CLAMP=0 (the second). Where the NaN bits come from a driver op (f32 and scalar f16 arithmetic) any NaN is accepted. Where translation chooses them (packed f16) they are compared bit for bit.
  • Negative checks, each fails: the setting always on, always off, the f32 NaN kept dropped, the f16 bits NaN kept dropped, the packed lane not re-clamped, the packed NaN selection skipped.
  • agc_driver_f32_output_modifier, agc_driver_float16_modifiers and agc_driver_packed_alu pass unchanged.
  • Full Release ctest: 492/492. After rebasing on main with #1850, the new test and agc_driver_f32_denormal_flush pass on both drivers.

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 (#1816, #1851 merged)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4793521909 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1866). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T22:25:32Z. --- ### What Part of #981. The clamp modifier always mapped a NaN result to +0. That is the hardware behaviour with `DX10_CLAMP`=1, the setting in every Astro's Playroom shader but ten. With `DX10_CLAMP`=0 the hardware writes the NaN unchanged. Translation now follows `GuestContext::floatMode.dx10Clamp` (#1816). Measured on RDNA2 (gfx1035) with the HW oracle, over 196 rows (quiet and signaling NaNs of both signs, ±inf, ±0, values above 1 and below 0) at IEEE=0 and IEEE=1. The ops cover f32 arithmetic, min/max/med3, unary, rcp/sqrt/exp, `mul_legacy`, f16 (`add/mul/max`, `cvt_f16_f32`, `cvt_f32_f16`), packed f16 (`pk_add/pk_max`) and f64 (`add/mul/max/fma_f64`): - `DX10_CLAMP`=1: a NaN result clamps to +0, as before. - `DX10_CLAMP`=0: a NaN result is written unchanged. Every other value clamps to [0, 1] as before. - Using that rule, every cell matched the hardware (0 mismatches). `IEEE_MODE` doesn't change the clamp itself. Changes: - `applyF32ResultModifiers`, `applyF16ResultModifiers` and `clampF16Bits` keep a NaN result when `DX10_CLAMP`=0. A shader without a known float mode keeps `DX10_CLAMP`=1. - Packed f16 with clamp and `DX10_CLAMP`=0 now goes through the existing NaN-on-the-bits selection, and the lane it picks is clamped. Without that, a min/max that returns the non-NaN operand would skip the clamp. - Unary f16 specials keep their NaN under `DX10_CLAMP`=0 instead of writing 0. - f64: the hardware follows the same rule, but there's no f64 execution test yet, so a clamped f64 result with `DX10_CLAMP`=0 throws. - TechnicalDebt: new clamp line; the float mode line lists `DX10_CLAMP` as read. ### Tested Linux (CachyOS, GCC), RADV (gfx1035) and CI's lavapipe (Mesa 25.2.8). - New `agc_driver_dx10_clamp`: 16 clamped instructions (f32, f16, packed f16) × 32 rows captured on hardware with `DX10_CLAMP`=1 and =0, IEEE=0, `FLOAT_MODE` 0xc0. 216 of the 512 cells differ between the two settings. The test runs with no mode and with `DX10_CLAMP`=1 (checked against the first table) and with `DX10_CLAMP`=0 (the second). Where the NaN bits come from a driver op (f32 and scalar f16 arithmetic) any NaN is accepted. Where translation chooses them (packed f16) they are compared bit for bit. - Negative checks, each fails: the setting always on, always off, the f32 NaN kept dropped, the f16 bits NaN kept dropped, the packed lane not re-clamped, the packed NaN selection skipped. - `agc_driver_f32_output_modifier`, `agc_driver_float16_modifiers` and `agc_driver_packed_alu` pass unchanged. - Full Release ctest: 492/492. After rebasing on `main` with #1850, the new test and `agc_driver_f32_denormal_flush` pass on both drivers. ### 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 (#1816, #1851 merged) - [x] AI-assisted: yes
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!1866
No description provided.