Skip to content

Runtime: draw --bary-fallback pixel shaders through a pass-through geometry shader - #4309

Open
DaniSBR1 wants to merge 1 commit into
ItsNotRexed:mainfrom
DaniSBR1:pr-bary-runtime
Open

DaniSBR1 wants to merge 1 commit into
ItsNotRexed:mainfrom
DaniSBR1:pr-bary-runtime

Conversation

@DaniSBR1

@DaniSBR1 DaniSBR1 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in #4084 (#4084 (comment)), new D3D item: the runtime half of the barycentric fallback.

What

A PS translated with --bary-fallback (#804) reads its raw barycentrics from an ordinary vec3 input, at the location its index entry gives as bary_fallback_input. On a device without VK_KHR_fragment_shader_barycentric (RDNA1 on AMD's Windows driver and on RADV, GCN on RADV, NVIDIA Pascal) that is the only way those shaders run: 321 of the game's pixel shaders read the pair. Nothing in runtime/ wrote that input, so the GPU tests drew these shaders (with tools/bary_fallback_gs.py) and a PSO could not.

create_graphics now adds the pass-through geometry shader when the PSO's PS has bary_fallback_input:

  • runtime/src/bary_fallback_gs.h builds it as SPIR-V words: the module tools/bary_fallback_gs.py writes (Position and the PS's vec4 inputs copied per vertex, (1,0,0) / (0,1,0) / (0,0,1) at the barycentric location). For the same input the two modules have the same instructions in the same order; only the id numbering differs.
  • It is built at PSO creation, not at build time, because it depends on both shaders. The pass-through locations come from the PS module after link_f16_halves, which can move a PS's decoded-half inputs, and not from the index. The VS's ClipDistance / CullDistance arrays are forwarded too, from the index's clip_distance_count / cull_distance_count, both at once if the VS writes both.
  • It goes in whether or not the device has the extension, since the translation already chose the input. It asks for no subgroup size because it uses no subgroup operation.
  • Refused with E_NOTIMPL and a log: a PSO that has its own GS, a non-triangle topology, a device without geometryShader, a VS that writes Layer / ViewportIndex (a GS can't read those to forward them), and a PS with PerVertexKHR inputs.
  • ShaderEntry reads bary_fallback_input, the two distance counts, and whether the VS writes Layer / ViewportIndex (from its capabilities).

#3898 and #3332 change the GS template in tools/bary_fallback_gs.py. If either lands first, I'll mirror the change in bary_fallback_gs.h.

Tests

On an RX 5700 XT (RDNA1, no barycentric extension), AMD Adrenalin 26.11.2, Vulkan 1.4.315, Windows 11. The full run was at aa2988c. After the rebase onto 03b09a0, this change's tests and the D3D12 draw, geometry and f16-halves tests passed again:

  • bary_fallback_gs_test (CPU): the module's interface and one-hot constants, clip/cull forwarding, locations taken from the PS module, and each refusal. CMake runs spirv-val on four generated modules (plain, no inputs, clip, clip+cull) where the Vulkan SDK has it: all valid.
  • d3d12_bary_fallback_test (GPU): a GLSL stand-in with a translated PS's interface, drawn through a PSO. The 2016 pixels inside the triangle hold the barycentrics of their centre within 2/255, plus the VS output at location 0. The 2016 pixels outside stay clear. The line-topology, own-GS and Layer-VS PSOs are refused. It passes with LEONIDA_VK_VALIDATION=1 too, with no messages.
  • shader_library_test: the new index fields.
  • Full ctest: 159/159 passed. 10 more skipped here because they need game files or something this machine lacks.
  • tests/test_bary_fallback_gs.py (27) and tests/test_ctest_registration.py (4) pass.

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

@Kam471 Kam471 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.

Built and ran this at 4291ad57 on Windows 11, RTX 5070 Ti:

  • bary_fallback_gs_test.exe -> exit 0: "interface, one-hot constants, clip/cull forwarding, PS-module locations and the refusals all as expected".
  • d3d12_bary_fallback_test.exe -> exit 0: "the layer drew a --bary-fallback PS through its pass-through geometry shader, 2016 pixels inside the triangle hold their barycentrics and location 0, 2016 outside stayed clear; a line topology, a PSO's own GS and a VS writing Layer refused".
  • ctest -R 'bary_fallback' -> 6/6 passed (0.26 s total).

The fallback path works end to end on this device and the guarded refusals are enforced. No blocking issue.

@SweatierKey

Copy link
Copy Markdown
Contributor

Tested 4291ad57 on Linux (WSL2, g++ -std=c++20 -O2 -Wall -Wextra, Python 3.13.5, spirv-val, CPU only, no Windows/GPU host, no game files): pass (Windows GPU draw test not run here).

  • C++20 unit test compiled & executed natively on Linux (clean worktree on 03b09a08):
    • g++ -std=c++20 -O2 -Wall -Wextra -Iruntime/src runtime/test/bary_fallback_gs_test.cpp compiled with 0 warnings and passed (bary_fallback_gs: interface, one-hot constants, clip/cull forwarding, PS-module locations and the refusals all as expected, with spirv-val on PATH).
  • Code read (runtime/src/bary_fallback_gs.h, runtime/src/shader_library.h):
    • Clean SPIR-V binary emitter for the pass-through geometry stage (Position, PS vec4 locations, ClipDistance/CullDistance arrays, one-hot (1,0,0)/(0,1,0)/(0,0,1) barycentric attribute); shader_library.h parses bary_fallback_input.location, clip_distance_count, cull_distance_count, and ShaderLayer/ShaderViewportIndex safely.
  • Hygiene: git diff --check, test_ctest_registration.py (4/4), test_docs_links.py (6/6), test_docs_repo_paths.py (2/2), test_text_write_line_endings.py (3/3), test_text_encoding.py (2/2) all pass.
  • Not run here: Windows MSVC build and Vulkan GPU graphics draw tests.

AI-assisted review.

@whattheflop whattheflop 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.

AI-assisted Windows CPU verification at 4291ad57. Built the focused tests under MSVC 19.38 and Vulkan SDK 1.4.363. Six CTests passed, including four offline module checks. Python helper and CTest registration tests passed 31/31 with uv CPython 3.12.13.

The C++ and Python emitters matched instruction for instruction after normalizing IDs on four sparse, empty, clip or cull cases. Two mixed clip/cull variants matched references built from both Python templates and validated for Vulkan 1.0. With the actual f16 linker moving fragment input IDs from 4/5 to 27/28, the generated GS consumed locations 0, 6, 27 and 28 and emitted those plus barycentrics at 29, omitting the old vertex-only location 4. Synthetic offline checks, no GPU execution.

@MKmajster

Copy link
Copy Markdown

Tested 4291ad5 on NVIDIA GeForce RTX 3070 (Ampere), driver 617.42, Windows 11 24H2, MSVC 19.44 RelWithDebInfo: pass. This device has VK_KHR_fragment_shader_barycentric, so this checks that the forced fallback path still draws correctly there, not the Pascal/RDNA1 case.

  • d3d12_bary_fallback, bary_fallback_gs, bary_fallback_gs_val_{plain,bare,clip,clip_cull}, shader_library: pass (shader_library_corpus skips without game files).
  • d3d12_bary_fallback_test with LEONIDA_VK_VALIDATION=1 (VK_LAYER_KHRONOS_validation 1.4.350): exit 0, no validation messages. the layer drew a --bary-fallback PS through its pass-through geometry shader, 2016 pixels inside the triangle hold their barycentrics and location 0, 2016 outside stayed clear.
  • Full CTest: 160/161. The one failure was a single d3d12_fence_timing SegFault during the -j 4 run. It passed 10/10 alone and 5/5 repeated under -j 8 with the queue/fence tests, and this branch doesn't touch it, so it's intermittent and separate.

No game files. AI-assisted (Claude).

@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 4291ad5 on Windows 11 (MSVC BuildTools 2022, Ninja, RelWithDebInfo, -j 4; no Vulkan validation layer installed), one GPU at a time, device line confirmed: AMD Radeon RDNA2 iGPU (driver 26.10.1; the device has VK_KHR_fragment_shader_barycentric, so this exercises the pass-through GS on a device that doesn't need it) and NVIDIA RTX 5080 (driver 616.92). ctest -R "bary_fallback|shader_library|d3d12_.*(draw|geometry|f16)": 13 tests per GPU, 12 pass, 1 skipped (shader_library_corpus, no translated corpus), 0 failed, including bary_fallback_gs plus its four spirv-val tests, d3d12_bary_fallback, d3d12_geometry, d3d12_translated_draw, d3d12_draw, d3d12_msaa_draw and d3d12_f16_halves. pytest tests/test_bary_fallback_gs.py tests/test_ctest_registration.py: 30 passed, 1 skipped. No game files. Not tested: a GPU without the barycentric extension.

AI-assisted.

@Flyander Flyander 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.

The generated fallback geometry stage also needs to account for built-in fragment inputs. In particular, PrimitiveId becomes a pipeline-linking requirement when this new GS is present.

std::vector<std::uint32_t> locations;
for (const auto& [location, var] : inputs)
if (location != bary) locations.push_back(location);
return bary_fallback_gs(locations, bary, vs.clip_distance_count, vs.cull_distance_count, error);

@Flyander Flyander Nov 23, 2026 •

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.

[P2] Forward or reject fragment PrimitiveId before adding the fallback GS. This generator copies only located inputs plus clip/cull distances and emits no BuiltIn PrimitiveId output. If ps_words reads PrimitiveId, the pipeline has Fragment and Geometry stages but the GS does not write the matching output, violating VUID-VkGraphicsPipelineCreateInfo-pStages-00740. Validation reports the invalid pipeline; without validation, the fragment ID is undefined. Detect that built-in and either pass it through or return a clear unsupported error. Vulkan VUID.

…ometry shader

A PS translated with --bary-fallback (ItsNotRexed#804) reads its raw barycentrics from an input location
(bary_fallback_input in the index), and nothing in the layer wrote it. create_graphics now builds the
pass-through GS that tools/bary_fallback_gs.py writes for the GPU tests (runtime/src/bary_fallback_gs.h):
Position, the linked PS's input locations and the VS's clip/cull distance arrays copied per vertex, and the
one-hot barycentrics. A PSO with its own GS, a non-triangle topology, no geometryShader, a VS writing
Layer/ViewportIndex or a PS with PerVertexKHR inputs is E_NOTIMPL with a log.

Tests: bary_fallback_gs_test (module interface, clip/cull forwarding, refusals; spirv-val on the generated
modules), d3d12_bary_fallback_test (a stand-in PS drawn through a PSO: every covered pixel holds its
barycentrics), shader_library_test (the new index fields). The C++ module matches the Python template
instruction for instruction. RX 5700 XT, AMD 26.11.2, Windows 11: ctest 159/159; the new D3D12 test also
passes under the Khronos validation layer.

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

Copy link
Copy Markdown
Contributor Author

Rebased onto main 98fa503. The conflicts were in docs/design.md, runtime/CMakeLists.txt, shader_library.h and d3d12_vulkan_device.cpp.

main now patches the PS for D24 depth (D3D-309) inside the loop that creates the shader modules. That patch now runs on the PS's words just before the bary-fallback GS is built. The built-in depth-only fragment shader goes through the same add_stage helper as the other stages. The patch only touches the depth output, so the GS still passes through the PS's input locations.

Re-tested on the RX 5700 XT (AMD 26.11.2, Windows 11): full ctest 179/179, including d3d12_depth24_draw and d3d12_bary_fallback.

Thanks @Kam471, @whattheflop, @SweatierKey, @MKmajster and @Vonsek for the runs on NVIDIA, RDNA2 and Linux.

AI-assisted (Claude Code).

@hah346

hah346 commented Nov 23, 2026

Copy link
Copy Markdown

Tested the rebased 267a0db0 on Windows 11 build 26300, GeForce RTX 2060 (616.64, subgroup 32), MSVC 19.51, Windows SDK headers 10.0.26100, with Khronos validation 1.4.369 active. Ten CTests passed (bary_fallback_gs and its four validator cases, d3d12_bary_fallback, d3d12_draw, d3d12_geometry, d3d12_depth24_draw, shader_library).

I also extended the synthetic barycentric draw locally to check the interaction introduced by the rebase. The test masks only the native D24 attachment capability in the test device's format query. The runtime then uses a real D32_SFLOAT_S8_UINT attachment, with a D24 API view, and draws the barycentric PS through the generated GS. With the fixture at z=0, a 0.125 depth clear, and GREATER comparison, the D24 bias of 4,194,304 is enough to draw. All 2,016 interior samples matched the expected barycentrics and passed-through color, and all 2,016 exterior samples stayed clear. With bias zero, all 4,032 judged samples stayed clear. Both runs passed with validation active and zero validation errors.

The only local code change for the extension is in the synthetic test. AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 267a0db (already contains main 98fa503) on Windows 11, RTX 4080 SUPER + Intel UHD 770 (MSVC 14.51): pass. Full runtime build.

  • ctest -R "bary_fallback|shader_library|d3d12_vulkan_device|d3d12_depth24|d3d12_compute": 11/11 pass on NVIDIA and 11/11 on Intel (shader_library_corpus skips, no game files).
  • d3d12_bary_fallback_test run directly on both GPUs: the layer drew the --bary-fallback pixel shader through the pass-through geometry stage, 2016 pixels inside the triangle hold their barycentrics and location 0, 2016 outside stayed clear. This ran the draw path on the NVIDIA card (which has the extension) and on Intel UHD 770 (which has no fragment barycentric), subgroup 32..32 and 8..32.
  • Control: main's runtime/shims/d3d12_vulkan_device.cpp with the PR's test: d3d12_bary_fallback_test fails at its line 139 (CreateGraphicsPipelineState(&lines...) == E_NOTIMPL) on both GPUs, so the test discriminates on both.

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