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