Skip to content

Shaders: lower ds_min_f32 / ds_max_f32 (and _rtn) with RDNA2's NaN rules - #628

Open
mercuryVM wants to merge 7 commits into
Marvisak:mainfrom
mercuryVM:impl/lds-float-minmax
Open

mercuryVM wants to merge 7 commits into
Marvisak:mainfrom
mercuryVM:impl/lds-float-minmax

Conversation

@mercuryVM

@mercuryVM mercuryVM commented Nov 24, 2026 •

Copy link
Copy Markdown

What

ds_min_f32, ds_max_f32, ds_min_rtn_f32 and ds_max_rtn_f32 were refused (an LDS operation), so any compute or mesh program that used one failed to translate. DXIL has no float atomics on groupshared memory, so each now lowers to a compare-exchange loop. The loop reads the dword, computes what RDNA2 would write, and retries cmpxchg until no other lane changed the dword in between.

What RDNA2 writes follows @Lyrnc's measurement on gfx1036, all pairs of 17 values in four modes, reported in a comment below:

  • An sNaN, data first, then memory, is written back quieted.
  • Otherwise a NaN loses to a number, whichever side it is on. Two quiet NaNs give the data.
  • Numbers compare as floats with -0 below +0. The order is computed on integers: the bits of negative floats get their magnitude reversed.
  • The _rtn forms return the dword's value from before.

The first commit used a pair of integer atomics (signed max plus unsigned min). The numbers come out the same, but that cannot express NaNs already in memory. The second commit replaces it with the loop after the measurement.

Known gap, marked shortcut: in the code: f32 denormals compare as kept. With the console's MODE flushing them, they would order as ±0 first, and the translator does not model MODE.

Helpers Lower::lds_float_min_max (the loop) and Lower::lds_float_pick (the choice) are in crates/victory-shader/src/emit/lower.rs. In valu_tests.rs, the test builder now takes a thread count and an LDS size (build_cs; build_valu_cs calls it unchanged), and run_cases_on picks the devices.

Effect on the title: none

Measured by @Parakeet-Wheat on the retail 1.0.2.1 shaders, in a comment below: the title contains 0 ds_{min,max}{,_rtn}_f32; its LDS minimum and maximum are all integer (ds_max_u32, ds_min_u32, ds_max_i32). victory translate --list --validate gives the same outcome on main and on this PR: 21,736 validated, 4,003 failed, 162 rejected. So this PR only completes the instruction set. It doesn't unblock anything in the game, and it can wait behind work that does.

Overlap

I searched open and closed PRs: #164 adds the integer LDS atomics with cmpst. No PR handles the float forms; #163 lists them as refused.

Tested

Windows 11, NVIDIA RTX 5070, AMD CPU. No game data.

  • ds_min_max_f32_order_floats_and_return_the_old_value: a one-thread program, for every pair from 19 values (±0, ±1, ±2.5, ±1e-30, ±denormal, ±inf, ±MAX, plus quiet and signalling NaNs of both signs) in memory and as data, for both ops. It checks the dword afterwards (read back with ds_read_b32) and the old value returned by _rtn, against a reference that follows the measured model. Hardware only: a one-wave program with LDS declares WaveSize(32) (existing behaviour), and WARP's pipeline creation refuses it.
  • ds_min_max_f32_are_atomic_across_waves: 64 threads each apply (thread - 32) as a float to one dword after a barrier. Checks max from -inf (31), max from 100 (100), min from +inf (-32) and min from -0 (-32). Runs on hardware and WARP.
  • Mutation check: dropping the "a NaN in memory loses to a number" rule fails the first test on hardware (ds_max_f32 on NaN [0x7fc00000] with 0 gave 0x7fc00000, want 0).
  • cargo test --workspace: all passed (victory-shader 71 passed). rustfmt --edition 2024 --check on both files: clean. cargo clippy -p victory-shader --all-targets: no warnings. cargo xtask policy: 151 files ok.

Not tested

  • The title was not run, and I have no shader package, so I can't say how many of the title's programs use these instructions. None of the 12 programs embedded in the executable does.
  • No console hardware. The NaN rules come from @Lyrnc's RDNA2 desktop measurement (gfx1036), not the console's silicon. Only the f32 denormals-kept mode is modelled.
  • No 64-lane (AMD) host GPU.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

The LDS float minimum and maximum were refused ("an LDS operation"), so
any program using one failed to translate. DXIL has no float atomics on
groupshared memory, so they become integer atomics: floats with the sign
bit clear order as signed integers, those with it set in reverse as
unsigned ones. The maximum is a signed max for non-negative data and an
unsigned min for the rest (the minimum: signed min, unsigned max). Each
lane issues both atomics, one with a value that leaves the dword as it
is, so each lane still changes it in a single atomic step; -0 orders
below +0, and NaN data leaves the dword. The _rtn forms return the old
value.

Tests: a one-thread program checks every pair of 14 values (and NaN
data) for both the dword afterwards and the returned old value; a
64-thread program checks the result under contention across waves.
The test builder now takes a thread count and an LDS size.

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

@LAKSHYA-cmd-tech LAKSHYA-cmd-tech 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.

Tested on Windows 11 with Rust 1.98.1 (x86_64-pc-windows-msvc).

  • cargo test -p victory-shader: all 71 tests passed (0 failed). Both new hardware and cross-wave atomic tests (ds_min_max_f32_order_floats_and_return_the_old_value and ds_min_max_f32_are_atomic_across_waves) succeeded cleanly.
  • cargo xtask policy: 151 files ok.
  • Verified integer atomic bit-representation transformation logic for float LDS min/max in emit/lower.rs.

@Lyrnc

Lyrnc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

RDNA2 hardware measurement of ds_min_f32 / ds_max_f32: the ordering of numbers, ±0 included, matches this PR. The NaN rules differ, and the hardware's rule is simpler than the gap marked shortcut: here.

Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. In a HIP kernel (assembled by the AMD driver's amd_comgr), each lane writes a "memory" value to its own LDS dword, runs ds_{min,max}{,_rtn}_f32 with a "data" value, and reads the dword back. All 289 pairs of 17 values: ±0, ±1, ±2.5, ±smallest denormal, ±inf, ±max, ±qNaN, and three sNaNs. Each pair ran under IEEE mode on/off × f32 denormals flushed/kept. RDNA2 desktop silicon, not the console's. No game data.

This model fits 289/289 in all four modes, for min and max, with and without _rtn:

  1. Denormals follow the f32 denormal mode (flushed to ±0 first when the mode flushes). IEEE mode makes no difference.
  2. An sNaN, data first, then memory, is written back quieted: data 0x7f800001 gives 0x7fc00001.
  3. Otherwise a NaN loses to a number, whichever side it is on: data 0 with memory 0x7fc00000 writes 0, and NaN data leaves a number unchanged. With both quiet NaNs the data is kept.
  4. ±0: min gives -0 if either is -0, max gives +0 unless both are -0.
  5. Everything else is the plain float compare.
  6. _rtn returns the memory value from before, on every row.

Against this PR, with f32 denormals kept, every row without a NaN matches (182/289), including -0 < +0. The 107 others:

Case Hardware This PR
a quiet NaN already in the dword, number as data the number is written (min(0, qNaN) = 0) integer compare of the bits (the shortcut:)
sNaN data quieted sNaN written (0x7fc00001) dword unchanged
data number, sNaN in the dword quieted sNaN of the dword stays integer compare

With f32 denormals flushed there are 28 more: data 0 with memory 0x80000001 gives 0x80000000 on the hardware (the denormal flushed to -0, then -0 < +0), where the integer order keeps 0x80000001. So the integer ordering is right for numbers in the denormals-kept mode. The NaN handling could follow rules 2-3, which is two more selects rather than a float compare.

AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.

A hardware measurement on the pull request (gfx1036, all pairs of 17
values in four modes) showed the NaN rules differ from the integer
atomics' order: an sNaN, data first, then memory, is written quieted;
otherwise a NaN loses to a number, also a NaN already in the dword (two
quiet NaNs give the data). No single integer atomic can express the
memory side, so the operation is now a compare-exchange loop around the
measured choice. Numbers keep the same order (-0 below +0).

The test reference follows the measured model, with quiet and
signalling NaNs of both signs in memory and as data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mercuryVM mercuryVM changed the title Shaders: lower ds_min_f32 / ds_max_f32 (and _rtn) as integer atomics Shaders: lower ds_min_f32 / ds_max_f32 (and _rtn) with RDNA2's NaN rules Nov 24, 2026
@mercuryVM

Copy link
Copy Markdown
Author

@Lyrnc thank you, that measurement settles it. I followed your model in 5d07613: the integer-atomic pair is replaced by a compare-exchange loop around rules 2-5, since no single integer atomic can let a NaN already in the dword lose to a number. Both quiet NaNs give the data, as you reported. The test now has quiet and signalling NaNs of both signs, in memory and as data (361 pairs per op, dword afterwards and _rtn value). It passes on an RTX 5070, and dropping the 'NaN in memory loses' rule makes it fail. Rule 1 (denormals following the f32 mode) is left as a marked shortcut: the translator does not model MODE, so this matches the denormals-kept mode only.

@LAKSHYA-cmd-tech thanks for testing. The lowering changed after your approval (now a CAS loop), so it may be worth another look.

AI-assisted (Claude Code).

@Lyrnc

Lyrnc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Tested 5d07613 merged onto main d2a4bae (clean) on both GPUs here: pass.

  • ds_min_max_f32_* tests, RTX 5080 (driver 610.88): 2 passed.
  • Same tests on the Radeon iGPU (gfx1036, driver 32.0.21042.62), through GPU: prefer high-performance D3D12 adapter and support VICTORY_ADAPTER #94's VICTORY_ADAPTER=amd (local merge only): 2 passed. This is the hardware the model came from, now running the translated DXIL.
  • lds_min_max_ref is the rule set that fitted 289/289 on the hardware with f32 denormals kept: sNaN data first, then memory, quieted; a NaN loses to a number; two quiet NaNs keep the data; -0 < +0.

AI use: Claude Code (Claude Opus 5.5) ran the tests and drafted this comment.

@slint6614-dev

Copy link
Copy Markdown

AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and the Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the D3D12 device for cargo test -p victory-shader --release (local adapter-selection patch, not part of this PR).

  • Whole victory-shader suite: 71 passed, 0 failed on both GPUs; both new ds_min_max_f32_* tests pass on the hardware device.
  • Negative control: with lower.rs reverted to main both new tests fail (the opcodes are refused before reaching the GPU), as expected.

mercuryVM and others added 2 commits November 24, 2026 05:46
# Conflicts:
#	crates/victory-shader/src/emit/valu_tests.rs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mercuryVM

Copy link
Copy Markdown
Author

Merged current main (58fed37) in a5ae95f. Only the end of valu_tests.rs conflicted; run_cases_on keeps #433's one-at-a-time lock. victory-shader: 91 passed on the RTX 5070. @Lyrnc @slint6614-dev thank you for the runs on RDNA2, RDNA4 and the RTX 5080.

Note for testing: cargo test -p victory-shader does not compile on current main (#719, a call to lowers without its new rsrc2 argument; @LAKSHYA-cmd-tech has a fix branch). I ran the suite with that one-line fix applied locally, not committed here.

AI-assisted (Claude Code).

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Merges cleanly with main; macOS arm64, rustc 1.98.1, cargo test -p victory-shader --locked with the PR merged into main 8e79c80 → 70 passed / 0 failed / 1 ignored (1 ignored = Windows GPU execution suite).

The lowering is a CAS loop around lds_float_pick, and I checked the pick against the hardware reference in the new tests: sNaN quieted with data first, a NaN losing to a number, two quiet NaNs giving the data, and the sign-flip key ordering -0 below +0 — all match lds_min_max_ref. _rtn is handled by the existing replace("_rtn", "") at the atomic matcher, and the tests cover both the old-value-return and read-back forms, plus cross-wave atomicity on WARP and hardware.

Three notes, none blocking:

  1. The host-execution tests in valu_tests.rs are Windows-gated, so on macOS this PR is compile-checked only; the gfx1036 measurements in the description are the real evidence — CI on Windows will exercise them.
  2. lower.rs now has the // ---- instructions ---- comment twice in a row (hunk at ~2251) — drop one.
  3. The denormal shortcut is documented in the comment; fine as long as MODE stays unmodelled.

@mercuryVM

Copy link
Copy Markdown
Author

@catminou4 thanks for the review. Note 2 is fixed in b108d2f (one // ---- instructions ---- left). I merged current main (ffe310e) on top in 717e732; it merged without conflicts. On that head, the CI's own commands pass locally: both cargo clippy ... -D warnings runs, cargo test --workspace --locked, and victory-shader 122 passed on the RTX 5070 and WARP.

AI-assisted (Claude Code).

@mercuryVM

Copy link
Copy Markdown
Author

@Parakeet-Wheat the same question as on #596, if you have time: how many ds_{min,max}{,_rtn}_f32 the retail shaders contain, and whether translate --list --validate changes any outcome with this PR. Counts only, nothing from the shaders.

AI-assisted (Claude Code).

@Parakeet-Wheat

Copy link
Copy Markdown

@mercuryVM here are the counts for 717e732 (which already contains main ffe310e):

  • Setup: Windows 11 26300, NVIDIA GeForce RTX 3070 (8 GB, driver 610.88), Ryzen 5 5600X, Rust 1.98.1 (x86_64-pc-windows-msvc), dxil.dll 1.8.2502, title 1.0.2.1.
  • victory shader-info --disasm over shaders.rpf and the executable: 0 ds_{min,max}{,_rtn}_f32. The title's LDS min/max are all integer: ds_max_u32 147, ds_min_u32 78, ds_max_i32 2. Its 4 "an LDS operation" refusals on main are other instructions.
  • victory translate <exe> shaders.rpf --list --validate, main vs this PR: no program changes outcome (21,736 validated, 4,003 failed, 162 rejected on both).
  • cargo test -p victory-shader --locked: 122 passed, 0 failed, 1 ignored. Both ds_min_max_f32_* tests pass, but each ran for over 60 s on this GPU (100 s for the whole suite, against about 45 s without them).

Offline translation and validation only; no shader contents or extracted data included. AI-assisted (Claude Code); run on my machine.

@mercuryVM

Copy link
Copy Markdown
Author

@Parakeet-Wheat thank you. Zero in the title, all LDS min/max integer: noted at the top of the description. I'll also cut the semantics test's run time (one dispatch per case makes it take over 60 s on your 3070) if this PR stays open.

AI-assisted (Claude Code).

@bvtterbroot

Copy link
Copy Markdown
Contributor

AMD check at 717e732 (base ffe310e), on an RDNA3 and an RDNA2 GPU:

  • Windows 11, Rust 1.98.1. Radeon RX 7900 XTX (RDNA3, driver 32.0.32029.2010) and the Ryzen 7 7800X3D's iGPU (RDNA2, gfx1036, driver 32.0.21045.29003). I picked each as the hardware device of run_cases with a local adapter-selection patch that is not part of this PR. WARP runs in each pass as well.
  • cargo test -p victory-shader -p victory-gpu --locked: 172 passed, 0 failed, 2 ignored, on both GPUs. That includes its 2 new tests (ds_min_max_f32_are_atomic_across_waves, ds_min_max_f32_order_floats_and_return_the_old_value).

Synthetic tests only, no game files. Tested with AI assistance (Claude Code).

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-shader/src/emit/valu_tests.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

geiyiliyabumo pushed a commit to geiyiliyabumo/VIctory that referenced this pull request Nov 25, 2026
GPU: Keep small readback copies fast on Wine and Proton
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants