Blender V5.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
8
9#include <cstring>
10
11#include "MEM_safe_multiply.h"
12
15#include "DNA_object_types.h"
16
17#include "BLI_math_base_c.hh"
18#include "BLI_utildefines.hh"
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
29namespace blender {
30
31static void lightprobe_init_data(ID *id)
32{
33 LightProbe *probe = id_cast<LightProbe *>(id);
35}
36
38{
39 LightProbe *probe = id_cast<LightProbe *>(id);
40
42}
43
44static void lightprobe_blend_write(BlendWriter *writer, ID *id, const void *id_address)
45{
46 LightProbe *prb = id_cast<LightProbe *>(id);
47
48 /* write LibData */
49 writer->write_id_struct(id_address, prb);
50 BKE_id_blend_write(writer, &prb->id);
51}
52
54 .id_code = LightProbe::id_type,
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 .foreach_working_space_color = nullptr,
73 .foreach_asset_weak_reference = nullptr,
74 .owner_pointer_get = nullptr,
75
76 .blend_write = lightprobe_blend_write,
77 .blend_read_data = nullptr,
78 .blend_read_after_liblink = nullptr,
79
80 .blend_read_undo_preserve = nullptr,
81
82 .lib_override_apply_post = nullptr,
83};
84
85void BKE_lightprobe_type_set(LightProbe *probe, const eLightProbeType lightprobe_type)
86{
87 probe->type = lightprobe_type;
88
89 switch (probe->type) {
91 probe->distinf = 0.3f;
92 probe->falloff = 1.0f;
93 probe->clipsta = 0.01f;
94 break;
96 probe->distinf = 0.1f;
97 probe->falloff = 0.5f;
98 probe->clipsta = 0.001f;
99 break;
101 probe->attenuation_type = LIGHTPROBE_SHAPE_ELLIPSOID;
102 break;
103 default:
104 BLI_assert_msg(0, "LightProbe type not configured.");
105 break;
106 }
107}
108
109LightProbe *BKE_lightprobe_add(Main *bmain, const char *name)
110{
111 LightProbe *probe;
112
113 probe = BKE_id_new<LightProbe>(bmain, name);
114
115 return probe;
116}
117
119 const LightProbeGridCacheFrame *cache)
120{
121 writer->write_struct_array(cache->block_len, cache->block_infos);
122
124
125 writer->write_float3_array(sample_count, reinterpret_cast<float *>(cache->irradiance.L0));
126 writer->write_float3_array(sample_count, reinterpret_cast<float *>(cache->irradiance.L1_a));
127 writer->write_float3_array(sample_count, reinterpret_cast<float *>(cache->irradiance.L1_b));
128 writer->write_float3_array(sample_count, reinterpret_cast<float *>(cache->irradiance.L1_c));
129
130 writer->write_float_array(sample_count, cache->visibility.L0);
131 writer->write_float_array(sample_count, cache->visibility.L1_a);
132 writer->write_float_array(sample_count, cache->visibility.L1_b);
133 writer->write_float_array(sample_count, cache->visibility.L1_c);
134
135 writer->write_int8_array(sample_count, reinterpret_cast<int8_t *>(cache->connectivity.validity));
136}
137
140{
141 if (!ELEM(
143 {
144 /* Do not try to read data from incompatible layout. Clear all pointers. */
145 *cache = LightProbeGridCacheFrame{};
146 return;
147 }
148
150
152
153 /* Baking data is not stored. */
154 cache->baking.L0 = nullptr;
155 cache->baking.L1_a = nullptr;
156 cache->baking.L1_b = nullptr;
157 cache->baking.L1_c = nullptr;
158 cache->baking.virtual_offset = nullptr;
159 cache->baking.validity = nullptr;
160 cache->surfels = nullptr;
161 cache->surfels_len = 0;
162
163 bool ok = true;
164 ok &= BLO_read_array(reader, reinterpret_cast<float **>(&cache->irradiance.L0), sample_count, 3);
165 ok &= BLO_read_array(
166 reader, reinterpret_cast<float **>(&cache->irradiance.L1_a), sample_count, 3);
167 ok &= BLO_read_array(
168 reader, reinterpret_cast<float **>(&cache->irradiance.L1_b), sample_count, 3);
169 ok &= BLO_read_array(
170 reader, reinterpret_cast<float **>(&cache->irradiance.L1_c), sample_count, 3);
171
172 ok &= BLO_read_array(reader, &cache->visibility.L0, sample_count);
173 ok &= BLO_read_array(reader, &cache->visibility.L1_a, sample_count);
174 ok &= BLO_read_array(reader, &cache->visibility.L1_b, sample_count);
175 ok &= BLO_read_array(reader, &cache->visibility.L1_c, sample_count);
176
177 ok &= BLO_read_array(reader, &cache->connectivity.validity, sample_count);
178
179 if (!ok) {
180 cache->block_len = 0;
181 }
182}
183
185{
186 if (cache->grid_static_cache != nullptr) {
187 writer->write_struct(cache->grid_static_cache);
189 }
190}
191
199
200template<typename T> static void spherical_harmonic_free(T &data)
201{
202 MEM_SAFE_DELETE(data.L0);
203 MEM_SAFE_DELETE(data.L1_a);
204 MEM_SAFE_DELETE(data.L1_b);
205 MEM_SAFE_DELETE(data.L1_c);
206}
207
208template<typename DataT, typename T> static void spherical_harmonic_copy(T &dst, T &src)
209{
210 dst.L0 = MEM_dupalloc(src.L0);
211 dst.L1_a = MEM_dupalloc(src.L1_a);
212 dst.L1_b = MEM_dupalloc(src.L1_b);
213 dst.L1_c = MEM_dupalloc(src.L1_c);
214}
215
217{
218 LightProbeGridCacheFrame *cache = MEM_new<LightProbeGridCacheFrame>("LightProbeGridCacheFrame");
219 return cache;
220}
221
223{
224 LightProbeGridCacheFrame *dst = MEM_dupalloc(src);
225 dst->block_infos = MEM_dupalloc(src->block_infos);
228 dst->connectivity.validity = MEM_dupalloc(src->connectivity.validity);
229 /* NOTE: Don't copy baking since it wouldn't be freed nor updated after completion. */
230 dst->baking.L0 = nullptr;
231 dst->baking.L1_a = nullptr;
232 dst->baking.L1_b = nullptr;
233 dst->baking.L1_c = nullptr;
234 dst->baking.virtual_offset = nullptr;
235 dst->baking.validity = nullptr;
236 dst->surfels = nullptr;
237 return dst;
238}
239
241{
242 MEM_SAFE_DELETE(cache->block_infos);
246 MEM_SAFE_DELETE(cache->baking.validity);
247 MEM_SAFE_DELETE(cache->connectivity.validity);
248 MEM_SAFE_DELETE_VOID(cache->surfels);
249 MEM_SAFE_DELETE(cache->baking.virtual_offset);
250
251 MEM_SAFE_DELETE(cache);
252}
253
255{
256 BLI_assert(object->lightprobe_cache == nullptr);
257
258 object->lightprobe_cache = MEM_new<LightProbeObjectCache>("LightProbeObjectCache");
259}
260
262{
263 BLI_assert(src_cache != nullptr);
264
265 LightProbeObjectCache *dst_cache = MEM_dupalloc(src_cache);
266
267 if (src_cache->grid_static_cache) {
269 src_cache->grid_static_cache);
270 }
271 return dst_cache;
272}
273
275{
276 if (object->lightprobe_cache == nullptr) {
277 return;
278 }
279
280 LightProbeObjectCache *cache = object->lightprobe_cache;
281
282 if (cache->shared == false) {
283 if (cache->grid_static_cache != nullptr) {
285 }
286 }
287
288 MEM_SAFE_DELETE(object->lightprobe_cache);
289}
290
292{
293 /* Overflow is checked to detect invalid file reads. */
296 const int block_size = cache->block_size;
297 if (!MEM_size_safe_multiply(block_size, block_size, &result) ||
298 !MEM_size_safe_multiply(result, block_size, &result) ||
300 {
301 return INT64_MAX;
302 }
303 return result;
304 }
305 /* LIGHTPROBE_CACHE_UNIFORM_GRID */
306 if (!MEM_size_safe_multiply(cache->size[0], cache->size[1], &result) ||
308 {
309 return INT64_MAX;
310 }
311 return result;
312}
313
314} // namespace blender
#define BKE_LIB_FOREACHID_PROCESS_IDSUPER(data_, id_super_, cb_flag_)
General operations for probes.
#define INT64_MAX
#define BLO_read_struct(reader, struct_name, ptr_p)
#define BLT_I18NCONTEXT_ID_LIGHTPROBE
#define FILTER_ID_IM
Definition DNA_ID.h:1206
#define FILTER_ID_LP
Definition DNA_ID.h:1229
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
bool MEM_size_safe_multiply(size_t a, size_t b, size_t *result)
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define T
BMesh Iterators.
static void lightprobe_grid_cache_frame_blend_read(BlendDataReader *reader, LightProbeGridCacheFrame *cache)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
struct LightProbeGridCacheFrame * BKE_lightprobe_grid_cache_frame_create()
static void lightprobe_foreach_id(ID *id, LibraryForeachIDData *data)
Definition lightprobe.cc:37
static void spherical_harmonic_free(T &data)
static void lightprobe_init_data(ID *id)
Definition lightprobe.cc:31
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
static void spherical_harmonic_copy(T &dst, T &src)
void BKE_lightprobe_cache_blend_read(struct BlendDataReader *reader, struct LightProbeObjectCache *cache)
static void lightprobe_grid_cache_frame_blend_write(BlendWriter *writer, const LightProbeGridCacheFrame *cache)
void BKE_lightprobe_type_set(struct LightProbe *probe, eLightProbeType lightprobe_type)
Definition lightprobe.cc:85
LightProbeGridCacheFrame * BKE_lightprobe_grid_cache_frame_copy(LightProbeGridCacheFrame *src)
BMesh const char void * data
static void lightprobe_blend_write(BlendWriter *writer, ID *id, const void *id_address)
Definition lightprobe.cc:44
void BKE_lightprobe_cache_blend_write(struct BlendWriter *writer, struct LightProbeObjectCache *cache)
struct LightProbe * BKE_lightprobe_add(struct Main *bmain, const char *name)
void BKE_lightprobe_cache_free(struct Object *object)
void BKE_lightprobe_grid_cache_frame_free(struct LightProbeGridCacheFrame *cache)
bool BLO_read_array(BlendDataReader *reader, T **ptr_p, const SizeT array_size, const int elems=1)
void BLO_read_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size, const int elems=1)
@ LIGHTPROBE_CACHE_ADAPTIVE_RESOLUTION
@ INDEX_ID_LP
Definition DNA_ID.h:1337
int64_t BKE_lightprobe_grid_cache_frame_sample_count(const struct LightProbeGridCacheFrame *cache)
IDTypeInfo IDType_ID_LP
Definition lightprobe.cc:53
LightProbeObjectCache * BKE_lightprobe_cache_copy(LightProbeObjectCache *src_cache)
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
void BKE_lightprobe_cache_create(struct Object *object)
signed char int8_t
Definition BKE_text.h:76
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:50
void write_int8_array(int64_t num, const int8_t *data)
void write_float_array(int64_t num, const float *data)
void write_float3_array(int64_t num, const float *data)
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct_array(const int64_t array_size, const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
eLightProbeGridCacheLayout data_layout
LightProbeConnectivityData connectivity
struct LightProbeGridCacheFrame * grid_static_cache
#define N_(msgid)