Skip to content

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

Open
TortitasT wants to merge 1 commit into
ItsNotRexed:mainfrom
TortitasT:d3d-448-per-stage-rootsig
Open

TortitasT wants to merge 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

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).

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.

7 participants