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