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

Manually merged
github merged 2 commits from gh-pr/162/head into gh-pr/162/base 2026-11-26 03:52:50 +00:00
Owner

@⁠Rareuncommon on GitHub · 2026-11-24 01:01 UTC · original
Rareuncommon:shaders/sad → main · +109 −0 · 1 files · 2 commits · merged 2026-11-25 00:18 UTC

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 #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

**@⁠Rareuncommon** on GitHub · 2026-11-24 01:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/162) `Rareuncommon:shaders/sad` → `main` · +109 −0 · 1 files · 2 commits · merged 2026-11-25 00:18 UTC **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 #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](https://claude.com/claude-code)
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-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:52:48 +00:00
github manually merged commit 5d6889e8be into gh-pr/162/base 2026-11-26 03:52:50 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 01:18 UTC · original

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

  • cargo test -p victory-shader: 58 passed, 0 failed (2 test binaries), including the 2 tests this PR adds.
  • The full workspace still needs #112 for the victory-gpu E0425.

No game data used.

**@⁠DaviTostes** on GitHub · 2026-11-24 01:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/162#issuecomment-7118975354) Tested `318082f` on **Arch Linux x86_64, rustc 1.98.1**: **pass**. - `cargo test -p victory-shader`: **58 passed, 0 failed** (2 test binaries), including the 2 tests this PR adds. - The full workspace still needs #112 for the `victory-gpu` E0425. No game data used.
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-24 05:55 UTC · original

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.

**@⁠Lyrnc** on GitHub · 2026-11-24 05:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/162#issuecomment-7123407418) 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.
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!162
No description provided.