Blender V5.3
kernel/light/background.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "kernel/globals.h"
8
10
11#include "kernel/light/area.h"
12#include "kernel/light/common.h"
13
14#include "util/math_intersect.h"
15
17
18/* Background Light */
19
20/* Clamp the ray differential so the background is not evaluated coarser than the importance map.
21 * At lower mip levels a sharp feature like the sun becomes bigger and blurred. This is not
22 * accounted for in the higher resolution importance map where the sun remains smaller, leading
23 * to noise. The blurring may also make shadows too soft and clamping reduces that bias. */
25{
26 return min(dD, kernel_data.background.map_dD);
27}
28
30 const float2 rand,
31 ccl_private float *pdf)
32{
33 /* for the following, the CDF values are actually a pair of floats, with the
34 * function value as X and the actual CDF as Y. The last entry's function
35 * value is the CDF total. */
36 const int res_x = kernel_data.background.map_res_x;
37 const int res_y = kernel_data.background.map_res_y;
38 const int cdf_width = res_x + 1;
39
40 /* This is basically std::lower_bound as used by PBRT. */
41 int first = 0;
42 int count = res_y;
43
44 while (count > 0) {
45 const int step = count >> 1;
46 const int middle = first + step;
47
48 if (kernel_data_fetch(light_background_marginal_cdf, middle).y < rand.y) {
49 first = middle + 1;
50 count -= step + 1;
51 }
52 else {
53 count = step;
54 }
55 }
56
57 const int index_v = max(0, first - 1);
58 kernel_assert(index_v >= 0 && index_v < res_y);
59
60 const float2 cdf_v = kernel_data_fetch(light_background_marginal_cdf, index_v);
61 const float2 cdf_next_v = kernel_data_fetch(light_background_marginal_cdf, index_v + 1);
62 const float2 cdf_last_v = kernel_data_fetch(light_background_marginal_cdf, res_y);
63
64 /* importance-sampled V direction */
65 const float dv = inverse_lerp(cdf_v.y, cdf_next_v.y, rand.y);
66 const float v = (index_v + dv) / res_y;
67
68 /* This is basically std::lower_bound as used by PBRT. */
69 first = 0;
70 count = res_x;
71 while (count > 0) {
72 const int step = count >> 1;
73 const int middle = first + step;
74
75 if (kernel_data_fetch(light_background_conditional_cdf, index_v * cdf_width + middle).y <
76 rand.x)
77 {
78 first = middle + 1;
79 count -= step + 1;
80 }
81 else {
82 count = step;
83 }
84 }
85
86 const int index_u = max(0, first - 1);
87 kernel_assert(index_u >= 0 && index_u < res_x);
88
89 const float2 cdf_u = kernel_data_fetch(light_background_conditional_cdf,
90 index_v * cdf_width + index_u);
91 const float2 cdf_next_u = kernel_data_fetch(light_background_conditional_cdf,
92 index_v * cdf_width + index_u + 1);
93 const float2 cdf_last_u = kernel_data_fetch(light_background_conditional_cdf,
94 index_v * cdf_width + res_x);
95
96 /* importance-sampled U direction */
97 const float du = inverse_lerp(cdf_u.y, cdf_next_u.y, rand.x);
98 const float u = (index_u + du) / res_x;
99
100 /* compute pdf */
101 const float sin_theta = sinf(M_PI_F * v);
102 const float denom = (M_2PI_F * M_PI_F * sin_theta) * cdf_last_u.x * cdf_last_v.x;
103
104 if (sin_theta == 0.0f || denom == 0.0f) {
105 *pdf = 0.0f;
106 }
107 else {
108 *pdf = (cdf_u.x * cdf_v.x) / denom;
109 }
110
111 /* compute direction */
112 return equirectangular_to_direction(u, v);
113}
114
115/* TODO(sergey): Same as above, after the release we should consider using
116 * `noinline` for all devices.
117 */
119{
120 const float2 uv = direction_to_equirectangular(direction);
121 const int res_x = kernel_data.background.map_res_x;
122 const int res_y = kernel_data.background.map_res_y;
123 const int cdf_width = res_x + 1;
124
125 const float sin_theta = sinf(uv.y * M_PI_F);
126
127 if (sin_theta == 0.0f) {
128 return 0.0f;
129 }
130
131 const int index_u = clamp(float_to_int(uv.x * res_x), 0, res_x - 1);
132 const int index_v = clamp(float_to_int(uv.y * res_y), 0, res_y - 1);
133
134 /* pdfs in V direction */
135 const float2 cdf_last_u = kernel_data_fetch(light_background_conditional_cdf,
136 index_v * cdf_width + res_x);
137 const float2 cdf_last_v = kernel_data_fetch(light_background_marginal_cdf, res_y);
138
139 const float denom = (M_2PI_F * M_PI_F * sin_theta) * cdf_last_u.x * cdf_last_v.x;
140
141 if (denom == 0.0f) {
142 return 0.0f;
143 }
144
145 /* pdfs in U direction */
146 const float2 cdf_u = kernel_data_fetch(light_background_conditional_cdf,
147 index_v * cdf_width + index_u);
148 const float2 cdf_v = kernel_data_fetch(light_background_marginal_cdf, index_v);
149
150 return (cdf_u.x * cdf_v.x) / denom;
151}
152
154 const float3 P,
155 const int index,
156 ccl_private float3 *lightpos,
157 ccl_private float3 *dir)
158{
159 const int portal = kernel_data.integrator.portal_offset + index;
160 const ccl_global KernelLight *klight = &kernel_data_fetch(lights, portal);
161
162 *lightpos = klight->co;
163 *dir = klight->area.dir;
164
165 /* Check whether portal is on the right side. */
166 if (dot(*dir, P - *lightpos) > 1e-4f) {
167 return true;
168 }
169
170 return false;
171}
172
174 const float3 P,
175 float3 direction,
176 const int ignore_portal,
177 ccl_private bool *is_possible)
178{
179 float portal_pdf = 0.0f;
180
181 int num_possible = 0;
182 for (int p = 0; p < kernel_data.integrator.num_portals; p++) {
183 if (p == ignore_portal) {
184 continue;
185 }
186
187 float3 lightpos;
188 float3 dir;
189 if (!background_portal_data_fetch_and_check_side(kg, P, p, &lightpos, &dir)) {
190 continue;
191 }
192
193 /* There's a portal that could be sampled from this position. */
194 if (is_possible) {
195 *is_possible = true;
196 }
197 num_possible++;
198
199 const int portal = kernel_data.integrator.portal_offset + p;
200 const ccl_global KernelLight *klight = &kernel_data_fetch(lights, portal);
201
202 const float3 axis_u = klight->area.axis_u;
203 const float len_u = klight->area.len_u;
204 const float3 axis_v = klight->area.axis_v;
205 const float len_v = klight->area.len_v;
206 const float3 inv_extent_u = axis_u / len_u;
207 const float3 inv_extent_v = axis_v / len_v;
208
209 const bool is_round = (klight->area.invarea < 0.0f);
210
212 direction,
213 1e-4f,
214 FLT_MAX,
215 lightpos,
216 inv_extent_u,
217 inv_extent_v,
218 dir,
219 nullptr,
220 nullptr,
221 nullptr,
222 nullptr,
223 is_round))
224 {
225 continue;
226 }
227
228 if (is_round) {
229 float t;
230 const float3 D = normalize_len(lightpos - P, &t);
231 portal_pdf += fabsf(klight->area.invarea) * light_pdf_area_to_solid_angle(dir, -D, t);
232 }
233 else {
234 portal_pdf += area_light_rect_sample(
235 P, &lightpos, axis_u, len_u, axis_v, len_v, zero_float2(), false);
236 }
237 }
238
239 if (ignore_portal >= 0) {
240 /* We have skipped a portal that could be sampled as well. */
241 num_possible++;
242 }
243
244 return (num_possible > 0) ? portal_pdf / num_possible : 0.0f;
245}
246
248{
249 int num_possible_portals = 0;
250 for (int p = 0; p < kernel_data.integrator.num_portals; p++) {
251 float3 lightpos;
252 float3 dir;
253 if (background_portal_data_fetch_and_check_side(kg, P, p, &lightpos, &dir)) {
254 num_possible_portals++;
255 }
256 }
257 return num_possible_portals;
258}
259
261 const float3 P,
262 float2 rand,
263 const int num_possible,
264 ccl_private int *sampled_portal,
265 ccl_private float *pdf)
266{
267 /* Pick a portal, then re-normalize rand.y. */
268 rand.y *= num_possible;
269 int portal = (int)rand.y;
270 rand.y -= portal;
271
272 /* TODO(sergey): Some smarter way of finding portal to sample
273 * is welcome.
274 */
275 for (int p = 0; p < kernel_data.integrator.num_portals; p++) {
276 /* Search for the sampled portal. */
277 float3 lightpos;
278 float3 dir;
279 if (!background_portal_data_fetch_and_check_side(kg, P, p, &lightpos, &dir)) {
280 continue;
281 }
282
283 if (portal == 0) {
284 /* p is the portal to be sampled. */
285 const int portal = kernel_data.integrator.portal_offset + p;
286 const ccl_global KernelLight *klight = &kernel_data_fetch(lights, portal);
287 const float3 axis_u = klight->area.axis_u;
288 const float3 axis_v = klight->area.axis_v;
289 const float len_u = klight->area.len_u;
290 const float len_v = klight->area.len_v;
291 const bool is_round = (klight->area.invarea < 0.0f);
292
293 float3 D;
294 if (is_round) {
295 lightpos += ellipse_sample(axis_u * len_u * 0.5f, axis_v * len_v * 0.5f, rand);
296 float t;
297 D = normalize_len(lightpos - P, &t);
298 *pdf = fabsf(klight->area.invarea) * light_pdf_area_to_solid_angle(dir, -D, t);
299 }
300 else {
301 *pdf = area_light_rect_sample(P, &lightpos, axis_u, len_u, axis_v, len_v, rand, true);
302 D = normalize(lightpos - P);
303 }
304
305 *pdf /= num_possible;
306 *sampled_portal = p;
307 return D;
308 }
309
310 portal--;
311 }
312
313 return zero_float3();
314}
315
317 const float2 rand,
318 ccl_private float *pdf)
319{
320 const float3 N = make_float3(kernel_data.background.sun);
321 const float angle = kernel_data.background.sun.w;
322 float unused;
323 return sample_uniform_cone(N, one_minus_cos(angle), rand, &unused, pdf);
324}
325
327{
328 const float3 N = make_float3(kernel_data.background.sun);
329 const float angle = kernel_data.background.sun.w;
330 return pdf_uniform_cone(N, D, angle);
331}
332
334 const float3 P,
335 float2 rand,
336 ccl_private float *pdf)
337{
338 float portal_method_pdf = kernel_data.background.portal_weight;
339 float sun_method_pdf = kernel_data.background.sun_weight;
340 float map_method_pdf = kernel_data.background.map_weight;
341
342 int num_portals = 0;
343 if (portal_method_pdf > 0.0f) {
344 /* Check if there are portals in the scene which we can sample. */
345 num_portals = background_num_possible_portals(kg, P);
346 if (num_portals == 0) {
347 portal_method_pdf = 0.0f;
348 }
349 }
350
351 float pdf_fac = (portal_method_pdf + sun_method_pdf + map_method_pdf);
352 if (pdf_fac == 0.0f) {
353 /* Use uniform as a fallback if we can't use any strategy. */
354 *pdf = 1.0f / M_4PI_F;
355 return sample_uniform_sphere(rand);
356 }
357
358 pdf_fac = 1.0f / pdf_fac;
359 portal_method_pdf *= pdf_fac;
360 sun_method_pdf *= pdf_fac;
361 map_method_pdf *= pdf_fac;
362
363 /* We have 100% in total and split it between the three categories.
364 * Therefore, we pick portals if rand.x is between 0 and portal_method_pdf,
365 * sun if rand.x is between portal_method_pdf and (portal_method_pdf + sun_method_pdf)
366 * and map if rand.x is between (portal_method_pdf + sun_method_pdf) and 1. */
367 const float sun_method_cdf = portal_method_pdf + sun_method_pdf;
368
369 int method = 0;
370 float3 D;
371 if (rand.x < portal_method_pdf) {
372 method = 0;
373 /* Rescale rand.x. */
374 if (portal_method_pdf != 1.0f) {
375 rand.x /= portal_method_pdf;
376 }
377
378 /* Sample a portal. */
379 int portal;
380 D = background_portal_sample(kg, P, rand, num_portals, &portal, pdf);
381 if (num_portals > 1) {
382 /* Ignore the chosen portal, its pdf is already included. */
383 *pdf += background_portal_pdf(kg, P, D, portal, nullptr);
384 }
385
386 /* Skip MIS if this is the only method. */
387 if (portal_method_pdf == 1.0f) {
388 return D;
389 }
390 *pdf *= portal_method_pdf;
391 }
392 else if (rand.x < sun_method_cdf) {
393 method = 1;
394 /* Rescale rand.x. */
395 if (sun_method_pdf != 1.0f) {
396 rand.x = (rand.x - portal_method_pdf) / sun_method_pdf;
397 }
398
399 D = background_sun_sample(kg, rand, pdf);
400
401 /* Skip MIS if this is the only method. */
402 if (sun_method_pdf == 1.0f) {
403 return D;
404 }
405 *pdf *= sun_method_pdf;
406 }
407 else {
408 method = 2;
409 /* Rescale rand.x. */
410 if (map_method_pdf != 1.0f) {
411 rand.x = (rand.x - sun_method_cdf) / map_method_pdf;
412 }
413
414 D = background_map_sample(kg, rand, pdf);
415
416 /* Skip MIS if this is the only method. */
417 if (map_method_pdf == 1.0f) {
418 return D;
419 }
420 *pdf *= map_method_pdf;
421 }
422
423 /* MIS weighting. */
424 if (method != 0 && portal_method_pdf != 0.0f) {
425 *pdf += portal_method_pdf * background_portal_pdf(kg, P, D, -1, nullptr);
426 }
427 if (method != 1 && sun_method_pdf != 0.0f) {
428 *pdf += sun_method_pdf * background_sun_pdf(kg, D);
429 }
430 if (method != 2 && map_method_pdf != 0.0f) {
431 *pdf += map_method_pdf * background_map_pdf(kg, D);
432 }
433 return D;
434}
435
437{
438 float portal_method_pdf = kernel_data.background.portal_weight;
439 float sun_method_pdf = kernel_data.background.sun_weight;
440 float map_method_pdf = kernel_data.background.map_weight;
441
442 float portal_pdf = 0.0f;
443 /* Portals are a special case here since we need to compute their pdf in order
444 * to find out if we can sample them. */
445 if (portal_method_pdf > 0.0f) {
446 /* Evaluate PDF of sampling this direction by portal sampling. */
447 bool is_possible = false;
448 portal_pdf = background_portal_pdf(kg, P, direction, -1, &is_possible);
449 if (!is_possible) {
450 /* Portal sampling is not possible here because all portals point to the wrong side.
451 * If other methods can be used instead, do so, otherwise uniform sampling is used as a
452 * fallback. */
453 portal_method_pdf = 0.0f;
454 }
455 }
456
457 float pdf_fac = (portal_method_pdf + sun_method_pdf + map_method_pdf);
458 if (pdf_fac == 0.0f) {
459 /* Use uniform as a fallback if we can't use any strategy. */
460 return 1.0f / M_4PI_F;
461 }
462
463 pdf_fac = 1.0f / pdf_fac;
464 portal_method_pdf *= pdf_fac;
465 sun_method_pdf *= pdf_fac;
466 map_method_pdf *= pdf_fac;
467
468 float pdf = portal_pdf * portal_method_pdf;
469 if (sun_method_pdf != 0.0f) {
470 pdf += background_sun_pdf(kg, direction) * sun_method_pdf;
471 }
472 if (map_method_pdf != 0.0f) {
473 pdf += background_map_pdf(kg, direction) * map_method_pdf;
474 }
475
476 return pdf;
477}
478
479template<bool in_volume_segment>
481 const float t,
482 ccl_private float &cos_theta_u,
484 ccl_private float3 &point_to_centroid,
485 ccl_private float &theta_d)
486{
487 if (in_volume_segment) {
488 if (t == FLT_MAX) {
489 /* In world volumes, distant lights can contribute to the lighting of the volume with
490 * specific configurations of procedurally generated volumes. Use a ray length of 1.0 in this
491 * case to give the distant light some weight, but one that isn't too high for a typical
492 * world volume use case. */
493 theta_d = 1.0f;
494 }
495 else {
496 theta_d = t;
497 }
498 }
499
500 /* Cover the whole sphere */
501 cos_theta_u = -1.0f;
502
503 distance = make_float2(1.0f, 1.0f);
504 point_to_centroid = -centroid;
505
506 return true;
507}
508
#define D
CCL_NAMESPACE_BEGIN ccl_device_inline float area_light_rect_sample(const float3 P, ccl_private float3 *light_p, const float3 axis_u, const float len_u, const float3 axis_v, const float len_v, const float2 rand, bool sample_coord)
Definition area.h:19
ccl_device_inline float sin_theta(const float3 w)
btScalar max
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
ccl_device float2 direction_to_equirectangular(const float3 dir)
ccl_device float3 equirectangular_to_direction(const float u, const float v)
ccl_device_inline float3 ellipse_sample(const float3 ru, const float3 rv, const float2 rand)
ccl_device float light_pdf_area_to_solid_angle(const float3 Ng, const float3 I, const float t)
#define kernel_assert(cond)
#define kernel_data
#define ccl_device_forceinline
#define kernel_data_fetch(name, index)
#define ccl_private
#define ccl_device_inline
#define M_4PI_F
#define ccl_global
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
float3 sample_uniform_cone(float2 rand, float cos_angle)
#define FLT_MAX
#define index(i)
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
float distance(float, float) RET
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
ccl_device_inline float3 background_sun_sample(KernelGlobals kg, const float2 rand, ccl_private float *pdf)
ccl_device_inline float background_sun_pdf(KernelGlobals kg, const float3 D)
ccl_device_forceinline bool background_light_tree_parameters(const float3 centroid, const float t, ccl_private float &cos_theta_u, ccl_private float2 &distance, ccl_private float3 &point_to_centroid, ccl_private float &theta_d)
ccl_device int background_num_possible_portals(KernelGlobals kg, const float3 P)
ccl_device_inline float background_portal_pdf(KernelGlobals kg, const float3 P, float3 direction, const int ignore_portal, ccl_private bool *is_possible)
ccl_device float background_light_pdf(KernelGlobals kg, const float3 P, float3 direction)
ccl_device_inline bool background_portal_data_fetch_and_check_side(KernelGlobals kg, const float3 P, const int index, ccl_private float3 *lightpos, ccl_private float3 *dir)
CCL_NAMESPACE_BEGIN ccl_device_forceinline float background_light_clamp_dD(KernelGlobals kg, const float dD)
ccl_device float background_map_pdf(KernelGlobals kg, const float3 direction)
ccl_device float3 background_map_sample(KernelGlobals kg, const float2 rand, ccl_private float *pdf)
ccl_device float3 background_portal_sample(KernelGlobals kg, const float3 P, float2 rand, const int num_possible, ccl_private int *sampled_portal, ccl_private float *pdf)
ccl_device_inline float3 background_light_sample(KernelGlobals kg, const float3 P, float2 rand, ccl_private float *pdf)
ccl_device_inline float inverse_lerp(const float a, const float b, const float x)
Definition math_base.h:554
ccl_device_inline int float_to_int(const float f)
Definition math_base.h:454
ccl_device_inline float one_minus_cos(const float angle)
Definition math_base.h:672
CCL_NAMESPACE_BEGIN ccl_device_inline float2 zero_float2()
Definition math_float2.h:13
ccl_device_inline float2 normalize_len(const float2 a, ccl_private float *t)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
ccl_device bool ray_quad_intersect(const float3 ray_P, const float3 ray_D, const float ray_tmin, const float ray_tmax, const float3 quad_P, const float3 inv_quad_u, const float3 inv_quad_v, const float3 quad_n, ccl_private float3 *isect_P, ccl_private float *isect_t, ccl_private float *isect_u, ccl_private float *isect_v, bool ellipse)
#define N
#define fabsf
#define ccl_device
#define M_2PI_F
#define sinf
#define make_float2
#define M_PI_F
ccl_device_inline float pdf_uniform_cone(const float3 N, const float3 D, const float angle)
ccl_device float3 sample_uniform_sphere(const float2 rand)
#define min(a, b)
Definition sort.cc:38
float x
float y