Shaders: lower v_sad_u8, v_sad_hi_u8, v_sad_u16, v_sad_u32 and v_msad_u8 #162
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!162
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/162/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 UTCStacked on #156; only the top commit (
5d6889e) belongs here. Rebuilt on #156's new head344704e(mainffe310e); the change is the same as318082f. It needs #156'slowers_valutest helper. I will rebase this ontomainwhen #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: asv_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).cargo fmt -p victory-shader -- --check: no diff in the lines this PR adds.cargo clippy -p victory-shader --all-targets --locked -- -D warningsstops on two unused imports inemit/mod.rson macOS, onmainas 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)
v_msad_u8skipping a byte whose src1 byte is 0 (the src0 reading matches only 443 of 900).cargo test -p victory-shaderon 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)
v_msad_u8's mask (a zero byte in src1 is skipped) is the one I would most like checked on hardware.No game data: the test input is hand-written instruction words.
AI-assisted: yes (Claude Code).
🤖 Generated with Claude Code
@DaviTostes on GitHub · 2026-11-24 01:18 UTC · original
Tested
318082fon 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.victory-gpuE0425.No game data used.
@Lyrnc on GitHub · 2026-11-24 05:55 UTC · original
RDNA2 hardware measurement of the five rules, including the
v_msad_u8mask 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_u32andv_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 src20,5and0xfffffff0(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.v_sad_u8: src2 + Σ|a_i − b_i| over 4 bytes, wrapping at 32 bitsv_sad_hi_u8: (byte sum << 16) + src2v_sad_u16: src2 + Σ over the two halvesv_sad_u32: |a − b| + src2v_msad_u8: asv_sad_u8, skipping a byte whose src1 byte is 0Mask examples: src0
0xffffffff, src10x00000000, src25givesv_msad_u8=0x5(every byte skipped) wherev_sad_u8=0x401. src10x00ff00ffgives0x0wherev_sad_u8gives0x1fe: 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.