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