Blender  V2.93
eevee_data.c
Go to the documentation of this file.
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 
25 #include "DRW_render.h"
26 
27 #include "BLI_ghash.h"
28 #include "BLI_memblock.h"
29 
30 #include "BKE_duplilist.h"
31 #include "BKE_modifier.h"
32 #include "BKE_object.h"
33 
34 #include "DEG_depsgraph_query.h"
35 
36 #include "GPU_vertex_buffer.h"
37 
38 #include "eevee_lightcache.h"
39 #include "eevee_private.h"
40 
41 /* Motion Blur data. */
42 
43 static void eevee_motion_blur_mesh_data_free(void *val)
44 {
47  switch (geom_mb->type) {
49  for (int j = 0; j < hair_mb->psys_len; j++) {
50  for (int i = 0; i < ARRAY_SIZE(hair_mb->psys[0].hair_pos); i++) {
51  GPU_VERTBUF_DISCARD_SAFE(hair_mb->psys[j].hair_pos[i]);
52  }
53  for (int i = 0; i < ARRAY_SIZE(hair_mb->psys[0].hair_pos); i++) {
54  DRW_TEXTURE_FREE_SAFE(hair_mb->psys[j].hair_pos_tx[i]);
55  }
56  }
57  break;
58 
60  for (int i = 0; i < ARRAY_SIZE(geom_mb->vbo); i++) {
61  GPU_VERTBUF_DISCARD_SAFE(geom_mb->vbo[i]);
62  }
63  break;
64  }
65  MEM_freeN(val);
66 }
67 
68 static uint eevee_object_key_hash(const void *key)
69 {
70  EEVEE_ObjectKey *ob_key = (EEVEE_ObjectKey *)key;
71  uint hash = BLI_ghashutil_ptrhash(ob_key->ob);
73  for (int i = 0; i < MAX_DUPLI_RECUR; i++) {
74  if (ob_key->id[i] != 0) {
76  }
77  else {
78  break;
79  }
80  }
81  return hash;
82 }
83 
84 /* Return false if equal. */
85 static bool eevee_object_key_cmp(const void *a, const void *b)
86 {
87  EEVEE_ObjectKey *key_a = (EEVEE_ObjectKey *)a;
88  EEVEE_ObjectKey *key_b = (EEVEE_ObjectKey *)b;
89 
90  if (key_a->ob != key_b->ob) {
91  return true;
92  }
93  if (key_a->parent != key_b->parent) {
94  return true;
95  }
96  if (memcmp(key_a->id, key_b->id, sizeof(key_a->id)) != 0) {
97  return true;
98  }
99  return false;
100 }
101 
103 {
104  if (mb->object == NULL) {
105  mb->object = BLI_ghash_new(eevee_object_key_hash, eevee_object_key_cmp, "EEVEE Object Motion");
106  }
107  if (mb->geom == NULL) {
108  mb->geom = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "EEVEE Mesh Motion");
109  }
110 }
111 
113 {
114  if (mb->object) {
116  mb->object = NULL;
117  }
118  if (mb->geom) {
120  mb->geom = NULL;
121  }
122 }
123 
125  Object *ob,
126  bool hair)
127 {
128  if (mb->object == NULL) {
129  return NULL;
130  }
131 
132  EEVEE_ObjectKey key, *key_p;
133  /* Small hack to avoid another comparison. */
134  key.ob = (Object *)((char *)ob + hair);
136  if (dup) {
138  memcpy(key.id, dup->persistent_id, sizeof(key.id));
139  }
140  else {
141  key.parent = key.ob;
142  memset(key.id, 0, sizeof(key.id));
143  }
144 
145  EEVEE_ObjectMotionData *ob_step = BLI_ghash_lookup(mb->object, &key);
146  if (ob_step == NULL) {
147  key_p = MEM_mallocN(sizeof(*key_p), __func__);
148  memcpy(key_p, &key, sizeof(*key_p));
149 
150  ob_step = MEM_callocN(sizeof(EEVEE_ObjectMotionData), __func__);
151 
152  BLI_ghash_insert(mb->object, key_p, ob_step);
153  }
154  return ob_step;
155 }
156 
157 static void *motion_blur_deform_data_get(EEVEE_MotionBlurData *mb, Object *ob, bool hair)
158 {
159  if (mb->geom == NULL) {
160  return NULL;
161  }
163  void *key;
164  if (dup) {
165  key = dup->ob;
166  }
167  else {
168  key = ob;
169  }
170  /* Only use data for object that have no modifiers. */
172  key = ob->data;
173  }
174  key = (char *)key + (int)hair;
175  EEVEE_GeometryMotionData *geom_step = BLI_ghash_lookup(mb->geom, key);
176  if (geom_step == NULL) {
177  if (hair) {
179  /* Ugly, we allocate for each modifiers and just fill based on modifier index in the list. */
180  int psys_len = (ob->type != OB_HAIR) ? BLI_listbase_count(&ob->modifiers) : 1;
181  hair_step = MEM_callocN(sizeof(EEVEE_HairMotionData) + sizeof(hair_step->psys[0]) * psys_len,
182  __func__);
183  hair_step->psys_len = psys_len;
184  geom_step = (EEVEE_GeometryMotionData *)hair_step;
185  geom_step->type = EEVEE_MOTION_DATA_HAIR;
186  }
187  else {
188  geom_step = MEM_callocN(sizeof(EEVEE_GeometryMotionData), __func__);
189  geom_step->type = EEVEE_MOTION_DATA_MESH;
190  }
191  BLI_ghash_insert(mb->geom, key, geom_step);
192  }
193  return geom_step;
194 }
195 
197 {
198  return motion_blur_deform_data_get(mb, ob, false);
199 }
200 
202 {
203  return motion_blur_deform_data_get(mb, ob, true);
204 }
205 
206 /* View Layer data. */
207 
208 void EEVEE_view_layer_data_free(void *storage)
209 {
210  EEVEE_ViewLayerData *sldata = (EEVEE_ViewLayerData *)storage;
211 
212  /* Lights */
213  MEM_SAFE_FREE(sldata->lights);
214  DRW_UBO_FREE_SAFE(sldata->light_ubo);
215  DRW_UBO_FREE_SAFE(sldata->shadow_ubo);
219  for (int i = 0; i < 2; i++) {
220  MEM_SAFE_FREE(sldata->shcasters_buffers[i].bbox);
221  MEM_SAFE_FREE(sldata->shcasters_buffers[i].update);
222  }
223 
224  if (sldata->fallback_lightcache) {
226  sldata->fallback_lightcache = NULL;
227  }
228 
229  /* Probes */
230  MEM_SAFE_FREE(sldata->probes);
231  DRW_UBO_FREE_SAFE(sldata->probe_ubo);
232  DRW_UBO_FREE_SAFE(sldata->grid_ubo);
233  DRW_UBO_FREE_SAFE(sldata->planar_ubo);
234  DRW_UBO_FREE_SAFE(sldata->common_ubo);
235 
243  for (int aov_index = 0; aov_index < MAX_AOVS; aov_index++) {
244  DRW_UBO_FREE_SAFE(sldata->renderpass_ubo.aovs[aov_index]);
245  }
246 
247  if (sldata->material_cache) {
249  sldata->material_cache = NULL;
250  }
251 }
252 
254 {
256 }
257 
259 {
260  sldata->common_ubo = GPU_uniformbuf_create(sizeof(sldata->common_data));
261 }
262 
264 {
267 
268  if (*sldata == NULL) {
269  *sldata = MEM_callocN(sizeof(**sldata), "EEVEE_ViewLayerData");
270  eevee_view_layer_init(*sldata);
271  }
272 
273  return *sldata;
274 }
275 
277 {
280 
281  if (*sldata == NULL) {
282  *sldata = MEM_callocN(sizeof(**sldata), "EEVEE_ViewLayerData");
283  eevee_view_layer_init(*sldata);
284  }
285 
286  return *sldata;
287 }
288 
289 /* Object data. */
290 
292 {
294  eevee_data->shadow_caster_id = -1;
295  eevee_data->need_update = false;
296  eevee_data->geom_update = false;
297 }
298 
300 {
301  if (ELEM(ob->type, OB_LIGHTPROBE, OB_LAMP)) {
302  return NULL;
303  }
305 }
306 
308 {
312  sizeof(EEVEE_ObjectEngineData),
314  NULL);
315 }
316 
317 /* Light probe data. */
318 
320 {
322  ped->need_update = false;
323 }
324 
326 {
327  if (ob->type != OB_LIGHTPROBE) {
328  return NULL;
329  }
331 }
332 
334 {
335  BLI_assert(ob->type == OB_LIGHTPROBE);
340  NULL);
341 }
342 
343 /* Light data. */
344 
346 {
348  led->need_update = true;
349 }
350 
352 {
353  if (ob->type != OB_LAMP) {
354  return NULL;
355  }
357 }
358 
360 {
361  BLI_assert(ob->type == OB_LAMP);
364  sizeof(EEVEE_LightEngineData),
366  NULL);
367 }
368 
369 /* World data. */
370 
372 {
374  wed->dd.recalc |= 1;
375 }
376 
378 {
380 }
381 
383 {
386  sizeof(EEVEE_WorldEngineData),
388  NULL);
389 }
General operations, lookup, etc. for blender objects.
int BKE_object_is_modified(struct Scene *scene, struct Object *ob)
Definition: object.c:4869
#define BLI_assert(a)
Definition: BLI_assert.h:58
size_t BLI_ghashutil_combine_hash(size_t hash_a, size_t hash_b)
unsigned int BLI_ghashutil_ptrhash(const void *key)
GHash * BLI_ghash_new(GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:718
bool BLI_ghashutil_ptrcmp(const void *a, const void *b)
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:756
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:1008
#define BLI_ghashutil_inthash(key)
Definition: BLI_ghash.h:356
void * BLI_ghash_lookup(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:803
int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BLI_memblock_destroy(BLI_memblock *mblk, MemblockValFreeFP free_callback) ATTR_NONNULL(1)
Definition: BLI_memblock.c:82
unsigned int uint
Definition: BLI_sys_types.h:83
#define ARRAY_SIZE(arr)
#define ELEM(...)
#define MAX_DUPLI_RECUR
@ OB_LAMP
@ OB_HAIR
@ OB_LIGHTPROBE
#define DRW_UBO_FREE_SAFE(ubo)
Definition: DRW_render.h:188
#define DRW_TEXTURE_FREE_SAFE(tex)
Definition: DRW_render.h:180
#define GPU_FRAMEBUFFER_FREE_SAFE(fb)
#define GPU_uniformbuf_create(size)
#define GPU_VERTBUF_DISCARD_SAFE(verts)
#define MEM_SAFE_FREE(v)
Scene scene
struct Object * DRW_object_get_dupli_parent(const Object *UNUSED(ob))
Definition: draw_manager.c:301
DrawData * DRW_drawdata_ensure(ID *id, DrawEngineType *engine_type, size_t size, DrawDataInitCb init_cb, DrawDataFreeCb free_cb)
Definition: draw_manager.c:899
void * DRW_view_layer_engine_data_get(DrawEngineType *engine_type)
Definition: draw_manager.c:787
void ** DRW_view_layer_engine_data_ensure(DrawEngineType *engine_type, void(*callback)(void *storage))
Definition: draw_manager.c:817
void ** DRW_view_layer_engine_data_ensure_ex(ViewLayer *view_layer, DrawEngineType *engine_type, void(*callback)(void *storage))
Definition: draw_manager.c:797
struct DupliObject * DRW_object_get_dupli(const Object *UNUSED(ob))
Definition: draw_manager.c:306
const DRWContextState * DRW_context_state_get(void)
DrawData * DRW_drawdata_get(ID *id, DrawEngineType *engine_type)
Definition: draw_manager.c:883
EEVEE_LightProbeEngineData * EEVEE_lightprobe_data_get(Object *ob)
Definition: eevee_data.c:325
EEVEE_GeometryMotionData * EEVEE_motion_blur_geometry_data_get(EEVEE_MotionBlurData *mb, Object *ob)
Definition: eevee_data.c:196
static void eevee_lightprobe_data_init(DrawData *dd)
Definition: eevee_data.c:319
EEVEE_LightEngineData * EEVEE_light_data_get(Object *ob)
Definition: eevee_data.c:351
static bool eevee_object_key_cmp(const void *a, const void *b)
Definition: eevee_data.c:85
EEVEE_ViewLayerData * EEVEE_view_layer_data_get(void)
Definition: eevee_data.c:253
static void eevee_world_data_init(DrawData *dd)
Definition: eevee_data.c:371
EEVEE_ViewLayerData * EEVEE_view_layer_data_ensure(void)
Definition: eevee_data.c:276
EEVEE_ObjectEngineData * EEVEE_object_data_ensure(Object *ob)
Definition: eevee_data.c:307
static void eevee_view_layer_init(EEVEE_ViewLayerData *sldata)
Definition: eevee_data.c:258
EEVEE_WorldEngineData * EEVEE_world_data_ensure(World *wo)
Definition: eevee_data.c:382
void EEVEE_motion_blur_data_init(EEVEE_MotionBlurData *mb)
Definition: eevee_data.c:102
static uint eevee_object_key_hash(const void *key)
Definition: eevee_data.c:68
EEVEE_ObjectEngineData * EEVEE_object_data_get(Object *ob)
Definition: eevee_data.c:299
static void eevee_light_data_init(DrawData *dd)
Definition: eevee_data.c:345
static void * motion_blur_deform_data_get(EEVEE_MotionBlurData *mb, Object *ob, bool hair)
Definition: eevee_data.c:157
EEVEE_HairMotionData * EEVEE_motion_blur_hair_data_get(EEVEE_MotionBlurData *mb, Object *ob)
Definition: eevee_data.c:201
static void eevee_motion_blur_mesh_data_free(void *val)
Definition: eevee_data.c:43
EEVEE_LightEngineData * EEVEE_light_data_ensure(Object *ob)
Definition: eevee_data.c:359
EEVEE_ViewLayerData * EEVEE_view_layer_data_ensure_ex(struct ViewLayer *view_layer)
Definition: eevee_data.c:263
void EEVEE_view_layer_data_free(void *storage)
Definition: eevee_data.c:208
void EEVEE_motion_blur_data_free(EEVEE_MotionBlurData *mb)
Definition: eevee_data.c:112
EEVEE_ObjectMotionData * EEVEE_motion_blur_object_data_get(EEVEE_MotionBlurData *mb, Object *ob, bool hair)
Definition: eevee_data.c:124
static void eevee_object_data_init(DrawData *dd)
Definition: eevee_data.c:291
EEVEE_LightProbeEngineData * EEVEE_lightprobe_data_ensure(Object *ob)
Definition: eevee_data.c:333
EEVEE_WorldEngineData * EEVEE_world_data_get(World *wo)
Definition: eevee_data.c:377
DrawEngineType draw_engine_eevee_type
Definition: eevee_engine.c:622
void EEVEE_lightcache_free(LightCache *lcache)
#define MAX_AOVS
Definition: eevee_private.h:56
@ EEVEE_MOTION_DATA_HAIR
@ EEVEE_MOTION_DATA_MESH
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
static unsigned a[3]
Definition: RandGen.cpp:92
#define hash
Definition: noise.c:169
static void hair_step(ParticleSimulationData *sim, float cfra, const bool use_render_params)
int recalc
Definition: DNA_ID.h:55
int persistent_id[8]
Definition: BKE_duplilist.h:54
struct Object * ob
Definition: BKE_duplilist.h:45
struct GPUVertBuf * vbo[2]
eEEVEEMotionData type
struct EEVEE_HairMotionData::@201 psys[0]
struct GPUTexture * hair_pos_tx[2]
struct GPUVertBuf * hair_pos[2]
struct GHash * geom
struct GHash * object
struct Object * ob
struct Object * parent
struct EEVEE_CommonUniformBuffer common_data
struct GPUTexture * shadow_cube_pool
struct GPUUniformBuf * combined
struct GPUUniformBuf * diff_light
struct GPUUniformBuf * shadow_ubo
struct GPUUniformBuf * probe_ubo
struct GPUUniformBuf * grid_ubo
struct GPUUniformBuf * emit
struct GPUFrameBuffer * shadow_fb
struct GPUUniformBuf * planar_ubo
struct GPUUniformBuf * spec_color
struct EEVEE_ViewLayerData::@202 renderpass_ubo
struct GPUUniformBuf * environment
struct LightCache * fallback_lightcache
struct GPUUniformBuf * diff_color
struct GPUUniformBuf * spec_light
struct GPUUniformBuf * common_ubo
struct BLI_memblock * material_cache
struct GPUUniformBuf * light_ubo
struct GPUUniformBuf * aovs[MAX_AOVS]
struct GPUTexture * shadow_cascade_pool
struct EEVEE_ShadowCasterBuffer shcasters_buffers[2]
struct EEVEE_LightsInfo * lights
struct EEVEE_LightProbesInfo * probes
ListBase modifiers
void * data