Skip to content

Shaders: min, max and med3 read f32 denormals as signed zeros (#136) - #229

Open
bawhen wants to merge 1 commit into
fxceless:mainfrom
bawhen:shader/min-max-med3-denormals
Open

bawhen wants to merge 1 commit into
fxceless:mainfrom
bawhen:shader/min-max-med3-denormals

Conversation

@bawhen

@bawhen bawhen commented Nov 24, 2026

Copy link
Copy Markdown

Refs #136: the "fp32 denormals and the sign of a flushed zero (v_min/max/med3_f32)" row.

Problem. The console's float mode reads an f32 denormal operand of v_min/max/min3/max3/med3_f32 as a zero of its sign. The translation left that to the host's denormal mode, which varies. And v_med3_f32 didn't order -0 below +0 the way v_min/max_f32 already do here: it used the host's FMin/FMax directly.

Change. A port of the min/max/med3 part of upstream Marvisak/VIctory 77053a0 ("accuracy fixes for discards, f16 rounding, denormals, med3 and signed zeros"). The fork's code here still matched upstream's from before that commit.

  • ftz: an f32 denormal becomes a zero of its sign, decided on the bits.
  • max_min flushes both operands, then calls max_min_flushed, the existing -0-below-+0 ordering.
  • med3 flushes its three operands and is built from max_min_flushed, so its median orders -0 below +0 too. The fork's NaN rule (the minimum of the three) is unchanged.

The f16 forms (v_max/min/med3_f16, v_pk_min/max_f16) go through the same helpers on values widened to f32. ftz never changes those, since the smallest f16 denormal (2^-24) is a normal f32.

The rest of 77053a0 is out of scope: discards, the f16 rounding mode, output modifiers, the FMA probe, and its test file.

Test. min_max_and_med3_read_denormals_as_signed_zeros runs all five instructions over every triple of seven values on the hardware adapter and WARP: ±0, ±denormal, the largest denormal, the smallest normal, and -1. Each result is compared bit for bit with a CPU reference that flushes and orders -0 below +0.

Results on an AMD Radeon RX 7700 (RDNA3, 16 GB, driver 32.0.32029.2010) and WARP, Windows 11:

RX 7700 WARP
main (4e17478) 0 wrong 223 wrong: v_min 35, v_max 35, v_min3 50, v_max3 37, v_med3 66
this PR 0 wrong 0 wrong

Examples on main (WARP):

  • v_min_f32(-0, +denormal) = +0, want -0. WARP flushes the denormal, so the -0/+0 rule never sees two zeros.
  • v_med3_f32(+0, +0, -0) = -0, want +0. No denormal is involved: the median ignored the zero ordering.

This RX 7700 is not affected. It already gives the console's result in all 343 cases for all five instructions. The fix is for hosts that don't flush, plus CI's WARP adapter. Upstream's 77053a0 names an RX 6700 XT (RDNA2) as one that doesn't flush, which suggests RDNA3 and RDNA2 drivers differ here. I haven't tested an RDNA2 card or NVIDIA.

  • cargo test --workspace --locked: 497 passed, 0 failed, 7 ignored (main's 496 plus the new test).
  • cargo clippy --workspace --all-targets [--all-features] --locked -- -D warnings: clean.
  • cargo xtask policy: ok. cargo fmt --check adds no differences in the two changed files beyond what main already has.
  • No game files used. All inputs are synthetic.

AI use: Claude Code (Claude Opus 5.5) found the upstream fix, wrote the port and the test, and ran them on my card; opened at my request.

🤖 Generated with Claude Code

The console's float mode reads f32 denormal operands of
v_min/max/min3/max3/med3_f32 as zeros of their sign. The translation
left that to the host, whose denormal mode varies, and v_med3_f32 took
-0 and +0 in whatever order the host's FMin/FMax gave. Port of the
min/max/med3 part of upstream Marvisak/VIctory 77053a0: `ftz` decides
the flush on the bits, `max_min` flushes and orders -0 below +0, and
`med3` is built from the same ordered minimum and maximum.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@towiice towiice left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Windows 11 (NVIDIA GeForce GTX 1650 SUPER, 4 GB Turing TU116, driver 610.88 + Microsoft Basic Render Driver / WARP):

  • \cargo test -p victory-shader --lib min_max_and_med3_read_denormals_as_signed_zeros: 1 passed; 0 failed (111.77s)

Expanding the \�_min_f32\ / \�_max_f32\ / \�_med3_f32\ flush-to-zero check ((m.2 & 3) == 0) to also flush denormal operands when \�bs == 0 && neg == 0, and routing \�_med3_f32\ through pairwise \ min/\ max\ under \ 32 != 3\ so each input is flushed before comparison, behaves cleanly on Turing + WARP.

@YeokiNoewChi

Copy link
Copy Markdown

RTX 3070 Ti 8 GB (32-lane waves), Windows 11, Series X profile. Tree: this fork with Emiriley's bench7 stack (which merges this PR) plus a few NVIDIA-side changes, VICTORY_SHADER_LOOP_LIMIT=256.

With this PR the main character's hair breaks. In the prologue cell, with a warm cache and in steady state, the hair is drawn as scattered shards with a dark wedge through it, and draws per frame drop from about 5,100 to about 3,400. Reverting only this PR's merge (and nothing else) brings the hair back to normal on the same cache and camera position.

Offline, the hair-strand program is where it changes. I translated the 70 programs whose names contain "hair" with VICTORY_EMIT_DUMP and compared the emitted DXIL with and without this PR: 60 differ. In bvh_skinning_smh:CSGenerateHairVB (#72f552f4553fb5f9), which builds the strands' vertex buffer, the only change is this pattern added five times around min/max operands:

and i32 %x, 2139095040      ; exponent bits
icmp eq i32 %e, 0
and i32 %x, -2147483648     ; sign bit
select i1 %c, i32 %sign, i32 %x
bitcast i32 ... to float

So any operand whose exponent bits are zero is replaced by a signed zero. My guess is that this program runs v_min_f32/v_max_f32 on values that are not normal floats (small integer or packed bit patterns, which look like denormals), and the flush destroys them. That would match the shards.

A possible way forward: limit the flush to operands known to be floats, or put it behind a switch until the console's denormal mode for these instructions is confirmed.

Hashes and counts only, nothing from the game. AI-assisted (Claude Code).

triippywhale pushed a commit to triippywhale/victory-fork that referenced this pull request Nov 25, 2026
…pload memory read together, on by default

A list's fills of upload memory from title memory (constants a few hundred
bytes long, some 12,000 a world frame) were each a kernel read of their
own. Between two steps that write title memory, a submission's fills from
all its lists are now gathered, sorted by where they read, and the ones
less than 4 KiB apart are read by one kernel call (spans of 1 MiB at most)
and handed out from there. A span the kernel cannot read whole goes fill by
fill as before. Ordering against title writes is unchanged: the fills are
done before any step that writes title memory and at the submission's end.

On by default as PalmVIDev fxceless#245 ships it (EricPlayZ measured the
submitting thread's copies at 37.5 -> 31.4 ms a frame on an RTX 2070
SUPER); VICTORY_JOIN_FILLS=0 turns it off. Ported into this tree's
unsplit host.rs (PalmVIDev has it in host/list_ops.rs).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017V7WBA8CxPD68KngjiMeJb
@HAL9000RELOADED

Copy link
Copy Markdown

RX 7900 XTX (RDNA3), Windows 10 + #402, main d510ee4: min_max_and_med3_read_denormals_as_signed_zeros passes with this PR. It also passes with the test alone on main, so this card already reads those denormals as the console does. The PR changes nothing on RDNA3 and breaks nothing. Run table: #129 (#129 (comment)).

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.

4 participants