Shaders: inline float constants as f16 in v_fma_mix and v_pack_b32_f16 #78

Open
tedin7 wants to merge 1 commit from upstream-pr/78 into main
tedin7 commented 2026-11-24 00:16:29 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by tedin7 on 2026-11-24T00:16:29Z.


What

v_fma_mix_f32, v_fma_mixlo_f16, v_fma_mixhi_f16 (for sources with op_sel_hi set) and v_pack_b32_f16 read their f16 sources through operand. For an inline float constant (codes 240..=248: 0.5, -0.5, 1, -1, 2, -2, 4, -4, 1/(2 pi)) that gives the f32 bits, e.g. 0x3F800000 for 1.0, whose low 16 bits are 0, so the shader saw 0.0 instead of 1.0. half_src and packed_src already used f16 values for these.

This adds f16_operand (the f16 bits in both halves, so op_sel works for either half) and uses it on those paths. The constant table now lives in one inline_f16 that half_src and packed_src share (no behavior change there). op_sel, neg and abs handling is untouched: they act on the value after it is read, and were right.

Why

Mixed-precision FMAs and packs with an inline constant such as v_fma_mix_f32 v0, v1, 1.0, v2 op_sel_hi:[1,1,1] computed with a 0.0 operand.

Tested

Linux x86_64 only, in the worktree:

  • cargo test -j4 -p victory-shader: 52 passed, 0 failed
  • cargo clippy -j4 -p victory-shader --tests: no new warnings (one existing dead-code warning for constant_color in emit/mod.rs)
  • rustfmt --edition 2024 --check crates/victory-shader/src/emit/lower.rs: clean
  • New test inline_float_constants_mean_the_same_in_f16: with inline_f16 temporarily returning the low half of resource::inline (the old value) it fails (left 0, right 0x3C00); with the fix it passes.

Not verified: lowering a whole shader (there is no unit-test harness for Lower, so the test covers the constant helper, not the call sites), the Windows runtime, and any GPU.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6571636510 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/78). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T00:16:29Z. --- ## What `v_fma_mix_f32`, `v_fma_mixlo_f16`, `v_fma_mixhi_f16` (for sources with op_sel_hi set) and `v_pack_b32_f16` read their f16 sources through `operand`. For an inline float constant (codes 240..=248: 0.5, -0.5, 1, -1, 2, -2, 4, -4, 1/(2 pi)) that gives the f32 bits, e.g. 0x3F800000 for 1.0, whose low 16 bits are 0, so the shader saw 0.0 instead of 1.0. `half_src` and `packed_src` already used f16 values for these. This adds `f16_operand` (the f16 bits in both halves, so op_sel works for either half) and uses it on those paths. The constant table now lives in one `inline_f16` that `half_src` and `packed_src` share (no behavior change there). op_sel, neg and abs handling is untouched: they act on the value after it is read, and were right. ## Why Mixed-precision FMAs and packs with an inline constant such as `v_fma_mix_f32 v0, v1, 1.0, v2 op_sel_hi:[1,1,1]` computed with a 0.0 operand. ## Tested Linux x86_64 only, in the worktree: - `cargo test -j4 -p victory-shader`: 52 passed, 0 failed - `cargo clippy -j4 -p victory-shader --tests`: no new warnings (one existing dead-code warning for `constant_color` in `emit/mod.rs`) - `rustfmt --edition 2024 --check crates/victory-shader/src/emit/lower.rs`: clean - New test `inline_float_constants_mean_the_same_in_f16`: with `inline_f16` temporarily returning the low half of `resource::inline` (the old value) it fails (left 0, right 0x3C00); with the fix it passes. Not verified: lowering a whole shader (there is no unit-test harness for `Lower`, so the test covers the constant helper, not the call sites), the Windows runtime, and any GPU.
DaviTostes commented 2026-11-24 06:54:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T00:23:54Z.


Tested e6faad2 stacked on #39 on Arch Linux x86_64, rustc 1.98.1: pass.

  • cargo test --workspace: 156 passed, 0 failed, 3 ignored; merge with #39 clean.
  • The PR adds 4 #[test]s for the f16 inline-constant forms, included in that run.
  • No game data used.

AI-assisted (DeepSeek V4.1 Flash coding agent).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7118025971 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/78#issuecomment-7118025971). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T00:23:54Z. --- Tested `e6faad2` stacked on **#39** on **Arch Linux x86_64, rustc 1.98.1**: **pass**. - `cargo test --workspace`: **156 passed, 0 failed, 3 ignored**; merge with #39 clean. - The PR adds 4 `#[test]`s for the f16 inline-constant forms, included in that run. - No game data used. AI-assisted (DeepSeek V4.1 Flash coding agent).
Lyrnc commented 2026-11-24 06:56:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-24T06:06:54Z.


RDNA2 hardware measurement: the low half is right (the constant is its f16 value), but the high half of an inline float constant is 0 on the hardware, not the f16 value again. So op_sel on such an operand reads 0.

Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr) runs each of the nine inline float constants (0.5, -0.5, 1, -1, 2, -2, 4, -4, 1/(2π)) as src1, with v4 = f32 3.0 and v5 = f16 2.0 in both halves. RDNA2 desktop silicon, not the console's. No game data.

Instruction (src1 = 1.0) Result Reading
v_fma_mix_f32 v, v4, 1.0, 0 op_sel_hi:[0,1,0] 0x40400000 (3.0) low half = f16 1.0
v_fma_mix_f32 v, v4, 1.0, 0 op_sel:[0,1,0] op_sel_hi:[0,1,0] 0x00000000 (0.0) high half = 0
v_fma_mix_f32 v, v4, 1.0, 0 op_sel_hi:[0,0,0] 0x40400000 (3.0) as f32 1.0
v_fma_mixlo_f16 v, v5, 1.0, 0 op_sel_hi:[1,1,0] 0x4000 2.0 × 1.0
v_pack_b32_f16 v, 1.0, v5 0x40003c00 low half = 0x3c00
v_pack_b32_f16 v, v5, 1.0 op_sel:[0,1] 0x00004000 high half = 0
v_pk_add_f16 v, v5, 1.0 (default op_sel_hi:[1,1]) 0x40004200 low 2+1, high 2+0
v_pk_add_f16 v, v5, 1.0 op_sel_hi:[1,0] 0x42004200 both halves 2+1

The same holds for all nine constants. 1/(2π) is 0x3118 as f16, so the f16 value is used, not a truncation of the f32.

So an inline float constant used as a 16-bit operand is {0x0000, f16}: the f16 value in bits 15:0 and 0 in bits 31:16. This is why compilers clear op_sel_hi to broadcast a constant into both halves of a packed op.

For this PR: f16_operand puts the f16 bits in both halves, so op_sel / op_sel_hi = 1 on a constant reads the f16 value where the hardware reads 0. half_src already does it the hardware's way (h, high bits 0). main's packed_src has the same both-halves behaviour (// shortcut: taken to fill both halves). #637 replaces that with an f16_operand that puts the constant in the low half only, and I measured its three expected values on this hardware too. Building on #637's helper would keep the two in step.

AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7123598545 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/78#issuecomment-7123598545). Originally posted by [Lyrnc](https://github.com/Lyrnc) on 2026-11-24T06:06:54Z. --- RDNA2 hardware measurement: **the low half is right (the constant is its f16 value), but the high half of an inline float constant is 0 on the hardware, not the f16 value again.** So `op_sel` on such an operand reads 0. **Setup.** AMD Radeon iGPU of a Ryzen 7 9800X3D, **gfx1036 (RDNA2)**, AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr) runs each of the nine inline float constants (0.5, -0.5, 1, -1, 2, -2, 4, -4, 1/(2π)) as src1, with v4 = f32 3.0 and v5 = f16 2.0 in both halves. RDNA2 desktop silicon, not the console's. No game data. | Instruction (src1 = `1.0`) | Result | Reading | |---|---|---| | `v_fma_mix_f32 v, v4, 1.0, 0 op_sel_hi:[0,1,0]` | `0x40400000` (3.0) | low half = f16 1.0 | | `v_fma_mix_f32 v, v4, 1.0, 0 op_sel:[0,1,0] op_sel_hi:[0,1,0]` | `0x00000000` (0.0) | **high half = 0** | | `v_fma_mix_f32 v, v4, 1.0, 0 op_sel_hi:[0,0,0]` | `0x40400000` (3.0) | as f32 1.0 | | `v_fma_mixlo_f16 v, v5, 1.0, 0 op_sel_hi:[1,1,0]` | `0x4000` | 2.0 × 1.0 | | `v_pack_b32_f16 v, 1.0, v5` | `0x40003c00` | low half = `0x3c00` | | `v_pack_b32_f16 v, v5, 1.0 op_sel:[0,1]` | `0x00004000` | **high half = 0** | | `v_pk_add_f16 v, v5, 1.0` (default `op_sel_hi:[1,1]`) | `0x40004200` | low 2+1, **high 2+0** | | `v_pk_add_f16 v, v5, 1.0 op_sel_hi:[1,0]` | `0x42004200` | both halves 2+1 | The same holds for all nine constants. 1/(2π) is `0x3118` as f16, so the f16 value is used, not a truncation of the f32. So an inline float constant used as a 16-bit operand is `{0x0000, f16}`: the f16 value in bits 15:0 and 0 in bits 31:16. This is why compilers clear `op_sel_hi` to broadcast a constant into both halves of a packed op. For this PR: `f16_operand` puts the f16 bits in both halves, so `op_sel` / `op_sel_hi` = 1 on a constant reads the f16 value where the hardware reads 0. `half_src` already does it the hardware's way (`h`, high bits 0). `main`'s `packed_src` has the same both-halves behaviour (`// shortcut: taken to fill both halves`). #637 replaces that with an `f16_operand` that puts the constant in the low half only, and I measured its three expected values on this hardware too. Building on #637's helper would keep the two in step. AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.
Marvisak commented 2026-11-25 01:07:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:15:38Z.


Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-shader/src/emit/lower.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148321753 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/78#issuecomment-7148321753). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:15:38Z. --- <!-- victory-triage v1 head=e6faad2c9fc52f82a72308f4fef4fd4838d4e332 verdict=needs-rebase at=2026-11-25T00:13:20Z --> Automated triage for the maintainers. **Needs rebase.** This PR does not merge cleanly into current `main` (ac2525d). Conflicting files: `crates/victory-shader/src/emit/lower.rs`. The content has not been reviewed yet; it will be once the branch is rebased onto `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/78:upstream-pr/78'
git switch 'upstream-pr/78'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/78'
git switch 'upstream-pr/78'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/78'
git switch 'upstream-pr/78'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/78'
git switch 'main'
git merge --squash 'upstream-pr/78'
git switch 'main'
git merge --ff-only 'upstream-pr/78'
git switch 'main'
git merge 'upstream-pr/78'
git push origin 'main'
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
Marvisak/VIctory!78
No description provided.