Blender V5.3
eevee_lightprobe_volume.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
5#pragma once
6
7#include "eevee_defines.hh"
13
15
23float3 grid_sample_position(float4x4 grid_local_to_world_mat, int3 grid_res, int3 cell_coord)
24{
25 float3 ls_cell_pos = (float3(cell_coord + 1)) / float3(grid_res + 1);
26 ls_cell_pos = ls_cell_pos * 2.0f - 1.0f;
27 float3 ws_cell_pos = (grid_local_to_world_mat * float4(ls_cell_pos, 1.0f)).xyz;
28 return ws_cell_pos;
29}
30
35bool grid_local_coord(VolumeProbeData grid_data, float3 P, float3 &r_lP)
36{
37 /* Position in cell units. */
38 /* NOTE: The vector-matrix multiplication swapped on purpose to cancel the matrix transpose. */
39 float3 lP = (float4(P, 1.0f) * grid_data.world_to_grid_transposed).xyz;
40 r_lP = clamp(lP, float3(0.5f), float3(grid_data.grid_size_padded) - 0.5f);
41 /* Sample is valid if position wasn't clamped. */
42 return all(equal(lP, r_lP));
43}
44
45int grid_brick_index_get(VolumeProbeData grid_data, int3 brick_coord)
46{
47 int3 grid_size_in_bricks = divide_ceil(grid_data.grid_size_padded,
49 int brick_index = grid_data.brick_offset;
50 brick_index += brick_coord.x;
51 brick_index += brick_coord.y * grid_size_in_bricks.x;
52 brick_index += brick_coord.z * grid_size_in_bricks.x * grid_size_in_bricks.y;
53 return brick_index;
54}
55
56/* Return cell corner from a corner ID [0..7]. */
57int3 grid_cell_corner(int cell_corner_id)
58{
59 return (int3(cell_corner_id) >> int3(0, 1, 2)) & 1;
60}
61
66{
67 int3 brick_coord = int3((lP - 0.5f) / float(IRRADIANCE_GRID_BRICK_SIZE - 1));
68 /* Avoid sampling adjacent bricks. */
69 return max(brick_coord, int3(0));
70}
71
76{
77 /* Avoid sampling adjacent bricks around the origin. */
78 lP = max(lP, float3(0.5f));
79 /* Local position inside the brick (still in grid sample spacing unit). */
80 float3 brick_lP = lP - float3(brick_coord) * float(IRRADIANCE_GRID_BRICK_SIZE - 1);
81 return brick_lP;
82}
83
88 VolumeProbeData grid_data,
89 uint2 brick_atlas_coord,
90 float3 brick_lP,
91 float3 lNg)
92{
93 /* A cell is the interpolation region between 8 texels. */
94 float3 cell_lP = brick_lP - 0.5f;
95 float3 cell_start = floor(cell_lP);
96 float3 cell_fract = cell_lP - cell_start;
97
98 /* NOTE(fclem): Use uint to avoid signed int modulo. */
99 uint vis_comp = uint(cell_start.z) % 4u;
100 /* Visibility is stored after the irradiance. */
101 int3 vis_coord = int3(int2(brick_atlas_coord), IRRADIANCE_GRID_BRICK_SIZE * 4) +
102 int3(cell_start);
103 /* Visibility is stored packed 1 cell per channel. */
104 vis_coord.z -= int(vis_comp);
105 float cell_visibility = texelFetch(atlas_tx, vis_coord, 0)[vis_comp];
106 int cell_visibility_bits = int(cell_visibility);
119 float trilinear_weights[8];
120 float total_weight = 0.0f;
121 for (int i = 0; i < 8; i++) {
122 int3 sample_position = grid_cell_corner(i);
123
124 float3 trilinear = select(
125 1.0f - cell_fract, cell_fract, greaterThan(sample_position, int3(0)));
126 float positional_weight = trilinear.x * trilinear.y * trilinear.z;
127
128 float len;
129 float3 corner_vec = float3(sample_position) - cell_fract;
130 float3 corner_dir = normalize_and_get_length(corner_vec, len);
131 float cos_theta = (len > 1e-8f) ? dot(lNg, corner_dir) : 1.0f;
132 float geometry_weight = saturate(cos_theta * 0.5f + 0.5f);
133
134 float validity_weight = float((cell_visibility_bits >> i) & 1);
135
136 /* Biases. See McGuire's presentation. */
137 positional_weight += 0.001f;
138 geometry_weight = square(geometry_weight) + 0.2f + grid_data.facing_bias;
139
140 trilinear_weights[i] = saturate(positional_weight * geometry_weight * validity_weight);
141 total_weight += trilinear_weights[i];
142 }
143 float total_weight_inv = safe_rcp(total_weight);
144
145 float3 trilinear_coord = float3(0.0f);
146 for (int i = 0; i < 8; i++) {
147 float3 sample_position = float3((int3(i) >> int3(0, 1, 2)) & 1);
148 trilinear_coord += sample_position * trilinear_weights[i] * total_weight_inv;
149 }
150 /* Replace sampling coordinates with manually weighted trilinear coordinates. */
151 return 0.5f + cell_start + trilinear_coord;
152}
153
154} // namespace eevee::lightprobe::volume
155
156namespace eevee {
157
159 /* NOTE: Use uint instead of IrradianceBrickPacked because Metal needs to know the exact type. */
161 [[uniform(IRRADIANCE_GRID_BUF_SLOT)]] VolumeProbeData (&grids_infos_buf)[IRRADIANCE_GRID_MAX];
163
165 {
166 float4 texture_coord = float4(atlas_coord, float(IRRADIANCE_GRID_BRICK_SIZE)) /
169 sh.L0.M0 = textureLod(irradiance_atlas_tx, texture_coord.xyz, 0.0f);
170 texture_coord.z += texture_coord.w;
171 sh.L1.Mn1 = textureLod(irradiance_atlas_tx, texture_coord.xyz, 0.0f);
172 texture_coord.z += texture_coord.w;
173 sh.L1.M0 = textureLod(irradiance_atlas_tx, texture_coord.xyz, 0.0f);
174 texture_coord.z += texture_coord.w;
175 sh.L1.Mp1 = textureLod(irradiance_atlas_tx, texture_coord.xyz, 0.0f);
176 return sh;
177 }
178
180 {
181 /* We need a 0.5 offset because of filtering. */
182 return sample_atlas(float3(0.5001f));
183 }
184
186 int grid_index_start = 0) const
187 {
188 float3 lP;
189 int index = select_volume(P, grid_index_start, lP);
190 VolumeProbeData grid_data = grids_infos_buf[index];
191 return sample_probe(grid_data, lP);
192 }
193
195 {
196 float3 lP;
197 int index = select_volume_dithered(sampling, P, 0, lP);
198 VolumeProbeData grid_data = grids_infos_buf[index];
199 return sample_probe(grid_data, lP);
200 }
201
203 float3 P,
204 float3 V,
205 float3 Ng) const
206 {
207 float3 lP;
208 int index = select_volume_dithered(sampling, P, 0, lP);
209 VolumeProbeData grid_data = grids_infos_buf[index];
210 return sample_probe_with_bias(grid_data, lP, Ng, V);
211 }
212
213 private:
214 int select_volume(float3 P, int grid_index_start, float3 &lP) const
215 {
216 int index = -1;
217#ifdef GPU_METAL
218/* NOTE: Performs a chunked unroll to avoid the compiler unrolling the entire loop, avoiding
219 * very high instruction counts and long compilation time. Full unroll results in 90k +
220 * instructions. Chunked unroll is 5.1k instructions with reduced register pressure, while
221 * retaining most of the benefits of unrolling. */
222# pragma clang loop unroll_count(16)
223#endif
224 for (int i = grid_index_start; i < IRRADIANCE_GRID_MAX; i++) {
225 /* Last grid is tagged as invalid to stop the iteration. */
226 if (grids_infos_buf[i].grid_size_padded.x == -1) {
227 /* Sample the last grid instead. */
228 index = i - 1;
229 break;
230 }
231
232 /* If sample fall inside the grid, step out of the loop. */
234 index = i;
235 break;
236 }
237 }
238 return index;
239 }
240
241 int select_volume_dithered(const Sampling &sampling,
242 float3 P,
243 int grid_index_start,
244 float3 &lP) const
245 {
246 int index = -1;
247 float random = square(random::pcg_4d(float4(P, sampling.rng_1D_get(SAMPLING_LIGHTPROBE))).x) *
248 0.75f;
249#ifdef GPU_METAL
250/* NOTE: Performs a chunked unroll to avoid the compiler unrolling the entire loop, avoiding
251 * very high instruction counts and long compilation time. Full unroll results in 90k +
252 * instructions. Chunked unroll is 5.1k instructions with reduced register pressure, while
253 * retaining most of the benefits of unrolling. */
254# pragma clang loop unroll_count(16)
255#endif
256 for (int i = grid_index_start; i < IRRADIANCE_GRID_MAX; i++) {
257 /* Last grid is tagged as invalid to stop the iteration. */
258 if (grids_infos_buf[i].grid_size_padded.x == -1) {
259 /* Sample the last grid instead. */
260 index = i - 1;
261 break;
262 }
263
264 /* If sample fall inside the grid, step out of the loop. */
266 index = i;
267 float distance_to_border = reduce_min(
268 min(lP, float3(grids_infos_buf[i].grid_size_padded) - lP));
269 if (distance_to_border < 0.5f + random) {
270 /* Try to sample another grid to get smooth transitions at borders. */
271 continue;
272 }
273 break;
274 }
275 }
276 return index;
277 }
278
279 SphericalHarmonicL1<float4> sample_probe_with_bias(VolumeProbeData grid_data,
280 float3 lP,
281 float3 Ng,
282 float3 V) const
283 {
284 float3x3 world_to_grid_transposed = to_float3x3(grid_data.world_to_grid_transposed);
285 float3 lNg = safe_normalize(Ng * world_to_grid_transposed);
286 float3 lV = safe_normalize(V * world_to_grid_transposed);
287
288 /* Shading point bias. */
289 lP += lNg * grid_data.normal_bias;
290 lP += lV * grid_data.view_bias;
291
292 return sample_probe(grid_data, lP, lNg);
293 }
294
295 SphericalHarmonicL1<float4> sample_probe(VolumeProbeData grid_data,
296 float3 lP,
297 float3 lNg = float3(0)) const
298 {
300 int brick_index = lightprobe::volume::grid_brick_index_get(grid_data, brick_coord);
301 IrradianceBrick brick = irradiance_brick_unpack(bricks_infos_buf[brick_index]);
302
303 float3 brick_lP = lightprobe::volume::grid_brick_local_coord(lP, brick_coord);
304
305 /* Sampling point bias. */
307 irradiance_atlas_tx, grid_data, brick.atlas_coord, brick_lP, lNg);
308
309 float3 atlas_coord = float3(float2(brick.atlas_coord), 0.0f) + brick_lP;
310
311 return sample_atlas(atlas_coord);
312 }
313};
314
315} // namespace eevee
ccl_device_inline float cos_theta(const float3 w)
btScalar max
#define IRRADIANCE_GRID_BUF_SLOT
#define IRRADIANCE_GRID_BRICK_SIZE
#define VOLUME_PROBE_TEX_SLOT
#define IRRADIANCE_BRICK_BUF_SLOT
#define uniform(slot)
#define storage(slot, qualifiers)
#define index(i)
#define sampler(slot)
VecBase< bool, D > greaterThan(VecOp< T, D >, VecOp< T, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define select(A, B, C)
#define floor
bool all(VecOp< bool, D >) RET
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
constexpr T clamp(T, U, U) RET
MatBase< 3, 3 > float3x3
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
TEX_TEMPLATE DataVec textureLod(T, FltCoord, float) RET
SamplerBase< float, 3 > sampler3D
TEX_TEMPLATE SizeVec textureSize(T, int) RET
float square(float v)
int divide_ceil(int a, int b)
float safe_rcp(float a)
VecT normalize_and_get_length(VecT vector, float &out_length)
float reduce_min(float2 a)
VecT safe_normalize(VecT vector)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
IMETHOD void random(Vector &a)
addDelta operator for displacement rotational velocity.
Definition frames.hpp:1283
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
VecBase< float, 3 > float3
float3 grid_brick_local_coord(float3 lP, int3 brick_coord)
bool grid_local_coord(VolumeProbeData grid_data, float3 P, float3 &r_lP)
int3 grid_cell_corner(int cell_corner_id)
float3 grid_bias_sample_coord(sampler3D atlas_tx, VolumeProbeData grid_data, uint2 brick_atlas_coord, float3 brick_lP, float3 lNg)
float3 grid_sample_position(float4x4 grid_local_to_world_mat, int3 grid_res, int3 cell_coord)
int grid_brick_index_get(VolumeProbeData grid_data, int3 brick_coord)
float4 pcg_4d(float4 v)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define saturate(a)
Definition smaa.cc:319
#define min(a, b)
Definition sort.cc:38
spherical_harmonics::BandL1< T > L1
spherical_harmonics::BandL0< T > L0
SphericalHarmonicL1< float4 > sample_probe(const Sampling &sampling, float3 P, float3 V, float3 Ng) const
SphericalHarmonicL1< float4 > sample_probe_no_dithered_no_biases(float3 P, int grid_index_start=0) const
VolumeProbeData(& grids_infos_buf)[IRRADIANCE_GRID_MAX]
SphericalHarmonicL1< float4 > sample_probe_no_bias(const Sampling &sampling, float3 P) const
SphericalHarmonicL1< float4 > sample_atlas(float3 atlas_coord) const
SphericalHarmonicL1< float4 > world() const
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
CCL_NAMESPACE_BEGIN struct Window V