Blender V5.3
shade_light.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
8
11#include "kernel/light/light.h"
12#include "kernel/light/sample.h"
13
14#include "kernel/geom/object.h"
15#include "kernel/types.h"
16
18
21{
22 /* Setup light sample. */
25
27
28 const float3 ray_P = INTEGRATOR_STATE(state, ray, P);
29 const float3 ray_D = INTEGRATOR_STATE(state, ray, D);
30 const float ray_time = INTEGRATOR_STATE(state, ray, time);
31 const PathRayVisibility path_visibility = INTEGRATOR_STATE(state, path, visibility);
32 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
33 const float3 N = INTEGRATOR_STATE(state, path, mis_origin_n);
34
35 /* Advance ray to new start distance. */
37
38 const LightEval light_eval = light_eval_from_intersection(
39 kg, &isect, ray_P, ray_D, N, path_flag);
40 if (light_eval.eval_fac == 0.0f) {
41 return SHADER_EVAL_EMPTY;
42 }
43
44 /* Use visibility flag to skip lights. */
45#ifdef __PASSES__
46 {
47 const ccl_global KernelLight *klight = &kernel_data_fetch(lights, isect.prim);
48 if (!is_light_shader_visible_to_path(klight->shader_id, path_visibility, path_flag)) {
49 return SHADER_EVAL_EMPTY;
50 }
51 }
52#endif
53
54 /* Evaluate light shader. */
55 Spectrum shader_eval;
57 kg, state, isect.prim, ray_P, ray_D, isect.t, ray_time, shader_eval);
58 if (eval_result == SHADER_EVAL_CACHE_MISS) {
60 }
61 const float3 eval = shader_eval * light_eval.eval_fac;
62 if (is_zero(eval)) {
63 return SHADER_EVAL_EMPTY;
64 }
65
66 /* MIS weighting. */
67 const float mis_weight = light_sample_mis_weight_forward_lamp(
68 kg, state, path_visibility, path_flag, isect.object, light_eval.pdf, ray_P);
69
70 /* Write to render buffer. */
71 guiding_record_surface_emission(kg, state, eval, mis_weight);
72 const ccl_global KernelLight *klight = &kernel_data_fetch(lights, isect.prim);
74 kg, state, eval, mis_weight, render_buffer, object_lightgroup(kg, klight->object_id));
75 return SHADER_EVAL_OK;
76}
77
78/* Evaluate light shader at intersection in forward path tracing. */
82{
84
86 if (result == SHADER_EVAL_CACHE_MISS) {
88 return;
89 }
90
91 /* TODO: we could get stuck in an infinite loop if there are precision issues
92 * and the same light is hit again.
93 *
94 * As a workaround count this as a transparent bounce. It makes some sense
95 * to interpret lights as transparent surfaces (and support making them opaque),
96 * but this needs to be revisited. */
97 const int transparent_bounce = INTEGRATOR_STATE(state, path, transparent_bounce) + 1;
98 INTEGRATOR_STATE_WRITE(state, path, transparent_bounce) = transparent_bounce;
99
100 if (transparent_bounce >= kernel_data.integrator.transparent_max_bounce) {
103 return;
104 }
105
109
110 /* TODO: in some cases we could continue directly to SHADE_BACKGROUND, but
111 * probably that optimization is probably not practical if we add lights to
112 * scene geometry. */
113}
114
116{
117 /* Read intersection and ray. */
121
122 Intersection isect = {};
123
124#ifdef __SHADOW_LINKING__
125 const bool is_shadow_linking = INTEGRATOR_STATE(state, shadow_path, flag) &
127
128 if (is_shadow_linking) {
129 /* Shadow linking stores the intersection in the shadow state,
130 * so it can be used directly. This can be any type of primitive. */
131 integrator_state_read_shadow_light_isect(state, &isect);
132 }
133 else
134#endif
135 {
136 /* NEE sampling from shade_surface only does lights and triangles. Here we
137 * have to recover the triangle UV from the ray, since the shadow state
138 * isect is used by shadow ray intersection and we want to save memory. */
139 isect.object = ray.self.light_object;
140 isect.prim = ray.self.light_prim;
141 isect.type = kernel_data_fetch(objects, isect.object).primitive_type;
142 isect.t = ray.tmax;
143
144 if (isect.type & PRIMITIVE_TRIANGLE) {
145 /* Triangles. */
146 const float2 uv = triangle_light_uv(kg, isect.object, isect.prim, ray.time, ray.P, ray.D);
147 isect.u = uv.x;
148 isect.v = uv.y;
149 }
150 else {
151 /* No NEE sampling for curves, points, etc. */
153 isect.u = 0.0f;
154 isect.v = 0.0f;
155 }
156 }
157
159 kernel_assert(isect.prim != PRIM_NONE);
160
161 float3 eval = zero_spectrum();
162 bool is_background = false;
163
164 /* Setup shader data */
165 ShaderDataCausticsStorage emission_sd_storage;
166 ccl_private ShaderData *emission_sd = AS_SHADER_DATA(&emission_sd_storage);
167
169 if (isect.type == PRIMITIVE_LAMP) {
170 /* Lights. */
171 const ccl_global KernelLight *klight = &kernel_data_fetch(lights, isect.prim);
172 const LightType light_type = LightType(klight->type);
173
174 if (light_type == LIGHT_BACKGROUND) {
175 /* Background light. */
176#ifdef __RAY_DIFFERENTIALS__
177 const float ray_dD = background_light_clamp_dD(kg, ray.dD);
178#else
179 const float ray_dD = 0.0f;
180#endif
181 shader_setup_from_background(kg, emission_sd, ray.P, ray.D, ray_dD, ray.time);
182 is_background = true;
183 }
184 else {
185 /* Other light types.
186 * Compute Ng and UV on demand so we don't have to store it in integrator state. */
187 const float3 P = (ray.tmax == FLT_MAX) ? -ray.D : ray.P + ray.tmax * ray.D;
188 float3 Ng = zero_float3();
189 float2 uv = zero_float2();
190 light_normal_uv_from_position(kg, klight, P, ray.D, Ng, uv);
191
193 emission_sd,
194 P,
195 Ng,
196 -ray.D,
197 klight->shader_id,
198 isect.object,
199 isect.prim,
200 uv.x,
201 uv.y,
202 ray.tmax,
203 ray.time,
204 false,
205 true);
206 }
207 }
208 else {
209 shader_setup_from_ray(kg, emission_sd, &ray, &isect);
210 }
211
212 /* Evaluate shader. */
213 PROFILING_SHADER(emission_sd->object, emission_sd->shader);
215
216 /* No proper path flag, we're evaluating this for all closures. that's
217 * weak but we'd have to do multiple evaluations otherwise. */
219 kg, state, emission_sd, nullptr, PATH_RAY_VISIBILITY_NONE, PATH_RAY_EMISSION);
220
221 if (emission_sd->runtime_flag & SR_CACHE_MISS) {
223 }
224
225 /* Evaluate emission closures. */
226 eval = (is_background) ? surface_shader_background(emission_sd) :
227 surface_shader_emission(emission_sd);
228
229 /* Probabilistic light termination.
230 * Light threshold is only used without light tree. */
231 if (!(kernel_data.kernel_features & KERNEL_FEATURE_LIGHT_TREE)) {
232 RNGState rng_state;
234 const float rand_terminate = path_state_rng_light_termination(kg, &rng_state);
235 const float bsdf_eval_average = INTEGRATOR_STATE(state, shadow_path, bsdf_eval_average);
236 if (light_sample_terminate(kg, eval, bsdf_eval_average, rand_terminate)) {
237 return SHADER_EVAL_EMPTY;
238 }
239 }
240 else if (is_zero(eval)) {
241 return SHADER_EVAL_EMPTY;
242 }
243
244 /* Update throughput. */
245 INTEGRATOR_STATE(state, shadow_path, throughput) *= eval;
246
247 return SHADER_EVAL_OK;
248}
249
250/* Evaluate light shader for next event estimation, after shade_surface and shade_volume and before
251 * shadow ray intersection. Only when the light has non-constant emission. */
272
#define D
ccl_device_forceinline float intersection_t_offset(const float t)
ccl_device_inline bool is_light_shader_visible_to_path(const int shader, const PathRayVisibility path_visibility, const uint32_t path_flag)
ccl_device_inline bool light_sample_terminate(KernelGlobals kg, ccl_private BsdfEval *ccl_restrict eval, const float rand_terminate)
ccl_device_inline float light_sample_mis_weight_forward_lamp(KernelGlobals kg, IntegratorState state, const PathRayVisibility path_visibility, const uint32_t path_flag, const int object_id, const float light_sample_pdf, const float3 P)
ccl_device_noinline_cpu ShaderEvalResult light_sample_shader_eval_forward(KernelGlobals kg, IntegratorState state, const int light_id, const float3 ray_P, const float3 ray_D, const float t, const float time, ccl_private Spectrum &r_eval)
#define kernel_assert(cond)
#define KERNEL_FEATURE_LIGHT_TREE
#define kernel_data
#define ccl_restrict
#define ccl_optional_struct_init
#define kernel_data_fetch(name, index)
#define AS_SHADER_DATA(shader_data_tiny_storage)
#define PRIM_NONE
#define zero_spectrum
#define OBJECT_NONE
#define ccl_private
#define ccl_device_inline
#define ccl_global
#define CCL_NAMESPACE_END
#define FLT_MAX
bool is_zero(float3x3 a)
const int state
ccl_gpu_kernel_postfix ccl_global KernelWorkTile const int ccl_global float * render_buffer
ccl_device_inline int object_lightgroup(KernelGlobals kg, const int object)
ccl_device_forceinline void guiding_record_surface_emission(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const Spectrum Le, ccl_attr_maybe_unused const float mis_weight)
ccl_device_forceinline void guiding_record_light_surface_segment(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const ccl_private Intersection *ccl_restrict isect)
CCL_NAMESPACE_BEGIN ccl_device_forceinline float background_light_clamp_dD(KernelGlobals kg, const float dD)
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 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)
uint32_t PathRayVisibility
@ SR_CACHE_MISS
@ PRIMITIVE_LAMP
@ PRIMITIVE_TRIANGLE
ShaderEvalResult
@ SHADER_EVAL_EMPTY
@ SHADER_EVAL_CACHE_MISS
@ SHADER_EVAL_OK
@ PATH_RAY_EMISSION
@ PATH_RAY_SHADOW_FOR_LIGHT_LINKING
@ PATH_RAY_VISIBILITY_NONE
ShaderData ShaderDataCausticsStorage
LightType
@ LIGHT_BACKGROUND
@ DEVICE_KERNEL_INTEGRATOR_SHADE_LIGHT_FORWARD
@ DEVICE_KERNEL_INTEGRATOR_SHADE_LIGHT_NEE
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_SHADOW
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_CLOSEST
ccl_device float2 triangle_light_uv(KernelGlobals kg, const int object, const int prim, const float time, const float3 ray_P, const float3 ray_D)
ccl_device_inline void film_write_surface_emission(KernelGlobals kg, ConstIntegratorState state, const Spectrum L, const float mis_weight, ccl_global float *ccl_restrict render_buffer, const int lightgroup=LIGHTGROUP_NONE)
CCL_NAMESPACE_BEGIN ccl_device_inline float2 zero_float2()
Definition math_float2.h:13
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
#define N
#define PROFILING_INIT_FOR_SHADER(kg, event)
#define PROFILING_EVENT(event)
#define PROFILING_SHADER(object, shader)
#define PROFILING_INIT(kg, event)
ccl_device_inline void shadow_path_state_rng_load(ConstIntegratorShadowState state, ccl_private RNGState *rng_state)
Definition path_state.h:345
ccl_device_inline float path_state_rng_light_termination(KernelGlobals kg, const ccl_private RNGState *state)
Definition path_state.h:424
@ PROFILING_SHADE_LIGHT_SETUP
Definition profiling.h:42
@ PROFILING_SHADE_LIGHT_EVAL
Definition profiling.h:43
#define ccl_device
ccl_device void integrator_shade_light_nee(KernelGlobals kg, IntegratorShadowState state, ccl_global float *ccl_restrict)
CCL_NAMESPACE_BEGIN ccl_device_inline ShaderEvalResult integrate_light_forward(KernelGlobals kg, IntegratorState state, ccl_global float *ccl_restrict render_buffer)
Definition shade_light.h:19
ccl_device void integrator_shade_light_forward(KernelGlobals kg, IntegratorState state, ccl_global float *ccl_restrict render_buffer)
Definition shade_light.h:79
ccl_device ShaderEvalResult integrate_light_nee(KernelGlobals kg, IntegratorShadowState state)
ccl_device_inline void shader_setup_from_sample(KernelGlobals kg, ccl_private ShaderData *ccl_restrict sd, const float3 P, const float3 Ng, const float3 I, const int shader, const int object, const int prim, const float u, const float v, const float t, const float time, const bool object_space, const bool is_lamp)
ccl_device_inline void shader_setup_from_ray(KernelGlobals kg, ccl_private ShaderData *ccl_restrict sd, const ccl_private Ray *ccl_restrict ray, const ccl_private Intersection *ccl_restrict isect)
Definition shader_data.h:48
ccl_device_inline void shader_setup_from_background(KernelGlobals kg, ccl_private ShaderData *ccl_restrict sd, const float3 ray_P, const float3 ray_D, const float ray_dD, const float ray_time)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING bsdf_eval_average
IntegratorShadowStateCPU * IntegratorShadowState
Definition state.h:229
#define INTEGRATOR_STATE_WRITE(state, nested_struct, member)
Definition state.h:235
#define INTEGRATOR_STATE(state, nested_struct, member)
Definition state.h:234
IntegratorStateCPU * IntegratorState
Definition state.h:227
ccl_device_forceinline void integrator_shadow_path_terminate(IntegratorShadowState state, const DeviceKernel current_kernel)
Definition state_flow.h:287
ccl_device_forceinline void integrator_shadow_path_next(IntegratorShadowState state, const DeviceKernel current_kernel, const DeviceKernel next_kernel)
Definition state_flow.h:279
ccl_device_forceinline void integrator_path_cache_miss(IntegratorState, const DeviceKernel)
Definition state_flow.h:238
ccl_device_forceinline void integrator_path_next(IntegratorState state, const DeviceKernel current_kernel, const DeviceKernel next_kernel)
Definition state_flow.h:219
ccl_device_forceinline void integrator_path_terminate(KernelGlobals kg, IntegratorState state, ccl_global float *ccl_restrict render_buffer, const DeviceKernel current_kernel)
Definition state_flow.h:227
ccl_device_forceinline void integrator_shadow_path_cache_miss(IntegratorShadowState, const DeviceKernel)
Definition state_flow.h:294
ccl_device_forceinline void integrator_state_read_shadow_ray(ConstIntegratorShadowState state, ccl_private Ray *ccl_restrict ray)
Definition state_util.h:86
ccl_device_forceinline void integrator_state_read_isect(ConstIntegratorState state, ccl_private Intersection *ccl_restrict isect)
Definition state_util.h:179
ccl_device_forceinline void integrator_state_read_shadow_ray_self(ConstIntegratorShadowState state, ccl_private Ray *ccl_restrict ray)
Definition state_util.h:136
float x
float y
ccl_device void surface_shader_eval(KernelGlobals kg, ConstIntegratorGenericState state, ccl_private ShaderData *ccl_restrict sd, ccl_global float *ccl_restrict buffer, const PathRayVisibility path_visibility, const uint32_t path_flag, bool use_caustics_storage=false)
ccl_device Spectrum surface_shader_background(const ccl_private ShaderData *sd)
ccl_device Spectrum surface_shader_emission(const ccl_private ShaderData *sd)
float3 Spectrum