Skip to content

Shaders: decode 16-bit integer inline float constants as f32 bits - #1295

Open
WALID1618Y wants to merge 1 commit into
Marvisak:mainfrom
WALID1618Y:fix/half-int-inline-constants
Open

WALID1618Y wants to merge 1 commit into
Marvisak:mainfrom
WALID1618Y:fix/half-int-inline-constants

Conversation

@WALID1618Y

Copy link
Copy Markdown

In crates/victory-shader/src/emit/lower.rs, half_src unconditionally decoded GFX10 inline float constants (240..=248) via f16_operand for all non-packed 16-bit instructions. On RDNA2 (GFX10), inline float constants use FP16 bit patterns only for 16-bit floating-point operands; 16-bit integer instructions (u16/i16/b16, including v_add_nc_u16, v_cvt_f16_u16/i16, and v_cmp_*_u16/i16) interpret those encodings as 32-bit FP32 bit patterns and select the low or high 16-bit half via op_sel (the non-packed counterpart to #637 for packed_src, per LLVM's GFX10 encoding table).

Change

  • Added a float: bool parameter to Lower::half_src, mirroring Lower::packed_src from Shaders: decode GFX10 packed inline constants by operand type #637:
    • float = true (f16_operand) for FP16 sources: half_fsrc, cvt_norm16, and v_cmp_class_f16.
    • float = false (operand) for 16-bit integer sources: v_cvt_f16_u16/v_cvt_f16_i16, 16-bit integer VALU ops (v_add_nc_u16, v_sub_nc_u16, v_mul_lo_u16, shifts, min/max), and v_cmp_*_u16/v_cmp_*_i16.
  • Added synthetic GPU regression test half_int_ops_decode_inline_float_constants_as_f32_bits in crates/victory-shader/src/emit/valu_tests.rs.
Instruction Inline constant & op_sel On main (ffe310e) Expected & observed after fix
v_add_nc_u16 (+ 0) 248 (1/(2π)), op_sel:[0,0] 0x00003118 0x0000f983
v_add_nc_u16 (+ 0) 248 (1/(2π)), op_sel:[1,0] 0x00000000 0x00003e22
v_add_nc_u16 (+ 5) 242 (1.0), op_sel:[0,0] 0x00003c05 0x00000005
v_add_nc_u16 (+ 5) 242 (1.0), op_sel:[1,0] 0x00000005 0x00003f85
v_cvt_f16_u16 242 (1.0), op_sel:[0] 0x00007380 0x00000000
v_cmp_eq_u16 (== 0xf983 / == 0x3118) 248 (1/(2π)) 0 / 1 1 / 0
v_add_f16 (control, + 0) 248 (1/(2π)), op_sel:[0,0] 0x00003118 0x00003118

Verification

  • Environment: Windows 11, Rust 1.98.1, dxil.dll validation enabled (VICTORY_DXIL), NVIDIA GeForce RTX 3050 (driver 32.0.16.2124) + Microsoft WARP (10.0.26100.9818). Synthetic tests only; I did not run the game.
  • Negative control: On unpatched main (ffe310e), the new test fails on hardware with v_add_nc_u16 low half of inline 248: got 0x00003118, want 0x0000f983.
  • Checks after fix:
    • cargo test -p victory-shader --locked (with dxil.dll): 121 passed, 0 failed, 1 ignored (4 differential tests skip without the console compiler)
    • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
    • cargo xtask policy: 154 files ok

Note: open PR #926 also edits half_src (to ignore op_sel on 16-bit compares); if merged first, rebasing is a one-line signature merge.

AI-assisted; all builds, DXIL validations, and GPU dispatches were executed locally.

@geiyiliyabumo

Copy link
Copy Markdown
Contributor

Independent verification of \6cf80f2\ on Windows 11 / AMD Radeon RX 9060 XT (RDNA4, \gfx1200) / Rust 1.98.1 MSVC:

  • Merge check vs \main\ (\ fe310e): clean
  • \cargo test -p victory-shader --lib valu_tests\ (real RX 9060 XT GPU execution): 14 passed, 0 failed (including \half_int_ops_decode_inline_float_constants_as_f32_bits)
  • \cargo clippy -p victory-shader --all-targets -- -D warnings\ (default and --all-features): clean
  • \cargo xtask policy: 154 files ok

Code audit looks solid: splitting \half_src(inst, k, float)\ matches \packed_src\ so 16-bit integer ops read raw f32 bits of inline float constants \240..=248\ while 16-bit float ops preserve f16 bits (including \�_cvt_f16_u16/\�_cvt_f16_i16\ and \�_cmp_*_u16/i16).

AI-assisted; synthetic tests only, no game files.

@adamdotpy

Copy link
Copy Markdown

Reviewed at head 6cf80f2 (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). Findings, ranked:

1. The claim is right and the change is complete at every call site. On RDNA2, a non-packed 16-bit integer VOP3 op reads the selected half of the inline constant's f32 bit pattern, while 16-bit float ops (and packed VOP3P ops via packed_src) read f16 bits. The mechanics confirm it: f16_operand (lower.rs:1245) maps constants 240..=248 to f16 words in the low half (0x3118 for 1/(2π) etc.), while operand maps them through crate::resource::inline to the f32 bits (0x3E22F983 for 1/(2π)). half_src now takes a float: bool and picks between the two; I audited all 7 call sites on the head: true for the f16 operand paths (4351, 4693, 5277) and false for the integer-source paths (4559 v_cvt_f16_u16/i16 — source is an integer even though the dst is f16, correct; 4632/4633 the 16-bit int ALU arm; 5394/5395 the u16/i16 compare arm). No caller was missed.

2. Red/green proven here. The PR's valu_tests.rs against main's lower.rs fails (half_int_ops_decode_inline_float_constants_as_f32_bits: 0 passed / 1 failed — old behavior reads 0x3118 where the f32 low half 0xF983 is expected); on 6cf80f2 it passes on the GPU. So the branch changes exactly the integer-operand constant decode and leaves the float one alone.

3. Hardware evidence on this machine. cargo test -p victory-shader --lib half_int_ops runs the new test through run_cases on the GPU: 6 cases pass — low/high half of inline 248 and 242 through v_add_nc_u16 (f32 halves), the v_add_f16 control keeping f16 bits (0x3118), v_cvt_f16_u16 reading the f32 low half of 1.0 as 0, and a v_cmp_eq_u16_e64 against inline 248 matching 0xF983 but not 0x3118. Full cargo test -p victory-shader --lib: 121 passed / 0 failed / 1 ignored.

4. Checks: cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings clean; cargo build --release -p victory builds; rustfmt --edition 2024 --check on both changed files shows only the same 3 pre-existing lower.rs hunks main has (shifted +8 lines) — the PR adds no new fmt diffs, valu_tests.rs fully clean.

5. Behavior note, not a defect: this changes what previously-translating shaders compute when a 16-bit integer op consumes an inline float constant — before, they silently used the f16 bits. That is precisely the bug being fixed (the f16 and f32 halves disagree, e.g. 0x3118 vs 0xF983), and the packed-op path is untouched, so VOP3P shaders are unaffected.

Nothing found that would block this.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-shader/src/emit/lower.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants