Fix T56657: Crash when creating a collection instance containing probes
[blender.git] / source / blender / draw / modes / object_mode.c
1 /*
2  * Copyright 2016, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Blender Institute
19  *
20  */
21
22 /** \file blender/draw/modes/object_mode.c
23  *  \ingroup draw
24  */
25
26 #include "DRW_engine.h"
27 #include "DRW_render.h"
28
29 #include "DNA_userdef_types.h"
30 #include "DNA_armature_types.h"
31 #include "DNA_constraint_types.h"
32 #include "DNA_camera_types.h"
33 #include "DNA_curve_types.h"
34 #include "DNA_mesh_types.h"
35 #include "DNA_meta_types.h"
36 #include "DNA_modifier_types.h"
37 #include "DNA_object_force_types.h"
38 #include "DNA_lightprobe_types.h"
39 #include "DNA_particle_types.h"
40 #include "DNA_rigidbody_types.h"
41 #include "DNA_smoke_types.h"
42 #include "DNA_view3d_types.h"
43 #include "DNA_world_types.h"
44
45 #include "BIF_gl.h"
46
47 #include "BLI_string_utils.h"
48
49 #include "BKE_anim.h"
50 #include "BKE_camera.h"
51 #include "BKE_constraint.h"
52 #include "BKE_curve.h"
53 #include "BKE_global.h"
54 #include "BKE_mball.h"
55 #include "BKE_mesh.h"
56 #include "BKE_modifier.h"
57 #include "BKE_movieclip.h"
58 #include "BKE_object.h"
59 #include "BKE_particle.h"
60 #include "BKE_image.h"
61 #include "BKE_texture.h"
62 #include "BKE_tracking.h"
63
64 #include "ED_view3d.h"
65
66 #include "GPU_batch.h"
67 #include "GPU_draw.h"
68 #include "GPU_shader.h"
69 #include "GPU_texture.h"
70
71 #include "MEM_guardedalloc.h"
72
73 #include "UI_resources.h"
74
75 #include "draw_mode_engines.h"
76 #include "draw_manager_text.h"
77 #include "draw_common.h"
78
79 #include "DEG_depsgraph_query.h"
80
81 extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
82 extern struct GPUTexture *globals_ramp; /* draw_common.c */
83 extern GlobalsUboStorage ts;
84
85 extern char datatoc_object_outline_prepass_vert_glsl[];
86 extern char datatoc_object_outline_prepass_geom_glsl[];
87 extern char datatoc_object_outline_prepass_frag_glsl[];
88 extern char datatoc_object_outline_resolve_frag_glsl[];
89 extern char datatoc_object_outline_detect_frag_glsl[];
90 extern char datatoc_object_outline_expand_frag_glsl[];
91 extern char datatoc_object_grid_frag_glsl[];
92 extern char datatoc_object_grid_vert_glsl[];
93 extern char datatoc_object_empty_image_frag_glsl[];
94 extern char datatoc_object_empty_image_vert_glsl[];
95 extern char datatoc_object_lightprobe_grid_vert_glsl[];
96 extern char datatoc_object_particle_prim_vert_glsl[];
97 extern char datatoc_object_particle_dot_vert_glsl[];
98 extern char datatoc_object_particle_dot_frag_glsl[];
99 extern char datatoc_common_globals_lib_glsl[];
100 extern char datatoc_common_fxaa_lib_glsl[];
101 extern char datatoc_gpu_shader_flat_color_frag_glsl[];
102 extern char datatoc_gpu_shader_flat_id_frag_glsl[];
103 extern char datatoc_common_fullscreen_vert_glsl[];
104 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
105
106 /* *********** LISTS *********** */
107 typedef struct OBJECT_PassList {
108         struct DRWPass *non_meshes[2];
109         struct DRWPass *spot_shapes[2];
110         struct DRWPass *ob_center;
111         struct DRWPass *outlines;
112         struct DRWPass *outlines_search;
113         struct DRWPass *outlines_expand;
114         struct DRWPass *outlines_bleed;
115         struct DRWPass *outlines_resolve;
116         struct DRWPass *grid;
117         struct DRWPass *bone_solid[2];
118         struct DRWPass *bone_outline[2];
119         struct DRWPass *bone_wire[2];
120         struct DRWPass *bone_envelope[2];
121         struct DRWPass *bone_axes[2];
122         struct DRWPass *particle;
123         struct DRWPass *lightprobes;
124 } OBJECT_PassList;
125
126 typedef struct OBJECT_FramebufferList {
127         struct GPUFrameBuffer *outlines_fb;
128         struct GPUFrameBuffer *blur_fb;
129         struct GPUFrameBuffer *expand_fb;
130         struct GPUFrameBuffer *ghost_fb;
131 } OBJECT_FramebufferList;
132
133 typedef struct OBJECT_StorageList {
134         struct OBJECT_PrivateData *g_data;
135 } OBJECT_StorageList;
136
137 typedef struct OBJECT_Data {
138         void *engine_type;
139         OBJECT_FramebufferList *fbl;
140         DRWViewportEmptyList *txl;
141         OBJECT_PassList *psl;
142         OBJECT_StorageList *stl;
143 } OBJECT_Data;
144
145 /* *********** STATIC *********** */
146
147 typedef struct OBJECT_ShadingGroupList {
148         /* Reference only */
149         struct DRWPass *non_meshes;
150         struct DRWPass *spot_shapes;
151         struct DRWPass *bone_solid;
152         struct DRWPass *bone_outline;
153         struct DRWPass *bone_wire;
154         struct DRWPass *bone_envelope;
155         struct DRWPass *bone_axes;
156
157         /* Empties */
158         DRWShadingGroup *plain_axes;
159         DRWShadingGroup *cube;
160         DRWShadingGroup *circle;
161         DRWShadingGroup *sphere;
162         DRWShadingGroup *sphere_solid;
163         DRWShadingGroup *cylinder;
164         DRWShadingGroup *capsule_cap;
165         DRWShadingGroup *capsule_body;
166         DRWShadingGroup *cone;
167         DRWShadingGroup *single_arrow;
168         DRWShadingGroup *single_arrow_line;
169         DRWShadingGroup *empty_axes;
170         /* GPUTexture -> EmptyImageShadingGroupData */
171         GHash *image_plane_map;
172
173         /* Force Field */
174         DRWShadingGroup *field_wind;
175         DRWShadingGroup *field_force;
176         DRWShadingGroup *field_vortex;
177         DRWShadingGroup *field_curve_sta;
178         DRWShadingGroup *field_curve_end;
179         DRWShadingGroup *field_tube_limit;
180         DRWShadingGroup *field_cone_limit;
181
182         /* Grease Pencil */
183         DRWShadingGroup *gpencil_axes;
184
185         /* Speaker */
186         DRWShadingGroup *speaker;
187
188         /* Probe */
189         DRWShadingGroup *probe_cube;
190         DRWShadingGroup *probe_planar;
191         DRWShadingGroup *probe_grid;
192
193         /* MetaBalls */
194         DRWShadingGroup *mball_handle;
195
196         /* Lamps */
197         DRWShadingGroup *lamp_center;
198         DRWShadingGroup *lamp_groundpoint;
199         DRWShadingGroup *lamp_groundline;
200         DRWShadingGroup *lamp_circle;
201         DRWShadingGroup *lamp_circle_shadow;
202         DRWShadingGroup *lamp_sunrays;
203         DRWShadingGroup *lamp_distance;
204         DRWShadingGroup *lamp_buflimit;
205         DRWShadingGroup *lamp_buflimit_points;
206         DRWShadingGroup *lamp_area_sphere;
207         DRWShadingGroup *lamp_area_square;
208         DRWShadingGroup *lamp_area_disk;
209         DRWShadingGroup *lamp_hemi;
210         DRWShadingGroup *lamp_spot_cone;
211         DRWShadingGroup *lamp_spot_blend;
212         DRWShadingGroup *lamp_spot_pyramid;
213         DRWShadingGroup *lamp_spot_blend_rect;
214         DRWShadingGroup *lamp_spot_volume;
215         DRWShadingGroup *lamp_spot_volume_rect;
216         DRWShadingGroup *lamp_spot_volume_outside;
217         DRWShadingGroup *lamp_spot_volume_rect_outside;
218
219         /* Helpers */
220         DRWShadingGroup *relationship_lines;
221         DRWShadingGroup *constraint_lines;
222
223         /* Camera */
224         DRWShadingGroup *camera;
225         DRWShadingGroup *camera_frame;
226         DRWShadingGroup *camera_tria;
227         DRWShadingGroup *camera_focus;
228         DRWShadingGroup *camera_clip;
229         DRWShadingGroup *camera_clip_points;
230         DRWShadingGroup *camera_mist;
231         DRWShadingGroup *camera_mist_points;
232         ListBase camera_path;
233
234         /* Wire */
235         DRWShadingGroup *wire;
236         DRWShadingGroup *wire_active;
237         DRWShadingGroup *wire_select;
238         DRWShadingGroup *wire_transform;
239
240         /* Points */
241         DRWShadingGroup *points;
242         DRWShadingGroup *points_active;
243         DRWShadingGroup *points_select;
244         DRWShadingGroup *points_transform;
245
246         /* Texture Space */
247         DRWShadingGroup *texspace;
248 } OBJECT_ShadingGroupList;
249
250 typedef struct OBJECT_PrivateData {
251         OBJECT_ShadingGroupList sgl;
252         OBJECT_ShadingGroupList sgl_ghost;
253
254         /* Outlines */
255         DRWShadingGroup *outlines_active;
256         DRWShadingGroup *outlines_select;
257         DRWShadingGroup *outlines_transform;
258
259         /* Lightprobes */
260         DRWShadingGroup *lightprobes_cube_select;
261         DRWShadingGroup *lightprobes_cube_active;
262         DRWShadingGroup *lightprobes_cube_transform;
263
264         DRWShadingGroup *lightprobes_planar_select;
265         DRWShadingGroup *lightprobes_planar_active;
266         DRWShadingGroup *lightprobes_planar_transform;
267
268         /* Objects Centers */
269         DRWShadingGroup *center_active;
270         DRWShadingGroup *center_selected;
271         DRWShadingGroup *center_deselected;
272         DRWShadingGroup *center_selected_lib;
273         DRWShadingGroup *center_deselected_lib;
274
275         /* Outlines id offset */
276         int id_ofs_active;
277         int id_ofs_select;
278         int id_ofs_transform;
279         int id_ofs_prb_active;
280         int id_ofs_prb_select;
281         int id_ofs_prb_transform;
282 } OBJECT_PrivateData; /* Transient data */
283
284 static struct {
285         /* Instance Data format */
286         struct GPUVertFormat *particle_format;
287         struct GPUVertFormat *empty_image_format;
288         struct GPUVertFormat *empty_image_wire_format;
289
290         /* fullscreen shaders */
291         GPUShader *outline_prepass_sh;
292         GPUShader *outline_prepass_wire_sh;
293         GPUShader *outline_resolve_sh;
294         GPUShader *outline_resolve_aa_sh;
295         GPUShader *outline_detect_sh;
296         GPUShader *outline_detect_wire_sh;
297         GPUShader *outline_fade_sh;
298
299         /* regular shaders */
300         GPUShader *object_empty_image_sh;
301         GPUShader *object_empty_image_wire_sh;
302         GPUShader *grid_sh;
303         GPUShader *part_dot_sh;
304         GPUShader *part_prim_sh;
305         GPUShader *part_axis_sh;
306         GPUShader *lightprobe_grid_sh;
307         float camera_pos[3];
308         float screenvecs[3][4];
309         float grid_settings[5];
310         int grid_flag;
311         float grid_normal[3];
312         float grid_axes[3];
313         int zpos_flag;
314         int zneg_flag;
315         float zplane_normal[3];
316         float zplane_axes[3];
317         float inv_viewport_size[2];
318         bool draw_grid;
319         /* Temp buffer textures */
320         struct GPUTexture *outlines_depth_tx;
321         struct GPUTexture *outlines_id_tx;
322         struct GPUTexture *outlines_color_tx;
323         struct GPUTexture *outlines_blur_tx;
324
325         ListBase smoke_domains;
326 } e_data = {NULL}; /* Engine data */
327
328
329 enum {
330         SHOW_AXIS_X  = (1 << 0),
331         SHOW_AXIS_Y  = (1 << 1),
332         SHOW_AXIS_Z  = (1 << 2),
333         SHOW_GRID    = (1 << 3),
334         PLANE_XY     = (1 << 4),
335         PLANE_XZ     = (1 << 5),
336         PLANE_YZ     = (1 << 6),
337         CLIP_ZPOS    = (1 << 7),
338         CLIP_ZNEG    = (1 << 8),
339         GRID_BACK    = (1 << 9),
340 };
341
342 /* Prototypes. */
343 static void DRW_shgroup_empty_ex(OBJECT_ShadingGroupList *sgl, float mat[4][4], float *draw_size, char draw_type, float *color);
344
345 /* *********** FUNCTIONS *********** */
346
347 static void OBJECT_engine_init(void *vedata)
348 {
349         OBJECT_FramebufferList *fbl = ((OBJECT_Data *)vedata)->fbl;
350
351         const float *viewport_size = DRW_viewport_size_get();
352         const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
353
354         if (DRW_state_is_fbo()) {
355                 e_data.outlines_depth_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_DEPTH_COMPONENT24,
356                                                                      &draw_engine_object_type);
357                 /* XXX TODO GPU_R16UI can overflow, it would cause no harm
358                  * (only bad colored or missing outlines) but we should
359                  * use 32bits only if the scene have that many objects */
360                 e_data.outlines_id_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_R16UI,
361                                                                   &draw_engine_object_type);
362
363                 GPU_framebuffer_ensure_config(&fbl->outlines_fb, {
364                         GPU_ATTACHMENT_TEXTURE(e_data.outlines_depth_tx),
365                         GPU_ATTACHMENT_TEXTURE(e_data.outlines_id_tx)
366                 });
367
368                 e_data.outlines_color_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RGBA8,
369                                                                      &draw_engine_object_type);
370
371                 GPU_framebuffer_ensure_config(&fbl->expand_fb, {
372                         GPU_ATTACHMENT_NONE,
373                         GPU_ATTACHMENT_TEXTURE(e_data.outlines_color_tx)
374                 });
375
376                 e_data.outlines_blur_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RGBA8,
377                                                                     &draw_engine_object_type);
378
379                 GPU_framebuffer_ensure_config(&fbl->blur_fb, {
380                         GPU_ATTACHMENT_NONE,
381                         GPU_ATTACHMENT_TEXTURE(e_data.outlines_blur_tx)
382                 });
383         }
384
385         /* Shaders */
386         if (!e_data.outline_resolve_sh) {
387                 /* Outline */
388                 e_data.outline_prepass_sh = DRW_shader_create_3D(datatoc_object_outline_prepass_frag_glsl, NULL);
389
390                 e_data.outline_prepass_wire_sh = DRW_shader_create(
391                             datatoc_object_outline_prepass_vert_glsl,
392                             datatoc_object_outline_prepass_geom_glsl,
393                             datatoc_object_outline_prepass_frag_glsl, NULL);
394
395                 e_data.outline_resolve_sh = DRW_shader_create_fullscreen(datatoc_object_outline_resolve_frag_glsl, NULL);
396
397                 e_data.outline_resolve_aa_sh = DRW_shader_create_with_lib(
398                             datatoc_common_fullscreen_vert_glsl, NULL,
399                             datatoc_object_outline_resolve_frag_glsl,
400                             datatoc_common_fxaa_lib_glsl,
401                             "#define FXAA_ALPHA\n"
402                             "#define USE_FXAA\n");
403
404                 e_data.outline_detect_sh = DRW_shader_create_with_lib(
405                             datatoc_common_fullscreen_vert_glsl, NULL,
406                             datatoc_object_outline_detect_frag_glsl,
407                             datatoc_common_globals_lib_glsl,
408                             NULL);
409
410                 e_data.outline_detect_wire_sh = DRW_shader_create_with_lib(
411                             datatoc_common_fullscreen_vert_glsl, NULL,
412                             datatoc_object_outline_detect_frag_glsl,
413                             datatoc_common_globals_lib_glsl,
414                             "#define WIRE\n");
415
416
417                 e_data.outline_fade_sh = DRW_shader_create_fullscreen(datatoc_object_outline_expand_frag_glsl, NULL);
418
419                 /* Empty images */
420                 e_data.object_empty_image_sh = DRW_shader_create(
421                            datatoc_object_empty_image_vert_glsl, NULL,
422                            datatoc_object_empty_image_frag_glsl, NULL);
423
424                 e_data.object_empty_image_wire_sh = DRW_shader_create(
425                            datatoc_object_empty_image_vert_glsl, NULL,
426                            datatoc_object_empty_image_frag_glsl,
427                            "#define USE_WIRE\n");
428
429                 /* Grid */
430                 e_data.grid_sh = DRW_shader_create_with_lib(
431                         datatoc_object_grid_vert_glsl, NULL,
432                         datatoc_object_grid_frag_glsl,
433                         datatoc_common_globals_lib_glsl, NULL);
434
435                 /* Particles */
436                 e_data.part_prim_sh = DRW_shader_create(
437                         datatoc_object_particle_prim_vert_glsl, NULL, datatoc_gpu_shader_flat_color_frag_glsl, NULL);
438
439                 e_data.part_axis_sh = DRW_shader_create(
440                         datatoc_object_particle_prim_vert_glsl, NULL, datatoc_gpu_shader_flat_color_frag_glsl,
441                         "#define USE_AXIS\n");
442
443                 e_data.part_dot_sh = DRW_shader_create(
444                         datatoc_object_particle_dot_vert_glsl, NULL, datatoc_object_particle_dot_frag_glsl, NULL);
445
446                 /* Lightprobes */
447                 e_data.lightprobe_grid_sh = DRW_shader_create(
448                         datatoc_object_lightprobe_grid_vert_glsl, NULL, datatoc_gpu_shader_flat_id_frag_glsl, NULL);
449         }
450
451         {
452                 /* Grid precompute */
453                 float invviewmat[4][4], invwinmat[4][4];
454                 float viewmat[4][4], winmat[4][4];
455                 const DRWContextState *draw_ctx = DRW_context_state_get();
456                 View3D *v3d = draw_ctx->v3d;
457                 Scene *scene = draw_ctx->scene;
458                 RegionView3D *rv3d = draw_ctx->rv3d;
459                 float grid_scale = ED_view3d_grid_scale(scene, v3d, NULL);
460                 float grid_res;
461
462                 const bool show_axis_x = (v3d->gridflag & V3D_SHOW_X) != 0;
463                 const bool show_axis_y = (v3d->gridflag & V3D_SHOW_Y) != 0;
464                 const bool show_axis_z = (v3d->gridflag & V3D_SHOW_Z) != 0;
465                 const bool show_floor = (v3d->gridflag & V3D_SHOW_FLOOR) != 0;
466                 e_data.draw_grid = show_axis_x || show_axis_y || show_axis_z || show_floor;
467
468                 DRW_viewport_matrix_get(winmat, DRW_MAT_WIN);
469                 DRW_viewport_matrix_get(viewmat, DRW_MAT_VIEW);
470                 DRW_viewport_matrix_get(invwinmat, DRW_MAT_WININV);
471                 DRW_viewport_matrix_get(invviewmat, DRW_MAT_VIEWINV);
472
473                 /* Setup camera pos */
474                 copy_v3_v3(e_data.camera_pos, invviewmat[3]);
475
476                 /* if perps */
477                 if (winmat[3][3] == 0.0f) {
478                         float fov;
479                         float viewvecs[2][4] = {
480                             {1.0f, -1.0f, -1.0f, 1.0f},
481                             {-1.0f, 1.0f, -1.0f, 1.0f}
482                         };
483
484                         /* convert the view vectors to view space */
485                         for (int i = 0; i < 2; i++) {
486                                 mul_m4_v4(invwinmat, viewvecs[i]);
487                                 mul_v3_fl(viewvecs[i], 1.0f / viewvecs[i][2]); /* perspective divide */
488                         }
489
490                         fov = angle_v3v3(viewvecs[0], viewvecs[1]) / 2.0f;
491                         grid_res = fabsf(tanf(fov)) / grid_scale;
492
493                         e_data.grid_flag = (1 << 4); /* XY plane */
494                         if (show_axis_x)
495                                 e_data.grid_flag |= SHOW_AXIS_X;
496                         if (show_axis_y)
497                                 e_data.grid_flag |= SHOW_AXIS_Y;
498                         if (show_floor)
499                                 e_data.grid_flag |= SHOW_GRID;
500
501                 }
502                 else {
503                         float viewdist = 1.0f / max_ff(fabsf(winmat[0][0]), fabsf(winmat[1][1]));
504                         grid_res = viewdist / grid_scale;
505
506                         if (ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT)) {
507                                 e_data.grid_flag = PLANE_YZ;
508                                 if (show_axis_y) {
509                                         e_data.grid_flag |= SHOW_AXIS_Y;
510                                 }
511                                 if (show_axis_z) {
512                                         e_data.grid_flag |= SHOW_AXIS_Z;
513                                 }
514                                 if (show_floor) {
515                                         e_data.grid_flag |= SHOW_GRID;
516                                         e_data.grid_flag |= GRID_BACK;
517                                 }
518                         }
519                         else if (ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) {
520                                 e_data.grid_flag = PLANE_XY;
521                                 if (show_axis_x) {
522                                         e_data.grid_flag |= SHOW_AXIS_X;
523                                 }
524                                 if (show_axis_y) {
525                                         e_data.grid_flag |= SHOW_AXIS_Y;
526                                 }
527                                 if (show_floor) {
528                                         e_data.grid_flag |= SHOW_GRID;
529                                         e_data.grid_flag |= GRID_BACK;
530                                 }
531                         }
532                         else if (ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK)) {
533                                 e_data.grid_flag = PLANE_XZ;
534                                 if (show_axis_x) {
535                                         e_data.grid_flag |= SHOW_AXIS_X;
536                                 }
537                                 if (show_axis_z) {
538                                         e_data.grid_flag |= SHOW_AXIS_Z;
539                                 }
540                                 if (show_floor) {
541                                         e_data.grid_flag |= SHOW_GRID;
542                                         e_data.grid_flag |= GRID_BACK;
543                                 }
544                         }
545                         else { /* RV3D_VIEW_USER */
546                                 e_data.grid_flag = PLANE_XY;
547                                 if (show_axis_x) {
548                                         e_data.grid_flag |= SHOW_AXIS_X;
549                                 }
550                                 if (show_axis_y) {
551                                         e_data.grid_flag |= SHOW_AXIS_Y;
552                                 }
553                                 if (show_floor) {
554                                         e_data.grid_flag |= SHOW_GRID;
555                                 }
556                         }
557                 }
558
559                 e_data.grid_normal[0] = (float)((e_data.grid_flag & PLANE_YZ) != 0);
560                 e_data.grid_normal[1] = (float)((e_data.grid_flag & PLANE_XZ) != 0);
561                 e_data.grid_normal[2] = (float)((e_data.grid_flag & PLANE_XY) != 0);
562
563                 e_data.grid_axes[0] = (float)((e_data.grid_flag & (PLANE_XZ | PLANE_XY)) != 0);
564                 e_data.grid_axes[1] = (float)((e_data.grid_flag & (PLANE_YZ | PLANE_XY)) != 0);
565                 e_data.grid_axes[2] = (float)((e_data.grid_flag & (PLANE_YZ | PLANE_XZ)) != 0);
566
567                 /* Vectors to recover pixel world position. Fix grid precision issue. */
568                 /* Using pixel at z = 0.0f in ndc space : gives average precision between
569                  * near and far plane. Note that it might not be the best choice. */
570                 copy_v4_fl4(e_data.screenvecs[0],  1.0f, -1.0f, 0.0f, 1.0f);
571                 copy_v4_fl4(e_data.screenvecs[1], -1.0f,  1.0f, 0.0f, 1.0f);
572                 copy_v4_fl4(e_data.screenvecs[2], -1.0f, -1.0f, 0.0f, 1.0f);
573
574                 for (int i = 0; i < 3; i++) {
575                         /* Doing 2 steps to recover world position of the corners of the frustum.
576                          * Using the inverse perspective matrix is giving very low precision output. */
577                         mul_m4_v4(invwinmat, e_data.screenvecs[i]);
578                         e_data.screenvecs[i][0] /= e_data.screenvecs[i][3]; /* perspective divide */
579                         e_data.screenvecs[i][1] /= e_data.screenvecs[i][3]; /* perspective divide */
580                         e_data.screenvecs[i][2] /= e_data.screenvecs[i][3]; /* perspective divide */
581                         e_data.screenvecs[i][3] = 1.0f;
582                         /* main instability come from this one */
583                         /* TODO : to make things even more stable, don't use
584                          * invviewmat and derive vectors from camera properties */
585                         mul_m4_v4(invviewmat, e_data.screenvecs[i]);
586                 }
587
588                 sub_v3_v3(e_data.screenvecs[0], e_data.screenvecs[2]);
589                 sub_v3_v3(e_data.screenvecs[1], e_data.screenvecs[2]);
590
591                 /* Z axis if needed */
592                 if (((rv3d->view == RV3D_VIEW_USER) || (rv3d->persp != RV3D_ORTHO)) && show_axis_z) {
593                         e_data.zpos_flag = SHOW_AXIS_Z;
594
595                         float zvec[4] = {0.0f, 0.0f, -1.0f, 0.0f};
596                         mul_m4_v4(invviewmat, zvec);
597
598                         /* z axis : chose the most facing plane */
599                         if (fabsf(zvec[0]) < fabsf(zvec[1])) {
600                                 e_data.zpos_flag |= PLANE_XZ;
601                         }
602                         else {
603                                 e_data.zpos_flag |= PLANE_YZ;
604                         }
605
606                         e_data.zneg_flag = e_data.zpos_flag;
607
608                         /* Persp : If camera is below floor plane, we switch clipping
609                          * Ortho : If eye vector is looking up, we switch clipping */
610                         if (((winmat[3][3] == 0.0f) && (e_data.camera_pos[2] > 0.0f)) ||
611                             ((winmat[3][3] != 0.0f) && (zvec[2] < 0.0f)))
612                         {
613                                 e_data.zpos_flag |= CLIP_ZPOS;
614                                 e_data.zneg_flag |= CLIP_ZNEG;
615                         }
616                         else {
617                                 e_data.zpos_flag |= CLIP_ZNEG;
618                                 e_data.zneg_flag |= CLIP_ZPOS;
619                         }
620
621                         e_data.zplane_normal[0] = (float)((e_data.zpos_flag & PLANE_YZ) != 0);
622                         e_data.zplane_normal[1] = (float)((e_data.zpos_flag & PLANE_XZ) != 0);
623                         e_data.zplane_normal[2] = (float)((e_data.zpos_flag & PLANE_XY) != 0);
624
625                         e_data.zplane_axes[0] = (float)((e_data.zpos_flag & (PLANE_XZ | PLANE_XY)) != 0);
626                         e_data.zplane_axes[1] = (float)((e_data.zpos_flag & (PLANE_YZ | PLANE_XY)) != 0);
627                         e_data.zplane_axes[2] = (float)((e_data.zpos_flag & (PLANE_YZ | PLANE_XZ)) != 0);
628
629                 }
630                 else {
631                         e_data.zneg_flag = e_data.zpos_flag = CLIP_ZNEG | CLIP_ZPOS;
632                 }
633
634                 float dist;
635                 if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
636                         Object *camera_object = DEG_get_evaluated_object(draw_ctx->depsgraph, v3d->camera);
637                         dist = ((Camera *)(camera_object->data))->clipend;
638                 }
639                 else {
640                         dist = v3d->far;
641                 }
642
643                 e_data.grid_settings[0] = dist / 2.0f; /* gridDistance */
644                 e_data.grid_settings[1] = grid_res; /* gridResolution */
645                 e_data.grid_settings[2] = grid_scale; /* gridScale */
646                 e_data.grid_settings[3] = v3d->gridsubdiv; /* gridSubdiv */
647                 e_data.grid_settings[4] = (v3d->gridsubdiv > 1) ? 1.0f / logf(v3d->gridsubdiv) : 0.0f; /* 1/log(gridSubdiv) */
648         }
649
650         copy_v2_v2(e_data.inv_viewport_size, DRW_viewport_size_get());
651         invert_v2(e_data.inv_viewport_size);
652 }
653
654 static void OBJECT_engine_free(void)
655 {
656         MEM_SAFE_FREE(e_data.particle_format);
657         MEM_SAFE_FREE(e_data.empty_image_format);
658         MEM_SAFE_FREE(e_data.empty_image_wire_format);
659         DRW_SHADER_FREE_SAFE(e_data.outline_prepass_sh);
660         DRW_SHADER_FREE_SAFE(e_data.outline_prepass_wire_sh);
661         DRW_SHADER_FREE_SAFE(e_data.outline_resolve_sh);
662         DRW_SHADER_FREE_SAFE(e_data.outline_resolve_aa_sh);
663         DRW_SHADER_FREE_SAFE(e_data.outline_detect_sh);
664         DRW_SHADER_FREE_SAFE(e_data.outline_detect_wire_sh);
665         DRW_SHADER_FREE_SAFE(e_data.outline_fade_sh);
666         DRW_SHADER_FREE_SAFE(e_data.object_empty_image_sh);
667         DRW_SHADER_FREE_SAFE(e_data.object_empty_image_wire_sh);
668         DRW_SHADER_FREE_SAFE(e_data.grid_sh);
669         DRW_SHADER_FREE_SAFE(e_data.part_prim_sh);
670         DRW_SHADER_FREE_SAFE(e_data.part_axis_sh);
671         DRW_SHADER_FREE_SAFE(e_data.part_dot_sh);
672         DRW_SHADER_FREE_SAFE(e_data.lightprobe_grid_sh);
673 }
674
675 static DRWShadingGroup *shgroup_outline(DRWPass *pass, const int *ofs, GPUShader *sh)
676 {
677         DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
678         DRW_shgroup_uniform_int(grp, "baseId", ofs, 1);
679
680         return grp;
681 }
682
683 /* currently same as 'shgroup_outline', new function to avoid confustion */
684 static DRWShadingGroup *shgroup_wire(DRWPass *pass, const float col[4], GPUShader *sh)
685 {
686         DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
687         DRW_shgroup_uniform_vec4(grp, "color", col, 1);
688
689         return grp;
690 }
691
692 /* currently same as 'shgroup_outline', new function to avoid confustion */
693 static DRWShadingGroup *shgroup_points(DRWPass *pass, const float col[4], GPUShader *sh)
694 {
695         DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
696         DRW_shgroup_uniform_vec4(grp, "color", col, 1);
697
698         return grp;
699 }
700
701 static int *shgroup_theme_id_to_probe_outline_counter(
702         OBJECT_StorageList *stl, int theme_id)
703 {
704         switch (theme_id) {
705                 case TH_ACTIVE:
706                         return &stl->g_data->id_ofs_prb_active;
707                 case TH_SELECT:
708                         return &stl->g_data->id_ofs_prb_select;
709                 case TH_TRANSFORM:
710                 default:
711                         return &stl->g_data->id_ofs_prb_transform;
712         }
713 }
714
715 static int *shgroup_theme_id_to_outline_counter(
716         OBJECT_StorageList *stl, int theme_id)
717 {
718         switch (theme_id) {
719                 case TH_ACTIVE:
720                         return &stl->g_data->id_ofs_active;
721                 case TH_SELECT:
722                         return &stl->g_data->id_ofs_select;
723                 case TH_TRANSFORM:
724                 default:
725                         return &stl->g_data->id_ofs_transform;
726         }
727 }
728
729 static DRWShadingGroup *shgroup_theme_id_to_probe_planar_outline_shgrp(
730         OBJECT_StorageList *stl, int theme_id)
731 {
732         /* does not increment counter */
733         switch (theme_id) {
734                 case TH_ACTIVE:
735                         return stl->g_data->lightprobes_planar_active;
736                 case TH_SELECT:
737                         return stl->g_data->lightprobes_planar_select;
738                 case TH_TRANSFORM:
739                 default:
740                         return stl->g_data->lightprobes_planar_transform;
741         }
742 }
743
744 static DRWShadingGroup *shgroup_theme_id_to_probe_cube_outline_shgrp(
745         OBJECT_StorageList *stl, int theme_id)
746 {
747         /* does not increment counter */
748         switch (theme_id) {
749                 case TH_ACTIVE:
750                         return stl->g_data->lightprobes_cube_active;
751                 case TH_SELECT:
752                         return stl->g_data->lightprobes_cube_select;
753                 case TH_TRANSFORM:
754                 default:
755                         return stl->g_data->lightprobes_cube_transform;
756         }
757 }
758
759 static DRWShadingGroup *shgroup_theme_id_to_outline_or(
760         OBJECT_StorageList *stl, int theme_id, DRWShadingGroup *fallback)
761 {
762         int *counter = shgroup_theme_id_to_outline_counter(stl, theme_id);
763         *counter += 1;
764
765         switch (theme_id) {
766                 case TH_ACTIVE:
767                         return stl->g_data->outlines_active;
768                 case TH_SELECT:
769                         return stl->g_data->outlines_select;
770                 case TH_TRANSFORM:
771                         return stl->g_data->outlines_transform;
772                 default:
773                         return fallback;
774         }
775 }
776
777 static DRWShadingGroup *shgroup_theme_id_to_wire_or(
778         OBJECT_ShadingGroupList *sgl, int theme_id, DRWShadingGroup *fallback)
779 {
780         switch (theme_id) {
781                 case TH_ACTIVE:
782                         return sgl->wire_active;
783                 case TH_SELECT:
784                         return sgl->wire_select;
785                 case TH_TRANSFORM:
786                         return sgl->wire_transform;
787                 default:
788                         return fallback;
789         }
790 }
791
792 static DRWShadingGroup *shgroup_theme_id_to_point_or(
793         OBJECT_ShadingGroupList *sgl, int theme_id, DRWShadingGroup *fallback)
794 {
795         switch (theme_id) {
796                 case TH_ACTIVE:
797                         return sgl->points_active;
798                 case TH_SELECT:
799                         return sgl->points_select;
800                 case TH_TRANSFORM:
801                         return sgl->points_transform;
802                 default:
803                         return fallback;
804         }
805 }
806
807 static void image_calc_aspect(Image *ima, ImageUser *iuser, float r_image_aspect[2])
808 {
809         float ima_x, ima_y;
810         if (ima) {
811                 int w, h;
812                 BKE_image_get_size(ima, iuser, &w, &h);
813                 ima_x = w;
814                 ima_y = h;
815         }
816         else {
817                 /* if no image, make it a 1x1 empty square, honor scale & offset */
818                 ima_x = ima_y = 1.0f;
819         }
820         /* Get the image aspect even if the buffer is invalid */
821         float sca_x = 1.0f, sca_y = 1.0f;
822         if (ima) {
823                 if (ima->aspx > ima->aspy) {
824                         sca_y = ima->aspy / ima->aspx;
825                 }
826                 else if (ima->aspx < ima->aspy) {
827                         sca_x = ima->aspx / ima->aspy;
828                 }
829         }
830
831         const float scale_x_inv = ima_x * sca_x;
832         const float scale_y_inv = ima_y * sca_y;
833         if (scale_x_inv > scale_y_inv) {
834                 r_image_aspect[0] = 1.0f;
835                 r_image_aspect[1] = scale_y_inv / scale_x_inv;
836         }
837         else {
838                 r_image_aspect[0] = scale_x_inv / scale_y_inv;
839                 r_image_aspect[1] = 1.0f;
840         }
841 }
842
843 /* per-image shading groups for image-type empty objects */
844 struct EmptyImageShadingGroupData {
845         DRWShadingGroup *shgrp_image;
846         DRWShadingGroup *shgrp_wire;
847         float image_aspect[2];
848 };
849
850 static void DRW_shgroup_empty_image(
851         OBJECT_ShadingGroupList *sgl, Object *ob, const float color[3])
852 {
853         /* TODO: 'StereoViews', see draw_empty_image. */
854
855         if (sgl->image_plane_map == NULL) {
856                 sgl->image_plane_map = BLI_ghash_ptr_new(__func__);
857         }
858
859         struct EmptyImageShadingGroupData *empty_image_data;
860
861         GPUTexture *tex = ob->data ?
862                 GPU_texture_from_blender(ob->data, ob->iuser, GL_TEXTURE_2D, false, 0.0f) : NULL;
863         void **val_p;
864
865         /* Create on demand, 'tex' may be NULL. */
866         if (BLI_ghash_ensure_p(sgl->image_plane_map, tex, &val_p)) {
867                 empty_image_data = *val_p;
868         }
869         else {
870                 empty_image_data = MEM_mallocN(sizeof(*empty_image_data), __func__);
871
872                 image_calc_aspect(ob->data, ob->iuser, empty_image_data->image_aspect);
873
874                 if (tex) {
875                         DRW_shgroup_instance_format(e_data.empty_image_format, {
876                                 {"objectColor",         DRW_ATTRIB_FLOAT, 4},
877                                 {"size",                DRW_ATTRIB_FLOAT, 1},
878                                 {"offset",              DRW_ATTRIB_FLOAT, 2},
879                                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16},
880                         });
881
882                         struct GPUBatch *geom = DRW_cache_image_plane_get();
883                         DRWShadingGroup *grp = DRW_shgroup_instance_create(
884                                 e_data.object_empty_image_sh, sgl->non_meshes, geom, e_data.empty_image_format);
885                         DRW_shgroup_uniform_texture(grp, "image", tex);
886                         DRW_shgroup_uniform_vec2(grp, "aspect", empty_image_data->image_aspect, 1);
887
888                         empty_image_data->shgrp_image = grp;
889                 }
890                 else {
891                         empty_image_data->shgrp_image = NULL;
892                 }
893
894                 {
895                         DRW_shgroup_instance_format(e_data.empty_image_wire_format, {
896                                 {"color",               DRW_ATTRIB_FLOAT, 4},
897                                 {"size",                DRW_ATTRIB_FLOAT, 1},
898                                 {"offset",              DRW_ATTRIB_FLOAT, 2},
899                                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16}
900                         });
901
902                         struct GPUBatch *geom = DRW_cache_image_plane_wire_get();
903                         DRWShadingGroup *grp = DRW_shgroup_instance_create(
904                                 e_data.object_empty_image_wire_sh, sgl->non_meshes, geom, e_data.empty_image_wire_format);
905                         DRW_shgroup_uniform_vec2(grp, "aspect", empty_image_data->image_aspect, 1);
906
907                         empty_image_data->shgrp_wire = grp;
908                 }
909
910                 *val_p = empty_image_data;
911         }
912
913         if (empty_image_data->shgrp_image != NULL) {
914                 DRW_shgroup_call_dynamic_add(
915                         empty_image_data->shgrp_image,
916                         ob->col,
917                         &ob->empty_drawsize,
918                         ob->ima_ofs,
919                         ob->obmat);
920         }
921
922         DRW_shgroup_call_dynamic_add(
923                 empty_image_data->shgrp_wire,
924                 color,
925                 &ob->empty_drawsize,
926                 ob->ima_ofs,
927                 ob->obmat);
928 }
929
930 static void OBJECT_cache_init(void *vedata)
931 {
932         OBJECT_PassList *psl = ((OBJECT_Data *)vedata)->psl;
933         OBJECT_StorageList *stl = ((OBJECT_Data *)vedata)->stl;
934         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
935         OBJECT_PrivateData *g_data;
936         const DRWContextState *draw_ctx = DRW_context_state_get();
937         const bool xray_enabled = ((draw_ctx->v3d->shading.flag & V3D_SHADING_XRAY) != 0) &&
938                                    (draw_ctx->v3d->shading.type < OB_MATERIAL);
939         /* TODO : use dpi setting for enabling the second pass */
940         const bool do_outline_expand = false;
941
942         if (!stl->g_data) {
943                 /* Alloc transient pointers */
944                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
945         }
946
947         g_data = stl->g_data;
948
949         {
950                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WIRE;
951                 psl->outlines = DRW_pass_create("Outlines Depth Pass", state);
952
953                 GPUShader *sh = e_data.outline_prepass_sh;
954
955                 if (xray_enabled) {
956                         sh = e_data.outline_prepass_wire_sh;
957                 }
958
959                 g_data->outlines_select = shgroup_outline(psl->outlines, &g_data->id_ofs_select, sh);
960                 g_data->outlines_transform = shgroup_outline(psl->outlines, &g_data->id_ofs_transform, sh);
961                 g_data->outlines_active = shgroup_outline(psl->outlines, &g_data->id_ofs_active, sh);
962
963                 g_data->id_ofs_select = 0;
964                 g_data->id_ofs_active = 0;
965                 g_data->id_ofs_transform = 0;
966         }
967
968         {
969                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_POINT;
970                 DRWPass *pass = psl->lightprobes = DRW_pass_create("Object Probe Pass", state);
971                 struct GPUBatch *sphere = DRW_cache_sphere_get();
972                 struct GPUBatch *quad = DRW_cache_quad_get();
973
974                 /* Cubemap */
975                 g_data->lightprobes_cube_select       = shgroup_instance_outline(pass, sphere, &g_data->id_ofs_prb_select);
976                 g_data->lightprobes_cube_active       = shgroup_instance_outline(pass, sphere, &g_data->id_ofs_prb_active);
977                 g_data->lightprobes_cube_transform    = shgroup_instance_outline(pass, sphere, &g_data->id_ofs_prb_transform);
978
979                 /* Planar */
980                 g_data->lightprobes_planar_select       = shgroup_instance_outline(pass, quad, &g_data->id_ofs_prb_select);
981                 g_data->lightprobes_planar_active       = shgroup_instance_outline(pass, quad, &g_data->id_ofs_prb_active);
982                 g_data->lightprobes_planar_transform    = shgroup_instance_outline(pass, quad, &g_data->id_ofs_prb_transform);
983
984                 g_data->id_ofs_prb_select = 0;
985                 g_data->id_ofs_prb_active = 0;
986                 g_data->id_ofs_prb_transform = 0;
987         }
988
989         {
990                 DRWState state = DRW_STATE_WRITE_COLOR;
991                 struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
992                 /* Don't occlude the "outline" detection pass if in xray mode (too much flickering). */
993                 float alphaOcclu = (xray_enabled) ? 1.0f : 0.35f;
994                 /* Reminder : bool uniforms need to be 4 bytes. */
995                 static const int bTrue = true;
996                 static const int bFalse = false;
997
998                 psl->outlines_search = DRW_pass_create("Outlines Detect Pass", state);
999
1000                 GPUShader *sh = (xray_enabled) ? e_data.outline_detect_wire_sh : e_data.outline_detect_sh;
1001                 DRWShadingGroup *grp = DRW_shgroup_create(sh, psl->outlines_search);
1002                 DRW_shgroup_uniform_texture_ref(grp, "outlineId", &e_data.outlines_id_tx);
1003                 DRW_shgroup_uniform_texture_ref(grp, "outlineDepth", &e_data.outlines_depth_tx);
1004                 DRW_shgroup_uniform_texture_ref(grp, "sceneDepth", &dtxl->depth);
1005                 DRW_shgroup_uniform_block(grp, "globalsBlock", globals_ubo);
1006                 DRW_shgroup_uniform_float_copy(grp, "alphaOcclu", alphaOcclu);
1007                 DRW_shgroup_uniform_int(grp, "idOffsets", &stl->g_data->id_ofs_active, 3);
1008                 DRW_shgroup_call_add(grp, quad, NULL);
1009
1010                 psl->outlines_expand = DRW_pass_create("Outlines Expand Pass", state);
1011
1012                 grp = DRW_shgroup_create(e_data.outline_fade_sh, psl->outlines_expand);
1013                 DRW_shgroup_uniform_texture_ref(grp, "outlineColor", &e_data.outlines_blur_tx);
1014                 DRW_shgroup_uniform_bool(grp, "doExpand", (do_outline_expand) ? &bTrue : &bFalse, 1);
1015                 DRW_shgroup_call_add(grp, quad, NULL);
1016
1017                 psl->outlines_bleed = DRW_pass_create("Outlines Bleed Pass", state);
1018
1019                 if (do_outline_expand) {
1020                         grp = DRW_shgroup_create(e_data.outline_fade_sh, psl->outlines_bleed);
1021                         DRW_shgroup_uniform_texture_ref(grp, "outlineColor", &e_data.outlines_color_tx);
1022                         DRW_shgroup_uniform_bool(grp, "doExpand", &bFalse, 1);
1023                         DRW_shgroup_call_add(grp, quad, NULL);
1024                 }
1025         }
1026
1027         {
1028                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND;
1029                 psl->outlines_resolve = DRW_pass_create("Outlines Resolve Pass", state);
1030
1031                 struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
1032                 GPUTexture **outline_tx = (do_outline_expand) ? &e_data.outlines_blur_tx : &e_data.outlines_color_tx;
1033
1034                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.outline_resolve_aa_sh, psl->outlines_resolve);
1035                 DRW_shgroup_uniform_texture_ref(grp, "outlineBluredColor", outline_tx);
1036                 DRW_shgroup_uniform_vec2(grp, "rcpDimensions", e_data.inv_viewport_size, 1);
1037                 DRW_shgroup_call_add(grp, quad, NULL);
1038         }
1039
1040         {
1041                 /* Grid pass */
1042                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND;
1043                 psl->grid = DRW_pass_create("Infinite Grid Pass", state);
1044
1045                 struct GPUBatch *geom = DRW_cache_grid_get();
1046                 static float mat[4][4];
1047                 unit_m4(mat);
1048
1049                 /* Create 3 quads to render ordered transparency Z axis */
1050                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.grid_sh, psl->grid);
1051                 DRW_shgroup_uniform_int(grp, "gridFlag", &e_data.zneg_flag, 1);
1052                 DRW_shgroup_uniform_vec3(grp, "planeNormal", e_data.zplane_normal, 1);
1053                 DRW_shgroup_uniform_vec3(grp, "planeAxes", e_data.zplane_axes, 1);
1054                 DRW_shgroup_uniform_vec3(grp, "cameraPos", e_data.camera_pos, 1);
1055                 DRW_shgroup_uniform_vec4(grp, "screenvecs[0]", e_data.screenvecs[0], 3);
1056                 DRW_shgroup_uniform_vec4(grp, "gridSettings", e_data.grid_settings, 1);
1057                 DRW_shgroup_uniform_float(grp, "gridOneOverLogSubdiv", &e_data.grid_settings[4], 1);
1058                 DRW_shgroup_uniform_block(grp, "globalsBlock", globals_ubo);
1059                 DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
1060                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
1061                 DRW_shgroup_call_add(grp, geom, mat);
1062
1063                 grp = DRW_shgroup_create(e_data.grid_sh, psl->grid);
1064                 DRW_shgroup_uniform_int(grp, "gridFlag", &e_data.grid_flag, 1);
1065                 DRW_shgroup_uniform_vec3(grp, "planeNormal", e_data.grid_normal, 1);
1066                 DRW_shgroup_uniform_vec3(grp, "planeAxes", e_data.grid_axes, 1);
1067                 DRW_shgroup_uniform_block(grp, "globalsBlock", globals_ubo);
1068                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
1069                 DRW_shgroup_call_add(grp, geom, mat);
1070
1071                 grp = DRW_shgroup_create(e_data.grid_sh, psl->grid);
1072                 DRW_shgroup_uniform_int(grp, "gridFlag", &e_data.zpos_flag, 1);
1073                 DRW_shgroup_uniform_vec3(grp, "planeNormal", e_data.zplane_normal, 1);
1074                 DRW_shgroup_uniform_vec3(grp, "planeAxes", e_data.zplane_axes, 1);
1075                 DRW_shgroup_uniform_block(grp, "globalsBlock", globals_ubo);
1076                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
1077                 DRW_shgroup_call_add(grp, geom, mat);
1078         }
1079
1080         for (int i = 0; i < 2; ++i) {
1081                 OBJECT_ShadingGroupList *sgl = (i == 1) ? &stl->g_data->sgl_ghost : &stl->g_data->sgl;
1082
1083                 /* Solid bones */
1084                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL;
1085                 sgl->bone_solid = psl->bone_solid[i] = DRW_pass_create("Bone Solid Pass", state);
1086                 sgl->bone_outline = psl->bone_outline[i] = DRW_pass_create("Bone Outline Pass", state);
1087
1088                 /* Wire bones */
1089                 state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND;
1090                 sgl->bone_wire = psl->bone_wire[i] = DRW_pass_create("Bone Wire Pass", state);
1091
1092                 /* distance outline around envelope bones */
1093                 state = DRW_STATE_ADDITIVE | DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CULL_FRONT;
1094                 sgl->bone_envelope = psl->bone_envelope[i] = DRW_pass_create("Bone Envelope Outline Pass", state);
1095
1096                 state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WIRE;
1097                 sgl->bone_axes = psl->bone_axes[i] = DRW_pass_create("Bone Axes Pass", state);
1098         }
1099
1100         for (int i = 0; i < 2; ++i) {
1101                 OBJECT_ShadingGroupList *sgl = (i == 1) ? &stl->g_data->sgl_ghost : &stl->g_data->sgl;
1102
1103                 /* Non Meshes Pass (Camera, empties, lamps ...) */
1104                 struct GPUBatch *geom;
1105                 struct GPUShader *sh;
1106
1107                 DRWState state =
1108                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH |
1109                         DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND | DRW_STATE_POINT | DRW_STATE_WIRE;
1110                 sgl->non_meshes = psl->non_meshes[i] = DRW_pass_create("Non Meshes Pass", state);
1111
1112                 /* Empties */
1113                 geom = DRW_cache_plain_axes_get();
1114                 sgl->plain_axes = shgroup_instance(sgl->non_meshes, geom);
1115
1116                 geom = DRW_cache_empty_cube_get();
1117                 sgl->cube = shgroup_instance(sgl->non_meshes, geom);
1118
1119                 geom = DRW_cache_circle_get();
1120                 sgl->circle = shgroup_instance(sgl->non_meshes, geom);
1121
1122                 geom = DRW_cache_empty_sphere_get();
1123                 sgl->sphere = shgroup_instance(sgl->non_meshes, geom);
1124
1125                 geom = DRW_cache_sphere_get();
1126                 sgl->sphere_solid = shgroup_instance_solid(sgl->non_meshes, geom);
1127
1128                 geom = DRW_cache_empty_cylinder_get();
1129                 sgl->cylinder = shgroup_instance(sgl->non_meshes, geom);
1130
1131                 geom = DRW_cache_empty_capsule_cap_get();
1132                 sgl->capsule_cap = shgroup_instance(sgl->non_meshes, geom);
1133
1134                 geom = DRW_cache_empty_capsule_body_get();
1135                 sgl->capsule_body = shgroup_instance(sgl->non_meshes, geom);
1136
1137                 geom = DRW_cache_empty_cone_get();
1138                 sgl->cone = shgroup_instance(sgl->non_meshes, geom);
1139
1140                 geom = DRW_cache_single_arrow_get();
1141                 sgl->single_arrow = shgroup_instance(sgl->non_meshes, geom);
1142
1143                 geom = DRW_cache_single_line_get();
1144                 sgl->single_arrow_line = shgroup_instance(sgl->non_meshes, geom);
1145
1146                 geom = DRW_cache_bone_arrows_get();
1147                 sgl->empty_axes = shgroup_instance_empty_axes(sgl->non_meshes, geom);
1148
1149                 /* initialize on first use */
1150                 sgl->image_plane_map = NULL;
1151
1152                 /* Force Field */
1153                 geom = DRW_cache_field_wind_get();
1154                 sgl->field_wind = shgroup_instance_scaled(sgl->non_meshes, geom);
1155
1156                 geom = DRW_cache_field_force_get();
1157                 sgl->field_force = shgroup_instance_screen_aligned(sgl->non_meshes, geom);
1158
1159                 geom = DRW_cache_field_vortex_get();
1160                 sgl->field_vortex = shgroup_instance_scaled(sgl->non_meshes, geom);
1161
1162                 geom = DRW_cache_screenspace_circle_get();
1163                 sgl->field_curve_sta = shgroup_instance_screen_aligned(sgl->non_meshes, geom);
1164
1165                 /* Grease Pencil */
1166                 geom = DRW_cache_gpencil_axes_get();
1167                 sgl->gpencil_axes = shgroup_instance(sgl->non_meshes, geom);
1168
1169                 /* Speaker */
1170                 geom = DRW_cache_speaker_get();
1171                 sgl->speaker = shgroup_instance(sgl->non_meshes, geom);
1172
1173                 /* Probe */
1174                 static float probeSize = 14.0f;
1175                 geom = DRW_cache_lightprobe_cube_get();
1176                 sgl->probe_cube = shgroup_instance_screenspace(sgl->non_meshes, geom, &probeSize);
1177
1178                 geom = DRW_cache_lightprobe_grid_get();
1179                 sgl->probe_grid = shgroup_instance_screenspace(sgl->non_meshes, geom, &probeSize);
1180
1181                 static float probePlanarSize = 20.0f;
1182                 geom = DRW_cache_lightprobe_planar_get();
1183                 sgl->probe_planar = shgroup_instance_screenspace(sgl->non_meshes, geom, &probePlanarSize);
1184
1185                 /* Camera */
1186                 geom = DRW_cache_camera_get();
1187                 sgl->camera = shgroup_camera_instance(sgl->non_meshes, geom);
1188
1189                 geom = DRW_cache_camera_frame_get();
1190                 sgl->camera_frame = shgroup_camera_instance(sgl->non_meshes, geom);
1191
1192                 geom = DRW_cache_camera_tria_get();
1193                 sgl->camera_tria = shgroup_camera_instance(sgl->non_meshes, geom);
1194
1195                 geom = DRW_cache_plain_axes_get();
1196                 sgl->camera_focus = shgroup_instance(sgl->non_meshes, geom);
1197
1198                 geom = DRW_cache_single_line_get();
1199                 sgl->camera_clip = shgroup_distance_lines_instance(sgl->non_meshes, geom);
1200                 sgl->camera_mist = shgroup_distance_lines_instance(sgl->non_meshes, geom);
1201
1202                 geom = DRW_cache_single_line_endpoints_get();
1203                 sgl->camera_clip_points = shgroup_distance_lines_instance(sgl->non_meshes, geom);
1204                 sgl->camera_mist_points = shgroup_distance_lines_instance(sgl->non_meshes, geom);
1205
1206                 BLI_listbase_clear(&sgl->camera_path);
1207
1208                 /* Texture Space */
1209                 geom = DRW_cache_empty_cube_get();
1210                 sgl->texspace = shgroup_instance(sgl->non_meshes, geom);
1211
1212                 /* Wires (for loose edges) */
1213                 sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
1214                 sgl->wire = shgroup_wire(sgl->non_meshes, ts.colorWire, sh);
1215                 sgl->wire_select = shgroup_wire(sgl->non_meshes, ts.colorSelect, sh);
1216                 sgl->wire_transform = shgroup_wire(sgl->non_meshes, ts.colorTransform, sh);
1217                 sgl->wire_active = shgroup_wire(sgl->non_meshes, ts.colorActive, sh);
1218
1219                 /* Points (loose points) */
1220                 sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_POINT_FIXED_SIZE_UNIFORM_COLOR);
1221                 sgl->points = shgroup_points(sgl->non_meshes, ts.colorWire, sh);
1222                 sgl->points_select = shgroup_points(sgl->non_meshes, ts.colorSelect, sh);
1223                 sgl->points_transform = shgroup_points(sgl->non_meshes, ts.colorTransform, sh);
1224                 sgl->points_active = shgroup_points(sgl->non_meshes, ts.colorActive, sh);
1225
1226                 /* Metaballs Handles */
1227                 sgl->mball_handle = shgroup_instance_mball_handles(sgl->non_meshes);
1228
1229                 /* Lamps */
1230                 /* TODO
1231                  * for now we create multiple times the same VBO with only lamp center coordinates
1232                  * but ideally we would only create it once */
1233
1234                 /* start with buflimit because we don't want stipples */
1235                 geom = DRW_cache_single_line_get();
1236                 sgl->lamp_buflimit = shgroup_distance_lines_instance(sgl->non_meshes, geom);
1237
1238                 sgl->lamp_center = shgroup_dynpoints_uniform_color(sgl->non_meshes, ts.colorLampNoAlpha, &ts.sizeLampCenter);
1239
1240                 geom = DRW_cache_lamp_get();
1241                 sgl->lamp_circle = shgroup_instance_screenspace(sgl->non_meshes, geom, &ts.sizeLampCircle);
1242                 geom = DRW_cache_lamp_shadows_get();
1243                 sgl->lamp_circle_shadow = shgroup_instance_screenspace(sgl->non_meshes, geom, &ts.sizeLampCircleShadow);
1244
1245                 geom = DRW_cache_lamp_sunrays_get();
1246                 sgl->lamp_sunrays = shgroup_instance_screenspace(sgl->non_meshes, geom, &ts.sizeLampCircle);
1247
1248                 sgl->lamp_groundline = shgroup_groundlines_uniform_color(sgl->non_meshes, ts.colorLamp);
1249                 sgl->lamp_groundpoint = shgroup_groundpoints_uniform_color(sgl->non_meshes, ts.colorLamp);
1250
1251                 geom = DRW_cache_screenspace_circle_get();
1252                 sgl->lamp_area_sphere = shgroup_instance_screen_aligned(sgl->non_meshes, geom);
1253
1254                 geom = DRW_cache_lamp_area_square_get();
1255                 sgl->lamp_area_square = shgroup_instance(sgl->non_meshes, geom);
1256
1257                 geom = DRW_cache_lamp_area_disk_get();
1258                 sgl->lamp_area_disk = shgroup_instance(sgl->non_meshes, geom);
1259
1260                 geom = DRW_cache_lamp_hemi_get();
1261                 sgl->lamp_hemi = shgroup_instance(sgl->non_meshes, geom);
1262
1263                 geom = DRW_cache_single_line_get();
1264                 sgl->lamp_distance = shgroup_distance_lines_instance(sgl->non_meshes, geom);
1265
1266                 geom = DRW_cache_single_line_endpoints_get();
1267                 sgl->lamp_buflimit_points = shgroup_distance_lines_instance(sgl->non_meshes, geom);
1268
1269                 geom = DRW_cache_lamp_spot_get();
1270                 sgl->lamp_spot_cone = shgroup_spot_instance(sgl->non_meshes, geom);
1271
1272                 geom = DRW_cache_circle_get();
1273                 sgl->lamp_spot_blend = shgroup_instance(sgl->non_meshes, geom);
1274
1275                 geom = DRW_cache_lamp_spot_square_get();
1276                 sgl->lamp_spot_pyramid = shgroup_instance(sgl->non_meshes, geom);
1277
1278                 geom = DRW_cache_square_get();
1279                 sgl->lamp_spot_blend_rect = shgroup_instance(sgl->non_meshes, geom);
1280
1281                 /* -------- STIPPLES ------- */
1282
1283                 /* Relationship Lines */
1284                 sgl->relationship_lines = shgroup_dynlines_dashed_uniform_color(sgl->non_meshes, ts.colorWire);
1285                 sgl->constraint_lines = shgroup_dynlines_dashed_uniform_color(sgl->non_meshes, ts.colorGridAxisZ);
1286
1287                 /* Force Field Curve Guide End (here because of stipple) */
1288                 /* TODO port to shader stipple */
1289                 geom = DRW_cache_screenspace_circle_get();
1290                 sgl->field_curve_end = shgroup_instance_screen_aligned(sgl->non_meshes, geom);
1291
1292                 /* Force Field Limits */
1293                 /* TODO port to shader stipple */
1294                 geom = DRW_cache_field_tube_limit_get();
1295                 sgl->field_tube_limit = shgroup_instance_scaled(sgl->non_meshes, geom);
1296
1297                 /* TODO port to shader stipple */
1298                 geom = DRW_cache_field_cone_limit_get();
1299                 sgl->field_cone_limit = shgroup_instance_scaled(sgl->non_meshes, geom);
1300
1301                 /* Spot shapes */
1302                 state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND | DRW_STATE_CULL_FRONT;
1303                 sgl->spot_shapes = psl->spot_shapes[i] = DRW_pass_create("Spot Shape Pass", state);
1304                 float cone_spot_alpha = 0.5f;
1305
1306                 geom = DRW_cache_lamp_spot_volume_get();
1307                 sgl->lamp_spot_volume = shgroup_instance_alpha(sgl->spot_shapes, geom, cone_spot_alpha);
1308
1309                 geom = DRW_cache_lamp_spot_square_volume_get();
1310                 sgl->lamp_spot_volume_rect = shgroup_instance_alpha(sgl->spot_shapes, geom, cone_spot_alpha);
1311
1312                 cone_spot_alpha = 0.3f;
1313                 geom = DRW_cache_lamp_spot_volume_get();
1314                 sgl->lamp_spot_volume_outside = shgroup_instance_alpha(sgl->spot_shapes, geom, cone_spot_alpha);
1315                 DRW_shgroup_state_disable(sgl->lamp_spot_volume_outside, DRW_STATE_CULL_FRONT);
1316                 DRW_shgroup_state_enable(sgl->lamp_spot_volume_outside, DRW_STATE_CULL_BACK);
1317
1318                 geom = DRW_cache_lamp_spot_square_volume_get();
1319                 sgl->lamp_spot_volume_rect_outside = shgroup_instance_alpha(sgl->spot_shapes, geom, cone_spot_alpha);
1320                 DRW_shgroup_state_disable(sgl->lamp_spot_volume_rect_outside, DRW_STATE_CULL_FRONT);
1321                 DRW_shgroup_state_enable(sgl->lamp_spot_volume_rect_outside, DRW_STATE_CULL_BACK);
1322         }
1323
1324         {
1325                 /* Object Center pass grouped by State */
1326                 DRWShadingGroup *grp;
1327                 static float outlineWidth, size;
1328
1329                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND | DRW_STATE_POINT;
1330                 psl->ob_center = DRW_pass_create("Obj Center Pass", state);
1331
1332                 outlineWidth = 1.0f * U.pixelsize;
1333                 size = U.obcenter_dia * U.pixelsize + outlineWidth;
1334
1335                 GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA);
1336
1337                 /* Active */
1338                 grp = DRW_shgroup_point_batch_create(sh, psl->ob_center);
1339                 DRW_shgroup_uniform_float(grp, "size", &size, 1);
1340                 DRW_shgroup_uniform_float(grp, "outlineWidth", &outlineWidth, 1);
1341                 DRW_shgroup_uniform_vec4(grp, "color", ts.colorActive, 1);
1342                 DRW_shgroup_uniform_vec4(grp, "outlineColor", ts.colorOutline, 1);
1343                 stl->g_data->center_active = grp;
1344
1345                 /* Select */
1346                 grp = DRW_shgroup_point_batch_create(sh, psl->ob_center);
1347                 DRW_shgroup_uniform_vec4(grp, "color", ts.colorSelect, 1);
1348                 stl->g_data->center_selected = grp;
1349
1350                 /* Deselect */
1351                 grp = DRW_shgroup_point_batch_create(sh, psl->ob_center);
1352                 DRW_shgroup_uniform_vec4(grp, "color", ts.colorDeselect, 1);
1353                 stl->g_data->center_deselected = grp;
1354
1355                 /* Select (library) */
1356                 grp = DRW_shgroup_point_batch_create(sh, psl->ob_center);
1357                 DRW_shgroup_uniform_vec4(grp, "color", ts.colorLibrarySelect, 1);
1358                 stl->g_data->center_selected_lib = grp;
1359
1360                 /* Deselect (library) */
1361                 grp = DRW_shgroup_point_batch_create(sh, psl->ob_center);
1362                 DRW_shgroup_uniform_vec4(grp, "color", ts.colorLibrary, 1);
1363                 stl->g_data->center_deselected_lib = grp;
1364         }
1365
1366         {
1367                 /* Particle Pass */
1368                 psl->particle = DRW_pass_create(
1369                         "Particle Pass",
1370                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL |
1371                         DRW_STATE_POINT | DRW_STATE_BLEND);
1372         }
1373 }
1374
1375 static void DRW_shgroup_mball_handles(OBJECT_ShadingGroupList *sgl, Object *ob, ViewLayer *view_layer)
1376 {
1377         MetaBall *mb = ob->data;
1378
1379         float *color;
1380         DRW_object_wire_theme_get(ob, view_layer, &color);
1381
1382         float draw_scale_xform[3][4]; /* Matrix of Scale and Translation */
1383         {
1384                 float scamat[3][3];
1385                 copy_m3_m4(scamat, ob->obmat);
1386                 /* Get the normalized inverse matrix to extract only
1387                  * the scale of Scamat */
1388                 float iscamat[3][3];
1389                 invert_m3_m3(iscamat, scamat);
1390                 normalize_m3(iscamat);
1391                 mul_m3_m3_post(scamat, iscamat);
1392
1393                 copy_v3_v3(draw_scale_xform[0], scamat[0]);
1394                 copy_v3_v3(draw_scale_xform[1], scamat[1]);
1395                 copy_v3_v3(draw_scale_xform[2], scamat[2]);
1396         }
1397
1398         for (MetaElem *ml = mb->elems.first; ml != NULL; ml = ml->next) {
1399                 /* draw radius */
1400                 float world_pos[3];
1401                 mul_v3_m4v3(world_pos, ob->obmat, &ml->x);
1402                 draw_scale_xform[0][3] = world_pos[0];
1403                 draw_scale_xform[1][3] = world_pos[1];
1404                 draw_scale_xform[2][3] = world_pos[2];
1405
1406                 DRW_shgroup_call_dynamic_add(sgl->mball_handle, draw_scale_xform, &ml->rad, color);
1407         }
1408 }
1409
1410 static void DRW_shgroup_lamp(OBJECT_ShadingGroupList *sgl, Object *ob, ViewLayer *view_layer)
1411 {
1412         Lamp *la = ob->data;
1413         float *color;
1414         int theme_id = DRW_object_wire_theme_get(ob, view_layer, &color);
1415         static float zero = 0.0f;
1416
1417         typedef struct LampEngineData {
1418                 DrawData dd;
1419                 float shape_mat[4][4];
1420                 float spot_blend_mat[4][4];
1421         } LampEngineData;
1422
1423         LampEngineData *lamp_engine_data =
1424                 (LampEngineData *)DRW_drawdata_ensure(
1425                         &ob->id,
1426                         &draw_engine_object_type,
1427                         sizeof(LampEngineData),
1428                         NULL,
1429                         NULL);
1430
1431         float (*shapemat)[4] = lamp_engine_data->shape_mat;
1432         float (*spotblendmat)[4] = lamp_engine_data->spot_blend_mat;
1433
1434         /* Don't draw the center if it's selected or active */
1435         if (theme_id == TH_LAMP)
1436                 DRW_shgroup_call_dynamic_add(sgl->lamp_center, ob->obmat[3]);
1437
1438         /* First circle */
1439         DRW_shgroup_call_dynamic_add(sgl->lamp_circle, ob->obmat[3], color);
1440
1441         /* draw dashed outer circle for shadow */
1442         if (la->type != LA_HEMI) {
1443                 DRW_shgroup_call_dynamic_add(sgl->lamp_circle_shadow, ob->obmat[3], color);
1444         }
1445
1446         /* Distance */
1447         if (ELEM(la->type, LA_HEMI, LA_SUN, LA_AREA)) {
1448                 DRW_shgroup_call_dynamic_add(sgl->lamp_distance, color, &zero, &la->dist, ob->obmat);
1449         }
1450
1451         copy_m4_m4(shapemat, ob->obmat);
1452
1453         if (la->type == LA_SUN) {
1454                 DRW_shgroup_call_dynamic_add(sgl->lamp_sunrays, ob->obmat[3], color);
1455         }
1456         else if (la->type == LA_SPOT) {
1457                 float size[3], sizemat[4][4];
1458                 static float one = 1.0f;
1459                 float cone_inside[3] = {0.0f, 0.0f, 0.0f};
1460                 float cone_outside[3] = {1.0f, 1.0f, 1.0f};
1461                 float blend = 1.0f - pow2f(la->spotblend);
1462                 size[0] = size[1] = sinf(la->spotsize * 0.5f) * la->dist;
1463                 size[2] = cosf(la->spotsize * 0.5f) * la->dist;
1464
1465                 size_to_mat4(sizemat, size);
1466                 mul_m4_m4m4(shapemat, ob->obmat, sizemat);
1467
1468                 size[0] = size[1] = blend; size[2] = 1.0f;
1469                 size_to_mat4(sizemat, size);
1470                 translate_m4(sizemat, 0.0f, 0.0f, -1.0f);
1471                 rotate_m4(sizemat, 'X', (float)(M_PI / 2));
1472                 mul_m4_m4m4(spotblendmat, shapemat, sizemat);
1473
1474                 if (la->mode & LA_SQUARE) {
1475                         DRW_shgroup_call_dynamic_add(sgl->lamp_spot_pyramid, color, &one, shapemat);
1476
1477                         /* hide line if it is zero size or overlaps with outer border,
1478                          * previously it adjusted to always to show it but that seems
1479                          * confusing because it doesn't show the actual blend size */
1480                         if (blend != 0.0f && blend != 1.0f) {
1481                                 DRW_shgroup_call_dynamic_add(sgl->lamp_spot_blend_rect, color, &one, spotblendmat);
1482                         }
1483
1484                         if (la->mode & LA_SHOW_CONE) {
1485
1486                                 DRW_shgroup_call_dynamic_add(sgl->lamp_spot_volume_rect, cone_inside, &one, shapemat);
1487                                 DRW_shgroup_call_dynamic_add(sgl->lamp_spot_volume_rect_outside, cone_outside, &one, shapemat);
1488                         }
1489                 }
1490                 else {
1491                         DRW_shgroup_call_dynamic_add(sgl->lamp_spot_cone, color, shapemat);
1492
1493                         /* hide line if it is zero size or overlaps with outer border,
1494                          * previously it adjusted to always to show it but that seems
1495                          * confusing because it doesn't show the actual blend size */
1496                         if (blend != 0.0f && blend != 1.0f) {
1497                                 DRW_shgroup_call_dynamic_add(sgl->lamp_spot_blend, color, &one, spotblendmat);
1498                         }
1499
1500                         if (la->mode & LA_SHOW_CONE) {
1501                                 DRW_shgroup_call_dynamic_add(sgl->lamp_spot_volume, cone_inside, &one, shapemat);
1502                                 DRW_shgroup_call_dynamic_add(sgl->lamp_spot_volume_outside, cone_outside, &one, shapemat);
1503                         }
1504                 }
1505
1506                 DRW_shgroup_call_dynamic_add(sgl->lamp_buflimit, color, &la->clipsta, &la->clipend, ob->obmat);
1507                 DRW_shgroup_call_dynamic_add(sgl->lamp_buflimit_points, color, &la->clipsta, &la->clipend, ob->obmat);
1508         }
1509         else if (la->type == LA_HEMI) {
1510                 static float hemisize = 2.0f;
1511                 DRW_shgroup_call_dynamic_add(sgl->lamp_hemi, color, &hemisize, shapemat);
1512         }
1513         else if (la->type == LA_AREA) {
1514                 float size[3] = {1.0f, 1.0f, 1.0f}, sizemat[4][4];
1515
1516                 if (ELEM(la->area_shape, LA_AREA_RECT, LA_AREA_ELLIPSE)) {
1517                         size[1] = la->area_sizey / la->area_size;
1518                         size_to_mat4(sizemat, size);
1519                         mul_m4_m4m4(shapemat, shapemat, sizemat);
1520                 }
1521
1522                 if (ELEM(la->area_shape, LA_AREA_DISK, LA_AREA_ELLIPSE)) {
1523                         DRW_shgroup_call_dynamic_add(sgl->lamp_area_disk, color, &la->area_size, shapemat);
1524                 }
1525                 else {
1526                         DRW_shgroup_call_dynamic_add(sgl->lamp_area_square, color, &la->area_size, shapemat);
1527                 }
1528         }
1529
1530         if (ELEM(la->type, LA_LOCAL, LA_SPOT)) {
1531                 /* We only want position not scale. */
1532                 shapemat[0][0] = shapemat[1][1] = shapemat[2][2] = 1.0f;
1533                 shapemat[0][1] = shapemat[0][2] = 0.0f;
1534                 shapemat[1][0] = shapemat[1][2] = 0.0f;
1535                 shapemat[2][0] = shapemat[2][1] = 0.0f;
1536                 DRW_shgroup_call_dynamic_add(sgl->lamp_area_sphere, color, &la->area_size, shapemat);
1537         }
1538
1539         /* Line and point going to the ground */
1540         DRW_shgroup_call_dynamic_add(sgl->lamp_groundline, ob->obmat[3]);
1541         DRW_shgroup_call_dynamic_add(sgl->lamp_groundpoint, ob->obmat[3]);
1542 }
1543
1544 static GPUBatch *batch_camera_path_get(
1545         ListBase *camera_paths, const MovieTrackingReconstruction *reconstruction)
1546 {
1547         GPUBatch *geom;
1548         static GPUVertFormat format = { 0 };
1549         static struct { uint pos; } attr_id;
1550         if (format.attr_len == 0) {
1551                 attr_id.pos = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
1552         }
1553         GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format);
1554         GPU_vertbuf_data_alloc(vbo, reconstruction->camnr);
1555
1556         MovieReconstructedCamera *camera = reconstruction->cameras;
1557         for (int a = 0; a < reconstruction->camnr; a++, camera++) {
1558                 GPU_vertbuf_attr_set(vbo, attr_id.pos, a, camera->mat[3]);
1559         }
1560
1561         geom = GPU_batch_create_ex(GPU_PRIM_LINE_STRIP, vbo, NULL, GPU_BATCH_OWNS_VBO);
1562
1563         /* Store the batch to do cleanup after drawing. */
1564         BLI_addtail(camera_paths, BLI_genericNodeN(geom));
1565         return geom;
1566 }
1567
1568 static void batch_camera_path_free(ListBase *camera_paths)
1569 {
1570         LinkData *link;
1571         while ((link = BLI_pophead(camera_paths))) {
1572                 GPUBatch *camera_path = link->data;
1573                 GPU_batch_discard(camera_path);
1574                 MEM_freeN(link);
1575         }
1576 }
1577
1578 static void DRW_shgroup_camera(OBJECT_ShadingGroupList *sgl, Object *ob, ViewLayer *view_layer)
1579 {
1580         const DRWContextState *draw_ctx = DRW_context_state_get();
1581         View3D *v3d = draw_ctx->v3d;
1582         Scene *scene = draw_ctx->scene;
1583         RegionView3D *rv3d = draw_ctx->rv3d;
1584
1585         Camera *cam = ob->data;
1586         const  Object *camera_object = DEG_get_evaluated_object(draw_ctx->depsgraph, v3d->camera);
1587         const bool is_active = (ob == camera_object);
1588         const bool look_through = (is_active && (rv3d->persp == RV3D_CAMOB));
1589         float *color;
1590         DRW_object_wire_theme_get(ob, view_layer, &color);
1591
1592         float vec[4][3], asp[2], shift[2], scale[3], drawsize;
1593
1594         scale[0] = 1.0f / len_v3(ob->obmat[0]);
1595         scale[1] = 1.0f / len_v3(ob->obmat[1]);
1596         scale[2] = 1.0f / len_v3(ob->obmat[2]);
1597
1598         BKE_camera_view_frame_ex(scene, cam, cam->drawsize, false, scale,
1599                                  asp, shift, &drawsize, vec);
1600
1601         /* Frame coords */
1602         copy_v2_v2(cam->drwcorners[0], vec[0]);
1603         copy_v2_v2(cam->drwcorners[1], vec[1]);
1604         copy_v2_v2(cam->drwcorners[2], vec[2]);
1605         copy_v2_v2(cam->drwcorners[3], vec[3]);
1606
1607         /* depth */
1608         cam->drwdepth = vec[0][2];
1609
1610         /* tria */
1611         cam->drwtria[0][0] = shift[0] + ((0.7f * drawsize) * scale[0]);
1612         cam->drwtria[0][1] = shift[1] + ((drawsize * (asp[1] + 0.1f)) * scale[1]);
1613         cam->drwtria[1][0] = shift[0];
1614         cam->drwtria[1][1] = shift[1] + ((1.1f * drawsize * (asp[1] + 0.7f)) * scale[1]);
1615
1616         if (look_through) {
1617                 /* Only draw the frame. */
1618                 DRW_shgroup_call_dynamic_add(
1619                         sgl->camera_frame, color, cam->drwcorners,
1620                         &cam->drwdepth, cam->drwtria, ob->obmat);
1621         }
1622         else {
1623                 DRW_shgroup_call_dynamic_add(
1624                         sgl->camera, color, cam->drwcorners,
1625                         &cam->drwdepth, cam->drwtria, ob->obmat);
1626
1627                 /* Active cam */
1628                 if (is_active) {
1629                         DRW_shgroup_call_dynamic_add(
1630                                 sgl->camera_tria, color,
1631                                 cam->drwcorners, &cam->drwdepth, cam->drwtria, ob->obmat);
1632                 }
1633         }
1634
1635         /* draw the rest in normalize object space */
1636         normalize_m4_m4(cam->drwnormalmat, ob->obmat);
1637
1638         if (cam->flag & CAM_SHOWLIMITS) {
1639                 static float col[3] = {0.5f, 0.5f, 0.25f}, col_hi[3] = {1.0f, 1.0f, 0.5f};
1640                 float sizemat[4][4], size[3] = {1.0f, 1.0f, 0.0f};
1641                 float focusdist = BKE_camera_object_dof_distance(ob);
1642
1643                 copy_m4_m4(cam->drwfocusmat, cam->drwnormalmat);
1644                 translate_m4(cam->drwfocusmat, 0.0f, 0.0f, -focusdist);
1645                 size_to_mat4(sizemat, size);
1646                 mul_m4_m4m4(cam->drwfocusmat, cam->drwfocusmat, sizemat);
1647
1648                 DRW_shgroup_call_dynamic_add(
1649                         sgl->camera_focus, (is_active ? col_hi : col),
1650                         &cam->drawsize, cam->drwfocusmat);
1651
1652                 DRW_shgroup_call_dynamic_add(
1653                         sgl->camera_clip, color,
1654                         &cam->clipsta, &cam->clipend, cam->drwnormalmat);
1655                 DRW_shgroup_call_dynamic_add(
1656                         sgl->camera_clip_points, (is_active ? col_hi : col),
1657                         &cam->clipsta, &cam->clipend, cam->drwnormalmat);
1658         }
1659
1660         if (cam->flag & CAM_SHOWMIST) {
1661                 World *world = scene->world;
1662
1663                 if (world) {
1664                         static float col[3] = {0.5f, 0.5f, 0.5f}, col_hi[3] = {1.0f, 1.0f, 1.0f};
1665                         world->mistend = world->miststa + world->mistdist;
1666                         DRW_shgroup_call_dynamic_add(
1667                                 sgl->camera_mist, color,
1668                                 &world->miststa, &world->mistend, cam->drwnormalmat);
1669                         DRW_shgroup_call_dynamic_add(
1670                                 sgl->camera_mist_points, (is_active ? col_hi : col),
1671                                 &world->miststa, &world->mistend, cam->drwnormalmat);
1672                 }
1673         }
1674
1675         /* Motion Tracking. */
1676         MovieClip *clip = BKE_object_movieclip_get(scene, ob, false);
1677         if ((v3d->flag2 & V3D_SHOW_RECONSTRUCTION) && (clip != NULL)) {
1678                 BLI_assert(BLI_listbase_is_empty(&sgl->camera_path));
1679                 const bool is_select = DRW_state_is_select();
1680                 const bool is_solid_bundle = (v3d->bundle_drawtype == OB_EMPTY_SPHERE) &&
1681                                              ((v3d->shading.type != OB_SOLID) ||
1682                                               ((v3d->shading.flag & V3D_SHADING_XRAY) == 0));
1683
1684                 MovieTracking *tracking = &clip->tracking;
1685                 /* Index must start in 1, to mimic BKE_tracking_track_get_indexed. */
1686                 int track_index = 1;
1687
1688                 uchar text_color_selected[4], text_color_unselected[4];
1689                 float bundle_color_unselected[4], bundle_color_solid[4];
1690
1691                 UI_GetThemeColor4ubv(TH_SELECT, text_color_selected);
1692                 UI_GetThemeColor4ubv(TH_TEXT, text_color_unselected);
1693                 UI_GetThemeColor4fv(TH_WIRE, bundle_color_unselected);
1694                 UI_GetThemeColor4fv(TH_BUNDLE_SOLID, bundle_color_solid);
1695
1696                 float camera_mat[4][4];
1697                 BKE_tracking_get_camera_object_matrix(draw_ctx->depsgraph, scene, ob, camera_mat);
1698
1699                 float bundle_scale_mat[4][4];
1700                 if (is_solid_bundle) {
1701                         scale_m4_fl(bundle_scale_mat, v3d->bundle_size);
1702                 }
1703
1704                 for (MovieTrackingObject *tracking_object = tracking->objects.first;
1705                      tracking_object != NULL;
1706                      tracking_object = tracking_object->next)
1707                 {
1708                         float tracking_object_mat[4][4];
1709
1710                         if (tracking_object->flag & TRACKING_OBJECT_CAMERA) {
1711                                 copy_m4_m4(tracking_object_mat, camera_mat);
1712                         }
1713                         else {
1714                                 const int framenr = BKE_movieclip_remap_scene_to_clip_frame(clip, DEG_get_ctime(draw_ctx->depsgraph));
1715                                 float object_mat[4][4];
1716                                 BKE_tracking_camera_get_reconstructed_interpolate(tracking, tracking_object, framenr, object_mat);
1717
1718                                 invert_m4(object_mat);
1719                                 mul_m4_m4m4(tracking_object_mat, cam->drwnormalmat, object_mat);
1720                         }
1721
1722                         ListBase *tracksbase = BKE_tracking_object_get_tracks(tracking, tracking_object);
1723                         for (MovieTrackingTrack *track = tracksbase->first; track; track = track->next) {
1724
1725                                 if ((track->flag & TRACK_HAS_BUNDLE) == 0) {
1726                                         continue;
1727                                 }
1728
1729                                 bool is_selected = TRACK_SELECTED(track);
1730
1731                                 float bundle_mat[4][4];
1732                                 copy_m4_m4(bundle_mat, tracking_object_mat);
1733                                 translate_m4(bundle_mat, track->bundle_pos[0], track->bundle_pos[1], track->bundle_pos[2]);
1734
1735                                 float *bundle_color;
1736                                 if (track->flag & TRACK_CUSTOMCOLOR) {
1737                                         bundle_color = track->color;
1738                                 }
1739                                 else if (is_solid_bundle) {
1740                                         bundle_color = bundle_color_solid;
1741                                 }
1742                                 else if (is_selected) {
1743                                         bundle_color = color;
1744                                 }
1745                                 else {
1746                                         bundle_color = bundle_color_unselected;
1747                                 }
1748
1749                                 if (is_select) {
1750                                         DRW_select_load_id(camera_object->select_color | (track_index << 16));
1751                                         track_index++;
1752                                 }
1753
1754                                 if (is_solid_bundle) {
1755
1756                                         if (is_selected) {
1757                                                 DRW_shgroup_empty_ex(sgl,
1758                                                                      bundle_mat,
1759                                                                      &v3d->bundle_size,
1760                                                                      v3d->bundle_drawtype,
1761                                                                      color);
1762                                         }
1763
1764                                         float bundle_color_v4[4] = {
1765                                             bundle_color[0],
1766                                             bundle_color[1],
1767                                             bundle_color[2],
1768                                             1.0f,
1769                                         };
1770
1771                                         mul_m4_m4m4(bundle_mat, bundle_mat, bundle_scale_mat);
1772                                         DRW_shgroup_call_dynamic_add(sgl->sphere_solid,
1773                                                                      bundle_mat,
1774                                                                      bundle_color_v4);
1775                                 }
1776                                 else {
1777                                         DRW_shgroup_empty_ex(sgl,
1778                                                              bundle_mat,
1779                                                              &v3d->bundle_size,
1780                                                              v3d->bundle_drawtype,
1781                                                              bundle_color);
1782                                 }
1783
1784                                 if ((v3d->flag2 & V3D_SHOW_BUNDLENAME) && !is_select) {
1785                                         struct DRWTextStore *dt = DRW_text_cache_ensure();
1786
1787                                         DRW_text_cache_add(dt,
1788                                                            bundle_mat[3],
1789                                                            track->name,
1790                                                            strlen(track->name),
1791                                                            10,
1792                                                            DRW_TEXT_CACHE_GLOBALSPACE | DRW_TEXT_CACHE_STRING_PTR,
1793                                                            is_selected ? text_color_selected : text_color_unselected);
1794                                 }
1795                         }
1796
1797                         if ((v3d->flag2 & V3D_SHOW_CAMERAPATH) && (tracking_object->flag & TRACKING_OBJECT_CAMERA) && !is_select) {
1798                                 MovieTrackingReconstruction *reconstruction;
1799                                 reconstruction = BKE_tracking_object_get_reconstruction(tracking, tracking_object);
1800
1801                                 if (reconstruction->camnr) {
1802                                         static float camera_path_color[4];
1803                                         UI_GetThemeColor4fv(TH_CAMERA_PATH, camera_path_color);
1804
1805                                         GPUBatch *geom = batch_camera_path_get(&sgl->camera_path, reconstruction);
1806                                         GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
1807                                         DRWShadingGroup *shading_group = DRW_shgroup_create(shader, sgl->non_meshes);
1808                                         DRW_shgroup_uniform_vec4(shading_group, "color", camera_path_color, 1);
1809                                         DRW_shgroup_call_add(shading_group, geom, camera_mat);
1810                                 }
1811                         }
1812                 }
1813         }
1814 }
1815
1816 static void DRW_shgroup_empty_ex(
1817         OBJECT_ShadingGroupList *sgl, float mat[4][4], float *draw_size, char draw_type, float *color)
1818 {
1819         switch (draw_type) {
1820                 case OB_PLAINAXES:
1821                         DRW_shgroup_call_dynamic_add(sgl->plain_axes, color, draw_size, mat);
1822                         break;
1823                 case OB_SINGLE_ARROW:
1824                         DRW_shgroup_call_dynamic_add(sgl->single_arrow, color, draw_size, mat);
1825                         DRW_shgroup_call_dynamic_add(sgl->single_arrow_line, color, draw_size, mat);
1826                         break;
1827                 case OB_CUBE:
1828                         DRW_shgroup_call_dynamic_add(sgl->cube, color, draw_size, mat);
1829                         break;
1830                 case OB_CIRCLE:
1831                         DRW_shgroup_call_dynamic_add(sgl->circle, color, draw_size, mat);
1832                         break;
1833                 case OB_EMPTY_SPHERE:
1834                         DRW_shgroup_call_dynamic_add(sgl->sphere, color, draw_size, mat);
1835                         break;
1836                 case OB_EMPTY_CONE:
1837                         DRW_shgroup_call_dynamic_add(sgl->cone, color, draw_size, mat);
1838                         break;
1839                 case OB_ARROWS:
1840                         DRW_shgroup_call_dynamic_add(sgl->empty_axes, color, draw_size, mat);
1841                         break;
1842                 case OB_EMPTY_IMAGE:
1843                         BLI_assert(!"Should never happen, use DRW_shgroup_empty instead.");
1844                         break;
1845         }
1846 }
1847
1848 static void DRW_shgroup_empty(OBJECT_ShadingGroupList *sgl, Object *ob, ViewLayer *view_layer)
1849 {
1850         float *color;
1851         DRW_object_wire_theme_get(ob, view_layer, &color);
1852
1853         switch (ob->empty_drawtype) {
1854                 case OB_PLAINAXES:
1855                 case OB_SINGLE_ARROW:
1856                 case OB_CUBE:
1857                 case OB_CIRCLE:
1858                 case OB_EMPTY_SPHERE:
1859                 case OB_EMPTY_CONE:
1860                 case OB_ARROWS:
1861                         DRW_shgroup_empty_ex(sgl, ob->obmat, &ob->empty_drawsize, ob->empty_drawtype, color);
1862                         break;
1863                 case OB_EMPTY_IMAGE:
1864                         DRW_shgroup_empty_image(sgl, ob, color);
1865                         break;
1866         }
1867 }
1868
1869 static void DRW_shgroup_forcefield(OBJECT_ShadingGroupList *sgl, Object *ob, ViewLayer *view_layer)
1870 {
1871         int theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
1872         float *color = DRW_color_background_blend_get(theme_id);
1873         PartDeflect *pd = ob->pd;
1874         Curve *cu = (ob->type == OB_CURVE) ? ob->data : NULL;
1875
1876         /* TODO Move this to depsgraph */
1877         float tmp[3];
1878         copy_v3_fl(pd->drawvec1, ob->empty_drawsize);
1879
1880         switch (pd->forcefield) {
1881                 case PFIELD_WIND:
1882                         pd->drawvec1[2] = pd->f_strength;
1883                         break;
1884                 case PFIELD_VORTEX:
1885                         if (pd->f_strength < 0.0f) {
1886                                 pd->drawvec1[1] = -pd->drawvec1[1];
1887                         }
1888                         break;
1889                 case PFIELD_GUIDE:
1890                         if (cu && (cu->flag & CU_PATH) && ob->runtime.curve_cache->path && ob->runtime.curve_cache->path->data) {
1891                                 where_on_path(ob, 0.0f, pd->drawvec1, tmp, NULL, NULL, NULL);
1892                                 where_on_path(ob, 1.0f, pd->drawvec2, tmp, NULL, NULL, NULL);
1893                         }
1894                         break;
1895         }
1896
1897         if (pd->falloff == PFIELD_FALL_TUBE) {
1898                 pd->drawvec_falloff_max[0] = pd->drawvec_falloff_max[1] = (pd->flag & PFIELD_USEMAXR) ? pd->maxrad : 1.0f;
1899                 pd->drawvec_falloff_max[2] = (pd->flag & PFIELD_USEMAX) ? pd->maxdist : 0.0f;
1900
1901                 pd->drawvec_falloff_min[0] = pd->drawvec_falloff_min[1] = (pd->flag & PFIELD_USEMINR) ? pd->minrad : 1.0f;
1902                 pd->drawvec_falloff_min[2] = (pd->flag & PFIELD_USEMIN) ? pd->mindist : 0.0f;
1903         }
1904         else if (pd->falloff == PFIELD_FALL_CONE) {
1905                 float radius, distance;
1906
1907                 radius = DEG2RADF((pd->flag & PFIELD_USEMAXR) ? pd->maxrad : 1.0f);
1908                 distance = (pd->flag & PFIELD_USEMAX) ? pd->maxdist : 0.0f;
1909                 pd->drawvec_falloff_max[0] = pd->drawvec_falloff_max[1] = distance * sinf(radius);
1910                 pd->drawvec_falloff_max[2] = distance * cosf(radius);
1911
1912                 radius = DEG2RADF((pd->flag & PFIELD_USEMINR) ? pd->minrad : 1.0f);
1913                 distance = (pd->flag & PFIELD_USEMIN) ? pd->mindist : 0.0f;
1914
1915                 pd->drawvec_falloff_min[0] = pd->drawvec_falloff_min[1] = distance * sinf(radius);
1916                 pd->drawvec_falloff_min[2] = distance * cosf(radius);
1917         }
1918         /* End of things that should go to depthgraph */
1919
1920         switch (pd->forcefield) {
1921                 case PFIELD_WIND:
1922                         DRW_shgroup_call_dynamic_add(sgl->field_wind, color, &pd->drawvec1, ob->obmat);
1923                         break;
1924                 case PFIELD_FORCE:
1925                         DRW_shgroup_call_dynamic_add(sgl->field_force, color, &pd->drawvec1, ob->obmat);
1926                         break;
1927                 case PFIELD_VORTEX:
1928                         DRW_shgroup_call_dynamic_add(sgl->field_vortex, color, &pd->drawvec1, ob->obmat);
1929                         break;
1930                 case PFIELD_GUIDE:
1931                         if (cu && (cu->flag & CU_PATH) && ob->runtime.curve_cache->path && ob->runtime.curve_cache->path->data) {
1932                                 DRW_shgroup_call_dynamic_add(sgl->field_curve_sta, color, &pd->f_strength, ob->obmat);
1933                                 DRW_shgroup_call_dynamic_add(sgl->field_curve_end, color, &pd->f_strength, ob->obmat);
1934                         }
1935                         break;
1936         }
1937
1938         if (pd->falloff == PFIELD_FALL_SPHERE) {
1939                 /* as last, guide curve alters it */
1940                 if ((pd->flag & PFIELD_USEMAX) != 0) {
1941                         DRW_shgroup_call_dynamic_add(sgl->field_curve_end, color, &pd->maxdist, ob->obmat);
1942                 }
1943
1944                 if ((pd->flag & PFIELD_USEMIN) != 0) {
1945                         DRW_shgroup_call_dynamic_add(sgl->field_curve_end, color, &pd->mindist, ob->obmat);
1946                 }
1947         }
1948         else if (pd->falloff == PFIELD_FALL_TUBE) {
1949                 if (pd->flag & (PFIELD_USEMAX | PFIELD_USEMAXR)) {
1950                         DRW_shgroup_call_dynamic_add(sgl->field_tube_limit, color, &pd->drawvec_falloff_max, ob->obmat);
1951                 }
1952
1953                 if (pd->flag & (PFIELD_USEMIN | PFIELD_USEMINR)) {
1954                         DRW_shgroup_call_dynamic_add(sgl->field_tube_limit, color, &pd->drawvec_falloff_min, ob->obmat);
1955                 }
1956         }
1957         else if (pd->falloff == PFIELD_FALL_CONE) {
1958                 if (pd->flag & (PFIELD_USEMAX | PFIELD_USEMAXR)) {
1959                         DRW_shgroup_call_dynamic_add(sgl->field_cone_limit, color, &pd->drawvec_falloff_max, ob->obmat);
1960                 }
1961
1962                 if (pd->flag & (PFIELD_USEMIN | PFIELD_USEMINR)) {
1963                         DRW_shgroup_call_dynamic_add(sgl->field_cone_limit, color, &pd->drawvec_falloff_min, ob->obmat);
1964                 }
1965         }
1966 }
1967
1968 static void DRW_shgroup_volume_extra(
1969         OBJECT_ShadingGroupList *sgl,
1970         Object *ob, ViewLayer *view_layer, Scene *scene, ModifierData *md)
1971 {
1972         SmokeModifierData *smd = (SmokeModifierData *)md;
1973         SmokeDomainSettings *sds = smd->domain;
1974         float *color;
1975         float one = 1.0f;
1976
1977         if (sds == NULL) {
1978                 return;
1979         }
1980
1981         DRW_object_wire_theme_get(ob, view_layer, &color);
1982
1983         /* Small cube showing voxel size. */
1984         float voxel_cubemat[4][4] = {{0.0f}};
1985         voxel_cubemat[0][0] = 1.0f / (float)sds->res[0];
1986         voxel_cubemat[1][1] = 1.0f / (float)sds->res[1];
1987         voxel_cubemat[2][2] = 1.0f / (float)sds->res[2];
1988         voxel_cubemat[3][0] = voxel_cubemat[3][1] = voxel_cubemat[3][2] = -1.0f;
1989         voxel_cubemat[3][3] = 1.0f;
1990         translate_m4(voxel_cubemat, 1.0f, 1.0f, 1.0f);
1991         mul_m4_m4m4(voxel_cubemat, ob->obmat, voxel_cubemat);
1992
1993         DRW_shgroup_call_dynamic_add(sgl->cube, color, &one, voxel_cubemat);
1994
1995         /* Don't show smoke before simulation starts, this could be made an option in the future. */
1996         if (!sds->draw_velocity || !sds->fluid || CFRA < sds->point_cache[0]->startframe) {
1997                 return;
1998         }
1999
2000         const bool use_needle = (sds->vector_draw_type == VECTOR_DRAW_NEEDLE);
2001         int line_count = (use_needle) ? 6 : 1;
2002         int slice_axis = -1;
2003         line_count *= sds->res[0] * sds->res[1] * sds->res[2];
2004
2005         if (sds->slice_method == MOD_SMOKE_SLICE_AXIS_ALIGNED &&
2006             sds->axis_slice_method == AXIS_SLICE_SINGLE)
2007         {
2008                 float invviewmat[4][4];
2009                 DRW_viewport_matrix_get(invviewmat, DRW_MAT_VIEWINV);
2010
2011                 const int axis = (sds->slice_axis == SLICE_AXIS_AUTO)
2012                                   ? axis_dominant_v3_single(invviewmat[2])
2013                                   : sds->slice_axis - 1;
2014                 slice_axis = axis;
2015                 line_count /= sds->res[axis];
2016         }
2017
2018         GPU_create_smoke_velocity(smd);
2019
2020         DRWShadingGroup *grp = DRW_shgroup_create(volume_velocity_shader_get(use_needle), sgl->non_meshes);
2021         DRW_shgroup_uniform_texture(grp, "velocityX", sds->tex_velocity_x);
2022         DRW_shgroup_uniform_texture(grp, "velocityY", sds->tex_velocity_y);
2023         DRW_shgroup_uniform_texture(grp, "velocityZ", sds->tex_velocity_z);
2024         DRW_shgroup_uniform_float_copy(grp, "displaySize", sds->vector_scale);
2025         DRW_shgroup_uniform_float_copy(grp, "slicePosition", sds->slice_depth);
2026         DRW_shgroup_uniform_int_copy(grp, "sliceAxis", slice_axis);
2027         DRW_shgroup_call_procedural_lines_add(grp, line_count, ob->obmat);
2028
2029         BLI_addtail(&e_data.smoke_domains, BLI_genericNodeN(smd));
2030 }
2031
2032 static void volumes_free_smoke_textures(void)
2033 {
2034         /* Free Smoke Textures after rendering */
2035         /* XXX This is a waste of processing and GPU bandwidth if nothing
2036          * is updated. But the problem is since Textures are stored in the
2037          * modifier we don't want them to take precious VRAM if the
2038          * modifier is not used for display. We should share them for
2039          * all viewport in a redraw at least. */
2040         for (LinkData *link = e_data.smoke_domains.first; link; link = link->next) {
2041                 SmokeModifierData *smd = (SmokeModifierData *)link->data;
2042                 GPU_free_smoke(smd);
2043         }
2044         BLI_freelistN(&e_data.smoke_domains);
2045 }
2046
2047 static void DRW_shgroup_gpencil(OBJECT_ShadingGroupList *sgl, Object *ob, ViewLayer *view_layer)
2048 {
2049         float *color;
2050         DRW_object_wire_theme_get(ob, view_layer, &color);
2051
2052         DRW_shgroup_call_dynamic_add(sgl->gpencil_axes, color, &ob->empty_drawsize, ob->obmat);
2053 }
2054
2055 static void DRW_shgroup_speaker(OBJECT_ShadingGroupList *sgl, Object *ob, ViewLayer *view_layer)
2056 {
2057         float *color;
2058         static float one = 1.0f;
2059         DRW_object_wire_theme_get(ob, view_layer, &color);
2060
2061         DRW_shgroup_call_dynamic_add(sgl->speaker, color, &one, ob->obmat);
2062 }
2063
2064 typedef struct OBJECT_LightProbeEngineData {
2065         DrawData dd;
2066
2067         float increment_x[3];
2068         float increment_y[3];
2069         float increment_z[3];
2070         float corner[3];
2071 } OBJECT_LightProbeEngineData;
2072
2073 static void DRW_shgroup_lightprobe(OBJECT_StorageList *stl, OBJECT_PassList *psl, Object *ob, ViewLayer *view_layer)
2074 {
2075         float *color;
2076         static float one = 1.0f;
2077         LightProbe *prb = (LightProbe *)ob->data;
2078         bool do_outlines = ((ob->base_flag & BASE_SELECTED) != 0);
2079         int theme_id = DRW_object_wire_theme_get(ob, view_layer, &color);
2080
2081         OBJECT_ShadingGroupList *sgl = (ob->dtx & OB_DRAWXRAY) ? &stl->g_data->sgl_ghost : &stl->g_data->sgl;
2082
2083         OBJECT_LightProbeEngineData *prb_data =
2084                 (OBJECT_LightProbeEngineData *)DRW_drawdata_ensure(
2085                         &ob->id,
2086                         &draw_engine_object_type,
2087                         sizeof(OBJECT_LightProbeEngineData),
2088                         NULL,
2089                         NULL);
2090
2091         if ((DRW_state_is_select() || do_outlines) && ((prb->flag & LIGHTPROBE_FLAG_SHOW_DATA) != 0)) {
2092                 int *call_id = shgroup_theme_id_to_probe_outline_counter(stl, theme_id);
2093
2094                 if (prb->type == LIGHTPROBE_TYPE_GRID) {
2095                         /* Update transforms */
2096                         float cell_dim[3], half_cell_dim[3];
2097                         cell_dim[0] = 2.0f / (float)(prb->grid_resolution_x);
2098                         cell_dim[1] = 2.0f / (float)(prb->grid_resolution_y);
2099                         cell_dim[2] = 2.0f / (float)(prb->grid_resolution_z);
2100
2101                         mul_v3_v3fl(half_cell_dim, cell_dim, 0.5f);
2102
2103                         /* First cell. */
2104                         copy_v3_fl(prb_data->corner, -1.0f);
2105                         add_v3_v3(prb_data->corner, half_cell_dim);
2106                         mul_m4_v3(ob->obmat, prb_data->corner);
2107
2108                         /* Opposite neighbor cell. */
2109                         copy_v3_fl3(prb_data->increment_x, cell_dim[0], 0.0f, 0.0f);
2110                         add_v3_v3(prb_data->increment_x, half_cell_dim);
2111                         add_v3_fl(prb_data->increment_x, -1.0f);
2112                         mul_m4_v3(ob->obmat, prb_data->increment_x);
2113                         sub_v3_v3(prb_data->increment_x, prb_data->corner);
2114
2115                         copy_v3_fl3(prb_data->increment_y, 0.0f, cell_dim[1], 0.0f);
2116                         add_v3_v3(prb_data->increment_y, half_cell_dim);
2117                         add_v3_fl(prb_data->increment_y, -1.0f);
2118                         mul_m4_v3(ob->obmat, prb_data->increment_y);
2119                         sub_v3_v3(prb_data->increment_y, prb_data->corner);
2120
2121                         copy_v3_fl3(prb_data->increment_z, 0.0f, 0.0f, cell_dim[2]);
2122                         add_v3_v3(prb_data->increment_z, half_cell_dim);
2123                         add_v3_fl(prb_data->increment_z, -1.0f);
2124                         mul_m4_v3(ob->obmat, prb_data->increment_z);
2125                         sub_v3_v3(prb_data->increment_z, prb_data->corner);
2126
2127                         uint cell_count = prb->grid_resolution_x * prb->grid_resolution_y * prb->grid_resolution_z;
2128                         DRWShadingGroup *grp = DRW_shgroup_create(e_data.lightprobe_grid_sh, psl->lightprobes);
2129                         DRW_shgroup_uniform_int_copy(grp, "call_id", *call_id);
2130                         DRW_shgroup_uniform_int(grp, "baseId", call_id, 1); /* that's correct */
2131                         DRW_shgroup_uniform_vec3(grp, "corner", prb_data->corner, 1);
2132                         DRW_shgroup_uniform_vec3(grp, "increment_x", prb_data->increment_x, 1);
2133                         DRW_shgroup_uniform_vec3(grp, "increment_y", prb_data->increment_y, 1);
2134                         DRW_shgroup_uniform_vec3(grp, "increment_z", prb_data->increment_z, 1);
2135                         DRW_shgroup_uniform_ivec3(grp, "grid_resolution", &prb->grid_resolution_x, 1);
2136                         DRW_shgroup_call_procedural_points_add(grp, cell_count, NULL);
2137                 }
2138                 else if (prb->type == LIGHTPROBE_TYPE_CUBE) {
2139                         float draw_size = 1.0f;
2140                         float probe_cube_mat[4][4];
2141                         // prb_data->draw_size = prb->data_draw_size * 0.1f;
2142                         // unit_m4(prb_data->probe_cube_mat);
2143                         // copy_v3_v3(prb_data->probe_cube_mat[3], ob->obmat[3]);
2144
2145                         DRWShadingGroup *grp = shgroup_theme_id_to_probe_cube_outline_shgrp(stl, theme_id);
2146                         /* TODO remove or change the drawing of the cube probes. Theses line draws nothing on purpose
2147                          * to keep the call ids correct. */
2148                         zero_m4(probe_cube_mat);
2149                         DRW_shgroup_call_dynamic_add(grp, call_id, &draw_size, probe_cube_mat);
2150                 }
2151                 else {
2152                         float draw_size = 1.0f;
2153                         DRWShadingGroup *grp = shgroup_theme_id_to_probe_planar_outline_shgrp(stl, theme_id);
2154                         DRW_shgroup_call_dynamic_add(grp, call_id, &draw_size, ob->obmat);
2155                 }
2156
2157                 *call_id += 1;
2158         }
2159
2160         switch (prb->type) {
2161                 case LIGHTPROBE_TYPE_PLANAR:
2162                         DRW_shgroup_call_dynamic_add(sgl->probe_planar, ob->obmat[3], color);
2163                         break;
2164                 case LIGHTPROBE_TYPE_GRID:
2165                         DRW_shgroup_call_dynamic_add(sgl->probe_grid, ob->obmat[3], color);
2166                         break;
2167                 case LIGHTPROBE_TYPE_CUBE:
2168                 default:
2169                         DRW_shgroup_call_dynamic_add(sgl->probe_cube, ob->obmat[3], color);
2170                         break;
2171         }
2172
2173
2174
2175         if (prb->type == LIGHTPROBE_TYPE_PLANAR) {
2176                 float mat[4][4];
2177                 copy_m4_m4(mat, ob->obmat);
2178                 normalize_m4(mat);
2179
2180                 DRW_shgroup_call_dynamic_add(sgl->single_arrow, color, &ob->empty_drawsize, mat);
2181                 DRW_shgroup_call_dynamic_add(sgl->single_arrow_line, color, &ob->empty_drawsize, mat);
2182
2183                 copy_m4_m4(mat, ob->obmat);
2184                 zero_v3(mat[2]);
2185
2186                 DRW_shgroup_call_dynamic_add(sgl->cube, color, &one, mat);
2187         }
2188
2189         if ((prb->flag & LIGHTPROBE_FLAG_SHOW_INFLUENCE) != 0) {
2190
2191                 prb->distfalloff = (1.0f - prb->falloff) * prb->distinf;
2192                 prb->distgridinf = prb->distinf;
2193
2194                 if (prb->type == LIGHTPROBE_TYPE_GRID) {
2195                         prb->distfalloff += 1.0f;
2196                         prb->distgridinf += 1.0f;
2197                 }
2198
2199                 if (prb->type == LIGHTPROBE_TYPE_GRID ||
2200                     prb->attenuation_type == LIGHTPROBE_SHAPE_BOX)
2201                 {
2202                         DRW_shgroup_call_dynamic_add(sgl->cube, color, &prb->distgridinf, ob->obmat);
2203                         DRW_shgroup_call_dynamic_add(sgl->cube, color, &prb->distfalloff, ob->obmat);
2204                 }
2205                 else if (prb->type == LIGHTPROBE_TYPE_PLANAR) {
2206                         float rangemat[4][4];
2207                         copy_m4_m4(rangemat, ob->obmat);
2208                         normalize_v3(rangemat[2]);
2209                         mul_v3_fl(rangemat[2], prb->distinf);
2210
2211                         DRW_shgroup_call_dynamic_add(sgl->cube, color, &one, rangemat);
2212
2213                         copy_m4_m4(rangemat, ob->obmat);
2214                         normalize_v3(rangemat[2]);
2215                         mul_v3_fl(rangemat[2], prb->distfalloff);
2216
2217                         DRW_shgroup_call_dynamic_add(sgl->cube, color, &one, rangemat);
2218                 }
2219                 else {
2220                         DRW_shgroup_call_dynamic_add(sgl->sphere, color, &prb->distgridinf, ob->obmat);
2221                         DRW_shgroup_call_dynamic_add(sgl->sphere, color, &prb->distfalloff, ob->obmat);
2222                 }
2223         }
2224
2225         if ((prb->flag & LIGHTPROBE_FLAG_SHOW_PARALLAX) != 0) {
2226                 if (prb->type != LIGHTPROBE_TYPE_PLANAR) {
2227                         float (*obmat)[4], *dist;
2228
2229                         if ((prb->flag & LIGHTPROBE_FLAG_CUSTOM_PARALLAX) != 0) {
2230                                 dist = &prb->distpar;
2231                                 /* TODO object parallax */
2232                                 obmat = ob->obmat;
2233                         }
2234                         else {
2235                                 dist = &prb->distinf;
2236                                 obmat = ob->obmat;
2237                         }
2238
2239                         if (prb->parallax_type == LIGHTPROBE_SHAPE_BOX) {
2240                                 DRW_shgroup_call_dynamic_add(sgl->cube, color, dist, obmat);
2241                         }
2242                         else {
2243                                 DRW_shgroup_call_dynamic_add(sgl->sphere, color, dist, obmat);
2244                         }
2245                 }
2246         }
2247
2248         if ((prb->flag & LIGHTPROBE_FLAG_SHOW_CLIP_DIST) != 0) {
2249                 if (prb->type != LIGHTPROBE_TYPE_PLANAR) {
2250                         static const float cubefacemat[6][4][4] = {
2251                                 {{0.0, 0.0, -1.0, 0.0}, {0.0, -1.0, 0.0, 0.0}, {-1.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 1.0}},
2252                                 {{0.0, 0.0, 1.0, 0.0}, {0.0, -1.0, 0.0, 0.0}, {1.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 1.0}},
2253                                 {{1.0, 0.0, 0.0, 0.0}, {0.0, 0.0, -1.0, 0.0}, {0.0, 1.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 1.0}},
2254                                 {{1.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}, {0.0, -1.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 1.0}},
2255                                 {{1.0, 0.0, 0.0, 0.0}, {0.0, -1.0, 0.0, 0.0}, {0.0, 0.0, -1.0, 0.0}, {0.0, 0.0, 0.0, 1.0}},
2256                                 {{-1.0, 0.0, 0.0, 0.0}, {0.0, -1.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}, {0.0, 0.0, 0.0, 1.0}},
2257                         };
2258
2259                         for (int i = 0; i < 6; ++i) {
2260                                 float clipmat[4][4];
2261                                 normalize_m4_m4(clipmat, ob->obmat);
2262                                 mul_m4_m4m4(clipmat, clipmat, cubefacemat[i]);
2263
2264                                 DRW_shgroup_call_dynamic_add(sgl->lamp_buflimit, color, &prb->clipsta, &prb->clipend, clipmat);
2265                                 DRW_shgroup_call_dynamic_add(sgl->lamp_buflimit_points, color, &prb->clipsta, &prb->clipend, clipmat);
2266                         }
2267                 }
2268         }
2269
2270         /* Line and point going to the ground */
2271         if (prb->type == LIGHTPROBE_TYPE_CUBE) {
2272                 DRW_shgroup_call_dynamic_add(sgl->lamp_groundline, ob->obmat[3]);
2273                 DRW_shgroup_call_dynamic_add(sgl->lamp_groundpoint, ob->obmat[3]);
2274         }
2275 }
2276
2277 static void DRW_shgroup_relationship_lines(
2278         OBJECT_ShadingGroupList *sgl,
2279         Depsgraph *depsgraph,
2280         Scene *scene,
2281         Object *ob)
2282 {
2283         if (ob->parent && DRW_check_object_visible_within_active_context(ob->parent)) {
2284                 DRW_shgroup_call_dynamic_add(sgl->relationship_lines, ob->parent->obmat[3]);
2285                 DRW_shgroup_call_dynamic_add(sgl->relationship_lines, ob->obmat[3]);
2286         }
2287
2288         if (ob->rigidbody_constraint) {
2289                 Object *rbc_ob1 = ob->rigidbody_constraint->ob1;
2290                 Object *rbc_ob2 = ob->rigidbody_constraint->ob2;
2291                 if (rbc_ob1 && DRW_check_object_visible_within_active_context(rbc_ob1)) {
2292                         DRW_shgroup_call_dynamic_add(sgl->relationship_lines, rbc_ob1->obmat[3]);
2293                         DRW_shgroup_call_dynamic_add(sgl->relationship_lines, ob->obmat[3]);
2294                 }
2295                 if (rbc_ob2 && DRW_check_object_visible_within_active_context(rbc_ob2)) {
2296                         DRW_shgroup_call_dynamic_add(sgl->relationship_lines, rbc_ob2->obmat[3]);
2297                         DRW_shgroup_call_dynamic_add(sgl->relationship_lines, ob->obmat[3]);
2298                 }
2299         }
2300
2301         /* Drawing the constraint lines */
2302         if (!BLI_listbase_is_empty(&ob->constraints)) {
2303                 bConstraint *curcon;
2304                 bConstraintOb *cob;
2305                 ListBase *list = &ob->constraints;
2306
2307                 cob = BKE_constraints_make_evalob(depsgraph, scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
2308
2309                 for (curcon = list->first; curcon; curcon = curcon->next) {
2310                         if (ELEM(curcon->type, CONSTRAINT_TYPE_FOLLOWTRACK, CONSTRAINT_TYPE_OBJECTSOLVER)) {
2311                                 /* special case for object solver and follow track constraints because they don't fill
2312                                  * constraint targets properly (design limitation -- scene is needed for their target
2313                                  * but it can't be accessed from get_targets callback) */
2314
2315                                 Object *camob = NULL;
2316
2317                                 if (curcon->type == CONSTRAINT_TYPE_FOLLOWTRACK) {
2318                                         bFollowTrackConstraint *data = (bFollowTrackConstraint *)curcon->data;
2319
2320                                         camob = data->camera ? data->camera : scene->camera;
2321                                 }
2322                                 else if (curcon->type == CONSTRAINT_TYPE_OBJECTSOLVER) {
2323                                         bObjectSolverConstraint *data = (bObjectSolverConstraint *)curcon->data;
2324
2325                                         camob = data->camera ? data->camera : scene->camera;
2326                                 }
2327
2328                                 if (camob) {
2329                                         DRW_shgroup_call_dynamic_add(sgl->constraint_lines, camob->obmat[3]);
2330                                         DRW_shgroup_call_dynamic_add(sgl->constraint_lines, ob->obmat[3]);
2331                                 }
2332                         }
2333                         else {
2334                                 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(curcon);
2335
2336                                 if ((cti && cti->get_constraint_targets) && (curcon->flag & CONSTRAINT_EXPAND)) {
2337                                         ListBase targets = {NULL, NULL};
2338                                         bConstraintTarget *ct;
2339
2340                                         cti->get_constraint_targets(curcon, &targets);
2341
2342                                         for (ct = targets.first; ct; ct = ct->next) {
2343                                                 /* calculate target's matrix */
2344                                                 if (cti->get_target_matrix) {
2345                                                         cti->get_target_matrix(depsgraph,
2346                                                                                curcon,
2347                                                                                cob,
2348                                                                                ct,
2349                                                                                DEG_get_ctime(depsgraph));
2350                                                 }
2351                                                 else {
2352                                                         unit_m4(ct->matrix);
2353                                                 }
2354
2355                                                 DRW_shgroup_call_dynamic_add(sgl->constraint_lines, ct->matrix[3]);
2356                                                 DRW_shgroup_call_dynamic_add(sgl->constraint_lines, ob->obmat[3]);
2357                                         }
2358
2359                                         if (cti->flush_constraint_targets) {
2360                                                 cti->flush_constraint_targets(curcon, &targets, 1);
2361                                         }
2362                                 }
2363                         }
2364                 }
2365
2366                 BKE_constraints_clear_evalob(cob);
2367         }
2368 }
2369
2370 static void DRW_shgroup_object_center(OBJECT_StorageList *stl, Object *ob, ViewLayer *view_layer, View3D *v3d)
2371 {
2372         if (v3d->overlay.flag & V3D_OVERLAY_HIDE_OBJECT_ORIGINS) {
2373                 return;
2374         }
2375
2376         const bool is_library = ob->id.us > 1 || ID_IS_LINKED(ob);
2377         DRWShadingGroup *shgroup;
2378
2379         if (ob == OBACT(view_layer)) {
2380                 shgroup = stl->g_data->center_active;
2381         }
2382         else if (ob->base_flag & BASE_SELECTED) {
2383                 if (is_library) {
2384                         shgroup = stl->g_data->center_selected_lib;
2385                 }
2386                 else {
2387                         shgroup = stl->g_data->center_selected;
2388                 }
2389         }
2390         else if (v3d->flag & V3D_DRAW_CENTERS) {
2391                 if (is_library) {
2392                         shgroup = stl->g_data->center_deselected_lib;
2393                 }
2394                 else {
2395                         shgroup = stl->g_data->center_deselected;
2396                 }
2397         }
2398         else {
2399                 return;
2400         }
2401
2402         DRW_shgroup_call_dynamic_add(shgroup, ob->obmat[3]);
2403 }
2404
2405 static void DRW_shgroup_texture_space(OBJECT_ShadingGroupList *sgl, Object *ob, int theme_id)
2406 {
2407         if (ob->data == NULL) {
2408                 return;
2409         }
2410
2411         ID *ob_data = ob->data;
2412         float *texcoloc = NULL;
2413         float *texcosize = NULL;
2414         if (ob->data != NULL) {
2415                 switch (GS(ob_data->name)) {
2416                         case ID_ME:
2417                                 BKE_mesh_texspace_get_reference((Mesh *)ob_data, NULL, &texcoloc, NULL, &texcosize);
2418                                 break;
2419                         case ID_CU:
2420                         {
2421                                 Curve *cu = (Curve *)ob_data;
2422                                 if (cu->bb == NULL || (cu->bb->flag & BOUNDBOX_DIRTY)) {
2423                                         BKE_curve_texspace_calc(cu);
2424                                 }
2425                                 texcoloc = cu->loc;
2426                                 texcosize = cu->size;
2427                                 break;
2428                         }
2429                         case ID_MB:
2430                         {
2431                                 MetaBall *mb = (MetaBall *)ob_data;
2432                                 texcoloc = mb->loc;
2433                                 texcosize = mb->size;
2434                                 break;
2435                         }
2436                         default:
2437                                 BLI_assert(0);
2438                 }
2439         }
2440
2441         float tmp[4][4] = {{0.0f}}, one = 1.0f;
2442         tmp[0][0] = texcosize[0];
2443         tmp[1][1] = texcosize[1];
2444         tmp[2][2] = texcosize[2];
2445         tmp[3][0] = texcoloc[0];
2446         tmp[3][1] = texcoloc[1];
2447         tmp[3][2] = texcoloc[2];
2448         tmp[3][3] = 1.0f;
2449
2450         mul_m4_m4m4(tmp, ob->obmat, tmp);
2451
2452         float color[4];
2453         UI_GetThemeColor4fv(theme_id, color);
2454
2455         DRW_shgroup_call_dynamic_add(sgl->texspace, color, &one, tmp);
2456 }
2457
2458 static void DRW_shgroup_bounds(OBJECT_ShadingGroupList *sgl, Object *ob, int theme_id)
2459 {
2460         float color[4], center[3], size[3], tmp[4][4], final_mat[4][4], one = 1.0f;
2461         BoundBox  bb_local;
2462
2463         if (ob->type == OB_MBALL && !BKE_mball_is_basis(ob)) {
2464                 return;
2465         }
2466
2467         BoundBox *bb = BKE_object_boundbox_get(ob);
2468
2469         if (!ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT,
2470                             OB_MBALL, OB_ARMATURE, OB_LATTICE))
2471         {
2472                 const float min[3] = {-1.0f, -1.0f, -1.0f}, max[3] = {1.0f, 1.0f, 1.0f};
2473                 bb = &bb_local;
2474                 BKE_boundbox_init_from_minmax(bb, min, max);
2475         }
2476
2477         UI_GetThemeColor4fv(theme_id, color);
2478         BKE_boundbox_calc_center_aabb(bb, center);
2479         BKE_boundbox_calc_size_aabb(bb, size);
2480
2481         switch (ob->boundtype) {
2482                 case OB_BOUND_BOX:
2483                         size_to_mat4(tmp, size);
2484                         copy_v3_v3(tmp[3], center);
2485                         mul_m4_m4m4(tmp, ob->obmat, tmp);
2486                         DRW_shgroup_call_dynamic_add(sgl->cube, color, &one, tmp);
2487                         break;
2488                 case OB_BOUND_SPHERE:
2489                         size[0] = max_fff(size[0], size[1], size[2]);
2490                         size[1] = size[2] = size[0];
2491                         size_to_mat4(tmp, size);
2492                         copy_v3_v3(tmp[3], center);
2493                         mul_m4_m4m4(tmp, ob->obmat, tmp);
2494                         DRW_shgroup_call_dynamic_add(sgl->sphere, color, &one, tmp);
2495                         break;
2496                 case OB_BOUND_CYLINDER:
2497                         size[0] = max_ff(size[0], size[1]);
2498                         size[1] = size[0];
2499                         size_to_mat4(tmp, size);
2500                         copy_v3_v3(tmp[3], center);
2501                         mul_m4_m4m4(tmp, ob->obmat, tmp);
2502                         DRW_shgroup_call_dynamic_add(sgl->cylinder, color, &one, tmp);
2503                         break;
2504                 case OB_BOUND_CONE:
2505                         size[0] = max_ff(size[0], size[1]);
2506                         size[1] = size[0];
2507                         size_to_mat4(tmp, size);
2508                         copy_v3_v3(tmp[3], center);
2509                         /* Cone batch has base at 0 and is pointing towards +Y. */
2510                         swap_v3_v3(tmp[1], tmp[2]);
2511                         tmp[3][2] -= size[2];
2512                         mul_m4_m4m4(tmp, ob->obmat, tmp);
2513                         DRW_shgroup_call_dynamic_add(sgl->cone, color, &one, tmp);
2514                         break;
2515                 case OB_BOUND_CAPSULE:
2516                         size[0] = max_ff(size[0], size[1]);
2517                         size[1] = size[0];
2518                         scale_m4_fl(tmp, size[0]);
2519                         copy_v2_v2(tmp[3], center);
2520                         tmp[3][2] = center[2] + max_ff(0.0f, size[2] - size[0]);
2521                         mul_m4_m4m4(final_mat, ob->obmat, tmp);
2522                         DRW_shgroup_call_dynamic_add(sgl->capsule_cap, color, &one, final_mat);
2523                         negate_v3(tmp[2]);
2524                         tmp[3][2] = center[2] - max_ff(0.0f, size[2] - size[0]);
2525                         mul_m4_m4m4(final_mat, ob->obmat, tmp);
2526                         DRW_shgroup_call_dynamic_add(sgl->capsule_cap, color, &one, final_mat);
2527                         tmp[2][2] = max_ff(0.0f, size[2] * 2.0f - size[0] * 2.0f);
2528                         mul_m4_m4m4(final_mat, ob->obmat, tmp);
2529                         DRW_shgroup_call_dynamic_add(sgl->capsule_body, color, &one, final_mat);
2530                         break;
2531         }
2532 }
2533
2534 static void OBJECT_cache_populate_particles(Object *ob,
2535                                             OBJECT_PassList *psl)
2536 {
2537         for (ParticleSystem *psys = ob->particlesystem.first; psys; psys = psys->next) {
2538                 if (!psys_check_enabled(ob, psys, false)) {
2539                         continue;
2540                 }
2541                 if (!DRW_check_psys_visible_within_active_context(ob, psys)) {
2542                         continue;
2543                 }
2544
2545                 ParticleSettings *part = psys->part;
2546                 int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
2547
2548                 static float mat[4][4];
2549                 unit_m4(mat);
2550
2551                 if (draw_as != PART_DRAW_PATH) {
2552                         struct GPUBatch *geom = DRW_cache_particles_get_dots(ob, psys);
2553                         DRWShadingGroup *shgrp = NULL;
2554                         static int screen_space[2] = {0, 1};
2555                         static float def_prim_col[3] = {0.5f, 0.5f, 0.5f};
2556                         static float def_sec_col[3] = {1.0f, 1.0f, 1.0f};
2557
2558                         /* Dummy particle format for instancing to work. */
2559                         DRW_shgroup_instance_format(e_data.particle_format, {{"dummy", DRW_ATTRIB_FLOAT, 1}});
2560
2561                         Material *ma = give_current_material(ob, part->omat);
2562
2563                         switch (draw_as) {
2564                                 case PART_DRAW_DOT:
2565                                         shgrp = DRW_shgroup_create(e_data.part_dot_sh, psl->particle);
2566                                         DRW_shgroup_uniform_vec3(shgrp, "color", ma ? &ma->r : def_prim_col, 1);
2567                                         DRW_shgroup_uniform_vec3(shgrp, "outlineColor", ma ? &ma->specr : def_sec_col, 1);
2568                                         DRW_shgroup_uniform_float(shgrp, "pixel_size", DRW_viewport_pixelsize_get(), 1);
2569                                         DRW_shgroup_uniform_float(shgrp, "size", &part->draw_size, 1);
2570                                         DRW_shgroup_uniform_texture(shgrp, "ramp", globals_ramp);
2571                                         DRW_shgroup_call_add(shgrp, geom, mat);
2572                                         break;
2573                                 case PART_DRAW_CROSS:
2574                                         shgrp = DRW_shgroup_instance_create(
2575                                                 e_data.part_prim_sh, psl->particle, DRW_cache_particles_get_prim(PART_DRAW_CROSS),
2576                                                 e_data.particle_format);
2577                                         DRW_shgroup_uniform_texture(shgrp, "ramp", globals_ramp);
2578                                         DRW_shgroup_uniform_vec3(shgrp, "color", ma ? &ma->r : def_prim_col, 1);
2579                                         DRW_shgroup_uniform_int(shgrp, "screen_space", &screen_space[0], 1);
2580                                         break;
2581                                 case PART_DRAW_CIRC:
2582                                         shgrp = DRW_shgroup_instance_create(
2583                                                 e_data.part_prim_sh, psl->particle, DRW_cache_particles_get_prim(PART_DRAW_CIRC),
2584                                                 e_data.particle_format);
2585                                         DRW_shgroup_uniform_texture(shgrp, "ramp", globals_ramp);
2586                                         DRW_shgroup_uniform_vec3(shgrp, "color", ma ? &ma->r : def_prim_col, 1);
2587                                         DRW_shgroup_uniform_int(shgrp, "screen_space", &screen_space[1], 1);
2588                                         break;
2589                                 case PART_DRAW_AXIS:
2590                                         shgrp = DRW_shgroup_instance_create(
2591                                                 e_data.part_axis_sh, psl->particle, DRW_cache_particles_get_prim(PART_DRAW_AXIS),
2592                                                 e_data.particle_format);
2593                                         DRW_shgroup_uniform_int(shgrp, "screen_space", &screen_space[0], 1);
2594                                         break;
2595                                 default:
2596                                         break;
2597                         }
2598
2599                         if (shgrp) {
2600                                 if (draw_as != PART_DRAW_DOT) {
2601                                         DRW_shgroup_uniform_float(shgrp, "draw_size", &part->draw_size, 1);
2602                                         DRW_shgroup_instance_batch(shgrp, geom);
2603                                 }
2604                         }
2605                 }
2606         }
2607 }
2608
2609 static void OBJECT_cache_populate(void *vedata, Object *ob)
2610 {
2611         OBJECT_PassList *psl = ((OBJECT_Data *)vedata)->psl;
2612         OBJECT_StorageList *stl = ((OBJECT_Data *)vedata)->stl;
2613         OBJECT_ShadingGroupList *sgl = (ob->dtx & OB_DRAWXRAY) ? &stl->g_data->sgl_ghost : &stl->g_data->sgl;
2614         const DRWContextState *draw_ctx = DRW_context_state_get();
2615         ViewLayer *view_layer = draw_ctx->view_layer;
2616         Scene *scene = draw_ctx->scene;
2617         View3D *v3d = draw_ctx->v3d;
2618         ModifierData *md = NULL;
2619         int theme_id = TH_UNDEFINED;
2620
2621         /* Handle particles first in case the emitter itself shouldn't be rendered. */
2622         if (ob->type == OB_MESH) {
2623                 OBJECT_cache_populate_particles(ob, psl);
2624         }
2625
2626         if (DRW_check_object_visible_within_active_context(ob) == false) {
2627                 return;
2628         }
2629
2630         const bool do_outlines = (draw_ctx->v3d->flag & V3D_SELECT_OUTLINE) && ((ob->base_flag & BASE_SELECTED) != 0) &&
2631                                  (DRW_object_is_renderable(ob) || (ob->dt == OB_WIRE));
2632         const bool show_relations = ((draw_ctx->v3d->flag & V3D_HIDE_HELPLINES) == 0);
2633         const bool hide_object_extra = (v3d->overlay.flag & V3D_OVERLAY_HIDE_OBJECT_XTRAS) != 0;
2634
2635         if (do_outlines) {
2636                 if (!BKE_object_is_in_editmode(ob) && !((ob == draw_ctx->obact) && (draw_ctx->object_mode & OB_MODE_ALL_PAINT))) {
2637                         struct GPUBatch *geom;
2638                         const bool xray_enabled = ((v3d->shading.flag & V3D_SHADING_XRAY) != 0) &&
2639                                                    (v3d->shading.type < OB_MATERIAL);
2640                         if (xray_enabled) {
2641                                 geom = DRW_cache_object_edge_detection_get(ob, NULL);
2642                         }
2643                         else {
2644                                 geom = DRW_cache_object_surface_get(ob);
2645                         }
2646                         if (geom) {
2647                                 theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
2648                                 DRWShadingGroup *shgroup = shgroup_theme_id_to_outline_or(stl, theme_id, NULL);
2649                                 if (shgroup != NULL) {
2650                                         DRW_shgroup_call_object_add(shgroup, geom, ob);
2651                                 }
2652                         }
2653                 }
2654         }
2655
2656         switch (ob->type) {
2657                 case OB_MESH:
2658                 {
2659                         if (hide_object_extra) {
2660                                 break;
2661                         }
2662                         if (ob != draw_ctx->object_edit) {
2663                                 Mesh *me = ob->data;
2664                                 if (me->totedge == 0) {
2665                                         struct GPUBatch *geom = DRW_cache_mesh_verts_get(ob);
2666                                         if (geom) {
2667                                                 if (theme_id == TH_UNDEFINED) {
2668                                                         theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
2669                                                 }
2670
2671                                                 DRWShadingGroup *shgroup = shgroup_theme_id_to_point_or(sgl, theme_id, sgl->points);
2672                                                 DRW_shgroup_call_object_add(shgroup, geom, ob);
2673                                         }
2674                                 }
2675                                 else {
2676                                         struct GPUBatch *geom = DRW_cache_mesh_loose_edges_get(ob);
2677                                         if (geom) {
2678                                                 if (theme_id == TH_UNDEFINED) {
2679                                                         theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
2680                                                 }
2681
2682                                                 DRWShadingGroup *shgroup = shgroup_theme_id_to_wire_or(sgl, theme_id, sgl->wire);
2683                                                 DRW_shgroup_call_object_add(shgroup, geom, ob);
2684                                         }
2685                                 }
2686                         }
2687                         break;
2688                 }
2689                 case OB_SURF:
2690                         break;
2691                 case OB_LATTICE:
2692                 {
2693                         if (ob != draw_ctx->object_edit) {
2694                                 if (hide_object_extra) {
2695                                         break;
2696                                 }
2697                                 struct GPUBatch *geom = DRW_cache_lattice_wire_get(ob, false);
2698                                 if (theme_id == TH_UNDEFINED) {
2699                                         theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
2700                                 }
2701
2702                                 DRWShadingGroup *shgroup = shgroup_theme_id_to_wire_or(sgl, theme_id, sgl->wire);
2703                                 DRW_shgroup_call_object_add(shgroup, geom, ob);
2704                         }
2705                         break;
2706                 }
2707                 case OB_CURVE:
2708                 {
2709                         if (ob != draw_ctx->object_edit) {
2710                                 if (hide_object_extra) {
2711                                         break;
2712                                 }
2713                                 struct GPUBatch *geom = DRW_cache_curve_edge_wire_get(ob);
2714                                 if (theme_id == TH_UNDEFINED) {
2715                                         theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
2716                                 }
2717                                 DRWShadingGroup *shgroup = shgroup_theme_id_to_wire_or(sgl, theme_id, sgl->wire);
2718                                 DRW_shgroup_call_object_add(shgroup, geom, ob);
2719                         }
2720                         break;
2721                 }
2722                 case OB_MBALL:
2723                 {
2724                         if (ob != draw_ctx->object_edit) {
2725                                 DRW_shgroup_mball_handles(sgl, ob, view_layer);
2726                         }
2727                         break;
2728                 }
2729                 case OB_LAMP:
2730                         if (hide_object_extra) {
2731                                 break;
2732                         }
2733                         DRW_shgroup_lamp(sgl, ob, view_layer);
2734                         break;
2735                 case OB_CAMERA:
2736                         if (hide_object_extra) {
2737                                 break;
2738                         }
2739                          DRW_shgroup_camera(sgl, ob, view_layer);
2740                         break;
2741                 case OB_EMPTY:
2742                         if (hide_object_extra) {
2743                                 break;
2744                         }
2745                         DRW_shgroup_empty(sgl, ob, view_layer);
2746                         break;
2747                 case OB_GPENCIL:
2748                         if (hide_object_extra) {
2749                                 break;
2750                         }
2751                         DRW_shgroup_gpencil(sgl, ob, view_layer);
2752                         break;
2753                 case OB_SPEAKER:
2754                         if (hide_object_extra) {
2755                                 break;
2756                         }
2757                         DRW_shgroup_speaker(sgl, ob, view_layer);
2758                         break;
2759                 case OB_LIGHTPROBE:
2760                         if (hide_object_extra) {
2761                                 break;
2762                         }
2763                         DRW_shgroup_lightprobe(stl, psl, ob, view_layer);
2764                         break;
2765                 case OB_ARMATURE:
2766                 {
2767                         if (v3d->overlay.flag & V3D_OVERLAY_HIDE_BONES) {
2768                                 break;
2769                         }
2770                         bArmature *arm = ob->data;
2771                         if (arm->edbo == NULL) {
2772                                 if (DRW_state_is_select() || !DRW_pose_mode_armature(ob, draw_ctx->obact)) {
2773                                         DRWArmaturePasses passes = {
2774                                             .bone_solid = sgl->bone_solid,
2775                                             .bone_outline = sgl->bone_outline,
2776                                             .bone_wire = sgl->bone_wire,
2777                                             .bone_envelope = sgl->bone_envelope,
2778                                             .bone_axes = sgl->bone_axes,
2779                                             .relationship_lines = NULL, /* Don't draw relationship lines */
2780                                         };
2781                                         DRW_shgroup_armature_object(ob, view_layer, passes);
2782                                 }
2783                         }
2784                         break;
2785                 }
2786                 default:
2787                         break;
2788         }
2789
2790         if (ob->pd && ob->pd->forcefield) {
2791                 DRW_shgroup_forcefield(sgl, ob, view_layer);
2792         }
2793
2794         /* don't show object extras in set's */
2795         if ((ob->base_flag & (BASE_FROM_SET | BASE_FROMDUPLI)) == 0) {
2796                 if ((draw_ctx->object_mode & OB_MODE_ALL_PAINT) == 0) {
2797                         DRW_shgroup_object_center(stl, ob, view_layer, v3d);
2798                 }
2799
2800                 if (show_relations && !DRW_state_is_select()) {
2801                         DRW_shgroup_relationship_lines(sgl, draw_ctx->depsgraph, scene, ob);
2802                 }
2803
2804                 if ((ob->dtx != 0) && theme_id == TH_UNDEFINED) {
2805                         theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
2806                 }
2807
2808                 if ((ob->dtx & OB_DRAWNAME) && DRW_state_show_text()) {
2809                         struct DRWTextStore *dt = DRW_text_cache_ensure();
2810
2811                         uchar color[4];
2812                         UI_GetThemeColor4ubv(theme_id, color);
2813
2814                         DRW_text_cache_add(
2815                                 dt, ob->obmat[3],
2816                                 ob->id.name + 2, strlen(ob->id.name + 2),
2817                                 10, DRW_TEXT_CACHE_GLOBALSPACE | DRW_TEXT_CACHE_STRING_PTR, color);
2818                 }
2819
2820                 if ((ob->dtx & OB_TEXSPACE) && ELEM(ob->type, OB_MESH, OB_CURVE, OB_MBALL)) {
2821                         DRW_shgroup_texture_space(sgl, ob, theme_id);
2822                 }
2823
2824                 if (ob->dtx & OB_DRAWBOUNDOX || ob->dt == OB_BOUNDBOX) {
2825                         DRW_shgroup_bounds(sgl, ob, theme_id);
2826                 }
2827
2828                 if (ob->dtx & OB_AXIS) {
2829                         float *color, axes_size = 1.0f;
2830                         DRW_object_wire_theme_get(ob, view_layer, &color);
2831
2832                         DRW_shgroup_call_dynamic_add(sgl->empty_axes, color, &axes_size, ob->obmat);
2833                 }
2834
2835                 if ((md = modifiers_findByType(ob, eModifierType_Smoke)) &&
2836                     (modifier_isEnabled(scene, md, eModifierMode_Realtime)) &&
2837                     (((SmokeModifierData *)md)->domain != NULL))
2838                 {
2839                         DRW_shgroup_volume_extra(sgl, ob, view_layer, scene, md);
2840                 }
2841         }
2842 }
2843
2844 static void OBJECT_draw_scene(void *vedata)
2845 {
2846
2847         OBJECT_PassList *psl = ((OBJECT_Data *)vedata)->psl;
2848         OBJECT_StorageList *stl = ((OBJECT_Data *)vedata)->stl;
2849         OBJECT_FramebufferList *fbl = ((OBJECT_Data *)vedata)->fbl;
2850         OBJECT_PrivateData *g_data = stl->g_data;
2851         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
2852         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
2853
2854         int id_ct_select =       g_data->id_ofs_select;
2855         int id_ct_active =       g_data->id_ofs_active;
2856         int id_ct_transform =    g_data->id_ofs_transform;
2857
2858         int id_ct_prb_select =       g_data->id_ofs_prb_select;
2859         int id_ct_prb_active =       g_data->id_ofs_prb_active;
2860         int id_ct_prb_transform =    g_data->id_ofs_prb_transform;
2861
2862         int outline_calls = id_ct_select + id_ct_active + id_ct_transform;
2863         outline_calls += id_ct_prb_select + id_ct_prb_active + id_ct_prb_transform;
2864
2865         float clearcol[4] = {0.0f, 0.0f, 0.0f, 0.0f};
2866
2867 //      DRW_draw_pass(psl->bone_envelope);  /* Never drawn in Object mode currently. */
2868
2869         MULTISAMPLE_SYNC_ENABLE(dfbl, dtxl);
2870
2871         /* This needs to be drawn after the oultine */
2872         DRW_draw_pass(stl->g_data->sgl.spot_shapes);
2873         DRW_draw_pass(stl->g_data->sgl.bone_solid);
2874         DRW_draw_pass(stl->g_data->sgl.bone_wire);
2875         DRW_draw_pass(stl->g_data->sgl.bone_outline);
2876         DRW_draw_pass(stl->g_data->sgl.non_meshes);
2877         DRW_draw_pass(psl->particle);
2878         DRW_draw_pass(stl->g_data->sgl.bone_axes);
2879
2880         MULTISAMPLE_SYNC_DISABLE(dfbl, dtxl)
2881
2882         if (DRW_state_is_fbo() && outline_calls > 0) {
2883                 DRW_stats_group_start("Outlines");
2884
2885                 g_data->id_ofs_active = 1;
2886                 g_data->id_ofs_select =    g_data->id_ofs_active + id_ct_active + id_ct_prb_active + 1;
2887                 g_data->id_ofs_transform = g_data->id_ofs_select + id_ct_select + id_ct_prb_select + 1;
2888
2889                 g_data->id_ofs_prb_active =       g_data->id_ofs_active       + id_ct_active;
2890                 g_data->id_ofs_prb_select =       g_data->id_ofs_select       + id_ct_select;
2891                 g_data->id_ofs_prb_transform =    g_data->id_ofs_transform    + id_ct_transform;
2892
2893                 /* Render filled polygon on a separate framebuffer */
2894                 GPU_framebuffer_bind(fbl->outlines_fb);
2895                 GPU_framebuffer_clear_color_depth(fbl->outlines_fb, clearcol, 1.0f);
2896                 DRW_draw_pass(psl->outlines);
2897                 DRW_draw_pass(psl->lightprobes);
2898
2899                 /* Search outline pixels */
2900                 GPU_framebuffer_bind(fbl->blur_fb);
2901                 DRW_draw_pass(psl->outlines_search);
2902
2903                 /* Expand outline to form a 3px wide line */
2904                 GPU_framebuffer_bind(fbl->expand_fb);
2905                 DRW_draw_pass(psl->outlines_expand);
2906
2907                 /* Bleed color so the AA can do it's stuff */
2908                 GPU_framebuffer_bind(fbl->blur_fb);
2909                 DRW_draw_pass(psl->outlines_bleed);
2910
2911                 /* restore main framebuffer */
2912                 GPU_framebuffer_bind(dfbl->default_fb);
2913                 DRW_stats_group_end();
2914         }
2915         else if (DRW_state_is_select()) {
2916                 /* Render probes spheres/planes so we can select them. */
2917                 DRW_draw_pass(psl->lightprobes);
2918         }
2919
2920         DRW_draw_pass(psl->ob_center);
2921
2922         if (DRW_state_is_fbo()) {
2923                 if (e_data.draw_grid) {
2924                         GPU_framebuffer_bind(dfbl->color_only_fb);
2925                         DRW_draw_pass(psl->grid);
2926                 }
2927
2928                 /* Combine with scene buffer last */
2929                 if (outline_calls > 0) {
2930                         DRW_draw_pass(psl->outlines_resolve);
2931                 }
2932         }
2933
2934         /* This has to be freed only after drawing empties! */
2935         if (stl->g_data->sgl.image_plane_map) {
2936                 BLI_ghash_free(stl->g_data->sgl.image_plane_map, NULL, MEM_freeN);
2937                 stl->g_data->sgl.image_plane_map = NULL;
2938         }
2939
2940         volumes_free_smoke_textures();
2941         batch_camera_path_free(&stl->g_data->sgl.camera_path);
2942
2943         if (!DRW_pass_is_empty(stl->g_data->sgl_ghost.bone_solid) ||
2944             !DRW_pass_is_empty(stl->g_data->sgl_ghost.bone_wire) ||
2945             !DRW_pass_is_empty(stl->g_data->sgl_ghost.bone_outline) ||
2946             !DRW_pass_is_empty(stl->g_data->sgl_ghost.non_meshes) ||
2947             !DRW_pass_is_empty(stl->g_data->sgl_ghost.bone_axes))
2948         {
2949                 if (DRW_state_is_fbo()) {
2950                         /* meh, late init to not request a depth buffer we won't use. */
2951                         const float *viewport_size = DRW_viewport_size_get();
2952                         const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
2953
2954                         GPUTexture *ghost_depth_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_DEPTH_COMPONENT24, &draw_engine_object_type);
2955                         GPU_framebuffer_ensure_config(&fbl->ghost_fb, {
2956                                 GPU_ATTACHMENT_TEXTURE(ghost_depth_tx),
2957                                 GPU_ATTACHMENT_TEXTURE(dtxl->color),
2958                         });
2959
2960                         GPU_framebuffer_bind(fbl->ghost_fb);
2961                         GPU_framebuffer_clear_depth(fbl->ghost_fb, 1.0f);
2962                 }
2963
2964                 DRW_draw_pass(stl->g_data->sgl_ghost.spot_shapes);
2965                 DRW_draw_pass(stl->g_data->sgl_ghost.bone_solid);
2966                 DRW_draw_pass(stl->g_data->sgl_ghost.bone_wire);
2967                 DRW_draw_pass(stl->g_data->sgl_ghost.bone_outline);
2968                 DRW_draw_pass(stl->g_data->sgl_ghost.non_meshes);
2969                 DRW_draw_pass(stl->g_data->sgl_ghost.bone_axes);
2970         }
2971
2972         batch_camera_path_free(&stl->g_data->sgl_ghost.camera_path);
2973         /* This has to be freed only after drawing empties! */
2974         if (stl->g_data->sgl_ghost.image_plane_map) {
2975                 BLI_ghash_free(stl->g_data->sgl_ghost.image_plane_map, NULL, MEM_freeN);
2976                 stl->g_data->sgl_ghost.image_plane_map = NULL;
2977         }
2978 }
2979
2980 static const DrawEngineDataSize OBJECT_data_size = DRW_VIEWPORT_DATA_SIZE(OBJECT_Data);
2981
2982 DrawEngineType draw_engine_object_type = {
2983         NULL, NULL,
2984         N_("ObjectMode"),
2985         &OBJECT_data_size,
2986         &OBJECT_engine_init,
2987         &OBJECT_engine_free,
2988         &OBJECT_cache_init,
2989         &OBJECT_cache_populate,
2990         NULL,
2991         NULL,
2992         &OBJECT_draw_scene,
2993         NULL,
2994         NULL,
2995 };