Skip to content

Shaders: lower v_med3_f32 through ordered integer keys - #3608

Open
Ingenioeren wants to merge 2 commits into
Marvisak:mainfrom
Ingenioeren:perf/v-med3-f32-dxil
Open

Ingenioeren wants to merge 2 commits into
Marvisak:mainfrom
Ingenioeren:perf/v-med3-f32-dxil

Conversation

@Ingenioeren

Copy link
Copy Markdown
Contributor

v_med3_f32 now lowers its numeric median through signed-integer ordered keys and DXIL IMin/IMax. The minimal dynamic-input synthetic compute shader shrinks from 89 to 65 written DXIL instructions: 24 fewer (26.97%), including loads, the UAV store and terminators. This measures DXIL size; runtime throughput was not benchmarked.

Reversing the negative half of the f32 bit patterns orders numbers exactly, including -0 < +0. Denormal operands map directly to their signed-zero keys. NaNs sort above every number; any NaN selects the minimum of the numeric operands, matching the existing med3 rule. Three NaNs retain the original two-FMin chain. The transform back to float bits is exact. Existing source modifiers, output modifiers and guards surround this lowering; it adds no wave operations. The f16 path keeps its existing helper.

The DXIL specification defines the signed integer min/max and floating minNum behavior used here. Searches of open upstream PRs for v_med3_f32, IMin, IMax and med3 performance found no PR covering this optimization; #3027 covers integer-source forms.

The test-only Module::written_instructions and Lowered.instructions counter helper is byte-identical to #3525/#3564/#3592/#3600, copied from #3600's fix/perf-v-perm-b32-dxil branch and checked against all four branch versions.

Validation:

  • cargo test --locked --offline -p victory-shader v_med3_f32_instruction_count -- --nocapture: written first; failed on the original lowering with actual 89 versus expected 65.
  • cargo test --locked --offline -p victory-shader med3_f32 -- --nocapture: 3 passed, 0 failed after implementation. The new runtime test compares 54,872 input triples over 20 modifier settings on both the RX 6750 XT and WARP: zero CPU-reference mismatches and zero adapter disagreements. It covers signed zeros, positive/negative denormals, ordinary and extreme finite values, infinities, quiet/signalling NaNs in every source, abs/neg, clamp and all output multipliers, across multiple waves.
  • cargo test --locked --offline -p victory-shader: baseline 300 passed, 2 failed, 3 ignored; after 302 passed, 2 failed, 3 ignored. Both runs fail only emit::rect::tests::rectangles_get_their_fourth_corner and emit::tests::elements_sharing_a_row_keep_their_order, with the local dxil.dll error Container part 'Pipeline State Validation' does not match expected for module. No tests were weakened or disabled.
  • cargo clippy --locked --offline -p victory-shader --all-targets -- -D warnings: passed.
  • cargo fmt --check: fails on pristine upstream main and this branch with the same 925 pre-existing formatting hunks; normalized hunk contents are identical, with only line numbers/checkout roots differing. The new code was formatted; unrelated formatting changes were excluded.
  • cargo run --locked --offline -p xtask -- policy --staged: passed, 3 files OK.
  • git diff --check: passed.

Execution coverage is limited to this RX 6750 XT and WARP. Other GPUs/drivers and game behavior were not tested. All shader inputs are synthetic; no game was launched and no game files or game-derived content were used.

AI assistance: prepared with OpenAI Codex under Claude Code's direction, reviewed by Ingenioeren.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oni-swr

oni-swr commented Nov 27, 2026

Copy link
Copy Markdown

Verification of #3608: PASS, with a file-size caveat.

Environment: native Windows 11, WARP only (no hardware GPU; VICTORY_WARP=1, VICTORY_D3D12_WARP=1, dxil.dll). AI-assisted (Claude).

  • Head 29882eb is unchanged. It merges onto main 63bcca4 with no conflicts.
  • cargo test -p victory-shader --lib:
    • base: 298 passed, 4 failed, 3 ignored.
    • head: 300 passed, 4 failed, 3 ignored.
    • The same 4 emit::synthetic_tests fail on both, so they are not caused by this PR. Three say "no pipeline for a wave of 32", which WARP cannot run. gathers_at_a_level_match_the_hardware fails on a lane mismatch.
    • The two extra passes are the PR's two new tests.
  • Control: I applied only the PR's tests and test helpers to base, without the lowering.
    • v_med3_f32_instruction_count fails on base with 89 against the expected 65, which confirms the 89 -> 65 claim.
    • med3_f32_ordered_keys_match_the_reference passes on base. It is an equivalence guard, so it does not fail without the change. It passes on the head too, which confirms the new lowering matches the reference.
  • cargo clippy -p victory-shader --all-targets -- -D warnings on head: clean.
  • Diff review: no correctness defects found.
    • The key x ^ ((x>>31) & 0x7fffffff) orders -0 (-1) below +0 (0). It is its own inverse.
    • Denormals map to the signed-zero keys.
    • NaNs map to the max key 0x7fffffff, so any NaN selects the minimum of the numeric operands. That matches the existing med3 rule.
    • Three NaNs are caught by least == 0x7fffffff and fall back to the original minNum chain. Only a NaN can have that key, since inf is 0x7f800000.
  • Size caveat: lower.rs is 523,938 bytes on its own. The 524,288-byte limit (xtask/src/policy.rs) leaves 350 bytes of room.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I reproduced the file-size issue with the exact current heads 29882eb and #3609 c444dff. The direct merge gives lower.rs 524,485 bytes, and cargo xtask policy rejects it against the 524,288-byte limit.

I moved the FP32 median helper into lower/med3.rs, keeping its original body. That brings lower.rs to 521,718 bytes. The branch preserves both PR histories, including the adopted RNE/RTZ fix from #3609. Integration branch 2dda7f5

On app-local WARP 1.0.22, all three median tests passed. The matrix reported zero mismatches across 54,872 input triples for each of 20 modifier settings, and the instruction-count test still expects 65 DXIL instructions. Both exp tests passed on c444dff alone and on the integration, including the 32 RNE/RTZ readbacks with WARP forced for both harness paths. Strict shader Clippy and asset policy passed.

If taking the module move, please retain the Git author, or use Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com> if folding it into another commit. AI-assisted with Codex.

Moves Lower::med3_f32 unchanged into emit/lower/med3.rs, wired like
emit/lower/bvh.rs, so lower.rs keeps room under the 524,288-byte
source limit (523,938 -> 521,171 bytes). No behaviour change.

Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Ingenioeren

Copy link
Copy Markdown
Contributor Author

Thanks @andrewpozdnakov7-jpg for reproducing the overflow and for the module move. I took it into this PR as d06df15 (with you as co-author): Lower::med3_f32 now lives unchanged in emit/lower/med3.rs, wired like lower/bvh.rs. I left #3609's changes out.

  • lower.rs on this branch: 523,938 -> 521,171 bytes (3,117 bytes of room under the 524,288-byte limit).
  • cargo test --locked --offline -p victory-shader med3: 7 passed (this includes v_med3_f32_instruction_count = 65 and med3_f32_ordered_keys_match_the_reference).
  • Full -p victory-shader: 302 passed, 2 failed, 3 ignored. The 2 failures are rect::tests::rectangles_get_their_fourth_corner and emit::tests::elements_sharing_a_row_keep_their_order. Both fail with "Pipeline State Validation does not match" from this PC's local dxil.dll, and they fail the same way without this commit.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean. cargo run -p xtask -- policy --staged: ok.

@ColorMeBoom

Copy link
Copy Markdown

Hardware run on AMD: RX 5700 XT (Navi 10, RDNA1, driver 32.0.21045.29003, Windows 11 26200), head d06df15 on 63bcca4.

  • med3_f32_ordered_keys_match_the_reference passes. All 20 modifier combinations, at 54,872 inputs each, have zero mismatches on the card and on WARP, for example:
    v_med3_f32 v5, v1, v2, v3 div:2 clamp: 54872 inputs, model mismatches ["hardware 0", "warp 0"], hardware/warp differ on 0
    v_med3_f32 v5, -abs(v1), abs(v2), -v3 div:2 clamp: 54872 inputs, model mismatches ["hardware 0", "warp 0"], hardware/warp differ on 0
    
  • Full victory-shader --lib --features host: 304 passed, 0 failed, 3 ignored.

AI use: Claude Code (Claude Opus 5.5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants