66de79314b44da9c6aa18c8352d4d4413b890318
[blender.git] / source / blender / draw / intern / draw_manager.c
1 /*
2  * Copyright 2016, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Blender Institute
19  *
20  */
21
22 /** \file blender/draw/intern/draw_manager.c
23  *  \ingroup draw
24  */
25
26 #include <stdio.h>
27
28 #include "BLI_listbase.h"
29 #include "BLI_mempool.h"
30 #include "BLI_rect.h"
31 #include "BLI_string.h"
32 #include "BLI_threads.h"
33
34 #include "BLF_api.h"
35
36 #include "BKE_global.h"
37 #include "BKE_mesh.h"
38 #include "BKE_object.h"
39 #include "BKE_workspace.h"
40
41 #include "draw_manager.h"
42 #include "DNA_camera_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meshdata_types.h"
45
46 #include "ED_space_api.h"
47 #include "ED_screen.h"
48 #include "ED_view3d.h"
49
50 #include "GPU_draw.h"
51 #include "GPU_extensions.h"
52 #include "GPU_framebuffer.h"
53 #include "GPU_immediate.h"
54 #include "GPU_uniformbuffer.h"
55 #include "GPU_viewport.h"
56 #include "GPU_matrix.h"
57
58 #include "IMB_colormanagement.h"
59
60 #include "RE_engine.h"
61 #include "RE_pipeline.h"
62
63 #include "UI_interface.h"
64 #include "UI_resources.h"
65
66 #include "WM_api.h"
67 #include "wm_window.h"
68
69 #include "draw_manager_text.h"
70 #include "draw_manager_profiling.h"
71
72 /* only for callbacks */
73 #include "draw_cache_impl.h"
74
75 #include "draw_mode_engines.h"
76 #include "engines/clay/clay_engine.h"
77 #include "engines/eevee/eevee_engine.h"
78 #include "engines/basic/basic_engine.h"
79 #include "engines/workbench/workbench_engine.h"
80 #include "engines/external/external_engine.h"
81
82 #include "../../../intern/gawain/gawain/gwn_context.h"
83
84 #include "DEG_depsgraph.h"
85 #include "DEG_depsgraph_query.h"
86
87 #ifdef USE_GPU_SELECT
88 #  include "GPU_select.h"
89 #endif
90
91 /** Render State: No persistent data between draw calls. */
92 DRWManager DST = {NULL};
93
94 ListBase DRW_engines = {NULL, NULL};
95
96 extern struct GPUUniformBuffer *view_ubo; /* draw_manager_exec.c */
97
98 static void drw_state_prepare_clean_for_draw(DRWManager *dst)
99 {
100         memset(dst, 0x0, offsetof(DRWManager, ogl_context));
101 }
102
103 /* This function is used to reset draw manager to a state
104  * where we don't re-use data by accident across different
105  * draw calls.
106  */
107 #ifdef DEBUG
108 static void drw_state_ensure_not_reused(DRWManager *dst)
109 {
110         memset(dst, 0xff, offsetof(DRWManager, ogl_context));
111 }
112 #endif
113
114 /* -------------------------------------------------------------------- */
115
116 void DRW_draw_callbacks_pre_scene(void)
117 {
118         RegionView3D *rv3d = DST.draw_ctx.rv3d;
119
120         gpuLoadProjectionMatrix(rv3d->winmat);
121         gpuLoadMatrix(rv3d->viewmat);
122 }
123
124 void DRW_draw_callbacks_post_scene(void)
125 {
126         RegionView3D *rv3d = DST.draw_ctx.rv3d;
127
128         gpuLoadProjectionMatrix(rv3d->winmat);
129         gpuLoadMatrix(rv3d->viewmat);
130 }
131
132 struct DRWTextStore *DRW_text_cache_ensure(void)
133 {
134         BLI_assert(DST.text_store_p);
135         if (*DST.text_store_p == NULL) {
136                 *DST.text_store_p = DRW_text_cache_create();
137         }
138         return *DST.text_store_p;
139 }
140
141
142 /* -------------------------------------------------------------------- */
143
144 /** \name Settings
145  * \{ */
146
147 bool DRW_object_is_renderable(Object *ob)
148 {
149         BLI_assert(BKE_object_is_visible(ob, OB_VISIBILITY_CHECK_UNKNOWN_RENDER_MODE));
150
151         if (ob->type == OB_MESH) {
152                 if (ob == DST.draw_ctx.object_edit) {
153                         IDProperty *props = BKE_layer_collection_engine_evaluated_get(ob, COLLECTION_MODE_EDIT, "");
154                         bool do_show_occlude_wire = BKE_collection_engine_property_value_get_bool(props, "show_occlude_wire");
155                         if (do_show_occlude_wire) {
156                                 return false;
157                         }
158                         bool do_show_weight = BKE_collection_engine_property_value_get_bool(props, "show_weight");
159                         if (do_show_weight) {
160                                 return false;
161                         }
162                 }
163         }
164
165         return true;
166 }
167
168 /**
169  * Return whether this object is visible depending if
170  * we are rendering or drawing in the viewport.
171  */
172 bool DRW_check_object_visible_within_active_context(Object *ob)
173 {
174         const eObjectVisibilityCheck mode = DRW_state_is_scene_render() ?
175                                              OB_VISIBILITY_CHECK_FOR_RENDER :
176                                              OB_VISIBILITY_CHECK_FOR_VIEWPORT;
177         return BKE_object_is_visible(ob, mode);
178 }
179
180 bool DRW_object_is_flat_normal(const Object *ob)
181 {
182         if (ob->type == OB_MESH) {
183                 const Mesh *me = ob->data;
184                 if (me->mpoly && me->mpoly[0].flag & ME_SMOOTH) {
185                         return false;
186                 }
187         }
188         return true;
189 }
190
191 /**
192  * Return true if the object has its own draw mode.
193  * Caller must check this is active */
194 int DRW_object_is_mode_shade(const Object *ob)
195 {
196         BLI_assert(ob == DST.draw_ctx.obact);
197         UNUSED_VARS_NDEBUG(ob);
198         if ((DST.draw_ctx.object_mode & OB_MODE_EDIT) == 0) {
199                 if (DST.draw_ctx.object_mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT | OB_MODE_TEXTURE_PAINT)) {
200                         if ((DST.draw_ctx.v3d->flag2 & V3D_SHOW_MODE_SHADE_OVERRIDE) == 0) {
201                                 return true;
202                         }
203                         else {
204                                 return false;
205                         }
206                 }
207         }
208         return -1;
209 }
210
211 /** \} */
212
213
214 /* -------------------------------------------------------------------- */
215
216 /** \name Color Management
217  * \{ */
218
219 /* Use color management profile to draw texture to framebuffer */
220 void DRW_transform_to_display(GPUTexture *tex)
221 {
222         drw_state_set(DRW_STATE_WRITE_COLOR);
223
224         Gwn_VertFormat *vert_format = immVertexFormat();
225         unsigned int pos = GWN_vertformat_attr_add(vert_format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
226         unsigned int texco = GWN_vertformat_attr_add(vert_format, "texCoord", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
227
228         const float dither = 1.0f;
229
230         bool use_ocio = false;
231
232         /* View transform is already applied for offscreen, don't apply again, see: T52046 */
233         if (!(DST.options.is_image_render && !DST.options.is_scene_render)) {
234                 Scene *scene = DST.draw_ctx.scene;
235                 use_ocio = IMB_colormanagement_setup_glsl_draw_from_space(
236                         &scene->view_settings, &scene->display_settings, NULL, dither, false);
237         }
238
239         if (!use_ocio) {
240                 /* View transform is already applied for offscreen, don't apply again, see: T52046 */
241                 if (DST.options.is_image_render && !DST.options.is_scene_render) {
242                         immBindBuiltinProgram(GPU_SHADER_2D_IMAGE_COLOR);
243                         immUniformColor4f(1.0f, 1.0f, 1.0f, 1.0f);
244                 }
245                 else {
246                         immBindBuiltinProgram(GPU_SHADER_2D_IMAGE_LINEAR_TO_SRGB);
247                 }
248                 immUniform1i("image", 0);
249         }
250
251         GPU_texture_bind(tex, 0); /* OCIO texture bind point is 0 */
252
253         float mat[4][4];
254         unit_m4(mat);
255         immUniformMatrix4fv("ModelViewProjectionMatrix", mat);
256
257         /* Full screen triangle */
258         immBegin(GWN_PRIM_TRIS, 3);
259         immAttrib2f(texco, 0.0f, 0.0f);
260         immVertex2f(pos, -1.0f, -1.0f);
261
262         immAttrib2f(texco, 2.0f, 0.0f);
263         immVertex2f(pos, 3.0f, -1.0f);
264
265         immAttrib2f(texco, 0.0f, 2.0f);
266         immVertex2f(pos, -1.0f, 3.0f);
267         immEnd();
268
269         GPU_texture_unbind(tex);
270
271         if (use_ocio) {
272                 IMB_colormanagement_finish_glsl_draw();
273         }
274         else {
275                 immUnbindProgram();
276         }
277 }
278
279 /** \} */
280
281
282 /* -------------------------------------------------------------------- */
283
284 /** \name Viewport (DRW_viewport)
285  * \{ */
286
287 void *drw_viewport_engine_data_ensure(void *engine_type)
288 {
289         void *data = GPU_viewport_engine_data_get(DST.viewport, engine_type);
290
291         if (data == NULL) {
292                 data = GPU_viewport_engine_data_create(DST.viewport, engine_type);
293         }
294         return data;
295 }
296
297 void DRW_engine_viewport_data_size_get(
298         const void *engine_type_v,
299         int *r_fbl_len, int *r_txl_len, int *r_psl_len, int *r_stl_len)
300 {
301         const DrawEngineType *engine_type = engine_type_v;
302
303         if (r_fbl_len) {
304                 *r_fbl_len = engine_type->vedata_size->fbl_len;
305         }
306         if (r_txl_len) {
307                 *r_txl_len = engine_type->vedata_size->txl_len;
308         }
309         if (r_psl_len) {
310                 *r_psl_len = engine_type->vedata_size->psl_len;
311         }
312         if (r_stl_len) {
313                 *r_stl_len = engine_type->vedata_size->stl_len;
314         }
315 }
316
317 const float *DRW_viewport_size_get(void)
318 {
319         return DST.size;
320 }
321
322 const float *DRW_viewport_invert_size_get(void)
323 {
324         return DST.inv_size;
325 }
326
327 const float *DRW_viewport_screenvecs_get(void)
328 {
329         return &DST.screenvecs[0][0];
330 }
331
332 const float *DRW_viewport_pixelsize_get(void)
333 {
334         return &DST.pixsize;
335 }
336
337 static void drw_viewport_cache_resize(void)
338 {
339         /* Release the memiter before clearing the mempools that references them */
340         GPU_viewport_cache_release(DST.viewport);
341
342         if (DST.vmempool != NULL) {
343                 BLI_mempool_clear_ex(DST.vmempool->calls, BLI_mempool_len(DST.vmempool->calls));
344                 BLI_mempool_clear_ex(DST.vmempool->states, BLI_mempool_len(DST.vmempool->states));
345                 BLI_mempool_clear_ex(DST.vmempool->shgroups, BLI_mempool_len(DST.vmempool->shgroups));
346                 BLI_mempool_clear_ex(DST.vmempool->uniforms, BLI_mempool_len(DST.vmempool->uniforms));
347                 BLI_mempool_clear_ex(DST.vmempool->passes, BLI_mempool_len(DST.vmempool->passes));
348         }
349
350         DRW_instance_data_list_free_unused(DST.idatalist);
351         DRW_instance_data_list_resize(DST.idatalist);
352 }
353
354 /* Not a viewport variable, we could split this out. */
355 static void drw_context_state_init(void)
356 {
357         if (DST.draw_ctx.obact) {
358                 DST.draw_ctx.object_mode = DST.draw_ctx.obact->mode;
359         }
360         else {
361                 DST.draw_ctx.object_mode = OB_MODE_OBJECT;
362         }
363
364         /* Edit object. */
365         if (DST.draw_ctx.object_mode & OB_MODE_EDIT) {
366                 DST.draw_ctx.object_edit = DST.draw_ctx.obact;
367         }
368         else {
369                 DST.draw_ctx.object_edit = NULL;
370         }
371
372         /* Pose object. */
373         if (DST.draw_ctx.object_mode & OB_MODE_POSE) {
374                 DST.draw_ctx.object_pose = DST.draw_ctx.obact;
375         }
376         else if (DST.draw_ctx.object_mode & OB_MODE_WEIGHT_PAINT) {
377                 DST.draw_ctx.object_pose = BKE_object_pose_armature_get(DST.draw_ctx.obact);
378         }
379         else {
380                 DST.draw_ctx.object_pose = NULL;
381         }
382 }
383
384 /* It also stores viewport variable to an immutable place: DST
385  * This is because a cache uniform only store reference
386  * to its value. And we don't want to invalidate the cache
387  * if this value change per viewport */
388 static void drw_viewport_var_init(void)
389 {
390         RegionView3D *rv3d = DST.draw_ctx.rv3d;
391         /* Refresh DST.size */
392         if (DST.viewport) {
393                 int size[2];
394                 GPU_viewport_size_get(DST.viewport, size);
395                 DST.size[0] = size[0];
396                 DST.size[1] = size[1];
397                 DST.inv_size[0] = 1.0f / size[0];
398                 DST.inv_size[1] = 1.0f / size[1];
399
400                 DefaultFramebufferList *fbl = (DefaultFramebufferList *)GPU_viewport_framebuffer_list_get(DST.viewport);
401                 DST.default_framebuffer = fbl->default_fb;
402
403                 DST.vmempool = GPU_viewport_mempool_get(DST.viewport);
404
405                 if (DST.vmempool->calls == NULL) {
406                         DST.vmempool->calls = BLI_mempool_create(sizeof(DRWCall), 0, 512, 0);
407                 }
408                 if (DST.vmempool->states == NULL) {
409                         DST.vmempool->states = BLI_mempool_create(sizeof(DRWCallState), 0, 512, BLI_MEMPOOL_ALLOW_ITER);
410                 }
411                 if (DST.vmempool->shgroups == NULL) {
412                         DST.vmempool->shgroups = BLI_mempool_create(sizeof(DRWShadingGroup), 0, 256, 0);
413                 }
414                 if (DST.vmempool->uniforms == NULL) {
415                         DST.vmempool->uniforms = BLI_mempool_create(sizeof(DRWUniform), 0, 512, 0);
416                 }
417                 if (DST.vmempool->passes == NULL) {
418                         DST.vmempool->passes = BLI_mempool_create(sizeof(DRWPass), 0, 64, 0);
419                 }
420
421                 DST.idatalist = GPU_viewport_instance_data_list_get(DST.viewport);
422                 DRW_instance_data_list_reset(DST.idatalist);
423         }
424         else {
425                 DST.size[0] = 0;
426                 DST.size[1] = 0;
427
428                 DST.inv_size[0] = 0;
429                 DST.inv_size[1] = 0;
430
431                 DST.default_framebuffer = NULL;
432                 DST.vmempool = NULL;
433         }
434
435         if (rv3d != NULL) {
436                 /* Refresh DST.screenvecs */
437                 copy_v3_v3(DST.screenvecs[0], rv3d->viewinv[0]);
438                 copy_v3_v3(DST.screenvecs[1], rv3d->viewinv[1]);
439                 normalize_v3(DST.screenvecs[0]);
440                 normalize_v3(DST.screenvecs[1]);
441
442                 /* Refresh DST.pixelsize */
443                 DST.pixsize = rv3d->pixsize;
444
445                 copy_m4_m4(DST.original_mat.mat[DRW_MAT_PERS], rv3d->persmat);
446                 copy_m4_m4(DST.original_mat.mat[DRW_MAT_PERSINV], rv3d->persinv);
447                 copy_m4_m4(DST.original_mat.mat[DRW_MAT_VIEW], rv3d->viewmat);
448                 copy_m4_m4(DST.original_mat.mat[DRW_MAT_VIEWINV], rv3d->viewinv);
449                 copy_m4_m4(DST.original_mat.mat[DRW_MAT_WIN], rv3d->winmat);
450                 invert_m4_m4(DST.original_mat.mat[DRW_MAT_WININV], rv3d->winmat);
451
452                 memcpy(DST.view_data.matstate.mat, DST.original_mat.mat, sizeof(DST.original_mat.mat));
453
454                 copy_v4_v4(DST.view_data.viewcamtexcofac, rv3d->viewcamtexcofac);
455         }
456         else {
457                 copy_v4_fl4(DST.view_data.viewcamtexcofac, 1.0f, 1.0f, 0.0f, 0.0f);
458         }
459
460         /* Reset facing */
461         DST.frontface = GL_CCW;
462         DST.backface = GL_CW;
463         glFrontFace(DST.frontface);
464
465         if (DST.draw_ctx.object_edit) {
466                 ED_view3d_init_mats_rv3d(DST.draw_ctx.object_edit, rv3d);
467         }
468
469         /* Alloc array of texture reference. */
470         if (DST.RST.bound_texs == NULL) {
471                 DST.RST.bound_texs = MEM_callocN(sizeof(GPUTexture *) * GPU_max_textures(), "Bound GPUTexture refs");
472         }
473         if (DST.RST.bound_tex_slots == NULL) {
474                 DST.RST.bound_tex_slots = MEM_callocN(sizeof(char) * GPU_max_textures(), "Bound Texture Slots");
475         }
476         if (DST.RST.bound_ubos == NULL) {
477                 DST.RST.bound_ubos = MEM_callocN(sizeof(GPUUniformBuffer *) * GPU_max_ubo_binds(), "Bound GPUUniformBuffer refs");
478         }
479         if (DST.RST.bound_ubo_slots == NULL) {
480                 DST.RST.bound_ubo_slots = MEM_callocN(sizeof(char) * GPU_max_ubo_binds(), "Bound Ubo Slots");
481         }
482
483         if (view_ubo == NULL) {
484                 view_ubo = DRW_uniformbuffer_create(sizeof(ViewUboStorage), NULL);
485         }
486
487         DST.override_mat = 0;
488         DST.dirty_mat = true;
489         DST.state_cache_id = 1;
490
491         DST.clipping.updated = false;
492
493         memset(DST.common_instance_data, 0x0, sizeof(DST.common_instance_data));
494 }
495
496 void DRW_viewport_matrix_get(float mat[4][4], DRWViewportMatrixType type)
497 {
498         BLI_assert(type >= 0 && type < DRW_MAT_COUNT);
499         BLI_assert(((DST.override_mat & (1 << type)) != 0)|| DST.draw_ctx.rv3d != NULL); /* Can't use this in render mode. */
500
501         copy_m4_m4(mat, DST.view_data.matstate.mat[type]);
502 }
503
504 void DRW_viewport_matrix_get_all(DRWMatrixState *state)
505 {
506         memcpy(state, DST.view_data.matstate.mat, sizeof(DRWMatrixState));
507 }
508
509 void DRW_viewport_matrix_override_set(const float mat[4][4], DRWViewportMatrixType type)
510 {
511         BLI_assert(type < DRW_MAT_COUNT);
512         copy_m4_m4(DST.view_data.matstate.mat[type], mat);
513         DST.override_mat |= (1 << type);
514         DST.dirty_mat = true;
515         DST.clipping.updated = false;
516 }
517
518 void DRW_viewport_matrix_override_unset(DRWViewportMatrixType type)
519 {
520         BLI_assert(type < DRW_MAT_COUNT);
521         copy_m4_m4(DST.view_data.matstate.mat[type], DST.original_mat.mat[type]);
522         DST.override_mat &= ~(1 << type);
523         DST.dirty_mat = true;
524         DST.clipping.updated = false;
525 }
526
527 void DRW_viewport_matrix_override_set_all(DRWMatrixState *state)
528 {
529         memcpy(DST.view_data.matstate.mat, state, sizeof(DRWMatrixState));
530         DST.override_mat = 0xFFFFFF;
531         DST.dirty_mat = true;
532         DST.clipping.updated = false;
533 }
534
535 void DRW_viewport_matrix_override_unset_all(void)
536 {
537         memcpy(DST.view_data.matstate.mat, DST.original_mat.mat, sizeof(DRWMatrixState));
538         DST.override_mat = 0;
539         DST.dirty_mat = true;
540         DST.clipping.updated = false;
541 }
542
543 bool DRW_viewport_is_persp_get(void)
544 {
545         RegionView3D *rv3d = DST.draw_ctx.rv3d;
546         if (rv3d) {
547                 return rv3d->is_persp;
548         }
549         else {
550                 return DST.view_data.matstate.mat[DRW_MAT_WIN][3][3] == 0.0f;
551         }
552         BLI_assert(0);
553         return false;
554 }
555
556 DefaultFramebufferList *DRW_viewport_framebuffer_list_get(void)
557 {
558         return GPU_viewport_framebuffer_list_get(DST.viewport);
559 }
560
561 DefaultTextureList *DRW_viewport_texture_list_get(void)
562 {
563         return GPU_viewport_texture_list_get(DST.viewport);
564 }
565
566 void DRW_viewport_request_redraw(void)
567 {
568         GPU_viewport_tag_update(DST.viewport);
569 }
570
571 /** \} */
572
573
574 /* -------------------------------------------------------------------- */
575 /** \name ViewLayers (DRW_scenelayer)
576  * \{ */
577
578 void *DRW_view_layer_engine_data_get(DrawEngineType *engine_type)
579 {
580         for (ViewLayerEngineData *sled = DST.draw_ctx.view_layer->drawdata.first; sled; sled = sled->next) {
581                 if (sled->engine_type == engine_type) {
582                         return sled->storage;
583                 }
584         }
585         return NULL;
586 }
587
588 void **DRW_view_layer_engine_data_ensure(DrawEngineType *engine_type, void (*callback)(void *storage))
589 {
590         ViewLayerEngineData *sled;
591
592         for (sled = DST.draw_ctx.view_layer->drawdata.first; sled; sled = sled->next) {
593                 if (sled->engine_type == engine_type) {
594                         return &sled->storage;
595                 }
596         }
597
598         sled = MEM_callocN(sizeof(ViewLayerEngineData), "ViewLayerEngineData");
599         sled->engine_type = engine_type;
600         sled->free = callback;
601         BLI_addtail(&DST.draw_ctx.view_layer->drawdata, sled);
602
603         return &sled->storage;
604 }
605
606 /** \} */
607
608
609 /* -------------------------------------------------------------------- */
610
611 /** \name Objects (DRW_object)
612  * \{ */
613
614 ObjectEngineData *DRW_object_engine_data_get(Object *ob, DrawEngineType *engine_type)
615 {
616         for (ObjectEngineData *oed = ob->drawdata.first; oed; oed = oed->next) {
617                 if (oed->engine_type == engine_type) {
618                         return oed;
619                 }
620         }
621         return NULL;
622 }
623
624 ObjectEngineData *DRW_object_engine_data_ensure(
625         Object *ob,
626         DrawEngineType *engine_type,
627         size_t size,
628         ObjectEngineDataInitCb init_cb,
629         ObjectEngineDataFreeCb free_cb)
630 {
631         BLI_assert(size >= sizeof(ObjectEngineData));
632         /* Try to re-use existing data. */
633         ObjectEngineData *oed = DRW_object_engine_data_get(ob, engine_type);
634         if (oed != NULL) {
635                 return oed;
636         }
637         /* Allocate new data. */
638         if ((ob->base_flag & BASE_FROMDUPLI) != 0) {
639                 /* NOTE: data is not persistent in this case. It is reset each redraw. */
640                 BLI_assert(free_cb == NULL); /* No callback allowed. */
641                 /* Round to sizeof(float) for DRW_instance_data_request(). */
642                 const size_t t = sizeof(float) - 1;
643                 size = (size + t) & ~t;
644                 size_t fsize = size / sizeof(float);
645                 if (DST.common_instance_data[fsize] == NULL) {
646                         DST.common_instance_data[fsize] = DRW_instance_data_request(DST.idatalist, fsize, 16);
647                 }
648                 oed = (ObjectEngineData *)DRW_instance_data_next(DST.common_instance_data[fsize]);
649                 memset(oed, 0, size);
650         }
651         else {
652                 oed = MEM_callocN(size, "ObjectEngineData");
653         }
654         oed->engine_type = engine_type;
655         oed->free = free_cb;
656         /* Perform user-side initialization, if needed. */
657         if (init_cb != NULL) {
658                 init_cb(oed);
659         }
660         /* Register in the list. */
661         BLI_addtail(&ob->drawdata, oed);
662         return oed;
663 }
664
665 /** \} */
666
667
668 /* -------------------------------------------------------------------- */
669
670 /** \name Rendering (DRW_engines)
671  * \{ */
672
673 static void drw_engines_init(void)
674 {
675         for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
676                 DrawEngineType *engine = link->data;
677                 ViewportEngineData *data = drw_viewport_engine_data_ensure(engine);
678                 PROFILE_START(stime);
679
680                 if (engine->engine_init) {
681                         engine->engine_init(data);
682                 }
683
684                 PROFILE_END_UPDATE(data->init_time, stime);
685         }
686 }
687
688 static void drw_engines_cache_init(void)
689 {
690         for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
691                 DrawEngineType *engine = link->data;
692                 ViewportEngineData *data = drw_viewport_engine_data_ensure(engine);
693
694                 if (data->text_draw_cache) {
695                         DRW_text_cache_destroy(data->text_draw_cache);
696                         data->text_draw_cache = NULL;
697                 }
698                 if (DST.text_store_p == NULL) {
699                         DST.text_store_p = &data->text_draw_cache;
700                 }
701
702                 if (engine->cache_init) {
703                         engine->cache_init(data);
704                 }
705         }
706 }
707
708 static void drw_engines_cache_populate(Object *ob)
709 {
710         DST.ob_state = NULL;
711
712         for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
713                 DrawEngineType *engine = link->data;
714                 ViewportEngineData *data = drw_viewport_engine_data_ensure(engine);
715
716                 if (engine->id_update) {
717                         engine->id_update(data, &ob->id);
718                 }
719
720                 if (engine->cache_populate) {
721                         engine->cache_populate(data, ob);
722                 }
723         }
724 }
725
726 static void drw_engines_cache_finish(void)
727 {
728         for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
729                 DrawEngineType *engine = link->data;
730                 ViewportEngineData *data = drw_viewport_engine_data_ensure(engine);
731
732                 if (engine->cache_finish) {
733                         engine->cache_finish(data);
734                 }
735         }
736 }
737
738 static void drw_engines_draw_background(void)
739 {
740         for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
741                 DrawEngineType *engine = link->data;
742                 ViewportEngineData *data = drw_viewport_engine_data_ensure(engine);
743
744                 if (engine->draw_background) {
745                         PROFILE_START(stime);
746
747                         DRW_stats_group_start(engine->idname);
748                         engine->draw_background(data);
749                         DRW_stats_group_end();
750
751                         PROFILE_END_UPDATE(data->background_time, stime);
752                         return;
753                 }
754         }
755
756         /* No draw_background found, doing default background */
757         if (DRW_state_draw_background()) {
758                 DRW_draw_background();
759         }
760 }
761
762 static void drw_engines_draw_scene(void)
763 {
764         for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
765                 DrawEngineType *engine = link->data;
766                 ViewportEngineData *data = drw_viewport_engine_data_ensure(engine);
767                 PROFILE_START(stime);
768
769                 if (engine->draw_scene) {
770                         DRW_stats_group_start(engine->idname);
771                         engine->draw_scene(data);
772                         /* Restore for next engine */
773                         if (DRW_state_is_fbo()) {
774                                 GPU_framebuffer_bind(DST.default_framebuffer);
775                         }
776                         DRW_stats_group_end();
777                 }
778
779                 PROFILE_END_UPDATE(data->render_time, stime);
780         }
781 }
782
783 static void drw_engines_draw_text(void)
784 {
785         for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
786                 DrawEngineType *engine = link->data;
787                 ViewportEngineData *data = drw_viewport_engine_data_ensure(engine);
788                 PROFILE_START(stime);
789
790                 if (data->text_draw_cache) {
791                         DRW_text_cache_draw(data->text_draw_cache, DST.draw_ctx.v3d, DST.draw_ctx.ar, false);
792                 }
793
794                 PROFILE_END_UPDATE(data->render_time, stime);
795         }
796 }
797
798 #define MAX_INFO_LINES 10
799
800 /**
801  * Returns the offset required for the drawing of engines info.
802  */
803 int DRW_draw_region_engine_info_offset(void)
804 {
805         int lines = 0;
806         for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
807                 DrawEngineType *engine = link->data;
808                 ViewportEngineData *data = drw_viewport_engine_data_ensure(engine);
809
810                 /* Count the number of lines. */
811                 if (data->info[0] != '\0') {
812                         lines++;
813                         char *c = data->info;
814                         while (*c++ != '\0') {
815                                 if (*c == '\n') {
816                                         lines++;
817                                 }
818                         }
819                 }
820         }
821         return MIN2(MAX_INFO_LINES, lines) * UI_UNIT_Y;
822 }
823
824 /**
825  * Actual drawing;
826  */
827 void DRW_draw_region_engine_info(void)
828 {
829         const char *info_array_final[MAX_INFO_LINES + 1];
830         /* This should be maxium number of engines running at the same time. */
831         char info_array[MAX_INFO_LINES][GPU_INFO_SIZE];
832         int i = 0;
833
834         const DRWContextState *draw_ctx = DRW_context_state_get();
835         ARegion *ar = draw_ctx->ar;
836         float fill_color[4] = {0.0f, 0.0f, 0.0f, 0.25f};
837
838         UI_GetThemeColor3fv(TH_HIGH_GRAD, fill_color);
839         mul_v3_fl(fill_color, fill_color[3]);
840
841         for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
842                 DrawEngineType *engine = link->data;
843                 ViewportEngineData *data = drw_viewport_engine_data_ensure(engine);
844
845                 if (data->info[0] != '\0') {
846                         char *chr_current = data->info;
847                         char *chr_start = chr_current;
848                         int line_len = 0;
849
850                         while (*chr_current++ != '\0') {
851                                 line_len++;
852                                 if (*chr_current == '\n') {
853                                         BLI_strncpy(info_array[i++], chr_start, line_len + 1);
854                                         /* Re-start counting. */
855                                         chr_start = chr_current + 1;
856                                         line_len = -1;
857                                 }
858                         }
859
860                         BLI_strncpy(info_array[i++], chr_start, line_len + 1);
861
862                         if (i >= MAX_INFO_LINES) {
863                                 break;
864                         }
865                 }
866         }
867
868         for (int j = 0; j < i; j++) {
869                 info_array_final[j] = info_array[j];
870         }
871         info_array_final[i] = NULL;
872
873         if (info_array[0] != NULL) {
874                 ED_region_info_draw_multiline(ar, info_array_final, fill_color, true);
875         }
876 }
877
878 #undef MAX_INFO_LINES
879
880 static void use_drw_engine(DrawEngineType *engine)
881 {
882         LinkData *ld = MEM_callocN(sizeof(LinkData), "enabled engine link data");
883         ld->data = engine;
884         BLI_addtail(&DST.enabled_engines, ld);
885 }
886
887 /**
888  * Use for external render engines.
889  */
890 static void drw_engines_enable_external(void)
891 {
892         use_drw_engine(DRW_engine_viewport_external_type.draw_engine);
893 }
894
895 /* TODO revisit this when proper layering is implemented */
896 /* Gather all draw engines needed and store them in DST.enabled_engines
897  * That also define the rendering order of engines */
898 static void drw_engines_enable_from_engine(RenderEngineType *engine_type, int drawtype,
899                                            int UNUSED(drawtype_wireframe), int drawtype_solid, int UNUSED(drawtype_texture))
900 {
901         switch (drawtype) {
902                 case OB_WIRE:
903                         break;
904
905                 case OB_SOLID:
906                         if (drawtype_solid == OB_LIGHTING_FLAT) {
907                                 use_drw_engine(&draw_engine_workbench_solid_flat);
908
909                         } else if (drawtype_solid == OB_LIGHTING_STUDIO) {
910                                 use_drw_engine(&draw_engine_workbench_solid_studio);
911
912                         }
913                         break;
914
915                 case OB_TEXTURE:
916                 case OB_MATERIAL:
917                 case OB_RENDER:
918                 default:
919                         /* TODO layers */
920                         if (engine_type->draw_engine != NULL) {
921                                 use_drw_engine(engine_type->draw_engine);
922                         }
923
924                         if ((engine_type->flag & RE_INTERNAL) == 0) {
925                                 drw_engines_enable_external();
926                         }
927                         break;
928         }
929 }
930
931 static void drw_engines_enable_from_object_mode(void)
932 {
933         use_drw_engine(&draw_engine_object_type);
934 }
935
936 static void drw_engines_enable_from_mode(int mode)
937 {
938         switch (mode) {
939                 case CTX_MODE_EDIT_MESH:
940                         use_drw_engine(&draw_engine_edit_mesh_type);
941                         break;
942                 case CTX_MODE_EDIT_CURVE:
943                         use_drw_engine(&draw_engine_edit_curve_type);
944                         break;
945                 case CTX_MODE_EDIT_SURFACE:
946                         use_drw_engine(&draw_engine_edit_surface_type);
947                         break;
948                 case CTX_MODE_EDIT_TEXT:
949                         use_drw_engine(&draw_engine_edit_text_type);
950                         break;
951                 case CTX_MODE_EDIT_ARMATURE:
952                         use_drw_engine(&draw_engine_edit_armature_type);
953                         break;
954                 case CTX_MODE_EDIT_METABALL:
955                         use_drw_engine(&draw_engine_edit_metaball_type);
956                         break;
957                 case CTX_MODE_EDIT_LATTICE:
958                         use_drw_engine(&draw_engine_edit_lattice_type);
959                         break;
960                 case CTX_MODE_POSE:
961                         use_drw_engine(&draw_engine_pose_type);
962                         break;
963                 case CTX_MODE_SCULPT:
964                         use_drw_engine(&draw_engine_sculpt_type);
965                         break;
966                 case CTX_MODE_PAINT_WEIGHT:
967                         use_drw_engine(&draw_engine_pose_type);
968                         use_drw_engine(&draw_engine_paint_weight_type);
969                         break;
970                 case CTX_MODE_PAINT_VERTEX:
971                         use_drw_engine(&draw_engine_paint_vertex_type);
972                         break;
973                 case CTX_MODE_PAINT_TEXTURE:
974                         use_drw_engine(&draw_engine_paint_texture_type);
975                         break;
976                 case CTX_MODE_PARTICLE:
977                         use_drw_engine(&draw_engine_particle_type);
978                         break;
979                 case CTX_MODE_OBJECT:
980                         break;
981                 default:
982                         BLI_assert(!"Draw mode invalid");
983                         break;
984         }
985 }
986
987 /**
988  * Use for select and depth-drawing.
989  */
990 static void drw_engines_enable_basic(void)
991 {
992         use_drw_engine(DRW_engine_viewport_basic_type.draw_engine);
993 }
994
995 static void drw_engines_enable(ViewLayer *view_layer, RenderEngineType *engine_type)
996 {
997         Object *obact = OBACT(view_layer);
998         const int mode = CTX_data_mode_enum_ex(DST.draw_ctx.object_edit, obact, DST.draw_ctx.object_mode);
999         View3D * v3d = DST.draw_ctx.v3d;
1000         const int drawtype = v3d->drawtype;
1001         const int drawtype_wireframe = v3d->drawtype_wireframe;
1002         const int drawtype_solid = v3d->drawtype_solid;
1003         const int drawtype_texture = v3d->drawtype_texture;
1004
1005         drw_engines_enable_from_engine(engine_type, drawtype, drawtype_wireframe, drawtype_solid, drawtype_texture);
1006
1007         if (DRW_state_draw_support()) {
1008                 drw_engines_enable_from_object_mode();
1009                 drw_engines_enable_from_mode(mode);
1010         }
1011 }
1012
1013 static void drw_engines_disable(void)
1014 {
1015         BLI_freelistN(&DST.enabled_engines);
1016 }
1017
1018 static unsigned int DRW_engines_get_hash(void)
1019 {
1020         unsigned int hash = 0;
1021         /* The cache depends on enabled engines */
1022         /* FIXME : if collision occurs ... segfault */
1023         for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
1024                 DrawEngineType *engine = link->data;
1025                 hash += BLI_ghashutil_strhash_p(engine->idname);
1026         }
1027
1028         return hash;
1029 }
1030
1031 /* -------------------------------------------------------------------- */
1032
1033 /** \name View Update
1034  * \{ */
1035
1036 void DRW_notify_view_update(const DRWUpdateContext *update_ctx)
1037 {
1038         RenderEngineType *engine_type = update_ctx->engine_type;
1039         ARegion *ar = update_ctx->ar;
1040         View3D *v3d = update_ctx->v3d;
1041         RegionView3D *rv3d = ar->regiondata;
1042         Depsgraph *depsgraph = update_ctx->depsgraph;
1043         Scene *scene = update_ctx->scene;
1044         ViewLayer *view_layer = update_ctx->view_layer;
1045
1046         if (rv3d->viewport == NULL) {
1047                 return;
1048         }
1049
1050         /* XXX Really nasty locking. But else this could
1051          * be executed by the material previews thread
1052          * while rendering a viewport. */
1053         BLI_mutex_lock(&DST.ogl_context_mutex);
1054
1055         /* Reset before using it. */
1056         drw_state_prepare_clean_for_draw(&DST);
1057
1058         DST.viewport = rv3d->viewport;
1059         DST.draw_ctx = (DRWContextState){
1060                 .ar = ar, .rv3d = rv3d, .v3d = v3d,
1061                 .scene = scene, .view_layer = view_layer, .obact = OBACT(view_layer),
1062                 .engine_type = engine_type,
1063                 .depsgraph = depsgraph, .object_mode = OB_MODE_OBJECT,
1064         };
1065
1066         drw_engines_enable(view_layer, engine_type);
1067
1068         for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
1069                 DrawEngineType *draw_engine = link->data;
1070                 ViewportEngineData *data = drw_viewport_engine_data_ensure(draw_engine);
1071
1072                 if (draw_engine->view_update) {
1073                         draw_engine->view_update(data);
1074                 }
1075         }
1076
1077         DST.viewport = NULL;
1078
1079         drw_engines_disable();
1080
1081         BLI_mutex_unlock(&DST.ogl_context_mutex);
1082 }
1083
1084 /** \} */
1085
1086 /** \name ID Update
1087  * \{ */
1088
1089 /* TODO(sergey): This code is run for each changed ID (including the ones which
1090  * are changed indirectly via update flush. Need to find a way to make this to
1091  * run really fast, hopefully without any memory allocations on a heap
1092  * Idea here could be to run every known engine's id_update() and make them
1093  * do nothing if there is no engine-specific data yet.
1094  */
1095 void DRW_notify_id_update(const DRWUpdateContext *update_ctx, ID *id)
1096 {
1097         RenderEngineType *engine_type = update_ctx->engine_type;
1098         ARegion *ar = update_ctx->ar;
1099         View3D *v3d = update_ctx->v3d;
1100         RegionView3D *rv3d = ar->regiondata;
1101         Depsgraph *depsgraph = update_ctx->depsgraph;
1102         Scene *scene = update_ctx->scene;
1103         ViewLayer *view_layer = update_ctx->view_layer;
1104         if (rv3d->viewport == NULL) {
1105                 return;
1106         }
1107         /* Reset before using it. */
1108         drw_state_prepare_clean_for_draw(&DST);
1109         DST.viewport = rv3d->viewport;
1110         DST.draw_ctx = (DRWContextState){
1111                 .ar = ar, .rv3d = rv3d, .v3d = v3d,
1112                 .scene = scene, .view_layer = view_layer, .obact = OBACT(view_layer),
1113                 .engine_type = engine_type,
1114                 .depsgraph = depsgraph, .object_mode = OB_MODE_OBJECT,
1115         };
1116         drw_engines_enable(view_layer, engine_type);
1117         for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
1118                 DrawEngineType *draw_engine = link->data;
1119                 ViewportEngineData *data = drw_viewport_engine_data_ensure(draw_engine);
1120                 if (draw_engine->id_update) {
1121                         draw_engine->id_update(data, id);
1122                 }
1123         }
1124         DST.viewport = NULL;
1125         drw_engines_disable();
1126 }
1127
1128 /** \} */
1129
1130 /* -------------------------------------------------------------------- */
1131
1132 /** \name Main Draw Loops (DRW_draw)
1133  * \{ */
1134
1135 /* Everything starts here.
1136  * This function takes care of calling all cache and rendering functions
1137  * for each relevant engine / mode engine. */
1138 void DRW_draw_view(const bContext *C)
1139 {
1140         Depsgraph *depsgraph = CTX_data_depsgraph(C);
1141         ARegion *ar = CTX_wm_region(C);
1142         View3D *v3d = CTX_wm_view3d(C);
1143         Scene *scene = DEG_get_evaluated_scene(depsgraph);
1144         RenderEngineType *engine_type = ED_view3d_engine_type(scene, v3d->drawtype);
1145
1146         /* Reset before using it. */
1147         drw_state_prepare_clean_for_draw(&DST);
1148         DRW_draw_render_loop_ex(depsgraph, engine_type, ar, v3d, C);
1149 }
1150
1151 /**
1152  * Used for both regular and off-screen drawing.
1153  * Need to reset DST before calling this function
1154  */
1155 void DRW_draw_render_loop_ex(
1156         struct Depsgraph *depsgraph,
1157         RenderEngineType *engine_type,
1158         ARegion *ar, View3D *v3d,
1159         const bContext *evil_C)
1160 {
1161
1162         Scene *scene = DEG_get_evaluated_scene(depsgraph);
1163         ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph);
1164         RegionView3D *rv3d = ar->regiondata;
1165
1166         DST.draw_ctx.evil_C = evil_C;
1167
1168         DST.viewport = rv3d->viewport;
1169         v3d->zbuf = true;
1170
1171         /* Setup viewport */
1172         GPU_viewport_engines_data_validate(DST.viewport, DRW_engines_get_hash());
1173
1174         DST.draw_ctx = (DRWContextState){
1175             .ar = ar, .rv3d = rv3d, .v3d = v3d,
1176             .scene = scene, .view_layer = view_layer, .obact = OBACT(view_layer),
1177             .engine_type = engine_type,
1178             .depsgraph = depsgraph,
1179
1180             /* reuse if caller sets */
1181             .evil_C = DST.draw_ctx.evil_C,
1182         };
1183         drw_context_state_init();
1184         drw_viewport_var_init();
1185
1186         /* Get list of enabled engines */
1187         drw_engines_enable(view_layer, engine_type);
1188
1189         /* Update ubos */
1190         DRW_globals_update();
1191
1192         /* No framebuffer allowed before drawing. */
1193         BLI_assert(GPU_framebuffer_current_get() == 0);
1194
1195         /* Init engines */
1196         drw_engines_init();
1197
1198         /* Cache filling */
1199         {
1200                 PROFILE_START(stime);
1201                 drw_engines_cache_init();
1202
1203                 DEG_OBJECT_ITER_FOR_RENDER_ENGINE_BEGIN(depsgraph, ob, DRW_iterator_mode_get())
1204                 {
1205                         drw_engines_cache_populate(ob);
1206                 }
1207                 DEG_OBJECT_ITER_FOR_RENDER_ENGINE_END;
1208
1209                 drw_engines_cache_finish();
1210
1211                 DRW_render_instance_buffer_finish();
1212
1213 #ifdef USE_PROFILE
1214                 double *cache_time = GPU_viewport_cache_time_get(DST.viewport);
1215                 PROFILE_END_UPDATE(*cache_time, stime);
1216 #endif
1217         }
1218
1219         DRW_stats_begin();
1220
1221         GPU_framebuffer_bind(DST.default_framebuffer);
1222
1223         /* Start Drawing */
1224         DRW_state_reset();
1225
1226         drw_engines_draw_background();
1227
1228         /* WIP, single image drawn over the camera view (replace) */
1229         bool do_bg_image = false;
1230         if (rv3d->persp == RV3D_CAMOB) {
1231                 Object *cam_ob = v3d->camera;
1232                 if (cam_ob && cam_ob->type == OB_CAMERA) {
1233                         Camera *cam = cam_ob->data;
1234                         if (!BLI_listbase_is_empty(&cam->bg_images)) {
1235                                 do_bg_image = true;
1236                         }
1237                 }
1238         }
1239
1240         if (do_bg_image) {
1241                 ED_view3d_draw_bgpic_test(scene, depsgraph, ar, v3d, false, true);
1242         }
1243
1244
1245         DRW_draw_callbacks_pre_scene();
1246         if (DST.draw_ctx.evil_C) {
1247                 ED_region_draw_cb_draw(DST.draw_ctx.evil_C, DST.draw_ctx.ar, REGION_DRAW_PRE_VIEW);
1248         }
1249
1250         drw_engines_draw_scene();
1251
1252         DRW_draw_callbacks_post_scene();
1253         if (DST.draw_ctx.evil_C) {
1254                 ED_region_draw_cb_draw(DST.draw_ctx.evil_C, DST.draw_ctx.ar, REGION_DRAW_POST_VIEW);
1255         }
1256
1257         DRW_state_reset();
1258
1259         drw_engines_draw_text();
1260
1261         if (DST.draw_ctx.evil_C) {
1262                 /* needed so manipulator isn't obscured */
1263                 glDisable(GL_DEPTH_TEST);
1264                 DRW_draw_manipulator_3d();
1265
1266                 DRW_draw_region_info();
1267
1268                 /* Draw 2D after region info so we can draw on top of the camera passepartout overlay.
1269                  * 'DRW_draw_region_info' sets the projection in pixel-space. */
1270                 DRW_draw_manipulator_2d();
1271                 glEnable(GL_DEPTH_TEST);
1272         }
1273
1274         DRW_stats_reset();
1275
1276         if (do_bg_image) {
1277                 ED_view3d_draw_bgpic_test(scene, depsgraph, ar, v3d, true, true);
1278         }
1279
1280         if (G.debug_value > 20) {
1281                 glDisable(GL_DEPTH_TEST);
1282                 rcti rect; /* local coordinate visible rect inside region, to accomodate overlapping ui */
1283                 ED_region_visible_rect(DST.draw_ctx.ar, &rect);
1284                 DRW_stats_draw(&rect);
1285                 glEnable(GL_DEPTH_TEST);
1286         }
1287
1288         GPU_framebuffer_restore();
1289
1290         DRW_state_reset();
1291         drw_engines_disable();
1292
1293         drw_viewport_cache_resize();
1294
1295 #ifdef DEBUG
1296         /* Avoid accidental reuse. */
1297         drw_state_ensure_not_reused(&DST);
1298 #endif
1299 }
1300
1301 void DRW_draw_render_loop(
1302         struct Depsgraph *depsgraph,
1303         ARegion *ar, View3D *v3d)
1304 {
1305         /* Reset before using it. */
1306         drw_state_prepare_clean_for_draw(&DST);
1307
1308         Scene *scene = DEG_get_evaluated_scene(depsgraph);
1309         RenderEngineType *engine_type = ED_view3d_engine_type(scene, v3d->drawtype);
1310
1311         DRW_draw_render_loop_ex(depsgraph, engine_type, ar, v3d, NULL);
1312 }
1313
1314 /* @viewport CAN be NULL, in this case we create one. */
1315 void DRW_draw_render_loop_offscreen(
1316         struct Depsgraph *depsgraph, RenderEngineType *engine_type,
1317         ARegion *ar, View3D *v3d,
1318         const bool draw_background, GPUOffScreen *ofs,
1319         GPUViewport *viewport)
1320 {
1321         RegionView3D *rv3d = ar->regiondata;
1322
1323         /* backup */
1324         void *backup_viewport = rv3d->viewport;
1325         {
1326                 /* backup (_never_ use rv3d->viewport) */
1327                 if (viewport == NULL) {
1328                         rv3d->viewport = GPU_viewport_create_from_offscreen(ofs);
1329                 }
1330                 else {
1331                         rv3d->viewport = viewport;
1332                 }
1333         }
1334
1335         GPU_framebuffer_restore();
1336
1337         /* Reset before using it. */
1338         drw_state_prepare_clean_for_draw(&DST);
1339         DST.options.is_image_render = true;
1340         DST.options.draw_background = draw_background;
1341         DRW_draw_render_loop_ex(depsgraph, engine_type, ar, v3d, NULL);
1342
1343         /* restore */
1344         {
1345                 if (viewport == NULL) {
1346                         /* don't free data owned by 'ofs' */
1347                         GPU_viewport_clear_from_offscreen(rv3d->viewport);
1348                         GPU_viewport_free(rv3d->viewport);
1349                 }
1350
1351                 rv3d->viewport = backup_viewport;
1352         }
1353
1354         /* we need to re-bind (annoying!) */
1355         GPU_offscreen_bind(ofs, false);
1356 }
1357
1358 void DRW_render_to_image(RenderEngine *engine, struct Depsgraph *depsgraph)
1359 {
1360         Scene *scene = DEG_get_evaluated_scene(depsgraph);
1361         ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph);
1362         RenderEngineType *engine_type = engine->type;
1363         DrawEngineType *draw_engine_type = engine_type->draw_engine;
1364         RenderData *r = &scene->r;
1365         Render *render = engine->re;
1366         /* Changing Context */
1367         DRW_opengl_context_enable();
1368         /* IMPORTANT: We dont support immediate mode in render mode!
1369          * This shall remain in effect until immediate mode supports
1370          * multiple threads. */
1371
1372         /* Reset before using it. */
1373         drw_state_prepare_clean_for_draw(&DST);
1374         DST.options.is_image_render = true;
1375         DST.options.is_scene_render = true;
1376         DST.options.draw_background = scene->r.alphamode == R_ADDSKY;
1377
1378         DST.draw_ctx = (DRWContextState){
1379             .scene = scene, .view_layer = view_layer,
1380             .engine_type = engine_type,
1381             .depsgraph = depsgraph, .object_mode = OB_MODE_OBJECT,
1382         };
1383         drw_context_state_init();
1384
1385         DST.viewport = GPU_viewport_create();
1386         const int size[2] = {(r->size * r->xsch) / 100, (r->size * r->ysch) / 100};
1387         GPU_viewport_size_set(DST.viewport, size);
1388
1389         drw_viewport_var_init();
1390
1391         ViewportEngineData *data = drw_viewport_engine_data_ensure(draw_engine_type);
1392
1393         /* set default viewport */
1394         glViewport(0, 0, size[0], size[1]);
1395
1396         /* Main rendering. */
1397         rctf view_rect;
1398         rcti render_rect;
1399         RE_GetViewPlane(render, &view_rect, &render_rect);
1400         if (BLI_rcti_is_empty(&render_rect)) {
1401                 BLI_rcti_init(&render_rect, 0, size[0], 0, size[1]);
1402         }
1403
1404         /* Init render result. */
1405         RenderResult *render_result = RE_engine_begin_result(
1406                 engine,
1407                 0,
1408                 0,
1409                 (int)size[0],
1410                 (int)size[1],
1411                 view_layer->name,
1412                 /* RR_ALL_VIEWS */ NULL);
1413
1414         RenderLayer *render_layer = render_result->layers.first;
1415         for (RenderView *render_view = render_result->views.first;
1416              render_view != NULL;
1417              render_view = render_view->next)
1418         {
1419                 RE_SetActiveRenderView(render, render_view->name);
1420                 engine_type->draw_engine->render_to_image(data, engine, render_layer, &render_rect);
1421                 DST.buffer_finish_called = false;
1422         }
1423
1424         RE_engine_end_result(engine, render_result, false, false, false);
1425
1426         /* Force cache to reset. */
1427         drw_viewport_cache_resize();
1428
1429         /* TODO grease pencil */
1430
1431         GPU_viewport_free(DST.viewport);
1432         GPU_framebuffer_restore();
1433
1434         /* Changing Context */
1435         DRW_opengl_context_disable();
1436
1437 #ifdef DEBUG
1438         /* Avoid accidental reuse. */
1439         drw_state_ensure_not_reused(&DST);
1440 #endif
1441 }
1442
1443 void DRW_render_object_iter(
1444         void *vedata, RenderEngine *engine, struct Depsgraph *depsgraph,
1445         void (*callback)(void *vedata, Object *ob, RenderEngine *engine, struct Depsgraph *depsgraph))
1446 {
1447         DEG_OBJECT_ITER_FOR_RENDER_ENGINE_BEGIN(depsgraph, ob, DRW_iterator_mode_get())
1448         {
1449                 DST.ob_state = NULL;
1450                 callback(vedata, ob, engine, depsgraph);
1451         }
1452         DEG_OBJECT_ITER_FOR_RENDER_ENGINE_END
1453 }
1454
1455 static struct DRWSelectBuffer {
1456         struct GPUFrameBuffer *framebuffer;
1457         struct GPUTexture *texture_depth;
1458 } g_select_buffer = {NULL};
1459
1460 static void draw_select_framebuffer_setup(const rcti *rect)
1461 {
1462         if (g_select_buffer.framebuffer == NULL) {
1463                 g_select_buffer.framebuffer = GPU_framebuffer_create();
1464         }
1465
1466         /* If size mismatch recreate the texture. */
1467         if ((g_select_buffer.texture_depth != NULL) &&
1468                 ((GPU_texture_width(g_select_buffer.texture_depth) != BLI_rcti_size_x(rect)) ||
1469                 (GPU_texture_height(g_select_buffer.texture_depth) != BLI_rcti_size_y(rect))))
1470         {
1471                 GPU_texture_free(g_select_buffer.texture_depth);
1472                 g_select_buffer.texture_depth = NULL;
1473         }
1474
1475         if (g_select_buffer.texture_depth == NULL) {
1476                 g_select_buffer.texture_depth = GPU_texture_create_depth(BLI_rcti_size_x(rect), BLI_rcti_size_y(rect), NULL);
1477
1478                 GPU_framebuffer_texture_attach(g_select_buffer.framebuffer, g_select_buffer.texture_depth, 0, 0);
1479
1480                 if (!GPU_framebuffer_check_valid(g_select_buffer.framebuffer, NULL)) {
1481                         printf("Error invalid selection framebuffer\n");
1482                 }
1483         }
1484 }
1485
1486 /* Must run after all instance datas have been added. */
1487 void DRW_render_instance_buffer_finish(void)
1488 {
1489         BLI_assert(!DST.buffer_finish_called && "DRW_render_instance_buffer_finish called twice!");
1490         DST.buffer_finish_called = true;
1491         DRW_instance_buffer_finish(DST.idatalist);
1492 }
1493
1494 /**
1495  * object mode select-loop, see: ED_view3d_draw_select_loop (legacy drawing).
1496  */
1497 void DRW_draw_select_loop(
1498         struct Depsgraph *depsgraph,
1499         ARegion *ar, View3D *v3d,
1500         bool UNUSED(use_obedit_skip), bool UNUSED(use_nearest), const rcti *rect,
1501         DRW_SelectPassFn select_pass_fn, void *select_pass_user_data)
1502 {
1503         Scene *scene = DEG_get_evaluated_scene(depsgraph);
1504         RenderEngineType *engine_type = ED_view3d_engine_type(scene, v3d->drawtype);
1505         ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph);
1506         Object *obact = OBACT(view_layer);
1507         Object *obedit = OBEDIT_FROM_OBACT(obact);
1508 #ifndef USE_GPU_SELECT
1509         UNUSED_VARS(vc, scene, view_layer, v3d, ar, rect);
1510 #else
1511         RegionView3D *rv3d = ar->regiondata;
1512
1513         /* Reset before using it. */
1514         drw_state_prepare_clean_for_draw(&DST);
1515
1516         /* backup (_never_ use rv3d->viewport) */
1517         void *backup_viewport = rv3d->viewport;
1518         rv3d->viewport = NULL;
1519
1520         bool use_obedit = false;
1521         int obedit_mode = 0;
1522         if (obedit != NULL) {
1523                 if (obedit->type == OB_MBALL) {
1524                         use_obedit = true;
1525                         obedit_mode = CTX_MODE_EDIT_METABALL;
1526                 }
1527                 else if (obedit->type == OB_ARMATURE) {
1528                         use_obedit = true;
1529                         obedit_mode = CTX_MODE_EDIT_ARMATURE;
1530                 }
1531         }
1532
1533         struct GPUViewport *viewport = GPU_viewport_create();
1534         GPU_viewport_size_set(viewport, (const int[2]){BLI_rcti_size_x(rect), BLI_rcti_size_y(rect)});
1535
1536         DST.viewport = viewport;
1537         v3d->zbuf = true;
1538
1539         DST.options.is_select = true;
1540
1541         /* Get list of enabled engines */
1542         if (use_obedit) {
1543                 drw_engines_enable_from_mode(obedit_mode);
1544         }
1545         else {
1546                 drw_engines_enable_basic();
1547                 drw_engines_enable_from_object_mode();
1548         }
1549
1550         /* Setup viewport */
1551
1552         /* Instead of 'DRW_context_state_init(C, &DST.draw_ctx)', assign from args */
1553         DST.draw_ctx = (DRWContextState){
1554                 .ar = ar, .rv3d = rv3d, .v3d = v3d,
1555                 .scene = scene, .view_layer = view_layer, .obact = obact,
1556                 .engine_type = engine_type,
1557                 .depsgraph = depsgraph,
1558         };
1559         drw_context_state_init();
1560         drw_viewport_var_init();
1561
1562         /* Update ubos */
1563         DRW_globals_update();
1564
1565         /* Init engines */
1566         drw_engines_init();
1567
1568         {
1569                 drw_engines_cache_init();
1570
1571                 if (use_obedit) {
1572 #if 0
1573                         drw_engines_cache_populate(obact);
1574 #else
1575                         FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, obact->mode, ob_iter) {
1576                                 drw_engines_cache_populate(ob_iter);
1577                         }
1578                         FOREACH_OBJECT_IN_MODE_END;
1579 #endif
1580                 }
1581                 else {
1582                         DEG_OBJECT_ITER_BEGIN(
1583                                 depsgraph, ob, DRW_iterator_mode_get(),
1584                                 DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY |
1585                                 DEG_ITER_OBJECT_FLAG_VISIBLE |
1586                                 DEG_ITER_OBJECT_FLAG_DUPLI)
1587                         {
1588                                 if ((ob->base_flag & BASE_SELECTABLED) != 0) {
1589                                         DRW_select_load_id(ob->select_color);
1590                                         drw_engines_cache_populate(ob);
1591                                 }
1592                         }
1593                         DEG_OBJECT_ITER_END;
1594                 }
1595
1596                 drw_engines_cache_finish();
1597
1598                 DRW_render_instance_buffer_finish();
1599         }
1600
1601         /* Setup framebuffer */
1602         draw_select_framebuffer_setup(rect);
1603         GPU_framebuffer_bind(g_select_buffer.framebuffer);
1604         GPU_framebuffer_clear_depth(g_select_buffer.framebuffer, 1.0f);
1605
1606         /* Start Drawing */
1607         DRW_state_reset();
1608         DRW_draw_callbacks_pre_scene();
1609
1610         DRW_state_lock(
1611                 DRW_STATE_WRITE_DEPTH |
1612                 DRW_STATE_DEPTH_ALWAYS |
1613                 DRW_STATE_DEPTH_LESS |
1614                 DRW_STATE_DEPTH_EQUAL |
1615                 DRW_STATE_DEPTH_GREATER |
1616                 DRW_STATE_DEPTH_ALWAYS);
1617
1618         /* Only 1-2 passes. */
1619         while (true) {
1620                 if (!select_pass_fn(DRW_SELECT_PASS_PRE, select_pass_user_data)) {
1621                         break;
1622                 }
1623
1624                 drw_engines_draw_scene();
1625
1626                 if (!select_pass_fn(DRW_SELECT_PASS_POST, select_pass_user_data)) {
1627                         break;
1628                 }
1629         }
1630
1631         DRW_state_lock(0);
1632
1633         DRW_draw_callbacks_post_scene();
1634
1635         DRW_state_reset();
1636         drw_engines_disable();
1637
1638 #ifdef DEBUG
1639         /* Avoid accidental reuse. */
1640         drw_state_ensure_not_reused(&DST);
1641 #endif
1642         GPU_framebuffer_restore();
1643
1644         /* Cleanup for selection state */
1645         GPU_viewport_free(viewport);
1646
1647         /* restore */
1648         rv3d->viewport = backup_viewport;
1649 #endif  /* USE_GPU_SELECT */
1650 }
1651
1652 static void draw_depth_texture_to_screen(GPUTexture *texture)
1653 {
1654         const float w = (float)GPU_texture_width(texture);
1655         const float h = (float)GPU_texture_height(texture);
1656
1657         Gwn_VertFormat *format = immVertexFormat();
1658         unsigned int texcoord = GWN_vertformat_attr_add(format, "texCoord", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
1659         unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
1660
1661         immBindBuiltinProgram(GPU_SHADER_3D_IMAGE_DEPTH_COPY);
1662
1663         GPU_texture_bind(texture, 0);
1664
1665         immUniform1i("image", 0); /* default GL_TEXTURE0 unit */
1666
1667         immBegin(GWN_PRIM_TRI_STRIP, 4);
1668
1669         immAttrib2f(texcoord, 0.0f, 0.0f);
1670         immVertex2f(pos, 0.0f, 0.0f);
1671
1672         immAttrib2f(texcoord, 1.0f, 0.0f);
1673         immVertex2f(pos, w, 0.0f);
1674
1675         immAttrib2f(texcoord, 0.0f, 1.0f);
1676         immVertex2f(pos, 0.0f, h);
1677
1678         immAttrib2f(texcoord, 1.0f, 1.0f);
1679         immVertex2f(pos, w, h);
1680
1681         immEnd();
1682
1683         GPU_texture_unbind(texture);
1684
1685         immUnbindProgram();
1686 }
1687
1688 /**
1689  * object mode select-loop, see: ED_view3d_draw_depth_loop (legacy drawing).
1690  */
1691 void DRW_draw_depth_loop(
1692         Depsgraph *depsgraph,
1693         ARegion *ar, View3D *v3d)
1694 {
1695         Scene *scene = DEG_get_evaluated_scene(depsgraph);
1696         RenderEngineType *engine_type = ED_view3d_engine_type(scene, v3d->drawtype);
1697         ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph);
1698         RegionView3D *rv3d = ar->regiondata;
1699
1700         DRW_opengl_context_enable();
1701
1702         /* backup (_never_ use rv3d->viewport) */
1703         void *backup_viewport = rv3d->viewport;
1704         rv3d->viewport = NULL;
1705
1706         /* Reset before using it. */
1707         drw_state_prepare_clean_for_draw(&DST);
1708
1709         struct GPUViewport *viewport = GPU_viewport_create();
1710         GPU_viewport_size_set(viewport, (const int[2]){ar->winx, ar->winy});
1711
1712         /* Setup framebuffer */
1713         draw_select_framebuffer_setup(&ar->winrct);
1714         GPU_framebuffer_bind(g_select_buffer.framebuffer);
1715         GPU_framebuffer_clear_depth(g_select_buffer.framebuffer, 1.0f);
1716
1717         bool cache_is_dirty;
1718         DST.viewport = viewport;
1719         v3d->zbuf = true;
1720
1721         DST.options.is_depth = true;
1722
1723         /* Get list of enabled engines */
1724         {
1725                 drw_engines_enable_basic();
1726                 drw_engines_enable_from_object_mode();
1727         }
1728
1729         /* Setup viewport */
1730         cache_is_dirty = true;
1731
1732         /* Instead of 'DRW_context_state_init(C, &DST.draw_ctx)', assign from args */
1733         DST.draw_ctx = (DRWContextState){
1734                 .ar = ar, .rv3d = rv3d, .v3d = v3d,
1735                 .scene = scene, .view_layer = view_layer, .obact = OBACT(view_layer),
1736                 .engine_type = engine_type,
1737                 .depsgraph = depsgraph,
1738         };
1739         drw_context_state_init();
1740         drw_viewport_var_init();
1741
1742         /* Update ubos */
1743         DRW_globals_update();
1744
1745         /* Init engines */
1746         drw_engines_init();
1747
1748         /* TODO : tag to refresh by the dependency graph */
1749         /* ideally only refresh when objects are added/removed */
1750         /* or render properties / materials change */
1751         if (cache_is_dirty) {
1752                 drw_engines_cache_init();
1753
1754                 DEG_OBJECT_ITER_FOR_RENDER_ENGINE_BEGIN(depsgraph, ob, DRW_iterator_mode_get())
1755                 {
1756                         drw_engines_cache_populate(ob);
1757                 }
1758                 DEG_OBJECT_ITER_FOR_RENDER_ENGINE_END;
1759
1760                 drw_engines_cache_finish();
1761
1762                 DRW_render_instance_buffer_finish();
1763         }
1764
1765         /* Start Drawing */
1766         DRW_state_reset();
1767         DRW_draw_callbacks_pre_scene();
1768         drw_engines_draw_scene();
1769         DRW_draw_callbacks_post_scene();
1770
1771         DRW_state_reset();
1772         drw_engines_disable();
1773
1774 #ifdef DEBUG
1775         /* Avoid accidental reuse. */
1776         drw_state_ensure_not_reused(&DST);
1777 #endif
1778
1779         /* TODO: Reading depth for operators should be done here. */
1780
1781         GPU_framebuffer_restore();
1782
1783         /* Cleanup for selection state */
1784         GPU_viewport_free(viewport);
1785
1786         /* Changin context */
1787         DRW_opengl_context_disable();
1788
1789         /* XXX Drawing the resulting buffer to the BACK_BUFFER */
1790         gpuPushMatrix();
1791         gpuPushProjectionMatrix();
1792         wmOrtho2_region_pixelspace(ar);
1793         gpuLoadIdentity();
1794
1795         glEnable(GL_DEPTH_TEST); /* Cannot write to depth buffer without testing */
1796         glDepthFunc(GL_ALWAYS);
1797         draw_depth_texture_to_screen(g_select_buffer.texture_depth);
1798         glDepthFunc(GL_LEQUAL);
1799
1800         gpuPopMatrix();
1801         gpuPopProjectionMatrix();
1802
1803         /* restore */
1804         rv3d->viewport = backup_viewport;
1805 }
1806
1807 /** \} */
1808
1809
1810 /* -------------------------------------------------------------------- */
1811
1812 /** \name Draw Manager State (DRW_state)
1813  * \{ */
1814
1815 void DRW_state_dfdy_factors_get(float dfdyfac[2])
1816 {
1817         GPU_get_dfdy_factors(dfdyfac);
1818 }
1819
1820 /**
1821  * When false, drawing doesn't output to a pixel buffer
1822  * eg: Occlusion queries, or when we have setup a context to draw in already.
1823  */
1824 bool DRW_state_is_fbo(void)
1825 {
1826         return ((DST.default_framebuffer != NULL) || DST.options.is_image_render);
1827 }
1828
1829 /**
1830  * For when engines need to know if this is drawing for selection or not.
1831  */
1832 bool DRW_state_is_select(void)
1833 {
1834         return DST.options.is_select;
1835 }
1836
1837 bool DRW_state_is_depth(void)
1838 {
1839         return DST.options.is_depth;
1840 }
1841
1842 /**
1843  * Whether we are rendering for an image
1844  */
1845 bool DRW_state_is_image_render(void)
1846 {
1847         return DST.options.is_image_render;
1848 }
1849
1850 /**
1851  * Whether we are rendering only the render engine,
1852  * or if we should also render the mode engines.
1853  */
1854 bool DRW_state_is_scene_render(void)
1855 {
1856         BLI_assert(DST.options.is_scene_render ?
1857                    DST.options.is_image_render : true);
1858         return DST.options.is_scene_render;
1859 }
1860
1861 /**
1862 * Whether we are rendering simple opengl render
1863 */
1864 bool DRW_state_is_opengl_render(void)
1865 {
1866         return DST.options.is_image_render && !DST.options.is_scene_render;
1867 }
1868
1869 /**
1870  * Gives you the iterator mode to use for depsgraph.
1871  */
1872 eDepsObjectIteratorMode DRW_iterator_mode_get(void)
1873 {
1874         return DRW_state_is_scene_render() ? DEG_ITER_OBJECT_MODE_RENDER :
1875                                              DEG_ITER_OBJECT_MODE_VIEWPORT;
1876 }
1877
1878 /**
1879  * Should text draw in this mode?
1880  */
1881 bool DRW_state_show_text(void)
1882 {
1883         return (DST.options.is_select) == 0 &&
1884                (DST.options.is_depth) == 0 &&
1885                (DST.options.is_scene_render) == 0;
1886 }
1887
1888 /**
1889  * Should draw support elements
1890  * Objects center, selection outline, probe data, ...
1891  */
1892 bool DRW_state_draw_support(void)
1893 {
1894         View3D *v3d = DST.draw_ctx.v3d;
1895         return (DRW_state_is_scene_render() == false) &&
1896                 (v3d != NULL) &&
1897                 ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0);
1898 }
1899
1900 /**
1901  * Whether we should render the background
1902  */
1903 bool DRW_state_draw_background(void)
1904 {
1905         if (DRW_state_is_image_render() == false) {
1906                 return true;
1907         }
1908         return DST.options.draw_background;
1909 }
1910
1911 /** \} */
1912
1913
1914 /* -------------------------------------------------------------------- */
1915
1916 /** \name Context State (DRW_context_state)
1917  * \{ */
1918
1919 const DRWContextState *DRW_context_state_get(void)
1920 {
1921         return &DST.draw_ctx;
1922 }
1923
1924 /** \} */
1925
1926
1927 /* -------------------------------------------------------------------- */
1928
1929 /** \name Init/Exit (DRW_engines)
1930  * \{ */
1931
1932 bool DRW_engine_render_support(DrawEngineType *draw_engine_type)
1933 {
1934         return draw_engine_type->render_to_image;
1935 }
1936
1937 void DRW_engine_register(DrawEngineType *draw_engine_type)
1938 {
1939         BLI_addtail(&DRW_engines, draw_engine_type);
1940 }
1941
1942 void DRW_engines_register(void)
1943 {
1944 #ifdef WITH_CLAY_ENGINE
1945         RE_engines_register(NULL, &DRW_engine_viewport_clay_type);
1946 #endif
1947         RE_engines_register(NULL, &DRW_engine_viewport_eevee_type);
1948         RE_engines_register(NULL, &DRW_engine_viewport_workbench_type);
1949
1950         DRW_engine_register(&draw_engine_workbench_solid_flat);
1951
1952         DRW_engine_register(&draw_engine_object_type);
1953         DRW_engine_register(&draw_engine_edit_armature_type);
1954         DRW_engine_register(&draw_engine_edit_curve_type);
1955         DRW_engine_register(&draw_engine_edit_lattice_type);
1956         DRW_engine_register(&draw_engine_edit_mesh_type);
1957         DRW_engine_register(&draw_engine_edit_metaball_type);
1958         DRW_engine_register(&draw_engine_edit_surface_type);
1959         DRW_engine_register(&draw_engine_edit_text_type);
1960         DRW_engine_register(&draw_engine_paint_texture_type);
1961         DRW_engine_register(&draw_engine_paint_vertex_type);
1962         DRW_engine_register(&draw_engine_paint_weight_type);
1963         DRW_engine_register(&draw_engine_particle_type);
1964         DRW_engine_register(&draw_engine_pose_type);
1965         DRW_engine_register(&draw_engine_sculpt_type);
1966
1967         /* setup callbacks */
1968         {
1969                 /* BKE: mball.c */
1970                 extern void *BKE_mball_batch_cache_dirty_cb;
1971                 extern void *BKE_mball_batch_cache_free_cb;
1972                 /* BKE: curve.c */
1973                 extern void *BKE_curve_batch_cache_dirty_cb;
1974                 extern void *BKE_curve_batch_cache_free_cb;
1975                 /* BKE: mesh.c */
1976                 extern void *BKE_mesh_batch_cache_dirty_cb;
1977                 extern void *BKE_mesh_batch_cache_free_cb;
1978                 /* BKE: lattice.c */
1979                 extern void *BKE_lattice_batch_cache_dirty_cb;
1980                 extern void *BKE_lattice_batch_cache_free_cb;
1981                 /* BKE: particle.c */
1982                 extern void *BKE_particle_batch_cache_dirty_cb;
1983                 extern void *BKE_particle_batch_cache_free_cb;
1984
1985                 BKE_mball_batch_cache_dirty_cb = DRW_mball_batch_cache_dirty;
1986                 BKE_mball_batch_cache_free_cb = DRW_mball_batch_cache_free;
1987
1988                 BKE_curve_batch_cache_dirty_cb = DRW_curve_batch_cache_dirty;
1989                 BKE_curve_batch_cache_free_cb = DRW_curve_batch_cache_free;
1990
1991                 BKE_mesh_batch_cache_dirty_cb = DRW_mesh_batch_cache_dirty;
1992                 BKE_mesh_batch_cache_free_cb = DRW_mesh_batch_cache_free;
1993
1994                 BKE_lattice_batch_cache_dirty_cb = DRW_lattice_batch_cache_dirty;
1995                 BKE_lattice_batch_cache_free_cb = DRW_lattice_batch_cache_free;
1996
1997                 BKE_particle_batch_cache_dirty_cb = DRW_particle_batch_cache_dirty;
1998                 BKE_particle_batch_cache_free_cb = DRW_particle_batch_cache_free;
1999         }
2000 }
2001
2002 extern struct Gwn_VertFormat *g_pos_format; /* draw_shgroup.c */
2003 extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
2004 extern struct GPUTexture *globals_ramp; /* draw_common.c */
2005 void DRW_engines_free(void)
2006 {
2007         DRW_opengl_context_enable();
2008
2009         DRW_TEXTURE_FREE_SAFE(g_select_buffer.texture_depth);
2010         GPU_FRAMEBUFFER_FREE_SAFE(g_select_buffer.framebuffer);
2011
2012         DRW_shape_cache_free();
2013         DRW_stats_free();
2014         DRW_globals_free();
2015
2016         DrawEngineType *next;
2017         for (DrawEngineType *type = DRW_engines.first; type; type = next) {
2018                 next = type->next;
2019                 BLI_remlink(&R_engines, type);
2020
2021                 if (type->engine_free) {
2022                         type->engine_free();
2023                 }
2024         }
2025
2026         DRW_UBO_FREE_SAFE(globals_ubo);
2027         DRW_UBO_FREE_SAFE(view_ubo);
2028         DRW_TEXTURE_FREE_SAFE(globals_ramp);
2029         MEM_SAFE_FREE(g_pos_format);
2030
2031         MEM_SAFE_FREE(DST.RST.bound_texs);
2032         MEM_SAFE_FREE(DST.RST.bound_tex_slots);
2033         MEM_SAFE_FREE(DST.RST.bound_ubos);
2034         MEM_SAFE_FREE(DST.RST.bound_ubo_slots);
2035
2036         DRW_opengl_context_disable();
2037
2038 #ifdef WITH_CLAY_ENGINE
2039         BLI_remlink(&R_engines, &DRW_engine_viewport_clay_type);
2040 #endif
2041 }
2042
2043 /** \} */
2044
2045 /** \name Init/Exit (DRW_opengl_ctx)
2046  * \{ */
2047
2048 void DRW_opengl_context_create(void)
2049 {
2050         BLI_assert(DST.ogl_context == NULL); /* Ensure it's called once */
2051
2052         BLI_mutex_init(&DST.ogl_context_mutex);
2053
2054         immDeactivate();
2055         /* This changes the active context. */
2056         DST.ogl_context = WM_opengl_context_create();
2057         /* Be sure to create gawain.context too. */
2058         DST.gwn_context = GWN_context_create();
2059         immActivate();
2060         /* Set default Blender OpenGL state */
2061         GPU_state_init();
2062         /* So we activate the window's one afterwards. */
2063         wm_window_reset_drawable();
2064 }
2065
2066 void DRW_opengl_context_destroy(void)
2067 {
2068         BLI_assert(BLI_thread_is_main());
2069         if (DST.ogl_context != NULL) {
2070                 WM_opengl_context_activate(DST.ogl_context);
2071                 GWN_context_active_set(DST.gwn_context);
2072                 GWN_context_discard(DST.gwn_context);
2073                 WM_opengl_context_dispose(DST.ogl_context);
2074                 BLI_mutex_end(&DST.ogl_context_mutex);
2075         }
2076 }
2077
2078 void DRW_opengl_context_enable(void)
2079 {
2080         if (DST.ogl_context != NULL) {
2081                 /* IMPORTANT: We dont support immediate mode in render mode!
2082                  * This shall remain in effect until immediate mode supports
2083                  * multiple threads. */
2084                 BLI_mutex_lock(&DST.ogl_context_mutex);
2085                 if (BLI_thread_is_main()) {
2086                         immDeactivate();
2087                 }
2088                 WM_opengl_context_activate(DST.ogl_context);
2089                 GWN_context_active_set(DST.gwn_context);
2090                 if (BLI_thread_is_main()) {
2091                         immActivate();
2092                         BLF_batch_reset();
2093                 }
2094         }
2095 }
2096
2097 void DRW_opengl_context_disable(void)
2098 {
2099         if (DST.ogl_context != NULL) {
2100 #ifdef __APPLE__
2101                 /* Need to flush before disabling draw context, otherwise it does not
2102                  * always finish drawing and viewport can be empty or partially drawn */
2103                 glFlush();
2104 #endif
2105
2106                 if (BLI_thread_is_main()) {
2107                         wm_window_reset_drawable();
2108                 }
2109                 else {
2110                         WM_opengl_context_release(DST.ogl_context);
2111                         GWN_context_active_set(NULL);
2112                 }
2113
2114                 BLI_mutex_unlock(&DST.ogl_context_mutex);
2115         }
2116 }
2117
2118 /** \} */