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