Blender V4.5
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
12#include "DNA_defaults.h"
14#include "DNA_object_types.h"
15
16#include "BLI_math_base.h"
17#include "BLI_utildefines.h"
18
19#include "BKE_idtype.hh"
20#include "BKE_lib_id.hh"
21#include "BKE_lib_query.hh"
22#include "BKE_lightprobe.h"
23
24#include "BLT_translation.hh"
25
26#include "BLO_read_write.hh"
27
28static void lightprobe_init_data(ID *id)
29{
30 LightProbe *probe = (LightProbe *)id;
32
34}
35
42
43static void lightprobe_blend_write(BlendWriter *writer, ID *id, const void *id_address)
44{
45 LightProbe *prb = (LightProbe *)id;
46
47 /* write LibData */
48 BLO_write_id_struct(writer, LightProbe, id_address, &prb->id);
49 BKE_id_blend_write(writer, &prb->id);
50}
51
53 /*id_code*/ LightProbe::id_type,
54 /*id_filter*/ FILTER_ID_LP,
55 /*dependencies_id_types*/ FILTER_ID_IM,
56 /*main_listbase_index*/ INDEX_ID_LP,
57 /*struct_size*/ sizeof(LightProbe),
58 /*name*/ "LightProbe",
59 /*name_plural*/ N_("lightprobes"),
60 /*translation_context*/ BLT_I18NCONTEXT_ID_LIGHTPROBE,
62 /*asset_type_info*/ nullptr,
63
64 /*init_data*/ lightprobe_init_data,
65 /*copy_data*/ nullptr,
66 /*free_data*/ nullptr,
67 /*make_local*/ nullptr,
68 /*foreach_id*/ lightprobe_foreach_id,
69 /*foreach_cache*/ nullptr,
70 /*foreach_path*/ nullptr,
71 /*owner_pointer_get*/ nullptr,
72
73 /*blend_write*/ lightprobe_blend_write,
74 /*blend_read_data*/ nullptr,
75 /*blend_read_after_liblink*/ nullptr,
76
77 /*blend_read_undo_preserve*/ nullptr,
78
79 /*lib_override_apply_post*/ nullptr,
80};
81
82void BKE_lightprobe_type_set(LightProbe *probe, const short lightprobe_type)
83{
84 probe->type = lightprobe_type;
85
86 switch (probe->type) {
88 probe->distinf = 0.3f;
89 probe->falloff = 1.0f;
90 probe->clipsta = 0.01f;
91 break;
93 probe->distinf = 0.1f;
94 probe->falloff = 0.5f;
95 probe->clipsta = 0.001f;
96 break;
99 break;
100 default:
101 BLI_assert_msg(0, "LightProbe type not configured.");
102 break;
103 }
104}
105
106LightProbe *BKE_lightprobe_add(Main *bmain, const char *name)
107{
108 LightProbe *probe;
109
110 probe = BKE_id_new<LightProbe>(bmain, name);
111
112 return probe;
113}
114
116 const LightProbeGridCacheFrame *cache)
117{
119
121
122 BLO_write_float3_array(writer, sample_count, (float *)cache->irradiance.L0);
123 BLO_write_float3_array(writer, sample_count, (float *)cache->irradiance.L1_a);
124 BLO_write_float3_array(writer, sample_count, (float *)cache->irradiance.L1_b);
125 BLO_write_float3_array(writer, sample_count, (float *)cache->irradiance.L1_c);
126
127 BLO_write_float_array(writer, sample_count, cache->visibility.L0);
128 BLO_write_float_array(writer, sample_count, cache->visibility.L1_a);
129 BLO_write_float_array(writer, sample_count, cache->visibility.L1_b);
130 BLO_write_float_array(writer, sample_count, cache->visibility.L1_c);
131
132 BLO_write_int8_array(writer, sample_count, (int8_t *)cache->connectivity.validity);
133}
134
137{
138 if (!ELEM(
140 {
141 /* Do not try to read data from incompatible layout. Clear all pointers. */
142 *cache = LightProbeGridCacheFrame{};
143 return;
144 }
145
147
149
150 /* Baking data is not stored. */
151 cache->baking.L0 = nullptr;
152 cache->baking.L1_a = nullptr;
153 cache->baking.L1_b = nullptr;
154 cache->baking.L1_c = nullptr;
155 cache->baking.virtual_offset = nullptr;
156 cache->baking.validity = nullptr;
157 cache->surfels = nullptr;
158 cache->surfels_len = 0;
159
160 BLO_read_float3_array(reader, sample_count, (float **)&cache->irradiance.L0);
161 BLO_read_float3_array(reader, sample_count, (float **)&cache->irradiance.L1_a);
162 BLO_read_float3_array(reader, sample_count, (float **)&cache->irradiance.L1_b);
163 BLO_read_float3_array(reader, sample_count, (float **)&cache->irradiance.L1_c);
164
165 BLO_read_float_array(reader, sample_count, &cache->visibility.L0);
166 BLO_read_float_array(reader, sample_count, &cache->visibility.L1_a);
167 BLO_read_float_array(reader, sample_count, &cache->visibility.L1_b);
168 BLO_read_float_array(reader, sample_count, &cache->visibility.L1_c);
169
170 BLO_read_int8_array(reader, sample_count, (int8_t **)&cache->connectivity.validity);
171}
172
180
188
189template<typename T> static void spherical_harmonic_free(T &data)
190{
192 MEM_SAFE_FREE(data.L1_a);
193 MEM_SAFE_FREE(data.L1_b);
194 MEM_SAFE_FREE(data.L1_c);
195}
196
197template<typename DataT, typename T> static void spherical_harmonic_copy(T &dst, T &src)
198{
199 dst.L0 = (DataT *)MEM_dupallocN(src.L0);
200 dst.L1_a = (DataT *)MEM_dupallocN(src.L1_a);
201 dst.L1_b = (DataT *)MEM_dupallocN(src.L1_b);
202 dst.L1_c = (DataT *)MEM_dupallocN(src.L1_c);
203}
204
211
213{
215 dst->block_infos = static_cast<LightProbeBlockData *>(MEM_dupallocN(src->block_infos));
218 dst->connectivity.validity = static_cast<uint8_t *>(MEM_dupallocN(src->connectivity.validity));
219 /* NOTE: Don't copy baking since it wouldn't be freed nor updated after completion. */
220 dst->baking.L0 = nullptr;
221 dst->baking.L1_a = nullptr;
222 dst->baking.L1_b = nullptr;
223 dst->baking.L1_c = nullptr;
224 dst->baking.virtual_offset = nullptr;
225 dst->baking.validity = nullptr;
226 dst->surfels = nullptr;
227 return dst;
228}
229
243
245{
246 BLI_assert(object->lightprobe_cache == nullptr);
247
248 object->lightprobe_cache = MEM_callocN<LightProbeObjectCache>("LightProbeObjectCache");
249}
250
252{
253 BLI_assert(src_cache != nullptr);
254
255 LightProbeObjectCache *dst_cache = static_cast<LightProbeObjectCache *>(
256 MEM_dupallocN(src_cache));
257
258 if (src_cache->grid_static_cache) {
260 src_cache->grid_static_cache);
261 }
262 return dst_cache;
263}
264
266{
267 if (object->lightprobe_cache == nullptr) {
268 return;
269 }
270
271 LightProbeObjectCache *cache = object->lightprobe_cache;
272
273 if (cache->shared == false) {
274 if (cache->grid_static_cache != nullptr) {
276 }
277 }
278
280}
281
283{
285 return cache->block_len * cube_i(cache->block_size);
286 }
287 /* LIGHTPROBE_CACHE_UNIFORM_GRID */
288 return int64_t(cache->size[0]) * cache->size[1] * cache->size[2];
289}
IDTypeInfo IDType_ID_LP
Definition lightprobe.cc:52
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:44
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1495
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2611
#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:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
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_write_float_array(BlendWriter *writer, int64_t num, const float *data_ptr)
void BLO_read_float3_array(BlendDataReader *reader, int64_t array_size, float **ptr_p)
Definition readfile.cc:5336
void BLO_read_float_array(BlendDataReader *reader, int64_t array_size, float **ptr_p)
Definition readfile.cc:5326
#define BLO_write_id_struct(writer, struct_name, id_address, id)
#define BLO_write_struct(writer, struct_name, data_ptr)
void BLO_write_float3_array(BlendWriter *writer, int64_t num, const float *data_ptr)
void BLO_write_int8_array(BlendWriter *writer, int64_t num, const int8_t *data_ptr)
#define BLO_write_struct_array(writer, struct_name, array_size, data_ptr)
void BLO_read_int8_array(BlendDataReader *reader, int64_t array_size, int8_t **ptr_p)
Definition readfile.cc:5290
#define BLO_read_struct_array(reader, struct_name, array_size, ptr_p)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define BLT_I18NCONTEXT_ID_LIGHTPROBE
@ INDEX_ID_LP
Definition DNA_ID.h:1244
Object groups, one object can be in many groups at once.
#define DNA_struct_default_get(struct_name)
@ LIGHTPROBE_TYPE_PLANE
@ LIGHTPROBE_TYPE_VOLUME
@ LIGHTPROBE_TYPE_SPHERE
@ LIGHTPROBE_SHAPE_ELIPSOID
@ LIGHTPROBE_CACHE_UNIFORM_GRID
@ LIGHTPROBE_CACHE_ADAPTIVE_RESOLUTION
Object is a sort of wrapper for general info.
BMesh const char void * data
long long int int64_t
#define MEM_SAFE_FREE(v)
#define FILTER_ID_IM
#define FILTER_ID_LP
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:82
static void lightprobe_init_data(ID *id)
Definition lightprobe.cc:28
static void lightprobe_blend_write(BlendWriter *writer, ID *id, const void *id_address)
Definition lightprobe.cc:43
static void spherical_harmonic_copy(T &dst, T &src)
static void lightprobe_grid_cache_frame_blend_write(BlendWriter *writer, const LightProbeGridCacheFrame *cache)
LightProbeGridCacheFrame * BKE_lightprobe_grid_cache_frame_create()
LightProbe * BKE_lightprobe_add(Main *bmain, const char *name)
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:36
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 * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
#define T
Definition DNA_ID.h:404
LightProbeVisibilityData visibility
LightProbeConnectivityData connectivity
LightProbeBlockData * block_infos
LightProbeIrradianceData irradiance
struct LightProbeGridCacheFrame * grid_static_cache
struct Collection * visibility_grp
struct LightProbeObjectCache * lightprobe_cache
#define N_(msgid)