Shaders: lower buffer_atomic_{sub,csub,inc,dec,fcmpswap,fmin,fmax} #1342

Open
AscilCH wants to merge 1 commit from upstream-pr/1342 into main
AscilCH commented 2026-11-24 17:33:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AscilCH on 2026-11-24T17:33:20Z.


Gap

Lower::buffer() in crates/victory-shader/src/emit/lower.rs was missing lowering for seven 32-bit MUBUF atomic instructions, failing shader translation with:

lower: Unsupported { offset: 36, what: "buffer_atomic_sub" }

Missing ops: buffer_atomic_sub (0x33), buffer_atomic_csub (0x34), buffer_atomic_inc (0x3c), buffer_atomic_dec (0x3d), buffer_atomic_fcmpswap (0x3e), buffer_atomic_fmin (0x3f), and buffer_atomic_fmax (0x40).

Fix

Added a BufferAtomic lowering dispatch in Lower::buffer():

  • buffer_atomic_sub (0x33): negates vals[0] (0 - val) and emits atomicBinOp.i32(Add) (matching image_atomic_sub).
  • buffer_atomic_csub (0x34): structured atomicCompareExchange.i32 CAS loop computing (old >= val) ? old - val : 0.
  • buffer_atomic_inc (0x3c) and buffer_atomic_dec (0x3d): structured CAS loops with RDNA2 unsigned wrap semantics ((old >= val) ? 0 : old + 1 and (old == 0 || old > val) ? val : old - 1).
  • buffer_atomic_fmin (0x3f) and buffer_atomic_fmax (0x40): structured CAS loops using RDNA2 self.max_min (-0.0 < +0.0, non-NaN wins).
  • buffer_atomic_fcmpswap (0x3e): structured CAS loop comparing with IEEE ordered equality (pred::OEQ, so +0.0 == -0.0 and NaN != NaN) and exiting without writing on mismatch.

Added hardware + WARP + dxil.dll test emit::valu_tests::buffer_atomics_sub_csub_inc_dec_and_float_match_rdna2 verifying both updated buffer memory and returned pre-op values (glc = 1) for all seven instructions across normal, clamp, wrap, -0.0/+0.0, and NaN cases.

Verification

Tested on Windows 11, NVIDIA RTX 3050 6GB Laptop GPU, WARP, and Windows SDK dxil.dll:

  • rustfmt --edition 2024 --check on valu_tests.rs: clean
  • cargo xtask policy: 154 files ok
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
  • cargo test -p victory-shader --locked -- buffer_atomics_sub_csub_inc_dec_and_float_match_rdna2 --nocapture --test-threads=1: passed (hardware + WARP + dxil.dll)
  • VICTORY_WARP=1 cargo test -p victory-shader --locked -- --test-threads=1: 121 passed, 0 failed, 1 ignored
<!-- greyfinch-sync:Marvisak/VIctory:issue:6587700768 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1342). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-24T17:33:20Z. --- ### Gap `Lower::buffer()` in `crates/victory-shader/src/emit/lower.rs` was missing lowering for seven 32-bit MUBUF atomic instructions, failing shader translation with: ```text lower: Unsupported { offset: 36, what: "buffer_atomic_sub" } ``` Missing ops: `buffer_atomic_sub` (`0x33`), `buffer_atomic_csub` (`0x34`), `buffer_atomic_inc` (`0x3c`), `buffer_atomic_dec` (`0x3d`), `buffer_atomic_fcmpswap` (`0x3e`), `buffer_atomic_fmin` (`0x3f`), and `buffer_atomic_fmax` (`0x40`). ### Fix Added a `BufferAtomic` lowering dispatch in `Lower::buffer()`: - **`buffer_atomic_sub` (`0x33`)**: negates `vals[0]` (`0 - val`) and emits `atomicBinOp.i32(Add)` (matching `image_atomic_sub`). - **`buffer_atomic_csub` (`0x34`)**: structured `atomicCompareExchange.i32` CAS loop computing `(old >= val) ? old - val : 0`. - **`buffer_atomic_inc` (`0x3c`)** and **`buffer_atomic_dec` (`0x3d`)**: structured CAS loops with RDNA2 unsigned wrap semantics (`(old >= val) ? 0 : old + 1` and `(old == 0 || old > val) ? val : old - 1`). - **`buffer_atomic_fmin` (`0x3f`)** and **`buffer_atomic_fmax` (`0x40`)**: structured CAS loops using RDNA2 `self.max_min` (`-0.0 < +0.0`, non-NaN wins). - **`buffer_atomic_fcmpswap` (`0x3e`)**: structured CAS loop comparing with IEEE ordered equality (`pred::OEQ`, so `+0.0 == -0.0` and `NaN != NaN`) and exiting without writing on mismatch. Added hardware + WARP + `dxil.dll` test `emit::valu_tests::buffer_atomics_sub_csub_inc_dec_and_float_match_rdna2` verifying both updated buffer memory and returned pre-op values (`glc = 1`) for all seven instructions across normal, clamp, wrap, `-0.0`/`+0.0`, and `NaN` cases. ### Verification Tested on Windows 11, NVIDIA RTX 3050 6GB Laptop GPU, WARP, and Windows SDK `dxil.dll`: - `rustfmt --edition 2024 --check` on `valu_tests.rs`: clean - `cargo xtask policy`: `154 files ok` - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: clean - `cargo test -p victory-shader --locked -- buffer_atomics_sub_csub_inc_dec_and_float_match_rdna2 --nocapture --test-threads=1`: passed (hardware + WARP + `dxil.dll`) - `VICTORY_WARP=1 cargo test -p victory-shader --locked -- --test-threads=1`: `121 passed, 0 failed, 1 ignored`
nullmarsh commented 2026-11-24 18:06:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by nullmarsh on 2026-11-24T18:01:06Z.


Ran on RDNA2 hardware through vkd3d-proton on a Steam Deck (not native Windows): AMD Van Gogh, RADV 26.1.99, SteamOS 3.8.28, Proton 11.0-3 (wine-11.0) with its vkd3d-proton 212991fc. Windows MSVC test binary (rustc 1.100.0) built from head 8448e91 on main ffe310e.

  • buffer_atomics_sub_csub_inc_dec_and_float_match_rdna2 passes: buffer_atomic_sub, csub, inc, dec, fcmpswap (-0 == +0, NaN != NaN), fmin and fmax, checking both the new memory value and the returned old value.
  • victory-shader lib tests: 120 passed, 1 failed, 1 ignored (main on the same host: 119/1/1). The one failure, stub_pipeline_is_created, fails on main too (likely vkd3d-proton accepting the zero DXIL digest the test expects refused; environment, not this PR).
  • No hangs. A pass means the translated DXIL gives these values on RDNA2 through vkd3d-proton/RADV; it isn't a console measurement.

Not tested: native Windows AMD drivers; WARP (this setup has none, so the WARP leg ran on RADV too); whether the test fails without the fix on this host.

AI-assisted (Claude Code, Claude Opus 5.5); no game data.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140036903 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1342#issuecomment-7140036903). Originally posted by [nullmarsh](https://github.com/nullmarsh) on 2026-11-24T18:01:06Z. --- Ran on RDNA2 hardware through vkd3d-proton on a Steam Deck (not native Windows): AMD Van Gogh, RADV 26.1.99, SteamOS 3.8.28, Proton 11.0-3 (wine-11.0) with its vkd3d-proton 212991fc. Windows MSVC test binary (rustc 1.100.0) built from head `8448e91` on main `ffe310e`. - `buffer_atomics_sub_csub_inc_dec_and_float_match_rdna2` passes: buffer_atomic_sub, csub, inc, dec, fcmpswap (-0 == +0, NaN != NaN), fmin and fmax, checking both the new memory value and the returned old value. - victory-shader lib tests: 120 passed, 1 failed, 1 ignored (main on the same host: 119/1/1). The one failure, `stub_pipeline_is_created`, fails on main too (likely vkd3d-proton accepting the zero DXIL digest the test expects refused; environment, not this PR). - No hangs. A pass means the translated DXIL gives these values on RDNA2 through vkd3d-proton/RADV; it isn't a console measurement. Not tested: native Windows AMD drivers; WARP (this setup has none, so the WARP leg ran on RADV too); whether the test fails without the fix on this host. AI-assisted (Claude Code, Claude Opus 5.5); no game data.
marquezinii commented 2026-11-24 19:04:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by marquezinii on 2026-11-24T18:10:50Z.


Ran natively on Windows (not through vkd3d) on an RTX 5070 (desktop, driver 621.40, the only adapter), Windows 11 26300, Rust 1.98.1 at 8448e91. cargo test -p victory-shader --lib -- --test-threads=1 gives 121 passed, 0 failed, 1 ignored. buffer_atomics_sub_csub_inc_dec_and_float_match_rdna2 passes on the hardware adapter and WARP. Together with nullmarsh's Steam Deck run, that covers RADV and a native NVIDIA driver.

I checked the compare-exchange loop bodies against the RDNA2 definitions:

  • csub: cur >= data ? cur - data : 0
  • inc: cur >= data ? 0 : cur + 1
  • dec: cur == 0 || cur > data ? data : cur - 1

All three match.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140273489 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1342#issuecomment-7140273489). Originally posted by [marquezinii](https://github.com/marquezinii) on 2026-11-24T18:10:50Z. --- Ran natively on Windows (not through vkd3d) on an RTX 5070 (desktop, driver 621.40, the only adapter), Windows 11 26300, Rust 1.98.1 at `8448e91`. `cargo test -p victory-shader --lib -- --test-threads=1` gives 121 passed, 0 failed, 1 ignored. `buffer_atomics_sub_csub_inc_dec_and_float_match_rdna2` passes on the hardware adapter and WARP. Together with nullmarsh's Steam Deck run, that covers RADV and a native NVIDIA driver. I checked the compare-exchange loop bodies against the RDNA2 definitions: - `csub`: `cur >= data ? cur - data : 0` - `inc`: `cur >= data ? 0 : cur + 1` - `dec`: `cur == 0 || cur > data ? data : cur - 1` All three match.
shortselow-byte commented 2026-11-24 20:03:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by shortselow-byte on 2026-11-24T19:06:37Z.


For triage: #164 (older, open, already reviewed) lowers the same seven buffer atomics in its buffer_atomic_ path: sub, csub, inc, dec, fcmpswap, fmin and fmax. git merge-tree against the current heads (8448e91 here, 825dbd9 there) shows the two conflict in emit/lower.rs, so they need reconciling rather than both landing.

For the record, this PR on its own also passes on a native AMD driver: isolated target directory, Windows 11, Rust 1.98.1 MSVC, RX 6900 XT, workspace 343 passed, 0 failed, both clippy runs clean.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7141613412 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1342#issuecomment-7141613412). Originally posted by [shortselow-byte](https://github.com/shortselow-byte) on 2026-11-24T19:06:37Z. --- For triage: #164 (older, open, already reviewed) lowers the same seven buffer atomics in its `buffer_atomic_` path: `sub`, `csub`, `inc`, `dec`, `fcmpswap`, `fmin` and `fmax`. `git merge-tree` against the current heads (`8448e91` here, `825dbd9` there) shows the two conflict in `emit/lower.rs`, so they need reconciling rather than both landing. For the record, this PR on its own also passes on a native AMD driver: isolated target directory, Windows 11, Rust 1.98.1 MSVC, RX 6900 XT, workspace 343 passed, 0 failed, both clippy runs clean.
catminou4 commented 2026-11-24 21:02:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T20:10:01Z.

Review: APPROVED


Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 96 passed, 0 failed. Execution/valu tests are #[cfg(all(test, windows))] and only run on Windows; macOS covers lowering/structure/IR unit tests.

Lowers buffer_atomic_{sub,csub,inc,dec,fcmpswap,fmin,fmax} with RDNA2-matched hardware cases (buffer_atomics_sub_csub_inc_dec_and_float_match_rdna2).

<!-- greyfinch-sync:Marvisak/VIctory:review:6046560406 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1342#pullrequestreview-6046560406). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T20:10:01Z. **Review:** APPROVED --- Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 96 passed, 0 failed. Execution/valu tests are #[cfg(all(test, windows))] and only run on Windows; macOS covers lowering/structure/IR unit tests. Lowers buffer_atomic_{sub,csub,inc,dec,fcmpswap,fmin,fmax} with RDNA2-matched hardware cases (buffer_atomics_sub_csub_inc_dec_and_float_match_rdna2).
Marvisak commented 2026-11-25 02:05:58 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:23:33Z.


Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-shader/src/emit/valu_tests.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149497387 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1342#issuecomment-7149497387). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:23:33Z. --- <!-- victory-triage v1 head=8448e91f535f531265c763258b47c0781b81223d verdict=needs-rebase at=2026-11-25T01:23:31Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-shader/src/emit/valu_tests.rs`). It will be reviewed once it is rebased onto the current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
sinan-cmyk commented 2026-11-25 02:06:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-25T01:48:41Z.


Offline measurement on a retail dump, and the shader tests on Ada

Result: no count change against main.

Offline. I merged head 8448e91 onto main ffe310e (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 21,724 on main, 21,724 with this PR.

No program changes its failure reason either. That is the expected result for a fix whose instructions sit behind the earlier failures on main. The count shows that the change rejects nothing. It does not show whether the output is right.

Tests: with the same merge, cargo test --locked -p victory-shader --lib passed 121, failed 0. The bitcode benchmark is ignored, as on main, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with VICTORY_WARP unset.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149983362 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1342#issuecomment-7149983362). Originally posted by [sinan-cmyk](https://github.com/sinan-cmyk) on 2026-11-25T01:48:41Z. --- ### Offline measurement on a retail dump, and the shader tests on Ada **Result: no count change against `main`.** **Offline.** I merged head 8448e91 onto `main` ffe310e (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 21,724 on `main`, 21,724 with this PR. No program changes its failure reason either. That is the expected result for a fix whose instructions sit behind the earlier failures on `main`. The count shows that the change rejects nothing. It does not show whether the output is right. **Tests:** with the same merge, `cargo test --locked -p victory-shader --lib` passed 121, failed 0. The bitcode benchmark is ignored, as on `main`, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with `VICTORY_WARP` unset. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1342:upstream-pr/1342'
git switch 'upstream-pr/1342'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1342'
git switch 'upstream-pr/1342'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1342'
git switch 'upstream-pr/1342'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1342'
git switch 'main'
git merge --squash 'upstream-pr/1342'
git switch 'main'
git merge --ff-only 'upstream-pr/1342'
git switch 'main'
git merge 'upstream-pr/1342'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!1342
No description provided.