Shaders: lower SAD differences through UMax and UMin #3640

Open
Ingenioeren wants to merge 1 commit from upstream-pr/3640 into main
Ingenioeren commented 2026-11-28 01:43:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Ingenioeren on 2026-11-28T01:43:45Z.


The four unsigned SAD forms now compute each field's absolute difference with DXIL UMax/UMin instead of a comparison and two selects. They also omit shifts by zero, the top field's redundant mask, and the initial zero-add. Accumulation and the high-byte form's shift still wrap at 32 bits.

Written instructions for minimal synthetic compute shaders with dynamic inputs, including setup, the UAV store and terminators:

Instruction Main This PR Reduction
v_sad_u8 55 46 16.4%
v_sad_hi_u8 56 47 16.1%
v_sad_u16 37 30 18.9%
v_sad_u32 24 22 8.3%

These measure DXIL size, not GPU time or frame rate. The helper lives in emit/lower/sad.rs; lower.rs shrinks from 520,563 to 520,115 bytes, below the 524,288-byte asset-policy limit.

v_msad_u8 is split into its own arm with the same emitted lowering (63 instructions). Its intrinsic optimization remains in #3564. Both PRs touch the former shared arm, so whichever lands second will need a small rebase. The test-only instruction counter is byte-identical to #3600 and the other open shader performance PRs.

Validation, using only synthetic shaders:

  • cargo test --locked --offline -p victory-shader sad_ -- --nocapture: the count regression failed before this change (55/56/37/24 against budgets 46/47/30/22), then passed after. The behavior test passed on both revisions: 70,861 inputs per form, each with and without EXEC masking, for all four SAD forms plus unchanged MSAD. RX 6750 XT and WARP produced zero CPU-model mismatches and zero adapter disagreements: 708,610 checked results per adapter/revision. Inputs cover every pair of byte values, individual field positions, unsigned boundaries, equal values, accumulator wraparound, all MSAD mask combinations, and deterministic random dwords. The CPU oracle uses byte arrays and a wide accumulator.
  • cargo test --locked --offline -p victory-shader --no-fail-fast: baseline 300 passed / 2 failed / 3 ignored; final 302 passed / the same 2 failed / 3 ignored. The known failures are emit::rect::tests::rectangles_get_their_fourth_corner and emit::tests::elements_sharing_a_row_keep_their_order, both rejected by the local dxil.dll for a Pipeline State Validation mismatch. Integration tests: 3 passed / 1 ignored; doc tests: 0. No checks or assertions were weakened.
  • cargo clippy --locked --offline -p victory-shader --all-targets --all-features -- -D warnings: passed.
  • cargo run --locked --offline -p xtask -- policy --staged: passed, 4 files OK.
  • cargo fmt --check: retains upstream formatting failures. Comparing each changed existing file with formatted main shows identical formatting deviations; the new module and test are formatted.
  • git diff --check: passed.

The related integer min/max PR #3027 has an unresolved pipeline-compilation hang on RADV. This PR also emits UMin/UMax; the Windows tests above do not resolve that report. Linux/RADV, other GPU drivers, and game behavior were not tested. No game was launched and no game files or game-derived data were used.

AI assistance: prepared with OpenAI Codex, reviewed by Ingenioeren.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5535479741 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3640). Originally posted by [Ingenioeren](https://github.com/Ingenioeren) on 2026-11-28T01:43:45Z. --- The four unsigned SAD forms now compute each field's absolute difference with DXIL UMax/UMin instead of a comparison and two selects. They also omit shifts by zero, the top field's redundant mask, and the initial zero-add. Accumulation and the high-byte form's shift still wrap at 32 bits. Written instructions for minimal synthetic compute shaders with dynamic inputs, including setup, the UAV store and terminators: | Instruction | Main | This PR | Reduction | |---|---:|---:|---:| | `v_sad_u8` | 55 | 46 | 16.4% | | `v_sad_hi_u8` | 56 | 47 | 16.1% | | `v_sad_u16` | 37 | 30 | 18.9% | | `v_sad_u32` | 24 | 22 | 8.3% | These measure DXIL size, not GPU time or frame rate. The helper lives in `emit/lower/sad.rs`; `lower.rs` shrinks from 520,563 to 520,115 bytes, below the 524,288-byte asset-policy limit. `v_msad_u8` is split into its own arm with the same emitted lowering (63 instructions). Its intrinsic optimization remains in #3564. Both PRs touch the former shared arm, so whichever lands second will need a small rebase. The test-only instruction counter is byte-identical to #3600 and the other open shader performance PRs. Validation, using only synthetic shaders: - `cargo test --locked --offline -p victory-shader sad_ -- --nocapture`: the count regression failed before this change (55/56/37/24 against budgets 46/47/30/22), then passed after. The behavior test passed on both revisions: 70,861 inputs per form, each with and without EXEC masking, for all four SAD forms plus unchanged MSAD. RX 6750 XT and WARP produced zero CPU-model mismatches and zero adapter disagreements: 708,610 checked results per adapter/revision. Inputs cover every pair of byte values, individual field positions, unsigned boundaries, equal values, accumulator wraparound, all MSAD mask combinations, and deterministic random dwords. The CPU oracle uses byte arrays and a wide accumulator. - `cargo test --locked --offline -p victory-shader --no-fail-fast`: baseline 300 passed / 2 failed / 3 ignored; final 302 passed / the same 2 failed / 3 ignored. The known failures are `emit::rect::tests::rectangles_get_their_fourth_corner` and `emit::tests::elements_sharing_a_row_keep_their_order`, both rejected by the local dxil.dll for a Pipeline State Validation mismatch. Integration tests: 3 passed / 1 ignored; doc tests: 0. No checks or assertions were weakened. - `cargo clippy --locked --offline -p victory-shader --all-targets --all-features -- -D warnings`: passed. - `cargo run --locked --offline -p xtask -- policy --staged`: passed, 4 files OK. - `cargo fmt --check`: retains upstream formatting failures. Comparing each changed existing file with formatted main shows identical formatting deviations; the new module and test are formatted. - `git diff --check`: passed. The related integer min/max PR #3027 has an unresolved pipeline-compilation hang on RADV. This PR also emits UMin/UMax; the Windows tests above do not resolve that report. Linux/RADV, other GPU drivers, and game behavior were not tested. No game was launched and no game files or game-derived data were used. AI assistance: prepared with OpenAI Codex, reviewed by Ingenioeren.
oni-swr commented 2026-11-28 02:14:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-28T02:14:53Z.


Independent WARP-only verification of #3640 (AI disclosure: Claude Sonnet 5.5; physical GPU not used)

  • Head bffaa159498d2f49721c2de6d4ed50aac1c00381 (still the PR head), base origin/main 63bcca43c6a00a758510398fa43096ce7e4f7df0. The merge-base is the base itself, so the merge is clean, with git merge-tree giving tree 9d9651c8.
  • Environment: VICTORY_WARP=1, VICTORY_D3D12_WARP=1, WARP d3d10warp.dll 10.0.26100.9818, Windows SDK dxil.dll 1.8.2502.11 (10.0.26100.0).

Control check. The tests don't exist on base, so I kept the PR's plumbing and tests and reverted only the SAD lowering arm to base's text.

  • sad_instruction_counts FAILED with 55/56/37/24/63 against budgets 46/47/30/22/63.
  • sad_fields_and_wrapping_accumulators_match_the_reference (the behavior oracle) passed.
  • On the PR head, cargo test --locked --offline -p victory-shader sad_ -j 4 gave 2 passed, 0 failed. Counts went 55→46, 56→47, 37→30, 24→22, and v_msad_u8 stayed at 63→63. The oracle covered 5 ops × guarded/unguarded × 70,861 inputs with 0 model mismatches and 0 WARP differences.

Full cargo test -p victory-shader --no-fail-fast -j 4

lib resource_tracking doc tests
base 63bcca43 298 passed / 4 failed / 3 ignored 3 / 0 / 1 ignored 0 / 0 / 0
head bffaa159 300 passed / 4 failed / 3 ignored 3 / 0 / 1 ignored 0 / 0 / 0

The same 4 tests fail on both, so they are pre-existing WARP limitations on base:

  • synthetic_tests::a_mask_joined_with_data_at_a_uniform_loop_is_its_number
  • synthetic_tests::gathers_at_a_level_match_the_hardware
  • synthetic_tests::permutes_mask_high_address_bits_and_divergent_branches
  • synthetic_tests::permutes_read_the_lane_named_and_zero_from_lanes_left_out

The difference of +2 passed is exactly the two new tests.

Other checks (merged tree)

  • cargo clippy -p victory-shader --all-targets --all-features -- -D warnings: clean.
  • cargo run -p xtask -- policy --staged with the merge staged: asset policy: 4 files ok.
  • lower.rs is 520,115 bytes on the head, down from 520,563 on base, against the 524,288-byte limit. That leaves 4,173 bytes of headroom.

Diff review. I read every hunk and found no defects.

  • sad.rs: v_sad_u8, v_sad_hi_u8, v_sad_u16 and v_sad_u32 use UMax/UMin (DXIL opcodes 39/40) and a subtract.
    • The top field skips the mask because the logical shift already zero-extends it.
    • Field 0 skips the shift.
    • For u32, (1 << width) is never evaluated, so there is no shift-by-32 overflow.
    • Accumulation wraps mod 2³² through the i32 add.
    • The hi_u8 shift by 16 wraps correctly.
  • lower.rs:
    • v_msad_u8 keeps its separate lowering (compare, select, mask on src1 byte ≠ 0), and the behavior oracle covers all 16 mask-byte combinations.
    • The sad module registration and the lowered.instructions test field are cfg(test) only.
    • Making lower_valu pub(super) is harmless.
  • bitcode.rs: written_instructions is cfg(test) and counts live phis and body instructions plus one terminator per laid-out block.
  • semantics_tests.rs: the oracle uses independent Rust references (abs_diff, byte and u16 chunks, u64 accumulator) plus edge values, all byte pairs, per-field carry and wrap, and 4096 xorshift cases. It runs both unguarded and under a partial EXEC mask.
  • Overlap: #3564 (open) also changes bitcode.rs/lower.rs for v_msad_u8 → Msad, and #3027 (open) changes bitcode.rs/lower.rs for min3/max3/med3. Textual conflicts are possible in lower.rs. In particular, the v_msad_u8 arm moved in this PR, and the 0x171 count budget (63) in sad.rs would need updating when #3564 lands. The semantic overlap is none. Neither PR touches the four SAD ops.

No game files, dumps, paths, shader names or retail hashes are included.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7232233267 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3640#issuecomment-7232233267). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-28T02:14:53Z. --- **Independent WARP-only verification of #3640** (AI disclosure: Claude Sonnet 5.5; physical GPU not used) - Head `bffaa159498d2f49721c2de6d4ed50aac1c00381` (still the PR head), base `origin/main` `63bcca43c6a00a758510398fa43096ce7e4f7df0`. The merge-base is the base itself, so the merge is clean, with `git merge-tree` giving tree `9d9651c8`. - Environment: `VICTORY_WARP=1`, `VICTORY_D3D12_WARP=1`, WARP `d3d10warp.dll` 10.0.26100.9818, Windows SDK `dxil.dll` 1.8.2502.11 (10.0.26100.0). **Control check.** The tests don't exist on base, so I kept the PR's plumbing and tests and reverted only the SAD lowering arm to base's text. - `sad_instruction_counts` FAILED with 55/56/37/24/63 against budgets 46/47/30/22/63. - `sad_fields_and_wrapping_accumulators_match_the_reference` (the behavior oracle) passed. - On the PR head, `cargo test --locked --offline -p victory-shader sad_ -j 4` gave 2 passed, 0 failed. Counts went 55→46, 56→47, 37→30, 24→22, and `v_msad_u8` stayed at 63→63. The oracle covered 5 ops × guarded/unguarded × 70,861 inputs with 0 model mismatches and 0 WARP differences. **Full `cargo test -p victory-shader --no-fail-fast -j 4`** | | lib | resource_tracking | doc tests | |---|---|---|---| | base `63bcca43` | 298 passed / 4 failed / 3 ignored | 3 / 0 / 1 ignored | 0 / 0 / 0 | | head `bffaa159` | 300 passed / 4 failed / 3 ignored | 3 / 0 / 1 ignored | 0 / 0 / 0 | The same 4 tests fail on both, so they are pre-existing WARP limitations on base: - `synthetic_tests::a_mask_joined_with_data_at_a_uniform_loop_is_its_number` - `synthetic_tests::gathers_at_a_level_match_the_hardware` - `synthetic_tests::permutes_mask_high_address_bits_and_divergent_branches` - `synthetic_tests::permutes_read_the_lane_named_and_zero_from_lanes_left_out` The difference of +2 passed is exactly the two new tests. **Other checks (merged tree)** - `cargo clippy -p victory-shader --all-targets --all-features -- -D warnings`: clean. - `cargo run -p xtask -- policy --staged` with the merge staged: `asset policy: 4 files ok`. - `lower.rs` is 520,115 bytes on the head, down from 520,563 on base, against the 524,288-byte limit. That leaves 4,173 bytes of headroom. **Diff review.** I read every hunk and found no defects. - `sad.rs`: `v_sad_u8`, `v_sad_hi_u8`, `v_sad_u16` and `v_sad_u32` use UMax/UMin (DXIL opcodes 39/40) and a subtract. - The top field skips the mask because the logical shift already zero-extends it. - Field 0 skips the shift. - For u32, `(1 << width)` is never evaluated, so there is no shift-by-32 overflow. - Accumulation wraps mod 2³² through the i32 add. - The `hi_u8` shift by 16 wraps correctly. - `lower.rs`: - `v_msad_u8` keeps its separate lowering (compare, select, mask on src1 byte ≠ 0), and the behavior oracle covers all 16 mask-byte combinations. - The `sad` module registration and the `lowered.instructions` test field are `cfg(test)` only. - Making `lower_valu` `pub(super)` is harmless. - `bitcode.rs`: `written_instructions` is `cfg(test)` and counts live phis and body instructions plus one terminator per laid-out block. - `semantics_tests.rs`: the oracle uses independent Rust references (`abs_diff`, byte and u16 chunks, u64 accumulator) plus edge values, all byte pairs, per-field carry and wrap, and 4096 xorshift cases. It runs both unguarded and under a partial EXEC mask. - Overlap: #3564 (open) also changes `bitcode.rs`/`lower.rs` for `v_msad_u8` → Msad, and #3027 (open) changes `bitcode.rs`/`lower.rs` for min3/max3/med3. Textual conflicts are possible in `lower.rs`. In particular, the `v_msad_u8` arm moved in this PR, and the `0x171` count budget (63) in `sad.rs` would need updating when #3564 lands. The semantic overlap is none. Neither PR touches the four SAD ops. No game files, dumps, paths, shader names or retail hashes are included.
Dukestar69 commented 2026-11-28 02:48:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-28T02:48:52Z.

Review: APPROVED


LGTM — verified on Windows (RTX 5090 + WARP):

  • cargo test -p victory-shader --lib sad passed (2/2: sad_instruction_counts and sad_fields_and_wrapping_accumulators_match_the_reference — 70,861 inputs × 10 variants with 0 mismatches on RTX 5090 hardware and WARP)
  • cargo test -p victory-shader --lib sums_of_absolute_differences_lower passed
  • cargo clippy -p victory-shader --all-targets -- -D warnings passed with 0 warnings
  • cargo xtask policy passed (192 files ok)

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6095262324 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3640#pullrequestreview-6095262324). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-28T02:48:52Z. **Review:** APPROVED --- LGTM — verified on Windows (RTX 5090 + WARP): - `cargo test -p victory-shader --lib sad` passed (2/2: `sad_instruction_counts` and `sad_fields_and_wrapping_accumulators_match_the_reference` — 70,861 inputs × 10 variants with 0 mismatches on RTX 5090 hardware and WARP) - `cargo test -p victory-shader --lib sums_of_absolute_differences_lower` passed - `cargo clippy -p victory-shader --all-targets -- -D warnings` passed with 0 warnings - `cargo xtask policy` passed (`192 files ok`) Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3640:upstream-pr/3640'
git switch 'upstream-pr/3640'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3640'
git switch 'upstream-pr/3640'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3640'
git switch 'upstream-pr/3640'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3640'
git switch 'main'
git merge --squash 'upstream-pr/3640'
git switch 'main'
git merge --ff-only 'upstream-pr/3640'
git switch 'main'
git merge 'upstream-pr/3640'
git push origin 'main'
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!3640
No description provided.