Blender V4.3
distribution.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
9
11
12/* Simple CDF based sampling over all lights in the scene, without taking into
13 * account shading position or normal. */
14
16{
17 /* This is basically std::upper_bound as used by PBRT, to find a point light or
18 * triangle to emit from, proportional to area. a good improvement would be to
19 * also sample proportional to power, though it's not so well defined with
20 * arbitrary shaders. */
21 int first = 0;
22 int len = kernel_data.integrator.num_distribution + 1;
23
24 do {
25 int half_len = len >> 1;
26 int middle = first + half_len;
27
28 if (rand < kernel_data_fetch(light_distribution, middle).totarea) {
29 len = half_len;
30 }
31 else {
32 first = middle + 1;
33 len = len - half_len - 1;
34 }
35 } while (len > 0);
36
37 /* Clamping should not be needed but float rounding errors seem to
38 * make this fail on rare occasions. */
39 int index = clamp(first - 1, 0, kernel_data.integrator.num_distribution - 1);
40
41 return index;
42}
43
45 const float rand,
47{
48 /* Sample light index from distribution. */
49 ls->emitter_id = light_distribution_sample(kg, rand);
50 ls->pdf_selection = kernel_data.integrator.distribution_pdf_lights;
51 return true;
52}
53
55{
56 return kernel_data.integrator.distribution_pdf_lights;
57}
58
#define kernel_data
const KernelGlobalsCPU *ccl_restrict KernelGlobals
#define kernel_data_fetch(name, index)
#define ccl_device
#define ccl_private
#define ccl_device_inline
#define ccl_device_noinline
#define CCL_NAMESPACE_END
ccl_device_inline float light_distribution_pdf_lamp(KernelGlobals kg)
CCL_NAMESPACE_BEGIN ccl_device int light_distribution_sample(KernelGlobals kg, const float rand)
int len
ccl_device_inline int clamp(int a, int mn, int mx)
Definition util/math.h:379