fix(shader): restore the per-instance hull PrimitiveId on MoltenVK, whose tessellation control dispatch spans instances #20111

Open
XIIYAZ wants to merge 1 commit from upstream-pr/20111 into main
XIIYAZ commented 2026-11-14 16:21:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XIIYAZ on 2026-11-14T16:21:03Z.


What

On MoltenVK, agc_driver_tessellation fails in its instanced relative-patch cases:

the patch in column 0 of an instanced draw with 4 patches per group and 8 primitives per group did not take its blue channel from the hull's relative patch index across instances, (255, 0, 96, 255)

When a hull program uses its relative patch index as data, TessellationLowering::groupedPatch builds it from PrimitiveId. When instances share hull threadgroups, it adds instance * instancePatches first. Vulkan restarts PrimitiveId at 0 for each instance. MoltenVK does not. It runs the tessellation control stage as one compute dispatch over every patch of every instance, and SPIRV-Cross derives the ID from the dispatch:

uint gl_PrimitiveID = min(gl_GlobalInvocationID.x / 3, spvIndirectParams[1] - 1);

So in instance 1, the hull sees PrimitiveId 3 for the first patch instead of 0:

  • The shared-group case adds the instance offset a second time: 1*3 + 3 = 6, 6 mod 4 = 2 instead of 3. That is blue 96 instead of 128.
  • The BREAK_WAVE_AT_EOI case gets 3 where the per-instance 0 belongs. That is blue 128 instead of 32.

SpirvTarget::tessellationPrimitiveIdPerInstance (true by default, false on VK_DRIVER_ID_MOLTENVK) records that difference. For an instanced draw on such a device, TessellationPrimitiveIdInstancePatches(request) gives the draw's patches per instance (indexCount / inputControlPoints). The hull's PrimitiveId is taken modulo that before the existing grouping. That restores the per-instance value Vulkan specifies, so both the shared-group and the break-at-instance paths stay exactly as they were:

  • RequestInputInfo puts the value in TessellationConfiguration::primitiveIdInstancePatches. It is derived per request, like instancePatches and drawParameters, and added at the end of the struct so positional initializers keep their meaning.
  • CacheKey appends TessellationPrimitiveIdInstancePatches(request) next to TessellationInstancePatches(request), plus the target flag. The driver already skips its draw-level stage entries for instanced tessellation draws.
  • Only the tessellation control stage changes. The domain-program refusal for relative patch data in shared-group instanced draws is unchanged.
  • The flag is part of the target, so RequestSerializer moves to version 58. Version 57 requests read with the flag true. RequestSerializerTests and ShaderMemory move their version 57 expectations to 58 and cover reading version 57.

Tested

Setup:

  • Apple M4, macOS 26.7.3, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1), Apple clang 21.0.0
  • full Release build with -DCMAKE_OSX_ARCHITECTURES=x86_64; tests run under Rosetta
  • ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1
  • on main with the open macOS build fixes applied

Results:

  • agc_driver_tessellation fails before this change and passes after it: all three instanced relative-patch draws (shared groups at 4/8 and 3/3, and BREAK_WAVE_AT_EOI) and every refusal case.
  • ctest -R 'tessellation|hull|domain|request_serializer|agc_shader_memory' passes.
  • I ran ctest -R '^(agc|shader|job)' (1578 tests, -j 1) and compared it with the same run before this change. agc_driver_tessellation is fixed and nothing that passed before fails because of this change. agc_driver_msaa_dcc_clear_2 failed once in that run, then passed 3 of 4 reruns; it has no tessellation stage. agc_driver_scratch_pool is intermittent on this machine with and without this change.

Not run on lavapipe, AMD or NVIDIA. There the flag stays true, primitiveIdInstancePatches is 0, and the generated shaders are unchanged. #20030 also moves the serializer from 57 to 58; whichever lands second takes 59.

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
  • 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: none
  • GPUs tested: Apple M4 (MoltenVK 1.4.2, LunarG Vulkan SDK 1.4.363.1, macOS 26.7.3)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5313530171 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20111). Originally posted by [XIIYAZ](https://github.com/XIIYAZ) on 2026-11-14T16:21:03Z. --- ### What On MoltenVK, `agc_driver_tessellation` fails in its instanced relative-patch cases: ``` the patch in column 0 of an instanced draw with 4 patches per group and 8 primitives per group did not take its blue channel from the hull's relative patch index across instances, (255, 0, 96, 255) ``` When a hull program uses its relative patch index as data, `TessellationLowering::groupedPatch` builds it from `PrimitiveId`. When instances share hull threadgroups, it adds `instance * instancePatches` first. Vulkan restarts `PrimitiveId` at 0 for each instance. MoltenVK does not. It runs the tessellation control stage as one compute dispatch over every patch of every instance, and SPIRV-Cross derives the ID from the dispatch: ``` uint gl_PrimitiveID = min(gl_GlobalInvocationID.x / 3, spvIndirectParams[1] - 1); ``` So in instance 1, the hull sees `PrimitiveId` 3 for the first patch instead of 0: - The shared-group case adds the instance offset a second time: `1*3 + 3 = 6`, `6 mod 4 = 2` instead of 3. That is blue 96 instead of 128. - The `BREAK_WAVE_AT_EOI` case gets 3 where the per-instance 0 belongs. That is blue 128 instead of 32. `SpirvTarget::tessellationPrimitiveIdPerInstance` (`true` by default, `false` on `VK_DRIVER_ID_MOLTENVK`) records that difference. For an instanced draw on such a device, `TessellationPrimitiveIdInstancePatches(request)` gives the draw's patches per instance (`indexCount / inputControlPoints`). The hull's `PrimitiveId` is taken modulo that before the existing grouping. That restores the per-instance value Vulkan specifies, so both the shared-group and the break-at-instance paths stay exactly as they were: - `RequestInputInfo` puts the value in `TessellationConfiguration::primitiveIdInstancePatches`. It is derived per request, like `instancePatches` and `drawParameters`, and added at the end of the struct so positional initializers keep their meaning. - `CacheKey` appends `TessellationPrimitiveIdInstancePatches(request)` next to `TessellationInstancePatches(request)`, plus the target flag. The driver already skips its draw-level stage entries for instanced tessellation draws. - Only the tessellation control stage changes. The domain-program refusal for relative patch data in shared-group instanced draws is unchanged. - The flag is part of the target, so `RequestSerializer` moves to version 58. Version 57 requests read with the flag `true`. `RequestSerializerTests` and `ShaderMemory` move their version 57 expectations to 58 and cover reading version 57. ### Tested Setup: - Apple M4, macOS 26.7.3, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1), Apple clang 21.0.0 - full Release build with `-DCMAKE_OSX_ARCHITECTURES=x86_64`; tests run under Rosetta - `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1` - on `main` with the open macOS build fixes applied Results: - `agc_driver_tessellation` fails before this change and passes after it: all three instanced relative-patch draws (shared groups at 4/8 and 3/3, and `BREAK_WAVE_AT_EOI`) and every refusal case. - `ctest -R 'tessellation|hull|domain|request_serializer|agc_shader_memory'` passes. - I ran `ctest -R '^(agc|shader|job)'` (1578 tests, `-j 1`) and compared it with the same run before this change. `agc_driver_tessellation` is fixed and nothing that passed before fails because of this change. `agc_driver_msaa_dcc_clear_2` failed once in that run, then passed 3 of 4 reruns; it has no tessellation stage. `agc_driver_scratch_pool` is intermittent on this machine with and without this change. Not run on lavapipe, AMD or NVIDIA. There the flag stays `true`, `primitiveIdInstancePatches` is 0, and the generated shaders are unchanged. #20030 also moves the serializer from 57 to 58; whichever lands second takes 59. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: Apple M4 (MoltenVK 1.4.2, LunarG Vulkan SDK 1.4.363.1, macOS 26.7.3) - [x] AI-assisted: yes
MarcZX10 commented 2026-11-14 17:09:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-14T17:09:54Z.

Review: COMMENTED


Reviewed — no blocking findings

The code read matches the stated MoltenVK per-instance PrimitiveId correction; the two excluded large files leave material integration and test details unverified, so a formal approval is not justified.

What I ran

Inspected the prepared diff and pinned source at head 7fe817ed6c0f275d1d0f32841d77c4b814d1b3cf using repo_read.py search and targeted reads. No build or tests were run in this review.

Results

Check Result Note
Diff and manifest review passed All prepared diff hunks inspected
Targeted source review passed Lowering, cache key, serializer, and request setup read
Build and tests not run No commands run

Findings

Blocking

None.

Non-blocking

None.

Not verified

The full text of core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp and core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp was excluded from the review folder because each exceeded its size limit; targeted changed ranges in both were read through the pinned reader. Other changed files whose full text was included were not read in full; the relevant changed hunks and targeted context were inspected. The author-reported build and test results were not independently run. No negative control was run.

To get this to a good state

Nothing is required. Optional: confirm the reported MoltenVK tessellation and serializer tests on the pinned head.

Continued review (pass 2)

The specified continuation lines add serialization version handling and coverage for the tessellation primitive ID per instance option; the inspected code shows consistent round-trip, legacy-default, and truncation checks.

What I ran

Read the requested line ranges at the pinned head using repo_read.py reader tools. No tests were run.

Results

Check Result Note
Requested lines passed All listed ranges read
Tests not run Continuation review only

Findings

Blocking

None in the inspected continuation lines.

Non-blocking

None.

Not verified

This review covers only the specified continuation lines and their immediate context. The rest of the pull request and test execution were not verified.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5906015962 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20111#pullrequestreview-5906015962). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-14T17:09:54Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The code read matches the stated MoltenVK per-instance PrimitiveId correction; the two excluded large files leave material integration and test details unverified, so a formal approval is not justified. ## What I ran Inspected the prepared diff and pinned source at head `7fe817ed6c0f275d1d0f32841d77c4b814d1b3cf` using `repo_read.py` search and targeted reads. No build or tests were run in this review. ## Results | Check | Result | Note | |---|---|---| | Diff and manifest review | passed | All prepared diff hunks inspected | | Targeted source review | passed | Lowering, cache key, serializer, and request setup read | | Build and tests | not run | No commands run | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The full text of `core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp` and `core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp` was excluded from the review folder because each exceeded its size limit; targeted changed ranges in both were read through the pinned reader. Other changed files whose full text was included were not read in full; the relevant changed hunks and targeted context were inspected. The author-reported build and test results were not independently run. No negative control was run. ## To get this to a good state Nothing is required. Optional: confirm the reported MoltenVK tessellation and serializer tests on the pinned head. ## Continued review (pass 2) The specified continuation lines add serialization version handling and coverage for the tessellation primitive ID per instance option; the inspected code shows consistent round-trip, legacy-default, and truncation checks. ## What I ran Read the requested line ranges at the pinned head using `repo_read.py` reader tools. No tests were run. ## Results | Check | Result | Note | |---|---|---| | Requested lines | passed | All listed ranges read | | Tests | not run | Continuation review only | ## Findings ### Blocking None in the inspected continuation lines. ### Non-blocking None. ## Not verified This review covers only the specified continuation lines and their immediate context. The rest of the pull request and test execution were not verified. ## To get this to a good state Nothing is required.
kurexw commented 2026-11-17 03:23:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kurexw on 2026-11-17T03:23:15Z.

Review: COMMENTED


Tested on an Apple M4 (macOS 27.2, MoltenVK 1.4.2 from the LunarG SDK 1.4.363.1, x86-64 build under Rosetta).

This PR doesn't merge cleanly into current main (bb95654123). git merge-tree reports conflicts in:

  • core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
  • core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp
  • core/shader/recompiler/tests/RequestSerializerTests.cpp

I didn't resolve them. I tested the PR's merge-base 2bc1db7ca8 against the PR head 7fe817ed6c, with no changes to either side. Both built apart from libcurl.prx (#17425).

The test set is the 29 agc_driver_* tests that fail on current main on this Mac (ds_permute, f32_denormal_flush, image_sample_lod_*, label_*, pixel_sample_*, provoking_vertex, queued_indirect_read_order, tessellation, and others); the 26 of them that exist at the merge-base ran. Environment: ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, VK_LOADER_DEBUG=layer, three runs per side. Every test process logged Insert instance layer "VK_LAYER_KHRONOS_validation" (layer 1.4.363), and none logged the missing-layer message.

merge-base (3 runs) this PR (3 runs)
agc_driver_tessellation Failed Passed
the other 25 identical identical
VUID- lines per run 10 10

At the merge-base, the failure is the patch in column 0 of an instanced draw with 4 patches per group and 8 primitives per group did not take its blue channel from the hull's relative patch index across instances, (255, 0, 96, 255). With this PR, it logs Tessellation tests passed. The 10 VUID- lines are the same on both sides: 4 VUID-VkDeviceCreateInfo-sType-unique (from Recorder.cpp, addressed by #21965) and 6 VUID-VkSamplerCreateInfo-samplerMipLodBias-04467. No regressions.

The conflicting serializer files are where request versions get bumped, so the version number will need to be rechecked after rebasing. #20153 and #21938 also move it to 63.

I didn't test this on Linux, Windows or another GPU vendor.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5932263392 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20111#pullrequestreview-5932263392). Originally posted by [kurexw](https://github.com/kurexw) on 2026-11-17T03:23:15Z. **Review:** COMMENTED --- Tested on an Apple M4 (macOS 27.2, MoltenVK 1.4.2 from the LunarG SDK 1.4.363.1, x86-64 build under Rosetta). This PR doesn't merge cleanly into current `main` (bb95654123). `git merge-tree` reports conflicts in: - `core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp` - `core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp` - `core/shader/recompiler/tests/RequestSerializerTests.cpp` I didn't resolve them. I tested the PR's merge-base 2bc1db7ca8 against the PR head 7fe817ed6c, with no changes to either side. Both built apart from `libcurl.prx` (#17425). The test set is the 29 `agc_driver_*` tests that fail on current `main` on this Mac (`ds_permute`, `f32_denormal_flush`, `image_sample_lod_*`, `label_*`, `pixel_sample_*`, `provoking_vertex`, `queued_indirect_read_order`, `tessellation`, and others); the 26 of them that exist at the merge-base ran. Environment: `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `VK_LOADER_DEBUG=layer`, three runs per side. Every test process logged `Insert instance layer "VK_LAYER_KHRONOS_validation"` (layer 1.4.363), and none logged the missing-layer message. | | merge-base (3 runs) | this PR (3 runs) | | --- | --- | --- | | `agc_driver_tessellation` | Failed | Passed | | the other 25 | identical | identical | | `VUID-` lines per run | 10 | 10 | At the merge-base, the failure is `the patch in column 0 of an instanced draw with 4 patches per group and 8 primitives per group did not take its blue channel from the hull's relative patch index across instances, (255, 0, 96, 255)`. With this PR, it logs `Tessellation tests passed`. The 10 `VUID-` lines are the same on both sides: 4 `VUID-VkDeviceCreateInfo-sType-unique` (from `Recorder.cpp`, addressed by #21965) and 6 `VUID-VkSamplerCreateInfo-samplerMipLodBias-04467`. No regressions. The conflicting serializer files are where request versions get bumped, so the version number will need to be rechecked after rebasing. #20153 and #21938 also move it to 63. I didn't test this on Linux, Windows or another GPU vendor.
ligmaSec commented 2026-12-03 18:36:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T18:36:42Z.

Review: COMMENTED


The workaround has two independent inputs worth preserving on rebase. The draw's patch count selects the hull PrimitiveId modulo, and the target flag controls whether that modulo is emitted. The older request round trip in this branch correctly defaults to per-instance IDs. Current main explicitly rejects request version 58 and the serializer now conflicts in the merge, so the new field needs the next available format version. It would be useful to keep the older-version assertion and check both lowering paths with the same instanced request on a non-MoltenVK CI host.

Source review of 7fe817ed6c. The MoltenVK execution evidence is from the author's run.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6176095146 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20111#pullrequestreview-6176095146). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T18:36:42Z. **Review:** COMMENTED --- The workaround has two independent inputs worth preserving on rebase. The draw's patch count selects the hull `PrimitiveId` modulo, and the target flag controls whether that modulo is emitted. The older request round trip in this branch correctly defaults to per-instance IDs. Current `main` explicitly rejects request version 58 and the serializer now conflicts in the merge, so the new field needs the next available format version. It would be useful to keep the older-version assertion and check both lowering paths with the same instanced request on a non-MoltenVK CI host. Source review of `7fe817ed6c`. The MoltenVK execution evidence is from the author's run. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/20111:upstream-pr/20111'
git switch 'upstream-pr/20111'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/20111'
git switch 'upstream-pr/20111'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/20111'
git switch 'upstream-pr/20111'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/20111'
git switch 'main'
git merge --squash 'upstream-pr/20111'
git switch 'main'
git merge --ff-only 'upstream-pr/20111'
git switch 'main'
git merge 'upstream-pr/20111'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!20111
No description provided.