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