shader: take MIN3 of the operands when a v_med3_f32 operand is NaN #1815
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1822"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by default-cybe on 2026-11-24T19:15:56Z.
Change
tools/pixel_aot.pyloweredv_med3_f32asmax(min(a, b), min(max(a, b), c)). HLSLminandmaxskip a NaN operand, so with one NaN operand the result was the larger of the other two operands, or the first one. The RDNA 2 ISA reference givesV_MIN3_F32of the three operands when any operand is NaN. With IEEE mode off, that is the smallest operand that is not NaN. For example,v_med3_f32 v12, v12, s44, s45withs44= NaN ands45= 0.75 gavev12onmain. The ISA rule givesmin(v12, 0.75).The ALU entry for
med3now calls a new preamble helper,med3_32. It tests the three encodings for NaN ((asuint(x) & 0x7fffffff) > 0x7f800000), asf32_comparisonandnum16already do, because desktop compilers may foldisnan. If any operand is NaN, the helper returnsmin(min(a, b), c). Otherwise it returns the previous expression, so finite results do not change. The integer and 16-bit med3 forms are not changed.The rule is written from the public ISA pseudocode (AMD RDNA 2 Instruction Set Architecture reference,
V_MED3_F32; the CLRX GCN VOP3 page gives the same NaN rule). Thedocs/experiments.mdparagraph that said NaN operands were not measured now describes the rule and what is checked.Verification
Commit
91fb38a4onorigin/mainecc6e6ce. Windows 11 build 26300, NVIDIA GeForce RTX 5070 (driver 32.0.16.1088) as the hardware adapter, and WARP. Debug layer status was not checked. DXC from Windows SDK 10.0.26100. No capability skips.Test changes in
tests/pixel_aot_check.py:check_three_way_templatesnow adds NaN to the operand values. It evaluates themin3,max3andmed3templates, and themed3_32helper taken from the preamble text, with a Python model of HLSLmin/max, where a NaN operand gives the other operand. It compares the results with a reference for the ISA rule (med3_f32).three_way_casecomputes both medians with that reference.check_three_way_drawadds a case with constants(NaN, 0.75, 0.375, NaN): the bounds ofv_med3_f32 v12, v12, s44, s45and the first operand ofv_med3_f32 v15, s47, v13, v12are NaN. As a negative control, a shader whose helper ignores the NaN operand must reportpixel_mismatchon that case.check_controlsexpectsmed3_32(...)in the translated source.Before the fix, with the new tests (on the earlier base
d6cbd5d7):Calling
check_three_way_drawalone (controls skipped) on the unfixed translator gaveExpected ['pass', 'pass', 'pass'], got ['pass', 'mismatch', 'pass']with first mismatchpixel_mismatch x=48 y=0, on WARP and on the RTX 5070. Only the NaN case failed.After the fix:
Evidence and follow-up
The fixture is the existing source-built
three_way()program. Its new case uses only constant-buffer values. No new files.Not tested:
v_min3_f32andv_max3_f32on a GPU. They are checked only against the HLSLmin/maxmodel in Python.tools/startup/gfx2hlsl.pyandmedian_fintools/ffx_dispatch_aot.py. They are separate translators and are left for a follow-up. #1712 adds a draw check forgfx2hlsl.py'sv_med3_f32but leaves NaN out; #1466 lowersv_med3_f16inpixel_aotand refuses non-finite operands.Checklist
AI use
This change was written with AI assistance: Conclave, model
cliproxy/claude-opus-5-5. Conclave wrote the change and ran the commands above on the machine described in Verification.Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:25:20Z.
Review: COMMENTED
Verified PR #1822 at commit
91fb38a47c16040c4c4ac03cb8802bf266e47af3(shader: take MIN3 of the operands when a v_med3_f32 operand is NaN).Review Summary
med3_32(a, b, c)checks operand encodings viamax(max(asuint(a) & 0x7fffffffu, asuint(b) & 0x7fffffffu), asuint(c) & 0x7fffffffu) > 0x7f800000u(avoiding float NaN-test folding while keeping±Infat0x7f800000uon the finite branch). When any operand is NaN, it returnsmin(min(a, b), c), matching RDNA2V_MED3_F32->V_MIN3_F32(smallest non-NaN operand for 1 or 2 NaNs, NaN for 3 NaNs, IEEE mode off).tests/pixel_aot_check.pyextendscheck_three_way_templateswith NaN combinations across all three operands, updatesthree_way_caseandcheck_three_way_drawwith a NaN constant-buffer case plus a negative control verifying that ignoring NaN triggerspixel_mismatch, anddocs/experiments.mdaccurately documents the behavior and coverage.Verification Commands Executed Locally
git diff --check origin/main...HEAD-> PASS (rc=0)python -B tests/docs_links_check.py-> PASS (17 Markdown files and 368 links)python -B tests/bidi_controls_check.py-> PASS (1313 tracked files)python -B tests/form_coverage_check.py-> PASS (form coverage checks passed)python -B tests/rdna2_tools_check.py-> PASS (148 tests OK, skipped=18)pixel_aot_check.check_three_way_templates()&_check_controls_inline()-> PASS--preset dev):ctest --preset dev -R '^(pixel_aot_three_way_warp|pixel_aot_region_body_warp|pixel_aot_e64_warp|pixel_aot_warp)$' --parallel 6 --no-tests=error --output-on-failure-> 4/4 passed (22.68s)/O2(--preset release):ctest --preset release -R '^(pixel_aot_three_way_warp|pixel_aot_region_body_warp|pixel_aot_e64_warp|pixel_aot_warp)$' --parallel 6 --no-tests=error --output-on-failure-> 4/4 passed (23.10s)tests/docs_links_check.py(rc=0),tests/bidi_controls_check.py(rc=0)med3_32(tools/pixel_aot.py) tomax(min(a, b), min(max(a, b), c)): failed locally (check_three_way_templatesrc=1,AssertionError: ('med3', -1.5, 0.0, nan, 0.0)) and on Win2025 (rc=1,tests/form_coverage_check.pyrc=1).med3_32tomax(max(a, b), c): failed locally (check_three_way_templatesrc=1,AssertionError: ('med3', -1.5, 0.0, nan, 0.0)).AI-assisted; commands listed were executed locally.