shader: clamp v_fract_f32 below 1.0 in the pixel translator #263

Merged
Spaghetih merged 1 commit from upstream-pr/267 into main 2026-11-21 19:56:32 +00:00
Spaghetih commented 2026-11-21 19:47:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Spaghetih on 2026-11-21T19:47:40Z.


Change

Independent problem: tools/pixel_aot.py lowered v_fract_f32 to a bare HLSL frac. On the target, v_fract_f32 returns x - floor(x) clamped to 0x3f7fffff, so the result never reaches 1.0. The AMD ISA entry says "result clamped to 0x3f7fffff", and LLVM's AMDGPU backend folds minnum(x - floor(x), 0x3f7fffff) plus a NaN check into a single v_fract_f32, as its fract-match.ll test shows. D3D frc has no such clamp, and the two desktop backends I tried disagree.

Measured with the repository renderer (offscreen_check --check-pixel-case), drawing v_fract_f32 on constant -2^-30 through the translator from main:

Backend Exported red Clamped reference (0x3f7fffff → half RTZ)
NVIDIA GeForce RTX 4070 Ti, driver 32.0.16.1088 1.0 0.999512 (mismatch)
WARP 10.0.26100.9818 0.999512 0.999512

The lowering now calls a fract32 helper in the preamble. It returns frac(x) unless that is ≥ 1.0, in which case it returns asfloat(0x3f7fffffu). NaN fails the comparison and propagates. ±inf gives NaN through frac, as S0 - floor(S0) does on the target.

Shared interface: tools/pixel_aot.py and tests/pixel_aot_check.py. #169 and #232 edit both files. I placed the helper before export16, changed only the existing fract entry in ALU, and put the new draw and control at positions neither PR touches. main + #169 + this PR and main + #232 + this PR both merge cleanly, and check_controls passes on each combination. (#169 and #232 conflict with each other independently of this change.)

Verification

Commit 294bf7c on 7233cec. Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100 DXC, Python 3.14.6. Debug layer enabled on both backends.

ctest --preset dev --no-tests=error -R '^pixel_aot_'
  • With this change: pixel_aot_hardware (RTX 4070 Ti) and pixel_aot_warp passed.
  • Negative control, the same tests with tools/pixel_aot.py from 7233cec: both failed. They stop at the new source-level control, because fract lowers to a bare frac. With that control removed, the new draw failed on hardware (pixel_mismatch x=0 y=0 channel=0 expected=0.999512 actual=1) and passed on WARP, matching the table above.
  • Full ctest --preset dev --no-tests=error: 118 passed, 0 failed, 3 capability skips (bvh_wave64_hardware, bvh_wave64_warp, wave_shuffle_warp). form_coverage_check.py passed.

Evidence and follow-up

The new draw check_fract_draw uses synthetic constants only, with inputs -2^-30, -0.25, 2.75 and -2^-23. The Python reference computes min(single(x - floor(x)), 0x3f7fffff) and asserts that the first input is the case where the unclamped value is exactly 1.0. A second reference that expects 1.0 for that channel must report a mismatch, so the check cannot pass if the renderer stops distinguishing the clamp. check_controls also asserts that shaping() lowers through fract32( and never a bare frac(. That check runs in both draw and binary mode, so the Linux job covers it too. docs/experiments.md gains one paragraph in the pixel-shader section.

Not measured: the value on AMD hardware itself. The clamp comes from the ISA text and LLVM's codegen, not from a draw on an RDNA GPU.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted (Claude Code). Every result above was run locally on the configuration listed.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6533590061 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/267). Originally posted by [Spaghetih](https://github.com/Spaghetih) on 2026-11-21T19:47:40Z. --- ## Change Independent problem: `tools/pixel_aot.py` lowered `v_fract_f32` to a bare HLSL `frac`. On the target, `v_fract_f32` returns x - floor(x) clamped to `0x3f7fffff`, so the result never reaches 1.0. The AMD ISA entry says "result clamped to 0x3f7fffff", and LLVM's AMDGPU backend folds `minnum(x - floor(x), 0x3f7fffff)` plus a NaN check into a single `v_fract_f32`, as its `fract-match.ll` test shows. D3D `frc` has no such clamp, and the two desktop backends I tried disagree. Measured with the repository renderer (`offscreen_check --check-pixel-case`), drawing `v_fract_f32` on constant -2^-30 through the translator from `main`: | Backend | Exported red | Clamped reference (0x3f7fffff → half RTZ) | |---|---|---| | NVIDIA GeForce RTX 4070 Ti, driver 32.0.16.1088 | `1.0` | `0.999512` (mismatch) | | WARP 10.0.26100.9818 | `0.999512` | `0.999512` | The lowering now calls a `fract32` helper in the preamble. It returns `frac(x)` unless that is ≥ 1.0, in which case it returns `asfloat(0x3f7fffffu)`. NaN fails the comparison and propagates. ±inf gives NaN through `frac`, as `S0 - floor(S0)` does on the target. Shared interface: `tools/pixel_aot.py` and `tests/pixel_aot_check.py`. #169 and #232 edit both files. I placed the helper before `export16`, changed only the existing `fract` entry in `ALU`, and put the new draw and control at positions neither PR touches. `main` + #169 + this PR and `main` + #232 + this PR both merge cleanly, and `check_controls` passes on each combination. (#169 and #232 conflict with each other independently of this change.) ## Verification Commit `294bf7c` on `7233cec`. Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100 DXC, Python 3.14.6. Debug layer enabled on both backends. ```powershell ctest --preset dev --no-tests=error -R '^pixel_aot_' ``` - With this change: `pixel_aot_hardware` (RTX 4070 Ti) and `pixel_aot_warp` passed. - Negative control, the same tests with `tools/pixel_aot.py` from `7233cec`: both failed. They stop at the new source-level control, because `fract` lowers to a bare `frac`. With that control removed, the new draw failed on hardware (`pixel_mismatch x=0 y=0 channel=0 expected=0.999512 actual=1`) and passed on WARP, matching the table above. - Full `ctest --preset dev --no-tests=error`: 118 passed, 0 failed, 3 capability skips (`bvh_wave64_hardware`, `bvh_wave64_warp`, `wave_shuffle_warp`). `form_coverage_check.py` passed. ## Evidence and follow-up The new draw `check_fract_draw` uses synthetic constants only, with inputs -2^-30, -0.25, 2.75 and -2^-23. The Python reference computes `min(single(x - floor(x)), 0x3f7fffff)` and asserts that the first input is the case where the unclamped value is exactly 1.0. A second reference that expects 1.0 for that channel must report a mismatch, so the check cannot pass if the renderer stops distinguishing the clamp. `check_controls` also asserts that `shaping()` lowers through `fract32(` and never a bare `frac(`. That check runs in both `draw` and `binary` mode, so the Linux job covers it too. `docs/experiments.md` gains one paragraph in the pixel-shader section. Not measured: the value on AMD hardware itself. The clamp comes from the ISA text and LLVM's codegen, not from a draw on an RDNA GPU. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. _AI-assisted (Claude Code). Every result above was run locally on the configuration listed._
upstream-sync closed this pull request 2026-11-24 06:42:08 +00:00
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
R0Y00/Xbox2Windows!263
No description provided.