Skip to content

Shaders: ds_swizzle_b32 offsets from 0xC000 are the rotate and FFT modes - #606

Open
upipibi wants to merge 3 commits into
Marvisak:mainfrom
upipibi:fix/ds-swizzle-rotate-fft
Open

upipibi wants to merge 3 commits into
Marvisak:mainfrom
upipibi:fix/ds-swizzle-rotate-fft

Conversation

@upipibi

@upipibi upipibi commented Nov 24, 2026 •

Copy link
Copy Markdown

Two commits on main d2a4bae: 3ad06fb (the two modes), da1a1f7 (rotations with a mask).

What

ds_swizzle_b32 in crates/victory-shader/src/emit/lower.rs took bit 15 of the offset as "quad permutation" for every offset from 0x8000. Offsets from 0xC000 are two other modes, and were lowered as a quad permutation of offset[7:0]:

  • 0xE000-0xFFFF, FFT: lane i reads lane (reverse5(i) >> popcount(mask)) | (i & mask), mask = offset[4:0].
  • 0xC000-0xDFFF, rotate: lane i reads lane ((i & mask) | ((i + rot) & ~mask)) & 31, rot = offset[9:5], negated when offset[10] is set, mask = offset[4:0]. Bits 11 and 12 are not read.

Both modes stay inside the lane's group of 32 (i & 32 is kept), like the and/or/xor mode. Offsets below 0xC000 are unchanged. As before, reads of invalid lanes are not zeroed.

Why

RDNA2 ISA (30-November-2020), 12.13.1 "DS_SWIZZLE_B32 Details", pp. 207-209:

This opcode supports two specific modes, FFT and rotate, plus two basic modes which swizzle in groups of 4 or 32 consecutive threads.

The FFT mode (offset >= 0xe000) swizzles the input based on offset[4:0] to support FFT calculation.

The rotate mode (offset >= 0xc000 and offset < 0xe000) rotates the input either left (offset[10] == 0) or right (offset[10] == 1) a number of threads equal to offset[9:5].

If offset < 0xc000, one of the basic swizzle modes is used based on offset[15].

Pseudocode, same section:

if (offset >= 0xe000) { // FFT decomposition
    mask = offset[4:0];
    for (i = 0; i < 64; i++) {
        j = reverse_bits(i & 0x1f);
        j = (j >> count_ones(mask));
        j |= (i & mask);
        j |= i & 0x20;
        thread_out[i] = thread_valid[j] ? thread_in[j] : 0;
    }
} else if (offset >= 0xc000) { // rotate
    rotate = offset[9:5]; mask = offset[4:0];
    if (offset[10]) { rotate = -rotate; }
    for (i = 0; i < 64; i++) {
        j = (i & mask) | ((i + rotate) & ~mask);
        j |= i & 0x20;
        thread_out[i] = thread_valid[j] ? thread_in[j] : 0;
    }
}

Rotations with a mask. The ISA's two worked examples for mask 1 disagree with this pseudocode (rotate left by 1: the pseudocode makes lanes read 0, 3, 2, 5, ...; the example lists 1, 0, 3, 6, ...). The first version of this PR refused that case. @Lyrnc then measured it on RDNA2 hardware (gfx1036, comment below): the pseudocode is what the hardware does, for all 2048 offsets 0xC000-0xC7FF in wave32 and wave64, bits 11 and 12 are ignored, and the FFT formula matches all 37 offsets tried. da1a1f7 lowers masked rotations accordingly.

Tests

Four tests in valu_tests.rs, from hand-written instruction words: one 32-thread wave32 compute program whose lanes hold 1..=32 (the input of the ISA's examples), swizzle, and store what they read. build_valu_cs is split so that the lowering part (build_cs) takes the thread count; modules go through dxil.dll validation (the Windows SDK's) as before.

Where the expected values come from:

Test Values from
ds_swizzle_fft_mode the ISA's printed lists for offset[4:0] = 0x00 and 0x10, "0x1f: No swizzle"; 0xFFF0 as 0x10
ds_swizzle_rotate_mode the ISA's mask = 0 examples (left and right by 1); by 0, 5 and 31 from its text
ds_swizzle_masked_rotations Lyrnc's hardware lane lists for 0xC021, 0xC421, 0xC023 (first and last entries as posted, the rest by the formula their run confirms), and 0xD821 for the ignored bits
ds_swizzle_basic_modes a quad reverse (0x801B) and a half swap (0x401F): the two basic modes, unchanged; passes before and after

cargo test -p victory-shader -- --test-threads=1:

  • main d2a4bae: 69 passed.
  • with the first three tests, before the fix: 70 passed, 2 failed:
    • offset 0xe000: left [1, 1, 1, 1, 5, 5, 5, 5, 9, ...], right [1, 17, 9, 25, 5, 21, 13, 29, 3, ...]
    • offset 0xc020: left [1, 1, 3, 1, 5, 5, 7, 5, ...], right [2, 3, 4, ..., 32, 1]
  • 3ad06fb: 72 passed, 0 failed.
  • da1a1f7: 73 passed, 0 failed.

The swizzle tests need a host with 32-lane waves (WaveSize(32)). They ran on the GPU; WARP refuses the pipeline and is skipped by name, so on a WARP-only runner they pass without checking anything.

cargo xtask policy: ok (151 files). cargo build -p victory and cargo build -p victory --profile diag --features diagnostics: both build; cargo clippy -p victory-shader --tests adds no warnings.

GPU: NVIDIA RTX 3070, driver 616.56, Windows 11. These runs show what the translated DXIL computes; that the lanes are the hardware's rests on the ISA and on Lyrnc's RDNA2 measurement.

Not tested here: wave64 programs, mesh and pixel stages (cross-lane swizzles are refused there as before), a title's shaders. I know of no compiler that emits these two modes, so this is correctness for legal ISA, not a known in-game fix.

No game files were used. AI-assisted (Claude Code).

🤖 Generated with Claude Code

@upipibi upipibi mentioned this pull request Nov 24, 2026
@Lyrnc

Lyrnc commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Hardware answer to the open question (the masked rotation), plus a check of both modes: on RDNA2 the ISA's pseudocode is what the hardware does; the mask = 1 example is not.

Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. Hand-written ds_swizzle_b32 vN, v4 offset:<raw 16-bit value> in a HIP kernel (assembled by the AMD driver's amd_comgr), one wave, lane i holding i + 1, so each output names its source lane. Wave32 and wave64 kernels. RDNA2 desktop silicon, not the console's. No game data.

Offsets Count Model wave32 wave64
Rotate 0xC000-0xC7FF: every mask, amount and direction (bits 11-12 clear) 2048 j = ((i & mask) | ((i + rot) & ~mask)) & 31, rot negated when bit 10 is set, then j |= i & 32 2048/2048 2048/2048
Rotate with bit 11 and/or 12 set (amounts 1, 5; masks 0, 1, 3; both directions) 36 the same: bits 11-12 are ignored 36/36 36/36
FFT 0xE000-0xE01F, 0xE3E0, 0xE800, 0xF000, 0xFFF0, 0xFFFF 37 (reverse5(i) >> popcount(mask)) | (i & mask) | (i & 32) 37/37 37/37

Rotate left by 1 with mask 1 (0xC021), the lane each lane reads:
0, 3, 2, 5, 4, 7, 6, 9, 8, ..., 28, 31, 30, 1
That is the pseudocode's list, not the example's 1, 0, 3, 6, .... Two more, for a test if you want one: right by 1 with mask 1 (0xC421) gives 30, 1, 0, 3, 2, 5, 4, ..., and left by 1 with mask 3 (0xC023) gives 0, 1, 2, 7, 4, 5, 6, 11, ....

Against this PR:

  • Your mask-0 rotation formula matches all 64 mask-0 offsets (both directions, every amount), and your FFT formula matches all 37 FFT offsets, including 0x1f (no swizzle) and 0xFFF0 = 0x10.
  • In wave64 both modes stay inside each group of 32, as your lowering keeps them.
  • The refused case could therefore be lowered with the pseudocode, j = ((i & mask) | ((i + rot) & ~mask)) & 31 plus the group bits, instead of refused. I can run a GPU check of that if you add it.

The raw lane lists (JSON, every offset above, both wave sizes) are available if useful.

AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.

upipibi added a commit to upipibi/VIctory that referenced this pull request Nov 24, 2026
A rotation with a mask (offset[4:0]) was refused, because the pseudocode
of 12.13.1 and its examples for mask 1 give different lanes. Lyrnc
measured it on RDNA2 hardware (gfx1036, Marvisak#606): the
pseudocode is what the hardware does, for all 2048 offsets
0xC000-0xC7FF in wave32 and wave64, and bits 11 and 12 are ignored:

    j = ((i & mask) | ((i + rot) & ~mask)) & 31, rot negated with bit 10

It is lowered so now. A test holds the measured lane lists for 0xC021,
0xC421 and 0xC023, and 0xD821 for the ignored bits.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Author

@Lyrnc thank you, that settles the one thing this PR had to leave open, and it is a far better check than anything I could run here.

Pushed da1a1f7 on top of 3ad06fb:

  • Masked rotations are now lowered instead of refused, with the formula you measured, which is the ISA's pseudocode: j = ((i & mask) | ((i + rot) & ~mask)) & 31, rot negated when offset[10] is set, plus the lane's group bits (i & 32). Bits 11 and 12 were already not read.
  • New test ds_swizzle_masked_rotations with your lane lists as the expected values, citing your comment as the source: 0xC021 (0, 3, 2, 5, 4, ..., 28, 31, 30, 1), 0xC421 (30, 1, 0, 3, 2, 5, 4, ...), 0xC023 (0, 1, 2, 7, 4, 5, 6, 11, ...), and 0xD821 (bits 11 and 12 set, same as 0xC021). You posted the first and last entries of each list; the entries between them in the test follow the formula you report as matching 2048/2048.
  • The reason a masked ds_swizzle rotation no longer exists.

On the RTX 3070 (driver 616.56): cargo test -p victory-shader -- --test-threads=1 gives 73 passed, 0 failed (72 before this commit); cargo build -p victory, the diag build and clippy are clean. That only shows the translated DXIL computes those lists; your run is what says they are the hardware's.

If you still have the probe set up, would you run your GPU check against da1a1f7? What I cannot see from here is a wave64 program (the lowering keeps i & 32 and takes everything else from i & 31, as your table says the hardware does, but my tests are wave32 only).

AI-assisted (Claude Code).

@Lyrnc

Lyrnc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@upipibi ran it against da1a1f7, including the wave64 case. The translated programs reproduce the hardware lane lists exactly, wave64 included.

What ran. da1a1f7 merged with main d2a4bae, plus #94 so the test host can pick the adapter (VICTORY_ADAPTER). A local test (not pushed) uses your swizzle_32 program shape with threads = 32 or 64, sets wave64 in build_cs for the 64-thread run, and compares each lane's result with the list measured on the gfx1036 hardware (the probe from my comment above, lane i holding i + 1). There are 115 offsets: rotations with masks 0, 1, 2, 3, 5, 0x10, 0x15 and 0x1F at amounts 0, 1, 5, 17 and 31 in both directions, all 32 FFT masks, 0xFFF0, and 0xD821 / 0xC821 (bits 11-12 set).

Host wave32 program, 32 threads wave64 program, 64 threads
AMD Radeon iGPU (gfx1036, 64-lane waves) 115/115 115/115
RTX 5080 (32-lane waves only) 115/115 not run: pipeline creation fails (The parameter is incorrect), no 64-lane waves

So in wave64 every lane 32-63 reads within its own half, as on the hardware. Your four ds_swizzle_* tests also pass on both adapters (Windows 11 26200, RTX 5080 driver 610.88, iGPU driver 32.0.21042.62, debug).

AI use: Claude Code (Claude Opus 5.5) wrote the local test, ran it on both GPUs and drafted this comment.

@slint6614-dev

Copy link
Copy Markdown

AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the hardware device of run_cases (local adapter-selection patch, not part of this PR); cargo test -p victory-shader --release valu_tests at the PR head. Negative control: the PR's non-test files reverted to its merge base, its new tests kept.

  • With this PR, all four ds_swizzle_* tests pass on both AMD GPUs.
  • Negative control: ds_swizzle_fft_mode, ds_swizzle_rotate_mode and ds_swizzle_masked_rotations fail on both GPUs without the fix, so the tests do catch the change on AMD hardware.
  • ds_swizzle_basic_modes also passes without the fix (those modes already worked on main), as expected.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conflicts with current main (ffe310e, post-#566 waterfall loops) in lower.rs/valu_tests.rs — needs a rebase before I can run the suite on the merged tree.

Static review of the change itself: offsets ≥ 0xE000 (FFT) and 0xC000-0xE000 (rotate) are now carved out of the quad/mask swizzle paths, both stay within the lane's group of 32, the FFT path bit-reverses the 5 index bits via unary.i32 op 30 and shifts down by the mask's popcount while keeping the mask bits, and the rotate path follows the ISA pseudocode with the direction bit 0x400 negating the count. The comment honestly flags that the ISA's examples for mask 1 disagree with its pseudocode and states which one hardware follows — good. The host-execution tests (Windows) cover the modes; macOS can't run them.

Rebase and I'll run cargo test -p victory-shader and finish the review.

@upipibi

upipibi commented Nov 24, 2026 •

Copy link
Copy Markdown
Author

Rebased onto main ffe310e (conflicts only in appended tests in valu_tests.rs; the change itself is unchanged) and pushed: new head 95b58c1. cargo test -p victory-shader -- --test-threads=1 gives 124 passed, 0 failed, 1 ignored on an RTX 3070 (driver 616.56), and cargo build -p victory plus the diag build are clean. Thanks @slint6614-dev for the AMD runs with the negative control.

AI-assisted (Claude Code).

lameloball2813-ui pushed a commit to lameloball2813-ui/pelican that referenced this pull request Nov 24, 2026
A rotation with a mask (offset[4:0]) was refused, because the pseudocode
of 12.13.1 and its examples for mask 1 give different lanes. Lyrnc
measured it on RDNA2 hardware (gfx1036, Marvisak/VIctory#606): the
pseudocode is what the hardware does, for all 2048 offsets
0xC000-0xC7FF in wave32 and wave64, and bits 11 and 12 are ignored:

    j = ((i & mask) | ((i + rot) & ~mask)) & 31, rot negated with bit 10

It is lowered so now. A test holds the measured lane lists for 0xC021,
0xC421 and 0xC023, and 0xD821 for the ignored bits.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(cherry picked from commit da1a1f7ba3b4eb28cc46e9e2352b7e32e1bd5c4b)
upipibi added a commit to upipibi/VIctory that referenced this pull request Nov 24, 2026
A rotation with a mask (offset[4:0]) was refused, because the pseudocode
of 12.13.1 and its examples for mask 1 give different lanes. Lyrnc
measured it on RDNA2 hardware (gfx1036, Marvisak#606): the
pseudocode is what the hardware does, for all 2048 offsets
0xC000-0xC7FF in wave32 and wave64, and bits 11 and 12 are ignored:

    j = ((i & mask) | ((i + rot) & ~mask)) & 31, rot negated with bit 10

It is lowered so now. A test holds the measured lane lists for 0xC021,
0xC421 and 0xC023, and 0xD821 for the ignored bits.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@upipibi
upipibi force-pushed the fix/ds-swizzle-rotate-fft branch from da1a1f7 to 95b58c1 Compare November 24, 2026 13:07

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested head 95b58c1 (fast-forward on main ffe310e) on Windows 11 x64 (Rust 1.98.1 MSVC) with NVIDIA GeForce RTX 5090 (Blackwell) and WARP:

  • cargo test -p victory-shader valu_tests: 17 passed, 0 failed (including all 4 new ds_swizzle_* tests: ds_swizzle_basic_modes, ds_swizzle_masked_rotations, ds_swizzle_fft_mode, and ds_swizzle_rotate_mode on both RTX 5090 and WARP)
  • cargo clippy -p victory-shader --all-targets: 0 warnings
  • cargo xtask policy: ok

Clean lowering of ds_swizzle_b32 rotate and FFT modes for offsets >= 0xC000, with solid wave-level GPU test coverage. Approving.

AI-assisted (Gemini 4 Neon).

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Careful work on the two previously-unsupported ds_swizzle_b32 offset ranges:

  • Mode decoding is right: offset >= 0xE000 → FFT, 0xC000..0xE000 → rotate, and both are excluded from the legacy quad/and-or-xor paths (!(fft || rotate) guard). The "other bits don't matter" case (0xFFF0 behaves as 0xE010) falls out of masking correctly.
  • FFT path: unary.i32 op 30 is bfrev (same op v_bfrev_b32 uses), >> 27 brings the reversed 5 index bits down, >> popcount(mask) shifts by the mask width, and i & mask keeps the lane's own mask bits — matches the ISA pseudocode. Identity at mask 0x1F checks out.
  • Rotate path: (i + n) & !mask | (i & mask) with n negated by bit 10 implements "rotate the non-mask bits, keep the mask bits", truncated to 5 bits by the mask — wraparound mod 32 correct. Bits 11/12 ignored as measured.
  • The ISA-example discrepancy is handled honestly: the comment states the ISA's examples for masked rotations list other lanes, and the test comment credits the hardware measurement (all 2048 offsets 0xC000-0xC7FF on gfx1036) that the pseudocode matches. Encoding that measured behavior in valu_tests is exactly the right call.
  • Test harness improvement (build_cs with configurable thread count, swizzle_32 running both hardware and warp backends with graceful skips) is clean and reusable.

Verified locally: test-merge with current main (ffe310e) clean; cargo test -p victory-shader --locked (macOS arm64, rustc 1.98.1) → 96 passed / 0 failed / 1 ignored.

Scope note: all four new tests execute real hardware via the host harness (valu_tests-style), so on this macOS run they compile but the execution paths skip without an SM 6.6 device — the Windows/GPU lane is where they actually assert. The lowering structure itself is platform-independent.

@bvtterbroot

Copy link
Copy Markdown
Contributor

AMD check at 95b58c1 (base ffe310e), on an RDNA3 and an RDNA2 GPU:

  • Windows 11, Rust 1.98.1. Radeon RX 7900 XTX (RDNA3, driver 32.0.32029.2010) and the Ryzen 7 7800X3D's iGPU (RDNA2, gfx1036, driver 32.0.21045.29003). I picked each as the hardware device of run_cases with a local adapter-selection patch that is not part of this PR. WARP runs in each pass as well.
  • cargo test -p victory-shader -p victory-gpu --locked: 174 passed, 0 failed, 2 ignored, on both GPUs. That includes its 4 new tests (ds_swizzle_basic_modes, ds_swizzle_fft_mode, ds_swizzle_masked_rotations, ds_swizzle_rotate_mode).

Synthetic tests only, no game files. Tested with AI assistance (Claude Code).

upipibi and others added 3 commits November 24, 2026 17:32
The lowering read bit 15 of the offset as the quad permutation, whatever
the rest. The ISA (12.13.1) has two more modes above it: from 0xE000 the
FFT order of offset[4:0], and from 0xC000 a rotation of the 32 lanes by
offset[9:5], right with offset[10]. Both are lowered as the pseudocode
gives them. A rotation with a mask (offset[4:0]) is refused by name: the
pseudocode and the examples for mask 1 give different lanes.

Three synthetic GPU tests over one 32-lane wave, expected values from
the ISA's examples; the FFT and rotate ones fail before.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A rotation with a mask (offset[4:0]) was refused, because the pseudocode
of 12.13.1 and its examples for mask 1 give different lanes. Lyrnc
measured it on RDNA2 hardware (gfx1036, Marvisak#606): the
pseudocode is what the hardware does, for all 2048 offsets
0xC000-0xC7FF in wave32 and wave64, and bits 11 and 12 are ignored:

    j = ((i & mask) | ((i + rot) & ~mask)) & 31, rot negated with bit 10

It is lowered so now. A test holds the measured lane lists for 0xC021,
0xC421 and 0xC023, and 0xD821 for the ignored bits.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… helper

Rebased onto 70564ae: try_build_valu_cs now returns a Result, so build_cs
does too and the swizzle test expects it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@upipibi
upipibi force-pushed the fix/ds-swizzle-rotate-fft branch from 95b58c1 to 05babc0 Compare November 24, 2026 23:35
@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Author

Rebased onto main 70564ae (now 05babc0). The only conflict was in valu_tests.rs, next to the new FSPX / dot4 tests. try_build_valu_cs now returns a Result, so the swizzle tests' build_cs does too (third commit). RTX 3070 (driver 616.56) plus WARP: the four ds_swizzle_* tests pass; cargo test -p victory-shader --locked gives 210 passed, 3 failed, and the 3 are #1744's NVIDIA heap-cap failures on main. Thanks to bvtterbroot for the RDNA3 / RDNA2 check of the previous head.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. ds_swizzle_b32 lowering in crates/victory-shader/src/emit/lower.rs now treats offsets from 0xE000 as FFT mode and 0xC000-0xDFFF as rotate mode. Before, those offsets were taken as quad permutations. Offsets below 0xC000 behave as before.

What I checked:

  • The formulas match the RDNA2 ISA pseudocode quoted in the PR.
  • FFT mode uses Bfrev (opcode 30 through unary.i32, as elsewhere in the file), shifted right by 27 and then by popcount(mask).
  • The rotate amount wraps correctly when negated, and both modes keep the lane's group of 32.
  • Masked rotations follow the pseudocode rather than the ISA's conflicting example. Hardware measurements on RDNA2 posted in the thread support that choice.

The four valu_tests cases take their expected values from the ISA's lists and those measurements. Reviewers report that they fail without the fix on AMD hardware.

Note: the swizzle tests are skipped on hosts without 32-lane waves.

CI: no check runs on the head commit.


Generated by Claude Code

fornorharry added a commit to NitricAcidDev/VIctory-Omega-Fork that referenced this pull request Nov 25, 2026
Pull request Marvisak#606 of fxceless/victory-gta6: the staging states are indexed
by the memory of their texture, and one over memory the runtime maps or
unmaps is marked, so that the first list that samples it uploads the new
bytes and not the next one. The index keeps each texture's bytes beside it
here, so that a mapping looks only at the textures it overlaps (a title that
streams maps thousands of ranges a second), and none is let go under the
index's lock.

AI-assisted. Its tests pass on an RTX 5080. Not run with the title.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants