Fix T56657: Crash when creating a collection instance containing probes
[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_view3d_types.h"
27
28 #include "BKE_object.h"
29
30 #include "GPU_shader.h"
31 #include "GPU_extensions.h"
32 #include "DRW_render.h"
33
34 #include "draw_mode_engines.h"
35
36 /* Structures */
37 typedef struct OVERLAY_StorageList {
38         struct OVERLAY_PrivateData *g_data;
39 } OVERLAY_StorageList;
40
41 typedef struct OVERLAY_PassList {
42         struct DRWPass *face_orientation_pass;
43         struct DRWPass *face_wireframe_pass;
44         struct DRWPass *face_wireframe_full_pass;
45 } OVERLAY_PassList;
46
47 typedef struct OVERLAY_Data {
48         void *engine_type;
49         DRWViewportEmptyList *fbl;
50         DRWViewportEmptyList *txl;
51         OVERLAY_PassList *psl;
52         OVERLAY_StorageList *stl;
53 } OVERLAY_Data;
54
55 typedef struct OVERLAY_PrivateData {
56         DRWShadingGroup *face_orientation_shgrp;
57         DRWShadingGroup *sculpt_wires_full;
58         DRWShadingGroup *sculpt_wires;
59         View3DOverlay overlay;
60         float wire_step_param[2];
61         bool ghost_stencil_test;
62         bool show_overlays;
63 } OVERLAY_PrivateData; /* Transient data */
64
65 /* *********** STATIC *********** */
66 static struct {
67         /* Face orientation shader */
68         struct GPUShader *face_orientation_sh;
69         /* Wireframe shader */
70         struct GPUShader *select_wireframe_sh;
71         struct GPUShader *face_wireframe_sh;
72         struct GPUShader *face_wireframe_pretty_sh;
73         struct GPUShader *face_wireframe_sculpt_sh;
74         struct GPUShader *face_wireframe_sculpt_pretty_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
85 extern struct GlobalsUboStorage ts; /* draw_common.c */
86
87 /* Functions */
88 static void overlay_engine_init(void *vedata)
89 {
90         OVERLAY_Data * data = (OVERLAY_Data *)vedata;
91         OVERLAY_StorageList *stl = data->stl;
92
93         if (!stl->g_data) {
94                 /* Alloc transient pointers */
95                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
96         }
97         stl->g_data->ghost_stencil_test = false;
98
99         if (!e_data.face_orientation_sh) {
100                 /* Face orientation */
101                 e_data.face_orientation_sh = DRW_shader_create(
102                         datatoc_overlay_face_orientation_vert_glsl, NULL,
103                         datatoc_overlay_face_orientation_frag_glsl, NULL);
104         }
105
106         if (!e_data.face_wireframe_sh) {
107                 bool use_geom = GPU_type_matches(GPU_DEVICE_INTEL, GPU_OS_ANY, GPU_DRIVER_ANY);
108
109                 e_data.select_wireframe_sh = DRW_shader_create(
110                         datatoc_overlay_face_wireframe_vert_glsl,
111                         datatoc_overlay_face_wireframe_geom_glsl,
112                         datatoc_overlay_face_wireframe_frag_glsl,
113                         "#define SELECT_EDGES\n"
114                         "#define LIGHT_EDGES\n"
115                         "#define USE_GEOM_SHADER\n");
116
117                 e_data.face_wireframe_sh = DRW_shader_create(
118                         datatoc_overlay_face_wireframe_vert_glsl,
119                         use_geom ? datatoc_overlay_face_wireframe_geom_glsl : NULL,
120                         datatoc_overlay_face_wireframe_frag_glsl,
121                         use_geom ? "#define USE_GEOM_SHADER\n"
122                                  : NULL);
123
124                 e_data.face_wireframe_pretty_sh = DRW_shader_create(
125                         datatoc_overlay_face_wireframe_vert_glsl,
126                         use_geom ? datatoc_overlay_face_wireframe_geom_glsl : NULL,
127                         datatoc_overlay_face_wireframe_frag_glsl,
128                         use_geom ? "#define USE_GEOM_SHADER\n"
129                                    "#define LIGHT_EDGES\n"
130                                  : "#define LIGHT_EDGES\n");
131
132                 e_data.face_wireframe_sculpt_sh = DRW_shader_create(
133                         datatoc_overlay_face_wireframe_vert_glsl,
134                         datatoc_overlay_face_wireframe_geom_glsl,
135                         datatoc_overlay_face_wireframe_frag_glsl,
136                         "#define USE_SCULPT\n"
137                         "#define USE_GEOM_SHADER\n");
138
139                 e_data.face_wireframe_sculpt_pretty_sh = DRW_shader_create(
140                         datatoc_overlay_face_wireframe_vert_glsl,
141                         datatoc_overlay_face_wireframe_geom_glsl,
142                         datatoc_overlay_face_wireframe_frag_glsl,
143                         "#define USE_SCULPT\n"
144                         "#define USE_GEOM_SHADER\n"
145                         "#define LIGHT_EDGES\n");
146         }
147 }
148
149 static void overlay_cache_init(void *vedata)
150 {
151         OVERLAY_Data * data = (OVERLAY_Data *)vedata;
152         OVERLAY_PassList *psl = data->psl;
153         OVERLAY_StorageList *stl = data->stl;
154
155         const DRWContextState *DCS = DRW_context_state_get();
156
157         View3D *v3d = DCS->v3d;
158         if (v3d) {
159                 stl->g_data->overlay = v3d->overlay;
160                 stl->g_data->show_overlays = (v3d->flag2 & V3D_RENDER_OVERRIDE) == 0;
161         }
162         else {
163                 memset(&stl->g_data->overlay, 0, sizeof(stl->g_data->overlay));
164                 stl->g_data->show_overlays = false;
165         }
166
167         {
168                 /* Face Orientation Pass */
169                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_BLEND;
170                 psl->face_orientation_pass = DRW_pass_create("Face Orientation", state);
171                 stl->g_data->face_orientation_shgrp = DRW_shgroup_create(
172                         e_data.face_orientation_sh, psl->face_orientation_pass);
173         }
174
175         {
176                 /* Wireframe */
177                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND;
178
179                 psl->face_wireframe_full_pass = DRW_pass_create("All Face Wires", state);
180
181                 stl->g_data->sculpt_wires_full = DRW_shgroup_create(e_data.face_wireframe_sculpt_sh, psl->face_wireframe_full_pass);
182                 DRW_shgroup_uniform_vec2(stl->g_data->sculpt_wires_full, "viewportSize", DRW_viewport_size_get(), 1);
183
184                 DRWShadingGroup *shgrp = DRW_shgroup_create(e_data.face_wireframe_sh, psl->face_wireframe_full_pass);
185                 DRW_shgroup_uniform_vec2(shgrp, "viewportSize", DRW_viewport_size_get(), 1);
186
187                 psl->face_wireframe_pass = DRW_pass_create("Face Wires", state);
188
189                 stl->g_data->sculpt_wires = DRW_shgroup_create(e_data.face_wireframe_sculpt_pretty_sh, psl->face_wireframe_pass);
190                 DRW_shgroup_uniform_vec2(stl->g_data->sculpt_wires, "viewportSize", DRW_viewport_size_get(), 1);
191                 DRW_shgroup_uniform_vec2(stl->g_data->sculpt_wires, "wireStepParam", stl->g_data->wire_step_param, 1);
192
193                 shgrp = DRW_shgroup_create(e_data.face_wireframe_pretty_sh, psl->face_wireframe_pass);
194                 DRW_shgroup_uniform_vec2(shgrp, "viewportSize", DRW_viewport_size_get(), 1);
195                 DRW_shgroup_uniform_vec2(shgrp, "wireStepParam", stl->g_data->wire_step_param, 1);
196
197                 /**
198                  * The wireframe threshold ranges from 0.0 to 1.0
199                  * When 1.0 we show all the edges, when 0.5 we show as many as 2.7.
200                  *
201                  * If we wanted 0.0 to match 2.7, factor would need to be 0.003f.
202                  * The range controls the falloff effect. If range was 0.0f we would get a hard cut (as in 2.7).
203                  * That said we are using a different algorithm so the results will always differ.
204                  */
205                 const float factor = 0.0045f;
206                 const float range = 0.00125f;
207                 stl->g_data->wire_step_param[1] = (1.0f - factor) + stl->g_data->overlay.wireframe_threshold * factor;
208                 stl->g_data->wire_step_param[0] = stl->g_data->wire_step_param[1] + range;
209         }
210 }
211
212 static void overlay_cache_populate(void *vedata, Object *ob)
213 {
214         OVERLAY_Data * data = (OVERLAY_Data *)vedata;
215         OVERLAY_StorageList *stl = data->stl;
216         OVERLAY_PrivateData *pd = stl->g_data;
217         OVERLAY_PassList *psl = data->psl;
218         const DRWContextState *draw_ctx = DRW_context_state_get();
219         View3D *v3d = draw_ctx->v3d;
220
221         if (!stl->g_data->show_overlays)
222                 return;
223
224         if (!DRW_object_is_renderable(ob) && (ob->dt != OB_WIRE))
225                 return;
226
227         if (DRW_object_is_renderable(ob) && stl->g_data->overlay.flag & V3D_OVERLAY_FACE_ORIENTATION) {
228                 struct GPUBatch *geom = DRW_cache_object_surface_get(ob);
229                 if (geom) {
230                         DRW_shgroup_call_add(pd->face_orientation_shgrp, geom, ob->obmat);
231                 }
232         }
233
234         if ((stl->g_data->overlay.flag & V3D_OVERLAY_WIREFRAMES) ||
235             (ob->dtx & OB_DRAWWIRE) ||
236             (ob->dt == OB_WIRE))
237         {
238                 /* Don't do that in edit mode. */
239                 if ((ob != draw_ctx->object_edit) && !BKE_object_is_in_editmode(ob)) {
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 = (stl->g_data->overlay.wireframe_threshold == 1.0f) ||
243                                                (ob->dtx & OB_DRAW_ALL_EDGES);
244
245                         if (is_sculpt_mode) {
246                                 DRWShadingGroup *shgrp = (all_wires || DRW_object_is_flat_normal(ob))
247                                                          ? stl->g_data->sculpt_wires_full
248                                                          : stl->g_data->sculpt_wires;
249                                 DRW_shgroup_call_sculpt_add(shgrp, ob, ob->obmat);
250                         }
251                         else {
252                                 int tri_count;
253                                 GPUTexture *verts = NULL, *faceids;
254                                 DRW_cache_object_face_wireframe_get(ob, &verts, &faceids, &tri_count);
255                                 if (verts) {
256                                         float *rim_col = ts.colorWire;
257                                         if ((ob->base_flag & BASE_SELECTED) != 0) {
258                                                 rim_col = (ob == draw_ctx->obact) ? ts.colorActive : ts.colorSelect;
259                                         }
260                                         DRWPass *pass = (all_wires) ? psl->face_wireframe_full_pass : psl->face_wireframe_pass;
261                                         GPUShader *sh = (all_wires) ? e_data.face_wireframe_sh : e_data.face_wireframe_pretty_sh;
262
263                                         if ((DRW_state_is_select() || DRW_state_is_depth()) &&
264                                             (v3d->shading.flag & V3D_SHADING_XRAY) != 0)
265                                         {
266                                                 static float params[2] = {1.2f, 1.0f}; /* Parameters for all wires */
267
268                                                 sh = e_data.select_wireframe_sh;
269                                                 DRWShadingGroup *shgrp = DRW_shgroup_create(sh, pass);
270                                                 DRW_shgroup_uniform_vec2(shgrp, "wireStepParam", (all_wires)
271                                                                                                  ? params
272                                                                                                  : stl->g_data->wire_step_param, 1);
273                                                 DRW_shgroup_uniform_texture(shgrp, "vertData", verts);
274                                                 DRW_shgroup_uniform_texture(shgrp, "faceIds", faceids);
275                                                 DRW_shgroup_call_object_procedural_triangles_culled_add(shgrp, tri_count, ob);
276                                         }
277                                         else {
278                                                 DRWShadingGroup *shgrp = DRW_shgroup_create(sh, pass);
279                                                 DRW_shgroup_stencil_mask(shgrp, (ob->dtx & OB_DRAWXRAY) ? 0x00 : 0xFF);
280                                                 DRW_shgroup_uniform_texture(shgrp, "vertData", verts);
281                                                 DRW_shgroup_uniform_texture(shgrp, "faceIds", faceids);
282                                                 DRW_shgroup_uniform_vec3(shgrp, "wireColor", ts.colorWire, 1);
283                                                 DRW_shgroup_uniform_vec3(shgrp, "rimColor", rim_col, 1);
284                                                 DRW_shgroup_call_object_procedural_triangles_culled_add(shgrp, tri_count, ob);
285                                         }
286
287                                 }
288                         }
289                 }
290         }
291
292         if (ob->dtx & OB_DRAWXRAY) {
293                 stl->g_data->ghost_stencil_test = true;
294         }
295 }
296
297 static void overlay_cache_finish(void *vedata)
298 {
299         OVERLAY_Data * data = (OVERLAY_Data *)vedata;
300         OVERLAY_PassList *psl = data->psl;
301         OVERLAY_StorageList *stl = data->stl;
302
303         const DRWContextState *ctx = DRW_context_state_get();
304         View3D *v3d = ctx->v3d;
305
306         /* only in solid mode */
307         if (v3d->shading.type == OB_SOLID && (v3d->shading.flag & V3D_SHADING_XRAY) == 0) {
308                 if (stl->g_data->ghost_stencil_test) {
309                         DRW_pass_state_add(psl->face_wireframe_pass, DRW_STATE_STENCIL_EQUAL);
310                         DRW_pass_state_add(psl->face_wireframe_full_pass, DRW_STATE_STENCIL_EQUAL);
311                 }
312         }
313 }
314
315 static void overlay_draw_scene(void *vedata)
316 {
317         OVERLAY_Data * data = (OVERLAY_Data *)vedata;
318         OVERLAY_PassList *psl = data->psl;
319         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
320
321         if (DRW_state_is_fbo()) {
322                 GPU_framebuffer_bind(dfbl->default_fb);
323         }
324         DRW_draw_pass(psl->face_orientation_pass);
325         DRW_draw_pass(psl->face_wireframe_pass);
326         DRW_draw_pass(psl->face_wireframe_full_pass);
327 }
328
329 static void overlay_engine_free(void)
330 {
331         DRW_SHADER_FREE_SAFE(e_data.face_orientation_sh);
332         DRW_SHADER_FREE_SAFE(e_data.select_wireframe_sh);
333         DRW_SHADER_FREE_SAFE(e_data.face_wireframe_sh);
334         DRW_SHADER_FREE_SAFE(e_data.face_wireframe_pretty_sh);
335         DRW_SHADER_FREE_SAFE(e_data.face_wireframe_sculpt_sh);
336         DRW_SHADER_FREE_SAFE(e_data.face_wireframe_sculpt_pretty_sh);
337 }
338
339 static const DrawEngineDataSize overlay_data_size = DRW_VIEWPORT_DATA_SIZE(OVERLAY_Data);
340
341 DrawEngineType draw_engine_overlay_type = {
342         NULL, NULL,
343         N_("OverlayEngine"),
344         &overlay_data_size,
345         &overlay_engine_init,
346         &overlay_engine_free,
347         &overlay_cache_init,
348         &overlay_cache_populate,
349         &overlay_cache_finish,
350         NULL,
351         &overlay_draw_scene,
352         NULL,
353         NULL,
354         NULL,
355 };