Skip to content

fix(agc): bind a stage's samplers above the per-stage limit after bind - #227

Merged
utkuhalis merged 1 commit into
AnyTogether:mainfrom
hedibravofox:fix/sampler-update-after-bind
Dec 3, 2026
Merged

utkuhalis merged 1 commit into
AnyTogether:mainfrom
hedibravofox:fix/sampler-update-after-bind

Conversation

@hedibravofox

Copy link
Copy Markdown

What

On MoltenVK a shader stage with more than 16 guest samplers is refused, although the device can bind many more. MoltenVK reports maxPerStageDescriptorSamplers 16, but maxPerStageDescriptorUpdateAfterBindSamplers 500000, with descriptorBindingSampledImageUpdateAfterBind supported; MVK_CONFIG_USE_METAL_ARGUMENT_BUFFERS, which the driver already turns on, doesn't change either number. A compute shader in Grand Theft Auto V needs 17, so the game stops with:

compute shader 0x36cc68800: shader sampler descriptors exceed per-stage limits: required 17, device limit 16

The driver already falls back to an update-after-bind set when a stage's storage buffers go over their per-stage limit. This does the same for samplers:

  • ShaderResources::addImageBinding: a sampler array above maxPerStageDescriptorSamplers switches the set to update-after-bind when the device supports update-after-bind sampled images and the array fits maxPerStageDescriptorUpdateAfterBindSamplers. Otherwise it's refused as before; the message then names whichever limit applies. The pipeline's sampler total is checked against SetDescriptorLimit, which already returns the update-after-bind set limit for such a set.
  • DescribeSetLayouts counts samplers per stage and per set and makes the same choice, so a described layout matches the one the build creates.

Only the set layout's UPDATE_AFTER_BIND_POOL bit and the pool's flag change. Under the Vulkan spec, descriptors in such a layout count against the update-after-bind limits, so the sampler binding itself needs no update-after-bind flag.

Tested

macOS 27.2.1, Apple M5 Pro, MoltenVK 1.4.2, x86-64 build under Rosetta 2 (AppleClang 21.0.0, Release):

  • agc_driver_graphics: the existing 17-sampler test now also checks four cases on the mock device.
    • Without update-after-bind sampled images, 17 samplers are refused with the same message as before.
    • With them and an update-after-bind limit of 32, the set gets VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT and an update-after-bind pool, and nothing leaks.
    • With an update-after-bind limit of 16, they're refused ("required 17, device limit 16").
    • 16 samplers stay on the regular path.
      It passed 10 runs out of 11. The failing run stopped on BdaResources.cpp's host-import budget check ("two ranges under a budget for two were not imported"), which this change doesn't touch; main passed 5 runs out of 5 in the same session.
  • Negative control: with the old refusal in addImageBinding, the test fails ("required 17, device limit 16").
  • ctest -R 'agc_driver_graphics|sampler|Sampler|descriptor|image_table|pipeline_library|shader_resources|table_sampler|update_after|stencil_ref_export': 91 tests pass.
  • python3 tools/check_conventions.py --base origin/main: all checks passed.
  • Grand Theft Auto V (PPSA04263, 01.011.001) on the same Mac. This also used a local base-address edit, fix(libc): refuse untrapped access over read traps on macOS #226's libc, and upstream fix(shader): synchronize GLC and DLC buffer accesses on MoltenVK with device atomics and the guest's release and acquire boykopovar/AnyPS5#14217 applied locally, which fixes its look-back scans; without those it doesn't reach this scene.
    • Without this change, it stopped on the error above.
    • With it, that shader builds, and the game runs for the rest of the 10-minute run without a driver failure or crash, preparing new shaders and textures for the scene.
    • The window still shows the opening caption, though. The scene's frames don't reach the screen: other compute shaders there never finish, and a queue waits on them for more than 30 s. MoltenVK also runs out of timestamp query buffers. Those are separate problems and not this PR's topic.
  • Not run: the VK_LAYER_KHRONOS_validation comparison from AGENTS.md, because no validation layer is installed on this Mac. There's no MoltenVK device test with a real 17-sampler shader either; the game run is the device evidence.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: PPSA04263 (Grand Theft Auto V) past its 17-sampler compute shader on macOS; the scene still doesn't show
  • GPUs tested: Apple M5 Pro, MoltenVK 1.4.2, macOS 27.2.1 (game run); mock device for agc_driver_graphics
  • AI-assisted: yes

🤖 Generated with Claude Code

A stage whose guest sampler array was larger than
maxPerStageDescriptorSamplers was refused. MoltenVK reports 16 there but
500000 for update-after-bind samplers, so a compute shader with 17
samplers stopped PPSA04263 on macOS.

When the device supports update-after-bind sampled images and the array
fits maxPerStageDescriptorUpdateAfterBindSamplers, the set now takes an
update-after-bind layout and pool, as it already does for storage
buffers above their limit; the pipeline's sampler total is checked
against the matching set limit. DescribeSetLayouts counts samplers per
stage and per set and makes the same choice. Other devices are refused
as before.

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

Copy link
Copy Markdown
Collaborator

Thanks. Looked through it:

  • ValidateDescriptorLayout already takes maxPerStageDescriptorUpdateAfterBindSamplers and maxDescriptorSetUpdateAfterBindSamplers for an update-after-bind layout when sampledImageUpdateAfterBind is set, so a 17-sampler layout gets past validation once updateAfterBind is on.
  • SetDescriptorLimit(SAMPLER, true) returns the update-after-bind set limit under the same condition, so the pipeline total check matches.
  • Agreed on the spec reading: descriptors in a layout created with UPDATE_AFTER_BIND_POOL count against the update-after-bind limits, so the sampler binding needs no binding flag.

Small note, not blocking: DescribeSetLayouts also switches to update-after-bind when the pipeline's sampler total goes over maxDescriptorSetSamplers, while addImageBinding only switches on the per-stage count and then refuses the total. The two only disagree in a case the build refuses anyway, the same way the storage-buffer path works today.

We'll merge once CI is green.

@utkuhalis
utkuhalis merged commit 036a251 into AnyTogether:main Dec 3, 2026
58 of 70 checks passed
@utkuhalis

Copy link
Copy Markdown
Collaborator

Merged, thanks! GTA V's 17-sampler compute shader builds on MoltenVK now.

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.

2 participants