Shaders: lower v_sad_u8, v_msad_u8, v_sad_u16, and v_sad_u32 #1197

Closed
DenRakEiw wants to merge 1 commit from upstream-pr/1197 into main
DenRakEiw commented 2026-11-24 16:24:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-24T16:24:11Z.


## Summary

Lowers RDNA2's unsigned sum-of-absolute-differences (SAD) instruction family in crates/victory-shader/src/emit/lower.rs:

  • v_sad_u8 (VOP3 0x15A): sum of 4 unsigned byte absolute differences + src2 accumulator.
  • v_msad_u8 (VOP3 0x171): masked 4-byte SAD — lanes where src0's byte is 0 contribute 0 regardless of src1 (while src1 == 0 is not skipped).
  • v_sad_u16 (VOP3 0x15C): sum of 2 unsigned 16-bit absolute differences + src2 accumulator.
  • v_sad_u32 (VOP3 0x15D): |src0 - src1| + src2 accumulator.
  • Supports VOP3 clamp (unsigned saturation to u32::MAX on accumulator overflow) across all four instructions, while rejecting invalid omod/abs/neg modifiers.

Tests

Added end-to-end synthetic GFX10 compute shader test v_sad_family_matches_rdna2 in crates/victory-shader/src/emit/valu_tests.rs (validated via dxil.dll and executed on both host GPU and WARP), covering:

  • v_sad_u8 (normal accumulation, u32 wraparound, and clamp saturation to u32::MAX)
  • v_msad_u8 (zero-masking on src0 bytes vs. non-masking on src1 == 0)
  • v_sad_u16 (normal accumulation and clamp saturation)
  • v_sad_u32 (normal accumulation, u32 wraparound, and clamp saturation)

Verification

  • cargo test -p victory-shader v_sad_family: passed on Windows 11 x64 / NVIDIA GeForce RTX 5090 (Blackwell) + WARP
  • cargo clippy -p victory-shader --all-targets: 0 warnings
  • cargo xtask policy: asset policy: 155 files ok

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:issue:6586436559 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1197). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-24T16:24:11Z. --- ## Summary Lowers RDNA2's unsigned sum-of-absolute-differences (SAD) instruction family in `crates/victory-shader/src/emit/lower.rs`: - **`v_sad_u8`** (VOP3 `0x15A`): sum of 4 unsigned byte absolute differences + `src2` accumulator. - **`v_msad_u8`** (VOP3 `0x171`): masked 4-byte SAD — lanes where `src0`'s byte is `0` contribute `0` regardless of `src1` (while `src1 == 0` is not skipped). - **`v_sad_u16`** (VOP3 `0x15C`): sum of 2 unsigned 16-bit absolute differences + `src2` accumulator. - **`v_sad_u32`** (VOP3 `0x15D`): `|src0 - src1|` + `src2` accumulator. - Supports VOP3 `clamp` (unsigned saturation to `u32::MAX` on accumulator overflow) across all four instructions, while rejecting invalid `omod`/`abs`/`neg` modifiers. ## Tests Added end-to-end synthetic GFX10 compute shader test `v_sad_family_matches_rdna2` in `crates/victory-shader/src/emit/valu_tests.rs` (validated via `dxil.dll` and executed on both host GPU and WARP), covering: - `v_sad_u8` (normal accumulation, `u32` wraparound, and `clamp` saturation to `u32::MAX`) - `v_msad_u8` (zero-masking on `src0` bytes vs. non-masking on `src1 == 0`) - `v_sad_u16` (normal accumulation and `clamp` saturation) - `v_sad_u32` (normal accumulation, `u32` wraparound, and `clamp` saturation) ## Verification - `cargo test -p victory-shader v_sad_family`: **passed** on Windows 11 x64 / **NVIDIA GeForce RTX 5090 (Blackwell)** + **WARP** - `cargo clippy -p victory-shader --all-targets`: **0 warnings** - `cargo xtask policy`: `asset policy: 155 files ok` AI-assisted (Gemini 4 Neon).
nullmarsh commented 2026-11-24 17:04:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by nullmarsh on 2026-11-24T16:34:59Z.


Ran on RDNA2 hardware through vkd3d-proton on a Steam Deck (not native Windows): AMD Van Gogh, RADV 26.1.99, SteamOS 3.8.28, Proton 11.0-3 (wine-11.0) with its vkd3d-proton 212991fc. Windows MSVC test binary (rustc 1.100.0) built from head 8967035 on main ffe310e. Your report lists NVIDIA and WARP, so this adds AMD hardware.

  • v_sad_family_matches_rdna2 passes: v_sad_u8, v_msad_u8, v_sad_u16 and v_sad_u32, wrapping and clamp cases.
  • victory-shader lib tests: 120 passed, 1 failed, 1 ignored (main on the same host: 119/1/1). The one failure, stub_pipeline_is_created, fails on main too (likely vkd3d-proton accepting the zero DXIL digest the test expects refused; environment, not this PR).
  • No hangs. A pass means the translated DXIL gives these values on RDNA2 through vkd3d-proton/RADV; it isn't a console measurement.

Not tested: native Windows AMD drivers; WARP (this setup has none, so the WARP leg ran on RADV too); whether the new test fails without the fix on this host.

AI-assisted (Claude Code, Claude Opus 5.5); no game data.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7137891436 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1197#issuecomment-7137891436). Originally posted by [nullmarsh](https://github.com/nullmarsh) on 2026-11-24T16:34:59Z. --- Ran on RDNA2 hardware through vkd3d-proton on a Steam Deck (not native Windows): AMD Van Gogh, RADV 26.1.99, SteamOS 3.8.28, Proton 11.0-3 (wine-11.0) with its vkd3d-proton 212991fc. Windows MSVC test binary (rustc 1.100.0) built from head `8967035` on main `ffe310e`. Your report lists NVIDIA and WARP, so this adds AMD hardware. - `v_sad_family_matches_rdna2` passes: v_sad_u8, v_msad_u8, v_sad_u16 and v_sad_u32, wrapping and clamp cases. - victory-shader lib tests: 120 passed, 1 failed, 1 ignored (main on the same host: 119/1/1). The one failure, `stub_pipeline_is_created`, fails on main too (likely vkd3d-proton accepting the zero DXIL digest the test expects refused; environment, not this PR). - No hangs. A pass means the translated DXIL gives these values on RDNA2 through vkd3d-proton/RADV; it isn't a console measurement. Not tested: native Windows AMD drivers; WARP (this setup has none, so the WARP leg ran on RADV too); whether the new test fails without the fix on this host. AI-assisted (Claude Code, Claude Opus 5.5); no game data.
Zipettte commented 2026-11-24 18:06:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Zipettte on 2026-11-24T18:00:19Z.


Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 8967035: cargo test -p victory-shader passes, 121 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140017793 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1197#issuecomment-7140017793). Originally posted by [Zipettte](https://github.com/Zipettte) on 2026-11-24T18:00:19Z. --- Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 8967035: `cargo test -p victory-shader` passes, 121 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
upstream-sync closed this pull request 2026-11-25 01:05:54 +00:00
Marvisak commented 2026-11-25 01:08:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:34:36Z.


Automated triage for the maintainers. Closing because another open pull request covers the same change.

See #162. Lowers v_sad_u8, v_msad_u8, v_sad_u16 and v_sad_u32, which the title does not use. The clean #162 covers the same instructions plus v_sad_hi_u8.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148655438 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1197#issuecomment-7148655438). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:34:36Z. --- Automated triage for the maintainers. Closing because another open pull request covers the same change. See #162. Lowers v_sad_u8, v_msad_u8, v_sad_u16 and v_sad_u32, which the title does not use. The clean #162 covers the same instructions plus v_sad_hi_u8. If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.
andrewpozdnakov7-jpg commented 2026-11-26 01:04:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-26T00:19:56Z.


The close note asks for anything #162 missed. It landed the wrapping forms, and the integer output path still refuses clamp. I copied v_sad_family_matches_rdna2 unchanged from your 89670357 onto current main 63bcca43. The fixture fails to lower at its first clamped v_sad_u8 with integer output modifiers. On the exact 89670357 head the same test passes, including the clamped SAD/MSAD cases. Both runs used app-local WARP SM 6.6 and the DXIL validator.

The remaining difference is unsigned saturation of the final accumulation. For four differences of 255 and an accumulator of u32::MAX - 10, wrapping gives 1009; the original clamped case returns u32::MAX. The clamp forms are listed in the GFX1030 assembly reference.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7178644076 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1197#issuecomment-7178644076). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-26T00:19:56Z. --- The close note asks for anything #162 missed. It landed the wrapping forms, and the integer output path still refuses `clamp`. I copied `v_sad_family_matches_rdna2` unchanged from your `89670357` onto current main `63bcca43`. The fixture fails to lower at its first clamped `v_sad_u8` with `integer output modifiers`. On the exact `89670357` head the same test passes, including the clamped `SAD`/`MSAD` cases. Both runs used app-local WARP SM 6.6 and the DXIL validator. The remaining difference is unsigned saturation of the final accumulation. For four differences of 255 and an accumulator of `u32::MAX - 10`, wrapping gives `1009`; the original clamped case returns `u32::MAX`. The `clamp` forms are listed in the [GFX1030 assembly reference](https://rocmdocs.amd.com/projects/llvm-project/en/latest/LLVM/llvm/html/AMDGPU/AMDGPUAsmGFX1030.html#vop3).

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!1197
No description provided.