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