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