fix(agc): reimport a multisampled target recreated with another pipe-bank XOR #7641
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7641"
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 milescoviello on 2026-10-18T19:03:11Z.
What
acquireinMultisampleTarget.cppreuses a resident multisampled target for any use with the same surface address, format, extent and sample count.checkDccCurrentthen requires every other layout field to match, and refuses withmultisampled DCC target layout or metadata changed.In Ruffle (PPSA68091), 7 of Ruffle's own
cache_as_bitmapvisual tests stop on that refusal. A temporary print of both layouts at the refusal showed that only the pipe-bank XOR differs. Address, DCC and CMASK addresses, DCC control, CMASK size, tile mode and element size are all the same:RADV gives each image its pipe-bank XOR (its tile swizzle) when it creates it, and doesn't change it afterwards. So the same surface with another XOR is a new image on recycled memory. Here Ruffle recreates the render target of a cached bitmap. The resident target's contents belong to the old image.
This PR:
recreatedis true when a use differs from a resident target only in its pipe-bank XOR.acquirethen reimports the resident target into its own image (Recreate). The target keeps its image, views and cached textures, takes the use's layout, drops its old guard ranges and DCC ranges, and imports its contents from the current DCC and CMASK metadata, as a first use does. The image is cleared to that color with a transfer clear recorded after the work already recorded against it, then the use's fast clear is applied as usual.An earlier head of this PR retired the resident target and created a new one. Retired targets stay allocated until device teardown, and
cache_as_bitmap__avm2_buttonrecreates its 864x864 4x RGBA8 target (about 12 MiB) 28 times in 32 s, so that kept about 330 MiB. Reimporting into the same image keeps the number of multisampled targets flat (see Tested).Tested
Ubuntu 24.04 (Linux 6.8), GCC 13.3, NVIDIA RTX 4070 Ti (595.99.02).
DccFastClearTests(agc_driver_msaa_dcc_clear_{2,4,8}) runs for each sample count:dccAlphaOnMsbmust still be refused.main'sMultisampleTarget.cpp, all three fail withmultisampled DCC target layout or metadata changed.^agc_driver_graphics$|msaa|multisample|dcc|cmask|fmask|alpha_to_coverage|fill|dispatchpass.tests/tests/swfs/visual), each the only game in the library, 32 s. These cases need #6543 and #7250 to get this far, so the stack ismain385ed71f2c+ #6543 + #7250 + #7233 + #7600. "Coarse" is the mean difference of 32x24-cell averages between the last frame and the test'soutput.expected.png, from 0 to 255.With this head, a temporary print counted the recreations and the number of multisampled targets the driver holds after each one:
cache_as_bitmap__avm2_buttonmultisampled DCC target layout or metadata changedcache_as_bitmap__avm2_button_statecache_as_bitmap__edittext_hscrollcache_as_bitmap__morphcache_as_bitmap__children_changed,__shape_changed,__cab_bitmapdata_invalidatetools/check_conventions.py --base upstream/mainpasses.tools/pr_overlap.pylists no overlapping lines with open pull requests. The retirement sits insideacquire's loop, andrecreatedbesidesameTarget, away from the lines that #6215, #6973, #6987, #6989, #6995 and #7002 change.388527d6bonmainf3059421f,agc_driver_alpha_to_coverage,agc_driver_dithered_alpha_to_coverageandagc_driver_msaa_dcc_clear_{2,4,8}pass three times each on an Intel UHD 770 and an AMD RX 9070 XT. With only this PR's test files onmain, the threemsaa_dcc_cleartests fail withmultisampled DCC target layout or metadata changed.VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 ctest -R '^agc_' -Vonmainf29546ac4eand onmain+ this PR (388527d6b2, a local merge).main: 812 of 812 pass; with this PR: 812 of 812 pass. Neither log reports the layer missing. VUIDs onmain:VUID-VkImageViewCreateInfo-usage-02275×6,VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605×18,VUID-vkCmdCopyBuffer-srcBuffer-00118×2,VUID-vkCmdDispatch-magFilter-04553×2,VUID-vkCmdDraw-multisampledRenderToSingleSampled-07285×2,VUID-vkCmdWaitEvents-srcStageMask-01158×2,VUID-vkSetEvent-event-09543×2; with this PR:VUID-VkImageViewCreateInfo-usage-02275×6,VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605×18,VUID-vkCmdCopyBuffer-srcBuffer-00118×2,VUID-vkCmdDispatch-magFilter-04553×2,VUID-vkCmdDraw-multisampledRenderToSingleSampled-07285×2,VUID-vkCmdWaitEvents-srcStageMask-01158×2,VUID-vkSetEvent-event-09543×2. None is new with this PR. Each ofmain's is the subject of an open PR: 00118 #7961, 04553 #7707, 07285 #7985, 01158 and 09543 #7913, 00605 #6782, 02275 #862.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T19:12:56Z.
Review: COMMENTED
The recreation path clears the old target's active flag and guard ranges, then leaves the object in the global target list so commands already recorded against its image remain valid. Could you point to the ownership/lifetime guarantee for the old resources after becomes false, especially if those recorded commands have not yet been submitted? The comment in the PR body explains the intent, but I could not verify from this diff whether a later cleanup path can destroy or reuse those resources before the queue completes. If it is retained until process teardown or otherwise fenced, documenting that invariant here would make the new retirement path easier to audit.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T19:13:00Z.
Review: COMMENTED
Clarification: the concern is whether the old MultisampleTarget resources remain alive until previously recorded GPU commands complete after active is set false. The backticks in my prior comment were interpreted by the shell, but the question is unchanged.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T19:13:24Z.
Review: COMMENTED
Follow-up after tracing the ownership path:
targets()retains the inactiveunique_ptr<MultisampleTarget>; inactive entries are not erased byacquire. The Vulkan device teardown waits forvkDeviceWaitIdlebeforeClearMultisampleTargets(device)erases them. That resolves my lifetime question: recorded commands keep their referenced image alive until teardown. I did not find a defect here.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-18T19:15:02Z.
@MarcZX10 yes, that's the guarantee.
ClearMultisampleTargets(device)is the only place that erases fromtargets(), and the device teardown reaches it aftervkDeviceWaitIdle. The retired object, with its image, memory, views and any textures it still holds, stays alive until then, whether or not the commands recorded against it have been submitted. The unmap retirement (retireUnmapped) already relies on the same invariant. The libSceAgcDriver debt entry for multisampled color targets states it: "Retired Vulkan images remain allocated until device teardown to preserve outstanding references."The cost is memory. A title that recreates a target many times keeps every retired image until teardown. I haven't counted how many recreations the Ruffle tests make over a run yet. I'll measure it and post the number here. If a title turns out to recreate targets every frame, the follow-up would be to free a retired target once a fence after its last use has signalled. I left that out to keep this change small.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T19:31:42Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cppcore/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cppcore/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cppcore/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cppcore/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cppcore/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cppImported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-18T19:42:54Z.
@MarcZX10 the count I owed, measured with a temporary print in the new retirement path. Ruffle on the same stack plus this head, 32 s each:
cache_as_bitmap__avm2_buttoncache_as_bitmap__morphcache_as_bitmap__shape_changedSo the cost isn't small in the first case. An 864x864 4x RGBA8 image is about 12 MiB, so
avm2_buttonkeeps about 330 MiB of retired images after 32 s, and a movie that recreates a cached bitmap about once a second keeps growing until teardown. The unmap retirement has the same retention, but a recreation can happen every frame, which an unmap can't, so I don't think this PR should merge with it. I'm working on freeing a retired target once the commands recorded against it have completed, throughRecorder::Keep. First I'm checking what else can still hold its image, such as the sampled views it hands out. I'll push that here with a test and a memory reading on the same Ruffle case.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-18T20:31:36Z.
@MarcZX10 pushed
388527d6b2, one commit replacing the retirement. A recreated target is now reimported into its own image instead of being replaced. It keeps its image, views and cached textures, takes the new layout, and imports its contents from the current DCC and CMASK metadata the way a first use does. That means a transfer clear to that color, recorded after the work already recorded against the image. Nothing is retired, so the retention question doesn't come up.The same Ruffle runs (same stack, 32 s each), with a temporary print counting the multisampled targets the driver holds after each recreation:
cache_as_bitmap__avm2_buttoncache_as_bitmap__avm2_button_statecache_as_bitmap__edittext_hscrollcache_as_bitmap__morphchildren_changed,shape_changed,cab_bitmapdata_invalidateThe frames match the retiring version's (avm2_button was 2.4 before too).
agc_driver_msaa_dcc_clear_{2,4,8}pass 3 of 3, and the 39-test area set passes. The test case is unchanged: the recreated XOR-0x4000 target must start from the new 1111 clear, and a furtherdccAlphaOnMsbchange must still be refused. I updated the description.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by savvaniss on 2026-10-18T21:53:00Z.
Intel and AMD Windows data:
agc_driver_msaa_dcc_clear_2,_4and_8fail onmainand pass with this PR, on an Intel UHD 770 and an AMD RX 9070 XT.Windows 11 Pro 26300, WinLibs GCC 15.2.0 r7, Release, every execution test built,
ANYPS5_NO_SHADER_CACHE=1, serial CTest. Intel UHD Graphics 770 (8086:a780), driver 32.0.101.7088. AMD Radeon RX 9070 XT (1002:7550), driver 32.0.31041.1004, withVkCacheemptied first. This head388527d6bmerged intomainf3059421f. "Without" means this PR's test files onmainf3059421f, with none of its other changes. That run used the previous headb01a2bfcd, whose test files are the same.agc_driver_alpha_to_coverage,agc_driver_dithered_alpha_to_coverage,agc_driver_msaa_dcc_clear_2,_4,_8msaa_dcc_cleartests fail:multisampled DCC target layout or metadata changedI also ran full CTest with
b01a2bfcdand the 14 other open AGC PRs I tested in the same batch merged together (#7647, #7630, #7627, #7603, #7600, #7591, #7579, #7535, #7533, #7474, #7462, #7050, #7048, #7650). Neither GPU showed a failure beyondmain's own, except #7650's new test.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-19T01:06:16Z.
Thanks @savvaniss, the before and after on Intel and AMD is useful. Added to the description. The title now matches the commit: it reimports the target rather than replacing it.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-20T11:18:38Z.
Re-checked against current
main: the merge is clean, and the new DCC fill absorption (absorbDccFill) composes withRecreate. A fill noted before the recreation either clears to the same uniform keyinitialDccClearalready imported, or is not taken and the normal fast-clear path runs. Therecreatedcondition, theRecreateclear ordering and the refusal of any other layout change are unchanged, andagc_driver_msaa_dcc_clear_{2,4,8}still fails onmainwith the refusal message; merged.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T11:26:07Z.
Thanks @adriadam10 for checking it against the new DCC fill absorption, and for the merge.
AI-assisted: yes (Claude Code)