8d4083f17238385fc6b668bcbfd2e1a29c237e2b
[blender.git] / source / blender / draw / modes / object_mode.c
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  * Copyright 2016, Blender Foundation.
17  */
18
19 /** \file
20  * \ingroup draw
21  */
22
23 #include "DRW_engine.h"
24 #include "DRW_render.h"
25
26 #include "DNA_userdef_types.h"
27 #include "DNA_armature_types.h"
28 #include "DNA_constraint_types.h"
29 #include "DNA_camera_types.h"
30 #include "DNA_curve_types.h"
31 #include "DNA_gpencil_types.h"
32 #include "DNA_mesh_types.h"
33 #include "DNA_meta_types.h"
34 #include "DNA_modifier_types.h"
35 #include "DNA_object_force_types.h"
36 #include "DNA_lightprobe_types.h"
37 #include "DNA_particle_types.h"
38 #include "DNA_rigidbody_types.h"
39 #include "DNA_smoke_types.h"
40 #include "DNA_view3d_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_world_types.h"
43
44 #include "BKE_anim.h"
45 #include "BKE_camera.h"
46 #include "BKE_constraint.h"
47 #include "BKE_curve.h"
48 #include "BKE_editmesh.h"
49 #include "BKE_image.h"
50 #include "BKE_mball.h"
51 #include "BKE_mesh.h"
52 #include "BKE_modifier.h"
53 #include "BKE_movieclip.h"
54 #include "BKE_object.h"
55 #include "BKE_particle.h"
56 #include "BKE_tracking.h"
57
58 #include "BLI_ghash.h"
59
60 #include "IMB_imbuf_types.h"
61
62 #include "ED_view3d.h"
63
64 #include "GPU_batch.h"
65 #include "GPU_draw.h"
66 #include "GPU_shader.h"
67 #include "GPU_texture.h"
68
69 #include "MEM_guardedalloc.h"
70
71 #include "UI_resources.h"
72
73 #include "draw_mode_engines.h"
74 #include "draw_manager_text.h"
75 #include "draw_common.h"
76
77 #include "DEG_depsgraph_query.h"
78
79 extern char datatoc_object_outline_prepass_vert_glsl[];
80 extern char datatoc_object_outline_prepass_geom_glsl[];
81 extern char datatoc_object_outline_prepass_frag_glsl[];
82 extern char datatoc_object_outline_resolve_frag_glsl[];
83 extern char datatoc_object_outline_detect_frag_glsl[];
84 extern char datatoc_object_outline_expand_frag_glsl[];
85 extern char datatoc_object_grid_frag_glsl[];
86 extern char datatoc_object_grid_vert_glsl[];
87 extern char datatoc_object_camera_image_frag_glsl[];
88 extern char datatoc_object_camera_image_vert_glsl[];
89 extern char datatoc_object_empty_image_frag_glsl[];
90 extern char datatoc_object_empty_image_vert_glsl[];
91 extern char datatoc_object_lightprobe_grid_vert_glsl[];
92 extern char datatoc_object_loose_points_frag_glsl[];
93 extern char datatoc_object_particle_prim_vert_glsl[];
94 extern char datatoc_object_particle_dot_vert_glsl[];
95 extern char datatoc_object_particle_dot_frag_glsl[];
96 extern char datatoc_common_colormanagement_lib_glsl[];
97 extern char datatoc_common_globals_lib_glsl[];
98 extern char datatoc_common_view_lib_glsl[];
99 extern char datatoc_common_fxaa_lib_glsl[];
100 extern char datatoc_gpu_shader_flat_color_frag_glsl[];
101 extern char datatoc_gpu_shader_flat_id_frag_glsl[];
102 extern char datatoc_common_fullscreen_vert_glsl[];
103 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
104 extern char datatoc_gpu_shader_3D_vert_glsl[];
105
106 /* *********** LISTS *********** */
107 typedef struct OBJECT_PassList {
108   struct DRWPass *non_meshes[2];
109   struct DRWPass *image_empties[2];
110   struct DRWPass *transp_shapes[2];
111   struct DRWPass *ob_center;
112   struct DRWPass *outlines;
113   struct DRWPass *outlines_search;
114   struct DRWPass *outlines_expand;
115   struct DRWPass *outlines_bleed;
116   struct DRWPass *outlines_resolve;
117   struct DRWPass *grid;
118   struct DRWPass *bone_solid[2];
119   struct DRWPass *bone_outline[2];
120   struct DRWPass *bone_wire[2];
121   struct DRWPass *bone_envelope[2];
122   struct DRWPass *bone_axes[2];
123   struct DRWPass *particle;
124   struct DRWPass *lightprobes;
125   struct DRWPass *camera_images_back;
126   struct DRWPass *camera_images_front;
127 } OBJECT_PassList;
128
129 typedef struct OBJECT_FramebufferList {
130   struct GPUFrameBuffer *outlines_fb;
131   struct GPUFrameBuffer *blur_fb;
132   struct GPUFrameBuffer *expand_fb;
133   struct GPUFrameBuffer *ghost_fb;
134 } OBJECT_FramebufferList;
135
136 typedef struct OBJECT_StorageList {
137   struct OBJECT_PrivateData *g_data;
138 } OBJECT_StorageList;
139
140 typedef struct OBJECT_Data {
141   void *engine_type;
142   OBJECT_FramebufferList *fbl;
143   DRWViewportEmptyList *txl;
144   OBJECT_PassList *psl;
145   OBJECT_StorageList *stl;
146 } OBJECT_Data;
147
148 typedef struct OBJECT_Shaders {
149   /* fullscreen shaders */
150   GPUShader *outline_prepass;
151   GPUShader *outline_prepass_wire;
152   GPUShader *outline_resolve;
153   GPUShader *outline_resolve_aa;
154   GPUShader *outline_detect;
155   GPUShader *outline_detect_wire;
156   GPUShader *outline_fade;
157   GPUShader *outline_fade_large;
158
159   /* regular shaders */
160   GPUShader *object_camera_image;
161   GPUShader *object_camera_image_cm;
162   GPUShader *object_empty_image;
163   GPUShader *object_empty_image_wire;
164   GPUShader *grid;
165   GPUShader *part_dot;
166   GPUShader *part_prim;
167   GPUShader *part_axis;
168   GPUShader *lightprobe_grid;
169   GPUShader *loose_points;
170 } OBJECT_Shaders;
171
172 /* *********** STATIC *********** */
173
174 typedef struct OBJECT_ShadingGroupList {
175   /* Reference only */
176   struct DRWPass *non_meshes;
177   struct DRWPass *image_empties;
178   struct DRWPass *transp_shapes;
179   struct DRWPass *bone_solid;
180   struct DRWPass *bone_outline;
181   struct DRWPass *bone_wire;
182   struct DRWPass *bone_envelope;
183   struct DRWPass *bone_axes;
184
185   /* Empties */
186   DRWEmptiesBufferList empties;
187
188   /* Force Field */
189   DRWCallBuffer *field_wind;
190   DRWCallBuffer *field_force;
191   DRWCallBuffer *field_vortex;
192   DRWCallBuffer *field_curve_sta;
193   DRWCallBuffer *field_curve_end;
194   DRWCallBuffer *field_tube_limit;
195   DRWCallBuffer *field_cone_limit;
196
197   /* Grease Pencil */
198   DRWCallBuffer *gpencil_axes;
199
200   /* Speaker */
201   DRWCallBuffer *speaker;
202
203   /* Probe */
204   DRWCallBuffer *probe_cube;
205   DRWCallBuffer *probe_planar;
206   DRWCallBuffer *probe_grid;
207
208   /* MetaBalls */
209   DRWCallBuffer *mball_handle;
210
211   /* Lights */
212   DRWCallBuffer *light_center;
213   DRWCallBuffer *light_groundpoint;
214   DRWCallBuffer *light_groundline;
215   DRWCallBuffer *light_circle;
216   DRWCallBuffer *light_circle_shadow;
217   DRWCallBuffer *light_sunrays;
218   DRWCallBuffer *light_distance;
219   DRWCallBuffer *light_buflimit;
220   DRWCallBuffer *light_buflimit_points;
221   DRWCallBuffer *light_area_sphere;
222   DRWCallBuffer *light_area_square;
223   DRWCallBuffer *light_area_disk;
224   DRWCallBuffer *light_hemi;
225   DRWCallBuffer *light_spot_cone;
226   DRWCallBuffer *light_spot_blend;
227   DRWCallBuffer *light_spot_pyramid;
228   DRWCallBuffer *light_spot_blend_rect;
229   DRWCallBuffer *light_spot_volume;
230   DRWCallBuffer *light_spot_volume_rect;
231   DRWCallBuffer *light_spot_volume_outside;
232   DRWCallBuffer *light_spot_volume_rect_outside;
233
234   /* Helpers */
235   DRWCallBuffer *relationship_lines;
236   DRWCallBuffer *constraint_lines;
237
238   /* Camera */
239   DRWCallBuffer *camera;
240   DRWCallBuffer *camera_frame;
241   DRWCallBuffer *camera_tria;
242   DRWCallBuffer *camera_focus;
243   DRWCallBuffer *camera_clip;
244   DRWCallBuffer *camera_clip_points;
245   DRWCallBuffer *camera_mist;
246   DRWCallBuffer *camera_mist_points;
247   DRWCallBuffer *camera_stereo_plane;
248   DRWCallBuffer *camera_stereo_plane_wires;
249   DRWCallBuffer *camera_stereo_volume;
250   DRWCallBuffer *camera_stereo_volume_wires;
251   ListBase camera_path;
252
253   /* Wire */
254   DRWShadingGroup *wire;
255   DRWShadingGroup *wire_active;
256   DRWShadingGroup *wire_select;
257   DRWShadingGroup *wire_transform;
258   /* Wire (duplicator) */
259   DRWShadingGroup *wire_dupli;
260   DRWShadingGroup *wire_dupli_select;
261
262   /* Points */
263   DRWShadingGroup *points;
264   DRWShadingGroup *points_active;
265   DRWShadingGroup *points_select;
266   DRWShadingGroup *points_transform;
267   /* Points (duplicator) */
268   DRWShadingGroup *points_dupli;
269   DRWShadingGroup *points_dupli_select;
270
271   /* Texture Space */
272   DRWCallBuffer *texspace;
273 } OBJECT_ShadingGroupList;
274
275 typedef struct OBJECT_PrivateData {
276   OBJECT_ShadingGroupList sgl;
277   OBJECT_ShadingGroupList sgl_ghost;
278
279   GHash *custom_shapes;
280
281   /* Outlines */
282   DRWShadingGroup *outlines_active;
283   DRWShadingGroup *outlines_select;
284   DRWShadingGroup *outlines_select_dupli;
285   DRWShadingGroup *outlines_transform;
286
287   /* Lightprobes */
288   DRWCallBuffer *lightprobes_cube_select;
289   DRWCallBuffer *lightprobes_cube_select_dupli;
290   DRWCallBuffer *lightprobes_cube_active;
291   DRWCallBuffer *lightprobes_cube_transform;
292
293   DRWCallBuffer *lightprobes_planar_select;
294   DRWCallBuffer *lightprobes_planar_select_dupli;
295   DRWCallBuffer *lightprobes_planar_active;
296   DRWCallBuffer *lightprobes_planar_transform;
297
298   /* Objects Centers */
299   DRWCallBuffer *center_active;
300   DRWCallBuffer *center_selected;
301   DRWCallBuffer *center_deselected;
302   DRWCallBuffer *center_selected_lib;
303   DRWCallBuffer *center_deselected_lib;
304
305   /* Outlines id offset (accessed as an array) */
306   int id_ofs_active;
307   int id_ofs_select;
308   int id_ofs_select_dupli;
309   int id_ofs_transform;
310
311   int id_ofs_prb_active;
312   int id_ofs_prb_select;
313   int id_ofs_prb_select_dupli;
314   int id_ofs_prb_transform;
315
316   bool xray_enabled;
317   bool xray_enabled_and_not_wire;
318 } OBJECT_PrivateData; /* Transient data */
319
320 typedef struct OBJECT_DupliData {
321   DRWShadingGroup *outline_shgrp;
322   GPUBatch *outline_geom;
323   DRWShadingGroup *extra_shgrp;
324   GPUBatch *extra_geom;
325   short base_flag;
326 } OBJECT_DupliData;
327
328 static struct {
329   /* Instance Data format */
330   struct GPUVertFormat *empty_image_format;
331   struct GPUVertFormat *empty_image_wire_format;
332
333   OBJECT_Shaders sh_data[GPU_SHADER_CFG_LEN];
334
335   float grid_settings[5];
336   float grid_mesh_size;
337   int grid_flag;
338   float grid_axes[3];
339   int zpos_flag;
340   int zneg_flag;
341   float zplane_axes[3];
342   float inv_viewport_size[2];
343   bool draw_grid;
344   /* Temp buffer textures */
345   struct GPUTexture *outlines_depth_tx;
346   struct GPUTexture *outlines_id_tx;
347   struct GPUTexture *outlines_color_tx;
348   struct GPUTexture *outlines_blur_tx;
349
350   ListBase smoke_domains;
351   ListBase movie_clips;
352 } e_data = {NULL}; /* Engine data */
353
354 enum {
355   SHOW_AXIS_X = (1 << 0),
356   SHOW_AXIS_Y = (1 << 1),
357   SHOW_AXIS_Z = (1 << 2),
358   SHOW_GRID = (1 << 3),
359   PLANE_XY = (1 << 4),
360   PLANE_XZ = (1 << 5),
361   PLANE_YZ = (1 << 6),
362   CLIP_ZPOS = (1 << 7),
363   CLIP_ZNEG = (1 << 8),
364   GRID_BACK = (1 << 9),
365 };
366
367 /* Prototypes. */
368 static void DRW_shgroup_empty_ex(OBJECT_ShadingGroupList *sgl,
369                                  const float mat[4][4],
370                                  const float *draw_size,
371                                  char draw_type,
372                                  const float color[4]);
373
374 /* *********** FUNCTIONS *********** */
375
376 static void OBJECT_engine_init(void *vedata)
377 {
378   OBJECT_FramebufferList *fbl = ((OBJECT_Data *)vedata)->fbl;
379
380   const float *viewport_size = DRW_viewport_size_get();
381   const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
382
383   if (DRW_state_is_fbo()) {
384     e_data.outlines_depth_tx = DRW_texture_pool_query_2d(
385         size[0], size[1], GPU_DEPTH_COMPONENT24, &draw_engine_object_type);
386     /* XXX TODO GPU_R16UI can overflow, it would cause no harm
387      * (only bad colored or missing outlines) but we should
388      * use 32bits only if the scene have that many objects */
389     e_data.outlines_id_tx = DRW_texture_pool_query_2d(
390         size[0], size[1], GPU_R16UI, &draw_engine_object_type);
391
392     GPU_framebuffer_ensure_config(&fbl->outlines_fb,
393                                   {GPU_ATTACHMENT_TEXTURE(e_data.outlines_depth_tx),
394                                    GPU_ATTACHMENT_TEXTURE(e_data.outlines_id_tx)});
395
396     e_data.outlines_color_tx = DRW_texture_pool_query_2d(
397         size[0], size[1], GPU_RGBA8, &draw_engine_object_type);
398
399     GPU_framebuffer_ensure_config(
400         &fbl->expand_fb, {GPU_ATTACHMENT_NONE, GPU_ATTACHMENT_TEXTURE(e_data.outlines_color_tx)});
401
402     e_data.outlines_blur_tx = DRW_texture_pool_query_2d(
403         size[0], size[1], GPU_RGBA8, &draw_engine_object_type);
404
405     GPU_framebuffer_ensure_config(
406         &fbl->blur_fb, {GPU_ATTACHMENT_NONE, GPU_ATTACHMENT_TEXTURE(e_data.outlines_blur_tx)});
407   }
408
409   /* Shaders */
410   const DRWContextState *draw_ctx = DRW_context_state_get();
411   OBJECT_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
412
413   const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[draw_ctx->sh_cfg];
414
415   if (!sh_data->outline_resolve) {
416     /* Outline */
417     sh_data->outline_prepass = GPU_shader_create_from_arrays({
418         .vert = (const char *[]){sh_cfg_data->lib, datatoc_gpu_shader_3D_vert_glsl, NULL},
419         .frag = (const char *[]){datatoc_object_outline_prepass_frag_glsl, NULL},
420         .defs = (const char *[]){sh_cfg_data->def, NULL},
421     });
422     sh_data->outline_prepass_wire = GPU_shader_create_from_arrays({
423         .vert = (const char *[]){sh_cfg_data->lib,
424                                  datatoc_common_view_lib_glsl,
425                                  datatoc_object_outline_prepass_vert_glsl,
426                                  NULL},
427         .geom = (const char *[]){sh_cfg_data->lib,
428                                  datatoc_common_view_lib_glsl,
429                                  datatoc_object_outline_prepass_geom_glsl,
430                                  NULL},
431         .frag = (const char *[]){datatoc_object_outline_prepass_frag_glsl, NULL},
432         .defs = (const char *[]){sh_cfg_data->def, NULL},
433     });
434
435     sh_data->outline_resolve = DRW_shader_create_fullscreen(
436         datatoc_object_outline_resolve_frag_glsl, NULL);
437
438     sh_data->outline_resolve_aa = DRW_shader_create_with_lib(
439         datatoc_common_fullscreen_vert_glsl,
440         NULL,
441         datatoc_object_outline_resolve_frag_glsl,
442         datatoc_common_fxaa_lib_glsl,
443         "#define FXAA_ALPHA\n"
444         "#define USE_FXAA\n");
445
446     sh_data->outline_detect = DRW_shader_create_with_lib(datatoc_common_fullscreen_vert_glsl,
447                                                          NULL,
448                                                          datatoc_object_outline_detect_frag_glsl,
449                                                          datatoc_common_globals_lib_glsl,
450                                                          NULL);
451
452     sh_data->outline_detect_wire = DRW_shader_create_with_lib(
453         datatoc_common_fullscreen_vert_glsl,
454         NULL,
455         datatoc_object_outline_detect_frag_glsl,
456         datatoc_common_globals_lib_glsl,
457         "#define WIRE\n");
458
459     sh_data->outline_fade = DRW_shader_create_fullscreen(datatoc_object_outline_expand_frag_glsl,
460                                                          NULL);
461     sh_data->outline_fade_large = DRW_shader_create_fullscreen(
462         datatoc_object_outline_expand_frag_glsl, "#define LARGE_OUTLINE\n");
463
464     /* Empty images */
465     {
466       const char *empty_image_defs = (
467               "#define DEPTH_UNCHANGED " STRINGIFY(OB_EMPTY_IMAGE_DEPTH_DEFAULT) "\n"
468               "#define DEPTH_FRONT " STRINGIFY(OB_EMPTY_IMAGE_DEPTH_FRONT) "\n"
469               "#define DEPTH_BACK " STRINGIFY(OB_EMPTY_IMAGE_DEPTH_BACK) "\n");
470
471       sh_data->object_empty_image = GPU_shader_create_from_arrays({
472           .vert = (const char *[]){sh_cfg_data->lib,
473                                    datatoc_common_view_lib_glsl,
474                                    datatoc_object_empty_image_vert_glsl,
475                                    NULL},
476           .frag = (const char *[]){datatoc_common_colormanagement_lib_glsl,
477                                    datatoc_object_empty_image_frag_glsl,
478                                    NULL},
479           .defs = (const char *[]){sh_cfg_data->def, empty_image_defs, NULL},
480       });
481       sh_data->object_empty_image_wire = GPU_shader_create_from_arrays({
482           .vert = (const char *[]){sh_cfg_data->lib,
483                                    datatoc_common_view_lib_glsl,
484                                    datatoc_object_empty_image_vert_glsl,
485                                    NULL},
486           .frag = (const char *[]){datatoc_object_empty_image_frag_glsl, NULL},
487           .defs = (const char *[]){sh_cfg_data->def, "#define USE_WIRE\n", empty_image_defs, NULL},
488       });
489
490       sh_data->object_camera_image_cm = GPU_shader_create_from_arrays({
491           .vert = (const char *[]){sh_cfg_data->lib, datatoc_object_camera_image_vert_glsl, NULL},
492           .frag = (const char *[]){datatoc_common_colormanagement_lib_glsl,
493                                    datatoc_object_camera_image_frag_glsl,
494                                    NULL},
495           .defs =
496               (const char *[]){sh_cfg_data->def, "#define DRW_STATE_DO_COLOR_MANAGEMENT\n", NULL},
497       });
498       sh_data->object_camera_image = GPU_shader_create_from_arrays({
499           .vert = (const char *[]){sh_cfg_data->lib, datatoc_object_camera_image_vert_glsl, NULL},
500           .frag = (const char *[]){datatoc_common_colormanagement_lib_glsl,
501                                    datatoc_object_camera_image_frag_glsl,
502                                    NULL},
503       });
504     }
505
506     /* Grid */
507     sh_data->grid = GPU_shader_create_from_arrays({
508         .vert = (const char *[]){datatoc_common_globals_lib_glsl,
509                                  datatoc_common_view_lib_glsl,
510                                  datatoc_object_grid_vert_glsl,
511                                  NULL},
512         .frag = (const char *[]){datatoc_common_globals_lib_glsl,
513                                  datatoc_common_view_lib_glsl,
514                                  datatoc_object_grid_frag_glsl,
515                                  NULL},
516     });
517
518     /* Particles */
519     sh_data->part_prim = DRW_shader_create_with_lib(datatoc_object_particle_prim_vert_glsl,
520                                                     NULL,
521                                                     datatoc_gpu_shader_flat_color_frag_glsl,
522                                                     datatoc_common_view_lib_glsl,
523                                                     NULL);
524
525     sh_data->part_axis = DRW_shader_create_with_lib(datatoc_object_particle_prim_vert_glsl,
526                                                     NULL,
527                                                     datatoc_gpu_shader_flat_color_frag_glsl,
528                                                     datatoc_common_view_lib_glsl,
529                                                     "#define USE_AXIS\n");
530
531     sh_data->part_dot = DRW_shader_create_with_lib(datatoc_object_particle_dot_vert_glsl,
532                                                    NULL,
533                                                    datatoc_object_particle_dot_frag_glsl,
534                                                    datatoc_common_view_lib_glsl,
535                                                    NULL);
536
537     /* Lightprobes */
538     sh_data->lightprobe_grid = GPU_shader_create_from_arrays({
539         .vert = (const char *[]){sh_cfg_data->lib,
540                                  datatoc_common_globals_lib_glsl,
541                                  datatoc_object_lightprobe_grid_vert_glsl,
542                                  NULL},
543         .frag = (const char *[]){datatoc_gpu_shader_flat_id_frag_glsl, NULL},
544         .defs = (const char *[]){sh_cfg_data->def, NULL},
545     });
546
547     /* Loose Points */
548     sh_data->loose_points = GPU_shader_create_from_arrays({
549         .vert = (const char *[]){sh_cfg_data->lib, datatoc_gpu_shader_3D_vert_glsl, NULL},
550         .frag = (const char *[]){datatoc_object_loose_points_frag_glsl, NULL},
551         .defs = (const char *[]){sh_cfg_data->def, NULL},
552     });
553   }
554
555   {
556     /* Grid precompute */
557     float viewinv[4][4], wininv[4][4];
558     float viewmat[4][4], winmat[4][4];
559     View3D *v3d = draw_ctx->v3d;
560     Scene *scene = draw_ctx->scene;
561     RegionView3D *rv3d = draw_ctx->rv3d;
562     float grid_scale = ED_view3d_grid_scale(scene, v3d, NULL);
563     float grid_res;
564
565     const bool show_axis_x = (v3d->gridflag & V3D_SHOW_X) != 0;
566     const bool show_axis_y = (v3d->gridflag & V3D_SHOW_Y) != 0;
567     const bool show_axis_z = (v3d->gridflag & V3D_SHOW_Z) != 0;
568     const bool show_floor = (v3d->gridflag & V3D_SHOW_FLOOR) != 0;
569     const bool show_ortho_grid = (v3d->gridflag & V3D_SHOW_ORTHO_GRID) != 0;
570     e_data.draw_grid = show_axis_x || show_axis_y || show_axis_z || show_floor;
571
572     DRW_view_winmat_get(NULL, winmat, false);
573     DRW_view_winmat_get(NULL, wininv, true);
574     DRW_view_viewmat_get(NULL, viewmat, false);
575     DRW_view_viewmat_get(NULL, viewinv, true);
576
577     /* if perps */
578     if (winmat[3][3] == 0.0f) {
579       float fov;
580       float viewvecs[2][4] = {
581           {1.0f, -1.0f, -1.0f, 1.0f},
582           {-1.0f, 1.0f, -1.0f, 1.0f},
583       };
584
585       /* convert the view vectors to view space */
586       for (int i = 0; i < 2; i++) {
587         mul_m4_v4(wininv, viewvecs[i]);
588         mul_v3_fl(viewvecs[i], 1.0f / viewvecs[i][2]); /* perspective divide */
589       }
590
591       fov = angle_v3v3(viewvecs[0], viewvecs[1]) / 2.0f;
592       grid_res = fabsf(tanf(fov)) / grid_scale;
593
594       e_data.grid_flag = (1 << 4); /* XY plane */
595       if (show_axis_x) {
596         e_data.grid_flag |= SHOW_AXIS_X;
597       }
598       if (show_axis_y) {
599         e_data.grid_flag |= SHOW_AXIS_Y;
600       }
601       if (show_floor) {
602         e_data.grid_flag |= SHOW_GRID;
603       }
604     }
605     else {
606       if (rv3d->view != RV3D_VIEW_USER) {
607         /* Allow 3 more subdivisions. */
608         grid_scale /= powf(v3d->gridsubdiv, 3);
609       }
610
611       float viewdist = 1.0f / max_ff(fabsf(winmat[0][0]), fabsf(winmat[1][1]));
612       grid_res = viewdist / grid_scale;
613
614       if (ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT)) {
615         e_data.draw_grid = show_ortho_grid;
616         e_data.grid_flag = PLANE_YZ | SHOW_AXIS_Y | SHOW_AXIS_Z | SHOW_GRID | GRID_BACK;
617       }
618       else if (ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) {
619         e_data.draw_grid = show_ortho_grid;
620         e_data.grid_flag = PLANE_XY | SHOW_AXIS_X | SHOW_AXIS_Y | SHOW_GRID | GRID_BACK;
621       }
622       else if (ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK)) {
623         e_data.draw_grid = show_ortho_grid;
624         e_data.grid_flag = PLANE_XZ | SHOW_AXIS_X | SHOW_AXIS_Z | SHOW_GRID | GRID_BACK;
625       }
626       else { /* RV3D_VIEW_USER */
627         e_data.grid_flag = PLANE_XY;
628         if (show_axis_x) {
629           e_data.grid_flag |= SHOW_AXIS_X;
630         }
631         if (show_axis_y) {
632           e_data.grid_flag |= SHOW_AXIS_Y;
633         }
634         if (show_floor) {
635           e_data.grid_flag |= SHOW_GRID;
636         }
637       }
638     }
639
640     e_data.grid_axes[0] = (float)((e_data.grid_flag & (PLANE_XZ | PLANE_XY)) != 0);
641     e_data.grid_axes[1] = (float)((e_data.grid_flag & (PLANE_YZ | PLANE_XY)) != 0);
642     e_data.grid_axes[2] = (float)((e_data.grid_flag & (PLANE_YZ | PLANE_XZ)) != 0);
643
644     /* Z axis if needed */
645     if (((rv3d->view == RV3D_VIEW_USER) || (rv3d->persp != RV3D_ORTHO)) && show_axis_z) {
646       e_data.zpos_flag = SHOW_AXIS_Z;
647
648       float zvec[3], campos[3];
649       negate_v3_v3(zvec, viewinv[2]);
650       copy_v3_v3(campos, viewinv[3]);
651
652       /* z axis : chose the most facing plane */
653       if (fabsf(zvec[0]) < fabsf(zvec[1])) {
654         e_data.zpos_flag |= PLANE_XZ;
655       }
656       else {
657         e_data.zpos_flag |= PLANE_YZ;
658       }
659
660       e_data.zneg_flag = e_data.zpos_flag;
661
662       /* Persp : If camera is below floor plane, we switch clipping
663        * Ortho : If eye vector is looking up, we switch clipping */
664       if (((winmat[3][3] == 0.0f) && (campos[2] > 0.0f)) ||
665           ((winmat[3][3] != 0.0f) && (zvec[2] < 0.0f))) {
666         e_data.zpos_flag |= CLIP_ZPOS;
667         e_data.zneg_flag |= CLIP_ZNEG;
668       }
669       else {
670         e_data.zpos_flag |= CLIP_ZNEG;
671         e_data.zneg_flag |= CLIP_ZPOS;
672       }
673
674       e_data.zplane_axes[0] = (float)((e_data.zpos_flag & (PLANE_XZ | PLANE_XY)) != 0);
675       e_data.zplane_axes[1] = (float)((e_data.zpos_flag & (PLANE_YZ | PLANE_XY)) != 0);
676       e_data.zplane_axes[2] = (float)((e_data.zpos_flag & (PLANE_YZ | PLANE_XZ)) != 0);
677     }
678     else {
679       e_data.zneg_flag = e_data.zpos_flag = CLIP_ZNEG | CLIP_ZPOS;
680     }
681
682     float dist;
683     if (rv3d->persp == RV3D_CAMOB && v3d->camera && v3d->camera->type == OB_CAMERA) {
684       Object *camera_object = DEG_get_evaluated_object(draw_ctx->depsgraph, v3d->camera);
685       dist = ((Camera *)(camera_object->data))->clip_end;
686     }
687     else {
688       dist = v3d->clip_end;
689     }
690
691     e_data.grid_settings[0] = dist / 2.0f;     /* gridDistance */
692     e_data.grid_settings[1] = grid_res;        /* gridResolution */
693     e_data.grid_settings[2] = grid_scale;      /* gridScale */
694     e_data.grid_settings[3] = v3d->gridsubdiv; /* gridSubdiv */
695     e_data.grid_settings[4] = (v3d->gridsubdiv > 1) ? 1.0f / logf(v3d->gridsubdiv) :
696                                                       0.0f; /* 1/log(gridSubdiv) */
697
698     if (winmat[3][3] == 0.0f) {
699       e_data.grid_mesh_size = dist;
700     }
701     else {
702       float viewdist = 1.0f / min_ff(fabsf(winmat[0][0]), fabsf(winmat[1][1]));
703       e_data.grid_mesh_size = viewdist * dist;
704     }
705   }
706
707   copy_v2_v2(e_data.inv_viewport_size, DRW_viewport_size_get());
708   invert_v2(e_data.inv_viewport_size);
709 }
710
711 static void OBJECT_engine_free(void)
712 {
713   MEM_SAFE_FREE(e_data.empty_image_format);
714   MEM_SAFE_FREE(e_data.empty_image_wire_format);
715
716   for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
717     OBJECT_Shaders *sh_data = &e_data.sh_data[sh_data_index];
718     GPUShader **sh_data_as_array = (GPUShader **)sh_data;
719     for (int i = 0; i < (sizeof(OBJECT_Shaders) / sizeof(GPUShader *)); i++) {
720       DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
721     }
722   }
723 }
724
725 static DRWShadingGroup *shgroup_outline(DRWPass *pass,
726                                         const int *ofs,
727                                         GPUShader *sh,
728                                         eGPUShaderConfig sh_cfg)
729 {
730   DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
731   DRW_shgroup_uniform_int(grp, "baseId", ofs, 1);
732
733   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
734     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
735   }
736   return grp;
737 }
738
739 /* currently same as 'shgroup_outline', new function to avoid confustion */
740 static DRWShadingGroup *shgroup_wire(DRWPass *pass,
741                                      const float col[4],
742                                      GPUShader *sh,
743                                      eGPUShaderConfig sh_cfg)
744 {
745   DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
746   DRW_shgroup_uniform_vec4(grp, "color", col, 1);
747
748   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
749     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
750   }
751   return grp;
752 }
753
754 /* currently same as 'shgroup_outline', new function to avoid confustion */
755 static DRWShadingGroup *shgroup_points(DRWPass *pass,
756                                        const float col[4],
757                                        GPUShader *sh,
758                                        eGPUShaderConfig sh_cfg)
759 {
760   DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
761   DRW_shgroup_uniform_vec4(grp, "color", col, 1);
762   DRW_shgroup_uniform_vec4(grp, "innerColor", G_draw.block.colorEditMeshMiddle, 1);
763
764   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
765     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
766   }
767   return grp;
768 }
769
770 static int *shgroup_theme_id_to_probe_outline_counter(OBJECT_StorageList *stl,
771                                                       int theme_id,
772                                                       const int base_flag)
773 {
774   if (UNLIKELY(base_flag & BASE_FROM_DUPLI)) {
775     switch (theme_id) {
776       case TH_ACTIVE:
777       case TH_SELECT:
778         return &stl->g_data->id_ofs_prb_select_dupli;
779       case TH_TRANSFORM:
780       default:
781         return &stl->g_data->id_ofs_prb_transform;
782     }
783   }
784
785   switch (theme_id) {
786     case TH_ACTIVE:
787       return &stl->g_data->id_ofs_prb_active;
788     case TH_SELECT:
789       return &stl->g_data->id_ofs_prb_select;
790     case TH_TRANSFORM:
791     default:
792       return &stl->g_data->id_ofs_prb_transform;
793   }
794 }
795
796 static int *shgroup_theme_id_to_outline_counter(OBJECT_StorageList *stl,
797                                                 int theme_id,
798                                                 const int base_flag)
799 {
800   if (UNLIKELY(base_flag & BASE_FROM_DUPLI)) {
801     switch (theme_id) {
802       case TH_ACTIVE:
803       case TH_SELECT:
804         return &stl->g_data->id_ofs_select_dupli;
805       case TH_TRANSFORM:
806       default:
807         return &stl->g_data->id_ofs_transform;
808     }
809   }
810
811   switch (theme_id) {
812     case TH_ACTIVE:
813       return &stl->g_data->id_ofs_active;
814     case TH_SELECT:
815       return &stl->g_data->id_ofs_select;
816     case TH_TRANSFORM:
817     default:
818       return &stl->g_data->id_ofs_transform;
819   }
820 }
821
822 static DRWCallBuffer *buffer_theme_id_to_probe_planar_outline_shgrp(OBJECT_StorageList *stl,
823                                                                     int theme_id)
824 {
825   /* does not increment counter */
826   switch (theme_id) {
827     case TH_ACTIVE:
828       return stl->g_data->lightprobes_planar_active;
829     case TH_SELECT:
830       return stl->g_data->lightprobes_planar_select;
831     case TH_TRANSFORM:
832     default:
833       return stl->g_data->lightprobes_planar_transform;
834   }
835 }
836
837 static DRWCallBuffer *buffer_theme_id_to_probe_cube_outline_shgrp(OBJECT_StorageList *stl,
838                                                                   int theme_id,
839                                                                   const int base_flag)
840 {
841   /* does not increment counter */
842   if (UNLIKELY(base_flag & BASE_FROM_DUPLI)) {
843     switch (theme_id) {
844       case TH_ACTIVE:
845       case TH_SELECT:
846         return stl->g_data->lightprobes_cube_select_dupli;
847       case TH_TRANSFORM:
848       default:
849         return stl->g_data->lightprobes_cube_transform;
850     }
851   }
852
853   switch (theme_id) {
854     case TH_ACTIVE:
855       return stl->g_data->lightprobes_cube_active;
856     case TH_SELECT:
857       return stl->g_data->lightprobes_cube_select;
858     case TH_TRANSFORM:
859     default:
860       return stl->g_data->lightprobes_cube_transform;
861   }
862 }
863
864 static DRWShadingGroup *shgroup_theme_id_to_outline_or_null(OBJECT_StorageList *stl,
865                                                             int theme_id,
866                                                             const int base_flag)
867 {
868   int *counter = shgroup_theme_id_to_outline_counter(stl, theme_id, base_flag);
869   *counter += 1;
870
871   if (UNLIKELY(base_flag & BASE_FROM_DUPLI)) {
872     switch (theme_id) {
873       case TH_ACTIVE:
874       case TH_SELECT:
875         return stl->g_data->outlines_select_dupli;
876       case TH_TRANSFORM:
877         return stl->g_data->outlines_transform;
878       default:
879         return NULL;
880     }
881   }
882
883   switch (theme_id) {
884     case TH_ACTIVE:
885       return stl->g_data->outlines_active;
886     case TH_SELECT:
887       return stl->g_data->outlines_select;
888     case TH_TRANSFORM:
889       return stl->g_data->outlines_transform;
890     default:
891       return NULL;
892   }
893 }
894
895 static DRWShadingGroup *shgroup_theme_id_to_wire(OBJECT_ShadingGroupList *sgl,
896                                                  int theme_id,
897                                                  const short base_flag)
898 {
899   if (UNLIKELY(base_flag & BASE_FROM_SET)) {
900     return sgl->wire_dupli;
901   }
902   else if (UNLIKELY(base_flag & BASE_FROM_DUPLI)) {
903     switch (theme_id) {
904       case TH_ACTIVE:
905       case TH_SELECT:
906         return sgl->wire_dupli_select;
907       case TH_TRANSFORM:
908         return sgl->wire_transform;
909       default:
910         return sgl->wire_dupli;
911     }
912   }
913
914   switch (theme_id) {
915     case TH_ACTIVE:
916       return sgl->wire_active;
917     case TH_SELECT:
918       return sgl->wire_select;
919     case TH_TRANSFORM:
920       return sgl->wire_transform;
921     default:
922       return sgl->wire;
923   }
924 }
925
926 static DRWShadingGroup *shgroup_theme_id_to_point(OBJECT_ShadingGroupList *sgl,
927                                                   int theme_id,
928                                                   const short base_flag)
929 {
930   if (UNLIKELY(base_flag & BASE_FROM_SET)) {
931     return sgl->points_dupli;
932   }
933   else if (UNLIKELY(base_flag & BASE_FROM_DUPLI)) {
934     switch (theme_id) {
935       case TH_ACTIVE:
936       case TH_SELECT:
937         return sgl->points_dupli_select;
938       case TH_TRANSFORM:
939         return sgl->points_transform;
940       default:
941         return sgl->points_dupli;
942     }
943   }
944
945   switch (theme_id) {
946     case TH_ACTIVE:
947       return sgl->points_active;
948     case TH_SELECT:
949       return sgl->points_select;
950     case TH_TRANSFORM:
951       return sgl->points_transform;
952     default:
953       return sgl->points;
954   }
955 }
956
957 static void image_calc_aspect(Image *ima, const int size[2], float r_image_aspect[2])
958 {
959   float ima_x, ima_y;
960   if (ima) {
961     ima_x = size[0];
962     ima_y = size[1];
963   }
964   else {
965     /* if no image, make it a 1x1 empty square, honor scale & offset */
966     ima_x = ima_y = 1.0f;
967   }
968   /* Get the image aspect even if the buffer is invalid */
969   float sca_x = 1.0f, sca_y = 1.0f;
970   if (ima) {
971     if (ima->aspx > ima->aspy) {
972       sca_y = ima->aspy / ima->aspx;
973     }
974     else if (ima->aspx < ima->aspy) {
975       sca_x = ima->aspx / ima->aspy;
976     }
977   }
978
979   const float scale_x_inv = ima_x * sca_x;
980   const float scale_y_inv = ima_y * sca_y;
981   if (scale_x_inv > scale_y_inv) {
982     r_image_aspect[0] = 1.0f;
983     r_image_aspect[1] = scale_y_inv / scale_x_inv;
984   }
985   else {
986     r_image_aspect[0] = scale_x_inv / scale_y_inv;
987     r_image_aspect[1] = 1.0f;
988   }
989 }
990
991 static void DRW_shgroup_empty_image(OBJECT_Shaders *sh_data,
992                                     OBJECT_ShadingGroupList *sgl,
993                                     Object *ob,
994                                     const float color[3],
995                                     RegionView3D *rv3d,
996                                     eGPUShaderConfig sh_cfg)
997 {
998   /* TODO: 'StereoViews', see draw_empty_image. */
999
1000   if (!BKE_object_empty_image_frame_is_visible_in_view3d(ob, rv3d)) {
1001     return;
1002   }
1003
1004   /* Calling 'BKE_image_get_size' may free the texture. Get the size from 'tex' instead,
1005    * see: T59347 */
1006   int size[2] = {0};
1007
1008   const bool use_alpha_blend = (ob->empty_image_flag & OB_EMPTY_IMAGE_USE_ALPHA_BLEND) != 0;
1009   GPUTexture *tex = NULL;
1010   Image *ima = ob->data;
1011
1012   if (ima != NULL) {
1013     tex = GPU_texture_from_blender(ima, ob->iuser, GL_TEXTURE_2D);
1014     if (tex) {
1015       size[0] = GPU_texture_orig_width(tex);
1016       size[1] = GPU_texture_orig_height(tex);
1017     }
1018   }
1019
1020   CLAMP_MIN(size[0], 1);
1021   CLAMP_MIN(size[1], 1);
1022
1023   float image_aspect[2];
1024   image_calc_aspect(ob->data, size, image_aspect);
1025
1026   char depth_mode;
1027   if (DRW_state_is_depth()) {
1028     /* Use the actual depth if we are doing depth tests to determine the distance to the object */
1029     depth_mode = OB_EMPTY_IMAGE_DEPTH_DEFAULT;
1030   }
1031   else {
1032     depth_mode = ob->empty_image_depth;
1033   }
1034
1035   {
1036     DRWShadingGroup *grp = DRW_shgroup_create(sh_data->object_empty_image_wire, sgl->non_meshes);
1037     DRW_shgroup_uniform_vec2_copy(grp, "aspect", image_aspect);
1038     DRW_shgroup_uniform_int_copy(grp, "depthMode", depth_mode);
1039     DRW_shgroup_uniform_float(grp, "size", &ob->empty_drawsize, 1);
1040     DRW_shgroup_uniform_vec2(grp, "offset", ob->ima_ofs, 1);
1041     DRW_shgroup_uniform_vec3(grp, "color", color, 1);
1042     if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
1043       DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
1044     }
1045     DRW_shgroup_call_no_cull(grp, DRW_cache_image_plane_wire_get(), ob);
1046   }
1047
1048   if (!BKE_object_empty_image_data_is_visible_in_view3d(ob, rv3d)) {
1049     return;
1050   }
1051
1052   if (tex && ((ob->color[3] > 0.0f) || !use_alpha_blend)) {
1053     DRWShadingGroup *grp = DRW_shgroup_create(
1054         sh_data->object_empty_image, (use_alpha_blend) ? sgl->image_empties : sgl->non_meshes);
1055     DRW_shgroup_uniform_vec2_copy(grp, "aspect", image_aspect);
1056     DRW_shgroup_uniform_int_copy(grp, "depthMode", depth_mode);
1057     DRW_shgroup_uniform_float(grp, "size", &ob->empty_drawsize, 1);
1058     DRW_shgroup_uniform_vec2(grp, "offset", ob->ima_ofs, 1);
1059     DRW_shgroup_uniform_texture(grp, "image", tex);
1060     DRW_shgroup_uniform_bool_copy(
1061         grp, "imagePremultiplied", (ima->alpha_mode == IMA_ALPHA_PREMUL));
1062     DRW_shgroup_uniform_vec4(grp, "objectColor", ob->color, 1);
1063     DRW_shgroup_uniform_bool_copy(grp, "useAlphaTest", !use_alpha_blend);
1064     if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
1065       DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
1066     }
1067     DRW_shgroup_call_no_cull(grp, DRW_cache_image_plane_get(), ob);
1068   }
1069 }
1070
1071 /* Draw Camera Background Images */
1072 typedef struct CameraEngineData {
1073   DrawData dd;
1074   ListBase bg_data;
1075 } CameraEngineData;
1076 typedef struct CameraEngineBGData {
1077   float transform_mat[4][4];
1078 } CameraEngineBGData;
1079
1080 static void camera_engine_data_free(DrawData *dd)
1081 {
1082   CameraEngineData *data = (CameraEngineData *)dd;
1083   for (LinkData *link = data->bg_data.first; link; link = link->next) {
1084     CameraEngineBGData *bg_data = (CameraEngineBGData *)link->data;
1085     MEM_freeN(bg_data);
1086   }
1087   BLI_freelistN(&data->bg_data);
1088 }
1089
1090 static void camera_background_images_stereo_setup(Scene *scene,
1091                                                   View3D *v3d,
1092                                                   Image *ima,
1093                                                   ImageUser *iuser)
1094 {
1095   if (BKE_image_is_stereo(ima)) {
1096     iuser->flag |= IMA_SHOW_STEREO;
1097
1098     if ((scene->r.scemode & R_MULTIVIEW) == 0) {
1099       iuser->multiview_eye = STEREO_LEFT_ID;
1100     }
1101     else if (v3d->stereo3d_camera != STEREO_3D_ID) {
1102       /* show only left or right camera */
1103       iuser->multiview_eye = v3d->stereo3d_camera;
1104     }
1105
1106     BKE_image_multiview_index(ima, iuser);
1107   }
1108   else {
1109     iuser->flag &= ~IMA_SHOW_STEREO;
1110   }
1111 }
1112
1113 static void DRW_shgroup_camera_background_images(OBJECT_Shaders *sh_data,
1114                                                  OBJECT_PassList *psl,
1115                                                  Object *ob,
1116                                                  RegionView3D *rv3d)
1117 {
1118   if (!BKE_object_empty_image_frame_is_visible_in_view3d(ob, rv3d)) {
1119     return;
1120   }
1121
1122   const DRWContextState *draw_ctx = DRW_context_state_get();
1123   struct ARegion *ar = draw_ctx->ar;
1124   View3D *v3d = draw_ctx->v3d;
1125   Scene *scene = draw_ctx->scene;
1126   Depsgraph *depsgraph = draw_ctx->depsgraph;
1127   Camera *cam = ob->data;
1128   const Object *camera_object = DEG_get_evaluated_object(depsgraph, v3d->camera);
1129   const bool is_active = (ob == camera_object);
1130   const bool look_through = (is_active && (rv3d->persp == RV3D_CAMOB));
1131
1132   if (look_through && (cam->flag & CAM_SHOW_BG_IMAGE)) {
1133     GPUBatch *batch = DRW_cache_image_plane_get();
1134
1135     /* load camera engine data */
1136     CameraEngineData *camera_engine_data = (CameraEngineData *)DRW_drawdata_ensure(
1137         &ob->id,
1138         &draw_engine_object_type,
1139         sizeof(CameraEngineData),
1140         NULL,
1141         camera_engine_data_free);
1142     LinkData *list_node = camera_engine_data->bg_data.first;
1143
1144     for (CameraBGImage *bgpic = cam->bg_images.first; bgpic; bgpic = bgpic->next) {
1145       if ((bgpic->flag & CAM_BGIMG_FLAG_DISABLED)) {
1146         continue;
1147       }
1148
1149       /* retrieve the image we want to show, continue to next when no image could be found */
1150       ImBuf *ibuf = NULL;
1151       GPUTexture *tex = NULL;
1152       float image_aspect_x, image_aspect_y;
1153       float image_aspect = 1.0;
1154       int image_width, image_height;
1155       bool premultiplied = false;
1156
1157       if (bgpic->source == CAM_BGIMG_SOURCE_IMAGE) {
1158         Image *image = bgpic->ima;
1159         if (image == NULL) {
1160           continue;
1161         }
1162         premultiplied = (image->alpha_mode == IMA_ALPHA_PREMUL);
1163         ImageUser *iuser = &bgpic->iuser;
1164         BKE_image_user_frame_calc(image, iuser, (int)DEG_get_ctime(depsgraph));
1165         if (image->source == IMA_SRC_SEQUENCE && !(iuser->flag & IMA_USER_FRAME_IN_RANGE)) {
1166           /* frame is out of range, dont show */
1167           continue;
1168         }
1169         else {
1170           camera_background_images_stereo_setup(scene, v3d, image, iuser);
1171         }
1172         tex = GPU_texture_from_blender(image, iuser, GL_TEXTURE_2D);
1173         if (tex == NULL) {
1174           continue;
1175         }
1176         ibuf = BKE_image_acquire_ibuf(image, iuser, NULL);
1177         if (ibuf == NULL) {
1178           continue;
1179         }
1180
1181         image_aspect_x = bgpic->ima->aspx;
1182         image_aspect_y = bgpic->ima->aspy;
1183
1184         image_width = ibuf->x;
1185         image_height = ibuf->y;
1186         BKE_image_release_ibuf(image, ibuf, NULL);
1187         image_aspect = (image_width * image_aspect_x) / (image_height * image_aspect_y);
1188       }
1189       else if (bgpic->source == CAM_BGIMG_SOURCE_MOVIE) {
1190         MovieClip *clip = NULL;
1191         if (bgpic->flag & CAM_BGIMG_FLAG_CAMERACLIP) {
1192           if (scene->camera) {
1193             clip = BKE_object_movieclip_get(scene, scene->camera, true);
1194           }
1195         }
1196         else {
1197           clip = bgpic->clip;
1198         }
1199
1200         if (clip == NULL) {
1201           continue;
1202         }
1203
1204         image_aspect_x = clip->aspx;
1205         image_aspect_y = clip->aspy;
1206
1207         BKE_movieclip_user_set_frame(&bgpic->cuser, (int)DEG_get_ctime(depsgraph));
1208         tex = GPU_texture_from_movieclip(clip, &bgpic->cuser, GL_TEXTURE_2D);
1209         if (tex == NULL) {
1210           continue;
1211         }
1212         BLI_addtail(&e_data.movie_clips, BLI_genericNodeN(clip));
1213         BKE_movieclip_get_size(clip, &bgpic->cuser, &image_width, &image_height);
1214         image_aspect = (image_width * image_aspect_x) / (image_height * image_aspect_y);
1215       }
1216       else {
1217         continue;
1218       }
1219
1220       /* ensure link_data is allocated to store matrice */
1221       CameraEngineBGData *bg_data;
1222       if (list_node != NULL) {
1223         bg_data = (CameraEngineBGData *)list_node->data;
1224         list_node = list_node->next;
1225       }
1226       else {
1227         bg_data = MEM_mallocN(sizeof(CameraEngineBGData), __func__);
1228         BLI_addtail(&camera_engine_data->bg_data, BLI_genericNodeN(bg_data));
1229       }
1230
1231       /* calculate the transformation matric for the current bg image */
1232       float uv2img_space[4][4];
1233       float img2cam_space[4][4];
1234       float rot_m4[4][4];
1235       float scale_m4[4][4];
1236       float translate_m4[4][4];
1237       float win_m4_scale[4][4];
1238       float win_m4_translate[4][4];
1239
1240       unit_m4(uv2img_space);
1241       unit_m4(img2cam_space);
1242       unit_m4(win_m4_scale);
1243       unit_m4(win_m4_translate);
1244       unit_m4(scale_m4);
1245       axis_angle_to_mat4_single(rot_m4, 'Z', -bgpic->rotation);
1246       unit_m4(translate_m4);
1247
1248       const float *size = DRW_viewport_size_get();
1249       float camera_aspect_x = 1.0;
1250       float camera_aspect_y = 1.0;
1251       float camera_offset_x = 0.0;
1252       float camera_offset_y = 0.0;
1253       float camera_aspect = 1.0;
1254       float camera_width = size[0];
1255       float camera_height = size[1];
1256
1257       if (!DRW_state_is_image_render()) {
1258         rctf render_border;
1259         ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &render_border, false);
1260         camera_width = render_border.xmax - render_border.xmin;
1261         camera_height = render_border.ymax - render_border.ymin;
1262         camera_aspect = camera_width / camera_height;
1263         const float camera_aspect_center_x = (render_border.xmax + render_border.xmin) / 2.0;
1264         const float camera_aspect_center_y = (render_border.ymax + render_border.ymin) / 2.0;
1265
1266         camera_aspect_x = camera_width / size[0];
1267         camera_aspect_y = camera_height / size[1];
1268         win_m4_scale[0][0] = camera_aspect_x;
1269         win_m4_scale[1][1] = camera_aspect_y;
1270
1271         camera_offset_x = (camera_aspect_center_x - (ar->winx / 2.0)) /
1272                           (0.5 * camera_width / camera_aspect_x);
1273         camera_offset_y = (camera_aspect_center_y - (ar->winy / 2.0)) /
1274                           (0.5 * camera_height / camera_aspect_y);
1275         win_m4_translate[3][0] = camera_offset_x;
1276         win_m4_translate[3][1] = camera_offset_y;
1277       }
1278
1279       /* Convert from uv space to image space -0.5..-.5 */
1280       uv2img_space[0][0] = image_width;
1281       uv2img_space[1][1] = image_height;
1282
1283       img2cam_space[0][0] = (1.0 / image_width);
1284       img2cam_space[1][1] = (1.0 / image_height);
1285
1286       /* Update scaling based on image and camera framing */
1287       float scale_x = bgpic->scale;
1288       float scale_y = bgpic->scale;
1289
1290       if (bgpic->flag & CAM_BGIMG_FLAG_CAMERA_ASPECT) {
1291         float fit_scale = image_aspect / camera_aspect;
1292         if (bgpic->flag & CAM_BGIMG_FLAG_CAMERA_CROP) {
1293           if (image_aspect > camera_aspect) {
1294             scale_x *= fit_scale;
1295           }
1296           else {
1297             scale_y /= fit_scale;
1298           }
1299         }
1300         else {
1301           if (image_aspect > camera_aspect) {
1302             scale_y /= fit_scale;
1303           }
1304           else {
1305             scale_x *= fit_scale;
1306           }
1307         }
1308       }
1309
1310       // scale image to match the desired aspect ratio
1311       scale_m4[0][0] = scale_x;
1312       scale_m4[1][1] = scale_y;
1313
1314       /* Translate, using coordinates that aren't squashed by the aspect. */
1315       translate_m4[3][0] = bgpic->offset[0] * 2.0f * max_ff(1.0f, 1.0f / camera_aspect);
1316       translate_m4[3][1] = bgpic->offset[1] * 2.0f * max_ff(1.0f, camera_aspect);
1317
1318       mul_m4_series(bg_data->transform_mat,
1319                     win_m4_translate,
1320                     win_m4_scale,
1321                     translate_m4,
1322                     img2cam_space,
1323                     scale_m4,
1324                     rot_m4,
1325                     uv2img_space);
1326
1327       DRWPass *pass = (bgpic->flag & CAM_BGIMG_FLAG_FOREGROUND) ? psl->camera_images_front :
1328                                                                   psl->camera_images_back;
1329       GPUShader *shader = DRW_state_do_color_management() ? sh_data->object_camera_image_cm :
1330                                                             sh_data->object_camera_image;
1331       DRWShadingGroup *grp = DRW_shgroup_create(shader, pass);
1332
1333       DRW_shgroup_uniform_float_copy(
1334           grp, "depth", (bgpic->flag & CAM_BGIMG_FLAG_FOREGROUND) ? 0.000001 : 0.999999);
1335       DRW_shgroup_uniform_float_copy(grp, "alpha", bgpic->alpha);
1336       DRW_shgroup_uniform_texture(grp, "image", tex);
1337       DRW_shgroup_uniform_bool_copy(grp, "imagePremultiplied", premultiplied);
1338
1339       DRW_shgroup_uniform_float_copy(
1340           grp, "flipX", (bgpic->flag & CAM_BGIMG_FLAG_FLIP_X) ? -1.0 : 1.0);
1341       DRW_shgroup_uniform_float_copy(
1342           grp, "flipY", (bgpic->flag & CAM_BGIMG_FLAG_FLIP_Y) ? -1.0 : 1.0);
1343       DRW_shgroup_uniform_mat4(grp, "TransformMat", bg_data->transform_mat);
1344
1345       DRW_shgroup_call(grp, batch, NULL);
1346     }
1347   }
1348 }
1349
1350 static void camera_background_images_free_textures(void)
1351 {
1352   for (LinkData *link = e_data.movie_clips.first; link; link = link->next) {
1353     MovieClip *clip = (MovieClip *)link->data;
1354     GPU_free_texture_movieclip(clip);
1355   }
1356   BLI_freelistN(&e_data.movie_clips);
1357 }
1358
1359 static void OBJECT_cache_init(void *vedata)
1360 {
1361   const GlobalsUboStorage *gb = &G_draw.block;
1362   OBJECT_PassList *psl = ((OBJECT_Data *)vedata)->psl;
1363   OBJECT_StorageList *stl = ((OBJECT_Data *)vedata)->stl;
1364   DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
1365   OBJECT_PrivateData *g_data;
1366   const DRWContextState *draw_ctx = DRW_context_state_get();
1367   OBJECT_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
1368
1369   const float outline_width = UI_GetThemeValuef(TH_OUTLINE_WIDTH);
1370   const bool do_outline_expand = (U.pixelsize > 1.0) || (outline_width > 2.0f);
1371   const bool do_large_expand = ((U.pixelsize > 1.0) && (outline_width > 2.0f)) ||
1372                                (outline_width > 4.0f);
1373
1374   if (!stl->g_data) {
1375     /* Alloc transient pointers */
1376     stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
1377   }
1378
1379   g_data = stl->g_data;
1380   g_data->xray_enabled = XRAY_ACTIVE(draw_ctx->v3d);
1381   g_data->xray_enabled_and_not_wire = g_data->xray_enabled &&
1382                                       draw_ctx->v3d->shading.type > OB_WIRE;
1383
1384   g_data->custom_shapes = BLI_ghash_ptr_new(__func__);
1385
1386   {
1387     DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL;
1388     psl->outlines = DRW_pass_create("Outlines Depth Pass", state);
1389
1390     GPUShader *sh = sh_data->outline_prepass;
1391
1392     if (g_data->xray_enabled_and_not_wire) {
1393       sh = sh_data->outline_prepass_wire;
1394     }
1395
1396     g_data->outlines_select = shgroup_outline(
1397         psl->outlines, &g_data->id_ofs_select, sh, draw_ctx->sh_cfg);
1398     g_data->outlines_select_dupli = shgroup_outline(
1399         psl->outlines, &g_data->id_ofs_select_dupli, sh, draw_ctx->sh_cfg);
1400     g_data->outlines_transform = shgroup_outline(
1401         psl->outlines, &g_data->id_ofs_transform, sh, draw_ctx->sh_cfg);
1402     g_data->outlines_active = shgroup_outline(
1403         psl->outlines, &g_data->id_ofs_active, sh, draw_ctx->sh_cfg);
1404
1405     g_data->id_ofs_select = 0;
1406     g_data->id_ofs_select_dupli = 0;
1407     g_data->id_ofs_active = 0;
1408     g_data->id_ofs_transform = 0;
1409   }
1410
1411   {
1412     DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL;
1413     DRWPass *pass = psl->lightprobes = DRW_pass_create("Object Probe Pass", state);
1414     struct GPUBatch *sphere = DRW_cache_sphere_get();
1415     struct GPUBatch *quad = DRW_cache_quad_get();
1416
1417     /* Cubemap */
1418     g_data->lightprobes_cube_select = buffer_instance_outline(
1419         pass, sphere, &g_data->id_ofs_prb_select, draw_ctx->sh_cfg);
1420     g_data->lightprobes_cube_select_dupli = buffer_instance_outline(
1421         pass, sphere, &g_data->id_ofs_prb_select_dupli, draw_ctx->sh_cfg);
1422     g_data->lightprobes_cube_active = buffer_instance_outline(
1423         pass, sphere, &g_data->id_ofs_prb_active, draw_ctx->sh_cfg);
1424     g_data->lightprobes_cube_transform = buffer_instance_outline(
1425         pass, sphere, &g_data->id_ofs_prb_transform, draw_ctx->sh_cfg);
1426
1427     /* Planar */
1428     g_data->lightprobes_planar_select = buffer_instance_outline(
1429         pass, quad, &g_data->id_ofs_prb_select, draw_ctx->sh_cfg);
1430     g_data->lightprobes_planar_select_dupli = buffer_instance_outline(
1431         pass, quad, &g_data->id_ofs_prb_select_dupli, draw_ctx->sh_cfg);
1432     g_data->lightprobes_planar_active = buffer_instance_outline(
1433         pass, quad, &g_data->id_ofs_prb_active, draw_ctx->sh_cfg);
1434     g_data->lightprobes_planar_transform = buffer_instance_outline(
1435         pass, quad, &g_data->id_ofs_prb_transform, draw_ctx->sh_cfg);
1436
1437     g_data->id_ofs_prb_select = 0;
1438     g_data->id_ofs_prb_select_dupli = 0;
1439     g_data->id_ofs_prb_active = 0;
1440     g_data->id_ofs_prb_transform = 0;
1441   }
1442
1443   {
1444     DRWState state = DRW_STATE_WRITE_COLOR;
1445     struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
1446     /* Don't occlude the "outline" detection pass if in xray mode (too much flickering). */
1447     float alphaOcclu = (g_data->xray_enabled) ? 1.0f : 0.35f;
1448
1449     psl->outlines_search = DRW_pass_create("Outlines Detect Pass", state);
1450
1451     GPUShader *sh = (g_data->xray_enabled_and_not_wire) ? sh_data->outline_detect_wire :
1452                                                           sh_data->outline_detect;
1453     DRWShadingGroup *grp = DRW_shgroup_create(sh, psl->outlines_search);
1454     DRW_shgroup_uniform_texture_ref(grp, "outlineId", &e_data.outlines_id_tx);
1455     DRW_shgroup_uniform_texture_ref(grp, "outlineDepth", &e_data.outlines_depth_tx);
1456     DRW_shgroup_uniform_texture_ref(grp, "sceneDepth", &dtxl->depth);
1457     DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
1458     DRW_shgroup_uniform_float_copy(grp, "alphaOcclu", alphaOcclu);
1459     DRW_shgroup_uniform_int(grp, "idOffsets", &stl->g_data->id_ofs_active, 4);
1460     DRW_shgroup_call(grp, quad, NULL);
1461
1462     /* This is the bleed pass if do_outline_expand is false. */
1463     GPUShader *fade_sh = (do_large_expand) ? sh_data->outline_fade_large : sh_data->outline_fade;
1464     psl->outlines_expand = DRW_pass_create("Outlines Expand Pass", state);
1465
1466     grp = DRW_shgroup_create(fade_sh, psl->outlines_expand);
1467     DRW_shgroup_uniform_texture_ref(grp, "outlineColor", &e_data.outlines_blur_tx);
1468     DRW_shgroup_uniform_bool_copy(grp, "doExpand", do_outline_expand);
1469     DRW_shgroup_call(grp, quad, NULL);
1470
1471     psl->outlines_bleed = DRW_pass_create("Outlines Bleed Pass", state);
1472
1473     if (do_outline_expand) {
1474       grp = DRW_shgroup_create(sh_data->outline_fade, psl->outlines_bleed);
1475       DRW_shgroup_uniform_texture_ref(grp, "outlineColor", &e_data.outlines_color_tx);
1476       DRW_shgroup_uniform_bool_copy(grp, "doExpand", false);
1477       DRW_shgroup_call(grp, quad, NULL);
1478     }
1479   }
1480
1481   {
1482     DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ALPHA;
1483     psl->outlines_resolve = DRW_pass_create("Outlines Resolve Pass", state);
1484
1485     struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
1486     GPUTexture **outline_tx = (do_outline_expand) ? &e_data.outlines_blur_tx :
1487                                                     &e_data.outlines_color_tx;
1488
1489     DRWShadingGroup *grp = DRW_shgroup_create(sh_data->outline_resolve_aa, psl->outlines_resolve);
1490     DRW_shgroup_uniform_texture_ref(grp, "outlineBluredColor", outline_tx);
1491     DRW_shgroup_uniform_vec2(grp, "rcpDimensions", e_data.inv_viewport_size, 1);
1492     DRW_shgroup_call(grp, quad, NULL);
1493   }
1494
1495   {
1496     /* Grid pass */
1497     DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ALPHA;
1498     psl->grid = DRW_pass_create("Infinite Grid Pass", state);
1499
1500     struct GPUBatch *geom = DRW_cache_grid_get();
1501     float grid_line_size = max_ff(0.0f, U.pixelsize - 1.0f) * 0.5f;
1502
1503     /* Create 3 quads to render ordered transparency Z axis */
1504     DRWShadingGroup *grp = DRW_shgroup_create(sh_data->grid, psl->grid);
1505     DRW_shgroup_uniform_int(grp, "gridFlag", &e_data.zneg_flag, 1);
1506     DRW_shgroup_uniform_vec3(grp, "planeAxes", e_data.zplane_axes, 1);
1507     DRW_shgroup_uniform_vec4(grp, "gridSettings", e_data.grid_settings, 1);
1508     DRW_shgroup_uniform_float_copy(grp, "lineKernel", grid_line_size);
1509     DRW_shgroup_uniform_float_copy(grp, "meshSize", e_data.grid_mesh_size);
1510     DRW_shgroup_uniform_float(grp, "gridOneOverLogSubdiv", &e_data.grid_settings[4], 1);
1511     DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
1512     DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
1513     DRW_shgroup_call(grp, geom, NULL);
1514
1515     grp = DRW_shgroup_create(sh_data->grid, psl->grid);
1516     DRW_shgroup_uniform_int(grp, "gridFlag", &e_data.grid_flag, 1);
1517     DRW_shgroup_uniform_vec3(grp, "planeAxes", e_data.grid_axes, 1);
1518     DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
1519     DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
1520     DRW_shgroup_call(grp, geom, NULL);
1521
1522     grp = DRW_shgroup_create(sh_data->grid, psl->grid);
1523     DRW_shgroup_uniform_int(grp, "gridFlag", &e_data.zpos_flag, 1);
1524     DRW_shgroup_uniform_vec3(grp, "planeAxes", e_data.zplane_axes, 1);
1525     DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
1526     DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
1527     DRW_shgroup_call(grp, geom, NULL);
1528   }
1529
1530   /* Camera background images */
1531   {
1532     DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND_ALPHA;
1533     psl->camera_images_back = DRW_pass_create("Camera Images Back", state);
1534     psl->camera_images_front = DRW_pass_create("Camera Images Front", state);
1535   }
1536
1537   for (int i = 0; i < 2; ++i) {
1538     OBJECT_ShadingGroupList *sgl = (i == 1) ? &stl->g_data->sgl_ghost : &stl->g_data->sgl;
1539
1540     /* Solid bones */
1541     DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL;
1542     sgl->bone_solid = psl->bone_solid[i] = DRW_pass_create("Bone Solid Pass", state);
1543     sgl->bone_outline = psl->bone_outline[i] = DRW_pass_create("Bone Outline Pass", state);
1544
1545     /* Wire bones */
1546     state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL |
1547             DRW_STATE_BLEND_ALPHA;
1548     sgl->bone_wire = psl->bone_wire[i] = DRW_pass_create("Bone Wire Pass", state);
1549
1550     /* distance outline around envelope bones */
1551     state = DRW_STATE_BLEND_ADD | DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL |
1552             DRW_STATE_CULL_FRONT;
1553     sgl->bone_envelope = psl->bone_envelope[i] = DRW_pass_create("Bone Envelope Outline Pass",
1554                                                                  state);
1555
1556     state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL;
1557     sgl->bone_axes = psl->bone_axes[i] = DRW_pass_create("Bone Axes Pass", state);
1558   }
1559
1560   for (int i = 0; i < 2; ++i) {
1561     OBJECT_ShadingGroupList *sgl = (i == 1) ? &stl->g_data->sgl_ghost : &stl->g_data->sgl;
1562
1563     /* Non Meshes Pass (Camera, empties, lights ...) */
1564     struct GPUBatch *geom;
1565     struct GPUShader *sh;
1566
1567     DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL |
1568                      DRW_STATE_BLEND_ALPHA;
1569     sgl->non_meshes = psl->non_meshes[i] = DRW_pass_create("Non Meshes Pass", state);
1570
1571     state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND_ALPHA;
1572     sgl->image_empties = psl->image_empties[i] = DRW_pass_create("Image Empties", state);
1573
1574     /* Empties */
1575     empties_callbuffers_create(sgl->non_meshes, &sgl->empties, draw_ctx->sh_cfg);
1576
1577     /* Force Field */
1578     geom = DRW_cache_field_wind_get();
1579     sgl->field_wind = buffer_instance_scaled(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1580
1581     geom = DRW_cache_field_force_get();
1582     sgl->field_force = buffer_instance_screen_aligned(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1583
1584     geom = DRW_cache_field_vortex_get();
1585     sgl->field_vortex = buffer_instance_scaled(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1586
1587     geom = DRW_cache_screenspace_circle_get();
1588     sgl->field_curve_sta = buffer_instance_screen_aligned(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1589
1590     /* Grease Pencil */
1591     geom = DRW_cache_gpencil_axes_get();
1592     sgl->gpencil_axes = buffer_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1593
1594     /* Speaker */
1595     geom = DRW_cache_speaker_get();
1596     sgl->speaker = buffer_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1597
1598     /* Probe */
1599     static float probeSize = 14.0f;
1600     geom = DRW_cache_lightprobe_cube_get();
1601     sgl->probe_cube = buffer_instance_screenspace(
1602         sgl->non_meshes, geom, &probeSize, draw_ctx->sh_cfg);
1603
1604     geom = DRW_cache_lightprobe_grid_get();
1605     sgl->probe_grid = buffer_instance_screenspace(
1606         sgl->non_meshes, geom, &probeSize, draw_ctx->sh_cfg);
1607
1608     static float probePlanarSize = 20.0f;
1609     geom = DRW_cache_lightprobe_planar_get();
1610     sgl->probe_planar = buffer_instance_screenspace(
1611         sgl->non_meshes, geom, &probePlanarSize, draw_ctx->sh_cfg);
1612
1613     /* Camera */
1614     geom = DRW_cache_camera_get();
1615     sgl->camera = buffer_camera_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1616
1617     geom = DRW_cache_camera_frame_get();
1618     sgl->camera_frame = buffer_camera_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1619
1620     geom = DRW_cache_camera_tria_get();
1621     sgl->camera_tria = buffer_camera_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1622
1623     geom = DRW_cache_plain_axes_get();
1624     sgl->camera_focus = buffer_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1625
1626     geom = DRW_cache_single_line_get();
1627     sgl->camera_clip = buffer_distance_lines_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1628     sgl->camera_mist = buffer_distance_lines_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1629
1630     geom = DRW_cache_single_line_endpoints_get();
1631     sgl->camera_clip_points = buffer_distance_lines_instance(
1632         sgl->non_meshes, geom, draw_ctx->sh_cfg);
1633     sgl->camera_mist_points = buffer_distance_lines_instance(
1634         sgl->non_meshes, geom, draw_ctx->sh_cfg);
1635
1636     geom = DRW_cache_quad_wires_get();
1637     sgl->camera_stereo_plane_wires = buffer_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1638
1639     geom = DRW_cache_empty_cube_get();
1640     sgl->camera_stereo_volume_wires = buffer_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1641
1642     BLI_listbase_clear(&sgl->camera_path);
1643
1644     /* Texture Space */
1645     geom = DRW_cache_empty_cube_get();
1646     sgl->texspace = buffer_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1647
1648     /* Wires (for loose edges) */
1649     sh = GPU_shader_get_builtin_shader_with_config(GPU_SHADER_3D_UNIFORM_COLOR, draw_ctx->sh_cfg);
1650     sgl->wire = shgroup_wire(sgl->non_meshes, gb->colorWire, sh, draw_ctx->sh_cfg);
1651     sgl->wire_select = shgroup_wire(sgl->non_meshes, gb->colorSelect, sh, draw_ctx->sh_cfg);
1652     sgl->wire_transform = shgroup_wire(sgl->non_meshes, gb->colorTransform, sh, draw_ctx->sh_cfg);
1653     sgl->wire_active = shgroup_wire(sgl->non_meshes, gb->colorActive, sh, draw_ctx->sh_cfg);
1654     /* Wire (duplicator) */
1655     sgl->wire_dupli = shgroup_wire(sgl->non_meshes, gb->colorDupli, sh, draw_ctx->sh_cfg);
1656     sgl->wire_dupli_select = shgroup_wire(
1657         sgl->non_meshes, gb->colorDupliSelect, sh, draw_ctx->sh_cfg);
1658
1659     /* Points (loose points) */
1660     sh = sh_data->loose_points;
1661     sgl->points = shgroup_points(sgl->non_meshes, gb->colorWire, sh, draw_ctx->sh_cfg);
1662     sgl->points_select = shgroup_points(sgl->non_meshes, gb->colorSelect, sh, draw_ctx->sh_cfg);
1663     sgl->points_transform = shgroup_points(
1664         sgl->non_meshes, gb->colorTransform, sh, draw_ctx->sh_cfg);
1665     sgl->points_active = shgroup_points(sgl->non_meshes, gb->colorActive, sh, draw_ctx->sh_cfg);
1666     /* Points (duplicator) */
1667     sgl->points_dupli = shgroup_points(sgl->non_meshes, gb->colorDupli, sh, draw_ctx->sh_cfg);
1668     sgl->points_dupli_select = shgroup_points(
1669         sgl->non_meshes, gb->colorDupliSelect, sh, draw_ctx->sh_cfg);
1670     DRW_shgroup_state_disable(sgl->points, DRW_STATE_BLEND_ALPHA);
1671     DRW_shgroup_state_disable(sgl->points_select, DRW_STATE_BLEND_ALPHA);
1672     DRW_shgroup_state_disable(sgl->points_transform, DRW_STATE_BLEND_ALPHA);
1673     DRW_shgroup_state_disable(sgl->points_active, DRW_STATE_BLEND_ALPHA);
1674     DRW_shgroup_state_disable(sgl->points_dupli, DRW_STATE_BLEND_ALPHA);
1675     DRW_shgroup_state_disable(sgl->points_dupli_select, DRW_STATE_BLEND_ALPHA);
1676
1677     /* Metaballs Handles */
1678     sgl->mball_handle = buffer_instance_mball_handles(sgl->non_meshes, draw_ctx->sh_cfg);
1679
1680     /* Lights */
1681     /* TODO
1682      * for now we create multiple times the same VBO with only light center coordinates
1683      * but ideally we would only create it once */
1684
1685     sh = GPU_shader_get_builtin_shader_with_config(
1686         GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA, draw_ctx->sh_cfg);
1687
1688     DRWShadingGroup *grp = DRW_shgroup_create(sh, sgl->non_meshes);
1689     DRW_shgroup_uniform_vec4(grp, "color", gb->colorLightNoAlpha, 1);
1690     DRW_shgroup_uniform_float(grp, "size", &gb->sizeLightCenter, 1);
1691     if (draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED) {
1692       DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
1693     }
1694
1695     sgl->light_center = buffer_dynpoints_uniform_color(grp);
1696
1697     geom = DRW_cache_single_line_get();
1698     sgl->light_buflimit = buffer_distance_lines_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1699
1700     geom = DRW_cache_light_get();
1701     sgl->light_circle = buffer_instance_screenspace(
1702         sgl->non_meshes, geom, &gb->sizeLightCircle, draw_ctx->sh_cfg);
1703     geom = DRW_cache_light_shadows_get();
1704     sgl->light_circle_shadow = buffer_instance_screenspace(
1705         sgl->non_meshes, geom, &gb->sizeLightCircleShadow, draw_ctx->sh_cfg);
1706
1707     geom = DRW_cache_light_sunrays_get();
1708     sgl->light_sunrays = buffer_instance_screenspace(
1709         sgl->non_meshes, geom, &gb->sizeLightCircle, draw_ctx->sh_cfg);
1710
1711     sgl->light_groundline = buffer_groundlines_uniform_color(
1712         sgl->non_meshes, gb->colorLight, draw_ctx->sh_cfg);
1713     sgl->light_groundpoint = buffer_groundpoints_uniform_color(
1714         sgl->non_meshes, gb->colorLight, draw_ctx->sh_cfg);
1715
1716     geom = DRW_cache_screenspace_circle_get();
1717     sgl->light_area_sphere = buffer_instance_screen_aligned(
1718         sgl->non_meshes, geom, draw_ctx->sh_cfg);
1719
1720     geom = DRW_cache_light_area_square_get();
1721     sgl->light_area_square = buffer_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1722
1723     geom = DRW_cache_light_area_disk_get();
1724     sgl->light_area_disk = buffer_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1725
1726     geom = DRW_cache_light_hemi_get();
1727     sgl->light_hemi = buffer_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1728
1729     geom = DRW_cache_single_line_get();
1730     sgl->light_distance = buffer_distance_lines_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1731
1732     geom = DRW_cache_single_line_endpoints_get();
1733     sgl->light_buflimit_points = buffer_distance_lines_instance(
1734         sgl->non_meshes, geom, draw_ctx->sh_cfg);
1735
1736     geom = DRW_cache_light_spot_get();
1737     sgl->light_spot_cone = buffer_spot_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1738
1739     geom = DRW_cache_circle_get();
1740     sgl->light_spot_blend = buffer_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1741
1742     geom = DRW_cache_light_spot_square_get();
1743     sgl->light_spot_pyramid = buffer_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1744
1745     geom = DRW_cache_square_get();
1746     sgl->light_spot_blend_rect = buffer_instance(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1747
1748     /* -------- STIPPLES ------- */
1749
1750     /* Relationship Lines */
1751     sgl->relationship_lines = buffer_dynlines_dashed_uniform_color(
1752         sgl->non_meshes, gb->colorWire, draw_ctx->sh_cfg);
1753     sgl->constraint_lines = buffer_dynlines_dashed_uniform_color(
1754         sgl->non_meshes, gb->colorGridAxisZ, draw_ctx->sh_cfg);
1755
1756     /* Force Field Curve Guide End (here because of stipple) */
1757     /* TODO port to shader stipple */
1758     geom = DRW_cache_screenspace_circle_get();
1759     sgl->field_curve_end = buffer_instance_screen_aligned(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1760
1761     /* Force Field Limits */
1762     /* TODO port to shader stipple */
1763     geom = DRW_cache_field_tube_limit_get();
1764     sgl->field_tube_limit = buffer_instance_scaled(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1765
1766     /* TODO port to shader stipple */
1767     geom = DRW_cache_field_cone_limit_get();
1768     sgl->field_cone_limit = buffer_instance_scaled(sgl->non_meshes, geom, draw_ctx->sh_cfg);
1769
1770     /* Transparent Shapes */
1771     state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND_ALPHA |
1772             DRW_STATE_CULL_FRONT;
1773     sgl->transp_shapes = psl->transp_shapes[i] = DRW_pass_create("Transparent Shapes", state);
1774
1775     sh = GPU_shader_get_builtin_shader_with_config(
1776         GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE, draw_ctx->sh_cfg);
1777
1778     DRWShadingGroup *grp_transp = DRW_shgroup_create(sh, sgl->transp_shapes);
1779     if (draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED) {
1780       DRW_shgroup_state_enable(grp_transp, DRW_STATE_CLIP_PLANES);
1781     }
1782
1783     DRWShadingGroup *grp_cull_back = DRW_shgroup_create_sub(grp_transp);
1784     DRW_shgroup_state_disable(grp_cull_back, DRW_STATE_CULL_FRONT);
1785     DRW_shgroup_state_enable(grp_cull_back, DRW_STATE_CULL_BACK);
1786
1787     DRWShadingGroup *grp_cull_none = DRW_shgroup_create_sub(grp_transp);
1788     DRW_shgroup_state_disable(grp_cull_none, DRW_STATE_CULL_FRONT);
1789
1790     /* Spot cones */
1791     geom = DRW_cache_light_spot_volume_get();
1792     sgl->light_spot_volume = buffer_instance_alpha(grp_transp, geom);
1793
1794     geom = DRW_cache_light_spot_square_volume_get();
1795     sgl->light_spot_volume_rect = buffer_instance_alpha(grp_transp, geom);
1796
1797     geom = DRW_cache_light_spot_volume_get();
1798     sgl->light_spot_volume_outside = buffer_instance_alpha(grp_cull_back, geom);
1799
1800     geom = DRW_cache_light_spot_square_volume_get();
1801     sgl->light_spot_volume_rect_outside = buffer_instance_alpha(grp_cull_back, geom);
1802
1803     /* Camera stereo volumes */
1804     geom = DRW_cache_cube_get();
1805     sgl->camera_stereo_volume = buffer_instance_alpha(grp_transp, geom);
1806
1807     geom = DRW_cache_quad_get();
1808     sgl->camera_stereo_plane = buffer_instance_alpha(grp_cull_none, geom);
1809   }
1810
1811   {
1812     /* Object Center pass grouped by State */
1813     DRWShadingGroup *grp;
1814     static float outlineWidth, size;
1815
1816     DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ALPHA;
1817     psl->ob_center = DRW_pass_create("Obj Center Pass", state);
1818
1819     outlineWidth = 1.0f * U.pixelsize;
1820     size = UI_GetThemeValuef(TH_OBCENTER_DIA) * U.pixelsize + outlineWidth;
1821
1822     GPUShader *sh = GPU_shader_get_builtin_shader_with_config(
1823         GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA, draw_ctx->sh_cfg);
1824
1825     /* Active */
1826     grp = DRW_shgroup_create(sh, psl->ob_center);
1827     DRW_shgroup_uniform_float(grp, "size", &size, 1);
1828     DRW_shgroup_uniform_float(grp, "outlineWidth", &outlineWidth, 1);
1829     DRW_shgroup_uniform_vec4(grp, "color", gb->colorActive, 1);
1830     DRW_shgroup_uniform_vec4(grp, "outlineColor", gb->colorOutline, 1);
1831     if (draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED) {
1832       DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
1833     }
1834     /* TODO find better name. */
1835     stl->g_data->center_active = buffer_dynpoints_uniform_color(grp);
1836
1837     /* Select */
1838     grp = DRW_shgroup_create_sub(grp);
1839     DRW_shgroup_uniform_vec4(grp, "color", gb->colorSelect, 1);
1840     stl->g_data->center_selected = buffer_dynpoints_uniform_color(grp);
1841
1842     /* Deselect */
1843     grp = DRW_shgroup_create_sub(grp);
1844     DRW_shgroup_uniform_vec4(grp, "color", gb->colorDeselect, 1);
1845     stl->g_data->center_deselected = buffer_dynpoints_uniform_color(grp);
1846
1847     /* Select (library) */
1848     grp = DRW_shgroup_create_sub(grp);
1849     DRW_shgroup_uniform_vec4(grp, "color", gb->colorLibrarySelect, 1);
1850     stl->g_data->center_selected_lib = buffer_dynpoints_uniform_color(grp);
1851
1852     /* Deselect (library) */
1853     grp = DRW_shgroup_create_sub(grp);
1854     DRW_shgroup_uniform_vec4(grp, "color", gb->colorLibrary, 1);
1855     stl->g_data->center_deselected_lib = buffer_dynpoints_uniform_color(grp);
1856   }
1857
1858   {
1859     /* Particle Pass */
1860     DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL |
1861                      DRW_STATE_BLEND_ALPHA;
1862     psl->particle = DRW_pass_create("Particle Pass", state);
1863   }
1864 }
1865
1866 static void DRW_shgroup_mball_handles(OBJECT_ShadingGroupList *sgl,
1867                                       Object *ob,
1868                                       ViewLayer *view_layer)
1869 {
1870   MetaBall *mb = ob->data;
1871
1872   float *color;
1873   DRW_object_wire_theme_get(ob, view_layer, &color);
1874
1875   float draw_scale_xform[3][4]; /* Matrix of Scale and Translation */
1876   {
1877     float scamat[3][3];
1878     copy_m3_m4(scamat, ob->obmat);
1879     /* Get the normalized inverse matrix to extract only
1880      * the scale of Scamat */
1881     float iscamat[3][3];
1882     invert_m3_m3(iscamat, scamat);
1883     normalize_m3(iscamat);
1884     mul_m3_m3_post(scamat, iscamat);
1885
1886     copy_v3_v3(draw_scale_xform[0], scamat[0]);
1887     copy_v3_v3(draw_scale_xform[1], scamat[1]);
1888     copy_v3_v3(draw_scale_xform[2], scamat[2]);
1889   }
1890
1891   for (MetaElem *ml = mb->elems.first; ml != NULL; ml = ml->next) {
1892     /* draw radius */
1893     float world_pos[3];
1894     mul_v3_m4v3(world_pos, ob->obmat, &ml->x);
1895     draw_scale_xform[0][3] = world_pos[0];
1896     draw_scale_xform[1][3] = world_pos[1];
1897     draw_scale_xform[2][3] = world_pos[2];
1898
1899     DRW_buffer_add_entry(sgl->mball_handle, draw_scale_xform, &ml->rad, color);
1900   }
1901 }
1902
1903 static void DRW_shgroup_light(OBJECT_ShadingGroupList *sgl, Object *ob, ViewLayer *view_layer)
1904 {
1905   Light *la = ob->data;
1906   float *color;
1907   int theme_id = DRW_object_wire_theme_get(ob, view_layer, &color);
1908   static float zero = 0.0f;
1909
1910   typedef struct LightEngineData {
1911     DrawData dd;
1912     float shape_mat[4][4];
1913     float spot_blend_mat[4][4];
1914   } LightEngineData;
1915
1916   LightEngineData *light_engine_data = (LightEngineData *)DRW_drawdata_ensure(
1917       &ob->id, &draw_engine_object_type, sizeof(LightEngineData), NULL, NULL);
1918
1919   float(*shapemat)[4] = light_engine_data->shape_mat;
1920   float(*spotblendmat)[4] = light_engine_data->spot_blend_mat;
1921
1922   if ((ob->base_flag & (BASE_FROM_SET | BASE_FROM_DUPLI)) == 0) {
1923     /* Don't draw the center if it's selected or active */
1924     if (theme_id == TH_LIGHT) {
1925       DRW_buffer_add_entry(sgl->light_center, ob->obmat[3]);
1926     }
1927   }
1928
1929   /* First circle */
1930   DRW_buffer_add_entry(sgl->light_circle, ob->obmat[3], color);
1931
1932   /* draw dashed outer circle for shadow */
1933   DRW_buffer_add_entry(sgl->light_circle_shadow, ob->obmat[3], color);
1934
1935   /* Distance */
1936   if (ELEM(la->type, LA_SUN, LA_AREA)) {
1937     DRW_buffer_add_entry(sgl->light_distance, color, &zero, &la->dist, ob->obmat);
1938   }
1939
1940   copy_m4_m4(shapemat, ob->obmat);
1941
1942   if (la->type == LA_SUN) {
1943     DRW_buffer_add_entry(sgl->light_sunrays, ob->obmat[3], color);
1944   }
1945   else if (la->type == LA_SPOT) {
1946     float size[3], sizemat[4][4];
1947     static float one = 1.0f;
1948     float cone_inside[4] = {0.0f, 0.0f, 0.0f, 0.5f};
1949     float cone_outside[4] = {1.0f, 1.0f, 1.0f, 0.3f};
1950     float blend = 1.0f - pow2f(la->spotblend);
1951     size[0] = size[1] = sinf(la->spotsize * 0.5f) * la->dist;
1952     size[2] = cosf(la->spotsize * 0.5f) * la->dist;
1953
1954     size_to_mat4(sizemat, size);
1955     mul_m4_m4m4(shapemat, ob->obmat, sizemat);
1956
1957     size[0] = size[1] = blend;
1958     size[2] = 1.0f;
1959     size_to_mat4(sizemat, size);
1960     translate_m4(sizemat, 0.0f, 0.0f, -1.0f);
1961     rotate_m4(sizemat, 'X', (float)(M_PI / 2));
1962     mul_m4_m4m4(spotblendmat, shapemat, sizemat);
1963
1964     if (la->mode & LA_SQUARE) {
1965       DRW_buffer_add_entry(sgl->light_spot_pyramid, color, &one, shapemat);
1966
1967       /* hide line if it is zero size or overlaps with outer border,
1968        * previously it adjusted to always to show it but that seems
1969        * confusing because it doesn't show the actual blend size */
1970       if (blend != 0.0f && blend != 1.0f) {
1971         DRW_buffer_add_entry(sgl->light_spot_blend_rect, color, &one, spotblendmat);
1972       }
1973
1974       if (la->mode & LA_SHOW_CONE) {
1975
1976         DRW_buffer_add_entry(sgl->light_spot_volume_rect, cone_inside, &one, shapemat);
1977         DRW_buffer_add_entry(sgl->light_spot_volume_rect_outside, cone_outside, &one, shapemat);
1978       }
1979     }
1980     else {
1981       DRW_buffer_add_entry(sgl->light_spot_cone, color, shapemat);
1982
1983       /* hide line if it is zero size or overlaps with outer border,
1984        * previously it adjusted to always to show it but that seems
1985        * confusing because it doesn't show the actual blend size */
1986       if (blend != 0.0f && blend != 1.0f) {
1987         DRW_buffer_add_entry(sgl->light_spot_blend, color, &one, spotblendmat);
1988       }
1989
1990       if (la->mode & LA_SHOW_CONE) {
1991         DRW_buffer_add_entry(sgl->light_spot_volume, cone_inside, &one, shapemat);
1992         DRW_buffer_add_entry(sgl->light_spot_volume_outside, cone_outside, &one, shapemat);
1993       }
1994     }
1995
1996     DRW_buffer_add_entry(sgl->light_buflimit, color, &la->clipsta, &la->clipend, ob->obmat);
1997     DRW_buffer_add_entry(sgl->light_buflimit_points, color, &la->clipsta, &la->clipend, ob->obmat);
1998   }
1999   else if (la->type == LA_AREA) {
2000     float size[3] = {1.0f, 1.0f, 1.0f}, sizemat[4][4];
2001
2002     if (ELEM(la->area_shape, LA_AREA_RECT, LA_AREA_ELLIPSE)) {
2003       size[1] = la->area_sizey / la->area_size;
2004       size_to_mat4(sizemat, size);
2005       mul_m4_m4m4(shapemat, shapemat, sizemat);
2006     }
2007
2008     if (ELEM(la->area_shape, LA_AREA_DISK, LA_AREA_ELLIPSE)) {
2009       DRW_buffer_add_entry(sgl->light_area_disk, color, &la->area_size, shapemat);
2010     }
2011     else {
2012       DRW_buffer_add_entry(sgl->light_area_square, color, &la->area_size, shapemat);
2013     }
2014   }
2015
2016   if (ELEM(la->type, LA_LOCAL, LA_SPOT)) {
2017     /* We only want position not scale. */
2018     shapemat[0][0] = shapemat[1][1] = shapemat[2][2] = 1.0f;
2019     shapemat[0][1] = shapemat[0][2] = 0.0f;
2020     shapemat[1][0] = shapemat[1][2] = 0.0f;
2021     shapemat[2][0] = shapemat[2][1] = 0.0f;
2022     DRW_buffer_add_entry(sgl->light_area_sphere, color, &la->area_size, shapemat);
2023   }
2024
2025   /* Line and point going to the ground */
2026   DRW_buffer_add_entry(sgl->light_groundline, ob->obmat[3]);
2027   DRW_buffer_add_entry(sgl->light_groundpoint, ob->obmat[3]);
2028 }
2029
2030 static GPUBatch *batch_camera_path_get(ListBase *camera_paths,
2031                                        const MovieTrackingReconstruction *reconstruction)
2032 {
2033   GPUBatch *geom;
2034   static GPUVertFormat format = {0};
2035   static struct {
2036     uint pos;
2037   } attr_id;
2038   if (format.attr_len == 0) {
2039     attr_id.pos = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
2040   }
2041   GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format);
2042   GPU_vertbuf_data_alloc(vbo, reconstruction->camnr);
2043
2044   MovieReconstructedCamera *camera = reconstruction->cameras;
2045   for (int a = 0; a < reconstruction->camnr; a++, camera++) {
2046     GPU_vertbuf_attr_set(vbo, attr_id.pos, a, camera->mat[3]);
2047   }
2048
2049   geom = GPU_batch_create_ex(GPU_PRIM_LINE_STRIP, vbo, NULL, GPU_BATCH_OWNS_VBO);
2050
2051   /* Store the batch to do cleanup after drawing. */
2052   BLI_addtail(camera_paths, BLI_genericNodeN(geom));
2053   return geom;
2054 }
2055
2056 static void batch_camera_path_free(ListBase *camera_paths)
2057 {
2058   LinkData *link;
2059   while ((link = BLI_pophead(camera_paths))) {
2060     GPUBatch *camera_path = link->data;
2061     GPU_batch_discard(camera_path);
2062     MEM_freeN(link);
2063   }
2064 }
2065
2066 /**
2067  * Draw the stereo 3d support elements (cameras, plane, volume).
2068  * They are only visible when not looking through the camera:
2069  */
2070 static void camera_view3d_stereoscopy_display_extra(OBJECT_ShadingGroupList *sgl,
2071                                                     Scene *scene,
2072                                                     ViewLayer *view_layer,
2073                                                     View3D *v3d,
2074                                                     Object *ob,
2075                                                     Camera *cam,
2076                                                     const float vec[4][3],
2077                                                     float drawsize,
2078                                                     const float scale[3])
2079 {
2080   const bool is_select = DRW_state_is_select();
2081   static float drw_tria_dummy[2][2][2] = {{{0}}};
2082   const float fac = (cam->stereo.pivot == CAM_S3D_PIVOT_CENTER) ? 2.0f : 1.0f;
2083   float origin[2][3] = {{0}};
2084   const char *viewnames[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME};
2085
2086   const bool is_stereo3d_cameras = (v3d->stereo3d_flag & V3D_S3D_DISPCAMERAS) &&
2087                                    (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D);
2088   const bool is_stereo3d_plane = (v3d->stereo3d_flag & V3D_S3D_DISPPLANE) &&
2089                                  (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D);
2090   const bool is_stereo3d_volume = (v3d->stereo3d_flag & V3D_S3D_DISPVOLUME);
2091
2092   float *color;
2093   DRW_object_wire_theme_get(ob, view_layer, &color);
2094
2095   for (int eye = 0; eye < 2; eye++) {
2096     float obmat[4][4];
2097     ob = BKE_camera_multiview_render(scene, ob, viewnames[eye]);
2098
2099     BKE_camera_multiview_model_matrix_scaled(&scene->r, ob, viewnames[eye], obmat);
2100
2101     copy_v2_v2(cam->runtime.drw_corners[eye][0], vec[0]);
2102     copy_v2_v2(cam->runtime.drw_corners[eye][1], vec[1]);
2103     copy_v2_v2(cam->runtime.drw_corners[eye][2], vec[2]);
2104     copy_v2_v2(cam->runtime.drw_corners[eye][3], vec[3]);
2105
2106     cam->runtime.drw_depth[eye] = vec[0][2];
2107
2108     if (cam->stereo.convergence_mode == CAM_S3D_OFFAXIS) {
2109       const float shift_x = ((BKE_camera_multiview_shift_x(&scene->r, ob, viewnames[eye]) -
2110                               cam->shiftx) *
2111                              (drawsize * scale[0] * fac));
2112
2113       for (int i = 0; i < 4; i++) {
2114         cam->runtime.drw_corners[eye][i][0] += shift_x;
2115       }
2116     }
2117
2118     /* Dummy triangle, draw on top of existent lines so it is invisible. */
2119     copy_v2_v2(drw_tria_dummy[eye][0], cam->runtime.drw_corners[eye][0]);
2120     copy_v2_v2(drw_tria_dummy[eye][1], cam->runtime.drw_corners[eye][0]);
2121
2122     if (is_stereo3d_cameras) {
2123       DRW_buffer_add_entry(sgl->camera_frame,
2124                            color,
2125                            cam->runtime.drw_corners[eye],
2126                            &cam->runtime.drw_depth[eye],
2127                            cam->runtime.drw_tria,
2128                            obmat);
2129
2130       DRW_buffer_add_entry(sgl->camera,
2131                            color,
2132                            cam->runtime.drw_corners[eye],
2133                            &cam->runtime.drw_depth[eye],
2134                            drw_tria_dummy[eye],
2135                            obmat);
2136     }
2137
2138     /* Connecting line. */
2139     mul_m4_v3(obmat, origin[eye]);
2140   }
2141
2142   /* Draw connecting lines. */
2143   if (is_stereo3d_cameras) {
2144     DRW_buffer_add_entry(sgl->relationship_lines, origin[0]);
2145     DRW_buffer_add_entry(sgl->relationship_lines, origin[1]);
2146   }
2147
2148   /* Draw convergence plane. */
2149   if (is_stereo3d_plane && !is_select) {
2150     float convergence_plane[4][2];
2151     const float offset = cam->stereo.convergence_distance / cam->runtime.drw_depth[0];
2152
2153     for (int i = 0; i < 4; i++) {
2154       mid_v2_v2v2(
2155           convergence_plane[i], cam->runtime.drw_corners[0][i], cam->runtime.drw_corners[1][i]);
2156       mul_v2_fl(convergence_plane[i], offset);
2157     }
2158
2159     /* We are using a -1,1 quad for this shading group, so we need to
2160      * scale and transform it to match the convergence plane border. */
2161     static float one = 1.0f;
2162     float plane_mat[4][4], scale_mat[4][4];
2163     float scale_factor[3] = {1.0f, 1.0f, 1.0f};
2164     float color_plane[2][4] = {
2165         {0.0f, 0.0f, 0.0f, v3d->stereo3d_convergence_alpha},
2166         {0.0f, 0.0f, 0.0f, 1.0f},
2167     };
2168
2169     const float height = convergence_plane[1][1] - convergence_plane[0][1];
2170     const float width = convergence_plane[2][0] - convergence_plane[0][0];
2171
2172     scale_factor[0] = width * 0.5f;
2173     scale_factor[1] = height * 0.5f;
2174
2175     copy_m4_m4(plane_mat, cam->runtime.drw_normalmat);
2176     translate_m4(plane_mat, 0.0f, 0.0f, -cam->stereo.convergence_distance);
2177     size_to_mat4(scale_mat, scale_factor);
2178     mul_m4_m4_post(plane_mat, scale_mat);
2179     translate_m4(plane_mat, 2.0f * cam->shiftx, (width / height) * 2.0f * cam->shifty, 0.0f);
2180
2181     if (v3d->stereo3d_convergence_alpha > 0.0f) {
2182       DRW_buffer_add_entry(sgl->camera_stereo_plane, color_plane[0], &one, plane_mat);
2183     }
2184     DRW_buffer_add_entry(sgl->camera_stereo_plane_wires, color_plane[1], &one, plane_mat);
2185   }
2186
2187   /* Draw convergence volume. */
2188   if (is_stereo3d_volume && !is_select) {
2189     static float one = 1.0f;
2190     float color_volume[3][4] = {
2191         {0.0f, 1.0f, 1.0f, v3d->stereo3d_volume_alpha},
2192         {1.0f, 0.0f, 0.0f, v3d->stereo3d_volume_alpha},
2193         {0.0f, 0.0f, 0.0f, 1.0f},
2194     };
2195
2196     for (int eye = 0; eye < 2; eye++) {
2197       float winmat[4][4], viewinv[4][4], viewmat[4][4], persmat[4][4], persinv[4][4];
2198       ob = BKE_camera_multiview_render(scene, ob, viewnames[eye]);
2199
2200       BKE_camera_multiview_window_matrix(&scene->r, ob, viewnames[eye], winmat);
2201       BKE_camera_multiview_model_matrix(&scene->r, ob, viewnames[eye], viewinv);
2202
2203       invert_m4_m4(viewmat, viewinv);
2204       mul_m4_m4m4(persmat, winmat, viewmat);
2205       invert_m4_m4(persinv, persmat);
2206
2207       if (v3d->stereo3d_volume_alpha > 0.0f) {
2208         DRW_buffer_add_entry(sgl->camera_stereo_volume, color_volume[eye], &one, persinv);
2209       }
2210       DRW_buffer_add_entry(sgl->camera_stereo_volume_wires, color_volume[2], &one, persinv);
2211     }
2212   }
2213 }
2214
2215 static void camera_view3d_reconstruction(OBJECT_ShadingGroupList *sgl,
2216                                          Scene *scene,
2217                                          View3D *v3d,
2218                                          Object *camera_object,
2219                                          Object *ob,
2220                                          const float color[4],
2221                                          const bool is_select)
2222 {
2223   const DRWContextState *draw_ctx = DRW_context_state_get();
2224   Camera *cam = ob->data;
2225   const Object *orig_camera_object = DEG_get_original_object(camera_object);
2226
2227   if ((v3d->flag2 & V3D_SHOW_RECONSTRUCTION) == 0) {
2228     return;
2229   }
2230
2231   MovieClip *clip = BKE_object_movieclip_get(scene, ob, false);
2232   if (clip == NULL) {
2233     return;
2234   }
2235
2236   BLI_assert(BLI_listbase_is_empty(&sgl->camera_path));
2237   const bool is_solid_bundle = (v3d->bundle_drawtype == OB_EMPTY_SPHERE) &&
2238                                ((v3d->shading.type != OB_SOLID) || !XRAY_FLAG_ENABLED(v3d));
2239
2240   MovieTracking *tracking = &clip->tracking;
2241   /* Index must start in 1, to mimic BKE_tracking_track_get_indexed. */
2242   int track_index = 1;
2243
2244   uchar text_color_selected[4], text_color_unselected[4];
2245   float bundle_color_unselected[4], bundle_color_solid[4];
2246
2247   UI_GetThemeColor4ubv(TH_SELECT, text_color_selected);
2248   UI_GetThemeColor4ubv(TH_TEXT, text_color_unselected);
2249   UI_GetThemeColor4fv(TH_WIRE, bundle_color_unselected);
2250   UI_GetThemeColor4fv(TH_BUNDLE_SOLID, bundle_color_solid);
2251
2252   float camera_mat[4][4];
2253   BKE_tracking_get_camera_object_matrix(scene, ob, camera_mat);
2254
2255   float bundle_scale_mat[4][4];
2256   if (is_solid_bundle) {
2257     scale_m4_fl(bundle_scale_mat, v3d->bundle_size);
2258   }
2259
2260   for (MovieTrackingObject *tracking_object = tracking->objects.first; tracking_object != NULL;
2261        tracking_object = tracking_object->next) {
2262     float tracking_object_mat[4][4];
2263
2264     if (tracking_object->flag & TRACKING_OBJECT_CAMERA) {
2265       copy_m4_m4(tracking_object_mat, camera_mat);
2266     }
2267     else {
2268       const int framenr = BKE_movieclip_remap_scene_to_clip_frame(
2269           clip, DEG_get_ctime(draw_ctx->depsgraph));
2270       float object_mat[4][4];
2271       BKE_tracking_camera_get_reconstructed_interpolate(
2272           tracking, tracking_object, framenr, object_mat);
2273
2274       invert_m4(object_mat);
2275       mul_m4_m4m4(tracking_object_mat, cam->runtime.drw_normalmat, object_mat);
2276     }
2277
2278     ListBase *tracksbase = BKE_tracking_object_get_tracks(tracking, tracking_object);
2279     for (MovieTrackingTrack *track = tracksbase->first; track; track = track->next) {
2280
2281       if ((track->flag & TRACK_HAS_BUNDLE) == 0) {
2282         continue;
2283       }
2284
2285       bool is_selected = TRACK_SELECTED(track);
2286
2287       float bundle_mat[4][4];
2288       copy_m4_m4(bundle_mat, tracking_object_mat);
2289       translate_m4(bundle_mat, track->bundle_pos[0], track->bundle_pos[1], track->bundle_pos[2]);
2290
2291       const float *bundle_color;
2292       if (track->flag & TRACK_CUSTOMCOLOR) {
2293         bundle_color = track->color;
2294       }
2295       else if (is_solid_bundle) {
2296         bundle_color = bundle_color_solid;
2297       }
2298       else if (is_selected) {
2299         bundle_color = color;
2300       }
2301       else {
2302         bundle_color = bundle_color_unselected;
2303       }
2304
2305       if (is_select) {
2306         DRW_select_load_id(orig_camera_object->runtime.select_id | (track_index << 16));
2307         track_index++;
2308       }
2309
2310       if (is_solid_bundle) {
2311
2312         if (is_selected) {
2313           DRW_shgroup_empty_ex(sgl, bundle_mat, &v3d->bundle_size, v3d->bundle_drawtype, color);
2314         }
2315
2316         float bundle_color_v4[4] = {
2317             bundle_color[0],
2318             bundle_color[1],
2319             bundle_color[2],
2320             1.0f,
2321         };
2322
2323         mul_m4_m4m4(bundle_mat, bundle_mat, bundle_scale_mat);
2324         DRW_buffer_add_entry(sgl->empties.sphere_solid, bundle_mat, bundle_color_v4);
2325       }
2326       else {
2327         DRW_shgroup_empty_ex(
2328             sgl, bundle_mat, &v3d->bundle_size, v3d->bundle_drawtype, bundle_color);
2329       }
2330
2331       if ((v3d->flag2 & V3D_SHOW_BUNDLENAME) && !is_select) {
2332         struct DRWTextStore *dt = DRW_text_cache_ensure();
2333
2334         DRW_text_cache_add(dt,
2335                            bundle_mat[3],
2336                            track->name,
2337                            strlen(track->name),
2338                            10,
2339                            0,
2340                            DRW_TEXT_CACHE_GLOBALSPACE | DRW_TEXT_CACHE_STRING_PTR,
2341                            is_selected ? text_color_selected : text_color_unselected);
2342       }
2343     }
2344
2345     if ((v3d->flag2 & V3D_SHOW_CAMERAPATH) && (tracking_object->flag & TRACKING_OBJECT_CAMERA) &&
2346         !is_select) {
2347       MovieTrackingReconstruction *reconstruction;
2348       reconstruction = BKE_tracking_object_get_reconstruction(tracking, tracking_object);
2349
2350       if (reconstruction->camnr) {
2351         static float camera_path_color[4];
2352         UI_GetThemeColor4fv(TH_CAMERA_PATH, camera_path_color);
2353
2354         GPUBatch *geom = batch_camera_path_get(&sgl->camera_path, reconstruction);
2355         GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
2356         DRWShadingGroup *shading_group = DRW_shgroup_create(shader, sgl->non_meshes);
2357         DRW_shgroup_uniform_vec4(shading_group, "color", camera_path_color, 1);
2358         DRW_shgroup_call_obmat(shading_group, geom, camera_mat);
2359       }
2360     }
2361   }
2362 }
2363
2364 static void DRW_shgroup_camera(OBJECT_ShadingGroupList *sgl, Object *ob, ViewLayer *view_layer)
2365 {
2366   const DRWContextState *draw_ctx = DRW_context_state_get();
2367   View3D *v3d = draw_ctx->v3d;
2368   Scene *scene = draw_ctx->scene;
2369   RegionView3D *rv3d = draw_ctx->rv3d;
2370
2371   Camera *cam = ob->data;
2372   Object *camera_object = DEG_get_evaluated_object(draw_ctx->depsgraph, v3d->camera);
2373   const bool is_select = DRW_state_is_select();
2374   const bool is_active = (ob == camera_object);
2375   const bool look_through = (is_active && (rv3d->persp == RV3D_CAMOB));
2376
2377   const bool is_multiview = (scene->r.scemode & R_MULTIVIEW) != 0;
2378   const bool is_stereo3d_view = (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D);
2379   const bool is_stereo3d_display_extra = is_active && is_multiview && (!look_through) &&
2380                                          ((v3d->stereo3d_flag) != 0);
2381   const bool is_stereo3d_cameras = (ob == scene->camera) && is_multiview && is_stereo3d_view &&
2382                                    (v3d->stereo3d_flag & V3D_S3D_DISPCAMERAS);
2383   const bool is_selection_camera_stereo = is_select && look_through && is_multiview &&
2384                                           is_stereo3d_view;
2385
2386   float *color;
2387   DRW_object_wire_theme_get(ob, view_layer, &color);
2388
2389   float vec[4][3], asp[2], shift[2], scale[3], drawsize;
2390
2391   /* BKE_camera_multiview_model_matrix already accounts for scale, don't do it here. */
2392   if (is_selection_camera_stereo) {
2393     scale[0] = 1.0f;
2394     scale[1] = 1.0f;
2395     scale[2] = 1.0f;
2396   }
2397   else {
2398     scale[0] = 1.0f / len_v3(ob->obmat[0]);
2399     scale[1] = 1.0f / len_v3(ob->obmat[1]);
2400     scale[2] = 1.0f / len_v3(ob->obmat[2]);
2401   }
2402
2403   BKE_camera_view_frame_ex(
2404       scene, cam, cam->drawsize, look_through, scale, asp, shift, &drawsize, vec);
2405
2406   /* Frame coords */
2407   copy_v2_v2(cam->runtime.drw_corners[0][0], vec[0]);
2408   copy_v2_v2(cam->runtime.drw_corners[0][1], vec[1]);
2409   copy_v2_v2(cam->runtime.drw_corners[0][2], vec[2]);
2410   copy_v2_v2(cam->runtime.drw_corners[0][3], vec[3]);
2411
2412   /* depth */
2413   cam->runtime.drw_depth[0] = vec[0][2];
2414
2415   /* tria */
2416   cam->runtime.drw_tria[0][0] = shift[0] + ((0.7f * drawsize) * scale[0]);
2417   cam->runtime.drw_tria[0][1] = shift[1] + ((drawsize * (asp[1] + 0.1f)) * scale[1]);
2418   cam->runtime.drw_tria[1][0] = shift[0];
2419   cam->runtime.drw_tria[1][1] = shift[1] + ((1.1f * drawsize * (asp[1] + 0.7f)) * scale[1]);
2420
2421   if (look_through) {
2422     if (!DRW_state_is_image_render()) {
2423       /* Only draw the frame. */
2424       float mat[4][4];
2425       if (is_multiview) {
2426         const bool is_left = v3d->multiview_eye == STEREO_LEFT_ID;
2427         const char *view_name = is_left ? STEREO_LEFT_NAME : STEREO_RIGHT_NAME;
2428         BKE_camera_multiview_model_matrix(&scene->r, ob, view_name, mat);
2429         const float shiftx = BKE_camera_multiview_shift_x(&scene->r, ob, view_name);
2430         const float delta_shiftx = shiftx - cam->shiftx;
2431         const float width = cam->runtime.drw_corners[0][2][0] - cam->runtime.drw_corners[0][0][0];
2432         for (int i = 0; i < 4; i++) {
2433           cam->runtime.drw_corners[0][i][0] -= delta_shiftx * width;
2434         }
2435       }
2436       else {
2437         copy_m4_m4(mat, ob->obmat);
2438       }
2439
2440       DRW_buffer_add_entry(sgl->camera_frame,
2441                            color,
2442                            cam->runtime.drw_corners[0],
2443                            &cam->runtime.drw_depth[0],
2444                            cam->runtime.drw_tria,
2445                            mat);
2446     }
2447   }
2448   else {
2449     if (!is_stereo3d_cameras) {
2450       DRW_buffer_add_entry(sgl->camera,
2451                            color,
2452                            cam->runtime.drw_corners[0],
2453                            &cam->runtime.drw_depth[0],
2454                            cam->runtime.drw_tria,
2455                            ob->obmat);
2456     }
2457
2458     /* Active cam */
2459     if (is_active) {
2460       DRW_buffer_add_entry(sgl->camera_tria,
2461                            color,
2462                            cam->runtime.drw_corners[0],
2463                            &cam->runtime.drw_depth[0],
2464                            cam->runtime.drw_tria,
2465                            ob->obmat);
2466     }
2467   }
2468
2469   /* draw the rest in normalize object space */
2470   normalize_m4_m4(cam->runtime.drw_normalmat, ob->obmat);
2471
2472   if (cam->flag & CAM_SHOWLIMITS) {
2473     static float col[4] = {0.5f, 0.5f, 0.25f, 1.0f}, col_hi[4] = {1.0f, 1.0f, 0.5f, 1.0f};
2474     float sizemat[4][4], size[3] = {1.0f, 1.0f, 0.0f};
2475     float focusdist = BKE_camera_object_dof_distance(ob);
2476
2477     copy_m4_m4(cam->runtime.drw_focusmat, cam->runtime.drw_normalmat);
2478     translate_m4(cam->runtime.drw_focusmat, 0.0f, 0.0f, -focusdist);
2479     size_to_mat4(sizemat, size);
2480     mul_m4_m4m4(cam->runtime.drw_focusmat, cam->runtime.drw_focusmat, sizemat);
2481
2482     DRW_buffer_add_entry(
2483         sgl->camera_focus, (is_active ? col_hi : col), &cam->drawsize, cam->runtime.drw_focusmat);
2484
2485     DRW_buffer_add_entry(
2486         sgl->camera_clip, color, &cam->clip_start, &cam->clip_end, cam->runtime.drw_normalmat);
2487     DRW_buffer_add_entry(sgl->camera_clip_points,
2488                          (is_active ? col_hi : col),
2489                          &cam->clip_start,
2490                          &cam->clip_end,
2491                          cam->runtime.drw_normalmat);
2492   }
2493
2494   if (cam->flag & CAM_SHOWMIST) {
2495     World *world = scene->world;
2496
2497     if (world) {
2498       static float col[4] = {0.5f, 0.5f, 0.5f, 1.0f}, col_hi[4] = {1.0f, 1.0f, 1.0f, 1.0f};
2499       world->mistend = world->miststa + world->mistdist;
2500       DRW_buffer_add_entry(
2501           sgl->camera_mist, color, &world->miststa, &world->mistend, cam->runtime.drw_normalmat);
2502       DRW_buffer_add_entry(sgl->camera_mist_points,
2503                            (is_active ? col_hi : col),
2504                            &world->miststa,
2505                            &world->mistend,
2506                            cam->runtime.drw_normalmat);
2507     }
2508   }
2509
2510   /* Stereo cameras, volumes, plane drawing. */
2511   if (is_stereo3d_display_extra) {
2512     camera_view3d_stereoscopy_display_extra(
2513         sgl, scene, view_layer, v3d, ob, cam, vec, drawsize, scale);
2514   }
2515
2516   /* Motion Tracking. */
2517   camera_view3d_reconstruction(sgl, scene, v3d, camera_object, ob, color, is_select);
2518 }
2519
2520 static void DRW_shgroup_empty_ex(OBJECT_ShadingGroupList *sgl,
2521                                  const float mat[4][4],
2522                                  const float *draw_size,
2523                                  char draw_type,
2524                                  const float color[4])
2525 {
2526   DRWEmptiesBufferList *buffers = &sgl->empties;
2527   switch (draw_type) {
2528     case OB_PLAINAXES:
2529       DRW_buffer_add_entry(buffers->plain_axes, color, draw_size, mat);
2530       break;
2531     case OB_SINGLE_ARROW:
2532       DRW_buffer_add_entry(buffers->single_arrow, color, draw_size, mat);
2533       DRW_buffer_add_entry(buffers->single_arrow_line, color, draw_size, mat);
2534       break;
2535     case OB_CUBE:
2536       DRW_buffer_add_entry(buffers->cube, color, draw_size, mat);
2537       break;
2538     case OB_CIRCLE:
2539       DRW_buffer_add_entry(buffers->circle, color, draw_size, mat);
2540       break;
2541     case OB_EMPTY_SPHERE:
2542       DRW_buffer_add_entry(buffers->sphere, color, draw_size, mat);
2543       break;
2544     case OB_EMPTY_CONE:
2545       DRW_buffer_add_entry(buffers->cone, color, draw_size, mat);
2546       break;
2547     case OB_ARROWS:
2548       DRW_buffer_add_entry(buffers->empty_axes, color, draw_size, mat);
2549       break;
2550     case OB_EMPTY_IMAGE:
2551       BLI_assert(!"Should never happen, use DRW_shgroup_empty instead.");
2552       break;
2553   }
2554 }
2555
2556 static void DRW_shgroup_empty(OBJECT_Shaders *sh_data,
2557                               OBJECT_ShadingGroupList *sgl,
2558                               Object *ob,
2559                               ViewLayer *view_layer,
2560                               RegionView3D *rv3d,
2561                               eGPUShaderConfig sh_cfg)
2562 {
2563   float *color;
2564   DRW_object_wire_theme_get(ob, view_layer, &color);
2565
2566   switch (ob->empty_drawtype) {
2567     case OB_PLAINAXES:
2568     case OB_SINGLE_ARROW:
2569     case OB_CUBE:
2570     case OB_CIRCLE:
2571     case OB_EMPTY_SPHERE:
2572     case OB_EMPTY_CONE:
2573     case OB_ARROWS:
2574       DRW_shgroup_empty_ex(sgl, ob->obmat, &ob->empty_drawsize, ob->empty_drawtype, color);
2575       break;
2576     case OB_EMPTY_IMAGE:
2577       DRW_shgroup_empty_image(sh_data, sgl, ob, color, rv3d, sh_cfg);
2578       break;
2579   }
2580 }
2581
2582 static void DRW_shgroup_forcefield(OBJECT_ShadingGroupList *sgl, Object *ob, ViewLayer *view_layer)
2583 {
2584   int theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
2585   float *color = DRW_color_background_blend_get(theme_id);
2586   PartDeflect *pd = ob->pd;
2587   Curve *cu = (ob->type == OB_CURVE) ? ob->data : NULL;
2588
2589   /* TODO Move this to depsgraph */
2590   float tmp[3];
2591   copy_v3_fl(pd->drawvec1, ob->empty_drawsize);
2592
2593   switch (pd->forcefield) {
2594     case PFIELD_WIND:
2595       pd->drawvec1[2] = pd->f_strength;
2596       break;
2597     case PFIELD_VORTEX:
2598       if (pd->f_strength < 0.0f) {
2599         pd->drawvec1[1] = -pd->drawvec1[1];
2600       }
2601       break;
2602     case PFIELD_GUIDE:
2603       if (cu && (cu->flag & CU_PATH) && ob->runtime.curve_cache->path &&
2604           ob->runtime.curve_cache->path->data) {
2605         where_on_path(ob, 0.0f, pd->drawvec1, tmp, NULL, NULL, NULL);
2606         where_on_path(ob, 1.0f, pd->drawvec2, tmp, NULL, NULL, NULL);
2607       }
2608       break;
2609   }
2610
2611   if (pd->falloff == PFIELD_FALL_TUBE) {
2612     pd->drawvec_falloff_max[0] = pd->drawvec_falloff_max[1] = (pd->flag & PFIELD_USEMAXR) ?
2613                                                                   pd->maxrad :
2614                                                                   1.0f;
2615     pd->drawvec_falloff_max[2] = (pd->flag & PFIELD_USEMAX) ? pd->maxdist : 0.0f;
2616
2617     pd->drawvec_falloff_min[0] = pd->drawvec_falloff_min[1] = (pd->flag & PFIELD_USEMINR) ?
2618                                                                   pd->minrad :
2619                                                                   1.0f;
2620     pd->drawvec_falloff_min[2] = (pd->flag & PFIELD_USEMIN) ? pd->mindist : 0.0f;
2621   }
2622   else if (pd->falloff == PFIELD_FALL_CONE) {
2623     float radius, distance;
2624
2625     radius = DEG2RADF((pd->flag & PFIELD_USEMAXR) ? pd->maxrad : 1.0f);
2626     distance = (pd->flag & PFIELD_USEMAX) ? pd->maxdist : 0.0f;
2627     pd->drawvec_falloff_max[0] = pd->drawvec_falloff_max[1] = distance * sinf(radius);
2628     pd->drawvec_falloff_max[2] = distance * cosf(radius);
2629
2630     radius = DEG2RADF((pd->flag & PFIELD_USEMINR) ? pd->minrad : 1.0f);
2631     distance = (pd->flag & PFIELD_USEMIN) ? pd->mindist : 0.0f;
2632
2633     pd->drawvec_falloff_min[0] = pd->drawvec_falloff_min[1] = distance * sinf(radius);
2634     pd->drawvec_falloff_min[2] = distance * cosf(radius);
2635   }
2636   /* End of things that should go to depthgraph */
2637
2638   switch (pd->forcefield) {
2639     case PFIELD_WIND:
2640       DRW_buffer_add_entry(sgl->field_wind, color, &pd->drawvec1, ob->obmat);
2641       break;
2642     case PFIELD_FORCE:
2643       DRW_buffer_add_entry(sgl->field_force, color, &pd->drawvec1, ob->obmat);
2644       break;
2645     case PFIELD_VORTEX:
2646       DRW_buffer_add_entry(sgl->field_vortex, color, &pd->drawvec1, ob->obmat);
2647       break;
2648     case PFIELD_GUIDE:
2649       if (cu && (cu->flag & CU_PATH) && ob->runtime.curve_cache->path &&
2650           ob->runtime.curve_cache->path->data) {
2651         DRW_buffer_add_entry(sgl->field_curve_sta, color, &pd->f_strength, ob->obmat);
2652         DRW_buffer_add_entry(sgl->field_curve_end, color, &pd->f_strength, ob->obmat);
2653       }
2654       break;
2655   }
2656
2657   if (pd->falloff == PFIELD_FALL_SPHERE) {
2658     /* as last, guide curve alters it */
2659     if ((pd->flag & PFIELD_USEMAX) != 0) {
2660       DRW_buffer_add_entry(sgl->field_curve_end, color, &pd->maxdist, ob->obmat);
2661     }
2662
2663     if ((pd->flag & PFIELD_USEMIN) != 0) {
2664       DRW_buffer_add_entry(sgl->field_curve_end, color, &pd->mindist, ob->obmat);
2665     }
2666   }
2667   else if (pd->falloff == PFIELD_FALL_TUBE) {
2668     if (pd->flag & (PFIELD_USEMAX | PFIELD_USEMAXR)) {
2669       DRW_buffer_add_entry(sgl->field_tube_limit, color, &pd->drawvec_falloff_max, ob->obmat);
2670     }
2671
2672     if (pd->flag & (PFIELD_USEMIN | PFIELD_USEMINR)) {
2673       DRW_buffer_add_entry(sgl->field_tube_limit, color, &pd->drawvec_falloff_min, ob->obmat);
2674     }
2675   }
2676   else if (pd->falloff == PFIELD_FALL_CONE) {
2677     if (pd->flag & (PFIELD_USEMAX | PFIELD_USEMAXR)) {
2678       DRW_buffer_add_entry(sgl->field_cone_limit, color, &pd->drawvec_falloff_max, ob->obmat);
2679     }
2680
2681     if (pd->flag & (PFIELD_USEMIN | PFIELD_USEMINR)) {
2682       DRW_buffer_add_entry(sgl->field_cone_limit, color, &pd->drawvec_falloff_min, ob->obmat);
2683     }
2684   }
2685 }
2686
2687 static void DRW_shgroup_volume_extra(OBJECT_ShadingGroupList *sgl,
2688                                      Object *ob,
2689                                      ViewLayer *view_layer,
2690                                      Scene *scene,
2691                                      ModifierData *md)
2692 {
2693   SmokeModifierData *smd = (SmokeModifierData *)md;
2694   SmokeDomainSettings *sds = smd->domain;
2695   float *color;
2696   float one = 1.0f;
2697
2698   if (sds == NULL) {
2699     return;
2700   }
2701
2702   DRW_object_wire_theme_get(ob, view_layer, &color);
2703
2704   /* Small cube showing voxel size. */
2705   float voxel_cubemat[4][4] = {{0.0f}};
2706   voxel_cubemat[0][0] = 1.0f / (float)sds->res[0];
2707   voxel_cubemat[1][1] = 1.0f / (float)sds->res[1];
2708   voxel_cubemat[2][2] = 1.0f / (float)sds->res[2];
2709   voxel_cubemat[3][0] = voxel_cubemat[3][1] = voxel_cubemat[3][2] = -1.0f;
2710   voxel_cubemat[3][3] = 1.0f;
2711   translate_m4(voxel_cubemat, 1.0f, 1.0f, 1.0f);
2712   mul_m4_m4m4(voxel_cubemat, ob->obmat, voxel_cubemat);
2713
2714   DRW_buffer_add_entry(sgl->empties.cube, color, &one, voxel_cubemat);
2715
2716   /* Don't show smoke before simulation starts, this could be made an option in the future. */
2717   if (!sds->draw_velocity || !sds->fluid || CFRA < sds->point_cache[0]->startframe) {
2718     return;
2719   }
2720
2721   const bool use_needle = (sds->vector_draw_type == VECTOR_DRAW_NEEDLE);
2722   int line_count = (use_needle) ? 6 : 1;
2723   int slice_axis = -1;
2724   line_count *= sds->res[0] * sds->res[1] * sds->res[2];
2725
2726   if (sds->slice_method == MOD_SMOKE_SLICE_AXIS_ALIGNED &&
2727       sds->axis_slice_method == AXIS_SLICE_SINGLE) {
2728     float viewinv[4][4];
2729     DRW_view_viewmat_get(NULL, viewinv, true);
2730
2731     const int axis = (sds->slice_axis == SLICE_AXIS_AUTO) ? axis_dominant_v3_single(viewinv[2]) :
2732                                                             sds->slice_axis - 1;
2733     slice_axis = axis;
2734     line_count /= sds->res[axis];
2735   }
2736
2737   GPU_create_smoke_velocity(smd);
2738
2739   DRWShadingGroup *grp = DRW_shgroup_create(volume_velocity_shader_get(use_needle),
2740                                             sgl->non_meshes);
2741   DRW_shgroup_uniform_texture(grp, "velocityX", sds->tex_velocity_x);
2742   DRW_shgroup_uniform_texture(grp, "velocityY", sds->tex_velocity_y);
2743   DRW_shgroup_uniform_texture(grp, "velocityZ", sds->tex_velocity_z);
2744   DRW_shgroup_uniform_float_copy(grp, "displaySize", sds->vector_scale);
2745   DRW_shgroup_uniform_float_copy(grp, "slicePosition", sds->slice_depth);
2746   DRW_shgroup_uniform_int_copy(grp, "sliceAxis", slice_axis);
2747   DRW_shgroup_call_procedural_lines(grp, ob, line_count);
2748
2749   BLI_addtail(&e_data.smoke_domains, BLI_genericNodeN(smd));
2750 }
2751
2752 static void volumes_free_smoke_textures(void)
2753 {
2754   /* Free Smoke Textures after rendering */
2755   /* XXX This is a waste of processing and GPU bandwidth if nothing
2756    * is updated. But the problem is since Textures are stored in the
2757    * modifier we don't want them to take precious VRAM if the
2758    * modifier is not used for display. We should share them for
2759    * all viewport in a redraw at least. */
2760   for (LinkData *link = e_data.smoke_domains.first; link; link = link->next) {
2761     SmokeModifierData *smd = (SmokeModifierData *)link->data;
2762     GPU_free_smoke_velocity(smd);
2763   }
2764   BLI_freelistN(&e_data.smoke_domains);
2765 }
2766
2767 static void DRW_shgroup_speaker(OBJECT_ShadingGroupList *sgl, Object *ob, ViewLayer *view_layer)
2768 {
2769   float *color;
2770   static float one = 1.0f;
2771   DRW_object_wire_theme_get(ob, view_layer, &color);
2772
2773   DRW_buffer_add_entry(sgl->speaker, color, &one, ob->obmat);
2774 }
2775
2776 typedef struct OBJECT_LightProbeEngineData {
2777   DrawData dd;
2778
2779   float increment_x[3];
2780   float increment_y[3];
2781   float increment_z[3];
2782   float corner[3];
2783 } OBJECT_LightProbeEngineData;
2784
2785 static void DRW_shgroup_lightprobe(OBJECT_Shaders *sh_data,
2786                                    OBJECT_StorageList *stl,
2787                                    OBJECT_PassList *psl,
2788                                    Object *ob,
2789                                    ViewLayer *view_layer,
2790                                    const eGPUShaderConfig sh_cfg)
2791 {
2792   float *color;
2793   static float one = 1.0f;
2794   LightProbe *prb = (LightProbe *)ob->data;
2795   bool do_outlines = ((ob->base_flag & BASE_SELECTED) != 0);
2796   int theme_id = DRW_object_wire_theme_get(ob, view_layer, &color);
2797
2798   OBJECT_ShadingGroupList *sgl = (ob->dtx & OB_DRAWXRAY) ? &stl->g_data->sgl_ghost :
2799                                                            &stl->g_data->sgl;
2800
2801   OBJECT_LightProbeEngineData *prb_data = (OBJECT_LightProbeEngineData *)DRW_drawdata_ensure(
2802       &ob->id, &draw_engine_object_type, sizeof(OBJECT_LightProbeEngineData), NULL, NULL);
2803
2804   if (DRW_state_is_select() || do_outlines) {
2805     int *call_id = shgroup_theme_id_to_probe_outline_counter(stl, theme_id, ob->base_flag);
2806
2807     if (prb->type == LIGHTPROBE_TYPE_GRID) {
2808       /* Update transforms */
2809       float cell_dim[3], half_cell_dim[3];
2810       cell_dim[0] = 2.0f / (float)(prb->grid_resolution_x);
2811       cell_dim[1] = 2.0f / (float)(prb->grid_resolution_y);
2812       cell_dim[2] = 2.0f / (float)(prb->grid_resolution_z);
2813
2814       mul_v3_v3fl(half_cell_dim, cell_dim, 0.5f);
2815
2816       /* First cell. */
2817       copy_v3_fl(prb_data->corner, -1.0f);
2818       add_v3_v3(prb_data->corner, half_cell_dim);
2819       mul_m4_v3(ob->obmat, prb_data->corner);
2820
2821       /* Opposite neighbor cell. */
2822       copy_v3_fl3(prb_data->increment_x, cell_dim[0], 0.0f, 0.0f);
2823       add_v3_v3(prb_data->increment_x, half_cell_dim);
2824       add_v3_fl(prb_data->increment_x, -1.0f);
2825       mul_m4_v3(ob->obmat, prb_data->increment_x);
2826       sub_v3_v3(prb_data->increment_x, prb_data->corner);
2827
2828       copy_v3_fl3(prb_data->increment_y, 0.0f, cell_dim[1], 0.0f);
2829       add_v3_v3(prb_data->increment_y, half_cell_dim);
2830       add_v3_fl(prb_data->increment_y, -1.0f);
2831       mul_m4_v3(ob->obmat, prb_data->increment_y);
2832       sub_v3_v3(prb_data->increment_y, prb_data->corner);
2833
2834       copy_v3_fl3(prb_data->increment_z, 0.0f, 0.0f, cell_dim[2]);
2835       add_v3_v3(prb_data->increment_z, half_cell_dim);
2836       add_v3_fl(prb_data->increment_z, -1.0f);
2837       mul_m4_v3(ob->obmat, prb_data->increment_z);
2838       sub_v3_v3(prb_data->increment_z, prb_data->corner);
2839
2840       uint cell_count = prb->grid_resolution_x * prb->grid_resolution_y * prb->grid_resolution_z;
2841       DRWShadingGroup *grp = DRW_shgroup_create(sh_data->lightprobe_grid, psl->lightprobes);
2842       DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
2843       DRW_shgroup_uniform_int_copy(grp, "call_id", *call_id);
2844       DRW_shgroup_uniform_int(grp, "baseId", call_id, 1); /* that's correct */
2845       DRW_shgroup_uniform_vec3(grp, "corner", prb_data->corner, 1);
2846       DRW_shgroup_uniform_vec3(grp, "increment_x", prb_data->increment_x, 1);
2847       DRW_shgroup_uniform_vec3(grp, "increment_y", prb_data->increment_y, 1);
2848       DRW_shgroup_uniform_vec3(grp, "increment_z", prb_data->increment_z, 1);
2849       DRW_shgroup_uniform_ivec3(grp, "grid_resolution", &prb->grid_resolution_x, 1);
2850       if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
2851         DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
2852       }
2853       DRW_shgroup_call_procedural_points(grp, NULL, cell_count);
2854       *call_id += 1;
2855     }
2856     else if (prb->type == LIGHTPROBE_TYPE_CUBE) {
2857       float draw_size = 1.0f;
2858       float probe_cube_mat[4][4];
2859       // prb_data->draw_size = prb->data_draw_size * 0.1f;
2860       // unit_m4(prb_data->probe_cube_mat);
2861       // copy_v3_v3(prb_data->probe_cube_mat[3], ob->obmat[3]);
2862
2863       DRWCallBuffer *buf = buffer_theme_id_to_probe_cube_outline_shgrp(
2864           stl, theme_id, ob->base_flag);
2865       /* TODO remove or change the drawing of the cube probes. Theses line draws nothing on purpose
2866        * to keep the call ids correct. */
2867       zero_m4(probe_cube_mat);
2868       DRW_buffer_add_entry(buf, call_id, &draw_size, probe_cube_mat);
2869       *call_id += 1;
2870     }
2871     else if (prb->flag & LIGHTPROBE_FLAG_SHOW_DATA) {
2872       float draw_size = 1.0f;
2873       DRWCallBuffer *buf = buffer_theme_id_to_probe_planar_outline_shgrp(stl, theme_id);
2874       DRW_buffer_add_entry(buf, call_id, &draw_size, ob->obmat);
2875       *call_id += 1;
2876     }
2877   }
2878
2879   switch (prb->type) {
2880     case LIGHTPROBE_TYPE_PLANAR:
2881       DRW_buffer_add_entry(sgl->probe_planar, ob->obmat[3], color);
2882       break;
2883     case LIGHTPROBE_TYPE_GRID:
2884       DRW_buffer_add_entry(sgl->probe_grid, ob->obmat[3], color);
2885       break;
2886     case LIGHTPROBE_TYPE_CUBE:
2887     default:
2888       DRW_buffer_add_entry(sgl->probe_cube, ob->obmat[3], color);
2889       break;
2890   }
2891
2892   if (prb->type == LIGHTPROBE_TYPE_PLANAR) {
2893     float mat[4][4];
2894     copy_m4_m4(mat, ob->obmat);
2895     normalize_m4(mat);
2896
2897     DRW_buffer_add_entry(sgl->empties.single_arrow, color, &ob->empty_drawsize, mat);
2898     DRW_buffer_add_entry(sgl->empties.single_arrow_line, color, &ob->empty_drawsize, mat);
2899
2900     copy_m4_m4(mat, ob->obmat);
2901     zero_v3(mat[2]);
2902
2903     DRW_buffer_add_entry(sgl->empties.cube, color, &one, mat);
2904   }
2905
2906   if ((prb->flag & LIGHTPROBE_FLAG_SHOW_INFLUENCE) != 0) {
2907
2908     prb->distfalloff = (1.0f - prb->falloff) * prb->distinf;
2909     prb->distgridinf = prb->distinf;
2910
2911     if (prb->type == LIGHTPROBE_TYPE_GRID) {
2912       prb->distfalloff += 1.0f;
2913       prb->distgridinf += 1.0f;
2914     }
2915
2916     if (prb->type == LIGHTPROBE_TYPE_GRID || prb->attenuation_type == LIGHTPROBE_SHAPE_BOX) {
2917       DRW_buffer_add_entry(sgl->empties.cube, color, &prb->distgridinf, ob->obmat);
2918       DRW_buffer_add_entry(sgl->empties.cube, color, &prb->distfalloff, ob->obmat);
2919     }
2920     else if (prb->type == LIGHTPROBE_TYPE_PLANAR) {
2921       float rangemat[4][4];
2922       copy_m4_m4(rangemat, ob->obmat);
2923       normalize_v3(rangemat[2]);
2924       mul_v3_fl(rangemat[2], prb->distinf);
2925
2926       DRW_buffer_add_entry(sgl->empties.cube, color, &one, rangemat);
2927
2928       copy_m4_m4(rangemat, ob->obmat);
2929       normalize_v3(rangemat[2]);
2930       mul_v3_fl(rangemat[2], prb->distfalloff);
2931
2932       DRW_buffer_add_entry(sgl->empties.cube, color, &one, rangemat);
2933     }
2934     else {
2935       DRW_buffer_add_entry(sgl->empties.sphere, color, &prb->distgridinf, ob->obmat);
2936       DRW_buffer_add_entry(sgl->empties.sphere, color, &prb->distfalloff, ob->obmat);
2937     }
2938   }
2939
2940   if ((prb->flag & LIGHTPROBE_FLAG_SHOW_PARALLAX) != 0) {
2941     if (prb->type != LIGHTPROBE_TYPE_PLANAR) {
2942       float(*obmat)[4], *dist;
2943
2944       if ((prb->flag & LIGHTPROBE_FLAG_CUSTOM_PARALLAX) != 0) {
2945         dist = &prb->distpar;
2946         /* TODO object parallax */
2947         obmat = ob->obmat;
2948       }
2949       else {
2950         dist = &prb->distinf;
2951         obmat = ob->obmat;
2952       }
2953
2954       if (prb->parallax_type == LIGHTPROBE_SHAPE_BOX) {
2955         DRW_buffer_add_entry(sgl->empties.cube, color, dist, obmat);
2956       }
2957       else {
2958         DRW_buffer_add_entry(sgl->empties.sphere, color, dist, obmat);
2959       }
2960     }
2961   }
2962
2963   if ((prb->flag & LIGHTPROBE_FLAG_SHOW_CLIP_DIST) != 0) {
2964     if (prb->type != LIGHTPROBE_TYPE_PLANAR) {
2965       static const float cubefacemat[6][4][4] = {
2966           {{0.0, 0.0, -1.0, 0.0},
2967            {0.0, -1.0, 0.0, 0.0},
2968            {-1.0, 0.0, 0.0, 0.0},
2969            {0.0, 0.0, 0.0, 1.0}},
2970           {{0.0, 0.0, 1.0, 0.0},
2971            {0.0, -1.0, 0.0, 0.0},
2972            {1.0, 0.0, 0.0, 0.0},
2973            {0.0, 0.0, 0.0, 1.0}},
2974           {{1.0, 0.0, 0.0, 0.0},
2975            {0.0, 0.0, -1.0, 0.0},
2976            {0.0, 1.0, 0.0, 0.0},
2977            {0.0, 0.0, 0.0, 1.0}},
2978           {{1.0, 0.0, 0.0, 0.0},
2979            {0.0, 0.0, 1.0, 0.0},
2980            {0.0, -1.0, 0.0, 0.0},
2981            {0.0, 0.0, 0.0, 1.0}},
2982           {{1.0, 0.0, 0.0, 0.0},
2983            {0.0, -1.0, 0.0, 0.0},
2984            {0.0, 0.0, -1.0, 0.0},
2985            {0.0, 0.0, 0.0, 1.0}},
2986           {{-1.0, 0.0, 0.0, 0.0},
2987            {0.0, -1.0, 0.0, 0.0},
2988            {0.0, 0.0, 1.0, 0.0},
2989            {0.0, 0.0, 0.0, 1.0}},
2990       };
2991
2992       for (int i = 0; i < 6; ++i) {
2993         float clipmat[4][4];
2994         normalize_m4_m4(clipmat, ob->obmat);
2995         mul_m4_m4m4(clipmat, clipmat, cubefacemat[i]);
2996
2997         DRW_buffer_add_entry(sgl->light_buflimit, color, &prb->clipsta, &prb->clipend, clipmat);
2998         DRW_buffer_add_entry(
2999             sgl->light_buflimit_points, color, &prb->clipsta, &prb->clipend, clipmat);
3000       }
3001     }
3002   }
3003
3004   /* Line and point going to the ground */
3005   if (prb->type == LIGHTPROBE_TYPE_CUBE) {
3006     DRW_buffer_add_entry(sgl->light_groundline, ob->obmat[3]);
3007     DRW_buffer_add_entry(sgl->light_groundpoint, ob->obmat[3]);
3008   }
3009 }
3010
3011 static void DRW_shgroup_relationship_lines(OBJECT_ShadingGroupList *sgl,
3012                                            Depsgraph *depsgraph,
3013                                            Scene *scene,
3014                                            Object *ob)
3015 {
3016   if (ob->parent && (DRW_object_visibility_in_active_context(ob->parent) & OB_VISIBLE_SELF)) {
3017     DRW_buffer_add_entry(sgl->relationship_lines, ob->runtime.parent_display_origin);
3018     DRW_buffer_add_entry(sgl->relationship_lines, ob->obmat[3]);
3019   }
3020
3021   if (ob->rigidbody_constraint) {
3022     Object *rbc_ob1 = ob->rigidbody_constraint->ob1;
3023     Object *rbc_ob2 = ob->rigidbody_constraint->ob2;
3024     if (rbc_ob1 && (DRW_object_visibility_in_active_context(rbc_ob1) & OB_VISIBLE_SELF)) {
3025       DRW_buffer_add_entry(sgl->relationship_lines, rbc_ob1->obmat[3]);
3026       DRW_buffer_add_entry(sgl->relationship_lines, ob->obmat[3]);
3027     }
3028     if (rbc_ob2 && (DRW_object_visibility_in_active_context(rbc_ob2) & OB_VISIBLE_SELF)) {
3029       DRW_buffer_add_entry(sgl->relationship_lines, rbc_ob2->obmat[3]);
3030       DRW_buffer_add_entry(sgl->relationship_lines, ob->obmat[3]);
3031     }
3032   }
3033
3034   /* Drawing the constraint lines */
3035   if (!BLI_listbase_is_empty(&ob->constraints)) {
3036     bConstraint *curcon;
3037     bConstraintOb *cob;
3038     ListBase *list = &ob->constraints;
3039
3040     cob = BKE_constraints_make_evalob(depsgraph, scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
3041
3042     for (curcon = list->first; curcon; curcon = curcon->next) {
3043       if (ELEM(curcon->type, CONSTRAINT_TYPE_FOLLOWTRACK, CONSTRAINT_TYPE_OBJECTSOLVER)) {
3044         /* special case for object solver and follow track constraints because they don't fill
3045          * constraint targets properly (design limitation -- scene is needed for their target
3046          * but it can't be accessed from get_targets callback) */
3047
3048         Object *camob = NULL;
3049
3050         if (curcon->type == CONSTRAINT_TYPE_FOLLOWTRACK) {
3051           bFollowTrackConstraint *data = (bFollowTrackConstraint *)curcon->data;
3052
3053           camob = data->camera ? data->camera : scene->camera;
3054         }
3055         else if (curcon->type == CONSTRAINT_TYPE_OBJECTSOLVER) {
3056           bObjectSolverConstraint *data = (bObjectSolverConstraint *)curcon->data;
3057
3058           camob = data->camera ? data->camera : scene->camera;
3059         }
3060
3061         if (camob) {
3062           DRW_buffer_add_entry(sgl->constraint_lines, camob->obmat[3]);
3063           DRW_buffer_add_entry(sgl->constraint_lines, ob->obmat[3]);
3064         }
3065       }
3066       else {
3067         const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(curcon);
3068
3069         if ((cti && cti->get_constraint_targets) && (curcon->flag & CONSTRAINT_EXPAND)) {
3070           ListBase targets = {NULL, NULL};
3071           bConstraintTarget *ct;
3072
3073           cti->get_constraint_targets(curcon, &targets);
3074
3075           for (ct = targets.first; ct; ct = ct->next) {
3076             /* calculate target's matrix */
3077             if (cti->get_target_matrix) {
3078               cti->get_target_matrix(depsgraph, curcon, cob, ct, DEG_get_ctime(depsgraph));
3079             }
3080             else {
3081               unit_m4(ct->matrix);
3082             }
3083
3084             DRW_buffer_add_entry(sgl->constraint_lines, ct->matrix[3]);
3085             DRW_buffer_add_entry(sgl->constraint_lines, ob->obmat[3]);
3086           }
3087
3088           if (cti->flush_constraint_targets) {
3089             cti->flush_constraint_targets(curcon, &targets, 1);
3090           }
3091         }
3092       }
3093     }
3094
3095     BKE_constraints_clear_evalob(cob);
3096   }
3097 }
3098
3099 static void DRW_shgroup_object_center(OBJECT_StorageList *stl,
3100                                       Object *ob,
3101                                       ViewLayer *view_layer,
3102                                       View3D *v3d)
3103 {
3104   if (v3d->overlay.flag & V3D_OVERLAY_HIDE_OBJECT_ORIGINS) {
3105     return;
3106   }
3107   const bool is_library = ob->id.us > 1 || ID_IS_LINKED(ob);
3108   DRWCallBuffer *buf;
3109
3110   if (ob == OBACT(view_layer)) {
3111     buf = stl->g_data->center_active;
3112   }
3113   else if (ob->base_flag & BASE_SELECTED) {
3114     if (is_library) {
3115       buf = stl->g_data->center_selected_lib;
3116     }
3117     else {
3118       buf = stl->g_data->center_selected;
3119     }
3120   }
3121   else if (v3d->flag & V3D_DRAW_CENTERS) {
3122     if (is_library) {
3123       buf = stl->g_data->center_deselected_lib;
3124     }
3125     else {
3126       buf = stl->g_data->center_deselected;
3127     }
3128   }
3129   else {
3130     return;
3131   }
3132
3133   DRW_buffer_add_entry(buf, ob->obmat[3]);
3134 }
3135
3136 static void DRW_shgroup_texture_space(OBJECT_ShadingGroupList *sgl, Object *ob, int theme_id)
3137 {
3138   if (ob->data == NULL) {
3139     return;
3140   }
3141
3142   ID *ob_data = ob->data;
3143   float *texcoloc = NULL;
3144   float *texcosize = NULL;
3145
3146   switch (GS(ob_data->name)) {
3147     case ID_ME:
3148       BKE_mesh_texspace_get_reference((Mesh *)ob_data, NULL, &texcoloc, NULL, &texcosize);
3149       break;
3150     case ID_CU: {
3151       Curve *cu = (Curve *)ob_data;
3152       if (cu->bb == NULL || (cu->bb->flag & BOUNDBOX_DIRTY)) {
3153         BKE_curve_texspace_calc(cu);
3154       }
3155       texcoloc = cu->loc;
3156       texcosize = cu->size;
3157       break;
3158     }
3159     case ID_MB: {
3160       MetaBall *mb = (MetaBall *)ob_data;
3161       texcoloc = mb->loc;
3162       texcosize = mb->size;
3163       break;
3164     }
3165     default:
3166       BLI_assert(0);
3167   }
3168
3169   float tmp[4][4] = {{0.0f}}, one = 1.0f;
3170   tmp[0][0] = texcosize[0];
3171   tmp[1][1] = texcosize[1];
3172   tmp[2][2] = texcosize[2];
3173   tmp[3][0] = texcoloc[0];
3174   tmp[3][1] = texcoloc[1];
3175   tmp[3][2] = texcoloc[2];
3176   tmp[3][3] = 1.0f;
3177
3178   mul_m4_m4m4(tmp, ob->obmat, tmp);
3179
3180   float color[4];
3181   UI_GetThemeColor4fv(theme_id, color);
3182
3183   DRW_buffer_add_entry(sgl->texspace, color, &one, tmp);
3184 }
3185
3186 static void DRW_shgroup_bounds(OBJECT_ShadingGroupList *sgl, Object *ob, int theme_id)
3187 {
3188   float color[4], center[3], size[3], tmp[4][4], final_mat[4][4], one = 1.0f;
3189   BoundBox bb_local;
3190
3191   if (ob->type == OB_MBALL && !BKE_mball_is_basis(ob)) {
3192     return;
3193   }
3194
3195   BoundBox *bb = BKE_object_boundbox_get(ob);
3196
3197   if (!ELEM(ob->type,
3198             OB_MESH,
3199             OB_CURVE,
3200             OB_SURF,
3201             OB_FONT,
3202             OB_MBALL,
3203             OB_ARMATURE,
3204             OB_LATTICE,
3205             OB_GPENCIL)) {
3206     const float min[3] = {-1.0f, -1.0f, -1.0f}, max[3] = {1.0f, 1.0f, 1.0f};
3207     bb = &bb_local;
3208     BKE_boundbox_init_from_minmax(bb, min, max);
3209   }
3210
3211   UI_GetThemeColor4fv(theme_id, color);
3212   BKE_boundbox_calc_center_aabb(bb, center);
3213   BKE_boundbox_calc_size_aabb(bb, size);
3214
3215   switch (ob->boundtype) {
3216     case OB_BOUND_BOX:
3217       size_to_mat4(tmp, size);
3218       copy_v3_v3(tmp[3], center);
3219       mul_m4_m4m4(tmp, ob->obmat, tmp);
3220       DRW_buffer_add_entry(sgl->empties.cube, color, &one, tmp);
3221       break;
3222     case OB_BOUND_SPHERE:
3223       size[0] = max_fff(size[0], size[1], size[2]);
3224       size[1] = size[2] = size[0];
3225       size_to_mat4(tmp, size);
3226       copy_v3_v3(tmp[3], center);
3227       mul_m4_m4m4(tmp, ob->obmat, tmp);
3228       DRW_buffer_add_entry(sgl->empties.sphere, color, &one, tmp);
3229       break;
3230     case OB_BOUND_CYLINDER:
3231       size[0] = max_ff(size[0], size[1]);
3232       size[1] = size[0];
3233       size_to_mat4(tmp, size);
3234       copy_v3_v3(tmp[3], center);
3235       mul_m4_m4m4(tmp, ob->obmat, tmp);
3236       DRW_buffer_add_entry(sgl->empties.cylinder, color, &one, tmp);
3237       break;
3238     case OB_BOUND_CONE:
3239       size[0] = max_ff(size[0], size[1]);
3240       size[1] = size[0];
3241       size_to_mat4(tmp, size);
3242       copy_v3_v3(tmp[3], center);
3243       /* Cone batch has base at 0 and is pointing towards +Y. */
3244       swap_v3_v3(tmp[1], tmp[2]);
3245       tmp[3][2] -= size[2];
3246       mul_m4_m4m4(tmp, ob->obmat, tmp);
3247       DRW_buffer_add_entry(sgl->empties.cone, color, &one, tmp);
3248       break;
3249     case OB_BOUND_CAPSULE:
3250       size[0] = max_ff(size[0], size[1]);
3251       size[1] = size[0];
3252       scale_m4_fl(tmp, size[0]);
3253       copy_v2_v2(tmp[3], center);
3254       tmp[3][2] = center[2] + max_ff(0.0f, size[2] - size[0]);
3255       mul_m4_m4m4(final_mat, ob->obmat, tmp);
3256       DRW_buffer_add_entry(sgl->empties.capsule_cap, color, &one, final_mat);
3257       negate_v3(tmp[2]);
3258       tmp[3][2] = center[2] - max_ff(0.0f, size[2] - size[0]);
3259       mul_m4_m4m4(final_mat, ob->obmat, tmp);
3260       DRW_buffer_add_entry(sgl->empties.capsule_cap, color, &one, final_mat);
3261       tmp[2][2] = max_ff(0.0f, size[2] * 2.0f - size[0] * 2.0f);
3262       mul_m4_m4m4(final_mat, ob->obmat, tmp);
3263       DRW_buffer_add_entry(sgl->empties.capsule_body, color, &one, final_mat);
3264       break;
3265   }
3266 }
3267
3268 static void OBJECT_cache_populate_particles(OBJECT_Shaders *sh_data,
3269                                             Object *ob,
3270                                             OBJECT_PassList *psl)
3271 {
3272   for (ParticleSystem *psys = ob->particlesystem.first; psys; psys = psys->next) {
3273     if (!DRW_object_is_visible_psys_in_active_context(ob, psys)) {
3274       continue;
3275     }
3276
3277     ParticleSettings *part = psys->part;
3278     int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
3279
3280     if (part->type == PART_HAIR) {
3281       /* Hairs should have been rendered by the render engine.*/
3282       continue;
3283     }
3284
3285     if (!ELEM(draw_as, PART_DRAW_NOT, PART_DRAW_OB, PART_DRAW_GR)) {
3286       struct GPUBatch *geom = DRW_cache_particles_get_dots(ob, psys);
3287       DRWShadingGroup *shgrp = NULL;
3288       struct GPUBatch *shape = NULL;
3289       static float def_prim_col[3] = {0.5f, 0.5f, 0.5f};
3290       static float def_sec_col[3] = {1.0f, 1.0f, 1.0f};
3291
3292       Material *ma = give_current_material(ob, part->omat);
3293
3294       switch (draw_as) {
3295         default:
3296         case PART_DRAW_DOT:
3297           shgrp = DRW_shgroup_create(sh_data->part_dot, psl->particle);
3298           DRW_shgroup_uniform_vec3(shgrp, "color", ma ? &ma->r : def_prim_col, 1);
3299           DRW_shgroup_uniform_vec3(shgrp, "outlineColor", ma ? &ma->specr : def_sec_col, 1);
3300           DRW_shgroup_uniform_float(shgrp, "pixel_size", DRW_viewport_pixelsize_get(), 1);
3301           DRW_shgroup_uniform_float(shgrp, "size", &part->draw_size, 1);
3302           DRW_shgroup_uniform_texture(shgrp, "ramp", G_draw.ramp);
3303           DRW_shgroup_call(shgrp, geom, NULL);
3304           break;
3305         case PART_DRAW_CROSS:
3306           shgrp = DRW_shgroup_create(sh_data->part_prim, psl->particle);
3307           DRW_shgroup_uniform_texture(shgrp, "ramp", G_draw.ramp);
3308           DRW_shgroup_uniform_vec3(shgrp, "color", ma ? &ma->r : def_prim_col, 1);
3309           DRW_shgroup_uniform_float(shgrp, "draw_size", &part->draw_size, 1);
3310           DRW_shgroup_uniform_bool_copy(shgrp, "screen_space", false);
3311           shape = DRW_cache_particles_get_prim(PART_DRAW_CROSS);
3312           DRW_shgroup_call_instances_with_attribs(shgrp, NULL, shape, geom);
3313           break;
3314         case PART_DRAW_CIRC:
3315           shape = DRW_cache_particles_get_prim(PART_DRAW_CIRC);
3316           shgrp = DRW_shgroup_create(sh_data->part_prim, psl->particle);
3317           DRW_shgroup_uniform_texture(shgrp, "ramp", G_draw.ramp);
3318           DRW_shgroup_uniform_vec3(shgrp, "color", ma ? &ma->r : def_prim_col, 1);
3319           DRW_shgroup_uniform_float(shgrp, "draw_size", &part->draw_size, 1);
3320           DRW_shgroup_uniform_bool_copy(shgrp, "screen_space", true);
3321           DRW_shgroup_call_instances_with_attribs(shgrp, NULL, shape, geom);
3322           break;
3323         case PART_DRAW_AXIS:
3324           shape = DRW_cache_particles_get_prim(PART_DRAW_AXIS);
3325           shgrp = DRW_shgroup_create(sh_data->part_axis, psl->particle);
3326           DRW_shgroup_uniform_float(shgrp, "draw_size", &part->draw_size, 1);
3327           DRW_shgroup_uniform_bool_copy(shgrp, "screen_space", false);
3328           DRW_shgroup_call_instances_with_attribs(shgrp, NULL, shape, geom);
3329           break;
3330       }
3331     }
3332   }
3333 }
3334
3335 static void OBJECT_gpencil_color_names(Object *ob, struct DRWTextStore *dt, uchar color[4])
3336 {
3337   if (ob->mode != OB_MODE_EDIT_GPENCIL) {
3338     return;
3339   }
3340
3341   bGPdata *gpd = (bGPdata *)ob->data;
3342   if (gpd == NULL) {
3343     return;
3344   }
3345
3346   for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
3347     if (gpl->flag & GP_LAYER_HIDE) {
3348       continue;
3349     }
3350     bGPDframe *gpf = gpl->actframe;
3351     if (gpf == NULL) {
3352       continue;
3353     }
3354     for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
3355       Material *ma = give_current_material(ob, gps->mat_nr + 1);
3356       if (ma == NULL) {
3357         continue;
3358       }
3359
3360       MaterialGPencilStyle *gp_style = ma->gp_style;
3361       /* skip stroke if it doesn't have any valid data */
3362       if ((gps->points == NULL) || (gps->totpoints < 1) || (gp_style == NULL)) {
3363         continue;
3364       }
3365       /* check if the color is visible */
3366       if (gp_style->flag & GP_STYLE_COLOR_HIDE) {
3367         continue;
3368       }
3369
3370       /* only if selected */
3371       if (gps->flag & GP_STROKE_SELECT) {
3372         float fpt[3];
3373         for (int i = 0; i < gps->totpoints; i++) {
3374           bGPDspoint *pt = &gps->points[i];
3375           if (pt->flag & GP_SPOINT_SELECT) {
3376             mul_v3_m4v3(fpt, ob->obmat, &pt->x);
3377             DRW_text_cache_add(dt,
3378                                fpt,
3379                                ma->id.name + 2,
3380                                strlen(ma->id.name + 2),
3381                                10,
3382                                0,
3383                                DRW_TEXT_CACHE_GLOBALSPACE | DRW_TEXT_CACHE_STRING_PTR,
3384                                color);
3385             break;
3386           }
3387         }
3388       }
3389     }
3390   }
3391 }
3392
3393 BLI_INLINE OBJECT_DupliData *OBJECT_duplidata_get(Object *ob, void *vedata, bool *init)
3394 {
3395   OBJECT_DupliData **dupli_data = (OBJECT_DupliData **)DRW_duplidata_get(vedata);
3396   *init = false;
3397   if (!ELEM(ob->type, OB_MESH, OB_SURF, OB_LATTICE, OB_CURVE, OB_FONT)) {
3398     return NULL;
3399   }
3400
3401   if (dupli_data) {
3402     if (*dupli_data == NULL) {
3403       *dupli_data = MEM_callocN(sizeof(OBJECT_DupliData), "OBJECT_DupliData");
3404       *init = true;
3405     }
3406     else if ((*dupli_data)->base_flag != ob->base_flag) {
3407       /* Select state might have change, reinit. */
3408       *init = true;
3409     }
3410     return *dupli_data;
3411   }
3412   return NULL;
3413 }
3414
3415 static void OBJECT_cache_populate(void *vedata, Object *ob)
3416 {
3417   OBJECT_PassList *psl = ((OBJECT_Data *)vedata)->psl;
3418   OBJECT_StorageList *stl = ((OBJECT_Data *)vedata)->stl;
3419   OBJECT_ShadingGroupList *sgl = (ob->dtx & OB_DRAWXRAY) ? &stl->g_data->sgl_ghost :
3420                                                            &stl->g_data->sgl;
3421   const DRWContextState *draw_ctx = DRW_context_state_get();
3422   const bool is_edit_mode = (ob == draw_ctx->object_edit) || BKE_object_is_in_editmode(ob);
3423   ViewLayer *view_layer = draw_ctx->view_layer;
3424   Scene *scene = draw_ctx->scene;
3425   View3D *v3d = draw_ctx->v3d;
3426   RegionView3D *rv3d = draw_ctx->rv3d;
3427   ModifierData *md = NULL;
3428   int theme_id = TH_UNDEFINED;
3429   const int ob_visibility = DRW_object_visibility_in_active_context(ob);
3430   OBJECT_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
3431
3432   /* Handle particles first in case the emitter itself shouldn't be rendered. */
3433   if (ob_visibility & OB_VISIBLE_PARTICLES) {
3434     OBJECT_cache_populate_particles(sh_data, ob, psl);
3435   }
3436
3437   if ((ob_visibility & OB_VISIBLE_SELF) == 0) {
3438     return;
3439   }
3440
3441   const bool do_outlines = ((draw_ctx->v3d->flag & V3D_SELECT_OUTLINE) &&
3442                             ((ob->base_flag & BASE_SELECTED) != 0) &&
3443                             ((DRW_object_is_renderable(ob) && (ob->dt > OB_WIRE)) ||
3444                              (ob->dt == OB_WIRE)));
3445   const bool show_relations = ((draw_ctx->v3d->flag & V3D_HIDE_HELPLINES) == 0);
3446   const bool hide_object_extra =
3447       ((v3d->overlay.flag & V3D_OVERLAY_HIDE_OBJECT_XTRAS) != 0 &&
3448        /* Show if this is the camera we're looking through since it's useful for selecting. */
3449        (((rv3d->persp == RV3D_CAMOB) && ((ID *)v3d->camera == ob->id.orig_id)) == 0));
3450
3451   /* Fast path for duplis. */
3452   bool init_duplidata;
3453   OBJECT_DupliData *dupli_data = OBJECT_duplidata_get(ob, vedata, &init_duplidata);
3454
3455   if (do_outlines) {
3456     if (!BKE_object_is_in_editmode(ob) &&
3457         !((ob == draw_ctx->obact) && (draw_ctx->object_mode & OB_MODE_ALL_PAINT))) {
3458       struct GPUBatch *geom;
3459       DRWShadingGroup *shgroup = NULL;
3460
3461       /* This fixes only the biggest case which is a plane in ortho view. */
3462       int flat_axis = 0;
3463       bool is_flat_object_viewed_from_side = ((rv3d->persp == RV3D_ORTHO) &&
3464                                               DRW_object_is_flat(ob, &flat_axis) &&
3465                                               DRW_object_axis_orthogonal_to_view(ob, flat_axis));
3466
3467       if (dupli_data && !init_duplidata) {
3468         geom = dupli_data->outline_geom;
3469         shgroup = dupli_data->outline_shgrp;
3470         /* TODO: Remove. Only here to increment outline id counter. */
3471         theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
3472         shgroup = shgroup_theme_id_to_outline_or_null(stl, theme_id, ob->base_flag);
3473       }
3474       else {
3475         if (stl->g_data->xray_enabled_and_not_wire || is_flat_object_viewed_from_side) {
3476           geom = DRW_cache_object_edge_detection_get(ob, NULL);
3477         }
3478         else {
3479           geom = DRW_cache_object_surface_get(ob);
3480         }
3481
3482         if (geom) {
3483           theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
3484           shgroup = shgroup_theme_id_to_outline_or_null(stl, theme_id, ob->base_flag);
3485         }
3486       }
3487
3488       if (shgroup && geom) {
3489         DRW_shgroup_call(shgroup, geom, ob);
3490       }
3491
3492       if (init_duplidata) {
3493         dupli_data->outline_shgrp = shgroup;
3494         dupli_data->outline_geom = geom;
3495       }
3496     }
3497   }
3498
3499   if (dupli_data && !init_duplidata) {
3500     if (dupli_data->extra_shgrp && dupli_data->extra_geom) {
3501       DRW_shgroup_call(dupli_data->extra_shgrp, dupli_data->extra_geom, ob);
3502     }
3503   }
3504   else {
3505     struct GPUBatch *geom = NULL;
3506     DRWShadingGroup *shgroup = NULL;
3507     switch (ob->type) {
3508       case OB_MESH: {
3509         if (hide_object_extra) {
3510           break;
3511         }
3512         Mesh *me = ob->data;
3513         if (!is_edit_mode && me->totedge == 0) {
3514           geom = DRW_cache_mesh_all_verts_get(ob);
3515           if (geom) {
3516             if (theme_id == TH_UNDEFINED) {
3517               theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
3518             }
3519             shgroup = shgroup_theme_id_to_point(sgl, theme_id, ob->base_flag);
3520             DRW_shgroup_call(shgroup, geom, ob);
3521           }
3522         }
3523         else {
3524           bool has_edit_mesh_cage = false;
3525           /* TODO: Should be its own function. */
3526           if (is_edit_mode) {
3527             BMEditMesh *embm = me->edit_mesh;
3528             has_edit_mesh_cage = embm->mesh_eval_cage &&
3529                                  (embm->mesh_eval_cage != embm->mesh_eval_final);
3530           }
3531           if ((!is_edit_mode && me->totedge > 0) || has_edit_mesh_cage) {
3532             geom = DRW_cache_mesh_loose_edges_get(ob);
3533             if (geom) {
3534               if (theme_id == TH_UNDEFINED) {
3535                 theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
3536               }
3537               shgroup = shgroup_theme_id_to_wire(sgl, theme_id, ob->base_flag);
3538               DRW_shgroup_call(shgroup, geom, ob);
3539             }
3540           }
3541         }
3542         break;
3543       }
3544       case OB_SURF: {
3545         if (hide_object_extra) {
3546           break;
3547         }
3548         geom = DRW_cache_surf_edge_wire_get(ob);
3549         if (geom == NULL) {
3550           break;
3551         }
3552         if (theme_id == TH_UNDEFINED) {
3553           theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
3554         }