Blender V5.3
eevee_lightprobe_volume_bake.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10#pragma once
11
12#include "draw_intersect.bsl.hh"
20
22
23struct SceneBound {
24 [[storage(0, read_write)]] CaptureInfoData &capture_info_buf;
25 [[storage(1, read)]] const ObjectBounds (&bounds_buf)[];
26
28};
29
34 [[resource_table]] SceneBound &srt, [[global_invocation_id]] const uint3 global_id)
35{
36 uint index = global_id.x;
37 if (index >= uint(srt.resource_len)) {
38 return;
39 }
40
41 ObjectBounds bounds = srt.bounds_buf[index];
42 if (!drw_bounds_are_valid(bounds)) {
43 return;
44 }
45
46 IsectBox box = isect_box_setup(bounds.bounding_corners[0].xyz,
47 bounds.bounding_corners[1].xyz,
48 bounds.bounding_corners[2].xyz,
49 bounds.bounding_corners[3].xyz);
50
51 float3 local_min = float3(FLT_MAX);
52 float3 local_max = float3(-FLT_MAX);
53 for (int i = 0; i < 8; i++) {
54 local_min = min(local_min, box.corners[i].xyz);
55 local_max = max(local_max, box.corners[i].xyz);
56 }
57
58 atomicMin(srt.capture_info_buf.scene_bound_x_min, floatBitsToOrderedInt(local_min.x));
59 atomicMax(srt.capture_info_buf.scene_bound_x_max, floatBitsToOrderedInt(local_max.x));
60
61 atomicMin(srt.capture_info_buf.scene_bound_y_min, floatBitsToOrderedInt(local_min.y));
62 atomicMax(srt.capture_info_buf.scene_bound_y_max, floatBitsToOrderedInt(local_max.y));
63
64 atomicMin(srt.capture_info_buf.scene_bound_z_min, floatBitsToOrderedInt(local_min.z));
65 atomicMax(srt.capture_info_buf.scene_bound_z_max, floatBitsToOrderedInt(local_max.z));
66}
67
70
71 [[storage(0, read)]] const int (&list_start_buf)[];
72 [[storage(6, read)]] const SurfelListInfoData &list_info_buf;
73
76
78 {
79 int surfel_first = imageLoad(cluster_list_img, grid_coord).r;
80 float search_radius_sqr = square(surfels.capture_info_buf.max_virtual_offset +
81 surfels.capture_info_buf.min_distance_to_surface);
82
83 int closest_surfel = -1;
84 float closest_distance_sqr = 1e10f;
85 for (int surfel_id = surfel_first; surfel_id > -1;
86 surfel_id = surfels.surfel_buf[surfel_id].next)
87 {
88 Surfel surfel = surfels.surfel_buf[surfel_id];
89
90 float3 probe_to_surfel = surfel.position - P;
91 float surfel_dist_sqr = length_squared(probe_to_surfel);
92 /* Do not consider surfels that are outside of search radius. */
93 if (surfel_dist_sqr > search_radius_sqr) {
94 continue;
95 }
96
97 if (closest_distance_sqr > surfel_dist_sqr) {
98 closest_distance_sqr = surfel_dist_sqr;
99 closest_surfel = surfel_id;
100 }
101 }
102 return closest_surfel;
103 }
104
105 float front_facing_offset(float surfel_distance)
106 {
107
108 if (abs(surfel_distance) > surfels.capture_info_buf.min_distance_to_surface) {
109 return 0.0f;
110 }
111 /* NOTE: distance can be negative. */
112 return surfel_distance - ((surfel_distance > 0.0f) ?
113 surfels.capture_info_buf.min_distance_to_surface :
114 -surfels.capture_info_buf.min_distance_to_surface);
115 }
116
117 float back_facing_offset(float surfel_distance)
118 {
119
120 if (surfel_distance > surfels.capture_info_buf.max_virtual_offset) {
121 return 0.0f;
122 }
123 /* NOTE: distance can be negative. */
124 return surfel_distance + ((surfel_distance > 0.0f) ?
125 surfels.capture_info_buf.min_distance_to_surface :
126 -surfels.capture_info_buf.min_distance_to_surface);
127 }
128
129 float compute_offset_length(int3 grid_coord, float3 P, float3 offset_direction)
130 {
131
132 int surfel_first = imageLoad(cluster_list_img, grid_coord).r;
133 float search_radius = max(surfels.capture_info_buf.max_virtual_offset,
134 surfels.capture_info_buf.min_distance_to_surface);
135 /* Scale it a bit to avoid missing surfaces. */
136 float ray_radius = surfels.capture_info_buf.surfel_radius * M_SQRT2;
137
138 /* Nearest and farthest surfels in offset direction on both sides. */
139 int surfel_pos = -1;
140 int surfel_neg = -1;
141 float surfel_distance_pos = +1e10f;
142 float surfel_distance_neg = -1e10f;
143 for (int surfel_id = surfel_first; surfel_id > -1;
144 surfel_id = surfels.surfel_buf[surfel_id].next)
145 {
146 Surfel surfel = surfels.surfel_buf[surfel_id];
147
148 float3 probe_to_surfel = surfel.position - P;
149 float surf_dist_signed = dot(offset_direction, probe_to_surfel);
150 /* Do not consider surfels that are outside of search radius. */
151 if (abs(surf_dist_signed) > search_radius) {
152 continue;
153 }
154 /* Emulate ray cast with any hit shader by discarding surfels outside of the ray radius. */
155 float ray_dist = distance(surf_dist_signed * offset_direction, probe_to_surfel);
156 if (ray_dist > ray_radius) {
157 continue;
158 }
159
160 if (surf_dist_signed > 0.0f) {
161 if (surfel_distance_pos > surf_dist_signed) {
162 surfel_distance_pos = surf_dist_signed;
163 surfel_pos = surfel_id;
164 }
165 }
166 else {
167 if (surfel_distance_neg < surf_dist_signed) {
168 surfel_distance_neg = surf_dist_signed;
169 surfel_neg = surfel_id;
170 }
171 }
172 }
173
174 bool has_neighbor_pos = surfel_pos != -1;
175 bool has_neighbor_neg = surfel_neg != -1;
176
177 if (has_neighbor_pos && has_neighbor_neg) {
178 /* If both sides have neighbors. */
179 bool is_front_facing_pos = dot(offset_direction, surfels.surfel_buf[surfel_pos].normal) <
180 0.0f;
181 bool is_front_facing_neg = dot(-offset_direction, surfels.surfel_buf[surfel_neg].normal) <
182 0.0f;
183 if (is_front_facing_pos && is_front_facing_neg) {
184 /* If both sides have same facing. */
185 if (is_front_facing_pos) {
186 /* If both sides are front facing. */
187 float distance_between_neighbors = surfel_distance_pos - surfel_distance_neg;
188 if (distance_between_neighbors < surfels.capture_info_buf.min_distance_to_surface * 2.0f)
189 {
190 /* Choose the middle point. */
191 return (surfel_distance_pos + surfel_distance_neg) / 2.0f;
192 }
193 /* Choose the maximum offset. */
194 /* NOTE: The maximum offset is always from positive side since it's the closest. */
195 return front_facing_offset(surfel_distance_pos);
196 }
197 /* If both sides are back facing. */
198 /* Choose the minimum offset. */
199 /* NOTE: The minimum offset is always from positive side since it's the closest. */
200 return back_facing_offset(surfel_distance_pos);
201 }
202 /* If both sides have different facing. */
203 float front_distance = is_front_facing_pos ? surfel_distance_pos : surfel_distance_neg;
204 float back_distance = !is_front_facing_pos ? surfel_distance_pos : surfel_distance_neg;
205 float front_offset = front_facing_offset(front_distance);
206 float back_offset = back_facing_offset(back_distance);
207 /* Choose the minimum offset. */
208 return (abs(front_offset) < abs(back_offset)) ? front_offset : back_offset;
209 }
210
211 if (has_neighbor_pos || has_neighbor_neg) {
212 /* Only one side have neighbor. */
213 int nearest_surfel_id = has_neighbor_pos ? surfel_pos : surfel_neg;
214 float surfel_distance = has_neighbor_pos ? surfel_distance_pos : surfel_distance_neg;
215 bool is_front_facing = dot(has_neighbor_pos ? offset_direction : -offset_direction,
216 surfels.surfel_buf[nearest_surfel_id].normal) < 0.0f;
217 if (is_front_facing) {
218 return front_facing_offset(surfel_distance);
219 }
220 return back_facing_offset(surfel_distance);
221 }
222 /* If no sides has neighbor (should never happen here since we already bailed out). */
223 return 0.0f;
224 }
225};
226
238 [[global_invocation_id]] const uint3 global_id)
239{
240 [[resource_table]] SurfelData &surfels = srt.surfels;
241
242 int3 grid_coord = int3(global_id);
243
244 if (any(greaterThanEqual(grid_coord, surfels.capture_info_buf.irradiance_grid_size))) {
245 return;
246 }
247
249 surfels.capture_info_buf.irradiance_grid_local_to_world,
250 surfels.capture_info_buf.irradiance_grid_size,
251 grid_coord);
252
253 int closest_surfel_id = srt.find_closest_surfel(grid_coord, P);
254 if (closest_surfel_id == -1) {
255 imageStoreFast(srt.virtual_offset_img, grid_coord, float4(0.0f));
256 return;
257 }
258
259 /* Offset direction towards the sampling point. */
260 // float3 offset_direction = safe_normalize(surfel_buf[closest_surfel_id].position - P);
261 /* NOTE: Use normal direction of the surfel instead for stability reasons. */
262 float3 offset_direction = surfels.surfel_buf[closest_surfel_id].normal;
263 bool is_front_facing = dot(surfels.surfel_buf[closest_surfel_id].position - P,
264 surfels.surfel_buf[closest_surfel_id].normal) < 0.0f;
265 if (is_front_facing) {
266 offset_direction = -offset_direction;
267 }
268
269 float offset_length = srt.compute_offset_length(grid_coord, P, offset_direction);
270
271 float3 virtual_offset = offset_direction * offset_length;
272
273 imageStoreFast(srt.virtual_offset_img, grid_coord, float4(virtual_offset, 0.0f));
274}
275
279
280 [[storage(0, read)]] const int (&list_start_buf)[];
281 [[storage(6, read)]] const SurfelListInfoData &list_info_buf;
282
289 [[image(5, read_write, SFLOAT_32)]] image3D validity_img;
290
292 float3 irradiance,
293 float visibility,
295 {
297 surfels.capture_info_buf.irradiance_grid_world_to_local_rotation, L);
298
299 /* Spherical harmonics need to be weighted by sphere area. */
300 irradiance *= 4.0f * M_PI;
301 visibility *= 4.0f * M_PI;
302
303 sh.encode_signal_sample(lL, float4(irradiance, visibility));
304 }
305
307 {
308 float3 L = safe_normalize(surfel.position - P);
309 bool facing = dot(-L, surfel.normal) > 0.0f;
310 SurfelRadiance surfel_radiance_indirect = surfel.radiance_indirect[radiance_src];
311
312 float4 irradiance_vis = float4(0.0f);
313 irradiance_vis += facing ? surfel.radiance_direct.front : surfel.radiance_direct.back;
314
315 /* Clamped brightness. */
316 float luma = max(1e-8f, reduce_max(irradiance_vis.rgb));
317 irradiance_vis.rgb *= 1.0f - max(0.0f, luma - surfels.capture_info_buf.clamp_direct) / luma;
318
319 /* NOTE: The indirect radiance is already normalized and this is wanted, because we are not
320 * integrating the same signal and we would have the SH lagging behind the surfel integration
321 * otherwise. */
322 irradiance_vis += facing ? surfel_radiance_indirect.front : surfel_radiance_indirect.back;
323
324 irradiance_capture(L, irradiance_vis.rgb, irradiance_vis.a, sh);
325 }
326
327 void validity_capture_surfel(Surfel surfel, float3 P, float &validity)
328 {
329 float3 L = safe_normalize(surfel.position - P);
330 bool facing = surfel.double_sided || dot(-L, surfel.normal) > 0.0f;
331 validity += float(facing);
332 }
333
334 void validity_capture_world(float3 /*L*/, float &validity)
335 {
336 validity += 1.0f;
337 }
338
340 {
341 float3 radiance = float3(0.0f);
342 float visibility = 0.0f;
343
344 if (surfels.capture_info_buf.capture_world_direct) {
345 SphereProbeUvArea atlas_coord = surfels.capture_info_buf.world_atlas_coord;
346 radiance = sphere_probes.sample_probe(L, 0.0f, atlas_coord).rgb;
347
348 /* Clamped brightness. */
349 float luma = max(1e-8f, reduce_max(radiance));
350 radiance *= 1.0f - max(0.0f, luma - surfels.capture_info_buf.clamp_direct) / luma;
351 }
352
353 if (surfels.capture_info_buf.capture_visibility_direct) {
354 visibility = 1.0f;
355 }
356
357 irradiance_capture(L, radiance, visibility, sh);
358 }
359};
360
372 [[resource_table]] const draw::View &views,
373 [[global_invocation_id]] const uint3 global_id)
374{
375 [[resource_table]] SurfelData &surfels = srt.surfels;
376
377 int3 grid_coord = int3(global_id);
378
379 if (any(greaterThanEqual(grid_coord, surfels.capture_info_buf.irradiance_grid_size))) {
380 return;
381 }
382
383 const ViewMatrices view = views.get(0);
384
386 surfels.capture_info_buf.irradiance_grid_local_to_world,
387 surfels.capture_info_buf.irradiance_grid_size,
388 grid_coord);
389
390 /* Add virtual offset to avoid baking inside of geometry as much as possible. */
391 P += imageLoadFast(srt.virtual_offset_img, grid_coord).xyz;
392
393 /* Project to get ray linked list. */
394 float irradiance_sample_ray_distance;
395 int list_index = eevee::surfel::list_index_get(
396 view, srt.list_info_buf.ray_grid_size, P, irradiance_sample_ray_distance);
397
398 /* Walk the ray to get which surfels the irradiance sample is between. */
399 int surfel_prev = -1;
400 int surfel_next = srt.list_start_buf[list_index];
401 /* Avoid spinning for eternity. */
402 for (int i = 0; i < 9999; i++) {
403 if (surfel_next <= -1) {
404 break;
405 }
406 /* Reminder: List is sorted with highest value first. */
407 if (surfels.surfel_buf[surfel_next].ray_distance < irradiance_sample_ray_distance) {
408 break;
409 }
410 surfel_prev = surfel_next;
411 surfel_next = surfels.surfel_buf[surfel_next].next;
412 assert(surfel_prev != surfel_next);
413 }
414
415 float3 sky_L = view.world_incident_vector(P);
416
418 sh.L0.M0 = imageLoadFast(srt.irradiance_L0_img, grid_coord);
419 sh.L1.Mn1 = imageLoadFast(srt.irradiance_L1_a_img, grid_coord);
420 sh.L1.M0 = imageLoadFast(srt.irradiance_L1_b_img, grid_coord);
421 sh.L1.Mp1 = imageLoadFast(srt.irradiance_L1_c_img, grid_coord);
422 float validity = imageLoadFast(srt.validity_img, grid_coord).r;
423
424 /* Un-normalize for accumulation. */
425 float weight_captured = surfels.capture_info_buf.sample_index * 2.0f;
426 sh.L0.M0 *= weight_captured;
427 sh.L1.Mn1 *= weight_captured;
428 sh.L1.M0 *= weight_captured;
429 sh.L1.Mp1 *= weight_captured;
430 validity *= weight_captured;
431
432 if (surfel_next > -1) {
433 Surfel surfel = surfels.surfel_buf[surfel_next];
434 srt.irradiance_capture_surfel(surfel, P, sh);
435 srt.validity_capture_surfel(surfel, P, validity);
436#if 0 /* For debugging the volume rays list. */
437 drw_debug_line(surfel.position, P, float4(0, 1, 0, 1), drw_debug_persistent_lifetime);
438#endif
439 }
440 else {
441 srt.irradiance_capture_world(-sky_L, sh);
442 srt.validity_capture_world(-sky_L, validity);
443#if 0 /* For debugging the volume rays list. */
444 drw_debug_line(P - sky_L, P, float4(0, 1, 1, 1), drw_debug_persistent_lifetime);
445#endif
446 }
447
448 if (surfel_prev > -1) {
449 Surfel surfel = surfels.surfel_buf[surfel_prev];
450 srt.irradiance_capture_surfel(surfel, P, sh);
451 srt.validity_capture_surfel(surfel, P, validity);
452#if 0 /* For debugging the volume rays list. */
453 drw_debug_line(surfel.position, P, float4(1, 0, 1, 1), drw_debug_persistent_lifetime);
454#endif
455 }
456 else {
457 srt.irradiance_capture_world(sky_L, sh);
458 srt.validity_capture_world(sky_L, validity);
459#if 0 /* For debugging the volume rays list. */
460 drw_debug_line(P + sky_L, P, float4(1, 1, 0, 1), drw_debug_persistent_lifetime);
461#endif
462 }
463
464 /* Normalize for storage. We accumulated 2 samples. */
465 weight_captured += 2.0f;
466 sh.L0.M0 /= weight_captured;
467 sh.L1.Mn1 /= weight_captured;
468 sh.L1.M0 /= weight_captured;
469 sh.L1.Mp1 /= weight_captured;
470 validity /= weight_captured;
471
472 imageStoreFast(srt.irradiance_L0_img, grid_coord, sh.L0.M0);
473 imageStoreFast(srt.irradiance_L1_a_img, grid_coord, sh.L1.Mn1);
474 imageStoreFast(srt.irradiance_L1_b_img, grid_coord, sh.L1.M0);
475 imageStoreFast(srt.irradiance_L1_c_img, grid_coord, sh.L1.Mp1);
476 imageStoreFast(srt.validity_img, grid_coord, float4(validity));
477}
478
479} // namespace eevee::lightprobe::volume
480
#define M_SQRT2
#define M_PI
btScalar max
void drw_debug_line(uint &vertid, float3 v1, float3 v2, uint v_color, uint lifetime)
IsectBox isect_box_setup(Box shape)
bool drw_bounds_are_valid(ObjectBounds bounds)
#define IRRADIANCE_BOUNDS_GROUP_SIZE
#define IRRADIANCE_GRID_BRICK_SIZE
#define IRRADIANCE_GRID_GROUP_SIZE
PipelineCompute eevee_lightprobe_volume_ray(eevee::lightprobe::volume::ray_capture)
PipelineCompute eevee_lightprobe_volume_offset(eevee::lightprobe::volume::volume_offset)
PipelineCompute eevee_lightprobe_volume_bounds(eevee::lightprobe::volume::scene_bounds)
@ SFLOAT_16_16_16_16
@ SFLOAT_32_32_32_32
#define assert(assertion)
#define FLT_MAX
#define storage(slot, qualifiers)
#define image(slot, qualifiers, format)
#define index(i)
#define push_constant
#define resource_table
#define global_invocation_id
#define compute
#define local_size(...)
int atomicMin(int &, int) RET
VecBase< bool, D > greaterThanEqual(VecOp< T, D >, VecOp< T, D >) RET
bool any(VecOp< bool, D >) RET
int atomicMax(int &, int) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
float distance(float, float) RET
ImageBase< int, 3, false, true > iimage3DAtomic
IMG_TEMPLATE DataVec imageLoad(const T &, IntCoord) RET
ImageBase< float, 3 > image3D
#define imageLoadFast
#define imageStoreFast
float square(float v)
float3 transform_direction(float3x3 mat, float3 direction)
float length_squared(VecT a)
float reduce_max(float2 a)
VecT safe_normalize(VecT vector)
int floatBitsToOrderedInt(float value)
#define L
void ray_capture(RayCapture &srt, const draw::View &views, const uint3 global_id)
void volume_offset(VolumeOffset &srt, const uint3 global_id)
void scene_bounds(SceneBound &srt, const uint3 global_id)
float3 grid_sample_position(float4x4 grid_local_to_world_mat, int3 grid_res, int3 cell_coord)
int list_index_get(const ViewMatrices view, int2 ray_grid_size, float3 P, float &r_ray_distance)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
float3 corners[8]
float4 bounding_corners[4]
spherical_harmonics::BandL1< T > L1
spherical_harmonics::BandL0< T > L0
void encode_signal_sample(float3 direction, T amplitude)
const CaptureInfoData & capture_info_buf
Surfel(& surfel_buf)[]
float3 world_incident_vector(float3 P) const
float4 sample_probe(float3 L, float lod, SphereProbeUvArea uv_area) const
void irradiance_capture_surfel(Surfel surfel, float3 P, SphericalHarmonicL1< float4 > &sh)
void irradiance_capture(float3 L, float3 irradiance, float visibility, SphericalHarmonicL1< float4 > &sh)
void irradiance_capture_world(float3 L, SphericalHarmonicL1< float4 > &sh)
void validity_capture_surfel(Surfel surfel, float3 P, float &validity)
float compute_offset_length(int3 grid_coord, float3 P, float3 offset_direction)
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136