shader(lower,cost): add exact float min/max and med3 rewrites with cache-key switches #165

Merged
Androtim merged 1 commit from upstream-pr/165 into main 2026-11-29 21:27:50 +00:00
Androtim commented 2026-11-29 20:54:20 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-11-29T20:54:20Z.


## Summary

  • Add exact bit-preserving float min/max and med3 rewrites in victory-shader (emit/cost.rs, emit/lower/minmax.rs, emit/bitcode/census.rs):
    • VICTORY_MINMAX_NONZERO (on by default): omits the two-zero sign test and select when one operand is a constant with nonzero magnitude bits.
    • VICTORY_MED3_CLAMP (on by default): lowers med3(x, lo, hi) with normal/infinite constant bounds to min(max(x, lo), hi) on every host (matching NaN-to-lo behavior, leaving zero/denormal/NaN bounds on the general form).
  • Add VICTORY_MINMAX_NONZERO and VICTORY_MED3_CLAMP to crates/victory-gpu/src/shader_cache.rs::build translation fingerprint.
  • Add emit/valu_tests/cost_checks.rs covering bit-exactness on hardware and WARP across denormals, signed zeros, infinities, and NaNs.

Test plan

  • cargo run -p xtask -- policy: passed (308 files ok)
  • cargo test -p victory-shader valu_tests: passed (hardware & WARP)
  • cargo test -p victory-shader wave64_tests: passed
  • cargo test -p victory-gpu shader_cache::: passed
  • Dual cargo clippy --workspace --all-targets (default and --features diagnostics) with -D warnings: passed
<!-- greyfinch-sync:Androtim/MiniVI:issue:5555017043 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/165). Originally posted by [Androtim](https://github.com/Androtim) on 2026-11-29T20:54:20Z. --- ## Summary - Add exact bit-preserving float `min`/`max` and `med3` rewrites in `victory-shader` (`emit/cost.rs`, `emit/lower/minmax.rs`, `emit/bitcode/census.rs`): - `VICTORY_MINMAX_NONZERO` (on by default): omits the two-zero sign test and select when one operand is a constant with nonzero magnitude bits. - `VICTORY_MED3_CLAMP` (on by default): lowers `med3(x, lo, hi)` with normal/infinite constant bounds to `min(max(x, lo), hi)` on every host (matching NaN-to-`lo` behavior, leaving zero/denormal/NaN bounds on the general form). - Add `VICTORY_MINMAX_NONZERO` and `VICTORY_MED3_CLAMP` to `crates/victory-gpu/src/shader_cache.rs::build` translation fingerprint. - Add `emit/valu_tests/cost_checks.rs` covering bit-exactness on hardware and WARP across denormals, signed zeros, infinities, and NaNs. ## Test plan - `cargo run -p xtask -- policy`: passed (308 files ok) - `cargo test -p victory-shader valu_tests`: passed (hardware & WARP) - `cargo test -p victory-shader wave64_tests`: passed - `cargo test -p victory-gpu shader_cache::`: passed - Dual `cargo clippy --workspace --all-targets` (default and `--features diagnostics`) with `-D warnings`: passed
Androtim commented 2026-11-29 21:05:35 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-11-29T21:05:35Z.


Automated triage for the maintainers (not an approval).

Verdict: needs-gpu-test · CI (check): passing

Review Summary

Logic, edge-case handling, and cache-key plumbing look solid, and CI is passing. Because this enables two new default-on DXIL shader lowering rewrites (VICTORY_MINMAX_NONZERO and VICTORY_MED3_CLAMP), it warrants a quick local real-GPU smoke test before merging.

Verified Changes

  • crates/victory-shader/src/emit/lower/minmax.rs (extracted from emit/lower.rs):
    • Lower::max_min: When Rewrite::MinMaxNonzero is enabled and either operand is a float constant with nonzero magnitude bits (k.to_bits() & 0x7FFF_FFFF != 0), skipping the two-zero sign-select sequence is exact because ((ia | ib) & 0x7FFF_FFFF) == 0 is always false regardless of the other operand. Zero constants (+0.0/-0.0) remain on the general path.
    • Lower::med3 / Lower::med3_clamp: When Rewrite::Med3Clamp is enabled and exactly two operands are normal or infinite float constants (excluding NaN, ±0, and denormals via k & 0x7FFF_FFFF > 0x7F80_0000 || k & 0x7F80_0000 == 0), ordering the bounds and emitting FMin(FMax(x, lo), hi) matches the general form across all inputs, including NaN (FMax(NaN, lo) -> lo, FMin(lo, hi) -> lo), ±0, and denormals.
  • crates/victory-shader/src/emit/cost.rs & crates/victory-gpu/src/shader_cache.rs:
    • Switches default to on unless set to "0", are picked up by crates/victory-shader/build.rs (VICTORY_SHADER_SWITCHES, verified by every_rewrite_has_a_switch_in_the_fingerprint), and are included in shader_cache::build so toggling either switch invalidates cached translations.
  • crates/victory-shader/src/emit/bitcode/census.rs & crates/victory-shader/src/emit/valu_tests/cost_checks.rs:
    • Adds a #[cfg(test)] thread-local census::counting helper and tests verifying bit-exactness across signed zeros, denormals, normals, infinities, and NaNs on both hardware D3D12 and WARP, alongside instruction-count reductions.

Minor Nits (non-blocking)

  • crates/victory-shader/src/emit/valu_tests/cost_checks.rs: tiny doc typo (if ewer is not 0) → fewer.

Next Step

Run the hardware valu_tests suite (specifically cost_checks) and a brief real-GPU game smoke test to confirm no driver-specific regressions with the shortened FMin/FMax sequences before merging.


Generated with Antigravity CLI (agy).

<!-- greyfinch-sync:Androtim/MiniVI:comment:7262596971 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/165#issuecomment-7262596971). Originally posted by [Androtim](https://github.com/Androtim) on 2026-11-29T21:05:35Z. --- <!-- minivi-triage v1 head=04b7aa298aa8aca2ceb5b8a87cf2fe09b218ceaf verdict=needs-gpu-test ci=passing at=2026-11-29T21:06:09Z --> Automated triage for the maintainers (not an approval). **Verdict:** `needs-gpu-test` · **CI (`check`):** `passing` ### Review Summary Logic, edge-case handling, and cache-key plumbing look solid, and CI is passing. Because this enables two new default-on DXIL shader lowering rewrites (`VICTORY_MINMAX_NONZERO` and `VICTORY_MED3_CLAMP`), it warrants a quick local real-GPU smoke test before merging. #### Verified Changes - **`crates/victory-shader/src/emit/lower/minmax.rs`** (extracted from `emit/lower.rs`): - `Lower::max_min`: When `Rewrite::MinMaxNonzero` is enabled and either operand is a float constant with nonzero magnitude bits (`k.to_bits() & 0x7FFF_FFFF != 0`), skipping the two-zero sign-select sequence is exact because `((ia | ib) & 0x7FFF_FFFF) == 0` is always false regardless of the other operand. Zero constants (`+0.0`/`-0.0`) remain on the general path. - `Lower::med3` / `Lower::med3_clamp`: When `Rewrite::Med3Clamp` is enabled and exactly two operands are normal or infinite float constants (excluding NaN, ±0, and denormals via `k & 0x7FFF_FFFF > 0x7F80_0000 || k & 0x7F80_0000 == 0`), ordering the bounds and emitting `FMin(FMax(x, lo), hi)` matches the general form across all inputs, including NaN (`FMax(NaN, lo) -> lo`, `FMin(lo, hi) -> lo`), ±0, and denormals. - **`crates/victory-shader/src/emit/cost.rs` & `crates/victory-gpu/src/shader_cache.rs`**: - Switches default to on unless set to `"0"`, are picked up by `crates/victory-shader/build.rs` (`VICTORY_SHADER_SWITCHES`, verified by `every_rewrite_has_a_switch_in_the_fingerprint`), and are included in `shader_cache::build` so toggling either switch invalidates cached translations. - **`crates/victory-shader/src/emit/bitcode/census.rs` & `crates/victory-shader/src/emit/valu_tests/cost_checks.rs`**: - Adds a `#[cfg(test)]` thread-local `census::counting` helper and tests verifying bit-exactness across signed zeros, denormals, normals, infinities, and NaNs on both hardware D3D12 and WARP, alongside instruction-count reductions. #### Minor Nits (non-blocking) - `crates/victory-shader/src/emit/valu_tests/cost_checks.rs`: tiny doc typo `(if ewer is not 0)` → `fewer`. #### Next Step Run the hardware `valu_tests` suite (specifically `cost_checks`) and a brief real-GPU game smoke test to confirm no driver-specific regressions with the shortened `FMin`/`FMax` sequences before merging. --- _Generated with Antigravity CLI (`agy`)._
Androtim commented 2026-11-29 21:20:19 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-11-29T21:20:19Z.


Verified PR #165 (04b7aa29) on live RTX 4060 Ti 8 GB Series S (verify-world.ps1 -AutoNudgeMenu, isolated D:\victory-data\cache-minivi-pr165, 75s warm run after initial cold shader/PSO cache population):

  • Cold cache pass (pr164-minmax): Reached 3D world cleanly (crashes=0, untranslated=0, VRAM 5656/6436 MB), populated cache-minivi-pr165 (1,103 files / 416 MB), while dropping ~1,470 draws/frame awaiting initial shader/PSO compilation (lit=0/24).
  • Warm cache pass (pr164-minmax-warm): 6/6 gates PASS (ok: true)
    • Reached real 3D world: world_windows=16, max_draws=11145
    • Zero crashes / DRED / panics: crashes=0, no_host_texture_errors=0, untranslated=0
    • FPS / frame time: median 8.95 fps (111.9 ms), preparing 54.3 ms, GPU 0.7 ms, copyback 14.0 ms
    • VRAM: max 5507 MB / budget min 6569 MB (over=0 MB, room_false=14.3%)
    • Texture streaming: uploaded 45552, final_waiting=2610, max_waiting=30810
    • Residency stalls: mapped() 6.4 ms/s (15400 calls, longest 21.0 ms)
    • Visuals: lit=19/24, median luma=110.74, near_black=0.0%, edge_energy=6.34, max_magenta=0.0%; inspected frame-5095.png (clean lighting, skin/wood/foliage detail with a couple transient dark water squares while streaming settles) and frame-5271.png (clean daylight lighting, water shading, wood grain, foliage, and clouds with zero NaN/black/magenta artifacts).
<!-- greyfinch-sync:Androtim/MiniVI:comment:7262795152 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/165#issuecomment-7262795152). Originally posted by [Androtim](https://github.com/Androtim) on 2026-11-29T21:20:19Z. --- Verified PR #165 (`04b7aa29`) on live RTX 4060 Ti 8 GB Series S (`verify-world.ps1 -AutoNudgeMenu`, isolated `D:\victory-data\cache-minivi-pr165`, 75s warm run after initial cold shader/PSO cache population): - **Cold cache pass (`pr164-minmax`):** Reached 3D world cleanly (crashes=0, untranslated=0, VRAM `5656/6436 MB`), populated `cache-minivi-pr165` (`1,103` files / `416 MB`), while dropping ~1,470 draws/frame awaiting initial shader/PSO compilation (`lit=0/24`). - **Warm cache pass (`pr164-minmax-warm`):** **6/6 gates PASS (`ok: true`)** - Reached real 3D world: `world_windows=16`, `max_draws=11145` - Zero crashes / DRED / panics: `crashes=0`, `no_host_texture_errors=0`, `untranslated=0` - FPS / frame time: median `8.95 fps` (`111.9 ms`), preparing `54.3 ms`, GPU `0.7 ms`, copyback `14.0 ms` - VRAM: max `5507 MB` / budget min `6569 MB` (`over=0 MB`, `room_false=14.3%`) - Texture streaming: uploaded `45552`, `final_waiting=2610`, `max_waiting=30810` - Residency stalls: `mapped()` `6.4 ms/s` (`15400` calls, longest `21.0 ms`) - Visuals: `lit=19/24`, median `luma=110.74`, `near_black=0.0%`, `edge_energy=6.34`, `max_magenta=0.0%`; inspected `frame-5095.png` (clean lighting, skin/wood/foliage detail with a couple transient dark water squares while streaming settles) and `frame-5271.png` (clean daylight lighting, water shading, wood grain, foliage, and clouds with zero NaN/black/magenta artifacts).
Androtim commented 2026-11-29 21:27:46 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-11-29T21:27:46Z.


Automated RTX 4060 Ti 8 GB Open-World A/B hardware verification PASSED (playtest/wave5 vs origin/main b1b6dc1, VICTORY_LEVELS_ON_DEMAND=0 play-parity):

  • Hardware cargo test (RTX 4060 Ti): 645 passed, 0 failed (victory 39/39, victory-shader emit::wave64_tests 19/19 & lower 19/19, victory-gpu 568/568).
  • 45s Open-World A/B (scripts/verify-world.ps1, DLSS Quality + open-world save):
    • FPS: median 9.4 fps vs 7.5 fps (+1.90 fps / +25%), min 8.3 fps vs 0.8 fps, preparing 53.6 ms vs 60.0 ms
    • Stability: 0 crashes / 0 DRED / 0 untranslated shaders
    • VRAM / Issue #149: max 5760 MB vs 6005 MB (-245 MB, 0 MB over budget); no room for new ones halved (9,000 vs 18,000); max_waiting dropped (36,432 vs 41,431)
    • Decoration bakes / Issue #150: 0 mismatches (other_bytes=0, showed_another_view=0, failed=0 vs 5 and 2 on baseline)
    • Residency stalls: mapped_rate 1.63 ms/s vs 6.9 ms/s (-76%, max 12.9 vs 51.9 ms)
    • Visual check: Clean open-world safehouse frames (frame-5975.png, frame-5983.png), edge_energy=4.69 vs 3.88, near_black=0.02%, magenta=0.0% (and #141 2bf6800 showed zero visual artifacts).
<!-- greyfinch-sync:Androtim/MiniVI:comment:7262894540 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/165#issuecomment-7262894540). Originally posted by [Androtim](https://github.com/Androtim) on 2026-11-29T21:27:46Z. --- Automated RTX 4060 Ti 8 GB **Open-World** A/B hardware verification **PASSED** (`playtest/wave5` vs `origin/main` `b1b6dc1`, `VICTORY_LEVELS_ON_DEMAND=0` play-parity): - **Hardware `cargo test` (RTX 4060 Ti):** **645 passed, 0 failed** (`victory` 39/39, `victory-shader` `emit::wave64_tests` 19/19 & `lower` 19/19, `victory-gpu` 568/568). - **45s Open-World A/B (`scripts/verify-world.ps1`, DLSS Quality + open-world save):** - **FPS:** median **`9.4 fps`** vs `7.5 fps` (**+1.90 fps / +25%**), min **`8.3 fps`** vs **`0.8 fps`**, preparing `53.6 ms` vs `60.0 ms` - **Stability:** **0** crashes / **0** DRED / **0** untranslated shaders - **VRAM / Issue #149:** max **`5760 MB`** vs `6005 MB` (**-245 MB**, `0 MB` over budget); `no room for new ones` halved (**`9,000`** vs `18,000`); `max_waiting` dropped (`36,432` vs `41,431`) - **Decoration bakes / Issue #150:** **`0` mismatches** (`other_bytes=0, showed_another_view=0, failed=0` vs `5` and `2` on baseline) - **Residency stalls:** `mapped_rate` **`1.63 ms/s`** vs `6.9 ms/s` (**-76%**, max `12.9` vs `51.9 ms`) - **Visual check:** Clean open-world safehouse frames (`frame-5975.png`, `frame-5983.png`), `edge_energy=4.69` vs `3.88`, `near_black=0.02%`, `magenta=0.0%` (and `#141` `2bf6800` showed zero visual artifacts).
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
Androtim/MiniVI!165
No description provided.