Shaders: lower ds_min_f32 / ds_max_f32 (and _rtn) with RDNA2's NaN rules #628
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!628
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-628"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@mercuryVM on GitHub · 2026-11-24 05:54 UTC · original
mercuryVM:impl/lds-float-minmax→main· +284 −6 · 2 files · 7 commits · openWhat
ds_min_f32,ds_max_f32,ds_min_rtn_f32andds_max_rtn_f32were 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 retriescmpxchguntil 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:
_rtnforms 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) andLower::lds_float_pick(the choice) are incrates/victory-shader/src/emit/lower.rs. Invalu_tests.rs, the test builder now takes a thread count and an LDS size (build_cs;build_valu_cscalls it unchanged), andrun_cases_onpicks 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 --validategives the same outcome onmainand 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 withds_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 declaresWaveSize(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.ds_max_f32 on NaN [0x7fc00000] with 0 gave 0x7fc00000, want 0).cargo test --workspace: all passed (victory-shader 71 passed).rustfmt --edition 2024 --checkon both files: clean.cargo clippy -p victory-shader --all-targets: no warnings.cargo xtask policy: 151 files ok.Not tested
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 <[email protected]>@Lyrnc on GitHub · 2026-11-24 06:06 UTC · original
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 markedshortcut: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}_f32with 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:0x7f800001gives0x7fc00001.0with memory0x7fc00000writes0, and NaN data leaves a number unchanged. With both quiet NaNs the data is kept._rtnreturns 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:
shortcut:)0x7fc00001)With f32 denormals flushed there are 28 more: data
0with memory0x80000001gives0x80000000on the hardware (the denormal flushed to -0, then -0 < +0), where the integer order keeps0x80000001. 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.
@mercuryVM on GitHub · 2026-11-24 06:30 UTC · original
@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_rtnvalue). 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 on GitHub · 2026-11-24 07:33 UTC · original
Tested
5d07613merged ontomaind2a4bae(clean) on both GPUs here: pass.ds_min_max_f32_*tests, RTX 5080 (driver 610.88): 2 passed.VICTORY_ADAPTER=amd(local merge only): 2 passed. This is the hardware the model came from, now running the translated DXIL.lds_min_max_refis 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 on GitHub · 2026-11-24 07:35 UTC · original
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).victory-shadersuite: 71 passed, 0 failed on both GPUs; both newds_min_max_f32_*tests pass on the hardware device.lower.rsreverted to main both new tests fail (the opcodes are refused before reaching the GPU), as expected.@mercuryVM on GitHub · 2026-11-24 08:48 UTC · original
Merged current
main(58fed37) ina5ae95f. Only the end ofvalu_tests.rsconflicted;run_cases_onkeeps #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-shaderdoes not compile on currentmain(#719, a call tolowerswithout its newrsrc2argument; @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).
@mercuryVM on GitHub · 2026-11-24 10:20 UTC · original
@catminou4 thanks for the review. Note 2 is fixed in
b108d2f(one// ---- instructions ----left). I merged currentmain(ffe310e) on top in717e732; it merged without conflicts. On that head, the CI's own commands pass locally: bothcargo clippy ... -D warningsruns,cargo test --workspace --locked, andvictory-shader122 passed on the RTX 5070 and WARP.AI-assisted (Claude Code).
@mercuryVM on GitHub · 2026-11-24 11:06 UTC · original
@Parakeet-Wheat the same question as on #596, if you have time: how many
ds_{min,max}{,_rtn}_f32the retail shaders contain, and whethertranslate --list --validatechanges any outcome with this PR. Counts only, nothing from the shaders.AI-assisted (Claude Code).
@Parakeet-Wheat on GitHub · 2026-11-24 11:31 UTC · original
@mercuryVM here are the counts for
717e732(which already containsmainffe310e):610.88), Ryzen 5 5600X, Rust 1.98.1 (x86_64-pc-windows-msvc),dxil.dll1.8.2502, title 1.0.2.1.victory shader-info --disasmovershaders.rpfand the executable: 0ds_{min,max}{,_rtn}_f32. The title's LDS min/max are all integer:ds_max_u32147,ds_min_u3278,ds_max_i322. Its 4 "an LDS operation" refusals onmainare other instructions.victory translate <exe> shaders.rpf --list --validate,mainvs 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. Bothds_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 on GitHub · 2026-11-24 11:53 UTC · original
@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 on GitHub · 2026-11-24 17:54 UTC · original
AMD check at
717e732(baseffe310e), on an RDNA3 and an RDNA2 GPU:run_caseswith 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).
@Marvisak on GitHub · 2026-11-25 00:31 UTC · original
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
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.