gpu: release stale clean buffers without memory pressure #1934
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!1934
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1934"
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-27 06:33 UTC · original
MrBigBritches:stale-buffer-release→main· +530 −5 · 9 files · 5 commits · openA cached buffer keeps its guest pages write-protected. Once a game reuses that memory for other data, every CPU write there faults and is uploaded again into a buffer nothing reads. In Octopath Traveler II, about a third of all write faults landed in buffers no binding had used for over 300 frames.
This releases clean buffers that no binding has used for a while, even without memory pressure, and adds a safety net for buffers that shaders read only through BDA.
It is the third part of #1809, redesigned. #1809 released any buffer unbound for 300 ticks and pinned BDA-read buffers permanently. Here, ranges are protected only when a shader is seen reading one again, and protection is cleared on unmap. It is independent of #1932 and #1933.
Changes
BufferReleasesandBufferReleaseRefaults, shown in the performance overlay's GPU memory section. A refault turns the row yellow.FindBuffer, which deliberately doesn't count as a use.BufferCache::FindBufferForShaderReadfinds or creates the buffer and touches it.FindBufferis unchanged.Why dropping a buffer without a download is safe
Shaders with FLAT/GLOBAL stores to guest memory are rejected at compile time (
ShaderInfoCollection, "writable FLAT/GLOBAL addresses require GPU ownership tracking"), and there is no store-through-BDA emitter. Address atomics and scalar stores aren't decoded. So every shader write to guest memory goes through a bound buffer, which is tracked as GPU-written. The memory-pressure collector already relies on the same thing.MemoryTrackeralso stops the process ("cannot untrack GPU-dirty memory") if a GPU-written range is ever released.Measurements
Octopath Traveler II, the same route on
mainand on an experiment build with the same release rule. The figures come from a temporary per-second log, which is not part of this PR.main→ releasemain→ releaseTesting
shader_recompiler_compute_tests:BufferCacheShaderReadLookupFindBufferlookup still doesn't.BufferCacheStaleReleaseBufferCacheReleaseRefaultBufferCacheDirtyGarbageCollectionpasses unchanged.main:vdecswdoesn't build, becausestd::aligned_allocisn't available in MSVC's C runtime.kernel_file_systemcrashes with0xc0000028.main, and #1922 fixes them.Behaviour change
A clean buffer that no binding uses no longer stays cached forever without memory pressure.
BufferCacheStaleReleasemakes that explicit.Known limits
@coderabbitai[bot] on GitHub · 2026-10-27 06:40 UTC · original
Walkthrough
BufferCache now tracks shader-read use and releases eligible stale buffers during garbage collection. It records released ranges and protects them when a later fault overlaps them. Unmapping clears release history for the affected range. The change also adds release and refault counters, a performance overlay row, and tests for use tracking, release eligibility, release limits, refault handling, and unmapping.
Suggested reviewers:
shadowbeat070,nmzikPriority:
Normal
Change: Feature
Merge Risk:
Low · up to
1ca8ePartial unmapping can permit repeated zero reads on a narrow shader-read path. Preserve the remaining release history and strengthen the remap test; these bounded issues warrant follow-up before or alongside merge.
Pre-merge checks:
4 ·
1
Full details: Title check
Explanation
The title names the primary change: periodic release of stale clean GPU buffers that no binding has used, even without memory pressure. It matches the raw summary (ReleaseStaleBuffers in the garbage collector, with the release and refault counters) and the objectives, which describe the same release rule. It does not mention the refault safety net or the BDA mesh index-buffer use, but those are supporting parts of the same change.
Full details: Description check
Explanation
The description states the problem (write faults into unused buffers) and the fix (releasing clean buffers unused for a period, plus a BDA refault safety net). It lists the changes, the rationale for dropping buffers without download, measurements, tests, behaviour change, and known limits. These match the raw summary and the objectives.
Full details: Docstring Coverage
Explanation
Docstring coverage is 15.38% 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 13 functions across 8 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.@ -665,0 +717,4 @@m_release_protected.Subtract(begin, end - begin);std::erase_if(m_recent_releases, [&](const ReleasedRange& range) {return range.begin < end && range.end > begin;});@coderabbitai[bot] on GitHub · 2026-10-27 06:40 UTC · original · edited 2026-10-27 06:45 UTC
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 6294
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 29823
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 12285
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 5797
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 9471
Retain surviving release ranges after a partial unmap.
When an unmap overlaps only part of a released multi-page buffer, the current predicate removes the complete release record. A later BDA fault in the still-mapped portion then has no record to restore protection. The recreated buffer may be released again and return zero data on a later BDA read.
Split each overlapping
ReleasedRangearound the aligned unmapped range.Suggested fix
Committable suggestion
@ -4208,0 +4570,4 @@"an unmap kept the protection of its range");age_out();Require(name, "released after remap", !registered(refault_offset, page),"a remapped range stayed protected from stale release");@coderabbitai[bot] on GitHub · 2026-10-27 06:40 UTC · original · edited 2026-10-27 06:45 UTC
The "released after remap" check can pass without testing stale release.
Step (j) unmaps and remaps
merged_sizebytes. Nothing then creates a buffer on that range. Whenage_out()runs, there is no buffer to release.!registered(refault_offset, page)is true whether or not the protection was cleared.If a regression kept the protection after unmap, this check would still pass. The earlier
IsReleaseProtectedcheck covers only the protection record. It does not cover the release behavior that the comment on step (j) claims.Create a buffer on the remapped range before
age_out(). Check that the buffer exists first, so the later check cannot pass vacuously.🧪 Proposed fix
Committable suggestion
@coderabbitai[bot] on GitHub · 2026-10-27 06:40 UTC · original · reviewed
Actionable comments posted: 2
f9698afe-dca8-42e1-917e-a7f382530532Reviewing files that changed from the base of the PR and between
fd6b28c4b2and1ca8e2ce00.src/common/debugCounters.hsrc/graphics/host_gpu/renderer/cache/bufferCache.cppsrc/graphics/host_gpu/renderer/cache/bufferCache.hsrc/graphics/host_gpu/renderer/cache/faultManager.cppsrc/graphics/host_gpu/renderer/renderContext.cppsrc/graphics/host_gpu/renderer/renderDraw.cppsrc/graphics/presentation/performanceOverlay.cppsrc/graphics/shader/recompiler/ir/passes/ShaderInfoCollection.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.
@ -665,0 +717,4 @@m_release_protected.Subtract(begin, end - begin);std::erase_if(m_recent_releases, [&](const ReleasedRange& range) {return range.begin < end && range.end > begin;});@MrBigBritches on GitHub · 2026-10-27 06:45 UTC · original
Correct in principle, but the impact is bounded. Protection is already split by Subtract; the only loss is the history record of an unrefaulted range. The worst case is one extra refault cycle before the range is protected, the same documented outcome as a record ageing out of the FIFO. The partial-unmap-then-BDA-only-read pattern is rare, and the suggested in-place splitting is O(N²) on the deque and breaks the capacity cap.
@ -4208,0 +4570,4 @@"an unmap kept the protection of its range");age_out();Require(name, "released after remap", !registered(refault_offset, page),"a remapped range stayed protected from stale release");@MrBigBritches on GitHub · 2026-10-27 06:45 UTC · original
The buffer is not removed by unmap. BufferCache::InvalidateMemory only invalidates tracker state (MemoryTracker::InvalidateRegion) and queues a BDA sync; buffers are only deleted by JoinOverlap, stale release and the pressure GC. So the merged buffer from (i) is still registered when age_out() runs, and the release check is not vacuous.
@ -665,0 +717,4 @@m_release_protected.Subtract(begin, end - begin);std::erase_if(m_recent_releases, [&](const ReleasedRange& range) {return range.begin < end && range.end > begin;});@coderabbitai[bot] on GitHub · 2026-10-27 06:46 UTC · original
@MrBigBritches Agreed.
Subtractpreserves protection outside the unmapped range. The loss affects only the recent-release history, which the bounded FIFO can also discard.My suggested split was not a suitable fix. Repeated middle erases and inserts can make the deque traversal O(N²), and the suggestion does not enforce
RECENT_RELEASE_CAPACITY.I withdraw the requested change under this best-effort history design. One qualification: protection after an extra refault cycle depends on the new release record surviving until the next fault. The code does not guarantee that bound if further unmaps or FIFO eviction remove the record.
✅ Review thread resolved.
You are interacting with an AI system.
@ -4208,0 +4570,4 @@"an unmap kept the protection of its range");age_out();Require(name, "released after remap", !registered(refault_offset, page),"a remapped range stayed protected from stale release");@coderabbitai[bot] on GitHub · 2026-10-27 06:46 UTC · original
🧩 Analysis chain
🏁 Script executed:
Length of output: 12556
@MrBigBritchesYou are correct. I checkedRenderContext::UnmapMemoryandBufferCache::InvalidateMemory. They invalidate tracker state and clear release protection, but they do not remove the cached buffer.The merged buffer from step (i) remains registered when
age_out()runs in step (j). The final check therefore tests stale release and is not vacuous.My finding was incorrect. I withdraw it; the proposed allocation is unnecessary.
✅ Review thread resolved.
You are interacting with an AI system.
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.