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