shader: take MIN3 of the operands when a v_med3_f32 operand is NaN #1815

Merged
default-cybe merged 1 commit from upstream-pr/1822 into main 2026-11-24 19:28:17 +00:00
default-cybe commented 2026-11-24 19:15:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by default-cybe on 2026-11-24T19:15:56Z.


Change

tools/pixel_aot.py lowered v_med3_f32 as max(min(a, b), min(max(a, b), c)). HLSL min and max skip 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 gives V_MIN3_F32 of 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, s45 with s44 = NaN and s45 = 0.75 gave v12 on main. The ISA rule gives min(v12, 0.75).

The ALU entry for med3 now calls a new preamble helper, med3_32. It tests the three encodings for NaN ((asuint(x) & 0x7fffffff) > 0x7f800000), as f32_comparison and num16 already do, because desktop compilers may fold isnan. If any operand is NaN, the helper returns min(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). The docs/experiments.md paragraph that said NaN operands were not measured now describes the rule and what is checked.

Verification

Commit 91fb38a4 on origin/main ecc6e6ce. 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_templates now adds NaN to the operand values. It evaluates the min3, max3 and med3 templates, and the med3_32 helper taken from the preamble text, with a Python model of HLSL min/max, where a NaN operand gives the other operand. It compares the results with a reference for the ISA rule (med3_f32).
  • three_way_case computes both medians with that reference.
  • check_three_way_draw adds a case with constants (NaN, 0.75, 0.375, NaN): the bounds of v_med3_f32 v12, v12, s44, s45 and the first operand of v_med3_f32 v15, s47, v13, v12 are NaN. As a negative control, a shader whose helper ignores the NaN operand must report pixel_mismatch on that case.
  • check_controls expects med3_32(...) in the translated source.

Before the fix, with the new tests (on the earlier base d6cbd5d7):

ctest -R "pixel_aot_three_way_(warp|hardware)" --timeout 120 --output-on-failure
AssertionError: ('med3', -1.5, 0.0, nan, 0.0)          # both adapters, 0 of 2 passed

Calling check_three_way_draw alone (controls skipped) on the unfixed translator gave Expected ['pass', 'pass', 'pass'], got ['pass', 'mismatch', 'pass'] with first mismatch pixel_mismatch x=48 y=0, on WARP and on the RTX 5070. Only the NaN case failed.

After the fix:

ctest -R "^pixel_aot_three_way_(warp|hardware)$" --timeout 120 --output-on-failure
100% tests passed out of 2
ctest -R "^pixel_aot_" -j 3 --timeout 300 --output-on-failure
100% tests passed out of 164      # 82 WARP + 82 hardware, includes pixel_aot_region_body_{warp,hardware}
python -B tests/docs_links_check.py
Checked 17 Markdown files and 368 links.

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:

  • NaN operands of v_min3_f32 and v_max3_f32 on a GPU. They are checked only against the HLSL min/max model in Python.
  • IEEE mode on (signaling NaN quieting) and NaN payloads. The pixel path assumes IEEE mode off.
  • The other median lowerings with the same finite-only form: tools/startup/gfx2hlsl.py and median_f in tools/ffx_dispatch_aot.py. They are separate translators and are left for a follow-up. #1712 adds a draw check for gfx2hlsl.py's v_med3_f32 but leaves NaN out; #1466 lowers v_med3_f16 in pixel_aot and refuses non-finite operands.
  • Debug and Release CI configurations, and an AMD adapter.

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 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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6589488056 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1822). Originally posted by [default-cybe](https://github.com/default-cybe) on 2026-11-24T19:15:56Z. --- ## Change `tools/pixel_aot.py` lowered `v_med3_f32` as `max(min(a, b), min(max(a, b), c))`. HLSL `min` and `max` skip 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 gives `V_MIN3_F32` of 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, s45` with `s44` = NaN and `s45` = 0.75 gave `v12` on `main`. The ISA rule gives `min(v12, 0.75)`. The ALU entry for `med3` now calls a new preamble helper, `med3_32`. It tests the three encodings for NaN (`(asuint(x) & 0x7fffffff) > 0x7f800000`), as `f32_comparison` and `num16` already do, because desktop compilers may fold `isnan`. If any operand is NaN, the helper returns `min(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). The `docs/experiments.md` paragraph that said NaN operands were not measured now describes the rule and what is checked. ## Verification Commit `91fb38a4` on `origin/main` `ecc6e6ce`. 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_templates` now adds NaN to the operand values. It evaluates the `min3`, `max3` and `med3` templates, and the `med3_32` helper taken from the preamble text, with a Python model of HLSL `min`/`max`, where a NaN operand gives the other operand. It compares the results with a reference for the ISA rule (`med3_f32`). - `three_way_case` computes both medians with that reference. - `check_three_way_draw` adds a case with constants `(NaN, 0.75, 0.375, NaN)`: the bounds of `v_med3_f32 v12, v12, s44, s45` and the first operand of `v_med3_f32 v15, s47, v13, v12` are NaN. As a negative control, a shader whose helper ignores the NaN operand must report `pixel_mismatch` on that case. - `check_controls` expects `med3_32(...)` in the translated source. Before the fix, with the new tests (on the earlier base `d6cbd5d7`): ``` ctest -R "pixel_aot_three_way_(warp|hardware)" --timeout 120 --output-on-failure AssertionError: ('med3', -1.5, 0.0, nan, 0.0) # both adapters, 0 of 2 passed ``` Calling `check_three_way_draw` alone (controls skipped) on the unfixed translator gave `Expected ['pass', 'pass', 'pass'], got ['pass', 'mismatch', 'pass']` with first mismatch `pixel_mismatch x=48 y=0`, on WARP and on the RTX 5070. Only the NaN case failed. After the fix: ``` ctest -R "^pixel_aot_three_way_(warp|hardware)$" --timeout 120 --output-on-failure 100% tests passed out of 2 ctest -R "^pixel_aot_" -j 3 --timeout 300 --output-on-failure 100% tests passed out of 164 # 82 WARP + 82 hardware, includes pixel_aot_region_body_{warp,hardware} python -B tests/docs_links_check.py Checked 17 Markdown files and 368 links. ``` ## 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: - NaN operands of `v_min3_f32` and `v_max3_f32` on a GPU. They are checked only against the HLSL `min`/`max` model in Python. - IEEE mode on (signaling NaN quieting) and NaN payloads. The pixel path assumes IEEE mode off. - The other median lowerings with the same finite-only form: `tools/startup/gfx2hlsl.py` and `median_f` in `tools/ffx_dispatch_aot.py`. They are separate translators and are left for a follow-up. #1712 adds a draw check for `gfx2hlsl.py`'s `v_med3_f32` but leaves NaN out; #1466 lowers `v_med3_f16` in `pixel_aot` and refuses non-finite operands. - Debug and Release CI configurations, and an AMD adapter. ## 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 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.
upstream-sync closed this pull request 2026-11-24 20:00:47 +00:00
tedin7 commented 2026-11-24 20:00:48 +00:00 (Migrated from github.com)

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

  • ISA semantics: med3_32(a, b, c) checks operand encodings via max(max(asuint(a) & 0x7fffffffu, asuint(b) & 0x7fffffffu), asuint(c) & 0x7fffffffu) > 0x7f800000u (avoiding float NaN-test folding while keeping ±Inf at 0x7f800000u on the finite branch). When any operand is NaN, it returns min(min(a, b), c), matching RDNA2 V_MED3_F32 -> V_MIN3_F32 (smallest non-NaN operand for 1 or 2 NaNs, NaN for 3 NaNs, IEEE mode off).
  • Tests & docs: tests/pixel_aot_check.py extends check_three_way_templates with NaN combinations across all three operands, updates three_way_case and check_three_way_draw with a NaN constant-buffer case plus a negative control verifying that ignoring NaN triggers pixel_mismatch, and docs/experiments.md accurately documents the behavior and coverage.
  • Findings: 0 P1, 0 P2. Clean-room verified.

Verification Commands Executed Locally

  • Linux checks:
    • 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
  • Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP):
    • Debug (--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)
    • Release /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)
    • Windows Python checks: tests/docs_links_check.py (rc=0), tests/bidi_controls_check.py (rc=0)
  • Mutation checks:
    1. Reverted NaN branch in med3_32 (tools/pixel_aot.py) to max(min(a, b), min(max(a, b), c)): failed locally (check_three_way_templates rc=1, AssertionError: ('med3', -1.5, 0.0, nan, 0.0)) and on Win2025 (rc=1, tests/form_coverage_check.py rc=1).
    2. Swapped NaN branch in med3_32 to max(max(a, b), c): failed locally (check_three_way_templates rc=1, AssertionError: ('med3', -1.5, 0.0, nan, 0.0)).

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6045881411 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1822#pullrequestreview-6045881411). Originally posted by [tedin7](https://github.com/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 - **ISA semantics**: `med3_32(a, b, c)` checks operand encodings via `max(max(asuint(a) & 0x7fffffffu, asuint(b) & 0x7fffffffu), asuint(c) & 0x7fffffffu) > 0x7f800000u` (avoiding float NaN-test folding while keeping `±Inf` at `0x7f800000u` on the finite branch). When any operand is NaN, it returns `min(min(a, b), c)`, matching RDNA2 `V_MED3_F32` -> `V_MIN3_F32` (smallest non-NaN operand for 1 or 2 NaNs, NaN for 3 NaNs, IEEE mode off). - **Tests & docs**: `tests/pixel_aot_check.py` extends `check_three_way_templates` with NaN combinations across all three operands, updates `three_way_case` and `check_three_way_draw` with a NaN constant-buffer case plus a negative control verifying that ignoring NaN triggers `pixel_mismatch`, and `docs/experiments.md` accurately documents the behavior and coverage. - **Findings**: 0 P1, 0 P2. Clean-room verified. ### Verification Commands Executed Locally - **Linux checks**: - `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 - **Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP)**: - Debug (`--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`) - Release `/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`) - Windows Python checks: `tests/docs_links_check.py` (`rc=0`), `tests/bidi_controls_check.py` (`rc=0`) - **Mutation checks**: 1. Reverted NaN branch in `med3_32` (`tools/pixel_aot.py`) to `max(min(a, b), min(max(a, b), c))`: failed locally (`check_three_way_templates` `rc=1`, `AssertionError: ('med3', -1.5, 0.0, nan, 0.0)`) and on Win2025 (`rc=1`, `tests/form_coverage_check.py` `rc=1`). 2. Swapped NaN branch in `med3_32` to `max(max(a, b), c)`: failed locally (`check_three_way_templates` `rc=1`, `AssertionError: ('med3', -1.5, 0.0, nan, 0.0)`). AI-assisted; commands listed were executed locally.
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!1815
No description provided.