Fix T56657: Crash when creating a collection instance containing probes
[blender.git] / source / blender / draw / modes / edit_mesh_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/edit_mesh_mode.c
23  *  \ingroup draw
24  */
25
26 #include "DRW_engine.h"
27 #include "DRW_render.h"
28
29 #include "GPU_shader.h"
30
31 #include "DNA_mesh_types.h"
32 #include "DNA_view3d_types.h"
33
34 #include "draw_common.h"
35
36 #include "draw_cache_impl.h"
37 #include "draw_mode_engines.h"
38
39 #include "edit_mesh_mode_intern.h" /* own include */
40
41 #include "BKE_object.h"
42
43 #include "BLI_dynstr.h"
44
45
46 extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
47 extern struct GlobalsUboStorage ts; /* draw_common.c */
48
49 extern char datatoc_edit_mesh_overlay_common_lib_glsl[];
50 extern char datatoc_edit_mesh_overlay_frag_glsl[];
51 extern char datatoc_edit_mesh_overlay_vert_glsl[];
52 extern char datatoc_edit_mesh_overlay_geom_tri_glsl[];
53 extern char datatoc_edit_mesh_overlay_geom_edge_glsl[];
54 extern char datatoc_edit_mesh_overlay_loosevert_vert_glsl[];
55 extern char datatoc_edit_mesh_overlay_facedot_frag_glsl[];
56 extern char datatoc_edit_mesh_overlay_facedot_vert_glsl[];
57 extern char datatoc_edit_mesh_overlay_ghost_clear_vert_glsl[];
58 extern char datatoc_edit_mesh_overlay_mix_frag_glsl[];
59 extern char datatoc_edit_mesh_overlay_facefill_vert_glsl[];
60 extern char datatoc_edit_mesh_overlay_facefill_frag_glsl[];
61 extern char datatoc_edit_normals_vert_glsl[];
62 extern char datatoc_edit_normals_geom_glsl[];
63 extern char datatoc_common_globals_lib_glsl[];
64
65 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
66
67 /* *********** LISTS *********** */
68 typedef struct EDIT_MESH_PassList {
69         struct DRWPass *vcolor_faces;
70         struct DRWPass *depth_hidden_wire;
71         struct DRWPass *ghost_clear_depth;
72         struct DRWPass *edit_face_overlay;
73         struct DRWPass *edit_face_occluded;
74         struct DRWPass *mix_occlude;
75         struct DRWPass *facefill_occlude;
76         struct DRWPass *normals;
77 } EDIT_MESH_PassList;
78
79 typedef struct EDIT_MESH_FramebufferList {
80         struct GPUFrameBuffer *occlude_wire_fb;
81         struct GPUFrameBuffer *ghost_wire_fb;
82 } EDIT_MESH_FramebufferList;
83
84 typedef struct EDIT_MESH_StorageList {
85         struct EDIT_MESH_PrivateData *g_data;
86 } EDIT_MESH_StorageList;
87
88 typedef struct EDIT_MESH_Data {
89         void *engine_type;
90         EDIT_MESH_FramebufferList *fbl;
91         DRWViewportEmptyList *txl;
92         EDIT_MESH_PassList *psl;
93         EDIT_MESH_StorageList *stl;
94 } EDIT_MESH_Data;
95
96 /* *********** STATIC *********** */
97 #define MAX_SHADERS 16
98
99 static struct {
100         /* weight/vert-color */
101         GPUShader *vcolor_face_shader;
102
103         /* Geometry */
104         GPUShader *overlay_tri_sh_cache[MAX_SHADERS];
105         GPUShader *overlay_loose_edge_sh_cache[MAX_SHADERS];
106
107         GPUShader *overlay_vert_sh;
108         GPUShader *overlay_facedot_sh;
109         GPUShader *overlay_mix_sh;
110         GPUShader *overlay_facefill_sh;
111         GPUShader *normals_face_sh;
112         GPUShader *normals_loop_sh;
113         GPUShader *normals_sh;
114         GPUShader *depth_sh;
115         GPUShader *ghost_clear_depth_sh;
116         /* temp buffer texture */
117         struct GPUTexture *occlude_wire_depth_tx;
118         struct GPUTexture *occlude_wire_color_tx;
119 } e_data = {NULL}; /* Engine data */
120
121 typedef struct EDIT_MESH_PrivateData {
122         /* weight/vert-color */
123         DRWShadingGroup *fvcolor_shgrp;
124         DRWShadingGroup *depth_shgrp_hidden_wire;
125
126         DRWShadingGroup *fnormals_shgrp;
127         DRWShadingGroup *vnormals_shgrp;
128         DRWShadingGroup *lnormals_shgrp;
129
130         DRWShadingGroup *face_overlay_shgrp;
131         DRWShadingGroup *ledges_overlay_shgrp;
132         DRWShadingGroup *lverts_overlay_shgrp;
133         DRWShadingGroup *facedot_overlay_shgrp;
134
135         DRWShadingGroup *face_occluded_shgrp;
136         DRWShadingGroup *ledges_occluded_shgrp;
137         DRWShadingGroup *lverts_occluded_shgrp;
138         DRWShadingGroup *facedot_occluded_shgrp;
139         DRWShadingGroup *facefill_occluded_shgrp;
140
141         int data_mask[4];
142         int ghost_ob;
143         int edit_ob;
144         bool do_zbufclip;
145         bool do_faces;
146         bool do_edges;
147         float edge_width_scale;
148 } EDIT_MESH_PrivateData; /* Transient data */
149
150 /* *********** FUNCTIONS *********** */
151 static int EDIT_MESH_sh_index(ToolSettings *tsettings, RegionView3D *rv3d, bool supports_fast_mode)
152 {
153         int result = tsettings->selectmode << 1;
154         if (supports_fast_mode) {
155                 SET_FLAG_FROM_TEST(result, (rv3d->rflag & RV3D_NAVIGATING), 1 << 0);
156         }
157         return result;
158 }
159
160 static char *EDIT_MESH_sh_defines(ToolSettings *tsettings, RegionView3D *rv3d, bool anti_alias, bool looseedge)
161 {
162         const int selectmode = tsettings->selectmode;
163         const int fast_mode = rv3d->rflag & RV3D_NAVIGATING;
164
165         char *str = NULL;
166         DynStr *ds = BLI_dynstr_new();
167
168         if (selectmode & SCE_SELECT_VERTEX) {
169                 BLI_dynstr_append(ds, "#define VERTEX_SELECTION\n");
170         }
171
172         if (selectmode & SCE_SELECT_EDGE) {
173                 BLI_dynstr_append(ds, "#define EDGE_SELECTION\n");
174         }
175
176         if (selectmode & SCE_SELECT_FACE) {
177                 BLI_dynstr_append(ds, "#define FACE_SELECTION\n");
178         }
179
180         if (!fast_mode) {
181                 BLI_dynstr_append(ds, "#define EDGE_FIX\n");
182         }
183
184         if (anti_alias) {
185                 BLI_dynstr_append(ds, "#define ANTI_ALIASING\n");
186         }
187
188         if (!looseedge) {
189                 BLI_dynstr_append(ds, "#define VERTEX_FACING\n");
190         }
191
192         str = BLI_dynstr_get_cstring(ds);
193         BLI_dynstr_free(ds);
194         return str;
195 }
196 static char *EDIT_MESH_sh_lib(void)
197 {
198         char *str = NULL;
199         DynStr *ds = BLI_dynstr_new();
200
201         BLI_dynstr_append(ds, datatoc_common_globals_lib_glsl);
202         BLI_dynstr_append(ds, datatoc_edit_mesh_overlay_common_lib_glsl);
203
204         str = BLI_dynstr_get_cstring(ds);
205         BLI_dynstr_free(ds);
206         return str;
207 }
208
209 static GPUShader *EDIT_MESH_ensure_shader(ToolSettings *tsettings, RegionView3D *rv3d, bool fast_mode, bool looseedge)
210 {
211         const int index = EDIT_MESH_sh_index(tsettings, rv3d, fast_mode);
212         if (looseedge) {
213                 if (!e_data.overlay_loose_edge_sh_cache[index]) {
214                         char *defines = EDIT_MESH_sh_defines(tsettings, rv3d, true, true);
215                         char *lib = EDIT_MESH_sh_lib();
216                         e_data.overlay_loose_edge_sh_cache[index] = DRW_shader_create_with_lib(
217                                 datatoc_edit_mesh_overlay_vert_glsl,
218                                 datatoc_edit_mesh_overlay_geom_edge_glsl,
219                                 datatoc_edit_mesh_overlay_frag_glsl,
220                                 lib,
221                                 defines);
222                         MEM_freeN(lib);
223                         MEM_freeN(defines);
224                 }
225                 return e_data.overlay_loose_edge_sh_cache[index];
226         }
227         else {
228                 if (!e_data.overlay_tri_sh_cache[index]) {
229                         char *defines = EDIT_MESH_sh_defines(tsettings, rv3d, true, false);
230                         char *lib = EDIT_MESH_sh_lib();
231                         e_data.overlay_tri_sh_cache[index] = DRW_shader_create_with_lib(
232                                 datatoc_edit_mesh_overlay_vert_glsl,
233                                 datatoc_edit_mesh_overlay_geom_tri_glsl,
234                                 datatoc_edit_mesh_overlay_frag_glsl,
235                                 lib,
236                                 defines);
237                         MEM_freeN(lib);
238                         MEM_freeN(defines);
239                 }
240                 return e_data.overlay_tri_sh_cache[index];
241         }
242 }
243
244 static void EDIT_MESH_engine_init(void *vedata)
245 {
246         EDIT_MESH_FramebufferList *fbl = ((EDIT_MESH_Data *)vedata)->fbl;
247
248         const float *viewport_size = DRW_viewport_size_get();
249         const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
250
251         e_data.occlude_wire_depth_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_DEPTH_COMPONENT24,
252                                                                  &draw_engine_edit_mesh_type);
253         e_data.occlude_wire_color_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RGBA8,
254                                                                  &draw_engine_edit_mesh_type);
255
256         GPU_framebuffer_ensure_config(&fbl->occlude_wire_fb, {
257                 GPU_ATTACHMENT_TEXTURE(e_data.occlude_wire_depth_tx),
258                 GPU_ATTACHMENT_TEXTURE(e_data.occlude_wire_color_tx)
259         });
260
261         if (!e_data.vcolor_face_shader) {
262                 e_data.vcolor_face_shader = GPU_shader_get_builtin_shader(GPU_SHADER_SIMPLE_LIGHTING_SMOOTH_COLOR_ALPHA);
263         }
264
265         if (!e_data.overlay_vert_sh) {
266                 char *lib = EDIT_MESH_sh_lib();
267                 e_data.overlay_vert_sh = DRW_shader_create_with_lib(
268                         datatoc_edit_mesh_overlay_loosevert_vert_glsl, NULL,
269                         datatoc_edit_mesh_overlay_frag_glsl,
270                         lib,
271                         "#define VERTEX_SELECTION\n");
272                 MEM_freeN(lib);
273         }
274         if (!e_data.overlay_facedot_sh) {
275                 e_data.overlay_facedot_sh = DRW_shader_create_with_lib(
276                         datatoc_edit_mesh_overlay_facedot_vert_glsl, NULL,
277                         datatoc_edit_mesh_overlay_facedot_frag_glsl,
278                         datatoc_common_globals_lib_glsl,
279                         "#define VERTEX_FACING\n");
280         }
281         if (!e_data.overlay_mix_sh) {
282                 e_data.overlay_mix_sh = DRW_shader_create_fullscreen(datatoc_edit_mesh_overlay_mix_frag_glsl, NULL);
283         }
284         if (!e_data.overlay_facefill_sh) {
285                 e_data.overlay_facefill_sh = DRW_shader_create_with_lib(
286                         datatoc_edit_mesh_overlay_facefill_vert_glsl, NULL,
287                         datatoc_edit_mesh_overlay_facefill_frag_glsl,
288                         datatoc_common_globals_lib_glsl, NULL);
289         }
290         if (!e_data.normals_face_sh) {
291                 e_data.normals_face_sh = DRW_shader_create(
292                         datatoc_edit_normals_vert_glsl,
293                         datatoc_edit_normals_geom_glsl,
294                         datatoc_gpu_shader_uniform_color_frag_glsl,
295                         "#define FACE_NORMALS\n");
296         }
297         if (!e_data.normals_loop_sh) {
298                 e_data.normals_loop_sh = DRW_shader_create(
299                         datatoc_edit_normals_vert_glsl,
300                         datatoc_edit_normals_geom_glsl,
301                         datatoc_gpu_shader_uniform_color_frag_glsl,
302                         "#define LOOP_NORMALS\n");
303         }
304         if (!e_data.normals_sh) {
305                 e_data.normals_sh = DRW_shader_create(
306                         datatoc_edit_normals_vert_glsl,
307                         datatoc_edit_normals_geom_glsl,
308                         datatoc_gpu_shader_uniform_color_frag_glsl, NULL);
309         }
310         if (!e_data.depth_sh) {
311                 e_data.depth_sh = DRW_shader_create_3D_depth_only();
312         }
313         if (!e_data.ghost_clear_depth_sh) {
314                 e_data.ghost_clear_depth_sh = DRW_shader_create(datatoc_edit_mesh_overlay_ghost_clear_vert_glsl,
315                                                                 NULL, NULL, NULL);
316         }
317
318 }
319
320 static DRWPass *edit_mesh_create_overlay_pass(
321         float *face_alpha, float *edge_width_scale, int *data_mask, bool do_edges,
322         DRWState statemod,
323         DRWShadingGroup **r_face_shgrp, DRWShadingGroup **r_ledges_shgrp,
324         DRWShadingGroup **r_lverts_shgrp, DRWShadingGroup **r_facedot_shgrp)
325 {
326         GPUShader *tri_sh, *ledge_sh;
327         const DRWContextState *draw_ctx = DRW_context_state_get();
328         RegionView3D *rv3d = draw_ctx->rv3d;
329         Scene *scene = draw_ctx->scene;
330         ToolSettings *tsettings = scene->toolsettings;
331
332         ledge_sh = EDIT_MESH_ensure_shader(tsettings, rv3d, false, true);
333         tri_sh = EDIT_MESH_ensure_shader(tsettings, rv3d, true, false);
334
335         DRWPass *pass = DRW_pass_create(
336                 "Edit Mesh Face Overlay Pass",
337                 DRW_STATE_WRITE_COLOR | DRW_STATE_POINT | statemod);
338
339         *r_face_shgrp = DRW_shgroup_create(tri_sh, pass);
340         DRW_shgroup_uniform_block(*r_face_shgrp, "globalsBlock", globals_ubo);
341         DRW_shgroup_uniform_vec2(*r_face_shgrp, "viewportSize", DRW_viewport_size_get(), 1);
342         DRW_shgroup_uniform_float(*r_face_shgrp, "faceAlphaMod", face_alpha, 1);
343         DRW_shgroup_uniform_float(*r_face_shgrp, "edgeScale", edge_width_scale, 1);
344         DRW_shgroup_uniform_ivec4(*r_face_shgrp, "dataMask", data_mask, 1);
345         DRW_shgroup_uniform_bool_copy(*r_face_shgrp, "doEdges", do_edges);
346
347         *r_ledges_shgrp = DRW_shgroup_create(ledge_sh, pass);
348         DRW_shgroup_uniform_block(*r_ledges_shgrp, "globalsBlock", globals_ubo);
349         DRW_shgroup_uniform_vec2(*r_ledges_shgrp, "viewportSize", DRW_viewport_size_get(), 1);
350         DRW_shgroup_uniform_float(*r_ledges_shgrp, "edgeScale", edge_width_scale, 1);
351         DRW_shgroup_uniform_ivec4(*r_ledges_shgrp, "dataMask", data_mask, 1);
352         DRW_shgroup_uniform_bool_copy(*r_ledges_shgrp, "doEdges", do_edges);
353
354         if ((tsettings->selectmode & SCE_SELECT_VERTEX) != 0) {
355                 *r_lverts_shgrp = DRW_shgroup_create(e_data.overlay_vert_sh, pass);
356                 DRW_shgroup_uniform_block(*r_lverts_shgrp, "globalsBlock", globals_ubo);
357                 DRW_shgroup_uniform_vec2(*r_lverts_shgrp, "viewportSize", DRW_viewport_size_get(), 1);
358                 DRW_shgroup_uniform_float(*r_lverts_shgrp, "edgeScale", edge_width_scale, 1);
359         }
360
361         if ((tsettings->selectmode & SCE_SELECT_FACE) != 0) {
362                 *r_facedot_shgrp = DRW_shgroup_create(e_data.overlay_facedot_sh, pass);
363                 DRW_shgroup_uniform_block(*r_facedot_shgrp, "globalsBlock", globals_ubo);
364                 DRW_shgroup_uniform_float(*r_facedot_shgrp, "edgeScale", edge_width_scale, 1);
365         }
366
367         return pass;
368 }
369
370 static float backwire_opacity;
371 static float face_mod;
372 static float size_normal;
373
374 static void EDIT_MESH_cache_init(void *vedata)
375 {
376         EDIT_MESH_PassList *psl = ((EDIT_MESH_Data *)vedata)->psl;
377         EDIT_MESH_StorageList *stl = ((EDIT_MESH_Data *)vedata)->stl;
378         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
379
380         const DRWContextState *draw_ctx = DRW_context_state_get();
381         View3D *v3d = draw_ctx->v3d;
382         Scene *scene = draw_ctx->scene;
383         ToolSettings *tsettings = scene->toolsettings;
384
385         static float zero = 0.0f;
386
387         if (!stl->g_data) {
388                 /* Alloc transient pointers */
389                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
390         }
391         stl->g_data->ghost_ob = 0;
392         stl->g_data->edit_ob = 0;
393         stl->g_data->do_faces = true;
394         stl->g_data->do_edges = true;
395
396         const bool xray_enabled = ((draw_ctx->v3d->shading.flag & V3D_SHADING_XRAY) != 0) &&
397                                    (draw_ctx->v3d->shading.type < OB_MATERIAL);
398         stl->g_data->do_zbufclip = ((v3d->flag & V3D_ZBUF_SELECT) == 0) || xray_enabled;
399
400         /* Applies on top of the theme edge width, so edge-mode can have thick edges. */
401         stl->g_data->edge_width_scale = (tsettings->selectmode & (SCE_SELECT_EDGE)) ? 1.75f : 1.0f;
402
403         stl->g_data->data_mask[0] = 0xFF; /* Face Flag */
404         stl->g_data->data_mask[1] = 0xFF; /* Edge Flag */
405         stl->g_data->data_mask[2] = 0xFF; /* Crease */
406         stl->g_data->data_mask[3] = 0xFF; /* BWeight */
407
408         if (draw_ctx->object_edit->type == OB_MESH) {
409                 if (BKE_object_is_in_editmode(draw_ctx->object_edit)) {
410                         const Mesh *me = draw_ctx->object_edit->data;
411                         if ((me->drawflag & ME_DRAW_FREESTYLE_FACE) == 0) {
412                                 stl->g_data->data_mask[0] &= ~VFLAG_FACE_FREESTYLE;
413                         }
414                         if ((me->drawflag & ME_DRAWFACES) == 0) {
415                                 stl->g_data->data_mask[0] &= ~(VFLAG_FACE_SELECTED & VFLAG_FACE_FREESTYLE);
416                                 stl->g_data->do_faces = false;
417                         }
418                         if ((me->drawflag & ME_DRAWSEAMS) == 0) {
419                                 stl->g_data->data_mask[1] &= ~VFLAG_EDGE_SEAM;
420                         }
421                         if ((me->drawflag & ME_DRAWSHARP) == 0) {
422                                 stl->g_data->data_mask[1] &= ~VFLAG_EDGE_SHARP;
423                         }
424                         if ((me->drawflag & ME_DRAW_FREESTYLE_EDGE) == 0) {
425                                 stl->g_data->data_mask[1] &= ~VFLAG_EDGE_FREESTYLE;
426                         }
427                         if ((me->drawflag & ME_DRAWEDGES) == 0) {
428                                 if ((tsettings->selectmode & SCE_SELECT_EDGE) == 0) {
429                                         stl->g_data->data_mask[1] &= ~(VFLAG_EDGE_ACTIVE & VFLAG_EDGE_SELECTED);
430                                         stl->g_data->do_edges = false;
431                                 }
432                         }
433                         if ((me->drawflag & ME_DRAWCREASES) == 0) {
434                                 stl->g_data->data_mask[2] = 0x0;
435                         }
436                         if ((me->drawflag & ME_DRAWBWEIGHTS) == 0) {
437                                 stl->g_data->data_mask[3] = 0x0;
438                         }
439                 }
440         }
441
442         {
443                 psl->vcolor_faces = DRW_pass_create(
444                         "Vert Color Pass",
445                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL);
446
447                 stl->g_data->fvcolor_shgrp = DRW_shgroup_create(e_data.vcolor_face_shader, psl->vcolor_faces);
448
449                 static float light[3] = {-0.3f, 0.5f, 1.0f};
450                 static float alpha = 1.0f;
451                 static float world_light = 1.0f;  /* XXX, see: paint_vertex_mode.c */
452                 DRW_shgroup_uniform_vec3(stl->g_data->fvcolor_shgrp, "light", light, 1);
453                 DRW_shgroup_uniform_float(stl->g_data->fvcolor_shgrp, "alpha", &alpha, 1);
454                 DRW_shgroup_uniform_float(stl->g_data->fvcolor_shgrp, "global", &world_light, 1);
455         }
456
457         {
458                 /* Complementary Depth Pass */
459                 psl->depth_hidden_wire = DRW_pass_create(
460                         "Depth Pass Hidden Wire",
461                         DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CULL_BACK);
462                 stl->g_data->depth_shgrp_hidden_wire = DRW_shgroup_create(e_data.depth_sh, psl->depth_hidden_wire);
463         }
464
465         {
466                 /* Depth clearing for ghosting. */
467                 psl->ghost_clear_depth = DRW_pass_create(
468                         "Ghost Depth Clear",
469                         DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS | DRW_STATE_STENCIL_NEQUAL);
470
471                 DRWShadingGroup *shgrp = DRW_shgroup_create(e_data.ghost_clear_depth_sh, psl->ghost_clear_depth);
472                 DRW_shgroup_stencil_mask(shgrp, 0x00);
473                 DRW_shgroup_call_add(shgrp, DRW_cache_fullscreen_quad_get(), NULL);
474         }
475
476         {
477                 /* Normals */
478                 psl->normals = DRW_pass_create(
479                         "Edit Mesh Normals Pass",
480                         DRW_STATE_WRITE_DEPTH | DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL);
481
482                 stl->g_data->fnormals_shgrp = DRW_shgroup_create(e_data.normals_face_sh, psl->normals);
483                 DRW_shgroup_uniform_float(stl->g_data->fnormals_shgrp, "normalSize", &size_normal, 1);
484                 DRW_shgroup_uniform_vec4(stl->g_data->fnormals_shgrp, "color", ts.colorNormal, 1);
485
486                 stl->g_data->vnormals_shgrp = DRW_shgroup_create(e_data.normals_sh, psl->normals);
487                 DRW_shgroup_uniform_float(stl->g_data->vnormals_shgrp, "normalSize", &size_normal, 1);
488                 DRW_shgroup_uniform_vec4(stl->g_data->vnormals_shgrp, "color", ts.colorVNormal, 1);
489
490                 stl->g_data->lnormals_shgrp = DRW_shgroup_create(e_data.normals_loop_sh, psl->normals);
491                 DRW_shgroup_uniform_float(stl->g_data->lnormals_shgrp, "normalSize", &size_normal, 1);
492                 DRW_shgroup_uniform_vec4(stl->g_data->lnormals_shgrp, "color", ts.colorLNormal, 1);
493         }
494
495         if (!stl->g_data->do_zbufclip) {
496                 psl->edit_face_overlay = edit_mesh_create_overlay_pass(
497                         &face_mod, &stl->g_data->edge_width_scale, stl->g_data->data_mask, stl->g_data->do_edges,
498                         DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND,
499                         &stl->g_data->face_overlay_shgrp, &stl->g_data->ledges_overlay_shgrp,
500                         &stl->g_data->lverts_overlay_shgrp, &stl->g_data->facedot_overlay_shgrp);
501         }
502         else {
503                 /* We render all wires with depth and opaque to a new fbo and blend the result based on depth values */
504                 psl->edit_face_occluded = edit_mesh_create_overlay_pass(
505                         &zero, &stl->g_data->edge_width_scale, stl->g_data->data_mask, stl->g_data->do_edges,
506                         DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WRITE_DEPTH,
507                         &stl->g_data->face_occluded_shgrp, &stl->g_data->ledges_occluded_shgrp,
508                         &stl->g_data->lverts_occluded_shgrp, &stl->g_data->facedot_occluded_shgrp);
509
510                 /* however we loose the front faces value (because we need the depth of occluded wires and
511                  * faces are alpha blended ) so we recover them in a new pass. */
512                 psl->facefill_occlude = DRW_pass_create(
513                         "Front Face Color",
514                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
515                 stl->g_data->facefill_occluded_shgrp = DRW_shgroup_create(e_data.overlay_facefill_sh, psl->facefill_occlude);
516                 DRW_shgroup_uniform_block(stl->g_data->facefill_occluded_shgrp, "globalsBlock", globals_ubo);
517                 DRW_shgroup_uniform_ivec4(stl->g_data->facefill_occluded_shgrp, "dataMask", stl->g_data->data_mask, 1);
518
519                 /* we need a full screen pass to combine the result */
520                 struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
521
522                 psl->mix_occlude = DRW_pass_create(
523                         "Mix Occluded Wires",
524                         DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND);
525                 DRWShadingGroup *mix_shgrp = DRW_shgroup_create(e_data.overlay_mix_sh, psl->mix_occlude);
526                 DRW_shgroup_call_add(mix_shgrp, quad, NULL);
527                 DRW_shgroup_uniform_float(mix_shgrp, "alpha", &backwire_opacity, 1);
528                 DRW_shgroup_uniform_texture_ref(mix_shgrp, "wireColor", &e_data.occlude_wire_color_tx);
529                 DRW_shgroup_uniform_texture_ref(mix_shgrp, "wireDepth", &e_data.occlude_wire_depth_tx);
530                 DRW_shgroup_uniform_texture_ref(mix_shgrp, "sceneDepth", &dtxl->depth);
531         }
532 }
533
534 static void edit_mesh_add_ob_to_pass(
535         Scene *scene, Object *ob, DRWShadingGroup *face_shgrp, DRWShadingGroup *ledges_shgrp,
536         DRWShadingGroup *lverts_shgrp, DRWShadingGroup *facedot_shgrp, DRWShadingGroup *facefill_shgrp)
537 {
538         struct GPUBatch *geo_ovl_tris, *geo_ovl_ledges, *geo_ovl_lverts, *geo_ovl_fcenter;
539         ToolSettings *tsettings = scene->toolsettings;
540
541         DRW_cache_mesh_wire_overlay_get(ob, &geo_ovl_tris, &geo_ovl_ledges, &geo_ovl_lverts);
542         DRW_shgroup_call_add(face_shgrp, geo_ovl_tris, ob->obmat);
543         DRW_shgroup_call_add(ledges_shgrp, geo_ovl_ledges, ob->obmat);
544
545         if (facefill_shgrp) {
546                 DRW_shgroup_call_add(facefill_shgrp, geo_ovl_tris, ob->obmat);
547         }
548
549         if ((tsettings->selectmode & SCE_SELECT_VERTEX) != 0) {
550                 DRW_shgroup_call_add(lverts_shgrp, geo_ovl_lverts, ob->obmat);
551         }
552
553         if (facedot_shgrp && (tsettings->selectmode & SCE_SELECT_FACE) != 0 ) {
554                 geo_ovl_fcenter = DRW_cache_face_centers_get(ob);
555                 DRW_shgroup_call_add(facedot_shgrp, geo_ovl_fcenter, ob->obmat);
556         }
557 }
558
559 static void EDIT_MESH_cache_populate(void *vedata, Object *ob)
560 {
561         EDIT_MESH_StorageList *stl = ((EDIT_MESH_Data *)vedata)->stl;
562         const DRWContextState *draw_ctx = DRW_context_state_get();
563         View3D *v3d = draw_ctx->v3d;
564         Scene *scene = draw_ctx->scene;
565         ToolSettings *tsettings = scene->toolsettings;
566         struct GPUBatch *geom;
567
568         if (ob->type == OB_MESH) {
569                 if ((ob == draw_ctx->object_edit) || BKE_object_is_in_editmode(ob)) {
570                         const Mesh *me = ob->data;
571                         bool do_occlude_wire = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_OCCLUDE_WIRE) != 0;
572                         bool do_show_weight = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_WEIGHT) != 0;
573                         bool fnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FACE_NORMALS) != 0;
574                         bool vnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_VERT_NORMALS) != 0;
575                         bool lnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_LOOP_NORMALS) != 0;
576
577                         bool show_face_dots = ((Mesh *)draw_ctx->object_edit->data)->drawflag & ME_DRAW_FACE_DOT;
578
579                         if (stl->g_data->do_faces == false &&
580                                 stl->g_data->do_edges == false &&
581                                 (tsettings->selectmode & SCE_SELECT_FACE))
582                         {
583                                 /* Force display of face centers in this case because that's
584                                  * the only way to see if a face is selected. */
585                                 show_face_dots = true;
586                         }
587
588                         /* Updating uniform */
589                         backwire_opacity = v3d->overlay.backwire_opacity;
590                         size_normal = v3d->overlay.normals_length;
591
592                         face_mod = (do_occlude_wire) ? 0.0f : 1.0f;
593
594                         if (!stl->g_data->do_faces) {
595                                 face_mod = 0.0f;
596                         }
597
598                         if (do_show_weight) {
599                                 geom = DRW_cache_mesh_surface_weights_get(ob);
600                                 DRW_shgroup_call_add(stl->g_data->fvcolor_shgrp, geom, ob->obmat);
601                         }
602
603                         if (do_occlude_wire) {
604                                 geom = DRW_cache_mesh_surface_get(ob);
605                                 DRW_shgroup_call_add(stl->g_data->depth_shgrp_hidden_wire, geom, ob->obmat);
606                         }
607
608                         if (fnormals_do) {
609                                 geom = DRW_cache_face_centers_get(ob);
610                                 DRW_shgroup_call_add(stl->g_data->fnormals_shgrp, geom, ob->obmat);
611                         }
612
613                         if (vnormals_do || lnormals_do) {
614                                 struct GPUBatch *geo_ovl_tris, *geo_ovl_ledges, *geo_ovl_lverts;
615                                 DRW_cache_mesh_normals_overlay_get(ob, &geo_ovl_tris, &geo_ovl_ledges, &geo_ovl_lverts);
616
617                                 if (vnormals_do) {
618                                         DRW_shgroup_call_add(stl->g_data->vnormals_shgrp, geo_ovl_tris, ob->obmat);
619                                         DRW_shgroup_call_add(stl->g_data->vnormals_shgrp, geo_ovl_ledges, ob->obmat);
620                                         DRW_shgroup_call_add(stl->g_data->vnormals_shgrp, geo_ovl_lverts, ob->obmat);
621                                 }
622
623                                 if (lnormals_do) {
624                                         DRW_shgroup_call_add(stl->g_data->lnormals_shgrp, geo_ovl_tris, ob->obmat);
625                                 }
626                         }
627
628                         if (stl->g_data->do_zbufclip) {
629                                 edit_mesh_add_ob_to_pass(
630                                         scene, ob,
631                                         stl->g_data->face_occluded_shgrp,
632                                         stl->g_data->ledges_occluded_shgrp,
633                                         stl->g_data->lverts_occluded_shgrp,
634                                         stl->g_data->facedot_occluded_shgrp,
635                                         (stl->g_data->do_faces) ? stl->g_data->facefill_occluded_shgrp : NULL);
636                         }
637                         else {
638                                 edit_mesh_add_ob_to_pass(
639                                         scene, ob,
640                                         stl->g_data->face_overlay_shgrp,
641                                         stl->g_data->ledges_overlay_shgrp,
642                                         stl->g_data->lverts_overlay_shgrp,
643                                         (show_face_dots) ? stl->g_data->facedot_overlay_shgrp : NULL,
644                                         NULL);
645                         }
646
647                         stl->g_data->ghost_ob += (ob->dtx & OB_DRAWXRAY) ? 1 : 0;
648                         stl->g_data->edit_ob += 1;
649
650                         /* 3D text overlay */
651                         if (me->drawflag & (ME_DRAWEXTRA_EDGELEN |
652                                             ME_DRAWEXTRA_FACEAREA |
653                                             ME_DRAWEXTRA_FACEANG |
654                                             ME_DRAWEXTRA_EDGEANG |
655                                             ME_DRAWEXTRA_INDICES))
656                         {
657                                 if (DRW_state_show_text()) {
658                                         DRW_edit_mesh_mode_text_measure_stats(
659                                                draw_ctx->ar, v3d, ob, &scene->unit);
660                                 }
661                         }
662                 }
663         }
664 }
665
666 static void EDIT_MESH_draw_scene(void *vedata)
667 {
668         EDIT_MESH_PassList *psl = ((EDIT_MESH_Data *)vedata)->psl;
669         EDIT_MESH_StorageList *stl = ((EDIT_MESH_Data *)vedata)->stl;
670         EDIT_MESH_FramebufferList *fbl = ((EDIT_MESH_Data *)vedata)->fbl;
671         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
672         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
673
674         DRW_draw_pass(psl->vcolor_faces);
675
676         DRW_draw_pass(psl->depth_hidden_wire);
677
678         if (stl->g_data->do_zbufclip) {
679                 float clearcol[4] = {0.0f, 0.0f, 0.0f, 0.0f};
680                 /* render facefill */
681                 DRW_draw_pass(psl->facefill_occlude);
682
683                 /* Render wires on a separate framebuffer */
684                 GPU_framebuffer_bind(fbl->occlude_wire_fb);
685                 GPU_framebuffer_clear_color_depth(fbl->occlude_wire_fb, clearcol, 1.0f);
686                 DRW_draw_pass(psl->normals);
687                 DRW_draw_pass(psl->edit_face_occluded);
688
689                 /* Combine with scene buffer */
690                 GPU_framebuffer_bind(dfbl->color_only_fb);
691                 DRW_draw_pass(psl->mix_occlude);
692         }
693         else {
694                 DRW_draw_pass(psl->normals);
695
696                 const DRWContextState *draw_ctx = DRW_context_state_get();
697                 View3D *v3d = draw_ctx->v3d;
698
699                 if (v3d->shading.type == OB_SOLID && (v3d->shading.flag & V3D_SHADING_XRAY) == 0) {
700                         if (stl->g_data->ghost_ob == 1 && stl->g_data->edit_ob == 1) {
701                                 /* In the case of single ghost object edit (common case for retopology):
702                                  * we duplicate the depht+stencil buffer and clear all depth to 1.0f where
703                                  * the stencil buffer is no 0x00. */
704                                 const float *viewport_size = DRW_viewport_size_get();
705                                 const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
706                                 struct GPUTexture *ghost_depth_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_DEPTH24_STENCIL8, &draw_engine_edit_mesh_type);
707                                 GPU_framebuffer_ensure_config(&fbl->ghost_wire_fb, {
708                                         GPU_ATTACHMENT_TEXTURE(ghost_depth_tx),
709                                         GPU_ATTACHMENT_TEXTURE(dtxl->color),
710                                 });
711
712                                 GPU_framebuffer_blit(dfbl->depth_only_fb, 0, fbl->ghost_wire_fb, 0, GPU_DEPTH_BIT | GPU_STENCIL_BIT);
713                                 GPU_framebuffer_bind(fbl->ghost_wire_fb);
714
715                                 DRW_draw_pass(psl->ghost_clear_depth);
716                         }
717                 }
718
719                 DRW_draw_pass(psl->edit_face_overlay);
720         }
721 }
722
723 static void EDIT_MESH_engine_free(void)
724 {
725         DRW_SHADER_FREE_SAFE(e_data.overlay_vert_sh);
726         DRW_SHADER_FREE_SAFE(e_data.overlay_facedot_sh);
727         DRW_SHADER_FREE_SAFE(e_data.overlay_mix_sh);
728         DRW_SHADER_FREE_SAFE(e_data.overlay_facefill_sh);
729         DRW_SHADER_FREE_SAFE(e_data.normals_loop_sh);
730         DRW_SHADER_FREE_SAFE(e_data.normals_face_sh);
731         DRW_SHADER_FREE_SAFE(e_data.normals_sh);
732         DRW_SHADER_FREE_SAFE(e_data.ghost_clear_depth_sh);
733
734         for (int i = 0; i < MAX_SHADERS; i++) {
735                 DRW_SHADER_FREE_SAFE(e_data.overlay_tri_sh_cache[i]);
736                 DRW_SHADER_FREE_SAFE(e_data.overlay_loose_edge_sh_cache[i]);
737         }
738 }
739
740 static const DrawEngineDataSize EDIT_MESH_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_MESH_Data);
741
742 DrawEngineType draw_engine_edit_mesh_type = {
743         NULL, NULL,
744         N_("EditMeshMode"),
745         &EDIT_MESH_data_size,
746         &EDIT_MESH_engine_init,
747         &EDIT_MESH_engine_free,
748         &EDIT_MESH_cache_init,
749         &EDIT_MESH_cache_populate,
750         NULL,
751         NULL,
752         &EDIT_MESH_draw_scene,
753         NULL,
754         NULL,
755 };