Skip to content

Translator: zero-based VS v0/v3 (VertexIndex - BaseVertex, InstanceIndex - BaseInstance); VS is legacy, not NGG (SHADER-33) - #436

Merged
ItsNotRexed merged 8 commits into
ItsNotRexed:mainfrom
S1m0ne1997:shader-33-vs-inputs
Nov 22, 2026
Merged

ItsNotRexed merged 8 commits into
ItsNotRexed:mainfrom
S1m0ne1997:shader-33-vs-inputs

Conversation

@S1m0ne1997

Copy link
Copy Markdown
Contributor

What and why

SHADER-33 (#156): confirm the vertex shader input layout, which docs/design.md marked "unverified" (NGG or legacy). It's legacy. Checking it turned up a real translator bug in the same two registers.

1. Legacy VS, measured (scripts/vs_input_survey.py, all 2,236 VS of 1.0.2.1):

  • No NGG markers at all: 0 exp prim, 0 s_sendmsg (so no GS_ALLOC_REQ), 0 v_mbcnt; every VS exports pos0 and param.
  • Input VGPRs match Mesa's GFX10 legacy-VS layout (radv_shader_args.c, declare_vs_input_vgprs, not is_ngg): v0 vertex id, v1 unused, v2 primitive id, v3 instance id. No VS reads v1/v2 on entry. 788 start with the fetch call and read attributes from v4 up. Of the other 1,448, 1,052 read v0 only, 386 read v0 and v3, 10 read neither. An NGG VS would have its inputs at v5/v8.

2. Those ids are zero-based on the hardware; Vulkan's aren't. Mesa ac_nir_lower_intrinsics_to_args.c: vertex_id is load_vertex_id_zero_base and instance_id is load_instance_id, with the bases in the base_vertex / start_instance SGPRs. Vulkan's VertexIndex / InstanceIndex include firstVertex/vertexOffset / firstInstance, so any draw with a non-zero base gave the shader different ids from the console.

  • New GPU test runtime/test/shaders/vs_base_ids.asm + ps_flat, drawn with the new vk_harness option VKH_DRAW=16,7 (firstVertex, firstInstance): main gives 0x7111 on every pixel (16 and 7 leaked into v0/v3), this branch gives 0x101 like the hardware. Wired into check.py and run_gpu_tests.py.
  • Fix in tools/rdna2spirv.py: v0 = VertexIndex - BaseVertex, v3 = InstanceIndex - BaseInstance (SPIR-V DrawParameters). Every VS's metadata now lists the device feature shaderDrawParameters, which vk_harness, vk_pipeline_check, vk_bindless_test, vk_texture_test and the runtime's Vulkan device (d3d12_vulkan_device.cpp, logged if missing) enable.
  • docs/design.md "Inputs and outputs": the "unverified" note becomes the measured rule, with the references and the GPU check.

@Ryx69: this changes what translated VS need from the device (shaderDrawParameters) and relies on the D3D12 draw's base vertex / first instance reaching vkCmdDraw* unchanged, so it's relevant to D3D-4.

Thanks to @eIBart0: vs_input_survey.py uses the same straight-line read-before-write rule as #240's scripts/audit_shader4_regs.py.

Tested

Windows 11, RTX 4090 driver 621.24, Vulkan SDK 1.4.363.0, llvm-mc 23.1.5 (MSYS2), MSVC 14.44, on main d937b7f:

  • vs_base_ids: 0x101/0x102 on 4096/4096 pixels with VKH_DRAW unset, 16,7 and 1000,3 (before the fix: 0x7111 with 16,7).
  • build.py 57/57 translated and valid; check.py 29 of 53 pass, 24 skipped (wave64 / mesh fixtures), 0 failed; run_gpu_tests.py 35/59 passed, 24 skipped, 0 failed.
  • Corpus, my own copy: all 2,236 VS translate and pass spirv-val; vk_pipeline_check creates all 2,236 VS pipelines on the 4090 (1,057 wave32, 1,179 wave64), 0 failed.
  • pytest tests --ignore=tests/semantic 1332 passed; semantic tests OK (440); ctest 18/18; tests/test_vs_input_survey.py OK. tests/test_shader_checks.py counts the new check.

AI-assisted (Claude Code).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. Counts only.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed (Mesa references and the vs_base_ids GPU test).

🤖 Generated with Claude Code

@AC-Storm-YT

Copy link
Copy Markdown
Contributor

GPU test of #436 at de5da76: pass

  • GPU: AMD Radeon RX 9070 XT (RDNA4, native wave64, subgroup 32..64), driver 32.0.31041.1004, Windows 11
  • Tools: llvm-mc 21.1.8 (WSL Ubuntu 26.04), Vulkan SDK 1.4.363.1, MSVC (VS 2026); runtime and test programs built from this branch
  • run_gpu_tests.py: 70 shaders translated, 70 pass spirv-val; 67/67 passed
  • check.py: 61 of 61 pass, 0 skipped (the wave64 and mesh tests run natively here)

vs_base_ids + ps_flat with vk_harness, 4096 pixels each:

VKH_DRAW this branch main's translator
unset 0x101, 0x102 0x101, 0x102
16,7 0x101, 0x102 0x7111, 0x7112
1000,3 0x101, 0x102 0x34e9, 0x34ea (= 0x101 + 1000 + 0x1000·3)

AI-assisted (Claude Code).

@kacper-serewis

Copy link
Copy Markdown
Contributor

Tested de5da76 on RTX 5070 (Blackwell, subgroup 32..32), NVIDIA 616.56, Windows 11 Pro 26200: pass.

  • runtime rebuilt from the PR (build_runtime.ps1, MSVC 19.50); ctest 19/19 passed
  • run_gpu_tests.py: 70 translated, 70 pass spirv-val; 41/67 passed, 26 skipped (wave64, 32-wide GPU)
  • with LEONIDA_W64_EMULATE=1: 67/67 passed (including the new vs_base_ids)
  • tests/semantic: 462 OK

Tools: llvm-mc 21.1.8 (WSL), Vulkan SDK 1.4.363.0.

@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested de5da76 on RTX 4090 (driver 621.14), Windows 11 26200, MSVC 19.44: pass

  • Clean RelWithDebInfo Ninja build of runtime/ (Vulkan SDK 1.4.363.0), then ctest --test-dir runtime/build --output-on-failure: 19/19 passed.
  • GPU tests with LEONIDA_VK_VALIDATION=1 and VK_LAYER_KHRONOS_validation: d3d12_com_test, d3d12_descriptor_test, d3d12_uint_clear_test, d3d12_vulkan_device_test, vk_command_replay_test all exit 0, with 0 validation messages.
  • python -I scripts/run_gpu_tests.py (LLVM 21 llvm-mc, validation on): 41/67 passed, 26 skipped (wave64/mesh, because this GPU is 32-wide), 0 failed. The new vs_base_ids passes: flat IDs are exact on all 4096 pixels.

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

@dcastell1

Copy link
Copy Markdown
Contributor

Tested de5da76 on an RTX 5090 (Blackwell), Windows 11 Pro 10.0.26300.9974, NVIDIA 621.24 / Vulkan 1.4.357, MSVC 14.44, llvm-mc 21.1.8 (WSL) and SPIRV-Tools 2025.1:

  • run_gpu_tests.py: 70 shaders: 70 translated, 70 pass spirv-val.
  • With LEONIDA_W64_EMULATE=1 (32-wide GPU): 67/67 passed, 0 skipped. This includes the new vs_base_ids: 4096 pixels: flat IDs exact, uv within 0, plus vs_clip, vs_clip_param4 and vs_clip_undeclared (four pixel masks each).
  • Wave64 compute/mesh tests (cs_wavemask, cs_wm_*, cs_w64x_*, ms_w64x_ballot) all pass under the same emulation; the rest of the texture/ALU/draw suite is unchanged.
  • VK_LOADER_LAYERS_DISABLE=~implicit~ was set because ReShade and the Epic/NVIDIA implicit layers are installed.

I did not run the native (non-emulated) suite on this GPU; without emulation the 64-wide tests skip by design. No game files were used.

@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested de5da76 on the 1.0.2.1 package, RTX 4090 (621.14), Win11 26200: the corpus side reproduces on a second copy, and the fix behaves as described. (The earlier 4090 comment covered the GPU suite; this one is the corpus run.)

scripts/vs_input_survey.py over all 2,236 VS (0 without a listing). The numbers are identical to the description:

  • NGG markers: exp prim 0, s_sendmsg 0, v_mbcnt 0. Every VS exports pos0 (2,236) and param (2,236).
  • 788 start with the fetch call, 1,052 read v0 only, 386 read v0 + v3, 10 read neither. No VS reads v1 or v2 on entry. Verdict: legacy (non-NGG) VS.
  • tests/test_vs_input_survey.py: 1 passed. tests/test_shader_checks.py: 15 passed.

Translation, all 2,236 VS, this branch vs its parent 4abd545 (8 shards each, llvm-mc with AMDGPU, Vulkan SDK 1.4.363.0 spirv-val):

  • Parent: 2,236 translated, 2,236 valid. This branch: 2,236 translated, 2,236 valid.
  • On this branch, all 2,236 declare DrawParameters with BaseVertex and BaseInstance, so every VS module changes.
  • vk_pipeline_check on this branch's output creates 2,236/2,236 VS pipelines, 0 failed (1,057 wave32, 1,179 wave64). The parent's output gives the same.

vs_base_ids + ps_flat, vk_harness built from this branch:

  • This branch: 0x101/0x102 on 4096/4096 pixels with VKH_DRAW unset, 16,7 and 1000,3.
  • Negative control, vs_base_ids translated by the parent's rdna2spirv.py with the same harness: 0x101 unset, 0x7111 with 16,7, 0x34e9 with 1000,3. The draw bases leak into v0/v3 exactly as the description says.
  • run_gpu_tests.py --filter: vs_base_ids, ps_flat and gs_tri PASS.

AI-assisted (Claude Code).

@evilclanker9000

Copy link
Copy Markdown
Contributor

I exercised the nonzero base through a synthetic guest's regular D3D12 import, including a V# fetch, submission, fence, and RGBA32_FLOAT readback. This pairs the translator at de5da76c5b0726efc70cf0bdf5369d05f8154f28 with #291 at 2aed1445. The local #291 runtime build includes this PR's device-side query and enable for shaderDrawParameters. The guest uses the source-built shader pair and three draws from #907. Its vertex shader reads the initial v0 directly as an index into its descriptor. I appended nine offscreen sentinel vertices to the same buffer and extended both descriptor bounds to cover them.

On Windows RTX 5080 and AMD Radeon Graphics, gta6rt --run produced exactly the CPU-predicted 64×64 image at both StartVertexLocation=0 and 9: 1,024 pixels in each quad, 496 in the triangle, and 1,552 clear (including the 32-pixel diagonal). I compared all four float channels at every pixel.

Negative control: in the validated VS module only, substitute raw VertexIndex for VertexIndex - BaseVertex in the v0 initializer. Start 0 still matched all 4,096 pixels on both devices. Start 9 instead fetched the in-bounds sentinels and left all 4,096 pixels clear: exactly the predicted 2,544 changed pixels. No Vulkan VUID appeared in the eight runs. This covers the combined synthetic route relevant to #313. AI-assisted verification.

@evilclanker9000

Copy link
Copy Markdown
Contributor

I checked the indexed case at de5da76c5b0726efc70cf0bdf5369d05f8154f28, including a negative vertex offset. The source-built vs_base_ids / ps_flat pair used the existing Vulkan harness with a uint16 index buffer [32,33,34,17,18,16]. I drew three indices at firstIndex 0 or 3, vertexOffset −12 or +4, and firstInstance 0 or 7. All effective vertex indices were nonnegative.

For firstIndex 0 the provoking raw index is 32, so the first two output words should be 0x121, 0x122; firstIndex 3 selects 17, giving 0x112, 0x113. Both words stayed invariant under either base, and Z/W equaled the expected pixel-center coordinates at every one of the 4,096 pixels on RTX 5080 and Radeon 1002:13c0.

Two separately assembled and validated controls replaced only one subtraction at a time. Keeping raw VertexIndex shifted the two flat words by the signed vertexOffset (−12 / +4). Keeping raw InstanceIndex shifted them by 0x7000 only when firstInstance was 7. All four channels matched each prediction in 24 frames per GPU, zero Khronos validation errors. Windows 11 build 26200, NVIDIA 616.92 and AMD 26.10.1. The draw here was directly from the synthetic Vulkan harness.

…dex - BaseInstance); VS is legacy, not NGG (SHADER-33)

Corpus (scripts/vs_input_survey.py, all 2,236 VS): no exp prim, no s_sendmsg, no v_mbcnt, every
VS exports pos0/param, so the console runs them as legacy hardware VS. Their input VGPRs are
Mesa's GFX10 legacy layout (radv_shader_args.c, declare_vs_input_vgprs, not is_ngg): v0 vertex
id, v1 unused, v2 primitive id, v3 instance id; no VS reads v1/v2 on entry, 788 start with the
fetch call and read attributes from v4.

Those ids are zero-based on the hardware (ac_nir_lower_intrinsics_to_args.c: vertex_id is
load_vertex_id_zero_base, instance_id is load_instance_id; the bases come in SGPRs), while
Vulkan's VertexIndex/InstanceIndex include firstVertex|vertexOffset/firstInstance. The
translator now seeds v0 = VertexIndex - BaseVertex and v3 = InstanceIndex - BaseInstance
(DrawParameters; feature shaderDrawParameters in every VS's metadata).

GPU test vs_base_ids.asm, drawn with VKH_DRAW=16,7 (new vk_harness option): 0x101 on every
pixel, where before it was 0x7111. Run by check.py and run_gpu_tests.py. vk_harness,
vk_pipeline_check, vk_bindless_test, vk_texture_test and the runtime's Vulkan device enable
shaderDrawParameters. design.md's "Inputs and outputs" row replaces the "unverified" note.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	scripts/run_gpu_tests.py
#	tests/test_shader_checks.py
#	tools/rdna2spirv.py
@IAmNotSiob

Copy link
Copy Markdown
Contributor

rebased this onto current main for you, since it's the open half of "VS input layout unverified" and it'd be a shame to lose it to conflicts ^^

branch: IAmNotSiob:shader-33-rebased (your commits + one merge of main + one small fix). i didn't push to your branch, so grab it if you want:

git fetch https://github.com/IAmNotSiob/Leonida shader-33-rebased
git merge FETCH_HEAD   # fast-forward onto your branch

what changed:

  • 3 conflicts, all mechanical, kept both sides: the DrawParameters feature next to main's new FMAKHR in rdna2spirv.py, vs_base_ids next to main's GET_LOD_TESTS in run_gpu_tests.py, and the fixture count in test_shader_checks.py (now + 11)
  • one real fix: tests/semantic/test_wave64_halves.py runs vertex shaders through spirv_wave_interp, and with your VertexIndex - BaseVertex those 10 tests hit BuiltIn BaseVertex is not modelled. the harness now passes BaseVertex/BaseInstance = 0

tested on the rebased branch:

  • tests/semantic: OK (was 10 errors before the harness fix)
  • pytest tests/test_shader_checks.py tests/test_translate.py: 524 passed, 2 skipped
  • GPU, NVIDIA RTX 6000 Ada (615.71, Linux): run_gpu_tests.py --filter vs_base_ids: PASS, 4096 pixels, flat ids exact

TL;DR: rebased + fixed the 10 semantic tests your change broke, and the new vs_base_ids test passes on a real gpu. branch is ready to pull if you want it. AI-assisted (Claude Code).

# Conflicts:
#	runtime/test/shaders/check.py
#	scripts/run_gpu_tests.py
#	tests/test_shader_checks.py
@S1m0ne1997

S1m0ne1997 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @IAmNotSiob for the rebase and for catching the 10 test_wave64_halves errors (the wave interpreter had no BaseVertex/BaseInstance). I took your branch as is: your 4e99a49 is in the history. Then I merged the 148 newer main commits (1f32be4). The three conflicts kept both sides: vs_base_ids next to TEXTURE_CONSTANTS in check.py, next to SIGNED_MIP_TEST in run_gpu_tests.py, and in the fixture count (+ len(check.TEXTURE_CONSTANTS) + 10).

Update: main moved again, so 8cc972b merges it once more (one conflict in check.py, kept base_ids_test and wide_pixel_skip side by side). On 8cc972b: tests/semantic OK, test_shader_checks.py + test_vs_input_survey.py 62 passed, and on the 4090 run_gpu_tests.py --filter vs_base_ids PASS (4096 pixels, flat IDs exact). The full run below is from 1f32be4.

Tested 1f32be4 on RTX 4090 (NVIDIA 621.24), Windows 11 26300, MSVC 14.44, Vulkan SDK 1.4.363.0, llvm-mc 23.1.5:

  • check.py: 117 of 151 pass, 34 skipped, vs_base_ids ok; run_gpu_tests.py: 128/167 passed, 39 skipped, no failures; d3d12_vulkan_device_test with validation: OK.
  • tests/semantic: OK. pytest: the 17 failures (test_tile_probe without numpy, test_amd_swizzle, test_make_game_assets_pools, test_world_index) fail the same way on current main here, not touched by this PR.
  • ctest: d3d12_draw fails its bundle-blend pixel the same way on current main 89b047c with this GPU, so it isn't from this PR. The three new translated-draw tests (d3d12_translated_draw, d3d12_m2_textured, d3d12_m3_frame) pass. On this machine their shader-build step only works when run by hand; under MSBuild it exits 1 the same way on main.

AI-assisted (Claude Code).

# Conflicts:
#	runtime/test/shaders/check.py
@mokkymiah

Copy link
Copy Markdown
Contributor

Tested 94f392ac on NVIDIA GeForce RTX 3070, driver 621.01, Vulkan 1.4.357, Windows 11 x64.

  • Built the full runtime and harnesses from this head (MSVC 19.44, Ninja, RelWithDebInfo).
  • python -I scripts/run_gpu_tests.py --filter vs_base_ids --build runtime/build: PASS, 1/1. The runner draws with VKH_DRAW=16,7 and ps_flat sees vertex 0 of instance 0; 4,096 pixels flat IDs exact, uv within 0.
  • Both shaders (vs_base_ids and ps_flat) translated and passed spirv-val (2/2).

This adds a third NVIDIA data point for the measured legacy-VS layout and the zero-based v0/v3 fix. No game data used. AI-assisted.

@zneel

zneel commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested 72a38ad2 on AMD Radeon RX 9070 XT (RDNA4, subgroup 32..64), AMD 26.11.2, Windows 11 Pro 10.0.26300, AMD Ryzen 7 9800X3D CPU (64 GB RAM): pass. That's the current head; the earlier 9070 XT run here was at de5da76.

Runtime and test programs built from this head (MSVC 14.51, Ninja, RelWithDebInfo), native Windows llvm-mc 21.1.8, Vulkan SDK 1.4.363.0, Python 3.12.10. No game files.

run this head main 4eb6155d
run_gpu_tests.py (whole suite, native wave64) 168/181, 0 failed; vs_base_ids PASS, 4096 pixels, flat IDs exact 167/180, 0 failed
same with LEONIDA_W64_EMULATE=1 168/181, 0 failed; vs_base_ids PASS 167/180, 0 failed
ctest 116/117; d3d12_draw fails same, d3d12_draw fails on main too
pytest tests/test_shader_checks.py tests/test_vs_input_survey.py 63 passed, 203 subtests
tests/semantic/test_wave64_halves.py 57 OK

The 13 skips are the same on both: the d3d12-run fixtures, plus ps_w64x_readlane, which #2085 addresses.

AI-assisted (Claude Code); I checked the results.

@crastinator

Copy link
Copy Markdown
Contributor

Tested 97afefd merged into main ec8ec4c9 (clean merge) on AMD Radeon RX 6700 XT (Navi 22, gfx1031, subgroup 32..64), RADV Mesa 26.3.1, CachyOS, Linux 7.3.5, GCC 16.2.1, llvm-mc 23.1.5: pass.

  • The new vs_base_ids passes natively ("4096 pixels: flat IDs exact, uv within 0").
  • Whole run_gpu_tests.py: 176/190 native, 176/190 with LEONIDA_W64_EMULATE=1 (the skips are the D3D12-only fixtures and ps_w64x_readlane, as on main). ctest 37/37.
  • tests/semantic/test_wave64_halves.py, tests/test_shader_checks.py and tests/test_vs_input_survey.py: 138 passed.

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

@ItsNotRexed
ItsNotRexed merged commit 51d954a into ItsNotRexed:main Nov 22, 2026
3 of 4 checks passed
evilclanker9000 pushed a commit to evilclanker9000/Leonida that referenced this pull request Nov 23, 2026
The Inputs and outputs row lost the qualifier when ItsNotRexed#436 merged
(51d954a), 31 s before ItsNotRexed#2063 landed. Restate the rule the translator and
tests/test_translate.py::test_mrtz_depth_only_from_an_enabled_x enforce,
including that an export without Z must not declare DepthReplacing.

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.