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