Fix T56657: Crash when creating a collection instance containing probes
[blender.git] / source / blender / draw / modes / edit_lattice_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 blender/draw/modes/edit_lattice_mode.c
23  *  \ingroup draw
24  */
25
26 #include "DRW_engine.h"
27 #include "DRW_render.h"
28
29 #include "BKE_object.h"
30
31 /* If builtin shaders are needed */
32 #include "GPU_shader.h"
33
34 #include "draw_common.h"
35
36 #include "draw_mode_engines.h"
37
38 /* If needed, contains all global/Theme colors
39  * Add needed theme colors / values to DRW_globals_update() and update UBO
40  * Not needed for constant color. */
41 extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
42 extern struct GlobalsUboStorage ts; /* draw_common.c */
43
44 extern char datatoc_common_globals_lib_glsl[];
45 extern char datatoc_edit_lattice_overlay_loosevert_vert_glsl[];
46 extern char datatoc_edit_lattice_overlay_frag_glsl[];
47
48 extern char datatoc_gpu_shader_3D_vert_glsl[];
49 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
50 extern char datatoc_gpu_shader_point_uniform_color_frag_glsl[];
51
52 /* *********** LISTS *********** */
53 /* All lists are per viewport specific datas.
54  * They are all free when viewport changes engines
55  * or is free itself. Use EDIT_LATTICE_engine_init() to
56  * initialize most of them and EDIT_LATTICE_cache_init()
57  * for EDIT_LATTICE_PassList */
58
59 typedef struct EDIT_LATTICE_PassList {
60         /* Declare all passes here and init them in
61          * EDIT_LATTICE_cache_init().
62          * Only contains (DRWPass *) */
63         struct DRWPass *wire_pass;
64         struct DRWPass *vert_pass;
65 } EDIT_LATTICE_PassList;
66
67 typedef struct EDIT_LATTICE_FramebufferList {
68         /* Contains all framebuffer objects needed by this engine.
69          * Only contains (GPUFrameBuffer *) */
70         struct GPUFrameBuffer *fb;
71 } EDIT_LATTICE_FramebufferList;
72
73 typedef struct EDIT_LATTICE_TextureList {
74         /* Contains all framebuffer textures / utility textures
75          * needed by this engine. Only viewport specific textures
76          * (not per object). Only contains (GPUTexture *) */
77         struct GPUTexture *texture;
78 } EDIT_LATTICE_TextureList;
79
80 typedef struct EDIT_LATTICE_StorageList {
81         /* Contains any other memory block that the engine needs.
82          * Only directly MEM_(m/c)allocN'ed blocks because they are
83          * free with MEM_freeN() when viewport is freed.
84          * (not per object) */
85         struct CustomStruct *block;
86         struct EDIT_LATTICE_PrivateData *g_data;
87 } EDIT_LATTICE_StorageList;
88
89 typedef struct EDIT_LATTICE_Data {
90         /* Struct returned by DRW_viewport_engine_data_ensure.
91          * If you don't use one of these, just make it a (void *) */
92         // void *fbl;
93         void *engine_type; /* Required */
94         EDIT_LATTICE_FramebufferList *fbl;
95         EDIT_LATTICE_TextureList *txl;
96         EDIT_LATTICE_PassList *psl;
97         EDIT_LATTICE_StorageList *stl;
98 } EDIT_LATTICE_Data;
99
100 /* *********** STATIC *********** */
101
102 static struct {
103         /* Custom shaders :
104          * Add sources to source/blender/draw/modes/shaders
105          * init in EDIT_LATTICE_engine_init();
106          * free in EDIT_LATTICE_engine_free(); */
107         GPUShader *wire_sh;
108
109         GPUShader *overlay_vert_sh;
110
111 } e_data = {NULL}; /* Engine data */
112
113 typedef struct EDIT_LATTICE_PrivateData {
114         /* This keeps the references of the shading groups for
115          * easy access in EDIT_LATTICE_cache_populate() */
116         DRWShadingGroup *wire_shgrp;
117         DRWShadingGroup *vert_shgrp;
118 } EDIT_LATTICE_PrivateData; /* Transient data */
119
120 /* *********** FUNCTIONS *********** */
121
122 /* Init Textures, Framebuffers, Storage and Shaders.
123  * It is called for every frames.
124  * (Optional) */
125 static void EDIT_LATTICE_engine_init(void *vedata)
126 {
127         EDIT_LATTICE_TextureList *txl = ((EDIT_LATTICE_Data *)vedata)->txl;
128         EDIT_LATTICE_FramebufferList *fbl = ((EDIT_LATTICE_Data *)vedata)->fbl;
129         EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
130
131         UNUSED_VARS(txl, fbl, stl);
132
133         /* Init Framebuffers like this: order is attachment order (for color texs) */
134         /*
135          * DRWFboTexture tex[2] = {{&txl->depth, GPU_DEPTH_COMPONENT24, 0},
136          *                         {&txl->color, GPU_RGBA8, DRW_TEX_FILTER}};
137          */
138
139         /* DRW_framebuffer_init takes care of checking if
140          * the framebuffer is valid and has the right size*/
141         /*
142          * float *viewport_size = DRW_viewport_size_get();
143          * DRW_framebuffer_init(&fbl->occlude_wire_fb,
144          *                     (int)viewport_size[0], (int)viewport_size[1],
145          *                     tex, 2);
146          */
147
148         if (!e_data.wire_sh) {
149                 e_data.wire_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_SMOOTH_COLOR);
150         }
151
152         if (!e_data.overlay_vert_sh) {
153                 e_data.overlay_vert_sh = DRW_shader_create_with_lib(
154                         datatoc_edit_lattice_overlay_loosevert_vert_glsl, NULL,
155                         datatoc_edit_lattice_overlay_frag_glsl,
156                         datatoc_common_globals_lib_glsl, NULL);
157         }
158 }
159
160 /* Here init all passes and shading groups
161  * Assume that all Passes are NULL */
162 static void EDIT_LATTICE_cache_init(void *vedata)
163 {
164         EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
165         EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
166
167         if (!stl->g_data) {
168                 /* Alloc transient pointers */
169                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
170         }
171
172         {
173                 psl->wire_pass = DRW_pass_create(
174                         "Lattice Wire",
175                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WIRE);
176                 stl->g_data->wire_shgrp = DRW_shgroup_create(e_data.wire_sh, psl->wire_pass);
177
178                 psl->vert_pass = DRW_pass_create(
179                         "Lattice Verts",
180                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_POINT);
181                 stl->g_data->vert_shgrp = DRW_shgroup_create(e_data.overlay_vert_sh, psl->vert_pass);
182
183                 DRW_shgroup_uniform_block(stl->g_data->vert_shgrp, "globalsBlock", globals_ubo);
184         }
185 }
186
187 /* Add geometry to shadingGroups. Execute for each objects */
188 static void EDIT_LATTICE_cache_populate(void *vedata, Object *ob)
189 {
190         EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
191         EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
192         const DRWContextState *draw_ctx = DRW_context_state_get();
193
194         UNUSED_VARS(psl);
195
196         if (ob->type == OB_LATTICE) {
197                 if ((ob == draw_ctx->object_edit) || BKE_object_is_in_editmode(ob)) {
198                         /* Get geometry cache */
199                         struct GPUBatch *geom;
200
201                         geom = DRW_cache_lattice_wire_get(ob, true);
202                         DRW_shgroup_call_add(stl->g_data->wire_shgrp, geom, ob->obmat);
203
204                         geom = DRW_cache_lattice_vert_overlay_get(ob);
205                         DRW_shgroup_call_add(stl->g_data->vert_shgrp, geom, ob->obmat);
206                 }
207         }
208 }
209
210 /* Optional: Post-cache_populate callback */
211 static void EDIT_LATTICE_cache_finish(void *vedata)
212 {
213         EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
214         EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
215
216         /* Do something here! dependant on the objects gathered */
217         UNUSED_VARS(psl, stl);
218 }
219
220 /* Draw time ! Control rendering pipeline from here */
221 static void EDIT_LATTICE_draw_scene(void *vedata)
222 {
223         EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
224         EDIT_LATTICE_FramebufferList *fbl = ((EDIT_LATTICE_Data *)vedata)->fbl;
225
226         /* Default framebuffer and texture */
227         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
228         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
229
230         UNUSED_VARS(fbl);
231
232         MULTISAMPLE_SYNC_ENABLE(dfbl, dtxl);
233
234         /* Show / hide entire passes, swap framebuffers ... whatever you fancy */
235         /*
236          * DRW_framebuffer_texture_detach(dtxl->depth);
237          * DRW_framebuffer_bind(fbl->custom_fb);
238          * DRW_draw_pass(psl->pass);
239          * DRW_framebuffer_texture_attach(dfbl->default_fb, dtxl->depth, 0, 0);
240          * DRW_framebuffer_bind(dfbl->default_fb);
241          */
242
243         /* ... or just render passes on default framebuffer. */
244         DRW_draw_pass(psl->wire_pass);
245         DRW_draw_pass(psl->vert_pass);
246
247         MULTISAMPLE_SYNC_DISABLE(dfbl, dtxl)
248
249         /* If you changed framebuffer, double check you rebind
250          * the default one with its textures attached before finishing */
251 }
252
253 /* Cleanup when destroying the engine.
254  * This is not per viewport ! only when quitting blender.
255  * Mostly used for freeing shaders */
256 static void EDIT_LATTICE_engine_free(void)
257 {
258         // Currently built-in, dont free
259         DRW_SHADER_FREE_SAFE(e_data.overlay_vert_sh);
260 }
261
262 static const DrawEngineDataSize EDIT_LATTICE_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_LATTICE_Data);
263
264 DrawEngineType draw_engine_edit_lattice_type = {
265         NULL, NULL,
266         N_("EditLatticeMode"),
267         &EDIT_LATTICE_data_size,
268         &EDIT_LATTICE_engine_init,
269         &EDIT_LATTICE_engine_free,
270         &EDIT_LATTICE_cache_init,
271         &EDIT_LATTICE_cache_populate,
272         &EDIT_LATTICE_cache_finish,
273         NULL, /* draw_background but not needed by mode engines */
274         &EDIT_LATTICE_draw_scene,
275         NULL,
276         NULL,
277 };