Blender V5.3
light/point.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/common.h"
12
13#include "util/defines.h"
14#include "util/math_intersect.h"
15
17
19 const float2 rand,
20 const float3 P,
21 const float3 N,
22 const int runtime_flags,
24{
25 const float r_sq = sqr(klight->spot.radius);
26
27 float3 lightN = P - klight->co;
28 const float d_sq = len_squared(lightN);
29 const float d = sqrtf(d_sq);
30 lightN /= d;
31
32 ls->eval_fac = klight->spot.eval_fac;
33
34 if (klight->spot.is_sphere) {
35 /* Spherical light geometry. */
36 float cos_theta;
37 if (d_sq > r_sq) {
38 /* Outside sphere. */
39 const float one_minus_cos = sin_sqr_to_one_minus_cos(r_sq / d_sq);
40 ls->D = sample_uniform_cone(-lightN, one_minus_cos, rand, &cos_theta, &ls->pdf);
41 }
42 else {
43 /* Inside sphere. */
44 const bool has_transmission = (runtime_flags & SR_BSDF_HAS_TRANSMISSION);
45 if (has_transmission) {
46 ls->D = sample_uniform_sphere(rand);
47 ls->pdf = M_1_2PI_F * 0.5f;
48 }
49 else {
50 sample_cos_hemisphere(N, rand, &ls->D, &ls->pdf);
51 }
52 cos_theta = -dot(ls->D, lightN);
53 }
54
55 /* Law of cosines. */
56 ls->t = d * cos_theta -
57 copysignf(safe_sqrtf(r_sq - d_sq + d_sq * sqr(cos_theta)), d_sq - r_sq);
58
59 /* Remap sampled point onto the sphere to prevent precision issues with small radius. */
60 ls->P = P + ls->D * ls->t;
61 ls->Ng = normalize(ls->P - klight->co);
62 ls->P = ls->Ng * klight->spot.radius + klight->co;
63 }
64 else {
65 /* Point light with ad-hoc radius based on oriented disk. */
66 ls->P = klight->co;
67 if (r_sq > 0.0f) {
68 ls->P += disk_light_sample(lightN, rand) * klight->spot.radius;
69 }
70
71 ls->D = safe_normalize_len(ls->P - P, &ls->t);
72 ls->Ng = -ls->D;
73
74 /* PDF. */
75 const float invarea = (r_sq > 0.0f) ? 1.0f / (r_sq * M_PI_F) : 1.0f;
76 ls->pdf = invarea * light_pdf_area_to_solid_angle(lightN, -ls->D, ls->t);
77 }
78
79 return true;
80}
81
83 const float d_sq, const float r_sq, const float3 N, const float3 D, const uint32_t path_flag)
84{
85 if (d_sq > r_sq) {
86 return M_1_2PI_F / sin_sqr_to_one_minus_cos(r_sq / d_sq);
87 }
88
89 const bool has_transmission = (path_flag & PATH_RAY_MIS_HAD_TRANSMISSION);
90 return has_transmission ? M_1_2PI_F * 0.5f : pdf_cos_hemisphere(N, D);
91}
92
94 const ccl_global KernelLight *klight,
95 const float3 Ng)
96{
97 /* Texture coordinates. */
98 const Transform itfm = lamp_get_inverse_transform(kg, klight);
99 const float2 uv = map_to_sphere(transform_direction(&itfm, Ng));
100 /* NOTE: Return barycentric coordinates in the same notation as Embree and OptiX. */
101 return make_float2(uv.y, 1.0f - uv.x - uv.y);
102}
103
106 const float3 P,
107 const float3 N,
108 const uint32_t path_flag)
109{
110 ls->D = safe_normalize_len(ls->P - P, &ls->t);
111
112 const float radius = klight->spot.radius;
113
114 if (klight->spot.is_sphere) {
115 const float d_sq = len_squared(P - klight->co);
116 const float r_sq = sqr(radius);
117 const float t_sq = sqr(ls->t);
118
119 /* NOTE : preserve pdf in area measure. */
120 const float jacobian_solid_angle_to_area = 0.5f * fabsf(d_sq - r_sq - t_sq) /
121 (radius * ls->t * t_sq);
122 ls->pdf = sphere_light_pdf(d_sq, r_sq, N, ls->D, path_flag) * jacobian_solid_angle_to_area;
123
124 ls->Ng = normalize(ls->P - klight->co);
125 }
126 else {
127 /* NOTE : preserve pdf in area measure. */
128 ls->pdf = ls->eval_fac * 4.0f * M_PI_F;
129
130 ls->Ng = -ls->D;
131 }
132}
133
135 const ccl_private Ray *ccl_restrict ray,
136 ccl_private float *t)
137{
138 const float radius = klight->spot.radius;
139 if (radius == 0.0f) {
140 return false;
141 }
142
143 if (klight->spot.is_sphere) {
144 float3 P;
145 return ray_sphere_intersect(ray->P, ray->D, ray->tmin, ray->tmax, klight->co, radius, &P, t);
146 }
147
148 float3 P;
149 const float3 diskN = normalize(ray->P - klight->co);
150 return ray_disk_intersect(
151 ray->P, ray->D, ray->tmin, ray->tmax, klight->co, diskN, radius, &P, t);
152}
153
156 const float3 ray_P,
157 const float3 ray_D,
158 const float t,
159 const float3 N,
160 const uint32_t path_flag)
161{
162 const float r_sq = sqr(klight->spot.radius);
163
164 LightEval light_eval = {klight->spot.eval_fac, 0.0f};
165
166 if (klight->spot.is_sphere) {
167 const float d_sq = len_squared(ray_P - klight->co);
168 light_eval.pdf = sphere_light_pdf(d_sq, r_sq, N, ray_D, path_flag);
169 }
170 else {
171 if (t != FLT_MAX) {
172 const float3 lightN = normalize(ray_P - klight->co);
173 const float invarea = (r_sq > 0.0f) ? 1.0f / (r_sq * M_PI_F) : 1.0f;
174 light_eval.pdf = invarea * light_pdf_area_to_solid_angle(lightN, -ray_D, t);
175 }
176 }
177
178 return light_eval;
179}
180
181template<bool in_volume_segment>
183 const float3 centroid,
184 const float3 P,
185 ccl_private float &cos_theta_u,
187 ccl_private float3 &point_to_centroid)
188{
189 float min_distance;
190 point_to_centroid = safe_normalize_len(centroid - P, &min_distance);
191 distance = min_distance * one_float2();
192
193 if (in_volume_segment) {
194 cos_theta_u = 1.0f; /* Any value in [-1, 1], irrelevant since theta = 0 */
195 return true;
196 }
197
198 const float radius = klight->spot.radius;
199
200 if (klight->spot.is_sphere) {
201 if (min_distance > radius) {
202 /* Equivalent to a disk light with the same angular span. */
203 cos_theta_u = cos_from_sin(radius / min_distance);
204 distance.x = min_distance / cos_theta_u;
205 }
206 else {
207 /* Similar to background light. */
208 cos_theta_u = -1.0f;
209 /* HACK: pack radiance scaling in the distance. */
210 distance = one_float2() * radius / M_SQRT2_F;
211 }
212 }
213 else {
214 const float hypotenuse = sqrtf(sqr(radius) + sqr(min_distance));
215 cos_theta_u = min_distance / hypotenuse;
216
217 distance.x = hypotenuse;
218 }
219
220 return true;
221}
222
#define D
ccl_device_inline float cos_theta(const float3 w)
ccl_device float3 disk_light_sample(const float3 n, const float2 rand)
ccl_device float light_pdf_area_to_solid_angle(const float3 Ng, const float3 I, const float t)
#define ccl_restrict
#define ccl_device_forceinline
#define M_SQRT2_F
#define ccl_private
#define ccl_device_inline
#define M_1_2PI_F
#define ccl_global
#define CCL_NAMESPACE_END
#define copysignf(x, y)
float3 sample_cos_hemisphere(const float2 rand)
float3 sample_uniform_cone(float2 rand, float cos_angle)
#define FLT_MAX
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
float distance(float, float) RET
float cos_from_sin(float s)
float3 transform_direction(float3x3 mat, float3 direction)
ccl_device_inline Transform lamp_get_inverse_transform(KernelGlobals kg, const ccl_global KernelLight *klight)
@ SR_BSDF_HAS_TRANSMISSION
@ PATH_RAY_MIS_HAD_TRANSMISSION
ccl_device_forceinline float2 point_light_uv(KernelGlobals kg, const ccl_global KernelLight *klight, const float3 Ng)
Definition light/point.h:93
ccl_device_inline bool point_light_intersect(const ccl_global KernelLight *klight, const ccl_private Ray *ccl_restrict ray, ccl_private float *t)
ccl_device_forceinline bool point_light_tree_parameters(const ccl_global KernelLight *klight, const float3 centroid, const float3 P, ccl_private float &cos_theta_u, ccl_private float2 &distance, ccl_private float3 &point_to_centroid)
ccl_device_forceinline float sphere_light_pdf(const float d_sq, const float r_sq, const float3 N, const float3 D, const uint32_t path_flag)
Definition light/point.h:82
ccl_device_inline LightEval point_light_eval_from_intersection(const ccl_global KernelLight *klight, const float3 ray_P, const float3 ray_D, const float t, const float3 N, const uint32_t path_flag)
ccl_device_forceinline void point_light_mnee_sample_update(const ccl_global KernelLight *klight, ccl_private LightSample *ls, const float3 P, const float3 N, const uint32_t path_flag)
CCL_NAMESPACE_BEGIN ccl_device_inline bool point_light_sample(const ccl_global KernelLight *klight, const float2 rand, const float3 P, const float3 N, const int runtime_flags, ccl_private LightSample *ls)
Definition light/point.h:18
ccl_device_inline float safe_sqrtf(const float f)
Definition math_base.h:571
ccl_device_inline float sin_sqr_to_one_minus_cos(const float s_sq)
Definition math_base.h:666
ccl_device_inline float one_minus_cos(const float angle)
Definition math_base.h:672
ccl_device_inline dual1 len_squared(const ccl_private dual< T > &a)
Definition math_dual.h:273
ccl_device_inline dual2 map_to_sphere(const dual3 co)
Definition math_dual.h:418
ccl_device_inline float2 one_float2()
Definition math_float2.h:18
ccl_device_inline float3 safe_normalize_len(const float3 a, ccl_private float *t)
ccl_device bool ray_disk_intersect(const float3 ray_P, const float3 ray_D, const float ray_tmin, const float ray_tmax, const float3 disk_P, const float3 disk_N, const float disk_radius, ccl_private float3 *isect_P, ccl_private float *isect_t)
CCL_NAMESPACE_BEGIN ccl_device bool ray_sphere_intersect(const float3 ray_P, const float3 ray_D, const float ray_tmin, const float ray_tmax, const float3 sphere_P, const float sphere_radius, ccl_private float3 *isect_P, ccl_private float *isect_t)
#define N
#define sqr
#define fabsf
#define sqrtf
#define make_float2
#define M_PI_F
ccl_device float3 sample_uniform_sphere(const float2 rand)
ccl_device_inline float pdf_cos_hemisphere(const float3 N, const float3 D)
float x
float y