tombox-xshader: lower scratch (private memory) loads and stores to a per-thread array #284

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

@⁠MeNoSkills on GitHub · 2026-11-23 12:15 UTC · original
MeNoSkills:feat/scratch-memory → main · +247 −1 · 5 files · 1 commits · closed without merging

Scratch (thread-private memory) loads and stores were refused: a flat_store_* in the scratch segment failed with "scratch-segment access unsupported" and a flat_load_* with "scratch-memory load unsupported", so any shader that spills to scratch did not translate. This lowers them.

Model

Scratch is memory private to each lane. The translator runs one lane per thread, so a static uint tb_scr[1024] in the translated HLSL (a static global is thread-private in HLSL) is that memory. The address of an access is:

  • the VGPR named by the address field, when the SADDR field is "off" (null), or
  • the SGPR named by SADDR, when SADDR is set (the VGPR field is then not used),

plus the signed 12-bit instruction offset. The offset decoding is unchanged (and pinned by opcodes.rs::flat_global_and_scratch_offsets_do_not_take_the_dlc_bit).

All the plain flat load and store widths take part: ubyte, sbyte, ushort, sshort, dword, dwordx2, dwordx3, dwordx4, and the stores byte, short, dword, dwordx2, dwordx3, dwordx4 with the _d16_hi variants. Sub-dword and unaligned accesses are done byte by byte, so they work at any alignment. A lane with exec off neither reads nor writes.

New hlsl/scratch.rs holds the helpers and emit_scratch; hlsl.rs gets a scratch module, a uses_scratch flag and one match arm for Enc::Flat with the scratch segment, placed before the global routing arm. The global path is untouched.

Assumptions, stated plainly

  • The size of the array is a fixed 4 KiB per thread (scratch::DWORDS). The real scratch size of a shader is not in anything the translator reads, so I did not guess a per-shader value. An access outside the array reads 0 and a write outside it is dropped; on the console that would be a fault.
  • Not lowered, still refused with a message that names the instruction: scratch accesses that are not in the list above (atomics, the _d16 loads), and the LDS=1 forms.
  • I have not seen a real shader that spills to scratch (no dump), so the address model comes from the encoding and the instruction semantics, not from a title.

Tests

exec_vop::scratch (new): 22 cases, run through the pixel-shader exec harness. Round trips for dword, with the offset positive and negative and the address in a second register; an out-of-range access (reads 0); every sub-dword load (ubyte/sbyte/ushort/sshort, signed and unsigned results); store_byte and store_short read-modify-write inside a dword; an unaligned dword store read back at three alignments; dwordx2/dwordx4 store and load, and one dword read out of a dwordx4 store; and two cases that address the access through an SGPR (SADDR set), one for a store, one for a load.

  • 22/22 on WARP and on an NVIDIA RTX 4070 Ti (Windows 10 build 19045, MSVC, TOMBOX_EXEC_HW=1). Without the change all of them fail with "scratch-segment access unsupported".
  • exec_mem::global::global_routing_and_refusals asserted that a scratch store is refused; that case now uses a scratch-segment atomic, which is still refused with an error that names "scratch".
  • Regression runs on the RTX: --lib 116, --test synthetic 24, --test isa_sweep 2, --test mesh 24, --test opcodes 26, and the full --test exec run with TOMBOX_EXEC_HW=1: 130 passed, 0 failed, 2 ignored (1704 s).
  • python .github/scripts/pr_gate.py --base upstream/main --head HEAD: 0 problems. The branch is on main f89e410.

Stage criteria

Per the answer in #187 this does not move a stage by itself: it removes one reason a real shader could fail to translate, and adds bit-exact exec tests for the new instructions. It is not tied to a BLOCKERS item.

Not verified

No real shader, and nothing run on AMD hardware. The 4 KiB size and the out-of-range behaviour are my choices (above), not measured console behaviour.

**@⁠MeNoSkills** on GitHub · 2026-11-23 12:15 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/284) `MeNoSkills:feat/scratch-memory` → `main` · +247 −1 · 5 files · 1 commits · closed without merging Scratch (thread-private memory) loads and stores were refused: a `flat_store_*` in the scratch segment failed with "scratch-segment access unsupported" and a `flat_load_*` with "scratch-memory load unsupported", so any shader that spills to scratch did not translate. This lowers them. ## Model Scratch is memory private to each lane. The translator runs one lane per thread, so a `static uint tb_scr[1024]` in the translated HLSL (a static global is thread-private in HLSL) is that memory. The address of an access is: - the VGPR named by the address field, when the SADDR field is "off" (null), or - the SGPR named by SADDR, when SADDR is set (the VGPR field is then not used), plus the signed 12-bit instruction offset. The offset decoding is unchanged (and pinned by `opcodes.rs::flat_global_and_scratch_offsets_do_not_take_the_dlc_bit`). All the plain flat load and store widths take part: `ubyte`, `sbyte`, `ushort`, `sshort`, `dword`, `dwordx2`, `dwordx3`, `dwordx4`, and the stores `byte`, `short`, `dword`, `dwordx2`, `dwordx3`, `dwordx4` with the `_d16_hi` variants. Sub-dword and unaligned accesses are done byte by byte, so they work at any alignment. A lane with exec off neither reads nor writes. New `hlsl/scratch.rs` holds the helpers and `emit_scratch`; `hlsl.rs` gets a `scratch` module, a `uses_scratch` flag and one match arm for `Enc::Flat` with the scratch segment, placed before the global routing arm. The global path is untouched. ## Assumptions, stated plainly - The size of the array is a fixed 4 KiB per thread (`scratch::DWORDS`). The real scratch size of a shader is not in anything the translator reads, so I did not guess a per-shader value. An access outside the array reads 0 and a write outside it is dropped; on the console that would be a fault. - Not lowered, still refused with a message that names the instruction: scratch accesses that are not in the list above (atomics, the `_d16` loads), and the LDS=1 forms. - I have not seen a real shader that spills to scratch (no dump), so the address model comes from the encoding and the instruction semantics, not from a title. ## Tests `exec_vop::scratch` (new): 22 cases, run through the pixel-shader exec harness. Round trips for dword, with the offset positive and negative and the address in a second register; an out-of-range access (reads 0); every sub-dword load (`ubyte`/`sbyte`/`ushort`/`sshort`, signed and unsigned results); `store_byte` and `store_short` read-modify-write inside a dword; an unaligned dword store read back at three alignments; `dwordx2`/`dwordx4` store and load, and one dword read out of a `dwordx4` store; and two cases that address the access through an SGPR (SADDR set), one for a store, one for a load. - 22/22 on WARP and on an NVIDIA RTX 4070 Ti (Windows 10 build 19045, MSVC, `TOMBOX_EXEC_HW=1`). Without the change all of them fail with "scratch-segment access unsupported". - `exec_mem::global::global_routing_and_refusals` asserted that a scratch store is refused; that case now uses a scratch-segment atomic, which is still refused with an error that names "scratch". - Regression runs on the RTX: `--lib` 116, `--test synthetic` 24, `--test isa_sweep` 2, `--test mesh` 24, `--test opcodes` 26, and the full `--test exec` run with `TOMBOX_EXEC_HW=1`: 130 passed, 0 failed, 2 ignored (1704 s). - `python .github/scripts/pr_gate.py --base upstream/main --head HEAD`: 0 problems. The branch is on main f89e410. ## Stage criteria Per the answer in #187 this does not move a stage by itself: it removes one reason a real shader could fail to translate, and adds bit-exact exec tests for the new instructions. It is not tied to a BLOCKERS item. ## Not verified No real shader, and nothing run on AMD hardware. The 4 KiB size and the out-of-range behaviour are my choices (above), not measured console behaviour.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:17:19 +00:00
github-bot closed this pull request 2026-11-26 06:17:19 +00:00
Author
Owner

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

Applied locally on main with your authorship kept (cherry-picked, 4f1cd12); 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/284#issuecomment-7103537085) Applied locally on main with your authorship kept (cherry-picked, 4f1cd12); 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

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

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

**@⁠omonkeyz** on GitHub · 2026-11-25 13:08 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/284#issuecomment-7163348010) Applied on main in 4f1cd12 (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.