AGC: a comparison-sampled R32F surface stays a CPU snapshot, compared against guest memory under the GPU mutex after every GPU write #1133

Closed
opened 2026-10-07 13:24:18 +00:00 by fisherjoey · 3 comments
fisherjoey commented 2026-10-07 13:24:18 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-07T13:24:18Z.


Summary

cachedTexture never views a storage image for a depthCompare lookup (source.reset() at Graphics/src/ShaderResources.cpp:315, and the !depthCompare condition at :339 on main e79c971f). A shadow map rendered on the GPU into host-imported memory is therefore served as a CPU snapshot uploaded into a D32_SFLOAT image. After each GPU write-back into the range, the snapshot's hit rule (equalsCommitted) flushes the pending results, waits for that GPU work through the flush hook, and compares the whole surface with guest memory, all under the GPU mutex.

A plain view would not serve the sampler on this GPU anyway. Driver 595.91.07 reports no depth-comparison sampling on R32_SFLOAT or R16_UNORM (vkGetPhysicalDeviceFormatProperties2); D32_SFLOAT and D16_UNORM support it.

Evidence

Demon's Souls (PPSA01341 v1.07), PS Studios movie. Every compare is the same surface: 0x2c25b0000+0x1000000, 1024x1024, fmt 22 (R32_SFLOAT), 4 layers (shadow cascades), on queues 0x30 and 0x40. That is about 10 compares a second, each equal in 1.3-8 ms (one took 53 ms). In about 1000 compares, one differed.

Per 10 s: queue 0x30 holds the GPU mutex 950-1163 ms, and an estimated 0.5-0.7 s of that is these compares. Queue 0 waits 1974-2170 ms for the mutex, 994-1127 ms of it behind queue 0x30. Direct dispatches on queue 0 spend 78-99 us each waiting for the lock.

Proposed fix

Give such surfaces a depth image that follows their storage image on the GPU:

  • A D32_SFLOAT (or D16_UNORM) image of the surface's shape. Whenever the storage image's version has moved since the last copy, it is refilled on the GPU, recorded into the open batch behind the producer: vkCmdCopyImageToBuffer from the storage image into a device buffer, then vkCmdCopyBufferToImage into the depth aspect.
  • Used only for host-imported R32F / R16U surfaces that are not 3D, whose storage image holds every level and layer, and that are at least 1 MiB (APS5_DEPTH_COPY_MIN_KIB). APS5_NO_DEPTH_COPY=1 restores the snapshot path.

Measured, per 10 s of the movie:

before after
queue 0x30 mutex held 950-1163 ms 195 ms
[hooksync] on queue 0x30 242 syncs, 16384K texture-compare 49 syncs, all <= 4K
queue 0 mutex wait 1974-2170 ms 1222 ms
direct dispatch on queue 0: lock wait / total 78-99 / 157-166 us 42.8 / 108 us

The attract movie went from 384-407 to 429-439 presents per 10 s (41.8-45.0 fps). The PS Studios movie stayed at 255 presents. It is limited by how the queue 0 worker thread gets scheduled, which I'll file separately. agc_driver_graphics and guest_agc_driver_resources pass.

About the 1 MiB floor: without it, a 48x24 R16_UNORM surface also got a copy, and both runs with that build hit the intermittent boot-time crash that looks like #479 (a compute shader reading a garbage descriptor). With the floor, 0 of 2 runs crashed. That is a small sample.

Another route would be to treat R32F as not natively comparable on devices that lack the format feature, and let the shader-side compare from #668 sample a plain view. That would skip the copy but adds ALU work per tap. I have not tried it.

Found on Linux 7.2 (Nobara 44), GCC 16, RTX 5070 Ti (595.91.07), with Demon's Souls PPSA01341 v1.07. Measured on main at c1241ad0 with #157, #861, #864 and #1051, plus the path fix from #1047.

I can send a PR for this if the approach looks right.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5745734786 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1133). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-07T13:24:18Z. --- ### Summary `cachedTexture` never views a storage image for a `depthCompare` lookup (`source.reset()` at `Graphics/src/ShaderResources.cpp:315`, and the `!depthCompare` condition at :339 on `main` e79c971f). A shadow map rendered on the GPU into host-imported memory is therefore served as a CPU snapshot uploaded into a D32_SFLOAT image. After each GPU write-back into the range, the snapshot's hit rule (`equalsCommitted`) flushes the pending results, waits for that GPU work through the flush hook, and compares the whole surface with guest memory, all under the GPU mutex. A plain view would not serve the sampler on this GPU anyway. Driver 595.91.07 reports no depth-comparison sampling on R32_SFLOAT or R16_UNORM (`vkGetPhysicalDeviceFormatProperties2`); D32_SFLOAT and D16_UNORM support it. ### Evidence Demon's Souls (PPSA01341 v1.07), PS Studios movie. Every compare is the same surface: `0x2c25b0000+0x1000000`, 1024x1024, fmt 22 (R32_SFLOAT), 4 layers (shadow cascades), on queues 0x30 and 0x40. That is about 10 compares a second, each `equal` in 1.3-8 ms (one took 53 ms). In about 1000 compares, one differed. Per 10 s: queue 0x30 holds the GPU mutex 950-1163 ms, and an estimated 0.5-0.7 s of that is these compares. Queue 0 waits 1974-2170 ms for the mutex, 994-1127 ms of it behind queue 0x30. Direct dispatches on queue 0 spend 78-99 us each waiting for the lock. ### Proposed fix Give such surfaces a depth image that follows their storage image on the GPU: - A D32_SFLOAT (or D16_UNORM) image of the surface's shape. Whenever the storage image's version has moved since the last copy, it is refilled on the GPU, recorded into the open batch behind the producer: `vkCmdCopyImageToBuffer` from the storage image into a device buffer, then `vkCmdCopyBufferToImage` into the depth aspect. - Used only for host-imported R32F / R16U surfaces that are not 3D, whose storage image holds every level and layer, and that are at least 1 MiB (`APS5_DEPTH_COPY_MIN_KIB`). `APS5_NO_DEPTH_COPY=1` restores the snapshot path. Measured, per 10 s of the movie: | | before | after | |---|---|---| | queue 0x30 mutex held | 950-1163 ms | 195 ms | | `[hooksync]` on queue 0x30 | 242 syncs, 16384K texture-compare | 49 syncs, all <= 4K | | queue 0 mutex wait | 1974-2170 ms | 1222 ms | | direct dispatch on queue 0: lock wait / total | 78-99 / 157-166 us | 42.8 / 108 us | The attract movie went from 384-407 to 429-439 presents per 10 s (41.8-45.0 fps). The PS Studios movie stayed at 255 presents. It is limited by how the queue 0 worker thread gets scheduled, which I'll file separately. `agc_driver_graphics` and `guest_agc_driver_resources` pass. About the 1 MiB floor: without it, a 48x24 R16_UNORM surface also got a copy, and both runs with that build hit the intermittent boot-time crash that looks like #479 (a compute shader reading a garbage descriptor). With the floor, 0 of 2 runs crashed. That is a small sample. Another route would be to treat R32F as not natively comparable on devices that lack the format feature, and let the shader-side compare from #668 sample a plain view. That would skip the copy but adds ALU work per tap. I have not tried it. Found on Linux 7.2 (Nobara 44), GCC 16, RTX 5070 Ti (595.91.07), with Demon's Souls PPSA01341 v1.07. Measured on `main` at c1241ad0 with #157, #861, #864 and #1051, plus the path fix from #1047. I can send a PR for this if the approach looks right. AI-assisted: yes.
fisherjoey commented 2026-10-07 18:01:18 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-07T18:01:18Z.


The fix described above is ready on a branch, rebased on current main (cea1ef13): https://github.com/fisherjoey/AnyPS5/compare/main...fisherjoey:AnyPS5:fix/depth-compare-copy

One commit. It keeps APS5_NO_DEPTH_COPY and APS5_DEPTH_COPY_MIN_KIB (default 1024) and adds a TechnicalDebt line for the 1 MiB floor and the 3D exclusion. A new GPU test, agc_driver_depth_compare_copy, writes a 512x512 R32F surface with a compute store, compare-samples it, rewrites it and samples it again. agc_driver and guest_agc_driver pass (160 of 160, same two exclusions).

I'll open the PR if the approach looks right.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6043782411 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1133#issuecomment-6043782411). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-07T18:01:18Z. --- The fix described above is ready on a branch, rebased on current `main` (cea1ef13): https://github.com/fisherjoey/AnyPS5/compare/main...fisherjoey:AnyPS5:fix/depth-compare-copy One commit. It keeps `APS5_NO_DEPTH_COPY` and `APS5_DEPTH_COPY_MIN_KIB` (default 1024) and adds a TechnicalDebt line for the 1 MiB floor and the 3D exclusion. A new GPU test, `agc_driver_depth_compare_copy`, writes a 512x512 R32F surface with a compute store, compare-samples it, rewrites it and samples it again. `agc_driver` and `guest_agc_driver` pass (160 of 160, same two exclusions). I'll open the PR if the approach looks right.
oneandonlydean commented 2026-10-08 08:26:29 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-08T08:26:29Z.


Please open it as a PR, with before/after numbers for this change alone on the same scene.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6055873170 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1133#issuecomment-6055873170). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-08T08:26:29Z. --- Please open it as a PR, with before/after numbers for this change alone on the same scene.
klosixgaming-arch commented 2026-10-08 11:52:53 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-08T11:52:53Z.


This is an excellent analysis of the depth surface comparison performance issue. Based on my review of recent PRs related to the AGC driver and shader recompiler, I can provide some context:

The proposed fix of giving such surfaces a depth image that follows their storage image on the GPU seems reasonable. The measured improvements (queue 0x30 mutex held time reduced from 950-1163 ms to 195 ms per 10 s) are significant and would likely improve performance for many titles that use shadow mapping.

Some observations based on similar issues I've seen:

  1. The fact that the driver doesn't support depth-comparison sampling on R32_SFLOAT or R16_UNORM explains why the current approach uses a CPU snapshot
  2. The proposed GPU copy approach (vkCmdCopyImageToBuffer followed by vkCmdCopyBufferToImage) is a standard technique for this type of problem
  3. The 1 MiB floor to avoid issues with small surfaces is a reasonable heuristic

I'd suggest implementing this fix, as it addresses a real performance bottleneck that affects many titles. The fact that the attract movie fps improved from 384-407 to 429-439 presents per 10 s demonstrates the practical benefit.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6059262674 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1133#issuecomment-6059262674). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-08T11:52:53Z. --- This is an excellent analysis of the depth surface comparison performance issue. Based on my review of recent PRs related to the AGC driver and shader recompiler, I can provide some context: The proposed fix of giving such surfaces a depth image that follows their storage image on the GPU seems reasonable. The measured improvements (queue 0x30 mutex held time reduced from 950-1163 ms to 195 ms per 10 s) are significant and would likely improve performance for many titles that use shadow mapping. Some observations based on similar issues I've seen: 1. The fact that the driver doesn't support depth-comparison sampling on R32_SFLOAT or R16_UNORM explains why the current approach uses a CPU snapshot 2. The proposed GPU copy approach (vkCmdCopyImageToBuffer followed by vkCmdCopyBufferToImage) is a standard technique for this type of problem 3. The 1 MiB floor to avoid issues with small surfaces is a reasonable heuristic I'd suggest implementing this fix, as it addresses a real performance bottleneck that affects many titles. The fact that the attract movie fps improved from 384-407 to 429-439 presents per 10 s demonstrates the practical benefit.
Sign in to join this conversation.
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#1133
No description provided.