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

Closed
liandraraiane wants to merge 1 commit from upstream-pr/635 into main
liandraraiane commented 2026-11-28 02:10:49 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by liandraraiane on 2026-11-28T02:10:49Z.


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

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5535693405 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/635). Originally posted by [liandraraiane](https://github.com/liandraraiane) on 2026-11-28T02:10:49Z. --- ## 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)

Pull request closed

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
trustno1-coder/VIctory-NVIDIA!635
No description provided.