fix(agc): bind memory only for the backed slices of a depth plane copy #22531
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/22531"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by bagodees on 2026-11-17T19:15:42Z.
What
A depth plane sampled as a 2D array was copied into an image with memory for every slice from 0 to
LAST_ARRAY, whatever backs them. Uncharted: Legacy of Thieves Collection (PPSA05684) samples its 2560x1440 D32 depth plane as a 2D array whoseLAST_ARRAYvaries by frame; in 11 of 57 cold runs it reached 896 to 1792. Each such sample allocated one copy of 14 to 28 GB plus a staging buffer of the same size, both placed in host memory after the device refused them. Only slice 0 has a depth surface, and 1792 slices of that plane (about 26 GB) is more than the console's memory, so most of the declared range cannot be backed.When slices past some point have neither a depth surface nor committed guest memory, the copy is now a sparse image of every declared slice that binds memory only up to the last slice that has either. The staging buffer covers only those slices. Unbound slices read zero, as uncommitted guest memory is read here, so loads, samples and size queries return what the fully bound copy returned. This needs sparse residency for the copy's format,
residencyNonResidentStrictand enough array layers for a sparse image; otherwise, and for cubes and thick layouts, the copy binds every slice as before. A copy is made again when a later sample finds more backed slices than it binds.SparseImageSupported,SparseRequirementsandBindSparseLevelsmove out of the sampled-texture path inTexture.cppso the copy can use them, andPlanSparseLevelstakes the aspect, since these copies are depth images.ContextgainsresidencyNonResidentStrict.Tested
Windows 11 Pro (26300), NVIDIA GeForce RTX 4080 (driver 621.01), WinLibs GCC 15.2 UCRT, Release, on
main89b0eef7ab. The driver change is in3e12a17b7e;3eca88d569changes only the test. The RTX 4080 reports sparse residency for D32, D16 and R16_UINT 2D images (up to 2048 layers) andresidencyNonResidentStrict.agc_driver_depth_plane_sampling(UnbackedTailCase): a 128x128 D32 2D array withLAST_ARRAY255 in a guest block of its own, whose first 2 slices are committed and whose other 254 are reserved and uncommitted (PROT_NONEon Linux, reserved arena memory on Windows), so the OS reports them unreadable on every host. At3e12a17b7ethe case sat at the end of the shared 8 MiB block instead, which fails wherever readable memory follows that block, as on the Linux run below. At3eca88d569, slices 0, 1, 2 and 255 are read withimage_load: the drawn value, the filled value, 0 and 0. Where the device can make the copy sparse it must hold less than 16 slices; here it holds 262144 bytes, against 33554432 onmain. Where it cannot, it must hold every slice. With the sparse path forced off, the case passes with every slice bound (33554432 bytes).3e12a17b7e:ctest -R "depth|sparse"(111 tests;agc_depth_range_remap,guest_ces_sparse_profiles*andwindows_sparse_loadexcluded) withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1,VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(SDK 1.4.363),VK_LOADER_DEBUG=layer, on the change and onmain. Every test that ran passed on both, apart from tests that skip themselves on this machine (agc_driver_native_depth_driver_queued_fill,agc_driver_sparse_dma_buf_import,agc_host_import_sparse) on both.agc_driver_texture_sparse_levelsandagc_driver_sparse_buffer_holeswere then built and run on both and pass. The new case fails onmain(shown by the first revision of this PR; onmainit does not build now, as it usesDepthPlaneCopyCanBeSparse). Every log shows the layer inserted, nowas not foundline and noVUID-.agc_depth_range_remap_tests.exeis removed by Windows Defender right after linking on this machine, onmaintoo, so it was not run.3eca88d569: bothagc_driver_depth_plane_samplingregistrations with the same environment, on the sparse path (262144 bytes) and with it forced off (33554432 bytes): pass, layer inserted, noVUID-.ctest -R depthat3e12a17b7eon Arch Linux with an RTX 4090 (driver 620.70.06): noVUID-, and bothagc_driver_depth_plane_samplingregistrations failed in the old placement of the case, with values that change between runs (one is the ASCII textTabl), which points to readable memory after the 8 MiB block rather than the copy.3eca88d569is not yet run there.tools/check_conventions.py --base upstream/mainandgit diff --checkpass.Not tested: AMD and Mesa drivers at the current head. tablefrogpillow's RADV and lavapipe runs were of the first revision (
a07e947ce5), whose shortened copy left loads past it out of bounds. Where these drivers lack sparse residency for depth formats, the copy binds every slice, as onmain.tools/pr_overlap.pyreports #18185 on neighbouring lines ofDepthSurfaceTexture(it adds fields to each slice) and #10894, #19697 and #20041 on the offsets table ofDepthPlaneSampling.cpp; neither implements this change.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T10:54:29Z.
Review: DISMISSED
Reviewed — changes requested
Changes are required before merge: compute the bound slice count from committed ranges without treating gaps before the last range as backed.
What I ran
3eca88d569daed6a6ee1999b0412da8183dda28dfrom the prepared diff and reader. No build or tests were run.VulkanDevice.cpp,Context.hpp,DepthSurface.hpp,Texture.hpp,DepthSurface.cpp,Texture.cpp,DepthPlaneSampling.cpp, andGuestMemory.cpp; also revieweddescription.md,commits.md,manifest.json, anddiff.patch.VulkanDevice.cppandTexture.cppfull texts (the changed hunks were read through the reader).files.jsonwas unavailable in the reader; the manifest lists all eight changed files and says six full texts are included.Results
Findings
Blocking
core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp:171—backedPlaneLayersderives the backing prefix fromranges.back().second. If committed guest memory has a gap and a later committed range, this counts every intervening layer as backed. The sparse image then binds those gap layers and stages them withReadCommitted, despite their pages being uncommitted. Compute the maximum backed prefix from contiguous committed bytes starting ataddress(while still accounting for depth surface slices), and add coverage for a committed range after a gap.Non-blocking
None.
Not verified
To get this to a good state
Fix the gap handling in
backedPlaneLayersand add a regression case with committed pages after an uncommitted gap. Then rerun the relevant depth-plane sampling tests.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T05:02:31Z.
Review: COMMENTED
Reviewed — comments
The sparse copy bounds memory use to the last backed layer, but the changes leave a correctness and resource-lifetime issue in the sparse image path.
What I ran
Reviewed head
73e3c65537860540c57c3720c92731f01e8a8d1fusingrepo_read.pyto inspect the prepared diff and targeted source ranges. No commands or tests were run during this review.Results
VulkanDevice.cppandTexture.cppwere inspected only at relevant rangesFindings
Blocking
None verified.
Non-blocking
The sparse image is created with
arrayLayers = layers, butPlanSparseLevelsis called witharrayLayers = max(backed, 1). The resulting bind plan allocates sparse binds only for the backed prefix, while the image's remaining array layers are intentionally nonresident. This appears consistent with the stated design, but depends on sparse residency semantics and the strict-nonresident feature; physical-device behavior was not tested here. Seecore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp:2646.The
GuestBlocktest destructor removes allocation tracking and unmaps/releases reserved memory before unregistering write-watch ranges. If unregistering consults or accesses the memory range, that order could be problematic; I did not inspect the unregister implementation. Seecore/libs/prx/libSceAgcDriver/tests/execution/DepthPlaneSampling.cpp:151.Not verified
core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppandcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppwere excluded from the review bundle; only the relevant changed and neighboring ranges were read throughrepo_read.py.Context.hpp,DepthSurface.hpp,Texture.hpp,DepthSurface.cpp,DepthPlaneSampling.cpp, and the debt note as included; targeted sections ofDepthSurface.cppandDepthPlaneSampling.cppand the relevant declaration/implementation ranges in the excluded files were inspected.To get this to a good state
Nothing is required. The points above are optional context for follow-up validation.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T18:30:49Z.
Review: COMMENTED
A shared mip tail can bind the layers that
Refreshskips. If the driver reportsimageMipTailFirstLod == 0withVK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT,PlanSparseLevelsbinds the one opaque tail for every declared array layer even when passed a smaller layer count.DepthPlaneCopystill setsresidentto the smaller count, andRefreshskips all layers at or above it. Those layers have bound, uninitialized depth texels, soresidencyNonResidentStrictcannot supply zeroes for them. CouldDepthPlaneCopyfall back to a fully initialized copy when its only mip shares a tail across layers, and cover that requirements shape in the sparse-plan test? Vulkan sparse mip tail rulesSource review of
73e3c65537. The current integration has conflicts in the depth copy and its test.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.