fix(agc): assemble layered stencil plane views through DepthPlaneCopy #26579

Merged
birdmarty merged 1 commit from upstream-pr/26579 into main 2026-11-24 14:41:09 +00:00
birdmarty commented 2026-11-24 05:54:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by birdmarty on 2026-11-24T05:54:15Z.


What

DepthSurfaceTexture previously forced every stencil view through DepthSurface::Sampled (sampled = !cropped && (stencil || (!layered && !integer))), which accepts only a single slice (depthOrLastArray == 0). Multi-slice k2DArray or kCube views over a resident depth surface's stencil plane threw sampling the stencil plane of depth surface ... is not implemented, even though multi-slice depth-plane views already assemble their slices through DepthPlaneCopy.

This change routes layered (k2DArray with depthOrLastArray != 0, or kCube) single-sample R8_UINT and R8_UNORM stencil views through DepthPlaneCopy, copying each resident slice from VK_IMAGE_ASPECT_STENCIL_BIT and detiling any non-resident slice from guest memory into a 4-byte-aligned staging stride. Single-slice stencil views stay on DepthSurface::Sampled (no extra copy for R8_UINT), and unsupported 1-byte formats (such as R8_SINT), multi-mip views, out-of-range baseArray values, and multisampled arrays still throw.

Changes

  • core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp:
    • Route stencil && layered single-sample views to DepthPlaneCopy (sampled = !cropped && !layered && (stencil || !integer)), accepting VK_FORMAT_R8_UINT and VK_FORMAT_R8_UNORM and keeping HasStencil(found->hostFormat) as the host-format check for stencil planes.
    • Extend DepthPlaneCopy for 1-byte stencil formats (VK_FORMAT_R8_UINT, VK_FORMAT_R8_UNORM): texelBytes(), sourceAspect() (VK_IMAGE_ASPECT_STENCIL_BIT), aspect() (VK_IMAGE_ASPECT_COLOR_BIT), 4-byte-aligned stagingLayerStride (for VUID-VkBufferImageCopy-bufferOffset-09101 and 4-byte detiler range alignment), {.pipeBankXor = resource.pipeBankXor} on TextureDetiler::Dispatch, and pipeBankXor in GuestLayer's cache key.
    • Add stencilSlice(device, address, base) via planeSurface(device, address) matching only active, non-retired 1-sample stencil-bearing owners of the same extent (never resurrecting a displaced older surface), call SyncStencilFromMemory and ApplyFastClear per matched slice, and key planeCopies() by base.stencilAddress for stencil views.
  • core/libs/prx/libSceAgcDriver/tests/execution/DepthPlaneSampling.cpp: add StencilArrayCase (3-slice 160x90 R8_UINT 2D array mixing 2 resident drawn stencil slices and 1 guest-memory tiled slice, baseArray = 1, redraw refresh, 2D shader image view, and displacement by a newer different-extent owner), StencilCubeCase (6-face 128x128 R8_UINT cube), StencilUnormArrayCase (2-slice 128x128 R8_UNORM 2D array), and refusal checks (R8_SINT, baseArray > lastArray).
  • core/libs/prx/libSceAgcDriver/tests/DepthSurfaceReuse.cpp: verify a 2-slice R8_UINT 2D array over a D16_UNORM_S8_UINT surface stored as D24_UNORM_S8_UINT copies VK_IMAGE_ASPECT_STENCIL_BIT into an R8_UINT VK_IMAGE_ASPECT_COLOR_BIT array image, and does not reuse a displaced older slice.
  • docs/dev/debt/changes/fix-agc-layered-stencil-plane-sampling.md: document layered stencil plane sampling scope and remaining gaps.

Tested

Windows 11 Pro (build 26300), Intel Core i9-9900K, AMD Radeon RX 6800 (AMD driver 26.10.1, Windows driver 32.0.21045.16010, Vulkan 1.4.315), MinGW-w64 GCC 15.3.0, Release, main 441fa65119.

Focused set (agc_driver_graphics, agc_driver_depth_plane_sampling, agc_driver_depth_plane_sampling_evicting, agc_driver_depth_plane_guest_layers, agc_driver_stencil_plane_memory, agc_driver_stencil_copy_draw, agc_driver_depth_copy_draw, agc_driver_depth_plane_fill, agc_driver_depth_plane_buffer_memory, agc_driver_depth_surface_last_writer) with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (1.4.363.0) and VK_LOADER_DEBUG=layer:

main 441fa65119 this change
Passed / failed 10 / 0 10 / 0
Validation layer inserted 11 11
VUID- lines 0 0

Without the fix (origin/main's DepthSurface.cpp with this branch's tests), both agc_driver_depth_plane_sampling and agc_driver_graphics fail with AGC graphics: sampling the stencil plane of depth surface ... as a ... texture of guest format 5 (vk 13), tile mode 4, dimension 2, levels 0-0, slice 0 is not implemented.

Kingdom Come: Deliverance II (PPSA21704, 01.052.000):

  • What the game does: renders 32 active 1-sample 160x90 D32_SFLOAT_S8_UINT depth/stencil slices spaced 0x10000 bytes apart (0x23c20f0000..0x23c22e0000, kZ64KBX, pipeBankXor = 0) and samples them in a compute shader as a 32-slice k2DArray of R8_UINT (guest format 5).
  • Without this change: on main e60b2f74ad + PR #26578 (fix/agc-ucp-cull-only-without-clip-planes), aborts at ~178–229 s entering the outdoor Siege of Suchdol transition with sampling the stencil plane of depth surface 0x23c22f0000 (160x90, vk format 130) as a 160x90 texture of guest format 5 (vk 13), tile mode 4, dimension 2, levels 0-0, slice 0 is not implemented (reproduced twice).
  • With this change: on main e60b2f74ad + PR #26578 + this change (no diagnostics), passes the stop cleanly across 4 consecutive runs: renders the outdoor Siege of Suchdol battlements and valley (~20–26 FPS), completes the post-cutscene loading screen (~47–59 FPS), and enters live first-person 3D gameplay ("Main quest started: Last Rites", aiming crossbow, walking up the ramparts stairs, game saving) with 0 visual glitches and 0 driver errors.

py -3 tools/check_conventions.py --base origin/main and py -3 tools/debt_fold.py --check pass. py -3 tools/pr_overlap.py --branch fix/agc-layered-stencil-plane-sampling (2,594 open PRs): all 9 line-adjacent PRs (#10894, #18185, #19697, #20041, #22531, #22778, #24083, #24327, #25610) and 19 conflicting PRs (#1368, #1395, #6808, #7256, #7642, #9508, #9895, #9950, #10193, #11444, #11551, #13863, #13920, #13928, #13929, #15621, #20315, #22062, #22647) were checked manually; none implements layered stencil plane sampling (#1368 is stale/conflicting from Oct 2026 before DepthPlaneCopy).

Not tested: multi-mip layered stencil views, multisampled stencil arrays, console hardware, other GPUs, Linux.

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: PPSA21704 #26577
  • GPUs tested: AMD Radeon RX 6800 (vendor=0x1002, device=0x73bf), AMD proprietary driver 26.10.1 (Windows driver 32.0.21045.16010, Vulkan 1.4.315), Windows 11 Pro (build 26300); Khronos validation layer 1.4.363.0 on main and this change, 0 VUIDs
  • AI-assisted: yes (Antigravity)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5466914920 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26579). Originally posted by [birdmarty](https://github.com/birdmarty) on 2026-11-24T05:54:15Z. --- ### What `DepthSurfaceTexture` previously forced every stencil view through `DepthSurface::Sampled` (`sampled = !cropped && (stencil || (!layered && !integer))`), which accepts only a single slice (`depthOrLastArray == 0`). Multi-slice `k2DArray` or `kCube` views over a resident depth surface's stencil plane threw `sampling the stencil plane of depth surface ... is not implemented`, even though multi-slice depth-plane views already assemble their slices through `DepthPlaneCopy`. This change routes layered (`k2DArray` with `depthOrLastArray != 0`, or `kCube`) single-sample `R8_UINT` and `R8_UNORM` stencil views through `DepthPlaneCopy`, copying each resident slice from `VK_IMAGE_ASPECT_STENCIL_BIT` and detiling any non-resident slice from guest memory into a 4-byte-aligned staging stride. Single-slice stencil views stay on `DepthSurface::Sampled` (no extra copy for `R8_UINT`), and unsupported 1-byte formats (such as `R8_SINT`), multi-mip views, out-of-range `baseArray` values, and multisampled arrays still throw. #### Changes - `core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp`: - Route `stencil && layered` single-sample views to `DepthPlaneCopy` (`sampled = !cropped && !layered && (stencil || !integer)`), accepting `VK_FORMAT_R8_UINT` and `VK_FORMAT_R8_UNORM` and keeping `HasStencil(found->hostFormat)` as the host-format check for stencil planes. - Extend `DepthPlaneCopy` for 1-byte stencil formats (`VK_FORMAT_R8_UINT`, `VK_FORMAT_R8_UNORM`): `texelBytes()`, `sourceAspect()` (`VK_IMAGE_ASPECT_STENCIL_BIT`), `aspect()` (`VK_IMAGE_ASPECT_COLOR_BIT`), 4-byte-aligned `stagingLayerStride` (for `VUID-VkBufferImageCopy-bufferOffset-09101` and 4-byte detiler range alignment), `{.pipeBankXor = resource.pipeBankXor}` on `TextureDetiler::Dispatch`, and `pipeBankXor` in `GuestLayer`'s cache key. - Add `stencilSlice(device, address, base)` via `planeSurface(device, address)` matching only active, non-retired 1-sample stencil-bearing owners of the same extent (never resurrecting a displaced older surface), call `SyncStencilFromMemory` and `ApplyFastClear` per matched slice, and key `planeCopies()` by `base.stencilAddress` for stencil views. - `core/libs/prx/libSceAgcDriver/tests/execution/DepthPlaneSampling.cpp`: add `StencilArrayCase` (3-slice 160x90 `R8_UINT` 2D array mixing 2 resident drawn stencil slices and 1 guest-memory tiled slice, `baseArray = 1`, redraw refresh, 2D shader image view, and displacement by a newer different-extent owner), `StencilCubeCase` (6-face 128x128 `R8_UINT` cube), `StencilUnormArrayCase` (2-slice 128x128 `R8_UNORM` 2D array), and refusal checks (`R8_SINT`, `baseArray > lastArray`). - `core/libs/prx/libSceAgcDriver/tests/DepthSurfaceReuse.cpp`: verify a 2-slice `R8_UINT` 2D array over a `D16_UNORM_S8_UINT` surface stored as `D24_UNORM_S8_UINT` copies `VK_IMAGE_ASPECT_STENCIL_BIT` into an `R8_UINT` `VK_IMAGE_ASPECT_COLOR_BIT` array image, and does not reuse a displaced older slice. - `docs/dev/debt/changes/fix-agc-layered-stencil-plane-sampling.md`: document layered stencil plane sampling scope and remaining gaps. ### Tested Windows 11 Pro (build 26300), Intel Core i9-9900K, AMD Radeon RX 6800 (AMD driver 26.10.1, Windows driver 32.0.21045.16010, Vulkan 1.4.315), MinGW-w64 GCC 15.3.0, Release, `main` `441fa65119`. Focused set (`agc_driver_graphics`, `agc_driver_depth_plane_sampling`, `agc_driver_depth_plane_sampling_evicting`, `agc_driver_depth_plane_guest_layers`, `agc_driver_stencil_plane_memory`, `agc_driver_stencil_copy_draw`, `agc_driver_depth_copy_draw`, `agc_driver_depth_plane_fill`, `agc_driver_depth_plane_buffer_memory`, `agc_driver_depth_surface_last_writer`) with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (1.4.363.0) and `VK_LOADER_DEBUG=layer`: | | `main` `441fa65119` | this change | |---|---|---| | Passed / failed | 10 / 0 | 10 / 0 | | Validation layer inserted | 11 | 11 | | `VUID-` lines | 0 | 0 | Without the fix (`origin/main`'s `DepthSurface.cpp` with this branch's tests), both `agc_driver_depth_plane_sampling` and `agc_driver_graphics` fail with `AGC graphics: sampling the stencil plane of depth surface ... as a ... texture of guest format 5 (vk 13), tile mode 4, dimension 2, levels 0-0, slice 0 is not implemented`. Kingdom Come: Deliverance II (`PPSA21704`, `01.052.000`): - **What the game does:** renders 32 active 1-sample `160x90` `D32_SFLOAT_S8_UINT` depth/stencil slices spaced `0x10000` bytes apart (`0x23c20f0000`..`0x23c22e0000`, `kZ64KBX`, `pipeBankXor = 0`) and samples them in a compute shader as a 32-slice `k2DArray` of `R8_UINT` (`guest format 5`). - **Without this change:** on `main` `e60b2f74ad` + PR #26578 (`fix/agc-ucp-cull-only-without-clip-planes`), aborts at ~178–229 s entering the outdoor Siege of Suchdol transition with `sampling the stencil plane of depth surface 0x23c22f0000 (160x90, vk format 130) as a 160x90 texture of guest format 5 (vk 13), tile mode 4, dimension 2, levels 0-0, slice 0 is not implemented` (reproduced twice). - **With this change:** on `main` `e60b2f74ad` + PR #26578 + this change (no diagnostics), passes the stop cleanly across 4 consecutive runs: renders the outdoor Siege of Suchdol battlements and valley (~20–26 FPS), completes the post-cutscene loading screen (~47–59 FPS), and enters live first-person 3D gameplay ("Main quest started: Last Rites", aiming crossbow, walking up the ramparts stairs, game saving) with 0 visual glitches and 0 driver errors. `py -3 tools/check_conventions.py --base origin/main` and `py -3 tools/debt_fold.py --check` pass. `py -3 tools/pr_overlap.py --branch fix/agc-layered-stencil-plane-sampling` (2,594 open PRs): all 9 line-adjacent PRs (#10894, #18185, #19697, #20041, #22531, #22778, #24083, #24327, #25610) and 19 conflicting PRs (#1368, #1395, #6808, #7256, #7642, #9508, #9895, #9950, #10193, #11444, #11551, #13863, #13920, #13928, #13929, #15621, #20315, #22062, #22647) were checked manually; none implements layered stencil plane sampling (`#1368` is stale/conflicting from Oct 2026 before `DepthPlaneCopy`). Not tested: multi-mip layered stencil views, multisampled stencil arrays, console hardware, other GPUs, Linux. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) and the licence notice of code adapted from upstream (see AGENTS.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: PPSA21704 #26577 - [x] GPUs tested: AMD Radeon RX 6800 (vendor=0x1002, device=0x73bf), AMD proprietary driver 26.10.1 (Windows driver 32.0.21045.16010, Vulkan 1.4.315), Windows 11 Pro (build 26300); Khronos validation layer 1.4.363.0 on `main` and this change, 0 VUIDs - [x] AI-assisted: yes (Antigravity)
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!26579
No description provided.