[Bf-blender-cvs] [92a99c14965] master: Fix Eevee backround render crash after recent changes from D15463

Brecht Van Lommel noreply at git.blender.org
Fri Jul 15 19:12:48 CEST 2022


Commit: 92a99c14965905e73f049bc1f92b597a903977fc
Author: Brecht Van Lommel
Date:   Fri Jul 15 18:58:35 2022 +0200
Branches: master
https://developer.blender.org/rB92a99c14965905e73f049bc1f92b597a903977fc

Fix Eevee backround render crash after recent changes from D15463

Backend initialization needs to be delayed until after the OpenGL context
is created. This worked fine in foreground mode because the OpenGL context
already exists for the window at the point GPU_backend_init_once was called,
but not for background mode.

Create the backend just in time in GPU_context_create as before, and
automatically free it when the last context id discarded. But check if any
GPU backend is supported before creating the OpenGL context.

Ref D15463, D15465

===================================================================

M	source/blender/gpu/GPU_context.h
M	source/blender/gpu/intern/gpu_context.cc
M	source/blender/gpu/tests/gpu_testing.cc
M	source/blender/windowmanager/intern/wm_init_exit.c

===================================================================

diff --git a/source/blender/gpu/GPU_context.h b/source/blender/gpu/GPU_context.h
index c81296093a1..9d92ea2cad9 100644
--- a/source/blender/gpu/GPU_context.h
+++ b/source/blender/gpu/GPU_context.h
@@ -17,14 +17,10 @@
 extern "C" {
 #endif
 
-/* GPU backends abstract the differences between different APIs. These must be
- * initialized before creating contexts, and deleted after the last context is
- * discarded. GPU_context_create automatically initializes a backend if none
- * exists yet. */
-bool GPU_backend_init_once(void);
-void GPU_backend_exit(void);
-bool GPU_backend_supported(eGPUBackendType type);
-
+/* GPU backends abstract the differences between different APIs. GPU_context_create
+ * automatically initializes the backend, and GPU_context_discard frees it when there
+ * are no more contexts. */
+bool GPU_backend_supported(void);
 eGPUBackendType GPU_backend_get_type(void);
 
 /** Opaque type hiding blender::gpu::Context. */
diff --git a/source/blender/gpu/intern/gpu_context.cc b/source/blender/gpu/intern/gpu_context.cc
index 5ae020e45a4..20d9208a199 100644
--- a/source/blender/gpu/intern/gpu_context.cc
+++ b/source/blender/gpu/intern/gpu_context.cc
@@ -44,6 +44,12 @@ using namespace blender::gpu;
 
 static thread_local Context *active_ctx = nullptr;
 
+static std::mutex backend_users_mutex;
+static int num_backend_users = 0;
+
+static void gpu_backend_create();
+static void gpu_backend_discard();
+
 /* -------------------------------------------------------------------- */
 /** \name gpu::Context methods
  * \{ */
@@ -86,7 +92,14 @@ Context *Context::get()
 
 GPUContext *GPU_context_create(void *ghost_window)
 {
-  GPU_backend_init_once();
+  {
+    std::scoped_lock lock(backend_users_mutex);
+    if (num_backend_users == 0) {
+      /* Automatically create backend when first context is created. */
+      gpu_backend_create();
+    }
+    num_backend_users++;
+  }
 
   Context *ctx = GPUBackend::get()->context_alloc(ghost_window);
 
@@ -99,6 +112,16 @@ void GPU_context_discard(GPUContext *ctx_)
   Context *ctx = unwrap(ctx_);
   delete ctx;
   active_ctx = nullptr;
+
+  {
+    std::scoped_lock lock(backend_users_mutex);
+    num_backend_users--;
+    BLI_assert(num_backend_users >= 0);
+    if (num_backend_users == 0) {
+      /* Discard backend when last context is discarded. */
+      gpu_backend_discard();
+    }
+  }
 }
 
 void GPU_context_active_set(GPUContext *ctx_)
@@ -189,11 +212,12 @@ void GPU_render_step()
 /** \name Backend selection
  * \{ */
 
+static const eGPUBackendType g_backend_type = GPU_BACKEND_OPENGL;
 static GPUBackend *g_backend = nullptr;
 
-bool GPU_backend_supported(eGPUBackendType type)
+bool GPU_backend_supported(void)
 {
-  switch (type) {
+  switch (g_backend_type) {
     case GPU_BACKEND_OPENGL:
 #ifdef WITH_OPENGL_BACKEND
       return true;
@@ -212,21 +236,12 @@ bool GPU_backend_supported(eGPUBackendType type)
   }
 }
 
-bool GPU_backend_init_once()
+static void gpu_backend_create()
 {
-  if (GPUBackend::get() != nullptr) {
-    return true;
-  }
-
-  const eGPUBackendType backend_type = GPU_BACKEND_OPENGL;
-  if (!GPU_backend_supported(backend_type)) {
-    return false;
-  }
+  BLI_assert(g_backend == nullptr);
+  BLI_assert(GPU_backend_supported(g_backend_type));
 
-  static std::mutex backend_init_mutex;
-  std::scoped_lock lock(backend_init_mutex);
-
-  switch (backend_type) {
+  switch (g_backend_type) {
 #ifdef WITH_OPENGL_BACKEND
     case GPU_BACKEND_OPENGL:
       g_backend = new GLBackend;
@@ -241,8 +256,6 @@ bool GPU_backend_init_once()
       BLI_assert(0);
       break;
   }
-
-  return true;
 }
 
 void gpu_backend_delete_resources()
@@ -251,7 +264,7 @@ void gpu_backend_delete_resources()
   g_backend->delete_resources();
 }
 
-void GPU_backend_exit()
+void gpu_backend_discard()
 {
   /* TODO: assert no resource left. */
   delete g_backend;
diff --git a/source/blender/gpu/tests/gpu_testing.cc b/source/blender/gpu/tests/gpu_testing.cc
index 5a2ad893360..224a9afcf59 100644
--- a/source/blender/gpu/tests/gpu_testing.cc
+++ b/source/blender/gpu/tests/gpu_testing.cc
@@ -17,7 +17,6 @@ void GPUTest::SetUp()
   GHOST_GLSettings glSettings = {0};
   CLG_init();
   ghost_system = GHOST_CreateSystem();
-  GPU_backend_init_once();
   ghost_context = GHOST_CreateOpenGLContext(ghost_system, glSettings);
   GHOST_ActivateOpenGLContext(ghost_context);
   context = GPU_context_create(nullptr);
@@ -29,7 +28,6 @@ void GPUTest::TearDown()
   GPU_exit();
   GPU_context_discard(context);
   GHOST_DisposeOpenGLContext(ghost_system, ghost_context);
-  GPU_backend_exit();
   GHOST_DisposeSystem(ghost_system);
   CLG_exit();
 }
diff --git a/source/blender/windowmanager/intern/wm_init_exit.c b/source/blender/windowmanager/intern/wm_init_exit.c
index 7324abfd096..7f5ec77e16d 100644
--- a/source/blender/windowmanager/intern/wm_init_exit.c
+++ b/source/blender/windowmanager/intern/wm_init_exit.c
@@ -169,7 +169,7 @@ void WM_init_opengl(void)
     wm_ghost_init(NULL);
   }
 
-  if (!GPU_backend_init_once()) {
+  if (!GPU_backend_supported()) {
     return;
   }
 
@@ -613,7 +613,6 @@ void WM_exit_ex(bContext *C, const bool do_python)
   else {
     UI_exit();
   }
-  GPU_backend_exit();
 
   BKE_blender_userdef_data_free(&U, false);



More information about the Bf-blender-cvs mailing list