Skip to content

Shaders: lower v_sad_u8, v_sad_hi_u8, v_sad_u16, v_sad_u32 and v_msad_u8 - #162

Merged
Marvisak merged 2 commits into
Marvisak:mainfrom
Rareuncommon:shaders/sad
Nov 25, 2026
Merged

Marvisak merged 2 commits into
Marvisak:mainfrom
Rareuncommon:shaders/sad

Conversation

@Rareuncommon

@Rareuncommon Rareuncommon commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #156; only the top commit (5d6889e) belongs here. Rebuilt on #156's new head 344704e (main ffe310e); the change is the same as 318082f. It needs #156's lowers_valu test helper. I will rebase this onto main when #156 lands.

The five sum-of-absolute-differences instructions, which the emitter refuses by name, now translate. The top commit adds 48 lines to crates/victory-shader/src/emit/lower.rs: 38 of lowering and a 10-line test.

What is in it

One arm in Lower::integer, next to the 24-bit multiply-adds:

  • v_sad_u8: src2 + the sum over the four bytes of |a − b|, unsigned, wrapping at 32 bits.
  • v_sad_hi_u8: (that byte sum << 16) + src2.
  • v_sad_u16: src2 + the sum over the two halves.
  • v_sad_u32: |a − b| + src2 on the whole dwords.
  • v_msad_u8: as v_sad_u8, leaving out a byte whose mask byte (src1) is 0.

|a − b| is the larger minus the smaller (an unsigned compare and two selects), so nothing wraps inside a part.

A test, sums_of_absolute_differences_lower, lowers all five (VOP3 0x15A to 0x15D and 0x171).

Tested

macOS 27 arm64, Rust 1.98.1, debug, no GPU and no game files:

  • cargo test -p victory-shader: 98 passed, 0 failed, 1 ignored (97 on Shaders: lower v_mul_hi_u32_u24 and v_mul_hi_i32_i24 #156's head, plus the new test).
  • Without the lowering the new test fails on the first opcode with "v_sad_u8 is not supported".
  • cargo fmt -p victory-shader -- --check: no diff in the lines this PR adds. cargo clippy -p victory-shader --all-targets --locked -- -D warnings stops on two unused imports in emit/mod.rs on macOS, on main as well, so clippy as CI runs it was not checked here. cargo xtask policy: 154 files ok.

Checked on hardware by others (see the comments)

  • @Lyrnc: the five instructions on RDNA2 silicon (gfx1036), 900 operand triples each, all matching the rules above, including v_msad_u8 skipping a byte whose src1 byte is 0 (the src0 reading matches only 443 of 900).
  • @DaviTostes: cargo test -p victory-shader on Arch Linux, 58 passed.

These are measurements of the instruction rules on hardware, not runs of this PR's emitted DXIL.

Not tested (by me)

  • The values: nothing here has executed the emitted DXIL. The value tests are Windows-only and need a GPU, which I do not have. The rules are the RDNA2 ISA's as I read them; v_msad_u8's mask (a zero byte in src1 is skipped) is the one I would most like checked on hardware.
  • The Windows-only crates were not built.
  • I cannot say how many of the title's programs use these; I have no game files.

No game data: the test input is hand-written instruction words.

AI-assisted: yes (Claude Code).

🤖 Generated with Claude Code

@DaviTostes

Copy link
Copy Markdown

Tested 318082f on Arch Linux x86_64, rustc 1.98.1: pass.

No game data used.

AntoninMichelq pushed a commit to AntoninMichelq/VIctory that referenced this pull request Nov 24, 2026
…6, v_sad_u32 and v_msad_u8) into the integration

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AZ9pSB4ZoXYUifuXg8gm66
@Lyrnc

Lyrnc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

RDNA2 hardware measurement of the five rules, including the v_msad_u8 mask you asked about: all five match.

Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr) runs v_sad_u8, v_sad_hi_u8, v_sad_u16, v_sad_u32 and v_msad_u8 (vD, v4, v5, v6) on 900 operand triples. The triples are pairs from 13 edge patterns (0, 0xffffffff, 0x80808080, 0x7f7f7f7f, byte- and half-swapped patterns...) with src2 0, 5 and 0xfffffff0 (to see the 32-bit wrap), plus 300 random triples. On top of those: 140 with zero bytes forced into src1 in each position and combination, and 60 with zero bytes in src0. RDNA2 desktop silicon, not the console's. No game data.

Rule (your description) Match
v_sad_u8: src2 + Σ|a_i − b_i| over 4 bytes, wrapping at 32 bits 900/900
v_sad_hi_u8: (byte sum << 16) + src2 900/900
v_sad_u16: src2 + Σ over the two halves 900/900
v_sad_u32: |a − b| + src2 900/900
v_msad_u8: as v_sad_u8, skipping a byte whose src1 byte is 0 900/900
(alternative: skipping a byte whose src0 byte is 0) 443/900

Mask examples: src0 0xffffffff, src1 0x00000000, src2 5 gives v_msad_u8 = 0x5 (every byte skipped) where v_sad_u8 = 0x401. src1 0x00ff00ff gives 0x0 where v_sad_u8 gives 0x1fe: only the two non-zero src1 bytes count, and they match.

So the reading you were least sure of, a zero byte in src1 being skipped, is what the hardware does.

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.

@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.

Verified locally on macOS 26 arm64, rustc 1.98.1 (aarch64-apple-darwin) at head 318082f:

  • cargo test -p victory-shader --locked: compiles clean, all ungated tests pass (no regressions vs main's 56).

Scope note: valu_tests/image_tests in victory-shader are #[cfg(all(test, windows))] — they need the Windows host backend (D3D12/dxil.dll), so they did not run here. What I verified is compilation on macOS plus all ungated tests. For this PR that means the new v_sad/v_msad lowering coverage still needs a Windows run before merge — I can't execute it on this machine. The lowering changes themselves compile everywhere.

The high dword of the 48-bit product whose low dword v_mul_u32_u24 and v_mul_i32_i24 already give: the low
24 bits of each operand, zero- or sign-extended, through the existing mul_hi.

The test module gets lowers_valu, a one-thread compute program with root constants in v1..v4 and a buffer
store of v0 (built with lower_with), so a vector instruction can be checked for lowering without a GPU.

AI-assisted (Claude Code).
src2 plus the sum of the unsigned differences |a - b| of the operands' bytes (v_sad_u8), halves (v_sad_u16)
or whole dwords (v_sad_u32), wrapping at 32 bits. v_sad_hi_u8 adds the byte sum shifted to the high half;
v_msad_u8 leaves out a byte whose mask byte (src1) is 0.

AI-assisted (Claude Code).
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
KainanP pushed a commit to KainanP/VIctory that referenced this pull request Nov 24, 2026
…fied together

Merges Marvisak#156 Marvisak#162 Marvisak#168 Marvisak#169 Marvisak#368 Marvisak#379 Marvisak#456 Marvisak#587 Marvisak#990 Marvisak#1021 Marvisak#1048 Marvisak#1052 Marvisak#1053
Marvisak#1095 Marvisak#1142 Marvisak#1190 Marvisak#1211 Marvisak#1223 Marvisak#1259 Marvisak#1293 on top of the bundle of Marvisak#1402
(which is on Marvisak#1319, on main ffe310e), each head unchanged. Hunks that
conflicted in emit/lower.rs, emit/valu_tests.rs and emit/diff_tests.rs were
merged by keeping both sides (additions next to each other); a PR was kept
only if cargo clippy -p victory-shader --all-targets -- -D warnings still
passed with it. No instruction name is handled in two places.

Marvisak#98 is not in this version: an offline measurement on a retail dump showed
it loses 31 programs that validate on Marvisak#1402.

cargo clippy --workspace --all-targets --locked -- -D warnings, with and
without --all-features, is clean and cargo test --workspace --locked passes
(381 passed, 6 ignored; main has 342 passed, 6 ignored).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
@Marvisak
Marvisak merged commit 83211b9 into Marvisak:main Nov 25, 2026
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.

5 participants