From 3fcd7ccbc00a8db3c828e0d6664242acefdf3536 Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Fri, 1 Mar 2024 14:38:09 +0100 Subject: [PATCH] Compositor: Enable lock-free GPU context activation on macOS This required to apply a small fix in the Metal texture uploader. Without synchronization pixels of a wrong pass can be uploaded to a wrong texture. This is because this code path is heavily reusing temporary allocations, and at some point the allocation is not considered as still in use, unless the command buffer used by the texture uploader is submitted. Ref #118919 Pull Request: https://projects.blender.org/blender/blender/pulls/118920 --- source/blender/gpu/metal/mtl_texture.mm | 2 ++ source/blender/render/intern/compositor.cc | 14 -------------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/source/blender/gpu/metal/mtl_texture.mm b/source/blender/gpu/metal/mtl_texture.mm index ee14026bd5b..08097e81e70 100644 --- a/source/blender/gpu/metal/mtl_texture.mm +++ b/source/blender/gpu/metal/mtl_texture.mm @@ -1122,6 +1122,8 @@ void gpu::MTLTexture::update_sub( /* Decrement texture reference counts. This ensures temporary texture views are released. */ [texture_handle release]; + ctx->main_command_buffer.submit(false); + /* Release temporary staging buffer allocation. * NOTE: Allocation will be tracked with command submission and released once no longer in use. */ diff --git a/source/blender/render/intern/compositor.cc b/source/blender/render/intern/compositor.cc index 0c4bf9b01c3..662b6082112 100644 --- a/source/blender/render/intern/compositor.cc +++ b/source/blender/render/intern/compositor.cc @@ -508,13 +508,6 @@ class RealtimeCompositor { * to avoid them blocking each other. */ BLI_assert(!BLI_thread_is_main() || G.background); - /* The realtime compositor uses GPU module and does not rely on the draw manager, or its global - * state. This means that activation of GPU context does not require lock of the main thread. - * - * However, while this has been tested on Linux and works well, on macOS it causes to - * spontaneous invalid colors in the composite output. The Windows has not been extensively - * tested yet. */ -#if defined(__linux__) || defined(_WIN32) if (G.background) { /* In the background mode the system context of the render engine might be nullptr, which * forces some code paths which more tightly couple it with the draw manager. @@ -529,9 +522,6 @@ class RealtimeCompositor { GPU_render_begin(); WM_system_gpu_context_activate(re_system_gpu_context); GPU_context_active_set(static_cast(re_blender_gpu_context)); -#else - DRW_render_context_enable(&render_); -#endif context_->update_input_data(input_data); @@ -546,14 +536,10 @@ class RealtimeCompositor { context_->viewer_output_to_viewer_image(); texture_pool_->free_unused_and_reset(); -#if defined(__linux__) || defined(_WIN32) GPU_flush(); GPU_render_end(); GPU_context_active_set(nullptr); WM_system_gpu_context_release(re_system_gpu_context); -#else - DRW_render_context_disable(&render_); -#endif } };