tombox-xshader: lower ds_swizzle_b32 rotate and FFT modes in compute shaders #186

Closed
github-bot wants to merge 2 commits from gh-pr/186/head into gh-pr/186/base
Owner

@⁠MeNoSkills on GitHub · 2026-11-22 22:25 UTC · original
MeNoSkills:feat/ds-swizzle-rotate-fft → main · +98 −4 · 3 files · 2 commits · closed without merging

Stacked on #184: this branch contains its commit (7136d02) plus one more. Until #184 is merged the diff shows both; the change proper is the second commit, 912069a, in src/hlsl/ds_ext.rs and tests/exec_mem/ds.rs.

#184 lowers the bit and quad modes of ds_swizzle_b32 in compute shaders and refuses offsets >= 0xc000 (rotate and FFT) because I had no ISA text to check them against. This adds those two modes.

Semantics and where they come from

For each lane i of a 32-lane group, the lane reads source lane j; an inactive source lane reads 0 (the same path as ds_bpermute_b32). mask = offset[4:0].

  • Rotate, offset 0xc000-0xdfff: j = (i & mask) | (((i + amount) & 31) & ~mask), with amount = offset[9:5] when offset[10] = 0 (left) and (32 - offset[9:5]) & 31 when offset[10] = 1 (right). Offset bits 12:11 are ignored.
  • FFT, offset 0xe000-0xffff: j = (reverse5(i) >> popcount(mask)) | (i & mask), where reverse5 reverses the low 5 bits. Offset bits 12:5 are ignored.

Sources, all public:

  • The encodings are LLVM's: llvm/test/MC/AMDGPU/ds_swizzle.s (swizzle(FFT,x) = 0xe000 | x, swizzle(ROTATE,dir,size) = 0xc000 | dir << 10 | size << 5, 0xd000 decodes as rotate), and ROTATE_MODE_LO = 0xc000 / FFT_MODE_LO = 0xe000 in SIDefines.h.
  • Mesa's ACO uses the same layout: ds_pattern_rotate(delta, mask) = mask | (delta << 5) | 0xc000, called with mask = ~(cluster_size - 1) & 0x1f for a rotate by delta inside a cluster (emit_rotate_by_constant).
  • The formulas are the DS_SWIZZLE_B32 pseudocode of AMD's RDNA 2 ISA guide, as quoted in public AnyPS5 PRs (boykopovar/AnyPS5#14081, #605, #16976). Those PRs also report hardware measurements on RDNA2 (gfx1035 and gfx1036): the rotate model on 16 chosen plus 384 random offsets, the FFT formula on ten masks and on a sweep of 0xe000-0xffff, the ignored offset bits, and that an inactive source reads 0. I have not seen that data myself; I did not run anything on AMD hardware.

The RDNA 2 guide's rotate examples disagree with its own pseudocode; I followed the pseudocode, as the measurements in those PRs do.

Change

source_lane in emit_ds_permute now handles all four ranges (bit, quad, rotate, FFT) and the "rotate and FFT are not lowered" refusal is gone. For a mask of 0x1f, FFT is the identity, so the reversed term is omitted (a shift by 32 is undefined in HLSL).

Tests

exec_mem::ds::ds_swizzle_rotate_and_fft_compute: 49 cases on 64 threads (two groups of 32) against reference functions written from the formulas above:

  • 35 rotate offsets: both directions, sizes 0, 1, 3 and 31, masks 0x00, 0x18, 0x1c and 0x01, plus offsets with bits 12:11 set and a rotate with a full mask;
  • 10 FFT masks (0x00, 0x10, 0x1f, 0x18, 0x1c, 0x1e, 0x01, 0x0f, 0x15, 0x08) and 4 offsets with bits 12:5 set.

It also pins literal values taken from the formulas: FFT 0xe000 maps lanes 0-3 to 0, 16, 8, 24; 0xe01f is the identity; FFT 0xe010 maps lanes 1, 16, 17 to 8, 16, 24; rotate left by 1 maps lanes 0-3 to 1-4, right by 1 maps them to 31, 0, 1, 2; a left rotate by 3 inside clusters of 8 maps lanes 0-7 to 3, 4, 5, 6, 7, 0, 1, 2.

All pass on WARP and on an NVIDIA RTX 4070 Ti (Windows 10 build 19045, MSVC, TOMBOX_EXEC_HW=1). Regression runs on the RTX: --lib 85, --test synthetic 22, --test isa_sweep 2 (541 mnemonics), --test mesh 24, --test exec ds_ 18, all passing. pr_gate: 0 problems.

Not verified

The reference functions encode the formulas above, so the tests check that the translation matches them, not AMD hardware. No title shader uses these offsets in anything I can run. Wave64 is not covered (compute here is two 32-lane groups). The pixel-shader path is unchanged and still handles only quad mode.

**@⁠MeNoSkills** on GitHub · 2026-11-22 22:25 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/186) `MeNoSkills:feat/ds-swizzle-rotate-fft` → `main` · +98 −4 · 3 files · 2 commits · closed without merging Stacked on #184: this branch contains its commit (7136d02) plus one more. Until #184 is merged the diff shows both; the change proper is the second commit, 912069a, in `src/hlsl/ds_ext.rs` and `tests/exec_mem/ds.rs`. #184 lowers the bit and quad modes of `ds_swizzle_b32` in compute shaders and refuses offsets >= 0xc000 (rotate and FFT) because I had no ISA text to check them against. This adds those two modes. ## Semantics and where they come from For each lane `i` of a 32-lane group, the lane reads source lane `j`; an inactive source lane reads 0 (the same path as `ds_bpermute_b32`). `mask = offset[4:0]`. - Rotate, offset 0xc000-0xdfff: `j = (i & mask) | (((i + amount) & 31) & ~mask)`, with `amount = offset[9:5]` when `offset[10] = 0` (left) and `(32 - offset[9:5]) & 31` when `offset[10] = 1` (right). Offset bits 12:11 are ignored. - FFT, offset 0xe000-0xffff: `j = (reverse5(i) >> popcount(mask)) | (i & mask)`, where `reverse5` reverses the low 5 bits. Offset bits 12:5 are ignored. Sources, all public: - The encodings are LLVM's: [llvm/test/MC/AMDGPU/ds_swizzle.s](https://github.com/llvm/llvm-project/blob/main/llvm/test/MC/AMDGPU/ds_swizzle.s) (`swizzle(FFT,x)` = 0xe000 | x, `swizzle(ROTATE,dir,size)` = 0xc000 | dir << 10 | size << 5, 0xd000 decodes as rotate), and `ROTATE_MODE_LO = 0xc000` / `FFT_MODE_LO = 0xe000` in SIDefines.h. - Mesa's ACO uses the same layout: `ds_pattern_rotate(delta, mask) = mask | (delta << 5) | 0xc000`, called with `mask = ~(cluster_size - 1) & 0x1f` for a rotate by `delta` inside a cluster (`emit_rotate_by_constant`). - The formulas are the DS_SWIZZLE_B32 pseudocode of AMD's RDNA 2 ISA guide, as quoted in public AnyPS5 PRs (boykopovar/AnyPS5#14081, #605, #16976). Those PRs also report hardware measurements on RDNA2 (gfx1035 and gfx1036): the rotate model on 16 chosen plus 384 random offsets, the FFT formula on ten masks and on a sweep of 0xe000-0xffff, the ignored offset bits, and that an inactive source reads 0. I have not seen that data myself; I did not run anything on AMD hardware. The RDNA 2 guide's rotate examples disagree with its own pseudocode; I followed the pseudocode, as the measurements in those PRs do. ## Change `source_lane` in `emit_ds_permute` now handles all four ranges (bit, quad, rotate, FFT) and the "rotate and FFT are not lowered" refusal is gone. For a mask of 0x1f, FFT is the identity, so the reversed term is omitted (a shift by 32 is undefined in HLSL). ## Tests `exec_mem::ds::ds_swizzle_rotate_and_fft_compute`: 49 cases on 64 threads (two groups of 32) against reference functions written from the formulas above: - 35 rotate offsets: both directions, sizes 0, 1, 3 and 31, masks 0x00, 0x18, 0x1c and 0x01, plus offsets with bits 12:11 set and a rotate with a full mask; - 10 FFT masks (0x00, 0x10, 0x1f, 0x18, 0x1c, 0x1e, 0x01, 0x0f, 0x15, 0x08) and 4 offsets with bits 12:5 set. It also pins literal values taken from the formulas: FFT 0xe000 maps lanes 0-3 to 0, 16, 8, 24; 0xe01f is the identity; FFT 0xe010 maps lanes 1, 16, 17 to 8, 16, 24; rotate left by 1 maps lanes 0-3 to 1-4, right by 1 maps them to 31, 0, 1, 2; a left rotate by 3 inside clusters of 8 maps lanes 0-7 to 3, 4, 5, 6, 7, 0, 1, 2. All pass on WARP and on an NVIDIA RTX 4070 Ti (Windows 10 build 19045, MSVC, `TOMBOX_EXEC_HW=1`). Regression runs on the RTX: `--lib` 85, `--test synthetic` 22, `--test isa_sweep` 2 (541 mnemonics), `--test mesh` 24, `--test exec ds_` 18, all passing. `pr_gate`: 0 problems. ## Not verified The reference functions encode the formulas above, so the tests check that the translation matches them, not AMD hardware. No title shader uses these offsets in anything I can run. Wave64 is not covered (compute here is two 32-lane groups). The pixel-shader path is unchanged and still handles only quad mode.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:13:13 +00:00
github-bot closed this pull request 2026-11-26 06:13:13 +00:00
Author
Owner

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

Applied in f6639fe (on top of #184's 3828799, merged in 42511d7) with your authorship kept, thanks.

**@⁠omonkeyz** on GitHub · 2026-11-22 23:23 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/186#issuecomment-7087537357) Applied in f6639fe (on top of #184's 3828799, merged in 42511d7) with your authorship kept, thanks.

Pull request closed

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