Merge branch 'master' into blender2.8
[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
32 #include "GPU_shader.h"
33 #include "GPU_extensions.h"
34 #include "DRW_render.h"
35
36 #include "draw_mode_engines.h"
37
38 /* Structures */
39 typedef struct OVERLAY_StorageList {
40         struct OVERLAY_PrivateData *g_data;
41 } OVERLAY_StorageList;
42
43 typedef struct OVERLAY_PassList {
44         struct DRWPass *face_orientation_pass;
45         struct DRWPass *face_wireframe_pass;
46 } OVERLAY_PassList;
47
48 typedef struct OVERLAY_Data {
49         void *engine_type;
50         DRWViewportEmptyList *fbl;
51         DRWViewportEmptyList *txl;
52         OVERLAY_PassList *psl;
53         OVERLAY_StorageList *stl;
54 } OVERLAY_Data;
55
56 typedef struct OVERLAY_PrivateData {
57         DRWShadingGroup *face_orientation_shgrp;
58         DRWShadingGroup *face_wires;
59         DRWShadingGroup *flat_wires;
60         DRWShadingGroup *sculpt_wires;
61         View3DOverlay overlay;
62         float wire_step_param[2];
63         bool ghost_stencil_test;
64         bool show_overlays;
65 } OVERLAY_PrivateData; /* Transient data */
66
67 /* *********** STATIC *********** */
68 static struct {
69         /* Face orientation shader */
70         struct GPUShader *face_orientation_sh;
71         /* Wireframe shader */
72         struct GPUShader *select_wireframe_sh;
73         struct GPUShader *face_wireframe_sh;
74         struct GPUShader *face_wireframe_sculpt_sh;
75 } e_data = {NULL};
76
77 /* Shaders */
78 extern char datatoc_overlay_face_orientation_frag_glsl[];
79 extern char datatoc_overlay_face_orientation_vert_glsl[];
80
81 extern char datatoc_overlay_face_wireframe_vert_glsl[];
82 extern char datatoc_overlay_face_wireframe_geom_glsl[];
83 extern char datatoc_overlay_face_wireframe_frag_glsl[];
84 extern char datatoc_gpu_shader_depth_only_frag_glsl[];
85
86 extern struct GlobalsUboStorage ts; /* draw_common.c */
87
88 /* Functions */
89 static void overlay_engine_init(void *vedata)
90 {
91         OVERLAY_Data *data = vedata;
92         OVERLAY_StorageList *stl = data->stl;
93
94         if (!stl->g_data) {
95                 /* Alloc transient pointers */
96                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
97         }
98         stl->g_data->ghost_stencil_test = false;
99
100         if (!e_data.face_orientation_sh) {
101                 /* Face orientation */
102                 e_data.face_orientation_sh = DRW_shader_create(
103                         datatoc_overlay_face_orientation_vert_glsl, NULL,
104                         datatoc_overlay_face_orientation_frag_glsl, NULL);
105         }
106
107         if (!e_data.face_wireframe_sh) {
108                 e_data.select_wireframe_sh = DRW_shader_create(
109                         datatoc_overlay_face_wireframe_vert_glsl,
110                         datatoc_overlay_face_wireframe_geom_glsl,
111                         datatoc_gpu_shader_depth_only_frag_glsl,
112                         "#define SELECT_EDGES\n");
113
114                 e_data.face_wireframe_sh = DRW_shader_create(
115                         datatoc_overlay_face_wireframe_vert_glsl,
116                         NULL,
117                         datatoc_overlay_face_wireframe_frag_glsl,
118                         NULL);
119
120                 e_data.face_wireframe_sculpt_sh = DRW_shader_create(
121                         datatoc_overlay_face_wireframe_vert_glsl,
122                         datatoc_overlay_face_wireframe_geom_glsl,
123                         datatoc_overlay_face_wireframe_frag_glsl,
124                         "#define USE_SCULPT\n");
125         }
126 }
127
128 static void overlay_cache_init(void *vedata)
129 {
130         OVERLAY_Data *data = vedata;
131         OVERLAY_PassList *psl = data->psl;
132         OVERLAY_StorageList *stl = data->stl;
133         OVERLAY_PrivateData *g_data = stl->g_data;
134
135         const DRWContextState *DCS = DRW_context_state_get();
136
137         View3D *v3d = DCS->v3d;
138         if (v3d) {
139                 g_data->overlay = v3d->overlay;
140                 g_data->show_overlays = (v3d->flag2 & V3D_RENDER_OVERRIDE) == 0;
141         }
142         else {
143                 memset(&g_data->overlay, 0, sizeof(g_data->overlay));
144                 g_data->show_overlays = false;
145         }
146
147         if (g_data->show_overlays == false) {
148                 g_data->overlay.flag = 0;
149         }
150
151         if (v3d->shading.type == OB_WIRE) {
152                 g_data->overlay.flag |= V3D_OVERLAY_WIREFRAMES;
153                 g_data->show_overlays = true;
154         }
155
156         {
157                 /* Face Orientation Pass */
158                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_BLEND;
159                 psl->face_orientation_pass = DRW_pass_create("Face Orientation", state);
160                 g_data->face_orientation_shgrp = DRW_shgroup_create(
161                         e_data.face_orientation_sh, psl->face_orientation_pass);
162         }
163
164         {
165                 /* Wireframe */
166                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND | DRW_STATE_FIRST_VERTEX_CONVENTION;
167                 float wire_size = max_ff(0.0f, U.pixelsize - 1.0f) * 0.5f;
168
169                 const bool use_select = (DRW_state_is_select() || DRW_state_is_depth());
170                 GPUShader *sculpt_wire_sh = use_select ? e_data.select_wireframe_sh : e_data.face_wireframe_sculpt_sh;
171                 GPUShader *face_wires_sh = use_select ? e_data.select_wireframe_sh : e_data.face_wireframe_sh;
172                 GPUShader *flat_wires_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
173
174                 psl->face_wireframe_pass = DRW_pass_create("Face Wires", state);
175
176                 g_data->flat_wires = DRW_shgroup_create(flat_wires_sh, psl->face_wireframe_pass);
177                 DRW_shgroup_uniform_vec4(g_data->flat_wires, "color", ts.colorWire, 1);
178
179                 g_data->sculpt_wires = DRW_shgroup_create(sculpt_wire_sh, psl->face_wireframe_pass);
180
181                 g_data->face_wires = DRW_shgroup_create(face_wires_sh, psl->face_wireframe_pass);
182                 DRW_shgroup_uniform_vec2(g_data->face_wires, "wireStepParam", g_data->wire_step_param, 1);
183
184                 if (!use_select) {
185                         DRW_shgroup_uniform_float_copy(g_data->sculpt_wires, "wireSize", wire_size);
186                         DRW_shgroup_uniform_float_copy(g_data->face_wires, "wireSize", wire_size);
187                 }
188
189                 /* Control aspect of the falloff. */
190                 const float sharpness = 4.0f;
191                 /* Scale and bias: Adjust with wiredata encoding. (see mesh_batch_cache_create_edges_wireframe_data) */
192                 const float decompress = (0xFF / (float)(0xFF - 0x20));
193                 g_data->wire_step_param[0] = -sharpness * decompress;
194                 g_data->wire_step_param[1] = decompress + sharpness * stl->g_data->overlay.wireframe_threshold;
195         }
196 }
197
198 static void overlay_cache_populate(void *vedata, Object *ob)
199 {
200         OVERLAY_Data *data = vedata;
201         OVERLAY_StorageList *stl = data->stl;
202         OVERLAY_PrivateData *pd = stl->g_data;
203         const DRWContextState *draw_ctx = DRW_context_state_get();
204         RegionView3D *rv3d = draw_ctx->rv3d;
205         View3D *v3d = draw_ctx->v3d;
206
207         if ((!pd->show_overlays) ||
208             (ob->dt < OB_WIRE) ||
209             (!DRW_object_is_renderable(ob) && (ob->dt != OB_WIRE)))
210         {
211                 return;
212         }
213
214         if (DRW_object_is_renderable(ob) && pd->overlay.flag & V3D_OVERLAY_FACE_ORIENTATION) {
215                 struct GPUBatch *geom = DRW_cache_object_surface_get(ob);
216                 if (geom) {
217                         DRW_shgroup_call_add(pd->face_orientation_shgrp, geom, ob->obmat);
218                 }
219         }
220
221         if ((pd->overlay.flag & V3D_OVERLAY_WIREFRAMES) ||
222             (v3d->shading.type == OB_WIRE) ||
223             (ob->dtx & OB_DRAWWIRE) ||
224             (ob->dt == OB_WIRE))
225         {
226                 bool has_edit_mesh_cage = false;
227                 if (ob->type == OB_MESH) {
228                         /* TODO: Should be its own function. */
229                         Mesh *me = (Mesh *)ob->data;
230                         BMEditMesh *embm = me->edit_btmesh;
231                         if (embm) {
232                                 has_edit_mesh_cage = embm->mesh_eval_cage && (embm->mesh_eval_cage != embm->mesh_eval_final);
233                         }
234                 }
235
236                 /* Don't do that in edit Mesh mode, unless there is a modifier preview. */
237                 if ((((ob != draw_ctx->object_edit) && !BKE_object_is_in_editmode(ob)) || has_edit_mesh_cage) ||
238                     ob->type != OB_MESH)
239                 {
240                         const bool is_active = (ob == draw_ctx->obact);
241                         const bool is_sculpt_mode = is_active && (draw_ctx->object_mode & OB_MODE_SCULPT) != 0;
242                         const bool all_wires = (pd->overlay.wireframe_threshold == 1.0f) ||
243                                                (ob->dtx & OB_DRAW_ALL_EDGES);
244                         const bool is_wire = (ob->dt < OB_SOLID);
245                         const int stencil_mask = (ob->dtx & OB_DRAWXRAY) ? 0x00 : 0xFF;
246                         DRWShadingGroup *shgrp = NULL;
247
248                         /* This fixes only the biggest case which is a plane in ortho view. */
249                         int flat_axis = 0;
250                         bool is_flat_object_viewed_from_side = (rv3d->persp == RV3D_ORTHO) &&
251                                                                DRW_object_is_flat(ob, &flat_axis) &&
252                                                                DRW_object_axis_orthogonal_to_view(ob, flat_axis);
253
254                         if (is_flat_object_viewed_from_side && !is_sculpt_mode) {
255                                 /* Avoid losing flat objects when in ortho views (see T56549) */
256                                 struct GPUBatch *geom = DRW_cache_object_wire_outline_get(ob);
257                                 if (geom) {
258                                         shgrp = pd->flat_wires;
259                                         shgrp = DRW_shgroup_create_sub(shgrp);
260                                         DRW_shgroup_stencil_mask(shgrp, stencil_mask);
261                                         DRW_shgroup_call_object_add(shgrp, geom, ob);
262                                 }
263                         }
264                         else {
265                                 struct GPUBatch *geom = DRW_cache_object_face_wireframe_get(ob);
266                                 if (geom || is_sculpt_mode) {
267                                         float *rim_col = ts.colorWire;
268                                         if (!is_sculpt_mode && !has_edit_mesh_cage && ((ob->base_flag & BASE_SELECTED) != 0)) {
269                                                 rim_col = (ob == draw_ctx->obact) ? ts.colorActive : ts.colorSelect;
270                                         }
271                                         shgrp = (is_sculpt_mode) ? pd->sculpt_wires : pd->face_wires;
272                                         shgrp = DRW_shgroup_create_sub(shgrp);
273
274                                         if ((DRW_state_is_select() || DRW_state_is_depth())) {
275                                                 static float params[2] = {1.2f, 1.0f}; /* Parameters for all wires */
276                                                 DRW_shgroup_uniform_vec2(shgrp, "wireStepParam", (all_wires)
277                                                                                                  ? params
278                                                                                                  : pd->wire_step_param, 1);
279                                         }
280                                         else {
281                                                 DRW_shgroup_stencil_mask(shgrp, stencil_mask);
282                                                 DRW_shgroup_uniform_vec3(shgrp, "wireColor", ts.colorWire, 1);
283                                                 DRW_shgroup_uniform_vec3(shgrp, "rimColor", rim_col, 1);
284                                         }
285
286                                         if (is_sculpt_mode) {
287                                                 DRW_shgroup_call_sculpt_add(shgrp, ob, ob->obmat);
288                                         }
289                                         else {
290                                                 DRW_shgroup_call_add(shgrp, geom, ob->obmat);
291                                         }
292                                 }
293                         }
294                         if (is_wire && shgrp != NULL) {
295                                 /* If object is wireframe, don't try to use stencil test. */
296                                 DRW_shgroup_state_disable(shgrp, DRW_STATE_STENCIL_EQUAL);
297
298                                 if (ob->dtx & OB_DRAWXRAY) {
299                                         DRW_shgroup_state_disable(shgrp, DRW_STATE_DEPTH_LESS_EQUAL);
300                                 }
301                         }
302                         else if ((ob->dtx & OB_DRAWXRAY) && shgrp != NULL) {
303                                 pd->ghost_stencil_test = true;
304                         }
305                 }
306         }
307 }
308
309 static void overlay_cache_finish(void *vedata)
310 {
311         OVERLAY_Data *data = vedata;
312         OVERLAY_PassList *psl = data->psl;
313         OVERLAY_StorageList *stl = data->stl;
314
315         const DRWContextState *ctx = DRW_context_state_get();
316         View3D *v3d = ctx->v3d;
317
318         /* only in solid mode */
319         if (v3d->shading.type == OB_SOLID && (v3d->shading.flag & XRAY_FLAG(v3d)) == 0) {
320                 if (stl->g_data->ghost_stencil_test) {
321                         DRW_pass_state_add(psl->face_wireframe_pass, DRW_STATE_STENCIL_EQUAL);
322                 }
323         }
324 }
325
326 static void overlay_draw_scene(void *vedata)
327 {
328         OVERLAY_Data *data = vedata;
329         OVERLAY_PassList *psl = data->psl;
330         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
331
332         if (DRW_state_is_fbo()) {
333                 GPU_framebuffer_bind(dfbl->default_fb);
334         }
335         DRW_draw_pass(psl->face_orientation_pass);
336         DRW_draw_pass(psl->face_wireframe_pass);
337 }
338
339 static void overlay_engine_free(void)
340 {
341         DRW_SHADER_FREE_SAFE(e_data.face_orientation_sh);
342         DRW_SHADER_FREE_SAFE(e_data.select_wireframe_sh);
343         DRW_SHADER_FREE_SAFE(e_data.face_wireframe_sh);
344         DRW_SHADER_FREE_SAFE(e_data.face_wireframe_sculpt_sh);
345 }
346
347 static const DrawEngineDataSize overlay_data_size = DRW_VIEWPORT_DATA_SIZE(OVERLAY_Data);
348
349 DrawEngineType draw_engine_overlay_type = {
350         NULL, NULL,
351         N_("OverlayEngine"),
352         &overlay_data_size,
353         &overlay_engine_init,
354         &overlay_engine_free,
355         &overlay_cache_init,
356         &overlay_cache_populate,
357         &overlay_cache_finish,
358         NULL,
359         &overlay_draw_scene,
360         NULL,
361         NULL,
362         NULL,
363 };