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