Blender V5.3
spot.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
10#include "util/math_fast.h"
11#include "util/math_intersect.h"
12
14
15/* Transform vector to spot light's local coordinate system. */
17 const ccl_global KernelLight *klight,
18 const float3 ray)
19{
20 const Transform itfm = lamp_get_inverse_transform(kg, klight);
21 float3 transformed_ray = safe_normalize(transform_direction(&itfm, ray));
22 transformed_ray.z = -transformed_ray.z;
23
24 return transformed_ray;
25}
26
27/* Compute spot light attenuation of a ray given in local coordinate system. */
29{
30 return smoothstepf((ray.z - spot->cos_half_spot_angle) * spot->spot_smooth);
31}
32
33ccl_device float2 spot_light_uv(const float3 ray, const float half_cot_half_spot_angle)
34{
35 /* Ensures that the spot light projects the full image regardless of the spot angle. */
36 const float factor = half_cot_half_spot_angle / ray.z;
37
38 /* NOTE: Return barycentric coordinates in the same notation as Embree and OptiX. */
39 return make_float2(ray.y * factor + 0.5f, -(ray.x + ray.y) * factor);
40}
41
42template<bool in_volume_segment>
44 const ccl_global KernelLight *klight,
45 const float2 rand,
46 const float3 P,
47 const float3 N,
48 const int runtime_flags,
50{
51 const float r_sq = sqr(klight->spot.radius);
52
53 float3 lightN = P - klight->co;
54 const float d_sq = len_squared(lightN);
55 const float d = sqrtf(d_sq);
56 lightN /= d;
57
58 ls->eval_fac = klight->spot.eval_fac;
59
60 if (klight->spot.is_sphere) {
61 /* Spherical light geometry. */
62 float cos_theta;
63 ls->t = FLT_MAX;
64 if (d_sq > r_sq) {
65 /* Outside sphere. */
66 const float one_minus_cos_half_spot_spread = 1.0f - klight->spot.cos_half_larger_spread;
67 const float one_minus_cos_half_angle = sin_sqr_to_one_minus_cos(r_sq / d_sq);
68
69 if (in_volume_segment || one_minus_cos_half_angle < one_minus_cos_half_spot_spread) {
70 /* Sample visible part of the sphere. */
71 ls->D = sample_uniform_cone(-lightN, one_minus_cos_half_angle, rand, &cos_theta, &ls->pdf);
72 }
73 else {
74 /* Sample spread cone. */
75 ls->D = sample_uniform_cone(
76 -klight->spot.dir, one_minus_cos_half_spot_spread, rand, &cos_theta, &ls->pdf);
77
79 P, ls->D, 0.0f, FLT_MAX, klight->co, klight->spot.radius, &ls->P, &ls->t))
80 {
81 /* Sampled direction does not intersect with the light. */
82 return false;
83 }
84 }
85 }
86 else {
87 /* Inside sphere. */
88 const bool has_transmission = (runtime_flags & SR_BSDF_HAS_TRANSMISSION);
89 if (has_transmission) {
90 ls->D = sample_uniform_sphere(rand);
91 ls->pdf = M_1_2PI_F * 0.5f;
92 }
93 else {
94 sample_cos_hemisphere(N, rand, &ls->D, &ls->pdf);
95 }
96 cos_theta = -dot(ls->D, lightN);
97 }
98
99 /* Attenuation. */
100 const float3 local_ray = spot_light_to_local(kg, klight, -ls->D);
101 if (d_sq > r_sq) {
102 ls->eval_fac *= spot_light_attenuation(&klight->spot, local_ray);
103 }
104 if (!in_volume_segment && ls->eval_fac == 0.0f) {
105 return false;
106 }
107
108 if (ls->t == FLT_MAX) {
109 /* Law of cosines. */
110 ls->t = d * cos_theta -
111 copysignf(safe_sqrtf(r_sq - d_sq + d_sq * sqr(cos_theta)), d_sq - r_sq);
112 ls->P = P + ls->D * ls->t;
113 }
114 else {
115 /* Already computed when sampling the spread cone. */
116 }
117
118 /* Remap sampled point onto the sphere to prevent precision issues with small radius. */
119 ls->Ng = normalize(ls->P - klight->co);
120 ls->P = ls->Ng * klight->spot.radius + klight->co;
121 }
122 else {
123 /* Point light with ad-hoc radius based on oriented disk. */
124 ls->P = klight->co;
125 if (r_sq > 0.0f) {
126 ls->P += disk_light_sample(lightN, rand) * klight->spot.radius;
127 }
128
129 ls->D = safe_normalize_len(ls->P - P, &ls->t);
130 ls->Ng = -ls->D;
131
132 /* Attenuation. */
133 const float3 local_ray = spot_light_to_local(kg, klight, -ls->D);
134 ls->eval_fac *= spot_light_attenuation(&klight->spot, local_ray);
135 if (!in_volume_segment && ls->eval_fac == 0.0f) {
136 return false;
137 }
138
139 /* PDF. */
140 const float invarea = (r_sq > 0.0f) ? 1.0f / (r_sq * M_PI_F) : 1.0f;
141 ls->pdf = invarea * light_pdf_area_to_solid_angle(lightN, -ls->D, ls->t);
142 }
143
144 return true;
145}
146
148 const float d_sq,
149 const float r_sq,
150 const float3 N,
151 const float3 D,
152 const uint32_t path_flag)
153{
154 if (d_sq > r_sq) {
155 return M_1_2PI_F /
156 min(sin_sqr_to_one_minus_cos(r_sq / d_sq), 1.0f - spot->cos_half_larger_spread);
157 }
158
159 const bool has_transmission = (path_flag & PATH_RAY_MIS_HAD_TRANSMISSION);
160 return has_transmission ? M_1_2PI_F * 0.5f : pdf_cos_hemisphere(N, D);
161}
162
164 const ccl_global KernelLight *klight,
166 const float3 P,
167 const float3 N,
168 const uint32_t path_flag)
169{
170 ls->D = safe_normalize_len(ls->P - P, &ls->t);
171
172 ls->eval_fac = klight->spot.eval_fac;
173
174 const float radius = klight->spot.radius;
175 bool use_attenuation = true;
176
177 if (klight->spot.is_sphere) {
178 const float d_sq = len_squared(P - klight->co);
179 const float r_sq = sqr(radius);
180 const float t_sq = sqr(ls->t);
181
182 /* NOTE : preserve pdf in area measure. */
183 const float jacobian_solid_angle_to_area = 0.5f * fabsf(d_sq - r_sq - t_sq) /
184 (radius * ls->t * t_sq);
185 ls->pdf = spot_light_pdf(&klight->spot, d_sq, r_sq, N, ls->D, path_flag) *
186 jacobian_solid_angle_to_area;
187
188 ls->Ng = normalize(ls->P - klight->co);
189
190 use_attenuation = (d_sq > r_sq);
191 }
192 else {
193 /* NOTE : preserve pdf in area measure. */
194 ls->pdf = ls->eval_fac * 4.0f * M_PI_F;
195
196 ls->Ng = -ls->D;
197 }
198
199 /* Attenuation. */
200 const float3 local_ray = spot_light_to_local(kg, klight, -ls->D);
201 if (use_attenuation) {
202 ls->eval_fac *= spot_light_attenuation(&klight->spot, local_ray);
203 }
204}
205
207 const ccl_private Ray *ccl_restrict ray,
208 ccl_private float *t)
209{
210 /* One sided. */
211 if (dot(ray->D, ray->P - klight->co) >= 0.0f) {
212 return false;
213 }
214
215 return point_light_intersect(klight, ray, t);
216}
217
219 const ccl_global KernelLight *klight,
220 const float3 ray_P,
221 const float3 ray_D,
222 const float t,
223 const float3 N,
224 const uint32_t path_flag)
225{
226 const float r_sq = sqr(klight->spot.radius);
227 const float d_sq = len_squared(ray_P - klight->co);
228
229 LightEval light_eval = {klight->spot.eval_fac, 0.0f};
230
231 if (klight->spot.is_sphere) {
232 light_eval.pdf = spot_light_pdf(&klight->spot, d_sq, r_sq, N, ray_D, path_flag);
233 }
234 else {
235 if (t != FLT_MAX) {
236 const float3 lightN = normalize(ray_P - klight->co);
237 const float invarea = (r_sq > 0.0f) ? 1.0f / (r_sq * M_PI_F) : 1.0f;
238 light_eval.pdf = invarea * light_pdf_area_to_solid_angle(lightN, -ray_D, t);
239 }
240 }
241
242 /* Attenuation. */
243 const float3 local_ray = spot_light_to_local(kg, klight, -ray_D);
244 if (!klight->spot.is_sphere || d_sq > r_sq) {
245 light_eval.eval_fac *= spot_light_attenuation(&klight->spot, local_ray);
246 }
247
248 return light_eval;
249}
250
251/* Find the ray segment lit by the spot light. */
253 const ccl_global KernelLight *klight,
254 const float3 P,
255 const float3 D,
257{
258 /* Convert to local space of the spot light. */
259 const Transform itfm = lamp_get_inverse_transform(kg, klight);
260 float3 local_P = P + klight->spot.dir * klight->spot.ray_segment_dp;
261 local_P = transform_point(&itfm, local_P);
262 const float3 local_D = transform_direction(&itfm, D);
263 const float3 axis = make_float3(0.0f, 0.0f, -1.0f);
264
265 /* Intersect the ray with the smallest enclosing cone of the light spread. */
266 return ray_cone_intersect(
267 axis, local_P, local_D, sqr(klight->spot.cos_half_spot_angle), t_range);
268}
269
270template<bool in_volume_segment>
272 const float3 centroid,
273 const float3 P,
274 const ccl_private KernelBoundingCone &bcone,
275 ccl_private float &cos_theta_u,
277 ccl_private float3 &point_to_centroid,
278 ccl_private float &energy)
279{
280 float min_distance;
281 point_to_centroid = safe_normalize_len(centroid - P, &min_distance);
282 distance = min_distance * one_float2();
283
284 const float radius = klight->spot.radius;
285
286 if (klight->spot.is_sphere) {
287 cos_theta_u = (min_distance > radius) ? cos_from_sin(radius / min_distance) : -1.0f;
288
289 if (in_volume_segment) {
290 return true;
291 }
292
293 distance = (min_distance > radius) ? min_distance * make_float2(1.0f / cos_theta_u, 1.0f) :
294 one_float2() * radius / M_SQRT2_F;
295 }
296 else {
297 const float hypotenuse = sqrtf(sqr(radius) + sqr(min_distance));
298 cos_theta_u = min_distance / hypotenuse;
299
300 if (in_volume_segment) {
301 return true;
302 }
303
304 distance.x = hypotenuse;
305 }
306
307 /* Apply a similar scaling as in `spot_light_attenuation()` to account for spot blend. */
308 {
309 /* Minimum angle formed by the emitter axis and the direction to the shading point,
310 * cos(theta') in the paper. */
311 const float cos_min_outgoing_angle = cosf(
312 fmaxf(0.0f, fast_acosf(dot(bcone.axis, -point_to_centroid)) - fast_acosf(cos_theta_u)));
313 /* Use `cos(bcone.theta_e)` instead of `klight->spot.cos_half_spot_angle` to account for
314 * non-uniform scaling. */
315 energy *= smoothstepf((cos_min_outgoing_angle - cosf(bcone.theta_e)) *
316 klight->spot.spot_smooth);
317 }
318
319 return true;
320}
321
#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
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#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)
float3 transform_point(float3x3 mat, float3 point)
VecT safe_normalize(VecT vector)
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_inline bool point_light_intersect(const ccl_global KernelLight *klight, const ccl_private Ray *ccl_restrict ray, ccl_private float *t)
ccl_device_inline float smoothstepf(const float f)
Definition math_base.h:532
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 dual1 len_squared(const ccl_private dual< T > &a)
Definition math_dual.h:273
ccl_device float fast_acosf(const float x)
Definition math_fast.h:259
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_inline bool ray_cone_intersect(const float3 axis, const float3 P, float3 D, const float cos_angle_sq, ccl_private Interval< float > *t_range)
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 ccl_device
#define fmaxf
#define make_float2
#define M_PI_F
#define cosf
ccl_device float3 sample_uniform_sphere(const float2 rand)
ccl_device_inline float pdf_cos_hemisphere(const float3 N, const float3 D)
#define min(a, b)
Definition sort.cc:38
ccl_device_forceinline void spot_light_mnee_sample_update(KernelGlobals kg, const ccl_global KernelLight *klight, ccl_private LightSample *ls, const float3 P, const float3 N, const uint32_t path_flag)
Definition spot.h:163
ccl_device float2 spot_light_uv(const float3 ray, const float half_cot_half_spot_angle)
Definition spot.h:33
ccl_device_inline LightEval spot_light_eval_from_intersection(KernelGlobals kg, const ccl_global KernelLight *klight, const float3 ray_P, const float3 ray_D, const float t, const float3 N, const uint32_t path_flag)
Definition spot.h:218
ccl_device float spot_light_attenuation(const ccl_global KernelSpotLight *spot, const float3 ray)
Definition spot.h:28
ccl_device_inline bool spot_light_sample(KernelGlobals kg, const ccl_global KernelLight *klight, const float2 rand, const float3 P, const float3 N, const int runtime_flags, ccl_private LightSample *ls)
Definition spot.h:43
CCL_NAMESPACE_BEGIN ccl_device float3 spot_light_to_local(KernelGlobals kg, const ccl_global KernelLight *klight, const float3 ray)
Definition spot.h:16
ccl_device_inline bool spot_light_intersect(const ccl_global KernelLight *klight, const ccl_private Ray *ccl_restrict ray, ccl_private float *t)
Definition spot.h:206
ccl_device_forceinline bool spot_light_tree_parameters(const ccl_global KernelLight *klight, const float3 centroid, const float3 P, const ccl_private KernelBoundingCone &bcone, ccl_private float &cos_theta_u, ccl_private float2 &distance, ccl_private float3 &point_to_centroid, ccl_private float &energy)
Definition spot.h:271
ccl_device_forceinline float spot_light_pdf(const ccl_global KernelSpotLight *spot, const float d_sq, const float r_sq, const float3 N, const float3 D, const uint32_t path_flag)
Definition spot.h:147
ccl_device_inline bool spot_light_valid_ray_segment(KernelGlobals kg, const ccl_global KernelLight *klight, const float3 P, const float3 D, ccl_private Interval< float > *t_range)
Definition spot.h:252
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136