Blender  V2.93
gpu_context.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2016 by Mike Erwin.
17  * All rights reserved.
18  */
19 
31 /* TODO Create cmake option. */
32 #define WITH_OPENGL_BACKEND 1
33 
34 #include "BLI_assert.h"
35 #include "BLI_utildefines.h"
36 
37 #include "GPU_context.h"
38 #include "GPU_framebuffer.h"
39 
40 #include "GHOST_C-api.h"
41 
42 #include "gpu_backend.hh"
43 #include "gpu_batch_private.hh"
44 #include "gpu_context_private.hh"
45 #include "gpu_matrix_private.h"
46 
47 #ifdef WITH_OPENGL_BACKEND
48 # include "gl_backend.hh"
49 # include "gl_context.hh"
50 #endif
51 
52 #include <mutex>
53 #include <vector>
54 
55 using namespace blender::gpu;
56 
57 static thread_local Context *active_ctx = nullptr;
58 
59 /* -------------------------------------------------------------------- */
63 namespace blender::gpu {
64 
66 {
67  thread_ = pthread_self();
68  is_active_ = false;
70 }
71 
73 {
75  delete state_manager;
76  delete front_left;
77  delete back_left;
78  delete front_right;
79  delete back_right;
80  delete imm;
81 }
82 
84 {
85  return (this == active_ctx) && pthread_equal(pthread_self(), thread_);
86 }
87 
89 {
90  return active_ctx;
91 }
92 
93 } // namespace blender::gpu
94 
97 /* -------------------------------------------------------------------- */
98 
99 GPUContext *GPU_context_create(void *ghost_window)
100 {
101  if (GPUBackend::get() == nullptr) {
102  /* TODO move where it make sense. */
104  }
105 
106  Context *ctx = GPUBackend::get()->context_alloc(ghost_window);
107 
109  return wrap(ctx);
110 }
111 
112 /* to be called after GPU_context_active_set(ctx_to_destroy) */
114 {
115  Context *ctx = unwrap(ctx_);
116  delete ctx;
117  active_ctx = nullptr;
118 }
119 
120 /* ctx can be NULL */
122 {
123  Context *ctx = unwrap(ctx_);
124 
125  if (active_ctx) {
127  }
128 
129  active_ctx = ctx;
130 
131  if (ctx) {
132  ctx->activate();
133  }
134 }
135 
137 {
138  return wrap(Context::get());
139 }
140 
141 /* -------------------------------------------------------------------- */
148 
150 {
151  main_context_mutex.lock();
152 }
153 
155 {
156  main_context_mutex.unlock();
157 }
158 
161 /* -------------------------------------------------------------------- */
166 
168 {
169  BLI_assert(g_backend == nullptr);
170 
171  switch (backend_type) {
172 #if WITH_OPENGL_BACKEND
173  case GPU_BACKEND_OPENGL:
174  g_backend = new GLBackend;
175  break;
176 #endif
177  default:
178  BLI_assert(0);
179  break;
180  }
181 }
182 
184 {
185  /* TODO assert no resource left. Currently UI textures are still not freed in their context
186  * correctly. */
187  delete g_backend;
188  g_backend = nullptr;
189 }
190 
191 GPUBackend *GPUBackend::get()
192 {
193  return g_backend;
194 }
195 
#define BLI_assert(a)
Definition: BLI_assert.h:58
ThreadMutex mutex
GHOST C-API function and type declarations.
struct GPUContext GPUContext
Definition: GPU_context.h:44
eGPUBackendType
Definition: GPU_context.h:35
@ GPU_BACKEND_OPENGL
Definition: GPU_context.h:37
virtual void deactivate(void)=0
GPUMatrixState * matrix_state
virtual void activate(void)=0
static Context * get(void)
Definition: gpu_context.cc:88
bool is_active_on_thread(void)
Definition: gpu_context.cc:83
void GPU_context_main_lock(void)
Definition: gpu_context.cc:149
void GPU_backend_init(eGPUBackendType backend_type)
Definition: gpu_context.cc:167
static thread_local Context * active_ctx
Definition: gpu_context.cc:57
void GPU_context_active_set(GPUContext *ctx_)
Definition: gpu_context.cc:121
GPUContext * GPU_context_active_get(void)
Definition: gpu_context.cc:136
void GPU_context_discard(GPUContext *ctx_)
Definition: gpu_context.cc:113
static std::mutex main_context_mutex
Definition: gpu_context.cc:147
GPUContext * GPU_context_create(void *ghost_window)
Definition: gpu_context.cc:99
static GPUBackend * g_backend
Definition: gpu_context.cc:165
void GPU_backend_exit(void)
Definition: gpu_context.cc:183
void GPU_context_main_unlock(void)
Definition: gpu_context.cc:154
GPUMatrixState * GPU_matrix_state_create(void)
Definition: gpu_matrix.cc:70
void GPU_matrix_state_discard(GPUMatrixState *state)
Definition: gpu_matrix.cc:91
static GPUContext * wrap(Context *ctx)
static Context * unwrap(GPUContext *ctx)