Shaders: coherent UAV views and device fences for programs that order memory across groups #913

Open
SpaghettiMaster545 wants to merge 2 commits from upstream-pr/914 into main
SpaghettiMaster545 commented 2026-11-25 12:40:36 +00:00 (Migrated from github.com)

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


What was wrong

The console keeps memory coherent between the groups of a dispatch where a program asks for it: cache-control bits (GLC, DLC, SLC) on a memory or scalar access, a memory atomic, or a cache invalidation. The translation gave every program plain UAVs and no fences, so on the host a group could read a stale copy of what another group of the same dispatch had written, and a thread's stores were not ordered for other groups before the atomic that announced them.

How it shows in game

The title's BVH builder (XboxBvhBuilder.dll) does exactly this: atomics, then reads of what other groups wrote. With plain views it can read stale nodes. On the AMD Radeon iGPU we saw loads go through a read-only view of the written buffer, which a program gets when the metadata names no UAV.

What the change does

  • A program with cache-control bits, a memory atomic or a cache invalidation (wants_coherent) has every UAV globally coherent: resource properties bit 14 on heap handles and root UAVs, and the coherent flag in the root UAVs' metadata.
  • In such a compute program, a device-wide memory fence (barrier mode 2, no group sync) goes where it waits for its stores (s_waitcnt_vscnt) or drops its caches (buffer_gl0_inv, buffer_gl1_inv, s_gl1_inv, s_dcache_inv). The host orders a thread's accesses for other groups only at a fence.
  • Every other program keeps plain views and no fences. Plain buffer accesses and LDS atomics do not count.

XboxBvhBuilder.dll: all 52 programs translate, validate and get host pipelines as before, and 14 of them now have coherent views.

How it was tested

  • New unit test coherent_views_follow_cache_bits_atomics_and_invalidations; cargo test --locked -p victory-shader --lib passes on this branch (216 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, a GPU test has group 0 store 42, wait for the store and set a flag atomically, while group 1 spins on the flag, invalidates and reads again. It gives 42 in every lane on both GPUs below. Its harness is not in this fork, so it is 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:6604695444 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/914). Originally posted by [SpaghettiMaster545](https://github.com/SpaghettiMaster545) on 2026-11-25T12:40:36Z. --- ## What was wrong The console keeps memory coherent between the groups of a dispatch where a program asks for it: cache-control bits (GLC, DLC, SLC) on a memory or scalar access, a memory atomic, or a cache invalidation. The translation gave every program plain UAVs and no fences, so on the host a group could read a stale copy of what another group of the same dispatch had written, and a thread's stores were not ordered for other groups before the atomic that announced them. ## How it shows in game The title's BVH builder (`XboxBvhBuilder.dll`) does exactly this: atomics, then reads of what other groups wrote. With plain views it can read stale nodes. On the AMD Radeon iGPU we saw loads go through a read-only view of the written buffer, which a program gets when the metadata names no UAV. ## What the change does - A program with cache-control bits, a memory atomic or a cache invalidation (`wants_coherent`) has every UAV globally coherent: resource properties bit 14 on heap handles and root UAVs, and the coherent flag in the root UAVs' metadata. - In such a compute program, a device-wide memory fence (barrier mode 2, no group sync) goes where it waits for its stores (`s_waitcnt_vscnt`) or drops its caches (`buffer_gl0_inv`, `buffer_gl1_inv`, `s_gl1_inv`, `s_dcache_inv`). The host orders a thread's accesses for other groups only at a fence. - Every other program keeps plain views and no fences. Plain buffer accesses and LDS atomics do not count. XboxBvhBuilder.dll: all 52 programs translate, validate and get host pipelines as before, and 14 of them now have coherent views. ## How it was tested - New unit test `coherent_views_follow_cache_bits_atomics_and_invalidations`; `cargo test --locked -p victory-shader --lib` passes on this branch (216 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, a GPU test has group 0 store 42, wait for the store and set a flag atomically, while group 1 spins on the flag, invalidates and reads again. It gives 42 in every lane on both GPUs below. Its harness is not in this fork, so it is 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).
SpaghettiMaster545 commented 2026-11-25 23:09:57 +00:00 (Migrated from github.com)

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


Follow-up pushed: 93c64fc. The device fence this PR adds at s_waitcnt_vscnt never ran, because ir.rs drops s_waitcnt_vscnt together with the scheduling hints (the dropped list at ir.rs:1145 on main). The commit keeps s_waitcnt_vscnt in the IR; the other waits are still dropped. A unit test checks that the wait reaches the lowering.

We found it while tracing garbage top-level roots from the title's BVH builder. Its bottom-up build stores a node, waits for the store, then publishes it to other groups with buffer_atomic_add. Without the fence, another group could see the counter before the node data.

Tested on this branch: the new test and coherent_views_follow_cache_bits_atomics_and_invalidations pass, and clippy reports nothing in the changed files. NVIDIA GeForce RTX 5070 Ti, driver 621.40, Windows 11.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7176562503 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/914#issuecomment-7176562503). Originally posted by [SpaghettiMaster545](https://github.com/SpaghettiMaster545) on 2026-11-25T22:36:01Z. --- Follow-up pushed: 93c64fc. The device fence this PR adds at `s_waitcnt_vscnt` never ran, because `ir.rs` drops `s_waitcnt_vscnt` together with the scheduling hints (the dropped list at `ir.rs:1145` on `main`). The commit keeps `s_waitcnt_vscnt` in the IR; the other waits are still dropped. A unit test checks that the wait reaches the lowering. We found it while tracing garbage top-level roots from the title's BVH builder. Its bottom-up build stores a node, waits for the store, then publishes it to other groups with `buffer_atomic_add`. Without the fence, another group could see the counter before the node data. Tested on this branch: the new test and `coherent_views_follow_cache_bits_atomics_and_invalidations` pass, and clippy reports nothing in the changed files. NVIDIA GeForce RTX 5070 Ti, driver 621.40, Windows 11.
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/914:upstream-pr/914'
git switch 'upstream-pr/914'

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