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