Shaders: LDS byte and half writes as one bounded compare-exchange, not AND then OR #912

Open
SpaghettiMaster545 wants to merge 3 commits from upstream-pr/913 into main
SpaghettiMaster545 commented 2026-11-25 12:34:55 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by SpaghettiMaster545 on 2026-11-25T12:34:55Z.


What was wrong

ds_write_b8/ds_write_b16 and their _d16_hi forms wrote their part of an LDS dword in two atomics: an AND that cleared the part, then an OR that set it. Lanes writing the same byte in one instruction ORed their values together (a value no lane wrote), and a lane reading the dword between the two atomics saw the part cleared.

How it shows

Programs that pack bytes or halves of neighbouring lanes into shared dwords read back mixed bits, so whatever they build from them (masks, counters, packed indices) comes out wrong on the host GPU.

What the change does

The part now replaces its bits in one compare-exchange, retried while another lane changes the dword in between, bounded at 65536 tries so a fault cannot spin the GPU. Lanes writing other parts of the dword all land, and one value of the lanes writing the same part stays, as on the console.

How it was tested

  • cargo test --locked -p victory-shader --lib passes on this branch (215 passed, 3 ignored); cargo clippy -p victory-shader --all-targets --locked -- -D warnings is clean. (A workspace-wide clippy fails on main already, in victory-gpu, which this change does not touch.)
  • In our tree, GPU tests of neighbouring lanes' byte and half writes into one dword, the _d16_hi writes, masked byte writes that keep the other bytes, and every byte/half read form are exact with this change. Their harness is not in this fork, so they are not part of this PR.
  • Played with this change in our integration branch.
  • GPU and driver: NVIDIA GeForce RTX 5070 Ti, driver 621.40, Windows 11; GPU tests also on an AMD Radeon iGPU, driver 24.10.24.

Developed with AI assistance (Claude Code).

<!-- greyfinch-sync:fxceless/victory-fork:issue:6604593228 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/913). Originally posted by [SpaghettiMaster545](https://github.com/SpaghettiMaster545) on 2026-11-25T12:34:55Z. --- ## What was wrong `ds_write_b8`/`ds_write_b16` and their `_d16_hi` forms wrote their part of an LDS dword in two atomics: an AND that cleared the part, then an OR that set it. Lanes writing the same byte in one instruction ORed their values together (a value no lane wrote), and a lane reading the dword between the two atomics saw the part cleared. ## How it shows Programs that pack bytes or halves of neighbouring lanes into shared dwords read back mixed bits, so whatever they build from them (masks, counters, packed indices) comes out wrong on the host GPU. ## What the change does The part now replaces its bits in one compare-exchange, retried while another lane changes the dword in between, bounded at 65536 tries so a fault cannot spin the GPU. Lanes writing other parts of the dword all land, and one value of the lanes writing the same part stays, as on the console. ## How it was tested - `cargo test --locked -p victory-shader --lib` passes on this branch (215 passed, 3 ignored); `cargo clippy -p victory-shader --all-targets --locked -- -D warnings` is clean. (A workspace-wide clippy fails on `main` already, in `victory-gpu`, which this change does not touch.) - In our tree, GPU tests of neighbouring lanes' byte and half writes into one dword, the `_d16_hi` writes, masked byte writes that keep the other bytes, and every byte/half read form are exact with this change. Their harness is not in this fork, so they are not part of this PR. - Played with this change in our integration branch. - GPU and driver: NVIDIA GeForce RTX 5070 Ti, driver 621.40, Windows 11; GPU tests also on an AMD Radeon iGPU, driver 24.10.24. Developed with AI assistance (Claude Code).
avt0m4t48 commented 2026-11-25 13:06:04 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by avt0m4t48 on 2026-11-25T12:51:32Z.


Independent Windows Server 2025 WARP check at 5cf45f7. I added a source-built 64-thread regression covering the byte and half writes and both _d16_hi forms. For each opcode, overlapping lanes must leave one of the two supplied values, and lanes writing adjacent fields must preserve both values and the other bits.

All eight cases passed with the PR. With just the production change removed, the first overlapping-byte case returned 3 from lanes supplying 1 or 2. The job sets VICTORY_WARP=1, so the first backend label in the negative-control log also refers to WARP.

The test-only commit is available to cherry-pick. Windows run

AI-assisted. Synthetic shader and inputs only.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7162954837 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/913#issuecomment-7162954837). Originally posted by [avt0m4t48](https://github.com/avt0m4t48) on 2026-11-25T12:51:32Z. --- Independent Windows Server 2025 WARP check at `5cf45f7`. I added a source-built 64-thread regression covering the byte and half writes and both `_d16_hi` forms. For each opcode, overlapping lanes must leave one of the two supplied values, and lanes writing adjacent fields must preserve both values and the other bits. All eight cases passed with the PR. With just the production change removed, the first overlapping-byte case returned 3 from lanes supplying 1 or 2. The job sets `VICTORY_WARP=1`, so the first backend label in the negative-control log also refers to WARP. The [test-only commit](https://github.com/avt0m4t48/victory-fork/commit/51a20b5ffd931d918dd1ffa9b1d1ced0ceb2e01a) is available to cherry-pick. [Windows run](https://github.com/avt0m4t48/victory-fork/actions/runs/45617261602) AI-assisted. Synthetic shader and inputs only.
eazyves commented 2026-11-25 14:10:47 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by eazyves on 2026-11-25T13:07:31Z.


Run on real RDNA4 hardware: RX 9070 XT, driver 32.0.32029.2010, Windows 11 26200, Rust 1.98.1, with avt0m4t48's test (51a20b5) on this PR's head 5cf45f7.

  • With the PR: ds_subword_writes_preserve_neighboring_bits_and_one_lanes_value passes, 4 of 4 runs.
  • With lower.rs put back to the merge base a253706: fails on its first case, with the test's own hardware label: op 0x1e, got 0xa5c3035a, so the byte is 3 from lanes that wrote 1 or 2.

So the merged value is not a WARP effect; the card shows it too, and this change removes it. One run takes about 50 s in a debug build.

A small thing in the diff: lds_merge sits between begin_guarded and that function's doc comment, so "Starts code only the lanes where guard holds run ..." now documents lds_merge.

AI-assisted (Claude Code); run on my machine, synthetic shader only, no game data.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7163325504 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/913#issuecomment-7163325504). Originally posted by [eazyves](https://github.com/eazyves) on 2026-11-25T13:07:31Z. --- Run on real RDNA4 hardware: RX 9070 XT, driver 32.0.32029.2010, Windows 11 26200, Rust 1.98.1, with avt0m4t48's test (51a20b5) on this PR's head `5cf45f7`. - With the PR: `ds_subword_writes_preserve_neighboring_bits_and_one_lanes_value` passes, 4 of 4 runs. - With `lower.rs` put back to the merge base `a253706`: fails on its first case, with the test's own `hardware` label: op `0x1e`, got `0xa5c3035a`, so the byte is 3 from lanes that wrote 1 or 2. So the merged value is not a WARP effect; the card shows it too, and this change removes it. One run takes about 50 s in a debug build. A small thing in the diff: `lds_merge` sits between `begin_guarded` and that function's doc comment, so "Starts code only the lanes where `guard` holds run ..." now documents `lds_merge`. AI-assisted (Claude Code); run on my machine, synthetic shader only, no game data.
SpaghettiMaster545 commented 2026-11-25 16:12:52 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by SpaghettiMaster545 on 2026-11-25T15:34:36Z.


Thanks to both of you. I took the test commit as it is (51a20b5). ds_subword_writes_preserve_neighboring_bits_and_one_lanes_value passes here on the RTX 5070 Ti (NVIDIA 621.40, Windows 11). I also moved begin_guarded's doc comment back onto begin_guarded (847ad17). cargo clippy -p victory-shader --all-targets --locked -- -D warnings is clean.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7167055752 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/913#issuecomment-7167055752). Originally posted by [SpaghettiMaster545](https://github.com/SpaghettiMaster545) on 2026-11-25T15:34:36Z. --- Thanks to both of you. I took the test commit as it is (`51a20b5`). `ds_subword_writes_preserve_neighboring_bits_and_one_lanes_value` passes here on the RTX 5070 Ti (NVIDIA 621.40, Windows 11). I also moved `begin_guarded`'s doc comment back onto `begin_guarded` (`847ad17`). `cargo clippy -p victory-shader --all-targets --locked -- -D warnings` is clean.
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/913:upstream-pr/913'
git switch 'upstream-pr/913'

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/913'
git switch 'upstream-pr/913'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/913'
git switch 'upstream-pr/913'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/913'
git switch 'main'
git merge --squash 'upstream-pr/913'
git switch 'main'
git merge --ff-only 'upstream-pr/913'
git switch 'main'
git merge 'upstream-pr/913'
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
fxceless/victory-fork!912
No description provided.