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