9096664cdd02b1b89df9021eeb0557ee32ea716b
[blender.git] / source / blender / draw / modes / edit_lattice_mode.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2016, Blender Foundation.
17  */
18
19 /** \file \ingroup draw
20  */
21
22 #include "DRW_engine.h"
23 #include "DRW_render.h"
24
25 #include "BKE_object.h"
26
27 /* If builtin shaders are needed */
28 #include "GPU_shader.h"
29
30 #include "draw_common.h"
31
32 extern char datatoc_gpu_shader_cfg_world_clip_lib_glsl[];
33 extern char datatoc_common_globals_lib_glsl[];
34
35 extern char datatoc_edit_lattice_overlay_loosevert_vert_glsl[];
36 extern char datatoc_edit_lattice_overlay_frag_glsl[];
37
38 /* *********** LISTS *********** */
39 /* All lists are per viewport specific datas.
40  * They are all free when viewport changes engines
41  * or is free itself. Use EDIT_LATTICE_engine_init() to
42  * initialize most of them and EDIT_LATTICE_cache_init()
43  * for EDIT_LATTICE_PassList */
44
45 typedef struct EDIT_LATTICE_PassList {
46         /* Declare all passes here and init them in
47          * EDIT_LATTICE_cache_init().
48          * Only contains (DRWPass *) */
49         struct DRWPass *wire_pass;
50         struct DRWPass *vert_pass;
51 } EDIT_LATTICE_PassList;
52
53 typedef struct EDIT_LATTICE_FramebufferList {
54         /* Contains all framebuffer objects needed by this engine.
55          * Only contains (GPUFrameBuffer *) */
56         struct GPUFrameBuffer *fb;
57 } EDIT_LATTICE_FramebufferList;
58
59 typedef struct EDIT_LATTICE_TextureList {
60         /* Contains all framebuffer textures / utility textures
61          * needed by this engine. Only viewport specific textures
62          * (not per object). Only contains (GPUTexture *) */
63         struct GPUTexture *texture;
64 } EDIT_LATTICE_TextureList;
65
66 typedef struct EDIT_LATTICE_StorageList {
67         /* Contains any other memory block that the engine needs.
68          * Only directly MEM_(m/c)allocN'ed blocks because they are
69          * free with MEM_freeN() when viewport is freed.
70          * (not per object) */
71         struct CustomStruct *block;
72         struct EDIT_LATTICE_PrivateData *g_data;
73 } EDIT_LATTICE_StorageList;
74
75 typedef struct EDIT_LATTICE_Data {
76         /* Struct returned by DRW_viewport_engine_data_ensure.
77          * If you don't use one of these, just make it a (void *) */
78         // void *fbl;
79         void *engine_type; /* Required */
80         EDIT_LATTICE_FramebufferList *fbl;
81         EDIT_LATTICE_TextureList *txl;
82         EDIT_LATTICE_PassList *psl;
83         EDIT_LATTICE_StorageList *stl;
84 } EDIT_LATTICE_Data;
85
86 typedef struct EDIT_LATTICE_Shaders {
87         GPUShader *wire;
88         GPUShader *overlay_vert;
89 } EDIT_LATTICE_Shaders;
90
91 /* *********** STATIC *********** */
92
93 static struct {
94         /* Custom shaders :
95          * Add sources to source/blender/draw/modes/shaders
96          * init in EDIT_LATTICE_engine_init();
97          * free in EDIT_LATTICE_engine_free(); */
98
99         EDIT_LATTICE_Shaders sh_data[GPU_SHADER_CFG_LEN];
100
101 } e_data = {NULL}; /* Engine data */
102
103 typedef struct EDIT_LATTICE_PrivateData {
104         /* This keeps the references of the shading groups for
105          * easy access in EDIT_LATTICE_cache_populate() */
106         DRWShadingGroup *wire_shgrp;
107         DRWShadingGroup *vert_shgrp;
108 } EDIT_LATTICE_PrivateData; /* Transient data */
109
110 /* *********** FUNCTIONS *********** */
111
112 /* Init Textures, Framebuffers, Storage and Shaders.
113  * It is called for every frames.
114  * (Optional) */
115 static void EDIT_LATTICE_engine_init(void *vedata)
116 {
117         EDIT_LATTICE_TextureList *txl = ((EDIT_LATTICE_Data *)vedata)->txl;
118         EDIT_LATTICE_FramebufferList *fbl = ((EDIT_LATTICE_Data *)vedata)->fbl;
119         EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
120
121         UNUSED_VARS(txl, fbl, stl);
122
123         /* Init Framebuffers like this: order is attachment order (for color texs) */
124         /*
125          * DRWFboTexture tex[2] = {{&txl->depth, GPU_DEPTH_COMPONENT24, 0},
126          *                         {&txl->color, GPU_RGBA8, DRW_TEX_FILTER}};
127          */
128
129         /* DRW_framebuffer_init takes care of checking if
130          * the framebuffer is valid and has the right size*/
131         /*
132          * float *viewport_size = DRW_viewport_size_get();
133          * DRW_framebuffer_init(&fbl->occlude_wire_fb,
134          *                     (int)viewport_size[0], (int)viewport_size[1],
135          *                     tex, 2);
136          */
137
138         const DRWContextState *draw_ctx = DRW_context_state_get();
139         EDIT_LATTICE_Shaders *sh_data = &e_data.sh_data[draw_ctx->shader_cfg];
140         const bool is_clip = (draw_ctx->rv3d->rflag & RV3D_CLIPPING) != 0;
141         if (is_clip) {
142                 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
143         }
144         const char *world_clip_lib_or_empty = is_clip ? datatoc_gpu_shader_cfg_world_clip_lib_glsl : "";
145         const char *world_clip_def_or_empty = is_clip ? "#define USE_WORLD_CLIP_PLANES\n" : "";
146
147         if (!sh_data->wire) {
148                 sh_data->wire = GPU_shader_get_builtin_shader_with_config(GPU_SHADER_3D_SMOOTH_COLOR, draw_ctx->shader_cfg);
149         }
150
151         if (!sh_data->overlay_vert) {
152                 sh_data->overlay_vert = GPU_shader_create_from_arrays({
153                         .vert = (const char *[]){
154                             world_clip_lib_or_empty,
155                             datatoc_common_globals_lib_glsl,
156                             datatoc_edit_lattice_overlay_loosevert_vert_glsl,
157                             NULL},
158                         .frag = (const char *[]){
159                             datatoc_common_globals_lib_glsl,
160                             datatoc_edit_lattice_overlay_frag_glsl,
161                             NULL},
162                         .defs = (const char *[]){world_clip_def_or_empty, NULL},
163                 });
164
165         }
166 }
167
168 /* Here init all passes and shading groups
169  * Assume that all Passes are NULL */
170 static void EDIT_LATTICE_cache_init(void *vedata)
171 {
172         EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
173         EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
174
175         const DRWContextState *draw_ctx = DRW_context_state_get();
176         RegionView3D *rv3d = draw_ctx->rv3d;
177         EDIT_LATTICE_Shaders *sh_data = &e_data.sh_data[draw_ctx->shader_cfg];
178
179         if (!stl->g_data) {
180                 /* Alloc transient pointers */
181                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
182         }
183
184         {
185                 psl->wire_pass = DRW_pass_create(
186                         "Lattice Wire",
187                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WIRE);
188                 stl->g_data->wire_shgrp = DRW_shgroup_create(sh_data->wire, psl->wire_pass);
189                 if (rv3d->rflag & RV3D_CLIPPING) {
190                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->wire_shgrp, rv3d);
191                 }
192
193                 psl->vert_pass = DRW_pass_create(
194                         "Lattice Verts",
195                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_POINT);
196                 stl->g_data->vert_shgrp = DRW_shgroup_create(sh_data->overlay_vert, psl->vert_pass);
197                 DRW_shgroup_uniform_block(stl->g_data->vert_shgrp, "globalsBlock", G_draw.block_ubo);
198                 if (rv3d->rflag & RV3D_CLIPPING) {
199                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->vert_shgrp, rv3d);
200                 }
201
202
203         }
204 }
205
206 /* Add geometry to shadingGroups. Execute for each objects */
207 static void EDIT_LATTICE_cache_populate(void *vedata, Object *ob)
208 {
209         EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
210         EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
211         const DRWContextState *draw_ctx = DRW_context_state_get();
212
213         UNUSED_VARS(psl);
214
215         if (ob->type == OB_LATTICE) {
216                 if ((ob == draw_ctx->object_edit) || BKE_object_is_in_editmode(ob)) {
217                         /* Get geometry cache */
218                         struct GPUBatch *geom;
219
220                         geom = DRW_cache_lattice_wire_get(ob, true);
221                         DRW_shgroup_call_add(stl->g_data->wire_shgrp, geom, ob->obmat);
222
223                         geom = DRW_cache_lattice_vert_overlay_get(ob);
224                         DRW_shgroup_call_add(stl->g_data->vert_shgrp, geom, ob->obmat);
225                 }
226         }
227 }
228
229 /* Optional: Post-cache_populate callback */
230 static void EDIT_LATTICE_cache_finish(void *vedata)
231 {
232         EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
233         EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
234
235         /* Do something here! dependent on the objects gathered */
236         UNUSED_VARS(psl, stl);
237 }
238
239 /* Draw time ! Control rendering pipeline from here */
240 static void EDIT_LATTICE_draw_scene(void *vedata)
241 {
242         EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
243         EDIT_LATTICE_FramebufferList *fbl = ((EDIT_LATTICE_Data *)vedata)->fbl;
244
245         /* Default framebuffer and texture */
246         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
247         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
248
249         UNUSED_VARS(fbl);
250
251         MULTISAMPLE_SYNC_ENABLE(dfbl, dtxl);
252
253         /* Show / hide entire passes, swap framebuffers ... whatever you fancy */
254         /*
255          * DRW_framebuffer_texture_detach(dtxl->depth);
256          * DRW_framebuffer_bind(fbl->custom_fb);
257          * DRW_draw_pass(psl->pass);
258          * DRW_framebuffer_texture_attach(dfbl->default_fb, dtxl->depth, 0, 0);
259          * DRW_framebuffer_bind(dfbl->default_fb);
260          */
261
262         /* ... or just render passes on default framebuffer. */
263         DRW_draw_pass(psl->wire_pass);
264         DRW_draw_pass(psl->vert_pass);
265
266         MULTISAMPLE_SYNC_DISABLE(dfbl, dtxl)
267
268         /* If you changed framebuffer, double check you rebind
269          * the default one with its textures attached before finishing */
270 }
271
272 /* Cleanup when destroying the engine.
273  * This is not per viewport ! only when quitting blender.
274  * Mostly used for freeing shaders */
275 static void EDIT_LATTICE_engine_free(void)
276 {
277         for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
278                 EDIT_LATTICE_Shaders *sh_data = &e_data.sh_data[sh_data_index];
279                 /* Don't free builtins. */
280                 sh_data->wire = NULL;
281                 GPUShader **sh_data_as_array = (GPUShader **)sh_data;
282                 for (int i = 0; i < (sizeof(EDIT_LATTICE_Shaders) / sizeof(GPUShader *)); i++) {
283                         DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
284                 }
285         }
286
287 }
288
289 static const DrawEngineDataSize EDIT_LATTICE_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_LATTICE_Data);
290
291 DrawEngineType draw_engine_edit_lattice_type = {
292         NULL, NULL,
293         N_("EditLatticeMode"),
294         &EDIT_LATTICE_data_size,
295         &EDIT_LATTICE_engine_init,
296         &EDIT_LATTICE_engine_free,
297         &EDIT_LATTICE_cache_init,
298         &EDIT_LATTICE_cache_populate,
299         &EDIT_LATTICE_cache_finish,
300         NULL, /* draw_background but not needed by mode engines */
301         &EDIT_LATTICE_draw_scene,
302         NULL,
303         NULL,
304 };