doxygen: add newline after \file
[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         float winmat[4][4];
278         float viewdist = rv3d->dist;
279         DRW_viewport_matrix_get(winmat, DRW_MAT_WIN);
280         /* special exception for ortho camera (viewdist isnt used for perspective cameras) */
281         if (rv3d->persp == RV3D_CAMOB && rv3d->is_persp == false) {
282                 viewdist = 1.0f / max_ff(fabsf(rv3d->winmat[0][0]), fabsf(rv3d->winmat[1][1]));
283         }
284         const float depth_ofs = bglPolygonOffsetCalc((float *)winmat, viewdist, 1.0f);
285
286         DRWPass *pass = DRW_pass_create(
287                 "Edit Mesh Face Overlay Pass",
288                 DRW_STATE_WRITE_COLOR | DRW_STATE_POINT | statemod);
289
290         DRWShadingGroup *grp;
291
292         GPUShader *vert_sh = sh_data->overlay_vert;
293         GPUShader *edge_sh = (select_vert) ? sh_data->overlay_edge : sh_data->overlay_edge_flat;
294         GPUShader *face_sh = sh_data->overlay_face;
295         GPUShader *facedot_sh = sh_data->overlay_facedot;
296
297         /* Faces */
298         if (select_face) {
299                 grp = *r_facedot_shgrp = DRW_shgroup_create(facedot_sh, pass);
300                 DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
301                 DRW_shgroup_state_enable(grp, DRW_STATE_WRITE_DEPTH);
302                 if (rv3d->rflag & RV3D_CLIPPING) {
303                         DRW_shgroup_world_clip_planes_from_rv3d(grp, rv3d);
304                 }
305         }
306
307         grp = *r_face_shgrp = DRW_shgroup_create(face_sh, pass);
308         DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
309         DRW_shgroup_uniform_float(grp, "faceAlphaMod", face_alpha, 1);
310         DRW_shgroup_uniform_ivec4(grp, "dataMask", data_mask, 1);
311         DRW_shgroup_uniform_bool_copy(grp, "doEdges", do_edges);
312         DRW_shgroup_uniform_float_copy(grp, "ofs", 0.0f);
313         if (rv3d->rflag & RV3D_CLIPPING) {
314                 DRW_shgroup_world_clip_planes_from_rv3d(grp, rv3d);
315         }
316
317         /* Cage geom needs to be offseted to avoid Z-fighting. */
318         grp = *r_face_cage_shgrp = DRW_shgroup_create_sub(*r_face_shgrp);
319         DRW_shgroup_state_enable(grp, DRW_STATE_OFFSET_NEGATIVE);
320
321         /* Edges */
322         grp = *r_edge_shgrp = DRW_shgroup_create(edge_sh, pass);
323         DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
324         DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
325         DRW_shgroup_uniform_vec2(grp, "viewportSizeInv", DRW_viewport_invert_size_get(), 1);
326         DRW_shgroup_uniform_ivec4(grp, "dataMask", data_mask, 1);
327         DRW_shgroup_uniform_bool_copy(grp, "doEdges", do_edges);
328         DRW_shgroup_uniform_float_copy(grp, "ofs", depth_ofs);
329         DRW_shgroup_uniform_float_copy(grp, "edgeScale", select_edge ? 1.75f : 1.0f);
330         DRW_shgroup_state_enable(grp, DRW_STATE_OFFSET_NEGATIVE);
331         /* To match blender loop structure. */
332         DRW_shgroup_state_enable(grp, DRW_STATE_FIRST_VERTEX_CONVENTION);
333         if (rv3d->rflag & RV3D_CLIPPING) {
334                 DRW_shgroup_world_clip_planes_from_rv3d(grp, rv3d);
335         }
336
337         /* Verts */
338         if (select_vert) {
339                 grp = *r_vert_shgrp = DRW_shgroup_create(vert_sh, pass);
340                 DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
341                 DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
342                 DRW_shgroup_uniform_float_copy(grp, "ofs", depth_ofs * 1.5f);
343                 DRW_shgroup_state_enable(grp, DRW_STATE_OFFSET_NEGATIVE | DRW_STATE_WRITE_DEPTH);
344                 DRW_shgroup_state_disable(grp, DRW_STATE_BLEND);
345                 if (rv3d->rflag & RV3D_CLIPPING) {
346                         DRW_shgroup_world_clip_planes_from_rv3d(grp, rv3d);
347                 }
348         }
349
350         return pass;
351 }
352
353 static float backwire_opacity;
354 static float face_mod;
355 static float size_normal;
356
357 static void EDIT_MESH_cache_init(void *vedata)
358 {
359         EDIT_MESH_PassList *psl = ((EDIT_MESH_Data *)vedata)->psl;
360         EDIT_MESH_StorageList *stl = ((EDIT_MESH_Data *)vedata)->stl;
361         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
362
363         const DRWContextState *draw_ctx = DRW_context_state_get();
364         View3D *v3d = draw_ctx->v3d;
365         RegionView3D *rv3d = draw_ctx->rv3d;
366         Scene *scene = draw_ctx->scene;
367         ToolSettings *tsettings = scene->toolsettings;
368         EDIT_MESH_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
369         static float zero = 0.0f;
370
371         if (!stl->g_data) {
372                 /* Alloc transient pointers */
373                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
374         }
375         stl->g_data->ghost_ob = 0;
376         stl->g_data->edit_ob = 0;
377         stl->g_data->do_faces = true;
378         stl->g_data->do_edges = true;
379
380         stl->g_data->do_zbufclip = ((v3d)->shading.flag & XRAY_FLAG(v3d)) != 0;
381
382         stl->g_data->data_mask[0] = 0xFF; /* Face Flag */
383         stl->g_data->data_mask[1] = 0xFF; /* Edge Flag */
384         stl->g_data->data_mask[2] = 0xFF; /* Crease */
385         stl->g_data->data_mask[3] = 0xFF; /* BWeight */
386
387         if (draw_ctx->object_edit->type == OB_MESH) {
388                 if (BKE_object_is_in_editmode(draw_ctx->object_edit)) {
389                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FREESTYLE_FACE) == 0) {
390                                 stl->g_data->data_mask[0] &= ~VFLAG_FACE_FREESTYLE;
391                         }
392                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FACES) == 0) {
393                                 stl->g_data->data_mask[0] &= ~(VFLAG_FACE_SELECTED & VFLAG_FACE_FREESTYLE);
394                                 stl->g_data->do_faces = false;
395                         }
396                         if ((tsettings->selectmode & SCE_SELECT_FACE) == 0) {
397                                 stl->g_data->data_mask[0] &= ~VFLAG_FACE_ACTIVE;
398                         }
399                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_SEAMS) == 0) {
400                                 stl->g_data->data_mask[1] &= ~VFLAG_EDGE_SEAM;
401                         }
402                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_SHARP) == 0) {
403                                 stl->g_data->data_mask[1] &= ~VFLAG_EDGE_SHARP;
404                         }
405                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FREESTYLE_EDGE) == 0) {
406                                 stl->g_data->data_mask[1] &= ~VFLAG_EDGE_FREESTYLE;
407                         }
408                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_EDGES) == 0) {
409                                 if ((tsettings->selectmode & SCE_SELECT_EDGE) == 0) {
410                                         stl->g_data->data_mask[1] &= ~(VFLAG_EDGE_ACTIVE & VFLAG_EDGE_SELECTED);
411                                         stl->g_data->do_edges = false;
412                                 }
413                         }
414                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_CREASES) == 0) {
415                                 stl->g_data->data_mask[2] = 0x0;
416                         }
417                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_BWEIGHTS) == 0) {
418                                 stl->g_data->data_mask[3] = 0x0;
419                         }
420                 }
421         }
422
423         {
424                 psl->weight_faces = DRW_pass_create(
425                         "Weight Pass",
426                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL);
427
428                 stl->g_data->fweights_shgrp = DRW_shgroup_create(sh_data->weight_face, psl->weight_faces);
429
430                 static float alpha = 1.0f;
431                 DRW_shgroup_uniform_float(stl->g_data->fweights_shgrp, "opacity", &alpha, 1);
432                 DRW_shgroup_uniform_texture(stl->g_data->fweights_shgrp, "colorramp", G_draw.weight_ramp);
433                 DRW_shgroup_uniform_block(stl->g_data->fweights_shgrp, "globalsBlock", G_draw.block_ubo);
434                 if (rv3d->rflag & RV3D_CLIPPING) {
435                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->fweights_shgrp, rv3d);
436                 }
437         }
438
439         {
440                 /* Complementary Depth Pass */
441                 psl->depth_hidden_wire = DRW_pass_create(
442                         "Depth Pass Hidden Wire",
443                         DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CULL_BACK);
444                 stl->g_data->depth_shgrp_hidden_wire = DRW_shgroup_create(sh_data->depth, psl->depth_hidden_wire);
445                 if (rv3d->rflag & RV3D_CLIPPING) {
446                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->fweights_shgrp, rv3d);
447                 }
448         }
449
450         {
451                 /* Depth clearing for ghosting. */
452                 psl->ghost_clear_depth = DRW_pass_create(
453                         "Ghost Depth Clear",
454                         DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS | DRW_STATE_STENCIL_NEQUAL);
455
456                 DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->ghost_clear_depth, psl->ghost_clear_depth);
457                 DRW_shgroup_stencil_mask(shgrp, 0x00);
458                 DRW_shgroup_call_add(shgrp, DRW_cache_fullscreen_quad_get(), NULL);
459         }
460
461         {
462                 /* Normals */
463                 psl->normals = DRW_pass_create(
464                         "Edit Mesh Normals Pass",
465                         DRW_STATE_WRITE_DEPTH | DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL);
466
467                 stl->g_data->fnormals_shgrp = DRW_shgroup_create(sh_data->normals_face, psl->normals);
468                 DRW_shgroup_uniform_float(stl->g_data->fnormals_shgrp, "normalSize", &size_normal, 1);
469                 DRW_shgroup_uniform_vec4(stl->g_data->fnormals_shgrp, "color", G_draw.block.colorNormal, 1);
470                 if (rv3d->rflag & RV3D_CLIPPING) {
471                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->fnormals_shgrp, rv3d);
472                 }
473
474                 stl->g_data->vnormals_shgrp = DRW_shgroup_create(sh_data->normals, psl->normals);
475                 DRW_shgroup_uniform_float(stl->g_data->vnormals_shgrp, "normalSize", &size_normal, 1);
476                 DRW_shgroup_uniform_vec4(stl->g_data->vnormals_shgrp, "color", G_draw.block.colorVNormal, 1);
477                 if (rv3d->rflag & RV3D_CLIPPING) {
478                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->vnormals_shgrp, rv3d);
479                 }
480
481                 stl->g_data->lnormals_shgrp = DRW_shgroup_create(sh_data->normals_loop, psl->normals);
482                 DRW_shgroup_uniform_float(stl->g_data->lnormals_shgrp, "normalSize", &size_normal, 1);
483                 DRW_shgroup_uniform_vec4(stl->g_data->lnormals_shgrp, "color", G_draw.block.colorLNormal, 1);
484                 if (rv3d->rflag & RV3D_CLIPPING) {
485                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->lnormals_shgrp, rv3d);
486                 }
487         }
488
489         if (!stl->g_data->do_zbufclip) {
490                 psl->edit_face_overlay = edit_mesh_create_overlay_pass(
491                         &face_mod, stl->g_data->data_mask, stl->g_data->do_edges, false,
492                         DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND,
493                         &stl->g_data->face_shgrp,
494                         &stl->g_data->face_cage_shgrp,
495                         &stl->g_data->facedot_shgrp,
496                         &stl->g_data->edge_shgrp,
497                         &stl->g_data->vert_shgrp);
498         }
499         else {
500                 /* We render all wires with depth and opaque to a new fbo and blend the result based on depth values */
501                 psl->edit_face_occluded = edit_mesh_create_overlay_pass(
502                         &zero, stl->g_data->data_mask, stl->g_data->do_edges, true,
503                         DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WRITE_DEPTH,
504                         &stl->g_data->face_shgrp,
505                         &stl->g_data->face_cage_shgrp,
506                         &stl->g_data->facedot_shgrp,
507                         &stl->g_data->edge_shgrp,
508                         &stl->g_data->vert_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(sh_data->overlay_facefill, psl->facefill_occlude);
516                 DRW_shgroup_uniform_block(stl->g_data->facefill_occluded_shgrp, "globalsBlock", G_draw.block_ubo);
517                 DRW_shgroup_uniform_ivec4(stl->g_data->facefill_occluded_shgrp, "dataMask", stl->g_data->data_mask, 1);
518                 if (rv3d->rflag & RV3D_CLIPPING) {
519                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->facefill_occluded_shgrp, rv3d);
520                 }
521
522                 /* we need a full screen pass to combine the result */
523                 struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
524
525                 psl->mix_occlude = DRW_pass_create(
526                         "Mix Occluded Wires",
527                         DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND);
528                 DRWShadingGroup *mix_shgrp = DRW_shgroup_create(sh_data->overlay_mix, psl->mix_occlude);
529                 DRW_shgroup_call_add(mix_shgrp, quad, NULL);
530                 DRW_shgroup_uniform_float(mix_shgrp, "alpha", &backwire_opacity, 1);
531                 DRW_shgroup_uniform_texture_ref(mix_shgrp, "wireColor", &e_data.occlude_wire_color_tx);
532                 DRW_shgroup_uniform_texture_ref(mix_shgrp, "wireDepth", &e_data.occlude_wire_depth_tx);
533                 DRW_shgroup_uniform_texture_ref(mix_shgrp, "sceneDepth", &dtxl->depth);
534         }
535 }
536
537 static void edit_mesh_add_ob_to_pass(
538         Scene *scene, Object *ob,
539         EDIT_MESH_PrivateData *g_data,
540         DRWShadingGroup *facedot_shgrp,
541         DRWShadingGroup *facefill_shgrp)
542 {
543         struct GPUBatch *geom_tris, *geom_verts, *geom_edges, *geom_fcenter;
544         ToolSettings *tsettings = scene->toolsettings;
545
546         bool has_edit_mesh_cage = false;
547         /* TODO: Should be its own function. */
548         Mesh *me = (Mesh *)ob->data;
549         BMEditMesh *embm = me->edit_mesh;
550         if (embm) {
551                 has_edit_mesh_cage = embm->mesh_eval_cage && (embm->mesh_eval_cage != embm->mesh_eval_final);
552         }
553
554         DRWShadingGroup *face_shgrp = (has_edit_mesh_cage) ? g_data->face_cage_shgrp : g_data->face_shgrp;
555         DRWShadingGroup *vert_shgrp = g_data->vert_shgrp;
556         DRWShadingGroup *edge_shgrp = g_data->edge_shgrp;
557
558         face_shgrp = (facefill_shgrp != NULL) ? facefill_shgrp : face_shgrp;
559
560         geom_tris = DRW_mesh_batch_cache_get_edit_triangles(ob->data);
561         geom_edges = DRW_mesh_batch_cache_get_edit_edges(ob->data);
562         DRW_shgroup_call_add(edge_shgrp, geom_edges, ob->obmat);
563         DRW_shgroup_call_add(face_shgrp, geom_tris, ob->obmat);
564
565         if ((tsettings->selectmode & SCE_SELECT_VERTEX) != 0) {
566                 geom_verts = DRW_mesh_batch_cache_get_edit_vertices(ob->data);
567                 DRW_shgroup_call_add(vert_shgrp, geom_verts, ob->obmat);
568         }
569
570         if (facedot_shgrp && (tsettings->selectmode & SCE_SELECT_FACE) != 0) {
571                 geom_fcenter = DRW_mesh_batch_cache_get_edit_facedots(ob->data);
572                 DRW_shgroup_call_add(facedot_shgrp, geom_fcenter, ob->obmat);
573         }
574 }
575
576 static void EDIT_MESH_cache_populate(void *vedata, Object *ob)
577 {
578         EDIT_MESH_StorageList *stl = ((EDIT_MESH_Data *)vedata)->stl;
579         const DRWContextState *draw_ctx = DRW_context_state_get();
580         View3D *v3d = draw_ctx->v3d;
581         Scene *scene = draw_ctx->scene;
582         ToolSettings *tsettings = scene->toolsettings;
583         struct GPUBatch *geom;
584
585         if (ob->type == OB_MESH) {
586                 if ((ob == draw_ctx->object_edit) || BKE_object_is_in_editmode(ob)) {
587                         bool do_occlude_wire = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_OCCLUDE_WIRE) != 0;
588                         bool do_show_weight = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_WEIGHT) != 0;
589                         bool fnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FACE_NORMALS) != 0;
590                         bool vnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_VERT_NORMALS) != 0;
591                         bool lnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_LOOP_NORMALS) != 0;
592
593                         bool show_face_dots = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FACE_DOT) != 0;
594
595                         if (stl->g_data->do_faces == false &&
596                             stl->g_data->do_edges == false &&
597                             (tsettings->selectmode & SCE_SELECT_FACE))
598                         {
599                                 /* Force display of face centers in this case because that's
600                                  * the only way to see if a face is selected. */
601                                 show_face_dots = true;
602                         }
603
604                         /* Updating uniform */
605                         backwire_opacity = v3d->overlay.backwire_opacity;
606                         size_normal = v3d->overlay.normals_length;
607
608                         face_mod = (do_occlude_wire) ? 0.0f : 1.0f;
609
610                         if (!stl->g_data->do_faces) {
611                                 face_mod = 0.0f;
612                         }
613
614                         if (do_show_weight) {
615                                 geom = DRW_cache_mesh_surface_weights_get(ob);
616                                 DRW_shgroup_call_add(stl->g_data->fweights_shgrp, geom, ob->obmat);
617                         }
618
619                         if (do_occlude_wire) {
620                                 geom = DRW_cache_mesh_surface_get(ob);
621                                 DRW_shgroup_call_add(stl->g_data->depth_shgrp_hidden_wire, geom, ob->obmat);
622                         }
623
624                         if (vnormals_do) {
625                                 geom = DRW_mesh_batch_cache_get_edit_vertices(ob->data);
626                                 DRW_shgroup_call_add(stl->g_data->vnormals_shgrp, geom, ob->obmat);
627                         }
628                         if (lnormals_do) {
629                                 geom = DRW_mesh_batch_cache_get_edit_lnors(ob->data);
630                                 DRW_shgroup_call_add(stl->g_data->lnormals_shgrp, geom, ob->obmat);
631                         }
632                         if (fnormals_do) {
633                                 geom = DRW_mesh_batch_cache_get_edit_facedots(ob->data);
634                                 DRW_shgroup_call_add(stl->g_data->fnormals_shgrp, geom, ob->obmat);
635                         }
636
637                         if (stl->g_data->do_zbufclip) {
638                                 edit_mesh_add_ob_to_pass(
639                                         scene, ob, stl->g_data,
640                                         stl->g_data->facedot_shgrp,
641                                         (stl->g_data->do_faces) ? stl->g_data->facefill_occluded_shgrp : NULL);
642                         }
643                         else {
644                                 edit_mesh_add_ob_to_pass(
645                                         scene, ob, stl->g_data,
646                                         (show_face_dots) ? stl->g_data->facedot_shgrp : NULL,
647                                         NULL);
648                         }
649
650                         stl->g_data->ghost_ob += (ob->dtx & OB_DRAWXRAY) ? 1 : 0;
651                         stl->g_data->edit_ob += 1;
652
653                         /* 3D text overlay */
654                         if (v3d->overlay.edit_flag & (V3D_OVERLAY_EDIT_EDGE_LEN |
655                                                       V3D_OVERLAY_EDIT_FACE_AREA |
656                                                       V3D_OVERLAY_EDIT_FACE_ANG |
657                                                       V3D_OVERLAY_EDIT_EDGE_ANG |
658                                                       V3D_OVERLAY_EDIT_INDICES))
659                         {
660                                 if (DRW_state_show_text()) {
661                                         DRW_edit_mesh_mode_text_measure_stats(
662                                                draw_ctx->ar, v3d, ob, &scene->unit);
663                                 }
664                         }
665                 }
666         }
667 }
668
669 static void EDIT_MESH_draw_scene(void *vedata)
670 {
671         EDIT_MESH_PassList *psl = ((EDIT_MESH_Data *)vedata)->psl;
672         EDIT_MESH_StorageList *stl = ((EDIT_MESH_Data *)vedata)->stl;
673         EDIT_MESH_FramebufferList *fbl = ((EDIT_MESH_Data *)vedata)->fbl;
674         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
675         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
676
677         DRW_draw_pass(psl->weight_faces);
678
679         DRW_draw_pass(psl->depth_hidden_wire);
680
681         if (stl->g_data->do_zbufclip) {
682                 float clearcol[4] = {0.0f, 0.0f, 0.0f, 0.0f};
683                 /* render facefill */
684                 DRW_draw_pass(psl->facefill_occlude);
685
686                 /* Render wires on a separate framebuffer */
687                 GPU_framebuffer_bind(fbl->occlude_wire_fb);
688                 GPU_framebuffer_clear_color_depth(fbl->occlude_wire_fb, clearcol, 1.0f);
689                 DRW_draw_pass(psl->normals);
690                 DRW_draw_pass(psl->edit_face_occluded);
691
692                 /* Combine with scene buffer */
693                 GPU_framebuffer_bind(dfbl->color_only_fb);
694                 DRW_draw_pass(psl->mix_occlude);
695         }
696         else {
697                 DRW_draw_pass(psl->normals);
698
699                 const DRWContextState *draw_ctx = DRW_context_state_get();
700                 View3D *v3d = draw_ctx->v3d;
701
702                 if (v3d->shading.type == OB_SOLID && (v3d->shading.flag & XRAY_FLAG(v3d)) == 0 &&
703                     stl->g_data->ghost_ob == 1 && stl->g_data->edit_ob == 1)
704                 {
705                         /* In the case of single ghost object edit (common case for retopology):
706                          * we duplicate the depht+stencil buffer and clear all depth to 1.0f where
707                          * the stencil buffer is no 0x00. */
708                         const float *viewport_size = DRW_viewport_size_get();
709                         const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
710                         struct GPUTexture *ghost_depth_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_DEPTH24_STENCIL8, &draw_engine_edit_mesh_type);
711                         GPU_framebuffer_ensure_config(&fbl->ghost_wire_fb, {
712                                 GPU_ATTACHMENT_TEXTURE(ghost_depth_tx),
713                                 GPU_ATTACHMENT_TEXTURE(dtxl->color),
714                         });
715
716                         GPU_framebuffer_blit(dfbl->depth_only_fb, 0, fbl->ghost_wire_fb, 0, GPU_DEPTH_BIT | GPU_STENCIL_BIT);
717                         GPU_framebuffer_bind(fbl->ghost_wire_fb);
718
719                         DRW_draw_pass(psl->ghost_clear_depth);
720
721                         DRW_draw_pass(psl->edit_face_overlay);
722                 }
723                 else {
724                         // MULTISAMPLE_SYNC_ENABLE(dfbl, dtxl);
725
726                         DRW_draw_pass(psl->edit_face_overlay);
727
728                         // MULTISAMPLE_SYNC_DISABLE(dfbl, dtxl)
729                 }
730         }
731
732         DRW_state_clip_planes_reset();
733 }
734
735 static void EDIT_MESH_engine_free(void)
736 {
737         for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
738                 EDIT_MESH_Shaders *sh_data = &e_data.sh_data[sh_data_index];
739                 /* Don't free builtins. */
740                 sh_data->depth = NULL;
741                 GPUShader **sh_data_as_array = (GPUShader **)sh_data;
742                 for (int i = 0; i < (sizeof(EDIT_MESH_Shaders) / sizeof(GPUShader *)); i++) {
743                         DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
744                 }
745         }
746 }
747
748 static const DrawEngineDataSize EDIT_MESH_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_MESH_Data);
749
750 DrawEngineType draw_engine_edit_mesh_type = {
751         NULL, NULL,
752         N_("EditMeshMode"),
753         &EDIT_MESH_data_size,
754         &EDIT_MESH_engine_init,
755         &EDIT_MESH_engine_free,
756         &EDIT_MESH_cache_init,
757         &EDIT_MESH_cache_populate,
758         NULL,
759         NULL,
760         &EDIT_MESH_draw_scene,
761         NULL,
762         NULL,
763 };