diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index d365f4663..a1f7664a1 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -839,9 +839,12 @@ The compute path, end to end: so descriptor bytes the application writes itself are what shaders load. - Upload and readback buffers keep their own memory, and their VA is their device address minus the heap base. - Shader programs (FSSM blobs) are copied in as the code base. -- **`CreateRootSignature`** parses the blob with `parse_rts0` and keeps each parameter's place in two layouts: - `pack()` for the graphics bind point and `pack_stage(Stage::Compute)` for the compute one (from s4, one dword per - table; section 3.5, D3D-448). A signature neither can place (more than 32 user dwords: the spill layout is open; the +- **`CreateRootSignature`** parses the blob with `parse_rts0` and keeps each parameter's place in two layouts, both + the console's (one dword per table; section 3.5, D3D-448): `pack_stage(Stage::Pixel)` for the graphics bind point + (from s8; a signature past s31 there keeps `pack()`'s s0 layout, logged) and `pack_stage(Stage::Compute)` for the + compute one (from s4). It also checks + `pack_stage(Stage::Mesh)`: a signature past a mesh shader's 16-dword window is logged, and `DispatchMesh` with it + invalidates the list. A signature neither bind point can place (past s31: the spill layout is open; the translated shaders have no below-base spill-pointer candidates) is refused with both reasons; one only a single layout places is created with a log line, and binding it at the other bind point invalidates the list. Before that it answers as the host runtime does (`compare_invalid_arguments`, "root signature:", RTX 5070 / 621.14): @@ -1877,7 +1880,8 @@ This is the least verifiable part of the layer. D3D12 gives the application a ro turns it into the user SGPRs (`s0..s31`) that the shader reads. The translator already copies those 32 dwords from push constants into `s0..s31`. The layer must produce them. -The default mapping (implemented by `rt::rootsig::pack` in `runtime/src/root_signature.{h,cpp}`) follows root +The first mapping the layer used (`rt::rootsig::pack` in `runtime/src/root_signature.{h,cpp}`, kept for the tools and +tests that compare the two models; the runtime now uses the console layout below at both bind points) follows root parameter order: | Root parameter | User-data dwords | @@ -1917,11 +1921,27 @@ below 4 GiB). A parameter that does not fit goes, with every later one, to the s - **Vertex/pixel/geometry s8 and mesh s16: a hypothesis.** It rests on the census above only; no signature confirms these bases, and visibility is not modelled (every parameter takes its place in every stage). -The runtime uses the console layout for the **compute** bind point only. Graphics keeps `pack()`: every stage of a -pipeline reads `s0..s31` from one push-constant block at offset 0 (`tools/rdna2spirv.py`, `ShaderUserData` in -`runtime/src/vulkan_replay.h`), so a mesh shader (base s16) and a pixel shader (base s8) in one pipeline would need -the same root argument at two offsets. That needs per-stage user-data windows in the translator's push-constant -contract (D3D-448 follow-up). A root signature carries both layouts; one that only one of them can place is +The runtime uses the console layout at **both** bind points. Every stage of a pipeline reads one push-constant block +(`ShaderUserData` in `runtime/src/vulkan_replay.h`), so the graphics bind point lays a signature out once with +`pack_stage(Stage::Pixel)` (the same s8 base as vertex and geometry): root dword k at push dword 8 + k. A vertex, +pixel or geometry shader reads SGPR i from push dword i, so it finds the arguments at s8.. as the console puts them. +A graphics signature past s31 (more than 24 dwords from s8) keeps `pack()`'s layout (from s0, two dwords per table), +logged as "keeps the s0 graphics layout": the graphics spill array is not modelled. `LEONIDA_GRAPHICS_STAGE_LAYOUT=0` +keeps `pack()` for every graphics signature (`RootArgumentLayout::graphics_console` false). +**The graphics root window (D3D-448-GRAPHICS):** a mesh shader's root arguments start at s16, so the translator loads +its s16..s31 from push dwords 8..23 instead (`user_data_dword` in `tools/rdna2spirv.py`); a mesh and a pixel shader +of one pipeline then find each argument in the same dword. A mesh shader's s8..s15 are below its base and read 0; +s0..s7 stay push dwords 0..7 for every stage. The index records the window as `"root_window": {"sgpr": 16, "dword": +8}`, and a mesh PSO whose mesh shader lacks it (translated before) is refused with a log line asking for the shaders +stage again. The mesh window holds 16 dwords (s16..s31) where the others hold 24 (s8..s31): a signature that fits the +graphics layout but not the mesh window, or one on `pack()`'s s0 layout, is created (the first is logged), and +`DispatchMesh` with it invalidates the list (`RootArgumentLayout::mesh_ok`, `d3d12_mesh_test`). Live evidence for s8 +and one dword per table: on the retail frame loop the VS of a textured draw reads table slots 0 and 2 through s8 and +its PS slot 1, which hold exactly the buffer, constants and texture the title bound (issue #14, comment 7131556366, +native Windows, RTX 5080). What mesh shaders read below s16 was counted on the 1.0.2.1 corpus +(`user_sgpr_census.py --base ms=16`, issue #14 comment 7138532347): of 9,282 mesh shaders, 9,279 read nothing below +s16; three read one 32-bit constant there (`s13` in one, `s15` in two), never a pointer, and they read it as 0 now. +Its meaning is not established. A root signature carries both layouts; one that only one of them can place is created, and binding it at the other bind point invalidates the command list (`Close` returns `E_INVALIDARG`). `s0..s3` of a compute dispatch are the driver's block; the layer leaves them zero (109 CS read `s1` as a constant, 42 `s3`; their meaning is open). diff --git a/docs/design.md b/docs/design.md index 324fa92e9..a09a2bd24 100644 --- a/docs/design.md +++ b/docs/design.md @@ -293,7 +293,7 @@ How the generated SPIR-V expects the runtime to work: | `s_*_saveexec_b32/b64`, `s_andn1/andn2_wrexec_b32/b64` | RDNA2 ISA SOP1. saveexec: `D = EXEC; EXEC = f(S0, EXEC); SCC = (EXEC != 0)`. wrexec: `EXEC = f(S0, EXEC); D = EXEC`. `f` is one of `and`, `or`, `xor`, `andn1` (`~S0 & EXEC`), `andn2` (`S0 & ~EXEC`), `orn1` (`~S0 \| EXEC`), `orn2` (`S0 \| ~EXEC`), `nand`, `nor`, `xnor`, the table in `Gen.EXEC_LOGIC`. The definitions match `tools/cf_ref.py` `_SAVEEXEC`, the FUZZ-8 model. Each invocation holds its own lane's bit (0 or ~0), so `f` is the bitwise op on that bit. A lane EXEC had off becomes active when `f` gives 1 for it (`nand`, `nor`, `orn1`, `orn2`, `xnor`, ...), as on hardware, and SCC is the wave vote. Until 2026-11-22 only `and`/`or`/`andn2_saveexec` and `andn2_wrexec` were translated. CPU-checked by `tests/semantic/test_saveexec_variants.py` (all ten saveexec kinds in wave32 and wave64, both wrexec kinds, partial EXEC and S0, an empty result, every lane against `cf_ref._SAVEEXEC`) and by `tools/cf_diff.py` (300 programs × wave32/64 still pass). Hardware-verified on native RDNA2 (`gfx1036`, Windows 11, driver `32.0.21045.29003`, wave32 & wave64, 0 mismatches): all 10 `saveexec` kinds and both `wrexec` kinds (`andn1`/`andn2`) match bit-for-bit on non-symmetric masks (`src0 = 0x0F0F0F0F`, `exec = 0x33333333` in wave32; `0x0F0F0F0F_00FF00FF` / `0x33333333_55555555` in wave64), zero-result `SCC = 0`, wave64 upper-32-bit-only `new_exec = 0x00000001_00000000` setting `SCC = 1`, and `sdst == src0` register aliasing (`s4, s4` and `s[4:5], s[4:5]`) reading original `src0` before writing old `EXEC` to `sdst`. **Not GPU-verified** for the seven new kinds. Measured on native gfx1100 (single 8-lane workgroup, 36 programs): all ten `saveexec` kinds and both `wrexec` forms follow `f` for three `S0`/`EXEC` pairs, including lanes `EXEC` had off being switched back on (`nand` with `S0=0x03`, `EXEC=0x0F` leaves `0xFFFFFFFC`, activating lanes 4-7), `D` holding the old `EXEC` for `saveexec` and the new one for `wrexec`, and `SCC` the `EXEC != 0` vote. | | Control flow | Reducible graphs use structured branches and natural loops. Irreducible graphs use the dispatch-loop form, which can also be forced with `--dispatch-loop` for comparisons. The control-flow unit test checks 18,228 generated graphs. Irreducible graphs use node splitting (`cfg_structure.split_irreducible`, up to `min(2*N, N+64)` blocks) and fall back to the dispatch-loop form when capped; it can also be forced with `--dispatch-loop` for comparisons. `cfg_structure.structure`, `cfg_structure.run`, and `rdna2spirv.assemble`'s structured emitter are iterative and hoist each guarded member's dominated tail continuation out of its enclosing guard (exact by strict dominance: no block outside the guard's subtree can target the continuation), keeping `OpSelectionMerge` nesting O(1) on sequential diamond/loop chains instead of nesting O(N) deep and hitting Python's recursion limit on multi-thousand-block shaders (e.g. 2,000 sequential diamonds nest to depth 2 instead of 2,000). The control-flow unit test (`tests/test_cfg_structure.py`) checks 20,000 generated graphs plus 2,000-diamond, 1,200-loop, and 1,500-early-exit chains. AMD A/B checks compare the two forms for selected pixel shaders with defined test inputs. For shaders that never export colour, the comparison checks pixel coverage. **Nesting is bounded (SHADER-299):** a tree deeper than `MAX_STRUCTURED_DEPTH` (16) is emitted in the dispatch-loop form instead, since `spirv-val` time grows roughly with the cube of the measured nesting depth. **Nesting is bounded (SHADER-299, SHADER-427):** a tree deeper than `MAX_STRUCTURED_DEPTH` (64; `--max-structured-depth N`, 0 = no bound) is emitted in the dispatch-loop form instead, since `spirv-val` time grows roughly with the cube of the measured nesting depth. Before #1904, over the 1.0.2.1 corpus (26,503 modules, `spirv-val` 2026.4): the deepest block of a module is nested 118 constructs deep, 12,963 modules are structured (median 15) and 6,000 of those are deeper than 16; an estimated 85% of the corpus's `spirv-val` time is in modules nested 20 or deeper. Median `spirv-val` time for the same module in each form: depth 20 181 -> 129 ms (1.4x), 30 507 -> 371 (1.4x), 50 1,712 -> 738 (2.3x), 64 2,692 -> 1,163 (2.3x), 77 5,122 -> 1,130 (4.5x), and the deepest (118) 13,683 -> 1,352 ms (10.1x). Depth 5-12 shows no gain (at depth 6 the flat form is slightly slower), which is why the bound first sat at 16. Since #1904 (`cfg_structure` hoists tails) the deepest tree in the corpus is 29: of 12,944 structured modules, 1,031 are deeper than 16 (747 CS, 284 PS), 57 deeper than 20, 24 deeper than 24. For those 1,031 the flat form saves only 12% of `spirv-val` time (1,235 -> 1,091 s summed, slowest module 8.3 -> 5.9 s), while on an RTX 5070 Ti (NVIDIA 621.24) it compiles slower: the 284 pixel shaders 1.67x (827 -> 1,382 s summed, two rounds), four compute modules at depth 19-24 2.0x (297 -> 591 s, 1.6-2.7x each), so the bound now sits above the corpus (SHADER-427). Both forms pass `spirv-val` on a 200-shader slice (200/200 each), and `gen.nesting` records the tree depth. AMD's Windows driver adds a reason to keep the structured form (SHADER-995): on a Radeon gfx1036 iGPU (32.0.21042.62) the dispatch-loop module of `XboxBvhBuilder.dll`'s `LeafRehydration` (tree depth 18, a 581-case switch) takes 34 minutes to create (XXDESIGN), where its structured module is created in 2.9 s; ten other programs of that DLL compile in the dispatch-loop form there in 0.4-40 s, 2.5-5x their structured time ([embedded_shaders.md](embedded_shaders.md), section 7). | | Loop guard | Each loop counts header visits per invocation and exits through its structured merge at `--loop-limit N` (default 65536, zero disables). Counters reset on entry, including nested EXEC retries and atomic loops. A PC-dispatch visit counts a dispatch iteration. Natural exits within the budget preserve their outputs. Five-star-recomp (b12a952) reported a story-load timeout when the shader input made its exit condition unreachable. | -| User data | **Push constants, 144 bytes:** dwords 0–31 = initial s0–s31 (the user SGPRs the console driver loads from the root signature: descriptor-table pointers, constants and so on), dwords 32–33 = the 64-bit **heap base**, dwords 34–35 = the **code base**: the Xbox VA where the runtime uploaded this shader's whole FSSM blob. `--mesh-compute` modules add dwords 36–37, the output buffer address (152 bytes; see "Mesh shaders as compute"). `--pc-tail-ubo` keeps only dwords 0–31 here (128 bytes) and moves the rest to a uniform block (see "Push-constant tail as a uniform block"). The runtime has to reproduce the console driver's root-signature-to-user-SGPR mapping; that comes from the exe or driver behaviour. | +| User data | **Push constants, 144 bytes:** dwords 0–31 = initial s0–s31 (the user SGPRs the console driver loads from the root signature: descriptor-table pointers, constants and so on), dwords 32–33 = the 64-bit **heap base**, dwords 34–35 = the **code base**: the Xbox VA where the runtime uploaded this shader's whole FSSM blob. `--mesh-compute` modules add dwords 36–37, the output buffer address (152 bytes; see "Mesh shaders as compute"). `--pc-tail-ubo` keeps only dwords 0–31 here (128 bytes) and moves the rest to a uniform block (see "Push-constant tail as a uniform block"). The runtime has to reproduce the console driver's root-signature-to-user-SGPR mapping; that comes from the exe or driver behaviour. **Mesh shaders** read their root SGPRs through the graphics root window: s16..s31 come from dwords 8..23 (where a VS, PS or GS reads s8..s23), s8..s15 are 0, s0..s7 dwords 0..7; the index records `"root_window": {"sgpr": 16, "dword": 8}` (D3D-448-GRAPHICS, `docs/d3d12_vulkan.md` 3.5). Checked by `tests/semantic/test_ms_root_window.py` (MeshEXT and `--mesh-compute`, CPU interpreter) and `d3d12_mesh_test` (RX 9070 XT). | | Direct resource heap | In the matched Prop/Tree BatchCull compute signature the two explicit tables and scalar constant occupy s4–s6. Bind the resource heap's uint32 arena-relative byte address at s7 (push-constant byte 28). The program supplies a zero high word and scales the resource index by 32 bytes. Other stages and signatures consume different SGPRs. See [descriptor heaps and the bindless model](d3d12_vulkan.md#34-descriptor-heaps-and-the-bindless-model) for the evidence and graphics examples. | | Xbox GPU addresses | Shaders build Xbox GPU virtual addresses, and some force the high dword to 0 (descriptor tables in the low 4 GB). Every memory access goes to **heap base + Xbox VA**, so the runtime maps the console's GPU address window into one host buffer-device-address allocation. | | Code-embedded data | 155 VS/PS had "undecodable words". They're **constant tables inside the code range** (float values like 0x3EF7…, plus zeros), read through `s_getpc_b64` + `s_add_u32`/`s_addc_u32` + `s_load` (147 of 155). The translator re-anchors each instruction to its true FSSM offset by matching encodings (when the llvm-mc messages give one offset per rejected word, `shader_ir.parse_file`'s `invalid_at`, those words are skipped exactly and nothing is searched forward: a forward search let a data word printed as a copy of a later instruction take that instruction's place and lose every one after it; counted on the 1.0.2.1 pipeline library with #327's vcc-tuple decoding, 36 compute programs that stopped at "cannot re-anchor" now translate and pass `spirv-val`, none lost; `tests/semantic/test_reanchor_llvm_offsets.py`), translates only code reachable from entry (a branch into data is rejected), and turns `s_getpc_b64` into code base + next-instruction offset. **The runtime uploads each shader's FSSM blob into the heap and passes that VA as the code base.** Re-anchoring compares bytes exactly, except that VOP3P `op_sel_hi[2]` (bit 14) is ignored on two-source packed ops: the compiler stores 0 there, and LLVM prints its default of 1. Unused VOP3/VOP3P `SRC1` (bits 49:41) and `SRC2` (bits 58:50) fields are also ignored when stored as 0 and printed as `0x80`: LLVM 23 re-encodes a source it doesn't read as inline constant 0 (one-source `v_rcp`/`v_log`/`v_sqrt_f32_e64`, two-source `v_sub_f32_e64`/`v_cmp_*_e64`/`v_mul_lo_u32`/`v_pk_mul_f16`). Both checks for clean listings and re-anchoring use the parsed operand count to apply this exception only to unused sources. A used source encoded as 0 reads `s0` and is never treated as inline zero. `tests/semantic/test_printed_encoding.py` checks used `SRC1`/`SRC2` and an unused field with synthetic listings. Confirmed by a count over the corpus with LLVM 23.1.5: without it, 1,471 of the 1,473 shaders with embedded data are rejected ("reachable instruction with normalized encoding"); with it, all 1,473 translate and pass `spirv-val`. Runs with LLVM 21 already translated all of them, so older LLVMs presumably print the stored 0 (not re-checked). Data words that decode with ignored fields set are printed re-encoded (e.g. `0xbe8d2242` as `s_rfe_b64` `0xbe802242`), so they can never match. Since the decoder consumed the same length, they are anchored in sequence and marked. A shader that can *reach* a marked word is rejected; none of the corpus does. All 155 now translate. A reachable instruction that falls through into words llvm-mc skipped (it printed no instruction there) is rejected too, as `fall-through into undecodable words`: until SHADER-813 the reachable path ended there without a word, so everything after the gap (exports, stores) was dropped from a module that still passed `spirv-val`. No VS/PS of the corpus has such a gap on a reachable path; in the FidelityFX FSPX programs it is the `v_dot4_i32_i8 ... op_sel` words LLVM 23 cannot decode (`docs/fspx.md`), so those programs are now refused instead of translated without their stores, until those words are decoded (#2038, #2323). `tests/test_rdna2spirv_decode.py` (`test_fall_through_into_undecodable_words_is_refused`). | diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 652003b19..05e125a70 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -3712,21 +3712,11 @@ if(TARGET Vulkan::Headers) DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/test/glsl/ms_draw.mesh COMMENT "Compiling the mesh test shader ms_draw.mesh" VERBATIM) - # The mesh test's signature is on the s0 fallback layout (see d3d12_mesh_test.cpp), so its pixel shader is - # ps_draw.frag reading the tint VA at s2:s3 instead of s10:s11. - set(LEONIDA_TEST_PS_S0 ${CMAKE_CURRENT_BINARY_DIR}/generated/ps_draw_s0.spv) - add_custom_command( - OUTPUT ${LEONIDA_TEST_PS_S0} - COMMAND ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE} -V --target-env vulkan1.3 -DROOT_BASE=0 -o ${LEONIDA_TEST_PS_S0} - ${CMAKE_CURRENT_SOURCE_DIR}/test/glsl/ps_draw.frag - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/test/glsl/ps_draw.frag - COMMENT "Compiling the mesh test's pixel shader ps_draw.frag (s0 layout)" - VERBATIM) add_executable(d3d12_mesh_test test/d3d12_mesh_test.cpp shims/d3d12_xs_device.cpp shims/d3d12_xs_pso_stream.cpp ${LEONIDA_TEST_MS} - ${LEONIDA_TEST_PS_S0} ${LEONIDA_TEST_VS_FULLSCREEN}) + ${LEONIDA_TEST_PS} ${LEONIDA_TEST_VS_FULLSCREEN}) target_link_libraries(d3d12_mesh_test PRIVATE gta6rt_d3d12_com) target_compile_definitions(d3d12_mesh_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX - LEONIDA_TEST_MS_SPV="${LEONIDA_TEST_MS}" LEONIDA_TEST_PS_SPV="${LEONIDA_TEST_PS_S0}" + LEONIDA_TEST_MS_SPV="${LEONIDA_TEST_MS}" LEONIDA_TEST_PS_SPV="${LEONIDA_TEST_PS}" LEONIDA_TEST_VS_FULLSCREEN_SPV="${LEONIDA_TEST_VS_FULLSCREEN}") target_compile_options(d3d12_mesh_test PRIVATE /W4 /permissive- /utf-8) add_test(NAME d3d12_mesh COMMAND d3d12_mesh_test) diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index b6ec111c7..f919cc1eb 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp @@ -529,6 +529,11 @@ class GraphicsCommandList final d3d12_trace::record("ID3D12GraphicsCommandList6::DispatchMesh"); if (x > 65535 || y > 65535 || z > 65535 || std::uint64_t(x) * y * z > (1u << 22)) return check(false); if (!graphics_pipeline_ || !graphics_.layout) return check(false); + // A mesh shader reads the root arguments from s16 through a 16-dword window (RootArgumentLayout::mesh_ok). + if (!graphics_.layout->mesh_ok) { + stub("DispatchMesh(root signature larger than a mesh shader's root window)"); + return check(false); + } if (!any_nonempty_viewport()) return; // as record_draw: nothing is rasterized leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, {}, 0, viewport_, scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; @@ -1440,9 +1445,20 @@ class GraphicsCommandList final if ((count && !data) || std::uint64_t(offset) + count > p->constant_count) return check(false); const auto* values = static_cast(data); const std::uint32_t first = first_dword(state, *p); + if (first == rt::rootsig::kNoSgpr) return spilled(); for (UINT i = 0; i < count; ++i) write(state, first + offset + i, values[i]); } + // A parameter in the spill array behind s[0:1] (a compute signature past s31: pack_stage gives it no SGPR, only a + // spill_dword): that array is not written yet, so its value is not stored; kNoSgpr plus an offset must never + // reach write(), where it would wrap round to s0. + static void spilled() { + static std::atomic said{false}; + if (!said.exchange(true)) + std::fprintf(stderr, "[rt] d3d12: root arguments in the spill array behind s[0:1] are not stored (the " + "array is not written yet)\n"); + } + void address(RootState& state, UINT index, std::uint64_t va, rt::rootsig::ParamType type) { if (list_.closed()) return; if (defer([=, compute = &state == &compute_](GraphicsCommandList& l) { @@ -1466,6 +1482,7 @@ class GraphicsCommandList final tables_.emplace_back(va, p->table_descriptors); } const std::uint32_t first = first_dword(state, *p); + if (first == rt::rootsig::kNoSgpr) return spilled(); write(state, first, std::uint32_t(va)); // In the console layouts a descriptor table is one dword, the 32-bit VA of its first descriptor (the heaps // live below 4 GiB); a root view keeps both halves. pack()'s graphics layout gives every address two dwords. diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index 1a0a36c6b..0d76ae44a 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h @@ -448,10 +448,13 @@ std::unique_ptr create_window_presenter(ID3D12Device* device, H // carries both layouts: // - graphics: rt::rootsig::pack_stage(Stage::Pixel), the console layout of vertex, pixel and geometry shaders // (from s8, one dword per table), which the game's own shaders read: VS_Blit/PS_BlitColour and the Scaleform -// VS load their table's first descriptor from s[8:9] with s9 forced to 0. A signature that would spill there +// VS load their table's first descriptor from s[8:9] with s9 forced to 0. A vertex, pixel or geometry shader +// reads SGPR i from push dword i, so root dword k is push dword 8 + k. A signature that would spill there // (more than 24 dwords) keeps rt::rootsig::pack() (from s0, two dwords per table), logged: the graphics spill -// array is not modelled. A mesh shader reads from s16 and shares the block with its pixel shader (s8), so mesh -// pipelines still see their arguments 8 dwords early (D3D-448); +// array is not modelled. A mesh shader's root arguments start at s16; the translator loads its s16.. from push +// dwords 8.. ("root_window" in index.json), so the same block serves both stages of a mesh pipeline. Its window +// is 16 dwords: mesh_ok says whether Stage::Mesh places the signature too, and a mesh draw with one it refuses +// invalidates the list (D3D-448-GRAPHICS); // - compute: rt::rootsig::pack_stage(Stage::Compute), the measured console layout (from s4, one dword per table). // A layout that refuses the signature is marked unusable; binding the signature there invalidates the list. // LEONIDA_GRAPHICS_STAGE_LAYOUT=0 keeps rt::rootsig::pack() (from s0, two dwords per address) for every graphics @@ -466,7 +469,7 @@ inline bool local_graphics_stage_layout() { struct RootArgumentLayout { struct Parameter { rt::rootsig::ParamType type; - std::uint32_t user_offset; // graphics: first dword; unused for a parameter with no dwords + std::uint32_t user_offset; // graphics: first push dword; unused for a parameter with no dwords std::uint32_t constant_count; // constants only std::uint32_t compute_offset = rt::rootsig::kNoSgpr; // compute: first user SGPR // A table: the slots its ranges declare, for the scan of title-written V#s before a submission @@ -479,6 +482,9 @@ struct RootArgumentLayout { // The graphics layout is the console's (pack_stage(Stage::Pixel)): a table takes one dword. False: pack()'s, two. bool graphics_console = false; bool graphics_ok = true; + // The console graphics layout also fits a mesh shader's 16-dword window (s16..s31 = push dwords 8..23). False + // for a signature on pack()'s s0 layout, which a mesh shader translated with the root window cannot read. + bool mesh_ok = false; bool compute_ok = true; // One past the last user-data dword the explicit root arguments take at each bind point, where the console puts // the directly indexed heaps (resource heap, then sampler heap). kNoSgpr: no such dword (the compute arguments diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index c272e03e9..12494d201 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -7320,6 +7320,15 @@ class PipelineState final std::fprintf(stderr, "[rt] d3d12: %s\n", refusal.c_str()); return stream_unsupported("mesh shader exceeds a device mesh limit"); } + // The graphics bind point lays root arguments out from push dword 8, which a mesh shader reads from + // its s16 only when translated with the graphics root window (index "root_window", D3D-448). + if (!stage.entry->graphics_root_window) { + std::fprintf(stderr, + "[rt] d3d12: mesh shader %s was translated before the graphics root window; translate " + "the shaders again (make_game.py --stages shaders)\n", + stage.entry->name.c_str()); + return E_INVALIDARG; + } } if (stage.entry->uses_code_base) { if (code_stage) { @@ -11855,8 +11864,8 @@ class Device final if (container_size != size) return E_INVALIDARG; } // The repository's root-signature model (runtime/src/root_signature.h): parse the RTS0 blob, then pack - // the parameters into the 32 user-data dwords. pack() refuses what it cannot place (more than 32 dwords: - // the console spills those to memory, in a layout that is still open), and so does this. + // the parameters into the 32 user-data dwords in the console layout. pack_stage() refuses what it cannot + // place (past s31: the console spills those to memory, in a layout that is still open), and so does this. const rt::rootsig::DecodeResult parsed = rt::rootsig::parse_rts0(bytes, size); if (!parsed.ok) { std::fprintf(stderr, "[rt] d3d12: root signature not understood: %s\n", parsed.error.c_str()); @@ -11865,11 +11874,14 @@ class Device final // Two layouts, one per bind point (RootArgumentLayout): graphics takes the console layout of vertex and // pixel shaders (s8, one dword per table) where it fits without a spill and keeps pack() otherwise, compute // the console layout pack_stage() measured (D3D-448). The signature is refused only when neither can place it. + // A mesh shader reads the same graphics block from its s16 (the translator's root window, push dword 8 = s16), + // so its 16-dword window only needs checking (D3D-448-GRAPHICS). const rt::rootsig::PackResult packed = rt::rootsig::pack(parsed.sig); // LEONIDA_GRAPHICS_STAGE_LAYOUT=0 keeps pack() for every graphics signature. const rt::rootsig::StagePackResult pixel = local_graphics_stage_layout() ? rt::rootsig::pack_stage(parsed.sig, rt::rootsig::Stage::Pixel) : rt::rootsig::StagePackResult{}; + const rt::rootsig::StagePackResult mesh = rt::rootsig::pack_stage(parsed.sig, rt::rootsig::Stage::Mesh); const rt::rootsig::StagePackResult compute = rt::rootsig::pack_stage(parsed.sig, rt::rootsig::Stage::Compute); if (!pixel.ok && packed.ok && local_graphics_stage_layout()) { static std::atomic kept{0}; @@ -11886,12 +11898,21 @@ class Device final } if (!graphics_ok) std::fprintf(stderr, "[rt] d3d12: root signature usable for compute only: %s\n", packed.error.c_str()); + else if (pixel.ok && !mesh.ok) { + static std::atomic too_large{0}; + const unsigned n = ++too_large; + if (n == 1 || (n & (n - 1)) == 0) + std::fprintf(stderr, + "[rt] d3d12: root signature too large for a mesh shader's root window: %s (%u so far)\n", + mesh.error.c_str(), n); + } if (!compute.ok) std::fprintf(stderr, "[rt] d3d12: root signature usable for graphics only: %s\n", compute.error.c_str()); RootArgumentLayout layout; layout.flags = parsed.sig.flags; layout.graphics_console = pixel.ok; layout.graphics_ok = graphics_ok; + layout.mesh_ok = pixel.ok && mesh.ok; layout.compute_ok = compute.ok; if (compute.ok && !compute.spill_dwords) layout.compute_arguments_end = compute.end_sgpr; // Vertex, pixel and geometry shaders share the s8 layout and one user-data block (a non-compute stage diff --git a/runtime/src/root_signature.h b/runtime/src/root_signature.h index 60b0d5632..e7976fc73 100644 --- a/runtime/src/root_signature.h +++ b/runtime/src/root_signature.h @@ -129,8 +129,11 @@ constexpr uint32_t kMaxD3d12Cost = 64; // in this layout equals the program's RSRC2 USER_SGPR count in 29 of 29, and the programs read the tables and // root CBVs at exactly these SGPRs (tools/rootsig_census.py --disasm). The game's compute census agrees (lowest // pointer s4, one-dword tables). -// - vertex/pixel/geometry s8 and mesh s16: a hypothesis from the corpus census only (lowest pointer SGPR per stage); -// no signature confirms them, and visibility is not modelled (every parameter takes its place in every stage). +// - vertex/pixel/geometry s8 and mesh s16: from the corpus census (lowest pointer SGPR per stage) and one live +// retail frame (a VS and PS reading their tables at s8, s9, s10; issue #14, comment 7131556366); no serialized +// signature confirms them yet, and visibility is not modelled (every parameter takes its place in every stage). +// The graphics bind point uses Stage::Pixel (s8, as vertex and geometry); a mesh shader reads the same dwords +// through its translator-side window (docs/d3d12_vulkan.md section 3.5). // A parameter that does not fit goes to the console's spill array behind s[0:1] (compute, section 7) together with // every parameter after it. For compute that packed spill array (1 dword per 32-bit constant, 2 per root descriptor, // 1 per descriptor table, no alignment padding) is modelled in StagePackResult; non-compute stages still refuse spills diff --git a/runtime/src/shader_library.h b/runtime/src/shader_library.h index ae045e80b..a359287cb 100644 --- a/runtime/src/shader_library.h +++ b/runtime/src/shader_library.h @@ -865,6 +865,9 @@ struct ShaderEntry { // stages or an index written without the analysis. std::string wave64_graphics; bool emulated() const { return wave64_exchange == "workgroup"; } + // Mesh shaders: the index's "root_window" is {"sgpr": 16, "dword": 8}, so the module reads its root arguments + // from the graphics window (push dword 8 = root dword 0, D3D-448 follow-up). False for one translated before it. + bool graphics_root_window = false; std::vector input_signature; // vertex shaders: what the input layout must feed std::vector inputs; // pixel shaders: input locations declared std::vector outputs; // pixel/vertex shaders: output locations written @@ -1177,6 +1180,12 @@ class ShaderLibrary { e.subgroup_size = u32(g); if (const Json* x = meta.find("wave64_exchange"); x && x->kind == Json::Kind::string) e.wave64_exchange = x->string; + if (const Json* r = meta.find("root_window"); r && r->kind == Json::Kind::object) { + const Json* sgpr = r->find("sgpr"); + const Json* dword = r->find("dword"); + e.graphics_root_window = sgpr && dword && sgpr->kind == Json::Kind::number && + dword->kind == Json::Kind::number && u32(sgpr) == 16 && u32(dword) == 8; + } if (const Json* wg = meta.find("wave64_graphics"); wg && wg->kind == Json::Kind::string) e.wave64_graphics = wg->string; if (const Json* s = meta.find("input_signature"); s && s->kind == Json::Kind::array) { diff --git a/runtime/test/d3d12_mesh_test.cpp b/runtime/test/d3d12_mesh_test.cpp index 2108cb1b7..e59414a01 100644 --- a/runtime/test/d3d12_mesh_test.cpp +++ b/runtime/test/d3d12_mesh_test.cpp @@ -113,22 +113,30 @@ int main(int argc, char** argv) { EXPECT(device_status == vktest::DeviceStatus::ready); const auto ms_program = fssm_program({0x4d530001, 0xbf810000}); + // The same mesh shader listed without "root_window": one translated before the graphics root window (D3D-448). + const auto ms_old_program = fssm_program({0x4d530002, 0xbf810000}); const auto ps_program = fssm_program({0x50530001, 0xbf810000}); const auto vs_program = fssm_program({0x56530002, 0xbf810000}); auto ms_container = shader_container(ms_program); + auto ms_old_container = shader_container(ms_old_program); const auto ps_container = shader_container(ps_program); // The synthetic container has one part at 36, followed by its eight-byte header. ms_container[36 + 8 + 2] = 13; // DXIL mesh shader kind + ms_old_container[36 + 8 + 2] = 13; const auto vs_container = shader_container(vs_program); const auto library = std::filesystem::temp_directory_path() / "leonida_mesh_test"; std::filesystem::create_directories(library); std::filesystem::copy_file(LEONIDA_TEST_MS_SPV, library / "ms_draw.spv", std::filesystem::copy_options::overwrite_existing); + std::filesystem::copy_file(LEONIDA_TEST_MS_SPV, library / "ms_old.spv", std::filesystem::copy_options::overwrite_existing); std::filesystem::copy_file(LEONIDA_TEST_PS_SPV, library / "ps_draw.spv", std::filesystem::copy_options::overwrite_existing); std::filesystem::copy_file(LEONIDA_TEST_VS_FULLSCREEN_SPV, library / "vs_fullscreen.spv", std::filesystem::copy_options::overwrite_existing); { std::ofstream index(library / "index.json", std::ios::binary); index << "{\"ms_draw\": {\"stage\": \"ms\", \"fssm_sha256\": \"" << leonida::sha256_hex(ms_program) + << "\", \"bindings\": [], \"push_constant_bytes\": 144, \"local_size\": [32, 1, 1], " + "\"max_vertices\": 4, \"max_primitives\": 2, \"root_window\": {\"sgpr\": 16, \"dword\": 8}},\n" + " \"ms_old\": {\"stage\": \"ms\", \"fssm_sha256\": \"" << leonida::sha256_hex(ms_old_program) << "\", \"bindings\": [], \"push_constant_bytes\": 144, \"local_size\": [32, 1, 1], " "\"max_vertices\": 4, \"max_primitives\": 2},\n" " \"vs_fullscreen\": {\"stage\": \"vs\", \"fssm_sha256\": \"" << leonida::sha256_hex(vs_program) @@ -164,23 +172,35 @@ int main(int argc, char** argv) { return true; }; const auto root_signature = [&](ID3D12Device* device, Ref& root) { - // The mesh fixture reads s0:s1 (offset) and the colour fixture, built as ps_draw_s0.spv, s2:s3 (tint VA). - // Select the runtime's packed s0 layout by declaring 25 words: one more than fits above s8. - D3D12_ROOT_PARAMETER params[3] = {}; + // [0] two constants (push dwords 8, 9: offset), [1] a root CBV (10, 11: tint), as in d3d12_draw_test. The + // mesh shader reads them as s16..s19 and the pixel shader as s8..s11 (the graphics root window, D3D-448). + D3D12_ROOT_PARAMETER params[2] = {}; params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; params[0].Constants = {0, 0, 2}; params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV; params[1].Descriptor = {1, 0}; - params[2].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; - params[2].Constants = {2, 0, 21}; D3D12_VERSIONED_ROOT_SIGNATURE_DESC root_desc{}; root_desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_0; - root_desc.Desc_1_0 = {3, params, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE}; + root_desc.Desc_1_0 = {2, params, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE}; + const auto blob = serialize_root_signature(root_desc); + return !blob.empty() && + SUCCEEDED(device->CreateRootSignature(0, blob.data(), blob.size(), IID_PPV_ARGS(&root.p))); + }; + // Twenty constants: s8..s27 for a pixel shader, but past a mesh shader's 16-dword window (s16..s31). Thirty: + // past the graphics window (24 from s8) and the compute one (28 from s4) too, as a retail start-up signature is. + const auto wide_root_signature = [&](ID3D12Device* device, Ref& root, UINT values = 20) { + D3D12_ROOT_PARAMETER param{}; + param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; + param.Constants = {0, 0, values}; + D3D12_VERSIONED_ROOT_SIGNATURE_DESC root_desc{}; + root_desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_0; + root_desc.Desc_1_0 = {1, ¶m, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE}; const auto blob = serialize_root_signature(root_desc); return !blob.empty() && SUCCEEDED(device->CreateRootSignature(0, blob.data(), blob.size(), IID_PPV_ARGS(&root.p))); }; const D3D12_SHADER_BYTECODE ms{ms_container.data(), ms_container.size()}; + const D3D12_SHADER_BYTECODE ms_old{ms_old_container.data(), ms_old_container.size()}; const D3D12_SHADER_BYTECODE ps{ps_container.data(), ps_container.size()}; const D3D12_SHADER_BYTECODE vs{vs_container.data(), vs_container.size()}; @@ -231,6 +251,14 @@ int main(int argc, char** argv) { vs_stream.add(TopologySubobject{.inner = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE}); vs_stream.add(TargetsSubobject{.inner = one_target()}); EXPECT(SUCCEEDED(create(device.p, vs_stream, vs_pso))); + // A mesh shader translated before the graphics root window would read its root arguments from the wrong + // dwords: refused, with a log line that asks for the shaders stage again. + { + Stream old_stream; + old_stream.add(RootSubobject{.inner = root.p}).add(MsSubobject{.inner = ms_old}).add(PsSubobject{.inner = ps}); + old_stream.add(TargetsSubobject{.inner = one_target()}); + EXPECT(create(device.p, old_stream, refused) == E_INVALIDARG && !refused.p); + } // Streams the PC runtime refuses. Stream bad = mesh_stream; @@ -466,6 +494,39 @@ int main(int argc, char** argv) { list->RSSetViewports(1, &viewport); list6->DispatchMesh(4096, 1025, 1); EXPECT(list->Close() == E_INVALIDARG); + // A root signature the graphics bind point places (s8..s27) but a mesh shader's window cannot (s16..s31): a + // vertex draw may use it, DispatchMesh may not. + { + Ref wide; + EXPECT(wide_root_signature(device.p, wide)); + EXPECT(SUCCEEDED(list->Reset(allocator.p, mesh_pso.p))); + list->SetGraphicsRootSignature(wide.p); + list->RSSetViewports(1, &viewport); + list6->DispatchMesh(1, 1, 1); + EXPECT(list->Close() == E_INVALIDARG); + // Past the console graphics window (24 dwords from s8): still created, on pack()'s s0 layout (logged). + // Setting its 30 constants is valid; a mesh shader, which reads the console layout, may not use it. + Ref huge; + EXPECT(wide_root_signature(device.p, huge, 30)); + EXPECT(SUCCEEDED(list->Reset(allocator.p, mesh_pso.p))); + list->SetGraphicsRootSignature(huge.p); + const std::array values{}; + list->SetGraphicsRoot32BitConstants(0, 30, values.data(), 0); + EXPECT(SUCCEEDED(list->Close())); + EXPECT(SUCCEEDED(list->Reset(allocator.p, mesh_pso.p))); + list->SetGraphicsRootSignature(huge.p); + list->RSSetViewports(1, &viewport); + list6->DispatchMesh(1, 1, 1); + EXPECT(list->Close() == E_INVALIDARG); // a mesh shader cannot read it + // Four constants: the console layout, and the mesh window holds them. + Ref console_layout; + EXPECT(wide_root_signature(device.p, console_layout, 4)); + EXPECT(SUCCEEDED(list->Reset(allocator.p, mesh_pso.p))); + list->SetGraphicsRootSignature(console_layout.p); + list->RSSetViewports(1, &viewport); + list6->DispatchMesh(1, 1, 1); + EXPECT(SUCCEEDED(list->Close())); + } // A DISPATCH_MESH command signature (D3D12_DISPATCH_MESH_ARGUMENTS, 12 bytes) is created; a stride below its // 12 bytes is refused as the PC runtime refuses it. ExecuteIndirect with it records nothing yet (#3777 replays // it), so the list still closes. diff --git a/runtime/test/glsl/ms_draw.mesh b/runtime/test/glsl/ms_draw.mesh index 8bfd21785..e66b04bb6 100644 --- a/runtime/test/glsl/ms_draw.mesh +++ b/runtime/test/glsl/ms_draw.mesh @@ -2,7 +2,8 @@ // A mesh shader written to the translator's runtime contract (docs/design.md, "User data" and "Mesh shaders"), // standing in for a translated one in d3d12_mesh_test. Each workgroup emits one quad (4 vertices, 2 triangles) on a // 4x4 grid of clip-space cells: column = WorkgroupId.x + WorkgroupId.z, row = WorkgroupId.y, so all three -// DispatchMesh counts reach the shader. s0, s1 (root constants) move the quad in clip space; the colour is red. The +// DispatchMesh counts reach the shader. Push dwords 8, 9 (the root constants, which a translated mesh shader reads as +// s16, s17 through the graphics root window) move the quad in clip space; the colour is red. The // corners are top-left, top-right, bottom-left, bottom-right and the triangles (0,1,2) and (2,1,3), clockwise in D3D // screen space as in d3d12_draw_test, so they are front-facing with the default winding. #extension GL_EXT_mesh_shader : require @@ -22,7 +23,7 @@ void main() { SetMeshOutputsEXT(4, 2); const uint i = gl_LocalInvocationIndex; const float column = float(gl_WorkGroupID.x + gl_WorkGroupID.z), row = float(gl_WorkGroupID.y); - const vec2 offset = vec2(uintBitsToFloat(pc.s[0]), uintBitsToFloat(pc.s[1])); + const vec2 offset = vec2(uintBitsToFloat(pc.s[8]), uintBitsToFloat(pc.s[9])); if (i < 4) { const vec2 corner = vec2(float(i & 1u), float(i >> 1)); const vec2 top_left = vec2(-1.0 + 0.5 * column, 1.0 - 0.5 * row); diff --git a/runtime/test/glsl/ps_draw.frag b/runtime/test/glsl/ps_draw.frag index d8e29425d..5f2ab1e86 100644 --- a/runtime/test/glsl/ps_draw.frag +++ b/runtime/test/glsl/ps_draw.frag @@ -3,7 +3,7 @@ // d3d12_draw_test. The tests' signatures are two root constants then a root CBV, so in the console's graphics layout // (from s8, two dwords per root view: docs/d3d12_vulkan.md, D3D-448) s10:s11 is the CBV: the console VA of three // floats (a tint), read at heap base + VA. The output is (red * tint.x, tint.y, blue * tint.z, 1). -// d3d12_mesh_test pads its signature onto the runtime's s0 fallback layout and builds this with -DROOT_BASE=0 (s2:s3). +// d3d12_mesh_test uses it as is: its mesh shader reads the same block through the root window (push dword 8 = s16). #extension GL_EXT_buffer_reference : require #extension GL_EXT_buffer_reference_uvec2 : require diff --git a/runtime/test/glsl/ps_image_atomic.frag b/runtime/test/glsl/ps_image_atomic.frag index 97c9b53b5..b23d09ed7 100644 --- a/runtime/test/glsl/ps_image_atomic.frag +++ b/runtime/test/glsl/ps_image_atomic.frag @@ -19,6 +19,6 @@ layout(push_constant) uniform UserData { layout(set = 0, binding = 4, r32ui) uniform uimage2D counters[]; void main() { - const uint before = imageAtomicMax(counters[0], ivec2(gl_FragCoord.xy), pc.s[0]); + const uint before = imageAtomicMax(counters[0], ivec2(gl_FragCoord.xy), pc.s[8]); target = vec4(color.rgb, float(before & 1u)); } diff --git a/runtime/test/root_signature_test.cpp b/runtime/test/root_signature_test.cpp index 169c656f9..c7f726599 100644 --- a/runtime/test/root_signature_test.cpp +++ b/runtime/test/root_signature_test.cpp @@ -492,6 +492,48 @@ int main() { "pack_stage: non-compute graphics stages still refuse spills"); } + // The graphics root window (D3D-448-GRAPHICS): the graphics bind point lays a signature out once with + // Stage::Pixel, root dword k at push dword 8 + k. A vertex, pixel or geometry shader reads SGPR i from push + // dword i, and a mesh shader its s16 + k from push dword 8 + k (tools/rdna2spirv.py, user_data_dword), so every + // stage of a pipeline finds each parameter in the same dword. The mesh window holds 16 dwords, the others 24. + { + const std::vector shapes = [] { + std::vector out(4); + out[0].params = {consts(2), view()}; + out[1].params = {table(), table(), consts(1)}; + out[2].params = {table(), consts(4), view(), table()}; + out[3].params.assign(16, table()); + return out; + }(); + for (std::size_t s = 0; s < shapes.size(); ++s) { + const StagePackResult vs = pack_stage(shapes[s], Stage::Vertex); + const StagePackResult ps = pack_stage(shapes[s], Stage::Pixel); + const StagePackResult gs = pack_stage(shapes[s], Stage::Geometry); + const StagePackResult ms = pack_stage(shapes[s], Stage::Mesh); + check(vs.ok && ps.ok && gs.ok && ms.ok, "graphics window: the shape fits every graphics stage"); + check(vs.first_sgpr == ps.first_sgpr && vs.first_sgpr == gs.first_sgpr, + "graphics window: VS, PS and GS place every parameter in the same SGPR"); + bool same_dword = vs.first_sgpr.size() == ms.first_sgpr.size(); + for (std::size_t i = 0; same_dword && i < vs.first_sgpr.size(); ++i) + same_dword = vs.first_sgpr[i] - 8 == ms.first_sgpr[i] - 16; + check(same_dword, "graphics window: a mesh shader's s16 + k is the pixel shader's s8 + k"); + } + RootSignature twenty; + twenty.params = {consts(20)}; + check(pack_stage(twenty, Stage::Vertex).ok && !pack_stage(twenty, Stage::Mesh).ok, + "graphics window: 20 dwords fit a pixel shader (24 from s8) but not a mesh shader (16 from s16)"); + RootSignature full; + full.params = {consts(24)}; + check(pack_stage(full, Stage::Pixel).ok, "graphics window: 24 constants fill s8..s31 exactly"); + full.params = {consts(23), view()}; + check(!pack_stage(full, Stage::Pixel).ok, "graphics window: a root view past s31 spills"); + RootSignature tables; + tables.params = {table(), table()}; + const StagePackResult t = pack_stage(tables, Stage::Pixel); + check(t.ok && t.first_sgpr == std::vector({8, 9}) && t.end_sgpr == 10, + "graphics window: two tables are s8 and s9, one dword each (pack() gave s[0:1] and s[2:3])"); + } + // pack_stage keeps pack()'s validation. { RootSignature invalid_sig; diff --git a/tests/semantic/test_ms_root_window.py b/tests/semantic/test_ms_root_window.py new file mode 100644 index 000000000..7dada7d5f --- /dev/null +++ b/tests/semantic/test_ms_root_window.py @@ -0,0 +1,116 @@ +"""A mesh shader reads its root arguments from the graphics window (D3D-448 follow-up), executed. + +The console starts a mesh shader's root arguments at s16 and a vertex, pixel or geometry shader's at s8 +(docs/d3d12_vulkan.md section 3.5). Both stages of a mesh pipeline read one push-constant block, which the runtime +fills once in root order with root dword k at push dword 8 + k. So the translator loads a mesh shader's s16..s31 +from push dwords 8..23, its s8..s15 (below its base, not root data) as 0, and s0..s7 from push dwords 0..7 as for +every other stage; a VS, PS or GS keeps reading SGPR i from push dword i. index.json records the window. + +The program (hand-written, no game data) exports, from each of its three vertex threads, a position made of the +user SGPRs it was started with: (s16, s17, s8, s1). It is run by spirv_mini_interp as a mesh shader (MeshEXT) and +with --mesh-compute, with push dword d holding 0x100 + d, so each word names the dword it came from. +""" +import struct +import unittest + +import support +import rdna2spirv +import spirv_mini_interp as si + +THREADS = 64 +OUT = 0x10000 # --mesh-compute: the workgroup's slot, at the output address +PUSH = [0x100 + d for d in range(36)] # s0..s31, then the heap and code bases (unused here) + +PROGRAM = """ +v_mbcnt_lo_u32_b32 v0, -1, 0 +v_mbcnt_hi_u32_b32 v0, -1, v0 +s_mov_b32 m0, 0x1003 +s_sendmsg sendmsg(MSG_GS_ALLOC_REQ) +s_barrier +v_mov_b32_e32 v1, s16 +v_mov_b32_e32 v2, s17 +v_mov_b32_e32 v3, s8 +v_mov_b32_e32 v4, s1 +v_cmpx_gt_u32_e32 3, v0 +exp pos0 v1, v2, v3, v4 done +v_cmpx_eq_u32_e32 0, v0 +v_mov_b32_e32 v9, 0x200400 +exp prim v9, off, off, off done +s_endpgm +""" + +# s16 -> push dword 8, s17 -> 9, s8 -> 0 (below the base), s1 -> push dword 1. +EXPECTED = (0x108, 0x109, 0, 0x101) + + +def _dxbc_psv0(threads, max_vertices, max_primitives): + """A DXBC container with one PSV0 part: a triangle mesh shader (stage 13) and its limits.""" + r = bytearray(48) + struct.pack_into("