Skip to content

Commit

Permalink
Fix resources getting deleted prematurely in vkutil::ResourcePool
Browse files Browse the repository at this point in the history
ResourcePool wasn't waiting for resources to expire before deleting them. Instead of deleting them on the spot, let them sit in the pool until they expire. Also add a cleanExcessExpiredResources() method that the renderContext can call every frame at the same time it's deleting other expired Vulkan resources.

Diffs=
7a9e69314d Fix resources getting deleted prematurely in vkutil::ResourcePool (#8411)

Co-authored-by: Chris Dalton <99840794+csmartdalton@users.noreply.github.com>
  • Loading branch information
csmartdalton and csmartdalton committed Oct 28, 2024
1 parent 195de20 commit 24ccc23
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5cde9010629994ed13d6b05a1bead5f89f3182c7
7a9e69314dcad3069d04f866b5dece40873023e0
27 changes: 17 additions & 10 deletions renderer/include/rive/renderer/vulkan/vkutil_resource_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class ResourcePool : public RefCnt<ResourcePool<T>>
resource = ref_rcp(m_releasedResources.front().resource.release());
m_releasedResources.pop_front();
resource->reset();
cleanExcessExpiredResources();
}
else
{
Expand All @@ -89,18 +90,24 @@ class ResourcePool : public RefCnt<ResourcePool<T>>
assert(mutableResource->debugging_refcnt() == 0);
assert(mutableResource->m_pool.get() == this);

if (m_releasedResources.size() < MaxResourcesInPool)
// Recycle the resource!
m_releasedResources.emplace_back(
mutableResource,
m_factory.vulkanContext()->currentFrameIdx());
// Do this last in case it deletes our "this".
mutableResource->m_pool = nullptr;

cleanExcessExpiredResources();
}

void cleanExcessExpiredResources()
{
while (m_releasedResources.size() > MaxResourcesInPool &&
m_factory.vulkanContext()->currentFrameIdx() >=
m_releasedResources.front().expirationFrameIdx)
{
// Recycle the resource!
m_releasedResources.emplace_back(
mutableResource,
m_factory.vulkanContext()->currentFrameIdx());
// Do this last in case it deletes our "this".
mutableResource->m_pool = nullptr;
return;
m_releasedResources.pop_front();
}

delete resource;
}

protected:
Expand Down
3 changes: 3 additions & 0 deletions renderer/src/vulkan/render_context_vulkan_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2137,6 +2137,9 @@ void RenderContextVulkanImpl::prepareToMapBuffers()
// buffers.
m_vk->onNewFrameBegun();

// Clean expired resources in our pool of descriptor set pools.
m_descriptorSetPoolPool->cleanExcessExpiredResources();

// Synchronize buffer sizes in the buffer rings.
m_flushUniformBufferRing.synchronizeSizeAt(m_bufferRingIdx);
m_imageDrawUniformBufferRing.synchronizeSizeAt(m_bufferRingIdx);
Expand Down

0 comments on commit 24ccc23

Please sign in to comment.