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