Repository navigation
gs-compute: draw geometry shaders on devices without the geometry stage (SHADER-1092) - #7336
autowert66 wants to merge 21 commits into
Conversation
|
Tested
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 No game files. AI-assisted (Claude Code). |
|
The u32 index-read bug LukeOsland1 reported is found and fixed at the head (03982bf).
@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. |
|
Tested What passes
1. It prints the pass line, then segfaults in
2.
With both changes: native ctest 61 pass, 2 skip, 0 fail. Before: 59 pass, 1 fail ( 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, No game files used. AI-assisted. |
|
Native Windows test of 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 AI-assisted (Codex). Synthetic inputs. |
4930f43 to
4dc2bd3
Compare
…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.
4dc2bd3 to
8a75187
Compare
|
Tested 8a75187 on Apple M3 Pro, macOS 27.2.1 (26B131), MoltenVK 1.4.2 (Homebrew) and KosmicKrisp (Mesa 26.3.1, Confirmed: Still open at this head: Not tested: full suite on KosmicKrisp, No game files. AI-assisted: yes (Claude Code). |
|
Native Windows geometry fallback retest at
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.
|
Thanks @vakaobr and @fuzeofficlal for the reviews — both findings were real and are fixed.
On the coverage point: |
|
Retested the author fix at
Synthetic tests on the author's unchanged head. AI-assisted. |
|
✅ Automated security review: CLEAN (commit
Scope: static analysis of added diff lines only; no code was executed. Please review findings manually. |
|
@autowert66, a heads-up on how this PR and #5128 (SHADER-67, mine, open) fit together. After #5128, This PR builds on
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 Fix, for whichever lands second:
This only matters if #5128 lands first; nothing is wrong on today's AI-assisted (Claude Code). No game files. |
|
Thanks, agreed. Whichever of the two lands second adapts. If #5128 goes first, #7336 rebases onto AI-assisted (Claude Code). |
|
checked: #7336 at
AI-assisted (Claude Code). No game data. |
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-computepublishes the capture module thattools/spirv_gs_to_compute.pylowers (records + meta buffers, one invocation per input primitive) next to the unchanged native module, with the layout in index.json asgs_compute.tools/gs_compute_stages.pygenerates the other two modules per GS, on the pattern oftools/mesh_compute_vs.py:<name>.gs_scatter.spvregroups 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.spvis 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 implicitOpEndPrimitive), 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_harnessruns it underVKH_GS_VS(+VKH_GS_SCATTER,VKH_GS_LAYOUT,VKH_GS_LINEAR, optionalVKH_GS_INDICES/VKH_GS_INDEX_TYPE/VKH_GS_COUNT/VKH_GS_FIRST), andLEONIDA_GS_COMPUTE=1builds 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_testdrives that exact command sequence throughvulkan_replayon 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_trithrough 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.VKH_GS_FIRSTpoint, andtests/test_gs_compute_stages.pypins 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.run_gpu_tests.py,LEONIDA_W64_EMULATE=1), on this head (rebased onto867d922f0), against the same suites on867d922f0: withLEONIDA_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 to867d922f0on both drivers, and the only difference with the flag isgs_triSKIP -> PASS. The failures are the same on both sides:cs_fma_fused(the documented MoltenVK 1.4.2 SHADER-182 case) andps_ssharpon MoltenVK, andcs_bvh,cs_bvh32,ps_cndmask_sign_modsandps_valu16_mad_selon KosmicKrisp (the documented NaN-constant canonicalization).cteston this head: 65 tests, 100% passed (63 passed, 2 skipped by design:shader_library_corpusandvk_sync_validation).command_list_test's positionalDispatchcaught a real bug here:tail/tail_wordssat before the grid, so{pipeline, user_data, x, y, z}leftyandzzero and the list refused the dispatch; they now follow the grid, asDraw's do.tests/test_spirv_gs_to_compute*.pyandtests/test_gs_compute_stages.pypass;compileallovertools/,scripts/andtests/is clean; the semantic suite runs 447 files / 5,957 tests and fails exactlytest_image_atomics_guardandtest_image_store_d16(48 failures, 6 passed), the same counts and failure set as867d922f0; the mesh unit tests pass (101 tests + 14 subtests).--gs-compute, and byte-identical to867d922f0's, on both drivers' suite runs.d3d12_geometry_testincluded.AI-assisted: yes
Checklist
docs/design.md, with how it was confirmed.