Skip to content

SHADER-67: per-stage code-base slots, a pixel shader reads its code base from dwords 38-39 - #5128

Open
AimLogin wants to merge 1 commit into
ItsNotRexed:mainfrom
AimLogin:claim/SHADER-67-code-base-slots
Open

AimLogin wants to merge 1 commit into
ItsNotRexed:mainfrom
AimLogin:claim/SHADER-67-code-base-slots

Conversation

@AimLogin

Copy link
Copy Markdown
Contributor

SHADER-67 (claim, scope correction; listed in #5032). Open question 14 in docs/d3d12_vulkan.md. No dependencies; based on main 3faac557.

This is @AGVibecoder's implementation from #1705, which they closed after --pc-tail-ubo (#2046) landed and invited someone to pick up. I carried it onto current main and settled the --pc-tail-ubo question; their commit is credited as co-author.

Why

A VS (or MS) and a PS in one pipeline can each read code-embedded constants through s_getpc_b64. The corpus has 54 VS, 93 PS and 266 MS that do (tools/feature_scan.py, §5.2). The push-constant block had one code-base slot, at dwords 34–35. So on main, CreateGraphicsPipelineState refuses every PSO in which two stages read their code base, with E_NOTIMPL ("two stages read their code base; one push-constant slot holds one").

What

  • Translator (tools/rdna2spirv.py, code_base_dword()):
    • A pixel shader that reads its code base takes it from dwords 38–39. Its push-constant block is then 160 bytes.
    • Every other stage keeps 34–35, in a 144-byte block.
    • Dwords 36–37 stay with --mesh-compute's output address, which a mesh-compute draw pushes to the VS that runs next to the PS.
    • The index records code_base_dword and the real push_constant_bytes.
    • Shaders that don't read a code base are byte-identical.
  • --pc-tail-ubo is unchanged. Every stage, the PS included, still reads its code base from dwords 2–3 of the tail block, and the tail stays 16 bytes. The runtime side of that flag (Runtime: D3D12 PSOs bind the --pc-tail-ubo set-1 block for 128-byte push-constant devices (3/3) #2402) writes one 16-byte tail per pipeline and accepts only that size. A PS slot at 6–7 would get today's working single-code-base PS refused on 128-byte devices. On those devices a pipeline keeps one code base, as on main.
  • Runtime (d3d12_vulkan_device.cpp, vulkan_replay.h, shader_library.h):
    • Each stage that reads its code base gets its own FSSM blob in the arena. The VS's VA goes to 34–35 and the PS's to 38–39. Both blobs are released with the pipeline.
    • The graphics layout's push-constant range is the stages' largest block: 144 bytes, or 160 with a code-base PS. The replay pushes the same size.
    • Compute PSOs, Dispatch and indirect dispatch keep their 144-byte range and push.
    • Two stages naming the same slot (an index translated before this change) are refused with both names in the log.
    • D3D-309's D24-as-D32 depth path is kept as it is.
  • vk_harness: VKH_CODE=vs,ps fills both slots, with a 160-byte range. With LEONIDA_PC_TAIL_UBO it exits 77.
  • Docs: the user-data row in design.md, and §5.2 and question 14 in d3d12_vulkan.md.

Tests

  • tests/semantic/test_code_base_slot.py (5 tests):
    • a PS reads dwords 38–39, checked in the interpreter with different words in both slots;
    • a VS keeps 34–35;
    • a PS without a code base stays 144 bytes;
    • with --pc-tail-ubo a PS reads the tail's dwords 2–3, with a 16-byte tail.
  • tests/test_pc_tail_ubo.py now expects ps_m15's 160-byte block without the flag.
  • d3d12_draw:
    • a VS and a PS that each read a constant from their own program match 4096/4096 pixels;
    • an index whose PS still names 34–35 is refused.

RelWithDebInfo (MSVC), LEONIDA_VK_VALIDATION=1 plus synchronization validation, implicit layers off. RTX 4060 Ti, driver 621.01, Windows 11 26200:

Check main 3faac557 this PR
full ctest 169/169, 0 VUIDs 169/169, 0 VUIDs
targeted D3D12 pipeline tests (draw, compute, FSPX compute, indirect dispatch/count/zero/RT, geometry, MSAA, D24 draw, depth resolve, bundles, pipeline library, set-0, f16 halves, typeless families, conservative raster, shader_library) — 18/18
python -m unittest discover -s tests/semantic (6250 tests) 3 failures, 3 errors the same 3 failures and 3 errors

The same tests skip as on main (no game package, corpus, LEONIDA_FIRST_LIGHT_DIR or RGB9E5 RTVs on this adapter), and gameinput is disabled by default. The semantic failures are in test_loop_ballot_barrier and test_vcmp16_opsel, and they match main name for name.

Not run here, help welcome:

  • Tests that need llvm-mc: d3d12_translated_draw, which runs the translator on ps_m15, a PS that reads its code base; the requires_tools tests in tests/test_pc_tail_ubo.py; and scripts/run_gpu_tests.py. This machine has no llvm-mc.
  • The before/after translated and spirv-val-valid counts over the shader corpus. Only PSs that read a code base should change.

If you have either, please post counts and pass/fail. No game data, please.

Overlap

No game files.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…ase from dwords 38-39

A VS (or MS) and a PS in one pipeline can each read code-embedded data
through s_getpc_b64, but the push-constant block had one code-base slot
(dwords 34-35), so CreateGraphicsPipelineState refused every such pair
with E_NOTIMPL (open question 14).

- Translator: a PS that reads its code base takes it from dwords 38-39
  (a 160-byte block); every other stage keeps 34-35. Dwords 36-37 stay
  --mesh-compute's output address. The index records code_base_dword and
  the real push_constant_bytes. Shaders without a code base are
  byte-identical. --pc-tail-ubo output is unchanged: every stage reads
  the tail block's dwords 2-3, since the runtime writes one 16-byte tail
  per pipeline.
- Runtime: each code-base stage gets its own FSSM blob in the arena and
  its VA in its slot; the graphics layout's range is the stages' largest
  block (144, or 160 with a code-base PS). Compute PSOs, Dispatch and
  indirect dispatch keep pushing 144 bytes. Two stages naming the same
  slot (an index translated before this change) are refused with both
  names logged.
- vk_harness: VKH_CODE=vs,ps fills both slots (not with --pc-tail-ubo).
- Tests: tests/semantic/test_code_base_slot.py, including the
  --pc-tail-ubo case; test_pc_tail_ubo expects ps_m15's 160-byte block;
  d3d12_draw draws a VS and a PS that each read their own program.
- Docs: design.md user-data row, d3d12_vulkan.md section 5.2 and
  question 14.

Based on AGVibecoder's implementation in ItsNotRexed#1705, carried onto current
main: kept beside D3D-309's D24-as-D32 depth path, the indirect
dispatch push made 144 bytes, and --pc-tail-ubo left as it is.

Co-Authored-By: Gino <ginoaelbrecht17@gmail.com>
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.

Built and ran at 473a33ef3 on Windows 11, RTX 5070 Ti:

  • tests/semantic/test_code_base_slot.py -> 5 tests, OK.
  • pytest tests/test_pc_tail_ubo.py -q -> 1 passed, 7 skipped (skips need a translated shader set).
  • d3d12_draw_test -> exit 0: M1: five draws and a culled triangle matched all 4096 pixels ... then an XOR logic op, a full-screen pass without vertex input and a VS and PS that each read their own code base.
  • git diff --check clean.

The per-stage code-base slots are now selected by stage: a pixel shader reads its code base from user-data dwords 38–39, while the vertex stage keeps the existing pair, and the new GLSL fixtures vs_code.vert/ps_code.frag each read their own base and pass the value through to the frame. The semantic test pins the slot choice, and pc_tail_ubo still passes. No blocking issue.

@Atomic258

Copy link
Copy Markdown
Contributor

Independent check — pass, GPU + corpus verified (Windows 11, NVIDIA RTX 5090, driver 617.42, Vulkan SDK 1.4.363.0, native llvm-mc).

Runtime (real GPU):

  • d3d12_draw_test → all pass incl. the new case: "a VS and PS that each read their own code base" matched all 4,096 pixels; and the stale-metadata pair (vs_code + ps_code_stale both at dwords 34-35) still refuses correctly — back-compat with pre-change indexes holds
  • d3d12_vulkan_device_test → all pass, everything compared bit-exact vs the host 5090

Translator on real game shaders (local corpus slice, 6 PS shaders containing s_getpc_b64; names withheld per policy):

  • head: all 6 → code_base_dword=38, push_constant_bytes=160, uses_code_base=true, 6/6 spirv-val clean
  • parent: same 6 → uses_code_base=true at the dword-34 default, push_constant_bytes=144 — exactly the collision this change removes; both translate on both, so no count regression

Code-reading notes (non-blocking): the per-stage arena blobs (code_[slot]), the 34/38 dword guard, and push_bytes = max(entry->push_constant_bytes) are all consistent; mesh_output (36-37) stays reserved as documented; old indexes default to 34, so only newly-translated PS shaders unlock a code-base VS+PS pair — which matches the commit message.

AI-assisted (Devin); corpus slice stayed local, counts only.

@AimLogin

Copy link
Copy Markdown
Contributor Author

Thanks @Kam471 and @Atomic258 for the independent runs. @Atomic258, your corpus slice answers the count question from the PR description: 6/6 PSs that read a code base translate, move to dwords 38–39 and pass spirv-val.

The 7 skips in tests/test_pc_tail_ubo.py don't need a translated shader set. They need an llvm-mc with the AMDGPU target plus spirv-as/spirv-val on PATH, and their inputs are in the repo: runtime/test/shaders/*.asm and conftest's synthetic containers. I built llvm-mc 21.1.8 with scripts/build_llvm_mc.ps1 and re-ran on 473a33ef and on main 3faac557:

  • pytest tests/test_pc_tail_ubo.py: 8 passed, 0 skipped on both.
    • Without the flag, ps_m15 reads 38–39 in a 160-byte block.
    • With --pc-tail-ubo it still reads tail dwords 2–3 from a 16-byte tail, and the rest of the module is unchanged.
  • Full ctest with LLVM_MC set, so the tests that need it are built: 177/177 on both, 0 VUIDs. Validation and sync validation were on, with the same 8 skips; RTX 4060 Ti, driver 621.01. Among them, d3d12_translated_draw runs ps_m15, a PS that reads its code base, through the translator and the GPU. The M2–M4, d3d12_buffer_uav and d3d12_subgroup tests pass too.

The llvm-mc-dependent step in scripts/run_gpu_tests.py hasn't been run here.

@AimLogin
AimLogin marked this pull request as ready for review November 23, 2026 09:01
@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 473a33e on NVIDIA GeForce RTX 4080, driver 617.42, Windows 11 (clang-cl cross-build): d3d12_draw_test exits 0. It prints no pass summary and no pixel lines, so the 4096/4096 pixel check and the refusal of an index whose PS still names 34-35 are not visible in this run; only the exit code is confirmed. The other D3D12 targets the PR lists were not run.

AI-assisted.

@kotekompotek

Copy link
Copy Markdown

Independent Windows CPU verification on 473a33ef:

  • python -m pytest tests/semantic/test_code_base_slot.py tests/test_pc_tail_ubo.py --basetemp D:\temp\pytest_tmp -q: 6 passed, 7 skipped in 1.29s

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 473a33e on Windows Server 2022, CPython 3.13.12: pass

  • pytest tests/test_pc_tail_ubo.py -> 1 passed, 7 skipped; python -I -m unittest discover -s tests/semantic -p "test_code_base_slot.py" -> Ran 5 tests, OK
  • C++ shader/pipeline parts untested (no compiler, no GPU).

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@ramenbellic

Copy link
Copy Markdown

Tested 473a33e on NVIDIA GeForce RTX 3070, driver 620.70.06, Linux (Bazzite, Fedora 45 base, kernel 7.3.4): pass

  • GPU suite (scripts/run_gpu_tests.py), this PR merged onto main 3faac55: 170/217 passed, 47 skipped, 0 failed native; 201/217 passed, 16 skipped, 0 failed with LEONIDA_W64_EMULATE=1
  • The PR's tests: tests/semantic/test_code_base_slot.py: OK; tests/test_pc_tail_ubo.py: 8 passed
  • tests/semantic: failures=3, errors=2, all in test_loop_ballot_barrier (wave64-emulate cases); main 3faac55 alone gives the same counts on this machine, so not from this PR
  • Whole shader corpus (26,503 game shaders, --wave64-emulate), this PR merged onto main 3faac55: 26503/26503 translated, 26503 pass spirv-val (main alone: 26503/26503); 93 of the .spv outputs differ from main's, none newly invalid

AI-assisted run (Claude Code); counts only, no game data.

@AimLogin

Copy link
Copy Markdown
Contributor Author

Thanks @ramenbellic for the full-corpus run. It's the only one on all 26,503 shaders so far.

About the 93 .spv that differ from main: going by the code, only a pixel shader that reads its code base should change.

  • Its UserData block gets the two extra members for dwords 36–39, and its code-base access chain moves from members 3/4 to 7/8.
  • Every other stage keeps the same block, so its SPIR-V should be byte-identical. --pc-tail-ubo output doesn't change either.
  • §5.2 counts 93 PS that keep s_getpc_b64 for data (tools/feature_scan.py).

So far the two numbers only agree. That isn't yet a match shader for shader, and I have no corpus here to check it myself. If you still have that run's output, three counts would settle it:

  1. Of the 93 differing .spv, how many have "stage": "ps" and "uses_code_base": true in index.json?
  2. How many index entries are a PS with "code_base_dword": 38 and "push_constant_bytes": 160? Expected: 93, and the same 93.
  3. Is any differing .spv from a stage other than PS? Expected: 0.

Counts only, please: no shader names, hashes or disassembly.

Your run was on 473a33ef merged onto 3faac55. I'll ask again once #5128's head is updated to current main.

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

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.

6 participants