Skip to content

Reject NaN in sampler mip readback - #2719

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Vonsek:codex/sampler-nan-readback
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Vonsek:codex/sampler-nan-readback

Conversation

@Vonsek

@Vonsek Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: ps_ssharp Sampler NaN readback

What and why

The S#/T# mip test could pass a render-target pixel with a NaN channel. The old comparison asked whether the difference exceeded the tolerance, which is false for NaN. Require each channel to be within the same absolute tolerance.

Tested

  • Generated a valid 64 × 64 RGBA32F readback on CPU and overwrote one channel at a time in its last pixel with the quiet NaN word 0x7fc00000. On the baseline the clean frame and all four corrupt frames passed. With the fix the clean frame still passes and all four corrupt frames report pixel (63,63).
  • python -I -m unittest discover -s tests -p test_shader_checks.py passed all 63 tests on Windows. This comparator regression used synthetic readbacks and ran on the CPU.

Checklist

  • No game data, keys or decryption.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not applicable.)

AI-assisted (Codex).

An unordered NaN never exceeded the absolute error limit, so a corrupt GPU result could pass. Require each readback channel to be within the existing tolerance.
@KiryaAfimin

Copy link
Copy Markdown

Tested 6e9eb12 on Windows 11 with an RTX 4070 (driver 621.01). All eight ps_ssharp sampler cases passed, with all 4,096 pixels matching in each case.

I also used a temporary fragment shader to write a quiet NaN at pixel (37, 19), one RGBA channel per draw. In each GPU readback, only that channel differed from the clean frame. The parent comparator accepted all four frames. This PR rejected all four and reported (37, 19) each time.

AI-assisted

@ricarmandoagent

Copy link
Copy Markdown

Additional check on 6e9eb1282e6300a15981906895d134b8b60f62ee (base 49475796) on NVIDIA GeForce RTX 4060 (Ada, AD107; Vulkan subgroupSize 32..32), driver 620.70.06, Vulkan 1.4.357, Bazzite 45 / Linux 7.3.4, Python 3.15.2, llvm-mc 23.1.5, shared prebuilt harness.

The RTX 4070 / Windows comment above already does the GPU NaN-frame check through a temporary shader. This adds a Linux run of the real sampler test plus an independent before/after of the comparator itself, which is the whole change.

GPU: --filter ps_ssharp -> 2 shaders translated, 2 pass spirv-val, ps_ssharp PASS "8 S#/T# cases, 4096 pixels each exact". So the stricter comparator does not reject a genuine readback.

Comparator probe (independent small script, not the PR's test), clean frame and one quiet-NaN channel 0x7fc00000 in the last pixel of each of the four channels:

# runtime/test/shaders/check.py = this PR
clean:    ''
nan ch0:  '(63,63): (nan, 1.0, 0.502, 0.502) != (1.0, 1.0, 0.502, 0.502)'
nan ch1:  '(63,63): ...'
nan ch2:  '(63,63): ...'
nan ch3:  '(63,63): ...'

# runtime/test/shaders/check.py = origin/main 49475796
clean:    ''
nan ch0:  ''
nan ch1:  ''
nan ch2:  ''
nan ch3:  ''

That is exactly the bug: abs(g - w) > 1e-6 is false for NaN, so the old comparator accepted all four corrupt frames. The PR's test_nan_in_any_sampler_channel_fails agrees: ok on this head, FAILED (failures=4) against main's check.py. Restored (git status clean).

python3 -I -m unittest discover -s tests -p test_shader_checks.py -> 63 tests OK on this head.

Nothing hardware-specific to qualify here: the comparator is CPU code, and the GPU run only shows real readbacks still pass. I did not repeat the temporary-shader GPU NaN injection (the comment above covers it on another part); the probe above exercises the same acceptance test directly.

Verdict: LGTM.

AI-assisted review.

@bobcatchris

Copy link
Copy Markdown
Contributor

Independent verification on Windows, CPU only, Python 3.12, at origin/main 49475796: applied this PR's two files over a clean main.

  • With the PR: python -IB tests/test_shader_checks.py → 61 tests, OK (0.87 s).
  • Control: reverting only runtime/test/shaders/check.py to main → FAILED (failures=4), including test_nan_in_any_sampler_channel_fails (sampler_mismatch(...) no longer starts with "(63,63):").

So the NaN-in-a-sampler-channel case is exactly what the change adds, and the remaining 57 tests pass either way. No game data was read.

@padhossein

Copy link
Copy Markdown
Contributor

Independent sampler readback fault sweep (6e9eb128). I injected positive/negative NaNs and infinities, each in all four channels at three positions (first pixel, interior, last). This head rejected 48/48 corruptions and accepted the original frame. The base accepted all 24 NaNs and rejected all 24 infinities. All 61 shader checker tests passed.

AI-assisted (Codex).

@crastinator

Copy link
Copy Markdown
Contributor

Tested 6e9eb12 merged into main ec8ec4c9 (clean merge) on AMD Radeon RX 6700 XT (Navi 22, gfx1031, subgroup 32..64), RADV Mesa 26.3.1, CachyOS, Linux 7.3.5, GCC 16.2.1, llvm-mc 23.1.5: pass.

  • Whole run_gpu_tests.py: 175/189 native, 175/189 with LEONIDA_W64_EMULATE=1 (the skips are the D3D12-only fixtures and ps_w64x_readlane, as on main). ctest 37/37.
  • tests/test_shader_checks.py: 72 passed.

No game files. AI-assisted (Claude Code).

@erickbobd

Copy link
Copy Markdown
Contributor

Tested 6e9eb12 merged onto main ec8ec4c on an NVIDIA GeForce RTX 5070 Ti (desktop), driver 617.14, Windows 11 (build 26200), subgroup 32..32: pass.

  • runtime/test/shaders/build.py + check.py (test programs built from main): 132 of 167 pass, 35 skipped, the same as main on this machine
  • ps_ssharp, the S#/T# test whose readback goes through sampler_mismatch, is ok with the new comparison
  • python -I tests/test_shader_checks.py: 70/70 OK

Setup note: LLVM_MC pointed at a shim over AMD RGA 2.15's clang -cc1as (LLVM 23.0.0git); see my note on #2463.

@ItsNotRexed
ItsNotRexed merged commit bd75994 into ItsNotRexed:main Nov 22, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants