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