Blender V5.3
eevee_lightprobe.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
14
15#include "eevee_instance.hh"
16#include "eevee_lightprobe.hh"
17
18#include <iostream>
19
20namespace blender::eevee {
21
22/* -------------------------------------------------------------------- */
25
27{
28 /* Initialize the world probe. */
29 world_sphere_.clipping_distances = float2(1.0f, 10.0f);
30 world_sphere_.world_to_probe_transposed = float3x4::identity();
31 world_sphere_.influence_shape = SHAPE_ELLIPSOID;
32 world_sphere_.parallax_shape = SHAPE_ELLIPSOID;
33 /* Full influence. */
34 world_sphere_.influence_scale = 0.0f;
35 world_sphere_.influence_bias = 1.0f;
36 world_sphere_.parallax_distance = 1e10f;
37 /* In any case, the world must always be up to valid and used for render. */
38 world_sphere_.use_for_render = true;
39}
40
42{
43 switch (resolution) {
44 case 128:
46 case 256:
48 case 512:
50 case 1024:
52 case 2048:
54 case 4096:
56 }
59}
60
62{
63 const SceneEEVEE &sce_eevee = inst_.scene->eevee;
64 sphere_object_resolution_ = resolution_to_probe_resolution_enum(sce_eevee.gi_cubemap_resolution);
65}
66
68{
69 auto_bake_enabled_ = inst_.is_viewport() &&
70 (inst_.scene->eevee.flag & SCE_EEVEE_GI_AUTOBAKE) != 0;
71}
72
73void LightProbeModule::sync_volume(const ObjectRef &ob_ref)
74{
75 VolumeProbe &grid = volume_map_.lookup_or_add_default(ObjectKey(ob_ref));
76 grid.used = true;
77 if (inst_.get_recalc_flags(ob_ref) != 0 || grid.initialized == false) {
78 const blender::LightProbe &lightprobe =
80
81 grid.initialized = true;
82 grid.updated = true;
83 grid.surfel_density = lightprobe.grid_surfel_density;
84 grid.object_to_world = ob_ref.object_to_world();
85 grid.cache = ob_ref.object->lightprobe_cache;
86
89
90 grid.normal_bias = lightprobe.grid_normal_bias;
91 grid.view_bias = lightprobe.grid_view_bias;
92 grid.facing_bias = lightprobe.grid_facing_bias;
93
94 grid.validity_threshold = lightprobe.grid_validity_threshold;
95 grid.dilation_threshold = lightprobe.grid_dilation_threshold;
96 grid.dilation_radius = lightprobe.grid_dilation_radius;
97 grid.intensity = lightprobe.intensity;
98
99 const bool has_valid_cache = grid.cache && grid.cache->grid_static_cache;
100 grid.viewport_display = has_valid_cache && (lightprobe.flag & LIGHTPROBE_FLAG_SHOW_DATA);
101 if (grid.viewport_display) {
102 int3 cache_size = grid.cache->grid_static_cache->size;
104 1.0f / float3(cache_size + 1));
105 grid.viewport_display_size = math::reduce_min(scale) * lightprobe.data_display_size;
106 }
107
108 /* Force reupload. */
109 inst_.volume_probes.bricks_free(grid.bricks);
110 }
111}
112
113void LightProbeModule::sync_sphere(const ObjectRef &ob_ref)
114{
115 SphereProbe &cube = sphere_map_.lookup_or_add_default(ObjectKey(ob_ref));
116 cube.used = true;
117 if (inst_.get_recalc_flags(ob_ref) != 0 || cube.initialized == false) {
118 const blender::LightProbe &light_probe = DRW_object_get_data_for_drawing<blender::LightProbe>(
119 *ob_ref.object);
120
121 cube.initialized = true;
122 cube.updated = true;
123 cube.do_render = true;
124
125 SphereProbeModule &probe_module = inst_.sphere_probes;
126 eLightProbeResolution probe_resolution = sphere_object_resolution_;
127 int subdivision_lvl = probe_module.subdivision_level_get(probe_resolution);
128
129 if (cube.atlas_coord.subdivision_lvl != subdivision_lvl) {
130 cube.atlas_coord.free();
131 cube.atlas_coord = find_empty_atlas_region(subdivision_lvl);
132 SphereProbeData &cube_data = *static_cast<SphereProbeData *>(&cube);
133 /* Update gpu data sampling coordinates. */
134 cube_data.atlas_coord = cube.atlas_coord.as_sampling_coord();
135 /* Coordinates have changed. Area might contain random data. Do not use for rendering. */
136 cube.use_for_render = false;
137 }
138
139 bool use_custom_parallax = (light_probe.flag & LIGHTPROBE_FLAG_CUSTOM_PARALLAX) != 0;
140 float influence_distance = light_probe.distinf;
141 float influence_falloff = light_probe.falloff;
142 float parallax_distance = light_probe.distpar;
143 parallax_distance = use_custom_parallax ? max_ff(parallax_distance, influence_distance) :
144 influence_distance;
145
146 auto to_eevee_shape = [](int bl_shape_type) {
147 return (bl_shape_type == LIGHTPROBE_SHAPE_BOX) ? SHAPE_CUBOID : SHAPE_ELLIPSOID;
148 };
149 cube.influence_shape = to_eevee_shape(light_probe.attenuation_type);
150 cube.parallax_shape = to_eevee_shape(use_custom_parallax ? light_probe.parallax_type :
151 light_probe.attenuation_type);
152
153 float4x4 object_to_world = math::scale(ob_ref.object_to_world(), float3(influence_distance));
154 cube.location = object_to_world.location();
155 cube.volume = math::abs(math::determinant(object_to_world));
156 cube.world_to_probe_transposed = float3x4(math::transpose(math::invert(object_to_world)));
157 cube.influence_scale = 1.0 / max_ff(1e-8f, influence_falloff);
158 cube.influence_bias = cube.influence_scale;
159 cube.parallax_distance = parallax_distance / influence_distance;
160 cube.clipping_distances = float2(light_probe.clipsta, light_probe.clipend);
161
162 float3 scale = influence_distance * math::to_scale(ob_ref.object_to_world());
163 cube.viewport_display = light_probe.flag & LIGHTPROBE_FLAG_SHOW_DATA;
164 cube.viewport_display_size = light_probe.data_display_size * math::reduce_add(scale / 3.0f);
165 }
166}
167
168void LightProbeModule::sync_planar(const ObjectRef &ob_ref)
169{
170 PlanarProbe &plane = planar_map_.lookup_or_add_default(ObjectKey(ob_ref));
171 plane.used = true;
172 if (inst_.get_recalc_flags(ob_ref) != 0 || plane.initialized == false) {
173 const blender::LightProbe &light_probe = DRW_object_get_data_for_drawing<blender::LightProbe>(
174 *ob_ref.object);
175
176 plane.initialized = true;
177 plane.updated = true;
178 plane.plane_to_world = ob_ref.object_to_world();
179 plane.plane_to_world.z_axis() = math::normalize(plane.plane_to_world.z_axis()) *
180 light_probe.distinf;
181 plane.world_to_plane = math::invert(plane.plane_to_world);
182 plane.clipping_offset = light_probe.clipsta;
183 plane.viewport_display = (light_probe.flag & LIGHTPROBE_FLAG_SHOW_DATA) != 0;
184 plane.parallax_distance = light_probe.distpar;
185 }
186}
187
189{
190 const blender::LightProbe &lightprobe =
192 switch (lightprobe.type) {
194 sync_sphere(ob_ref);
195 return;
197 sync_planar(ob_ref);
198 return;
200 sync_volume(ob_ref);
201 return;
202 }
204}
205
206void LightProbeModule::sync_world(const blender::World *world, bool has_update)
207{
208 const eLightProbeResolution probe_resolution = static_cast<eLightProbeResolution>(
209 world->probe_resolution);
210
211 SphereProbeModule &sph_module = inst_.sphere_probes;
212 int subdivision_lvl = sph_module.subdivision_level_get(probe_resolution);
213
214 if (subdivision_lvl != world_sphere_.atlas_coord.subdivision_lvl) {
215 world_sphere_.atlas_coord.free();
216 world_sphere_.atlas_coord = find_empty_atlas_region(subdivision_lvl);
217 SphereProbeData &world_data = *static_cast<SphereProbeData *>(&world_sphere_);
218 world_data.atlas_coord = world_sphere_.atlas_coord.as_sampling_coord();
219 has_update = true;
220 }
221
222 if (has_update) {
223 world_sphere_.do_render = true;
224 }
225}
226
228{
229 /* Check for deleted or updated grid. */
230 volume_update_ = false;
231 volume_map_.remove_if([&](const Map<ObjectKey, VolumeProbe>::MutableItem &item) {
232 VolumeProbe &grid = item.value;
233 bool remove_grid = !grid.used;
234 if (grid.updated || remove_grid) {
235 volume_update_ = true;
236 }
237 grid.updated = false;
238 grid.used = false;
239 return remove_grid;
240 });
241
242 /* Check for deleted or updated cube. */
243 sphere_update_ = false;
244 sphere_map_.remove_if([&](const Map<ObjectKey, SphereProbe>::MutableItem &item) {
245 SphereProbe &cube = item.value;
246 bool remove_cube = !cube.used;
247 if (cube.updated || remove_cube) {
248 sphere_update_ = true;
249 }
250 cube.updated = false;
251 cube.used = false;
252 return remove_cube;
253 });
254
255 /* Check for deleted or updated plane. */
256 planar_update_ = false;
257 planar_map_.remove_if([&](const Map<ObjectKey, PlanarProbe>::MutableItem &item) {
258 PlanarProbe &plane = item.value;
259 bool remove_plane = !plane.used;
260 if (plane.updated || remove_plane) {
261 planar_update_ = true;
262 }
263 plane.updated = false;
264 plane.used = false;
265 return remove_plane;
266 });
267}
268
269SphereProbeAtlasCoord LightProbeModule::find_empty_atlas_region(int subdivision_level) const
270{
271 int layer_count = sphere_layer_count();
272 SphereProbeAtlasCoord::LocationFinder location_finder(layer_count, subdivision_level);
273
274 location_finder.mark_space_used(world_sphere_.atlas_coord);
275 for (const SphereProbe &probe : sphere_map_.values()) {
276 location_finder.mark_space_used(probe.atlas_coord);
277 }
278 return location_finder.first_free_spot();
279}
280
281int LightProbeModule::sphere_layer_count() const
282{
283 int max_layer = world_sphere_.atlas_coord.atlas_layer;
284 for (const SphereProbe &probe : sphere_map_.values()) {
285 max_layer = max_ii(max_layer, probe.atlas_coord.atlas_layer);
286 }
287 int layer_count = max_layer + 1;
288 return layer_count;
289}
290
292
293/* -------------------------------------------------------------------- */
296
298 int subdivision_level)
299{
300 subdivision_level_ = subdivision_level;
301 areas_per_dimension_ = 1 << subdivision_level_;
302 areas_per_layer_ = square_i(areas_per_dimension_);
303 /* Always add an additional layer to make sure that there is always a free area.
304 * If this area is chosen the atlas will grow. */
305 int area_len = (allocated_layer_count + 1) * areas_per_layer_;
306 areas_occupancy_.resize(area_len, false);
307}
308
310{
311 if (coord.atlas_layer == -1) {
312 /* Coordinate not allocated yet. */
313 return;
314 }
315 /* The input probe data can be stored in a different subdivision level and should tag all areas
316 * of the target subdivision level. Shift right if subdivision is higher, left if lower. */
317 const int shift_right = max_ii(coord.subdivision_lvl - subdivision_level_, 0);
318 const int shift_left = max_ii(subdivision_level_ - coord.subdivision_lvl, 0);
319 const int2 pos_in_location_finder = (coord.area_location() >> shift_right) << shift_left;
320 /* Tag all areas this probe overlaps. */
321 const int layer_offset = coord.atlas_layer * areas_per_layer_;
322 const int areas_overlapped_per_dim = 1 << shift_left;
323 for (const int y : IndexRange(areas_overlapped_per_dim)) {
324 for (const int x : IndexRange(areas_overlapped_per_dim)) {
325 const int2 pos = pos_in_location_finder + int2(x, y);
326 const int area_index = pos.x + pos.y * areas_per_dimension_;
327 areas_occupancy_[area_index + layer_offset].set();
328 }
329 }
330}
331
333{
335 result.subdivision_lvl = subdivision_level_;
336 for (int index : areas_occupancy_.index_range()) {
337 if (!areas_occupancy_[index]) {
338 result.atlas_layer = index / areas_per_layer_;
339 result.area_index = index % areas_per_layer_;
340 return result;
341 }
342 }
343 /* There should always be a free area. See constructor. */
345 return result;
346}
347
349{
350 std::ostream &os = std::cout;
351 int layer = 0, row = 0, column = 0;
352 os << "subdivision " << subdivision_level_ << "\n";
353 for (bool spot_taken : areas_occupancy_) {
354 if (row == 0 && column == 0) {
355 os << "layer " << layer << "\n";
356 }
357 os << (spot_taken ? 'X' : '-');
358 column++;
359 if (column == areas_per_dimension_) {
360 os << "\n";
361 column = 0;
362 row++;
363 }
364 if (row == areas_per_dimension_) {
365 row = 0;
366 layer++;
367 }
368 }
369}
370
372
373} // namespace blender::eevee
#define BLI_assert_unreachable()
constexpr IndexRange()=default
MutableMapItem< Key, Value > MutableItem
Definition BLI_map.hh:133
float4x4 object_to_world(int instance_index) const
A running instance of the engine.
VolumeProbeModule volume_probes
uint get_recalc_flags(const ObjectRef &ob_ref)
void sync_world(const blender::World *world, bool has_update)
void sync_probe(const ObjectRef &ob_ref)
void mark_space_used(const SphereProbeAtlasCoord &coord)
LocationFinder(int allocated_layer_count, int subdivision_level)
void bricks_free(Vector< IrradianceBrickPacked > &bricks)
#define layer
float3x3 scale(float3x3 mat, float2 scale)
static eLightProbeResolution resolution_to_probe_resolution_enum(int resolution)
MatBase< T, NumCol, NumRow > transpose(const MatBase< T, NumRow, NumCol > &mat)
MatBase< T, NumCol, NumRow > scale(const MatBase< T, NumCol, NumRow > &mat, const VectorT &scale)
T reduce_min(const VecBase< T, Size > &a)
CartesianBasis invert(const CartesianBasis &basis)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
VecBase< T, 3 > to_scale(const MatBase< T, NumCol, NumRow > &mat)
T cube(const T &a)
T abs(const T &a)
VecBase< T, 3 > transform_direction(const MatBase< T, 3, 3 > &mat, const VecBase< T, 3 > &direction)
T reduce_add(const VecBase< T, Size > &a)
T determinant(const MatBase< T, Size, Size > &mat)
MINLINE int square_i(int a)
MINLINE int max_ii(int a, int b)
MatBase< float, 4, 4 > float4x4
MatBase< float, 3, 4 > float3x4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
T & DRW_object_get_data_for_drawing(const Object &object)
@ LIGHT_PROBE_RESOLUTION_512
@ LIGHT_PROBE_RESOLUTION_4096
@ LIGHT_PROBE_RESOLUTION_2048
@ LIGHT_PROBE_RESOLUTION_256
@ LIGHT_PROBE_RESOLUTION_128
@ LIGHT_PROBE_RESOLUTION_1024
MatBase< float, 3, 3 > float3x3
MINLINE float max_ff(float a, float b)
VecBase< float, 3 > float3
@ LIGHTPROBE_FLAG_CUSTOM_PARALLAX
Point object_to_world(Point pt, float4x4 object_to_world)
struct LightProbeGridCacheFrame * grid_static_cache
struct LightProbeObjectCache * lightprobe_cache
SphereProbeAtlasCoord atlas_coord
Vector< IrradianceBrickPacked > bricks
const LightProbeObjectCache * cache