Shaders: lower v_med3_f32 through ordered integer keys #3608

Open
github-bot wants to merge 2 commits from github-bot/gh-3608 into main AGit
Owner

@⁠Ingenioeren on GitHub · 2026-11-27 20:40 UTC · original
Ingenioeren:perf/v-med3-f32-dxil → main · +148 −1 · 4 files · 2 commits · open

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.

**@⁠Ingenioeren** on GitHub · 2026-11-27 20:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/3608) `Ingenioeren:perf/v-med3-f32-dxil` → `main` · +148 −1 · 4 files · 2 commits · open `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](https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/DXIL.rst) 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 20:42:18 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-27 21:15 UTC · original

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 29882eb9 is unchanged. It merges onto main 63bcca43 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.
    • Merged on main with #3597, #3602, #3587 and #3600, the file is 523,671 bytes (617 bytes of room), which reproduces 1127ba's figure.
    • This PR adds about 3.4 KB, so it would overflow that stack by about 2.7 KB.
    • It also hits a trivial textual conflict with that stack, in the tests module of lower.rs.
    • Whichever of these lands last has to move code out of lower.rs.
**@⁠oni-swr** on GitHub · 2026-11-27 21:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/3608#issuecomment-7228276750) **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 29882eb9 is unchanged. It merges onto main 63bcca43 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. - Merged on main with #3597, #3602, #3587 and #3600, the file is 523,671 bytes (617 bytes of room), which reproduces 1127ba's figure. - This PR adds about 3.4 KB, so it would overflow that stack by about 2.7 KB. - It also hits a trivial textual conflict with that stack, in the tests module of `lower.rs`. - Whichever of these lands last has to move code out of `lower.rs`.
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-27 21:43 UTC · original

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 <[email protected]> if folding it into another commit. AI-assisted with Codex.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-27 21:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/3608#issuecomment-7228697194) 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`](https://github.com/andrewpozdnakov7-jpg/VIctory/tree/review/median-and-exp-source-size) 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 <[email protected]>` 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 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Owner

@⁠Ingenioeren on GitHub · 2026-11-27 22:03 UTC · original

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.
**@⁠Ingenioeren** on GitHub · 2026-11-27 22:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/3608#issuecomment-7228987750) 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.
Author
Owner

@⁠ColorMeBoom on GitHub · 2026-11-27 23:48 UTC · original

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).

**@⁠ColorMeBoom** on GitHub · 2026-11-27 23:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/3608#issuecomment-7230428415) 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).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3608/head:github-bot/gh-3608'
git switch 'github-bot/gh-3608'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3608'
git switch 'github-bot/gh-3608'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3608'
git switch 'github-bot/gh-3608'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3608'
git switch 'main'
git merge --squash 'github-bot/gh-3608'
git switch 'main'
git merge --ff-only 'github-bot/gh-3608'
git switch 'main'
git merge 'github-bot/gh-3608'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3608
No description provided.