Shaders: f16 math in native half on cards with 16-bit operations, and its count in victory translate #2833

Closed
xndbogdan wants to merge 15 commits from upstream-pr/2833 into main
xndbogdan commented 2026-11-29 17:53:19 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by xndbogdan on 2026-11-29T17:53:19Z.


Two changes, both on 16-bit float math in translated shaders: a count of how much of it there is, and native half arithmetic for it on cards that run 16-bit operations.

1. victory translate counts the instructions on 16-bit floats

From my Marvisak/VIctory#2866. One more line after the program count:

f16          N of M instructions on 16-bit floats (P%), K packed (v_pk_*); counted in the code, not as run

An instruction counts when its name has an _f16 operand or result. It is counted once where it appears in the code, not by how often it runs. Nobody has posted this number for the title's shaders yet; it says how much of the shader code the second change can reach.

2. f16 arithmetic in native half

Ported from Marvisak/VIctory#2825 by @0xkn0wh3r3 (that PR and its account are gone; the commit credits them as co-author). Their translator was different enough that this is a port onto this one, not a cherry-pick.

Today each console f16 operation is done in f32 and converted back with the host's f32tof16. On NVIDIA that rounds toward zero, unless VICTORY_F16_NEAREST=1. Where the card reports Native16BitShaderOpsSupported, these now become DXIL half arithmetic:

  • v_add/sub/subrev/mul_f16, v_fma/fmac_f16, v_min/max/min3/max3/med3_f16
  • v_pk_add/mul/fma/fmac/min/max_f16

That happens only where the program rounds 16-bit results to nearest even: no MODE write, or round-to-nearest set. Toward zero or toward an infinity, a mode that isn't known at that point, output modifiers, conversions, v_mad/mac_f16, v_fma_mix*, and the transcendental and rounding operations keep the f32 path. VICTORY_F16_IN_F32=1 restores the old behaviour exactly.

What changes in the results:

  • Sums, differences, products: rounded once to nearest even, as the console does. Today, on NVIDIA, they are truncated, so where a result is not exact its last place can be one lower than the console's.
  • Fused multiply-adds: rounded once on cards whose 16-bit FMad is fused (RTX 4070 Ti, 5070 Ti and 5080, measured in Marvisak/VIctory#2384), as the console's v_fma_f16. The f32 path rounds them twice.
  • Minima, maxima, medians: the same as the f32 path. v_med3_f16 uses the host's own min/max, as this fork's f32 med3 does, so signed-zero behaviour does not change.
  • NaN payloads: the host's.

Cost: #2825 measured, on an RTX 5070 Ti with GPU timestamps, dependent chains of 512 instructions: v_pk_fma_f16 39x, v_fma_f16 18x, v_mul_f16 + v_add_f16 21x faster. How much of that shows in a frame depends on the share from change 1.

How it's wired:

  • victory-gpu asks the device once, next to the existing wave-lane query, and tells the translator through set_host_native16, like set_host_rdna.
  • The answer is in host_settings() and translation_fingerprint(), so a cached translation of one path is never served to the other.
  • The half type, the 16-bit data layout, the low-precision shader flags and SFI0's native 16-bit bit are set only when a program uses half. The optimiser's usage scan clears them again if a pass removes every use.

Tests

On an RTX 5080 (driver 621.24) and WARP, Windows 11 build 26300, Ryzen 7 9800X3D:

  • native_f16_arithmetic_rounds_once_to_nearest_even compares against an exact model: integers in units of 2^-48, rounded once.
    • On both adapters: v_add/sub/subrev/mul_f16, v_pk_add/mul_f16, 192 random finite operand sets each, denormals included.
    • On the RTX 5080 only, since WARP rounds its 16-bit FMad in f32 first: v_fma_f16, v_fmac_f16 and v_pk_fma_f16, the same random sets plus #2384's five single-rounding cases.
  • native_f16_min_max_med3_and_clamp_match_the_f32_path runs min, max, min3, max3, med3, pk_min and pk_max both ways over zeros of both signs, denormals, infinities, NaNs and ordinary values, and checks the clamp's +0 for NaN and negatives.
  • native_16bit_operations_are_declared_where_used_and_toward_zero_stays_in_f32: SFI0's bit is set for v_add_f16 and v_pk_fma_f16, and not for a conversion, a toward-zero program, or anything with the switch off. The toward-zero program's results stay those of the f32 path.
  • The lowered programs pass dxil.dll validation.
  • cargo test --locked -p victory-shader -p victory-gpu -p victory, rebased on main at e1251c31: victory passes, victory-shader 536 passed and 4 failed, victory-gpu 1131 passed and 2 failed. All six fail the same way on main at e1251c31:
    • victory-shader: barycentric_tests::first_half_values_render_on_hardware_and_warp, rect::tests::rectangles_are_drawn_whole, rect::tests::the_rectangle_stage_is_a_geometry_shader ("Pipeline State Validation does not match"), and bitcode::opt::tests::hw_tests::split_lds_wave32_read_after_read_barrier_coalescing_and_parity (neg_uav_load_between_reads mask=ALL pattern).
    • victory-gpu: list::thin::tests::bindings_record_no_work ("a call is in no list") and shader_cache::tests::every_switch_of_the_translator_is_in_the_key (VICTORY_ORACLE_RUNS is listed but not read).
  • cargo clippy adds no warnings.

Not done

  • No game run. I don't have the game, so I haven't seen it in play or measured a frame. Something to look at in a run: anything drawn from 16-bit math (hair, particles, post effects). Results change from truncated to rounded to nearest, and back with VICTORY_F16_IN_F32=1 for an A/B.
  • No count from the title's shaders. victory translate <game dir>\shaders.rpf would give it.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5553452081 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2833). Originally posted by [xndbogdan](https://github.com/xndbogdan) on 2026-11-29T17:53:19Z. --- Two changes, both on 16-bit float math in translated shaders: a count of how much of it there is, and native `half` arithmetic for it on cards that run 16-bit operations. ## 1. `victory translate` counts the instructions on 16-bit floats From my Marvisak/VIctory#2866. One more line after the program count: ``` f16 N of M instructions on 16-bit floats (P%), K packed (v_pk_*); counted in the code, not as run ``` An instruction counts when its name has an `_f16` operand or result. It is counted once where it appears in the code, not by how often it runs. Nobody has posted this number for the title's shaders yet; it says how much of the shader code the second change can reach. ## 2. f16 arithmetic in native `half` Ported from Marvisak/VIctory#2825 by @0xkn0wh3r3 (that PR and its account are gone; the commit credits them as co-author). Their translator was different enough that this is a port onto this one, not a cherry-pick. Today each console f16 operation is done in f32 and converted back with the host's `f32tof16`. On NVIDIA that rounds toward zero, unless `VICTORY_F16_NEAREST=1`. Where the card reports `Native16BitShaderOpsSupported`, these now become DXIL `half` arithmetic: - `v_add/sub/subrev/mul_f16`, `v_fma/fmac_f16`, `v_min/max/min3/max3/med3_f16` - `v_pk_add/mul/fma/fmac/min/max_f16` That happens only where the program rounds 16-bit results to nearest even: no `MODE` write, or round-to-nearest set. Toward zero or toward an infinity, a mode that isn't known at that point, output modifiers, conversions, `v_mad/mac_f16`, `v_fma_mix*`, and the transcendental and rounding operations keep the f32 path. **`VICTORY_F16_IN_F32=1` restores the old behaviour exactly.** **What changes in the results:** - **Sums, differences, products:** rounded once to nearest even, as the console does. Today, on NVIDIA, they are truncated, so where a result is not exact its last place can be one lower than the console's. - **Fused multiply-adds:** rounded once on cards whose 16-bit `FMad` is fused (RTX 4070 Ti, 5070 Ti and 5080, measured in Marvisak/VIctory#2384), as the console's `v_fma_f16`. The f32 path rounds them twice. - **Minima, maxima, medians:** the same as the f32 path. `v_med3_f16` uses the host's own min/max, as this fork's f32 `med3` does, so signed-zero behaviour does not change. - **NaN payloads:** the host's. **Cost:** #2825 measured, on an RTX 5070 Ti with GPU timestamps, dependent chains of 512 instructions: `v_pk_fma_f16` 39x, `v_fma_f16` 18x, `v_mul_f16` + `v_add_f16` 21x faster. How much of that shows in a frame depends on the share from change 1. **How it's wired:** - `victory-gpu` asks the device once, next to the existing wave-lane query, and tells the translator through `set_host_native16`, like `set_host_rdna`. - The answer is in `host_settings()` and `translation_fingerprint()`, so a cached translation of one path is never served to the other. - The `half` type, the 16-bit data layout, the low-precision shader flags and SFI0's native 16-bit bit are set only when a program uses `half`. The optimiser's usage scan clears them again if a pass removes every use. ## Tests On an RTX 5080 (driver 621.24) and WARP, Windows 11 build 26300, Ryzen 7 9800X3D: - `native_f16_arithmetic_rounds_once_to_nearest_even` compares against an exact model: integers in units of 2^-48, rounded once. - On both adapters: `v_add/sub/subrev/mul_f16`, `v_pk_add/mul_f16`, 192 random finite operand sets each, denormals included. - On the RTX 5080 only, since WARP rounds its 16-bit `FMad` in f32 first: `v_fma_f16`, `v_fmac_f16` and `v_pk_fma_f16`, the same random sets plus #2384's five single-rounding cases. - `native_f16_min_max_med3_and_clamp_match_the_f32_path` runs min, max, min3, max3, med3, pk_min and pk_max both ways over zeros of both signs, denormals, infinities, NaNs and ordinary values, and checks the clamp's +0 for NaN and negatives. - `native_16bit_operations_are_declared_where_used_and_toward_zero_stays_in_f32`: SFI0's bit is set for `v_add_f16` and `v_pk_fma_f16`, and not for a conversion, a toward-zero program, or anything with the switch off. The toward-zero program's results stay those of the f32 path. - The lowered programs pass `dxil.dll` validation. - `cargo test --locked -p victory-shader -p victory-gpu -p victory`, rebased on `main` at `e1251c31`: `victory` passes, `victory-shader` 536 passed and 4 failed, `victory-gpu` 1131 passed and 2 failed. All six fail the same way on `main` at `e1251c31`: - `victory-shader`: `barycentric_tests::first_half_values_render_on_hardware_and_warp`, `rect::tests::rectangles_are_drawn_whole`, `rect::tests::the_rectangle_stage_is_a_geometry_shader` ("Pipeline State Validation does not match"), and `bitcode::opt::tests::hw_tests::split_lds_wave32_read_after_read_barrier_coalescing_and_parity` (`neg_uav_load_between_reads mask=ALL pattern`). - `victory-gpu`: `list::thin::tests::bindings_record_no_work` ("a call is in no list") and `shader_cache::tests::every_switch_of_the_translator_is_in_the_key` (`VICTORY_ORACLE_RUNS` is listed but not read). - `cargo clippy` adds no warnings. ## Not done - **No game run.** I don't have the game, so I haven't seen it in play or measured a frame. Something to look at in a run: anything drawn from 16-bit math (hair, particles, post effects). Results change from truncated to rounded to nearest, and back with `VICTORY_F16_IN_F32=1` for an A/B. - **No count from the title's shaders.** `victory translate <game dir>\shaders.rpf` would give it. _AI-assisted (Claude Code)._ 🤖 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!2833
No description provided.