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