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