Skip to content

Runtime: geometry shaders in graphics PSOs (VS + GS + PS) - #2109

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
TheHoracio:geometry-stage
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
TheHoracio:geometry-stage

Conversation

@TheHoracio

Copy link
Copy Markdown
Contributor

Claimed in #1902 (new D3D item).

What and why

CreateGraphicsPipelineState refused any PSO with a GS ("graphics pipeline stage not supported yet (hull/domain/geometry/stream output)"), and nothing enabled geometryShader. The translator already handles all 195 geometry shaders in the corpus ($GS_VoxelizeGeneric, triangles in, a triangle strip out; docs/design.md, "Geometry shaders", GPU-checked with gs_tri.asm). Without this PR, every PSO that uses one fails.

  • Stage table (d3d12_vulkan_device.cpp): VS, GS, PS. The GS goes through the same path as the other stages: lookup by FSSM hash in the index (stage: "gs"), set-0 bindings, code base, and subgroup size.
  • Rules:
    • A GS needs geometryShader; without it the PSO is E_NOTIMPL with a log.
    • The PSO's PrimitiveTopologyType must be TRIANGLE, the translated GS's input primitive, as D3D12 requires the topology to match the GS input; otherwise E_INVALIDARG.
    • Hull, domain and stream output stay refused.
  • Push constants: the range names VK_SHADER_STAGE_GEOMETRY_BIT only on a device that has the stage. The loop over the stage table would otherwise name a stage the device can't run.
  • Devices: the standalone device enables geometryShader where the GPU has it and reports it in VulkanDeviceInfo::features. vk_test_device.h enables it too.
  • Docs: a "Geometry shaders" bullet in docs/d3d12_vulkan.md, and geometry removed from the "refused" list.

Tested

AMD Radeon RX 7800 XT (RDNA3), driver 26.10.1 (32.0.32015.7029), Windows 11 Pro (26300), MSVC 14.44, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, LEONIDA_VK_VALIDATION=1:

  • d3d12_geometry_test (new, in ctest with SKIP_RETURN_CODE 77): test/glsl/gs_double.geom has a translated GS's interface and emits every triangle twice, the copy one clip-space unit to the right.
    • The GS pipeline draws a top-left quad red, and its copy fills the top-right quadrant.
    • A pipeline without a GS draws the same quad bottom-left in green, and the bottom-right quadrant stays blue.
    • All 4,096 pixels match, with 0 validation errors. A GS PSO with a LINE topology type is E_INVALIDARG.
  • Negative control: with the GS left out of the stage table, the copy is missing; the test fails at pixel (32, 0), blue instead of red.
  • Regression: ctest -j4 110/111. The one failure is d3d12_draw (bundle blend), which also fails on clean main and which fix(test): use monotonic fence value (31) for bundle blend pass in d3d12_draw_test #1807 addresses.

Not covered: the test GS is GLSL with the translator's interface, not a translated $GS_VoxelizeGeneric. A real one through this path needs its VS's outputs to line up with the GS inputs, which #1694's interface check covers on the translator side. Stream output (SOSetTargets) is still open.

Checklist

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

AI-assisted (Claude Code).

🤖 Generated with Claude Code

@JavierBlanco23

Copy link
Copy Markdown
Contributor

Tested a580e16e on NVIDIA GeForce RTX 5080, driver 616.56, Windows 11 (MSVC 19.44, Ninja): pass.

  • cmake --build runtime/build --target d3d12_geometry_test
  • d3d12_geometry_test.exe: geometry: a VS+GS+PS pipeline drew its quad and the GS's copy (top half red), a pipeline without a GS only the quad (bottom-left green), all 4096 pixels matched; a GS with a line topology refused.

@umnitus

umnitus commented Nov 22, 2026

Copy link
Copy Markdown

Tested a580e16 on an RTX 3060 12 GB, NVIDIA 621.14, Vulkan 1.4.357, Windows 11 26H2: pass.

I built and ran the unmodified d3d12_geometry_test.exe twice. All 4,096 pixels matched in both runs. The geometry shader drew the expected copy of the quad, and the pipeline with the incompatible line topology was rejected as expected. The repeat run exited with code 0.

Build: clang-cl 23.1.6, Windows SDK 10.0.26100 via xwin, CMake 4.5.1, Ninja 1.13.2, and glslang 16.6.0. Vulkan validation layers were disabled. Synthetic inputs only. AI-assisted setup and verification; tests ran locally.

@0xTehuti 0xTehuti left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

GPU Verification & Code Review Findings (NVIDIA Turing / RTX 2070 SUPER)

Tested on Windows 11 with MSVC 2022, Ninja, CMake, and Vulkan SDK 1.4:

  • GPU: NVIDIA GeForce RTX 2070 SUPER (Driver 595.79, 8192 MB VRAM, Vulkan 1.4.363)
  • Validation Layers: Khronos Validation Layer active (LEONIDA_VK_VALIDATION=1)

1. Hardware Test Results

  • d3d12_geometry_test: PASSED (0.33s, 4,096 / 4,096 pixels matched exactly, 0 validation errors).
  • d3d12_vulkan_device_test: PASSED (9.32s, 0 validation errors).

2. Critical Defect Detected: Heap Corruption in Recipe::required_sizes

While the synthetic test in d3d12_geometry_test passed (because its test shaders do not specify subgroup sizes), there is an out-of-bounds write in runtime/shims/d3d12_vulkan_device.cpp that will corrupt memory when processing real translated geometry and pixel shaders:

In line 2532:

struct Recipe {
    std::vector<VkPipelineShaderStageCreateInfo> stages;
    VkPipelineShaderStageRequiredSubgroupSizeCreateInfo required_sizes[2]{};  // <-- declared size 2
    std::vector<VkVertexInputAttributeDescription> attributes;
    ...
};

Lines 2134–2184 iterate over Stage stages[3] (VS, GS, PS). When line 2182 is reached for the fragment stage (stage_infos.size() == 2):

subgroup_size_for(*stage.entry, required_sizes[stage_infos.size()], info);

It accesses required_sizes[2], which is beyond the 2-element array bounds. This writes 24 bytes directly into the adjacent recipe_.attributes (std::vector control block). Furthermore, info.pNext points to this out-of-bounds address, resulting in dangling / overwritten pointers during variant compilation.

Required Fix

Update required_sizes array bound from 2 to 3 in runtime/shims/d3d12_vulkan_device.cpp:

--- a/runtime/shims/d3d12_vulkan_device.cpp
+++ b/runtime/shims/d3d12_vulkan_device.cpp
@@ -2532,1 +2532,1 @@
-        VkPipelineShaderStageRequiredSubgroupSizeCreateInfo required_sizes[2]{};  // what `stages` chain
+        VkPipelineShaderStageRequiredSubgroupSizeCreateInfo required_sizes[3]{};  // what `stages` chain

Once this patch is applied, this PR is clean and ready for merge on NVIDIA Turing.

AI-assisted verification (Orca / Antigravity). No game data used.

TheHoracio added a commit to TheHoracio/Leonida that referenced this pull request Nov 22, 2026
…, PS)

The recipe kept two VkPipelineShaderStageRequiredSubgroupSizeCreateInfo, one
per stage of the old VS+PS table; with a GS the PS's subgroup request wrote a
third one past the array, into the recipe's attribute vector (found by
@0xTehuti on ItsNotRexed#2109). The array now has three entries and a static_assert ties
it to the stage table.

d3d12_geometry_test's PS asks for a 32-wide subgroup, so a VS+GS+PS pipeline
fills the third struct: without the fix the test crashes (3 of 3 runs).

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

Copy link
Copy Markdown
Contributor Author

@0xTehuti, thanks, that was a real overflow: required_sizes[stage_infos.size()] reached index 2 for the PS once the GS was in the table. Fixed in b82b7693:

  • Recipe::required_sizes now has three entries.
  • A static_assert in create_graphics ties its size to the stage table, so a future stage (mesh) can't outgrow it unnoticed.
  • Regression test: d3d12_geometry_test's PS index entry now asks for a 32-wide subgroup ("subgroup_size": 32), and the device gets the test device's subgroup caps. That makes a VS+GS+PS pipeline fill the third struct. On the RX 7800 XT the fragment stage can require a size.
    • Without the fix: segfault in 3 of 3 runs.
    • With it: 3 of 3 pass, all 4,096 pixels match, 0 validation errors.
  • Regression: ctest -j4 is unchanged, with only the known d3d12_draw failure (fix(test): use monotonic fence value (31) for bundle blend pass in d3d12_draw_test #1807).

@JavierBlanco23 and @umnitus, thanks for the RTX 5080 and RTX 3060 runs. They were on a580e16e, before this fix, which only crashes once a stage asks for a subgroup size. AI-assisted.

@whattheflop

Copy link
Copy Markdown
Contributor

Independent verification of b82b7693b4b82afe5312efea4820a14a033ef6fd on Windows 11 (26200), MSVC 19.38 x64, Windows SDK 26100 and Vulkan SDK 1.4.363.0. The stock target built. I ran a scratch copy linked to the exact-head runtime, with the line-topology negative case omitted and 5 s fence / 25 s process deadlines. Each process used one ICD and Khronos core + synchronization validation.

  • RTX 3080 (616.64, Vulkan 1.4.351) and Radeon iGPU (32.0.21030.2001, Vulkan 1.4.315) each matched 4,096/4,096 RGBA8 pixels with the GLSL GS and no-GS control, with 0 validation errors. The GS produced 1,024 red source pixels and 1,024 red copy pixels. The control left 1,024 green and 1,024 clear blue pixels.
  • The Radeon iGPU also matched 4,096/4,096 with 0 validation errors using the hand-written, translator-generated gs_tri.asm fixture and location-1 GLSL stages. The GS cleared the blue component of the magenta vertex input as expected, giving 1,024 red, 1,024 green and 2,048 clear blue pixels.

All modules used passed spirv-val. The RTX uses fixed wave32 and the Radeon exposes default wave64 with required subgroup sizes limited to compute, so these graphics runs used device subgroup sizes. Synthetic shaders only. AI-assisted.

TheHoracio and others added 2 commits November 22, 2026 18:05
CreateGraphicsPipelineState refused any desc.GS, although all 195 geometry
shaders translate (triangles in, a triangle strip out; docs/design.md
"Geometry shaders"). The stage table now holds VS, GS and PS; a GS needs the
geometryShader feature (enabled on the standalone and test devices where
the GPU has it, E_NOTIMPL otherwise), and the PSO's topology type must be
TRIANGLE, the GS's input (E_INVALIDARG otherwise). The push-constant range
names the geometry stage only where the device has it. Hull, domain and
stream output stay refused.

d3d12_geometry_test (in ctest): a GS that emits each triangle twice, against
a pipeline without one, and a line topology refused.

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

The recipe kept two VkPipelineShaderStageRequiredSubgroupSizeCreateInfo, one
per stage of the old VS+PS table; with a GS the PS's subgroup request wrote a
third one past the array, into the recipe's attribute vector (found by
@0xTehuti on ItsNotRexed#2109). The array now has three entries and a static_assert ties
it to the stage table.

d3d12_geometry_test's PS asks for a 32-wide subgroup, so a VS+GS+PS pipeline
fills the third struct: without the fix the test crashes (3 of 3 runs).

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

Copy link
Copy Markdown
Contributor Author

@whattheflop, thanks for the RTX 3080 and iGPU runs, including the translator-generated gs_tri.asm path. Rebased onto main 49475796 as 9b50f175; the one conflict was the test-shader list in runtime/CMakeLists.txt (gs_double.geom next to vs_draw_int.vert, and d3d12_geometry next to d3d12_msaa_draw). Full ctest: 117/117 pass, 0 validation errors. Windows 11 Pro 26300, RX 7800 XT + Radeon iGPU (AMD Adrenalin), MSVC 19.4x, Vulkan SDK 1.4.363.0, LEONIDA_VK_VALIDATION=1. AI-assisted.

@ItsNotRexed
ItsNotRexed merged commit c68a35b into ItsNotRexed:main Nov 22, 2026
3 of 4 checks passed
ItsNotRexed added a commit that referenced this pull request Nov 22, 2026
…since #2109

#2109 made the PSO stages {vs, gs, ps}; the --f16-interp-fallback link (SHADER-378, #2389) still read the PS from
stages[1], found the empty GS slot and never ran, so d3d12_f16_halves drew nothing. With a GS the PS reads the GS's
outputs, so the VS/PS link is skipped there (logged once). Also: xgameruntime_test re-reads the package id after
clearing the LEONIDA_PACKAGE_ID override.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
NiiRoZz added a commit to NiiRoZz/Leonida that referenced this pull request Nov 22, 2026
ItsNotRexed#2402's user_data_layout call took {stages[0], stages[1]}, written when a
graphics PSO's stages were {vs, ps}. Since ItsNotRexed#2109 they are {vs, gs, ps}, so on
main stages[1] is the (usually empty) GS slot: the PS's pc_tail never reached
the pipeline layout, set 1 was left out, and a --pc-tail-ubo PS failed
VUID-VkGraphicsPipelineCreateInfo-layout-07988 and drew nothing
(d3d12_draw_test line 358, d3d12_bundle's twin run). All three stages are
passed now; user_data_layout skips the absent ones. This is the same slip
81ec7f7 fixed for the f16 halves link.

Found rebasing ItsNotRexed#3106 onto main f847748. With it, d3d12_draw_test,
d3d12_bundle, d3d12_typeless_families, d3d12_ia_bounds and d3d12_compute pass
with LEONIDA_VK_VALIDATION=1 and no validation error on an RTX 5090 (NVIDIA
621.35) and an AMD Radeon iGPU (AMD 26.5.2).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
NiiRoZz added a commit to NiiRoZz/Leonida that referenced this pull request Nov 22, 2026
ItsNotRexed#2402's user_data_layout call took {stages[0], stages[1]}, written when a
graphics PSO's stages were {vs, ps}. Since ItsNotRexed#2109 they are {vs, gs, ps}, so on
main stages[1] is the (usually empty) GS slot: the PS's pc_tail never reached
the pipeline layout, set 1 was left out, and a --pc-tail-ubo PS failed
VUID-VkGraphicsPipelineCreateInfo-layout-07988 and drew nothing
(d3d12_draw_test line 358, d3d12_bundle's twin run). All three stages are
passed now; user_data_layout skips the absent ones. This is the same slip
81ec7f7 fixed for the f16 halves link.

Found rebasing ItsNotRexed#3106 onto main f847748. With it, d3d12_draw_test,
d3d12_bundle, d3d12_typeless_families, d3d12_ia_bounds and d3d12_compute pass
with LEONIDA_VK_VALIDATION=1 and no validation error on an RTX 5090 (NVIDIA
621.35) and an AMD Radeon iGPU (AMD 26.5.2).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

6 participants