Skip to content

fix(agc): reimport a multisampled target recreated with another pipe-bank XOR - #7641

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
milescoviello:fix/msaa-target-recreated-xor
Oct 20, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
milescoviello:fix/msaa-target-recreated-xor

Conversation

@milescoviello

@milescoviello milescoviello commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

What

acquire in MultisampleTarget.cpp reuses a resident multisampled target for any use with the same surface address, format, extent and sample count. checkDccCurrent then requires every other layout field to match, and refuses with multisampled DCC target layout or metadata changed.

In Ruffle (PPSA68091), 7 of Ruffle's own cache_as_bitmap visual 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:

surface 0x4004c60000 864x864 fmt 37 s4 | ... xor 0xc000/0x4000
surface 0x4004b40000 1080x1080 fmt 37 s4 | ... xor 0xc000/0x0

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:

  • recreated is true when a use differs from a resident target only in its pipe-bank XOR.
  • acquire then 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.
  • Any other layout change is still refused as before.

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_button recreates 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).

With this head, a temporary print counted the recreations and the number of multisampled targets the driver holds after each one:

Test SWF without this PR with it recreations in 32 s targets held (max) coarse
cache_as_bitmap__avm2_button stops at 12 s: multisampled DCC target layout or metadata changed runs to the limit 28 2 2.4
cache_as_bitmap__avm2_button_state stops at 11 s on the same line runs to the limit 35 2 1.1
cache_as_bitmap__edittext_hscroll stops at 12 s on the same line runs to the limit 43 2 no expected image in Ruffle's tree
cache_as_bitmap__morph stops at 11 s on the same line runs to the limit 2 2 0.5
cache_as_bitmap__children_changed, __shape_changed, __cab_bitmapdata_invalidate stop at 11 s on the same line run to the limit 1 each 2 0.1, 0.1, 0.0

tools/check_conventions.py --base upstream/main passes. tools/pr_overlap.py lists no overlapping lines with open pull requests. The retirement sits inside acquire's loop, and recreated beside sameTarget, away from the lines that #6215, #6973, #6987, #6989, #6995 and #7002 change.

Checklist

🤖 Generated with Claude Code

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up after tracing the ownership path: targets() retains the inactive unique_ptr<MultisampleTarget>; inactive entries are not erased by acquire. The Vulkan device teardown waits for vkDeviceWaitIdle before ClearMultisampleTargets(device) erases them. That resolves my lifetime question: recorded commands keep their referenced image alive until teardown. I did not find a defect here.

@milescoviello

milescoviello commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor Author

@MarcZX10 yes, that's the guarantee. ClearMultisampleTargets(device) is the only place that erases from targets(), and the device teardown reaches it after vkDeviceWaitIdle. 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)

@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

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.

Pull request Shared files not compared
PR 6683 core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp
PR 6808 core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp
PR 6971 core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp
PR 7002 core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp
PR 7948 core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp
PR 8223 core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp

@github-actions github-actions Bot added the agc libSceAgc* label Oct 18, 2026
@milescoviello

Copy link
Copy Markdown
Contributor Author

@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:

Test Recreations Target
cache_as_bitmap__avm2_button 28 864x864, 4x RGBA8
cache_as_bitmap__morph 2 160x130, 4x
cache_as_bitmap__shape_changed 1 160x130, 4x

So the cost isn't small in the first case. An 864x864 4x RGBA8 image is about 12 MiB, so avm2_button keeps 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, through Recorder::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)

…bank XOR

RADV gives each image its own pipe-bank XOR when it creates it. When Ruffle
recreates a 4x render target on the same memory, the next draw names the
same surface, DCC and CMASK with another XOR, and the resident target refused
it as a changed layout ("multisampled DCC target layout or metadata
changed"). A use that differs from a resident DCC target only in that XOR now
keeps the target's image, views and cached textures, takes the new layout,
and reimports its contents from the current DCC and CMASK metadata the way a
first use does, clearing the image to that color after the work already
recorded against it. Any other layout change is still refused.

Reusing the image keeps memory flat: Ruffle's cache_as_bitmap__avm2_button
recreates its 864x864 4x target 28 times in 32 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@milescoviello
milescoviello force-pushed the fix/msaa-target-recreated-xor branch from b01a2bf to 388527d Compare October 18, 2026 20:30
@milescoviello

Copy link
Copy Markdown
Contributor Author

@MarcZX10 pushed 388527d, 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:

Test Recreations Targets held (max) Coarse vs. reference
cache_as_bitmap__avm2_button 28 2 2.4
cache_as_bitmap__avm2_button_state 35 2 1.1
cache_as_bitmap__edittext_hscroll 43 2 no reference image
cache_as_bitmap__morph 2 2 0.5
children_changed, shape_changed, cab_bitmapdata_invalidate 1 each 2 0.1, 0.1, 0.0

The 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 further dccAlphaOnMsb change must still be refused. I updated the description.

AI-assisted: yes (Claude Code)

@savvaniss

Copy link
Copy Markdown
Contributor

Intel and AMD Windows data: agc_driver_msaa_dcc_clear_2, _4 and _8 fail on main and 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, with VkCache emptied first. This head 388527d6b merged into main f3059421f. "Without" means this PR's test files on main f3059421f, with none of its other changes. That run used the previous head b01a2bfcd, whose test files are the same.

Tests, ×3 UHD 770 RX 9070 XT
This head: agc_driver_alpha_to_coverage, agc_driver_dithered_alpha_to_coverage, agc_driver_msaa_dcc_clear_2, _4, _8 5 of 5 pass 5 of 5 pass
Without the three msaa_dcc_clear tests fail: multisampled DCC target layout or metadata changed same

I also ran full CTest with b01a2bfcd and 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 beyond main's own, except #7650's new test.

AI-assisted: yes (Claude Code)

@milescoviello

Copy link
Copy Markdown
Contributor Author

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)

@adriadam10 adriadam10 self-assigned this Oct 19, 2026
@adriadam10
adriadam10 merged commit e25c975 into boykopovar:main Oct 20, 2026
17 of 18 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Re-checked against current main: the merge is clean, and the new DCC fill absorption (absorbDccFill) composes with Recreate. A fill noted before the recreation either clears to the same uniform key initialDccClear already imported, or is not taken and the normal fast-clear path runs. The recreated condition, the Recreate clear ordering and the refusal of any other layout change are unchanged, and agc_driver_msaa_dcc_clear_{2,4,8} still fails on main with the refusal message; merged.

@milescoviello

Copy link
Copy Markdown
Contributor Author

Thanks @adriadam10 for checking it against the new DCC fill absorption, and for the merge.

AI-assisted: yes (Claude Code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc*

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants