From 5fdd6abbc07a0b2e4120944b4e624e3a70ddb76e Mon Sep 17 00:00:00 2001 From: iFeyz <62035583+iFeyz@users.noreply.github.com> Date: Tue, 24 Nov 2026 16:45:13 +0100 Subject: [PATCH 1/6] D3D-448-GRAPHICS: graphics root arguments in the console layout, mesh shaders through a window The graphics bind point laid root arguments out with pack() (from s0, two dwords per table), while the console starts them at s8 for VS/PS/GS with one dword per table (the corpus census, and the retail frame loop reading its tables at s8..s10). Compute already took the console layout (D3D-448); graphics waited because a mesh shader (s16) and a pixel shader (s8) share one push-constant block. - translator: user_data_dword() - a mesh shader loads s16..s31 from push dwords 8..23, s8..s15 as 0, s0..s7 unchanged, so root dword k is push dword 8 + k for every graphics stage; index.json records the window as "root_window" on mesh shaders. VS/PS/GS/CS are unchanged. - runtime: CreateRootSignature lays graphics out with pack_stage(Stage::Vertex) and checks Stage::Mesh (mesh_ok); a table is one dword at both bind points; DispatchMesh with a signature past the 16-dword mesh window invalidates the list; a mesh PSO whose mesh shader has no root_window (translated before) is refused with a log line. - fixtures: the GLSL stand-ins read the root arguments at push dword 8 + k; the M1.5/M2/M3 asm read their tables as one dword from s8. - tests: tests/semantic/test_ms_root_window.py (MeshEXT and --mesh-compute on the CPU interpreter), graphics-window cases in root_signature_test, the two refusals in d3d12_mesh_test. - docs: d3d12_vulkan.md 3.5, design.md user-data row. --- docs/d3d12_vulkan.md | 34 +++++--- docs/design.md | 2 +- runtime/shims/d3d12_com.cpp | 11 ++- runtime/shims/d3d12_com.h | 14 +-- runtime/shims/d3d12_vulkan_device.cpp | 49 +++++++---- runtime/src/root_signature.h | 7 +- runtime/src/shader_library.h | 9 ++ runtime/test/d3d12_mesh_test.cpp | 42 ++++++++- runtime/test/glsl/ms_draw.mesh | 5 +- runtime/test/glsl/ps_depth_write.frag | 4 +- runtime/test/glsl/ps_draw.frag | 4 +- runtime/test/glsl/ps_image_atomic.frag | 2 +- runtime/test/glsl/vs_depth.vert | 6 +- runtime/test/glsl/vs_draw.vert | 4 +- runtime/test/glsl/vs_draw_int.vert | 2 +- runtime/test/root_signature_test.cpp | 42 +++++++++ runtime/test/shaders/ps_m15.asm | 13 +-- runtime/test/shaders/ps_m2.asm | 6 ++ runtime/test/shaders/ps_m2_array.asm | 6 ++ runtime/test/shaders/ps_m2_literal.asm | 4 + runtime/test/shaders/ps_m2_uoff.asm | 8 ++ runtime/test/shaders/ps_m3.asm | 5 ++ runtime/test/shaders/ps_m3_post.asm | 7 ++ runtime/test/shaders/ps_m3_query.asm | 5 ++ runtime/test/shaders/vs_m15.asm | 19 ++-- runtime/test/shaders/vs_m3.asm | 10 +++ tests/semantic/test_ms_root_window.py | 116 +++++++++++++++++++++++++ tools/rdna2spirv.py | 29 ++++++- 28 files changed, 396 insertions(+), 69 deletions(-) create mode 100644 tests/semantic/test_ms_root_window.py diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 8bf2bd38ea..fef8f53cbd 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -794,9 +794,11 @@ 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::Vertex)` for the graphics bind point + (from s8, the graphics root window) 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): @@ -1714,7 +1716,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 | @@ -1754,12 +1757,23 @@ 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 -created, and binding it at the other bind point invalidates the command list (`Close` returns `E_INVALIDARG`). +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::Vertex)`: 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. **The graphics root window (D3D-448 +follow-up):** 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 is created with a log line, and `DispatchMesh` with it invalidates the list. 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 a mesh shader reads in s8..s15 is not measured: +`user_sgpr_census.py --base ms=16` on a corpus lists any read below s16. 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 ca71cf2dfc..6113103732 100644 --- a/docs/design.md +++ b/docs/design.md @@ -281,7 +281,7 @@ How the generated SPIR-V expects the runtime to work: | EXEC | A per-invocation `active` flag. VALU writes and exports are masked by it, scalar ops aren't, and `v_cmpx`, saveexec and EXEC writes update it. Measured on native gfx1100 (8 threads): a `v_mov_b32` under `EXEC=0x0F` leaves the other lanes untouched, `EXEC=0` writes nothing, a store writes only active lanes and `v_cmpx_gt_u32` narrows EXEC (4 of 4 cases); note that AMD clang 20 rejects `v_add_co_u32`, `v_sub_co_u32`, `v_add_co_ci_u32` and `v_mad_u64_u32` for `gfx1100`, `gfx1101` and `gfx1102`, so this toolchain could not reproduce the RX 7600 result below, which leaves open whether those parts support the opcodes and says only that they could not be assembled here. **A pixel is written when its lane was on in EXEC at the last `exp ... vm` the path executed (if it executed one) and at the last export to the color target; a path with no export at all goes by EXEC at the end.** The `vm` half is the RDNA2 ISA reference guide's rule (30-November-2020, §11.1 and §11.2.1: EXEC at the export is the valid mask and the final one sent is used; an `exp null ... vm` carries only the mask). The color half is measured: an export sends data only for the lanes that are on, a later export to the same target replaces the earlier one for the whole wave, and a lane with no data from the last one is not written even when a later `exp null done vm` marks it valid. So `exp mrt0 done` (no `vm`) with every lane on, followed by an EXEC write, still writes every pixel, and EXEC off at that export and back on afterwards writes none. **Measured on RDNA2** (AMD Radeon iGPU, gfx1036, driver 26.10.1) with the pixel-shader oracle of #3061 (`ps_oracle.py`), hand-written ISA as the pixel shader of a real draw (wave64 stage): 18 programs that cut EXEC to half of the target before, between or after `exp mrt0` (with and without `vm` and `done`) and `exp null done vm`; all 18 fit the rule, 6 of them differed from the translator, which until 2026-11-23 discarded by EXEC at the end on a path without `vm` and kept the older export's value (or the output's initial value) for a lane that was off at the last color export. The latches (`%vm_mask`, `%col_mask_mrtN`) are only emitted where a path can write EXEC after a `vm` or color export; everywhere else EXEC at the end is the same mask and the SPIR-V is unchanged. With several color targets a pixel stays when any target got an export from its lane (Vulkan cannot skip one attachment per pixel; not measured, the oracle's pipeline has one target); `mrtz` is not part of the color mask (not measured). **Measuring exports needs `s_waitcnt expcnt(0)` after each one**: an export is asynchronous, and without the wait the hardware takes the data and the EXEC of a later moment (`v_mov v9, A; exp mrt0 v9 done vm; v_mov v9, B` shows B, and an EXEC write right after the export decides which pixels are written, which looks like "EXEC at the end decides"); 64 `s_nop 15` have the same effect as the wait. Checked on the CPU by `tests/semantic/test_export_valid_mask.py` and on the GPU by `runtime/test/shaders/ps_exp_color_mask.asm` (RTX 5080 and the Radeon). VALU scalar lane-mask destinations (`v_cmp_*`, `v_add_co_u32`, `v_add_co_ci_u32`, `v_sub_co_u32`, `v_sub_co_ci_u32`, `v_subrev_co_u32`, `v_subrev_co_ci_u32`, `v_mad_u64_u32`, `v_mad_i64_i32`) write 0 for lanes off in EXEC (`SDST[lane] = EXEC[lane] ? bit : 0`, AMD RDNA2 ISA). Until 2026-11-22 the six `*_co_*` ops and `v_mad_u64_u32`/`v_mad_i64_i32` did not AND the carry/sign bit with `active`, so inactive lanes leaked 1 bits into `VCC`/SGPR masks (`s_bcnt1`, `s_cbranch_vccz`, `v_cndmask_b32`). Checked on the CPU by `tests/semantic/test_carry_exec_mask.py` (wave32, wave64, and `--wave64-emulate` structure), and **hardware-verified on an AMD RX 7600 (gfx1102)** with `v_add_co_u32` and `v_add_co_ci_u32` carrying on every active lane: VCC reads `0xffffffff` with full EXEC, `0x0000ffff` for the mask `0x0000ffff` and `0xaaaaaaaa` for `0xaaaaaaaa` in wave32, `vcc_lo = 0x0000ffff` with `vcc_hi = 0x0000000f` for the same partial mask in wave64, and `0` when EXEC is 0 even with VCC preloaded to all ones; with one carrying lane under mask `0x0000ffff` VCC is `0x00000001`, and `v_sub_co_u32`/`v_subrev_co_u32` with a borrow under that mask give `0x0000ffff` too. The `*_co_ci_` forms, `v_mad_u64_u32` and `v_mad_i64_i32` share the same path but were not measured separately; it is also GPU-verified on native RDNA2 (gfx1036, AMD Radeon Graphics) with partial EXEC masks (inactive lanes write 0 in sdst for `v_add_co_u32` and `v_mad_u64_u32`). In a shader that writes EXEC, a run of two or more lane-local VALU instructions (no cross-lane read, no lane-mask, SCC, carry or EXEC write) sits inside one `if (active)` and writes its VGPRs directly, instead of an `OpSelect(active, new, old)` per write: the same result, but the driver predicates the run with its own EXEC instead of keeping both values live. On an RX 5700 XT (RADV) a pixel shader with 8 divergent if/else regions of `v_fma`/`v_mul`/`v_add`/`v_min`/`v_max` went from 0.85 ms to 0.69 ms (the same program as native SPIR-V: 0.65 ms). Not under `--wave64-emulate`. Checked by `tests/semantic/test_active_runs.py`. | | `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). | -| 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 follow-up, `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). | | 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, 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`). | | VOP3 opcode 0x140 | LLVM disassembly of the synthetic words in `tests/test_rdna2_decode.py` gives `v_mad_legacy_f32` on gfx1010 through gfx1013 and `v_fma_legacy_f32` on gfx1030. The direct decoder defaults to gfx1013, matching the disassembler CLI. Checked by the unit test `test_vop3_legacy_mad_fma_target` in `tests/test_rdna2_decode.py`, which decodes the same words directly and runs the Python disassembler backend for gfx1013 and gfx1030. | diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 4f4904e965..f67254bc5c 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp @@ -486,6 +486,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 (!(viewport_.width > 0) || !(viewport_.height > 0)) return; // as record_draw: nothing is rasterized leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, {}, 0, viewport_, scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; @@ -1242,9 +1247,9 @@ class GraphicsCommandList final if (!p) return; const std::uint32_t first = first_dword(state, *p); write(state, first, std::uint32_t(va)); - // In the compute layout 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. The graphics layout gives every address two dwords. - if (!(is_compute(state) && type == rt::rootsig::ParamType::Table)) write(state, first + 1, std::uint32_t(va >> 32)); + // In the console layout of both bind points 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 (D3D-448). + if (type != rt::rootsig::ParamType::Table) write(state, first + 1, std::uint32_t(va >> 32)); } D3D12_COMMAND_LIST_TYPE type_; diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index c0ff3cf8da..f389e39cf2 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h @@ -381,21 +381,25 @@ std::unique_ptr create_window_presenter(ID3D12Device* device, H // Where each root parameter's arguments go in the 32 user-data dwords the translated shaders read (docs section // 3.5). Built when a Vulkan device creates the root signature. A signature can be bound at either bind point, so it -// carries both layouts: -// - graphics: rt::rootsig::pack() (from s0, two dwords per table; D3D-448 keeps it until per-stage user-data windows -// exist, because a mesh shader (s16) and a pixel shader (s8) share one push-constant block); -// - compute: rt::rootsig::pack_stage(Stage::Compute), the measured console layout (from s4, one dword per table). +// carries both layouts, each the console layout rt::rootsig::pack_stage() gives (one dword per table): +// - graphics: Stage::Vertex, from s8. A vertex, pixel or geometry shader reads SGPR i from push dword i, so root +// dword k is push dword 8 + k for all three. 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 follow-up); +// - compute: Stage::Compute, from s4 (measured, D3D-448). // A layout that refuses the signature is marked unusable; binding the signature there invalidates the list. 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 }; std::vector parameters; std::uint32_t flags = 0; // D3D12_ROOT_SIGNATURE_FLAGS from the serialized blob bool graphics_ok = true; + bool mesh_ok = true; // the graphics layout also fits a mesh shader's 16-dword window bool compute_ok = true; }; diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 97ed334a11..d8b18a11da 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -3558,6 +3558,15 @@ class PipelineState final std::fprintf(stderr, "[rt] d3d12: %s\n", refusal.c_str()); return E_NOTIMPL; } + // 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) { @@ -6020,40 +6029,42 @@ 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()); return E_INVALIDARG; } - // Two layouts, one per bind point (RootArgumentLayout): graphics keeps pack(), compute takes the console - // layout pack_stage() measured (D3D-448). The signature is refused only when neither can place it. - const rt::rootsig::PackResult packed = rt::rootsig::pack(parsed.sig); + // One layout per bind point (RootArgumentLayout), both the console's (D3D-448): graphics from s8, where a + // vertex, pixel or geometry shader reads SGPR i from push dword i, and compute from s4. A mesh shader reads + // the same graphics window from its s16, so its 16-dword window only needs checking. The signature is + // refused only when neither bind point can place it. + const rt::rootsig::StagePackResult graphics = rt::rootsig::pack_stage(parsed.sig, rt::rootsig::Stage::Vertex); + 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 (!packed.ok && !compute.ok) { - std::fprintf(stderr, "[rt] d3d12: root signature refused: %s (compute: %s)\n", packed.error.c_str(), + if (!graphics.ok && !compute.ok) { + std::fprintf(stderr, "[rt] d3d12: root signature refused: %s (compute: %s)\n", graphics.error.c_str(), compute.error.c_str()); return E_INVALIDARG; } - if (!packed.ok) - std::fprintf(stderr, "[rt] d3d12: root signature usable for compute only: %s\n", packed.error.c_str()); + if (!graphics.ok) + std::fprintf(stderr, "[rt] d3d12: root signature usable for compute only: %s\n", graphics.error.c_str()); + else if (!mesh.ok) + std::fprintf(stderr, "[rt] d3d12: root signature too large for a mesh shader's root window: %s\n", + mesh.error.c_str()); 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_ok = packed.ok; + layout.graphics_ok = graphics.ok; + layout.mesh_ok = graphics.ok && mesh.ok; layout.compute_ok = compute.ok; - for (std::size_t i = 0; i < parsed.sig.params.size(); ++i) { - std::uint32_t first = UINT32_MAX; - for (std::uint32_t d = 0; packed.ok && d < packed.dword_count; ++d) - if (packed.provenance[d].param_index == i) { - first = d; - break; - } - layout.parameters.push_back({parsed.sig.params[i].type, first, parsed.sig.params[i].num_values, + for (std::size_t i = 0; i < parsed.sig.params.size(); ++i) + layout.parameters.push_back({parsed.sig.params[i].type, + graphics.ok ? graphics.first_sgpr[i] : rt::rootsig::kNoSgpr, + parsed.sig.params[i].num_values, compute.ok ? compute.first_sgpr[i] : rt::rootsig::kNoSgpr}); - } register_static_samplers(parsed.sig); auto* signature = new RootSignature( std::move(layout), leonida::sha256_hex({static_cast(blob), size})); diff --git a/runtime/src/root_signature.h b/runtime/src/root_signature.h index 3769629400..d1b8e38b44 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::Vertex; 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; that array is not modelled, so such a signature is refused, never truncated. enum class Stage : uint32_t { Vertex = 0, Pixel = 1, Geometry = 2, Compute = 3, Mesh = 4 }; diff --git a/runtime/src/shader_library.h b/runtime/src/shader_library.h index 9f752317ee..9cfb902ef2 100644 --- a/runtime/src/shader_library.h +++ b/runtime/src/shader_library.h @@ -827,6 +827,9 @@ struct ShaderEntry { // subgroup size. Empty for a native translation. std::string wave64_exchange; 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 @@ -1026,6 +1029,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* s = meta.find("input_signature"); s && s->kind == Json::Kind::array) { for (const Json& item : s->array) { SignatureElement element; diff --git a/runtime/test/d3d12_mesh_test.cpp b/runtime/test/d3d12_mesh_test.cpp index 1b99b8d0b1..b4bf9adce9 100644 --- a/runtime/test/d3d12_mesh_test.cpp +++ b/runtime/test/d3d12_mesh_test.cpp @@ -106,19 +106,26 @@ int main() { 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}); const auto ms_container = shader_container(ms_program), ps_container = shader_container(ps_program); + const auto ms_old_container = shader_container(ms_old_program); 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) @@ -152,7 +159,8 @@ int main() { return true; }; const auto root_signature = [&](ID3D12Device* device, Ref& root) { - // [0] two constants (s0, s1: offset), [1] a root CBV (s2:s3: tint), as in d3d12_draw_test. + // [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}; @@ -165,7 +173,20 @@ int main() { 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). + const auto wide_root_signature = [&](ID3D12Device* device, Ref& root) { + D3D12_ROOT_PARAMETER param{}; + param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; + param.Constants = {0, 0, 20}; + 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()}; @@ -211,6 +232,14 @@ int main() { 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; @@ -399,6 +428,17 @@ int main() { 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); + } EXPECT(SUCCEEDED(queue->Signal(fence.p, 2)) && SUCCEEDED(fence->SetEventOnCompletion(2, nullptr))); // D3D-291: pipeline statistics around mesh draws. Query 0 holds a vertex draw, two mesh draws and another diff --git a/runtime/test/glsl/ms_draw.mesh b/runtime/test/glsl/ms_draw.mesh index 8bfd21785d..e66b04bb6f 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_depth_write.frag b/runtime/test/glsl/ps_depth_write.frag index 959d1dd9de..b176e08ccf 100644 --- a/runtime/test/glsl/ps_depth_write.frag +++ b/runtime/test/glsl/ps_depth_write.frag @@ -1,5 +1,5 @@ #version 460 -// A pixel shader that writes its own depth (SV_Depth), for d3d12_depth24_draw_test: s2 is the depth it writes. On a +// A pixel shader that writes its own depth (SV_Depth), for d3d12_depth24_draw_test: s10 is the depth it writes. On a // D24 target the value is stored quantised to 24 bits, and D3D applies no depth bias to it. layout(push_constant) uniform UserData { @@ -9,5 +9,5 @@ layout(push_constant) uniform UserData { } pc; void main() { - gl_FragDepth = uintBitsToFloat(pc.s[2]); + gl_FragDepth = uintBitsToFloat(pc.s[10]); } diff --git a/runtime/test/glsl/ps_draw.frag b/runtime/test/glsl/ps_draw.frag index 2361330f41..b356cf7433 100644 --- a/runtime/test/glsl/ps_draw.frag +++ b/runtime/test/glsl/ps_draw.frag @@ -1,6 +1,6 @@ #version 460 // A pixel shader written to the translator's runtime contract, standing in for a translated one in -// d3d12_draw_test. s2:s3 is a root CBV: the console VA of three floats (a tint), read at heap base + VA. The output +// d3d12_draw_test. s10:s11 is a root 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). #extension GL_EXT_buffer_reference : require #extension GL_EXT_buffer_reference_uvec2 : require @@ -25,6 +25,6 @@ uvec2 add64(uvec2 a, uvec2 b) { } void main() { - Floats tint = Floats(add64(pc.heap_base, uvec2(pc.s[2], pc.s[3]))); + Floats tint = Floats(add64(pc.heap_base, uvec2(pc.s[10], pc.s[11]))); target = vec4(color.r * tint.f[0], tint.f[1], color.b * tint.f[2], 1.0); } diff --git a/runtime/test/glsl/ps_image_atomic.frag b/runtime/test/glsl/ps_image_atomic.frag index 97c9b53b5a..b23d09ed75 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/glsl/vs_depth.vert b/runtime/test/glsl/vs_depth.vert index de1d6ee40d..1ade14c156 100644 --- a/runtime/test/glsl/vs_depth.vert +++ b/runtime/test/glsl/vs_depth.vert @@ -1,7 +1,7 @@ #version 460 // A full-screen triangle at a chosen depth, written to the translator's runtime contract (docs/design.md, "User -// data"), for d3d12_depth24_draw_test: s0 is the depth at the centre and s1 how much it changes per unit of clip-space -// x, so s1 = 0 gives a flat triangle and anything else a constant slope across the target. +// data"), for d3d12_depth24_draw_test: s8 is the depth at the centre and s9 how much it changes per unit of clip-space +// x, so s9 = 0 gives a flat triangle and anything else a constant slope across the target. layout(push_constant) uniform UserData { uint s[32]; @@ -12,5 +12,5 @@ layout(push_constant) uniform UserData { void main() { const vec2 corner = vec2(float(gl_VertexIndex & 2), float((gl_VertexIndex << 1) & 2)); const vec2 xy = corner * 2.0 - 1.0; - gl_Position = vec4(xy, uintBitsToFloat(pc.s[0]) + uintBitsToFloat(pc.s[1]) * xy.x, 1.0); + gl_Position = vec4(xy, uintBitsToFloat(pc.s[8]) + uintBitsToFloat(pc.s[9]) * xy.x, 1.0); } diff --git a/runtime/test/glsl/vs_draw.vert b/runtime/test/glsl/vs_draw.vert index 3cde02a50e..a76402e76b 100644 --- a/runtime/test/glsl/vs_draw.vert +++ b/runtime/test/glsl/vs_draw.vert @@ -2,7 +2,7 @@ // A vertex shader written to the translator's runtime contract (docs/design.md, "Vertex fetch" and "User data"), // standing in for a translated one in d3d12_draw_test. Inputs sit at location = D3D input register: COLOR is // register 0 and POSITION register 1 here, the reverse of the input layout's order, so the test shows that the -// layout is matched by semantic. s0, s1 (root constants) move the vertex in clip space. +// layout is matched by semantic. s8, s9 (root constants) move the vertex in clip space. layout(location = 0) in vec4 color; // COLOR0, input register 0 layout(location = 1) in vec4 position; // POSITION0, input register 1 @@ -16,7 +16,7 @@ layout(push_constant) uniform UserData { } pc; void main() { - const vec2 offset = vec2(uintBitsToFloat(pc.s[0]), uintBitsToFloat(pc.s[1])); + const vec2 offset = vec2(uintBitsToFloat(pc.s[8]), uintBitsToFloat(pc.s[9])); gl_Position = vec4(position.xy + offset, 0.0, 1.0); out_color = color; } diff --git a/runtime/test/glsl/vs_draw_int.vert b/runtime/test/glsl/vs_draw_int.vert index ea57824692..cb4f5ca73b 100644 --- a/runtime/test/glsl/vs_draw_int.vert +++ b/runtime/test/glsl/vs_draw_int.vert @@ -14,7 +14,7 @@ layout(push_constant) uniform UserData { } pc; void main() { - const vec2 offset = vec2(uintBitsToFloat(pc.s[0]), uintBitsToFloat(pc.s[1])); + const vec2 offset = vec2(uintBitsToFloat(pc.s[8]), uintBitsToFloat(pc.s[9])); gl_Position = vec4(position.xy + offset, 0.0, 1.0); out_color = vec4(color); } diff --git a/runtime/test/root_signature_test.cpp b/runtime/test/root_signature_test.cpp index 831739a8c3..9c3d6c69fa 100644 --- a/runtime/test/root_signature_test.cpp +++ b/runtime/test/root_signature_test.cpp @@ -449,6 +449,48 @@ int main() { check(!pack_stage(tables, Stage::Mesh).ok, "pack_stage: a 17th table spills on mesh"); } + // The graphics root window (D3D-448 follow-up): the graphics bind point lays a signature out once with + // Stage::Vertex, 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 zero; diff --git a/runtime/test/shaders/ps_m15.asm b/runtime/test/shaders/ps_m15.asm index 4c924a72fc..9baeecc53d 100644 --- a/runtime/test/shaders/ps_m15.asm +++ b/runtime/test/shaders/ps_m15.asm @@ -5,7 +5,8 @@ ; code base, dwords 34-35, is the console VA of the FSSM program the layer uploaded). The table of four floats is at ; blob offset 0x100: 0x100 - 4 = 0xfc. build.py writes that blob (fssm-at / fssm-data below) next to the SPIR-V, and ; adds its SHA-256 to the index, as the translator's --fssm-dir does for the game's programs. -; scale = (0.5, 2.0, 1.0, 1.0) s4 = bias, a float from the root constants (s2, s3 are the VS offset) +; scale = (0.5, 2.0, 1.0, 1.0) s11 = bias, a float from the root constants (s9, s10 are the VS offset; +; console root layout from s8, D3D-448) ; mrt0 = (r * 0.5 + bias, g * 2.0 + bias, b * 1.0 + bias, a * 1.0) ; The test draws colour (0.25, 0.5, 0.75, 1) with bias 0 and 0.125, and colour (1, 0, 0.5, 1) with bias 0.25. Every ; value is a short binary fraction, so the float render target holds the exact result, bit for bit: @@ -18,7 +19,7 @@ s_getpc_b64 s[6:7] s_add_u32 s6, s6, 0xfc s_addc_u32 s7, s7, 0 -s_load_dwordx4 s[8:11], s[6:7], 0x0 +s_load_dwordx4 s[12:15], s[6:7], 0x0 v_interp_p1_f32_e32 v2, v0, attr0.x v_interp_p2_f32_e32 v2, v1, attr0.x v_interp_p1_f32_e32 v3, v0, attr0.y @@ -28,9 +29,9 @@ v_interp_p2_f32_e32 v4, v1, attr0.z v_interp_p1_f32_e32 v5, v0, attr0.w v_interp_p2_f32_e32 v5, v1, attr0.w s_waitcnt lgkmcnt(0) -v_fma_f32 v2, v2, s8, s4 -v_fma_f32 v3, v3, s9, s4 -v_fma_f32 v4, v4, s10, s4 -v_mul_f32_e32 v5, s11, v5 +v_fma_f32 v2, v2, s12, s11 +v_fma_f32 v3, v3, s13, s11 +v_fma_f32 v4, v4, s14, s11 +v_mul_f32_e32 v5, s15, v5 exp mrt0 v2, v3, v4, v5 done vm s_endpgm diff --git a/runtime/test/shaders/ps_m2.asm b/runtime/test/shaders/ps_m2.asm index 0f23cefa3d..c19b075501 100644 --- a/runtime/test/shaders/ps_m2.asm +++ b/runtime/test/shaders/ps_m2.asm @@ -7,6 +7,12 @@ ; attr0.xy = (u, v), 0..1 over the quad. mrt0 = the texel under the pixel. ; fssm-at: 0x10 ; fssm-data: 0x324d5350 0x20535020 +; Console root layout (D3D-448): root arguments from s8, a descriptor table is one dword (its 32-bit VA; the heaps +; live below 4 GiB, so the high half is 0). The first lines rebuild the registers the program below was written for. +s_mov_b32 s0, s8 +s_mov_b32 s2, s9 +s_mov_b32 s1, 0 +s_mov_b32 s3, 0 s_load_dwordx8 s[8:15], s[0:1], 0x0 s_load_dwordx4 s[16:19], s[2:3], 0x0 s_waitcnt lgkmcnt(0) diff --git a/runtime/test/shaders/ps_m2_array.asm b/runtime/test/shaders/ps_m2_array.asm index 96260bd0a2..2d9723bd7b 100644 --- a/runtime/test/shaders/ps_m2_array.asm +++ b/runtime/test/shaders/ps_m2_array.asm @@ -4,6 +4,12 @@ ; attr0.xy = (u, v), 0..1 over the quad. mrt0 = the texel under the pixel. ; fssm-at: 0x10 ; fssm-data: 0x324d5350 0x59525241 +; Console root layout (D3D-448): root arguments from s8, a descriptor table is one dword (its 32-bit VA; the heaps +; live below 4 GiB, so the high half is 0). The first lines rebuild the registers the program below was written for. +s_mov_b32 s0, s8 +s_mov_b32 s2, s9 +s_mov_b32 s1, 0 +s_mov_b32 s3, 0 s_load_dwordx8 s[8:15], s[0:1], 0x0 s_load_dwordx4 s[16:19], s[2:3], 0x0 s_waitcnt lgkmcnt(0) diff --git a/runtime/test/shaders/ps_m2_literal.asm b/runtime/test/shaders/ps_m2_literal.asm index f77ef197f4..6268c292ff 100644 --- a/runtime/test/shaders/ps_m2_literal.asm +++ b/runtime/test/shaders/ps_m2_literal.asm @@ -5,6 +5,10 @@ ; s[0:1] = console VA of the SRV table's first slot (the T#). attr0.xy = (u, v). mrt0 = the sampled texel. ; fssm-at: 0x10 ; fssm-data: 0x324d5350 0x4c544c20 +; Console root layout (D3D-448): root arguments from s8, a descriptor table is one dword (its 32-bit VA; the heaps +; live below 4 GiB, so the high half is 0). The first lines rebuild the registers the program below was written for. +s_mov_b32 s0, s8 +s_mov_b32 s1, 0 s_load_dwordx8 s[8:15], s[0:1], 0x0 s_movk_i32 s16, 0x92 s_mov_b32 s17, 0xc00000 diff --git a/runtime/test/shaders/ps_m2_uoff.asm b/runtime/test/shaders/ps_m2_uoff.asm index 1c96f50d55..59f7be679b 100644 --- a/runtime/test/shaders/ps_m2_uoff.asm +++ b/runtime/test/shaders/ps_m2_uoff.asm @@ -6,6 +6,14 @@ ; the descriptor index and byte stride, respectively. ; fssm-at: 0x10 ; fssm-data: 0x324d5350 0x4f465520 +; Console root layout (D3D-448): root arguments from s8, a descriptor table is one dword (its 32-bit VA; the heaps +; live below 4 GiB, so the high half is 0). The first lines rebuild the registers the program below was written for. +s_mov_b32 s0, s8 +s_mov_b32 s2, s9 +s_mov_b32 s4, s10 +s_mov_b32 s5, s11 +s_mov_b32 s1, 0 +s_mov_b32 s3, 0 s_mul_i32 s20, s4, s5 s_load_dwordx8 s[8:15], s[0:1], s20 s_load_dwordx4 s[16:19], s[2:3], 0x0 diff --git a/runtime/test/shaders/ps_m3.asm b/runtime/test/shaders/ps_m3.asm index be2a59e2db..4153ce7981 100644 --- a/runtime/test/shaders/ps_m3.asm +++ b/runtime/test/shaders/ps_m3.asm @@ -4,6 +4,11 @@ ; mrt1 = (c1 + c2) an R32_FLOAT target: one float add, so the CPU gets the same bits ; fssm-at: 0x10 ; fssm-data: 0x33535350 0x20535020 +; Console root layout (D3D-448): root arguments from s8, a descriptor table is one dword (its 32-bit VA; the heaps +; live below 4 GiB, so the high half is 0). The first lines rebuild the registers the program below was written for. +s_mov_b32 s5, s13 +s_mov_b32 s6, s14 +s_mov_b32 s7, s15 v_mov_b32_e32 v2, s5 v_mov_b32_e32 v3, s6 v_mov_b32_e32 v4, s7 diff --git a/runtime/test/shaders/ps_m3_post.asm b/runtime/test/shaders/ps_m3_post.asm index 8488030587..d4e093b4a2 100644 --- a/runtime/test/shaders/ps_m3_post.asm +++ b/runtime/test/shaders/ps_m3_post.asm @@ -6,6 +6,13 @@ ; attr0.xy = (u, v), 0..1 over the target. mrt0 = (texel.b, texel.g, texel.r, s4) ; fssm-at: 0x10 ; fssm-data: 0x33505350 0x20535020 +; Console root layout (D3D-448): root arguments from s8, a descriptor table is one dword (its 32-bit VA; the heaps +; live below 4 GiB, so the high half is 0). The first lines rebuild the registers the program below was written for. +s_mov_b32 s0, s8 +s_mov_b32 s2, s9 +s_mov_b32 s4, s10 +s_mov_b32 s1, 0 +s_mov_b32 s3, 0 s_load_dwordx8 s[8:15], s[0:1], 0x0 s_load_dwordx4 s[16:19], s[2:3], 0x0 s_waitcnt lgkmcnt(0) diff --git a/runtime/test/shaders/ps_m3_query.asm b/runtime/test/shaders/ps_m3_query.asm index 0693cf5e23..636d89fc88 100644 --- a/runtime/test/shaders/ps_m3_query.asm +++ b/runtime/test/shaders/ps_m3_query.asm @@ -4,6 +4,11 @@ ; Keep the probe single-output so its count can be compared exactly with the CPU's visible rectangle area. ; fssm-at: 0x10 ; fssm-data: 0x33515350 0x20535020 +; Console root layout (D3D-448): root arguments from s8, a descriptor table is one dword (its 32-bit VA; the heaps +; live below 4 GiB, so the high half is 0). The first lines rebuild the registers the program below was written for. +s_mov_b32 s5, s13 +s_mov_b32 s6, s14 +s_mov_b32 s7, s15 v_mov_b32_e32 v2, s5 v_mov_b32_e32 v3, s6 v_mov_b32_e32 v4, s7 diff --git a/runtime/test/shaders/vs_m15.asm b/runtime/test/shaders/vs_m15.asm index 3ac449b0b4..2f4946f639 100644 --- a/runtime/test/shaders/vs_m15.asm +++ b/runtime/test/shaders/vs_m15.asm @@ -3,23 +3,26 @@ ; a D3D12 graphics PSO found by the SHA-256 of its FSSM program). ; ; User data, from the root signature [0] descriptor table, [1] four 32-bit constants (docs/d3d12_vulkan.md 3.5): -; s[0:1] = console VA of the table's first slot: the 16 bytes of a V# (base VA, stride 32, 9 records) -; s2, s3 = the draw's clip-space offset (x, y) as floats; s4, s5 = used by the pixel shader (s4: bias) +; s8 = console VA of the table's first slot: the 16 bytes of a V# (base VA, stride 32, 9 records). In the console +; root layout (D3D-448) a table is one dword, its 32-bit VA (the heaps live below 4 GiB): s[0:1] = (s8, 0) +; s9, s10 = the draw's clip-space offset (x, y) as floats; s11, s12 = used by the pixel shader (s11: bias) ; v0 = VertexIndex. The draw has BaseVertex 0 and BaseInstance 0, so v0 is the same with and without the translator's ; zero-based ids (PR 436): both give 0, 1, 2, ... for DrawInstanced(n, 1, 0, 0). A non-zero StartVertexLocation would ; make the two differ (hardware: zero-based, base in an SGPR), so the test never uses one. ; Vertex record, 32 bytes: position xyzw at 0, colour rgba at 16 (both float). -; pos0 = (position.x + s2, position.y + s3, position.z, position.w) +; pos0 = (position.x + s9, position.y + s10, position.z, position.w) ; param0 = colour ; fssm-at: 0x10 ; fssm-data: 0x35314d56 0x20535620 -s_load_dwordx4 s[8:11], s[0:1], 0x0 +s_mov_b32 s0, s8 +s_mov_b32 s1, 0 +s_load_dwordx4 s[16:19], s[0:1], 0x0 s_waitcnt lgkmcnt(0) -buffer_load_dwordx4 v[4:7], v0, s[8:11], 0 idxen -buffer_load_dwordx4 v[8:11], v0, s[8:11], 0 idxen offset:16 +buffer_load_dwordx4 v[4:7], v0, s[16:19], 0 idxen +buffer_load_dwordx4 v[8:11], v0, s[16:19], 0 idxen offset:16 s_waitcnt vmcnt(0) -v_add_f32_e32 v4, s2, v4 -v_add_f32_e32 v5, s3, v5 +v_add_f32_e32 v4, s9, v4 +v_add_f32_e32 v5, s10, v5 exp pos0 v4, v5, v6, v7 done exp param0 v8, v9, v10, v11 s_endpgm diff --git a/runtime/test/shaders/vs_m3.asm b/runtime/test/shaders/vs_m3.asm index 3bcc2278e7..6768e06ebe 100644 --- a/runtime/test/shaders/vs_m3.asm +++ b/runtime/test/shaders/vs_m3.asm @@ -8,6 +8,16 @@ ; The corners are multiples of 1/32 and the depths are dyadic, so every operation here is exact however it is fused. ; fssm-at: 0x10 ; fssm-data: 0x33534d56 0x20535620 +; Console root layout (D3D-448): root arguments from s8, a descriptor table is one dword (its 32-bit VA; the heaps +; live below 4 GiB, so the high half is 0). The first lines rebuild the registers the program below was written for. +s_mov_b32 s0, s8 +s_mov_b32 s1, s9 +s_mov_b32 s2, s10 +s_mov_b32 s3, s11 +s_mov_b32 s4, s12 +s_mov_b32 s5, s13 +s_mov_b32 s6, s14 +s_mov_b32 s7, s15 v_and_b32_e32 v1, 1, v0 v_and_b32_e32 v2, 2, v0 v_cvt_f32_u32_e32 v1, v1 diff --git a/tests/semantic/test_ms_root_window.py b/tests/semantic/test_ms_root_window.py new file mode 100644 index 0000000000..7dada7d5f5 --- /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(" Date: Tue, 24 Nov 2026 17:52:02 +0100 Subject: [PATCH 2/6] D3D-448-GRAPHICS: a graphics signature past the console window keeps the PC layout instead of being refused Measured by madhyn on the retail title with this pull request (#974 review): one of its start-up root signatures is past the console's inline graphics window (24 dwords from s8) and the compute one (28 from s4), so CreateRootSignature refused it outright and the title stopped with an unhandled exception before the frame loop it reached without this change. On the console such a signature goes to the argument block behind s[0:1], modelled for compute only (ROOT-SIG-SPILL-MODEL, #1009). Until it is for graphics, the graphics bind point keeps pack()'s layout for it (from s0, two dwords per table), what the layer did before the console layout, so no signature is worse off than on main. The new RootArgumentLayout::graphics_console tells address() which layout the table is in; a mesh shader still refuses such a signature. d3d12_mesh_test: a 30-constant signature is created and DispatchMesh with it invalidates the list. --- docs/d3d12_vulkan.md | 6 +++- runtime/shims/d3d12_com.cpp | 6 ++-- runtime/shims/d3d12_com.h | 3 ++ runtime/shims/d3d12_vulkan_device.cpp | 40 +++++++++++++++++++++------ runtime/test/d3d12_mesh_test.cpp | 16 +++++++++-- 5 files changed, 56 insertions(+), 15 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index fef8f53cbd..fcb89172fa 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -1767,7 +1767,11 @@ argument in the same dword. A mesh shader's s8..s15 are below its base and read 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 is created with a log line, and `DispatchMesh` with it invalidates the list. Live evidence for +not the mesh window is created with a log line, and `DispatchMesh` with it invalidates the list. A signature past +the graphics window (more than 24 dwords from s8) goes, on the console, to the argument block behind `s[0:1]`, which +is modelled for compute only (ROOT-SIG-SPILL-MODEL); until it is for graphics, such a signature keeps `pack()`'s +layout at the graphics bind point (from s0, two dwords per table, `RootArgumentLayout::graphics_console` false) rather +than being refused: the retail title creates one at start-up (29..32 dwords) and stops if it is refused. 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 a mesh shader reads in s8..s15 is not measured: diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index f67254bc5c..7fcfceebbb 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp @@ -1248,8 +1248,10 @@ class GraphicsCommandList final const std::uint32_t first = first_dword(state, *p); write(state, first, std::uint32_t(va)); // In the console layout of both bind points 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 (D3D-448). - if (type != rt::rootsig::ParamType::Table) write(state, first + 1, std::uint32_t(va >> 32)); + // descriptor (the heaps live below 4 GiB); a root view keeps both halves (D3D-448). A graphics signature + // past the console window keeps pack()'s layout, where every address takes two dwords. + const bool console = is_compute(state) || state.layout->graphics_console; + if (!(console && type == rt::rootsig::ParamType::Table)) write(state, first + 1, std::uint32_t(va >> 32)); } D3D12_COMMAND_LIST_TYPE type_; diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index f389e39cf2..fd9e8d79d9 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h @@ -399,6 +399,9 @@ struct RootArgumentLayout { std::vector parameters; std::uint32_t flags = 0; // D3D12_ROOT_SIGNATURE_FLAGS from the serialized blob bool graphics_ok = true; + // The graphics layout is the console's (pack_stage, one dword per table). False for a signature past its 24-dword + // window, which keeps pack()'s layout (from s0, two dwords per table) until the argument block is modelled. + bool graphics_console = true; bool mesh_ok = true; // the graphics layout also fits a mesh shader's 16-dword window bool compute_ok = true; }; diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index d8b18a11da..71e0871542 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -6038,18 +6038,30 @@ class Device final } // One layout per bind point (RootArgumentLayout), both the console's (D3D-448): graphics from s8, where a // vertex, pixel or geometry shader reads SGPR i from push dword i, and compute from s4. A mesh shader reads - // the same graphics window from its s16, so its 16-dword window only needs checking. The signature is - // refused only when neither bind point can place it. + // the same graphics window from its s16, so its 16-dword window only needs checking. + // + // A signature past the console's inline graphics window (24 dwords from s8) goes, on the console, to the + // argument block behind s[0:1], which is not modelled for graphics yet (ROOT-SIG-SPILL-MODEL does compute). + // Such a signature keeps pack()'s layout at the graphics bind point (from s0, two dwords per table), what + // the layer used before the console layout: refusing it instead stops the retail title at start-up (#974 + // review, a 29..32-dword startup signature). The signature is refused only when no layout can place it. const rt::rootsig::StagePackResult graphics = rt::rootsig::pack_stage(parsed.sig, rt::rootsig::Stage::Vertex); 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 (!graphics.ok && !compute.ok) { + const rt::rootsig::PackResult fallback = graphics.ok ? rt::rootsig::PackResult{} : rt::rootsig::pack(parsed.sig); + const bool graphics_usable = graphics.ok || fallback.ok; + if (!graphics_usable && !compute.ok) { std::fprintf(stderr, "[rt] d3d12: root signature refused: %s (compute: %s)\n", graphics.error.c_str(), compute.error.c_str()); return E_INVALIDARG; } - if (!graphics.ok) + if (!graphics_usable) std::fprintf(stderr, "[rt] d3d12: root signature usable for compute only: %s\n", graphics.error.c_str()); + else if (!graphics.ok) + std::fprintf(stderr, + "[rt] d3d12: root signature past the console's graphics window (%s): graphics keeps the PC " + "layout from s0 until the argument block behind s[0:1] is modelled\n", + graphics.error.c_str()); else if (!mesh.ok) std::fprintf(stderr, "[rt] d3d12: root signature too large for a mesh shader's root window: %s\n", mesh.error.c_str()); @@ -6057,14 +6069,24 @@ class Device final 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_ok = graphics.ok; + layout.graphics_ok = graphics_usable; + layout.graphics_console = graphics.ok; layout.mesh_ok = graphics.ok && mesh.ok; layout.compute_ok = compute.ok; - for (std::size_t i = 0; i < parsed.sig.params.size(); ++i) - layout.parameters.push_back({parsed.sig.params[i].type, - graphics.ok ? graphics.first_sgpr[i] : rt::rootsig::kNoSgpr, - parsed.sig.params[i].num_values, + for (std::size_t i = 0; i < parsed.sig.params.size(); ++i) { + std::uint32_t first = rt::rootsig::kNoSgpr; + if (graphics.ok) { + first = graphics.first_sgpr[i]; + } else if (fallback.ok) { + for (std::uint32_t d = 0; d < fallback.dword_count; ++d) + if (fallback.provenance[d].param_index == i) { + first = d; + break; + } + } + layout.parameters.push_back({parsed.sig.params[i].type, first, parsed.sig.params[i].num_values, compute.ok ? compute.first_sgpr[i] : rt::rootsig::kNoSgpr}); + } register_static_samplers(parsed.sig); auto* signature = new RootSignature( std::move(layout), leonida::sha256_hex({static_cast(blob), size})); diff --git a/runtime/test/d3d12_mesh_test.cpp b/runtime/test/d3d12_mesh_test.cpp index b4bf9adce9..92b7f387d0 100644 --- a/runtime/test/d3d12_mesh_test.cpp +++ b/runtime/test/d3d12_mesh_test.cpp @@ -173,11 +173,12 @@ int main() { 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). - const auto wide_root_signature = [&](ID3D12Device* device, Ref& root) { + // 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, 20}; + 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}; @@ -438,6 +439,15 @@ int main() { list->RSSetViewports(1, &viewport); list6->DispatchMesh(1, 1, 1); EXPECT(list->Close() == E_INVALIDARG); + // Past every console window: still created, the graphics bind point keeping the PC layout from s0 until + // the argument block behind s[0:1] is modelled (refusing it stopped the retail title at start-up). + Ref huge; + EXPECT(wide_root_signature(device.p, huge, 30)); + 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 still cannot read it } EXPECT(SUCCEEDED(queue->Signal(fence.p, 2)) && SUCCEEDED(fence->SetEventOnCompletion(2, nullptr))); From 161314c2660db1d6b240d3d03eebc4aa8736e391 Mon Sep 17 00:00:00 2001 From: iFeyz <62035583+iFeyz@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:13:32 +0100 Subject: [PATCH 3/6] D3D-448-GRAPHICS: docs - what mesh shaders read below s16, counted on the corpus (#14 comment 7138532347): 3 of 9,282, one 32-bit constant each --- docs/d3d12_vulkan.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index fcb89172fa..b216cd791d 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -1774,8 +1774,10 @@ layout at the graphics bind point (from s0, two dwords per table, `RootArgumentL than being refused: the retail title creates one at start-up (29..32 dwords) and stops if it is refused. 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 a mesh shader reads in s8..s15 is not measured: -`user_sgpr_census.py --base ms=16` on a corpus lists any read below s16. A root signature carries both layouts; +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, From c170c08dfa64e4495b7e5037dc546a4383ba7d6e Mon Sep 17 00:00:00 2001 From: iFeyz <62035583+iFeyz@users.noreply.github.com> Date: Tue, 24 Nov 2026 19:11:14 +0100 Subject: [PATCH 4/6] D3D-448-GRAPHICS: LEONIDA_GRAPHICS_ROOT_LAYOUT=pc puts every graphics signature back in the PC layout (diagnostic) One build can then compare the console layout with the layout used before it on the retail title, as the #974 review needs (madhyn's frame-loop stack stops after 2 presents with the console layout, runs with the PC one). Mesh draws are refused in that mode: a mesh shader translated with the root window reads the console layout only. Compute is unchanged. d3d12_mesh_test checks both sides with one signature. --- docs/d3d12_vulkan.md | 5 ++++- runtime/shims/d3d12_vulkan_device.cpp | 32 +++++++++++++++++++-------- runtime/test/d3d12_mesh_test.cpp | 19 ++++++++++++++++ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index b216cd791d..e810d3dc2c 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -1771,7 +1771,10 @@ not the mesh window is created with a log line, and `DispatchMesh` with it inval the graphics window (more than 24 dwords from s8) goes, on the console, to the argument block behind `s[0:1]`, which is modelled for compute only (ROOT-SIG-SPILL-MODEL); until it is for graphics, such a signature keeps `pack()`'s layout at the graphics bind point (from s0, two dwords per table, `RootArgumentLayout::graphics_console` false) rather -than being refused: the retail title creates one at start-up (29..32 dwords) and stops if it is refused. Live evidence for +than being refused: the retail title creates one at start-up (29..32 dwords) and stops if it is refused. +`LEONIDA_GRAPHICS_ROOT_LAYOUT=pc` (diagnostic) gives every graphics signature that `pack()` layout, as before the +console layout, so one build compares both on the retail title; `DispatchMesh` is then refused, compute is unchanged +(`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 diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 71e0871542..2cb6b70ad6 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -6048,21 +6048,35 @@ class Device final const rt::rootsig::StagePackResult graphics = rt::rootsig::pack_stage(parsed.sig, rt::rootsig::Stage::Vertex); 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); - const rt::rootsig::PackResult fallback = graphics.ok ? rt::rootsig::PackResult{} : rt::rootsig::pack(parsed.sig); - const bool graphics_usable = graphics.ok || fallback.ok; + // LEONIDA_GRAPHICS_ROOT_LAYOUT=pc (diagnostic, #974 review): every graphics signature keeps pack()'s layout, + // as before the console layout, so one build compares both on the retail title. Mesh draws are refused then + // (a mesh shader translated with the root window reads the console layout only). Compute is unaffected. + std::size_t layout_length = 0; + char layout_value[8] = {}; + const bool graphics_pc = getenv_s(&layout_length, layout_value, "LEONIDA_GRAPHICS_ROOT_LAYOUT") == 0 && + layout_length > 1 && _stricmp(layout_value, "pc") == 0; + static std::atomic graphics_pc_said{false}; + if (graphics_pc && !graphics_pc_said.exchange(true)) + std::fprintf(stderr, "[rt] d3d12: LEONIDA_GRAPHICS_ROOT_LAYOUT=pc: graphics root arguments from s0, two " + "dwords per table, for every root signature (diagnostic)\n"); + const bool graphics_console = graphics.ok && !graphics_pc; + const rt::rootsig::PackResult fallback = + graphics_console ? rt::rootsig::PackResult{} : rt::rootsig::pack(parsed.sig); + const bool graphics_usable = graphics_console || fallback.ok; if (!graphics_usable && !compute.ok) { - std::fprintf(stderr, "[rt] d3d12: root signature refused: %s (compute: %s)\n", graphics.error.c_str(), - compute.error.c_str()); + std::fprintf(stderr, "[rt] d3d12: root signature refused: %s (compute: %s)\n", + graphics.ok ? fallback.error.c_str() : graphics.error.c_str(), compute.error.c_str()); return E_INVALIDARG; } if (!graphics_usable) - std::fprintf(stderr, "[rt] d3d12: root signature usable for compute only: %s\n", graphics.error.c_str()); + std::fprintf(stderr, "[rt] d3d12: root signature usable for compute only: %s\n", + graphics.ok ? fallback.error.c_str() : graphics.error.c_str()); else if (!graphics.ok) std::fprintf(stderr, "[rt] d3d12: root signature past the console's graphics window (%s): graphics keeps the PC " "layout from s0 until the argument block behind s[0:1] is modelled\n", graphics.error.c_str()); - else if (!mesh.ok) + else if (graphics_console && !mesh.ok) std::fprintf(stderr, "[rt] d3d12: root signature too large for a mesh shader's root window: %s\n", mesh.error.c_str()); if (!compute.ok) @@ -6070,12 +6084,12 @@ class Device final RootArgumentLayout layout; layout.flags = parsed.sig.flags; layout.graphics_ok = graphics_usable; - layout.graphics_console = graphics.ok; - layout.mesh_ok = graphics.ok && mesh.ok; + layout.graphics_console = graphics_console; + layout.mesh_ok = graphics_console && mesh.ok; layout.compute_ok = compute.ok; for (std::size_t i = 0; i < parsed.sig.params.size(); ++i) { std::uint32_t first = rt::rootsig::kNoSgpr; - if (graphics.ok) { + if (graphics_console) { first = graphics.first_sgpr[i]; } else if (fallback.ok) { for (std::uint32_t d = 0; d < fallback.dword_count; ++d) diff --git a/runtime/test/d3d12_mesh_test.cpp b/runtime/test/d3d12_mesh_test.cpp index 92b7f387d0..e8a9f7cebe 100644 --- a/runtime/test/d3d12_mesh_test.cpp +++ b/runtime/test/d3d12_mesh_test.cpp @@ -448,6 +448,25 @@ int main() { list->RSSetViewports(1, &viewport); list6->DispatchMesh(1, 1, 1); EXPECT(list->Close() == E_INVALIDARG); // a mesh shader still cannot read it + // LEONIDA_GRAPHICS_ROOT_LAYOUT=pc (diagnostic): even a signature that fits every window keeps the PC + // layout at the graphics bind point, so a mesh shader, which reads the console layout, may not use it. + EXPECT(_putenv_s("LEONIDA_GRAPHICS_ROOT_LAYOUT", "pc") == 0); + Ref pc_layout; + EXPECT(wide_root_signature(device.p, pc_layout, 4)); + EXPECT(_putenv_s("LEONIDA_GRAPHICS_ROOT_LAYOUT", "") == 0); + EXPECT(SUCCEEDED(list->Reset(allocator.p, mesh_pso.p))); + list->SetGraphicsRootSignature(pc_layout.p); + list->RSSetViewports(1, &viewport); + list6->DispatchMesh(1, 1, 1); + EXPECT(list->Close() == E_INVALIDARG); + // The same signature without the variable: console layout, the mesh window holds it. + 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())); } EXPECT(SUCCEEDED(queue->Signal(fence.p, 2)) && SUCCEEDED(fence->SetEventOnCompletion(2, nullptr))); From b2da1c0add9009678130b9bf898f2e6b8a4ae30f Mon Sep 17 00:00:00 2001 From: iFeyz <62035583+iFeyz@users.noreply.github.com> Date: Tue, 24 Nov 2026 19:13:15 +0100 Subject: [PATCH 5/6] D3D-448-GRAPHICS: a graphics signature past the console window logs its dwords per shader visibility No VS or PS reads s[0:1] as an address (root_spill_census), so a 29..32-dword start-up signature may fit each stage once a parameter takes SGPRs only in the stages it is visible to. The fallback now logs the console-layout dwords per D3D12_SHADER_VISIBILITY and the per-stage sums, counts a retail run can report. --- docs/d3d12_vulkan.md | 5 ++++- runtime/shims/d3d12_vulkan_device.cpp | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index e810d3dc2c..953f6460c5 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -1771,7 +1771,10 @@ not the mesh window is created with a log line, and `DispatchMesh` with it inval the graphics window (more than 24 dwords from s8) goes, on the console, to the argument block behind `s[0:1]`, which is modelled for compute only (ROOT-SIG-SPILL-MODEL); until it is for graphics, such a signature keeps `pack()`'s layout at the graphics bind point (from s0, two dwords per table, `RootArgumentLayout::graphics_console` false) rather -than being refused: the retail title creates one at start-up (29..32 dwords) and stops if it is refused. +than being refused: the retail title creates one at start-up (29..32 dwords) and stops if it is refused. Since no VS +or PS reads `s[0:1]` as an address (below), such a signature may instead fit each stage once a parameter takes SGPRs +only in the stages it is visible to (visibility is not modelled yet): the layer logs its dwords per +`D3D12_SHADER_VISIBILITY` and the per-stage sums that would give, counts that settle it on the retail title. `LEONIDA_GRAPHICS_ROOT_LAYOUT=pc` (diagnostic) gives every graphics signature that `pack()` layout, as before the console layout, so one build compares both on the retail title; `DispatchMesh` is then refused, compute is unchanged (`d3d12_mesh_test`). Live evidence for diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 2cb6b70ad6..a355825cfc 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -6071,11 +6071,26 @@ class Device final if (!graphics_usable) std::fprintf(stderr, "[rt] d3d12: root signature usable for compute only: %s\n", graphics.ok ? fallback.error.c_str() : graphics.error.c_str()); - else if (!graphics.ok) + else if (!graphics.ok) { std::fprintf(stderr, "[rt] d3d12: root signature past the console's graphics window (%s): graphics keeps the PC " "layout from s0 until the argument block behind s[0:1] is modelled\n", graphics.error.c_str()); + // Counts for the open question behind it: no VS or PS reads s[0:1] as an address (root_spill_census), so + // such a signature may fit each stage once a parameter takes SGPRs only in the stages it is visible to. + // Console-layout dwords per D3D12_SHADER_VISIBILITY (constant: its values, root view 2, table 1). + std::uint32_t by_visibility[8] = {}; + for (const auto& param : parsed.sig.params) + by_visibility[std::min(static_cast(param.visibility), 7u)] += + rt::rootsig::stage_dwords(param.type, param.num_values); + const std::uint32_t* v = by_visibility; + std::fprintf(stderr, + "[rt] d3d12: root signature dwords by visibility: all %u, vertex %u, hull %u, domain %u, " + "geometry %u, pixel %u, amplification %u, mesh %u; per stage with visibility: vertex %u, " + "pixel %u, geometry %u, mesh %u (windows 24, 24, 24, 16)\n", + v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[0] + v[1], v[0] + v[5], v[0] + v[4], + v[0] + v[7]); + } else if (graphics_console && !mesh.ok) std::fprintf(stderr, "[rt] d3d12: root signature too large for a mesh shader's root window: %s\n", mesh.error.c_str()); From 00fc23b9a9eeb1a75f1fec2b58024bea15c26663 Mon Sep 17 00:00:00 2001 From: iFeyz <62035583+iFeyz@users.noreply.github.com> Date: Tue, 24 Nov 2026 19:53:18 +0100 Subject: [PATCH 6/6] D3D-448-GRAPHICS: a graphics signature past the console window keeps its inline part; the rest goes to the argument block On the console the parameter that does not fit and every one after it go to the argument block behind s[0:1], the ones before stay inline. No VS, PS or GS of 1.0.2.1 reads s[0:1] as an address (root_spill_census), so the graphics bind point now lays the inline part out from s8 and stores nothing for the rest (rt::rootsig::Spill::block), instead of giving the whole signature the PC layout. A write to such a parameter is dropped before kNoSgpr plus an offset could wrap to dword 0. The retail start-up signatures (25 and 30 dwords, every parameter visible to every stage, #974 review) are the case; the per-visibility log line is gone, LEONIDA_GRAPHICS_ROOT_LAYOUT=pc stays. Compute still refuses a spill. --- docs/d3d12_vulkan.md | 25 +++++++++-------- runtime/shims/d3d12_com.cpp | 15 +++++++++-- runtime/shims/d3d12_vulkan_device.cpp | 39 ++++++++++----------------- runtime/src/root_signature.cpp | 22 ++++++++++----- runtime/src/root_signature.h | 19 ++++++++++--- runtime/test/d3d12_mesh_test.cpp | 10 +++++-- runtime/test/root_signature_test.cpp | 24 +++++++++++++++++ 7 files changed, 104 insertions(+), 50 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 953f6460c5..8c94097f38 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -1767,17 +1767,20 @@ argument in the same dword. A mesh shader's s8..s15 are below its base and read 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 is created with a log line, and `DispatchMesh` with it invalidates the list. A signature past -the graphics window (more than 24 dwords from s8) goes, on the console, to the argument block behind `s[0:1]`, which -is modelled for compute only (ROOT-SIG-SPILL-MODEL); until it is for graphics, such a signature keeps `pack()`'s -layout at the graphics bind point (from s0, two dwords per table, `RootArgumentLayout::graphics_console` false) rather -than being refused: the retail title creates one at start-up (29..32 dwords) and stops if it is refused. Since no VS -or PS reads `s[0:1]` as an address (below), such a signature may instead fit each stage once a parameter takes SGPRs -only in the stages it is visible to (visibility is not modelled yet): the layer logs its dwords per -`D3D12_SHADER_VISIBILITY` and the per-stage sums that would give, counts that settle it on the retail title. -`LEONIDA_GRAPHICS_ROOT_LAYOUT=pc` (diagnostic) gives every graphics signature that `pack()` layout, as before the -console layout, so one build compares both on the retail title; `DispatchMesh` is then refused, compute is unchanged -(`d3d12_mesh_test`). Live evidence for +not the mesh window is created with a log line, and `DispatchMesh` with it invalidates the list. In a signature past +the graphics window (more than 24 dwords from s8), the parameter that does not fit and every one after it go, on the +console, to the argument block behind `s[0:1]` (as measured for compute, section 7), and the ones before stay inline. +No VS, PS or GS reads `s[0:1]` as an address (below), so a draw reads nothing in that block: the graphics bind point +lays the inline part out from s8 and stores nothing for the rest (`rt::rootsig::Spill::block`, logged once per +signature; compute keeps refusing a spill until ROOT-SIG-SPILL-MODEL). The retail title creates two such signatures at +start-up, 25 and 30 dwords with every parameter visible to every stage (#974 review, RTX 3060 Ti under Wine), and stops +if they are refused. `LEONIDA_GRAPHICS_ROOT_LAYOUT=pc` (diagnostic) gives every graphics signature `pack()`'s layout +instead (from s0, two dwords per table, `RootArgumentLayout::graphics_console` false), as before the console layout, +so one build compares both on the retail title; `DispatchMesh` is then refused, compute is unchanged +(`d3d12_mesh_test`). On that review's stack the console layout stops the title after 2 presents at the `int3` it +raises for a removed device (issue #14, DesertGamer's fence finding), where the PC layout runs 130 s: the draws now +read the title's own tables and one of them faults the GPU there. DadaCap's `boot-prototype`, which also lays graphics +out from s8, runs 76..90 s on an RTX 5070 with and without this change. 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 diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 7fcfceebbb..4349a6a91c 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp @@ -1234,9 +1234,19 @@ 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 of the argument block behind s[0:1] (rt::rootsig::Spill::block, graphics): no shader reads it, so + // its value is not stored; kNoSgpr plus an offset must never reach write(). + static void spilled() { + static std::atomic said{false}; + if (!said.exchange(true)) + std::fprintf(stderr, "[rt] d3d12: root arguments in the argument block behind s[0:1] are not stored (no " + "graphics shader reads them)\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) { @@ -1246,10 +1256,11 @@ class GraphicsCommandList final const RootArgumentLayout::Parameter* p = parameter(state, index, type); if (!p) return; 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 layout of both bind points 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 (D3D-448). A graphics signature - // past the console window keeps pack()'s layout, where every address takes two dwords. + // descriptor (the heaps live below 4 GiB); a root view keeps both halves (D3D-448). Under + // LEONIDA_GRAPHICS_ROOT_LAYOUT=pc the graphics bind point keeps pack()'s layout, two dwords per address. const bool console = is_compute(state) || state.layout->graphics_console; if (!(console && type == rt::rootsig::ParamType::Table)) write(state, first + 1, std::uint32_t(va >> 32)); } diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index a355825cfc..fee4b2e7bf 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -6040,12 +6040,15 @@ class Device final // vertex, pixel or geometry shader reads SGPR i from push dword i, and compute from s4. A mesh shader reads // the same graphics window from its s16, so its 16-dword window only needs checking. // - // A signature past the console's inline graphics window (24 dwords from s8) goes, on the console, to the - // argument block behind s[0:1], which is not modelled for graphics yet (ROOT-SIG-SPILL-MODEL does compute). - // Such a signature keeps pack()'s layout at the graphics bind point (from s0, two dwords per table), what - // the layer used before the console layout: refusing it instead stops the retail title at start-up (#974 - // review, a 29..32-dword startup signature). The signature is refused only when no layout can place it. - const rt::rootsig::StagePackResult graphics = rt::rootsig::pack_stage(parsed.sig, rt::rootsig::Stage::Vertex); + // A parameter past the console's inline graphics window (24 dwords from s8) goes, on the console, to the + // argument block behind s[0:1] with every parameter after it, and the ones before stay inline. No VS, PS or + // GS of 1.0.2.1 reads s[0:1] as an address (root_spill_census), so a draw reads nothing in that block: the + // inline part is laid out and the rest takes no SGPR (Spill::block). The retail title creates two such + // signatures at start-up, 25 and 30 dwords with every parameter visible to every stage (#974 review). + // Compute keeps refusing what spills: its block is ROOT-SIG-SPILL-MODEL's. The signature is refused only + // when no layout can place it. + const rt::rootsig::StagePackResult graphics = + rt::rootsig::pack_stage(parsed.sig, rt::rootsig::Stage::Vertex, rt::rootsig::Spill::block); 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); // LEONIDA_GRAPHICS_ROOT_LAYOUT=pc (diagnostic, #974 review): every graphics signature keeps pack()'s layout, @@ -6071,26 +6074,12 @@ class Device final if (!graphics_usable) std::fprintf(stderr, "[rt] d3d12: root signature usable for compute only: %s\n", graphics.ok ? fallback.error.c_str() : graphics.error.c_str()); - else if (!graphics.ok) { + else if (graphics_console && graphics.first_spilled != SIZE_MAX) std::fprintf(stderr, - "[rt] d3d12: root signature past the console's graphics window (%s): graphics keeps the PC " - "layout from s0 until the argument block behind s[0:1] is modelled\n", - graphics.error.c_str()); - // Counts for the open question behind it: no VS or PS reads s[0:1] as an address (root_spill_census), so - // such a signature may fit each stage once a parameter takes SGPRs only in the stages it is visible to. - // Console-layout dwords per D3D12_SHADER_VISIBILITY (constant: its values, root view 2, table 1). - std::uint32_t by_visibility[8] = {}; - for (const auto& param : parsed.sig.params) - by_visibility[std::min(static_cast(param.visibility), 7u)] += - rt::rootsig::stage_dwords(param.type, param.num_values); - const std::uint32_t* v = by_visibility; - std::fprintf(stderr, - "[rt] d3d12: root signature dwords by visibility: all %u, vertex %u, hull %u, domain %u, " - "geometry %u, pixel %u, amplification %u, mesh %u; per stage with visibility: vertex %u, " - "pixel %u, geometry %u, mesh %u (windows 24, 24, 24, 16)\n", - v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[0] + v[1], v[0] + v[5], v[0] + v[4], - v[0] + v[7]); - } + "[rt] d3d12: root signature past the console's graphics window: root parameters %zu..%zu " + "(%u dwords) go to the argument block behind s[0:1], which no graphics shader reads; the " + "others stay inline from s8\n", + graphics.first_spilled, parsed.sig.params.size() - 1, graphics.spilled_dwords); else if (graphics_console && !mesh.ok) std::fprintf(stderr, "[rt] d3d12: root signature too large for a mesh shader's root window: %s\n", mesh.error.c_str()); diff --git a/runtime/src/root_signature.cpp b/runtime/src/root_signature.cpp index a0eb96cced..3852c587c4 100644 --- a/runtime/src/root_signature.cpp +++ b/runtime/src/root_signature.cpp @@ -48,7 +48,7 @@ PackResult pack(const RootSignature& sig) { return out; } -StagePackResult pack_stage(const RootSignature& sig, Stage stage) { +StagePackResult pack_stage(const RootSignature& sig, Stage stage, Spill spill) { StagePackResult out; out.stage = stage; out.base = stage_base(stage); @@ -73,12 +73,22 @@ StagePackResult pack_stage(const RootSignature& sig, Stage stage) { return out; } const uint64_t dwords = stage_dwords(p.type, p.num_values); + if (out.first_spilled != SIZE_MAX) { // already in the argument block: every later parameter follows + first.push_back(kNoSgpr); + out.spilled_dwords += static_cast(dwords); + continue; + } if (next + dwords > kMaxUserSgprs) { - // The console moves this parameter and every later one to the spill array behind s[0:1]; that array's - // layout is not modelled here, so refuse rather than drop arguments. - out.error = "root parameter " + std::to_string(i) + " does not fit in the user SGPRs from s" + - std::to_string(out.base) + " (spill array not modelled)"; - return out; + // The console moves this parameter and every later one to the spill array behind s[0:1]. + if (spill == Spill::refuse) { + out.error = "root parameter " + std::to_string(i) + " does not fit in the user SGPRs from s" + + std::to_string(out.base) + " (spill array not modelled)"; + return out; + } + out.first_spilled = i; + out.spilled_dwords = static_cast(dwords); + first.push_back(kNoSgpr); + continue; } first.push_back(static_cast(next)); next += dwords; diff --git a/runtime/src/root_signature.h b/runtime/src/root_signature.h index d1b8e38b44..95f68c4cd8 100644 --- a/runtime/src/root_signature.h +++ b/runtime/src/root_signature.h @@ -11,6 +11,7 @@ #pragma once #include +#include #include #include #include @@ -135,7 +136,8 @@ constexpr uint32_t kMaxD3d12Cost = 64; // The graphics bind point uses Stage::Vertex; 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; that array is not modelled, so such a signature is refused, never truncated. +// every parameter after it. pack_stage refuses such a signature by default; Spill::block (graphics) lays the inline +// part out and leaves the rest in that block, which no graphics shader reads. enum class Stage : uint32_t { Vertex = 0, Pixel = 1, Geometry = 2, Compute = 3, Mesh = 4 }; [[nodiscard]] constexpr uint32_t stage_base(Stage stage) noexcept { @@ -172,14 +174,23 @@ struct StagePackResult { std::string error; // set when !ok; names the refusing parameter Stage stage = Stage::Compute; uint32_t base = 0; // stage_base(stage) - std::vector first_sgpr; // per parameter: its first user SGPR (kNoSgpr when the signature is refused) + std::vector first_sgpr; // per parameter: its first user SGPR (kNoSgpr when the signature is refused, + // and for a parameter in the argument block under Spill::block) uint32_t end_sgpr = 0; // one past the last SGPR used: the RSRC2 USER_SGPR count the layout implies uint32_t d3d12_cost = 0; // cost against the 64-dword D3D12 limit + size_t first_spilled = SIZE_MAX; // Spill::block: the first parameter in the argument block, if any + uint32_t spilled_dwords = 0; // and the console-layout dwords from it on }; +// What a parameter that does not fit becomes. refuse: the signature is refused (the default; the compute block's +// layout is ROOT-SIG-SPILL-MODEL's). block: it and every later parameter go to the argument block behind s[0:1], as +// on the console, and take no SGPR (kNoSgpr); the ones before keep their inline SGPRs. For graphics no VS, PS or GS +// reads s[0:1] as an address (tools/root_spill_census.py on 1.0.2.1), so nothing a draw reads is in that block. +enum class Spill : uint32_t { refuse = 0, block = 1 }; + // Lays the parameters out for `stage` in the console layout. Fails like pack() on more than 64 parameters, a -// constant parameter of 0 values or the 64-dword cost limit, and on any parameter that would spill. -[[nodiscard]] StagePackResult pack_stage(const RootSignature& sig, Stage stage); +// constant parameter of 0 values or the 64-dword cost limit, and, under Spill::refuse, on any parameter that spills. +[[nodiscard]] StagePackResult pack_stage(const RootSignature& sig, Stage stage, Spill spill = Spill::refuse); // D3D12 cost of one parameter against the 64-dword limit. [[nodiscard]] constexpr uint32_t param_cost(ParamType type, uint32_t num_values) noexcept { diff --git a/runtime/test/d3d12_mesh_test.cpp b/runtime/test/d3d12_mesh_test.cpp index e8a9f7cebe..ccde1658b1 100644 --- a/runtime/test/d3d12_mesh_test.cpp +++ b/runtime/test/d3d12_mesh_test.cpp @@ -439,12 +439,18 @@ int main() { list->RSSetViewports(1, &viewport); list6->DispatchMesh(1, 1, 1); EXPECT(list->Close() == E_INVALIDARG); - // Past every console window: still created, the graphics bind point keeping the PC layout from s0 until - // the argument block behind s[0:1] is modelled (refusing it stopped the retail title at start-up). + // Past every console window: still created (refusing it stopped the retail title at start-up), its 30 + // constants in the argument block behind s[0:1], which no graphics shader reads. Setting them is valid + // and stores nothing; a mesh shader still may not use the signature. 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 spilled_values{}; + list->SetGraphicsRoot32BitConstants(0, 30, spilled_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 still cannot read it diff --git a/runtime/test/root_signature_test.cpp b/runtime/test/root_signature_test.cpp index 9c3d6c69fa..7574106c91 100644 --- a/runtime/test/root_signature_test.cpp +++ b/runtime/test/root_signature_test.cpp @@ -449,6 +449,30 @@ int main() { check(!pack_stage(tables, Stage::Mesh).ok, "pack_stage: a 17th table spills on mesh"); } + // Spill::block (graphics): the parameters that fit stay inline from s8; the first that does not, and every one + // after it even if it would fit, go to the argument block behind s[0:1] and take no SGPR, as on the console. + { + RootSignature sig; + sig.params = {table(), view(), consts(20), table(), consts(1)}; // s8, s9..s10, s11..s30, then s31 is free + const StagePackResult r = pack_stage(sig, Stage::Vertex, Spill::block); + check(r.ok && r.first_sgpr == std::vector({8, 9, 11, 31, kNoSgpr}), + "pack_stage block: a table at s31 still fits, the constant after it spills"); + check(r.first_spilled == 4 && r.spilled_dwords == 1 && r.end_sgpr == 32, + "pack_stage block: the first spilled parameter and its dwords"); + RootSignature big; + big.params = {table(), consts(30), table()}; // the 30 constants cannot fit: they and the last table spill + const StagePackResult b = pack_stage(big, Stage::Vertex, Spill::block); + check(b.ok && b.first_sgpr == std::vector({8, kNoSgpr, kNoSgpr}) && b.first_spilled == 1 && + b.spilled_dwords == 31 && b.end_sgpr == 9, + "pack_stage block: every parameter after the first spilled one spills, even one that would fit"); + check(!pack_stage(big, Stage::Vertex).ok, "pack_stage: the default still refuses a spill"); + RootSignature small; + small.params = {table(), consts(4)}; + const StagePackResult fits = pack_stage(small, Stage::Vertex, Spill::block); + check(fits.ok && fits.first_spilled == SIZE_MAX && fits.spilled_dwords == 0, + "pack_stage block: nothing spills when everything fits"); + } + // The graphics root window (D3D-448 follow-up): the graphics bind point lays a signature out once with // Stage::Vertex, 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