Blender V4.3
lightprobe.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include <cstring>
10
12#include "DNA_defaults.h"
14#include "DNA_object_types.h"
15
16#include "BLI_math_base.h"
17#include "BLI_span.hh"
18#include "BLI_utildefines.h"
19
20#include "BKE_idtype.hh"
21#include "BKE_lib_id.hh"
22#include "BKE_lib_query.hh"
23#include "BKE_lightprobe.h"
24
25#include "BLT_translation.hh"
26
27#include "BLO_read_write.hh"
28
29static void lightprobe_init_data(ID *id)
30{
31 LightProbe *probe = (LightProbe *)id;
33
35}
36
43
44static void lightprobe_blend_write(BlendWriter *writer, ID *id, const void *id_address)
45{
46 LightProbe *prb = (LightProbe *)id;
47
48 /* write LibData */
49 BLO_write_id_struct(writer, LightProbe, id_address, &prb->id);
50 BKE_id_blend_write(writer, &prb->id);
51}
52
54 /*id_code*/ ID_LP,
55 /*id_filter*/ FILTER_ID_LP,
56 /*dependencies_id_types*/ FILTER_ID_IM,
57 /*main_listbase_index*/ INDEX_ID_LP,
58 /*struct_size*/ sizeof(LightProbe),
59 /*name*/ "LightProbe",
60 /*name_plural*/ N_("lightprobes"),
61 /*translation_context*/ BLT_I18NCONTEXT_ID_LIGHTPROBE,
63 /*asset_type_info*/ nullptr,
64
65 /*init_data*/ lightprobe_init_data,
66 /*copy_data*/ nullptr,
67 /*free_data*/ nullptr,
68 /*make_local*/ nullptr,
69 /*foreach_id*/ lightprobe_foreach_id,
70 /*foreach_cache*/ nullptr,
71 /*foreach_path*/ nullptr,
72 /*owner_pointer_get*/ nullptr,
73
74 /*blend_write*/ lightprobe_blend_write,
75 /*blend_read_data*/ nullptr,
76 /*blend_read_after_liblink*/ nullptr,
77
78 /*blend_read_undo_preserve*/ nullptr,
79
80 /*lib_override_apply_post*/ nullptr,
81};
82
83void BKE_lightprobe_type_set(LightProbe *probe, const short lightprobe_type)
84{
85 probe->type = lightprobe_type;
86
87 switch (probe->type) {
89 probe->distinf = 0.3f;
90 probe->falloff = 1.0f;
91 probe->clipsta = 0.01f;
92 break;
94 probe->distinf = 0.1f;
95 probe->falloff = 0.5f;
96 probe->clipsta = 0.001f;
97 break;
100 break;
101 default:
102 BLI_assert_msg(0, "LightProbe type not configured.");
103 break;
104 }
105}
106
107void *BKE_lightprobe_add(Main *bmain, const char *name)
108{
109 LightProbe *probe;
110
111 probe = static_cast<LightProbe *>(BKE_id_new(bmain, ID_LP, name));
112
113 return probe;
114}
115
117 const LightProbeGridCacheFrame *cache)
118{
120
122
123 BLO_write_float3_array(writer, sample_count, (float *)cache->irradiance.L0);
124 BLO_write_float3_array(writer, sample_count, (float *)cache->irradiance.L1_a);
125 BLO_write_float3_array(writer, sample_count, (float *)cache->irradiance.L1_b);
126 BLO_write_float3_array(writer, sample_count, (float *)cache->irradiance.L1_c);
127
128 BLO_write_float_array(writer, sample_count, cache->visibility.L0);
129 BLO_write_float_array(writer, sample_count, cache->visibility.L1_a);
130 BLO_write_float_array(writer, sample_count, cache->visibility.L1_b);
131 BLO_write_float_array(writer, sample_count, cache->visibility.L1_c);
132
133 BLO_write_int8_array(writer, sample_count, (int8_t *)cache->connectivity.validity);
134}
135
138{
139 if (!ELEM(
141 {
142 /* Do not try to read data from incompatible layout. Clear all pointers. */
143 memset(cache, 0, sizeof(*cache));
144 return;
145 }
146
148
150
151 /* Baking data is not stored. */
152 cache->baking.L0 = nullptr;
153 cache->baking.L1_a = nullptr;
154 cache->baking.L1_b = nullptr;
155 cache->baking.L1_c = nullptr;
156 cache->baking.virtual_offset = nullptr;
157 cache->baking.validity = nullptr;
158 cache->surfels = nullptr;
159 cache->surfels_len = 0;
160
161 BLO_read_float3_array(reader, sample_count, (float **)&cache->irradiance.L0);
162 BLO_read_float3_array(reader, sample_count, (float **)&cache->irradiance.L1_a);
163 BLO_read_float3_array(reader, sample_count, (float **)&cache->irradiance.L1_b);
164 BLO_read_float3_array(reader, sample_count, (float **)&cache->irradiance.L1_c);
165
166 BLO_read_float_array(reader, sample_count, &cache->visibility.L0);
167 BLO_read_float_array(reader, sample_count, &cache->visibility.L1_a);
168 BLO_read_float_array(reader, sample_count, &cache->visibility.L1_b);
169 BLO_read_float_array(reader, sample_count, &cache->visibility.L1_c);
170
171 BLO_read_int8_array(reader, sample_count, (int8_t **)&cache->connectivity.validity);
172}
173
181
189
190template<typename T> static void spherical_harmonic_free(T &data)
191{
192 MEM_SAFE_FREE(data.L0);
193 MEM_SAFE_FREE(data.L1_a);
194 MEM_SAFE_FREE(data.L1_b);
195 MEM_SAFE_FREE(data.L1_c);
196}
197
198template<typename DataT, typename T> static void spherical_harmonic_copy(T &dst, T &src)
199{
200 dst.L0 = (DataT *)MEM_dupallocN(src.L0);
201 dst.L1_a = (DataT *)MEM_dupallocN(src.L1_a);
202 dst.L1_b = (DataT *)MEM_dupallocN(src.L1_b);
203 dst.L1_c = (DataT *)MEM_dupallocN(src.L1_c);
204}
205
207{
209 MEM_callocN(sizeof(LightProbeGridCacheFrame), "LightProbeGridCacheFrame"));
210 return cache;
211}
212
214{
216 dst->block_infos = static_cast<LightProbeBlockData *>(MEM_dupallocN(src->block_infos));
219 dst->connectivity.validity = static_cast<uint8_t *>(MEM_dupallocN(src->connectivity.validity));
220 /* NOTE: Don't copy baking since it wouldn't be freed nor updated after completion. */
221 dst->baking.L0 = nullptr;
222 dst->baking.L1_a = nullptr;
223 dst->baking.L1_b = nullptr;
224 dst->baking.L1_c = nullptr;
225 dst->baking.virtual_offset = nullptr;
226 dst->baking.validity = nullptr;
227 dst->surfels = nullptr;
228 return dst;
229}
230
244
246{
247 BLI_assert(object->lightprobe_cache == nullptr);
248
249 object->lightprobe_cache = static_cast<LightProbeObjectCache *>(
250 MEM_callocN(sizeof(LightProbeObjectCache), "LightProbeObjectCache"));
251}
252
254{
255 BLI_assert(src_cache != nullptr);
256
257 LightProbeObjectCache *dst_cache = static_cast<LightProbeObjectCache *>(
258 MEM_dupallocN(src_cache));
259
260 if (src_cache->grid_static_cache) {
262 src_cache->grid_static_cache);
263 }
264 return dst_cache;
265}
266
268{
269 if (object->lightprobe_cache == nullptr) {
270 return;
271 }
272
273 LightProbeObjectCache *cache = object->lightprobe_cache;
274
275 if (cache->shared == false) {
276 if (cache->grid_static_cache != nullptr) {
278 }
279 }
280
281 MEM_SAFE_FREE(object->lightprobe_cache);
282}
283
285{
287 return cache->block_len * cube_i(cache->block_size);
288 }
289 /* LIGHTPROBE_CACHE_UNIFORM_GRID */
290 return int64_t(cache->size[0]) * cache->size[1] * cache->size[2];
291}
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:39
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1482
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2560
#define BKE_LIB_FOREACHID_PROCESS_IDSUPER(data_, id_super_, cb_flag_)
@ IDWALK_CB_NOP
General operations for probes.
#define BLI_assert(a)
Definition BLI_assert.h:50
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:57
MINLINE int cube_i(int a)
#define ELEM(...)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
void BLO_read_float3_array(BlendDataReader *reader, int array_size, float **ptr_p)
Definition readfile.cc:4977
void BLO_write_float3_array(BlendWriter *writer, uint num, const float *data_ptr)
void BLO_read_float_array(BlendDataReader *reader, int array_size, float **ptr_p)
Definition readfile.cc:4967
#define BLO_write_id_struct(writer, struct_name, id_address, id)
#define BLO_write_struct(writer, struct_name, data_ptr)
void BLO_write_float_array(BlendWriter *writer, uint num, const float *data_ptr)
#define BLO_write_struct_array(writer, struct_name, array_size, data_ptr)
void BLO_write_int8_array(BlendWriter *writer, uint num, const int8_t *data_ptr)
#define BLO_read_struct_array(reader, struct_name, array_size, ptr_p)
#define BLO_read_struct(reader, struct_name, ptr_p)
void BLO_read_int8_array(BlendDataReader *reader, int array_size, int8_t **ptr_p)
Definition readfile.cc:4941
#define BLT_I18NCONTEXT_ID_LIGHTPROBE
@ INDEX_ID_LP
Definition DNA_ID.h:1303
#define FILTER_ID_IM
Definition DNA_ID.h:1171
#define FILTER_ID_LP
Definition DNA_ID.h:1194
@ ID_LP
Object groups, one object can be in many groups at once.
#define DNA_struct_default_get(struct_name)
@ LIGHTPROBE_CACHE_UNIFORM_GRID
@ LIGHTPROBE_CACHE_ADAPTIVE_RESOLUTION
struct LightProbe LightProbe
@ LIGHTPROBE_SHAPE_ELIPSOID
@ LIGHTPROBE_TYPE_PLANE
@ LIGHTPROBE_TYPE_VOLUME
@ LIGHTPROBE_TYPE_SPHERE
Object is a sort of wrapper for general info.
#define MEM_SAFE_FREE(v)
void BKE_lightprobe_cache_create(Object *object)
static void spherical_harmonic_free(T &data)
void BKE_lightprobe_type_set(LightProbe *probe, const short lightprobe_type)
Definition lightprobe.cc:83
static void lightprobe_init_data(ID *id)
Definition lightprobe.cc:29
static void lightprobe_blend_write(BlendWriter *writer, ID *id, const void *id_address)
Definition lightprobe.cc:44
static void spherical_harmonic_copy(T &dst, T &src)
IDTypeInfo IDType_ID_LP
Definition lightprobe.cc:53
static void lightprobe_grid_cache_frame_blend_write(BlendWriter *writer, const LightProbeGridCacheFrame *cache)
LightProbeGridCacheFrame * BKE_lightprobe_grid_cache_frame_create()
int64_t BKE_lightprobe_grid_cache_frame_sample_count(const LightProbeGridCacheFrame *cache)
static void lightprobe_grid_cache_frame_blend_read(BlendDataReader *reader, LightProbeGridCacheFrame *cache)
static void lightprobe_foreach_id(ID *id, LibraryForeachIDData *data)
Definition lightprobe.cc:37
void BKE_lightprobe_cache_free(Object *object)
LightProbeGridCacheFrame * BKE_lightprobe_grid_cache_frame_copy(LightProbeGridCacheFrame *src)
void BKE_lightprobe_grid_cache_frame_free(LightProbeGridCacheFrame *cache)
LightProbeObjectCache * BKE_lightprobe_cache_copy(LightProbeObjectCache *src_cache)
void BKE_lightprobe_cache_blend_write(BlendWriter *writer, LightProbeObjectCache *cache)
void BKE_lightprobe_cache_blend_read(BlendDataReader *reader, LightProbeObjectCache *cache)
void * BKE_lightprobe_add(Main *bmain, const char *name)
void *(* MEM_callocN)(size_t len, const char *str)
Definition mallocn.cc:42
void *(* MEM_dupallocN)(const void *vmemh)
Definition mallocn.cc:39
__int64 int64_t
Definition stdint.h:89
unsigned char uint8_t
Definition stdint.h:78
signed char int8_t
Definition stdint.h:75
Definition DNA_ID.h:413
LightProbeVisibilityData visibility
LightProbeConnectivityData connectivity
LightProbeBlockData * block_infos
LightProbeIrradianceData irradiance
struct LightProbeGridCacheFrame * grid_static_cache
struct Collection * visibility_grp
#define N_(msgid)