graphics: evict and retry when an image cannot be allocated #1271
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!1271
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1271/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?
@msfvn on GitHub · 2026-10-09 02:25 UTC · original
msfvn:evict-on-image-oom→main· +72 −9 · 4 files · 2 commits · closed without mergingProblem
When device memory is full,
Image's constructor exits withfailed to create image. The texture cache collector runs between submissions and only frees images older than a number of GC ticks, so when the working set grows quickly (scene transitions) it cannot free memory in time. On 4-6 GB GPUs this ends the game:main(with the community non-RT patch) withfailed to create image.Change
Imagerecords the allocation failure (allocation_failed) instead of exiting.TextureCache::InsertImagethen callsEvictForAllocation()and retries, up to three times:ResolveDepthOverlap,ExpandImageandAssociateStencilpass theirs askeep);CommandScheduler::Finish()so their deferred destruction runs and the memory is returned before the retry. The caller holdsm_lock; none of the scheduler's deferred operations take it.Testing
Linux, RTX 3050 Laptop 4 GB, 7.5 GB RAM, Astro Bot with the community non-RT patch, scripted input through the intro, 420 s runs on current
main:main: exits at 227 s withfailed to create image: extent=2048x2048x1 format=146.main+ this change (together with #1269, #1270 and the other open memory changes): runs the full 420 s with correct rendering. While the ship-interior scene does not fit in VRAM, images are evicted and recreated and the frame rate drops to about 1 fps; slow, but no longer fatal.AI involvement: written with Claude Code (AI assistant) at my request and reviewed by a C++ developer I know; tested on my machine (RTX 3050 Laptop 4 GB, CachyOS) as described above, including playing Astro Bot on this build. Happy to adjust anything you point out.
@coderabbitai[bot] on GitHub · 2026-10-09 02:27 UTC · original · edited 2026-10-11 02:07 UTC
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
4adb503c-93e4-4f58-8c3a-da9561f4f658📥 Commits
Reviewing files that changed from the base of the PR and between 6715e56ac0c3f5b9fa280f396217e74f92a0fc05 and
e21bdcc3d2.📒 Files selected for processing (2)
src/graphics/host_gpu/renderer/cache/textureCache.cppsrc/graphics/host_gpu/renderer/cache/textureCache.hIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.
📝 Walkthrough
📝 Walkthrough
Walkthrough
Imagerecords failed image allocation instead of terminating.TextureCache::InsertImageremoves failed entries and retries allocation after attempting to evict eligible cached images. The cache preserves specified images during eviction. Eviction requires an active scheduler and at least two GC ticks. It applies limits to selected image memory and GPU-modified image downloads.Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Bug fix
Merge Risk: 🟡 Moderate · up to
e21bdUnder image-memory pressure, an overlapping-image operation can use an image after eviction. Protect the merged candidate during retry before merging.
Pre-merge checks |
4 |
1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
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.@ -1925,0 +1979,4 @@return false;}m_scheduler.Finish();return true;@coderabbitai[bot] on GitHub · 2026-10-09 02:27 UTC · original · edited 2026-10-11 02:05 UTC
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Eviction can free images that the code calling
InsertImagestill uses.EvictForAllocationruns insideInsertImage. Several callers keep a live source image around that call:ResolveDepthOverlap(Line 756) keepscached/cached_id. After the insert, it readscachedand passescached_idtoCopyImage.ExpandImage(Line 908) keepssource_id. After the insert, it usessource_idwithCopyImageandCopyImageMip.FindImage(Line 1326) keeps nothing alive, so it is not affected.The victim filter accepts any registered image whose LRU tick is below
m_gc_tick - 2.RefreshCopySourceandRefreshImagedo not callTouchImage, so an old copy source stays in the victim range.Here is the failure sequence when a victim is freed:
FreeImageclearsGpuModified.DeleteImagedefersm_slot_images.erase.m_scheduler.Finish()runs that deferred erase right away.cachedorsource_id.The result is a use-after-free. The caller also copies from a source image whose native contents were destroyed. If the download was skipped, the GPU-written data is lost.
Exclude the in-flight source images from eviction. One fix: touch the source with
TouchImagebefore callingInsertImageinResolveDepthOverlapandExpandImage. A more robust fix: pass a set of protectedImageIds intoEvictForAllocationand skip them in theForEachItemBelowcallback.Proposed minimal fix
In
ResolveDepthOverlap, addTouchImage(cached);beforeInsertImage(info). Also take thecachedreference again after the call.✅ Addressed in commit 6715e56
@coderabbitai[bot] on GitHub · 2026-10-09 02:27 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
ed96da72-46b9-42ec-804e-30c098144c01📥 Commits
Reviewing files that changed from the base of the PR and between
7b9997baeaand c60ebe1ec71c855e2d439a40c7e2aaa5719cf038.📒 Files selected for processing (4)
src/graphics/host_gpu/renderer/cache/textureCache.cppsrc/graphics/host_gpu/renderer/cache/textureCache.hsrc/graphics/host_gpu/renderer/image/image.cppsrc/graphics/host_gpu/renderer/image/image.hIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.
@ -1925,0 +1950,4 @@m_lru_cache.ForEachItemBelow(m_gc_tick - 2, [&](ImageId id) {const auto* image = m_slot_images.try_get(id);if (image == nullptr || !image->registered || image->depth_id || id == keep ||image->binding.is_bound || image->binding.is_target) {@coderabbitai[bot] on GitHub · 2026-10-11 01:19 UTC · original
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
Protect the merged image during allocation retry.
When
FindImageprocesses multiple overlapping candidates,ResolveOverlapcan receivem_slot_images[result].infoasrequested. If that call reachesExpandImage,InsertImageprotectssource_idbut notresult. An allocation failure can therefore evictresult;Finish()completes its deferred erasure before the retry readsrequestedagain. This leaves the retry with a danglingImageInforeference. Pass all live candidate IDs to eviction, includingresult, rather than excluding onlykeep.@coderabbitai[bot] on GitHub · 2026-10-11 01:19 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
fb5e3547-8485-4523-9560-559c3baeaa5e📥 Commits
Reviewing files that changed from the base of the PR and between c60ebe1ec71c855e2d439a40c7e2aaa5719cf038 and 6715e56ac0c3f5b9fa280f396217e74f92a0fc05.
📒 Files selected for processing (2)
src/graphics/host_gpu/renderer/cache/textureCache.cppsrc/graphics/host_gpu/renderer/cache/textureCache.hIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.
@ -1925,0 +1979,4 @@return false;}m_scheduler.Finish();return true;@msfvn on GitHub · 2026-10-11 02:00 UTC · original
Good catch, fixed in
e21bdcc3.InsertImagenow takes the image the caller keeps using (ResolveDepthOverlappassescached_id,ExpandImagepassessource_id, andAssociateStencilpassesdepth_id, which had the same issue), andEvictForAllocationskips it. It also skips images bound or targeted by the draw being prepared (binding.is_bound/is_target, reset per draw inResetBindings). I also rebased onto currentmainto resolve the conflict with5559cfcb.@ -1925,0 +1979,4 @@return false;}m_scheduler.Finish();return true;@coderabbitai[bot] on GitHub · 2026-10-11 02:05 UTC · original
@msfvnThanks for the fix and the clarification. The supplied code protectscached_id,source_id, anddepth_idduring allocation recovery. The victim filter also excludes bound and targeted images. This addresses the reported use-after-free.✏️ 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.
@Nmzik on GitHub · 2026-10-21 18:47 UTC · original
This is a hack and uses RAM as fallback (performance will be low)
Pull request closed