From f70ec20ab8aedd716e44291016c19f8729b4131f Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Thu, 14 Nov 2024 17:18:55 +0100 Subject: [PATCH] Fix: Leak of GHOST GPU contexts in Metal backend Need to respect the ownership and lifetime of objects. Pull Request: https://projects.blender.org/blender/blender/pulls/130282 --- source/blender/gpu/metal/mtl_shader.hh | 3 ++- source/blender/gpu/metal/mtl_shader.mm | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/source/blender/gpu/metal/mtl_shader.hh b/source/blender/gpu/metal/mtl_shader.hh index e5b29ba43ea..cab5eea762d 100644 --- a/source/blender/gpu/metal/mtl_shader.hh +++ b/source/blender/gpu/metal/mtl_shader.hh @@ -442,7 +442,8 @@ class MTLParallelShaderCompiler { std::mutex queue_mutex; std::deque parallel_work_queue; - void parallel_compilation_thread_func(GPUContext *blender_gpu_context); + void parallel_compilation_thread_func(GPUContext *blender_gpu_context, + GHOST_ContextHandle ghost_gpu_context); BatchHandle create_batch(size_t batch_size); void add_item_to_batch(ParallelWork *work_item, BatchHandle batch_handle); void add_parallel_item_to_queue(ParallelWork *add_parallel_item_to_queuework_item, diff --git a/source/blender/gpu/metal/mtl_shader.mm b/source/blender/gpu/metal/mtl_shader.mm index 743fbd4cb6a..485fc9061fc 100644 --- a/source/blender/gpu/metal/mtl_shader.mm +++ b/source/blender/gpu/metal/mtl_shader.mm @@ -1940,13 +1940,14 @@ void MTLParallelShaderCompiler::create_compile_threads() GPU_context_active_set(main_thread_context); /* Create a new thread */ - compile_threads.push_back(std::thread([this, per_thread_context] { - this->parallel_compilation_thread_func(per_thread_context); + compile_threads.push_back(std::thread([this, per_thread_context, ghost_gpu_context] { + this->parallel_compilation_thread_func(per_thread_context, ghost_gpu_context); })); } } -void MTLParallelShaderCompiler::parallel_compilation_thread_func(GPUContext *blender_gpu_context) +void MTLParallelShaderCompiler::parallel_compilation_thread_func( + GPUContext *blender_gpu_context, GHOST_ContextHandle ghost_gpu_context) { /* Contexts can only be created on the main thread so we have to * pass one in and make it active here */ @@ -2006,6 +2007,11 @@ void MTLParallelShaderCompiler::parallel_compilation_thread_func(GPUContext *ble } GPU_context_discard(blender_gpu_context); + + GHOST_SystemHandle ghost_system = reinterpret_cast( + GPU_backend_ghost_system_get()); + BLI_assert(ghost_system); + GHOST_DisposeGPUContext(ghost_system, ghost_gpu_context); } BatchHandle MTLParallelShaderCompiler::create_batch(size_t batch_size)