Skip to content

Translator/runtime: per-stage code-base slots, a PS reads its code base from dwords 38-39 (SHADER-67) - #1705

Closed
AGVibecoder wants to merge 1 commit into
ProjectLeonida:mainfrom
AGVibecoder:shader-67-per-stage-code-base
Closed

AGVibecoder wants to merge 1 commit into
ProjectLeonida:mainfrom
AGVibecoder:shader-67-per-stage-code-base

Conversation

@AGVibecoder

Copy link
Copy Markdown
Contributor

SHADER-67: per-stage code-base slots in the translator and harness (open question 14; the follow-up to the E_NOTIMPL #291 left behind). No Depends on: it builds on main as it is (#291 / #1083 and the bindless set 0 are already there).

Problem

A VS (or MS) and a PS in one pipeline can both read code-embedded data through s_getpc_b64. The corpus has 54 VS, 93 PS and 266 MS that do (tools/feature_scan.py, docs/d3d12_vulkan.md §5.2). But the push-constant block had one code-base slot, dwords 34–35. So CreateGraphicsPipelineState refused any pair where both stages read it, logging "two stages read their code base; one push-constant slot holds one (question 14)" and returning E_NOTIMPL.

Change

Translator (tools/rdna2spirv.py, code_base_dword())

  • A pixel shader that reads its code base now takes it from dwords 38–39, so its block is 160 bytes. Every other stage keeps 34–35.
  • The PS slot isn't 36–37 because --mesh-compute modules use 36–37 for their output address, and the following draw pushes that address to mesh_compute_vs.py's VS, in the same pipeline as the PS.
  • Shaders that don't read a code base produce byte-identical SPIR-V.
  • %UserData now runs up to the last dword the shader reads, with explicit members and no offset gaps.
  • spirv/index.json gains code_base_dword (34 or 38) next to uses_code_base. push_constant_bytes is now the real block size: 144, 152 for --mesh-compute, or 160.

Runtime

  • CreateGraphicsPipelineState gives each code-base stage its own FSSM blob in the arena, with its VA in that stage's slot.
  • The layout's push-constant range is the stages' largest push_constant_bytes, and push_constants_fit() checks that size.
  • Replay pushes push_bytes for draws and 144 bytes for dispatches. ShaderUserData now covers dwords 0–39, with static asserts on the size and the dwords 38–39 offset.
  • Two stages whose index entries name the same slot are still refused, now with both shader names in the log. That happens with an index translated before this change, whose PS has no code_base_dword and so defaults to 34.
  • GS pipelines aren't built yet. If they are, a GS would share 34–35 with the VS, but 0 of the corpus's 195 GS keep s_getpc_b64 for data.

Harness (vk_harness)

  • VKH_CODE=vs,ps puts code bases in dwords 34–35 and 38–39.
  • The push-constant range is 160 bytes wherever the device's maxPushConstantsSize allows it, and unchanged otherwise.

Docs: the contract's user-data row in docs/design.md; in docs/d3d12_vulkan.md, section 5.2 (now "Implemented"), the code-base row of §5, the M1 refusal and push-constant notes, and open question 14.

Tested

Windows 11 Pro 26200, Ryzen 9 5900X, AMD Radeon RX 7900 XTX (AMD proprietary 26.10.1 / LLPC, Vulkan 1.4.359), MSVC 19.44 (VS Build Tools 14.44) + Ninja, Vulkan SDK 1.4.363, Python 3.12.10. Everything below was run on the rebased commit 1a74469c.

  • python -m compileall -q make_game.py tools tests and python -I make_game.py --help: ok.
  • python -m unittest discover -s tests/semantic: 1402 tests, OK. This includes the new tests/semantic/test_code_base_slot.py:
    • A translated PS runs in spirv_mini_interp with different words in dwords 34–35, 36–37 and 38–39. It must return the 38–39 code base plus the next-instruction offset, with a carry into the high dword.
    • The VS keeps its 144-byte block and its %c3/%c4 access chain.
    • A PS without a code base stays at 144 bytes.
    • The index fields are checked.
  • tests/test_shader_meta.py, test_feature_scan.py, test_feature_scan_bad_index.py, test_run_gpu_tests.py, test_shader_index.py: all pass.
  • tests/test_mesh_compute.py (pytest): 9 passed, 13 skipped. The skipped ones need llvm-mc.
  • spirv-val --target-env vulkan1.3 on the translated VS and PS: valid.
  • Runtime build (cmake -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo, -j 4): no errors, and no warnings in the changed files.
  • ctest: 92 pass. Skipped: bink_decode, bink_audio, fsr_dispatch and bvh_builder, which need the package DLLs. gameinput is disabled upstream.
    • loader failed once in the parallel run on its own guard ("PE images ... not readable straight away ... 97 ms", security-software scanning). Run alone it passed twice, in the default temp folder and with LEONIDA_TEST_DIR on another drive. This change doesn't touch the loader.
  • d3d12_draw_test on the GPU, with LEONIDA_VK_VALIDATION=1: all pixels match and there are no validation messages. Its new draw:
    • One pipeline whose VS reads a constant from its own FSSM blob through its code base (dwords 34–35) and whose PS reads one from its own blob (dwords 38–39). The constants are red in the VS blob and green in the PS blob, and each blob has 0 at the other's offset, so the top-right quadrant is yellow only if each stage reads its own blob.
    • A pair whose PS index entry has no code_base_dword is refused with E_NOTIMPL.
    • Mutation check: without data.ps_code_base = ... in replay, the test fails with that quadrant red.
  • vk_harness on the GPU with a translated PS: a hand-written listing (s_getpc_b64 → s_load_dwordx2 from its own code → export) was translated, assembled with spirv-as and drawn with VKH_CODE=0x1000,0x2000. The data sits at code base + 4 in the heap, with a decoy at the VS's address. Result: 4096/4096 pixels (0x600dc0de, 0x12345678, 0x2004, 0).

Not run: an .asm GPU fixture in runtime/test/shaders + check.py / scripts/run_gpu_tests.py. This machine has no llvm-mc with the amdgcn target, so build.py can't assemble fixtures, and I didn't add a fixture I couldn't run. The vk_harness run above covers the same path, with SPIR-V from the translator under test, but without llvm-mc. A follow-up can add ps_code_base.asm with VKH_CODE/VKH_HEAP entries in both PIXEL_ENV tables once llvm-mc is available. Nothing was tested on NVIDIA, Intel or MoltenVK.

No game data: every shader and program in the tests is hand-written.

AI-assisted.

— Claude Opus 5.5 (high effort), AI-assisted

🤖 Generated with Claude Code

…ords 38-39 (SHADER-67)

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

- Translator: a PS that reads its code base takes it from dwords 38-39
  (block 160 bytes); every other stage keeps 34-35. 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
  unchanged.
- Runtime: each code-base stage gets its own FSSM blob in the arena and
  its VA in its slot; the layout's range is the stages' largest block.
  Two stages naming the same slot (an index from before this change)
  are refused with both names logged.
- vk_harness: VKH_CODE=vs,ps fills both slots; the range is 160 bytes
  where the device has them.
- Tests: tests/semantic/test_code_base_slot.py; d3d12_draw_test draws a
  VS and a PS that each read a constant from their own program.
- Docs: design.md user-data row, d3d12_vulkan.md section 5.2, question 14.

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

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Independently verified commit 1a74469c on Windows 11 (MSVC 19.44, Ninja, VK_LOADER_LAYERS_DISABLE=~implicit~, synthetic scenes):

  • Python: python -m unittest tests/semantic/test_code_base_slot.py -v — 4/4 passed.
  • NVIDIA GeForce RTX 5080 (driver 616.92, Vulkan 1.4.351):
    • d3d12_draw_test.exe: PASS (exit=0) — including the new VS+PS separate code-base pass (a VS and PS that each read their own code base).
    • d3d12_translated_draw_test.exe (M1.5): PASS (exit=0) — 4064/4096 exact, 32 diagonal edge pixels.
    • d3d12_m2_textured_test.exe (M2): PASS (exit=0) — 4096/4096 exact, error 0, sampler control 1536 pixels differ.
    • d3d12_m3_frame_test.exe (M3): PASS (exit=0) — 6 frames, 3 in flight, 4096/4096 exact across 3 targets/frame, 3555 occlusion samples exact.
  • AMD Radeon(TM) Graphics (driver 26.10.1, Vulkan 1.4.315):
    • d3d12_draw_test.exe, d3d12_translated_draw_test.exe, d3d12_m2_textured_test.exe, d3d12_m3_frame_test.exe: 4/4 PASS (exit=0) with the same exact pixel and occlusion counts.
  • Note: VK_LAYER_KHRONOS_validation is not installed locally.

AI Assisted.

@Thatters

Copy link
Copy Markdown
Contributor

Tested 1a74469c on an RTX 5070 Ti (driver 621.24), Windows 11 Pro 25H2, MSVC 14.51 + Ninja (scripts/build_runtime.ps1): pass, with the validation layer on. This adds validation and the full GPU suite to @Vonsek's 5080 run.

Check Result
d3d12_draw_test, LEONIDA_D3D12=vulkan LEONIDA_VK_VALIDATION=1 pass, 0 VUIDs; the new pass "a VS and PS that each read their own code base" matches all 4096 pixels
ctest -j 4, validation on 95/95 passed, 0 VUIDs. With LEONIDA_GAME_DIR set to my own package copy, fsr_dispatch and bvh_builder also pass (the Bink tests skip without a carved movie)
scripts/run_gpu_tests.py, native 125/163 passed, 0 failed (38 skipped: 30 wave64-only, 8 milestone fixtures run by their D3D12 tests)
scripts/run_gpu_tests.py, LEONIDA_W64_EMULATE=1 155/163 passed, 0 failed (the same 8 fixtures skipped)
unittest discover -s tests/semantic 1402 tests OK, including test_code_base_slot.py

Build: one warning, the existing C4459 on extent (vulkan_replay.h:1930), not from this PR.

AI-assisted.

@kapitche

Copy link
Copy Markdown
Collaborator

Tested on Windows 11 with two GPUs on one machine (runtime built from this PR's head, MSVC 14.51): pass. ctest: 95/95 on the RTX 4080 SUPER. The Intel UHD 770 failures are the known ones in #1548: d3d12_vulkan_device, d3d12_descriptor, d3d12_present, d3d12_m3_frame.

@erLCoder

erLCoder commented Nov 22, 2026 •

Copy link
Copy Markdown

Independently tested 1a74469cddac2bde8c77033533fff95ecda3a54e on NVIDIA GeForce GTX 1660 Ti (Turing), driver 581.42, Windows 10 Enterprise 22H2 build 19045.6466. Vulkan device API 1.4.312, loader 1.4.309, maxPushConstantsSize=256. Build: Visual Studio 2022, MSVC 19.39.33523, Windows SDK 10.0.26100, CMake 4.1.0 (VS generator), glslang 16.6.0, Python 3.14.2; locally fetched Khronos Vulkan headers.

  • d3d12_draw_test: PASS, including the distinct VS+PS code-base draw; 4096/4096 pixels matched and the stale same-slot PS was refused as expected. This test uses hand-written GLSL fixtures.
  • Semantic suite: 1402 tests run, OK (2 skipped); test_code_base_slot.py: 4/4 pass. compileall: pass.
  • Initial CTest run, before installing the translated-shader tools or validation layer: 86 passed, 1 failed, 5 skipped, 1 disabled (93 registered). d3d12_vulkan_device failed on invalid-argument comparisons with the Windows 10 host; repeated twice and reproduced on upstream main 89b047cd. This appears to match d3d12_vulkan_device_test: skip invalid-argument cases a Windows 10 or elevated host can't compare #1737.
  • Baseline main draw test failed twice at the bundle-blend check after fence signal 4 followed 20; separately covered by Tests: wait for the bundle blend draws in d3d12_draw (fence 21, not 4) #1793 and absent from this PR head.

Follow-up on the same commit and hardware, with conda-forge llvm-mc 23.1.6 (AMDGPU), SPIR-V Tools 2026.4, and VK_LAYER_KHRONOS_validation 1.4.357. Implicit overlay layers disabled; validation was on for these runs:

  • d3d12_draw_test: PASS again, 4096/4096, including distinct VS+PS code bases.
  • d3d12_translated_draw_test (M1.5): PASS; 4064/4096 pixels exact and 32 triangle-edge pixels clear, as expected. VS and PS were assembled from synthetic RDNA2 and translated to SPIR-V.
  • d3d12_m2_textured_test: PASS; 4096/4096 pixels, error 0, sampler negative control differs in 1536 pixels.
  • d3d12_m3_frame_test: PASS; 6 frames, 3 in flight, 4096/4096 pixels across three targets per frame, 3555 occlusion samples exact.
  • vk_harness: PASS with a hand-written RDNA2 PS translated by this commit and a translated VS. VKH_CODE=0x1000,0x2000; PS reads words at 0x2004 with decoys at 0x1004. All 4096 pixels matched (0x600dc0de, 0x12345678, 0x2004, 0). The Vulkan loader confirmed the validation layer was inserted.
  • No VUID or validation error/warning appeared in these five test logs. Full CTest was not rerun with validation enabled. No game data was used.

AI-assisted (OpenAI Codex).

@AGVibecoder

Copy link
Copy Markdown
Contributor Author

Closing this PR. Since #2046 (--pc-tail-ubo) landed, SHADER-67's second code-base slot in the push-constant tail needs a design decision about where the PS code base goes in the tail uniform block, so it's no longer a plain rebase. Thanks to everyone who tested it (RTX 5080, 5070 Ti, 4080 SUPER, GTX 1660 Ti, RX 7900 XTX). Anyone who wants to pick it up again is welcome to start from this branch.

— Claude Opus 5.5 (low effort), AI-assisted

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.

5 participants