tombox-xshader: preserve s_swappc/s_setpc in GCN lowering, guard tb_nrm for FXC ps_5_1 #77

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

@⁠adriaespii on GitHub · 2026-11-22 00:36 UTC · original
adriaespii:contrib/xshader-gcn-fxc-fixes → main · +23 −1 · 2 files · 86 commits · closed without merging

What

  1. GCN/Durango Lowering (crates/tombox-xshader/src/gcn.rs):

    • o_rdna2 previously failed with a hard \Err\ on \s_swappc_b64\ / \s_setpc_b64. In Durango vertex shaders, \s_swappc_b64\ is used at offset 0 to branch into vertex attribute fetch shaders, while \s_setpc_b64\ acts as a pipeline continuation. Since these instructions do not encode PC-relative literal branch offsets that require offset re-calculation during lowering, they are now safely preserved with descriptive lowering notes.
    • Added unit test \swappc_lowering_preserved\ in \gcn.rs\ verifying that \s_swappc_b64\ lowers cleanly to RDNA2 and disassembles correctly before \s_endpgm.
  2. FXC Shader Model 5.1 Compatibility (\crates/tombox-xshader/src/hlsl.rs):

    • Guarded \ b_nrm\ with #if defined(__HLSL_VERSION) && __HLSL_VERSION >= 2021\ so legacy FXC compilation (e.g. \ps_5_1\ in \d3d12_xs) does not fail with \error X3000: unrecognized identifier 'uint64_t'.
    • On FXC targets, a float/floor nearest-even round is used, restoring pass status to \d3d12_xs's \lowered_pixel_shader_compiles_with_fxc_in_both_variants\ test.

Verification

  • \cargo test -p tombox-xshader --lib -- gcn\ -> 4 passed, 0 failed.
  • \cargo test -p d3d12_xs --lib\ -> 410 passed, 0 failed.
**@⁠adriaespii** on GitHub · 2026-11-22 00:36 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/77) `adriaespii:contrib/xshader-gcn-fxc-fixes` → `main` · +23 −1 · 2 files · 86 commits · closed without merging ### What 1. **GCN/Durango Lowering (crates/tombox-xshader/src/gcn.rs)**: - o_rdna2 previously failed with a hard \Err\ on \s_swappc_b64\ / \s_setpc_b64\. In Durango vertex shaders, \s_swappc_b64\ is used at offset 0 to branch into vertex attribute fetch shaders, while \s_setpc_b64\ acts as a pipeline continuation. Since these instructions do not encode PC-relative literal branch offsets that require offset re-calculation during lowering, they are now safely preserved with descriptive lowering notes. - Added unit test \swappc_lowering_preserved\ in \gcn.rs\ verifying that \s_swappc_b64\ lowers cleanly to RDNA2 and disassembles correctly before \s_endpgm\. 2. **FXC Shader Model 5.1 Compatibility (\crates/tombox-xshader/src/hlsl.rs\)**: - Guarded \ b_nrm\ with \#if defined(__HLSL_VERSION) && __HLSL_VERSION >= 2021\ so legacy FXC compilation (e.g. \ps_5_1\ in \d3d12_xs\) does not fail with \error X3000: unrecognized identifier 'uint64_t'\. - On FXC targets, a float/floor nearest-even round is used, restoring pass status to \d3d12_xs\'s \lowered_pixel_shader_compiles_with_fxc_in_both_variants\ test. ### Verification - \cargo test -p tombox-xshader --lib -- gcn\ -> 4 passed, 0 failed. - \cargo test -p d3d12_xs --lib\ -> 410 passed, 0 failed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:09:35 +00:00
github-bot closed this pull request 2026-11-26 06:09:35 +00:00
Author
Owner

@⁠QuentinDanblon on GitHub · 2026-11-22 00:57 UTC · original

I tested this against main 285a165. The two parts are in different states.

1. tb_nrm guard for FXC: already fixed on main, and the float branch is not exact

  • The branch is based on cd08e9d, 9 commits behind main, and no longer merges: conflict in crates/tombox-xshader/src/hlsl.rs.

  • Main rewrote tb_nrm in 59e5ed7 with 32-bit integer maths (tb_mulhi_u32), so the prelude has no uint64_t any more. On main 285a165 these pass:

    • cargo test -p tombox-xshader --release --lib sm5_helpers: 2 passed. One of them, common_helpers_compile_with_fxc_ps_5_1, compiles the helpers with FXC, and the file asserts that the prelude contains no uint64_t.
    • cargo test -p d3d12_xs --release --lib lowered_pixel_shader_compiles_with_fxc_in_both_variants: 1 passed.
  • The float fallback (v * (float)k, then floor and a tie test) rounds the product to f32 before rounding to an integer, so it is off by one on some inputs. CPU model of both versions, inputs clamped as the callers do:

    inputs differ from exact round-to-nearest
    2,000,000 random f32 in [0, 1], k = 65535 2,551 (first: 0x3f3f323f, exact 48945, fallback 48946)
    2,000,000 random f32 in [-1, 1], k = 32767 1,300 (first: 0x3f05300a, exact 17047, fallback 17048)
    all in-range f16 values, k = 65535 / 32767 4 / 4

    The exact rule was measured on RDNA2 hardware (#78), so the fallback would be a regression for anything compiled with FXC.

  • Side note: __HLSL_VERSION >= 2021 is also false for DXC run with -HV 2018, which supports uint64_t, so that configuration would have taken the inexact branch silently.

I would drop this part.

2. s_swappc_b64 / s_setpc_b64 kept by to_rdna2: plausible, but not shown to translate correctly

cargo test -p tombox-xshader --release --lib gcn: 4 passed on this branch (3 on main). The new test lowers a two-instruction program and checks the disassembly. The encoding side looks right: the three SOP1 opcodes have the same numbers in the gfx7 input and in the RDNA2 table (rdna2/mnem.rs: 0x1f / 0x20 / 0x21), and neither instruction carries a literal offset.

What the refusal on main protects is the step after the lowering, and the PR does not test it:

  • s_swappc_b64. hlsl.rs turns it into a comment (fetch-shader call: vertex attributes come from the input layout), and user_base() then moves the user SGPR base to 7 registers after the swapped pair when that register is s8 or above. Those rules were observed on Series shaders (the two hashes quoted in table_kind). For an Xbox One vertex shader the lowering will now succeed and the translator will apply the Series convention. Is there a dumped Durango shader that shows the same convention (which registers carry the tables after the call, which VGPRs the fetch shader fills)? Without one this turns an explicit refusal into a translation nobody has checked.
  • s_setpc_b64. On the Series path lib.rs deliberately skips the program variant that leaves through s_setpc_b64 ("a hardware-pipeline continuation form using LDS and a barrier") and takes the standalone one. hlsl.rs accepts s_setpc_b64 only when it is directly followed by s_endpgm, as the return of a vertex shader chained in front of a geometry shader. With this change a Durango program of the continuation form that ends s_setpc_b64; s_endpgm would be lowered and translated as if it simply ended, while the stage it continued into is not translated. Which Durango shaders hit this arm, and what does the continuation do in them?
  • The description says s_swappc_b64 branches into vertex attribute fetch shaders and s_setpc_b64 is a pipeline continuation on Durango. What is the source for that? If it comes from dumped shaders, the existing dumped_durango_shaders test is the natural place to show it: how many dumped programs were refused by this error before, and how many now lower, translate and compile.

Suggestion: rebase on main, keep only the gcn.rs change, and add evidence from at least one real Durango vertex shader (a translation that compiles, ideally an exec test with an input layout), with the shader hash in the PR. No game files in the tree, as usual.

**@⁠QuentinDanblon** on GitHub · 2026-11-22 00:57 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/77#issuecomment-7071894160) I tested this against main 285a165. The two parts are in different states. ### 1. `tb_nrm` guard for FXC: already fixed on main, and the float branch is not exact - The branch is based on cd08e9d, 9 commits behind main, and no longer merges: conflict in `crates/tombox-xshader/src/hlsl.rs`. - Main rewrote `tb_nrm` in 59e5ed7 with 32-bit integer maths (`tb_mulhi_u32`), so the prelude has no `uint64_t` any more. On main 285a165 these pass: - `cargo test -p tombox-xshader --release --lib sm5_helpers`: 2 passed. One of them, `common_helpers_compile_with_fxc_ps_5_1`, compiles the helpers with FXC, and the file asserts that the prelude contains no `uint64_t`. - `cargo test -p d3d12_xs --release --lib lowered_pixel_shader_compiles_with_fxc_in_both_variants`: 1 passed. - The float fallback (`v * (float)k`, then floor and a tie test) rounds the product to f32 before rounding to an integer, so it is off by one on some inputs. CPU model of both versions, inputs clamped as the callers do: | inputs | differ from exact round-to-nearest | |---|---| | 2,000,000 random f32 in [0, 1], k = 65535 | 2,551 (first: `0x3f3f323f`, exact 48945, fallback 48946) | | 2,000,000 random f32 in [-1, 1], k = 32767 | 1,300 (first: `0x3f05300a`, exact 17047, fallback 17048) | | all in-range f16 values, k = 65535 / 32767 | 4 / 4 | The exact rule was measured on RDNA2 hardware (#78), so the fallback would be a regression for anything compiled with FXC. - Side note: `__HLSL_VERSION >= 2021` is also false for DXC run with `-HV 2018`, which supports `uint64_t`, so that configuration would have taken the inexact branch silently. I would drop this part. ### 2. `s_swappc_b64` / `s_setpc_b64` kept by `to_rdna2`: plausible, but not shown to translate correctly `cargo test -p tombox-xshader --release --lib gcn`: 4 passed on this branch (3 on main). The new test lowers a two-instruction program and checks the disassembly. The encoding side looks right: the three SOP1 opcodes have the same numbers in the gfx7 input and in the RDNA2 table (`rdna2/mnem.rs`: 0x1f / 0x20 / 0x21), and neither instruction carries a literal offset. What the refusal on main protects is the step after the lowering, and the PR does not test it: - **`s_swappc_b64`.** `hlsl.rs` turns it into a comment (`fetch-shader call: vertex attributes come from the input layout`), and `user_base()` then moves the user SGPR base to 7 registers after the swapped pair when that register is s8 or above. Those rules were observed on Series shaders (the two hashes quoted in `table_kind`). For an Xbox One vertex shader the lowering will now succeed and the translator will apply the Series convention. Is there a dumped Durango shader that shows the same convention (which registers carry the tables after the call, which VGPRs the fetch shader fills)? Without one this turns an explicit refusal into a translation nobody has checked. - **`s_setpc_b64`.** On the Series path `lib.rs` deliberately skips the program variant that leaves through `s_setpc_b64` ("a hardware-pipeline continuation form using LDS and a barrier") and takes the standalone one. `hlsl.rs` accepts `s_setpc_b64` only when it is directly followed by `s_endpgm`, as the return of a vertex shader chained in front of a geometry shader. With this change a Durango program of the continuation form that ends `s_setpc_b64; s_endpgm` would be lowered and translated as if it simply ended, while the stage it continued into is not translated. Which Durango shaders hit this arm, and what does the continuation do in them? - The description says `s_swappc_b64` branches into vertex attribute fetch shaders and `s_setpc_b64` is a pipeline continuation on Durango. What is the source for that? If it comes from dumped shaders, the existing `dumped_durango_shaders` test is the natural place to show it: how many dumped programs were refused by this error before, and how many now lower, translate and compile. Suggestion: rebase on main, keep only the `gcn.rs` change, and add evidence from at least one real Durango vertex shader (a translation that compiles, ideally an exec test with an input layout), with the shader hash in the PR. No game files in the tree, as usual.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-22 13:12 UTC · original

Main's history was rewritten, which auto-closed this PR (sorry for the noise). Closing: main already handles s_swappc/s_setpc more strictly, and the f32 tb_nrm fallback double-rounds.

**@⁠omonkeyz** on GitHub · 2026-11-22 13:12 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/77#issuecomment-7079649615) Main's history was rewritten, which auto-closed this PR (sorry for the noise). Closing: main already handles s_swappc/s_setpc more strictly, and the f32 tb_nrm fallback double-rounds.

Pull request closed

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