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