Blender  V3.3
eevee_data.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2016 Blender Foundation. */
3 
10 #include "DRW_render.h"
11 
12 #include "BLI_ghash.h"
13 #include "BLI_memblock.h"
14 
15 #include "BKE_duplilist.h"
16 #include "BKE_modifier.h"
17 #include "BKE_object.h"
18 
19 #include "DEG_depsgraph_query.h"
20 
21 #include "GPU_vertex_buffer.h"
22 
23 #include "eevee_lightcache.h"
24 #include "eevee_private.h"
25 
26 /* Motion Blur data. */
27 
28 static void eevee_motion_blur_mesh_data_free(void *val)
29 {
31  if (mb_data->hair_data != NULL) {
32  MEM_freeN(mb_data->hair_data);
33  }
34  if (mb_data->geometry_data != NULL) {
35  MEM_freeN(mb_data->geometry_data);
36  }
37  MEM_freeN(val);
38 }
39 
40 static uint eevee_object_key_hash(const void *key)
41 {
42  EEVEE_ObjectKey *ob_key = (EEVEE_ObjectKey *)key;
43  uint hash = BLI_ghashutil_ptrhash(ob_key->ob);
45  for (int i = 0; i < MAX_DUPLI_RECUR; i++) {
46  if (ob_key->id[i] != 0) {
48  }
49  else {
50  break;
51  }
52  }
53  return hash;
54 }
55 
56 /* Return false if equal. */
57 static bool eevee_object_key_cmp(const void *a, const void *b)
58 {
59  EEVEE_ObjectKey *key_a = (EEVEE_ObjectKey *)a;
60  EEVEE_ObjectKey *key_b = (EEVEE_ObjectKey *)b;
61 
62  if (key_a->ob != key_b->ob) {
63  return true;
64  }
65  if (key_a->parent != key_b->parent) {
66  return true;
67  }
68  if (memcmp(key_a->id, key_b->id, sizeof(key_a->id)) != 0) {
69  return true;
70  }
71  return false;
72 }
73 
75 {
76  GPU_vertbuf_discard(step_data->hair_pos);
77  DRW_texture_free(step_data->hair_pos_tx);
78  MEM_freeN(step_data);
79 }
80 
82 {
83  if (mb->object == NULL) {
84  mb->object = BLI_ghash_new(eevee_object_key_hash, eevee_object_key_cmp, "EEVEE Object Motion");
85  }
86  for (int i = 0; i < 2; i++) {
87  if (mb->position_vbo_cache[i] == NULL) {
89  BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "EEVEE duplicate vbo cache");
90  }
91  if (mb->hair_motion_step_cache[i] == NULL) {
93  BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "EEVEE hair motion step cache");
94  }
95  }
96 }
97 
99 {
100  if (mb->object) {
102  mb->object = NULL;
103  }
104  for (int i = 0; i < 2; i++) {
105  if (mb->position_vbo_cache[i]) {
107  mb->position_vbo_cache[i] = NULL;
108  }
109  if (mb->hair_motion_step_cache[i]) {
112  mb->hair_motion_step_cache[i] = NULL;
113  }
114  }
115 }
116 
118  Object *ob,
119  bool is_psys)
120 {
121  if (mb->object == NULL) {
122  return NULL;
123  }
124 
125  EEVEE_ObjectKey key, *key_p;
126  /* Assumes that all instances have the same object pointer. This is currently the case because
127  * instance objects are temporary objects on the stack. */
128  /* WORKAROUND: Duplicate object key for particle system (hairs) to be able to store dupli offset
129  * matrix along with the emitter obmat. (see T97380) */
130  key.ob = (void *)((char *)ob + is_psys);
132  if (dup) {
134  memcpy(key.id, dup->persistent_id, sizeof(key.id));
135  }
136  else {
137  key.parent = ob;
138  memset(key.id, 0, sizeof(key.id));
139  }
140 
141  EEVEE_ObjectMotionData *ob_step = BLI_ghash_lookup(mb->object, &key);
142  if (ob_step == NULL) {
143  key_p = MEM_mallocN(sizeof(*key_p), __func__);
144  memcpy(key_p, &key, sizeof(*key_p));
145 
146  ob_step = MEM_callocN(sizeof(EEVEE_ObjectMotionData), __func__);
147 
148  BLI_ghash_insert(mb->object, key_p, ob_step);
149  }
150  return ob_step;
151 }
152 
154 {
155  if (mb_data->geometry_data == NULL) {
156  EEVEE_GeometryMotionData *geom_step = MEM_callocN(sizeof(EEVEE_GeometryMotionData), __func__);
157  geom_step->type = EEVEE_MOTION_DATA_MESH;
158  mb_data->geometry_data = geom_step;
159  }
160  return mb_data->geometry_data;
161 }
162 
164 {
165  if (mb_data->hair_data == NULL) {
166  /* Ugly, we allocate for each modifiers and just fill based on modifier index in the list. */
167  int psys_len = BLI_listbase_count(&ob->modifiers);
169  sizeof(EEVEE_HairMotionData) + sizeof(hair_step->psys[0]) * psys_len, __func__);
170  hair_step->psys_len = psys_len;
172  mb_data->hair_data = hair_step;
173  }
174  return mb_data->hair_data;
175 }
176 
178 {
179  if (mb_data->hair_data == NULL) {
181  sizeof(EEVEE_HairMotionData) + sizeof(hair_step->psys[0]), __func__);
182  hair_step->psys_len = 1;
184  mb_data->hair_data = hair_step;
185  }
186  return mb_data->hair_data;
187 }
188 
189 /* View Layer data. */
190 
191 void EEVEE_view_layer_data_free(void *storage)
192 {
193  EEVEE_ViewLayerData *sldata = (EEVEE_ViewLayerData *)storage;
194 
195  /* Lights */
196  MEM_SAFE_FREE(sldata->lights);
197  DRW_UBO_FREE_SAFE(sldata->light_ubo);
198  DRW_UBO_FREE_SAFE(sldata->shadow_ubo);
199  GPU_FRAMEBUFFER_FREE_SAFE(sldata->shadow_fb);
202  for (int i = 0; i < 2; i++) {
203  MEM_SAFE_FREE(sldata->shcasters_buffers[i].bbox);
204  MEM_SAFE_FREE(sldata->shcasters_buffers[i].update);
205  }
206 
207  if (sldata->fallback_lightcache) {
209  sldata->fallback_lightcache = NULL;
210  }
211 
212  /* Probes */
213  MEM_SAFE_FREE(sldata->probes);
214  DRW_UBO_FREE_SAFE(sldata->probe_ubo);
215  DRW_UBO_FREE_SAFE(sldata->grid_ubo);
216  DRW_UBO_FREE_SAFE(sldata->planar_ubo);
217  DRW_UBO_FREE_SAFE(sldata->common_ubo);
218 
226  for (int aov_index = 0; aov_index < MAX_AOVS; aov_index++) {
227  DRW_UBO_FREE_SAFE(sldata->renderpass_ubo.aovs[aov_index]);
228  }
229 
230  if (sldata->material_cache) {
232  sldata->material_cache = NULL;
233  }
234 }
235 
237 {
239 }
240 
242 {
243  sldata->common_ubo = GPU_uniformbuf_create(sizeof(sldata->common_data));
244 }
245 
247 {
250 
251  if (*sldata == NULL) {
252  *sldata = MEM_callocN(sizeof(**sldata), "EEVEE_ViewLayerData");
253  eevee_view_layer_init(*sldata);
254  }
255 
256  return *sldata;
257 }
258 
260 {
263 
264  if (*sldata == NULL) {
265  *sldata = MEM_callocN(sizeof(**sldata), "EEVEE_ViewLayerData");
266  eevee_view_layer_init(*sldata);
267  }
268 
269  return *sldata;
270 }
271 
272 /* Object data. */
273 
275 {
277  eevee_data->shadow_caster_id = -1;
278  eevee_data->need_update = false;
279  eevee_data->geom_update = false;
280 }
281 
283 {
284  if (ELEM(ob->type, OB_LIGHTPROBE, OB_LAMP)) {
285  return NULL;
286  }
288 }
289 
291 {
295  sizeof(EEVEE_ObjectEngineData),
297  NULL);
298 }
299 
300 /* Light probe data. */
301 
303 {
305  ped->need_update = false;
306 }
307 
309 {
310  if (ob->type != OB_LIGHTPROBE) {
311  return NULL;
312  }
314 }
315 
317 {
318  BLI_assert(ob->type == OB_LIGHTPROBE);
323  NULL);
324 }
325 
326 /* Light data. */
327 
329 {
331  led->need_update = true;
332 }
333 
335 {
336  if (ob->type != OB_LAMP) {
337  return NULL;
338  }
340 }
341 
343 {
344  BLI_assert(ob->type == OB_LAMP);
347  sizeof(EEVEE_LightEngineData),
349  NULL);
350 }
351 
352 /* World data. */
353 
355 {
357  wed->dd.recalc |= 1;
358 }
359 
361 {
363 }
364 
366 {
369  sizeof(EEVEE_WorldEngineData),
371  NULL);
372 }
General operations, lookup, etc. for blender objects.
#define BLI_assert(a)
Definition: BLI_assert.h:46
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:689
void(* GHashValFreeFP)(void *val)
Definition: BLI_ghash.h:38
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:734
bool BLI_ghashutil_ptrcmp(const void *a, const void *b)
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:710
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:863
#define BLI_ghashutil_inthash(key)
Definition: BLI_ghash.h:579
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:66
unsigned int uint
Definition: BLI_sys_types.h:67
#define ELEM(...)
#define MAX_DUPLI_RECUR
@ OB_LAMP
@ OB_LIGHTPROBE
#define DRW_UBO_FREE_SAFE(ubo)
Definition: DRW_render.h:191
#define DRW_TEXTURE_FREE_SAFE(tex)
Definition: DRW_render.h:183
#define GPU_uniformbuf_create(size)
void GPU_vertbuf_discard(GPUVertBuf *)
#define MEM_SAFE_FREE(v)
struct Object * DRW_object_get_dupli_parent(const Object *UNUSED(ob))
Definition: draw_manager.c:264
DrawData * DRW_drawdata_ensure(ID *id, DrawEngineType *engine_type, size_t size, DrawDataInitCb init_cb, DrawDataFreeCb free_cb)
Definition: draw_manager.c:866
void * DRW_view_layer_engine_data_get(DrawEngineType *engine_type)
Definition: draw_manager.c:757
void ** DRW_view_layer_engine_data_ensure(DrawEngineType *engine_type, void(*callback)(void *storage))
Definition: draw_manager.c:787
void ** DRW_view_layer_engine_data_ensure_ex(ViewLayer *view_layer, DrawEngineType *engine_type, void(*callback)(void *storage))
Definition: draw_manager.c:767
struct DupliObject * DRW_object_get_dupli(const Object *UNUSED(ob))
Definition: draw_manager.c:269
DrawData * DRW_drawdata_get(ID *id, DrawEngineType *engine_type)
Definition: draw_manager.c:850
void DRW_texture_free(GPUTexture *tex)
EEVEE_LightProbeEngineData * EEVEE_lightprobe_data_get(Object *ob)
Definition: eevee_data.c:308
static void eevee_lightprobe_data_init(DrawData *dd)
Definition: eevee_data.c:302
EEVEE_LightEngineData * EEVEE_light_data_get(Object *ob)
Definition: eevee_data.c:334
static bool eevee_object_key_cmp(const void *a, const void *b)
Definition: eevee_data.c:57
EEVEE_ViewLayerData * EEVEE_view_layer_data_get(void)
Definition: eevee_data.c:236
static void eevee_world_data_init(DrawData *dd)
Definition: eevee_data.c:354
EEVEE_ViewLayerData * EEVEE_view_layer_data_ensure(void)
Definition: eevee_data.c:259
EEVEE_ObjectEngineData * EEVEE_object_data_ensure(Object *ob)
Definition: eevee_data.c:290
static void eevee_view_layer_init(EEVEE_ViewLayerData *sldata)
Definition: eevee_data.c:241
EEVEE_WorldEngineData * EEVEE_world_data_ensure(World *wo)
Definition: eevee_data.c:365
void EEVEE_motion_blur_data_init(EEVEE_MotionBlurData *mb)
Definition: eevee_data.c:81
static uint eevee_object_key_hash(const void *key)
Definition: eevee_data.c:40
EEVEE_GeometryMotionData * EEVEE_motion_blur_geometry_data_get(EEVEE_ObjectMotionData *mb_data)
Definition: eevee_data.c:153
EEVEE_ObjectEngineData * EEVEE_object_data_get(Object *ob)
Definition: eevee_data.c:282
static void eevee_light_data_init(DrawData *dd)
Definition: eevee_data.c:328
void EEVEE_motion_hair_step_free(EEVEE_HairMotionStepData *step_data)
Definition: eevee_data.c:74
static void eevee_motion_blur_mesh_data_free(void *val)
Definition: eevee_data.c:28
EEVEE_LightEngineData * EEVEE_light_data_ensure(Object *ob)
Definition: eevee_data.c:342
EEVEE_HairMotionData * EEVEE_motion_blur_curves_data_get(EEVEE_ObjectMotionData *mb_data)
Definition: eevee_data.c:177
EEVEE_HairMotionData * EEVEE_motion_blur_hair_data_get(EEVEE_ObjectMotionData *mb_data, Object *ob)
Definition: eevee_data.c:163
EEVEE_ViewLayerData * EEVEE_view_layer_data_ensure_ex(struct ViewLayer *view_layer)
Definition: eevee_data.c:246
void EEVEE_view_layer_data_free(void *storage)
Definition: eevee_data.c:191
void EEVEE_motion_blur_data_free(EEVEE_MotionBlurData *mb)
Definition: eevee_data.c:98
static void eevee_object_data_init(DrawData *dd)
Definition: eevee_data.c:274
EEVEE_ObjectMotionData * EEVEE_motion_blur_object_data_get(EEVEE_MotionBlurData *mb, Object *ob, bool is_psys)
Definition: eevee_data.c:117
EEVEE_LightProbeEngineData * EEVEE_lightprobe_data_ensure(Object *ob)
Definition: eevee_data.c:316
EEVEE_WorldEngineData * EEVEE_world_data_get(World *wo)
Definition: eevee_data.c:360
DrawEngineType draw_engine_eevee_type
Definition: eevee_engine.c:618
void EEVEE_lightcache_free(LightCache *lcache)
#define MAX_AOVS
Definition: eevee_private.h:41
@ EEVEE_MOTION_DATA_HAIR
@ EEVEE_MOTION_DATA_MESH
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
static unsigned a[3]
Definition: RandGen.cpp:78
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
#define hash
Definition: noise.c:153
static void hair_step(ParticleSimulationData *sim, float cfra, const bool use_render_params)
int recalc
Definition: DNA_ID.h:40
int persistent_id[8]
Definition: BKE_duplilist.h:45
eEEVEEMotionData type
struct GPUTexture * hair_pos_tx
struct GPUVertBuf * hair_pos
struct GHash * hair_motion_step_cache[2]
struct GHash * position_vbo_cache[2]
struct GHash * object
struct Object * parent
EEVEE_GeometryMotionData * geometry_data
EEVEE_HairMotionData * hair_data
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 EEVEE_ViewLayerData::@210 renderpass_ubo
struct GPUUniformBuf * spec_color
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