1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
/*
* Copyright 2016, Blender Foundation.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor(s): Blender Institute
*
*/
/** \file blender/draw/modes/particle_mode.c
* \ingroup draw
*/
#include "DRW_engine.h"
#include "DRW_render.h"
/* If builtin shaders are needed */
#include "GPU_shader.h"
#include "draw_common.h"
#include "draw_mode_engines.h"
/* *********** LISTS *********** */
/* All lists are per viewport specific datas.
* They are all free when viewport changes engines
* or is free itself. Use PARTICLE_engine_init() to
* initialize most of them and PARTICLE_cache_init()
* for PARTICLE_PassList */
typedef struct PARTICLE_PassList {
/* Declare all passes here and init them in
* PARTICLE_cache_init().
* Only contains (DRWPass *) */
struct DRWPass *pass;
} PARTICLE_PassList;
typedef struct PARTICLE_FramebufferList {
/* Contains all framebuffer objects needed by this engine.
* Only contains (GPUFrameBuffer *) */
struct GPUFrameBuffer *fb;
} PARTICLE_FramebufferList;
typedef struct PARTICLE_TextureList {
/* Contains all framebuffer textures / utility textures
* needed by this engine. Only viewport specific textures
* (not per object). Only contains (GPUTexture *) */
struct GPUTexture *texture;
} PARTICLE_TextureList;
typedef struct PARTICLE_StorageList {
/* Contains any other memory block that the engine needs.
* Only directly MEM_(m/c)allocN'ed blocks because they are
* free with MEM_freeN() when viewport is freed.
* (not per object) */
struct CustomStruct *block;
struct PARTICLE_PrivateData *g_data;
} PARTICLE_StorageList;
typedef struct PARTICLE_Data {
/* Struct returned by DRW_viewport_engine_data_ensure.
* If you don't use one of these, just make it a (void *) */
// void *fbl;
void *engine_type; /* Required */
PARTICLE_FramebufferList *fbl;
PARTICLE_TextureList *txl;
PARTICLE_PassList *psl;
PARTICLE_StorageList *stl;
} PARTICLE_Data;
/* *********** STATIC *********** */
static struct {
/* Custom shaders :
* Add sources to source/blender/draw/modes/shaders
* init in PARTICLE_engine_init();
* free in PARTICLE_engine_free(); */
struct GPUShader *custom_shader;
} e_data = {NULL}; /* Engine data */
typedef struct PARTICLE_PrivateData {
/* This keeps the references of the shading groups for
* easy access in PARTICLE_cache_populate() */
DRWShadingGroup *group;
} PARTICLE_PrivateData; /* Transient data */
/* *********** FUNCTIONS *********** */
/* Init Textures, Framebuffers, Storage and Shaders.
* It is called for every frames.
* (Optional) */
static void PARTICLE_engine_init(void *vedata)
{
PARTICLE_TextureList *txl = ((PARTICLE_Data *)vedata)->txl;
PARTICLE_FramebufferList *fbl = ((PARTICLE_Data *)vedata)->fbl;
PARTICLE_StorageList *stl = ((PARTICLE_Data *)vedata)->stl;
UNUSED_VARS(txl, fbl, stl);
/* Init Framebuffers like this: order is attachment order (for color texs) */
/*
* DRWFboTexture tex[2] = {{&txl->depth, DRW_TEX_DEPTH_24, 0},
* {&txl->color, DRW_TEX_RGBA_8, DRW_TEX_FILTER}};
*/
/* DRW_framebuffer_init takes care of checking if
* the framebuffer is valid and has the right size*/
/*
* float *viewport_size = DRW_viewport_size_get();
* DRW_framebuffer_init(&fbl->occlude_wire_fb,
* (int)viewport_size[0], (int)viewport_size[1],
* tex, 2);
*/
if (!e_data.custom_shader) {
e_data.custom_shader = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
}
}
/* Here init all passes and shading groups
* Assume that all Passes are NULL */
static void PARTICLE_cache_init(void *vedata)
{
PARTICLE_PassList *psl = ((PARTICLE_Data *)vedata)->psl;
PARTICLE_StorageList *stl = ((PARTICLE_Data *)vedata)->stl;
if (!stl->g_data) {
/* Alloc transient pointers */
stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
}
{
/* Create a pass */
DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND | DRW_STATE_WIRE;
psl->pass = DRW_pass_create("My Pass", state);
/* Create a shadingGroup using a function in draw_common.c or custom one */
/*
* stl->g_data->group = shgroup_dynlines_uniform_color(psl->pass, ts.colorWire);
* -- or --
* stl->g_data->group = DRW_shgroup_create(e_data.custom_shader, psl->pass);
*/
stl->g_data->group = DRW_shgroup_create(e_data.custom_shader, psl->pass);
/* Uniforms need a pointer to it's value so be sure it's accessible at
* any given time (i.e. use static vars) */
static float color[4] = {0.2f, 0.5f, 0.3f, 1.0};
DRW_shgroup_uniform_vec4(stl->g_data->group, "color", color, 1);
}
}
/* Add geometry to shadingGroups. Execute for each objects */
static void PARTICLE_cache_populate(void *vedata, Object *ob)
{
PARTICLE_PassList *psl = ((PARTICLE_Data *)vedata)->psl;
PARTICLE_StorageList *stl = ((PARTICLE_Data *)vedata)->stl;
UNUSED_VARS(psl, stl);
if (ob->type == OB_MESH) {
/* Get geometry cache */
struct Gwn_Batch *geom = DRW_cache_mesh_surface_get(ob);
/* Add geom to a shading group */
DRW_shgroup_call_add(stl->g_data->group, geom, ob->obmat);
}
}
/* Optional: Post-cache_populate callback */
static void PARTICLE_cache_finish(void *vedata)
{
PARTICLE_PassList *psl = ((PARTICLE_Data *)vedata)->psl;
PARTICLE_StorageList *stl = ((PARTICLE_Data *)vedata)->stl;
/* Do something here! dependant on the objects gathered */
UNUSED_VARS(psl, stl);
}
/* Draw time ! Control rendering pipeline from here */
static void PARTICLE_draw_scene(void *vedata)
{
PARTICLE_PassList *psl = ((PARTICLE_Data *)vedata)->psl;
PARTICLE_FramebufferList *fbl = ((PARTICLE_Data *)vedata)->fbl;
/* Default framebuffer and texture */
DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
UNUSED_VARS(fbl, dfbl, dtxl);
/* Show / hide entire passes, swap framebuffers ... whatever you fancy */
/*
* DRW_framebuffer_texture_detach(dtxl->depth);
* DRW_framebuffer_bind(fbl->custom_fb);
* DRW_draw_pass(psl->pass);
* DRW_framebuffer_texture_attach(dfbl->default_fb, dtxl->depth, 0, 0);
* DRW_framebuffer_bind(dfbl->default_fb);
*/
/* ... or just render passes on default framebuffer. */
DRW_draw_pass(psl->pass);
/* If you changed framebuffer, double check you rebind
* the default one with its textures attached before finishing */
}
/* Cleanup when destroying the engine.
* This is not per viewport ! only when quitting blender.
* Mostly used for freeing shaders */
static void PARTICLE_engine_free(void)
{
// DRW_SHADER_FREE_SAFE(custom_shader);
}
/* Create collection settings here.
*
* Be sure to add this function there :
* source/blender/draw/DRW_engine.h
* source/blender/blenkernel/intern/layer.c
* source/blenderplayer/bad_level_call_stubs/stubs.c
*
* And relevant collection settings to :
* source/blender/makesrna/intern/rna_scene.c
* source/blender/blenkernel/intern/layer.c
*/
#if 0
void PARTICLE_collection_settings_create(CollectionEngineSettings *ces)
{
BLI_assert(ces);
// BKE_collection_engine_property_add_int(ces, "my_bool_prop", false);
// BKE_collection_engine_property_add_int(ces, "my_int_prop", 0);
// BKE_collection_engine_property_add_float(ces, "my_float_prop", 0.0f);
}
#endif
static const DrawEngineDataSize PARTICLE_data_size = DRW_VIEWPORT_DATA_SIZE(PARTICLE_Data);
DrawEngineType draw_engine_particle_type = {
NULL, NULL,
N_("ParticleMode"),
&PARTICLE_data_size,
&PARTICLE_engine_init,
&PARTICLE_engine_free,
&PARTICLE_cache_init,
&PARTICLE_cache_populate,
&PARTICLE_cache_finish,
NULL, /* draw_background but not needed by mode engines */
&PARTICLE_draw_scene,
NULL,
};
|