xshader: deterministic HLSL emission; GDS base from M0[31:16] per ISA #21

Closed
QuentinDanblon wants to merge 0 commits from upstream-pr/21 into main
QuentinDanblon commented 2026-11-21 17:37:29 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by QuentinDanblon on 2026-11-21T17:37:29Z.


Library / area: tombox-xshader (HLSL emission, GDS lowering)

Exports changed (exports.txt rows): none

Source of the behaviour: brcly/TomBox 2c729ef (GDAgent, crates declare MIT OR Apache-2.0); AMD RDNA2 ISA guide (30 Nov 2020) §3.7 and §10.5

1. Deterministic HLSL

emit_body builds its loop list by iterating a HashMap, so the bool tb_c{b} declarations come out in hash order. The same shader can then give different HLSL text, and so a different shader-cache key and a recompile. The list is now sorted before emission (port of 2c729ef). I checked every other HashMap/HashSet iteration in hlsl.rs and hlsl/*.rs; none of them affects the emitted text.
New test tests/synthetic.rs::loops_translate_deterministically translates a 6-loop compute shader 17 times. It fails without the sort and passes with it.

2. GDS base = M0[31:16]

hlsl/gds.rs used m0 & 0xffff as the GDS base. The ISA defines GDS M0 as {base[31:16], size[15:0]} in bytes (§3.7 M# descriptor, §10.5 "[15:0] holds SIZE … [31:16] holds BASE"; DS_APPEND/DS_CONSUME use M0.base). LLVM's GWS/append lowering also puts the base in the high half. The existing test was written to the same low-bits assumption. Now:

  • addr = ((m0 >> 16) + vaddr + offset) & ~3 (only offset for append/consume);
  • in range when vaddr + offset < (m0 & 0xffff) and inside the emulated buffer. Out of range: writes are dropped and reads return 0, which is the existing path (§3.6, §10.4.3).

tests/exec_mem/gds.rs now builds M0 as base << 16 | size. It covers nonzero bases (0x100, 0x200, 0xfbf8 at the buffer limit) and a size-clamp case where half the lanes fall outside size. The new tests fail against the old gds.rs (0/2) and pass with the fix (2/2).

I had no title GDS shader to check this against (none in the repo). If you have title evidence that M0 carries the base in the low bits, that would override the ISA reading.

Tests

  • cargo test -p tombox-xshader --release --test exec exec_mem::gds: 2/2

  • cargo test -p tombox-xshader --release --test synthetic: 8/8

  • cargo test -p tombox-xshader --release --lib: 8/8

  • full --test exec: no new failures. The 4 vop::basic failures are already present at 226938c and are fixed in a separate PR.

  • --test fixtures was not run: analysis/shaders is not in the repo (see #13).

  • cargo test -p tombox-xshader passes (except the pre-existing failures above)

  • 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:6532197912 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/21). Originally posted by [QuentinDanblon](https://github.com/QuentinDanblon) on 2026-11-21T17:37:29Z. --- **Library / area:** tombox-xshader (HLSL emission, GDS lowering) **Exports changed (exports.txt rows):** none **Source of the behaviour:** brcly/TomBox [2c729ef](https://github.com/brcly/TomBox/commit/2c729eff5da2c2baa4b1a922df62d9dc9291c40b) (GDAgent, crates declare MIT OR Apache-2.0); AMD RDNA2 ISA guide (30 Nov 2020) §3.7 and §10.5 ### 1. Deterministic HLSL `emit_body` builds its loop list by iterating a `HashMap`, so the `bool tb_c{b}` declarations come out in hash order. The same shader can then give different HLSL text, and so a different shader-cache key and a recompile. The list is now sorted before emission (port of 2c729ef). I checked every other `HashMap`/`HashSet` iteration in `hlsl.rs` and `hlsl/*.rs`; none of them affects the emitted text. New test `tests/synthetic.rs::loops_translate_deterministically` translates a 6-loop compute shader 17 times. It fails without the sort and passes with it. ### 2. GDS base = M0[31:16] `hlsl/gds.rs` used `m0 & 0xffff` as the GDS base. The ISA defines GDS M0 as `{base[31:16], size[15:0]}` in bytes (§3.7 M# descriptor, §10.5 "[15:0] holds SIZE … [31:16] holds BASE"; DS_APPEND/DS_CONSUME use `M0.base`). LLVM's GWS/append lowering also puts the base in the high half. The existing test was written to the same low-bits assumption. Now: - `addr = ((m0 >> 16) + vaddr + offset) & ~3` (only `offset` for append/consume); - in range when `vaddr + offset < (m0 & 0xffff)` and inside the emulated buffer. Out of range: writes are dropped and reads return 0, which is the existing path (§3.6, §10.4.3). `tests/exec_mem/gds.rs` now builds M0 as `base << 16 | size`. It covers nonzero bases (0x100, 0x200, 0xfbf8 at the buffer limit) and a size-clamp case where half the lanes fall outside `size`. The new tests fail against the old `gds.rs` (0/2) and pass with the fix (2/2). I had no title GDS shader to check this against (none in the repo). If you have title evidence that M0 carries the base in the low bits, that would override the ISA reading. ### Tests - `cargo test -p tombox-xshader --release --test exec exec_mem::gds`: 2/2 - `cargo test -p tombox-xshader --release --test synthetic`: 8/8 - `cargo test -p tombox-xshader --release --lib`: 8/8 - full `--test exec`: no new failures. The 4 `vop::basic` failures are already present at 226938c and are fixed in a separate PR. - `--test fixtures` was not run: `analysis/shaders` is not in the repo (see #13). - [x] `cargo test -p tombox-xshader` passes (except the pre-existing failures above) - [x] No game or console files and no binaries included - [x] No fake success; anything not possible is left `missing` with a note
QuentinDanblon commented 2026-11-21 21:03:20 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by QuentinDanblon on 2026-11-21T21:03:20Z.


Merged in the source sync 1159c96 (adapted). Thanks! Closing.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7069201721 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/21#issuecomment-7069201721). Originally posted by [QuentinDanblon](https://github.com/QuentinDanblon) on 2026-11-21T21:03:20Z. --- Merged in the source sync 1159c96 (adapted). Thanks! Closing.

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!21
No description provided.