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

Closed
GDAgent wants to merge 1 commit from upstream-pr/199 into main
GDAgent commented 2026-11-23 00:55:21 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by GDAgent on 2026-11-23T00:55:21Z.


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
<!-- greyfinch-sync:omonkeyz/xbox-emulator:issue:6550072042 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/199). Originally posted by [GDAgent](https://github.com/GDAgent) on 2026-11-23T00:55:21Z. --- **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
omonkeyz commented 2026-11-23 01:10:49 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-23T01:10:49Z.


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.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7088938022 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/199#issuecomment-7088938022). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-23T01:10:49Z. --- 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.
GDAgent commented 2026-11-23 10:01:42 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by GDAgent on 2026-11-23T10:01:42Z.


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.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7097400988 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/199#issuecomment-7097400988). Originally posted by [GDAgent](https://github.com/GDAgent) on 2026-11-23T10:01:42Z. --- 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.
omonkeyz commented 2026-11-23 14:08:36 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-23T14:08:36Z.


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.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7103544582 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/199#issuecomment-7103544582). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-23T14:08:36Z. --- 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.
kiwe147 commented 2026-11-23 18:57:05 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by kiwe147 on 2026-11-23T18:57:05Z.


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)

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7110883936 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/199#issuecomment-7110883936). Originally posted by [kiwe147](https://github.com/kiwe147) on 2026-11-23T18:57:05Z. --- 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)
omonkeyz commented 2026-11-25 13:09:01 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-25T13:09:01Z.


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

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7163361549 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/199#issuecomment-7163361549). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-25T13:09:01Z. --- 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

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
omonkeyz/xbox-emulator!199
No description provided.