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

Open
github-bot wants to merge 2 commits from github-bot/gh-914 into main AGit
Owner

@⁠SpaghettiMaster545 on GitHub · 2026-11-25 12:40 UTC · original
SpaghettiMaster545:pr/coherent → main · +134 −4 · 3 files · 2 commits · open

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** on GitHub · 2026-11-25 12:40 UTC · [original](https://github.com/fxceless/victory-fork/pull/914) `SpaghettiMaster545:pr/coherent` → `main` · +134 −4 · 3 files · 2 commits · open ## 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).
The console keeps memory coherent between the groups of a dispatch only
where a program asks: cache-control bits (GLC, DLC, SLC) on a memory or
scalar access, a memory atomic, or a cache invalidation. Such a program
(the title's BVH builder: atomics, then reads of what other groups wrote)
now has every UAV globally coherent (resource properties bit 14, and the
root UAVs' metadata), and a device-wide memory fence (barrier mode 2, no
group sync) 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. Written anew from the
mechanism; no other project's code.

Tests:
- unit (coherent_views_follow_cache_bits_atomics_and_invalidations):
  plain buffer accesses and LDS atomics keep plain views; glc, dlc, slc,
  a buffer atomic, buffer_gl0_inv and s_load glc get coherent ones;
- GPU (in our tree; its harness is not in this fork):
  group 0 waits, stores 42, waits for the store and sets a flag atomically;
  group 1 reads the data (0), spins on the flag, invalidates and rereads:
  42 in every lane on the RTX 5070 Ti and the Radeon iGPU. The same with
  the rule switched off also gives 42 on both (their drivers keep no
  stale copy there), so the test checks the coherent form rather than
  showing the plain one fail. What did fail on the Radeon: loads through a
  read-only view of the written buffer, which a program gets when the
  metadata names no UAV.

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

(cherry picked from commit 369f26f, without the GPU test, whose harness this
fork lacks)
The IR dropped every s_waitcnt with the scheduling hints (ir.rs DROPPED), so
the device fence this change gives s_waitcnt_vscnt in a program that orders
memory across groups was never emitted; only the fences at cache
invalidations were. s_waitcnt_vscnt is kept in the IR now; the other waits
are still dropped. A unit test builds a store, s_waitcnt_vscnt and an atomic
and checks that the wait is in the IR and s_waitcnt is not.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:53:45 +00:00
Author
Owner

@⁠SpaghettiMaster545 on GitHub · 2026-11-25 22:36 UTC · original

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.

**@⁠SpaghettiMaster545** on GitHub · 2026-11-25 22:36 UTC · [original](https://github.com/fxceless/victory-fork/pull/914#issuecomment-7176562503) 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 has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/lower.rs
  • crates/victory-shader/src/emit/shader.rs
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.
git fetch -u origin '+refs/pull/914/head:github-bot/gh-914'
git switch 'github-bot/gh-914'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-914'
git switch 'github-bot/gh-914'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-914'
git switch 'github-bot/gh-914'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-914'
git switch 'main'
git merge --squash 'github-bot/gh-914'
git switch 'main'
git merge --ff-only 'github-bot/gh-914'
git switch 'main'
git merge 'github-bot/gh-914'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.