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