[Fix] (DirectVulkan): age-based eviction for the content-addressed cache family - ProgramFactory entries (shader modules/layouts), PipelineFactory graphics pipelines, compute pipelines and per-layout descriptor-set tracking now retire after ~1024 idle frame boundaries (render-pass-manager sweep precedent), render-pass eviction purges pipelines hashed on the dying handle (closes a handle-recycling stale-pipeline hazard), and the program reflection cache is lifetime-id-keyed and cleared at EGL teardown - shader/program churn no longer grows Vulkan objects without bound

This commit is contained in:
2026-07-27 22:05:25 -04:00
parent c540fb88ee
commit 34685b4bb0
13 changed files with 417 additions and 7 deletions
@@ -171,6 +171,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
m_programFactory = nullptr;
m_device = VK_NULL_HANDLE;
m_minDynamicOffsetAlignment = 1;
m_frameCounter = 0;
m_frameCount = 0;
m_maxBindings = 0;
m_setsPerFrame = 0;
@@ -211,6 +212,56 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
}
void UniformManager::OnDescriptorSetLayoutDestroyed(VkDescriptorSetLayout descriptorSetLayout) {
SizeT purgedSets = 0;
for (auto& frame : m_frames) {
const auto it = frame.descriptorSetCacheByLayout.find(descriptorSetLayout);
if (it != frame.descriptorSetCacheByLayout.end()) {
purgedSets += it->second.sets.size();
frame.descriptorSetCacheByLayout.erase(it);
}
}
if (purgedSets > 0) {
// The per-draw reuse memo folds the layout handle into its signature; drop
// it so a recycled handle value cannot revive a purged set mid-frame.
m_hasLastDescriptor = false;
MGLOG_D("UniformDescriptorBinder: purged %zu descriptor sets for destroyed layout", purgedSets);
}
}
void UniformManager::OnFrameBoundary() {
++m_frameCounter;
// Sweep cadence and retire age mirror VkRenderPassManager::OnPresent. Only the
// CPU-side tracking is reclaimed here: the sets' pool slots stay occupied until
// Shutdown destroys the pools (no FREE_DESCRIPTOR_SET_BIT, no mid-life pool
// reset), exactly as they would had the entry been kept. What the sweep buys is
// that an idle layout's tracking vectors stop accumulating, and that a
// destroyed-then-recycled layout handle finds no stale entry to hit.
constexpr Uint64 kSweepInterval = 256;
constexpr Uint64 kRetireAgeFrames = 1024;
if ((m_frameCounter % kSweepInterval) != 0) {
return;
}
SizeT purgedSets = 0;
for (auto& frame : m_frames) {
for (auto it = frame.descriptorSetCacheByLayout.begin();
it != frame.descriptorSetCacheByLayout.end();) {
if (m_frameCounter - it->second.lastUsedFrame > kRetireAgeFrames) {
purgedSets += it->second.sets.size();
it = frame.descriptorSetCacheByLayout.erase(it);
} else {
++it;
}
}
}
if (purgedSets > 0) {
m_hasLastDescriptor = false;
MGLOG_D("UniformDescriptorBinder::OnFrameBoundary: dropped %zu idle descriptor sets", purgedSets);
}
}
Bool UniformManager::ResolveSamplerDescriptor(VkCommandBuffer commandBuffer,
const MG_State::GLState::ProgramObject& program,
const ProgramFactory::VkProgramObject& programObj,
@@ -989,6 +1040,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
VkDescriptorSet& outDescriptorSet) {
auto& frame = m_frames[frameIndex];
auto& cache = frame.descriptorSetCacheByLayout[programObj.descriptorSetLayout];
// Every layout used in a frame is acquired at least once (the per-draw
// descriptor-reuse memo starts each frame invalidated and folds the layout
// into its signature), so an actively-used entry is stamped every frame.
cache.lastUsedFrame = m_frameCounter;
if (cache.cursor < cache.sets.size()) {
outDescriptorSet = cache.sets[cache.cursor++];
} else {