Performance ideas from two other Rust game ports: native 16-bit shader math, and an LTO build profile #2384

Open
opened 2026-11-26 05:04:15 +00:00 by github-bot · 5 comments
Owner

@⁠xndbogdan on GitHub · 2026-11-25 12:25 UTC · original

I read VIctory and had my agent check OpenInfestation and OpenV2K, two recomps of mine that were recently finished, for anything in their renderers that would help shaders here. Most of it doesn't carry over: those ports compile their own GLSL to SPIR-V for Vulkan, while VIctory translates console code to DXIL. VIctory also already has what I would have suggested first: per-command GPU timing (VICTORY_GPU_TIMING), a wave size declared only where a program needs one, and non-uniform indexing only where it is needed. Two things do carry over. Nothing below was run or measured on VIctory.

1. Every f16 result is rounded in software

emit/lower.rs does 16-bit float math in f32 and narrows each result with narrow16, because the host's legacyF32ToF16 rounds toward zero on the RX 6700 XT and WARP. Its round-to-nearest path is about 44 integer operations: bit tests, shifts and selects for normal, denormal, infinite and NaN halves. Each source is also widened with legacyF16ToF32. So one console instruction such as v_pk_fma_f16 (two halves, three sources each, packed16) becomes roughly 100 host operations, where the console runs one.

#400 measured 180–320 ms of a ~800 ms world frame on the GPU, so translated ALU work is not free. Nobody has measured how much of it is f16 math.

A possible direction: where the device reports Native16BitShaderOpsSupported, emit DXIL half arithmetic (SM 6.2 native 16-bit types, with the matching shader flag), so the host GPU does the f16 operation itself and rounds as RDNA2 does. Keep the current path for:

  • programs whose MODE rounds 16-bit results toward zero (mode16 == Some(3), around the title's 16-bit interpolation);
  • devices without native 16-bit operations. The translated program would then depend on that capability, so the shader cache key would need to include it.

To check before writing it:

  • how much the hot shaders use f16 math: VICTORY_GPU_TIMING's slowest commands, and a count of f16 opcodes over the translated corpus;
  • that the host's native f16 matches the console on denormals, NaN payloads and signed zeros, using the existing f16 hardware tests.

2. The release build has no LTO and no target CPU

Both ports ship a build like this:

# Cargo.toml
[profile.ship]
inherits = "release"
lto = "fat"
codegen-units = 1
# .cargo/config.toml
[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-cpu=x86-64-v3"]

What they measured (OpenInfestation docs/PERFORMANCE.md, OpenV2K docs/PERFORMANCE.md): fat LTO took 5–10% off the CPU frame, and x86-64-v2 took 2–4% (v3 was no better than v2 for their code). Event logs and snapshots stayed byte-identical, because Rust never contracts or reassociates floating point unless asked. VIctory's [profile.release] has neither setting. The README already requires AVX2, so x86-64-v3 drops no supported CPU.

Where it could help here: shader translation at first use (draws wait on it), translate --cache, page comparisons, detiling and BCPack. It would not speed up GPU work or the driver's pipeline compiles. Much of the copy time in #400 is spent in kernel calls (write-watch), which no compiler flag shortens. It needs an A/B on the diag build before anyone trusts a number. A fat-LTO build is several times slower to compile (about 5 minutes against 1 there), so it belongs in its own profile, not in release.

**@⁠xndbogdan** on GitHub · 2026-11-25 12:25 UTC · [original](https://github.com/Marvisak/VIctory/issues/2384) I read VIctory and had my agent check [OpenInfestation](https://github.com/xndbogdan/OpenInfestation) and [OpenV2K](https://github.com/xndbogdan/OpenV2K), two recomps of mine that were recently finished, for anything in their renderers that would help shaders here. Most of it doesn't carry over: those ports compile their own GLSL to SPIR-V for Vulkan, while VIctory translates console code to DXIL. VIctory also already has what I would have suggested first: per-command GPU timing (`VICTORY_GPU_TIMING`), a wave size declared only where a program needs one, and non-uniform indexing only where it is needed. Two things do carry over. **Nothing below was run or measured on VIctory.** ### 1. Every f16 result is rounded in software `emit/lower.rs` does 16-bit float math in f32 and narrows each result with [`narrow16`](https://github.com/Marvisak/VIctory/blob/940d32f/crates/victory-shader/src/emit/lower.rs#L2128-L2209), because the host's `legacyF32ToF16` rounds toward zero on the RX 6700 XT and WARP. Its round-to-nearest path is about 44 integer operations: bit tests, shifts and selects for normal, denormal, infinite and NaN halves. Each source is also widened with `legacyF16ToF32`. So one console instruction such as `v_pk_fma_f16` (two halves, three sources each, [`packed16`](https://github.com/Marvisak/VIctory/blob/940d32f/crates/victory-shader/src/emit/lower.rs#L6660)) becomes roughly 100 host operations, where the console runs one. #400 measured 180–320 ms of a ~800 ms world frame on the GPU, so translated ALU work is not free. Nobody has measured how much of it is f16 math. A possible direction: where the device reports `Native16BitShaderOpsSupported`, emit DXIL `half` arithmetic (SM 6.2 native 16-bit types, with the matching shader flag), so the host GPU does the f16 operation itself and rounds as RDNA2 does. Keep the current path for: - programs whose MODE rounds 16-bit results toward zero (`mode16 == Some(3)`, around the title's 16-bit interpolation); - devices without native 16-bit operations. The translated program would then depend on that capability, so the shader cache key would need to include it. To check before writing it: - how much the hot shaders use f16 math: `VICTORY_GPU_TIMING`'s slowest commands, and a count of f16 opcodes over the translated corpus; - that the host's native f16 matches the console on denormals, NaN payloads and signed zeros, using the existing f16 hardware tests. ### 2. The release build has no LTO and no target CPU Both ports ship a build like this: ```toml # Cargo.toml [profile.ship] inherits = "release" lto = "fat" codegen-units = 1 ``` ```toml # .cargo/config.toml [target.x86_64-pc-windows-msvc] rustflags = ["-C", "target-cpu=x86-64-v3"] ``` What they measured ([OpenInfestation docs/PERFORMANCE.md](https://github.com/xndbogdan/OpenInfestation/blob/main/docs/PERFORMANCE.md#the-shipped-build-2026-11-01), [OpenV2K docs/PERFORMANCE.md](https://github.com/xndbogdan/OpenV2K/blob/main/docs/PERFORMANCE.md#the-shipped-build-2026-11-07)): fat LTO took 5–10% off the CPU frame, and x86-64-v2 took 2–4% (v3 was no better than v2 for their code). Event logs and snapshots stayed byte-identical, because Rust never contracts or reassociates floating point unless asked. VIctory's `[profile.release]` has neither setting. The README already requires AVX2, so x86-64-v3 drops no supported CPU. Where it could help here: shader translation at first use (draws wait on it), `translate --cache`, page comparisons, detiling and BCPack. It would not speed up GPU work or the driver's pipeline compiles. Much of the copy time in #400 is spent in kernel calls (write-watch), which no compiler flag shortens. It needs an A/B on the diag build before anyone trusts a number. A fat-LTO build is several times slower to compile (about 5 minutes against 1 there), so it belongs in its own profile, not in `release`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:04:15 +00:00
Author
Owner

@⁠Ingenioeren on GitHub · 2026-11-25 12:34 UTC · original

I tested the native f16 arithmetic idea on 940d32f using the existing D3D12 dispatch/readback helper and half-precision CPU reference. The RX 6750 XT (driver 32.0.21045.29003) and WARP on Windows 11 build 26200 both reported Native16BitShaderOpsSupported. The synthetic SM 6.6 shader used raw binary16 inputs and outputs. DXC disassembly confirmed fadd half and fmul half. CPU reference

Across 132,096 input pairs per backend, every non-NaN result matched the round-to-nearest-even reference bit for bit, including signed zeros and subnormals. NaN classification also matched. NaN bits differed between the backends in 4,380 additions and 4,386 multiplications. For example, input bits 0x7c01 and 0x0001 returned 0x7c01 on the Radeon and 0x7fff on WARP for both operations. VIctory's current software narrowing explicitly sets the quiet bit. Current narrowing

That gives concrete coverage for native add and multiply in the default rounding mode. A native path that needs bit-identical NaNs still needs an explicit policy for them. FMA, other rounding modes and performance were outside this probe.

AI-assisted. One synthetic test passed on both backends. No game files used.

**@⁠Ingenioeren** on GitHub · 2026-11-25 12:34 UTC · [original](https://github.com/Marvisak/VIctory/issues/2384#issuecomment-7162560304) I tested the native f16 arithmetic idea on `940d32f` using the existing D3D12 dispatch/readback helper and half-precision CPU reference. The RX 6750 XT (driver `32.0.21045.29003`) and WARP on Windows 11 build 26200 both reported `Native16BitShaderOpsSupported`. The synthetic SM 6.6 shader used raw binary16 inputs and outputs. DXC disassembly confirmed `fadd half` and `fmul half`. [CPU reference](https://github.com/Marvisak/VIctory/blob/940d32f69b22265690a0b5a15d6ded572ba33baf/crates/victory-shader/src/emit/semantics_tests.rs#L960-L1010) Across 132,096 input pairs per backend, every non-NaN result matched the round-to-nearest-even reference bit for bit, including signed zeros and subnormals. NaN classification also matched. NaN **bits** differed between the backends in 4,380 additions and 4,386 multiplications. For example, input bits `0x7c01` and `0x0001` returned `0x7c01` on the Radeon and `0x7fff` on WARP for both operations. VIctory's current software narrowing explicitly sets the quiet bit. [Current narrowing](https://github.com/Marvisak/VIctory/blob/940d32f69b22265690a0b5a15d6ded572ba33baf/crates/victory-shader/src/emit/lower.rs#L2191-L2209) That gives concrete coverage for native add and multiply in the default rounding mode. A native path that needs bit-identical NaNs still needs an explicit policy for them. FMA, other rounding modes and performance were outside this probe. _AI-assisted. One synthetic test passed on both backends. No game files used._
Author
Owner

@⁠xndbogdan on GitHub · 2026-11-25 12:53 UTC · original

Thanks for testing this, that's more than I had. Good to see add and multiply match bit for bit outside NaNs. One question on those: the Radeon passed 0x7c01 through unquieted. Does the console's v_add_f16 do the same in the title's shader mode? If it does, native ops might match the console's NaNs more closely than the current narrowing, and only WARP would need the software path. Fused multiply-add and a timing on a real shader seem like the next things to check.

That's all I had to add here. Good luck getting the port running better!

**@⁠xndbogdan** on GitHub · 2026-11-25 12:53 UTC · [original](https://github.com/Marvisak/VIctory/issues/2384#issuecomment-7162989775) Thanks for testing this, that's more than I had. Good to see add and multiply match bit for bit outside NaNs. One question on those: the Radeon passed `0x7c01` through unquieted. Does the console's `v_add_f16` do the same in the title's shader mode? If it does, native ops might match the console's NaNs more closely than the current narrowing, and only WARP would need the software path. Fused multiply-add and a timing on a real shader seem like the next things to check. That's all I had to add here. Good luck getting the port running better!
Author
Owner

@⁠DrDarkDK on GitHub · 2026-11-25 14:00 UTC · original

I tested native f16 FMad on an RTX 4070 Ti (driver 621.01) and WARP. For the cancellation case (0x3c01, 0x3e00, 0xbe00), both returned 0x1600. Separate half multiply and add returned 0x1800.

Two inputs distinguished the backends. All values below are binary16 bits.

Inputs (a, b, c) Exact result rounded once RTX 4070 Ti WARP f32 FMad then narrow
3c01, 3e00, 8001 3e01 3e01 3e02 3e02
3c03, 3e00, 0001 3e05 3e05 3e04 3e04

These two inputs could serve as regression cases for a native f16 path. The probe used synthetic D3D12 shaders and covered rounding only.

AI-assisted with Codex.

**@⁠DrDarkDK** on GitHub · 2026-11-25 14:00 UTC · [original](https://github.com/Marvisak/VIctory/issues/2384#issuecomment-7164626948) I tested native f16 `FMad` on an RTX 4070 Ti (driver 621.01) and WARP. For the cancellation case `(0x3c01, 0x3e00, 0xbe00)`, both returned `0x1600`. Separate half multiply and add returned `0x1800`. Two inputs distinguished the backends. All values below are binary16 bits. | Inputs `(a, b, c)` | Exact result rounded once | RTX 4070 Ti | WARP | f32 FMad then narrow | |---|---:|---:|---:|---:| | `3c01, 3e00, 8001` | `3e01` | `3e01` | `3e02` | `3e02` | | `3c03, 3e00, 0001` | `3e05` | `3e05` | `3e04` | `3e04` | These two inputs could serve as regression cases for a native f16 path. The probe used synthetic D3D12 shaders and covered rounding only. *AI-assisted with Codex.*
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 16:01 UTC · original · edited 2026-11-26 03:46 UTC

Automated triage for the maintainers.

Both suggestions still describe main (63bcca4). 16-bit float results are computed in f32 and rounded in software by Lower::narrow16 in crates/victory-shader/src/emit/lower.rs, and nothing emits native DXIL half arithmetic. [profile.release] in Cargo.toml sets only debug = "line-tables-only". There is no LTO profile, and .cargo/config.toml has no target-cpu. Both fit the project's scope.

The probes in the thread are useful input for a native-f16 path:

  • Add and multiply match outside NaN payloads.
  • Native half FMA rounds once on the RTX 4070 Ti and RTX 5080, but WARP rounds through binary32 like the current narrow16. The latest comment gives two all-normal regression vectors for this.
  • The cost of the current path is measured in the open PR #2806.

Whether to pursue either suggestion is left to the maintainers.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 16:01 UTC · [original](https://github.com/Marvisak/VIctory/issues/2384#issuecomment-7167520674) · edited 2026-11-26 03:46 UTC <!-- victory-issue-triage v1 verdict=tracked seen=2026-11-26T01:51:13Z at=2026-11-26T03:53:00Z --> Automated triage for the maintainers. Both suggestions still describe main (`63bcca4`). 16-bit float results are computed in f32 and rounded in software by `Lower::narrow16` in `crates/victory-shader/src/emit/lower.rs`, and nothing emits native DXIL `half` arithmetic. `[profile.release]` in `Cargo.toml` sets only `debug = "line-tables-only"`. There is no LTO profile, and `.cargo/config.toml` has no `target-cpu`. Both fit the project's scope. The probes in the thread are useful input for a native-f16 path: - Add and multiply match outside NaN payloads. - Native half FMA rounds once on the RTX 4070 Ti and RTX 5080, but WARP rounds through binary32 like the current `narrow16`. The latest comment gives two all-normal regression vectors for this. - The cost of the current path is measured in the open PR #2806. Whether to pursue either suggestion is left to the maintainers. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠steven-seagull27 on GitHub · 2026-11-26 01:51 UTC · original

The native half-FMA difference also occurs when every operand and the result are normal binary16 values. I used the three shaders from #2806 at 5dfeaf69 and scaled the earlier discriminating cases by 1024 (multiply both a and c by 1024, keep b fixed). Two regression vectors from D3D12 readback, in binary16 bits:

a, b, c Exact FMA rounded once RTX 5080 native half2 WARP native half2 PR's narrow16, both adapters
6401, 3e00, 8400 6601 6601 6602 6602
6403, 3e00, 0400 6605 6605 6604 6604

I swept 16,384 finite triples: a = ±(1 + m/1024) × S for every m = 0..1023, b = 1.5, c = ±k × 2^-24 × S, with independent signs, S ∈ {1, 1024}, and k ∈ {1, 4}. For each scale, rounding once and rounding through binary32 disagreed in 684 of 4,096 cases at k=1; at k=4 both references agreed for all 4,096. I cross-checked the references using exact integer arithmetic in units of 2^-24.

  • RTX 5080 native half2 matched single rounding in all 16,384 cases. WARP native half2 matched binary32-then-binary16 nearest-even in all 16,384. Both devices reported native 16-bit shader ops support.
  • The PR's software narrow16 matched the two-stage nearest-even reference on both devices. The f32tof16 variant matched binary32 nearest-even followed by binary16 truncation toward zero on both devices.

These are deliberately selected midpoint cases, so the counts do not estimate how often a game shader encounters the difference. Windows build 26300.10075, NVIDIA driver 621.40, SDK 10.0.26100.0. The shader compiled to two DXIL FMad.f16 calls for the packed operation. I used a local readback fixture and Codex assistance. No console measurement.

**@⁠steven-seagull27** on GitHub · 2026-11-26 01:51 UTC · [original](https://github.com/Marvisak/VIctory/issues/2384#issuecomment-7180205586) The native half-FMA difference also occurs when **every operand and the result are normal binary16 values**. I used the three shaders from #2806 at `5dfeaf69` and scaled the earlier discriminating cases by 1024 (multiply both `a` and `c` by 1024, keep `b` fixed). Two regression vectors from D3D12 readback, in binary16 bits: | `a, b, c` | Exact FMA rounded once | RTX 5080 native `half2` | WARP native `half2` | PR's `narrow16`, both adapters | |---|---:|---:|---:|---:| | `6401, 3e00, 8400` | `6601` | `6601` | `6602` | `6602` | | `6403, 3e00, 0400` | `6605` | `6605` | `6604` | `6604` | I swept 16,384 finite triples: `a = ±(1 + m/1024) × S` for every `m = 0..1023`, `b = 1.5`, `c = ±k × 2^-24 × S`, with independent signs, `S ∈ {1, 1024}`, and `k ∈ {1, 4}`. For each scale, rounding once and rounding through binary32 disagreed in **684 of 4,096** cases at `k=1`; at `k=4` both references agreed for all 4,096. I cross-checked the references using exact integer arithmetic in units of 2^-24. - RTX 5080 native `half2` matched single rounding in all 16,384 cases. WARP native `half2` matched binary32-then-binary16 nearest-even in all 16,384. Both devices reported native 16-bit shader ops support. - The PR's software `narrow16` matched the two-stage nearest-even reference on both devices. The `f32tof16` variant matched binary32 nearest-even followed by binary16 truncation toward zero on both devices. These are deliberately selected midpoint cases, so the counts do not estimate how often a game shader encounters the difference. Windows build 26300.10075, NVIDIA driver 621.40, SDK 10.0.26100.0. The shader compiled to two DXIL `FMad.f16` calls for the packed operation. I used a local readback fixture and Codex assistance. No console measurement.
This discussion has been locked. Commenting is limited to contributors.
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#2384
No description provided.