fix(shader): preserve NaN operands in float min/max lowering #3352

Open
github-bot wants to merge 2 commits from github-bot/gh-3352 into main AGit
Owner

@⁠Thomas73737 on GitHub · 2026-11-26 19:49 UTC · original
Thomas73737:fix/shader-minmax-nan-operand → main · +288 −62 · 2 files · 2 commits · open

Summary

  • make max_min_flushed explicitly return the other operand when one float min/max operand is NaN, and the first operand when both are NaN
  • add valu_tests coverage for f32/f16 v_min/v_max/v_min3/v_max3 NaN operand handling

Rationale

RDNA2 and DXIL FMax/FMin semantics treat a NaN operand as returning the other operand. The existing lowering depended on DXIL op behavior after custom zero/denormal handling; making NaN propagation explicit avoids driver/backend quirks that could turn NaNs into dark/black pixels in color-sensitive passes such as sun/glow/water highlights.

I could not reproduce or confirm the reported sun/water black artifact in this Linux sandbox: no game files were scanned/downloaded, no Windows/D3D12/WARP/GPU capture is available, and I cannot attribute the report to shader min/max NaN handling without a capture. This is a targeted, low-risk semantic hardening based on shader-lowering inspection.

Validation

  • cargo fmt --package victory-shader -- crates/victory-shader/src/emit/lower.rs crates/victory-shader/src/emit/valu_tests.rs
  • cargo test -p victory-shader --locked --lib emit::valu_tests (compiles/tests run in this Linux environment; the newly added NaN min/max tests are Windows-gated by valu_tests module cfg)
  • not run: Windows/D3D12/WARP/game repro or GPU capture

Risks / follow-ups

  • May not address the reported visual artifact if the root cause is elsewhere (resource state, format conversion, blending, tonemap, or missing GPU feature).
  • Needs Windows/D3D12 validation and, ideally, a PIX/GPU capture or before/after screenshot from the reported scene.

AI-assisted: yes

**@⁠Thomas73737** on GitHub · 2026-11-26 19:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/3352) `Thomas73737:fix/shader-minmax-nan-operand` → `main` · +288 −62 · 2 files · 2 commits · open ## Summary - make `max_min_flushed` explicitly return the other operand when one float min/max operand is NaN, and the first operand when both are NaN - add `valu_tests` coverage for f32/f16 `v_min/v_max/v_min3/v_max3` NaN operand handling ## Rationale RDNA2 and DXIL `FMax`/`FMin` semantics treat a NaN operand as returning the other operand. The existing lowering depended on DXIL op behavior after custom zero/denormal handling; making NaN propagation explicit avoids driver/backend quirks that could turn NaNs into dark/black pixels in color-sensitive passes such as sun/glow/water highlights. I could not reproduce or confirm the reported sun/water black artifact in this Linux sandbox: no game files were scanned/downloaded, no Windows/D3D12/WARP/GPU capture is available, and I cannot attribute the report to shader min/max NaN handling without a capture. This is a targeted, low-risk semantic hardening based on shader-lowering inspection. ## Validation - `cargo fmt --package victory-shader -- crates/victory-shader/src/emit/lower.rs crates/victory-shader/src/emit/valu_tests.rs` - `cargo test -p victory-shader --locked --lib emit::valu_tests` *(compiles/tests run in this Linux environment; the newly added NaN min/max tests are Windows-gated by `valu_tests` module cfg)* - not run: Windows/D3D12/WARP/game repro or GPU capture ## Risks / follow-ups - May not address the reported visual artifact if the root cause is elsewhere (resource state, format conversion, blending, tonemap, or missing GPU feature). - Needs Windows/D3D12 validation and, ideally, a PIX/GPU capture or before/after screenshot from the reported scene. AI-assisted: yes
Explicitly handle NaN operands in float max/min lowering: if one operand is NaN return the other, if both are NaN return the first. This matches RDNA2/DXIL FMax/FMin semantics and avoids driver-dependent NaN propagation that could black out color-sensitive passes. Add Windows-gated valu tests for f32/f16 min/max/min3/max3 NaN handling. Not validated against the reported sun/water artifact in this Linux sandbox (no game files, no Windows/D3D12/GPU capture).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 19:53:02 +00:00
github-bot force-pushed github-bot/gh-3352 from 34bdc28742 to a9650b2fe3 2026-11-26 20:09:25 +00:00 Compare
Author
Owner

@⁠Jackster on GitHub · 2026-11-26 20:04 UTC · original

AMD/WARP check at 16c700e: both new tests fail, and the failure is the fixture, not the lowering.

v_max3_f32 [NaN, 1.0, 2.0] returns 2.0 (0x40000000) on the RX 7900 XTX and the same value on WARP; the fixture expects 1.0 (0x3f800000). v_max3_f16 does the same (0x4000 vs 0x3c00).

For a three-operand max the NaN is ignored and the result is the max of the other two operands, so 2.0 is right: the ([nan, a, b], a, a) and ([a, nan, b], a, a) rows only hold for v_max_f32/v_max_f16, where the other operand is unambiguous. The min3 rows are fine (min(1.0, 2.0) = 1.0), and both two-operand forms pass on both backends.

Suggested fix: for v_max3_* expect b (or give the two non-NaN operands the same value). Since the test stops at v_max3_f32, the remaining ops do not run — after the change, this should complete.

Hardware: AMD Radeon RX 7900 XTX (RDNA3, driver 32.0.32029.2010), Windows 11. Synthetic shaders only, no game files. AI-assisted.

**@⁠Jackster** on GitHub · 2026-11-26 20:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/3352#issuecomment-7202267422) AMD/WARP check at `16c700e`: both new tests fail, and the failure is the fixture, not the lowering. `v_max3_f32 [NaN, 1.0, 2.0]` returns `2.0` (`0x40000000`) on the RX 7900 XTX and the same value on WARP; the fixture expects `1.0` (`0x3f800000`). `v_max3_f16` does the same (`0x4000` vs `0x3c00`). For a three-operand max the NaN is ignored and the result is the max of the **other two** operands, so `2.0` is right: the `([nan, a, b], a, a)` and `([a, nan, b], a, a)` rows only hold for `v_max_f32`/`v_max_f16`, where the other operand is unambiguous. The `min3` rows are fine (`min(1.0, 2.0) = 1.0`), and both two-operand forms pass on both backends. Suggested fix: for `v_max3_*` expect `b` (or give the two non-NaN operands the same value). Since the test stops at `v_max3_f32`, the remaining ops do not run — after the change, this should complete. Hardware: AMD Radeon RX 7900 XTX (RDNA3, driver 32.0.32029.2010), Windows 11. Synthetic shaders only, no game files. AI-assisted.
Author
Owner

@⁠Thomas73737 on GitHub · 2026-11-26 20:09 UTC · original

Thanks for the check. Fixed in a9650b2: updated the NaN valu test fixtures so v_max3/v_min3 expect max/min of the remaining operands when a NaN is present (e.g. [NaN,1,2] -> max3 2, min3 1); 2-op cases unchanged. Pushed to the PR.

**@⁠Thomas73737** on GitHub · 2026-11-26 20:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/3352#issuecomment-7202369762) Thanks for the check. Fixed in `a9650b2`: updated the NaN valu test fixtures so `v_max3/v_min3` expect max/min of the remaining operands when a NaN is present (e.g. `[NaN,1,2]` -> max3 `2`, min3 `1`); 2-op cases unchanged. Pushed to the PR.
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 20:16 UTC · original · reviewed

Tested on RTX 5090 (Windows / D3D12) against origin/main (63bcca4). Both new tests in valu_tests.rs fail:

emit::valu_tests::max_and_min_treat_a_nan_as_the_other_operand:
  assertion `left == right` failed: hardware: v_max3_f32 [7fc00000, 3f800000, 40000000, 0]
    left: 1073741824 (0x40000000 = 2.0)
   right: 1065353216 (0x3f800000 = 1.0)

emit::valu_tests::f16_max_and_min_treat_a_nan_as_the_other_operand:
  assertion `left == right` failed: hardware: v_max3_f16 [7e00, 3c00, 4000, 0]
    left: 16384 (0x4000 = 2.0)
   right: 15360 (0x3c00 = 1.0)

In both tests, cases defines expected outputs assuming 2-operand min/max (ignoring src2 = 2.0: [nan, 1.0, 2.0] -> 1.0, [nan, nan, 2.0] -> nan), and then reuses those same expected values for 3-operand v_max3_* / v_min3_* — where src2 = 2.0 participates, so v_max3(NaN, 1.0, 2.0) = 2.0 and v_max3(NaN, NaN, 2.0) = 2.0. Splitting the expected values (or separate case tables) for 2-operand vs 3-operand ops will fix both tests.

cargo clippy -p victory-shader --all-targets -- -D warnings and cargo xtask policy (191 files ok) both pass.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 20:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/3352#pullrequestreview-6080035338) · **reviewed** Tested on RTX 5090 (Windows / D3D12) against `origin/main` (`63bcca4`). Both new tests in `valu_tests.rs` fail: ``` emit::valu_tests::max_and_min_treat_a_nan_as_the_other_operand: assertion `left == right` failed: hardware: v_max3_f32 [7fc00000, 3f800000, 40000000, 0] left: 1073741824 (0x40000000 = 2.0) right: 1065353216 (0x3f800000 = 1.0) emit::valu_tests::f16_max_and_min_treat_a_nan_as_the_other_operand: assertion `left == right` failed: hardware: v_max3_f16 [7e00, 3c00, 4000, 0] left: 16384 (0x4000 = 2.0) right: 15360 (0x3c00 = 1.0) ``` In both tests, `cases` defines expected outputs assuming 2-operand `min`/`max` (ignoring `src2 = 2.0`: `[nan, 1.0, 2.0] -> 1.0`, `[nan, nan, 2.0] -> nan`), and then reuses those same expected values for 3-operand `v_max3_*` / `v_min3_*` — where `src2 = 2.0` participates, so `v_max3(NaN, 1.0, 2.0) = 2.0` and `v_max3(NaN, NaN, 2.0) = 2.0`. Splitting the expected values (or separate case tables) for 2-operand vs 3-operand ops will fix both tests. `cargo clippy -p victory-shader --all-targets -- -D warnings` and `cargo xtask policy` (191 files ok) both pass. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠Thomas73737 on GitHub · 2026-11-26 20:18 UTC · original

Thanks for testing on RTX 5090. This was the same fixture issue Jackster reported: the 3-op NaN cases reused 2-op expectations. Fixed in a9650b2 (already pushed): v_max3/v_min3 now expect max/min of the remaining operands when a NaN is present ([NaN,1,2] -> max3 2, min3 1; [NaN,NaN,2] -> 2), with 2-op cases unchanged.

**@⁠Thomas73737** on GitHub · 2026-11-26 20:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/3352#issuecomment-7202517768) Thanks for testing on RTX 5090. This was the same fixture issue Jackster reported: the 3-op NaN cases reused 2-op expectations. Fixed in `a9650b2` (already pushed): `v_max3/v_min3` now expect max/min of the remaining operands when a NaN is present (`[NaN,1,2]` -> max3 `2`, min3 `1`; `[NaN,NaN,2]` -> `2`), with 2-op cases unchanged.
Author
Owner

@⁠upipibi on GitHub · 2026-11-26 20:29 UTC · original

@⁠Thomas73737 check on a9650b2 fails only on frame::tests::waits_end_on_ticks_and_count_frames (crates/victory-gpu/src/frame.rs:231, left: 0 right: 1), a timing test in a crate this PR does not touch; #1362 hit the same failure and #2877 describes the cause. Re-run check (or push an empty commit). Cargo stopped there, so victory-shader's tests, which cover your change, have not run in CI yet. Fix any failure the re-run shows.

AI-assisted (Claude Code, Claude Sonnet 5.5).

**@⁠upipibi** on GitHub · 2026-11-26 20:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/3352#issuecomment-7202744204) @⁠Thomas73737 `check` on a9650b2 fails only on `frame::tests::waits_end_on_ticks_and_count_frames` (crates/victory-gpu/src/frame.rs:231, `left: 0 right: 1`), a timing test in a crate this PR does not touch; #1362 hit the same failure and #2877 describes the cause. Re-run `check` (or push an empty commit). Cargo stopped there, so victory-shader's tests, which cover your change, have not run in CI yet. Fix any failure the re-run shows. AI-assisted (Claude Code, Claude Sonnet 5.5).
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-26 20:35 UTC · original · edited 2026-11-26 20:37 UTC

The corrected fixtures at a9650b2 passed three consecutive runs on app-local Agility 1.620.2 / WARP 1.0.22 with the DXIL validator. Both NaN tests and both signed-zero tests passed each time, including the FP16 and FP32 cases. The same fixtures passed three runs in a control that restored the previous max_min_flushed lowering. That confirms the new expectations on WARP. The reported sun/water artifact still needs a capture or adapter result that distinguishes the two lowerings.

I also checked the failed CI job. The failing assertion is the frame clock's zero-timeout poll after the second tick. The test-only change in #2265 already covers this timing assumption. @⁠Thomas73737, the two new shader tests are ready for a retest on the RX 7900 XTX or RTX 5090 using a9650b2.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 20:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/3352#issuecomment-7202872015) · edited 2026-11-26 20:37 UTC The corrected fixtures at `a9650b2` passed three consecutive runs on app-local Agility 1.620.2 / WARP 1.0.22 with the DXIL validator. Both NaN tests and both signed-zero tests passed each time, including the FP16 and FP32 cases. The same fixtures passed three runs in a control that restored the previous `max_min_flushed` lowering. That confirms the new expectations on WARP. The reported sun/water artifact still needs a capture or adapter result that distinguishes the two lowerings. I also checked the [failed CI job](https://github.com/Marvisak/VIctory/actions/runs/45888817119/job/140146112990). The failing assertion is the frame clock's zero-timeout poll after the second tick. The test-only change in #2265 already covers this timing assumption. @⁠Thomas73737, the two new shader tests are ready for a retest on the RX 7900 XTX or RTX 5090 using `a9650b2`.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 20:40 UTC · original · requested changes

Requesting changes on a9650b2f for a dual-NaN selection bug in max_min_flushed and unrelated whole-file diff churn:

Requested changes

  1. [P2] Two-NaN tie selects b instead of a (crates/victory-shader/src/emit/lower.rs:2223-2226):
    let r = self.s.m.select(c, b_nan, a, r);
    self.s.m.select(c, a_nan, b, r)
    
    When both a and b are NaN, the second select overrides a and returns b (source 1). This contradicts both the PR summary ("and the first operand when both are NaN") and the repository's RDNA2 hardware reference in crates/victory-shader/src/emit/semantics_tests.rs:889-896 ((true, true) | (false, true) => a), and also causes all-NaN v_min3_*/v_max3_*/v_med3_* to return the last operand rather than the first. Swapping the two selects (select(c, a_nan, b, r) first, then select(c, b_nan, a, r)) fixes both the binary and cascaded ternary paths.
  2. [P3] Scope out unrelated whole-file formatting diffs:
    The PR currently includes ~400 lines of unrelated rustfmt reformatting across emit/lower.rs and emit/valu_tests.rs (plus three unmentioned #[cfg(windows)] guards in lower.rs:11691, 11745, 11796). Keeping lower.rs changes scoped strictly to max_min_flushed (and moving/documenting the #[cfg(windows)] test guards separately if intended) avoids unnecessary merge conflicts in a size-capped hot file (522,541 bytes).
  3. [P3] Strengthen NaN payload test coverage (emit/valu_tests.rs:790, 836):
    The new tests pass identical quiet NaNs (f32::NAN / 0x7E00) and check only .is_nan(), so they cannot catch the dual-NaN payload inversion. Using distinct quiet NaN payloads (e.g., 0x7FC0_0001 vs. 0x7FC0_0002, plus [a, b, nan] and three distinct NaNs for min3/max3) will directly guard the rule.

(Local verification on a9650b2f: clippy default & --all-features clean; emit::valu_tests 37 passed, 0 failed on RTX 5090 + WARP.)

**@⁠fuzeofficlal** on GitHub · 2026-11-26 20:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/3352#pullrequestreview-6080279421) · **requested changes** Requesting changes on `a9650b2f` for a dual-NaN selection bug in `max_min_flushed` and unrelated whole-file diff churn: ### Requested changes 1. **[P2] Two-NaN tie selects `b` instead of `a` (`crates/victory-shader/src/emit/lower.rs:2223-2226`):** ```rust let r = self.s.m.select(c, b_nan, a, r); self.s.m.select(c, a_nan, b, r) ``` When both `a` and `b` are NaN, the second `select` overrides `a` and returns `b` (source 1). This contradicts both the PR summary (*"and the first operand when both are NaN"*) and the repository's RDNA2 hardware reference in `crates/victory-shader/src/emit/semantics_tests.rs:889-896` (`(true, true) | (false, true) => a`), and also causes all-NaN `v_min3_*`/`v_max3_*`/`v_med3_*` to return the last operand rather than the first. Swapping the two `select`s (`select(c, a_nan, b, r)` first, then `select(c, b_nan, a, r)`) fixes both the binary and cascaded ternary paths. 2. **[P3] Scope out unrelated whole-file formatting diffs:** The PR currently includes ~400 lines of unrelated `rustfmt` reformatting across `emit/lower.rs` and `emit/valu_tests.rs` (plus three unmentioned `#[cfg(windows)]` guards in `lower.rs:11691, 11745, 11796`). Keeping `lower.rs` changes scoped strictly to `max_min_flushed` (and moving/documenting the `#[cfg(windows)]` test guards separately if intended) avoids unnecessary merge conflicts in a size-capped hot file (`522,541` bytes). 3. **[P3] Strengthen NaN payload test coverage (`emit/valu_tests.rs:790, 836`):** The new tests pass identical quiet NaNs (`f32::NAN` / `0x7E00`) and check only `.is_nan()`, so they cannot catch the dual-NaN payload inversion. Using distinct quiet NaN payloads (e.g., `0x7FC0_0001` vs. `0x7FC0_0002`, plus `[a, b, nan]` and three distinct NaNs for `min3`/`max3`) will directly guard the rule. *(Local verification on `a9650b2f`: `clippy` default & `--all-features` clean; `emit::valu_tests` 37 passed, 0 failed on RTX 5090 + WARP.)*
Author
Owner

@⁠Jackster on GitHub · 2026-11-26 21:02 UTC · original

Follow-up at a9650b2: the corrected fixtures pass on AMD.

cargo test -p victory-shader --lib treat_a_nan_as_the_other_operand — 2 passed on the RX 7900 XTX (RDNA3, driver 32.0.32029.2010) and WARP, with no backend skipped. That covers the two cases that failed at 16c700e.

Synthetic shaders only, no game files. AI-assisted.

**@⁠Jackster** on GitHub · 2026-11-26 21:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/3352#issuecomment-7203323900) Follow-up at `a9650b2`: the corrected fixtures pass on AMD. `cargo test -p victory-shader --lib treat_a_nan_as_the_other_operand` — 2 passed on the RX 7900 XTX (RDNA3, driver 32.0.32029.2010) and WARP, with no backend skipped. That covers the two cases that failed at `16c700e`. Synthetic shaders only, no game files. AI-assisted.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3352/head:github-bot/gh-3352'
git switch 'github-bot/gh-3352'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3352'
git switch 'github-bot/gh-3352'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3352'
git switch 'github-bot/gh-3352'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3352'
git switch 'main'
git merge --squash 'github-bot/gh-3352'
git switch 'main'
git merge --ff-only 'github-bot/gh-3352'
git switch 'main'
git merge 'github-bot/gh-3352'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3352
No description provided.