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