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