41f31d0b7163697dbc6ab219be3000c8530c5115
[blender.git] / source / blender / draw / modes / overlay_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 overlay_mode.c
23  *  \ingroup draw_engine
24  */
25
26 #include "DNA_mesh_types.h"
27 #include "DNA_view3d_types.h"
28
29 #include "BKE_editmesh.h"
30 #include "BKE_object.h"
31 #include "BKE_global.h"
32
33 #include "GPU_shader.h"
34 #include "GPU_extensions.h"
35 #include "DRW_render.h"
36
37 #include "draw_mode_engines.h"
38
39 /* Structures */
40 typedef struct OVERLAY_StorageList {
41         struct OVERLAY_PrivateData *g_data;
42 } OVERLAY_StorageList;
43
44 typedef struct OVERLAY_PassList {
45         struct DRWPass *face_orientation_pass;
46         struct DRWPass *face_wireframe_pass;
47 } OVERLAY_PassList;
48
49 typedef struct OVERLAY_Data {
50         void *engine_type;
51         DRWViewportEmptyList *fbl;
52         DRWViewportEmptyList *txl;
53         OVERLAY_PassList *psl;
54         OVERLAY_StorageList *stl;
55 } OVERLAY_Data;
56
57 typedef struct OVERLAY_PrivateData {
58         DRWShadingGroup *face_orientation_shgrp;
59         DRWShadingGroup *face_wires_shgrp;
60         DRWShadingGroup *flat_wires_shgrp;
61         DRWShadingGroup *sculpt_wires_shgrp;
62         View3DOverlay overlay;
63         float wire_step_param[2];
64         bool ghost_stencil_test;
65         bool show_overlays;
66 } OVERLAY_PrivateData; /* Transient data */
67
68 typedef struct OVERLAY_Shaders {
69         /* Face orientation shader */
70         struct GPUShader *face_orientation;
71         /* Wireframe shader */
72         struct GPUShader *select_wireframe;
73         struct GPUShader *face_wireframe;
74         struct GPUShader *face_wireframe_sculpt;
75 } OVERLAY_Shaders;
76
77 /* *********** STATIC *********** */
78 static struct {
79         /* 0: normal, 1: clipped. */
80         OVERLAY_Shaders sh_data[2];
81 } e_data = {NULL};
82
83 extern char datatoc_common_world_clip_lib_glsl[];
84
85 /* Shaders */
86 extern char datatoc_overlay_face_orientation_frag_glsl[];
87 extern char datatoc_overlay_face_orientation_vert_glsl[];
88
89 extern char datatoc_overlay_face_wireframe_vert_glsl[];
90 extern char datatoc_overlay_face_wireframe_geom_glsl[];
91 extern char datatoc_overlay_face_wireframe_frag_glsl[];
92 extern char datatoc_gpu_shader_depth_only_frag_glsl[];
93
94 extern struct GlobalsUboStorage ts; /* draw_common.c */
95
96 static int OVERLAY_sh_data_index_from_rv3d(const RegionView3D *rv3d)
97 {
98         if (rv3d->rflag & RV3D_CLIPPING) {
99                 return 1;
100         }
101         return 0;
102 }
103
104 /* Functions */
105 static void overlay_engine_init(void *vedata)
106 {
107         OVERLAY_Data *data = vedata;
108         OVERLAY_StorageList *stl = data->stl;
109
110         const DRWContextState *draw_ctx = DRW_context_state_get();
111         OVERLAY_Shaders *sh_data = &e_data.sh_data[OVERLAY_sh_data_index_from_rv3d(draw_ctx->rv3d)];
112         const bool is_clip = (draw_ctx->rv3d->rflag & RV3D_CLIPPING) != 0;
113
114         if (is_clip) {
115                 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
116         }
117
118         if (!stl->g_data) {
119                 /* Alloc transient pointers */
120                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
121         }
122         stl->g_data->ghost_stencil_test = false;
123
124         const char *world_clip_lib_or_empty = is_clip ? datatoc_common_world_clip_lib_glsl : "";
125         const char *world_clip_def_or_empty = is_clip ? "#define USE_WORLD_CLIP_PLANES\n" : "";
126
127         if (!sh_data->face_orientation) {
128                 /* Face orientation */
129                 sh_data->face_orientation = DRW_shader_create_from_arrays({
130                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_overlay_face_orientation_vert_glsl, NULL},
131                         .frag = (const char *[]){datatoc_overlay_face_orientation_frag_glsl, NULL},
132                         .defs = (const char *[]){world_clip_def_or_empty, NULL}});
133         }
134
135         if (!sh_data->face_wireframe) {
136                 sh_data->select_wireframe = DRW_shader_create_from_arrays({
137                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_overlay_face_wireframe_vert_glsl, NULL},
138                         .geom = (const char *[]){world_clip_lib_or_empty, datatoc_overlay_face_wireframe_geom_glsl, NULL},
139                         .frag = (const char *[]){datatoc_gpu_shader_depth_only_frag_glsl, NULL},
140                         .defs = (const char *[]){world_clip_def_or_empty, "#define SELECT_EDGES\n", NULL}});
141
142                 sh_data->face_wireframe = DRW_shader_create_from_arrays({
143                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_overlay_face_wireframe_vert_glsl, NULL},
144                         .frag = (const char *[]){datatoc_overlay_face_wireframe_frag_glsl, NULL},
145                         .defs = (const char *[]){world_clip_def_or_empty, NULL}});
146
147                 sh_data->face_wireframe_sculpt = DRW_shader_create_from_arrays({
148                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_overlay_face_wireframe_vert_glsl, NULL},
149                         .geom = (const char *[]){world_clip_lib_or_empty, datatoc_overlay_face_wireframe_geom_glsl, NULL},
150                         .frag = (const char *[]){datatoc_overlay_face_wireframe_frag_glsl, NULL},
151                         .defs = (const char *[]){world_clip_def_or_empty, "#define USE_SCULPT\n", NULL}});
152         }
153 }
154
155 static void overlay_cache_init(void *vedata)
156 {
157         OVERLAY_Data *data = vedata;
158         OVERLAY_PassList *psl = data->psl;
159         OVERLAY_StorageList *stl = data->stl;
160         OVERLAY_PrivateData *g_data = stl->g_data;
161
162         const DRWContextState *draw_ctx = DRW_context_state_get();
163         RegionView3D *rv3d = draw_ctx->rv3d;
164         OVERLAY_Shaders *sh_data = &e_data.sh_data[OVERLAY_sh_data_index_from_rv3d(rv3d)];
165
166         const DRWContextState *DCS = DRW_context_state_get();
167
168         View3D *v3d = DCS->v3d;
169         if (v3d) {
170                 g_data->overlay = v3d->overlay;
171                 g_data->show_overlays = (v3d->flag2 & V3D_RENDER_OVERRIDE) == 0;
172         }
173         else {
174                 memset(&g_data->overlay, 0, sizeof(g_data->overlay));
175                 g_data->show_overlays = false;
176         }
177
178         if (g_data->show_overlays == false) {
179                 g_data->overlay.flag = 0;
180         }
181
182         if (v3d->shading.type == OB_WIRE) {
183                 g_data->overlay.flag |= V3D_OVERLAY_WIREFRAMES;
184                 g_data->show_overlays = true;
185         }
186
187         {
188                 /* Face Orientation Pass */
189                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_BLEND;
190                 psl->face_orientation_pass = DRW_pass_create("Face Orientation", state);
191                 g_data->face_orientation_shgrp = DRW_shgroup_create(
192                         sh_data->face_orientation, psl->face_orientation_pass);
193                 if (rv3d->rflag & RV3D_CLIPPING) {
194                         DRW_shgroup_world_clip_planes_from_rv3d(g_data->face_orientation_shgrp, rv3d);
195                 }
196         }
197
198         {
199                 /* Wireframe */
200                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND | DRW_STATE_FIRST_VERTEX_CONVENTION;
201                 float wire_size = max_ff(0.0f, U.pixelsize - 1.0f) * 0.5f;
202
203                 const bool use_select = (DRW_state_is_select() || DRW_state_is_depth());
204                 GPUShader *sculpt_wire_sh = use_select ? sh_data->select_wireframe : sh_data->face_wireframe_sculpt;
205                 GPUShader *face_wires_sh = use_select ? sh_data->select_wireframe : sh_data->face_wireframe;
206                 GPUShader *flat_wires_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
207
208                 psl->face_wireframe_pass = DRW_pass_create("Face Wires", state);
209
210                 g_data->flat_wires_shgrp = DRW_shgroup_create(flat_wires_sh, psl->face_wireframe_pass);
211                 if (rv3d->rflag & RV3D_CLIPPING) {
212                         DRW_shgroup_world_clip_planes_from_rv3d(g_data->flat_wires_shgrp, rv3d);
213                 }
214
215                 g_data->sculpt_wires_shgrp = DRW_shgroup_create(sculpt_wire_sh, psl->face_wireframe_pass);
216                 if (rv3d->rflag & RV3D_CLIPPING) {
217                         DRW_shgroup_world_clip_planes_from_rv3d(g_data->sculpt_wires_shgrp, rv3d);
218                 }
219
220                 g_data->face_wires_shgrp = DRW_shgroup_create(face_wires_sh, psl->face_wireframe_pass);
221                 DRW_shgroup_uniform_vec2(g_data->face_wires_shgrp, "wireStepParam", g_data->wire_step_param, 1);
222                 if (rv3d->rflag & RV3D_CLIPPING) {
223                         DRW_shgroup_world_clip_planes_from_rv3d(g_data->face_wires_shgrp, rv3d);
224                 }
225
226                 if (!use_select) {
227                         DRW_shgroup_uniform_float_copy(g_data->sculpt_wires_shgrp, "wireSize", wire_size);
228                         DRW_shgroup_uniform_float_copy(g_data->face_wires_shgrp, "wireSize", wire_size);
229                 }
230
231                 /* Control aspect of the falloff. */
232                 const float sharpness = 4.0f;
233                 /* Scale and bias: Adjust with wiredata encoding. (see mesh_batch_cache_create_edges_wireframe_data) */
234                 const float decompress = (0xFF / (float)(0xFF - 0x20));
235                 g_data->wire_step_param[0] = -sharpness * decompress;
236                 g_data->wire_step_param[1] = decompress + sharpness * stl->g_data->overlay.wireframe_threshold;
237
238
239         }
240 }
241
242 static void overlay_cache_populate(void *vedata, Object *ob)
243 {
244         OVERLAY_Data *data = vedata;
245         OVERLAY_StorageList *stl = data->stl;
246         OVERLAY_PrivateData *pd = stl->g_data;
247         const DRWContextState *draw_ctx = DRW_context_state_get();
248         RegionView3D *rv3d = draw_ctx->rv3d;
249         View3D *v3d = draw_ctx->v3d;
250
251         if ((!pd->show_overlays) ||
252             (ob->dt < OB_WIRE) ||
253             (!DRW_object_is_renderable(ob) && (ob->dt != OB_WIRE)))
254         {
255                 return;
256         }
257
258         if (DRW_object_is_renderable(ob) && pd->overlay.flag & V3D_OVERLAY_FACE_ORIENTATION) {
259                 struct GPUBatch *geom = DRW_cache_object_surface_get(ob);
260                 if (geom) {
261                         DRW_shgroup_call_add(pd->face_orientation_shgrp, geom, ob->obmat);
262                 }
263         }
264
265         if ((pd->overlay.flag & V3D_OVERLAY_WIREFRAMES) ||
266             (v3d->shading.type == OB_WIRE) ||
267             (ob->dtx & OB_DRAWWIRE) ||
268             (ob->dt == OB_WIRE))
269         {
270                 const bool is_edit_mode = BKE_object_is_in_editmode(ob);
271                 bool has_edit_mesh_cage = false;
272                 if (ob->type == OB_MESH) {
273                         /* TODO: Should be its own function. */
274                         Mesh *me = (Mesh *)ob->data;
275                         BMEditMesh *embm = me->edit_btmesh;
276                         if (embm) {
277                                 has_edit_mesh_cage = embm->mesh_eval_cage && (embm->mesh_eval_cage != embm->mesh_eval_final);
278                         }
279                 }
280
281                 /* Don't do that in edit Mesh mode, unless there is a modifier preview. */
282                 if ((((ob != draw_ctx->object_edit) && !is_edit_mode) || has_edit_mesh_cage) ||
283                     ob->type != OB_MESH)
284                 {
285                         const bool is_active = (ob == draw_ctx->obact);
286                         const bool is_sculpt_mode = is_active && (draw_ctx->object_mode & OB_MODE_SCULPT) != 0;
287                         const bool all_wires = (pd->overlay.wireframe_threshold == 1.0f) ||
288                                                (ob->dtx & OB_DRAW_ALL_EDGES);
289                         const bool is_wire = (ob->dt < OB_SOLID);
290                         const int stencil_mask = (ob->dtx & OB_DRAWXRAY) ? 0x00 : 0xFF;
291                         DRWShadingGroup *shgrp = NULL;
292
293                         const float *rim_col = NULL;
294                         const float *wire_col = NULL;
295                         if (UNLIKELY(ob->base_flag & BASE_FROM_SET)) {
296                                 rim_col = ts.colorDupli;
297                                 wire_col = ts.colorDupli;
298                         }
299                         else if (UNLIKELY(ob->base_flag & BASE_FROM_DUPLI)) {
300                                 if (ob->base_flag & BASE_SELECTED) {
301                                         if (G.moving & G_TRANSFORM_OBJ) {
302                                                 rim_col = ts.colorTransform;
303                                         }
304                                         else {
305                                                 rim_col = ts.colorDupliSelect;
306                                         }
307                                 }
308                                 else {
309                                         rim_col = ts.colorDupli;
310                                 }
311                                 wire_col = ts.colorDupli;
312                         }
313                         else if ((ob->base_flag & BASE_SELECTED) &&
314                                  (!is_edit_mode && !is_sculpt_mode && !has_edit_mesh_cage))
315                         {
316                                 if (G.moving & G_TRANSFORM_OBJ) {
317                                         rim_col = ts.colorTransform;
318                                 }
319                                 else if (ob == draw_ctx->obact) {
320                                         rim_col = ts.colorActive;
321                                 }
322                                 else {
323                                         rim_col = ts.colorSelect;
324                                 }
325                                 wire_col = ts.colorWire;
326                         }
327                         else {
328                                 rim_col = ts.colorWire;
329                                 wire_col = ts.colorWire;
330                         }
331                         BLI_assert(rim_col && wire_col);
332
333                         /* This fixes only the biggest case which is a plane in ortho view. */
334                         int flat_axis = 0;
335                         bool is_flat_object_viewed_from_side = (
336                                 (rv3d->persp == RV3D_ORTHO) &&
337                                 DRW_object_is_flat(ob, &flat_axis) &&
338                                 DRW_object_axis_orthogonal_to_view(ob, flat_axis));
339
340                         if (is_flat_object_viewed_from_side && !is_sculpt_mode) {
341                                 /* Avoid losing flat objects when in ortho views (see T56549) */
342                                 struct GPUBatch *geom = DRW_cache_object_all_edges_get(ob);
343                                 if (geom) {
344                                         shgrp = pd->flat_wires_shgrp;
345                                         shgrp = DRW_shgroup_create_sub(shgrp);
346                                         DRW_shgroup_stencil_mask(shgrp, stencil_mask);
347                                         DRW_shgroup_call_object_add(shgrp, geom, ob);
348                                         DRW_shgroup_uniform_vec4(shgrp, "color", rim_col, 1);
349                                 }
350                         }
351                         else {
352                                 struct GPUBatch *geom = DRW_cache_object_face_wireframe_get(ob);
353                                 if (geom || is_sculpt_mode) {
354                                         shgrp = (is_sculpt_mode) ? pd->sculpt_wires_shgrp : pd->face_wires_shgrp;
355                                         shgrp = DRW_shgroup_create_sub(shgrp);
356
357                                         static float all_wires_params[2] = {0.0f, 10.0f}; /* Parameters for all wires */
358                                         DRW_shgroup_uniform_vec2(
359                                                 shgrp, "wireStepParam", (all_wires) ?
360                                                 all_wires_params : pd->wire_step_param, 1);
361
362                                         if (!(DRW_state_is_select() || DRW_state_is_depth())) {
363                                                 DRW_shgroup_stencil_mask(shgrp, stencil_mask);
364                                                 DRW_shgroup_uniform_vec3(shgrp, "wireColor", wire_col, 1);
365                                                 DRW_shgroup_uniform_vec3(shgrp, "rimColor", rim_col, 1);
366                                         }
367
368                                         if (is_sculpt_mode) {
369                                                 DRW_shgroup_call_sculpt_add(shgrp, ob, ob->obmat);
370                                         }
371                                         else {
372                                                 DRW_shgroup_call_add(shgrp, geom, ob->obmat);
373                                         }
374                                 }
375                         }
376                         if (is_wire && shgrp != NULL) {
377                                 /* If object is wireframe, don't try to use stencil test. */
378                                 DRW_shgroup_state_disable(shgrp, DRW_STATE_STENCIL_EQUAL);
379
380                                 if (ob->dtx & OB_DRAWXRAY) {
381                                         DRW_shgroup_state_disable(shgrp, DRW_STATE_DEPTH_LESS_EQUAL);
382                                 }
383                         }
384                         else if ((ob->dtx & OB_DRAWXRAY) && shgrp != NULL) {
385                                 pd->ghost_stencil_test = true;
386                         }
387                 }
388         }
389 }
390
391 static void overlay_cache_finish(void *vedata)
392 {
393         OVERLAY_Data *data = vedata;
394         OVERLAY_PassList *psl = data->psl;
395         OVERLAY_StorageList *stl = data->stl;
396
397         const DRWContextState *ctx = DRW_context_state_get();
398         View3D *v3d = ctx->v3d;
399
400         /* only in solid mode */
401         if (v3d->shading.type == OB_SOLID && (v3d->shading.flag & XRAY_FLAG(v3d)) == 0) {
402                 if (stl->g_data->ghost_stencil_test) {
403                         DRW_pass_state_add(psl->face_wireframe_pass, DRW_STATE_STENCIL_EQUAL);
404                 }
405         }
406 }
407
408 static void overlay_draw_scene(void *vedata)
409 {
410         OVERLAY_Data *data = vedata;
411         OVERLAY_PassList *psl = data->psl;
412         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
413
414         if (DRW_state_is_fbo()) {
415                 GPU_framebuffer_bind(dfbl->default_fb);
416         }
417         DRW_draw_pass(psl->face_orientation_pass);
418         DRW_draw_pass(psl->face_wireframe_pass);
419 }
420
421 static void overlay_engine_free(void)
422 {
423         for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
424                 OVERLAY_Shaders *sh_data = &e_data.sh_data[sh_data_index];
425                 GPUShader **sh_data_as_array = (GPUShader **)sh_data;
426                 for (int i = 0; i < (sizeof(OVERLAY_Shaders) / sizeof(GPUShader *)); i++) {
427                         DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
428                 }
429         }
430 }
431
432 static const DrawEngineDataSize overlay_data_size = DRW_VIEWPORT_DATA_SIZE(OVERLAY_Data);
433
434 DrawEngineType draw_engine_overlay_type = {
435         NULL, NULL,
436         N_("OverlayEngine"),
437         &overlay_data_size,
438         &overlay_engine_init,
439         &overlay_engine_free,
440         &overlay_cache_init,
441         &overlay_cache_populate,
442         &overlay_cache_finish,
443         NULL,
444         &overlay_draw_scene,
445         NULL,
446         NULL,
447         NULL,
448 };