tombox-xshader: declare UAVs accessed with GLC or DLC globallycoherent #199

Closed
github-bot wants to merge 1 commit from gh-pr/199/head into gh-pr/199/base
Owner

@⁠GDAgent on GitHub · 2026-11-23 00:55 UTC · original
GDAgent:fix/xshader-glc-coherent → main · +87 −3 · 3 files · 1 commits · closed without merging

Library / area: tombox-xshader (hlsl.rs UAV declarations, tests/exec_mem/ds.rs, tests/exec_mem/mimg.rs)

Exports changed (exports.txt rows): none

Source of the behaviour: RDNA2 ISA: buffer and image accesses with GLC or DLC set bypass the per-CU (L0/L1) caches, so a wave sees data written by waves of other thread groups. D3D12/HLSL: a UAV is only coherent across thread groups when declared globallycoherent.

Persistent-thread kernels depend on this: a work-queue consumer polls node data that another group publishes and loops until it appears. The translation dropped both bits and declared every UAV as a plain RWStructuredBuffer / RWBuffer / RWTexture*, which D3D12 only makes coherent within a thread group, so a consumer could keep reading a stale value and never leave its loop.

Changes:

  • A UAV is declared globallycoherent when the same shader both reads it with a GLC/DLC load (MUBUF/MTBUF GLC bit 14 or DLC bit 15, MIMG GLC bit 13 or DLC bit 7) and writes it (any store or atomic). In that case, groups of one dispatch talk to each other through the resource. Data written by an earlier dispatch is already made visible by the UAV barrier between dispatches, so a read-only GLC load keeps a plain declaration.
  • Atomics do not count as coherent reads: there GLC selects the returned pre-operation value, and atomics are device-coherent anyway. They do count as writes.
  • Slots are recorded per emitted instruction, so every candidate of a run-time buffer select is covered. A slot counts once, whatever view (structured, typed, image) reaches it. Bindless UAV arrays are declared once per type, so all bindless UAVs count as one slot (a superset, never a missing coherence).

Effect on one title's shader archive (35382 translated shaders, HLSL compared with main): 45 compute shaders get globallycoherent declarations (119 in total), including the persistent culling kernel below with the same 3 coherent UAVs as before. Marking every GLC/DLC-accessed UAV, as the first version of this PR did, gave 153 shaders (48 of them pixel shaders) 269 declarations. The other 35337 shaders translate byte-identically to main.

Tests:

  • exec_mem::ds::glc_and_dlc_buffer_accesses_are_globally_coherent: plain, GLC, DLC and GLC+DLC loads of a buffer the shader also stores to; a GLC atomic that must not mark the UAV; a GLC load of a buffer that is never written (stays plain); a GLC load plus an atomic on the same buffer (every view of it marked).
  • exec_mem::mimg::image_glc_and_dlc_accesses_are_globally_coherent: plain, GLC and DLC image loads followed by an image_store, and a GLC load without a store (stays plain).
  • Both check the generated HLSL. WARP is coherent either way, so the cross-group behaviour itself is not covered by an execution test.
  • cargo test -p tombox-xshader --no-fail-fast on current main (d8d37e4): 346 passed, 0 failed (WARP).
  • Native Windows 11; not run under Proton.

Not verified in a title: this came from a GPU hang (DXGI_ERROR_DEVICE_HUNG) in The Matrix Awakens inside a persistent culling kernel whose loads carry GLC and DLC. The translated kernel now declares those UAVs globallycoherent and compiles, but it is not shown that this alone ends the hang: the runs since then used a loop guard, and the same kernel also needs its 64 lanes in one wave (separate PR). No measurement of the cost of globallycoherent in a title.

  • cargo test -p tombox-xshader passes
  • No game or console files and no binaries included
  • No fake success; anything not possible is left missing with a note
**@⁠GDAgent** on GitHub · 2026-11-23 00:55 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/199) `GDAgent:fix/xshader-glc-coherent` → `main` · +87 −3 · 3 files · 1 commits · closed without merging **Library / area:** `tombox-xshader` (`hlsl.rs` UAV declarations, `tests/exec_mem/ds.rs`, `tests/exec_mem/mimg.rs`) **Exports changed (exports.txt rows):** none **Source of the behaviour:** RDNA2 ISA: buffer and image accesses with GLC or DLC set bypass the per-CU (L0/L1) caches, so a wave sees data written by waves of other thread groups. D3D12/HLSL: a UAV is only coherent across thread groups when declared `globallycoherent`. Persistent-thread kernels depend on this: a work-queue consumer polls node data that another group publishes and loops until it appears. The translation dropped both bits and declared every UAV as a plain `RWStructuredBuffer` / `RWBuffer` / `RWTexture*`, which D3D12 only makes coherent within a thread group, so a consumer could keep reading a stale value and never leave its loop. Changes: - A UAV is declared `globallycoherent` when the same shader both reads it with a GLC/DLC load (MUBUF/MTBUF GLC bit 14 or DLC bit 15, MIMG GLC bit 13 or DLC bit 7) **and** writes it (any store or atomic). In that case, groups of one dispatch talk to each other through the resource. Data written by an earlier dispatch is already made visible by the UAV barrier between dispatches, so a read-only GLC load keeps a plain declaration. - Atomics do not count as coherent reads: there GLC selects the returned pre-operation value, and atomics are device-coherent anyway. They do count as writes. - Slots are recorded per emitted instruction, so every candidate of a run-time buffer select is covered. A slot counts once, whatever view (structured, typed, image) reaches it. Bindless UAV arrays are declared once per type, so all bindless UAVs count as one slot (a superset, never a missing coherence). Effect on one title's shader archive (35382 translated shaders, HLSL compared with `main`): 45 compute shaders get `globallycoherent` declarations (119 in total), including the persistent culling kernel below with the same 3 coherent UAVs as before. Marking every GLC/DLC-accessed UAV, as the first version of this PR did, gave 153 shaders (48 of them pixel shaders) 269 declarations. The other 35337 shaders translate byte-identically to `main`. Tests: - `exec_mem::ds::glc_and_dlc_buffer_accesses_are_globally_coherent`: plain, GLC, DLC and GLC+DLC loads of a buffer the shader also stores to; a GLC atomic that must not mark the UAV; a GLC load of a buffer that is never written (stays plain); a GLC load plus an atomic on the same buffer (every view of it marked). - `exec_mem::mimg::image_glc_and_dlc_accesses_are_globally_coherent`: plain, GLC and DLC image loads followed by an `image_store`, and a GLC load without a store (stays plain). - Both check the generated HLSL. WARP is coherent either way, so the cross-group behaviour itself is not covered by an execution test. - `cargo test -p tombox-xshader --no-fail-fast` on current `main` (`d8d37e4`): 346 passed, 0 failed (WARP). - Native Windows 11; not run under Proton. **Not verified in a title:** this came from a GPU hang (`DXGI_ERROR_DEVICE_HUNG`) in The Matrix Awakens inside a persistent culling kernel whose loads carry GLC and DLC. The translated kernel now declares those UAVs `globallycoherent` and compiles, but it is **not** shown that this alone ends the hang: the runs since then used a loop guard, and the same kernel also needs its 64 lanes in one wave (separate PR). No measurement of the cost of `globallycoherent` in a title. - [x] `cargo test -p tombox-xshader` passes - [x] No game or console files and no binaries included - [x] No fake success; anything not possible is left `missing` with a note
RDNA2 buffer and image accesses with GLC or DLC set bypass the per-CU
caches, so a wave sees data that waves of other thread groups wrote.
Persistent-thread kernels depend on this: a work-queue consumer polls
node data that another group publishes and loops until it appears.

The translation dropped both bits and declared every UAV as a plain
RWStructuredBuffer / RWBuffer / RWTexture*, which D3D12 only makes
coherent within a thread group. A consumer could then keep reading a
stale value and never leave its loop; in a title this showed up as a
GPU hang (DXGI_ERROR_DEVICE_HUNG) in a persistent culling kernel whose
loads carry GLC and DLC.

Now a UAV is declared globallycoherent when the same shader both reads
it with a GLC or DLC load (MUBUF/MTBUF GLC bit 14 or DLC bit 15, MIMG
GLC bit 13 or DLC bit 7) and writes it (any store or atomic). That is
the case where groups of one dispatch talk to each other through the
resource; data written by an earlier dispatch is already made visible
by the UAV barrier between dispatches, so a read-only GLC load keeps a
plain declaration. Atomics do not count as coherent reads: there GLC
selects the returned pre-operation value, and atomics are
device-coherent anyway. Slots are recorded per emitted instruction, so
every candidate of a run-time buffer select is covered, and a slot
counts once whatever view (structured, typed, image) reaches it.
Bindless UAV arrays are declared once per type, so all bindless UAVs
count as one slot (a superset, never a missing coherence).

Over one title's shader archive (35382 translated shaders) 45 compute
shaders get globallycoherent declarations (119 in total), including
the persistent culling kernel above; marking every GLC/DLC-accessed UAV
gave 153 shaders (48 of them pixel shaders) 269 declarations. Every
other shader translates byte-identically to main.

Tests: tests/exec_mem/ds.rs glc_and_dlc_buffer_accesses_are_globally_coherent
(plain, GLC, DLC and GLC+DLC loads of a buffer the shader also stores
to, a GLC atomic that must not mark, a GLC load of a buffer that is
never written, which stays plain, and a GLC load plus an atomic on the
same buffer, which marks every view of it) and tests/exec_mem/mimg.rs
image_glc_and_dlc_accesses_are_globally_coherent (plain, GLC and DLC
image loads followed by an image_store, and a GLC load without a
store, which stays plain). They check the generated HLSL; WARP
is coherent either way, so the cross-group behaviour itself is not
covered by an execution test.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:13:34 +00:00
github-bot closed this pull request 2026-11-26 06:13:34 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-23 01:10 UTC · original

Thanks, the change looks correct, and it passes the gate here. It's on hold for now, not rejected. Declaring these UAVs globallycoherent can cost GPU time, and Trackmania is in the middle of a performance bisect. It will merge after that, with its own A/B run. Is a cheaper rule possible? For example, mark a UAV only when it is both written and read with GLC/DLC in the same shader, or only when the GLC/DLC load sits inside a loop that polls. If you can narrow it, please push it to this branch with a test that covers the case that stays coherent.

**@⁠omonkeyz** on GitHub · 2026-11-23 01:10 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/199#issuecomment-7088938022) Thanks, the change looks correct, and it passes the gate here. It's on hold for now, not rejected. Declaring these UAVs globallycoherent can cost GPU time, and Trackmania is in the middle of a performance bisect. It will merge after that, with its own A/B run. Is a cheaper rule possible? For example, mark a UAV only when it is both written and read with GLC/DLC in the same shader, or only when the GLC/DLC load sits inside a loop that polls. If you can narrow it, please push it to this branch with a test that covers the case that stays coherent.
Author
Owner

@⁠GDAgent on GitHub · 2026-11-23 10:01 UTC · original

Rebased on d8d37e4, narrowed, and pushed as e930f7c. I went with your first option, slightly widened so it stays correct:

A UAV is now globallycoherent only when the same shader reads it with a GLC/DLC load and also writes it (any store or atomic). That is the case where groups of one dispatch talk to each other through the resource. Data written by an earlier dispatch is already made visible by the UAV barrier between dispatches, so a read-only GLC load keeps a plain declaration. I count atomics as writes, because a producer can publish through an atomic rather than a GLC store. GLC on the atomic itself still doesn't mark anything.

I measured it on one title's shader archive (35382 translated shaders), comparing the HLSL with main:

  • 45 compute shaders get globallycoherent declarations (119 in total).
  • The persistent culling kernel that hung keeps the same 3 coherent UAVs as before.
  • The first version marked 153 shaders (48 of them pixel shaders) with 269 declarations. No pixel shader is marked now.
  • The other 35337 shaders translate byte-identically to main.

The tests now pin the cases that stay coherent and the ones that don't:

  • Buffers: GLC, DLC and GLC+DLC loads of a buffer that is also stored to are marked. A plain load is not, a GLC atomic alone is not, and a GLC load of a buffer that is never written is not. A GLC load plus an atomic on the same buffer marks every view of it.
  • Images: a GLC or DLC load followed by an image_store is marked. A plain load with a store is not, and a GLC load without a store is not.

cargo test -p tombox-xshader passes: 346 passed, 0 failed. I didn't do the polling-loop variant: the written-and-read rule is easier to test, and on this archive it already leaves only the compute shaders that both publish and read the same resource. There's no title A/B from me.

**@⁠GDAgent** on GitHub · 2026-11-23 10:01 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/199#issuecomment-7097400988) Rebased on `d8d37e4`, narrowed, and pushed as `e930f7c`. I went with your first option, slightly widened so it stays correct: A UAV is now `globallycoherent` only when the same shader **reads it with a GLC/DLC load and also writes it** (any store or atomic). That is the case where groups of one dispatch talk to each other through the resource. Data written by an earlier dispatch is already made visible by the UAV barrier between dispatches, so a read-only GLC load keeps a plain declaration. I count atomics as writes, because a producer can publish through an atomic rather than a GLC store. GLC on the atomic itself still doesn't mark anything. I measured it on one title's shader archive (35382 translated shaders), comparing the HLSL with `main`: - 45 compute shaders get `globallycoherent` declarations (119 in total). - The persistent culling kernel that hung keeps the same 3 coherent UAVs as before. - The first version marked 153 shaders (48 of them pixel shaders) with 269 declarations. No pixel shader is marked now. - The other 35337 shaders translate byte-identically to `main`. The tests now pin the cases that stay coherent and the ones that don't: - Buffers: GLC, DLC and GLC+DLC loads of a buffer that is also stored to are marked. A plain load is not, a GLC atomic alone is not, and a GLC load of a buffer that is never written is not. A GLC load plus an atomic on the same buffer marks every view of it. - Images: a GLC or DLC load followed by an `image_store` is marked. A plain load with a store is not, and a GLC load without a store is not. `cargo test -p tombox-xshader` passes: 346 passed, 0 failed. I didn't do the polling-loop variant: the written-and-read rule is easier to test, and on this archive it already leaves only the compute shaders that both publish and read the same resource. There's no title A/B from me.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-23 14:08 UTC · original

Applied locally on main with your authorship kept (cherry-picked, e543de8); the tombox-xshader suite passes in a sandbox (exec 155/155 on WARP). It ships in the next release; this PR will be closed when that is pushed. Thanks.

**@⁠omonkeyz** on GitHub · 2026-11-23 14:08 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/199#issuecomment-7103544582) Applied locally on main with your authorship kept (cherry-picked, e543de8); the tombox-xshader suite passes in a sandbox (exec 155/155 on WARP). It ships in the next release; this PR will be closed when that is pushed. Thanks.
Author
Owner

@⁠kiwe147 on GitHub · 2026-11-23 18:57 UTC · original

Tested head e930f7c3c6, applied with git apply (whitespace ignored, the line endings of my checkout differ) on main f89e410, together with #229, #232 (one tree; the PRs touch different files). It applied without conflicts and built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, NVIDIA GeForce RTX 3080 (driver 621.24), MSVC, Rust 1.91.1.

run result
tombox-xshader lib + opcodes, synthetic, mesh, linkage, mixed_table, ps_linkage, centroid, gcn_dims, amp, xshader_cli, isa_sweep 206 passed, 0 failed
the tests the PRs of this tree add, in the exec suite on WARP 2 passed, 0 failed
the same tests with TOMBOX_EXEC_HW=1 (RTX 3080) 2 passed, 0 failed

No new failure against main f89e410 in any of these. This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

**@⁠kiwe147** on GitHub · 2026-11-23 18:57 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/199#issuecomment-7110883936) Tested head `e930f7c3c6`, applied with `git apply` (whitespace ignored, the line endings of my checkout differ) on `main` `f89e410`, together with #229, #232 (one tree; the PRs touch different files). It applied without conflicts and built. Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, NVIDIA GeForce RTX 3080 (driver 621.24), MSVC, Rust 1.91.1. | run | result | |---|---| | `tombox-xshader` lib + `opcodes`, `synthetic`, `mesh`, `linkage`, `mixed_table`, `ps_linkage`, `centroid`, `gcn_dims`, `amp`, `xshader_cli`, `isa_sweep` | 206 passed, 0 failed | | the tests the PRs of this tree add, in the `exec` suite on WARP | 2 passed, 0 failed | | the same tests with `TOMBOX_EXEC_HW=1` (RTX 3080) | 2 passed, 0 failed | No new failure against `main` `f89e410` in any of these. This is one combined tree, not a run of this PR alone. AI-assisted: yes (Claude Code)
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-25 13:09 UTC · original

Applied on main in e543de8 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

**@⁠omonkeyz** on GitHub · 2026-11-25 13:09 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/199#issuecomment-7163361549) Applied on main in e543de8 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.