renderer: decide color metadata clears on the GPU #1283
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1283
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1283/head"
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?
@NeySlim on GitHub · 2026-10-09 07:25 UTC · original
NeySlim:perf/gpu-meta-clear→main· +621 −14 · 13 files · 6 commits · closed without mergingProblem
When a color target or a view of it is bound,
MaterializeColorCleardecides on the CPU whether each metadata slice holds a fast-clear key (CMASK key 0, DCC keys 0x20, 0x00, 0x40, 0x80, 0xc0). When a compute shader wrote the metadata, which is how games fast-clear,ReadMemoryfirst downloads it and the command thread waits until the GPU has finished the work recorded so far.God of War Ragnarök (PPSA08332, 05.005) rewrites the metadata of several targets by compute every frame. The command thread blocks on these readbacks about 4 times per frame: 3.4 ms per frame in the opening cutscene and 4.8 ms per frame in the first gameplay section (scheduler traces,
perf record -e sched:sched_switch, blocked time attributed by stack).Change
VK_EXT_conditional_renderingis available, the decision runs on the GPU. A compute pass (gpu_meta_clear_check.comp, one workgroup per slice) checks that all bytes of a slice equal one of the keys the target decodes and writes one predicate per key. When a key matches, it also writes the expanded keys (0xff) in place, as the CPU path does in guest memory.vkCmdClearAttachmentsunder conditional rendering, one per slice and per decodable key, with the color the CPU path would use. Same decision, same point in the command stream.vkCmdClearAttachmentsbut not to transfer clears, so the GPU path needs an image created with color attachment usage and a view format with the color attachment feature. Otherwise the CPU path is used, as before.BufferCachecan watch a range: a GPU write, an upload, an invalidation, a CPU write or a shader address write marks it changed. When a target is bound again with unchanged metadata and the same decode inputs, the last decision is reused: the bytes are either expanded or not a clear for these inputs, so the CPU path would not clear either. A shader address write has no known range, so every draw or dispatch with address writes (has_address_writes) marks all watched ranges changed: in frames with many such shaders, as in God of War Ragnarök, the decision cache rarely hits and the gain comes from removing the readbacks. Each watch keeps its own change state, so watching a range that overlaps another does not hide a change already recorded for the other. Unmapping memory drops its watches and decisions.Test
RenderExecutorColorMetadataClearruns with conditional rendering enabled in the test harness. After the native compute fill of the metadata, binding the target must not read the metadata back (the range stays GPU-written). All existing cases of the test now go through the GPU path and still pass: clear value, expanded keys after a later readback, rebinding unchanged metadata keeps rendered texels, CPU re-clear, partial and repeated fills, texture views. With the GPU path disabled the new check fails (binding a target read its GPU-written metadata back to the CPU).The test also removes the color attachment usage from the cached image (standing in for a host format that cannot be rendered to; on RADV every color format the emulator maps supports it, so no real format reaches this case here) and checks that the next fill is decided through a readback and still clears; without the capability check it fails. After unmapping, no watched range or cached decision may remain; it fails without the purge. Two overlapping watches: after a GPU write to the overlap and a new watch of the second range, the first must still report the change; it fails when a new watch clears the changes of the ranges it overlaps. The harness only queries the conditional rendering features when the device exposes the extension.
Validation
Linux, AMD Radeon 8060S (RDNA 3.5), Mesa RADV 26.2.3.
shader_recompiler_compute_tests, one case per process: same results as main (595 of 604 pass; the 9 others fail on main too on this setup).resource_tracking_testsandresource_materialization_testspass.Not tested on Windows.
VK_EXT_conditional_renderingis available on current AMD, NVIDIA and Intel drivers; without it the existing path is used.AI use
I used Claude Code (AI agent) to measure where the GPU command thread waits, write the change and the test, and run the measurements and tests above on my machine. I reviewed the diff and the results before opening this PR.
@coderabbitai[bot] on GitHub · 2026-10-09 07:32 UTC · original · edited 2026-10-21 23:04 UTC
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
1faa17c0-1f11-4b74-93da-a11b77dc1e44📥 Commits
Reviewing files that changed from the base of the PR and between 71afd45b990eebbd39c604cd2b6baead99201789 and b34f3f7ba5b2848c38d74a30e5f9fd50856e4d5d.
📒 Files selected for processing: 9
CMakeLists.txtsrc/graphics/host_gpu/graphicContext.hsrc/graphics/host_gpu/renderer/cache/bufferCache.cppsrc/graphics/host_gpu/renderer/cache/textureCache.cppsrc/graphics/host_gpu/renderer/cache/textureCache.hsrc/graphics/host_gpu/renderer/renderCompute.cppsrc/graphics/host_gpu/renderer/renderDraw.cppsrc/graphics/presentation/window/vulkanWindow.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.
📝 Walkthrough
The change adds Vulkan conditional-rendering support and a compute shader that checks metadata slices against supported clear codes.
BufferCachetracks watched memory ranges and shader address writes.TextureCacheuses the resulting predicates to conditionally clear eligible image slices and retains the metadata readback path when GPU materialization is unavailable. Tests cover conditional-rendering support, metadata clearing, watched-range changes, and unmapping.Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Refactor
Merge Risk: ⚪ Minimal · up to
b34f3The overlapping-watch fix preserves pending metadata changes, and no remaining issue is established that should block merging after normal checks.
Pre-merge checks:
4 ·
1
Full details: Docstring Coverage
Explanation
Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 28 functions across 10 files. (2 skipped: 1 unsupported, 1 too large.)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@ -1116,0 +1170,4 @@if (const auto found = m_meta_clear_decisions.find(address);found != m_meta_clear_decisions.end() && found->second == decision &&m_buffer_cache.IsWatchedRangeUnchanged(address, decision.size)) {return true;@coderabbitai[bot] on GitHub · 2026-10-09 07:32 UTC · original · edited 2026-10-09 07:46 UTC
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 6024
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 22547
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 13111
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 12262
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 11244
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 11213
Include the native image and layer in the GPU clear decision cache.
m_meta_clear_decisionsis keyed only by the metadata address and decision parameters.FindImagecan free a cached native image and create a replacement for the same guest data.FreeImagedoes not remove the decision. A later cache hit can therefore return beforeClearImageSlicesIfruns for the replacement image. The replacement can render stale or uninitialized contents.Store the native image and first layer in the cached decision, and remove decisions when their image is deleted.
Suggested fix
@coderabbitai[bot] on GitHub · 2026-10-09 07:32 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
bfb73cd8-6b64-4b62-8ea2-f73cea015e08📥 Commits
Reviewing files that changed from the base of the PR and between
7b9997baeaand 9c6c388f1437d6961f7ba50a755839a63f32425b.📒 Files selected for processing (13)
CMakeLists.txtsrc/graphics/host_gpu/graphicContext.hsrc/graphics/host_gpu/renderer/cache/bufferCache.cppsrc/graphics/host_gpu/renderer/cache/bufferCache.hsrc/graphics/host_gpu/renderer/cache/metaClearCheck.cppsrc/graphics/host_gpu/renderer/cache/metaClearCheck.hsrc/graphics/host_gpu/renderer/cache/textureCache.cppsrc/graphics/host_gpu/renderer/cache/textureCache.hsrc/graphics/host_gpu/renderer/renderCompute.cppsrc/graphics/host_gpu/renderer/renderDraw.cppsrc/graphics/host_gpu/shaders/gpu_meta_clear_check.compsrc/graphics/presentation/window/vulkanWindow.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.
@ -1116,0 +1170,4 @@if (const auto found = m_meta_clear_decisions.find(address);found != m_meta_clear_decisions.end() && found->second == decision &&m_buffer_cache.IsWatchedRangeUnchanged(address, decision.size)) {return true;@NeySlim on GitHub · 2026-10-09 07:46 UTC · original
No change needed. The decision cache only skips a redundant check: both deciding paths expand every matched slice to 0xff (the compute check with its expand flag, the CPU path with WriteBacking), so a second run over unchanged bytes decodes no code and clears nothing, whatever native image is bound. The cache is dropped as soon as the bytes change (IsWatchedRangeUnchanged), which is the only case where a new decision can differ. Keying it by image would re-record a compute pass that clears nothing; the contents of a replacement image created from guest data are a separate property of FreeImage, unchanged by this PR and the same as the CPU path on main.
@ -1116,0 +1170,4 @@if (const auto found = m_meta_clear_decisions.find(address);found != m_meta_clear_decisions.end() && found->second == decision &&m_buffer_cache.IsWatchedRangeUnchanged(address, decision.size)) {return true;@coderabbitai[bot] on GitHub · 2026-10-09 07:47 UTC · original
@NeySlim, you’re right. Thanks for the clarification.Both paths expand matched metadata slices to
0xff. With unchanged bytes and decode parameters, another check would clear nothing, regardless of the native image. Adding the image and first layer to the cache would not fix replacement image contents.I withdraw this finding. No change is needed for this comment.
✏️ Learnings added
✅ Review thread resolved.
You are interacting with an AI system.
@coderabbitai[bot] on GitHub · 2026-10-09 09:43 UTC · original · reviewed
ℹ️ Review info
⚙️ Run configuration
6908937e-2dcc-4207-8da4-e41ad8cc8142📥 Commits
Reviewing files that changed from the base of the PR and between 9c6c388f1437d6961f7ba50a755839a63f32425b and e08765630b23d3585eea568a28f83dfe67324203.
📒 Files selected for processing (4)
src/graphics/host_gpu/renderer/cache/bufferCache.cppsrc/graphics/host_gpu/renderer/cache/bufferCache.hsrc/graphics/host_gpu/renderer/cache/textureCache.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
@NeySlim on GitHub · 2026-10-09 10:01 UTC · original
@coderabbitai Re the outside-diff comment on
bufferCache.cpp(overlapping watches): confirmed, a newWatchRangecleared the shared change set in the overlap and could hide a write recorded for another watch. Fixed in 0a9a1d37: each watch now keeps its own changed flag (address writes are tracked by a counter each watch compares with), and watching a range only resets that watch.RenderExecutorColorMetadataClearchecks two overlapping watches; it fails when a new watch clears the changes of the ranges it overlaps.@coderabbitai[bot] on GitHub · 2026-10-09 10:05 UTC · original
🧩 Analysis chain
🏁 Script executed:
Length of output: 8204
@NeySlimThanks for the fix and explanation. I verified thatWatchRangeresets only the watch at the specified address. Each watch keeps its ownchangedflag and compares its address-write counter with the global counter.The regression test checks that creating an overlapping watch leaves the first watch changed and the new watch unchanged. This addresses the reported issue. I inspected the code but did not run the test.
✏️ Learnings added
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
You are interacting with an AI system.
@NeySlim on GitHub · 2026-10-11 08:39 UTC · original
Rebased on main after the depth to color copy (
1029511b). Conflicts in graphicContext.h and vulkanWindow.cpp: both sides kept (VK_KHR_maintenance8andVK_EXT_conditional_renderingare both requested and chained into the device create info).Linux, Radeon 8060S (RADV): GPU suite one case per process and ctest match a clean main build; RenderExecutorColorMetadataClear still fails when the GPU decision is disabled.
@NeySlim on GitHub · 2026-10-19 14:02 UTC · original
Re-measured on main
e5f0ee7fcin God of War Ragnarök (new game, no tessellation, #1165 on both sides since main alone stops on a short file read): the proven fills from #1628 never cover the game's color metadata, so main still reads it back 5 times per frame and the command thread blocks 16.5 ms per frame on these readbacks in the opening cutscene and 15.3 ms in the sled section, while this PR leaves no readback. The frame rate gain stays small over 5 runs per side: 8.60 to 8.64 fps in the cutscene (noise) and 7.31 to 7.45 fps in the sled section (+2%), because the command thread waits elsewhere instead (35 ms per frame off CPU on both sides). The "repeated Goat fill stays CPU-owned" test from main is now "repeated Goat fill avoids readback": with this PR the Goat decision is taken on the GPU when conditional rendering is available, so the metadata stays GPU-written and is never read back.@Nmzik on GitHub · 2026-10-22 14:16 UTC · original
b54057942dPull request closed