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

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

@⁠tedin7 on GitHub · 2026-11-24 00:16 UTC · original
tedin7:vic-fma-mix-f16 → main · +49 −19 · 1 files · 1 commits · open

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.

**@⁠tedin7** on GitHub · 2026-11-24 00:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/78) `tedin7:vic-fma-mix-f16` → `main` · +49 −19 · 1 files · 1 commits · open ## 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.
These read f16 sources with the f32 bits of an inline constant, whose low half is 0 (1.0 became 0.0). Use the f16 value, as half_src and packed_src do.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:49:27 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 00:23 UTC · original

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).

**@⁠DaviTostes** on GitHub · 2026-11-24 00:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/78#issuecomment-7118025971) 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).
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-24 06:06 UTC · original

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.

**@⁠Lyrnc** on GitHub · 2026-11-24 06:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/78#issuecomment-7123598545) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:15 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 00:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/78#issuecomment-7148321753) <!-- 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 has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/lower.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/78/head:github-bot/gh-78'
git switch 'github-bot/gh-78'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-78'
git switch 'github-bot/gh-78'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-78'
git switch 'github-bot/gh-78'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-78'
git switch 'main'
git merge --squash 'github-bot/gh-78'
git switch 'main'
git merge --ff-only 'github-bot/gh-78'
git switch 'main'
git merge 'github-bot/gh-78'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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!78
No description provided.