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

Manually merged
github merged 1 commit from gh-pr/267/head into gh-pr/267/base 2026-11-26 21:38:46 +00:00
Owner

@⁠Spaghetih on GitHub · 2026-11-21 19:47 UTC · original
Spaghetih:shader/fract-clamp → main · +40 −1 · 3 files · 1 commits · merged 2026-11-21 19:56 UTC

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.

**@⁠Spaghetih** on GitHub · 2026-11-21 19:47 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/267) `Spaghetih:shader/fract-clamp` → `main` · +40 −1 · 3 files · 1 commits · merged 2026-11-21 19:56 UTC ## 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._
v_fract_f32 returns x - floor(x) clamped to 0x3f7fffff. The lowering used a bare D3D frac, which has no clamp: for -2^-30 an RTX 4070 Ti exported 1.0, while WARP exported the clamped value. Route fract through a helper that applies the clamp and lets NaN propagate, and draw four fract inputs with a negative control.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:38:44 +00:00
github manually merged commit 294bf7cee3 into gh-pr/267/base 2026-11-26 21:38:46 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.