tombox-xshader: seed merged_wave_info, vertex and instance index for merged vertex shaders #278

Closed
github-bot wants to merge 2 commits from gh-pr/278/head into gh-pr/278/base
Owner

@⁠GDAgent on GitHub · 2026-11-23 10:56 UTC · original
GDAgent:fix/xshader-merged-vs-wave-info → main · +104 −5 · 2 files · 2 commits · closed without merging

Library / area: tombox-xshader (hlsl.rs vertex-shader entry setup, tests/exec.rs)

Exports changed (exports.txt rows): none

Source of the behaviour: RDNA2 ISA and console shader disassembly. Most vertex shaders of an RDNA2 Series title are compiled as the ES half of a merged ES/GS program. They start with

s_bfe_i32     s32, s3, 0x80000   ; ES vertex count of the wave (merged_wave_info[7:0])
s_bfm_b64     exec, s32, 0
s_cmp_eq_u32  s32, 64
s_cselect_b64 exec, -1, exec

and take their inputs in the merged layout: the vertex index in v5, the instance index in v8, and the fetched attributes from v9 on. In The Matrix Awakens (Xbox Series X build), a sample merged VS reads exactly v5, v8, v9..v11 (ATTRIBUTE0.xyz) and v13 (ATTRIBUTE13.x) before writing them. That matches this layout: five GS VGPRs, then ES v5 vertex id, v6/v7, v8 instance id, then attribute groups of four.

The translation never set s3, so exec became 0 at the second instruction and every output write of these shaders was skipped. It also loaded the vertex and instance index into v0/v3 (or, for vertex shaders that hand their outputs to a geometry shader through LDS, not at all), and the attributes into v4 onwards.

Changes:

  • A vertex shader whose first 16 instructions read s3 with s_bfe_u32/s_bfe_i32, and which reads s3 before writing it, is treated as a merged ES:
    • s3 starts as 0x40 (a full wave, so exec covers the vertex);
    • SV_VertexID goes to v5 and SV_InstanceID to v8;
    • attribute groups start at v9 instead of v4.
  • Every other vertex shader is unchanged. The GS side already seeds s3 (0x100), and #200 uses the same s3/v5 convention for mesh shaders; this PR does not touch either.

Effect on The Matrix Awakens' shader archive (35382 translated shaders, HLSL compared with main):

  • 10233 of 19598 vertex shaders are detected as merged ES and change. Every other shader is byte-identical to main.
  • Merged VS that read a VGPR below v32 that nothing initialises: 4263 before, 1475 after. 929 of the remaining ones read only v0, as the LDS address operand of their output writes, which the translation ignores.
  • A random sample of 60 of the changed shaders compiles with DXC at vs_6_0.

Tests (tests/exec.rs):

  • merged_vertex_shader_prologue_keeps_the_vertex_active: the input-layout draw of vertex_fetch_reads_sparse_attributes_from_a_vertex_buffer with the merged prologue in front and the attributes read from v9. On main nothing is drawn (the render target keeps its clear value); now the expected pixel is written (WARP).
  • merged_vertex_shader_reads_vertex_and_instance_index_from_v5_and_v8: s3, v5, v8 and v9 are seeded, v0/v3 are not, and DXC compiles the result.
  • On main de1e1da: cargo test -p tombox-xshader --no-fail-fast 346 passed, 0 failed (WARP); cargo test -p d3d12_xs --lib -- --test-threads=1 (with TOMBOX_REMOVED_EXIT_MS=off) 659 passed, 4 failed, 71 ignored. The same 4 fail on main here (alias::tests::msaa_depth_with_a_larger_pc_layout_never_overlaps_the_next_title_placement, placed_offset::tests::uneven_views_get_exact_helpers_and_msaa_shares_the_alias_heap, title_shadow::tests::buffer_then_texture_migrates_to_one_local_heap_and_keeps_bytes, xs_exports::console_fixed_tests::fixed_result_dxgix_exports_match_every_console_d3d12_xs).
  • Native Windows 11; not run under Proton.

Not verified in a title: no title run with this change yet. The Matrix runs so far stop before the city geometry draws. The v6/v7 inputs of the merged layout are not seeded: no shader in the archive was seen reading them before writing them.

  • 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
**@⁠GDAgent** on GitHub · 2026-11-23 10:56 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/278) `GDAgent:fix/xshader-merged-vs-wave-info` → `main` · +104 −5 · 2 files · 2 commits · closed without merging **Library / area:** `tombox-xshader` (`hlsl.rs` vertex-shader entry setup, `tests/exec.rs`) **Exports changed (exports.txt rows):** none **Source of the behaviour:** RDNA2 ISA and console shader disassembly. Most vertex shaders of an RDNA2 Series title are compiled as the ES half of a merged ES/GS program. They start with ``` s_bfe_i32 s32, s3, 0x80000 ; ES vertex count of the wave (merged_wave_info[7:0]) s_bfm_b64 exec, s32, 0 s_cmp_eq_u32 s32, 64 s_cselect_b64 exec, -1, exec ``` and take their inputs in the merged layout: the vertex index in `v5`, the instance index in `v8`, and the fetched attributes from `v9` on. In The Matrix Awakens (Xbox Series X build), a sample merged VS reads exactly `v5`, `v8`, `v9..v11` (`ATTRIBUTE0.xyz`) and `v13` (`ATTRIBUTE13.x`) before writing them. That matches this layout: five GS VGPRs, then ES `v5` vertex id, `v6`/`v7`, `v8` instance id, then attribute groups of four. The translation never set `s3`, so `exec` became 0 at the second instruction and every output write of these shaders was skipped. It also loaded the vertex and instance index into `v0`/`v3` (or, for vertex shaders that hand their outputs to a geometry shader through LDS, not at all), and the attributes into `v4` onwards. Changes: - A vertex shader whose first 16 instructions read `s3` with `s_bfe_u32`/`s_bfe_i32`, and which reads `s3` before writing it, is treated as a merged ES: - `s3` starts as `0x40` (a full wave, so `exec` covers the vertex); - `SV_VertexID` goes to `v5` and `SV_InstanceID` to `v8`; - attribute groups start at `v9` instead of `v4`. - Every other vertex shader is unchanged. The GS side already seeds `s3` (`0x100`), and #200 uses the same `s3`/`v5` convention for mesh shaders; this PR does not touch either. Effect on The Matrix Awakens' shader archive (35382 translated shaders, HLSL compared with `main`): - 10233 of 19598 vertex shaders are detected as merged ES and change. Every other shader is byte-identical to `main`. - Merged VS that read a VGPR below `v32` that nothing initialises: 4263 before, 1475 after. 929 of the remaining ones read only `v0`, as the LDS address operand of their output writes, which the translation ignores. - A random sample of 60 of the changed shaders compiles with DXC at `vs_6_0`. Tests (`tests/exec.rs`): - `merged_vertex_shader_prologue_keeps_the_vertex_active`: the input-layout draw of `vertex_fetch_reads_sparse_attributes_from_a_vertex_buffer` with the merged prologue in front and the attributes read from `v9`. On `main` nothing is drawn (the render target keeps its clear value); now the expected pixel is written (WARP). - `merged_vertex_shader_reads_vertex_and_instance_index_from_v5_and_v8`: `s3`, `v5`, `v8` and `v9` are seeded, `v0`/`v3` are not, and DXC compiles the result. - On `main` `de1e1da`: `cargo test -p tombox-xshader --no-fail-fast` 346 passed, 0 failed (WARP); `cargo test -p d3d12_xs --lib -- --test-threads=1` (with `TOMBOX_REMOVED_EXIT_MS=off`) 659 passed, 4 failed, 71 ignored. The same 4 fail on `main` here (`alias::tests::msaa_depth_with_a_larger_pc_layout_never_overlaps_the_next_title_placement`, `placed_offset::tests::uneven_views_get_exact_helpers_and_msaa_shares_the_alias_heap`, `title_shadow::tests::buffer_then_texture_migrates_to_one_local_heap_and_keeps_bytes`, `xs_exports::console_fixed_tests::fixed_result_dxgix_exports_match_every_console_d3d12_xs`). - Native Windows 11; not run under Proton. **Not verified in a title:** no title run with this change yet. The Matrix runs so far stop before the city geometry draws. The `v6`/`v7` inputs of the merged layout are not seeded: no shader in the archive was seen reading them before writing them. - [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
Most vertex shaders of an RDNA2 Series title are compiled as the ES half
of a merged ES/GS program. They start with

    s_bfe_i32  s32, s3, 0x80000     ; ES vertex count of the wave
    s_bfm_b64  exec, s32, 0
    s_cmp_eq_u32 s32, 64
    s_cselect_b64 exec, -1, exec

and take their inputs in the merged layout: the vertex index in v5, the
instance index in v8 and the fetched attributes from v9 on. The
translation never set s3, so exec became 0 at the second instruction and
every output write of the shader was skipped; it also loaded the vertex
and instance index into v0/v3 (or, for vertex shaders that hand their
outputs to a geometry shader through LDS, not at all) and the attributes
into v4 onwards.

A vertex shader whose first 16 instructions read s3 with s_bfe_u32 or
s_bfe_i32, and which reads s3 before writing it, is now treated as a
merged ES: s3 starts as 0x40 (a full wave, so exec covers the vertex),
SV_VertexID goes to v5, SV_InstanceID to v8, and the attribute groups
start at v9. Other vertex shaders are unchanged.

In The Matrix Awakens' shader archive (Xbox Series X build) this covers
10233 of 19598 vertex shaders; their HLSL changes, every other shader is
byte-identical to main. Before the change, 4263 of them read a VGPR
below v32 that nothing initialised; after it 1475 do, 929 of those only
through the LDS address operand of the output writes, which the
translation ignores. A random sample of 60 of the changed shaders
compiles with DXC at vs_6_0.

Tests (tests/exec.rs):
- merged_vertex_shader_prologue_keeps_the_vertex_active: the input-layout
  draw of vertex_fetch_reads_sparse_attributes_from_a_vertex_buffer with
  the merged prologue in front and the attributes read from v9; on main
  nothing is drawn (the render target keeps its clear value), now the
  expected pixel is written (WARP);
- merged_vertex_shader_reads_vertex_and_instance_index_from_v5_and_v8:
  s3, v5, v8 and v9 are seeded, v0/v3 are not, and DXC compiles it.

Not verified in a title.
The merged-ES prologue builds exec from s3 with s_bfm and a wave-size compare. s3 is now 0x20 for a wave32 program and 0x40 for a wave64 program, taken from the wave size the translator already derives for the program, so the vertex stays active in both modes. The merged vertex shader test now covers both wave sizes.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:17:01 +00:00
github-bot closed this pull request 2026-11-26 06:17:01 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-23 13:20 UTC · original

Review: the merged-ES register layout is right (v5 vertex id, v8 instance id, attributes from v9). One change needed: s3 = 0x40 only works for wave64. In a wave32 program s_bfm_b32 uses width 64 & 31 = 0, so exec is 0 again. Please seed 0x20 for wave32 and 0x40 for wave64, and add a wave32 case to the test.

**@⁠omonkeyz** on GitHub · 2026-11-23 13:20 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/278#issuecomment-7102388637) Review: the merged-ES register layout is right (v5 vertex id, v8 instance id, attributes from v9). One change needed: s3 = 0x40 only works for wave64. In a wave32 program s_bfm_b32 uses width 64 & 31 = 0, so exec is 0 again. Please seed 0x20 for wave32 and 0x40 for wave64, and add a wave32 case to the test.
Author
Owner

@⁠GDAgent on GitHub · 2026-11-23 13:54 UTC · original

Thanks, agreed. Pushed 5b93ec3 (rebased onto current main, so the first commit is now cdd791e).

  • s3 is now seeded with 0x20 for a wave32 program and 0x40 for a wave64 program. The wave size is the one the translator already derives for the program (mesh::wave_size over its instructions, stored as wave64 on the emitter), so no new detection was added.
  • New test merged_vertex_shader_seeds_wave_info_for_the_program_wave_size builds the merged prologue twice: a wave32 form (s_bfm_b32, s_cmp_eq_u32 .., 32, s_cselect_b32 exec_lo) and a wave64 form (s_bfm_b64, compare with 64, s_cselect_b64). It checks that the wave32 shader gets s3 = 0x20u; (and never 0x40) and the wave64 one gets s3 = 0x40u;, that both compile with dxc, and that the vertex and instance id still come from v5 and v8. The check is on the generated HLSL, not on executed lanes: I did not run a wave32 draw on a GPU.
  • cargo test --locked -p tombox-xshader (native Windows 11, GPU-gated suites off): 348 passed, 0 failed, 18 ignored. The PR gate reports 0 problems.
**@⁠GDAgent** on GitHub · 2026-11-23 13:54 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/278#issuecomment-7103210749) Thanks, agreed. Pushed 5b93ec3 (rebased onto current main, so the first commit is now cdd791e). - s3 is now seeded with 0x20 for a wave32 program and 0x40 for a wave64 program. The wave size is the one the translator already derives for the program (`mesh::wave_size` over its instructions, stored as `wave64` on the emitter), so no new detection was added. - New test `merged_vertex_shader_seeds_wave_info_for_the_program_wave_size` builds the merged prologue twice: a wave32 form (`s_bfm_b32`, `s_cmp_eq_u32 .., 32`, `s_cselect_b32 exec_lo`) and a wave64 form (`s_bfm_b64`, compare with 64, `s_cselect_b64`). It checks that the wave32 shader gets `s3 = 0x20u;` (and never 0x40) and the wave64 one gets `s3 = 0x40u;`, that both compile with dxc, and that the vertex and instance id still come from v5 and v8. The check is on the generated HLSL, not on executed lanes: I did not run a wave32 draw on a GPU. - `cargo test --locked -p tombox-xshader` (native Windows 11, GPU-gated suites off): 348 passed, 0 failed, 18 ignored. The PR gate reports 0 problems.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-23 14:27 UTC · original

Applied locally on main with your authorship kept (cherry-picked, 343c15a); the tombox-xshader suite passes in a sandbox (exec 158/158 on WARP). It ships in the next release; this PR will be closed when that is pushed. Thanks.

**@⁠omonkeyz** on GitHub · 2026-11-23 14:27 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/278#issuecomment-7104040191) Applied locally on main with your authorship kept (cherry-picked, 343c15a); the tombox-xshader suite passes in a sandbox (exec 158/158 on WARP). It ships in the next release; this PR will be closed when that is pushed. Thanks.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-25 13:07 UTC · original

Applied on main in 343c15a (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

**@⁠omonkeyz** on GitHub · 2026-11-25 13:07 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/278#issuecomment-7163326481) Applied on main in 343c15a (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

Pull request closed

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