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