DRW: generalize selecting between regular/clipped shaders
[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         EDIT_LATTICE_Shaders sh_data[DRW_SHADER_SLOT_LEN];
109
110 } e_data = {NULL}; /* Engine data */
111
112 typedef struct EDIT_LATTICE_PrivateData {
113         /* This keeps the references of the shading groups for
114          * easy access in EDIT_LATTICE_cache_populate() */
115         DRWShadingGroup *wire_shgrp;
116         DRWShadingGroup *vert_shgrp;
117 } EDIT_LATTICE_PrivateData; /* Transient data */
118
119 /* *********** FUNCTIONS *********** */
120
121 /* Init Textures, Framebuffers, Storage and Shaders.
122  * It is called for every frames.
123  * (Optional) */
124 static void EDIT_LATTICE_engine_init(void *vedata)
125 {
126         EDIT_LATTICE_TextureList *txl = ((EDIT_LATTICE_Data *)vedata)->txl;
127         EDIT_LATTICE_FramebufferList *fbl = ((EDIT_LATTICE_Data *)vedata)->fbl;
128         EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
129
130         UNUSED_VARS(txl, fbl, stl);
131
132         /* Init Framebuffers like this: order is attachment order (for color texs) */
133         /*
134          * DRWFboTexture tex[2] = {{&txl->depth, GPU_DEPTH_COMPONENT24, 0},
135          *                         {&txl->color, GPU_RGBA8, DRW_TEX_FILTER}};
136          */
137
138         /* DRW_framebuffer_init takes care of checking if
139          * the framebuffer is valid and has the right size*/
140         /*
141          * float *viewport_size = DRW_viewport_size_get();
142          * DRW_framebuffer_init(&fbl->occlude_wire_fb,
143          *                     (int)viewport_size[0], (int)viewport_size[1],
144          *                     tex, 2);
145          */
146
147         const DRWContextState *draw_ctx = DRW_context_state_get();
148         EDIT_LATTICE_Shaders *sh_data = &e_data.sh_data[draw_ctx->shader_slot];
149         const bool is_clip = (draw_ctx->rv3d->rflag & RV3D_CLIPPING) != 0;
150         if (is_clip) {
151                 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
152         }
153         const char *world_clip_lib_or_empty = is_clip ? datatoc_common_world_clip_lib_glsl : "";
154         const char *world_clip_def_or_empty = is_clip ? "#define USE_WORLD_CLIP_PLANES\n" : "";
155
156         if (!sh_data->wire) {
157                 sh_data->wire = GPU_shader_get_builtin_shader(GPU_SHADER_3D_SMOOTH_COLOR);
158         }
159
160         if (!sh_data->overlay_vert) {
161                 sh_data->overlay_vert = DRW_shader_create_from_arrays({
162                         .vert = (const char *[]){
163                             world_clip_lib_or_empty,
164                             datatoc_common_globals_lib_glsl,
165                             datatoc_edit_lattice_overlay_loosevert_vert_glsl,
166                             NULL},
167                         .frag = (const char *[]){
168                             datatoc_common_globals_lib_glsl,
169                             datatoc_edit_lattice_overlay_frag_glsl,
170                             NULL},
171                         .defs = (const char *[]){world_clip_def_or_empty, NULL}});
172
173         }
174 }
175
176 /* Here init all passes and shading groups
177  * Assume that all Passes are NULL */
178 static void EDIT_LATTICE_cache_init(void *vedata)
179 {
180         EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
181         EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
182
183         const DRWContextState *draw_ctx = DRW_context_state_get();
184         RegionView3D *rv3d = draw_ctx->rv3d;
185         EDIT_LATTICE_Shaders *sh_data = &e_data.sh_data[draw_ctx->shader_slot];
186
187         if (!stl->g_data) {
188                 /* Alloc transient pointers */
189                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
190         }
191
192         {
193                 psl->wire_pass = DRW_pass_create(
194                         "Lattice Wire",
195                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WIRE);
196                 stl->g_data->wire_shgrp = DRW_shgroup_create(sh_data->wire, psl->wire_pass);
197
198                 psl->vert_pass = DRW_pass_create(
199                         "Lattice Verts",
200                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_POINT);
201                 stl->g_data->vert_shgrp = DRW_shgroup_create(sh_data->overlay_vert, psl->vert_pass);
202                 DRW_shgroup_uniform_block(stl->g_data->vert_shgrp, "globalsBlock", G_draw.block_ubo);
203                 if (rv3d->rflag & RV3D_CLIPPING) {
204                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->vert_shgrp, rv3d);
205                 }
206
207
208         }
209 }
210
211 /* Add geometry to shadingGroups. Execute for each objects */
212 static void EDIT_LATTICE_cache_populate(void *vedata, Object *ob)
213 {
214         EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
215         EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
216         const DRWContextState *draw_ctx = DRW_context_state_get();
217
218         UNUSED_VARS(psl);
219
220         if (ob->type == OB_LATTICE) {
221                 if ((ob == draw_ctx->object_edit) || BKE_object_is_in_editmode(ob)) {
222                         /* Get geometry cache */
223                         struct GPUBatch *geom;
224
225                         geom = DRW_cache_lattice_wire_get(ob, true);
226                         DRW_shgroup_call_add(stl->g_data->wire_shgrp, geom, ob->obmat);
227
228                         geom = DRW_cache_lattice_vert_overlay_get(ob);
229                         DRW_shgroup_call_add(stl->g_data->vert_shgrp, geom, ob->obmat);
230                 }
231         }
232 }
233
234 /* Optional: Post-cache_populate callback */
235 static void EDIT_LATTICE_cache_finish(void *vedata)
236 {
237         EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
238         EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
239
240         /* Do something here! dependent on the objects gathered */
241         UNUSED_VARS(psl, stl);
242 }
243
244 /* Draw time ! Control rendering pipeline from here */
245 static void EDIT_LATTICE_draw_scene(void *vedata)
246 {
247         EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
248         EDIT_LATTICE_FramebufferList *fbl = ((EDIT_LATTICE_Data *)vedata)->fbl;
249
250         /* Default framebuffer and texture */
251         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
252         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
253
254         UNUSED_VARS(fbl);
255
256         MULTISAMPLE_SYNC_ENABLE(dfbl, dtxl);
257
258         /* Show / hide entire passes, swap framebuffers ... whatever you fancy */
259         /*
260          * DRW_framebuffer_texture_detach(dtxl->depth);
261          * DRW_framebuffer_bind(fbl->custom_fb);
262          * DRW_draw_pass(psl->pass);
263          * DRW_framebuffer_texture_attach(dfbl->default_fb, dtxl->depth, 0, 0);
264          * DRW_framebuffer_bind(dfbl->default_fb);
265          */
266
267         /* ... or just render passes on default framebuffer. */
268         DRW_draw_pass(psl->wire_pass);
269         DRW_draw_pass(psl->vert_pass);
270
271         MULTISAMPLE_SYNC_DISABLE(dfbl, dtxl)
272
273         /* If you changed framebuffer, double check you rebind
274          * the default one with its textures attached before finishing */
275 }
276
277 /* Cleanup when destroying the engine.
278  * This is not per viewport ! only when quitting blender.
279  * Mostly used for freeing shaders */
280 static void EDIT_LATTICE_engine_free(void)
281 {
282         for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
283                 EDIT_LATTICE_Shaders *sh_data = &e_data.sh_data[sh_data_index];
284                 /* Don't free builtins. */
285                 sh_data->wire = NULL;
286                 GPUShader **sh_data_as_array = (GPUShader **)sh_data;
287                 for (int i = 0; i < (sizeof(EDIT_LATTICE_Shaders) / sizeof(GPUShader *)); i++) {
288                         DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
289                 }
290         }
291
292 }
293
294 static const DrawEngineDataSize EDIT_LATTICE_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_LATTICE_Data);
295
296 DrawEngineType draw_engine_edit_lattice_type = {
297         NULL, NULL,
298         N_("EditLatticeMode"),
299         &EDIT_LATTICE_data_size,
300         &EDIT_LATTICE_engine_init,
301         &EDIT_LATTICE_engine_free,
302         &EDIT_LATTICE_cache_init,
303         &EDIT_LATTICE_cache_populate,
304         &EDIT_LATTICE_cache_finish,
305         NULL, /* draw_background but not needed by mode engines */
306         &EDIT_LATTICE_draw_scene,
307         NULL,
308         NULL,
309 };