908bc1db7a98b92b2a1a146742398a0160174579
[blender.git] / source / blender / draw / modes / edit_lattice_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 blender/draw/modes/edit_lattice_mode.c
26  *  \ingroup draw
27  */
28
29 #include "DRW_engine.h"
30 #include "DRW_render.h"
31
32 #include "BKE_object.h"
33
34 /* If builtin shaders are needed */
35 #include "GPU_shader.h"
36
37 #include "draw_common.h"
38
39 #include "draw_mode_engines.h"
40
41 extern char datatoc_common_world_clip_lib_glsl[];
42 extern char datatoc_common_globals_lib_glsl[];
43
44 extern char datatoc_edit_lattice_overlay_loosevert_vert_glsl[];
45 extern char datatoc_edit_lattice_overlay_frag_glsl[];
46
47 /* *********** LISTS *********** */
48 /* All lists are per viewport specific datas.
49  * They are all free when viewport changes engines
50  * or is free itself. Use EDIT_LATTICE_engine_init() to
51  * initialize most of them and EDIT_LATTICE_cache_init()
52  * for EDIT_LATTICE_PassList */
53
54 typedef struct EDIT_LATTICE_PassList {
55         /* Declare all passes here and init them in
56          * EDIT_LATTICE_cache_init().
57          * Only contains (DRWPass *) */
58         struct DRWPass *wire_pass;
59         struct DRWPass *vert_pass;
60 } EDIT_LATTICE_PassList;
61
62 typedef struct EDIT_LATTICE_FramebufferList {
63         /* Contains all framebuffer objects needed by this engine.
64          * Only contains (GPUFrameBuffer *) */
65         struct GPUFrameBuffer *fb;
66 } EDIT_LATTICE_FramebufferList;
67
68 typedef struct EDIT_LATTICE_TextureList {
69         /* Contains all framebuffer textures / utility textures
70          * needed by this engine. Only viewport specific textures
71          * (not per object). Only contains (GPUTexture *) */
72         struct GPUTexture *texture;
73 } EDIT_LATTICE_TextureList;
74
75 typedef struct EDIT_LATTICE_StorageList {
76         /* Contains any other memory block that the engine needs.
77          * Only directly MEM_(m/c)allocN'ed blocks because they are
78          * free with MEM_freeN() when viewport is freed.
79          * (not per object) */
80         struct CustomStruct *block;
81         struct EDIT_LATTICE_PrivateData *g_data;
82 } EDIT_LATTICE_StorageList;
83
84 typedef struct EDIT_LATTICE_Data {
85         /* Struct returned by DRW_viewport_engine_data_ensure.
86          * If you don't use one of these, just make it a (void *) */
87         // void *fbl;
88         void *engine_type; /* Required */
89         EDIT_LATTICE_FramebufferList *fbl;
90         EDIT_LATTICE_TextureList *txl;
91         EDIT_LATTICE_PassList *psl;
92         EDIT_LATTICE_StorageList *stl;
93 } EDIT_LATTICE_Data;
94
95 typedef struct EDIT_LATTICE_Shaders {
96         GPUShader *wire;
97         GPUShader *overlay_vert;
98 } EDIT_LATTICE_Shaders;
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
108         /* 0: normal, 1: clipped. */
109         EDIT_LATTICE_Shaders sh_data[2];
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 static int EDIT_LATTICE_sh_data_index_from_rv3d(const RegionView3D *rv3d)
123 {
124         if (rv3d->rflag & RV3D_CLIPPING) {
125                 return 1;
126         }
127         return 0;
128 }
129
130 /* Init Textures, Framebuffers, Storage and Shaders.
131  * It is called for every frames.
132  * (Optional) */
133 static void EDIT_LATTICE_engine_init(void *vedata)
134 {
135         EDIT_LATTICE_TextureList *txl = ((EDIT_LATTICE_Data *)vedata)->txl;
136         EDIT_LATTICE_FramebufferList *fbl = ((EDIT_LATTICE_Data *)vedata)->fbl;
137         EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
138
139         UNUSED_VARS(txl, fbl, stl);
140
141         /* Init Framebuffers like this: order is attachment order (for color texs) */
142         /*
143          * DRWFboTexture tex[2] = {{&txl->depth, GPU_DEPTH_COMPONENT24, 0},
144          *                         {&txl->color, GPU_RGBA8, DRW_TEX_FILTER}};
145          */
146
147         /* DRW_framebuffer_init takes care of checking if
148          * the framebuffer is valid and has the right size*/
149         /*
150          * float *viewport_size = DRW_viewport_size_get();
151          * DRW_framebuffer_init(&fbl->occlude_wire_fb,
152          *                     (int)viewport_size[0], (int)viewport_size[1],
153          *                     tex, 2);
154          */
155
156         const DRWContextState *draw_ctx = DRW_context_state_get();
157         EDIT_LATTICE_Shaders *sh_data = &e_data.sh_data[EDIT_LATTICE_sh_data_index_from_rv3d(draw_ctx->rv3d)];
158         const bool is_clip = (draw_ctx->rv3d->rflag & RV3D_CLIPPING) != 0;
159         if (is_clip) {
160                 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
161         }
162         const char *world_clip_lib_or_empty = is_clip ? datatoc_common_world_clip_lib_glsl : "";
163         const char *world_clip_def_or_empty = is_clip ? "#define USE_WORLD_CLIP_PLANES\n" : "";
164
165         if (!sh_data->wire) {
166                 sh_data->wire = GPU_shader_get_builtin_shader(GPU_SHADER_3D_SMOOTH_COLOR);
167         }
168
169         if (!sh_data->overlay_vert) {
170                 sh_data->overlay_vert = DRW_shader_create_from_arrays({
171                         .vert = (const char *[]){
172                             world_clip_lib_or_empty,
173                             datatoc_common_globals_lib_glsl,
174                             datatoc_edit_lattice_overlay_loosevert_vert_glsl,
175                             NULL},
176                         .frag = (const char *[]){
177                             datatoc_common_globals_lib_glsl,
178                             datatoc_edit_lattice_overlay_frag_glsl,
179                             NULL},
180                         .defs = (const char *[]){world_clip_def_or_empty, NULL}});
181
182         }
183 }
184
185 /* Here init all passes and shading groups
186  * Assume that all Passes are NULL */
187 static void EDIT_LATTICE_cache_init(void *vedata)
188 {
189         EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
190         EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
191
192         const DRWContextState *draw_ctx = DRW_context_state_get();
193         RegionView3D *rv3d = draw_ctx->rv3d;
194         EDIT_LATTICE_Shaders *sh_data = &e_data.sh_data[EDIT_LATTICE_sh_data_index_from_rv3d(rv3d)];
195
196         if (!stl->g_data) {
197                 /* Alloc transient pointers */
198                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
199         }
200
201         {
202                 psl->wire_pass = DRW_pass_create(
203                         "Lattice Wire",
204                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WIRE);
205                 stl->g_data->wire_shgrp = DRW_shgroup_create(sh_data->wire, psl->wire_pass);
206
207                 psl->vert_pass = DRW_pass_create(
208                         "Lattice Verts",
209                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_POINT);
210                 stl->g_data->vert_shgrp = DRW_shgroup_create(sh_data->overlay_vert, psl->vert_pass);
211                 DRW_shgroup_uniform_block(stl->g_data->vert_shgrp, "globalsBlock", G_draw.block_ubo);
212                 if (rv3d->rflag & RV3D_CLIPPING) {
213                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->vert_shgrp, rv3d);
214                 }
215
216
217         }
218 }
219
220 /* Add geometry to shadingGroups. Execute for each objects */
221 static void EDIT_LATTICE_cache_populate(void *vedata, Object *ob)
222 {
223         EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
224         EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
225         const DRWContextState *draw_ctx = DRW_context_state_get();
226
227         UNUSED_VARS(psl);
228
229         if (ob->type == OB_LATTICE) {
230                 if ((ob == draw_ctx->object_edit) || BKE_object_is_in_editmode(ob)) {
231                         /* Get geometry cache */
232                         struct GPUBatch *geom;
233
234                         geom = DRW_cache_lattice_wire_get(ob, true);
235                         DRW_shgroup_call_add(stl->g_data->wire_shgrp, geom, ob->obmat);
236
237                         geom = DRW_cache_lattice_vert_overlay_get(ob);
238                         DRW_shgroup_call_add(stl->g_data->vert_shgrp, geom, ob->obmat);
239                 }
240         }
241 }
242
243 /* Optional: Post-cache_populate callback */
244 static void EDIT_LATTICE_cache_finish(void *vedata)
245 {
246         EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
247         EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
248
249         /* Do something here! dependent on the objects gathered */
250         UNUSED_VARS(psl, stl);
251 }
252
253 /* Draw time ! Control rendering pipeline from here */
254 static void EDIT_LATTICE_draw_scene(void *vedata)
255 {
256         EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
257         EDIT_LATTICE_FramebufferList *fbl = ((EDIT_LATTICE_Data *)vedata)->fbl;
258
259         /* Default framebuffer and texture */
260         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
261         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
262
263         UNUSED_VARS(fbl);
264
265         MULTISAMPLE_SYNC_ENABLE(dfbl, dtxl);
266
267         /* Show / hide entire passes, swap framebuffers ... whatever you fancy */
268         /*
269          * DRW_framebuffer_texture_detach(dtxl->depth);
270          * DRW_framebuffer_bind(fbl->custom_fb);
271          * DRW_draw_pass(psl->pass);
272          * DRW_framebuffer_texture_attach(dfbl->default_fb, dtxl->depth, 0, 0);
273          * DRW_framebuffer_bind(dfbl->default_fb);
274          */
275
276         /* ... or just render passes on default framebuffer. */
277         DRW_draw_pass(psl->wire_pass);
278         DRW_draw_pass(psl->vert_pass);
279
280         MULTISAMPLE_SYNC_DISABLE(dfbl, dtxl)
281
282         /* If you changed framebuffer, double check you rebind
283          * the default one with its textures attached before finishing */
284 }
285
286 /* Cleanup when destroying the engine.
287  * This is not per viewport ! only when quitting blender.
288  * Mostly used for freeing shaders */
289 static void EDIT_LATTICE_engine_free(void)
290 {
291         for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
292                 EDIT_LATTICE_Shaders *sh_data = &e_data.sh_data[sh_data_index];
293                 /* Don't free builtins. */
294                 sh_data->wire = NULL;
295                 GPUShader **sh_data_as_array = (GPUShader **)sh_data;
296                 for (int i = 0; i < (sizeof(EDIT_LATTICE_Shaders) / sizeof(GPUShader *)); i++) {
297                         DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
298                 }
299         }
300
301 }
302
303 static const DrawEngineDataSize EDIT_LATTICE_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_LATTICE_Data);
304
305 DrawEngineType draw_engine_edit_lattice_type = {
306         NULL, NULL,
307         N_("EditLatticeMode"),
308         &EDIT_LATTICE_data_size,
309         &EDIT_LATTICE_engine_init,
310         &EDIT_LATTICE_engine_free,
311         &EDIT_LATTICE_cache_init,
312         &EDIT_LATTICE_cache_populate,
313         &EDIT_LATTICE_cache_finish,
314         NULL, /* draw_background but not needed by mode engines */
315         &EDIT_LATTICE_draw_scene,
316         NULL,
317         NULL,
318 };