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