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