Edit Mode: Reduce number of edges drawn
[blender.git] / source / blender / draw / modes / edit_mesh_mode.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2016, Blender Foundation.
17  */
18
19 /** \file \ingroup draw
20  */
21
22 #include "DRW_engine.h"
23 #include "DRW_render.h"
24
25 #include "GPU_extensions.h"
26
27 #include "DNA_mesh_types.h"
28 #include "DNA_view3d_types.h"
29
30 #include "draw_common.h"
31
32 #include "draw_cache_impl.h"
33 #include "draw_mode_engines.h"
34
35 #include "edit_mesh_mode_intern.h" /* own include */
36
37 #include "BKE_editmesh.h"
38 #include "BKE_object.h"
39
40 #include "BIF_glutil.h"
41
42 #include "BLI_dynstr.h"
43 #include "BLI_string_utils.h"
44
45 extern char datatoc_gpu_shader_cfg_world_clip_lib_glsl[];
46
47 extern char datatoc_paint_weight_vert_glsl[];
48 extern char datatoc_paint_weight_frag_glsl[];
49
50 extern char datatoc_edit_mesh_overlay_common_lib_glsl[];
51 extern char datatoc_edit_mesh_overlay_frag_glsl[];
52 extern char datatoc_edit_mesh_overlay_vert_glsl[];
53 extern char datatoc_edit_mesh_overlay_geom_glsl[];
54 extern char datatoc_edit_mesh_overlay_mix_frag_glsl[];
55 extern char datatoc_edit_mesh_overlay_facefill_vert_glsl[];
56 extern char datatoc_edit_mesh_overlay_facefill_frag_glsl[];
57 extern char datatoc_edit_normals_vert_glsl[];
58 extern char datatoc_edit_normals_geom_glsl[];
59 extern char datatoc_common_globals_lib_glsl[];
60
61 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
62 extern char datatoc_gpu_shader_3D_smooth_color_frag_glsl[];
63 extern char datatoc_gpu_shader_flat_color_frag_glsl[];
64 extern char datatoc_gpu_shader_point_varying_color_frag_glsl[];
65 extern char datatoc_gpu_shader_depth_only_frag_glsl[];
66
67 /* *********** LISTS *********** */
68 typedef struct EDIT_MESH_PassList {
69         struct DRWPass *weight_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 #define MAX_SHADERS 16
97
98 /** Can only contain shaders (freed as array). */
99 typedef struct EDIT_MESH_Shaders {
100         /* weight */
101         GPUShader *weight_face;
102
103         /* Geometry */
104         GPUShader *overlay_vert;
105         GPUShader *overlay_edge;
106         GPUShader *overlay_edge_flat;
107         GPUShader *overlay_face;
108         GPUShader *overlay_facedot;
109
110         GPUShader *overlay_mix;
111         GPUShader *overlay_facefill;
112         GPUShader *normals_face;
113         GPUShader *normals_loop;
114         GPUShader *normals;
115         GPUShader *depth;
116         GPUShader *ghost_clear_depth;
117 } EDIT_MESH_Shaders;
118
119 /* *********** STATIC *********** */
120 static struct {
121         EDIT_MESH_Shaders sh_data[GPU_SHADER_CFG_LEN];
122
123         /* temp buffer texture */
124         struct GPUTexture *occlude_wire_depth_tx;
125         struct GPUTexture *occlude_wire_color_tx;
126 } e_data = {NULL}; /* Engine data */
127
128 typedef struct EDIT_MESH_PrivateData {
129         /* weight */
130         DRWShadingGroup *fweights_shgrp;
131         DRWShadingGroup *depth_shgrp_hidden_wire;
132
133         DRWShadingGroup *fnormals_shgrp;
134         DRWShadingGroup *vnormals_shgrp;
135         DRWShadingGroup *lnormals_shgrp;
136
137         DRWShadingGroup *vert_shgrp;
138         DRWShadingGroup *edge_shgrp;
139         DRWShadingGroup *face_shgrp;
140         DRWShadingGroup *face_cage_shgrp;
141         DRWShadingGroup *facedot_shgrp;
142
143         DRWShadingGroup *facefill_occluded_shgrp;
144
145         int data_mask[4];
146         int ghost_ob;
147         int edit_ob;
148         bool do_zbufclip;
149         bool do_faces;
150         bool do_edges;
151 } EDIT_MESH_PrivateData; /* Transient data */
152
153 /* *********** FUNCTIONS *********** */
154
155
156 static void EDIT_MESH_engine_init(void *vedata)
157 {
158         EDIT_MESH_FramebufferList *fbl = ((EDIT_MESH_Data *)vedata)->fbl;
159
160         const DRWContextState *draw_ctx = DRW_context_state_get();
161         EDIT_MESH_Shaders *sh_data = &e_data.sh_data[draw_ctx->shader_cfg];
162         const bool is_clip = (draw_ctx->rv3d->rflag & RV3D_CLIPPING) != 0;
163
164         const float *viewport_size = DRW_viewport_size_get();
165         const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
166
167         e_data.occlude_wire_depth_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_DEPTH_COMPONENT24,
168                                                                  &draw_engine_edit_mesh_type);
169         e_data.occlude_wire_color_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RGBA8,
170                                                                  &draw_engine_edit_mesh_type);
171
172         GPU_framebuffer_ensure_config(&fbl->occlude_wire_fb, {
173                 GPU_ATTACHMENT_TEXTURE(e_data.occlude_wire_depth_tx),
174                 GPU_ATTACHMENT_TEXTURE(e_data.occlude_wire_color_tx)
175         });
176
177         if (is_clip) {
178                 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
179         }
180
181         const char *world_clip_lib_or_empty = is_clip ? datatoc_gpu_shader_cfg_world_clip_lib_glsl : "";
182         const char *world_clip_def_or_empty = is_clip ? "#define USE_WORLD_CLIP_PLANES\n" : "";
183
184         if (!sh_data->weight_face) {
185                 sh_data->weight_face = GPU_shader_create_from_arrays({
186                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_common_globals_lib_glsl, datatoc_paint_weight_vert_glsl, NULL},
187                         .frag = (const char *[]){datatoc_common_globals_lib_glsl, datatoc_paint_weight_frag_glsl, NULL},
188                         .defs = (const char *[]){world_clip_def_or_empty, NULL},
189                 });
190
191                 char *lib = BLI_string_joinN(world_clip_lib_or_empty, datatoc_common_globals_lib_glsl, datatoc_edit_mesh_overlay_common_lib_glsl);
192                 /* Use geometry shader to draw edge wireframe. This ensure us
193                  * the same result accross platforms and more flexibility. But
194                  * we pay the cost of running a geometry shader.
195                  * In the future we might consider using only the vertex shader
196                  * and loading data manually with buffer textures. */
197                 const bool use_geom_shader = true;
198                 const char *geom_sh_code[] = {lib, datatoc_edit_mesh_overlay_geom_glsl, NULL};
199                 if (!use_geom_shader) {
200                         geom_sh_code[0] = NULL;
201                 }
202                 const char *use_geom_def = use_geom_shader ? "#define USE_GEOM_SHADER\n" : "";
203                 sh_data->overlay_face = GPU_shader_create_from_arrays({
204                         .vert = (const char *[]){lib, datatoc_edit_mesh_overlay_vert_glsl, NULL},
205                         .frag = (const char *[]){datatoc_gpu_shader_3D_smooth_color_frag_glsl, NULL},
206                         .defs = (const char *[]){world_clip_def_or_empty, "#define FACE\n", NULL},
207                 });
208                 sh_data->overlay_edge = GPU_shader_create_from_arrays({
209                         .vert = (const char *[]){lib, datatoc_edit_mesh_overlay_vert_glsl, NULL},
210                         .frag = (const char *[]){lib, datatoc_edit_mesh_overlay_frag_glsl, NULL},
211                         .defs = (const char *[]){world_clip_def_or_empty, use_geom_def, "#define EDGE\n", NULL},
212                         .geom = (use_geom_shader) ? geom_sh_code : NULL,
213                 });
214                 sh_data->overlay_edge_flat = GPU_shader_create_from_arrays({
215                         .vert = (const char *[]){lib, datatoc_edit_mesh_overlay_vert_glsl, NULL},
216                         .frag = (const char *[]){lib, datatoc_edit_mesh_overlay_frag_glsl, NULL},
217                         .defs = (const char *[]){world_clip_def_or_empty, use_geom_def, "#define EDGE\n", "#define FLAT\n", NULL},
218                         .geom = (use_geom_shader) ? geom_sh_code : NULL,
219                 });
220                 sh_data->overlay_vert = GPU_shader_create_from_arrays({
221                         .vert = (const char *[]){lib, datatoc_edit_mesh_overlay_vert_glsl, NULL},
222                         .frag = (const char *[]){datatoc_gpu_shader_point_varying_color_frag_glsl, NULL},
223                         .defs = (const char *[]){world_clip_def_or_empty, "#define VERT\n", NULL},
224                 });
225                 sh_data->overlay_facedot = GPU_shader_create_from_arrays({
226                         .vert = (const char *[]){lib, datatoc_edit_mesh_overlay_vert_glsl, NULL},
227                         .frag = (const char *[]){datatoc_gpu_shader_point_varying_color_frag_glsl, NULL},
228                         .defs = (const char *[]){world_clip_def_or_empty, "#define FACEDOT\n", NULL},
229                 });
230                 sh_data->overlay_facefill = GPU_shader_create_from_arrays({
231                         .vert = (const char *[]){lib, datatoc_edit_mesh_overlay_facefill_vert_glsl, NULL},
232                         .frag = (const char *[]){lib, datatoc_edit_mesh_overlay_facefill_frag_glsl, NULL},
233                         .defs = (const char *[]){world_clip_def_or_empty, NULL},
234                 });
235                 MEM_freeN(lib);
236
237                 sh_data->overlay_mix = DRW_shader_create_fullscreen(datatoc_edit_mesh_overlay_mix_frag_glsl, NULL);
238
239                 sh_data->normals_face = GPU_shader_create_from_arrays({
240                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_edit_normals_vert_glsl, NULL},
241                         .geom = (const char *[]){world_clip_lib_or_empty, datatoc_edit_normals_geom_glsl, NULL},
242                         .frag = (const char *[]){datatoc_gpu_shader_uniform_color_frag_glsl, NULL},
243                         .defs = (const char *[]){world_clip_def_or_empty, "#define FACE_NORMALS\n", NULL},
244                 });
245
246                 sh_data->normals_loop = GPU_shader_create_from_arrays({
247                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_edit_normals_vert_glsl, NULL},
248                         .geom = (const char *[]){world_clip_lib_or_empty, datatoc_edit_normals_geom_glsl, NULL},
249                         .frag = (const char *[]){datatoc_gpu_shader_uniform_color_frag_glsl, NULL},
250                         .defs = (const char *[]){world_clip_def_or_empty, "#define LOOP_NORMALS\n", NULL},
251                 });
252
253                 sh_data->normals = GPU_shader_create_from_arrays({
254                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_edit_normals_vert_glsl, NULL},
255                         .geom = (const char *[]){world_clip_lib_or_empty, datatoc_edit_normals_geom_glsl, NULL},
256                         .frag = (const char *[]){datatoc_gpu_shader_uniform_color_frag_glsl, NULL},
257                         .defs = (const char *[]){world_clip_def_or_empty, NULL},
258                 });
259
260                 sh_data->depth = DRW_shader_create_3D_depth_only(draw_ctx->shader_cfg);
261
262                 sh_data->ghost_clear_depth = DRW_shader_create_fullscreen(datatoc_gpu_shader_depth_only_frag_glsl, NULL);
263         }
264 }
265
266 static DRWPass *edit_mesh_create_overlay_pass(
267         float *face_alpha, int *data_mask, bool do_edges, bool UNUSED(xray),
268         DRWState statemod,
269         DRWShadingGroup **r_face_shgrp, DRWShadingGroup **r_face_cage_shgrp, DRWShadingGroup **r_facedot_shgrp,
270         DRWShadingGroup **r_edge_shgrp, DRWShadingGroup **r_vert_shgrp)
271 {
272         const DRWContextState *draw_ctx = DRW_context_state_get();
273         RegionView3D *rv3d = draw_ctx->rv3d;
274         Scene *scene = draw_ctx->scene;
275         ToolSettings *tsettings = scene->toolsettings;
276         EDIT_MESH_Shaders *sh_data = &e_data.sh_data[draw_ctx->shader_cfg];
277         const bool select_vert = (tsettings->selectmode & SCE_SELECT_VERTEX) != 0;
278         const bool select_face = (tsettings->selectmode & SCE_SELECT_FACE) != 0;
279         const bool select_edge = (tsettings->selectmode & SCE_SELECT_EDGE) != 0;
280         float winmat[4][4];
281         float viewdist = rv3d->dist;
282         DRW_viewport_matrix_get(winmat, DRW_MAT_WIN);
283         /* special exception for ortho camera (viewdist isnt used for perspective cameras) */
284         if (rv3d->persp == RV3D_CAMOB && rv3d->is_persp == false) {
285                 viewdist = 1.0f / max_ff(fabsf(rv3d->winmat[0][0]), fabsf(rv3d->winmat[1][1]));
286         }
287         const float depth_ofs = bglPolygonOffsetCalc((float *)winmat, viewdist, 1.0f);
288
289         DRWPass *pass = DRW_pass_create(
290                 "Edit Mesh Face Overlay Pass",
291                 DRW_STATE_WRITE_COLOR | DRW_STATE_POINT | statemod);
292
293         DRWShadingGroup *grp;
294
295         GPUShader *vert_sh = sh_data->overlay_vert;
296         GPUShader *edge_sh = (select_vert) ? sh_data->overlay_edge : sh_data->overlay_edge_flat;
297         GPUShader *face_sh = sh_data->overlay_face;
298         GPUShader *facedot_sh = sh_data->overlay_facedot;
299
300         /* Faces */
301         if (select_face) {
302                 grp = *r_facedot_shgrp = DRW_shgroup_create(facedot_sh, pass);
303                 DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
304                 DRW_shgroup_state_enable(grp, DRW_STATE_WRITE_DEPTH);
305                 if (rv3d->rflag & RV3D_CLIPPING) {
306                         DRW_shgroup_world_clip_planes_from_rv3d(grp, rv3d);
307                 }
308         }
309
310         grp = *r_face_shgrp = DRW_shgroup_create(face_sh, pass);
311         DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
312         DRW_shgroup_uniform_float(grp, "faceAlphaMod", face_alpha, 1);
313         DRW_shgroup_uniform_ivec4(grp, "dataMask", data_mask, 1);
314         DRW_shgroup_uniform_bool_copy(grp, "doEdges", do_edges);
315         DRW_shgroup_uniform_float_copy(grp, "ofs", 0.0f);
316         if (rv3d->rflag & RV3D_CLIPPING) {
317                 DRW_shgroup_world_clip_planes_from_rv3d(grp, rv3d);
318         }
319
320         /* Cage geom needs to be offseted to avoid Z-fighting. */
321         grp = *r_face_cage_shgrp = DRW_shgroup_create_sub(*r_face_shgrp);
322         DRW_shgroup_state_enable(grp, DRW_STATE_OFFSET_NEGATIVE);
323
324         /* Verts */
325         if (select_vert) {
326                 grp = *r_vert_shgrp = DRW_shgroup_create(vert_sh, pass);
327                 DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
328                 DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
329                 DRW_shgroup_uniform_float_copy(grp, "ofs", depth_ofs * 1.5f);
330                 DRW_shgroup_state_enable(grp, DRW_STATE_OFFSET_NEGATIVE | DRW_STATE_WRITE_DEPTH);
331                 DRW_shgroup_state_disable(grp, DRW_STATE_BLEND);
332                 if (rv3d->rflag & RV3D_CLIPPING) {
333                         DRW_shgroup_world_clip_planes_from_rv3d(grp, rv3d);
334                 }
335         }
336
337         /* Edges */
338         grp = *r_edge_shgrp = DRW_shgroup_create(edge_sh, pass);
339         DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
340         DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
341         DRW_shgroup_uniform_vec2(grp, "viewportSizeInv", DRW_viewport_invert_size_get(), 1);
342         DRW_shgroup_uniform_ivec4(grp, "dataMask", data_mask, 1);
343         DRW_shgroup_uniform_bool_copy(grp, "doEdges", do_edges);
344         DRW_shgroup_uniform_float_copy(grp, "ofs", depth_ofs);
345         DRW_shgroup_uniform_float_copy(grp, "edgeScale", select_edge ? 1.75f : 1.0f);
346         DRW_shgroup_state_enable(grp, DRW_STATE_OFFSET_NEGATIVE);
347         /* To match blender loop structure. */
348         DRW_shgroup_state_enable(grp, DRW_STATE_FIRST_VERTEX_CONVENTION);
349         if (rv3d->rflag & RV3D_CLIPPING) {
350                 DRW_shgroup_world_clip_planes_from_rv3d(grp, rv3d);
351         }
352
353         return pass;
354 }
355
356 static float backwire_opacity;
357 static float face_mod;
358 static float size_normal;
359
360 static void EDIT_MESH_cache_init(void *vedata)
361 {
362         EDIT_MESH_PassList *psl = ((EDIT_MESH_Data *)vedata)->psl;
363         EDIT_MESH_StorageList *stl = ((EDIT_MESH_Data *)vedata)->stl;
364         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
365
366         const DRWContextState *draw_ctx = DRW_context_state_get();
367         View3D *v3d = draw_ctx->v3d;
368         RegionView3D *rv3d = draw_ctx->rv3d;
369         Scene *scene = draw_ctx->scene;
370         ToolSettings *tsettings = scene->toolsettings;
371         EDIT_MESH_Shaders *sh_data = &e_data.sh_data[draw_ctx->shader_cfg];
372         static float zero = 0.0f;
373
374         if (!stl->g_data) {
375                 /* Alloc transient pointers */
376                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
377         }
378         stl->g_data->ghost_ob = 0;
379         stl->g_data->edit_ob = 0;
380         stl->g_data->do_faces = true;
381         stl->g_data->do_edges = true;
382
383         stl->g_data->do_zbufclip = ((v3d)->shading.flag & XRAY_FLAG(v3d)) != 0;
384
385         stl->g_data->data_mask[0] = 0xFF; /* Face Flag */
386         stl->g_data->data_mask[1] = 0xFF; /* Edge Flag */
387         stl->g_data->data_mask[2] = 0xFF; /* Crease */
388         stl->g_data->data_mask[3] = 0xFF; /* BWeight */
389
390         if (draw_ctx->object_edit->type == OB_MESH) {
391                 if (BKE_object_is_in_editmode(draw_ctx->object_edit)) {
392                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FREESTYLE_FACE) == 0) {
393                                 stl->g_data->data_mask[0] &= ~VFLAG_FACE_FREESTYLE;
394                         }
395                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FACES) == 0) {
396                                 stl->g_data->data_mask[0] &= ~(VFLAG_FACE_SELECTED & VFLAG_FACE_FREESTYLE);
397                                 stl->g_data->do_faces = false;
398                         }
399                         if ((tsettings->selectmode & SCE_SELECT_FACE) == 0) {
400                                 stl->g_data->data_mask[0] &= ~VFLAG_FACE_ACTIVE;
401                         }
402                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_SEAMS) == 0) {
403                                 stl->g_data->data_mask[1] &= ~VFLAG_EDGE_SEAM;
404                         }
405                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_SHARP) == 0) {
406                                 stl->g_data->data_mask[1] &= ~VFLAG_EDGE_SHARP;
407                         }
408                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FREESTYLE_EDGE) == 0) {
409                                 stl->g_data->data_mask[1] &= ~VFLAG_EDGE_FREESTYLE;
410                         }
411                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_EDGES) == 0) {
412                                 if ((tsettings->selectmode & SCE_SELECT_EDGE) == 0) {
413                                         stl->g_data->data_mask[1] &= ~(VFLAG_EDGE_ACTIVE & VFLAG_EDGE_SELECTED);
414                                         stl->g_data->do_edges = false;
415                                 }
416                         }
417                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_CREASES) == 0) {
418                                 stl->g_data->data_mask[2] = 0x0;
419                         }
420                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_BWEIGHTS) == 0) {
421                                 stl->g_data->data_mask[3] = 0x0;
422                         }
423                 }
424         }
425
426         {
427                 psl->weight_faces = DRW_pass_create(
428                         "Weight Pass",
429                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL);
430
431                 stl->g_data->fweights_shgrp = DRW_shgroup_create(sh_data->weight_face, psl->weight_faces);
432
433                 static float alpha = 1.0f;
434                 DRW_shgroup_uniform_float(stl->g_data->fweights_shgrp, "opacity", &alpha, 1);
435                 DRW_shgroup_uniform_texture(stl->g_data->fweights_shgrp, "colorramp", G_draw.weight_ramp);
436                 DRW_shgroup_uniform_block(stl->g_data->fweights_shgrp, "globalsBlock", G_draw.block_ubo);
437                 if (rv3d->rflag & RV3D_CLIPPING) {
438                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->fweights_shgrp, rv3d);
439                 }
440         }
441
442         {
443                 /* Complementary Depth Pass */
444                 psl->depth_hidden_wire = DRW_pass_create(
445                         "Depth Pass Hidden Wire",
446                         DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CULL_BACK);
447                 stl->g_data->depth_shgrp_hidden_wire = DRW_shgroup_create(sh_data->depth, psl->depth_hidden_wire);
448                 if (rv3d->rflag & RV3D_CLIPPING) {
449                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->fweights_shgrp, rv3d);
450                 }
451         }
452
453         {
454                 /* Depth clearing for ghosting. */
455                 psl->ghost_clear_depth = DRW_pass_create(
456                         "Ghost Depth Clear",
457                         DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS | DRW_STATE_STENCIL_NEQUAL);
458
459                 DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->ghost_clear_depth, psl->ghost_clear_depth);
460                 DRW_shgroup_stencil_mask(shgrp, 0x00);
461                 DRW_shgroup_call_add(shgrp, DRW_cache_fullscreen_quad_get(), NULL);
462         }
463
464         {
465                 /* Normals */
466                 psl->normals = DRW_pass_create(
467                         "Edit Mesh Normals Pass",
468                         DRW_STATE_WRITE_DEPTH | DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL);
469
470                 stl->g_data->fnormals_shgrp = DRW_shgroup_create(sh_data->normals_face, psl->normals);
471                 DRW_shgroup_uniform_float(stl->g_data->fnormals_shgrp, "normalSize", &size_normal, 1);
472                 DRW_shgroup_uniform_vec4(stl->g_data->fnormals_shgrp, "color", G_draw.block.colorNormal, 1);
473                 if (rv3d->rflag & RV3D_CLIPPING) {
474                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->fnormals_shgrp, rv3d);
475                 }
476
477                 stl->g_data->vnormals_shgrp = DRW_shgroup_create(sh_data->normals, psl->normals);
478                 DRW_shgroup_uniform_float(stl->g_data->vnormals_shgrp, "normalSize", &size_normal, 1);
479                 DRW_shgroup_uniform_vec4(stl->g_data->vnormals_shgrp, "color", G_draw.block.colorVNormal, 1);
480                 if (rv3d->rflag & RV3D_CLIPPING) {
481                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->vnormals_shgrp, rv3d);
482                 }
483
484                 stl->g_data->lnormals_shgrp = DRW_shgroup_create(sh_data->normals_loop, psl->normals);
485                 DRW_shgroup_uniform_float(stl->g_data->lnormals_shgrp, "normalSize", &size_normal, 1);
486                 DRW_shgroup_uniform_vec4(stl->g_data->lnormals_shgrp, "color", G_draw.block.colorLNormal, 1);
487                 if (rv3d->rflag & RV3D_CLIPPING) {
488                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->lnormals_shgrp, rv3d);
489                 }
490         }
491
492         if (!stl->g_data->do_zbufclip) {
493                 psl->edit_face_overlay = edit_mesh_create_overlay_pass(
494                         &face_mod, stl->g_data->data_mask, stl->g_data->do_edges, false,
495                         DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND,
496                         &stl->g_data->face_shgrp,
497                         &stl->g_data->face_cage_shgrp,
498                         &stl->g_data->facedot_shgrp,
499                         &stl->g_data->edge_shgrp,
500                         &stl->g_data->vert_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->data_mask, stl->g_data->do_edges, true,
506                         DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WRITE_DEPTH,
507                         &stl->g_data->face_shgrp,
508                         &stl->g_data->face_cage_shgrp,
509                         &stl->g_data->facedot_shgrp,
510                         &stl->g_data->edge_shgrp,
511                         &stl->g_data->vert_shgrp);
512
513                 /* however we loose the front faces value (because we need the depth of occluded wires and
514                  * faces are alpha blended ) so we recover them in a new pass. */
515                 psl->facefill_occlude = DRW_pass_create(
516                         "Front Face Color",
517                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
518                 stl->g_data->facefill_occluded_shgrp = DRW_shgroup_create(sh_data->overlay_facefill, psl->facefill_occlude);
519                 DRW_shgroup_uniform_block(stl->g_data->facefill_occluded_shgrp, "globalsBlock", G_draw.block_ubo);
520                 DRW_shgroup_uniform_ivec4(stl->g_data->facefill_occluded_shgrp, "dataMask", stl->g_data->data_mask, 1);
521                 if (rv3d->rflag & RV3D_CLIPPING) {
522                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->facefill_occluded_shgrp, rv3d);
523                 }
524
525                 /* we need a full screen pass to combine the result */
526                 struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
527
528                 psl->mix_occlude = DRW_pass_create(
529                         "Mix Occluded Wires",
530                         DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND);
531                 DRWShadingGroup *mix_shgrp = DRW_shgroup_create(sh_data->overlay_mix, psl->mix_occlude);
532                 DRW_shgroup_call_add(mix_shgrp, quad, NULL);
533                 DRW_shgroup_uniform_float(mix_shgrp, "alpha", &backwire_opacity, 1);
534                 DRW_shgroup_uniform_texture_ref(mix_shgrp, "wireColor", &e_data.occlude_wire_color_tx);
535                 DRW_shgroup_uniform_texture_ref(mix_shgrp, "wireDepth", &e_data.occlude_wire_depth_tx);
536                 DRW_shgroup_uniform_texture_ref(mix_shgrp, "sceneDepth", &dtxl->depth);
537         }
538 }
539
540 static void edit_mesh_add_ob_to_pass(
541         Scene *scene, Object *ob,
542         EDIT_MESH_PrivateData *g_data,
543         DRWShadingGroup *facedot_shgrp,
544         DRWShadingGroup *facefill_shgrp)
545 {
546         struct GPUBatch *geom_tris, *geom_verts, *geom_edges, *geom_fcenter;
547         ToolSettings *tsettings = scene->toolsettings;
548
549         bool has_edit_mesh_cage = false;
550         /* TODO: Should be its own function. */
551         Mesh *me = (Mesh *)ob->data;
552         BMEditMesh *embm = me->edit_btmesh;
553         if (embm) {
554                 has_edit_mesh_cage = embm->mesh_eval_cage && (embm->mesh_eval_cage != embm->mesh_eval_final);
555         }
556
557         DRWShadingGroup *face_shgrp = (has_edit_mesh_cage) ? g_data->face_cage_shgrp : g_data->face_shgrp;
558         DRWShadingGroup *vert_shgrp = g_data->vert_shgrp;
559         DRWShadingGroup *edge_shgrp = g_data->edge_shgrp;
560
561         face_shgrp = (facefill_shgrp != NULL) ? facefill_shgrp : face_shgrp;
562
563         geom_tris = DRW_mesh_batch_cache_get_edit_triangles(ob->data);
564         geom_edges = DRW_mesh_batch_cache_get_edit_edges(ob->data);
565         DRW_shgroup_call_add(edge_shgrp, geom_edges, ob->obmat);
566         DRW_shgroup_call_add(face_shgrp, geom_tris, ob->obmat);
567
568         if ((tsettings->selectmode & SCE_SELECT_VERTEX) != 0) {
569                 geom_verts = DRW_mesh_batch_cache_get_edit_vertices(ob->data);
570                 DRW_shgroup_call_add(vert_shgrp, geom_verts, ob->obmat);
571         }
572
573         if (facedot_shgrp && (tsettings->selectmode & SCE_SELECT_FACE) != 0) {
574                 geom_fcenter = DRW_mesh_batch_cache_get_edit_facedots(ob->data);
575                 DRW_shgroup_call_add(facedot_shgrp, geom_fcenter, ob->obmat);
576         }
577 }
578
579 static void EDIT_MESH_cache_populate(void *vedata, Object *ob)
580 {
581         EDIT_MESH_StorageList *stl = ((EDIT_MESH_Data *)vedata)->stl;
582         const DRWContextState *draw_ctx = DRW_context_state_get();
583         View3D *v3d = draw_ctx->v3d;
584         Scene *scene = draw_ctx->scene;
585         ToolSettings *tsettings = scene->toolsettings;
586         struct GPUBatch *geom;
587
588         if (ob->type == OB_MESH) {
589                 if ((ob == draw_ctx->object_edit) || BKE_object_is_in_editmode(ob)) {
590                         bool do_occlude_wire = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_OCCLUDE_WIRE) != 0;
591                         bool do_show_weight = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_WEIGHT) != 0;
592                         bool fnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FACE_NORMALS) != 0;
593                         bool vnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_VERT_NORMALS) != 0;
594                         bool lnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_LOOP_NORMALS) != 0;
595
596                         bool show_face_dots = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FACE_DOT) != 0;
597
598                         if (stl->g_data->do_faces == false &&
599                             stl->g_data->do_edges == false &&
600                             (tsettings->selectmode & SCE_SELECT_FACE))
601                         {
602                                 /* Force display of face centers in this case because that's
603                                  * the only way to see if a face is selected. */
604                                 show_face_dots = true;
605                         }
606
607                         /* Updating uniform */
608                         backwire_opacity = v3d->overlay.backwire_opacity;
609                         size_normal = v3d->overlay.normals_length;
610
611                         face_mod = (do_occlude_wire) ? 0.0f : 1.0f;
612
613                         if (!stl->g_data->do_faces) {
614                                 face_mod = 0.0f;
615                         }
616
617                         if (do_show_weight) {
618                                 geom = DRW_cache_mesh_surface_weights_get(ob);
619                                 DRW_shgroup_call_add(stl->g_data->fweights_shgrp, geom, ob->obmat);
620                         }
621
622                         if (do_occlude_wire) {
623                                 geom = DRW_cache_mesh_surface_get(ob);
624                                 DRW_shgroup_call_add(stl->g_data->depth_shgrp_hidden_wire, geom, ob->obmat);
625                         }
626
627                         if (vnormals_do) {
628                                 geom = DRW_mesh_batch_cache_get_edit_vertices(ob->data);
629                                 DRW_shgroup_call_add(stl->g_data->vnormals_shgrp, geom, ob->obmat);
630                         }
631                         if (lnormals_do) {
632                                 geom = DRW_mesh_batch_cache_get_edit_lnors(ob->data);
633                                 DRW_shgroup_call_add(stl->g_data->lnormals_shgrp, geom, ob->obmat);
634                         }
635                         if (fnormals_do) {
636                                 geom = DRW_mesh_batch_cache_get_edit_facedots(ob->data);
637                                 DRW_shgroup_call_add(stl->g_data->fnormals_shgrp, geom, ob->obmat);
638                         }
639
640                         if (stl->g_data->do_zbufclip) {
641                                 edit_mesh_add_ob_to_pass(
642                                         scene, ob, stl->g_data,
643                                         stl->g_data->facedot_shgrp,
644                                         (stl->g_data->do_faces) ? stl->g_data->facefill_occluded_shgrp : NULL);
645                         }
646                         else {
647                                 edit_mesh_add_ob_to_pass(
648                                         scene, ob, stl->g_data,
649                                         (show_face_dots) ? stl->g_data->facedot_shgrp : NULL,
650                                         NULL);
651                         }
652
653                         stl->g_data->ghost_ob += (ob->dtx & OB_DRAWXRAY) ? 1 : 0;
654                         stl->g_data->edit_ob += 1;
655
656                         /* 3D text overlay */
657                         if (v3d->overlay.edit_flag & (V3D_OVERLAY_EDIT_EDGE_LEN |
658                                                       V3D_OVERLAY_EDIT_FACE_AREA |
659                                                       V3D_OVERLAY_EDIT_FACE_ANG |
660                                                       V3D_OVERLAY_EDIT_EDGE_ANG |
661                                                       V3D_OVERLAY_EDIT_INDICES))
662                         {
663                                 if (DRW_state_show_text()) {
664                                         DRW_edit_mesh_mode_text_measure_stats(
665                                                draw_ctx->ar, v3d, ob, &scene->unit);
666                                 }
667                         }
668                 }
669         }
670 }
671
672 static void EDIT_MESH_draw_scene(void *vedata)
673 {
674         EDIT_MESH_PassList *psl = ((EDIT_MESH_Data *)vedata)->psl;
675         EDIT_MESH_StorageList *stl = ((EDIT_MESH_Data *)vedata)->stl;
676         EDIT_MESH_FramebufferList *fbl = ((EDIT_MESH_Data *)vedata)->fbl;
677         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
678         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
679
680         DRW_draw_pass(psl->weight_faces);
681
682         DRW_draw_pass(psl->depth_hidden_wire);
683
684         if (stl->g_data->do_zbufclip) {
685                 float clearcol[4] = {0.0f, 0.0f, 0.0f, 0.0f};
686                 /* render facefill */
687                 DRW_draw_pass(psl->facefill_occlude);
688
689                 /* Render wires on a separate framebuffer */
690                 GPU_framebuffer_bind(fbl->occlude_wire_fb);
691                 GPU_framebuffer_clear_color_depth(fbl->occlude_wire_fb, clearcol, 1.0f);
692                 DRW_draw_pass(psl->normals);
693                 DRW_draw_pass(psl->edit_face_occluded);
694
695                 /* Combine with scene buffer */
696                 GPU_framebuffer_bind(dfbl->color_only_fb);
697                 DRW_draw_pass(psl->mix_occlude);
698         }
699         else {
700                 DRW_draw_pass(psl->normals);
701
702                 const DRWContextState *draw_ctx = DRW_context_state_get();
703                 View3D *v3d = draw_ctx->v3d;
704
705                 if (v3d->shading.type == OB_SOLID && (v3d->shading.flag & XRAY_FLAG(v3d)) == 0 &&
706                     stl->g_data->ghost_ob == 1 && stl->g_data->edit_ob == 1)
707                 {
708                         /* In the case of single ghost object edit (common case for retopology):
709                          * we duplicate the depht+stencil buffer and clear all depth to 1.0f where
710                          * the stencil buffer is no 0x00. */
711                         const float *viewport_size = DRW_viewport_size_get();
712                         const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
713                         struct GPUTexture *ghost_depth_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_DEPTH24_STENCIL8, &draw_engine_edit_mesh_type);
714                         GPU_framebuffer_ensure_config(&fbl->ghost_wire_fb, {
715                                 GPU_ATTACHMENT_TEXTURE(ghost_depth_tx),
716                                 GPU_ATTACHMENT_TEXTURE(dtxl->color),
717                         });
718
719                         GPU_framebuffer_blit(dfbl->depth_only_fb, 0, fbl->ghost_wire_fb, 0, GPU_DEPTH_BIT | GPU_STENCIL_BIT);
720                         GPU_framebuffer_bind(fbl->ghost_wire_fb);
721
722                         DRW_draw_pass(psl->ghost_clear_depth);
723
724                         DRW_draw_pass(psl->edit_face_overlay);
725                 }
726                 else {
727                         // MULTISAMPLE_SYNC_ENABLE(dfbl, dtxl);
728
729                         DRW_draw_pass(psl->edit_face_overlay);
730
731                         // MULTISAMPLE_SYNC_DISABLE(dfbl, dtxl)
732                 }
733         }
734
735         DRW_state_clip_planes_reset();
736 }
737
738 static void EDIT_MESH_engine_free(void)
739 {
740         for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
741                 EDIT_MESH_Shaders *sh_data = &e_data.sh_data[sh_data_index];
742                 /* Don't free builtins. */
743                 sh_data->depth = NULL;
744                 GPUShader **sh_data_as_array = (GPUShader **)sh_data;
745                 for (int i = 0; i < (sizeof(EDIT_MESH_Shaders) / sizeof(GPUShader *)); i++) {
746                         DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
747                 }
748         }
749 }
750
751 static const DrawEngineDataSize EDIT_MESH_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_MESH_Data);
752
753 DrawEngineType draw_engine_edit_mesh_type = {
754         NULL, NULL,
755         N_("EditMeshMode"),
756         &EDIT_MESH_data_size,
757         &EDIT_MESH_engine_init,
758         &EDIT_MESH_engine_free,
759         &EDIT_MESH_cache_init,
760         &EDIT_MESH_cache_populate,
761         NULL,
762         NULL,
763         &EDIT_MESH_draw_scene,
764         NULL,
765         NULL,
766 };