gpu: fix corrupt textures when a shorter mip chain reuses a cached texture's memory (PPSA09809) #1781
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!1781
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1781"
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?
@MrBigBritches on GitHub · 2026-10-21 03:32 UTC · original
MrBigBritches:texture-mip-layout-fix→main· +425 −10 · 3 files · 8 commits · openA texture usually comes with mip levels, smaller copies of itself (half size, quarter size and so on) that the GPU uses when the surface is far away. On the PS5 the whole chain is stored in one block of memory, smallest level first. So where the full-size level (the base level) sits depends on how many levels the texture has. A one-level texture keeps its base level at the start of the block. An eight-level texture at the same address keeps it after the seven smaller levels.
Kyty caches each texture as a host image. When the game asked for a texture at an address the cache already had, Kyty only compared mip layouts when both had the same level count. A request with a different level count reused the cached image, so its base level was read from the wrong offset.
Octopath Traveler II streams one-level ground textures into memory that an eight-level texture used before. A few minutes into a session, the paving outside the mansion showed window-shaped bright rectangles and dark patches. Half of each texture was right and the other half was whatever followed it in memory, and it changed from run to run.
Example - Ground textures
Changes
Compare mip placement whenever level counts differ
ResolveOverlapnow compares the levels both layouts share whenever the level counts differ. Any difference makes the request a separate guest layout with its own image, as an equal-count mismatch already did.FindImage's equal-backing fast path runs the same check. It used to accept a cached image with more levels and the same allocation size (common with padded tiled layouts) beforeResolveOverlapran.Retire the replaced image safely
When a request becomes a separate image, the cached image it replaces still needs handling. This applies to every same-address mismatch. That includes a different level count, a different tile mode, or a different mip layout with the same level count.
Known limitations
mip.sizecounts every layer, so if both counts differ the shared levels never match and the request gets its own image even when the per-layer placement is identical. This costs memory, not correctness. A temporary log found no such request in Octopath Traveler II, Astro Bot, LEGO Batman or Ratchet & Clank: Rift Apart.r128) descriptors size the chain fromLAST_LEVEL, because they have noMAX_MIP. A view with a shorterLAST_LEVELthan the real chain would now get its own image. A temporary log found nor128textures at all in the same four games.Testing
New
UnifiedTextureCacheFlowcases inShaderRecompilerComputeTests:Each new case was checked against a deliberately broken cache and fails without the code it covers. The full test suite passes except
kernel_file_system, which fails the same way onmain.In-game, the Octopath Traveler II ground corruption is gone.
Partially fixes https://git.gtahub.st/github/KytyPS5--KytyPS5/issues/1617
@coderabbitai[bot] on GitHub · 2026-10-21 03:38 UTC · original · edited 2026-10-27 22:24 UTC
Walkthrough
The texture cache compares shared mip placement when it resolves overlaps or reuses images with different mip counts. When an idle cached image has an incompatible tile mode or mip layout,
RetireReplacedLayoutconditionally publishes GPU-written contents, marks bindings for rediscovery, and frees the image. New tests cover linear and tiled mip chains, content preservation during retirement, and render-target base offsets when mip reuse is enabled.Suggested reviewers:
nmzikPriority:
Normal
Change: Bug fix · Severity of issue fixed: Low
Merge Risk:
Moderate · up to
66675Retiring an image with an incompatible layout can overwrite newer partial GPU buffer writes with older image contents. That could cause stale or corrupt data in uncommon alias scenarios. The mip-layout reuse fix itself looks sound, but this data-ordering gap should be resolved or explicitly accepted before merge.
Pre-merge checks:
4 ·
1
#1617, is closed and completed. It supplies historical context only, so no active directly linked targets remain and no linked-issue coding requirements apply. The text…Full details: Title check
Explanation
The title names the main change: a shorter mip chain reusing a cached texture's memory caused corrupt textures, and the fix is in the texture cache. It matches the changeset, which compares mip placement in ResolveOverlap and FindImage and retires replaced images. The PPSA09809 game ID is extra detail but does not make the title inaccurate.
Full details: Description check
Explanation
The description is detailed and on-topic. It explains the mip-level placement problem in the texture cache, lists the changes to ResolveOverlap, FindImage and the image retirement path, and describes the new UnifiedTextureCacheFlow tests. It also states known limitations.
Full details: Linked Issues check
Explanation
The only directly linked issue,
#1617, is closed and completed. It supplies historical context only, so no active directly linked targets remain and no linked-issue coding requirements apply. The texture cache changes in textureCache.cpp (whole-PR diff from merge base0685bffbto head66675fdb) address the mip-placement problem described in the current PR description. This is not a linked-issue requirement check.Full details: Out of Scope Changes check
Explanation
The whole-PR diff of textureCache.cpp is limited to texture cache behavior. It adds SameMipPlacement and SharedLevelsMatch helpers. It uses them in ResolveOverlap when level counts differ, and in the FindImage equal-backing fast path. It adds RetireReplacedLayout, which publishes or frees a replaced same-address image. It also adds a depth-overlap guard that returns the cached image only when shared levels match. textureCache.h adds one matching private declaration. The summary attributes the UnifiedTextureCacheFlow test additions in ShaderRecompilerComputeTests.cpp to these same paths. Each change connects directly to the mip-layout reuse defect in the PR description. No unrelated change is evidenced. The test file was assessed from the summary only, not by a line-level read.
Full details: Docstring Coverage
Explanation
Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 7 functions across 2 files. (1 skipped: 1 too large.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
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.@ -820,2 +870,2 @@left.surface_z == right.surface_z;}))) {cached.info.mip_layout.begin(), SameMipPlacement)) ||shared_levels_differ) {@coderabbitai[bot] on GitHub · 2026-10-21 03:38 UTC · original · edited 2026-10-21 05:54 UTC
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
Preserve GPU-owned contents when rejecting an aged image.
If this new mismatch rejects a cached image more than 32 ticks after its last access, the existing
safe_to_deletebranch callsFreeImage. For a GPU-modified image,FreeImageclears the modification and deletes the image without downloading its contents. Retain or synchronize that image before retirement so a later read does not lose the GPU write.✅ Addressed in commit 710da85
@coderabbitai[bot] on GitHub · 2026-10-21 03:38 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
f313f0b0-caae-4abf-b2a5-c34a5e08ff48📥 Commits
Reviewing files that changed from the base of the PR and between
7b7f9a70c6and 29519f0b47a0c63af492b3d8612a728dc3eb75d8.📒 Files selected for processing: 2
src/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; 7 remain after this review.
@MrBigBritches on GitHub · 2026-10-21 05:54 UTC · original
Both addressed in 710da851. FindImage’s SameBacking fast path now also requires the shared mip levels to be placed identically when the level counts differ (the regression test covers the equal-size case). A cached image rejected for a layout mismatch is only freed when it isn’t GPU-modified; GPU-written images are left for texture GC, which downloads before freeing.
@coderabbitai[bot] on GitHub · 2026-10-21 06:00 UTC · original · edited 2026-10-21 06:26 UTC
On
src/graphics/host_gpu/renderer/cache/textureCache.cppline 819 (outdated):🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 6639
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 8099
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 15054
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 8975
Compare shared mip placement when layer counts differ.
ResolveOverlapskipsSharedLevelsMatchwhen mip counts differ and layer counts differ.SameBackingallows a cached image with equal-or-greater layer and mip counts, and the later fallback can return that image when the request is smaller. The old code also compared full mip layouts only when all resource counts matched.Remove the layer-count condition:
Suggested fix
✅ Addressed in commit 3a48875
@coderabbitai[bot] on GitHub · 2026-10-21 06:00 UTC · original · edited 2026-10-21 06:29 UTC
On
src/graphics/host_gpu/renderer/cache/textureCache.cppline 827 (outdated):🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 2397
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 37167
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 37167
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 14569
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 9247
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 16130
Keep the retained older image out of range-based synchronization.
When
ResolveOverlaprejects a layout mismatch, the branch keeps a GPU-modified cached image registered. A later GPU write can mark the new equal-range image GPU-modified as well.FindImageFromRangecan then select the older image first andBufferCache::SynchronizeBufferFromImagecan download stale contents.Keep the older image available for safe readback, but make range lookup select the newer safe equal-range image after it becomes GPU-written. Do not delete the older GPU-modified image before its contents are preserved.
✅ Addressed in commit 3a48875
@coderabbitai[bot] on GitHub · 2026-10-21 06:00 UTC · original · reviewed
Actionable comments posted: 2
ℹ️ Review info
⚙️ Run configuration
b163a784-328b-451a-b75a-54aefc76eb5a📥 Commits
Reviewing files that changed from the base of the PR and between 29519f0b47a0c63af492b3d8612a728dc3eb75d8 and 710da85162bfd595719a24e26a9aada59d8563b1.
📒 Files selected for processing: 2
src/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; 7 remain after this review.
@MrBigBritches on GitHub · 2026-10-21 06:17 UTC · original
On
src/graphics/host_gpu/renderer/cache/textureCache.cppline 819 (outdated):Layer counts: Agreed. mip_layout describes placement within a single layer, so the layer count doesn’t affect whether shared levels line up. I dropped the layer-count condition in 3a48875b.
@MrBigBritches on GitHub · 2026-10-21 06:17 UTC · original
On
src/graphics/host_gpu/renderer/cache/textureCache.cppline 827 (outdated):Retained image: Good catch. Keeping the GPU-modified image avoided losing its writes but left a second image over the same range. In 3a48875b the idle cached image is retired the same way as a BC alias conflict (BcAlias::Resolve): it is published to the buffer cache first, the new image uploads from there, and then it’s freed. A new test covers this: an idle GPU-cleared chain replaced by a one-level texture is retired, FindImageFromRange returns the new image, and that image reads the GPU-written data. Images used in the last 32 frames still coexist with the new one, as they did before this PR. Retiring those every time would churn when a title alternates between views.
@coderabbitai[bot] on GitHub · 2026-10-21 06:26 UTC · original
On
src/graphics/host_gpu/renderer/cache/textureCache.cppline 819 (outdated):@MrBigBritches Thanks for the clarification and fix. The supplied
ResolveOverlapcode now checks shared mip placement when mip counts differ, regardless of layer counts. This addresses the finding.✏️ Learnings added
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@coderabbitai[bot] on GitHub · 2026-10-21 06:29 UTC · original
On
src/graphics/host_gpu/renderer/cache/textureCache.cppline 827 (outdated):@MrBigBritches Thanks for the fix and clarification. The supplied code now sends idle layout conflicts through
BcAlias::Resolve, which publishes safe GPU-written contents before freeing the cached image. Your regression test covers retirement, replacement lookup, and data preservation.Keeping recently used images avoids churn when a title alternates between views. That behavior is unchanged by this fix.
✏️ Learnings added
🧠 Learnings used
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@coderabbitai[bot] on GitHub · 2026-10-27 21:30 UTC · original · edited 2026-10-27 21:57 UTC
On
src/graphics/host_gpu/renderer/cache/textureCache.cppline 861 (outdated):Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 13422
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 6892
Check shared mip placement before depth-image reuse.
ResolveOverlapcallsResolveDepthOverlapbefore checkingshared_levels_differ. A multi-mip depth request with fewer levels can therefore reuse a cached depth image even when shared mip placement differs, which can expose the wrong mip offsets.Suggested fix
✅ Addressed in commit
66675fd@coderabbitai[bot] on GitHub · 2026-10-27 21:30 UTC · original · reviewed
Actionable comments posted: 1
00291bc1-333a-4a91-9b15-ba256368bc8fReviewing files that changed from the base of the PR and between 660dd70efaaa4f9486a186ef8b5e998f69af28db and
2ae9be679e.src/graphics/host_gpu/renderer/cache/textureCache.cppsrc/graphics/host_gpu/renderer/cache/textureCache.htests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
@MrBigBritches on GitHub · 2026-10-27 21:55 UTC · original
On
src/graphics/host_gpu/renderer/cache/textureCache.cppline 861 (outdated):Fixed in
66675fdb. The depth path no longer hands back the cached image unchanged when the shared mip placement differs, so the request gets its own image as color requests do. Its convert-and-copy result is kept, because existing layered depth/color aliases rely on it. That copy maps levels by index, which predates this PR. I'm tracking it as a follow-up along with the ResolveOverlap fallback that frees an idle image without publishing it.@coderabbitai[bot] on GitHub · 2026-10-27 21:57 UTC · original
On
src/graphics/host_gpu/renderer/cache/textureCache.cppline 861 (outdated):@MrBigBritches The supplied code confirms the fix. When shared mip placement differs,
ResolveOverlaprejects an unchangedcached_idfromResolveDepthOverlap. It still accepts a converted replacement image. This addresses the reuse concern without removing the existing conversion path.The level-index copy behavior and the idle-image fallback remain separate follow-ups. Would you like me to open a GitHub issue for those two items?
🧠 Learnings used
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@MrBigBritches on GitHub · 2026-10-27 22:19 UTC · original
@coderabbitai resume
@coderabbitai[bot] on GitHub · 2026-10-27 22:19 UTC · original · edited 2026-10-27 22:24 UTC
✅ Action performed
Reviews resumed and review finished.
@coderabbitai[bot] on GitHub · 2026-10-27 22:24 UTC · original · reviewed
5e9f61e0-bd50-4677-8db7-05f6394b5b62Reviewing files that changed from the base of the PR and between
2ae9be679eand66675fdb41.src/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.
@MrBigBritches on GitHub · 2026-10-27 22:45 UTC · original
The cause predates this PR: InvalidateMemoryFromGPU only flags images whose base address equals the write address, so a GPU buffer write that starts inside an image leaves it GPU-modified. BcAlias::Resolve on main already publishes through the same path with the same exposure. Before this PR the retired image's GPU contents were always dropped, so this change trades that loss for the narrower partial-write case. Fixing partial-overlap invalidation changes behaviour for every title, so I'm tracking it as a separate follow-up rather than widening this PR.
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.