Skip to content

Shaders: lower v_swap_b32 and v_xnor_b32 - #127

Closed
7ROBE wants to merge 1 commit into
Marvisak:mainfrom
7ROBE:fix/shader-xnor-swap
Closed

7ROBE wants to merge 1 commit into
Marvisak:mainfrom
7ROBE:fix/shader-xnor-swap

Conversation

@7ROBE

@7ROBE 7ROBE commented Nov 24, 2026

Copy link
Copy Markdown

What is in it

  • Lowers RDNA2 v_swap_b32 (swapping two VGPRs under the active EXEC guard; ir.rs already marks both operands as read and written) in crates/victory-shader/src/emit/lower.rs.
  • Lowers RDNA2 v_xnor_b32 (~(src0 ^ src1)) in crates/victory-shader/src/emit/lower.rs.
  • Adds a synthetic compute-shader unit test v_xnor_b32_and_v_swap_b32_translate covering v_xnor_b32 and v_swap_b32 (distinct VGPRs and self-swap).

Refs #97.

Tested

  • cargo xtask policy: asset policy: 150 files ok
  • cargo test --workspace: 240 passed, 0 failed, 3 ignored (including emit::lower::tests::v_xnor_b32_and_v_swap_b32_translate)
  • cargo clippy --workspace --all-targets: passed (0 warnings)
  • rustfmt --check --edition 2024 crates/victory-shader/src/emit/lower.rs: passed

Hardware / OS

  • Windows 11 Pro (26H2), Intel Core i5-14600KF, AMD Radeon RX 9060 XT (Adrenalin 26.11.2)

AI-assisted

  • Yes (Antigravity / Gemini 3.1 Pro)

@raffclar

Copy link
Copy Markdown
Contributor

This isn't needed anymore. #58 added them

@tanishq-dubey

Copy link
Copy Markdown

Related self-swap edge case on main at 9aef3d9: a synthetic compute shader that reads v1 after v_swap_b32 v1, v1 and stores it to a UAV fails translation with a value defined after its use (offset 36). The existing self-swap here is immediately followed by s_endpgm, so it may not exercise a later use.

In ir.rs, the same VGPR is recorded twice as a write; skipping the second write when v.dst.as_ref() == v.src.first() fixed the regression. An end-to-end synthetic test then returned both 0x12345678 and 0xffffffff unchanged on an RTX 5090 using Wine 11.22 and vkd3d-proton. No game assets used. AI-assisted.

@DaviTostes

Copy link
Copy Markdown

Tested 60e8adf on Arch Linux x86_64, rustc 1.98.1: pass.

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

Lyrnc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

RDNA2 hardware check: both match.

Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. Hand-written instructions in a HIP kernel (assembled by the AMD driver's amd_comgr). RDNA2 desktop silicon, not the console's. No game data. 400 operand pairs (edge values and 300 random).

  • v_xnor_b32: ~(src0 ^ src1), 400/400.
  • v_swap_b32 v16, v17 with distinct registers swaps them, 400/400. v_swap_b32 v18, v18 leaves the register unchanged, 400/400 (your self-swap case).

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.

@Dathinel

Copy link
Copy Markdown
Contributor

I checked what remains to integrate after #58. On main@d2a4bae1, applying #127's test function without either lowering change gives 1 passed for v_xnor_b32_and_v_swap_b32_translate. Clippy also passes in that configuration.

With the full #127 change applied to the same main, cargo clippy --frozen -p victory-shader --features host --all-targets -- -D warnings reports two unreachable match arms in lower.rs, one for each opcode. The integrated file matches GitHub's test merge.

I'd drop the duplicate lowering changes. The added dst != src check is inside the unreachable swap arm. If you want to keep a distinct regression test here, the case reported on 9aef3d9 reads the register after swapping it with itself. The current test ends immediately after that instruction and checks translation. The RDNA2 measurement above supplies the expected register value for a readback test. AI tools assisted with the comparison.

@Marvisak Marvisak closed this Nov 24, 2026
cmt8 added a commit to cmt8/VIctory that referenced this pull request Nov 25, 2026
Under Wine and Proton, D3DCompile is Wine's d3dcompiler_47, whose HLSL
compiler is vkd3d-shader's. It refuses the clamp (E5005: no Load or
Interlocked* methods on RWByteAddressBuffer; [allow_uav_condition] is
"not yet implemented") and ignores [RootSignature], so the clamp is never
made and every indirect execution is dropped ("no argument clamp:
indirect execution dropped"). The window check's compare shader and the
tests' WORKER_CS fail the same way.

The three shaders are rewritten with what both compilers take; the rules
are unchanged (MOST_DRAW, one budget charge per group of 64 in lane order,
the 2^27 budget, the window rules, the gathered records):

- The buffers a shader writes are RWStructuredBuffer<uint> indexed in
  words: the destination record at id.x * (stride / 4), copied words at
  + off / 4; the budget's words 0, 1, 2 (bytes 0, 4, 8 as before); the
  window check's results at words 0, 1, 2.., 10... A root UAV can be
  either kind, and stride, offsets and addresses are already refused
  unless multiples of 4 (validate_layout, record_clamp_as, gatherable,
  record_gather_as), so the same bytes are written. The read-only source
  and count stay ByteAddressBuffer: Load on those builds. Gathered
  records (bit 2) read their offsets from the table behind count_buf as
  before and land in slot id.x, in words like the rest.
- Interlocked* are called as functions on the buffer's word.
- [allow_uav_condition] is dropped: Microsoft's d3dcompiler_47 builds the
  loop without it.
- The root signatures are serialized in code (host::root_signature,
  root_constants, root_view; version 1.1, the same parameters as the RS
  strings, root SRVs static while set and root UAVs volatile, the
  defaults of the strings) and passed to the pipeline. The gather pass
  binds the same five parameters; its table is upload memory written
  before the list is submitted, which a root SRV static while set allows.

A test checks the clamp's output on the host GPU word by word (offsets
into the source, a count at an offset, strides past the smallest, every
kind): the WARP value tests skip where there is no WARP adapter, as under
Wine.

The form descends from Marvisak/VIctory PR Marvisak#1279 (Mumogg), written for
upstream's simpler clamp; fork PR Marvisak#127 (BitWuehler) did the same for this
fork's clamp before the group charge and the window check came in.

Tested: cargo xwin build --release -p victory; cargo xtask policy (180
files ok). Windows tests under Wine 11.22 (Arch wine 11.22-1; its
d3dcompiler_47 is vkd3d-shader 2.1, Wine bundled) with the vkd3d-proton
of Proton Experimental (experimental-11.0-20261112) on an RX 7900 XTX
(RADV, Mesa 26.3.1): victory-gpu 176 passed, 3 failed (was 170 passed,
8 failed on this commit's parent); fixed:
indirect::tests::argument_clamp_builds,
host_list_budget_starts_empty_at_each_submission,
host_list_reuses_and_grows_scratch_across_submissions,
host_list_window_check_finds_a_record_written_under_its_window,
host_list_window_executions_need_no_barrier_of_their_own; no new
failure. indirect:: passes (16 of 16) also with Proton Experimental's own
d3dcompiler_47 (vkd3d-shader 2.1) and with Microsoft's d3dcompiler_47
10.0.26100.3916 loaded natively (the parent: 10 of 15 with Proton's,
15 of 15 with Microsoft's). With the WARP harness pointed at the RX 7900
XTX for the run only, the WARP value tests, the gathered records' one
included, pass under Wine's compiler (the parent's clamp does not build
there); warp_budget_saturates_without_wraparound_and_preserves_draw_fields
then fails now and then on this commit and on its parent alike, under
Microsoft's compiler too (on a real card the second group can charge
first). Not run on Windows.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants