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