Skip to content

fix(shader): end a pixel shader's failed BDA reads through one exit - #7377

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
EagleBearerYS:fix/shader-pixel-bda-single-exit
Oct 19, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
EagleBearerYS:fix/shader-pixel-bda-single-exit

Conversation

@EagleBearerYS

@EagleBearerYS EagleBearerYS commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

What

On AMD's Windows driver, two titles crash the shader compiler inside vkCreateGraphicsPipelines:

StopBdaInvocationIf (SpirvBdaFault.cpp) ends a pixel invocation at every guest-memory read that faults. That's an OpTerminateInvocation when SPV_KHR_terminate_invocation is supported (#5804), and an OpKill otherwise. A large shader therefore carries one exit per read.

This is poonasor's fix from poonasor/AnyPS5@eb2e2309, ported to current main with them as co-author:

  • the pixel shader's guest code becomes its own function, and a failed read sets a Private flag and returns;
  • the entry point calls that function and terminates once if the flag is set;
  • primitive-ordered pixel shaders keep the old per-site form, because their interlock instructions must stay in the entry point.

Two changes from eb2e230:

  • The flag's interface listing: the flag is listed in the entry point's interface only from SPIR-V 1.4 on, as other Private variables are. Earlier versions list only Input and Output variables there.
  • Shaders without a fault site: these keep a plain entry point, with no exit and no SPV_KHR_terminate_invocation. Otherwise every unordered pixel shader would declare the extension. agc_driver_layered_volume_draw validates its mesh pipeline without shaderTerminateInvocation enabled, and rejects such a shader with "SPV_KHR_terminate_invocation requires enabled shaderTerminateInvocation in a fragment shader".

Tests: orderedPixelShaderTests (agc_driver_graphics) now expects these exits:

shader with SPV_KHR_terminate_invocation without it
unordered pixel shader with BDA reads one OpTerminateInvocation in the module, in the entry point one OpKill in the entry point, plus the shader's own
unordered pixel shader without BDA reads no exit in the entry point; the module passes ValidateShaders without shaderTerminateInvocation the same
primitive-ordered pixel shader unchanged unchanged

#6416 also edits StopBdaInvocationIf. Its write masking applies to every stage except pixel shaders, and this PR changes only pixel shaders, so the two compose. They merge without conflicts.

Tested

  • GitHub's Linux (lavapipe, Mesa 25.2.8) and Windows runners (verify run):
    • Red, on main's code: agc_driver_graphics fails with "AGC graphics: a pixel shader with BDA reads and SPV_KHR_terminate_invocation did not end its BDA faults through one OpTerminateInvocation in its entry point (5 in the module)".
    • Green, with the fix: all 15 selected AGC tests, agc_driver_graphics, agc_driver_layered_volume_draw and agc_driver_gpu_buffer_format among them, pass 10 of 10 runs on both runners. Two of them skip on the Windows runner, which has no Vulkan device.
    • Suites: libkernel passes on both runners, and libc on Windows. On the Linux runner, libc's precise_wait timing test failed in this run. It also failed there on main's code in an earlier run of the same tree, and it passes in the Linux mirror below.
  • Windows, the full CI build (fork Build): 1442 of 1442 tests pass, and check_skips passes.
  • Linux, a run that mirrors CI's Linux job (a cloud machine, Ubuntu 24.04, GCC 13.3, Mesa lavapipe 25.2.8 with LLVM 20.1.2, ANYPS5_ENABLE_SPIRV_TOOLS=ON, run as an unprivileged user):
    • Totals: 1415 tests. 5 fail, the IPv6 tests that machine fails on main too, and no test changes status against main 195d0d0.
    • check_skips flags only that machine's IPv6 skip, guest_sce_net, which main shows too.
    • SPIR-V validity: SPIRV-Tools validates the test's unordered pixel shader with BDA reads before and after optimization, at SPIR-V 1.3, 1.4, 1.5 and 1.6, with and without the extension. The flag is in the interface at 1.4 and later, and not at 1.3.
  • AMD Radeon 860M (RDNA 3.5, driver 32.0.13058.8001), Windows 11 26300, WinLibs GCC 15.2.0 r7, Release, run by LeandroLP23 at e8b3198 on main 6efb726 with 13 other open PRs, built once as one tree (comment):
    • agc_driver_gpu_buffer_format, agc_driver_graphics and agc_driver_layered_volume_draw pass 3 of 3 runs;
    • of all the agc_* tests, 713 of 716 pass. LeandroLP23 reports that the 3 that fail (agc_driver_host_import_lifetime, agc_driver_quad_pixel_mask and agc_driver_layered_volume_vertex_no_gpl) fail the same way on unchanged main there.
  • Not tested: either title. The tests have now run on AMD hardware (above), but the crash, and the fact that one exit avoids it, still come only from the two reports above: poonasor's offline reproduction, and Cleverdee's and poonasor's runs past the crash with eb2e230.

Checklist

Every guest-memory read of a pixel shader that can fault ended the
invocation with its own OpTerminateInvocation, or OpKill without the
extension. A main-menu pixel shader of NHL 27 (PPSA34063) has 216 such
exits and Demon's Souls (PPSA01342) up to 112, and AMD's Windows driver
crashes in vkCreateGraphicsPipelines on those modules
(amdvlk64.dll+0x9e251f).

The guest code is now emitted as its own function: a failed read sets a
private flag and returns, and the entry point calls it and terminates
once when the flag is set. A pixel shader without a fault site keeps a
plain entry point, with no exit and no SPV_KHR_terminate_invocation.
Primitive-ordered pixel shaders keep the old form, as their interlock
instructions must stay in the entry point.

Based on poonasor's design in poonasor/AnyPS5@eb2e2309, ported to
current main.

Co-authored-by: poonasor <12569806+poonasor@users.noreply.github.com>
@github-actions github-actions Bot added shader core/shader agc libSceAgc* labels Oct 18, 2026
@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1395 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 4040 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 5021 core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
PR 5441 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 5499 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 5500 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 5925 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6177 core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
PR 6188 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6194 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6235 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 6280 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6529 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6535 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6548 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6563 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6612 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 6639 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 6647 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6762 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 6804 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6808 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6941 core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 6947 core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 6970 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6972 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6977 core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
PR 7085 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 7201 core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 7254 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 7256 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 7390 core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 7392 core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvBdaFault.cpp
PR 7484 core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp

@LeandroLP23

Copy link
Copy Markdown
Contributor

AMD on Windows: the tests this PR names pass on an RDNA 3.5 GPU.

Radeon 860M (RDNA 3.5, driver 32.0.13058.8001), Windows 11 26300, WinLibs GCC 15.2.0 r7, Release. main 6efb7261b with this PR (e8b319833) merged, together with #7296, #7446, #7445, #6708, #6748, #6785, #6706, #6613, #7449, #7391, #7489, #7484, #7514. They were built once, so the result is for the combination; each merge was clean.

  • agc_driver_gpu_buffer_format, agc_driver_graphics, agc_driver_layered_volume_draw: pass, 3 runs of 3.
  • Every agc_* test on the combined build: 713 of 716 pass. The 3 that fail, agc_driver_host_import_lifetime, agc_driver_quad_pixel_mask and agc_driver_layered_volume_vertex_no_gpl, fail the same way on unchanged main here, so none comes from these PRs.

AI-assisted

@EagleBearerYS

Copy link
Copy Markdown
Contributor Author

@LeandroLP23 thanks for running them on the Radeon 860M. Your run is now in the Tested section, with your note that the three agc_* failures fail the same way on unchanged main there.

@github-actions github-actions Bot added the unblocks title Names a title ID on the Unblocks line label Oct 18, 2026
@thuanquang

Copy link
Copy Markdown
Contributor

A reproduction of the crash on a discrete RDNA 2 card, red on main and green with this PR. RX 6700 XT (gfx1031, poonasor's model), AMD Windows driver 32.0.21045.5002 (the 21045 branch of Cleverdee's 32.0.21045.11001), Windows 11 26200, WinLibs GCC 15.2.0 r7, Release.

The test, agc_driver_pixel_bda_stops: a pixel shader that does N chained global_load_dword reads of mapped guest memory and exports the result, drawn once over mapped memory (all pixels must come out white) and once with the reads pointed past it (the draw must report the failed read and leave the target unchanged). It also counts the module's invocation exits. Each run below is a fresh renamed copy of the test executable, so AMD's per-executable pipeline cache starts empty, with ANYPS5_NO_SHADER_CACHE=1. Two runs per row:

N reads main c4c2b3aa7 main + this PR (e8b319833)
96 the pipeline is created; the test then fails its exit count (97 exits) passes, 2 exits
128 FATAL: unhandled exception 0xc0000005, rip amdvlk64.dll+0x9e251f passes, 2 exits
160 the same crash passes, 2 exits
216 (NHL 27's exit count) the same crash passes, 2 exits
250 the same crash passes, 2 exits (one run)

On main the process dies in the first draw, at amdvlk64.dll+0x9e251f, the offset in both reports above. With this PR the module ends the invocation in 2 places, the one BDA exit and the valid-mask kill, and every draw is correct.

With this PR, the three tests the description names and this one pass 3 of 3 runs each under ctest on this GPU: agc_driver_graphics, agc_driver_gpu_buffer_format, agc_driver_layered_volume_draw and agc_driver_pixel_bda_stops. The first, cold run of the 160-read pipeline took 167 s here, while parallel builds kept the CPU busy; the later runs, served from the driver cache, took about 5 s. I haven't timed it on an idle machine.

The test is on my fork as one commit on top of this PR's head: thuanquang/AnyPS5@d95a7903a (tests/execution/PixelBdaStops.cpp, 160 reads). Feel free to take it into this PR if you want an execution test of the crash. On a device that compiles the per-read form, it still fails on main, at the exit count.

@wachi35676

Copy link
Copy Markdown
Contributor

AMD hardware result, which the PR lists as not tested. Tested at e8b319833 on Windows 11 (GCC 15.2.0, Release), merged onto main 9f25d0bf5, on an AMD Radeon iGPU, RDNA2 (1002:13c0, 32.0.21042.62, ANYPS5_GPU=AMD) and an RTX 5070 Ti (617.42).

  • agc_driver_graphics, agc_driver_layered_volume_draw and agc_driver_gpu_buffer_format pass on both.
  • In the 715-test agc_|shader|recompiler set, no test changes status against main on either GPU.
  • With the three shader files set back to main, agc_driver_graphics fails on both GPUs with ... did not end its BDA faults through one OpTerminateInvocation in its entry point (5 in the module).

I didn't reproduce the amdvlk crash itself; that needs the titles' large shaders.

AI-assisted: yes (Claude Code); results above were run on this machine.

RINNECODER added a commit to RINNECODER/AnyPS5 that referenced this pull request Oct 19, 2026
* fix(shader): pass the minimum LOD sampler bank into non-entry-point functions

CompilerMSL::to_name qualifies an argument-buffer member only while it emits the
default entry point. The minimum LOD bank is referenced by name, so a pixel body
emitted in its own function saw minimumLodSamplers out of scope and the shader
failed to compile. A real access chain on the bank in every function that samples
through it makes extract_global_variables_from_function pass the bank as an
argument, exactly like the original sampler array.

* fix(shader): end a pixel shader's failed BDA reads through one exit

Integrates upstream boykopovar#7377. A pixel body with BDA reads is emitted as a second
function and the entry point terminates once, after it returns, when a fault was
recorded; the fault sites return instead of terminating from wherever they are.
The bridge change below makes the minimum LOD sampler bank visible in that second
function.

* fix(shader): type a wrapped body's store pointers before its purity check

SPIRV-Cross clears expression types at the start of every compile pass, and
function_is_pure then reads the type of each OpStore pointer, including blocks
that are never emitted. A pixel body wrapped in its own function by the one-exit
BDA change hit an unregistered access chain there and the shader failed with
"Cannot resolve expression type". Register the type of a callee's store pointers
just before its OpFunctionCall is emitted.

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

Apple GPU / MoltenVK run. One regression on Metal: agc_driver_stencil_ref_export_unused fails with this PR and passes without it.

Machine: Apple M2 Max, macOS 27.2, x86-64 build under Rosetta. Head e8b3198, PR diff applied cleanly on top of the tree below. Drivers: MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0) and MoltenVK main + SPIRV-Cross #2704.

Tested: the tree is main a373e85 + mugurc's macOS series (ci/macos-series-2, includes ImportWatched) + the PRs needed to build on macOS (#6667, #6668, #7524, #6969, and two files of #7332), not plain main (it doesn't build on macOS yet). Base on that tree: 40 ^agc failures with MoltenVK 1.4.2, the same known failures in every run below unless noted.

  • agc_driver_graphics, agc_driver_gpu_buffer_format, agc_driver_layered_volume_draw pass (the mesh case skips: no VK_EXT_mesh_shader).
  • thuanquang's PixelBdaStops.cpp (d95a790) passes: 160 chained reads, 2 invocation exits. Without the PR (exit-count check disabled) Metal also compiles the 161-exit module and both draws are correct, so Apple doesn't have the amdvlk crash.
  • Full ^agc suite: 40 of 728 fail, as on the base, but one of them is new: agc_driver_stencil_ref_export_unused fails 5 of 5 runs with the PR on MoltenVK 1.4.2 and also on MoltenVK main + #2704, and passes on the base tree. (The count matches only because agc_driver_cross_workgroup_flag, intermittent on Apple, passed in this run.)

MoltenVK rejects the fragment shader:

program_source:56:5: error: no matching function for call to '_22'
    _22(gl_HelperInvocation, gl_FragStencilRefARB, out.out_mrt_0, vsharp);
program_source:25:6: note: candidate function not viable: no known conversion from 'int' to 'uint &' (aka 'unsigned int &') for 2nd argument
[gpu] driver failure: AGC graphics: vkCreateGraphicsPipelines: Vulkan result -3

The guest code is now its own function (%22), and gl_FragStencilRefARB (BuiltIn FragStencilRefEXT, OpVariable %_ptr_Output_int) is stored there. In this test the pipeline doesn't export stencil, so SPIRV-Cross makes the builtin a local int gl_FragStencilRefARB; in main0. It still declares the helper's parameter with the builtin's MSL type, thread uint&, so the call doesn't compile. Before this PR the store stayed in the entry point and no parameter was needed. agc_driver_stencil_ref_export, where the stencil output is live, passes.

That's a SPIRV-Cross issue, but this PR is what exposes it. Possible ways around it: declare the stencil reference output as a 32-bit uint (FragStencilRefEXT takes a scalar 32-bit integer), or keep the per-site form for pixel shaders that write FragStencilRefEXT, as for primitive-ordered ones. I haven't tried either. The MSL and SPIR-V dumps are available if useful.

GPUs tested: Apple M2 Max (MoltenVK 1.4.2; MoltenVK main + SPIRV-Cross #2704 for the stencil failure)

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

@oneandonlydean
oneandonlydean merged commit 4ffb0cd into boykopovar:main Oct 19, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* shader core/shader unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants