Fix #137382: Vulkan: Memory leak when rendering

Each time when rendering an image a new context is created. When the
context is destroyed it can still contain a render graph, which ownership
isn't transferred back to the device. Resulting in an increase of several
MB per render. The render graph is cleanly destroyed during quiting as
there is a master list of created render graphs.

Fixed by moving the ownership of the render graph back to the device
when a context is unregistered.

Pull Request: https://projects.blender.org/blender/blender/pulls/138095
This commit is contained in:
Jeroen Bakker
2025-04-29 09:39:45 +02:00
parent 5ff7ffe31b
commit f6a8993570
2 changed files with 11 additions and 0 deletions

View File

@@ -27,6 +27,7 @@ class VKBatch;
class VKStateManager;
class VKShader;
class VKThreadData;
class VKDevice;
enum RenderGraphFlushFlags {
NONE = 0,
@@ -37,6 +38,8 @@ enum RenderGraphFlushFlags {
ENUM_OPERATORS(RenderGraphFlushFlags, RenderGraphFlushFlags::WAIT_FOR_COMPLETION);
class VKContext : public Context, NonCopyable {
friend class VKDevice;
private:
VkExtent2D vk_extent_ = {};
VkSurfaceFormatKHR swap_chain_format_ = {};

View File

@@ -464,6 +464,14 @@ void VKDevice::context_register(VKContext &context)
void VKDevice::context_unregister(VKContext &context)
{
if (context.render_graph_.has_value()) {
render_graph::VKRenderGraph &render_graph = context.render_graph();
context.render_graph_.reset();
BLI_assert_msg(render_graph.is_empty(),
"Unregistering a context that still has an unsubmitted render graph.");
render_graph.reset();
BLI_thread_queue_push(unused_render_graphs_, &render_graph);
}
orphaned_data.move_data(context.discard_pool, timeline_value_ + 1);
contexts_.remove(contexts_.first_index_of(std::reference_wrapper(context)));
}