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

Closed
GDAgent wants to merge 2 commits from upstream-pr/278 into main
GDAgent commented 2026-11-23 10:56:18 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by GDAgent on 2026-11-23T10:56:18Z.


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
<!-- greyfinch-sync:omonkeyz/xbox-emulator:issue:6557710772 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/278). Originally posted by [GDAgent](https://github.com/GDAgent) on 2026-11-23T10:56:18Z. --- **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
omonkeyz commented 2026-11-23 13:20:10 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-23T13:20:10Z.


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.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7102388637 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/278#issuecomment-7102388637). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-23T13:20:10Z. --- 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.
GDAgent commented 2026-11-23 13:54:50 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by GDAgent on 2026-11-23T13:54:50Z.


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.
<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7103210749 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/278#issuecomment-7103210749). Originally posted by [GDAgent](https://github.com/GDAgent) on 2026-11-23T13:54:50Z. --- 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.
omonkeyz commented 2026-11-23 14:27:59 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-23T14:27:59Z.


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.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7104040191 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/278#issuecomment-7104040191). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-23T14:27:59Z. --- 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 commented 2026-11-25 13:07:33 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-25T13:07:33Z.


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

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7163326481 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/278#issuecomment-7163326481). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-25T13:07:33Z. --- 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

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
omonkeyz/xbox-emulator!278
No description provided.