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