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