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.
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.
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.
16 * Copyright 2016, Blender Foundation.
19 /** \file blender/draw/modes/edit_lattice_mode.c
23 #include "DRW_engine.h"
24 #include "DRW_render.h"
26 #include "BKE_object.h"
28 /* If builtin shaders are needed */
29 #include "GPU_shader.h"
31 #include "draw_common.h"
33 extern char datatoc_gpu_shader_cfg_world_clip_lib_glsl[];
34 extern char datatoc_common_globals_lib_glsl[];
36 extern char datatoc_edit_lattice_overlay_loosevert_vert_glsl[];
37 extern char datatoc_edit_lattice_overlay_frag_glsl[];
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 */
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;
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;
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;
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.
72 struct CustomStruct *block;
73 struct EDIT_LATTICE_PrivateData *g_data;
74 } EDIT_LATTICE_StorageList;
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 *) */
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;
87 typedef struct EDIT_LATTICE_Shaders {
89 GPUShader *overlay_vert;
90 } EDIT_LATTICE_Shaders;
92 /* *********** STATIC *********** */
96 * Add sources to source/blender/draw/modes/shaders
97 * init in EDIT_LATTICE_engine_init();
98 * free in EDIT_LATTICE_engine_free(); */
100 EDIT_LATTICE_Shaders sh_data[GPU_SHADER_CFG_LEN];
102 } e_data = {NULL}; /* Engine data */
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 */
111 /* *********** FUNCTIONS *********** */
113 /* Init Textures, Framebuffers, Storage and Shaders.
114 * It is called for every frames.
116 static void EDIT_LATTICE_engine_init(void *vedata)
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;
122 UNUSED_VARS(txl, fbl, stl);
124 /* Init Framebuffers like this: order is attachment order (for color texs) */
126 * DRWFboTexture tex[2] = {{&txl->depth, GPU_DEPTH_COMPONENT24, 0},
127 * {&txl->color, GPU_RGBA8, DRW_TEX_FILTER}};
130 /* DRW_framebuffer_init takes care of checking if
131 * the framebuffer is valid and has the right size*/
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],
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;
143 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
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" : "";
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);
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,
159 .frag = (const char *[]){
160 datatoc_common_globals_lib_glsl,
161 datatoc_edit_lattice_overlay_frag_glsl,
163 .defs = (const char *[]){world_clip_def_or_empty, NULL},
169 /* Here init all passes and shading groups
170 * Assume that all Passes are NULL */
171 static void EDIT_LATTICE_cache_init(void *vedata)
173 EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
174 EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
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];
181 /* Alloc transient pointers */
182 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
186 psl->wire_pass = DRW_pass_create(
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);
194 psl->vert_pass = DRW_pass_create(
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);
207 /* Add geometry to shadingGroups. Execute for each objects */
208 static void EDIT_LATTICE_cache_populate(void *vedata, Object *ob)
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();
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;
221 geom = DRW_cache_lattice_wire_get(ob, true);
222 DRW_shgroup_call_add(stl->g_data->wire_shgrp, geom, ob->obmat);
224 geom = DRW_cache_lattice_vert_overlay_get(ob);
225 DRW_shgroup_call_add(stl->g_data->vert_shgrp, geom, ob->obmat);
230 /* Optional: Post-cache_populate callback */
231 static void EDIT_LATTICE_cache_finish(void *vedata)
233 EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
234 EDIT_LATTICE_StorageList *stl = ((EDIT_LATTICE_Data *)vedata)->stl;
236 /* Do something here! dependent on the objects gathered */
237 UNUSED_VARS(psl, stl);
240 /* Draw time ! Control rendering pipeline from here */
241 static void EDIT_LATTICE_draw_scene(void *vedata)
243 EDIT_LATTICE_PassList *psl = ((EDIT_LATTICE_Data *)vedata)->psl;
244 EDIT_LATTICE_FramebufferList *fbl = ((EDIT_LATTICE_Data *)vedata)->fbl;
246 /* Default framebuffer and texture */
247 DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
248 DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
252 MULTISAMPLE_SYNC_ENABLE(dfbl, dtxl);
254 /* Show / hide entire passes, swap framebuffers ... whatever you fancy */
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);
263 /* ... or just render passes on default framebuffer. */
264 DRW_draw_pass(psl->wire_pass);
265 DRW_draw_pass(psl->vert_pass);
267 MULTISAMPLE_SYNC_DISABLE(dfbl, dtxl)
269 /* If you changed framebuffer, double check you rebind
270 * the default one with its textures attached before finishing */
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)
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]);
290 static const DrawEngineDataSize EDIT_LATTICE_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_LATTICE_Data);
292 DrawEngineType draw_engine_edit_lattice_type = {
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,