Skip to content

rdna2spirv: a pixel shader's k-th colour export goes to its k-th declared SV_Target (SHADER-210, SHADER-213) - #4033

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
mrp1no:shader-213-sparse-sv-target
Nov 23, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
mrp1no:shader-213-sparse-sv-target

Conversation

@mrp1no

@mrp1no mrp1no commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Closes SHADER-210 and SHADER-213 (claim: #3918 (comment)).

What was wrong

A pixel shader's colour exports are packed. exp mrtK writes the K-th SV_Target the shader declares, not render target K. For example, a shader that declares SV_Target0 and SV_Target2 exports mrt0 and mrt1. The translator decorated mrt1 with Location 1, so the second colour went to render target 1 instead of render target 2.

The change

  • tools/rdna2spirv.py: for a pixel shader, translate reads the sorted SV_Target indices from the OSG1 chunk, using the existing output_signature_full (sysv 64). Export mrtK now becomes %out_mrt<target> with Location <target>.

    • An export past the declared targets is refused (Unsupported), rather than guessed.
    • Without an OSG1 (hand-written listings, run_gpu_tests.py fixtures), mrtK stays render target K, as before.
  • tests/semantic/test_sparse_sv_target.py: 5 tests on hand-built OSG1 containers and listings (no game data):

    • gaps {0,2} (run through the fragment emulator: the 2.0 value lands in %out_mrt2), {1,3} and {0,7};
    • consecutive targets unchanged;
    • no OSG1;
    • an export past the declared targets.

    3 of the 5 fail on main.

  • docs/design.md: the "Inputs and outputs" row says exports are packed and gives the corpus count below (SHADER-210).

Measured on the 1.0.2.1 corpus (counts only)

  • 221 of 10,500 pixel shaders declare non-consecutive SV_Targets. All 221 export mrt0..mrtk-1, one per declared target; none exports past them.
  • With this PR, all 221 translate and pass spirv-val, and their output locations change in all 221.
  • An 800-module slice of other shaders translates to identical SPIR-V.
  • Full semantic suite: 7,461 tests. The only failures are the same 10 that main has today (test_loop_ballot_barrier, test_cmp_class_f64 (TEST-853), test_vcmp16_opsel, test_mutations).

Note for #3544

@Vonsek's #3544 indexes mrt_types by the export index. With this PR, the output name and location use the declared target. If both land, the type lookup should use the same target (self.mrt_targets[k]). Whichever merges second needs a one-line adjustment, and I'm happy to do it.

🤖 Generated with Claude Code

@SweatierKey

Copy link
Copy Markdown
Contributor

Tested 0b00efcb on Linux (WSL2, Python 3.13.5, spirv-as/spirv-val, CPU only, no GPU, no game files): pass.

  • Red/green on main (aa2988cc):
    • Tests only against main: python3 -I tests/semantic/test_sparse_sv_target.py -v fails 3/5 tests (all sparse-target gap cases and the out-of-range export rejection; [0, 2] yields {'%out_mrt0': 0, '%out_mrt1': 1} instead of {'%out_mrt0': 0, '%out_mrt2': 2}).
    • Whole PR:
      • tests/semantic/test_sparse_sv_target.py: 5/5 OK.
      • tests/test_semantic_translator_use.py: 3/3 OK.
      • tests/test_docs_repo_paths.py (2/2), tests/test_docs_links.py (6/6), tests/test_text_write_line_endings.py (3/3), tests/test_text_encoding.py (2/2): all OK.
  • SPIR-V validation: Assembled and validated all 4 translated cases (targets=[0, 2], [1, 3], [0, 7], and None/no OSG1) with spirv-as --target-env vulkan1.2 + spirv-val --target-env vulkan1.2 — 4/4 valid.
  • Code read: g.mrt_targets is sorted unique semantic indices for sysv == 64 (D3D_NAME_TARGET), export() remaps both name (%out_mrt{rt}) and tgt (mrt{rt}) before storing into self.outputs[name], and leaves non-OSG1 hand-written listings/fixtures on the identity mapping mrtK -> Location K.
  • Not checked: Corpus-wide translation or GPU execution on sparse-MRT pixel shaders.

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested 0b00efcb36cdd624ab9728581e9ecab1ceb0f4a4 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

Sparse color exports passed on Linux, RTX 5090, driver 595.99.02, at 0b00efcb. I translated the PR's two-export pixel shader with synthetic signatures declaring targets {0,2}, {1,3}, and {0,7}. For each pair, the first declared location returned 1.0 in all four channels and the second returned 2.0. All 4,096 pixels matched on each readback. The contiguous {0,1} and absent-signature controls produced the same values at locations 0 and 1.

The SPIR-V passed Vulkan 1.2 validation. A temporary adaptation of the head's draw harness attached one RGBA32F image at the selected output location per draw, with preceding locations unused. This checked each location separately across ten draws.

AI-assisted verification.

@requiemsnkrp

Copy link
Copy Markdown

Tested 0b00efcb (merged onto main aa2988cc) on NVIDIA GeForce RTX 3060, driver 32.0.16.1714, Windows 11, MSVC 19.51, Vulkan SDK 1.4.363.0: pass.

  • tests/semantic/test_sparse_sv_target.py (llvm-mc 21): Ran 5 tests ... OK. As a negative control, with tools/rdna2spirv.py from main swapped in: FAILED (failures=3), matching the description.
  • Full ctest (-E "present|swapchain|frame_pacer", LEONIDA_VK_VALIDATION=1, llvm-mc build, so the ctest shader fixtures go through this translator): 100% tests passed, 0 tests failed out of 148 (9 skipped/disabled, same set as main: shader_library_corpus, bink_decode, bink_play_headless, fsr_dispatch, bvh_builder, bink_audio, gameinput, d3d12_rgb9e5_rtv, d3d12_first_light).
  • Not run: the full semantic suite and the corpus count, which needs game files.

AI-assisted (Claude). No game files.

@AwkGrep256

Copy link
Copy Markdown
Contributor

Tested 0b00efc (base main aa2988c) on two AMD GPUs under Linux RADV: pass, no regression.

Setup: CachyOS, kernel 7.3.5, Mesa 26.3.1, llvm-mc 23.1.5; test programs built from this head. The PR changes only the translator, so the programs are the same as on main.

RDNA2: Ryzen 7 9800X3D iGPU (RADV RAPHAEL_MENDOCINO, gfx1036) RDNA4: RX 9070 XT (RADV GFX1201)
run_gpu_tests.py 196/210 passed, 14 skipped, 0 failed 196/210 passed, 14 skipped, 0 failed
shaders 214 translated, 214 pass spirv-val the same

The 14 skips are the usual ones on these cards (run by d3d12_*_test).

CPU:

Not covered: the GPU fixtures are hand-written listings without an OSG1, so on hardware they exercise the unchanged "no OSG1, mrtK stays target K" path. The sparse-target remapping itself is checked by the new semantic test only. vk_harness binds one colour attachment (colorAttachmentCount = 1, vk_harness.cpp:551). The one multi-target fixture, ps_m3 (exp mrt1), runs through d3d12_translated_draw_test, which skips here. So a hardware check of the remap would need a sparse-target fixture on that D3D12 path, or a second attachment in vk_harness.

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

@tedin7

tedin7 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 0b00efc (PR #4033 head, on top of main aa2988c) on NVIDIA GeForce RTX 5080, driver 580.65.06, Ubuntu 22.04.5 (Linux, Vulkan 1.4.363.1 SDK, llvm-mc 21): pass.

The PR only changes tools/rdna2spirv.py (plus a test and docs), so no C++ rebuild was needed. I reused the existing runtime/build/linux build.

Commands and results:

  • python3.13 -m pytest tests/semantic/test_sparse_sv_target.py -q: 5 passed. With tools/rdna2spirv.py restored to main aa2988c, the same file gives 3 failed, 2 passed, so the new tests do guard the fix.
  • python3.13 -I scripts/run_gpu_tests.py --build runtime/build/linux --llvm-mc /usr/lib/llvm-21/bin/llvm-mc: 164/210 passed, 46 skipped, 0 failed. Same as main.
  • LEONIDA_W64_EMULATE=1 with the same command: 195/210 passed, 15 skipped, 0 failed. Same as main.

I did not run the full 7,461-test semantic suite or the 1.0.2.1 corpus counts from the PR body (no game data here).

AI-assisted run.

@TreXito

TreXito commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 0b00efcb (base main aa2988cc) on AMD Radeon RX 7800 XT (RDNA3, gfx1101, subgroup 32..64), AMD driver 26.11.2 (32.0.32029.2010, LLPC), Windows 10 Pro 22H2 build 19045.7846, MSVC 19.44 (VS 17.14) RelWithDebInfo via scripts\build_runtime.ps1 with LLVM_MC set (so CMake translates the D3D12 fixtures with this PR's rdna2spirv.py), Vulkan SDK 1.4.363.0, llvm-mc 21.1.8: pass, no regression. This adds the Windows + AMD driver data point; the earlier AMD runs were on RADV.

main aa2988cc 0b00efcb
python -I tests/semantic/test_sparse_sv_target.py FAILED (failures=3) (main's rdna2spirv.py swapped in) Ran 5 tests, OK
run_gpu_tests.py --build runtime\build 196/210 passed, 14 skipped, 0 failed 196/210 passed, 14 skipped, 0 failed; 214 translated, 214 pass spirv-val
ctest -j 4 (153 tests) 3 failed: loader, d3d12_vulkan_device, d3d12_conservative_raster 2 failed: d3d12_vulkan_device, d3d12_conservative_raster

None of the ctest failures come from this PR:

  • loader fails on main only under -j 4, from Defender scanning newly written PE files (46 ms > 25 ms threshold); 5/5 pass in isolation.
  • d3d12_vulkan_device and d3d12_conservative_raster fail on both. The first is 3 invalid-argument cases the AMD host runtime refuses and the layer accepts. The second appears under ctest's LEONIDA_HOST_ADAPTER=warp on Windows 10's WARP, and passes 10/10 against the AMD host.
  • d3d12_translated_draw, d3d12_m2_textured, d3d12_m2_formats, d3d12_m3_frame and d3d12_m3_first_pass pass with the translated fixtures.

Same limit as @AwkGrep256 noted: the pixel-shader fixtures carry no SV_Target OSG1 (runtime/test/shaders/build.py writes OSG1 only for the VS clip and GS fixtures), so on hardware this exercises the unchanged "no OSG1" path. The remap itself is covered by the semantic test only.

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

@sovljanskii

Copy link
Copy Markdown
Contributor

Whole-corpus check with my own 1.0.2.1 copy (Apple M4, Python 3.15, SPIRV-Tools from Homebrew): pass. Your counts hold exactly, and the rest of the PS corpus is byte-identical, not only an 800-module slice.

Method. I ran translate() on every PS listing (12,101, chosen from the carve index) in main aa2988cc and in 0b00efcb, and compared the modules shader by shader. The PR side also recorded Gen.mrt_targets. I assembled and validated every module that differs (spirv-as + spirv-val --target-env vulkan1.2).

count
PS translated on main / on this head 12,101 / 12,101
PS that declare SV_Targets (OSG1) 10,500
non-consecutive SV_Targets 221
modules that differ 221, exactly those
differing modules that pass spirv-val 221 / 221
other PS, byte-identical 11,880
refused by the new past-the-targets check 0

So no shader with consecutive targets changes, and the 10,500 in your text is the number of PS that declare an SV_Target. The other 1,601 have none, keep the no-OSG1 path, and are identical.

No game data (counts only). AI-assisted.

@slopradish slopradish left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

CPU verification: checked out pull/4033/head in an isolated worktree and ran pytest -q tests/semantic/test_sparse_sv_target.py: 5 passed.

This fixes a real correctness bug with strong evidence: the corpus count (221 PS with non-consecutive SV_Target declarations, all exporting packed mrt0..mrt(k-1)) makes the old mapping unambiguously wrong, and the 5 synthetic tests cover sparse target sets {0,2}, {1,3}, {0,7}, the consecutive case, and the export-past-declarations refusal. The fallback for listings without OSG1 preserves existing GPU fixtures.

The change is properly scoped: it reads the OSG1 through the existing output_signature_full helper rather than adding a second signature parser. Contract table updated with the count and the reasoning. No GPU run from me; hardware behaviour remains the author's measurement. No game files involved.

mrp1no and others added 2 commits November 23, 2026 04:39
…ared SV_Target (SHADER-213)

Colour exports are packed: a pixel shader that declares SV_Target0 and
SV_Target2 exports mrt0 and mrt1. The translator decorated exp mrtN with
Location N, so the second value went to render target 1 instead of 2. It
now reads the shader's OSG1 (system value 64, SV_Target n in register n)
and maps export K to the K-th declared target. The index's outputs list
follows, so it names render targets.

- Without an OSG1 (hand-written listings, run_gpu_tests.py fixtures), mrtK
  stays location K.
- An export past the declared targets is Unsupported.

tests/semantic/test_sparse_sv_target.py covers gaps at 2, 3 and 7,
consecutive targets, no signature, and an export past the declared ones.
It runs the [0, 2] case through the interpreter. With the old translator,
3 of its 5 tests fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…R-210)

The Inputs and outputs row said exp mrtN goes to location N. Counted over
the 1.0.2.1 corpus (counts only):

- 10,500 pixel shaders have SV_Target outputs, and every one of the 29,290
  targets keeps register = semantic index.
- 221 of them declare non-consecutive targets, and all 221 export
  mrt0..mrt(k-1). The k-th export is the k-th declared target, never
  register N.

The row now states the rule, the count and the test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mrp1no
mrp1no force-pushed the shader-213-sparse-sv-target branch from 0b00efc to 396c2c7 Compare November 23, 2026 03:40
@mrp1no

mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main (396c2c73) to clear a docs/design.md conflict: main added the "Partial pixel color exports" row next to the "Inputs and outputs" row this PR edits, and both are kept. No code change. test_sparse_sv_target, test_export_valid_mask and the docs tests pass; the export/target subset of the semantic suite shows only main's known test_loop_ballot_barrier failure. AI-assisted (Claude Code).

@Kev-Ball

Copy link
Copy Markdown

Tested 396c2c73 on an NVIDIA GeForce RTX 3080 Ti (32-lane subgroups), driver 620.73.04, CachyOS Linux 7.3.5-2-cachyos. I rebuilt the Vulkan harnesses from this revision and selected the NVIDIA device explicitly.

Checked sparse color locations with an actual Vulkan readback using the PR’s test_sparse_sv_target.py synthetic OSG1 and unmodified translator. I adapted a scratch copy of vk_harness to route one selected fragment output location to its RGBA32F readback attachment, with the other subpass slots set to VK_ATTACHMENT_UNUSED and the blend attachment count matching the slot count. I assembled/validated each translated module for Vulkan 1.2 and drew the PR’s two-export fixture twice per layout. For {0,2}, {1,3}, and {0,7}, every first slot read (1,1,1,1) and every second slot read (2,2,2,2) at 4,096/4,096 pixels. Six draws, all exact.

AI-assisted verification with synthetic inputs.

@throny

throny commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 396c2c7 on NVIDIA RTX 4080, driver 610.88, Windows 11: pass

This is the rebased head on 7b7d73f8. Ran (MSVC 19.44 / VS 2022 BuildTools, Ninja, RelWithDebInfo, Vulkan SDK 1.4.363.0, native llvm-mc 23.1.6; this PR has no runtime C++ change, so the test programs were built from its merge base main 7b7d73f8):

  • python -I scripts/run_gpu_tests.py --build <build>: 170/217 passed, 47 skipped, 0 failed.
  • tests/semantic/test_sparse_sv_target.py (run from tests/semantic): 5 tests OK.

At the earlier head 0b00efcb I also checked the corpus side on my 1.0.2.1 copy. All 221 pixel shaders with non-consecutive SV_Targets translated and passed spirv-val at both base and PR, and every one moved its colour outputs onto declared SV_Target locations. 300 ordinary pixel shaders were byte-identical. Those numbers are from 0b00efcb, not re-run at this head.

AI-assisted.

@bobcatchris

Copy link
Copy Markdown
Contributor

Independent Apple Silicon / MoltenVK check of head 396c2c73 (hardware reports Apple M2 / Mac14,3, not M1), macOS 26.4, MoltenVK 1.4.2, subgroup range 4..32, llvm-mc 23.1.6. Translator/Python-only; one harness built from base 3eb4410b shared for the A/B.

  • python -m unittest discover -s tests/semantic -p test_sparse_sv_target.py: 5/5 OK.
  • python -I scripts/run_gpu_tests.py --build <shared harness>: 152/217 passed, 58 skipped, 7 failed — the known MoltenVK expected set, identical per test to base 3eb4410b (0 status differences).

This PR adds no GPU fixture of its own, so the pixel-export behaviour is checked here only by the semantic tests, not on hardware. No code changes, no game data; counts only.

AI-assisted.

@ItsNotRexed
ItsNotRexed merged commit e09df73 into ItsNotRexed:main Nov 23, 2026
2 of 4 checks passed
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.