Blender V5.3
draw_gpu_context.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2016 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_global.hh"
10
11#include "GPU_capabilities.hh"
12#include "GPU_context.hh"
13#include "GPU_state.hh"
14
15#include "RE_engine.h"
16#include "RE_pipeline.h"
17
18#include "DRW_engine.hh"
19#include "DRW_render.hh"
20
21#include "WM_api.hh"
22#include "wm_window.hh"
23
24namespace blender {
25
26/* -------------------------------------------------------------------- */
32
33static TicketMutex *draw_mutex = nullptr;
34static TicketMutex *submission_mutex = nullptr;
35
41
47
49{
51 BLI_assert(locked);
52 UNUSED_VARS_NDEBUG(locked);
53}
54
59
67
73
75
76/* -------------------------------------------------------------------- */
80
81/* Context that can be shared across threads. Usage is guarded by a ticket mutex.
82 * Should eventually be moved to GPU module after we get rid of the WM calls. */
84 /* Should be private but needs to be public for XR workaround. */
85 public:
86 TicketMutex *mutex_ = nullptr;
90 GPUContext *blender_gpu_context_ = nullptr;
91
92 /* NOTE: This changes the active context. */
101
112
113 void enable()
114 {
116 /* IMPORTANT: We don't support immediate mode in render mode!
117 * This shall remain in effect until immediate mode supports
118 * multiple threads. */
120
122
126 }
127
129 {
131 }
132
133 /* Restore window drawable after disabling if restore is true. */
134 void disable(bool restore = false)
135 {
137
138 if (BLI_thread_is_main() && restore) {
140 }
141 else {
143 GPU_context_active_set(nullptr);
144 }
145 /* Render boundaries are opened and closed here as this may be
146 * called outside of an existing render loop. */
148
150 DRW_lock_end();
151 }
152};
153
155
156/* -------------------------------------------------------------------- */
162
167
169{
170 BLI_assert(viewport_context == nullptr); /* Ensure it's called once */
171
173
174 viewport_context = MEM_new<ContextShared>(__func__);
175 preview_context = MEM_new<ContextShared>(__func__);
176
178}
179
181{
183 if (viewport_context == nullptr) {
184 return;
185 }
187
188 MEM_SAFE_DELETE(viewport_context);
189 MEM_SAFE_DELETE(preview_context);
190}
191
193
194/* -------------------------------------------------------------------- */
197
198void DRW_gpu_context_enable_ex(bool /*restore*/)
199{
200 if (viewport_context == nullptr) {
201 return;
202 }
204}
205
207{
208 if (viewport_context == nullptr) {
209 return;
210 }
211 viewport_context->disable(restore);
212}
213
215{
216 if (preview_context == nullptr) {
217 return;
218 }
220}
221
223{
224 if (preview_context == nullptr) {
225 return;
226 }
228}
229
231{
232 /* TODO: should be replace by a more elegant alternative. */
233
234 if (G.background && viewport_context == nullptr) {
236 }
238}
239
241{
242 if (viewport_context == nullptr) {
243 return false;
244 }
246 return true;
247}
248
253
258
260{
261 /* If thread is main you should use DRW_gpu_context_enable(). */
263
265 WM_system_gpu_context_activate(re_system_gpu_context);
266}
267
269{
270 WM_system_gpu_context_release(re_system_gpu_context);
271 DRW_lock_end();
272}
273
275{
276 /* If thread is main you should use DRW_gpu_context_enable(). */
278
279 GPU_context_active_set(static_cast<GPUContext *>(re_gpu_context));
280}
281
282void DRW_blender_gpu_render_context_disable(void * /*re_gpu_context*/)
283{
284 GPU_flush();
285 GPU_context_active_set(nullptr);
286}
287
289{
290 if (G.background && viewport_context == nullptr) {
292 }
293
295
299 return;
300 }
301
302 GHOST_IContext *re_viewport_system_gpu_context = RE_system_gpu_context_get(render);
303
304 /* Changing Context */
305 if (re_viewport_system_gpu_context != nullptr) {
306 DRW_system_gpu_render_context_enable(re_viewport_system_gpu_context);
307 /* We need to query gpu context after a gl context has been bound. */
308 void *re_viewport_context = RE_blender_gpu_context_ensure(render);
309 DRW_blender_gpu_render_context_enable(re_viewport_context);
310 }
311 else {
313 }
314}
315
317{
322 return;
323 }
324
325 GHOST_IContext *re_viewport_system_gpu_context = RE_system_gpu_context_get(render);
326
327 if (re_viewport_system_gpu_context != nullptr) {
328 void *re_viewport_context = RE_blender_gpu_context_ensure(render);
329 /* GPU rendering may occur during context disable. */
330 DRW_blender_gpu_render_context_disable(re_viewport_context);
332 DRW_system_gpu_render_context_disable(re_viewport_system_gpu_context);
333 }
334 else {
335 /* Usually the case for a preview job. The `Render` is created inside the render thread which
336 * is too late to create a GPU context. */
339 }
340}
341
343
344/* -------------------------------------------------------------------- */
347
348#ifdef WITH_XR_OPENXR
349
350GHOST_IContext *DRW_system_gpu_context_get()
351{
352 /* XXX: There should really be no such getter, but for VR we currently can't easily avoid it.
353 * OpenXR needs some low level info for the GPU context that will be used for submitting the
354 * final frame-buffer. VR could in theory create its own context, but that would mean we have to
355 * switch to it just to submit the final frame, which has notable performance impact.
356 *
357 * We could "inject" a context through DRW_system_gpu_render_context_enable(), but that would
358 * have to work from the main thread, which is tricky to get working too. The preferable solution
359 * would be using a separate thread for VR drawing where a single context can stay active. */
360
362}
363
364void *DRW_xr_blender_gpu_context_get()
365{
366 /* XXX: See comment on #DRW_system_gpu_context_get(). */
367
369}
370
371void DRW_xr_drawing_begin()
372{
373 /* XXX: See comment on #DRW_system_gpu_context_get(). */
374
377}
378
379void DRW_xr_drawing_end()
380{
381 /* XXX: See comment on #DRW_system_gpu_context_get(). */
382
384 DRW_lock_end();
385}
386
387#endif
388
390
391/* -------------------------------------------------------------------- */
418
420{
421 if (!BLI_thread_is_main()) {
422 return false;
423 }
424
426 /* Context release is requested from the outside of the draw manager main draw loop, indicate
427 * this to the `DRW_gpu_context_activate()` so that it restores drawable of the window.
428 */
429 return false;
430 }
431
432 GPU_context_active_set(nullptr);
434
435 return true;
436}
437
438void DRW_gpu_context_activate(bool drw_state)
439{
440 if (!BLI_thread_is_main()) {
441 return;
442 }
443
444 if (drw_state) {
447 }
448 else {
450 }
451}
452
454
455} // namespace blender
#define UNUSED_VARS_NDEBUG(...)
GHOST_IContext * system_gpu_context_
void disable(bool restore=false)
GPUContext * blender_gpu_context_
BMesh Iterators.
void DRW_gpu_context_enable()
void * RE_blender_gpu_context_ensure(Render *re)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static ContextShared * preview_context
void DRW_render_context_enable(Render *render)
void DRW_blender_gpu_render_context_enable(void *re_gpu_context)
void BLI_ticket_mutex_lock(TicketMutex *ticket)
Definition threads.cc:555
void DRW_render_context_disable(Render *render)
void DRW_system_gpu_render_context_disable(GHOST_IContext *re_system_gpu_context)
GPUContext * GPU_context_create(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context)
static TicketMutex * submission_mutex
void GPU_context_main_unlock()
bool GPU_use_main_context_workaround()
void GPU_context_end_frame(GPUContext *ctx)
void DRW_system_gpu_render_context_enable(GHOST_IContext *re_system_gpu_context)
void WM_system_gpu_context_release(GHOST_IContext *context)
void DRW_gpu_context_disable_ex(bool restore)
void GPU_render_begin()
void DRW_blender_gpu_render_context_disable(void *re_gpu_context)
void GPU_context_active_set(GPUContext *)
GHOST_IContext * WM_system_gpu_context_create()
void DRW_mutexes_exit()
void DRW_gpu_context_enable_ex(bool restore)
bool DRW_gpu_context_is_enabled()
void GPU_context_begin_frame(GPUContext *ctx)
void wm_window_reset_drawable()
Global G
Definition blender.cc:57
void WM_init_gpu_offscreen()
void DRW_gpu_context_disable()
static void drw_gpu_preview_context_disable()
void WM_system_gpu_context_dispose(GHOST_IContext *context)
static void drw_gpu_preview_context_enable()
void GPU_render_end()
void DRW_lock_start()
TicketMutex * BLI_ticket_mutex_alloc()
Definition threads.cc:513
void BLI_ticket_mutex_free(TicketMutex *ticket)
Definition threads.cc:523
int BLI_thread_is_main()
Definition threads.cc:184
void DRW_submission_end()
static ContextShared * viewport_context
void WM_system_gpu_context_activate(GHOST_IContext *context)
void BLI_ticket_mutex_unlock(TicketMutex *ticket)
Definition threads.cc:565
static TicketMutex * draw_mutex
void DRW_gpu_context_destroy()
void GPU_flush()
Definition gpu_state.cc:284
void DRW_lock_end()
bool DRW_gpu_context_release()
void DRW_submission_start()
bool DRW_gpu_context_try_enable()
bool BLI_ticket_mutex_lock_check_recursive(TicketMutex *ticket)
Definition threads.cc:560
void DRW_mutexes_init()
void GPU_context_main_lock()
void DRW_gpu_context_activate(bool drw_state)
GHOST_IContext * RE_system_gpu_context_get(Render *re)
void DRW_gpu_context_create()
GPUContext * GPU_context_active_get()
void GPU_context_discard(GPUContext *)