Repository navigation
fix(shader): preserve NaN operands in float min/max lowering - #3352
Thomas73737 wants to merge 1 commit into
Conversation
869921e to
16c700e
Compare
|
AMD/WARP check at
For a three-operand max the NaN is ignored and the result is the max of the other two operands, so Suggested fix: for Hardware: AMD Radeon RX 7900 XTX (RDNA3, driver 32.0.32029.2010), Windows 11. Synthetic shaders only, no game files. AI-assisted. |
|
Thanks for the check. Fixed in |
Dukestar69
left a comment
There was a problem hiding this comment.
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).
|
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 |
|
@Thomas73737 AI-assisted (Claude Code, Claude Sonnet 5.5). |
|
The corrected fixtures at 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 |
fuzeofficlal
left a comment
There was a problem hiding this comment.
Requesting changes on a9650b2f for a dual-NaN selection bug in max_min_flushed and unrelated whole-file diff churn:
Requested changes
- [P2] Two-NaN tie selects
binstead ofa(crates/victory-shader/src/emit/lower.rs:2223-2226):When bothlet r = self.s.m.select(c, b_nan, a, r); self.s.m.select(c, a_nan, b, r)
aandbare NaN, the secondselectoverridesaand returnsb(source 1). This contradicts both the PR summary ("and the first operand when both are NaN") and the repository's RDNA2 hardware reference incrates/victory-shader/src/emit/semantics_tests.rs:889-896((true, true) | (false, true) => a), and also causes all-NaNv_min3_*/v_max3_*/v_med3_*to return the last operand rather than the first. Swapping the twoselects (select(c, a_nan, b, r)first, thenselect(c, b_nan, a, r)) fixes both the binary and cascaded ternary paths. - [P3] Scope out unrelated whole-file formatting diffs:
The PR currently includes ~400 lines of unrelatedrustfmtreformatting acrossemit/lower.rsandemit/valu_tests.rs(plus three unmentioned#[cfg(windows)]guards inlower.rs:11691, 11745, 11796). Keepinglower.rschanges scoped strictly tomax_min_flushed(and moving/documenting the#[cfg(windows)]test guards separately if intended) avoids unnecessary merge conflicts in a size-capped hot file (522,541bytes). - [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_0001vs.0x7FC0_0002, plus[a, b, nan]and three distinct NaNs formin3/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.)
|
Follow-up at
Synthetic shaders only, no game files. AI-assisted. |
|
Updated in
I did not reformat/scope the earlier The previous CI failure was unrelated |
fuzeofficlal
left a comment
There was a problem hiding this comment.
Thanks for fixing max_min_flushed at crates/victory-shader/src/emit/lower.rs:2235-2238 so dual-NaN selection keeps the first operand a, and for adding distinct-payload coverage. However, two issues block approval on 0f0a362e:
- New f16 test fails on hardware GPU (
crates/victory-shader/src/emit/valu_tests.rs:865): Runningcargo test --locked -p victory-shader --lib -- --test-threads=1 emit::valu_tests:: emit::lower::passes 70/70 underVICTORY_WARP=1, but fails 69/70 underVICTORY_WARP=0on RTX 5090:Becausehardware: v_max_f16 [7e01, 7e02, 4000, 0] left: 32767 right: 32257half_fsrc(lower.rs:6921) widens f16 operands vialegacyF16ToF32beforemax_min_flushedandnarrow16, adapters that canonicalize f16 NaN payloads return0x7FFF(32767) rather than preserving0x7E01(32257) (comparev_med3_f16_matches_rdna2_with_nanatvalu_tests.rs:950-953, which assertsis_f16_nan(got_h)for the all-NaN f16 case). Please avoid asserting exact non-canonical f16 NaN payload bits on host execution unless the f16 payload is preserved end-to-end without host canonicalization. - Unrelated formatting & guard noise still present:
git diff 63bcca43..0f0a362estill contains ~35+ unrelatedrustfmthunks acrosslower.rs(lines528–11883) andvalu_tests.rs(lines239–979), plus three unrelated#[cfg(windows)]additions inlower.rs(11700,11754,11805). Please revert the unrelated formatting/#[cfg(windows)]hunks so the diff stays scoped strictly tomax_min_flushedand the new NaN tests.
fuzeofficlal
left a comment
There was a problem hiding this comment.
Thanks for fixing the f16 NaN failure — 8d55a54a now passes all 70 emit::valu_tests:: and emit::lower:: tests on both RTX 5090 (VICTORY_WARP=0) and WARP (VICTORY_WARP=1), as well as rustfmt --edition 2024 --check and cargo clippy.
However, the unrelated diff cleanup requested in the previous review is still missing: relative to 63bcca43, the PR still carries 47 formatting-only hunks (38 in crates/victory-shader/src/emit/lower.rs and 9 in crates/victory-shader/src/emit/valu_tests.rs) plus the 3 unrelated #[cfg(windows)] validator guards at lower.rs:11703, lower.rs:11757, and lower.rs:11808, alongside only ~14 functional lines (lower.rs:2233-2241 and valu_tests.rs:857-866).
Please drop the unrelated rustfmt churn and the three #[cfg(windows)] hunks so the diff is scoped strictly to the max_min_flushed NaN fix and its test update.
|
Updated in
Combined with |
|
Thomas73737, both updated NaN tests passed on exact head 562a3cd checks the operand magnitudes as integers against the infinity bits. The same shader dumped by DXC now has both unsigned integer comparisons. All 37 VALU tests passed on both revisions, including the new negative-NaN and infinity cases on the follow-up. Strict shader Clippy and asset policy passed. The commit is ready for review or cherry-pick. A run on an adapter that exhibited the driver fold would verify the hardware side. Please retain the Git author if you take the commit. If folding it into another commit, please add AI-assisted with Codex. Synthetic shaders only. |
Handle NaN operands explicitly in float max/min lowering: if one operand is NaN return the other, if both are NaN keep the first operand's NaN. Classify NaNs by magnitude bits to avoid host floating-point comparison folding quirks. Add Windows-gated valu tests for f32/f16 min/max/min3/max3 NaN handling, including distinct NaN payloads, negative NaNs, and all-NaN 3-op cases. Not validated against the reported sun/water artifact in this Linux sandbox (no game files, no Windows/D3D12/GPU capture). Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com> AI-assisted: yes
b6ac474 to
e5993b0
Compare
|
Updated in
Local: |
…rands explicitly A NaN operand of v_min/v_max (f32, f16, and the min3/max3 built on them) gives the other operand on GFX10 (either IEEE mode, quiet NaNs); DXIL's FMax/FMin specifies the same. The switch spells it out with integer NaN tests a driver cannot fold away. A new test runs one-NaN and two-NaN cases with and without it: the RTX 4070 and WARP already give the hardware's result without, so it stays off. v_med3 (min3 on a NaN) and clamp (NaN to 0, DX10_CLAMP) were already right; GFX10 has no v_min/v_max_legacy. Reimplements Marvisak/VIctory#3352 (Thomas73737, 16c700e4), which uses fcmp uno.
|
I reran both suites on exact head The earlier RTX 5090 result was on |
|
I combined your NaN-selection change at exact On WARP with Agility 1.620.2, all 438,976 output words matched your head exactly, across both opcodes, four modifier configurations, and NaNs, signed zeros, subnormals and infinities. All 37 VALU tests passed, including your f32/f16 NaN checks. The existing min/max/clamp model also passed. DXIL counts against your head went from 96 to 90 for one min3 or max3 and 712 to 640 for a 12-operation chain. Strict all-feature shader Clippy and the asset policy passed. @Thomas73737, the combined tree is ready for merge coordination. Please retain Git authorship on cherry-pick or rebase, or add AI-assisted with OpenAI Codex. |
|
I combined #3744 at Against the previous integration, all 47,790 output words matched on Windows WARP with Agility 1.620.2. The test covers both literal positions, signed zeros, subnormals, infinities, signaling and quiet NaNs, and a dynamic control. The ordinary literal max went from 64 to 51 DXIL instructions and the negative-zero min went from 64 to 57. The dynamic control stayed at 66. All 37 VALU tests passed, including the f32 and f16 NaN tests. The min/max/clamp model, 13 bitcode tests, strict all-feature shader Clippy and the asset policy also passed. @Thomas73737, the fork commit is available for merge coordination. Please retain the Git authors on cherry-pick or rebase, or add AI-assisted with OpenAI Codex. |
Summary
max_min_flushedexplicitly return the other operand when one float min/max operand is NaN, and the first operand when both are NaNvalu_testscoverage for f32/f16v_min/v_max/v_min3/v_max3NaN operand handlingRationale
RDNA2 and DXIL
FMax/FMinsemantics 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.rscargo 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 byvalu_testsmodule cfg)Risks / follow-ups
AI-assisted: yes