Shaders: max, min and med3 as the host's own FMax and FMin on NVIDIA #635

Open
github-bot wants to merge 1 commit from github-bot/gh-635 into main AGit
Owner

@⁠liandraraiane on GitHub · 2026-11-28 02:10 UTC · original
liandraraiane:perf/nvidia-native-minmax → main · +762 −28 · 10 files · 1 commits · open

The problem

The console's v_max_f32 and v_min_f32 order -0 below +0, which DXIL's FMax and FMin do not promise. So on every host but AMD RDNA the translator adds a bitcast, integer compare and select after each of them, and spells v_med3_f32 between two constants out with NaN tests. The same goes for the min3/max3 and the 16-bit forms.

On an RTX 3060 the card's own FMax and FMin already give the console's results, so that code does nothing there but cost time.

The change

On NVIDIA (vendor 0x10DE) the MAX_MIN and MED3 forms, until now only for AMD RDNA, are written. VICTORY_NVIDIA_FLOAT=0 turns them off.

  • One exception, found by testing: where an operand is a constant -0, the spelled-out form is kept. The driver makes a saturate of min(max(x, -0), 1) and of med3(x, -0, 1), which gives +0 where the console gives -0. Module::negative_zero finds the constant given as bits, as a negated +0, moved to a register or behind a select. It does not follow joins of blocks, so a -0 arriving only through a phi is not seen (the difference would be the sign of a zero).
  • fract and the legacy multiply are unchanged: the host's results differ there (Frc gives 1.0 for a tiny negative number; a zero's sign differs).
  • AMD behaves as before, except that a constant -0 now takes the spelled-out form there too.
  • host_rdna() is renamed native_float(), since it now answers for two vendors.
  • victory translate and victory replay --retranslate take VICTORY_HOST_NVIDIA=1.

The shader cache key includes these bits and the translator's source, so shaders are translated again on the first run.

Measurements

RTX 3060 12 GB, driver 610.88, Windows 11. native_float_costs: synthetic compute kernels, 131,072 threads of 4,096 rounds, GPU timestamps, the two forms turn by turn, medians of 61 runs. Three runs agreed within 0.1 percentage points.

Kernel (share of max/min/med3) Spelled out Host's own Time on the card
3 of 6 2.643 ms 1.221 ms -53.8 %
2 of 20 2.964 ms 1.986 ms -33.0 %
1 of 10 1.753 ms 1.299 ms -25.9 %
none (control) 1.347 ms 1.348 ms +0.0 %

These are arithmetic-bound kernels, not the title. I have not run the title, so I have no frame time to report. Real shaders also wait on memory and textures, and a CPU-bound frame will not change; I expect a much smaller gain in a game.

Tests

  • nvidia_forms_give_what_the_spelled_out_ones_give (runs on NVIDIA cards, skipped elsewhere): 42 instruction patterns, about 6,000 inputs each (zeros of both signs, NaNs, infinities, denormals, pseudo-random bit patterns), the two forms compared bit for bit, NaN payloads aside. Passes. With the -0 guard removed it fails on 6 patterns.
  • a_constant_negative_zero_keeps_the_spelled_out_form: needs no particular card.
  • cargo test --workspace --release --locked: victory-shader 292 passed, 5 ignored. victory-gpu 441 passed and 2 failed (host_list_budget_starts_empty_at_each_submission, warp_batch_charges_every_execution), the same two as on main without this change (see #67).
  • The whole victory-shader suite with the NVIDIA forms forced on (a local hack, not committed): 290 passed, 2 failed, both on WARP, after the same cases had passed on the card. WARP does not order the zeros and is never given these forms.
  • cargo clippy --workspace --all-targets --all-features: no warning on a changed line.

Help wanted

Tested on Ampere only. A run on an RTX 20, 40 or 50 card would settle the others:

cargo test -p victory-shader --release --locked --lib nvidia_forms
cargo test -p victory-shader --release --locked --lib native_float_costs -- --ignored --nocapture

No game files, dumps or extracted data are included. This change and its testing were done with an AI assistant.

🤖 Generated with Claude Code

**@⁠liandraraiane** on GitHub · 2026-11-28 02:10 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/635) `liandraraiane:perf/nvidia-native-minmax` → `main` · +762 −28 · 10 files · 1 commits · open ## The problem The console's `v_max_f32` and `v_min_f32` order -0 below +0, which DXIL's FMax and FMin do not promise. So on every host but AMD RDNA the translator adds a bitcast, integer compare and select after each of them, and spells `v_med3_f32` between two constants out with NaN tests. The same goes for the min3/max3 and the 16-bit forms. On an RTX 3060 the card's own FMax and FMin already give the console's results, so that code does nothing there but cost time. ## The change On NVIDIA (vendor 0x10DE) the `MAX_MIN` and `MED3` forms, until now only for AMD RDNA, are written. `VICTORY_NVIDIA_FLOAT=0` turns them off. - **One exception, found by testing:** where an operand is a constant -0, the spelled-out form is kept. The driver makes a saturate of `min(max(x, -0), 1)` and of `med3(x, -0, 1)`, which gives +0 where the console gives -0. `Module::negative_zero` finds the constant given as bits, as a negated +0, moved to a register or behind a select. It does not follow joins of blocks, so a -0 arriving only through a phi is not seen (the difference would be the sign of a zero). - `fract` and the legacy multiply are unchanged: the host's results differ there (`Frc` gives 1.0 for a tiny negative number; a zero's sign differs). - AMD behaves as before, except that a constant -0 now takes the spelled-out form there too. - `host_rdna()` is renamed `native_float()`, since it now answers for two vendors. - `victory translate` and `victory replay --retranslate` take `VICTORY_HOST_NVIDIA=1`. The shader cache key includes these bits and the translator's source, so shaders are translated again on the first run. ## Measurements RTX 3060 12 GB, driver 610.88, Windows 11. `native_float_costs`: synthetic compute kernels, 131,072 threads of 4,096 rounds, GPU timestamps, the two forms turn by turn, medians of 61 runs. Three runs agreed within 0.1 percentage points. | Kernel (share of max/min/med3) | Spelled out | Host's own | Time on the card | |---|---|---|---| | 3 of 6 | 2.643 ms | 1.221 ms | -53.8 % | | 2 of 20 | 2.964 ms | 1.986 ms | -33.0 % | | 1 of 10 | 1.753 ms | 1.299 ms | -25.9 % | | none (control) | 1.347 ms | 1.348 ms | +0.0 % | **These are arithmetic-bound kernels, not the title.** I have not run the title, so I have no frame time to report. Real shaders also wait on memory and textures, and a CPU-bound frame will not change; I expect a much smaller gain in a game. ## Tests - `nvidia_forms_give_what_the_spelled_out_ones_give` (runs on NVIDIA cards, skipped elsewhere): 42 instruction patterns, about 6,000 inputs each (zeros of both signs, NaNs, infinities, denormals, pseudo-random bit patterns), the two forms compared bit for bit, NaN payloads aside. Passes. With the -0 guard removed it fails on 6 patterns. - `a_constant_negative_zero_keeps_the_spelled_out_form`: needs no particular card. - `cargo test --workspace --release --locked`: `victory-shader` 292 passed, 5 ignored. `victory-gpu` 441 passed and 2 failed (`host_list_budget_starts_empty_at_each_submission`, `warp_batch_charges_every_execution`), the same two as on `main` without this change (see #67). - The whole `victory-shader` suite with the NVIDIA forms forced on (a local hack, not committed): 290 passed, 2 failed, both on WARP, after the same cases had passed on the card. WARP does not order the zeros and is never given these forms. - `cargo clippy --workspace --all-targets --all-features`: no warning on a changed line. ## Help wanted Tested on Ampere only. A run on an RTX 20, 40 or 50 card would settle the others: ``` cargo test -p victory-shader --release --locked --lib nvidia_forms cargo test -p victory-shader --release --locked --lib native_float_costs -- --ignored --nocapture ``` No game files, dumps or extracted data are included. This change and its testing were done with an AI assistant. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The console's v_max_f32 and v_min_f32 order -0 below +0, so the translator
adds an integer compare and select after DXIL's FMax and FMin, and spells a
median of three out with NaN tests. NVIDIA's FMax and FMin already give the
console's results bit for bit, so there they are now written alone (the
MAX_MIN and MED3 forms until now kept for AMD RDNA).

A constant -0 keeps the spelled-out form: the NVIDIA driver makes a saturate
of min(max(x, -0), 1), which gives +0 where the console gives -0.

VICTORY_NVIDIA_FLOAT=0 turns it off. Fract and the legacy multiply stay as
they were: the host's results differ there.

Measured on an RTX 3060 (driver 610.88), native_float_costs, time on the
card of synthetic arithmetic kernels: 3 of 6 instructions max/min/med3
-53.9 %, 2 of 20 -33.0 %, 1 of 10 -25.8 %, none 0.0 %. What a frame of the
title gains has not been measured.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 02:15:25 +00:00
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/635/head:github-bot/gh-635'
git switch 'github-bot/gh-635'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-635'
git switch 'github-bot/gh-635'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-635'
git switch 'github-bot/gh-635'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-635'
git switch 'main'
git merge --squash 'github-bot/gh-635'
git switch 'main'
git merge --ff-only 'github-bot/gh-635'
git switch 'main'
git merge 'github-bot/gh-635'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.