Blender V5.3
kernel/light/light.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2010-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
8#include "kernel/globals.h"
9
11
12#include "kernel/light/area.h"
14#include "kernel/light/point.h"
15#include "kernel/light/spot.h"
16#include "kernel/light/sun.h"
18#include "kernel/sample/lcg.h"
19#include "kernel/types.h"
20
22
23/* Light info. */
24
26 const int index,
27 const int bounce)
28{
29 return (bounce > kernel_data_fetch(lights, index).max_bounces);
30}
31
32/* Light linking. */
33
35{
36#ifdef __LIGHT_LINKING__
37 if (!(kernel_data.kernel_features & KERNEL_FEATURE_LIGHT_LINKING)) {
38 return OBJECT_NONE;
39 }
40
41 return sd->object;
42#else
43 return OBJECT_NONE;
44#endif
45}
46
48{
49#ifdef __LIGHT_LINKING__
50 if (!(kernel_data.kernel_features & KERNEL_FEATURE_LIGHT_LINKING)) {
51 return OBJECT_NONE;
52 }
53
54 return INTEGRATOR_STATE(state, path, mis_ray_object);
55#else
56 return OBJECT_NONE;
57#endif
58}
59
61 const int receiver,
62 const int emitter)
63{
64#ifdef __LIGHT_LINKING__
65 if (!(kernel_data.kernel_features & KERNEL_FEATURE_LIGHT_LINKING)) {
66 return true;
67 }
68
69 kernel_assert(emitter != OBJECT_NONE);
70 kernel_assert(receiver != OBJECT_NONE);
71
72 const uint64_t set_membership = kernel_data_fetch(objects, emitter).light_set_membership;
73 const uint receiver_set = kernel_data_fetch(objects, receiver).receiver_light_set;
74 return ((uint64_t(1) << uint64_t(receiver_set)) & set_membership) != 0;
75#else
76 return true;
77#endif
78}
79
80/* Sample point on an individual light. */
81
82template<bool in_volume_segment>
84 const int lamp,
85 const float2 rand,
86 const float3 P,
87 const float3 N,
88 const int runtime_flags,
89 const uint32_t path_flag,
91{
92 const ccl_global KernelLight *klight = &kernel_data_fetch(lights, lamp);
93 if (path_flag & PATH_RAY_SHADOW_CATCHER_PASS) {
94 if (klight->shader_id & SHADER_EXCLUDE_SHADOW_CATCHER) {
95 return false;
96 }
97 }
98
99 const LightType type = (LightType)klight->type;
100 ls->type = type;
101 ls->shader = klight->shader_id;
102 ls->object = klight->object_id;
103 ls->prim = lamp;
104 ls->group = object_lightgroup(kg, ls->object);
105
106 if (in_volume_segment && (type == LIGHT_SUN || type == LIGHT_BACKGROUND)) {
107 /* Distant lights in a volume get a dummy sample, position will not actually
108 * be used in that case. Only when sampling from a specific scatter position
109 * do we actually need to evaluate these. */
110 ls->P = zero_float3();
111 ls->Ng = zero_float3();
112 ls->D = zero_float3();
113 ls->pdf = 1.0f;
114 ls->eval_fac = 0.0f;
115 ls->t = FLT_MAX;
116 return true;
117 }
118
119 if (type == LIGHT_SUN) {
120 if (!sun_light_sample(klight, rand, ls)) {
121 return false;
122 }
123 }
124 else if (type == LIGHT_BACKGROUND) {
125 /* infinite area light (e.g. light dome or env light) */
126 const float3 D = -background_light_sample(kg, P, rand, &ls->pdf);
127
128 ls->P = D;
129 ls->Ng = D;
130 ls->D = -D;
131 ls->t = FLT_MAX;
132 ls->eval_fac = 1.0f;
133 }
134 else if (type == LIGHT_SPOT) {
135 if (!spot_light_sample<in_volume_segment>(kg, klight, rand, P, N, runtime_flags, ls)) {
136 return false;
137 }
138 }
139 else if (type == LIGHT_POINT) {
140 if (!point_light_sample(klight, rand, P, N, runtime_flags, ls)) {
141 return false;
142 }
143 }
144 else {
145 /* area light */
146 if (!area_light_sample<in_volume_segment>(klight, rand, P, ls)) {
147 return false;
148 }
149 }
150
151 return in_volume_segment || (ls->pdf > 0.0f);
152}
153
154/* Sample a point on the chosen emitter. */
155
156template<bool in_volume_segment>
158 const float3 rand_light,
159 const float time,
160 const float3 P,
161 const float3 N,
162 const int object_receiver,
163 const int runtime_flags,
164 const int bounce,
165 const uint32_t path_flag,
167{
168 /* The first two dimensions of the Sobol sequence have better stratification, use them to sample
169 * position on the light. */
170 const float2 rand = make_float2(rand_light);
171
172 int prim;
173 int visibility_flag;
174 int object_id;
175#ifdef __LIGHT_TREE__
176 if (kernel_data.integrator.use_light_tree) {
177 const ccl_global KernelLightTreeEmitter *kemitter = &kernel_data_fetch(light_tree_emitters,
178 ls->emitter_id);
179 prim = kemitter->light.id;
180 visibility_flag = kemitter->visibility_flag;
181 object_id = (prim >= 0) ? ls->object : kemitter->object_id;
182 }
183 else
184#endif
185 {
187 light_distribution, ls->emitter_id);
188 prim = kdistribution->prim;
189 object_id = kdistribution->object_id;
190 visibility_flag = kdistribution->visibility_flag;
191 }
192
193 if (!light_link_object_match(kg, object_receiver, object_id)) {
194 return false;
195 }
196
197 if (prim >= 0) {
198 /* Mesh light. */
199
200 /* Exclude synthetic meshes from shadow catcher pass. */
201 if ((path_flag & PATH_RAY_SHADOW_CATCHER_PASS) &&
202 !(kernel_data_fetch(object_flag, object_id) & SD_OBJECT_SHADOW_CATCHER))
203 {
204 return false;
205 }
206
207 if (!triangle_light_sample<in_volume_segment>(kg, prim, object_id, rand, time, ls, P)) {
208 return false;
209 }
210 ls->shader |= visibility_flag;
211 }
212 else {
213 const int light = ~prim;
214
215 if (UNLIKELY(light_select_reached_max_bounces(kg, light, bounce))) {
216 return false;
217 }
218
219 if (!light_sample<in_volume_segment>(kg, light, rand, P, N, runtime_flags, path_flag, ls)) {
220 return false;
221 }
222 }
223
224 ls->pdf *= ls->pdf_selection;
225 return in_volume_segment || (ls->pdf > 0.0f);
226}
227
228/* Intersect ray with individual light. */
229
230/* Returns the total number of hits (the input num_hits plus the number of the new intersections).
231 */
232template<bool is_main_path>
234 const ccl_private Ray *ccl_restrict ray,
236 const int last_prim,
237 const int last_object,
238 const int last_type,
239 const PathRayVisibility path_visibility,
240 const uint32_t path_flag,
241 const uint8_t path_mnee,
242 const int receiver_forward,
243 ccl_private uint *lcg_state,
244 int num_hits)
245{
246#ifdef __SHADOW_LINKING__
247 const bool is_indirect_ray = !(path_visibility & PATH_RAY_VISIBILITY_CAMERA);
248#endif
249
250 for (int lamp = 0; lamp < kernel_data.integrator.num_lights; lamp++) {
251 const ccl_global KernelLight *klight = &kernel_data_fetch(lights, lamp);
252 const int object = klight->object_id;
253
254 if (path_visibility & PATH_RAY_VISIBILITY_CAMERA) {
255 if (klight->shader_id & SHADER_EXCLUDE_CAMERA) {
256 continue;
257 }
258 }
259 else {
260 if (!(klight->shader_id & SHADER_USE_MIS)) {
261 continue;
262 }
263
264#ifdef __MNEE__
265 /* This path should have been resolved with mnee, it will
266 * generate a firefly for small lights since it is improbable. */
267 if ((path_mnee & PATH_MNEE_CULL_LIGHT_CONNECTION) && klight->use_caustics) {
268 continue;
269 }
270#endif
271 }
272
273 if (path_flag & PATH_RAY_SHADOW_CATCHER_PASS) {
274 if (klight->shader_id & SHADER_EXCLUDE_SHADOW_CATCHER) {
275 continue;
276 }
277 }
278
279#ifdef __SHADOW_LINKING__
280 /* For the main path exclude shadow-linked lights if intersecting with an indirect light ray.
281 * Those lights are handled via dedicated light intersect and shade kernels.
282 * For the shadow path used for the dedicated light shading ignore all non-shadow-linked
283 * lights. */
284 if (kernel_data.kernel_features & KERNEL_FEATURE_SHADOW_LINKING) {
285 if (is_main_path) {
286 if (is_indirect_ray &&
287 kernel_data_fetch(objects, object).shadow_set_membership != LIGHT_LINK_MASK_ALL)
288 {
289 continue;
290 }
291 }
292 else if (kernel_data_fetch(objects, object).shadow_set_membership == LIGHT_LINK_MASK_ALL) {
293 continue;
294 }
295 }
296#endif
297
298#ifdef __LIGHT_LINKING__
299 /* Light linking. */
300 if (!(path_visibility & PATH_RAY_VISIBILITY_CAMERA) &&
301 !light_link_object_match(kg, receiver_forward, object))
302 {
303 continue;
304 }
305#endif
306
307 const LightType type = (LightType)klight->type;
308 float t = 0.0f;
309
310 if (type == LIGHT_SPOT) {
311 if (!spot_light_intersect(klight, ray, &t)) {
312 continue;
313 }
314 }
315 else if (type == LIGHT_POINT) {
316 if (!point_light_intersect(klight, ray, &t)) {
317 continue;
318 }
319 }
320 else if (type == LIGHT_AREA) {
321 if (!area_light_intersect(klight, ray, &t)) {
322 continue;
323 }
324 }
325 else if (type == LIGHT_SUN) {
326 if (is_main_path || ray->tmax != FLT_MAX) {
327 continue;
328 }
329 if (!sun_light_intersect(klight, ray, &t)) {
330 continue;
331 }
332 }
333 else {
334 continue;
335 }
336
337 /* Avoid self-intersections. */
338 if (last_prim == lamp && last_object == object && last_type == PRIMITIVE_LAMP) {
339 continue;
340 }
341
342 ++num_hits;
343
344#ifdef __SHADOW_LINKING__
345 if (!is_main_path) {
346 /* The non-main rays are only raced by the dedicated light kernel, after the shadow linking
347 * feature check. */
349
350 if ((isect->prim != PRIM_NONE) && (lcg_step_float(lcg_state) > 1.0f / num_hits)) {
351 continue;
352 }
353 }
354 else
355#endif
356 if (t >= isect->t)
357 {
358 continue;
359 }
360
361 isect->t = t;
362 isect->u = 0.0f;
363 isect->v = 0.0f;
364 isect->type = PRIMITIVE_LAMP;
365 isect->prim = lamp;
366 isect->object = object;
367 }
368
369 return num_hits;
370}
371
372/* Lights intersection for the main path.
373 * Intersects spot, point, and area lights. */
376 const ccl_private Ray *ccl_restrict ray,
378 const int last_prim,
379 const int last_object,
380 const int last_type,
381 const PathRayVisibility path_visibility,
382 const uint32_t path_flag)
383{
384 const uint8_t path_mnee = INTEGRATOR_STATE(state, path, mnee);
385 const int receiver_forward = light_link_receiver_forward(kg, state);
386
388 ray,
389 isect,
390 last_prim,
391 last_object,
392 last_type,
393 path_visibility,
394 path_flag,
395 path_mnee,
396 receiver_forward,
397 nullptr,
398 0);
399
400 return isect->prim != PRIM_NONE;
401}
402
403/* Lights intersection for the shadow linking.
404 * Intersects spot, point, area, and sun lights.
405 *
406 * Returns the total number of hits (the input num_hits plus the number of the new intersections).
407 */
409 const ccl_private Ray *ccl_restrict ray,
411 const int last_prim,
412 const int last_object,
413 const int last_type,
414 const PathRayVisibility path_visibility,
415 const uint32_t path_flag,
416 const int receiver_forward,
417 ccl_private uint *lcg_state,
418 const int num_hits)
419{
421 ray,
422 isect,
423 last_prim,
424 last_object,
425 last_type,
426 path_visibility,
427 path_flag,
429 receiver_forward,
430 lcg_state,
431 num_hits);
432}
433
434/* Setup light sample from intersection. */
435
439 const float3 ray_P,
440 const float3 ray_D,
441 const float3 N,
442 const uint32_t path_flag)
443{
444 const ccl_global KernelLight *klight = &kernel_data_fetch(lights, isect->prim);
445 const LightType type = (LightType)klight->type;
446
447 if (type == LIGHT_SPOT) {
448 return spot_light_eval_from_intersection(kg, klight, ray_P, ray_D, isect->t, N, path_flag);
449 }
450 if (type == LIGHT_POINT) {
451 return point_light_eval_from_intersection(klight, ray_P, ray_D, isect->t, N, path_flag);
452 }
453 if (type == LIGHT_AREA) {
454 return area_light_eval_from_intersection(klight, ray_P, ray_D, isect->t);
455 }
456
457 kernel_assert(!"Invalid lamp type in light_eval_from_intersection");
458 return LightEval{};
459}
460
461/* Get light coordinates from position on light. */
463 const ccl_global KernelLight *klight,
464 const float3 P,
465 const float3 D,
468{
469 const LightType type = (LightType)klight->type;
470
471 if (type == LIGHT_SPOT) {
472 Ng = (klight->spot.is_sphere) ? normalize(P - klight->co) : -D;
473 const float3 local_ray = spot_light_to_local(kg, klight, -D);
474 uv = spot_light_uv(local_ray, klight->spot.half_cot_half_spot_angle);
475 }
476 else if (type == LIGHT_POINT) {
477 Ng = (klight->spot.is_sphere) ? normalize(P - klight->co) : -D;
478 uv = point_light_uv(kg, klight, Ng);
479 }
480 else if (type == LIGHT_AREA) {
481 Ng = klight->area.dir;
482 uv = area_light_uv(klight, P);
483 }
484 else if (type == LIGHT_SUN) {
485 Ng = -D;
486 uv = sun_light_uv(kg, klight, D);
487 }
488 else {
489 kernel_assert(0);
490 }
491}
492
#define D
ccl_device_inline bool area_light_sample(const ccl_global KernelLight *klight, const float2 rand, const float3 P, ccl_private LightSample *ls)
Definition area.h:329
ccl_device_inline LightEval area_light_eval_from_intersection(const ccl_global KernelLight *klight, const float3 ray_P, const float3 ray_D, const float t)
Definition area.h:445
ccl_device_inline float2 area_light_uv(const ccl_global KernelLight *klight, const float3 P)
Definition area.h:429
ccl_device_inline bool area_light_intersect(const ccl_global KernelLight *klight, const ccl_private Ray *ccl_restrict ray, ccl_private float *t)
Definition area.h:390
#define UNLIKELY(x)
unsigned long long int uint64_t
#define kernel_assert(cond)
#define kernel_data
#define ccl_restrict
#define ccl_device_forceinline
#define kernel_data_fetch(name, index)
#define PRIM_NONE
#define OBJECT_NONE
#define ccl_private
#define ccl_device_inline
#define KERNEL_FEATURE_SHADOW_LINKING
#define LIGHT_LINK_MASK_ALL
#define KERNEL_FEATURE_LIGHT_LINKING
#define ccl_global
#define CCL_NAMESPACE_END
#define FLT_MAX
#define index(i)
VecBase< float, D > normalize(VecOp< float, D >) RET
const int state
ccl_device_inline int object_lightgroup(KernelGlobals kg, const int object)
ccl_device_inline float3 background_light_sample(KernelGlobals kg, const float3 P, float2 rand, ccl_private float *pdf)
ccl_device void light_normal_uv_from_position(KernelGlobals kg, const ccl_global KernelLight *klight, const float3 P, const float3 D, ccl_private float3 &Ng, ccl_private float2 &uv)
ccl_device_inline bool light_sample(KernelGlobals kg, const int lamp, const float2 rand, const float3 P, const float3 N, const int runtime_flags, const uint32_t path_flag, ccl_private LightSample *ls)
ccl_device_forceinline int lights_intersect_impl(KernelGlobals kg, const ccl_private Ray *ccl_restrict ray, ccl_private Intersection *ccl_restrict isect, const int last_prim, const int last_object, const int last_type, const PathRayVisibility path_visibility, const uint32_t path_flag, const uint8_t path_mnee, const int receiver_forward, ccl_private uint *lcg_state, int num_hits)
ccl_device_inline bool light_link_object_match(KernelGlobals kg, const int receiver, const int emitter)
CCL_NAMESPACE_BEGIN ccl_device_inline bool light_select_reached_max_bounces(KernelGlobals kg, const int index, const int bounce)
ccl_device LightEval light_eval_from_intersection(KernelGlobals kg, const ccl_private Intersection *ccl_restrict isect, const float3 ray_P, const float3 ray_D, const float3 N, const uint32_t path_flag)
ccl_device_inline int light_link_receiver_nee(KernelGlobals kg, const ccl_private ShaderData *sd)
ccl_device bool lights_intersect(KernelGlobals kg, IntegratorState state, const ccl_private Ray *ccl_restrict ray, ccl_private Intersection *ccl_restrict isect, const int last_prim, const int last_object, const int last_type, const PathRayVisibility path_visibility, const uint32_t path_flag)
ccl_device int lights_intersect_shadow_linked(KernelGlobals kg, const ccl_private Ray *ccl_restrict ray, ccl_private Intersection *ccl_restrict isect, const int last_prim, const int last_object, const int last_type, const PathRayVisibility path_visibility, const uint32_t path_flag, const int receiver_forward, ccl_private uint *lcg_state, const int num_hits)
ccl_device_inline int light_link_receiver_forward(KernelGlobals kg, IntegratorState state)
uint32_t PathRayVisibility
@ PATH_MNEE_NONE
@ PATH_MNEE_CULL_LIGHT_CONNECTION
@ PRIMITIVE_LAMP
@ PATH_RAY_SHADOW_CATCHER_PASS
@ PATH_RAY_VISIBILITY_CAMERA
@ SD_OBJECT_SHADOW_CATCHER
@ SHADER_EXCLUDE_SHADOW_CATCHER
@ SHADER_EXCLUDE_CAMERA
@ SHADER_USE_MIS
LightType
@ LIGHT_AREA
@ LIGHT_SUN
@ LIGHT_SPOT
@ LIGHT_BACKGROUND
@ LIGHT_POINT
ccl_device float lcg_step_float(T rng)
Definition lcg.h:22
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_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_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_forceinline bool triangle_light_sample(KernelGlobals kg, const int prim, const int object, const float2 rand, const float time, ccl_private LightSample *ls, const float3 P)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
#define N
#define ccl_device
#define make_float2
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_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
#define INTEGRATOR_STATE(state, nested_struct, member)
Definition state.h:234
IntegratorStateCPU * IntegratorState
Definition state.h:227
ccl_device bool sun_light_intersect(const ccl_global KernelLight *klight, const ccl_private Ray *ccl_restrict ray, ccl_private float *t)
Definition sun.h:57
CCL_NAMESPACE_BEGIN ccl_device_inline float2 sun_light_uv(KernelGlobals kg, const ccl_global KernelLight *klight, const float3 D)
Definition sun.h:15
ccl_device_inline bool sun_light_sample(const ccl_global KernelLight *klight, const float2 rand, ccl_private LightSample *ls)
Definition sun.h:34