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