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