Skip to content

GPU recomp: fix PS interp ints, wave op EXEC zeroing, ALU edge cases, and BVH det underflow - #107

Merged
ivall merged 1 commit into
xcl-dev:mainfrom
raylitebull:fix/recomp-alu-interp-and-wave-ops
Nov 23, 2026
Merged

ivall merged 1 commit into
xcl-dev:mainfrom
raylitebull:fix/recomp-alu-interp-and-wave-ops

Conversation

@raylitebull

Copy link
Copy Markdown
Member

Summary

Fixes 8 RDNA2 recompiler & BVH correctness bugs in crates/xcl/src/gpu/recomp/emit.rs and crates/xcl/src/gpu/bvh.rs:

  1. v_interp_mov_f32 / v_interp_p2_f32 corrupting integer PS inputs (emit.rs):

    • Changed asuint((float)xi.a{slot}[{chan}]) to asuint(xi.a{slot}[{chan}]) so nointerpolation uint / int PS inputs preserve their exact bit patterns instead of undergoing integer-to-float conversion before asuint.
  2. ds_swizzle_b32 / ds_bpermute_b32 not zeroing inactive source lanes (emit.rs):

    • Masked cross-lane results with select(lanebit_at(exec_lo, eh, src_lane), t, 0u) after lane exchange so inactive source lanes (EXEC[src] == 0) return 0 per RDNA2 ISA across native wave32, native wave64, and split_wave64.
  3. v_ldexp_f32 overflow/underflow/NaN on large exponents (emit.rs):

    • Replaced HLSL ldexp(x, (float)(int)b) (x * exp2(b), which fails for |b| >= 127, 0 * Inf, and Inf * 0) with a bit-exact IEEE-754 ldexp_f(float x, int n) helper handling |n| >= 127, ±0, ±Inf, NaN, and subnormals.
  4. 32-bit literal (code == 255) placement in f64 instructions (emit.rs):

    • Threaded f64: bool through code64/read64 so 32-bit literals populate the upper 32 bits ({0u, lit}) for *_f64 ops while preserving {lit, 0u} for 64-bit integer/bitmask ops.
  5. v_cvt_pknorm_i16_f32 NaN & fmt_encode SNORM NaN / 11-11-10 unsigned float sign loss (emit.rs):

    • Mapped isnan(f) to 0.0f before clamping in cvt_norm_i16_f32 and SNORM fmt_encode (instead of clamp(NaN, -1, 1) yielding -1.0f / -32767).
    • Clamped f <= 0.0f to 0u and preserved NaN encoding (0x7E0u/0x3F0u) in 11/10-bit unsigned float fmt_encode (previously negative floats had their sign bit stripped and became positive).
  6. Signed bitfield extract (s_bfe_i64, s_bfe_i32, v_bfe_i32) logical shift (emit.rs):

    • Switched to arithmetic right shift (((int64_t)v) >> o / ((int)v) >> o) before sign extension so extractions crossing the MSB (o + w > 32/64 or w >= 64) sign-extend properly.
  7. v_cmp_class_f16 destroying f16 sNaN (emit.rs):

    • Applied abs (& 0x7FFFu) and neg (^ 0x8000u) bitwise directly on the 16-bit half before fclass16, avoiding the f16tof32/f32tof16 round-trip that quieted sNaN (0x7C01) to qNaN (0x7E00).
  8. BVH triangle intersection det * det underflow/overflow (bvh.rs, emit.rs):

    • Replaced (i + j) * det <= det * det with sign-based barycentric comparisons in both Rust bvh::triangle_hit and HLSL bvh_intersect, preventing false hits on small triangles (|det| < 1.08e-19) and overflow on large triangles (|det| > 1.84e19).

Testing

  • Added 8 new unit & live GPU kernel tests (all compiled with DXC and executed natively on GPU, 0 skipped):
    • ps_interp_preserves_integer_and_float_inputs
    • gpu_ds_swizzle_and_bpermute_zero_inactive_lanes
    • gpu_ldexp_f32
    • gpu_f64_literal_high_word
    • gpu_pknorm_i16_f32_nan_and_fmt_encode_snorm_ufloat
    • gpu_signed_bfe_arithmetic_shift
    • gpu_cmp_class_f16_preserves_snan
    • triangle_hit_small_and_large_det_avoids_underflow_and_overflow
  • GPU / Driver: AMD Radeon RX 9070 XT, driver 32.0.32015.2008 (GPU wave/recompiler tests verified on AMD only).
  • Verified after rebasing onto origin/main (c85cd6f): cargo build --workspace and cargo test --workspace -- --test-threads=4 pass with 0 failures and 0 new warnings.

…, and BVH det underflow

- v_interp_mov_f32 / v_interp_p2_f32: use asuint(xi.a{slot}[{chan}]) directly instead of casting through float, preserving uint/int PS inputs
- ds_swizzle_b32 / ds_bpermute_b32: zero results when the source lane is inactive in EXEC (native wave32/64 and split_wave64)
- v_ldexp_f32: replace HLSL ldexp() with bit-exact ldexp_f() handling |n| >= 127, +-0, +-inf, NaN, and subnormals
- f64 32-bit literal (code 255): place literal in high 32 bits for f64 ops while keeping low 32 bits for integer/bitmask ops
- v_cvt_pknorm_i16_f32 & fmt_encode: map NaN to 0 for SNORM conversions, clamp <= 0 to 0 and preserve NaN for 11/10-bit unsigned float formats
- s_bfe_i64 / s_bfe_i32 / v_bfe_i32: arithmetic-shift before sign extension so o + w > width and w >= 64 preserve the sign bit
- v_cmp_class_f16: apply abs/neg bitwise on the 16-bit half so sNaN is not quieted by an f32 round-trip
- bvh::triangle_hit & HLSL bvh_intersect: replace det * det comparison with sign-based barycentric test to avoid underflow/overflow
@ivall
ivall merged commit c813f08 into xcl-dev:main Nov 23, 2026
ivall added a commit that referenced this pull request Nov 23, 2026
…s a gamepad), "What is missing" removed; HANDOFF: PRs #107-#109
@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Merged. On the retail shader pack it still translates 26 698 / 26 698 programs in full, and DXC on every 150th record (179 variants) gives 0 errors. Checked on Windows 11 with an RX 9070: cargo test --workspace green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with XCL_SPLIT_WAVE64=1; Unicorn Overlord runs with #107–#109 merged. Thanks!

ivall added a commit that referenced this pull request Nov 23, 2026
…gain (#107 regression)

Bisected on XDefiant (warm DXIL cache): 668f550 draws the title screen's text, c813f08 (#107
alone) does not. #107 made v_interp_mov_f32 / v_interp_p2_f32 of an int / uint input a bit copy
(what the ISA does). XDefiant's UI pixel shader reads its RenderType (an int input) that way and
compares it with 1 / 2: with the bits the text takes those branches (image_sample_d of the
font / four image_sample_lz taps at offsets from ds_swizzle derivatives) and draws nothing; with
the converted value it takes the default branch and draws the text (red – whether that is the
console's colour is not known). Those branches are the open bug; tried and not it: a
helper-lane-aware ballot in pixel shaders, not zeroing ds_swizzle sources. #107's test checks the
conversion now. The survey takes XCL_DXC_ARGS (comparing DXC optimisation levels).

HANDOFF: the regression, the RenderType finding, DXC compile-time measurements; the cold-cache
note corrected (the text missing for good was this regression).
@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Heads-up: one part of this PR is reverted in 58309b9 – it broke XDefiant's title-screen text (bisected on an RX 9070 with a warm DXIL cache: 668f550 draws it, c813f08 = this merge alone does not).

The part: v_interp_mov_f32 / v_interp_p2_f32 of int / uint PS inputs as a bit copy (asuint(xi.aN[c])). That is what the ISA does, but XDefiant's UI pixel shader reads its RenderType (an int4 input) this way and compares it with 1 / 2 (v_cmpx_eq_u32). With the bits, the text takes those branches – RenderType 1 samples the font with image_sample_d, 2 takes four image_sample_lz taps at offsets built from ds_swizzle derivatives – and draws nothing; with the old converted value (never 1 or 2) it takes the default branch and the text is drawn. So the bit copy is probably right and something in those two branches is wrong (gradients / derivatives); I tried a helper-lane-aware ballot for pixel shaders and leaving ds_swizzle sources unzeroed – neither brought the text back. Notes in HANDOFF ("XDefiant's UI text: the #107 regression"). If you want to dig in, the shader is the UI PS with inputs IO2_UI_UserBig0, IO4_UI_UserSmall0, IO6_UI_RenderType0 (int4), IO7_Gfx_Color0.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants