Shaders: lower SAD differences through UMax and UMin #3640
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3640"
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?
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:
v_sad_u8v_sad_hi_u8v_sad_u16v_sad_u32These measure DXIL size, not GPU time or frame rate. The helper lives in
emit/lower/sad.rs;lower.rsshrinks from 520,563 to 520,115 bytes, below the 524,288-byte asset-policy limit.v_msad_u8is 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 areemit::rect::tests::rectangles_get_their_fourth_cornerandemit::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.
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)
bffaa159498d2f49721c2de6d4ed50aac1c00381(still the PR head), baseorigin/main63bcca43c6a00a758510398fa43096ce7e4f7df0. The merge-base is the base itself, so the merge is clean, withgit merge-treegiving tree9d9651c8.VICTORY_WARP=1,VICTORY_D3D12_WARP=1, WARPd3d10warp.dll10.0.26100.9818, Windows SDKdxil.dll1.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_countsFAILED 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.cargo test --locked --offline -p victory-shader sad_ -j 4gave 2 passed, 0 failed. Counts went 55→46, 56→47, 37→30, 24→22, andv_msad_u8stayed 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 463bcca43bffaa159The 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_numbersynthetic_tests::gathers_at_a_level_match_the_hardwaresynthetic_tests::permutes_mask_high_address_bits_and_divergent_branchessynthetic_tests::permutes_read_the_lane_named_and_zero_from_lanes_left_outThe 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 --stagedwith the merge staged:asset policy: 4 files ok.lower.rsis 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_u16andv_sad_u32use UMax/UMin (DXIL opcodes 39/40) and a subtract.(1 << width)is never evaluated, so there is no shift-by-32 overflow.hi_u8shift by 16 wraps correctly.lower.rs:v_msad_u8keeps its separate lowering (compare, select, mask on src1 byte ≠ 0), and the behavior oracle covers all 16 mask-byte combinations.sadmodule registration and thelowered.instructionstest field arecfg(test)only.lower_valupub(super)is harmless.bitcode.rs:written_instructionsiscfg(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.bitcode.rs/lower.rsforv_msad_u8→ Msad, and #3027 (open) changesbitcode.rs/lower.rsfor min3/max3/med3. Textual conflicts are possible inlower.rs. In particular, thev_msad_u8arm moved in this PR, and the0x171count budget (63) insad.rswould 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.
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 sadpassed (2/2:sad_instruction_countsandsad_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_lowerpassedcargo clippy -p victory-shader --all-targets -- -D warningspassed with 0 warningscargo xtask policypassed (192 files ok)Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.