a79c0e2dc3107242e6f0a2509644425f08124ea9
[blender.git] / source / blender / draw / modes / edit_mesh_mode.c
1 /*
2  * Copyright 2016, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Blender Institute
19  *
20  */
21
22 /** \file blender/draw/modes/edit_mesh_mode.c
23  *  \ingroup draw
24  */
25
26 #include "DRW_engine.h"
27 #include "DRW_render.h"
28
29 #include "GPU_shader.h"
30
31 #include "DNA_mesh_types.h"
32 #include "DNA_view3d_types.h"
33
34 #include "draw_common.h"
35
36 #include "draw_mode_engines.h"
37
38 #include "edit_mesh_mode_intern.h" /* own include */
39
40 #include "BKE_object.h"
41
42 #include "BLI_dynstr.h"
43
44
45 extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
46 extern struct GlobalsUboStorage ts; /* draw_common.c */
47
48 extern char datatoc_edit_mesh_overlay_common_lib_glsl[];
49 extern char datatoc_edit_mesh_overlay_frag_glsl[];
50 extern char datatoc_edit_mesh_overlay_vert_glsl[];
51 extern char datatoc_edit_mesh_overlay_geom_tri_glsl[];
52 extern char datatoc_edit_mesh_overlay_geom_edge_glsl[];
53 extern char datatoc_edit_mesh_overlay_loosevert_vert_glsl[];
54 extern char datatoc_edit_mesh_overlay_facedot_frag_glsl[];
55 extern char datatoc_edit_mesh_overlay_facedot_vert_glsl[];
56 extern char datatoc_edit_mesh_overlay_ghost_clear_vert_glsl[];
57 extern char datatoc_edit_mesh_overlay_mix_frag_glsl[];
58 extern char datatoc_edit_mesh_overlay_facefill_vert_glsl[];
59 extern char datatoc_edit_mesh_overlay_facefill_frag_glsl[];
60 extern char datatoc_edit_normals_vert_glsl[];
61 extern char datatoc_edit_normals_geom_glsl[];
62 extern char datatoc_common_globals_lib_glsl[];
63
64 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
65
66 /* *********** LISTS *********** */
67 typedef struct EDIT_MESH_PassList {
68         struct DRWPass *vcolor_faces;
69         struct DRWPass *depth_hidden_wire;
70         struct DRWPass *ghost_clear_depth;
71         struct DRWPass *edit_face_overlay;
72         struct DRWPass *edit_face_occluded;
73         struct DRWPass *mix_occlude;
74         struct DRWPass *facefill_occlude;
75         struct DRWPass *normals;
76 } EDIT_MESH_PassList;
77
78 typedef struct EDIT_MESH_FramebufferList {
79         struct GPUFrameBuffer *occlude_wire_fb;
80         struct GPUFrameBuffer *ghost_wire_fb;
81 } EDIT_MESH_FramebufferList;
82
83 typedef struct EDIT_MESH_StorageList {
84         struct EDIT_MESH_PrivateData *g_data;
85 } EDIT_MESH_StorageList;
86
87 typedef struct EDIT_MESH_Data {
88         void *engine_type;
89         EDIT_MESH_FramebufferList *fbl;
90         DRWViewportEmptyList *txl;
91         EDIT_MESH_PassList *psl;
92         EDIT_MESH_StorageList *stl;
93 } EDIT_MESH_Data;
94
95 /* *********** STATIC *********** */
96 #define MAX_SHADERS 16
97
98 static struct {
99         /* weight/vert-color */
100         GPUShader *vcolor_face_shader;
101
102         /* Geometry */
103         GPUShader *overlay_tri_sh_cache[MAX_SHADERS];
104         GPUShader *overlay_loose_edge_sh_cache[MAX_SHADERS];
105
106         GPUShader *overlay_vert_sh;
107         GPUShader *overlay_facedot_sh;
108         GPUShader *overlay_mix_sh;
109         GPUShader *overlay_facefill_sh;
110         GPUShader *normals_face_sh;
111         GPUShader *normals_loop_sh;
112         GPUShader *normals_sh;
113         GPUShader *depth_sh;
114         GPUShader *ghost_clear_depth_sh;
115         /* temp buffer texture */
116         struct GPUTexture *occlude_wire_depth_tx;
117         struct GPUTexture *occlude_wire_color_tx;
118 } e_data = {NULL}; /* Engine data */
119
120 typedef struct EDIT_MESH_PrivateData {
121         /* weight/vert-color */
122         DRWShadingGroup *fvcolor_shgrp;
123         DRWShadingGroup *depth_shgrp_hidden_wire;
124
125         DRWShadingGroup *fnormals_shgrp;
126         DRWShadingGroup *vnormals_shgrp;
127         DRWShadingGroup *lnormals_shgrp;
128
129         DRWShadingGroup *face_overlay_shgrp;
130         DRWShadingGroup *ledges_overlay_shgrp;
131         DRWShadingGroup *lverts_overlay_shgrp;
132         DRWShadingGroup *facedot_overlay_shgrp;
133
134         DRWShadingGroup *face_occluded_shgrp;
135         DRWShadingGroup *ledges_occluded_shgrp;
136         DRWShadingGroup *lverts_occluded_shgrp;
137         DRWShadingGroup *facedot_occluded_shgrp;
138         DRWShadingGroup *facefill_occluded_shgrp;
139
140         int ghost_ob;
141         int edit_ob;
142         bool do_zbufclip;
143 } EDIT_MESH_PrivateData; /* Transient data */
144
145 /* *********** FUNCTIONS *********** */
146 static int EDIT_MESH_sh_index(ToolSettings *tsettings, RegionView3D *rv3d, bool supports_fast_mode)
147 {
148         int result = tsettings->selectmode << 1;
149         if (supports_fast_mode) {
150                 SET_FLAG_FROM_TEST(result, (rv3d->rflag & RV3D_NAVIGATING), 1 << 0);
151         }
152         return result;
153 }
154
155 static char *EDIT_MESH_sh_defines(ToolSettings *tsettings, RegionView3D *rv3d, bool anti_alias, bool looseedge)
156 {
157         const int selectmode = tsettings->selectmode;
158         const int fast_mode = rv3d->rflag & RV3D_NAVIGATING;
159
160         char *str = NULL;
161         DynStr *ds = BLI_dynstr_new();
162
163         if (selectmode & SCE_SELECT_VERTEX) {
164                 BLI_dynstr_append(ds, "#define VERTEX_SELECTION\n");
165         }
166
167         if (selectmode & SCE_SELECT_EDGE) {
168                 BLI_dynstr_append(ds, "#define EDGE_SELECTION\n");
169         }
170
171         if (selectmode & SCE_SELECT_FACE) {
172                 BLI_dynstr_append(ds, "#define FACE_SELECTION\n");
173         }
174
175         if (!fast_mode) {
176                 BLI_dynstr_append(ds, "#define EDGE_FIX\n");
177         }
178
179         if (anti_alias) {
180                 BLI_dynstr_append(ds, "#define ANTI_ALIASING\n");
181         }
182
183         if (!looseedge) {
184                 BLI_dynstr_append(ds, "#define VERTEX_FACING\n");
185         }
186
187         str = BLI_dynstr_get_cstring(ds);
188         BLI_dynstr_free(ds);
189         return str;
190 }
191 static char *EDIT_MESH_sh_lib(void)
192 {
193         char *str = NULL;
194         DynStr *ds = BLI_dynstr_new();
195
196         BLI_dynstr_append(ds, datatoc_common_globals_lib_glsl);
197         BLI_dynstr_append(ds, datatoc_edit_mesh_overlay_common_lib_glsl);
198
199         str = BLI_dynstr_get_cstring(ds);
200         BLI_dynstr_free(ds);
201         return str;
202 }
203
204 static GPUShader *EDIT_MESH_ensure_shader(ToolSettings *tsettings, RegionView3D *rv3d, bool fast_mode, bool looseedge)
205 {
206         const int index = EDIT_MESH_sh_index(tsettings, rv3d, fast_mode);
207         if (looseedge) {
208                 if (!e_data.overlay_loose_edge_sh_cache[index]) {
209                         char *defines = EDIT_MESH_sh_defines(tsettings, rv3d, true, true);
210                         char *lib = EDIT_MESH_sh_lib();
211                         e_data.overlay_loose_edge_sh_cache[index] = DRW_shader_create_with_lib(
212                                 datatoc_edit_mesh_overlay_vert_glsl,
213                                 datatoc_edit_mesh_overlay_geom_edge_glsl,
214                                 datatoc_edit_mesh_overlay_frag_glsl,
215                                 lib,
216                                 defines);
217                         MEM_freeN(lib);
218                         MEM_freeN(defines);
219                 }
220                 return e_data.overlay_loose_edge_sh_cache[index];
221         }
222         else {
223                 if (!e_data.overlay_tri_sh_cache[index]) {
224                         char *defines = EDIT_MESH_sh_defines(tsettings, rv3d, true, false);
225                         char *lib = EDIT_MESH_sh_lib();
226                         e_data.overlay_tri_sh_cache[index] = DRW_shader_create_with_lib(
227                                 datatoc_edit_mesh_overlay_vert_glsl,
228                                 datatoc_edit_mesh_overlay_geom_tri_glsl,
229                                 datatoc_edit_mesh_overlay_frag_glsl,
230                                 lib,
231                                 defines);
232                         MEM_freeN(lib);
233                         MEM_freeN(defines);
234                 }
235                 return e_data.overlay_tri_sh_cache[index];
236         }
237 }
238
239 static void EDIT_MESH_engine_init(void *vedata)
240 {
241         EDIT_MESH_FramebufferList *fbl = ((EDIT_MESH_Data *)vedata)->fbl;
242
243         const float *viewport_size = DRW_viewport_size_get();
244         const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
245
246         e_data.occlude_wire_depth_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_DEPTH_COMPONENT24,
247                                                                  &draw_engine_edit_mesh_type);
248         e_data.occlude_wire_color_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RGBA8,
249                                                                  &draw_engine_edit_mesh_type);
250
251         GPU_framebuffer_ensure_config(&fbl->occlude_wire_fb, {
252                 GPU_ATTACHMENT_TEXTURE(e_data.occlude_wire_depth_tx),
253                 GPU_ATTACHMENT_TEXTURE(e_data.occlude_wire_color_tx)
254         });
255
256         if (!e_data.vcolor_face_shader) {
257                 e_data.vcolor_face_shader = GPU_shader_get_builtin_shader(GPU_SHADER_SIMPLE_LIGHTING_SMOOTH_COLOR_ALPHA);
258         }
259
260         if (!e_data.overlay_vert_sh) {
261                 char *lib = EDIT_MESH_sh_lib();
262                 e_data.overlay_vert_sh = DRW_shader_create_with_lib(
263                         datatoc_edit_mesh_overlay_loosevert_vert_glsl, NULL,
264                         datatoc_edit_mesh_overlay_frag_glsl,
265                         lib,
266                         "#define VERTEX_SELECTION\n");
267                 MEM_freeN(lib);
268         }
269         if (!e_data.overlay_facedot_sh) {
270                 e_data.overlay_facedot_sh = DRW_shader_create_with_lib(
271                         datatoc_edit_mesh_overlay_facedot_vert_glsl, NULL,
272                         datatoc_edit_mesh_overlay_facedot_frag_glsl,
273                         datatoc_common_globals_lib_glsl,
274                         "#define VERTEX_FACING\n");
275         }
276         if (!e_data.overlay_mix_sh) {
277                 e_data.overlay_mix_sh = DRW_shader_create_fullscreen(datatoc_edit_mesh_overlay_mix_frag_glsl, NULL);
278         }
279         if (!e_data.overlay_facefill_sh) {
280                 e_data.overlay_facefill_sh = DRW_shader_create_with_lib(
281                         datatoc_edit_mesh_overlay_facefill_vert_glsl, NULL,
282                         datatoc_edit_mesh_overlay_facefill_frag_glsl,
283                         datatoc_common_globals_lib_glsl, NULL);
284         }
285         if (!e_data.normals_face_sh) {
286                 e_data.normals_face_sh = DRW_shader_create(
287                         datatoc_edit_normals_vert_glsl,
288                         datatoc_edit_normals_geom_glsl,
289                         datatoc_gpu_shader_uniform_color_frag_glsl,
290                         "#define FACE_NORMALS\n");
291         }
292         if (!e_data.normals_loop_sh) {
293                 e_data.normals_loop_sh = DRW_shader_create(
294                         datatoc_edit_normals_vert_glsl,
295                         datatoc_edit_normals_geom_glsl,
296                         datatoc_gpu_shader_uniform_color_frag_glsl,
297                         "#define LOOP_NORMALS\n");
298         }
299         if (!e_data.normals_sh) {
300                 e_data.normals_sh = DRW_shader_create(
301                         datatoc_edit_normals_vert_glsl,
302                         datatoc_edit_normals_geom_glsl,
303                         datatoc_gpu_shader_uniform_color_frag_glsl, NULL);
304         }
305         if (!e_data.depth_sh) {
306                 e_data.depth_sh = DRW_shader_create_3D_depth_only();
307         }
308         if (!e_data.ghost_clear_depth_sh) {
309                 e_data.ghost_clear_depth_sh = DRW_shader_create(datatoc_edit_mesh_overlay_ghost_clear_vert_glsl,
310                                                                 NULL, NULL, NULL);
311         }
312 }
313
314 static DRWPass *edit_mesh_create_overlay_pass(
315         float *faceAlpha, DRWState statemod,
316         DRWShadingGroup **r_face_shgrp, DRWShadingGroup **r_ledges_shgrp,
317         DRWShadingGroup **r_lverts_shgrp, DRWShadingGroup **r_facedot_shgrp)
318 {
319         static float edge_width_scale;
320
321         GPUShader *tri_sh, *ledge_sh;
322         const DRWContextState *draw_ctx = DRW_context_state_get();
323         RegionView3D *rv3d = draw_ctx->rv3d;
324         Scene *scene = draw_ctx->scene;
325         ToolSettings *tsettings = scene->toolsettings;
326
327         ledge_sh = EDIT_MESH_ensure_shader(tsettings, rv3d, false, true);
328         tri_sh = EDIT_MESH_ensure_shader(tsettings, rv3d, true, false);
329
330         DRWPass *pass = DRW_pass_create(
331                 "Edit Mesh Face Overlay Pass",
332                 DRW_STATE_WRITE_COLOR | DRW_STATE_POINT | statemod);
333
334
335         /* Applies on top of the theme edge width, so edge-mode can have thick edges. */
336         edge_width_scale = (tsettings->selectmode & (SCE_SELECT_EDGE)) ? 1.75f : 1.0f;
337
338         *r_face_shgrp = DRW_shgroup_create(tri_sh, pass);
339         DRW_shgroup_uniform_block(*r_face_shgrp, "globalsBlock", globals_ubo);
340         DRW_shgroup_uniform_vec2(*r_face_shgrp, "viewportSize", DRW_viewport_size_get(), 1);
341         DRW_shgroup_uniform_float(*r_face_shgrp, "faceAlphaMod", faceAlpha, 1);
342         DRW_shgroup_uniform_float(*r_face_shgrp, "edgeScale", &edge_width_scale, 1);
343
344         *r_ledges_shgrp = DRW_shgroup_create(ledge_sh, pass);
345         DRW_shgroup_uniform_block(*r_ledges_shgrp, "globalsBlock", globals_ubo);
346         DRW_shgroup_uniform_vec2(*r_ledges_shgrp, "viewportSize", DRW_viewport_size_get(), 1);
347         DRW_shgroup_uniform_float(*r_ledges_shgrp, "edgeScale", &edge_width_scale, 1);
348
349         if ((tsettings->selectmode & (SCE_SELECT_VERTEX)) != 0) {
350                 *r_lverts_shgrp = DRW_shgroup_create(e_data.overlay_vert_sh, pass);
351                 DRW_shgroup_uniform_block(*r_lverts_shgrp, "globalsBlock", globals_ubo);
352                 DRW_shgroup_uniform_vec2(*r_lverts_shgrp, "viewportSize", DRW_viewport_size_get(), 1);
353                 DRW_shgroup_uniform_float(*r_lverts_shgrp, "edgeScale", &edge_width_scale, 1);
354         }
355
356         if ((tsettings->selectmode & (SCE_SELECT_FACE)) != 0) {
357                 *r_facedot_shgrp = DRW_shgroup_create(e_data.overlay_facedot_sh, pass);
358                 DRW_shgroup_uniform_block(*r_facedot_shgrp, "globalsBlock", globals_ubo);
359                 DRW_shgroup_uniform_float(*r_facedot_shgrp, "edgeScale", &edge_width_scale, 1);
360         }
361
362         return pass;
363 }
364
365 static float backwire_opacity;
366 static float face_mod;
367 static float size_normal;
368
369 static void EDIT_MESH_cache_init(void *vedata)
370 {
371         EDIT_MESH_PassList *psl = ((EDIT_MESH_Data *)vedata)->psl;
372         EDIT_MESH_StorageList *stl = ((EDIT_MESH_Data *)vedata)->stl;
373         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
374
375         const DRWContextState *draw_ctx = DRW_context_state_get();
376         View3D *v3d = draw_ctx->v3d;
377
378         static float zero = 0.0f;
379
380         if (!stl->g_data) {
381                 /* Alloc transient pointers */
382                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
383         }
384         stl->g_data->ghost_ob = 0;
385         stl->g_data->edit_ob = 0;
386
387         const bool xray_enabled = ((draw_ctx->v3d->shading.flag & V3D_SHADING_XRAY) != 0) &&
388                                    (draw_ctx->v3d->shading.type < OB_MATERIAL);
389         stl->g_data->do_zbufclip = ((v3d->flag & V3D_ZBUF_SELECT) == 0) || xray_enabled;
390
391         {
392                 psl->vcolor_faces = DRW_pass_create(
393                         "Vert Color Pass",
394                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL);
395
396                 stl->g_data->fvcolor_shgrp = DRW_shgroup_create(e_data.vcolor_face_shader, psl->vcolor_faces);
397
398                 static float light[3] = {-0.3f, 0.5f, 1.0f};
399                 static float alpha = 1.0f;
400                 static float world_light = 1.0f;  /* XXX, see: paint_vertex_mode.c */
401                 DRW_shgroup_uniform_vec3(stl->g_data->fvcolor_shgrp, "light", light, 1);
402                 DRW_shgroup_uniform_float(stl->g_data->fvcolor_shgrp, "alpha", &alpha, 1);
403                 DRW_shgroup_uniform_float(stl->g_data->fvcolor_shgrp, "global", &world_light, 1);
404         }
405
406         {
407                 /* Complementary Depth Pass */
408                 psl->depth_hidden_wire = DRW_pass_create(
409                         "Depth Pass Hidden Wire",
410                         DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CULL_BACK);
411                 stl->g_data->depth_shgrp_hidden_wire = DRW_shgroup_create(e_data.depth_sh, psl->depth_hidden_wire);
412         }
413
414         {
415                 /* Depth clearing for ghosting. */
416                 psl->ghost_clear_depth = DRW_pass_create(
417                         "Ghost Depth Clear",
418                         DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS | DRW_STATE_STENCIL_NEQUAL);
419
420                 DRWShadingGroup *shgrp = DRW_shgroup_create(e_data.ghost_clear_depth_sh, psl->ghost_clear_depth);
421                 DRW_shgroup_stencil_mask(shgrp, 0x00);
422                 DRW_shgroup_call_add(shgrp, DRW_cache_fullscreen_quad_get(), NULL);
423         }
424
425         {
426                 /* Normals */
427                 psl->normals = DRW_pass_create(
428                         "Edit Mesh Normals Pass",
429                         DRW_STATE_WRITE_DEPTH | DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL);
430
431                 stl->g_data->fnormals_shgrp = DRW_shgroup_create(e_data.normals_face_sh, psl->normals);
432                 DRW_shgroup_uniform_float(stl->g_data->fnormals_shgrp, "normalSize", &size_normal, 1);
433                 DRW_shgroup_uniform_vec4(stl->g_data->fnormals_shgrp, "color", ts.colorNormal, 1);
434
435                 stl->g_data->vnormals_shgrp = DRW_shgroup_create(e_data.normals_sh, psl->normals);
436                 DRW_shgroup_uniform_float(stl->g_data->vnormals_shgrp, "normalSize", &size_normal, 1);
437                 DRW_shgroup_uniform_vec4(stl->g_data->vnormals_shgrp, "color", ts.colorVNormal, 1);
438
439                 stl->g_data->lnormals_shgrp = DRW_shgroup_create(e_data.normals_loop_sh, psl->normals);
440                 DRW_shgroup_uniform_float(stl->g_data->lnormals_shgrp, "normalSize", &size_normal, 1);
441                 DRW_shgroup_uniform_vec4(stl->g_data->lnormals_shgrp, "color", ts.colorLNormal, 1);
442         }
443
444         if (!stl->g_data->do_zbufclip) {
445                 psl->edit_face_overlay = edit_mesh_create_overlay_pass(
446                         &face_mod, DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND,
447                         &stl->g_data->face_overlay_shgrp, &stl->g_data->ledges_overlay_shgrp,
448                         &stl->g_data->lverts_overlay_shgrp, &stl->g_data->facedot_overlay_shgrp);
449         }
450         else {
451                 /* We render all wires with depth and opaque to a new fbo and blend the result based on depth values */
452                 psl->edit_face_occluded = edit_mesh_create_overlay_pass(
453                         &zero, DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WRITE_DEPTH,
454                         &stl->g_data->face_occluded_shgrp, &stl->g_data->ledges_occluded_shgrp,
455                         &stl->g_data->lverts_occluded_shgrp, &stl->g_data->facedot_occluded_shgrp);
456
457                 /* however we loose the front faces value (because we need the depth of occluded wires and
458                  * faces are alpha blended ) so we recover them in a new pass. */
459                 psl->facefill_occlude = DRW_pass_create(
460                         "Front Face Color",
461                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
462                 stl->g_data->facefill_occluded_shgrp = DRW_shgroup_create(e_data.overlay_facefill_sh, psl->facefill_occlude);
463                 DRW_shgroup_uniform_block(stl->g_data->facefill_occluded_shgrp, "globalsBlock", globals_ubo);
464
465                 /* we need a full screen pass to combine the result */
466                 struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
467
468                 psl->mix_occlude = DRW_pass_create(
469                         "Mix Occluded Wires",
470                         DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND);
471                 DRWShadingGroup *mix_shgrp = DRW_shgroup_create(e_data.overlay_mix_sh, psl->mix_occlude);
472                 DRW_shgroup_call_add(mix_shgrp, quad, NULL);
473                 DRW_shgroup_uniform_float(mix_shgrp, "alpha", &backwire_opacity, 1);
474                 DRW_shgroup_uniform_texture_ref(mix_shgrp, "wireColor", &e_data.occlude_wire_color_tx);
475                 DRW_shgroup_uniform_texture_ref(mix_shgrp, "wireDepth", &e_data.occlude_wire_depth_tx);
476                 DRW_shgroup_uniform_texture_ref(mix_shgrp, "sceneDepth", &dtxl->depth);
477         }
478 }
479
480 static void edit_mesh_add_ob_to_pass(
481         Scene *scene, Object *ob, DRWShadingGroup *face_shgrp, DRWShadingGroup *ledges_shgrp,
482         DRWShadingGroup *lverts_shgrp, DRWShadingGroup *facedot_shgrp, DRWShadingGroup *facefill_shgrp)
483 {
484         struct GPUBatch *geo_ovl_tris, *geo_ovl_ledges, *geo_ovl_lverts, *geo_ovl_fcenter;
485         ToolSettings *tsettings = scene->toolsettings;
486
487         DRW_cache_mesh_wire_overlay_get(ob, &geo_ovl_tris, &geo_ovl_ledges, &geo_ovl_lverts);
488         DRW_shgroup_call_add(face_shgrp, geo_ovl_tris, ob->obmat);
489         DRW_shgroup_call_add(ledges_shgrp, geo_ovl_ledges, ob->obmat);
490
491         if (facefill_shgrp) {
492                 DRW_shgroup_call_add(facefill_shgrp, geo_ovl_tris, ob->obmat);
493         }
494
495         if ((tsettings->selectmode & SCE_SELECT_VERTEX) != 0) {
496                 DRW_shgroup_call_add(lverts_shgrp, geo_ovl_lverts, ob->obmat);
497         }
498
499         if (facedot_shgrp && (tsettings->selectmode & SCE_SELECT_FACE) != 0 ) {
500                 geo_ovl_fcenter = DRW_cache_face_centers_get(ob);
501                 DRW_shgroup_call_add(facedot_shgrp, geo_ovl_fcenter, ob->obmat);
502         }
503 }
504
505 static void EDIT_MESH_cache_populate(void *vedata, Object *ob)
506 {
507         EDIT_MESH_StorageList *stl = ((EDIT_MESH_Data *)vedata)->stl;
508         const DRWContextState *draw_ctx = DRW_context_state_get();
509         View3D *v3d = draw_ctx->v3d;
510         Scene *scene = draw_ctx->scene;
511         struct GPUBatch *geom;
512
513         if (ob->type == OB_MESH) {
514                 if ((ob == draw_ctx->object_edit) || BKE_object_is_in_editmode(ob)) {
515                         const Mesh *me = ob->data;
516                         bool do_occlude_wire = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_OCCLUDE_WIRE) != 0;
517                         bool do_show_weight = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_WEIGHT) != 0;
518
519                         /* Updating uniform */
520                         backwire_opacity = v3d->overlay.backwire_opacity;
521
522                         bool fnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FACE_NORMALS) != 0;
523                         bool vnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_VERT_NORMALS) != 0;
524                         bool lnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_LOOP_NORMALS) != 0;
525                         /* Updating uniform */
526                         size_normal = v3d->overlay.normals_length;
527
528                         face_mod = (do_occlude_wire) ? 0.0f : 1.0f;
529
530                         if (do_show_weight) {
531                                 geom = DRW_cache_mesh_surface_weights_get(ob);
532                                 DRW_shgroup_call_add(stl->g_data->fvcolor_shgrp, geom, ob->obmat);
533                         }
534
535                         if (do_occlude_wire) {
536                                 geom = DRW_cache_mesh_surface_get(ob);
537                                 DRW_shgroup_call_add(stl->g_data->depth_shgrp_hidden_wire, geom, ob->obmat);
538                         }
539
540                         if (fnormals_do) {
541                                 geom = DRW_cache_face_centers_get(ob);
542                                 DRW_shgroup_call_add(stl->g_data->fnormals_shgrp, geom, ob->obmat);
543                         }
544
545                         if (vnormals_do || lnormals_do) {
546                                 struct GPUBatch *geo_ovl_tris, *geo_ovl_ledges, *geo_ovl_lverts;
547                                 DRW_cache_mesh_normals_overlay_get(ob, &geo_ovl_tris, &geo_ovl_ledges, &geo_ovl_lverts);
548
549                                 if (vnormals_do) {
550                                         DRW_shgroup_call_add(stl->g_data->vnormals_shgrp, geo_ovl_tris, ob->obmat);
551                                         DRW_shgroup_call_add(stl->g_data->vnormals_shgrp, geo_ovl_ledges, ob->obmat);
552                                         DRW_shgroup_call_add(stl->g_data->vnormals_shgrp, geo_ovl_lverts, ob->obmat);
553                                 }
554
555                                 if (lnormals_do) {
556                                         DRW_shgroup_call_add(stl->g_data->lnormals_shgrp, geo_ovl_tris, ob->obmat);
557                                 }
558                         }
559
560                         if (stl->g_data->do_zbufclip) {
561                                 edit_mesh_add_ob_to_pass(
562                                         scene, ob,
563                                         stl->g_data->face_occluded_shgrp,
564                                         stl->g_data->ledges_occluded_shgrp,
565                                         stl->g_data->lverts_occluded_shgrp,
566                                         stl->g_data->facedot_occluded_shgrp,
567                                         stl->g_data->facefill_occluded_shgrp);
568                         }
569                         else {
570                                 edit_mesh_add_ob_to_pass(
571                                         scene, ob,
572                                         stl->g_data->face_overlay_shgrp,
573                                         stl->g_data->ledges_overlay_shgrp,
574                                         stl->g_data->lverts_overlay_shgrp,
575                                         (me->drawflag & ME_DRAW_FACE_DOT) ? stl->g_data->facedot_overlay_shgrp : NULL,
576                                         NULL);
577                         }
578
579                         stl->g_data->ghost_ob += (ob->dtx & OB_DRAWXRAY) ? 1 : 0;
580                         stl->g_data->edit_ob += 1;
581
582                         /* 3D text overlay */
583                         if (me->drawflag & (ME_DRAWEXTRA_EDGELEN |
584                                             ME_DRAWEXTRA_FACEAREA |
585                                             ME_DRAWEXTRA_FACEANG |
586                                             ME_DRAWEXTRA_EDGEANG |
587                                             ME_DRAWEXTRA_INDICES))
588                         {
589                                 if (DRW_state_show_text()) {
590                                         DRW_edit_mesh_mode_text_measure_stats(
591                                                draw_ctx->ar, v3d, ob, &scene->unit);
592                                 }
593                         }
594                 }
595         }
596 }
597
598 static void EDIT_MESH_draw_scene(void *vedata)
599 {
600         EDIT_MESH_PassList *psl = ((EDIT_MESH_Data *)vedata)->psl;
601         EDIT_MESH_StorageList *stl = ((EDIT_MESH_Data *)vedata)->stl;
602         EDIT_MESH_FramebufferList *fbl = ((EDIT_MESH_Data *)vedata)->fbl;
603         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
604         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
605
606         DRW_draw_pass(psl->vcolor_faces);
607
608         DRW_draw_pass(psl->depth_hidden_wire);
609
610         if (stl->g_data->do_zbufclip) {
611                 float clearcol[4] = {0.0f, 0.0f, 0.0f, 0.0f};
612                 /* render facefill */
613                 DRW_draw_pass(psl->facefill_occlude);
614
615                 /* Render wires on a separate framebuffer */
616                 GPU_framebuffer_bind(fbl->occlude_wire_fb);
617                 GPU_framebuffer_clear_color_depth(fbl->occlude_wire_fb, clearcol, 1.0f);
618                 DRW_draw_pass(psl->normals);
619                 DRW_draw_pass(psl->edit_face_occluded);
620
621                 /* Combine with scene buffer */
622                 GPU_framebuffer_bind(dfbl->color_only_fb);
623                 DRW_draw_pass(psl->mix_occlude);
624         }
625         else {
626                 DRW_draw_pass(psl->normals);
627
628                 const DRWContextState *draw_ctx = DRW_context_state_get();
629                 View3D *v3d = draw_ctx->v3d;
630
631                 if (v3d->shading.type == OB_SOLID && (v3d->shading.flag & V3D_SHADING_XRAY) == 0) {
632                         if (stl->g_data->ghost_ob == 1 && stl->g_data->edit_ob == 1) {
633                                 /* In the case of single ghost object edit (common case for retopology):
634                                  * we duplicate the depht+stencil buffer and clear all depth to 1.0f where
635                                  * the stencil buffer is no 0x00. */
636                                 const float *viewport_size = DRW_viewport_size_get();
637                                 const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
638                                 struct GPUTexture *ghost_depth_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_DEPTH24_STENCIL8, &draw_engine_edit_mesh_type);
639                                 GPU_framebuffer_ensure_config(&fbl->ghost_wire_fb, {
640                                         GPU_ATTACHMENT_TEXTURE(ghost_depth_tx),
641                                         GPU_ATTACHMENT_TEXTURE(dtxl->color),
642                                 });
643
644                                 GPU_framebuffer_blit(dfbl->depth_only_fb, 0, fbl->ghost_wire_fb, 0, GPU_DEPTH_BIT | GPU_STENCIL_BIT);
645                                 GPU_framebuffer_bind(fbl->ghost_wire_fb);
646
647                                 DRW_draw_pass(psl->ghost_clear_depth);
648                         }
649                 }
650
651                 DRW_draw_pass(psl->edit_face_overlay);
652         }
653 }
654
655 static void EDIT_MESH_engine_free(void)
656 {
657         DRW_SHADER_FREE_SAFE(e_data.overlay_vert_sh);
658         DRW_SHADER_FREE_SAFE(e_data.overlay_facedot_sh);
659         DRW_SHADER_FREE_SAFE(e_data.overlay_mix_sh);
660         DRW_SHADER_FREE_SAFE(e_data.overlay_facefill_sh);
661         DRW_SHADER_FREE_SAFE(e_data.normals_loop_sh);
662         DRW_SHADER_FREE_SAFE(e_data.normals_face_sh);
663         DRW_SHADER_FREE_SAFE(e_data.normals_sh);
664         DRW_SHADER_FREE_SAFE(e_data.ghost_clear_depth_sh);
665
666         for (int i = 0; i < MAX_SHADERS; i++) {
667                 DRW_SHADER_FREE_SAFE(e_data.overlay_tri_sh_cache[i]);
668                 DRW_SHADER_FREE_SAFE(e_data.overlay_loose_edge_sh_cache[i]);
669         }
670 }
671
672 static const DrawEngineDataSize EDIT_MESH_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_MESH_Data);
673
674 DrawEngineType draw_engine_edit_mesh_type = {
675         NULL, NULL,
676         N_("EditMeshMode"),
677         &EDIT_MESH_data_size,
678         &EDIT_MESH_engine_init,
679         &EDIT_MESH_engine_free,
680         &EDIT_MESH_cache_init,
681         &EDIT_MESH_cache_populate,
682         NULL,
683         NULL,
684         &EDIT_MESH_draw_scene,
685         NULL,
686         NULL,
687 };