Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClément Foucault <foucault.clem@gmail.com>2020-08-07 18:00:28 +0300
committerClément Foucault <foucault.clem@gmail.com>2020-08-08 02:16:40 +0300
commit1dd737759639c63d3279be774202585de778dac5 (patch)
treeb0b666aa69477ee7eb0cc2b5a379522bd75d1a62 /source/blender/gpu/intern
parent0ccf3f89d2e2389d433d1ab682ad04310a9b19ae (diff)
GPUBackend: Add new GPUBackend object to manage GL object allocations
This just set a global object responsible for allocating new objects in a thread safe way without needing any GPUContext bound to this thread. This also introduce the GLContext which will contain all the GL related functions for the current context.
Diffstat (limited to 'source/blender/gpu/intern')
-rw-r--r--source/blender/gpu/intern/gpu_backend.hh37
-rw-r--r--source/blender/gpu/intern/gpu_context.cc95
-rw-r--r--source/blender/gpu/intern/gpu_context_private.h44
-rw-r--r--source/blender/gpu/intern/gpu_init_exit.c3
4 files changed, 131 insertions, 48 deletions
diff --git a/source/blender/gpu/intern/gpu_backend.hh b/source/blender/gpu/intern/gpu_backend.hh
new file mode 100644
index 00000000000..24f592f214f
--- /dev/null
+++ b/source/blender/gpu/intern/gpu_backend.hh
@@ -0,0 +1,37 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Copyright 2020, Blender Foundation.
+ * All rights reserved.
+ */
+
+/** \file
+ * \ingroup gpu
+ *
+ * GPUBackend derived class contain allocators that do not need a context bound.
+ * The backend is init at startup and is accessible using GPU_backend_get() */
+
+#pragma once
+
+struct GPUContext;
+
+class GPUBackend {
+ public:
+ virtual ~GPUBackend(){};
+
+ virtual GPUContext *context_alloc(void *ghost_window) = 0;
+};
+
+GPUBackend *gpu_backend_get(void);
diff --git a/source/blender/gpu/intern/gpu_context.cc b/source/blender/gpu/intern/gpu_context.cc
index c3c0863f6cd..9707c32cede 100644
--- a/source/blender/gpu/intern/gpu_context.cc
+++ b/source/blender/gpu/intern/gpu_context.cc
@@ -28,6 +28,9 @@
* - free can be called from any thread
*/
+/* TODO Create cmake option. */
+#define WITH_OPENGL_BACKEND 1
+
#include "BLI_assert.h"
#include "BLI_utildefines.h"
@@ -36,63 +39,25 @@
#include "GHOST_C-api.h"
+#include "gpu_backend.hh"
#include "gpu_batch_private.h"
#include "gpu_context_private.h"
#include "gpu_matrix_private.h"
+#ifdef WITH_OPENGL_BACKEND
+# include "gl_backend.hh"
+# include "gl_context.hh"
+#endif
+
#include <mutex>
-#include <pthread.h>
-#include <string.h>
-#include <unordered_set>
#include <vector>
-#if TRUST_NO_ONE
-# if 0
-extern "C" {
-extern int BLI_thread_is_main(void); /* Blender-specific function */
-}
-
-static bool thread_is_main()
-{
- /* "main" here means the GL context's thread */
- return BLI_thread_is_main();
-}
-# endif
-#endif
-
static std::vector<GLuint> orphaned_buffer_ids;
static std::vector<GLuint> orphaned_texture_ids;
static std::mutex orphans_mutex;
static std::mutex main_context_mutex;
-struct GPUContext {
- GLuint default_vao;
- GLuint default_framebuffer;
- GPUFrameBuffer *current_fbo;
- std::unordered_set<GPUBatch *> batches; /* Batches that have VAOs from this context */
-#ifdef DEBUG
- std::unordered_set<GPUFrameBuffer *>
- framebuffers; /* Framebuffers that have FBO from this context */
-#endif
- struct GPUMatrixState *matrix_state;
- std::vector<GLuint> orphaned_vertarray_ids;
- std::vector<GLuint> orphaned_framebuffer_ids;
- std::mutex orphans_mutex; /* todo: try spinlock instead */
-#if TRUST_NO_ONE
- pthread_t thread; /* Thread on which this context is active. */
- bool thread_is_used;
-#endif
-
- GPUContext()
- {
-#if TRUST_NO_ONE
- thread_is_used = false;
-#endif
- current_fbo = 0;
- }
-};
-
static thread_local GPUContext *active_ctx = NULL;
static void orphans_add(GPUContext *ctx, std::vector<GLuint> *orphan_list, GLuint id)
@@ -142,8 +107,12 @@ static void orphans_clear(GPUContext *ctx)
GPUContext *GPU_context_create(void *ghost_window)
{
- /* BLI_assert(thread_is_main()); */
- GPUContext *ctx = new GPUContext;
+ if (gpu_backend_get() == NULL) {
+ /* TODO move where it make sense. */
+ GPU_backend_init(GPU_BACKEND_OPENGL);
+ }
+
+ GPUContext *ctx = gpu_backend_get()->context_alloc(ghost_window);
glGenVertexArrays(1, &ctx->default_vao);
if (ghost_window != NULL) {
ctx->default_framebuffer = GHOST_GetDefaultOpenGLFramebuffer((GHOST_WindowHandle)ghost_window);
@@ -364,3 +333,37 @@ void GPU_context_main_unlock(void)
{
main_context_mutex.unlock();
}
+
+/* -------------------------------------------------------------------- */
+/** \name Backend selection
+ * \{ */
+
+static GPUBackend *g_backend;
+
+void GPU_backend_init(eGPUBackendType backend_type)
+{
+ BLI_assert(g_backend == NULL);
+
+ switch (backend_type) {
+#if WITH_OPENGL_BACKEND
+ case GPU_BACKEND_OPENGL:
+ g_backend = new GLBackend;
+ break;
+#endif
+ default:
+ BLI_assert(0);
+ break;
+ }
+}
+
+void GPU_backend_exit(void)
+{
+ delete g_backend;
+}
+
+GPUBackend *gpu_backend_get(void)
+{
+ return g_backend;
+}
+
+/** \} */
diff --git a/source/blender/gpu/intern/gpu_context_private.h b/source/blender/gpu/intern/gpu_context_private.h
index 08fbefe3b3f..374a05bc25f 100644
--- a/source/blender/gpu/intern/gpu_context_private.h
+++ b/source/blender/gpu/intern/gpu_context_private.h
@@ -27,12 +27,52 @@
#include "GPU_context.h"
+/* TODO cleanup this ifdef */
#ifdef __cplusplus
-extern "C" {
-#endif
+
+# include <mutex>
+# include <pthread.h>
+# include <string.h>
+# include <unordered_set>
+# include <vector>
struct GPUFrameBuffer;
+struct GPUContext {
+ GLuint default_vao;
+ GLuint default_framebuffer;
+ GPUFrameBuffer *current_fbo;
+ std::unordered_set<GPUBatch *> batches; /* Batches that have VAOs from this context */
+# ifdef DEBUG
+ std::unordered_set<GPUFrameBuffer *>
+ framebuffers; /* Framebuffers that have FBO from this context */
+# endif
+ struct GPUMatrixState *matrix_state;
+ std::vector<GLuint> orphaned_vertarray_ids;
+ std::vector<GLuint> orphaned_framebuffer_ids;
+ std::mutex orphans_mutex; /* todo: try spinlock instead */
+# if TRUST_NO_ONE
+ pthread_t thread; /* Thread on which this context is active. */
+ bool thread_is_used;
+# endif
+
+ GPUContext()
+ {
+# if TRUST_NO_ONE
+ thread_is_used = false;
+# endif
+ current_fbo = 0;
+ };
+
+ virtual ~GPUContext(){};
+};
+
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
GLuint GPU_vao_default(void);
GLuint GPU_framebuffer_default(void);
diff --git a/source/blender/gpu/intern/gpu_init_exit.c b/source/blender/gpu/intern/gpu_init_exit.c
index c5061ec9ba3..462a1d395c2 100644
--- a/source/blender/gpu/intern/gpu_init_exit.c
+++ b/source/blender/gpu/intern/gpu_init_exit.c
@@ -26,6 +26,7 @@
#include "BLI_sys_types.h"
#include "GPU_batch.h"
#include "GPU_buffers.h"
+#include "GPU_context.h"
#include "GPU_immediate.h"
#include "intern/gpu_codegen.h"
@@ -92,6 +93,8 @@ void GPU_exit(void)
gpu_extensions_exit();
gpu_platform_exit(); /* must come last */
+ GPU_backend_exit();
+
initialized = false;
}