tombox-xshader: seed merged_wave_info, vertex and instance index for merged vertex shaders #278
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
library
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/omonkeyz--xbox-emulator!278
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/278/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 mergingLibrary / area:
tombox-xshader(hlsl.rsvertex-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
and take their inputs in the merged layout: the vertex index in
v5, the instance index inv8, and the fetched attributes fromv9on. In The Matrix Awakens (Xbox Series X build), a sample merged VS reads exactlyv5,v8,v9..v11(ATTRIBUTE0.xyz) andv13(ATTRIBUTE13.x) before writing them. That matches this layout: five GS VGPRs, then ESv5vertex id,v6/v7,v8instance id, then attribute groups of four.The translation never set
s3, soexecbecame 0 at the second instruction and every output write of these shaders was skipped. It also loaded the vertex and instance index intov0/v3(or, for vertex shaders that hand their outputs to a geometry shader through LDS, not at all), and the attributes intov4onwards.Changes:
s3withs_bfe_u32/s_bfe_i32, and which readss3before writing it, is treated as a merged ES:s3starts as0x40(a full wave, soexeccovers the vertex);SV_VertexIDgoes tov5andSV_InstanceIDtov8;v9instead ofv4.s3(0x100), and #200 uses the sames3/v5convention for mesh shaders; this PR does not touch either.Effect on The Matrix Awakens' shader archive (35382 translated shaders, HLSL compared with
main):main.v32that nothing initialises: 4263 before, 1475 after. 929 of the remaining ones read onlyv0, as the LDS address operand of their output writes, which the translation ignores.vs_6_0.Tests (
tests/exec.rs):merged_vertex_shader_prologue_keeps_the_vertex_active: the input-layout draw ofvertex_fetch_reads_sparse_attributes_from_a_vertex_bufferwith the merged prologue in front and the attributes read fromv9. Onmainnothing 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,v8andv9are seeded,v0/v3are not, and DXC compiles the result.mainde1e1da:cargo test -p tombox-xshader --no-fail-fast346 passed, 0 failed (WARP);cargo test -p d3d12_xs --lib -- --test-threads=1(withTOMBOX_REMOVED_EXIT_MS=off) 659 passed, 4 failed, 71 ignored. The same 4 fail onmainhere (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).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/v7inputs of the merged layout are not seeded: no shader in the archive was seen reading them before writing them.cargo test -p tombox-xshaderpassesmissingwith a noteMost 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.@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.
@GDAgent on GitHub · 2026-11-23 13:54 UTC · original
Thanks, agreed. Pushed
5b93ec3(rebased onto current main, so the first commit is nowcdd791e).mesh::wave_sizeover its instructions, stored aswave64on the emitter), so no new detection was added.merged_vertex_shader_seeds_wave_info_for_the_program_wave_sizebuilds 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 getss3 = 0x20u;(and never 0x40) and the wave64 one getss3 = 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 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-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.Pull request closed