Blender V5.3
shade_dedicated_light.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2023 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
10#include "kernel/light/sun.h"
11
13
15
16#ifdef __SHADOW_LINKING__
17
19shadow_linking_light_eval_from_intersection(KernelGlobals kg,
21 const ccl_private Ray &ccl_restrict ray,
22 const float3 N,
23 const uint32_t path_flag)
24{
25 const ccl_global KernelLight *klight = &kernel_data_fetch(lights, isect.prim);
26 const LightType type = LightType(klight->type);
27
28 return (type == LIGHT_SUN) ?
30 light_eval_from_intersection(kg, &isect, ray.P, ray.D, N, path_flag);
31}
32
33ccl_device_inline float shadow_linking_light_sample_mis_weight(
36 const PathRayVisibility path_visibility,
37 const uint32_t path_flag,
38 const int light_id,
39 const int object_id,
40 const float light_sample_pdf,
41 const float3 P)
42{
43 if (kernel_data_fetch(lights, light_id).type == LIGHT_SUN) {
45 kg, state, path_visibility, path_flag, object_id, light_sample_pdf);
46 }
47
49 kg, state, path_visibility, path_flag, object_id, light_sample_pdf, P);
50}
51
52/* Setup ray for the shadow path.
53 * Expects that the current state of the ray is the one calculated by the surface bounce, and the
54 * intersection corresponds to a point on an emitter. */
55ccl_device void shadow_linking_setup_ray_from_intersection(
59{
60 /* The ray->tmin follows the value configured at the surface bounce.
61 * it is the same for the continued main path and for this shadow ray. There is no need to push
62 * it forward here. */
63
64 ray->tmax = isect->t;
65
66 /* Use the same self intersection primitives as the main path.
67 * Those are copied to the dedicated storage from the main intersection after the surface bounce,
68 * but before the main intersection is re-used to find light to trace a ray to. */
69 ray->self.object = INTEGRATOR_STATE(state, shadow_link, last_isect_object);
70 ray->self.prim = INTEGRATOR_STATE(state, shadow_link, last_isect_prim);
71
72 ray->self.light_object = isect->object;
73 ray->self.light_prim = isect->prim;
74}
75
76ccl_device bool shadow_linking_shade_light(KernelGlobals kg,
80 ccl_private float &ccl_restrict light_weight,
81 ccl_private float &mis_weight,
82 ccl_private int &ccl_restrict light_group,
83 ccl_private int &ccl_restrict shader_id)
84{
85 const PathRayVisibility path_visibility = INTEGRATOR_STATE(state, path, visibility);
86 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
87 const float3 N = INTEGRATOR_STATE(state, path, mis_origin_n);
88 const LightEval light_eval = shadow_linking_light_eval_from_intersection(
89 kg, isect, ray, N, path_flag);
90 if (light_eval.eval_fac == 0.0f) {
91 /* No light to be sampled, so no direct light contribution either. */
92 return SHADER_EVAL_EMPTY;
93 }
94
95 const ccl_global KernelLight *klight = &kernel_data_fetch(lights, isect.prim);
96
97 if (!is_light_shader_visible_to_path(klight->shader_id, path_visibility, path_flag)) {
98 return false;
99 }
100
101 /* MIS weighting. */
102 mis_weight = shadow_linking_light_sample_mis_weight(
103 kg, state, path_visibility, path_flag, isect.prim, isect.object, light_eval.pdf, ray.P);
104
105 light_weight = light_eval.eval_fac * mis_weight *
106 INTEGRATOR_STATE(state, shadow_link, dedicated_light_weight);
107 light_group = object_lightgroup(kg, klight->object_id);
108 shader_id = klight->shader_id;
109
110 return SHADER_EVAL_OK;
111}
112
113ccl_device bool shadow_linking_shade_surface_emission(KernelGlobals kg,
115 ccl_private float &ccl_restrict light_weight,
116 ccl_private float &mis_weight,
117 ccl_private int &ccl_restrict light_group,
118 ccl_private int &ccl_restrict shader_id)
119{
120 ShaderDataTinyStorage emission_sd_storage;
121 ccl_private ShaderData *emission_sd = AS_SHADER_DATA(&emission_sd_storage);
122
123 const PathRayVisibility path_visibility = INTEGRATOR_STATE(state, path, visibility);
124 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
125
126 integrate_surface_shader_setup(kg, state, emission_sd);
127
128# ifdef __VOLUME__
129 if (emission_sd->shader_flag & SD_HAS_ONLY_VOLUME) {
130 return SHADER_EVAL_EMPTY;
131 }
132# endif
133
135 kg, state, path_visibility, path_flag, emission_sd);
136
137 light_weight = mis_weight * INTEGRATOR_STATE(state, shadow_link, dedicated_light_weight);
138 light_group = object_lightgroup(kg, emission_sd->object);
139 shader_id = emission_sd->shader;
140
141 return SHADER_EVAL_OK;
142}
143
144ccl_device void shadow_linking_shade(KernelGlobals kg, IntegratorState state)
145{
146 /* Read intersection from integrator state into local memory. */
149
150 /* Read ray from integrator state into local memory. */
153
154 float light_weight = 0.0f;
155 float mis_weight = 1.0f;
156 int light_group = LIGHTGROUP_NONE;
157 int shader_id = SHADER_NONE;
158
159 if (isect.type == PRIMITIVE_LAMP) {
160 if (!shadow_linking_shade_light(
161 kg, state, ray, isect, light_weight, mis_weight, light_group, shader_id))
162 {
163 return;
164 }
165 }
166 else {
167 if (!shadow_linking_shade_surface_emission(
168 kg, state, light_weight, mis_weight, light_group, shader_id))
169 {
170 return;
171 }
172 }
173
174 /* Evaluate constant part of light shader, rest will optionally be done in another kernel. */
175 Spectrum light_eval;
176 const bool is_constant_light_shader = light_sample_shader_eval_nee_constant(
177 kg, shader_id, isect.prim, isect.type == PRIMITIVE_LAMP, light_eval);
178 light_eval *= light_weight;
179
180 if (is_zero(light_eval)) {
181 return;
182 }
183
184 shadow_linking_setup_ray_from_intersection(state, &ray, &isect);
185
186 /* Branch off shadow kernel. */
188 kg, state, &ray, light_eval, light_group, 0, is_constant_light_shader);
189
190 if (!is_constant_light_shader) {
191 integrator_state_write_shadow_light_isect(shadow_state, &isect);
192 }
193
194 /* The light is accumulated from the shade_surface kernel, which will make the clamping decision
195 * based on the actual value of the bounce. For the dedicated shadow ray we want to follow the
196 * main path clamping rules, which subtracts one from the bounds before accumulation. */
198 shadow_state, shadow_path, bounce) = INTEGRATOR_STATE(shadow_state, shadow_path, bounce) - 1;
199
200 /* No need to update the volume stack as the surface bounce already performed enter-exit check.
201 */
202
203 const uint32_t shadow_flag = INTEGRATOR_STATE(state, path, flag);
204
205 if (kernel_data.kernel_features & KERNEL_FEATURE_LIGHT_PASSES) {
206 /* The diffuse and glossy pass weights are written into the main path as part of the path
207 * configuration at a surface bounce. */
208 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, pass_diffuse_weight) = INTEGRATOR_STATE(
210 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, pass_glossy_weight) = INTEGRATOR_STATE(
211 state, path, pass_glossy_weight);
212 }
213
214 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, visibility) = INTEGRATOR_STATE(
215 state, path, visibility);
216 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, flag) = shadow_flag |
218
219# if defined(__PATH_GUIDING__)
220 if (kernel_data.integrator.train_guiding) {
221 INTEGRATOR_STATE(shadow_state, shadow_path, guiding_light_linking_mis_weight) = mis_weight;
222 }
223# endif
224}
225
226#endif /* __SHADOW_LINKING__ */
227
230 ccl_global float *ccl_restrict /*render_buffer*/)
231{
233
234#ifdef __SHADOW_LINKING__
235 shadow_linking_shade(kg, state);
236
237 /* Restore self-intersection check primitives in the main state before returning to the
238 * intersect_closest() state. */
239 shadow_linking_restore_last_primitives(state);
240#else
241 kernel_assert(!"integrator_intersect_dedicated_light is not supposed to be scheduled");
242#endif
243
245}
246
#define D
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 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_NAMESPACE_BEGIN ccl_device bool light_sample_shader_eval_nee_constant(KernelGlobals kg, const int shader_id, const int prim, const bool is_light, ccl_private Spectrum &eval)
ccl_device_inline float light_sample_mis_weight_forward_surface(KernelGlobals kg, IntegratorState state, const PathRayVisibility path_visibility, const uint32_t path_flag, const ccl_private ShaderData *sd)
ccl_device_inline float light_sample_mis_weight_forward_distant(KernelGlobals kg, IntegratorState state, const PathRayVisibility path_visibility, const uint32_t path_flag, const int object_id, const float light_sample_pdf)
#define kernel_assert(cond)
#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 SHADER_NONE
#define ccl_private
#define ccl_device_inline
#define KERNEL_FEATURE_LIGHT_PASSES
#define LIGHTGROUP_NONE
#define ccl_global
#define CCL_NAMESPACE_END
bool is_zero(float3x3 a)
const int state
ccl_device_inline int object_lightgroup(KernelGlobals kg, const int object)
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
@ SD_HAS_ONLY_VOLUME
@ PRIMITIVE_LAMP
ShaderData ShaderDataTinyStorage
@ SHADER_EVAL_EMPTY
@ SHADER_EVAL_OK
@ PATH_RAY_SHADOW_FOR_LIGHT_LINKING
LightType
@ LIGHT_SUN
#define N
#define PROFILING_INIT(kg, event)
unsigned int uint32_t
Definition BKE_text.h:81
@ PROFILING_SHADE_DEDICATED_LIGHT
Definition profiling.h:31
#define ccl_device
CCL_NAMESPACE_BEGIN ccl_device void integrator_shade_dedicated_light(KernelGlobals kg, IntegratorState state, ccl_global float *ccl_restrict)
ccl_device_inline IntegratorShadowState integrate_direct_light_shadow_init_common(KernelGlobals kg, IntegratorState state, const ccl_private Ray *ccl_restrict ray, const Spectrum bsdf_spectrum, const int light_group, const int mnee_vertex_count, const bool constant_light_shader)
ccl_device_forceinline void integrator_shade_surface_next_kernel(IntegratorState state)
CCL_NAMESPACE_BEGIN ccl_device_forceinline void integrate_surface_shader_setup(KernelGlobals kg, ConstIntegratorState state, ccl_private ShaderData *sd)
pass_diffuse_weight
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_state_read_ray(ConstIntegratorState state, ccl_private Ray *ccl_restrict ray)
Definition state_util.h:49
ccl_device_forceinline void integrator_state_read_isect(ConstIntegratorState state, ccl_private Intersection *ccl_restrict isect)
Definition state_util.h:179
ccl_device LightEval sun_light_eval_from_intersection(const ccl_global KernelLight *klight, const float3 ray_D)
Definition sun.h:76
float3 Spectrum