Skip to content

gs-compute: draw geometry shaders on devices without the geometry stage (SHADER-1092) - #7336

Closed
autowert66 wants to merge 21 commits into
ProjectLeonida:mainfrom
autowert66:gs-draw-path
Closed

autowert66 wants to merge 21 commits into
ProjectLeonida:mainfrom
autowert66:gs-draw-path

Conversation

@autowert66

@autowert66 autowert66 commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as: SHADER-1092

What and why

A geometry shader on a device without the Vulkan geometry stage (Apple GPUs) now has two draw paths, both from one capture module:

rdna2spirv.py --gs-compute publishes the capture module that tools/spirv_gs_to_compute.py lowers (records + meta buffers, one invocation per input primitive) next to the unchanged native module, with the layout in index.json as gs_compute. tools/gs_compute_stages.py generates the other two modules per GS, on the pattern of tools/mesh_compute_vs.py:

  • <name>.gs_scatter.spv regroups the real vertex shader's per-vertex outputs and the draw's index buffer (R16_UINT, R32_UINT, or non-indexed) into the capture pass's primitive-major input buffer. The index load touches only the width the draw selects, at that width's own scale, and the push constants' member 8 is added to the fetched value (D3D's BaseVertexLocation).
  • <name>.gs_vs.spv is the vertex shader of the record-driven draw: the strip-to-triangle decode walks the meta buffer's end flags as straight-line selects, the vertices past the last end flag form the invocation's trailing strip (a geometry shader's implicit OpEndPrimitive), a triangle past the emitted count (or a primitive past the count) gets a position outside the clip volume, and each corner's record feeds the outputs at the GS's own output slots, so the pixel shader is unchanged. Its record and meta addresses are heap-relative console VAs, like every other stage's, read from push-constant bytes 144..164 (the Draw tail); a pipeline that takes them declares the larger range, and every other draw still pushes the 144-byte block.

The harness path: vk_harness runs it under VKH_GS_VS (+ VKH_GS_SCATTER, VKH_GS_LAYOUT, VKH_GS_LINEAR, optional VKH_GS_INDICES/VKH_GS_INDEX_TYPE/VKH_GS_COUNT/VKH_GS_FIRST), and LEONIDA_GS_COMPUTE=1 builds and runs the fixtures with it: dispatch the scatter, barrier, dispatch the capture, barrier, draw the records as a triangle list.

The D3D12 layer path: a graphics PSO with a geometry shader now draws through the capture path where the device has no geometryShader, instead of being refused -- staying on the direct Vulkan geometry stage where it has one. The graphics pipeline's vertex stage becomes the generated draw VS (no vertex input); the game's vertex shader runs fused with its fetch as one compute pass (<vs>.gs_input.spv, generated per vertex shader, pairing facts in a parameters block the PSO writes once), the geometry shader as the capture pass (<gs>.gs_compute_bda.spv, physical addresses from push constants, no descriptor sets), and a draw on such a pipeline records the two dispatches, the barriers, and the record-driven triangle-list draw. A u16 draw whose last index's containing dword crosses the bound index buffer is refused (the fused pass reads that dword whole). Scope limits (R32G32B32A32_FLOAT layouts, six vertex-buffer slots, one instance, 252 push-constant bytes) are refused with a log.

vk_gs_fallback_test drives that exact command sequence through vulkan_replay on a real device, against stages CMake builds from the glslang fixtures: gs_double (emitting each triangle twice, so the doubled quad proves the decode's meta-flag walk over more than one emitted primitive) and vs_pair, whose location-1 output the geometry shader does not read -- a leak from the fused pass's scratch region would move or recolour the quad. Non-indexed, u16 and u32 draws must reach the same image.

Devices with the geometry stage keep the direct path: the native module is untouched, and the GPU test takes the capture path only under the env, so RADV/Windows coverage is unchanged.

Tested

Apple M1 Max (32-wide), MoltenVK 1.4.2 and KosmicKrisp (Mesa 26.3.1):

  • gs_tri through the capture path (LEONIDA_GS_COMPUTE=1 LEONIDA_W64_EMULATE=1, scripts/run_gpu_tests.py): 4096/4096 pixels exact on both drivers, four ways -- non-indexed, a u16 index buffer, the same u16 indices with base vertex 3 (the real triangle behind a degenerate copy, so a scatter that dropped the base would draw nothing), and @LukeOsland1's permuted u32 index buffer from gs-compute: fix 32-bit indexed geometry draws (SHADER-1092 follow-up) #7696 (all three index kinds, with the u32 element scale his report found doubled). On MoltenVK and KosmicKrisp, main skips gs_tri: no geometry stage.
  • @tupacfan04's review finding on gs-compute: fix 32-bit indexed geometry draws (SHADER-1092 follow-up) #7696 (whose scatter this PR shares) is fixed: the index read now loads only the width the kind selects, at that width's own scale, so a u16 draw no longer addresses a u32 span past its index buffer and vice versa; a non-indexed draw keeps pointing the address at readable memory. The base-vertex run covers his VKH_GS_FIRST point, and tests/test_gs_compute_stages.py pins the single width-selected load on the CPU.
  • vk_gs_fallback_test: non-indexed, u16 and u32 all draw the doubled quad, 64x64 pixels exact on MoltenVK and KosmicKrisp, with the Khronos validation layer reporting nothing on either. Its teardown used to destroy the render target and the two buffers a second time after ~Device's registry loop -- undefined behaviour that crashed at exit (reproducibly on KosmicKrisp, and on MoltenVK once the test's allocations shifted), which is what looked like a KosmicKrisp startup crash.
  • Full suite (run_gpu_tests.py, LEONIDA_W64_EMULATE=1), on this head (rebased onto 867d922f0), against the same suites on 867d922f0: with LEONIDA_GS_COMPUTE=1, MoltenVK is 210/241 passed, 29 skipped, 2 failed and KosmicKrisp 205/241, 32 skipped, 4 failed. Without the flag the branch is 209/241, 30 skipped, 2 failed on MoltenVK and 204/241, 33 skipped, 4 failed on KosmicKrisp -- per-test identical to 867d922f0 on both drivers, and the only difference with the flag is gs_tri SKIP -> PASS. The failures are the same on both sides: cs_fma_fused (the documented MoltenVK 1.4.2 SHADER-182 case) and ps_ssharp on MoltenVK, and cs_bvh, cs_bvh32, ps_cndmask_sign_mods and ps_valu16_mad_sel on KosmicKrisp (the documented NaN-constant canonicalization).
  • Native ctest on this head: 65 tests, 100% passed (63 passed, 2 skipped by design: shader_library_corpus and vk_sync_validation). command_list_test's positional Dispatch caught a real bug here: tail/tail_words sat before the grid, so {pipeline, user_data, x, y, z} left y and z zero and the list refused the dispatch; they now follow the grid, as Draw's do.
  • CPU: tests/test_spirv_gs_to_compute*.py and tests/test_gs_compute_stages.py pass; compileall over tools/, scripts/ and tests/ is clean; the semantic suite runs 447 files / 5,957 tests and fails exactly test_image_atomics_guard and test_image_store_d16 (48 failures, 6 passed), the same counts and failure set as 867d922f0; the mesh unit tests pass (101 tests + 14 subtests).
  • The native module is byte-identical with and without --gs-compute, and byte-identical to 867d922f0's, on both drivers' suite runs.
  • The Windows runtime, its shims and their tests build with clang-cl against an xwin SDK: 343 targets linked, d3d12_geometry_test included.

AI-assisted: yes

Checklist

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

@AnaNazzDev

Copy link
Copy Markdown

Tested 436a4c813 on NVIDIA GeForce GTX 1080 (Pascal, 32-wide, has the geometry stage), driver 582.66, Windows 11 (10.0.26200): pass. This is a first non-Apple run, and a desktop driver where both paths can be compared on one device.

vk_harness and the other Vulkan test programs built from this PR with MSVC (VS 18, RelWithDebInfo), llvm-mc 21.1.8 (scripts/build_llvm_mc.ps1), SPIRV-Tools from Vulkan SDK 1.4.363.0.

run_gpu_tests.py --filter gs_:

flags gs_tri
none (native VKH_GS path) 4096/4096 exact
LEONIDA_GS_COMPUTE=1 non-indexed 4096/4096 exact; u16 indices 4096/4096 exact
LEONIDA_GS_COMPUTE=1 LEONIDA_W64_EMULATE=1 non-indexed 4096/4096 exact; u16 indices 4096/4096 exact

gs_tri.gs_scatter.spv and gs_tri.gs_vs.spv assembled and validated.

Native path unchanged: the full default suite gives the same per-test results on this branch (its own harness) and on its merge base with main (main's harness): 177/235 passed, 58 skipped, no failure, identical PASS/SKIP lists.

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

@autowert66

Copy link
Copy Markdown
Author

The u32 index-read bug LukeOsland1 reported is found and fixed at the head (03982bf).

load_dword scales its element index by four itself (dw()), and both u32 index reads passed an already-multiplied index: a u32 draw resolved source vertex i at element i*16 instead of i*4. The u16 and non-indexed paths never touch the scaling, which is why every run so far — mine, @AnaNazzDev's GTX 1080 pass, and the u16 half of the original report — read exact. The same over-scale sat in the fused pass generator the layer fallback uses, fixed in the same commit.

run_gpu_tests now gives gs_tri's capture path a u32 index run, and it catches the bug exactly as reported: on MoltenVK, with the over-scale temporarily restored, it fails at (0,0) with u16 and non-indexed still 4096/4096; with the fix all three are pixel-exact.

PASS gs_tri  non-indexed: 4096/4096 pixels exact; u16 indices: 4096/4096 pixels exact; u32 indices: 4096/4096 pixels exact

@LukeOsland1 — thank you for the hardware catch; it needed u32 coverage none of my runs had. Your permuted-index regression would land right on top of this: the over-scaled reads of 0,1,2 go past the 12-byte buffer, so the plain case fails too, and a permuted fixture would also catch any wrong-element scaling that stays in bounds.

@AnaNazzDev — thanks for the independent pass; good to see the native path and the capture path agreeing on Pascal as well.

@vakaobr

vakaobr commented Nov 23, 2026

Copy link
Copy Markdown

Tested 4930f434b (the head as is: it conflicts with current main, so not merged; base 1cc6e3c12) on an Apple M5 Pro (GPU family Apple 10 / Metal 4), macOS 27.2.1, MoltenVK 1.4.2 (universal release, loaded directly), Apple clang, native build. The capture path works here, and I found two bugs, both with a fix below.

What passes

  • run_gpu_tests.py --filter gs_ with LEONIDA_GS_COMPUTE=1, and with LEONIDA_GS_COMPUTE=1 LEONIDA_W64_EMULATE=1: gs_tri non-indexed, u16 indices and permuted u32 indices all 4096/4096 pixels exact. Without the flag it skips (no geometryShader), as on the base.
  • vk_gs_fallback_test draws all three: "non-indexed, u16 and u32 all drew the doubled quad: 64x64 pixels exact".

1. vk_gs_fallback_test crashes after it passes (exit 139)

It prints the pass line, then segfaults in ~Device():

frame #0: libMoltenVK.dylib`MVKDevice::destroyPresentableSwapchainImage(...) + 268
frame #1: libMoltenVK.dylib`vkDestroyImage + 84
frame #2: vk_gs_fallback_test`vktest::Device::~Device() at vk_test_device.h:136
frame #4: vk_gs_fallback_test`main at vk_gs_fallback_test.cpp:475

rt comes from d.image(...) (line 236), and heap and readback from d.buffer(...) (lines 77 and 263). vktest::Device keeps all three in images_ / buffers_ and destroys them (and frees their memory) in its destructor, but the test also destroys them itself at lines 466 to 468. So each is destroyed twice. On the M1 Max the second destroy happened not to fault; on the M5 Pro, MoltenVK reads the freed MVKImage and crashes. Dropping the test's three calls fixes it: 3/3 runs pass with exit 0.

2. command_list fails on every platform: line 159: list.commands().size() == 1

tail and tail_words were inserted into struct Dispatch before x, y, z. command_list_test.cpp builds a Dispatch by position (lines 150, 155, 156: Dispatch dispatch{0x1234, {}, 2, 1, 1}), so with brace elision 2, 1, 1 now fill tail[0..2], leaving x = y = z = 0: a dispatch with no groups records nothing. It fails 4/4 here and passes 4/4 on the base. The runtime (d3d12_com.cpp:1168/1206/1217) and vk_gs_fallback_test value-initialise and assign by name, so they are not affected. Appending the two fields after z, as Draw's already are ("Appended so a Draw built by the plain fields needs no change"), fixes it without touching any caller.

With both changes: native ctest 61 pass, 2 skip, 0 fail. Before: 59 pass, 1 fail (command_list), 1 crash (vk_gs_fallback). The base gives 59 pass, 2 skip, plus one xdxr_build_check failure that didn't repeat in 4 reruns. gs_tri through the capture path is unchanged (4096/4096 ×3). The Windows side cross-builds clean (clang-cl/lld 23.1.6, xwin SDK 10.0.26100, static CRT).

Diff against 4930f43 (2 files, +5/-5)
diff --git a/runtime/src/command_list.h b/runtime/src/command_list.h
index d30d9b429..81aa7ef95 100644
--- a/runtime/src/command_list.h
+++ b/runtime/src/command_list.h
@@ -379,11 +379,13 @@ struct ResolveSubresource {
 struct Dispatch {
     std::uintptr_t pipeline;
     std::array<std::uint32_t, 32> user_data;
-    std::array<std::uint32_t, 28> tail{};
-    std::uint32_t tail_words = 0;
     std::uint32_t x;
     std::uint32_t y;
     std::uint32_t z;
+    // Appended after the grid, as Draw's are, so a Dispatch built by position ({pipeline, user_data, x, y, z})
+    // keeps its grid.
+    std::array<std::uint32_t, 28> tail{};
+    std::uint32_t tail_words = 0;
 };
 
 // What one draw through the geometry fallback records (SHADER-1092): the fused pass's and the capture pass's
diff --git a/runtime/test/vk_gs_fallback_test.cpp b/runtime/test/vk_gs_fallback_test.cpp
index 92acbe423..80a9ab580 100644
--- a/runtime/test/vk_gs_fallback_test.cpp
+++ b/runtime/test/vk_gs_fallback_test.cpp
@@ -463,9 +463,7 @@ int main() {
     for (const VkShaderModule module : {input_module, capture_module, draw_vs_module, ps_module})
         destroy_module(d.device, module, nullptr);
     d.fn<PFN_vkDestroyImageView>("vkDestroyImageView")(d.device, rt_view, nullptr);
-    d.fn<PFN_vkDestroyBuffer>("vkDestroyBuffer")(d.device, heap, nullptr);
-    d.fn<PFN_vkDestroyBuffer>("vkDestroyBuffer")(d.device, readback, nullptr);
-    d.fn<PFN_vkDestroyImage>("vkDestroyImage")(d.device, rt, nullptr);
+    // heap, readback and rt come from d.buffer() / d.image(): ~Device destroys them and frees their memory.
     if (d.validation_errors()) {
         std::fprintf(stderr, "%u validation errors\n", d.validation_errors());
         return 1;

A note on coverage: through the #7446 macOS host, d3d12_geometry still skips on Apple ("SKIP: no geometryShader on this GPU"), as on the base: the test checks geometryShader before it builds the PSO. So on a device without the geometry stage, the D3D12 layer's new path (a GS PSO drawing through the capture passes) isn't run end to end by any test. It's only run through vk_gs_fallback_test, which drives vulkan_replay directly. If that's intended, fine. Otherwise d3d12_geometry could take the fallback there instead of skipping.

No game files used. AI-assisted.

@fuzeofficlal

Copy link
Copy Markdown

Native Windows test of 4930f434 on RTX 5090 (616.56) and Radeon gfx1036 (32.0.21042.62), with Khronos validation 1.4.369. The vk_gs_fallback test matched 4,096/4,096 pixels for each of the non-indexed, u16 and u32 draws on both adapters.

CTest reports pass, but validation flags the test's late resource cleanup on both adapters. The heap and readback buffers and target image are owned by vktest::Device. The explicit destroy calls in vk_gs_fallback_test.cpp release the same handles before the device's destructor, which reports the errors after the test checks the validation count. Removing those three explicit destroy calls in the scratch fixture preserved all pixel matches and produced zero validation messages on both adapters.

AI-assisted (Codex). Synthetic inputs.

…ted GS (SHADER-1092)

A translated geometry shader also gets its compute capture module
(tools/spirv_gs_to_compute.py) as <name>.gs_compute.spv, with the layout
in index.json as "gs_compute", for devices without the Vulkan geometry
stage (Apple GPUs). The native module is unchanged. build.py asks for it
with LEONIDA_GS_COMPUTE=1 and publishes the sidecar.
…ath (SHADER-1092)

Generated per GS from its index.json gs_compute layout, on the pattern of
tools/mesh_compute_vs.py (push-constant tail dwords from 36, addresses as
64-bit push constants, PhysicalStorageBuffer reads, a position outside the
clip volume for degenerate slots):

- <name>.gs_scatter.spv regroups the linear per-vertex outputs of the real
  vertex shader and the draw's index buffer into the capture pass's
  primitive-major input buffer.
- <name>.gs_vs.spv is the vertex shader of the record-driven draw: the
  strip-to-triangle decode walks the meta buffer's end flags as straight-line
  selects, with the trailing strip a geometry shader's implicit
  OpEndPrimitive ends, and each corner's record feeding the outputs at the
  GS's own slots, so the pixel shader is unchanged.
…1092)

LEONIDA_GS_COMPUTE=1 translates the geometry fixtures with --gs-compute and has
vk_harness run the scatter and capture passes as compute, then draw the records
with the generated vertex shader, so gs_tri runs on devices without a geometry
stage. Both the non-indexed draw and a u16 index buffer are checked.

The scatter read the upper u16 of an index word as the lower one (byte % 2 is
always 0 for an even byte offset); it takes byte % 4 now. The draw vertex
shader's record, meta and primitive count are pushed from dword 36, the offset
tools/gs_compute_stages.py reads them from.
…ck (SHADER-1092)

The capture module with its three buffers as 64-bit physical addresses
instead of set-1 descriptors: the console VAs come from six push-constant
members added to the module's UserData block (offsets 144..164) and are
added to the heap base, for the D3D12 layer where no descriptor set can
be bound at dispatch time. rdna2spirv.py --gs-compute publishes both
flavors (<name>.gs_compute.spv, <name>.gs_compute_bda.spv); the ssbo
flavor's output is byte-identical to before.
…HADER-1092)

Two new generated flavors, addressing every buffer through the push-
constant block (heap-relative console VAs, no descriptor sets):

- <vs>.gs_input.spv (per vertex shader, --vs-input): the fused fetch +
  vertex shader + regroup pass. One invocation per input-primitive
  vertex slot resolves the slot's source vertex through the index
  buffer, fetches the vertex's attributes from the bound vertex buffers
  through the per-slot VA/stride table in push constants, runs the
  vertex shader's own code unchanged, and stores the outputs the
  geometry shader consumes straight into the capture pass's input
  buffer. The per-PSO pairing facts (slot counts, the output-slot
  permutation) ride a small parameters block; unconsumed outputs go to
  a scratch slot so the module has no data-dependent branches.
- <name>.gs_vs_bda.spv (per geometry shader, --layer): the record-driven
  draw vertex shader reading its record, meta and primitive count from
  the draw's own s0..s4.
…rows

An unconsumed output's scratch vec4 landed at pv*nin + nin, which is the
next primitive-vertex slot's position: the two stores race within the
dispatch and the capture pass could read the scratch as a position. The
scratch now lands in a region past the dense rows, one vec4 per slot
(slots*nin + pv), which is what the buffer's documented capacity always
held.
…back needs (SHADER-1092)

ShaderEntry parses the gs_compute layout rdna2spirv --gs-compute
publishes (verts_in, max_verts, the input slots with the position one
marked), and the library exposes a shader's generated sidecar modules
next to its own. Dispatch carries a generated stage's own parameters
past the 144-byte block -- tail dwords at push-constant bytes 144..,
a pipeline that takes them declaring the larger range -- and the replay
pushes them.
load_dword scales its element index by four itself (dw()); both u32
index reads passed an index already multiplied by four, so a u32 draw
resolved source vertex i at element i*16 instead of i*4. The u16 and
non-indexed paths never touch the scaling, which is why every run so
far read exact; LukeOsland1's u32 runs on an RTX 4070 Ti SUPER and a
Radeon iGPU caught it (4096/4096 pixels differing), and the same
over-scale sat in the fused pass's generator.

run_gpu_tests gives gs_tri's capture path a u32 index run to cover it:
the plain 0,1,2 buffer is 12 bytes, so the over-scaled reads go past
its end and the run fails on the previous head. On MoltenVK all three
runs are pixel-exact with the fix.
… indices

LukeOsland1's fixture from ItsNotRexed#7696: the linear buffer's first three slots hold a degenerate triangle, so a
scatter that ignored the index buffer would draw it and fail the gradient check -- only following the
u32 indices (1, 3, 0) restores the real one. It also catches the u32 over-scale: on MoltenVK the run is
pixel-exact at 03982bf and fails at (0,0) with the over-scale restored. It replaces the plain 0,1,2
u32 run, which a non-indexing scatter would pass.
…xtures

The rewrite took only translated modules' plain vec4 position input and
output. glslang models the same interface as the gl_PerVertex block --
an input array of the struct and a plain output struct, Position at
member 0 -- which the d3d12 tests' shaders stand in with (gs_double's
interface is a translated one's). The block's member-0 accesses are
rewritten like the plain ones: the gl_in read drops its member index,
and the output store's chain becomes a pointer copy of the private
position. A module whose push-constant block is never read as a uint
(an unused fixture block) gets the PushConstant-uint pointer type the
rewrite's own member loads need. Translated modules produce
byte-identical output (verified on gs_tri with --buffers bda).
… it, and vs_pair

gs_double.geom's block becomes the five-member layout rdna2spirv emits
(four plain uints for the bases) so the capture module's --buffers bda
flavor can extend it; the stage body still does not use it. vs_pair.vert
is the vertex shader that pairs with it through the geometry fallback
(SHADER-1092): gs_double reads position and the colour at location 0,
and the colour at location 1 is the unconsumed output whose store the
fused pass keeps out of the capture rows.
…, and the draw VS the heap base

The fused module declared %gsc_gid without BuiltIn GlobalInvocationId,
so SPIRV-Cross emitted it as an ordinary stage input and Metal rejected
the stage_in struct in a kernel; spirv-val accepted the module, the
driver did not. The draw VS now adds the push-constant block's own
heap base (members 1..2) to its record and meta addresses, as the
capture and fused passes already did theirs: the D3D12 layer records
console VAs, heap-relative, while vk_harness passes absolute addresses
against a zero base, which reads the same. --vs-input no longer demands
the index.json its pass never reads.
… without the geometry stage

vk_gs_fallback_test drives the exact command sequence the D3D12 layer
records for a GS PSO: the fused fetch + vertex + regroup dispatch, the
capture dispatch, the barrier between them and the one before the
record-driven triangle-list draw, all replayed through vulkan_replay
against stages built by CMake from the glslang fixtures -- gs_double
(the capture module with --buffers bda, and the draw VS) and vs_pair
(the fused pass), whose colour at location 1 the geometry shader does
not read, so a leak from the scratch region would move or recolour the
quad. gs_double emits each triangle twice, so the doubled quad proves
the strip decode's meta-flag walk over more than one emitted
primitive, which the harness path never produced. Non-indexed, u16 and
u32 index draws must reach the same image; on Apple M1 Max under
MoltenVK all three are 4096/4096 pixels exact, and the Khronos
validation layer reports nothing.
…ry stage (SHADER-1092)

A graphics PSO with a geometry shader now draws through the capture
path where the device has no geometryShader (Apple GPUs) instead of
being refused, staying on the direct Vulkan geometry stage where it has
one. The graphics pipeline's vertex stage becomes the generated
record-driven draw VS (<gs>.gs_vs.spv, the module vk_harness draws
with), with no vertex input; the game's vertex shader runs in a fused
fetch + vertex + regroup pass (<vs>.gs_input.spv) and the geometry
shader in the capture pass (<gs>.gs_compute_bda.spv), dispatched as
compute before every draw and separated by barriers. Everything the
passes read rides push constants: the game's root arguments in their
usual s registers, the pairing facts in a parameters block the PSO
writes once (which of the vertex shader's outputs the geometry shader
consumes; one it does not read is stored to a scratch region no pass
reads back), and the per-draw facts in the tail words Dispatch and Draw
now carry past the 144-byte block.

gs_compute_stages.py's draw VS reads its record and meta addresses as
heap-relative console VAs like every other stage, so the layer's
recorder fills them in as it does the rest; the --layer flavor that
read them from the draw's own s registers is gone, which also keeps
the game's root arguments reaching the pixel shader untouched.

Scope, refused with a log: geometry shaders translated without
--gs-compute, a vertex shader that does not feed every geometry shader
input, input layouts with a format other than R32G32B32A32_FLOAT, more
than six vertex-buffer slots, per-instance data, draws of more than one
instance, and devices with fewer than 252 bytes of push constants.

The Windows runtime and its tests build with clang-cl against an xwin
SDK (d3d12_geometry_test included); the draw path itself is
vk_gs_fallback_test's, proven on MoltenVK.
…(SHADER-1092)

The record-driven draw's vertex shader reads the record and meta
addresses and the primitive count at push-constant bytes 144.., the
count its pipeline's layout declares; every other draw still pushes
the 144-byte block alone. This is the emit half of Draw's tail_words,
matching Dispatch's from the vocabulary commit.
…ects (SHADER-1092)

The scatter and the fused pass both loaded the u16 and the u32 read
unconditionally and picked between the results, so the width the draw
did not select could address bytes past the index buffer it bound; the
review of ItsNotRexed#7696 found this in the shared scatter (thanks @tupacfan04).
Both stages now select the load's address: one load, at the selected
width's own scale, touching only that width's bytes. A non-indexed
draw keeps pointing the index address at any readable memory, as before.

The layer's prepare refuses a u16 draw whose last index's containing
dword crosses the bound index buffer's end, out of scope rather than
undefined. The harness gains VKH_GS_COUNT so a VKH_GS_FIRST run can
draw a slice of the index file, the u16 fixture gains a second triangle
copy for that run, and a CPU regression walks the scatter's SSA and
pins the single width-selected load.
…1092)

vktest::Device's helpers register every image and buffer they create,
and its destructor destroys each once. The test also destroyed the
render-target image and the heap and readback buffers by hand at the
end of main, so the destructor ran vkDestroyImage and vkDestroyBuffer
on dangling handles: undefined behaviour that crashed at teardown
(reproducibly on KosmicKrisp, and on MoltenVK once the test's
allocations shifted), after every check had already passed. The view,
the pipelines, layouts and modules stay the test's own to destroy.
…ex (SHADER-1092)

The generated stages read the index at the draw's ordinal and add the
push constants' member 8 to the fetched value -- D3D's BaseVertexLocation;
the layer implements StartIndexLocation by offsetting the index buffer's
address. The run passed VKH_GS_FIRST as if it were a first index, so it
fetched vertices past the linear buffer and drew nothing. The run now
keeps a degenerate copy of the triangle first in the linear buffer and
the real one behind it: indices 0,1,2 plus base 3 draw the real
triangle, and a scatter that dropped the base would draw the zero-area
copy and fail the check.
…1092)

The Geometry shaders row described the capture lowering as the end of
the story and listed the draw path as not done. It now records the
--gs-compute flag, the generated scatter and draw vertex shader, the
layer's PSO fallback (the fused fetch, the capture dispatch, the
record-driven draw), its scope, and how the path was confirmed: gs_tri
four ways at 4096/4096 pixels on MoltenVK and KosmicKrisp,
vk_gs_fallback_test's doubled quad 64x64 exact for the three index
kinds on both drivers, and the CPU checks. The row's duplicated
sentence is folded into one. The metadata index row gains gs_compute,
and docs/testing.md gains a "GPUs without a geometry stage" section
beside the mesh one.
The fallback's draw is a plain triangle-list draw, but its positional
Draw initializer in record_draw stopped at first_instance, leaving the
mesh flag to be initialized from the tail array -- which clang-cl
refuses to convert to bool. Pass false for mesh; the tail dwords stay
in tail and tail_words.
@lucasmartin042009-sudo

Copy link
Copy Markdown

Tested 8a75187 on Apple M3 Pro, macOS 27.2.1 (26B131), MoltenVK 1.4.2 (Homebrew) and KosmicKrisp (Mesa 26.3.1, driverInfo checked): native build, no merge.

Confirmed: run_gpu_tests.py --filter gs_ skips gs_tri without the flag, and with LEONIDA_GS_COMPUTE=1 (with or without LEONIDA_W64_EMULATE=1) passes on both drivers: non-indexed, u16, u16 base vertex 3, permuted u32, all 4096/4096 exact. vk_gs_fallback_test draws the doubled quad 64x64 exact and exits 0 on both (MoltenVK 2 runs, KosmicKrisp 3); no crash at ~Device here. Full MoltenVK suite: 179/241 without the flag, 210/241 with LEONIDA_GS_COMPUTE=1 LEONIDA_W64_EMULATE=1; the only failures are cs_fma_fused and ps_ssharp, the same as main, and gs_tri goes SKIP to PASS.

Still open at this head: ctest is 64/65 passed, 2 skipped; command_list fails (line 159: list.commands().size() == 1), reproduced on rerun. This is the Dispatch field order reported on 4930f43: tail/tail_words still sit before x, y, z in runtime/src/command_list.h, so Dispatch{0x1234, {}, 2, 1, 1} leaves the grid at 0.

Not tested: full suite on KosmicKrisp, d3d12_geometry (skips on Apple), the Windows build, validation layers.

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

@fuzeofficlal

Copy link
Copy Markdown

Native Windows geometry fallback retest at 8a75187ec0c9 with Khronos Vulkan validation 1.4.369.

  • RTX 5090 (616.56) and integrated AMD gfx1036 (32.0.21042.62) each passed the stock vk_gs_fallback test. The non-indexed, u16 index, and u32 index draws each matched all 4,096 pixels per adapter. The teardown validation messages I saw at 4930f434 are gone on both.
  • command_list still fails at line 159 on Windows. The positional Dispatch initializers place {2, 1, 1} in tail after the layout change, so x, y, and z remain zero and the dispatch is dropped. In a scratch copy, I changed only the three test initializers to use .pipeline, .x, .y, and .z explicitly. The full command_list test then passed with the production header unchanged.

Synthetic fixtures, AI-assisted.

tail and tail_words went in before x, y and z, so a positional
Dispatch{pipeline, user_data, x, y, z} -- the form command_list_test
uses -- filled them and left y and z zero, which the list refuses as
"no groups records nothing". They are appended after the grid now, as
Draw's fields are, so the plain form keeps setting its grid; the
geometry fallback's passes set tail and tail_words by name. Found by
@vakaobr's ctest report on ItsNotRexed#7336.
@autowert66

Copy link
Copy Markdown
Author

Thanks @vakaobr and @fuzeofficlal for the reviews — both findings were real and are fixed.

  • The teardown double destroy (79bc488): vk_gs_fallback_test destroyed its render target and the heap and readback buffers by hand after vktest::Device's destructor already destroys every image and buffer its helpers registered, so the destructor ran vkDestroyImage/vkDestroyBuffer on dangling handles. That is also what crashed at exit on KosmicKrisp — with the buffered output lost, so it looked like a startup crash.
  • The Dispatch field order (616dd5a): tail and tail_words now come after x, y, z, as Draw's fields do, so the positional {pipeline, user_data, x, y, z} form still sets the grid. Every Dispatch in runtime/ either uses that form or sets x/y/z by name; command_list_test passes again.
  • ctest on the new head: 65 tests, 100% passed (63 passed, 2 skipped by design: shader_library_corpus and vk_sync_validation). Also re-ran the GPU checks on both drivers: gs_tri through the capture path 4096/4096 pixels exact (non-indexed, u16, u16 with base vertex, permuted u32) and vk_gs_fallback_test 64x64 exact, on MoltenVK 1.4.2 and KosmicKrisp (Mesa 26.3.1), Khronos validation clean.

On the coverage point: d3d12_geometry_test skips on a device without geometryShader and cannot take the fallback as it stands — its input layout is R32G32_FLOAT + R8G8B8A8_UNORM, while the fallback's scope is R32G32B32A32_FLOAT layouts, and it would also need the gs_compute block in its index.json and the generated stages for its own vs_draw/gs_double. So on those devices the layer path is covered by vk_gs_fallback_test, which replays the layer's exact command sequence (the fused fetch dispatch, the capture dispatch, the barriers and the record-driven draw) through vulkan_replay. Making d3d12_geometry take the fallback (with an R32G32B32A32_FLOAT layout and the sidecars) is a reasonable follow-up; it is more than a small change, so I did not fold it in here.

@fuzeofficlal

Copy link
Copy Markdown

Retested the author fix at 616dd5a64f55 on native Windows. Thanks for fixing the command-list initialization.

  • The stock command_list test now passes. It previously failed at line 159 on 8a75187ec0c9.
  • Stock vk_gs_fallback passes on RTX 5090 (616.56) and integrated Radeon gfx1036 (32.0.21042.62). Non-indexed, u16, and u32 draws each matched all 4,096 pixels per adapter.
  • Both targeted CTest runs pass 2/2 with Khronos Vulkan core and synchronization validation 1.4.369 enabled. No validation or teardown errors.

Synthetic tests on the author's unchanged head. AI-assisted.

@rideinstead

Copy link
Copy Markdown

✅ Automated security review: CLEAN (commit 4930f434)

  • No findings.

Scope: static analysis of added diff lines only; no code was executed. Please review findings manually.

@AimLogin

Copy link
Copy Markdown
Contributor

@autowert66, a heads-up on how this PR and #5128 (SHADER-67, mine, open) fit together. After #5128, leonida::ShaderUserData is 160 bytes, not 144: a pixel shader that reads its own code base gets dwords 38–39. The plain block is leonida::shader_user_data_bytes (144).

This PR builds on sizeof(leonida::ShaderUserData) in three places:

  • push_bytes = sizeof(leonida::ShaderUserData) + (gs_fallback ? 20 : 0)
  • the range sizeof(leonida::ShaderUserData) + 4 * tail_words
  • the limit check max_push_constants_size < sizeof(leonida::ShaderUserData) + 108

After #5128 these move the GS-compute data from offset 144 to 160 and raise the device requirement by 16 bytes. Unless the GS-compute shaders are regenerated for offset 160, they would read the wrong words. The compute push sizeof(data) would also be 160 into a 144 range.

Fix, for whichever lands second:

  • Base the offsets and sizes on shader_user_data_bytes.
  • If a code-base PS shares the pipeline, place the GS data after its 160-byte block.

This only matters if #5128 lands first; nothing is wrong on today's main. #5128 adds static_assert(leonida::shader_user_data_bytes == offsetof(ShaderUserData, mesh_output)) next to the struct, and a d3d12_compute_test case that creates a compute PSO on a simulated 152-byte device, so a size taken from sizeof shows up there.

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

@autowert66

Copy link
Copy Markdown
Author

Thanks, agreed. Whichever of the two lands second adapts. If #5128 goes first, #7336 rebases onto leonida::shader_user_data_bytes for the push sizes, the tail range and the limit checks (including the compute push range and the 144 in vk_pipeline_check.cpp), and moves the GS-compute tail after a code-base pixel shader's 160-byte block. If #7336 goes first, the same sites are where #5128's static_assert and the 152-byte device case should flag them.

AI-assisted (Claude Code).

@Michele0303

Copy link
Copy Markdown

checked: #7336 at 616dd5a | base: main@5d74c27 | on: macOS 27.0.1 arm64 (Apple M5), MoltenVK 1.4.2, Python 3.13 | result: gs_tri with LEONIDA_GS_COMPUTE=1 (with and without LEONIDA_W64_EMULATE=1) passes all four draws at 4096/4096 exact, while main skips it (no geometryShader). vk_gs_fallback_test is 64x64 exact. Without the flag the full run_gpu_tests.py is per-test identical to main (180/241, 60 skipped, cs_fma_fused fails on both), ms_* is unchanged with and without LEONIDA_MESH_COMPUTE, and the gs/runner pytest files show the same results as main. No Khronos validation layer on this machine, and the D3D12 shim path was not built.

  • spirv_gs_to_compute: the capture buffers use their own set, not set 1 (SHADER-825) #4670 conflicts with this PR in tools/spirv_gs_to_compute.py (decos block). With the conflict resolved to CAPTURE_SET = 2, gs_tri under LEONIDA_GS_COMPUTE=1 fails: MoltenVK rejects the capture pipeline (vkCreateComputePipelines -> -3, vk_harness.cpp:935) because the harness layout is {empty, set 1}. Whichever lands second needs vk_harness to take the set from the layout's capture_set.

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

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.

9 participants