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                                 stl->g_data->do_zbufclip = false;
404                         }
405                         if ((tsettings->selectmode & SCE_SELECT_FACE) == 0) {
406                                 stl->g_data->data_mask[0] &= ~VFLAG_FACE_ACTIVE;
407                         }
408                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_SEAMS) == 0) {
409                                 stl->g_data->data_mask[1] &= ~VFLAG_EDGE_SEAM;
410                         }
411                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_SHARP) == 0) {
412                                 stl->g_data->data_mask[1] &= ~VFLAG_EDGE_SHARP;
413                         }
414                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FREESTYLE_EDGE) == 0) {
415                                 stl->g_data->data_mask[1] &= ~VFLAG_EDGE_FREESTYLE;
416                         }
417                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_EDGES) == 0) {
418                                 if ((tsettings->selectmode & SCE_SELECT_EDGE) == 0) {
419                                         stl->g_data->data_mask[1] &= ~(VFLAG_EDGE_ACTIVE & VFLAG_EDGE_SELECTED);
420                                         stl->g_data->do_edges = false;
421                                 }
422                         }
423                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_CREASES) == 0) {
424                                 stl->g_data->data_mask[2] = 0x0;
425                         }
426                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_BWEIGHTS) == 0) {
427                                 stl->g_data->data_mask[3] = 0x0;
428                         }
429                 }
430         }
431
432         {
433                 psl->weight_faces = DRW_pass_create(
434                         "Weight Pass",
435                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL);
436
437                 stl->g_data->fweights_shgrp = DRW_shgroup_create(sh_data->weight_face, psl->weight_faces);
438
439                 static float alpha = 1.0f;
440                 DRW_shgroup_uniform_float(stl->g_data->fweights_shgrp, "opacity", &alpha, 1);
441                 DRW_shgroup_uniform_texture(stl->g_data->fweights_shgrp, "colorramp", G_draw.weight_ramp);
442                 DRW_shgroup_uniform_block(stl->g_data->fweights_shgrp, "globalsBlock", G_draw.block_ubo);
443                 if (rv3d->rflag & RV3D_CLIPPING) {
444                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->fweights_shgrp, rv3d);
445                 }
446         }
447
448         {
449                 /* Complementary Depth Pass */
450                 psl->depth_hidden_wire = DRW_pass_create(
451                         "Depth Pass Hidden Wire",
452                         DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CULL_BACK);
453                 stl->g_data->depth_shgrp_hidden_wire = DRW_shgroup_create(sh_data->depth, psl->depth_hidden_wire);
454                 if (rv3d->rflag & RV3D_CLIPPING) {
455                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->depth_shgrp_hidden_wire, rv3d);
456                 }
457
458                 psl->depth_hidden_wire_in_front = DRW_pass_create(
459                         "Depth Pass Hidden Wire In Front",
460                         DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CULL_BACK);
461                 stl->g_data->depth_shgrp_hidden_wire_in_front = DRW_shgroup_create(sh_data->depth, psl->depth_hidden_wire_in_front);
462                 if (rv3d->rflag & RV3D_CLIPPING) {
463                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->depth_shgrp_hidden_wire_in_front, rv3d);
464                 }
465         }
466
467         {
468                 /* Normals */
469                 psl->normals = DRW_pass_create(
470                         "Edit Mesh Normals Pass",
471                         DRW_STATE_WRITE_DEPTH | DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL);
472
473                 stl->g_data->fnormals_shgrp = DRW_shgroup_create(sh_data->normals_face, psl->normals);
474                 DRW_shgroup_uniform_float(stl->g_data->fnormals_shgrp, "normalSize", &size_normal, 1);
475                 DRW_shgroup_uniform_vec4(stl->g_data->fnormals_shgrp, "color", G_draw.block.colorNormal, 1);
476                 if (rv3d->rflag & RV3D_CLIPPING) {
477                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->fnormals_shgrp, rv3d);
478                 }
479
480                 stl->g_data->vnormals_shgrp = DRW_shgroup_create(sh_data->normals, psl->normals);
481                 DRW_shgroup_uniform_float(stl->g_data->vnormals_shgrp, "normalSize", &size_normal, 1);
482                 DRW_shgroup_uniform_vec4(stl->g_data->vnormals_shgrp, "color", G_draw.block.colorVNormal, 1);
483                 if (rv3d->rflag & RV3D_CLIPPING) {
484                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->vnormals_shgrp, rv3d);
485                 }
486
487                 stl->g_data->lnormals_shgrp = DRW_shgroup_create(sh_data->normals_loop, psl->normals);
488                 DRW_shgroup_uniform_float(stl->g_data->lnormals_shgrp, "normalSize", &size_normal, 1);
489                 DRW_shgroup_uniform_vec4(stl->g_data->lnormals_shgrp, "color", G_draw.block.colorLNormal, 1);
490                 if (rv3d->rflag & RV3D_CLIPPING) {
491                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->lnormals_shgrp, rv3d);
492                 }
493         }
494
495         /* For in front option */
496         psl->edit_face_overlay_in_front = edit_mesh_create_overlay_pass(
497                 &face_mod, stl->g_data->data_mask, stl->g_data->do_edges,
498                 DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND,
499                 &stl->g_data->face_shgrp_in_front,
500                 &stl->g_data->face_cage_shgrp_in_front,
501                 &stl->g_data->facedot_shgrp_in_front,
502                 &stl->g_data->edge_shgrp_in_front,
503                 &stl->g_data->vert_shgrp_in_front);
504
505         if (!stl->g_data->do_zbufclip) {
506                 psl->edit_face_overlay = edit_mesh_create_overlay_pass(
507                         &face_mod, stl->g_data->data_mask, stl->g_data->do_edges,
508                         DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND,
509                         &stl->g_data->face_shgrp,
510                         &stl->g_data->face_cage_shgrp,
511                         &stl->g_data->facedot_shgrp,
512                         &stl->g_data->edge_shgrp,
513                         &stl->g_data->vert_shgrp);
514         }
515         else {
516                 /* We render all wires with depth and opaque to a new fbo and blend the result based on depth values */
517                 psl->edit_face_occluded = edit_mesh_create_overlay_pass(
518                         &zero, stl->g_data->data_mask, stl->g_data->do_edges,
519                         DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WRITE_DEPTH,
520                         &stl->g_data->face_shgrp,
521                         &stl->g_data->face_cage_shgrp,
522                         &stl->g_data->facedot_shgrp,
523                         &stl->g_data->edge_shgrp,
524                         &stl->g_data->vert_shgrp);
525
526                 /* however we loose the front faces value (because we need the depth of occluded wires and
527                  * faces are alpha blended ) so we recover them in a new pass. */
528                 psl->facefill_occlude = DRW_pass_create(
529                         "Front Face Color",
530                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
531                 stl->g_data->facefill_occluded_shgrp = DRW_shgroup_create(sh_data->overlay_facefill, psl->facefill_occlude);
532                 DRW_shgroup_uniform_block(stl->g_data->facefill_occluded_shgrp, "globalsBlock", G_draw.block_ubo);
533                 DRW_shgroup_uniform_ivec4(stl->g_data->facefill_occluded_shgrp, "dataMask", stl->g_data->data_mask, 1);
534                 if (rv3d->rflag & RV3D_CLIPPING) {
535                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->facefill_occluded_shgrp, rv3d);
536                 }
537
538                 /* we need a full screen pass to combine the result */
539                 struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
540
541                 psl->mix_occlude = DRW_pass_create(
542                         "Mix Occluded Wires",
543                         DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND);
544                 DRWShadingGroup *mix_shgrp = DRW_shgroup_create(sh_data->overlay_mix, psl->mix_occlude);
545                 DRW_shgroup_call_add(mix_shgrp, quad, NULL);
546                 DRW_shgroup_uniform_float(mix_shgrp, "alpha", &backwire_opacity, 1);
547                 DRW_shgroup_uniform_texture_ref(mix_shgrp, "wireColor", &e_data.occlude_wire_color_tx);
548                 DRW_shgroup_uniform_texture_ref(mix_shgrp, "wireDepth", &e_data.occlude_wire_depth_tx);
549                 DRW_shgroup_uniform_texture_ref(mix_shgrp, "sceneDepth", &dtxl->depth);
550         }
551 }
552
553 static void edit_mesh_add_ob_to_pass(
554         Scene *scene, Object *ob,
555         DRWShadingGroup *vert_shgrp,
556         DRWShadingGroup *edge_shgrp,
557         DRWShadingGroup *face_shgrp,
558         DRWShadingGroup *face_cage_shgrp,
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_mesh;
569         if (embm) {
570                 has_edit_mesh_cage = embm->mesh_eval_cage && (embm->mesh_eval_cage != embm->mesh_eval_final);
571         }
572
573         face_shgrp = (has_edit_mesh_cage) ? face_cage_shgrp : face_shgrp;
574         face_shgrp = (facefill_shgrp != NULL) ? facefill_shgrp : face_shgrp;
575
576         geom_tris = DRW_mesh_batch_cache_get_edit_triangles(ob->data);
577         geom_edges = DRW_mesh_batch_cache_get_edit_edges(ob->data);
578         DRW_shgroup_call_add(edge_shgrp, geom_edges, ob->obmat);
579         DRW_shgroup_call_add(face_shgrp, geom_tris, ob->obmat);
580
581         if ((tsettings->selectmode & SCE_SELECT_VERTEX) != 0) {
582                 geom_verts = DRW_mesh_batch_cache_get_edit_vertices(ob->data);
583                 DRW_shgroup_call_add(vert_shgrp, geom_verts, ob->obmat);
584         }
585
586         if (facedot_shgrp && (tsettings->selectmode & SCE_SELECT_FACE) != 0) {
587                 geom_fcenter = DRW_mesh_batch_cache_get_edit_facedots(ob->data);
588                 DRW_shgroup_call_add(facedot_shgrp, geom_fcenter, ob->obmat);
589         }
590 }
591
592 static void EDIT_MESH_cache_populate(void *vedata, Object *ob)
593 {
594         EDIT_MESH_StorageList *stl = ((EDIT_MESH_Data *)vedata)->stl;
595         EDIT_MESH_PrivateData *g_data = stl->g_data;
596         const DRWContextState *draw_ctx = DRW_context_state_get();
597         View3D *v3d = draw_ctx->v3d;
598         Scene *scene = draw_ctx->scene;
599         ToolSettings *tsettings = scene->toolsettings;
600         struct GPUBatch *geom;
601
602         if (ob->type == OB_MESH) {
603                 if ((ob == draw_ctx->object_edit) || BKE_object_is_in_editmode(ob)) {
604                         bool do_in_front = (ob->dtx & OB_DRAWXRAY) != 0;
605                         bool do_occlude_wire = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_OCCLUDE_WIRE) != 0;
606                         bool do_show_weight = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_WEIGHT) != 0;
607                         bool fnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FACE_NORMALS) != 0;
608                         bool vnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_VERT_NORMALS) != 0;
609                         bool lnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_LOOP_NORMALS) != 0;
610
611                         bool show_face_dots = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FACE_DOT) != 0;
612
613                         if (g_data->do_faces == false &&
614                             g_data->do_edges == false &&
615                             (tsettings->selectmode & SCE_SELECT_FACE))
616                         {
617                                 /* Force display of face centers in this case because that's
618                                  * the only way to see if a face is selected. */
619                                 show_face_dots = true;
620                         }
621
622                         /* Updating uniform */
623                         backwire_opacity = v3d->overlay.backwire_opacity;
624                         size_normal = v3d->overlay.normals_length;
625
626                         face_mod = (do_occlude_wire) ? 0.0f : 1.0f;
627
628                         if (!g_data->do_faces) {
629                                 face_mod = 0.0f;
630                         }
631
632                         if (do_show_weight) {
633                                 geom = DRW_cache_mesh_surface_weights_get(ob);
634                                 DRW_shgroup_call_add(g_data->fweights_shgrp, geom, ob->obmat);
635                         }
636
637                         if (do_occlude_wire || do_in_front) {
638                                 geom = DRW_cache_mesh_surface_get(ob);
639                                 DRW_shgroup_call_add(do_in_front ? g_data->depth_shgrp_hidden_wire_in_front
640                                                                  : g_data->depth_shgrp_hidden_wire,
641                                                      geom, ob->obmat);
642                         }
643
644                         if (vnormals_do) {
645                                 geom = DRW_mesh_batch_cache_get_edit_vertices(ob->data);
646                                 DRW_shgroup_call_add(g_data->vnormals_shgrp, geom, ob->obmat);
647                         }
648                         if (lnormals_do) {
649                                 geom = DRW_mesh_batch_cache_get_edit_lnors(ob->data);
650                                 DRW_shgroup_call_add(g_data->lnormals_shgrp, geom, ob->obmat);
651                         }
652                         if (fnormals_do) {
653                                 geom = DRW_mesh_batch_cache_get_edit_facedots(ob->data);
654                                 DRW_shgroup_call_add(g_data->fnormals_shgrp, geom, ob->obmat);
655                         }
656
657                         if (g_data->do_zbufclip) {
658                                 edit_mesh_add_ob_to_pass(
659                                         scene, ob,
660                                         g_data->vert_shgrp,
661                                         g_data->edge_shgrp,
662                                         g_data->face_shgrp,
663                                         g_data->face_cage_shgrp,
664                                         g_data->facedot_shgrp,
665                                         (g_data->do_faces) ? g_data->facefill_occluded_shgrp : NULL);
666                         }
667                         else if (do_in_front) {
668                                 edit_mesh_add_ob_to_pass(
669                                         scene, ob,
670                                         g_data->vert_shgrp_in_front,
671                                         g_data->edge_shgrp_in_front,
672                                         g_data->face_shgrp_in_front,
673                                         g_data->face_cage_shgrp_in_front,
674                                         (show_face_dots) ? g_data->facedot_shgrp_in_front : NULL,
675                                         NULL);
676                         }
677                         else {
678                                 edit_mesh_add_ob_to_pass(
679                                         scene, ob,
680                                         g_data->vert_shgrp,
681                                         g_data->edge_shgrp,
682                                         g_data->face_shgrp,
683                                         g_data->face_cage_shgrp,
684                                         (show_face_dots) ? g_data->facedot_shgrp : NULL,
685                                         NULL);
686                         }
687
688                         g_data->ghost_ob += (ob->dtx & OB_DRAWXRAY) ? 1 : 0;
689                         g_data->edit_ob += 1;
690
691                         /* 3D text overlay */
692                         if (v3d->overlay.edit_flag & (V3D_OVERLAY_EDIT_EDGE_LEN |
693                                                       V3D_OVERLAY_EDIT_FACE_AREA |
694                                                       V3D_OVERLAY_EDIT_FACE_ANG |
695                                                       V3D_OVERLAY_EDIT_EDGE_ANG |
696                                                       V3D_OVERLAY_EDIT_INDICES))
697                         {
698                                 if (DRW_state_show_text()) {
699                                         DRW_edit_mesh_mode_text_measure_stats(
700                                                draw_ctx->ar, v3d, ob, &scene->unit);
701                                 }
702                         }
703                 }
704         }
705 }
706
707 static void EDIT_MESH_draw_scene(void *vedata)
708 {
709         EDIT_MESH_PassList *psl = ((EDIT_MESH_Data *)vedata)->psl;
710         EDIT_MESH_StorageList *stl = ((EDIT_MESH_Data *)vedata)->stl;
711         EDIT_MESH_FramebufferList *fbl = ((EDIT_MESH_Data *)vedata)->fbl;
712         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
713
714         DRW_draw_pass(psl->weight_faces);
715
716         DRW_draw_pass(psl->depth_hidden_wire);
717
718         if (stl->g_data->do_zbufclip) {
719                 float clearcol[4] = {0.0f, 0.0f, 0.0f, 0.0f};
720
721                 DRW_draw_pass(psl->depth_hidden_wire_in_front);
722
723                 /* render facefill */
724                 DRW_draw_pass(psl->facefill_occlude);
725
726                 /* Render wires on a separate framebuffer */
727                 GPU_framebuffer_bind(fbl->occlude_wire_fb);
728                 GPU_framebuffer_clear_color_depth(fbl->occlude_wire_fb, clearcol, 1.0f);
729                 DRW_draw_pass(psl->normals);
730                 DRW_draw_pass(psl->edit_face_occluded);
731
732                 /* Combine with scene buffer */
733                 GPU_framebuffer_bind(dfbl->color_only_fb);
734                 DRW_draw_pass(psl->mix_occlude);
735         }
736         else {
737                 const DRWContextState *draw_ctx = DRW_context_state_get();
738                 View3D *v3d = draw_ctx->v3d;
739
740                 DRW_draw_pass(psl->normals);
741                 DRW_draw_pass(psl->edit_face_overlay);
742
743                 if (v3d->shading.type == OB_SOLID && (v3d->shading.flag & XRAY_FLAG(v3d)) == 0 &&
744                     stl->g_data->ghost_ob == 1 && stl->g_data->edit_ob == 1)
745                 {
746                         /* In the case of single ghost object edit (common case for retopology):
747                          * we clear the depth buffer so that only the depth of the retopo mesh
748                          * is occluding the edit cage. */
749                         GPU_framebuffer_clear_depth(dfbl->default_fb, 1.0f);
750                 }
751
752                 DRW_draw_pass(psl->depth_hidden_wire_in_front);
753                 DRW_draw_pass(psl->edit_face_overlay_in_front);
754         }
755
756         DRW_state_clip_planes_reset();
757 }
758
759 static void EDIT_MESH_engine_free(void)
760 {
761         for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
762                 EDIT_MESH_Shaders *sh_data = &e_data.sh_data[sh_data_index];
763                 /* Don't free builtins. */
764                 sh_data->depth = NULL;
765                 GPUShader **sh_data_as_array = (GPUShader **)sh_data;
766                 for (int i = 0; i < (sizeof(EDIT_MESH_Shaders) / sizeof(GPUShader *)); i++) {
767                         DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
768                 }
769         }
770 }
771
772 static const DrawEngineDataSize EDIT_MESH_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_MESH_Data);
773
774 DrawEngineType draw_engine_edit_mesh_type = {
775         NULL, NULL,
776         N_("EditMeshMode"),
777         &EDIT_MESH_data_size,
778         &EDIT_MESH_engine_init,
779         &EDIT_MESH_engine_free,
780         &EDIT_MESH_cache_init,
781         &EDIT_MESH_cache_populate,
782         NULL,
783         NULL,
784         &EDIT_MESH_draw_scene,
785         NULL,
786         NULL,
787 };