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