xshader: make vop::basic pass — ISA-exact SCC, BFE, fract/log/rsq/sqrt, u32→f32 #24

Closed
github-bot wants to merge 1 commit from gh-pr/24/head into gh-pr/24/base
Owner

@⁠QuentinDanblon on GitHub · 2026-11-21 18:00 UTC · original
QuentinDanblon:fix/isa-exact-basic → main · +89 −42 · 5 files · 1 commits · closed without merging

Library / area: tombox-xshader (ALU translation)

Exports changed (exports.txt rows): none

Source of the behaviour: AMD RDNA2 ISA pseudocode and functional examples; D3D frc table; LLVM amdgcn_fract folding; LLVM PR #220911 (BFE width field)

At 226938c the existing tests/exec_vop/basic.rs suites fail: vop::basic::{salu_basic, vop1_basic, vop2_basic, vop3_basic}. Opaque mode (runtime inputs) never ran for salu/vop1/vop3, because constant mode failed first. Once constant mode passed, opaque mode found 3 more bugs. Several of these ops are hot in title shaders (mnemonics.txt: v_sqrt_f32 58k uses, v_log_f32 57k, v_rsq_f32 52k, v_fract_f32 28k).

Translator fixes

Op Was Now (ISA)
s_lshl_b64 SCC never set SCC = D ≠ 0
s_bfm_b64 high dword always 0 ((1<<S0[5:0])-1) << S1[5:0] in 64 bits
s_addk_i32 SCC never set SCC = signed overflow
s_bfe_i32, v_bfe_i32 wrapped shifts for offset+width ≥ 32 shared tb_bfe_i32v: width mod 32 (as s_bfe_u32 already did), width 0 → 0, offset+width ≥ 32 → arithmetic shift
v_fract_f32/f16 HLSL frac() (WARP returns 0.7 for −3.7) x − floor(x) clamped to 0x3f7fffff / 0x3bff; fract(−0) = +0
v_log_f32, v_rsq_f32, v_sqrt_f32 intrinsics; DXIL validation error on log(+INF) constant denormals flushed, input NaN quieted, log(±0) = −INF, rsq(±0) = ±INF, sqrt(−0) = −0; log(+INF) handled before log2
v_add/sub/subrev_f32 DXC fast-math folded x−x→0, x+(−x)→0, x+0→x (wrong for INF / −0) precise (no extra ALU in measured DXIL; only the invalid folds are lost)
v_cvt_f32_u32 WARP truncates above 2^31 exact: two 16-bit halves summed, round-to-nearest-even
v_fma_mix_* product rounded before add when a multiplicand is f32 fused through tb_fma

Cost (DXIL instructions, old intrinsic = 2): fract 8, log 11, rsq 10, sqrt 9, u32→f32 6, v_bfe_i32 13→12. The extra instructions are full-rate integer/select ops. This is the tradeoff to review, given the shader-cost notes in BLOCKERS.md. The results now match the ISA on every host GPU instead of depending on how that driver handles denormals and NaN in frc/rsq/log. Wrong INF/NaN/denormal edge cases are a plausible source of post-process artifacts (bloom), but I have not checked that against a title.

Test-model corrections (basic.rs, exec.rs)

  • Generated NaN bits are not pinned. The host generates them: AMD/WARP give 0xffc00000 (the ISA examples), NVIDIA 0x7fffffff, DXC constant folding 0x7fc00000. A GEN_NAN expectation accepts any NaN only where the ISA generates one. Propagated NaNs and every non-NaN result are still compared exactly. Mnemonics that rely on this: v_sub_f32, v_subrev_f32, v_mad_legacy_f32 (INF−INF), v_fract_f32 (INF), v_log/rsq/sqrt_f32 (negative input).
  • fract(−0): the model expected −0. The ISA pseudocode -0 + -floor(-0) gives +0, as does the D3D frc table.
  • v_ceil_f16: the input expression evaluated to a positive denormal (0x10001). It is now the intended negative denormal.
  • v_fma_mixhi_f16: the ISA writes only D[31:16], so the low half is kept.

Tests (WARP, DXC 1.9.2609)

  • --test exec vop::basic: 0/4 → 4/4
  • full --test exec: 56 pass / 4 fail → 60 pass / 0 fail (1 ignored, as before)
  • --lib 8, --test opcodes 22, --test synthetic 7: all pass
  • --test fixtures was not run: analysis/shaders is not in the repo (#13).

Not changed: s_bfe_i64/u64 width (LLVM PR suggests mod 64, untested); v_fma_mixlo/mixhi with f32 multiplicands still round twice (rare difference, documented in the code).

  • cargo test -p tombox-xshader passes
  • No game or console files and no binaries included
  • No fake success; anything not possible is left missing with a note
**@⁠QuentinDanblon** on GitHub · 2026-11-21 18:00 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/24) `QuentinDanblon:fix/isa-exact-basic` → `main` · +89 −42 · 5 files · 1 commits · closed without merging **Library / area:** tombox-xshader (ALU translation) **Exports changed (exports.txt rows):** none **Source of the behaviour:** AMD RDNA2 ISA pseudocode and functional examples; D3D `frc` table; LLVM `amdgcn_fract` folding; LLVM PR #220911 (BFE width field) At 226938c the existing `tests/exec_vop/basic.rs` suites fail: `vop::basic::{salu_basic, vop1_basic, vop2_basic, vop3_basic}`. Opaque mode (runtime inputs) never ran for salu/vop1/vop3, because constant mode failed first. Once constant mode passed, opaque mode found 3 more bugs. Several of these ops are hot in title shaders (mnemonics.txt: `v_sqrt_f32` 58k uses, `v_log_f32` 57k, `v_rsq_f32` 52k, `v_fract_f32` 28k). ### Translator fixes | Op | Was | Now (ISA) | |---|---|---| | `s_lshl_b64` | SCC never set | SCC = D ≠ 0 | | `s_bfm_b64` | high dword always 0 | `((1<<S0[5:0])-1) << S1[5:0]` in 64 bits | | `s_addk_i32` | SCC never set | SCC = signed overflow | | `s_bfe_i32`, `v_bfe_i32` | wrapped shifts for offset+width ≥ 32 | shared `tb_bfe_i32v`: width mod 32 (as `s_bfe_u32` already did), width 0 → 0, offset+width ≥ 32 → arithmetic shift | | `v_fract_f32/f16` | HLSL `frac()` (WARP returns 0.7 for −3.7) | `x − floor(x)` clamped to 0x3f7fffff / 0x3bff; fract(−0) = +0 | | `v_log_f32`, `v_rsq_f32`, `v_sqrt_f32` | intrinsics; DXIL validation error on `log(+INF)` constant | denormals flushed, input NaN quieted, log(±0) = −INF, rsq(±0) = ±INF, sqrt(−0) = −0; log(+INF) handled before `log2` | | `v_add/sub/subrev_f32` | DXC fast-math folded x−x→0, x+(−x)→0, x+0→x (wrong for INF / −0) | `precise` (no extra ALU in measured DXIL; only the invalid folds are lost) | | `v_cvt_f32_u32` | WARP truncates above 2^31 | exact: two 16-bit halves summed, round-to-nearest-even | | `v_fma_mix_*` | product rounded before add when a multiplicand is f32 | fused through `tb_fma` | **Cost** (DXIL instructions, old intrinsic = 2): fract 8, log 11, rsq 10, sqrt 9, u32→f32 6, v_bfe_i32 13→12. The extra instructions are full-rate integer/select ops. This is the tradeoff to review, given the shader-cost notes in BLOCKERS.md. The results now match the ISA on every host GPU instead of depending on how that driver handles denormals and NaN in `frc`/`rsq`/`log`. Wrong INF/NaN/denormal edge cases are a plausible source of post-process artifacts (bloom), but I have not checked that against a title. ### Test-model corrections (`basic.rs`, `exec.rs`) - **Generated NaN bits are not pinned**. The host generates them: AMD/WARP give 0xffc00000 (the ISA examples), NVIDIA 0x7fffffff, DXC constant folding 0x7fc00000. A `GEN_NAN` expectation accepts any NaN only where the ISA *generates* one. Propagated NaNs and every non-NaN result are still compared exactly. Mnemonics that rely on this: `v_sub_f32`, `v_subrev_f32`, `v_mad_legacy_f32` (INF−INF), `v_fract_f32` (INF), `v_log/rsq/sqrt_f32` (negative input). - fract(−0): the model expected −0. The ISA pseudocode `-0 + -floor(-0)` gives +0, as does the D3D frc table. - `v_ceil_f16`: the input expression evaluated to a positive denormal (0x10001). It is now the intended negative denormal. - `v_fma_mixhi_f16`: the ISA writes only D[31:16], so the low half is kept. ### Tests (WARP, DXC 1.9.2609) - `--test exec vop::basic`: 0/4 → 4/4 - full `--test exec`: 56 pass / 4 fail → 60 pass / 0 fail (1 ignored, as before) - `--lib` 8, `--test opcodes` 22, `--test synthetic` 7: all pass - `--test fixtures` was not run: `analysis/shaders` is not in the repo (#13). Not changed: `s_bfe_i64/u64` width (LLVM PR suggests mod 64, untested); `v_fma_mixlo/mixhi` with f32 multiplicands still round twice (rare difference, documented in the code). - [x] `cargo test -p tombox-xshader` passes - [x] No game or console files and no binaries included - [x] No fake success; anything not possible is left `missing` with a note
Translator (RDNA2 ISA reference, docs.amd.com Et~wpu9g~Ffl7d9q0QZ~Og):
- s_lshl_b64: set SCC = (D.u64 != 0) (ISA SOP2 op 31).
- s_bfm_b64: full 64-bit mask ((1ULL << S0[5:0]) - 1) << S1[5:0] (op 37);
  the high word was always 0.
- s_addk_i32: SCC = signed overflow (SOPK op 15 pseudocode); was never set.
- s_bfe_i32 / v_bfe_i32: width is taken modulo 32 for the 32-bit forms (LLVM
  PR #220911: width 32 is "unencodable in a BFE width field"; S_BFE_U32
  already used [20:16]&31), width 0 gives 0, and offset+width >= 32 sign-extends
  from bit 31 (was a wrapped shift). Shared tb_bfe_i32v helper.
- v_fract_f32/f16: explicit S0 + -floor(S0) clamped below 1.0 (ISA op 32,
  DX frc table, LLVM amdgcn_fract folding); WARP's frc gave 0.7 for -3.7.
- v_log_f32 / v_rsq_f32 / v_sqrt_f32: input NaNs returned quieted, denormal
  inputs flushed ("denormals are flushed"), log(+0/-0) = -INF, rsq(+-0) =
  +-INF, sqrt(-0) = -0; log(+INF) handled explicitly (DXIL validator rejects
  Log of a constant INF: "No indefinite logarithm").
- v_add/v_sub/v_subrev_f32: precise, so DXC's fast-math folds (x - x -> 0,
  x + -x -> 0, x + 0 -> x) no longer break INF/-0 results; no extra DXIL ALU.
- v_cvt_f32_u32: exact RNE via two exact half conversions + one add (D3D only
  recommends RNE for utof; WARP truncates above 2^31).
- v_fma_mix_*: fused (tb_fma) when a multiplicand is 32-bit; f16*f16 products
  are exact in f32 so the plain form already rounds once.

Tests:
- GEN_NAN wildcard: NaNs the ISA *generates* (INF - INF, fract(INF), log/rsq/
  sqrt of a negative) accept any NaN; the ISA examples give 0xffc00000 but the
  bits come from the host (AMD/WARP 0xffc00000, NVIDIA 0x7fffffff, DXC folding
  0x7fc00000). Propagated input NaNs and non-NaN results stay exact.
- fract(-0) = +0 (ISA pseudocode, DX frc table, LLVM fold); v_ceil_f16 edge
  case input fixed to the intended 0x8001; v_fma_mixhi_f16 keeps the low half
  of the destination (ISA: D.f[31:16] = ...), model expected the high half.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:07:34 +00:00
github-bot closed this pull request 2026-11-26 06:07:34 +00:00
Author
Owner

@⁠QuentinDanblon on GitHub · 2026-11-21 18:12 UTC · original

Merge note: this PR and #22 both edit the same CUBE_HELPERS line in hlsl.rs. #22 adds tb_cmp_class16 after tb_cmp_class, and this PR rewrites tb_bfe_i32. To resolve, keep this PR's version of the line and add #22's bool tb_cmp_class16(...) line after it. I merged all of my open PRs locally in one tree (#15, #16, #18, #21, #22, #24, #25) and ran the tests there: --test exec 70 passed / 0 failed (1 ignored), --lib 8, --test opcodes 22, --test synthetic 8. I can rebase whichever PR lands second.

**@⁠QuentinDanblon** on GitHub · 2026-11-21 18:12 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/24#issuecomment-7067082003) Merge note: this PR and #22 both edit the same `CUBE_HELPERS` line in `hlsl.rs`. #22 adds `tb_cmp_class16` after `tb_cmp_class`, and this PR rewrites `tb_bfe_i32`. To resolve, keep this PR's version of the line and add #22's `bool tb_cmp_class16(...)` line after it. I merged all of my open PRs locally in one tree (#15, #16, #18, #21, #22, #24, #25) and ran the tests there: `--test exec` 70 passed / 0 failed (1 ignored), `--lib` 8, `--test opcodes` 22, `--test synthetic` 8. I can rebase whichever PR lands second.
Author
Owner

@⁠QuentinDanblon on GitHub · 2026-11-21 18:30 UTC · original

Overlap note: #29 (@⁠artareias) fixes s_lshl_b64, s_bfm_b64 and s_addk_i32, a subset of this PR. This PR also covers s_bfe_i32/v_bfe_i32 (width mod 32, the open question in #29), v_fract/log/rsq/sqrt, v_cvt_f32_u32 and v_fma_mix, and makes all four vop::basic suites pass. If #29 lands first, I'll rebase and drop the duplicated hunks.

**@⁠QuentinDanblon** on GitHub · 2026-11-21 18:30 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/24#issuecomment-7067303040) Overlap note: #29 (@⁠artareias) fixes `s_lshl_b64`, `s_bfm_b64` and `s_addk_i32`, a subset of this PR. This PR also covers `s_bfe_i32`/`v_bfe_i32` (width mod 32, the open question in #29), `v_fract`/`log`/`rsq`/`sqrt`, `v_cvt_f32_u32` and `v_fma_mix`, and makes all four `vop::basic` suites pass. If #29 lands first, I'll rebase and drop the duplicated hunks.
Author
Owner

@⁠QuentinDanblon on GitHub · 2026-11-21 20:23 UTC · original

Two follow-ups.

Hardware run. I ran all my open PRs merged together with TOMBOX_EXEC_HW=1 on an RTX 5090. --test exec gives 60 pass / 6 fail, against 57 pass / 9 fail on main on the same GPU. This PR fixes salu_basic, vop2_basic and vop3_basic on hardware too.

The 6 that remain also fail on main, only on NVIDIA:

  • v_trunc/ceil/floor_f32 of a NaN return 0x7fffffff instead of the input NaN quieted;
  • v_add_nc_i16 loses the destination high half;
  • s_denorm_mode effect;
  • v_cmp(x)_nlg/ngt_f16: the NVIDIA driver fails CreateGraphicsPipelineState with E_OUTOFMEMORY;
  • v_trig_preop_f64: denormal results come back as 0;
  • image_get_lod precision.

I'm working on these in a follow-up stacked on this PR.

Open question on s_bfe_i32 width. This PR follows your test model: width = S1[22:16] mod 32, so width 32 gives 0. Two sources read it differently:

  • LLVM SIInstrInfo::splitScalar64BitBFE masks the scalar width with 0x7f0000 (7 bits).
  • AnyPS5 (GPL, behavioural reference only) clamps the 7-bit width to 32, so width 32 returns the whole field. AnyPS5 doesn't say whether that was measured on hardware.

I can't settle it without RDNA2 hardware. If you have a measured answer, I'll align s_bfe_i32, s_bfe_u32 and the test model with it.

**@⁠QuentinDanblon** on GitHub · 2026-11-21 20:23 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/24#issuecomment-7068720887) Two follow-ups. **Hardware run.** I ran all my open PRs merged together with `TOMBOX_EXEC_HW=1` on an RTX 5090. `--test exec` gives 60 pass / 6 fail, against 57 pass / 9 fail on main on the same GPU. This PR fixes `salu_basic`, `vop2_basic` and `vop3_basic` on hardware too. The 6 that remain also fail on main, only on NVIDIA: - `v_trunc/ceil/floor_f32` of a NaN return 0x7fffffff instead of the input NaN quieted; - `v_add_nc_i16` loses the destination high half; - `s_denorm_mode` effect; - `v_cmp(x)_nlg/ngt_f16`: the NVIDIA driver fails `CreateGraphicsPipelineState` with E_OUTOFMEMORY; - `v_trig_preop_f64`: denormal results come back as 0; - `image_get_lod` precision. I'm working on these in a follow-up stacked on this PR. **Open question on `s_bfe_i32` width.** This PR follows your test model: width = S1[22:16] mod 32, so width 32 gives 0. Two sources read it differently: - LLVM `SIInstrInfo::splitScalar64BitBFE` masks the scalar width with `0x7f0000` (7 bits). - AnyPS5 (GPL, behavioural reference only) clamps the 7-bit width to 32, so width 32 returns the whole field. AnyPS5 doesn't say whether that was measured on hardware. I can't settle it without RDNA2 hardware. If you have a measured answer, I'll align `s_bfe_i32`, `s_bfe_u32` and the test model with it.
Author
Owner

@⁠QuentinDanblon on GitHub · 2026-11-21 21:37 UTC · original

Superseded: after the source sync (1159c96/0fc7295), all four vop::basic suites pass on main (WARP, --test exec 70/70). The NVIDIA-only follow-ups will come in a separate PR based on main. Closing.

**@⁠QuentinDanblon** on GitHub · 2026-11-21 21:37 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/24#issuecomment-7069609704) Superseded: after the source sync (1159c96/0fc7295), all four vop::basic suites pass on main (WARP, --test exec 70/70). The NVIDIA-only follow-ups will come in a separate PR based on main. Closing.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.