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