Skip to content

D3D-448: console root-argument layout, measured for compute and used there - #4312

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
TortitasT:d3d-448-per-stage-rootsig
Nov 23, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
TortitasT:d3d-448-per-stage-rootsig

Conversation

@TortitasT

Copy link
Copy Markdown

Item: D3D-448 (claimed in #4084; my finding from the D3D-315 work, #4163).

What and why

rt::rootsig::pack() lays root arguments out from s0 with two dwords per descriptor table. The shader census in docs/d3d12_vulkan.md §3.5 points to a per-stage base and one-dword table pointers instead. If the game's signatures follow the console layout, every root argument lands at the wrong push-constant offset. This PR measures the console layout where it can be measured (compute), models it, and switches the compute bind point to it.

Model: rt::rootsig::pack_stage(sig, Stage) puts parameters in root order from the stage base, each while it fits whole:

  • a constant takes one dword per value;
  • a root CBV/SRV/UAV takes two (its 64-bit VA);
  • a descriptor table takes one (the 32-bit VA of its first descriptor; the heaps live below 4 GiB);
  • the first parameter that doesn't fit is refused, since the spill array behind s[0:1] isn't modelled.

Compute (base s4) is measured three ways, on my own Xbox Series 1.0.2.1 copy (counts only):

  1. The console compiler's output already recorded in section 7.
  2. The package's FidelityFX DLL, whose 29 compute containers carry their RTS0 next to the console-compiled program. With the existing tools/rootsig_census.py --disasm, base s4 plus the signature's span in this layout equals the program's RSRC2 USER_SGPR in 29 of 29:
    • 27 signatures are table, table, four root CBVs: span 10, launched with 14 user SGPRs;
    • 2 are table, table, one root CBV: span 4, launched with 8;
    • pack() spans 12 and 6, two short of both.
  3. The reads: tools/root_sig_contract.py --model console (new) finds all 20 translated FidelityFX programs compatible. The pack() layout contradicts 7: they read the second table at s5, which that layout calls the first table's high half.

Vertex/pixel/geometry (s8) and mesh (s16) stay a hypothesis from the census only. Visibility isn't modelled.

Runtime change: compute only

  • CreateRootSignature builds both layouts. RootArgumentLayout::Parameter gains compute_offset, and the layout records graphics_ok / compute_ok.
  • The compute bind point's setters use the console layout. A table writes one dword; a root view writes two.
  • Graphics keeps pack(). Every stage reads s0..s31 from one push-constant block at offset 0 (ShaderUserData, rdna2spirv.py), so a mesh shader (s16) and a pixel shader (s8) in one pipeline would need the same argument at two offsets. That needs per-stage user-data windows in the push-constant contract, which I'll propose separately in D3D12 → Vulkan layer: critical path, who builds what, open methods #313 as a follow-up.
  • A signature neither layout can place is refused with both reasons. One that only a single layout places is created with a log line, and binding it at the other bind point invalidates the list (Close returns E_INVALIDARG).
  • s0..s3 of a compute dispatch are the driver's block and stay zero. 109 CS read s1 and 42 read s3 as constants; their meaning is open.

Test fixtures

  • GLSL compute fixtures (cs_user_data, cs_set0_sample, cs_sg_probe) read the new places: constants from s4, root views as pairs, tables as one 32-bit dword.
  • Asm fixtures a D3D12 compute PSO runs (cs_m4_fill, cs_m4_indirect, cs_buf_typed, cs_buf_counter, and cs_w64x_readlane / cs_wqm_w32 in d3d12_subgroup_test) are built with LEONIDA_USER_DATA_SHIFT=4.
    • compute_fixture.py shifts every SGPR by 4 and raises the RSRC2 user count, so the thread-group IDs follow.
    • The .asm sources are unchanged, so the standalone harness (check.py, run_gpu_tests.py) and tests/semantic keep running them with user data at s0.
    • The four D3D12-only fixtures read their table as s[8:9]: s8 is the 32-bit VA, and s9 is a user dword the runtime leaves zero. A note in each header says so.

Tested

NVIDIA GeForce RTX 4080, driver 621.24, Windows 11, MSVC 14.44, Vulkan SDK 1.4.363, LLVM 21.1.8 llvm-mc:

  • full runtime suite: ctest passes 153 of 153 (gameinput is disabled here because SDL3 isn't installed);
  • root_signature_test: 56 checks, including the pack_stage cases (the FidelityFX shape, stage bases, the 28/16-SGPR budgets, spill refusal);
  • d3d12_compute, d3d12_fspx_compute, d3d12_set0_compute, d3d12_m4_compute, d3d12_buffer_uav, d3d12_subgroup, d3d12_pipeline_library, d3d12_com: all pass;
  • python -I tests/test_compute_fixture_shift.py: 8 tests pass;
  • tests/test_root_sig_contract.py: all functions and parametrized cases pass. I ran them through a small local harness because pytest isn't installed here; please let CI's pytest job confirm.
  • tests/test_docs_links.py passes.

Overlaps: #3929 also edits runtime/src/root_signature.{h,cpp}, and #3987 edits tools/root_sig_contract.py. My changes there are additive. Whichever lands second may need a small rebase.

AI-assisted (Claude Code). No game data: counts and signature shapes only.

🤖 Generated with Claude Code

…there

rt::rootsig::pack_stage() lays a root signature out for one stage the
way the console compiler does: root order from the stage base, a table
one dword (its 32-bit VA), a root view two, constants one per value,
and the first parameter that does not fit refused (the spill array
behind s[0:1] is not modelled).

Compute (base s4) is measured: on the package's FidelityFX DLL, whose 29
compute containers carry their RTS0 next to the console-compiled program,
base plus span equals RSRC2 USER_SGPR in 29 of 29, and
root_sig_contract.py --model console finds all 20 translated programs
compatible (the s0 model contradicts 7). VS/PS/GS s8 and MS s16 stay a
census hypothesis.

The compute bind point now uses this layout; graphics keeps pack(),
since every stage reads one push-constant block and a mesh shader (s16)
and a pixel shader (s8) would collide. A signature carries both
layouts; binding it where its layout refused it invalidates the list.

Test fixtures follow: the compute GLSL shaders read the new places, and
the asm fixtures a D3D12 compute PSO runs are built with
LEONIDA_USER_DATA_SHIFT=4 (compute_fixture.py shifts every SGPR and the
RSRC2 user count), so the standalone harness keeps using them at s0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked 7c225260 on Windows 11, RTX 5070 Ti:

  • tests/test_compute_fixture_shift.py -> 8 tests, OK; tests/test_root_sig_contract.py -> exit 0 (run with tools/ on sys.path, as its import expects).
  • root_signature_test.exe -> exit 0: per-stage packing checks pass, including compute budget 28 dwords (20 one-dword tables fit compute, which pack() refuses at 40 dwords), 16 mesh tables exactly and the 17th spilling, zero-value/over-cost/too-many-parameter refusals.
  • d3d12_compute_test.exe -> exit 0: CBVs, raw/structured/typed buffer SRVs with OOB zeroing and negative controls, and compute root constants + a root UAV + a descriptor table reached the shader through the arena; 64 elements matched in each of three dispatches.
  • ctest -R 'root_signature|d3d12_compute' -> the two built tests passed; root_signature_oracle is not built by these targets (Not Run).
  • git diff --check clean.

No blocking issue.

@SweatierKey

Copy link
Copy Markdown
Contributor

Tested 7c225260 on Linux (WSL2, g++ -std=c++20 -O2 -Wall -Wextra, Python 3.13.5, CPU only, no Windows/GPU host, no game files): pass (Windows GPU compute tests not run here).

  • C++20 & Python tests executed on Linux (clean worktree on 03b09a08):
    • g++ -std=c++20 -O2 -Wall -Wextra -Iruntime/src runtime/src/root_signature.cpp runtime/test/root_signature_test.cpp compiled with 0 warnings and passed 56/56 checks (including all 19 new pack_stage checks: compute s4, mesh s16, graphics s8, FidelityFX s4..s14, 28-constant compute budget, 20 one-dword tables, mesh 16-table exact / 17th spill, overflow/cost/zero-value refusals).
    • python3 -I tests/test_compute_fixture_shift.py -v: 8/8 OK.
    • git diff --check, test_ctest_registration.py (4/4), test_docs_links.py (6/6), test_docs_repo_paths.py (2/2), test_text_write_line_endings.py (3/3), test_text_encoding.py (2/2): all pass.
  • Code read (root_signature.h/.cpp, d3d12_vulkan_device.cpp):
    • Clearly distinguishes measured compute layout (s4, 1-dword tables, 2-dword root views, FidelityFX 29/29 USER_SGPR match) from census-derived graphics/mesh hypotheses, and refuses spilling parameters instead of silently truncating.
  • Not run here: tests/test_root_sig_contract.py (pytest not installed in this -I env), Windows MSVC build, and Vulkan GPU compute tests.

AI-assisted review.

@molodayamama

Copy link
Copy Markdown

Independent check of 7c2252609d8f21dfe9f49d2b0cdf80cefc13b2f5 on Windows 10 22H2 (19045.7058), NVIDIA GeForce RTX 4060 (driver 32.0.16.1088), MSVC 19.40, Ninja RelWithDebInfo and glslang 16.6.0. Synthetic fixtures only.

  • Focused CTest: 5/5 passed with LEONIDA_VK_VALIDATION=1; the Vulkan tests printed validation on. Tests: root_signature, d3d12_com, d3d12_compute, d3d12_set0_compute, d3d12_pipeline_library.
  • The compute test matched 64 elements in each of three dispatches with root constants, a root UAV and a descriptor table. The set-0 compute test matched 64 texels per checked view/sampler path and confirmed the unbound-view fallback.
  • Ran the Python tests through pytest 9.2.0 on CPython 3.12.12: python -m pytest -q tests/test_root_sig_contract.py tests/test_compute_fixture_shift.py returned 63 passed.

This is the focused GLSL-backed runtime run plus the Python checks. AI-assisted (Codex).

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the current head 7c225260. The new compute root-argument offsets are used by the compute bind point, while graphics keeps its separately documented layout; spill cases fail closed. The measured signatures and focused tests support the one-dword table model. No blocking issue found.

@ddm-j

ddm-j commented Nov 23, 2026

Copy link
Copy Markdown

Heads-up for when this lands: open PRs that add a D3D12 compute fixture without LEONIDA_USER_DATA_SHIFT=4 will break the same way as the tests this PR already updates. I searched the runtime/CMakeLists.txt changes of all 190 open PRs that touch it for added test/shaders/build.py steps building a cs_ fixture without the shift. Two turned up:

AI-assisted (Claude Code).

@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 7c225260 merged onto main (two trivial conflicts with the newer descriptor_type field in RootArgumentLayout::Parameter / the push_back in d3d12_vulkan_device.cpp, both kept) on NVIDIA GeForce RTX 5080, Linux 580.65.06, runtime cross-built with clang-cl and run under Wine 11: pass for direct compute binding, plus one gap in ExecuteIndirect.

Direct binding, on the GPU. A hand-written wave32 compute kernel, run through gta6rt --run from a synthetic guest (no game data). It reads its root arguments where this PR puts them. Signature [descriptor table, 1 constant, root UAV, 4 constants], packed as s4 | s5 | s[6:7] | s8..s11, with s0..s3 zero. The table is read as a 32-bit pointer with a zero high word.

  • 5 dispatches each change one parameter: 120 of 120 output words right.
  • 120 of 120 words of the buffers not named stay at their poison value.
  • A 29-constant signature is refused at the compute bind point (Close gives E_INVALIDARG).
  • Control, the same kernel on main: FAIL. Under the old packing the table sits at s0:s1, so s4 is the root UAV's high word. Nothing is written, every word stays 0xdeadbeef, and the 29-constant signature is accepted (Close gives S_OK).

pack_stage on the CPU. A small C++ harness against root_signature.cpp, signature [4 constants, table, 1 constant]:

Stage Constants Table Constant
compute s4 s8 s9
VS/PS s8 s12 s13
MS s16 s20 s21

So the per-stage bases and one-dword tables are modelled for every stage, while the runtime uses them only for compute, as the description says.

Gap: ExecuteIndirect with a DISPATCH signature. build_indirect_signature (runtime/shims/d3d12_com.cpp, the root_dwords lambda) sets a.user_dword = p.user_offset + dest, the graphics offset, also for a DISPATCH signature. Measured with [INDIRECT CONSTANT on parameter 1, DISPATCH] and argument value 0x4c540003:

  • The value lands in s2, inside the zero block.
  • s5 keeps the value set directly with SetComputeRoot32BitConstant:
    s0..s11 = 00000000 00000000 4c540003 00000000 | 00011000 | 4c540001 | 00070000 00000000 | ...
  • Likely fix: use compute_offset when the signature is a dispatch one. DISPATCH is the last argument, so the kind is known only after the loop. The same applies to indirect root CBV/SRV/UAV arguments (two dwords at compute_offset).

No game files. AI-assisted (Claude Code).

@Roberto0953

Copy link
Copy Markdown
Contributor

Tested 7c225260 merged into main 3faac557 (clean merge) on Windows 11 Pro 26300, MSVC 14.44, RTX 3080 (617.42): pass, with an independent end-to-end confirmation of the compute layout.

The PR's tests here: ctest -R root_sig: 3/3 passed (root_sig, root_signature, root_signature_oracle); pytest tests/test_root_sig_contract.py tests/test_rootsig_census.py: 79 passed. Build of gta6rt: 0 warnings. The GPU compute tests that need glslangValidator were not built on this machine.

Independent check. We have a small synthetic Xbox-style title (our own code, no game files) that runs under gta6rt --run through the device proxy. Its hand-written RDNA2 compute program exists in two forms:

  • one reads its root arguments the way pack() lays them out (from s0);
  • one reads them the way the corpus census in §3.5 suggests: table pointer in s4 as one dword, a root constant in s5, the thread-group id after the user SGPRs.

The second form was written before this PR, from the census alone. Each dispatch writes 1,024 texels that are compared with a CPU reference.

Compute program reads its arguments main 3faac557 main + this PR
console layout (s4, one dword per table) 0 of 1,024 texels, with no message 1,024 of 1,024
pack() layout (from s0) 1,024 of 1,024 0 of 1,024, as intended by the switch

0 validation messages in every run.

So a program written to the console layout by someone else, without knowledge of pack_stage(), lands exactly where this PR puts the arguments. Table, constant and thread-group id all line up.

Graphics, for whoever takes the next step: the same title has a VS/PS pair that reads from s8 with one-dword tables. It stays wrong with this PR (graphics keeps pack(), as the description says): the pixel shader finds no T#/S# and only the clear colour matches. That fixture will confirm or refute a graphics base of s8 the same way once one is proposed.

One note for reviewers: on main the mismatch is silent. The console-layout program runs, nothing is logged, and the output is just wrong. That is the failure a first boot would show before this PR.

AI-assisted (Claude Code); run on this machine.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 7c22526 on NVIDIA GeForce RTX 4080, driver 617.42, Windows 11 (clang-cl cross-build): root_signature_test exits 0 with 40 PASS lines and no FAIL, including "pack_stage: compute budget is 28, not 32" and "pack_stage: a parameter that does not fit whole is refused and named" (the PR body says 56 checks; I count 40 printed lines). d3d12_compute_test, d3d12_fspx_compute_test, d3d12_set0_compute_test, d3d12_m4_compute_test and d3d12_subgroup_test exit 0 and print no pass summary, so only their exit codes are confirmed. d3d12_buffer_uav_test, d3d12_pipeline_library_test and d3d12_com_test were not run.

AI-assisted.

@dracbat

dracbat commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 7c225260 merged with main 3faac557 for the test (clean merge) on an AMD Radeon RX 5700 (RDNA1, gfx1010; subgroup sizes 32..64, default 64, requirable for compute only), AMD Adrenalin 26.11.2 (Windows driver 32.0.21045.29003, Vulkan 1.4.315), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363.1, llvm-mc 21.1.8, Ninja, RelWithDebInfo: fail, in one test, because of a fixture main added after this branch's base. The PR's own tests pass. As far as I can see this is its first AMD run.

  • The PR's tests, with and without LEONIDA_VK_VALIDATION=1: 12 of 12, 0 VUID lines.
    • root_sig, root_signature (the pack_stage cases) and root_signature_oracle;
    • d3d12_com, d3d12_compute, d3d12_fspx_compute and d3d12_set0_compute;
    • d3d12_m4_compute (1,924 words, error 0) and d3d12_buffer_uav;
    • d3d12_pipeline_library, d3d12_subgroup and subgroup_size.
  • d3d12_subgroup on a 64-wide GPU: cs_w64x_readlane now reads its V# at s[4:7]. It is exact at a required size of 64 with full subgroups, and so is its --wave64-emulate build. cs_wqm_w32 is exact at a required 32. The NVIDIA runs can't judge the native wave64 case.
  • Python: tests/test_compute_fixture_shift.py, test_root_sig_contract.py and test_rootsig_census.py: 87 passed (2 subtests). test_docs_links.py, test_ctest_registration.py and test_docs_repo_paths.py: 12 passed. git diff --check is clean.
  • Full ctest -j 2 -E "d3d12_present|vk_presenter_sync|xbox_present": 174 of 175 without validation and 173 of 175 with it (0 VUID lines).
  • On @tedin7's ExecuteIndirect note: on main + this PR, CreateCommandSignature keeps only the kind and the stride, so no indirect root argument is written at either offset yet. The DISPATCH-signature gap appears with Runtime: ExecuteIndirect with multi-argument command signatures (root constants/descriptors, VB/IB views per record) on VK_EXT_device_generated_commands #2077's root_dwords writer. Whichever of the two lands second needs compute_offset there.

AI-assisted (Claude Code).

@mrp1no

mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

@TortitasT, a follow-up to this PR rather than an overlap, as @KoertLichtendonk asked me to compare them here.

#4312 is on main as 7c225260. It moved compute root arguments to pack_stage's layout and updated cs_set0_sample.comp, cs_sg_probe.comp and cs_user_data.comp to match. cs_indirect_dispatch_count.comp reached main at the same time through c262fb7b, so this PR couldn't see it. That fixture still read its root UAV at s[0]:s[1], so on 561b65a8 every layer dispatch of d3d12_indirect_dispatch_count_test wrote somewhere other than the UAV (0/0 in all eight cases).

#5792 changes only that fixture, to s[4]:s[5] like cs_sg_probe.comp. It doesn't touch the layout, the runtime or anything this PR added. With it the test passes on a UHD 750 and an RTX 3060 (mine) and on an RTX 4090 and an AMD iGPU (@shdann's run on #5792). If you'd rather fold it into a D3D-448 follow-up of yours, tell me and I'll close mine.

AI-assisted (Claude Code).

tedin7 added a commit to tedin7/Leonida that referenced this pull request Nov 23, 2026
…layout

main's ItsNotRexed#4312 (D3D-448) gives a D3D12 compute PSO its root arguments from s4, a descriptor table as one 32-bit dword. cs_lanterns_rise.asm now count those user SGPRs in RSRC2 and start with moves that rebuild the layout the code was written for (the table as s[0:1] or s[4:5] with a zero high half).
tedin7 added a commit to tedin7/Leonida that referenced this pull request Nov 23, 2026
…layout

main's ItsNotRexed#4312 (D3D-448) gives a D3D12 compute PSO its root arguments from s4, a descriptor table as one 32-bit dword. cs_lanterns_bloom_h.asm, cs_lanterns_bloom_h_nobarrier.asm, cs_lanterns_bloom_v.asm now count those user SGPRs in RSRC2 and start with moves that rebuild the layout the code was written for (the table as s[0:1] or s[4:5] with a zero high half).
tedin7 added a commit to tedin7/Leonida that referenced this pull request Nov 23, 2026
…layout

main's ItsNotRexed#4312 (D3D-448) gives a D3D12 compute PSO its root arguments from s4, a descriptor table as one 32-bit dword. cs_lanterns_tiled.asm now count those user SGPRs in RSRC2 and start with moves that rebuild the layout the code was written for (the table as s[0:1] or s[4:5] with a zero high half).
tedin7 added a commit to tedin7/Leonida that referenced this pull request Nov 23, 2026
… layout

main's ItsNotRexed#4312 (D3D-448) gives a D3D12 compute PSO its root arguments from s4, a descriptor table as one 32-bit dword. cs_lanterns_rt_shadow.asm now count those user SGPRs in RSRC2 and start with moves that rebuild the layout the code was written for (the table as s[0:1] or s[4:5] with a zero high half).
tedin7 added a commit to tedin7/Leonida that referenced this pull request Nov 23, 2026
… layout

main's ItsNotRexed#4312 (D3D-448) gives a D3D12 compute PSO its root arguments from s4, a descriptor table as one 32-bit dword. cs_lanterns_samplers.asm now count those user SGPRs in RSRC2 and start with moves that rebuild the layout the code was written for (the table as s[0:1] or s[4:5] with a zero high half).
tedin7 added a commit to tedin7/Leonida that referenced this pull request Nov 23, 2026
… layout

main's ItsNotRexed#4312 (D3D-448) gives a D3D12 compute PSO its root arguments from s4, a descriptor table as one 32-bit dword. cs_lanterns_xy.asm now count those user SGPRs in RSRC2 and start with moves that rebuild the layout the code was written for (the table as s[0:1] or s[4:5] with a zero high half).
tedin7 added a commit to tedin7/Leonida that referenced this pull request Nov 23, 2026
… layout

main's ItsNotRexed#4312 (D3D-448) gives a D3D12 compute PSO its root arguments from s4, a descriptor table as one 32-bit dword. cs_lanterns_gbuf_decode.asm now count those user SGPRs in RSRC2 and start with moves that rebuild the layout the code was written for (the table as s[0:1] or s[4:5] with a zero high half).
tedin7 added a commit to tedin7/Leonida that referenced this pull request Nov 23, 2026
… layout

main's ItsNotRexed#4312 (D3D-448) gives a D3D12 compute PSO its root arguments from s4, a descriptor table as one 32-bit dword. cs_lanterns_rt_alpha.asm now count those user SGPRs in RSRC2 and start with moves that rebuild the layout the code was written for (the table as s[0:1] or s[4:5] with a zero high half).
ttd0x added a commit to ttd0x/Leonida that referenced this pull request Nov 23, 2026
ItsNotRexed#4312 moved the compute root arguments to start at s4 and updated cs_user_data.comp, cs_set0_sample.comp and cs_sg_probe.comp, but not this fixture, which still read the root UAV from pc.s[0], pc.s[1] (the driver's block, zero). d3d12_indirect_dispatch_count then failed on main with "count 0 update 1: host 6/10, layer 0/0, expected 6/10". Read it from pc.s[4], pc.s[5] and fix the header comment.

Fixes ItsNotRexed#6162. Claim: ItsNotRexed#6089 (comment)

AI-assisted (Claude Code).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.