Outliner: Add object(s) to new collection
[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_camera_types.h"
32 #include "DNA_curve_types.h"
33 #include "DNA_mesh_types.h"
34 #include "DNA_meta_types.h"
35 #include "DNA_object_force.h"
36 #include "DNA_lightprobe_types.h"
37 #include "DNA_particle_types.h"
38 #include "DNA_view3d_types.h"
39 #include "DNA_world_types.h"
40
41 #include "BIF_gl.h"
42
43 #include "BKE_anim.h"
44 #include "BKE_camera.h"
45 #include "BKE_curve.h"
46 #include "BKE_global.h"
47 #include "BKE_mball.h"
48 #include "BKE_object.h"
49 #include "BKE_particle.h"
50 #include "BKE_image.h"
51 #include "BKE_texture.h"
52
53 #include "ED_view3d.h"
54
55 #include "GPU_shader.h"
56 #include "GPU_texture.h"
57
58 #include "UI_resources.h"
59
60 #include "draw_mode_engines.h"
61 #include "draw_manager_text.h"
62 #include "draw_common.h"
63
64 #include "DEG_depsgraph_query.h"
65
66 extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
67 extern struct GPUTexture *globals_ramp; /* draw_common.c */
68 extern GlobalsUboStorage ts;
69
70 extern char datatoc_object_outline_resolve_frag_glsl[];
71 extern char datatoc_object_outline_detect_frag_glsl[];
72 extern char datatoc_object_outline_expand_frag_glsl[];
73 extern char datatoc_object_grid_frag_glsl[];
74 extern char datatoc_object_grid_vert_glsl[];
75 extern char datatoc_object_empty_image_frag_glsl[];
76 extern char datatoc_object_empty_image_vert_glsl[];
77 extern char datatoc_object_lightprobe_grid_vert_glsl[];
78 extern char datatoc_object_particle_prim_vert_glsl[];
79 extern char datatoc_object_particle_dot_vert_glsl[];
80 extern char datatoc_object_particle_dot_frag_glsl[];
81 extern char datatoc_common_globals_lib_glsl[];
82 extern char datatoc_common_fxaa_lib_glsl[];
83 extern char datatoc_gpu_shader_flat_color_frag_glsl[];
84 extern char datatoc_gpu_shader_fullscreen_vert_glsl[];
85 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
86
87 /* *********** LISTS *********** */
88 typedef struct OBJECT_PassList {
89         struct DRWPass *non_meshes;
90         struct DRWPass *ob_center;
91         struct DRWPass *outlines;
92         struct DRWPass *outlines_search;
93         struct DRWPass *outlines_expand;
94         struct DRWPass *outlines_bleed;
95         struct DRWPass *outlines_resolve;
96         struct DRWPass *grid;
97         struct DRWPass *bone_solid;
98         struct DRWPass *bone_wire;
99         struct DRWPass *bone_envelope;
100         struct DRWPass *particle;
101         struct DRWPass *lightprobes;
102         /* use for empty/background images */
103         struct DRWPass *reference_image;
104 } OBJECT_PassList;
105
106 typedef struct OBJECT_FramebufferList {
107         struct GPUFrameBuffer *outlines;
108         struct GPUFrameBuffer *blur;
109 } OBJECT_FramebufferList;
110
111 typedef struct OBJECT_StorageList {
112         struct OBJECT_PrivateData *g_data;
113 } OBJECT_StorageList;
114
115 typedef struct OBJECT_Data {
116         void *engine_type;
117         OBJECT_FramebufferList *fbl;
118         DRWViewportEmptyList *txl;
119         OBJECT_PassList *psl;
120         OBJECT_StorageList *stl;
121 } OBJECT_Data;
122
123 /* *********** STATIC *********** */
124
125 typedef struct OBJECT_PrivateData {
126         /* Empties */
127         DRWShadingGroup *plain_axes;
128         DRWShadingGroup *cube;
129         DRWShadingGroup *circle;
130         DRWShadingGroup *sphere;
131         DRWShadingGroup *cone;
132         DRWShadingGroup *single_arrow;
133         DRWShadingGroup *single_arrow_line;
134         DRWShadingGroup *arrows;
135         DRWShadingGroup *axis_names;
136         /* GPUTexture -> EmptyImageShadingGroupData */
137         GHash *image_plane_map;
138
139         /* Force Field */
140         DRWShadingGroup *field_wind;
141         DRWShadingGroup *field_force;
142         DRWShadingGroup *field_vortex;
143         DRWShadingGroup *field_curve_sta;
144         DRWShadingGroup *field_curve_end;
145         DRWShadingGroup *field_tube_limit;
146         DRWShadingGroup *field_cone_limit;
147
148         /* Speaker */
149         DRWShadingGroup *speaker;
150
151         /* Probe */
152         DRWShadingGroup *probe_cube;
153         DRWShadingGroup *probe_planar;
154         DRWShadingGroup *probe_grid;
155
156         /* MetaBalls */
157         DRWShadingGroup *mball_circle;
158
159         /* Lamps */
160         DRWShadingGroup *lamp_center;
161         DRWShadingGroup *lamp_center_group;
162         DRWShadingGroup *lamp_groundpoint;
163         DRWShadingGroup *lamp_groundline;
164         DRWShadingGroup *lamp_circle;
165         DRWShadingGroup *lamp_circle_shadow;
166         DRWShadingGroup *lamp_sunrays;
167         DRWShadingGroup *lamp_distance;
168         DRWShadingGroup *lamp_buflimit;
169         DRWShadingGroup *lamp_buflimit_points;
170         DRWShadingGroup *lamp_area;
171         DRWShadingGroup *lamp_hemi;
172         DRWShadingGroup *lamp_spot_cone;
173         DRWShadingGroup *lamp_spot_blend;
174         DRWShadingGroup *lamp_spot_pyramid;
175         DRWShadingGroup *lamp_spot_blend_rect;
176
177         /* Helpers */
178         DRWShadingGroup *relationship_lines;
179
180         /* Objects Centers */
181         DRWShadingGroup *center_active;
182         DRWShadingGroup *center_selected;
183         DRWShadingGroup *center_deselected;
184         DRWShadingGroup *center_selected_lib;
185         DRWShadingGroup *center_deselected_lib;
186
187         /* Camera */
188         DRWShadingGroup *camera;
189         DRWShadingGroup *camera_frame;
190         DRWShadingGroup *camera_tria;
191         DRWShadingGroup *camera_focus;
192         DRWShadingGroup *camera_clip;
193         DRWShadingGroup *camera_clip_points;
194         DRWShadingGroup *camera_mist;
195         DRWShadingGroup *camera_mist_points;
196
197         /* Outlines */
198         DRWShadingGroup *outlines_active;
199         DRWShadingGroup *outlines_active_group;
200         DRWShadingGroup *outlines_select;
201         DRWShadingGroup *outlines_select_group;
202         DRWShadingGroup *outlines_transform;
203
204         /* Lightprobes */
205         DRWShadingGroup *lightprobes_cube;
206         DRWShadingGroup *lightprobes_planar;
207
208         /* Wire */
209         DRWShadingGroup *wire;
210         DRWShadingGroup *wire_active;
211         DRWShadingGroup *wire_active_group;
212         DRWShadingGroup *wire_select;
213         DRWShadingGroup *wire_select_group;
214         DRWShadingGroup *wire_transform;
215 } OBJECT_PrivateData; /* Transient data */
216
217 static struct {
218         /* fullscreen shaders */
219         GPUShader *outline_resolve_sh;
220         GPUShader *outline_resolve_aa_sh;
221         GPUShader *outline_detect_sh;
222         GPUShader *outline_fade_sh;
223
224         /* regular shaders */
225         GPUShader *object_empty_image_sh;
226         GPUShader *object_empty_image_wire_sh;
227         GPUShader *grid_sh;
228         GPUShader *part_dot_sh;
229         GPUShader *part_prim_sh;
230         GPUShader *part_axis_sh;
231         GPUShader *lightprobe_grid_sh;
232         float camera_pos[3];
233         float screenvecs[3][4];
234         float grid_settings[5];
235         int grid_flag;
236         float grid_normal[3];
237         float grid_axes[3];
238         int zpos_flag;
239         int zneg_flag;
240         float zplane_normal[3];
241         float zplane_axes[3];
242         float inv_viewport_size[2];
243         bool draw_grid;
244         /* Temp buffer textures */
245         struct GPUTexture *outlines_depth_tx;
246         struct GPUTexture *outlines_color_tx;
247         struct GPUTexture *outlines_blur_tx;
248 } e_data = {NULL}; /* Engine data */
249
250
251 enum {
252         SHOW_AXIS_X  = (1 << 0),
253         SHOW_AXIS_Y  = (1 << 1),
254         SHOW_AXIS_Z  = (1 << 2),
255         SHOW_GRID    = (1 << 3),
256         PLANE_XY     = (1 << 4),
257         PLANE_XZ     = (1 << 5),
258         PLANE_YZ     = (1 << 6),
259         CLIP_ZPOS    = (1 << 7),
260         CLIP_ZNEG    = (1 << 8),
261 };
262
263 /* *********** FUNCTIONS *********** */
264
265 static void OBJECT_engine_init(void *vedata)
266 {
267         OBJECT_FramebufferList *fbl = ((OBJECT_Data *)vedata)->fbl;
268
269         const float *viewport_size = DRW_viewport_size_get();
270
271         if (DRW_state_is_fbo()) {
272                 DRWFboTexture tex[2] = {
273                         {&e_data.outlines_depth_tx, DRW_TEX_DEPTH_24, DRW_TEX_TEMP},
274                         {&e_data.outlines_color_tx, DRW_TEX_RGBA_8, DRW_TEX_FILTER | DRW_TEX_TEMP},
275                 };
276
277                 DRW_framebuffer_init(
278                         &fbl->outlines, &draw_engine_object_type,
279                         (int)viewport_size[0], (int)viewport_size[1],
280                         tex, 2);
281
282                 DRWFboTexture blur_tex = {&e_data.outlines_blur_tx, DRW_TEX_RGBA_8, DRW_TEX_FILTER | DRW_TEX_TEMP};
283                 DRW_framebuffer_init(
284                         &fbl->blur, &draw_engine_object_type,
285                         (int)viewport_size[0], (int)viewport_size[1],
286                         &blur_tex, 1);
287         }
288
289         if (!e_data.outline_resolve_sh) {
290                 e_data.outline_resolve_sh = DRW_shader_create_fullscreen(datatoc_object_outline_resolve_frag_glsl, NULL);
291         }
292
293         if (!e_data.outline_resolve_aa_sh) {
294                 e_data.outline_resolve_aa_sh = DRW_shader_create_with_lib(
295                             datatoc_gpu_shader_fullscreen_vert_glsl, NULL,
296                             datatoc_object_outline_resolve_frag_glsl,
297                             datatoc_common_fxaa_lib_glsl,
298                             "#define FXAA_ALPHA\n"
299                             "#define USE_FXAA\n");
300         }
301
302         if (!e_data.outline_detect_sh) {
303                 e_data.outline_detect_sh = DRW_shader_create_fullscreen(datatoc_object_outline_detect_frag_glsl, NULL);
304         }
305
306         if (!e_data.outline_fade_sh) {
307                 e_data.outline_fade_sh = DRW_shader_create_fullscreen(datatoc_object_outline_expand_frag_glsl, NULL);
308         }
309
310         if (!e_data.object_empty_image_sh) {
311                 e_data.object_empty_image_sh = DRW_shader_create_with_lib(
312                            datatoc_object_empty_image_vert_glsl, NULL,
313                            datatoc_object_empty_image_frag_glsl,
314                            datatoc_common_globals_lib_glsl, NULL);
315         }
316
317         if (!e_data.object_empty_image_wire_sh) {
318                 e_data.object_empty_image_wire_sh = DRW_shader_create_with_lib(
319                            datatoc_object_empty_image_vert_glsl, NULL,
320                            datatoc_object_empty_image_frag_glsl,
321                            datatoc_common_globals_lib_glsl,
322                            "#define USE_WIRE\n");
323         }
324
325         if (!e_data.grid_sh) {
326                 e_data.grid_sh = DRW_shader_create_with_lib(
327                         datatoc_object_grid_vert_glsl, NULL,
328                         datatoc_object_grid_frag_glsl,
329                         datatoc_common_globals_lib_glsl, NULL);
330         }
331
332         if (!e_data.part_prim_sh) {
333                 e_data.part_prim_sh = DRW_shader_create(
334                         datatoc_object_particle_prim_vert_glsl, NULL, datatoc_gpu_shader_flat_color_frag_glsl, NULL);
335         }
336
337         if (!e_data.part_axis_sh) {
338                 e_data.part_axis_sh = DRW_shader_create(
339                         datatoc_object_particle_prim_vert_glsl, NULL, datatoc_gpu_shader_flat_color_frag_glsl,
340                         "#define USE_AXIS\n");
341         }
342
343         if (!e_data.part_dot_sh) {
344                 e_data.part_dot_sh = DRW_shader_create(
345                         datatoc_object_particle_dot_vert_glsl, NULL, datatoc_object_particle_dot_frag_glsl, NULL);
346         }
347
348         if (!e_data.lightprobe_grid_sh) {
349                 e_data.lightprobe_grid_sh = DRW_shader_create(
350                         datatoc_object_lightprobe_grid_vert_glsl, NULL, datatoc_gpu_shader_uniform_color_frag_glsl, NULL);
351         }
352
353         {
354                 /* Grid precompute */
355                 float invviewmat[4][4], invwinmat[4][4];
356                 float viewmat[4][4], winmat[4][4];
357                 const DRWContextState *draw_ctx = DRW_context_state_get();
358                 View3D *v3d = draw_ctx->v3d;
359                 Scene *scene = draw_ctx->scene;
360                 RegionView3D *rv3d = draw_ctx->rv3d;
361                 float grid_scale = ED_view3d_grid_scale(scene, v3d, NULL);
362                 float grid_res;
363
364                 const bool show_axis_x = (v3d->gridflag & V3D_SHOW_X) != 0;
365                 const bool show_axis_y = (v3d->gridflag & V3D_SHOW_Y) != 0;
366                 const bool show_axis_z = (v3d->gridflag & V3D_SHOW_Z) != 0;
367                 const bool show_floor = (v3d->gridflag & V3D_SHOW_FLOOR) != 0;
368                 e_data.draw_grid = show_axis_x || show_axis_y || show_axis_z || show_floor;
369
370                 DRW_viewport_matrix_get(winmat, DRW_MAT_WIN);
371                 DRW_viewport_matrix_get(viewmat, DRW_MAT_VIEW);
372                 DRW_viewport_matrix_get(invwinmat, DRW_MAT_WININV);
373                 DRW_viewport_matrix_get(invviewmat, DRW_MAT_VIEWINV);
374
375                 /* Setup camera pos */
376                 copy_v3_v3(e_data.camera_pos, invviewmat[3]);
377
378                 /* if perps */
379                 if (winmat[3][3] == 0.0f) {
380                         float fov;
381                         float viewvecs[2][4] = {
382                             {1.0f, -1.0f, -1.0f, 1.0f},
383                             {-1.0f, 1.0f, -1.0f, 1.0f}
384                         };
385
386                         /* convert the view vectors to view space */
387                         for (int i = 0; i < 2; i++) {
388                                 mul_m4_v4(invwinmat, viewvecs[i]);
389                                 mul_v3_fl(viewvecs[i], 1.0f / viewvecs[i][2]); /* perspective divide */
390                         }
391
392                         fov = angle_v3v3(viewvecs[0], viewvecs[1]) / 2.0f;
393                         grid_res = fabsf(tanf(fov)) / grid_scale;
394
395                         e_data.grid_flag = (1 << 4); /* XY plane */
396                         if (show_axis_x)
397                                 e_data.grid_flag |= SHOW_AXIS_X;
398                         if (show_axis_y)
399                                 e_data.grid_flag |= SHOW_AXIS_Y;
400                         if (show_floor)
401                                 e_data.grid_flag |= SHOW_GRID;
402
403                 }
404                 else {
405                         float viewdist = 1.0f / max_ff(fabsf(winmat[0][0]), fabsf(winmat[1][1]));
406                         grid_res = viewdist / grid_scale;
407
408                         if (ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT)) {
409                                 e_data.grid_flag = PLANE_YZ;
410                                 e_data.grid_flag |= SHOW_AXIS_Y;
411                                 e_data.grid_flag |= SHOW_AXIS_Z;
412                                 e_data.grid_flag |= SHOW_GRID;
413                         }
414                         else if (ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) {
415                                 e_data.grid_flag = PLANE_XY;
416                                 e_data.grid_flag |= SHOW_AXIS_X;
417                                 e_data.grid_flag |= SHOW_AXIS_Y;
418                                 e_data.grid_flag |= SHOW_GRID;
419                         }
420                         else if (ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK)) {
421                                 e_data.grid_flag = PLANE_XZ;
422                                 e_data.grid_flag |= SHOW_AXIS_X;
423                                 e_data.grid_flag |= SHOW_AXIS_Z;
424                                 e_data.grid_flag |= SHOW_GRID;
425                         }
426                         else { /* RV3D_VIEW_USER */
427                                 e_data.grid_flag = PLANE_XY;
428                                 if (show_axis_x)
429                                         e_data.grid_flag |= SHOW_AXIS_X;
430                                 if (show_axis_y)
431                                         e_data.grid_flag |= SHOW_AXIS_Y;
432                                 if (show_floor)
433                                         e_data.grid_flag |= SHOW_GRID;
434                         }
435                 }
436
437                 e_data.grid_normal[0] = (float)((e_data.grid_flag & PLANE_YZ) != 0);
438                 e_data.grid_normal[1] = (float)((e_data.grid_flag & PLANE_XZ) != 0);
439                 e_data.grid_normal[2] = (float)((e_data.grid_flag & PLANE_XY) != 0);
440
441                 e_data.grid_axes[0] = (float)((e_data.grid_flag & (PLANE_XZ | PLANE_XY)) != 0);
442                 e_data.grid_axes[1] = (float)((e_data.grid_flag & (PLANE_YZ | PLANE_XY)) != 0);
443                 e_data.grid_axes[2] = (float)((e_data.grid_flag & (PLANE_YZ | PLANE_XZ)) != 0);
444
445                 /* Vectors to recover pixel world position. Fix grid precision issue. */
446                 /* Using pixel at z = 0.0f in ndc space : gives average precision between
447                  * near and far plane. Note that it might not be the best choice. */
448                 copy_v4_fl4(e_data.screenvecs[0],  1.0f, -1.0f, 0.0f, 1.0f);
449                 copy_v4_fl4(e_data.screenvecs[1], -1.0f,  1.0f, 0.0f, 1.0f);
450                 copy_v4_fl4(e_data.screenvecs[2], -1.0f, -1.0f, 0.0f, 1.0f);
451
452                 for (int i = 0; i < 3; i++) {
453                         /* Doing 2 steps to recover world position of the corners of the frustum.
454                          * Using the inverse perspective matrix is giving very low precision output. */
455                         mul_m4_v4(invwinmat, e_data.screenvecs[i]);
456                         e_data.screenvecs[i][0] /= e_data.screenvecs[i][3]; /* perspective divide */
457                         e_data.screenvecs[i][1] /= e_data.screenvecs[i][3]; /* perspective divide */
458                         e_data.screenvecs[i][2] /= e_data.screenvecs[i][3]; /* perspective divide */
459                         e_data.screenvecs[i][3] = 1.0f;
460                         /* main instability come from this one */
461                         /* TODO : to make things even more stable, don't use
462                          * invviewmat and derive vectors from camera properties */
463                         mul_m4_v4(invviewmat, e_data.screenvecs[i]);
464                 }
465
466                 sub_v3_v3(e_data.screenvecs[0], e_data.screenvecs[2]);
467                 sub_v3_v3(e_data.screenvecs[1], e_data.screenvecs[2]);
468
469                 /* Z axis if needed */
470                 if (((rv3d->view == RV3D_VIEW_USER) || (rv3d->persp != RV3D_ORTHO)) && show_axis_z) {
471                         e_data.zpos_flag = SHOW_AXIS_Z;
472
473                         float zvec[4] = {0.0f, 0.0f, -1.0f, 0.0f};
474                         mul_m4_v4(invviewmat, zvec);
475
476                         /* z axis : chose the most facing plane */
477                         if (fabsf(zvec[0]) < fabsf(zvec[1])) {
478                                 e_data.zpos_flag |= PLANE_XZ;
479                         }
480                         else {
481                                 e_data.zpos_flag |= PLANE_YZ;
482                         }
483
484                         e_data.zneg_flag = e_data.zpos_flag;
485
486                         /* Persp : If camera is below floor plane, we switch clipping
487                          * Ortho : If eye vector is looking up, we switch clipping */
488                         if (((winmat[3][3] == 0.0f) && (e_data.camera_pos[2] > 0.0f)) ||
489                             ((winmat[3][3] != 0.0f) && (zvec[2] < 0.0f)))
490                         {
491                                 e_data.zpos_flag |= CLIP_ZPOS;
492                                 e_data.zneg_flag |= CLIP_ZNEG;
493                         }
494                         else {
495                                 e_data.zpos_flag |= CLIP_ZNEG;
496                                 e_data.zneg_flag |= CLIP_ZPOS;
497                         }
498
499                         e_data.zplane_normal[0] = (float)((e_data.zpos_flag & PLANE_YZ) != 0);
500                         e_data.zplane_normal[1] = (float)((e_data.zpos_flag & PLANE_XZ) != 0);
501                         e_data.zplane_normal[2] = (float)((e_data.zpos_flag & PLANE_XY) != 0);
502
503                         e_data.zplane_axes[0] = (float)((e_data.zpos_flag & (PLANE_XZ | PLANE_XY)) != 0);
504                         e_data.zplane_axes[1] = (float)((e_data.zpos_flag & (PLANE_YZ | PLANE_XY)) != 0);
505                         e_data.zplane_axes[2] = (float)((e_data.zpos_flag & (PLANE_YZ | PLANE_XZ)) != 0);
506
507                 }
508                 else {
509                         e_data.zneg_flag = e_data.zpos_flag = CLIP_ZNEG | CLIP_ZPOS;
510                 }
511
512                 float dist;
513                 if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
514                         Object *camera_object = DEG_get_evaluated_object(draw_ctx->depsgraph, v3d->camera);
515                         dist = ((Camera *)camera_object)->clipend;
516                 }
517                 else {
518                         dist = v3d->far;
519                 }
520
521                 e_data.grid_settings[0] = dist / 2.0f; /* gridDistance */
522                 e_data.grid_settings[1] = grid_res; /* gridResolution */
523                 e_data.grid_settings[2] = grid_scale; /* gridScale */
524                 e_data.grid_settings[3] = v3d->gridsubdiv; /* gridSubdiv */
525                 e_data.grid_settings[4] = (v3d->gridsubdiv > 1) ? 1.0f / logf(v3d->gridsubdiv) : 0.0f; /* 1/log(gridSubdiv) */
526         }
527
528         copy_v2_v2(e_data.inv_viewport_size, DRW_viewport_size_get());
529         invert_v2(e_data.inv_viewport_size);
530 }
531
532 static void OBJECT_engine_free(void)
533 {
534         DRW_SHADER_FREE_SAFE(e_data.outline_resolve_sh);
535         DRW_SHADER_FREE_SAFE(e_data.outline_resolve_aa_sh);
536         DRW_SHADER_FREE_SAFE(e_data.outline_detect_sh);
537         DRW_SHADER_FREE_SAFE(e_data.outline_fade_sh);
538         DRW_SHADER_FREE_SAFE(e_data.object_empty_image_sh);
539         DRW_SHADER_FREE_SAFE(e_data.object_empty_image_wire_sh);
540         DRW_SHADER_FREE_SAFE(e_data.grid_sh);
541         DRW_SHADER_FREE_SAFE(e_data.part_prim_sh);
542         DRW_SHADER_FREE_SAFE(e_data.part_axis_sh);
543         DRW_SHADER_FREE_SAFE(e_data.part_dot_sh);
544         DRW_SHADER_FREE_SAFE(e_data.lightprobe_grid_sh);
545 }
546
547 static DRWShadingGroup *shgroup_outline(DRWPass *pass, const float col[4], GPUShader *sh)
548 {
549         DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
550         DRW_shgroup_uniform_vec4(grp, "color", col, 1);
551
552         return grp;
553 }
554
555 /* currently same as 'shgroup_outline', new function to avoid confustion */
556 static DRWShadingGroup *shgroup_wire(DRWPass *pass, const float col[4], GPUShader *sh)
557 {
558         DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
559         DRW_shgroup_uniform_vec4(grp, "color", col, 1);
560
561         return grp;
562 }
563
564 static DRWShadingGroup *shgroup_theme_id_to_outline_or(
565         OBJECT_StorageList *stl, int theme_id, DRWShadingGroup *fallback)
566 {
567         switch (theme_id) {
568                 case TH_ACTIVE:
569                         return stl->g_data->outlines_active;
570                 case TH_SELECT:
571                         return stl->g_data->outlines_select;
572                 case TH_GROUP_ACTIVE:
573                         return stl->g_data->outlines_select_group;
574                 case TH_TRANSFORM:
575                         return stl->g_data->outlines_transform;
576                 default:
577                         return fallback;
578         }
579 }
580
581 static DRWShadingGroup *shgroup_theme_id_to_wire_or(
582         OBJECT_StorageList *stl, int theme_id, DRWShadingGroup *fallback)
583 {
584         switch (theme_id) {
585                 case TH_ACTIVE:
586                         return stl->g_data->wire_active;
587                 case TH_SELECT:
588                         return stl->g_data->wire_select;
589                 case TH_GROUP_ACTIVE:
590                         return stl->g_data->wire_select_group;
591                 case TH_TRANSFORM:
592                         return stl->g_data->wire_transform;
593                 default:
594                         return fallback;
595         }
596 }
597
598 static void image_calc_aspect(Image *ima, ImageUser *iuser, float r_image_aspect[2])
599 {
600         float ima_x, ima_y;
601         if (ima) {
602                 int w, h;
603                 BKE_image_get_size(ima, iuser, &w, &h);
604                 ima_x = w;
605                 ima_y = h;
606         }
607         else {
608                 /* if no image, make it a 1x1 empty square, honor scale & offset */
609                 ima_x = ima_y = 1.0f;
610         }
611         /* Get the image aspect even if the buffer is invalid */
612         float sca_x = 1.0f, sca_y = 1.0f;
613         if (ima) {
614                 if (ima->aspx > ima->aspy) {
615                         sca_y = ima->aspy / ima->aspx;
616                 }
617                 else if (ima->aspx < ima->aspy) {
618                         sca_x = ima->aspx / ima->aspy;
619                 }
620         }
621
622         const float scale_x_inv = ima_x * sca_x;
623         const float scale_y_inv = ima_y * sca_y;
624         if (scale_x_inv > scale_y_inv) {
625                 r_image_aspect[0] = 1.0f;
626                 r_image_aspect[1] = scale_y_inv / scale_x_inv;
627         }
628         else {
629                 r_image_aspect[0] = scale_x_inv / scale_y_inv;
630                 r_image_aspect[1] = 1.0f;
631         }
632 }
633
634 /* per-image shading groups for image-type empty objects */
635 struct EmptyImageShadingGroupData {
636         DRWShadingGroup *shgrp_image;
637         DRWShadingGroup *shgrp_wire;
638         float image_aspect[2];
639 };
640
641 static void DRW_shgroup_empty_image(
642         OBJECT_StorageList *stl, OBJECT_PassList *psl, Object *ob, const float color[3])
643 {
644         /* TODO: 'StereoViews', see draw_empty_image. */
645
646         if (stl->g_data->image_plane_map == NULL) {
647                 stl->g_data->image_plane_map = BLI_ghash_ptr_new(__func__);
648         }
649
650         struct EmptyImageShadingGroupData *empty_image_data;
651
652         GPUTexture *tex = ob->data ?
653                 GPU_texture_from_blender(ob->data, ob->iuser, GL_TEXTURE_2D, false, false, false) : NULL;
654         void **val_p;
655
656         /* Create on demand, 'tex' may be NULL. */
657         if (BLI_ghash_ensure_p(stl->g_data->image_plane_map, tex, &val_p)) {
658                 empty_image_data = *val_p;
659         }
660         else {
661                 empty_image_data = MEM_mallocN(sizeof(*empty_image_data), __func__);
662
663                 image_calc_aspect(ob->data, ob->iuser, empty_image_data->image_aspect);
664
665                 if (tex) {
666                         struct Gwn_Batch *geom = DRW_cache_image_plane_get();
667                         DRWShadingGroup *grp = DRW_shgroup_instance_create(
668                                 e_data.object_empty_image_sh, psl->non_meshes, geom);
669                         DRW_shgroup_attrib_float(grp, "objectColor", 4);
670                         DRW_shgroup_attrib_float(grp, "size", 1);
671                         DRW_shgroup_attrib_float(grp, "offset", 2);
672                         DRW_shgroup_attrib_float(grp, "InstanceModelMatrix", 16);
673
674                         DRW_shgroup_uniform_texture(grp, "image", tex);
675                         DRW_shgroup_uniform_vec2(grp, "aspect", empty_image_data->image_aspect, 1);
676
677                         empty_image_data->shgrp_image = grp;
678                 }
679                 else {
680                         empty_image_data->shgrp_image = NULL;
681                 }
682
683                 {
684                         struct Gwn_Batch *geom = DRW_cache_image_plane_wire_get();
685                         DRWShadingGroup *grp = DRW_shgroup_instance_create(
686                                 e_data.object_empty_image_wire_sh, psl->non_meshes, geom);
687                         DRW_shgroup_attrib_float(grp, "color", 3);
688                         DRW_shgroup_attrib_float(grp, "size", 1);
689                         DRW_shgroup_attrib_float(grp, "offset", 2);
690                         DRW_shgroup_attrib_float(grp, "InstanceModelMatrix", 16);
691
692                         DRW_shgroup_uniform_vec2(grp, "aspect", empty_image_data->image_aspect, 1);
693
694                         empty_image_data->shgrp_wire = grp;
695                 }
696
697                 *val_p = empty_image_data;
698         }
699
700         if (empty_image_data->shgrp_image != NULL) {
701                 DRW_shgroup_call_dynamic_add(
702                         empty_image_data->shgrp_image,
703                         ob->col,
704                         &ob->empty_drawsize,
705                         ob->ima_ofs,
706                         ob->obmat);
707         }
708
709         DRW_shgroup_call_dynamic_add(
710                 empty_image_data->shgrp_wire,
711                 color,
712                 &ob->empty_drawsize,
713                 ob->ima_ofs,
714                 ob->obmat);
715 }
716
717 static void OBJECT_cache_init(void *vedata)
718 {
719         OBJECT_PassList *psl = ((OBJECT_Data *)vedata)->psl;
720         OBJECT_StorageList *stl = ((OBJECT_Data *)vedata)->stl;
721         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
722
723         if (!stl->g_data) {
724                 /* Alloc transient pointers */
725                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
726         }
727
728         {
729                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_WIRE;
730                 psl->outlines = DRW_pass_create("Outlines Depth Pass", state);
731
732                 GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
733
734                 /* Select */
735                 stl->g_data->outlines_select = shgroup_outline(psl->outlines, ts.colorSelect, sh);
736                 stl->g_data->outlines_select_group = shgroup_outline(psl->outlines, ts.colorGroupActive, sh);
737
738                 /* Transform */
739                 stl->g_data->outlines_transform = shgroup_outline(psl->outlines, ts.colorTransform, sh);
740
741                 /* Active */
742                 stl->g_data->outlines_active = shgroup_outline(psl->outlines, ts.colorActive, sh);
743                 stl->g_data->outlines_active_group = shgroup_outline(psl->outlines, ts.colorGroupActive, sh);
744         }
745
746         {
747                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS;
748                 psl->lightprobes = DRW_pass_create("Object Probe Pass", state);
749
750                 /* Cubemap */
751                 stl->g_data->lightprobes_cube = shgroup_instance(psl->lightprobes, DRW_cache_sphere_get());
752
753                 /* Planar */
754                 stl->g_data->lightprobes_planar = shgroup_instance(psl->lightprobes, DRW_cache_quad_get());
755         }
756
757         {
758                 DRWState state = DRW_STATE_WRITE_COLOR;
759                 struct Gwn_Batch *quad = DRW_cache_fullscreen_quad_get();
760                 static float alphaOcclu = 0.35f;
761                 /* Reminder : bool uniforms need to be 4 bytes. */
762                 static const int bTrue = true;
763                 static const int bFalse = false;
764
765                 psl->outlines_search = DRW_pass_create("Outlines Detect Pass", state);
766
767                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.outline_detect_sh, psl->outlines_search);
768                 DRW_shgroup_uniform_buffer(grp, "outlineColor", &e_data.outlines_color_tx);
769                 DRW_shgroup_uniform_buffer(grp, "outlineDepth", &e_data.outlines_depth_tx);
770                 DRW_shgroup_uniform_buffer(grp, "sceneDepth", &dtxl->depth);
771                 DRW_shgroup_uniform_float(grp, "alphaOcclu", &alphaOcclu, 1);
772                 DRW_shgroup_call_add(grp, quad, NULL);
773
774                 psl->outlines_expand = DRW_pass_create("Outlines Expand Pass", state);
775
776                 grp = DRW_shgroup_create(e_data.outline_fade_sh, psl->outlines_expand);
777                 DRW_shgroup_uniform_buffer(grp, "outlineColor", &e_data.outlines_blur_tx);
778                 DRW_shgroup_uniform_bool(grp, "doExpand", &bTrue, 1);
779                 DRW_shgroup_call_add(grp, quad, NULL);
780
781                 psl->outlines_bleed = DRW_pass_create("Outlines Bleed Pass", state);
782
783                 grp = DRW_shgroup_create(e_data.outline_fade_sh, psl->outlines_bleed);
784                 DRW_shgroup_uniform_buffer(grp, "outlineColor", &e_data.outlines_color_tx);
785                 DRW_shgroup_uniform_bool(grp, "doExpand", &bFalse, 1);
786                 DRW_shgroup_call_add(grp, quad, NULL);
787         }
788
789         {
790                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND;
791                 psl->outlines_resolve = DRW_pass_create("Outlines Resolve Pass", state);
792
793                 struct Gwn_Batch *quad = DRW_cache_fullscreen_quad_get();
794
795                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.outline_resolve_aa_sh, psl->outlines_resolve);
796                 DRW_shgroup_uniform_buffer(grp, "outlineBluredColor", &e_data.outlines_blur_tx);
797                 DRW_shgroup_uniform_vec2(grp, "rcpDimensions", e_data.inv_viewport_size, 1);
798                 DRW_shgroup_call_add(grp, quad, NULL);
799         }
800
801         {
802                 /* Grid pass */
803                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND;
804                 psl->grid = DRW_pass_create("Infinite Grid Pass", state);
805
806                 struct Gwn_Batch *quad = DRW_cache_fullscreen_quad_get();
807                 static float mat[4][4];
808                 unit_m4(mat);
809
810                 /* Create 3 quads to render ordered transparency Z axis */
811                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.grid_sh, psl->grid);
812                 DRW_shgroup_uniform_int(grp, "gridFlag", &e_data.zneg_flag, 1);
813                 DRW_shgroup_uniform_vec3(grp, "planeNormal", e_data.zplane_normal, 1);
814                 DRW_shgroup_uniform_vec3(grp, "planeAxes", e_data.zplane_axes, 1);
815                 DRW_shgroup_uniform_vec3(grp, "cameraPos", e_data.camera_pos, 1);
816                 DRW_shgroup_uniform_vec4(grp, "screenvecs[0]", e_data.screenvecs[0], 3);
817                 DRW_shgroup_uniform_vec4(grp, "gridSettings", e_data.grid_settings, 1);
818                 DRW_shgroup_uniform_float(grp, "gridOneOverLogSubdiv", &e_data.grid_settings[4], 1);
819                 DRW_shgroup_uniform_block(grp, "globalsBlock", globals_ubo);
820                 DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
821                 DRW_shgroup_uniform_buffer(grp, "depthBuffer", &dtxl->depth);
822                 DRW_shgroup_call_add(grp, quad, mat);
823
824                 grp = DRW_shgroup_create(e_data.grid_sh, psl->grid);
825                 DRW_shgroup_uniform_int(grp, "gridFlag", &e_data.grid_flag, 1);
826                 DRW_shgroup_uniform_vec3(grp, "planeNormal", e_data.grid_normal, 1);
827                 DRW_shgroup_uniform_vec3(grp, "planeAxes", e_data.grid_axes, 1);
828                 DRW_shgroup_uniform_block(grp, "globalsBlock", globals_ubo);
829                 DRW_shgroup_uniform_buffer(grp, "depthBuffer", &dtxl->depth);
830                 DRW_shgroup_call_add(grp, quad, mat);
831
832                 grp = DRW_shgroup_create(e_data.grid_sh, psl->grid);
833                 DRW_shgroup_uniform_int(grp, "gridFlag", &e_data.zpos_flag, 1);
834                 DRW_shgroup_uniform_vec3(grp, "planeNormal", e_data.zplane_normal, 1);
835                 DRW_shgroup_uniform_vec3(grp, "planeAxes", e_data.zplane_axes, 1);
836                 DRW_shgroup_uniform_block(grp, "globalsBlock", globals_ubo);
837                 DRW_shgroup_uniform_buffer(grp, "depthBuffer", &dtxl->depth);
838                 DRW_shgroup_call_add(grp, quad, mat);
839         }
840
841         {
842                 /* Solid bones */
843                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS;
844                 psl->bone_solid = DRW_pass_create("Bone Solid Pass", state);
845         }
846
847         {
848                 /* Wire bones */
849                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND;
850                 psl->bone_wire = DRW_pass_create("Bone Wire Pass", state);
851         }
852
853         {
854                 /* distance outline around envelope bones */
855                 DRWState state = DRW_STATE_ADDITIVE | DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND;
856                 psl->bone_envelope = DRW_pass_create("Bone Envelope Outline Pass", state);
857         }
858
859         {
860                 /* Non Meshes Pass (Camera, empties, lamps ...) */
861                 struct Gwn_Batch *geom;
862
863                 DRWState state =
864                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH |
865                         DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND | DRW_STATE_POINT;
866                 state |= DRW_STATE_WIRE;
867                 psl->non_meshes = DRW_pass_create("Non Meshes Pass", state);
868
869                 /* Empties */
870                 geom = DRW_cache_plain_axes_get();
871                 stl->g_data->plain_axes = shgroup_instance(psl->non_meshes, geom);
872
873                 geom = DRW_cache_cube_get();
874                 stl->g_data->cube = shgroup_instance(psl->non_meshes, geom);
875
876                 geom = DRW_cache_circle_get();
877                 stl->g_data->circle = shgroup_instance(psl->non_meshes, geom);
878
879                 geom = DRW_cache_empty_sphere_get();
880                 stl->g_data->sphere = shgroup_instance(psl->non_meshes, geom);
881
882                 geom = DRW_cache_empty_cone_get();
883                 stl->g_data->cone = shgroup_instance(psl->non_meshes, geom);
884
885                 geom = DRW_cache_single_arrow_get();
886                 stl->g_data->single_arrow = shgroup_instance(psl->non_meshes, geom);
887
888                 geom = DRW_cache_single_line_get();
889                 stl->g_data->single_arrow_line = shgroup_instance(psl->non_meshes, geom);
890
891                 geom = DRW_cache_arrows_get();
892                 stl->g_data->arrows = shgroup_instance(psl->non_meshes, geom);
893
894                 geom = DRW_cache_axis_names_get();
895                 stl->g_data->axis_names = shgroup_instance_axis_names(psl->non_meshes, geom);
896
897                 /* initialize on first use */
898                 stl->g_data->image_plane_map = NULL;
899
900                 /* Force Field */
901                 geom = DRW_cache_field_wind_get();
902                 stl->g_data->field_wind = shgroup_instance_scaled(psl->non_meshes, geom);
903
904                 geom = DRW_cache_field_force_get();
905                 stl->g_data->field_force = shgroup_instance_screen_aligned(psl->non_meshes, geom);
906
907                 geom = DRW_cache_field_vortex_get();
908                 stl->g_data->field_vortex = shgroup_instance_scaled(psl->non_meshes, geom);
909
910                 geom = DRW_cache_screenspace_circle_get();
911                 stl->g_data->field_curve_sta = shgroup_instance_screen_aligned(psl->non_meshes, geom);
912
913                 /* Speaker */
914                 geom = DRW_cache_speaker_get();
915                 stl->g_data->speaker = shgroup_instance(psl->non_meshes, geom);
916
917                 /* Probe */
918                 static float probeSize = 14.0f;
919                 geom = DRW_cache_lightprobe_cube_get();
920                 stl->g_data->probe_cube = shgroup_instance_screenspace(psl->non_meshes, geom, &probeSize);
921
922                 geom = DRW_cache_lightprobe_grid_get();
923                 stl->g_data->probe_grid = shgroup_instance_screenspace(psl->non_meshes, geom, &probeSize);
924
925                 static float probePlanarSize = 20.0f;
926                 geom = DRW_cache_lightprobe_planar_get();
927                 stl->g_data->probe_planar = shgroup_instance_screenspace(psl->non_meshes, geom, &probePlanarSize);
928
929                 /* Camera */
930                 geom = DRW_cache_camera_get();
931                 stl->g_data->camera = shgroup_camera_instance(psl->non_meshes, geom);
932
933                 geom = DRW_cache_camera_frame_get();
934                 stl->g_data->camera_frame = shgroup_camera_instance(psl->non_meshes, geom);
935
936                 geom = DRW_cache_camera_tria_get();
937                 stl->g_data->camera_tria = shgroup_camera_instance(psl->non_meshes, geom);
938
939                 geom = DRW_cache_plain_axes_get();
940                 stl->g_data->camera_focus = shgroup_instance(psl->non_meshes, geom);
941
942                 geom = DRW_cache_single_line_get();
943                 stl->g_data->camera_clip = shgroup_distance_lines_instance(psl->non_meshes, geom);
944                 stl->g_data->camera_mist = shgroup_distance_lines_instance(psl->non_meshes, geom);
945
946                 geom = DRW_cache_single_line_endpoints_get();
947                 stl->g_data->camera_clip_points = shgroup_distance_lines_instance(psl->non_meshes, geom);
948                 stl->g_data->camera_mist_points = shgroup_distance_lines_instance(psl->non_meshes, geom);
949
950         }
951
952         {
953                 GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
954
955                 /* Unselected */
956                 stl->g_data->wire = shgroup_wire(psl->non_meshes, ts.colorWire, sh);
957
958                 /* Select */
959                 stl->g_data->wire_select = shgroup_wire(psl->non_meshes, ts.colorSelect, sh);
960                 stl->g_data->wire_select_group = shgroup_wire(psl->non_meshes, ts.colorGroupActive, sh);
961
962                 /* Transform */
963                 stl->g_data->wire_transform = shgroup_wire(psl->non_meshes, ts.colorTransform, sh);
964
965                 /* Active */
966                 stl->g_data->wire_active = shgroup_wire(psl->non_meshes, ts.colorActive, sh);
967                 stl->g_data->wire_active_group = shgroup_wire(psl->non_meshes, ts.colorGroupActive, sh);
968         }
969
970         {
971                 /* Metaballs Helpers */
972                 struct Gwn_Batch *geom;
973                 geom = DRW_cache_screenspace_circle_get();
974                 stl->g_data->mball_circle = shgroup_instance_mball_helpers(psl->non_meshes, geom);
975         }
976
977         {
978                 /* Lamps */
979                 /* TODO
980                  * for now we create multiple times the same VBO with only lamp center coordinates
981                  * but ideally we would only create it once */
982                 struct Gwn_Batch *geom;
983
984                 /* start with buflimit because we don't want stipples */
985                 geom = DRW_cache_single_line_get();
986                 stl->g_data->lamp_buflimit = shgroup_distance_lines_instance(psl->non_meshes, geom);
987
988                 stl->g_data->lamp_center = shgroup_dynpoints_uniform_color(psl->non_meshes, ts.colorLampNoAlpha, &ts.sizeLampCenter);
989                 stl->g_data->lamp_center_group = shgroup_dynpoints_uniform_color(psl->non_meshes, ts.colorGroup, &ts.sizeLampCenter);
990
991                 geom = DRW_cache_lamp_get();
992                 stl->g_data->lamp_circle = shgroup_instance_screenspace(psl->non_meshes, geom, &ts.sizeLampCircle);
993                 geom = DRW_cache_lamp_shadows_get();
994                 stl->g_data->lamp_circle_shadow = shgroup_instance_screenspace(psl->non_meshes, geom, &ts.sizeLampCircleShadow);
995
996                 geom = DRW_cache_lamp_sunrays_get();
997                 stl->g_data->lamp_sunrays = shgroup_instance_screenspace(psl->non_meshes, geom, &ts.sizeLampCircle);
998
999                 stl->g_data->lamp_groundline = shgroup_groundlines_uniform_color(psl->non_meshes, ts.colorLamp);
1000                 stl->g_data->lamp_groundpoint = shgroup_groundpoints_uniform_color(psl->non_meshes, ts.colorLamp);
1001
1002                 geom = DRW_cache_lamp_area_get();
1003                 stl->g_data->lamp_area = shgroup_instance(psl->non_meshes, geom);
1004
1005                 geom = DRW_cache_lamp_hemi_get();
1006                 stl->g_data->lamp_hemi = shgroup_instance(psl->non_meshes, geom);
1007
1008                 geom = DRW_cache_single_line_get();
1009                 stl->g_data->lamp_distance = shgroup_distance_lines_instance(psl->non_meshes, geom);
1010
1011                 geom = DRW_cache_single_line_endpoints_get();
1012                 stl->g_data->lamp_buflimit_points = shgroup_distance_lines_instance(psl->non_meshes, geom);
1013
1014                 geom = DRW_cache_lamp_spot_get();
1015                 stl->g_data->lamp_spot_cone = shgroup_spot_instance(psl->non_meshes, geom);
1016
1017                 geom = DRW_cache_circle_get();
1018                 stl->g_data->lamp_spot_blend = shgroup_instance(psl->non_meshes, geom);
1019
1020                 geom = DRW_cache_lamp_spot_square_get();
1021                 stl->g_data->lamp_spot_pyramid = shgroup_instance(psl->non_meshes, geom);
1022
1023                 geom = DRW_cache_square_get();
1024                 stl->g_data->lamp_spot_blend_rect = shgroup_instance(psl->non_meshes, geom);
1025         }
1026
1027         {
1028                 /* -------- STIPPLES ------- */
1029                 /* TODO port to shader stipple */
1030                 struct Gwn_Batch *geom;
1031
1032                 /* Relationship Lines */
1033                 stl->g_data->relationship_lines = shgroup_dynlines_uniform_color(psl->non_meshes, ts.colorWire);
1034                 DRW_shgroup_state_enable(stl->g_data->relationship_lines, DRW_STATE_STIPPLE_3);
1035
1036                 /* Force Field Curve Guide End (here because of stipple) */
1037                 geom = DRW_cache_screenspace_circle_get();
1038                 stl->g_data->field_curve_end = shgroup_instance_screen_aligned(psl->non_meshes, geom);
1039
1040                 /* Force Field Limits */
1041                 geom = DRW_cache_field_tube_limit_get();
1042                 stl->g_data->field_tube_limit = shgroup_instance_scaled(psl->non_meshes, geom);
1043
1044                 geom = DRW_cache_field_cone_limit_get();
1045                 stl->g_data->field_cone_limit = shgroup_instance_scaled(psl->non_meshes, geom);
1046         }
1047
1048         {
1049                 /* Object Center pass grouped by State */
1050                 DRWShadingGroup *grp;
1051                 static float outlineWidth, size;
1052
1053                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND | DRW_STATE_POINT;
1054                 psl->ob_center = DRW_pass_create("Obj Center Pass", state);
1055
1056                 outlineWidth = 1.0f * U.pixelsize;
1057                 size = U.obcenter_dia * U.pixelsize + outlineWidth;
1058
1059                 GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA);
1060
1061                 /* Active */
1062                 grp = DRW_shgroup_point_batch_create(sh, psl->ob_center);
1063                 DRW_shgroup_uniform_float(grp, "size", &size, 1);
1064                 DRW_shgroup_uniform_float(grp, "outlineWidth", &outlineWidth, 1);
1065                 DRW_shgroup_uniform_vec4(grp, "color", ts.colorActive, 1);
1066                 DRW_shgroup_uniform_vec4(grp, "outlineColor", ts.colorOutline, 1);
1067                 stl->g_data->center_active = grp;
1068
1069                 /* Select */
1070                 grp = DRW_shgroup_point_batch_create(sh, psl->ob_center);
1071                 DRW_shgroup_uniform_vec4(grp, "color", ts.colorSelect, 1);
1072                 stl->g_data->center_selected = grp;
1073
1074                 /* Deselect */
1075                 grp = DRW_shgroup_point_batch_create(sh, psl->ob_center);
1076                 DRW_shgroup_uniform_vec4(grp, "color", ts.colorDeselect, 1);
1077                 stl->g_data->center_deselected = grp;
1078
1079                 /* Select (library) */
1080                 grp = DRW_shgroup_point_batch_create(sh, psl->ob_center);
1081                 DRW_shgroup_uniform_vec4(grp, "color", ts.colorLibrarySelect, 1);
1082                 stl->g_data->center_selected_lib = grp;
1083
1084                 /* Deselect (library) */
1085                 grp = DRW_shgroup_point_batch_create(sh, psl->ob_center);
1086                 DRW_shgroup_uniform_vec4(grp, "color", ts.colorLibrary, 1);
1087                 stl->g_data->center_deselected_lib = grp;
1088         }
1089
1090         {
1091                 /* Particle Pass */
1092                 psl->particle = DRW_pass_create(
1093                         "Particle Pass",
1094                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS |
1095                         DRW_STATE_POINT | DRW_STATE_BLEND);
1096         }
1097
1098         {
1099                 /* Empty/Background Image Pass */
1100                 psl->reference_image = DRW_pass_create(
1101                         "Refrence Image Pass",
1102                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND);
1103         }
1104 }
1105
1106 static void DRW_shgroup_mball_helpers(OBJECT_StorageList *stl, Object *ob, ViewLayer *view_layer)
1107 {
1108         MetaBall *mb = ob->data;
1109
1110         float *color;
1111         DRW_object_wire_theme_get(ob, view_layer, &color);
1112
1113         for (MetaElem *ml = mb->elems.first; ml != NULL; ml = ml->next) {
1114                 /* draw radius */
1115                 BKE_mball_element_calc_display_m3x4(ml->draw_scale_xform, ob->obmat, &ml->x);
1116                 DRW_shgroup_call_dynamic_add(stl->g_data->mball_circle, ml->draw_scale_xform, &ml->rad, color);
1117         }
1118 }
1119
1120 static void DRW_shgroup_lamp(OBJECT_StorageList *stl, Object *ob, ViewLayer *view_layer)
1121 {
1122         Lamp *la = ob->data;
1123         float *color;
1124         int theme_id = DRW_object_wire_theme_get(ob, view_layer, &color);
1125         static float zero = 0.0f;
1126
1127         typedef struct LampEngineData {
1128                 ObjectEngineData engine_data;
1129                 float shape_mat[4][4];
1130                 float spot_blend_mat[4][4];
1131         } LampEngineData;
1132
1133         LampEngineData *lamp_engine_data =
1134                 (LampEngineData *)DRW_object_engine_data_ensure(
1135                         ob,
1136                         &draw_engine_object_type,
1137                         sizeof(LampEngineData),
1138                         NULL,
1139                         NULL);
1140
1141         float (*shapemat)[4] = lamp_engine_data->shape_mat;
1142         float (*spotblendmat)[4] = lamp_engine_data->spot_blend_mat;
1143
1144         /* Don't draw the center if it's selected or active */
1145         if (theme_id == TH_GROUP)
1146                 DRW_shgroup_call_dynamic_add(stl->g_data->lamp_center_group, ob->obmat[3]);
1147         else if (theme_id == TH_LAMP)
1148                 DRW_shgroup_call_dynamic_add(stl->g_data->lamp_center, ob->obmat[3]);
1149
1150         /* First circle */
1151         DRW_shgroup_call_dynamic_add(stl->g_data->lamp_circle, ob->obmat[3], color);
1152
1153         /* draw dashed outer circle if shadow is on. remember some lamps can't have certain shadows! */
1154         if (la->type != LA_HEMI) {
1155                 if ((la->mode & LA_SHAD_RAY) || ((la->mode & LA_SHAD_BUF) && (la->type == LA_SPOT))) {
1156                         DRW_shgroup_call_dynamic_add(stl->g_data->lamp_circle_shadow, ob->obmat[3], color);
1157                 }
1158         }
1159
1160         /* Distance */
1161         if (ELEM(la->type, LA_HEMI, LA_SUN, LA_AREA)) {
1162                 DRW_shgroup_call_dynamic_add(stl->g_data->lamp_distance, color, &zero, &la->dist, ob->obmat);
1163         }
1164
1165         copy_m4_m4(shapemat, ob->obmat);
1166
1167         if (la->type == LA_SUN) {
1168                 DRW_shgroup_call_dynamic_add(stl->g_data->lamp_sunrays, ob->obmat[3], color);
1169         }
1170         else if (la->type == LA_SPOT) {
1171                 float size[3], sizemat[4][4];
1172                 static float one = 1.0f;
1173                 float blend = 1.0f - pow2f(la->spotblend);
1174
1175                 size[0] = size[1] = sinf(la->spotsize * 0.5f) * la->dist;
1176                 size[2] = cosf(la->spotsize * 0.5f) * la->dist;
1177
1178                 size_to_mat4(sizemat, size);
1179                 mul_m4_m4m4(shapemat, ob->obmat, sizemat);
1180
1181                 size[0] = size[1] = blend; size[2] = 1.0f;
1182                 size_to_mat4(sizemat, size);
1183                 translate_m4(sizemat, 0.0f, 0.0f, -1.0f);
1184                 rotate_m4(sizemat, 'X', (float)(M_PI / 2));
1185                 mul_m4_m4m4(spotblendmat, shapemat, sizemat);
1186
1187                 if (la->mode & LA_SQUARE) {
1188                         DRW_shgroup_call_dynamic_add(stl->g_data->lamp_spot_pyramid, color, &one, shapemat);
1189
1190                         /* hide line if it is zero size or overlaps with outer border,
1191                          * previously it adjusted to always to show it but that seems
1192                          * confusing because it doesn't show the actual blend size */
1193                         if (blend != 0.0f && blend != 1.0f) {
1194                                 DRW_shgroup_call_dynamic_add(stl->g_data->lamp_spot_blend_rect, color, &one, spotblendmat);
1195                         }
1196                 }
1197                 else {
1198                         DRW_shgroup_call_dynamic_add(stl->g_data->lamp_spot_cone, color, shapemat);
1199
1200                         /* hide line if it is zero size or overlaps with outer border,
1201                          * previously it adjusted to always to show it but that seems
1202                          * confusing because it doesn't show the actual blend size */
1203                         if (blend != 0.0f && blend != 1.0f) {
1204                                 DRW_shgroup_call_dynamic_add(stl->g_data->lamp_spot_blend, color, &one, spotblendmat);
1205                         }
1206                 }
1207
1208                 DRW_shgroup_call_dynamic_add(stl->g_data->lamp_buflimit, color, &la->clipsta, &la->clipend, ob->obmat);
1209                 DRW_shgroup_call_dynamic_add(stl->g_data->lamp_buflimit_points, color, &la->clipsta, &la->clipend, ob->obmat);
1210         }
1211         else if (la->type == LA_HEMI) {
1212                 static float hemisize = 2.0f;
1213                 DRW_shgroup_call_dynamic_add(stl->g_data->lamp_hemi, color, &hemisize, shapemat);
1214         }
1215         else if (la->type == LA_AREA) {
1216                 float size[3] = {1.0f, 1.0f, 1.0f}, sizemat[4][4];
1217
1218                 if (la->area_shape == LA_AREA_RECT) {
1219                         size[1] = la->area_sizey / la->area_size;
1220                         size_to_mat4(sizemat, size);
1221                         mul_m4_m4m4(shapemat, shapemat, sizemat);
1222                 }
1223
1224                 DRW_shgroup_call_dynamic_add(stl->g_data->lamp_area, color, &la->area_size, shapemat);
1225         }
1226
1227         /* Line and point going to the ground */
1228         DRW_shgroup_call_dynamic_add(stl->g_data->lamp_groundline, ob->obmat[3]);
1229         DRW_shgroup_call_dynamic_add(stl->g_data->lamp_groundpoint, ob->obmat[3]);
1230 }
1231
1232 static void DRW_shgroup_camera(OBJECT_StorageList *stl, Object *ob, ViewLayer *view_layer)
1233 {
1234         const DRWContextState *draw_ctx = DRW_context_state_get();
1235         View3D *v3d = draw_ctx->v3d;
1236         Scene *scene = draw_ctx->scene;
1237         RegionView3D *rv3d = draw_ctx->rv3d;
1238
1239         Camera *cam = ob->data;
1240         const bool is_active = (ob == v3d->camera);
1241         const bool look_through = (is_active && (rv3d->persp == RV3D_CAMOB));
1242         float *color;
1243         DRW_object_wire_theme_get(ob, view_layer, &color);
1244
1245         float vec[4][3], asp[2], shift[2], scale[3], drawsize;
1246
1247         scale[0] = 1.0f / len_v3(ob->obmat[0]);
1248         scale[1] = 1.0f / len_v3(ob->obmat[1]);
1249         scale[2] = 1.0f / len_v3(ob->obmat[2]);
1250
1251         BKE_camera_view_frame_ex(scene, cam, cam->drawsize, false, scale,
1252                                  asp, shift, &drawsize, vec);
1253
1254         /* Frame coords */
1255         copy_v2_v2(cam->drwcorners[0], vec[0]);
1256         copy_v2_v2(cam->drwcorners[1], vec[1]);
1257         copy_v2_v2(cam->drwcorners[2], vec[2]);
1258         copy_v2_v2(cam->drwcorners[3], vec[3]);
1259
1260         /* depth */
1261         cam->drwdepth = vec[0][2];
1262
1263         /* tria */
1264         cam->drwtria[0][0] = shift[0] + ((0.7f * drawsize) * scale[0]);
1265         cam->drwtria[0][1] = shift[1] + ((drawsize * (asp[1] + 0.1f)) * scale[1]);
1266         cam->drwtria[1][0] = shift[0];
1267         cam->drwtria[1][1] = shift[1] + ((1.1f * drawsize * (asp[1] + 0.7f)) * scale[1]);
1268
1269         if (look_through) {
1270                 /* Only draw the frame. */
1271                 DRW_shgroup_call_dynamic_add(
1272                         stl->g_data->camera_frame, color, cam->drwcorners,
1273                         &cam->drwdepth, cam->drwtria, ob->obmat);
1274         }
1275         else {
1276                 DRW_shgroup_call_dynamic_add(
1277                         stl->g_data->camera, color, cam->drwcorners,
1278                         &cam->drwdepth, cam->drwtria, ob->obmat);
1279
1280                 /* Active cam */
1281                 if (is_active) {
1282                         DRW_shgroup_call_dynamic_add(
1283                                 stl->g_data->camera_tria, color,
1284                                 cam->drwcorners, &cam->drwdepth, cam->drwtria, ob->obmat);
1285                 }
1286         }
1287
1288         /* draw the rest in normalize object space */
1289         copy_m4_m4(cam->drwnormalmat, ob->obmat);
1290         normalize_m4(cam->drwnormalmat);
1291
1292         if (cam->flag & CAM_SHOWLIMITS) {
1293                 static float col[3] = {0.5f, 0.5f, 0.25f}, col_hi[3] = {1.0f, 1.0f, 0.5f};
1294                 float sizemat[4][4], size[3] = {1.0f, 1.0f, 0.0f};
1295                 float focusdist = BKE_camera_object_dof_distance(ob);
1296
1297                 copy_m4_m4(cam->drwfocusmat, cam->drwnormalmat);
1298                 translate_m4(cam->drwfocusmat, 0.0f, 0.0f, -focusdist);
1299                 size_to_mat4(sizemat, size);
1300                 mul_m4_m4m4(cam->drwfocusmat, cam->drwfocusmat, sizemat);
1301
1302                 DRW_shgroup_call_dynamic_add(
1303                         stl->g_data->camera_focus, (is_active ? col_hi : col),
1304                         &cam->drawsize, cam->drwfocusmat);
1305
1306                 DRW_shgroup_call_dynamic_add(
1307                         stl->g_data->camera_clip, color,
1308                         &cam->clipsta, &cam->clipend, cam->drwnormalmat);
1309                 DRW_shgroup_call_dynamic_add(
1310                         stl->g_data->camera_clip_points, (is_active ? col_hi : col),
1311                         &cam->clipsta, &cam->clipend, cam->drwnormalmat);
1312         }
1313
1314         if (cam->flag & CAM_SHOWMIST) {
1315                 World *world = scene->world;
1316
1317                 if (world) {
1318                         static float col[3] = {0.5f, 0.5f, 0.5f}, col_hi[3] = {1.0f, 1.0f, 1.0f};
1319                         world->mistend = world->miststa + world->mistdist;
1320                         DRW_shgroup_call_dynamic_add(
1321                                 stl->g_data->camera_mist, color,
1322                                 &world->miststa, &world->mistend, cam->drwnormalmat);
1323                         DRW_shgroup_call_dynamic_add(
1324                                 stl->g_data->camera_mist_points, (is_active ? col_hi : col),
1325                                 &world->miststa, &world->mistend, cam->drwnormalmat);
1326                 }
1327         }
1328 }
1329
1330 static void DRW_shgroup_empty(OBJECT_StorageList *stl, OBJECT_PassList *psl, Object *ob, ViewLayer *view_layer)
1331 {
1332         float *color;
1333         DRW_object_wire_theme_get(ob, view_layer, &color);
1334
1335         switch (ob->empty_drawtype) {
1336                 case OB_PLAINAXES:
1337                         DRW_shgroup_call_dynamic_add(stl->g_data->plain_axes, color, &ob->empty_drawsize, ob->obmat);
1338                         break;
1339                 case OB_SINGLE_ARROW:
1340                         DRW_shgroup_call_dynamic_add(stl->g_data->single_arrow, color, &ob->empty_drawsize, ob->obmat);
1341                         DRW_shgroup_call_dynamic_add(stl->g_data->single_arrow_line, color, &ob->empty_drawsize, ob->obmat);
1342                         break;
1343                 case OB_CUBE:
1344                         DRW_shgroup_call_dynamic_add(stl->g_data->cube, color, &ob->empty_drawsize, ob->obmat);
1345                         break;
1346                 case OB_CIRCLE:
1347                         DRW_shgroup_call_dynamic_add(stl->g_data->circle, color, &ob->empty_drawsize, ob->obmat);
1348                         break;
1349                 case OB_EMPTY_SPHERE:
1350                         DRW_shgroup_call_dynamic_add(stl->g_data->sphere, color, &ob->empty_drawsize, ob->obmat);
1351                         break;
1352                 case OB_EMPTY_CONE:
1353                         DRW_shgroup_call_dynamic_add(stl->g_data->cone, color, &ob->empty_drawsize, ob->obmat);
1354                         break;
1355                 case OB_ARROWS:
1356                         DRW_shgroup_call_dynamic_add(stl->g_data->arrows, color, &ob->empty_drawsize, ob->obmat);
1357                         DRW_shgroup_call_dynamic_add(stl->g_data->axis_names, color, &ob->empty_drawsize, ob->obmat);
1358                         break;
1359                 case OB_EMPTY_IMAGE:
1360                         DRW_shgroup_empty_image(stl, psl, ob, color);
1361                         break;
1362         }
1363 }
1364
1365 static void DRW_shgroup_forcefield(OBJECT_StorageList *stl, Object *ob, ViewLayer *view_layer)
1366 {
1367         int theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
1368         float *color = DRW_color_background_blend_get(theme_id);
1369         PartDeflect *pd = ob->pd;
1370         Curve *cu = (ob->type == OB_CURVE) ? ob->data : NULL;
1371
1372         /* TODO Move this to depsgraph */
1373         float tmp[3];
1374         copy_v3_fl(pd->drawvec1, ob->empty_drawsize);
1375
1376         switch (pd->forcefield) {
1377                 case PFIELD_WIND:
1378                         pd->drawvec1[2] = pd->f_strength;
1379                         break;
1380                 case PFIELD_VORTEX:
1381                         if (pd->f_strength < 0.0f) {
1382                                 pd->drawvec1[1] = -pd->drawvec1[1];
1383                         }
1384                         break;
1385                 case PFIELD_GUIDE:
1386                         if (cu && (cu->flag & CU_PATH) && ob->curve_cache->path && ob->curve_cache->path->data) {
1387                                 where_on_path(ob, 0.0f, pd->drawvec1, tmp, NULL, NULL, NULL);
1388                                 where_on_path(ob, 1.0f, pd->drawvec2, tmp, NULL, NULL, NULL);
1389                         }
1390                         break;
1391         }
1392
1393         if (pd->falloff == PFIELD_FALL_TUBE) {
1394                 pd->drawvec_falloff_max[0] = pd->drawvec_falloff_max[1] = (pd->flag & PFIELD_USEMAXR) ? pd->maxrad : 1.0f;
1395                 pd->drawvec_falloff_max[2] = (pd->flag & PFIELD_USEMAX) ? pd->maxdist : 0.0f;
1396
1397                 pd->drawvec_falloff_min[0] = pd->drawvec_falloff_min[1] = (pd->flag & PFIELD_USEMINR) ? pd->minrad : 1.0f;
1398                 pd->drawvec_falloff_min[2] = (pd->flag & PFIELD_USEMIN) ? pd->mindist : 0.0f;
1399         }
1400         else if (pd->falloff == PFIELD_FALL_CONE) {
1401                 float radius, distance;
1402
1403                 radius = DEG2RADF((pd->flag & PFIELD_USEMAXR) ? pd->maxrad : 1.0f);
1404                 distance = (pd->flag & PFIELD_USEMAX) ? pd->maxdist : 0.0f;
1405                 pd->drawvec_falloff_max[0] = pd->drawvec_falloff_max[1] = distance * sinf(radius);
1406                 pd->drawvec_falloff_max[2] = distance * cosf(radius);
1407
1408                 radius = DEG2RADF((pd->flag & PFIELD_USEMINR) ? pd->minrad : 1.0f);
1409                 distance = (pd->flag & PFIELD_USEMIN) ? pd->mindist : 0.0f;
1410
1411                 pd->drawvec_falloff_min[0] = pd->drawvec_falloff_min[1] = distance * sinf(radius);
1412                 pd->drawvec_falloff_min[2] = distance * cosf(radius);
1413         }
1414         /* End of things that should go to depthgraph */
1415
1416         switch (pd->forcefield) {
1417                 case PFIELD_WIND:
1418                         DRW_shgroup_call_dynamic_add(stl->g_data->field_wind, color, &pd->drawvec1, ob->obmat);
1419                         break;
1420                 case PFIELD_FORCE:
1421                         DRW_shgroup_call_dynamic_add(stl->g_data->field_force, color, &pd->drawvec1, ob->obmat);
1422                         break;
1423                 case PFIELD_VORTEX:
1424                         DRW_shgroup_call_dynamic_add(stl->g_data->field_vortex, color, &pd->drawvec1, ob->obmat);
1425                         break;
1426                 case PFIELD_GUIDE:
1427                         if (cu && (cu->flag & CU_PATH) && ob->curve_cache->path && ob->curve_cache->path->data) {
1428                                 DRW_shgroup_call_dynamic_add(stl->g_data->field_curve_sta, color, &pd->f_strength, ob->obmat);
1429                                 DRW_shgroup_call_dynamic_add(stl->g_data->field_curve_end, color, &pd->f_strength, ob->obmat);
1430                         }
1431                         break;
1432         }
1433
1434         if (pd->falloff == PFIELD_FALL_SPHERE) {
1435                 /* as last, guide curve alters it */
1436                 if ((pd->flag & PFIELD_USEMAX) != 0) {
1437                         DRW_shgroup_call_dynamic_add(stl->g_data->field_curve_end, color, &pd->maxdist, ob->obmat);
1438                 }
1439
1440                 if ((pd->flag & PFIELD_USEMIN) != 0) {
1441                         DRW_shgroup_call_dynamic_add(stl->g_data->field_curve_end, color, &pd->mindist, ob->obmat);
1442                 }
1443         }
1444         else if (pd->falloff == PFIELD_FALL_TUBE) {
1445                 if (pd->flag & (PFIELD_USEMAX | PFIELD_USEMAXR)) {
1446                         DRW_shgroup_call_dynamic_add(stl->g_data->field_tube_limit, color, &pd->drawvec_falloff_max, ob->obmat);
1447                 }
1448
1449                 if (pd->flag & (PFIELD_USEMIN | PFIELD_USEMINR)) {
1450                         DRW_shgroup_call_dynamic_add(stl->g_data->field_tube_limit, color, &pd->drawvec_falloff_min, ob->obmat);
1451                 }
1452         }
1453         else if (pd->falloff == PFIELD_FALL_CONE) {
1454                 if (pd->flag & (PFIELD_USEMAX | PFIELD_USEMAXR)) {
1455                         DRW_shgroup_call_dynamic_add(stl->g_data->field_cone_limit, color, &pd->drawvec_falloff_max, ob->obmat);
1456                 }
1457
1458                 if (pd->flag & (PFIELD_USEMIN | PFIELD_USEMINR)) {
1459                         DRW_shgroup_call_dynamic_add(stl->g_data->field_cone_limit, color, &pd->drawvec_falloff_min, ob->obmat);
1460                 }
1461         }
1462 }
1463
1464 static void DRW_shgroup_speaker(OBJECT_StorageList *stl, Object *ob, ViewLayer *view_layer)
1465 {
1466         float *color;
1467         static float one = 1.0f;
1468         DRW_object_wire_theme_get(ob, view_layer, &color);
1469
1470         DRW_shgroup_call_dynamic_add(stl->g_data->speaker, color, &one, ob->obmat);
1471 }
1472
1473 typedef struct OBJECT_LightProbeEngineData {
1474         ObjectEngineData engine_data;
1475
1476         float prb_mats[6][4][4];
1477         float probe_cube_mat[4][4];
1478         float draw_size;
1479         float increment_x[3];
1480         float increment_y[3];
1481         float increment_z[3];
1482         float corner[3];
1483 } OBJECT_LightProbeEngineData;
1484
1485 static void DRW_shgroup_lightprobe(OBJECT_StorageList *stl, OBJECT_PassList *psl, Object *ob, ViewLayer *view_layer)
1486 {
1487         float *color;
1488         static float one = 1.0f;
1489         LightProbe *prb = (LightProbe *)ob->data;
1490         bool do_outlines = ((ob->base_flag & BASE_SELECTED) != 0);
1491         DRW_object_wire_theme_get(ob, view_layer, &color);
1492
1493         OBJECT_LightProbeEngineData *prb_data =
1494                 (OBJECT_LightProbeEngineData *)DRW_object_engine_data_ensure(
1495                         ob,
1496                         &draw_engine_object_type,
1497                         sizeof(OBJECT_LightProbeEngineData),
1498                         NULL,
1499                         NULL);
1500
1501         if ((DRW_state_is_select() || do_outlines) && ((prb->flag & LIGHTPROBE_FLAG_SHOW_DATA) != 0)) {
1502
1503                 if (prb->type == LIGHTPROBE_TYPE_GRID) {
1504                         /* Update transforms */
1505                         float cell_dim[3], half_cell_dim[3];
1506                         cell_dim[0] = 2.0f / (float)(prb->grid_resolution_x);
1507                         cell_dim[1] = 2.0f / (float)(prb->grid_resolution_y);
1508                         cell_dim[2] = 2.0f / (float)(prb->grid_resolution_z);
1509
1510                         mul_v3_v3fl(half_cell_dim, cell_dim, 0.5f);
1511
1512                         /* First cell. */
1513                         copy_v3_fl(prb_data->corner, -1.0f);
1514                         add_v3_v3(prb_data->corner, half_cell_dim);
1515                         mul_m4_v3(ob->obmat, prb_data->corner);
1516
1517                         /* Opposite neighbor cell. */
1518                         copy_v3_fl3(prb_data->increment_x, cell_dim[0], 0.0f, 0.0f);
1519                         add_v3_v3(prb_data->increment_x, half_cell_dim);
1520                         add_v3_fl(prb_data->increment_x, -1.0f);
1521                         mul_m4_v3(ob->obmat, prb_data->increment_x);
1522                         sub_v3_v3(prb_data->increment_x, prb_data->corner);
1523
1524                         copy_v3_fl3(prb_data->increment_y, 0.0f, cell_dim[1], 0.0f);
1525                         add_v3_v3(prb_data->increment_y, half_cell_dim);
1526                         add_v3_fl(prb_data->increment_y, -1.0f);
1527                         mul_m4_v3(ob->obmat, prb_data->increment_y);
1528                         sub_v3_v3(prb_data->increment_y, prb_data->corner);
1529
1530                         copy_v3_fl3(prb_data->increment_z, 0.0f, 0.0f, cell_dim[2]);
1531                         add_v3_v3(prb_data->increment_z, half_cell_dim);
1532                         add_v3_fl(prb_data->increment_z, -1.0f);
1533                         mul_m4_v3(ob->obmat, prb_data->increment_z);
1534                         sub_v3_v3(prb_data->increment_z, prb_data->corner);
1535
1536                         DRWShadingGroup *grp = DRW_shgroup_instance_create(e_data.lightprobe_grid_sh, psl->lightprobes, DRW_cache_sphere_get());
1537                         DRW_shgroup_set_instance_count(grp, prb->grid_resolution_x * prb->grid_resolution_y * prb->grid_resolution_z);
1538                         DRW_shgroup_uniform_vec4(grp, "color", color, 1);
1539                         DRW_shgroup_uniform_vec3(grp, "corner", prb_data->corner, 1);
1540                         DRW_shgroup_uniform_vec3(grp, "increment_x", prb_data->increment_x, 1);
1541                         DRW_shgroup_uniform_vec3(grp, "increment_y", prb_data->increment_y, 1);
1542                         DRW_shgroup_uniform_vec3(grp, "increment_z", prb_data->increment_z, 1);
1543                         DRW_shgroup_uniform_ivec3(grp, "grid_resolution", &prb->grid_resolution_x, 1);
1544                         DRW_shgroup_uniform_float(grp, "sphere_size", &prb->data_draw_size, 1);
1545                 }
1546                 else if (prb->type == LIGHTPROBE_TYPE_CUBE) {
1547                         prb_data->draw_size = prb->data_draw_size * 0.1f;
1548                         unit_m4(prb_data->probe_cube_mat);
1549                         copy_v3_v3(prb_data->probe_cube_mat[3], ob->obmat[3]);
1550                         DRW_shgroup_call_dynamic_add(stl->g_data->lightprobes_cube, color, &prb_data->draw_size, prb_data->probe_cube_mat);
1551                 }
1552                 else {
1553                         prb_data->draw_size = 1.0f;
1554                         DRW_shgroup_call_dynamic_add(stl->g_data->lightprobes_planar, color, &prb_data->draw_size, ob->obmat);
1555                 }
1556         }
1557
1558         switch (prb->type) {
1559                 case LIGHTPROBE_TYPE_PLANAR:
1560                         DRW_shgroup_call_dynamic_add(stl->g_data->probe_planar, ob->obmat[3], color);
1561                         break;
1562                 case LIGHTPROBE_TYPE_GRID:
1563                         DRW_shgroup_call_dynamic_add(stl->g_data->probe_grid, ob->obmat[3], color);
1564                         break;
1565                 case LIGHTPROBE_TYPE_CUBE:
1566                 default:
1567                         DRW_shgroup_call_dynamic_add(stl->g_data->probe_cube, ob->obmat[3], color);
1568                         break;
1569         }
1570
1571
1572
1573         if (prb->type == LIGHTPROBE_TYPE_PLANAR) {
1574                 float (*mat)[4];
1575                 mat = (float (*)[4])(prb_data->prb_mats[0]);
1576                 copy_m4_m4(mat, ob->obmat);
1577                 normalize_m4(mat);
1578
1579                 DRW_shgroup_call_dynamic_add(stl->g_data->single_arrow, color, &ob->empty_drawsize, mat);
1580                 DRW_shgroup_call_dynamic_add(stl->g_data->single_arrow_line, color, &ob->empty_drawsize, mat);
1581
1582                 mat = (float (*)[4])(prb_data->prb_mats[1]);
1583                 copy_m4_m4(mat, ob->obmat);
1584                 zero_v3(mat[2]);
1585
1586                 DRW_shgroup_call_dynamic_add(stl->g_data->cube, color, &one, mat);
1587         }
1588
1589         if ((prb->flag & LIGHTPROBE_FLAG_SHOW_INFLUENCE) != 0) {
1590
1591                 prb->distfalloff = (1.0f - prb->falloff) * prb->distinf;
1592                 prb->distgridinf = prb->distinf;
1593
1594                 if (prb->type == LIGHTPROBE_TYPE_GRID) {
1595                         prb->distfalloff += 1.0f;
1596                         prb->distgridinf += 1.0f;
1597                 }
1598
1599                 if (prb->type == LIGHTPROBE_TYPE_GRID ||
1600                     prb->attenuation_type == LIGHTPROBE_SHAPE_BOX)
1601                 {
1602                         DRW_shgroup_call_dynamic_add(stl->g_data->cube, color, &prb->distgridinf, ob->obmat);
1603                         DRW_shgroup_call_dynamic_add(stl->g_data->cube, color, &prb->distfalloff, ob->obmat);
1604                 }
1605                 else if (prb->type == LIGHTPROBE_TYPE_PLANAR) {
1606                         float (*rangemat)[4];
1607                         rangemat = (float (*)[4])(prb_data->prb_mats[2]);
1608                         copy_m4_m4(rangemat, ob->obmat);
1609                         normalize_v3(rangemat[2]);
1610                         mul_v3_fl(rangemat[2], prb->distinf);
1611
1612                         DRW_shgroup_call_dynamic_add(stl->g_data->cube, color, &one, rangemat);
1613
1614                         rangemat = (float (*)[4])(prb_data->prb_mats[3]);
1615                         copy_m4_m4(rangemat, ob->obmat);
1616                         normalize_v3(rangemat[2]);
1617                         mul_v3_fl(rangemat[2], prb->distfalloff);
1618
1619                         DRW_shgroup_call_dynamic_add(stl->g_data->cube, color, &one, rangemat);
1620                 }
1621                 else {
1622                         DRW_shgroup_call_dynamic_add(stl->g_data->sphere, color, &prb->distgridinf, ob->obmat);
1623                         DRW_shgroup_call_dynamic_add(stl->g_data->sphere, color, &prb->distfalloff, ob->obmat);
1624                 }
1625         }
1626
1627         if ((prb->flag & LIGHTPROBE_FLAG_SHOW_PARALLAX) != 0) {
1628                 if (prb->type != LIGHTPROBE_TYPE_PLANAR) {
1629                         float (*obmat)[4], *dist;
1630
1631                         if ((prb->flag & LIGHTPROBE_FLAG_CUSTOM_PARALLAX) != 0) {
1632                                 dist = &prb->distpar;
1633                                 /* TODO object parallax */
1634                                 obmat = ob->obmat;
1635                         }
1636                         else {
1637                                 dist = &prb->distinf;
1638                                 obmat = ob->obmat;
1639                         }
1640
1641                         if (prb->parallax_type == LIGHTPROBE_SHAPE_BOX) {
1642                                 DRW_shgroup_call_dynamic_add(stl->g_data->cube, color, dist, obmat);
1643                         }
1644                         else {
1645                                 DRW_shgroup_call_dynamic_add(stl->g_data->sphere, color, dist, obmat);
1646                         }
1647                 }
1648         }
1649
1650         if ((prb->flag & LIGHTPROBE_FLAG_SHOW_CLIP_DIST) != 0) {
1651                 if (prb->type != LIGHTPROBE_TYPE_PLANAR) {
1652                         static const float cubefacemat[6][4][4] = {
1653                                 {{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}},
1654                                 {{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}},
1655                                 {{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}},
1656                                 {{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}},
1657                                 {{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}},
1658                                 {{-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}},
1659                         };
1660
1661                         for (int i = 0; i < 6; ++i) {
1662                                 float (*clipmat)[4];
1663                                 clipmat = (float (*)[4])(prb_data->prb_mats[i]);
1664
1665                                 normalize_m4_m4(clipmat, ob->obmat);
1666                                 mul_m4_m4m4(clipmat, clipmat, cubefacemat[i]);
1667
1668                                 DRW_shgroup_call_dynamic_add(stl->g_data->lamp_buflimit, color, &prb->clipsta, &prb->clipend, clipmat);
1669                                 DRW_shgroup_call_dynamic_add(stl->g_data->lamp_buflimit_points, color, &prb->clipsta, &prb->clipend, clipmat);
1670                         }
1671                 }
1672         }
1673
1674         /* Line and point going to the ground */
1675         if (prb->type == LIGHTPROBE_TYPE_CUBE) {
1676                 DRW_shgroup_call_dynamic_add(stl->g_data->lamp_groundline, ob->obmat[3]);
1677                 DRW_shgroup_call_dynamic_add(stl->g_data->lamp_groundpoint, ob->obmat[3]);
1678         }
1679 }
1680
1681 static void DRW_shgroup_relationship_lines(OBJECT_StorageList *stl, Object *ob)
1682 {
1683         if (ob->parent && DRW_check_object_visible_within_active_context(ob->parent)) {
1684                 DRW_shgroup_call_dynamic_add(stl->g_data->relationship_lines, ob->obmat[3]);
1685                 DRW_shgroup_call_dynamic_add(stl->g_data->relationship_lines, ob->parent->obmat[3]);
1686         }
1687 }
1688
1689 static void DRW_shgroup_object_center(OBJECT_StorageList *stl, Object *ob, ViewLayer *view_layer, View3D *v3d)
1690 {
1691         const bool is_library = ob->id.us > 1 || ID_IS_LINKED(ob);
1692         DRWShadingGroup *shgroup;
1693
1694         if (ob == OBACT(view_layer)) {
1695                 shgroup = stl->g_data->center_active;
1696         }
1697         else if (ob->base_flag & BASE_SELECTED) {
1698                 if (is_library) {
1699                         shgroup = stl->g_data->center_selected_lib;
1700                 }
1701                 else {
1702                         shgroup = stl->g_data->center_selected;
1703                 }
1704         }
1705         else if (v3d->flag & V3D_DRAW_CENTERS) {
1706                 if (is_library) {
1707                         shgroup = stl->g_data->center_deselected_lib;
1708                 }
1709                 else {
1710                         shgroup = stl->g_data->center_deselected;
1711                 }
1712         }
1713         else {
1714                 return;
1715         }
1716
1717         DRW_shgroup_call_dynamic_add(shgroup, ob->obmat[3]);
1718 }
1719
1720 static void OBJECT_cache_populate_particles(Object *ob,
1721                                             OBJECT_PassList *psl)
1722 {
1723         for (ParticleSystem *psys = ob->particlesystem.first; psys; psys = psys->next) {
1724                 if (psys_check_enabled(ob, psys, false)) {
1725                         ParticleSettings *part = psys->part;
1726                         int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
1727
1728                         if (draw_as == PART_DRAW_PATH && !psys->pathcache && !psys->childcache) {
1729                                 draw_as = PART_DRAW_DOT;
1730                         }
1731
1732                         static float mat[4][4];
1733                         unit_m4(mat);
1734
1735                         if (draw_as != PART_DRAW_PATH) {
1736                                 struct Gwn_Batch *geom = DRW_cache_particles_get_dots(psys);
1737                                 DRWShadingGroup *shgrp = NULL;
1738                                 static int screen_space[2] = {0, 1};
1739                                 static float def_prim_col[3] = {0.5f, 0.5f, 0.5f};
1740                                 static float def_sec_col[3] = {1.0f, 1.0f, 1.0f};
1741
1742                                 Material *ma = give_current_material(ob, part->omat);
1743
1744                                 switch (draw_as) {
1745                                         case PART_DRAW_DOT:
1746                                                 shgrp = DRW_shgroup_create(e_data.part_dot_sh, psl->particle);
1747                                                 DRW_shgroup_uniform_vec3(shgrp, "color", ma ? &ma->r : def_prim_col, 1);
1748                                                 DRW_shgroup_uniform_vec3(shgrp, "outlineColor", ma ? &ma->specr : def_sec_col, 1);
1749                                                 DRW_shgroup_uniform_float(shgrp, "pixel_size", DRW_viewport_pixelsize_get(), 1);
1750                                                 DRW_shgroup_uniform_float(shgrp, "size", &part->draw_size, 1);
1751                                                 DRW_shgroup_uniform_texture(shgrp, "ramp", globals_ramp);
1752                                                 DRW_shgroup_call_add(shgrp, geom, mat);
1753                                                 break;
1754                                         case PART_DRAW_CROSS:
1755                                                 shgrp = DRW_shgroup_instance_create(
1756                                                         e_data.part_prim_sh, psl->particle, DRW_cache_particles_get_prim(PART_DRAW_CROSS));
1757                                                 DRW_shgroup_uniform_texture(shgrp, "ramp", globals_ramp);
1758                                                 DRW_shgroup_uniform_vec3(shgrp, "color", ma ? &ma->r : def_prim_col, 1);
1759                                                 DRW_shgroup_uniform_int(shgrp, "screen_space", &screen_space[0], 1);
1760                                                 break;
1761                                         case PART_DRAW_CIRC:
1762                                                 shgrp = DRW_shgroup_instance_create(
1763                                                         e_data.part_prim_sh, psl->particle, DRW_cache_particles_get_prim(PART_DRAW_CIRC));
1764                                                 DRW_shgroup_uniform_texture(shgrp, "ramp", globals_ramp);
1765                                                 DRW_shgroup_uniform_vec3(shgrp, "color", ma ? &ma->r : def_prim_col, 1);
1766                                                 DRW_shgroup_uniform_int(shgrp, "screen_space", &screen_space[1], 1);
1767                                                 break;
1768                                         case PART_DRAW_AXIS:
1769                                                 shgrp = DRW_shgroup_instance_create(
1770                                                         e_data.part_axis_sh, psl->particle, DRW_cache_particles_get_prim(PART_DRAW_AXIS));
1771                                                 DRW_shgroup_uniform_int(shgrp, "screen_space", &screen_space[0], 1);
1772                                                 break;
1773                                         default:
1774                                                 break;
1775                                 }
1776
1777                                 if (shgrp) {
1778                                         if (draw_as != PART_DRAW_DOT) {
1779                                                 DRW_shgroup_uniform_float(shgrp, "draw_size", &part->draw_size, 1);
1780                                                 DRW_shgroup_instance_batch(shgrp, geom);
1781                                         }
1782                                 }
1783                         }
1784                 }
1785         }
1786 }
1787
1788 static void OBJECT_cache_populate(void *vedata, Object *ob)
1789 {
1790         OBJECT_PassList *psl = ((OBJECT_Data *)vedata)->psl;
1791         OBJECT_StorageList *stl = ((OBJECT_Data *)vedata)->stl;
1792         const DRWContextState *draw_ctx = DRW_context_state_get();
1793         Scene *scene = draw_ctx->scene;
1794         ViewLayer *view_layer = draw_ctx->view_layer;
1795         View3D *v3d = draw_ctx->v3d;
1796         int theme_id = TH_UNDEFINED;
1797
1798         /* Handle particles first in case the emitter itself shouldn't be rendered. */
1799         if (ob->type == OB_MESH) {
1800                 OBJECT_cache_populate_particles(ob, psl);
1801         }
1802
1803         if (DRW_check_object_visible_within_active_context(ob) == false) {
1804                 return;
1805         }
1806
1807         //CollectionEngineSettings *ces_mode_ob = BKE_layer_collection_engine_evaluated_get(ob, COLLECTION_MODE_OBJECT, "");
1808
1809         //bool do_wire = BKE_collection_engine_property_value_get_bool(ces_mode_ob, "show_wire");
1810         bool do_outlines = ((ob->base_flag & BASE_SELECTED) != 0);
1811
1812         if (do_outlines) {
1813                 Object *obedit = scene->obedit;
1814                 if (ob != obedit && !((ob == draw_ctx->obact) && (draw_ctx->object_mode & OB_MODE_ALL_PAINT))) {
1815                         struct Gwn_Batch *geom = DRW_cache_object_surface_get(ob);
1816                         if (geom) {
1817                                 theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
1818                                 DRWShadingGroup *shgroup = shgroup_theme_id_to_outline_or(stl, theme_id, NULL);
1819                                 if (shgroup != NULL) {
1820                                         DRW_shgroup_call_add(shgroup, geom, ob->obmat);
1821                                 }
1822                         }
1823                 }
1824         }
1825
1826         switch (ob->type) {
1827                 case OB_MESH:
1828                 {
1829                         Mesh *me = ob->data;
1830                         if (me->totpoly == 0) {
1831                                 Object *obedit = scene->obedit;
1832                                 if (ob != obedit) {
1833                                         struct Gwn_Batch *geom = DRW_cache_mesh_edges_get(ob);
1834                                         if (geom) {
1835                                                 if (theme_id == TH_UNDEFINED) {
1836                                                         theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
1837                                                 }
1838
1839                                                 DRWShadingGroup *shgroup = shgroup_theme_id_to_wire_or(stl, theme_id, stl->g_data->wire);
1840                                                 DRW_shgroup_call_add(shgroup, geom, ob->obmat);
1841                                         }
1842                                 }
1843                         }
1844                         break;
1845                 }
1846                 case OB_SURF:
1847                         break;
1848                 case OB_LATTICE:
1849                 {
1850                         Object *obedit = scene->obedit;
1851                         if (ob != obedit) {
1852                                 struct Gwn_Batch *geom = DRW_cache_lattice_wire_get(ob, false);
1853                                 if (theme_id == TH_UNDEFINED) {
1854                                         theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
1855                                 }
1856
1857                                 DRWShadingGroup *shgroup = shgroup_theme_id_to_wire_or(stl, theme_id, stl->g_data->wire);
1858                                 DRW_shgroup_call_add(shgroup, geom, ob->obmat);
1859                         }
1860                         break;
1861                 }
1862
1863                 case OB_CURVE:
1864                 {
1865                         Object *obedit = scene->obedit;
1866                         if (ob != obedit) {
1867                                 struct Gwn_Batch *geom = DRW_cache_curve_edge_wire_get(ob);
1868                                 if (theme_id == TH_UNDEFINED) {
1869                                         theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
1870                                 }
1871                                 DRWShadingGroup *shgroup = shgroup_theme_id_to_wire_or(stl, theme_id, stl->g_data->wire);
1872                                 DRW_shgroup_call_add(shgroup, geom, ob->obmat);
1873                         }
1874                         break;
1875                 }
1876                 case OB_MBALL:
1877                 {
1878                         Object *obedit = scene->obedit;
1879                         if (ob != obedit) {
1880                                 DRW_shgroup_mball_helpers(stl, ob, view_layer);
1881                         }
1882                         break;
1883                 }
1884                 case OB_LAMP:
1885                         DRW_shgroup_lamp(stl, ob, view_layer);
1886                         break;
1887                 case OB_CAMERA:
1888                         DRW_shgroup_camera(stl, ob, view_layer);
1889                         break;
1890                 case OB_EMPTY:
1891                         DRW_shgroup_empty(stl, psl, ob, view_layer);
1892                         break;
1893                 case OB_SPEAKER:
1894                         DRW_shgroup_speaker(stl, ob, view_layer);
1895                         break;
1896                 case OB_LIGHTPROBE:
1897                         DRW_shgroup_lightprobe(stl, psl, ob, view_layer);
1898                         break;
1899                 case OB_ARMATURE:
1900                 {
1901                         bArmature *arm = ob->data;
1902                         if (arm->edbo == NULL) {
1903                                 if (DRW_state_is_select() || !DRW_pose_mode_armature(ob, draw_ctx->obact)) {
1904                                         DRW_shgroup_armature_object(
1905                                                 ob, view_layer, psl->bone_solid, psl->bone_wire, psl->bone_envelope,
1906                                                 stl->g_data->relationship_lines);
1907                                 }
1908                         }
1909                         break;
1910                 }
1911                 default:
1912                         break;
1913         }
1914
1915         if (ob->pd && ob->pd->forcefield) {
1916                 DRW_shgroup_forcefield(stl, ob, view_layer);
1917         }
1918
1919         /* don't show object extras in set's */
1920         if ((ob->base_flag & (BASE_FROM_SET | BASE_FROMDUPLI)) == 0) {
1921
1922                 DRW_shgroup_object_center(stl, ob, view_layer, v3d);
1923
1924                 DRW_shgroup_relationship_lines(stl, ob);
1925
1926                 if ((ob->dtx & OB_DRAWNAME) && DRW_state_show_text()) {
1927                         struct DRWTextStore *dt = DRW_text_cache_ensure();
1928                         if (theme_id == TH_UNDEFINED) {
1929                                 theme_id = DRW_object_wire_theme_get(ob, view_layer, NULL);
1930                         }
1931
1932                         unsigned char color[4];
1933                         UI_GetThemeColor4ubv(theme_id, color);
1934
1935                         DRW_text_cache_add(
1936                                 dt, ob->obmat[3],
1937                                 ob->id.name + 2, strlen(ob->id.name + 2),
1938                                 10, DRW_TEXT_CACHE_GLOBALSPACE | DRW_TEXT_CACHE_STRING_PTR, color);
1939                 }
1940         }
1941 }
1942
1943 static void OBJECT_draw_scene(void *vedata)
1944 {
1945
1946         OBJECT_PassList *psl = ((OBJECT_Data *)vedata)->psl;
1947         OBJECT_StorageList *stl = ((OBJECT_Data *)vedata)->stl;
1948         OBJECT_FramebufferList *fbl = ((OBJECT_Data *)vedata)->fbl;
1949         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
1950         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
1951
1952         float clearcol[4] = {0.0f, 0.0f, 0.0f, 0.0f};
1953
1954         if (DRW_state_is_fbo()) {
1955                 DRW_stats_group_start("Outlines");
1956                 /* attach temp textures */
1957                 DRW_framebuffer_texture_attach(fbl->outlines, e_data.outlines_depth_tx, 0, 0);
1958                 DRW_framebuffer_texture_attach(fbl->outlines, e_data.outlines_color_tx, 0, 0);
1959                 DRW_framebuffer_texture_attach(fbl->blur, e_data.outlines_blur_tx, 0, 0);
1960                 
1961                 /* Render filled polygon on a separate framebuffer */
1962                 DRW_framebuffer_bind(fbl->outlines);
1963                 DRW_framebuffer_clear(true, true, false, clearcol, 1.0f);
1964                 DRW_draw_pass(psl->outlines);
1965                 DRW_draw_pass(psl->lightprobes);
1966
1967                 /* detach textures */
1968                 DRW_framebuffer_texture_detach(e_data.outlines_depth_tx);
1969
1970                 /* Search outline pixels */
1971                 DRW_framebuffer_bind(fbl->blur);
1972                 DRW_draw_pass(psl->outlines_search);
1973
1974                 /* Expand outline to form a 3px wide line */
1975                 DRW_framebuffer_bind(fbl->outlines);
1976                 DRW_draw_pass(psl->outlines_expand);
1977
1978                 /* Bleed color so the AA can do it's stuff */
1979                 DRW_framebuffer_bind(fbl->blur);
1980                 DRW_draw_pass(psl->outlines_bleed);
1981
1982                 /* detach temp textures */
1983                 DRW_framebuffer_texture_detach(e_data.outlines_color_tx);
1984                 DRW_framebuffer_texture_detach(e_data.outlines_blur_tx);
1985
1986                 /* restore main framebuffer */
1987                 DRW_framebuffer_bind(dfbl->default_fb);
1988                 DRW_stats_group_end();
1989         }
1990         else if (DRW_state_is_select()) {
1991                 /* Render probes spheres/planes so we can select them. */
1992                 DRW_draw_pass(psl->lightprobes);
1993         }
1994
1995         MULTISAMPLE_SYNC_ENABLE(dfbl)
1996
1997         /* This needs to be drawn after the oultine */
1998 //      DRW_draw_pass(psl->bone_envelope);  /* Never drawn in Object mode currently. */
1999         DRW_draw_pass(psl->bone_wire);
2000         DRW_draw_pass(psl->bone_solid);
2001         DRW_draw_pass(psl->non_meshes);
2002         DRW_draw_pass(psl->particle);
2003         DRW_draw_pass(psl->reference_image);
2004
2005         MULTISAMPLE_SYNC_DISABLE(dfbl)
2006
2007         DRW_draw_pass(psl->ob_center);
2008
2009         if (DRW_state_is_fbo()) {
2010                 if (e_data.draw_grid) {
2011                         DRW_framebuffer_texture_detach(dtxl->depth);
2012                         DRW_draw_pass(psl->grid);
2013                         DRW_framebuffer_texture_attach(dfbl->default_fb, dtxl->depth, 0, 0);
2014                 }
2015
2016                 /* Combine with scene buffer last */
2017                 DRW_draw_pass(psl->outlines_resolve);
2018         }
2019
2020         /* This has to be freed only after drawing empties! */
2021         if (stl->g_data->image_plane_map) {
2022                 BLI_ghash_free(stl->g_data->image_plane_map, NULL, MEM_freeN);
2023         }
2024 }
2025
2026 void OBJECT_collection_settings_create(IDProperty *props)
2027 {
2028         BLI_assert(props &&
2029                    props->type == IDP_GROUP &&
2030                    props->subtype == IDP_GROUP_SUB_MODE_OBJECT);
2031         BKE_collection_engine_property_add_int(props, "show_wire", false);
2032         BKE_collection_engine_property_add_int(props, "show_backface_culling", false);
2033 }
2034
2035 static const DrawEngineDataSize OBJECT_data_size = DRW_VIEWPORT_DATA_SIZE(OBJECT_Data);
2036
2037 DrawEngineType draw_engine_object_type = {
2038         NULL, NULL,
2039         N_("ObjectMode"),
2040         &OBJECT_data_size,
2041         &OBJECT_engine_init,
2042         &OBJECT_engine_free,
2043         &OBJECT_cache_init,
2044         &OBJECT_cache_populate,
2045         NULL,
2046         NULL,
2047         &OBJECT_draw_scene,
2048         NULL,
2049         NULL,
2050 };