Blender V5.3
intersect_closest.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
12
13#include "kernel/light/light.h"
14
15#include "kernel/bvh/bvh.h"
16
18
21{
22 /* When direct lighting is disabled for baking, we skip light sampling in
23 * integrate_surface_direct_light for the first bounce. Therefore, in order
24 * for MIS to be consistent, we also need to skip evaluating lights here. */
25 return (kernel_data.integrator.filter_closures & FILTER_CLOSURE_DIRECT_LIGHT) &&
26 (INTEGRATOR_STATE(state, path, bounce) == 1);
27}
28
31 const int shader_flags)
32{
33
34 /* Optional AO bounce termination.
35 * We continue evaluating emissive/transparent surfaces and volumes, similar
36 * to direct lighting. Only if we know there are none can we terminate the
37 * path immediately. */
38 if (path_state_ao_bounce(kg, state)) {
39 if (shader_flags & (SD_HAS_TRANSPARENT_SHADOW | SD_HAS_EMISSION)) {
41 }
42#ifdef __VOLUME__
43 else if (!integrator_state_volume_stack_is_empty(kg, state)) {
45 }
46#endif
47 else {
48 return true;
49 }
50 }
51
52 /* Load random number state. */
53 RNGState rng_state;
54 path_state_rng_load(state, &rng_state);
55
56 /* We perform path termination in this kernel to avoid launching shade_surface
57 * and evaluating the shader when not needed. Only for emission and transparent
58 * surfaces in front of emission do we need to evaluate the shader, since we
59 * perform MIS as part of indirect rays. */
60 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
61 const float continuation_probability = path_state_continuation_probability(kg, state, path_flag);
62 INTEGRATOR_STATE_WRITE(state, path, continuation_probability) = continuation_probability;
63
64 guiding_record_continuation_probability(kg, state, continuation_probability);
65
66 if (continuation_probability != 1.0f) {
67 const float terminate = path_state_rng_1D(kg, &rng_state, PRNG_TERMINATE);
68
69 if (continuation_probability == 0.0f || terminate >= continuation_probability) {
70 if (shader_flags & SD_HAS_EMISSION) {
71 /* Mark path to be terminated right after shader evaluation on the surface. */
73 }
74#ifdef __VOLUME__
75 else if (!integrator_state_volume_stack_is_empty(kg, state)) {
76 /* TODO: only do this for emissive volumes. */
78 }
79#endif
80 else {
81 return true;
82 }
83 }
84 }
85
86 return false;
87}
88
89#ifdef __SHADOW_CATCHER__
90/* Split path if a shadow catcher was hit. */
91ccl_device_forceinline void integrator_split_shadow_catcher(
96{
97 /* Test if we hit a shadow catcher object, and potentially split the path to continue tracing two
98 * paths from here. */
99 const uint object_flags = intersection_get_object_flags(kg, isect);
100 if (!kernel_shadow_catcher_is_path_split_bounce(kg, state, object_flags)) {
101 return;
102 }
103
104 film_write_shadow_catcher_bounce_data(kg, state, render_buffer);
105
106 /* Mark state as having done a shadow catcher split so that it stops contributing to
107 * the shadow catcher matte pass, but keeps contributing to the combined pass. */
109
110 /* Copy current state to new state. */
112
113 /* Initialize new state.
114 *
115 * Note that the splitting leaves kernel and sorting counters as-is, so use INIT semantic for
116 * the matte path. */
117
118 /* Mark current state so that it will only track contribution of shadow catcher objects ignoring
119 * non-catcher objects. */
121
122 /* Shadow catcher path does not use guiding.
123 * Clear the path_segment to ensure we do not reference possibly stale data from the main path.
124 */
125# ifdef __PATH_GUIDING__
126 INTEGRATOR_STATE_WRITE(state, guiding, path_segment) = nullptr;
127# endif
128
129 if (kernel_data.film.pass_background != PASS_UNUSED && !kernel_data.background.transparent) {
130 /* If using background pass, schedule background shading kernel so that we have a background
131 * to alpha-over on. The background kernel will then continue the path afterwards. */
134 return;
135 }
136
137# ifdef __VOLUME__
138 if (!integrator_state_volume_stack_is_empty(kg, state)) {
139 /* Volume stack is not empty. Re-init the volume stack to exclude any non-shadow catcher
140 * objects from it, and then continue shading volume and shadow catcher surface after. */
142 return;
143 }
144# endif
145
146 /* Continue with shading shadow catcher surface. */
147 const int shader = intersection_get_shader(kg, isect);
148 const int flags = kernel_data_fetch(shaders, shader).flags;
149 const bool use_caustics = kernel_data.integrator.use_caustics &&
150 (object_flags & SD_OBJECT_CAUSTICS_RECEIVER);
151 const bool use_raytrace_kernel = (flags & SD_HAS_RAYTRACE);
152
153 if (use_caustics) {
155 }
156 else if (use_raytrace_kernel) {
159 }
160 else {
162 }
163}
164
165/* Schedule next kernel to be executed after updating volume stack for shadow catcher. */
166template<DeviceKernel current_kernel>
167ccl_device_forceinline void integrator_intersect_next_kernel_after_shadow_catcher_volume(
169{
170 /* Continue with shading shadow catcher surface. Same as integrator_split_shadow_catcher, but
171 * using NEXT instead of INIT. */
174
175 const int shader = intersection_get_shader(kg, &isect);
176 const int shader_flags = kernel_data_fetch(shaders, shader).flags;
177 const uint object_flags = intersection_get_object_flags(kg, &isect);
178 const bool use_caustics = kernel_data.integrator.use_caustics &&
179 (object_flags & SD_OBJECT_CAUSTICS_RECEIVER);
180 const bool use_raytrace_kernel = (shader_flags & SD_HAS_RAYTRACE);
181
182 if (use_caustics) {
184 }
185 else if (use_raytrace_kernel) {
187 kg, state, current_kernel, DEVICE_KERNEL_INTEGRATOR_SHADE_SURFACE_RAYTRACE, shader);
188 }
189 else {
191 kg, state, current_kernel, DEVICE_KERNEL_INTEGRATOR_SHADE_SURFACE, shader);
192 }
193}
194
195/* Schedule next kernel to be executed after executing background shader for shadow catcher. */
196template<DeviceKernel current_kernel>
197ccl_device_forceinline void integrator_intersect_next_kernel_after_shadow_catcher_background(
199{
200# ifdef __VOLUME__
201 /* Same logic as integrator_split_shadow_catcher, but using NEXT instead of INIT. */
202 if (!integrator_state_volume_stack_is_empty(kg, state)) {
203 /* Volume stack is not empty. Re-init the volume stack to exclude any non-shadow catcher
204 * objects from it, and then continue shading volume and shadow catcher surface after. */
206 return;
207 }
208# endif
209
210 /* Continue with shading shadow catcher surface. */
211 integrator_intersect_next_kernel_after_shadow_catcher_volume<current_kernel>(kg, state);
212}
213#endif
214
215/* Schedule next kernel to be executed after intersect closest.
216 *
217 * Note that current_kernel is a template value since making this a variable
218 * leads to poor performance with CUDA atomics. */
219template<DeviceKernel current_kernel>
221 KernelGlobals kg,
225 const bool hit)
226{
227 /* Continue with volume kernel if we are inside a volume, regardless if we hit anything. */
228#ifdef __VOLUME__
229 if (!integrator_state_volume_stack_is_empty(kg, state)) {
230 const bool hit_surface = hit && !(isect->type & PRIMITIVE_LAMP);
231 const int shader = (hit_surface) ? intersection_get_shader(kg, isect) : SHADER_NONE;
232 const int flags = (hit_surface) ? kernel_data_fetch(shaders, shader).flags : 0;
233
234 if (!integrator_intersect_terminate(kg, state, flags)) {
235 if (kernel_data.integrator.volume_ray_marching) {
238 }
239 else {
241 }
242 }
243 else {
244 integrator_path_terminate(kg, state, render_buffer, current_kernel);
245 }
246 return;
247 }
248#endif
249
250 if (hit) {
251 /* Hit a surface, continue with light or surface kernel. */
252 if (isect->type & PRIMITIVE_LAMP) {
254 }
255 else {
256 /* Hit a surface, continue with surface kernel unless terminated. */
257 const int shader = intersection_get_shader(kg, isect);
258 const int flags = kernel_data_fetch(shaders, shader).flags;
259
260 if (!integrator_intersect_terminate(kg, state, flags)) {
261 const uint object_flags = intersection_get_object_flags(kg, isect);
262 const bool use_caustics = kernel_data.integrator.use_caustics &&
263 (object_flags & SD_OBJECT_CAUSTICS_RECEIVER);
264 const bool use_raytrace_kernel = (flags & SD_HAS_RAYTRACE);
265 if (use_caustics) {
267 }
268 else if (use_raytrace_kernel) {
270 kg, state, current_kernel, DEVICE_KERNEL_INTEGRATOR_SHADE_SURFACE_RAYTRACE, shader);
271 }
272 else {
274 kg, state, current_kernel, DEVICE_KERNEL_INTEGRATOR_SHADE_SURFACE, shader);
275 }
276
277#ifdef __SHADOW_CATCHER__
278 /* Handle shadow catcher. */
279 integrator_split_shadow_catcher(kg, state, isect, render_buffer);
280#endif
281 }
282 else {
283 integrator_path_terminate(kg, state, render_buffer, current_kernel);
284 }
285 }
286 }
287 else {
288 /* Nothing hit, continue with background kernel. */
290 integrator_path_terminate(kg, state, render_buffer, current_kernel);
291 }
292 else {
294 }
295 }
296}
297
298/* Schedule next kernel to be executed after shade volume.
299 *
300 * The logic here matches integrator_intersect_next_kernel, except that
301 * volume shading and termination testing have already been done. */
302template<DeviceKernel current_kernel>
304 KernelGlobals kg,
308{
309 if (isect->prim != PRIM_NONE) {
310 /* Hit a surface, continue with light or surface kernel. */
311 if (isect->type & PRIMITIVE_LAMP) {
313 return;
314 }
315
316 /* Hit a surface, continue with surface kernel unless terminated. */
317 const int shader = intersection_get_shader(kg, isect);
318 const int shader_flags = kernel_data_fetch(shaders, shader).flags;
319 const uint object_flags = intersection_get_object_flags(kg, isect);
320 const bool use_caustics = kernel_data.integrator.use_caustics &&
321 (object_flags & SD_OBJECT_CAUSTICS_RECEIVER);
322 const bool use_raytrace_kernel = (shader_flags & SD_HAS_RAYTRACE);
323
324 if (use_caustics) {
326 }
327 else if (use_raytrace_kernel) {
329 kg, state, current_kernel, DEVICE_KERNEL_INTEGRATOR_SHADE_SURFACE_RAYTRACE, shader);
330 }
331 else {
333 kg, state, current_kernel, DEVICE_KERNEL_INTEGRATOR_SHADE_SURFACE, shader);
334 }
335
336#ifdef __SHADOW_CATCHER__
337 /* Handle shadow catcher. */
338 integrator_split_shadow_catcher(kg, state, isect, render_buffer);
339#endif
340 return;
341 }
342 /* Nothing hit, continue with background kernel. */
344 integrator_path_terminate(kg, state, render_buffer, current_kernel);
345 }
346 else {
348 }
349}
350
354{
356
357 /* Read ray from integrator state into local memory. */
360 kernel_assert(ray.tmax != 0.0f);
361
362 const uint visibility = path_state_ray_visibility(state);
363 const int last_isect_prim = INTEGRATOR_STATE(state, isect, prim);
364 const int last_isect_object = INTEGRATOR_STATE(state, isect, object);
365
366 /* Trick to use short AO rays to approximate indirect light at the end of the path. */
367 if (path_state_ao_bounce(kg, state)) {
368 ray.tmax = kernel_data.integrator.ao_bounces_distance;
369
370 if (last_isect_object != OBJECT_NONE) {
371 const float object_ao_distance = kernel_data_fetch(objects, last_isect_object).ao_distance;
372 if (object_ao_distance != 0.0f) {
373 ray.tmax = object_ao_distance;
374 }
375 }
376 }
377
378 /* Scene Intersection. */
380 isect.object = OBJECT_NONE;
381 isect.prim = PRIM_NONE;
382 ray.self.object = last_isect_object;
383 ray.self.prim = last_isect_prim;
384 ray.self.light_object = OBJECT_NONE;
385 ray.self.light_prim = PRIM_NONE;
386 bool hit = scene_intersect(kg, &ray, visibility, &isect);
387
388 /* TODO: remove this and do it in the various intersection functions instead. */
389 if (!hit) {
390 isect.prim = PRIM_NONE;
391 }
392
393 /* Setup mnee flag to signal last intersection with a caster */
394 const PathRayVisibility path_visibility = INTEGRATOR_STATE(state, path, visibility);
395 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
396
397#ifdef __MNEE__
398 /* Path culling logic for MNEE (removes fireflies at the cost of bias) */
399 if (kernel_data.integrator.use_caustics) {
400 /* The following firefly removal mechanism works by culling light connections when
401 * a ray comes from a caustic caster directly after bouncing off a different caustic
402 * receiver */
403 bool from_caustic_caster = false;
404 bool from_caustic_receiver = false;
405 if (!(path_visibility & PATH_RAY_VISIBILITY_CAMERA) && last_isect_object != OBJECT_NONE) {
406 const uint object_flags = kernel_data_fetch(object_flag, last_isect_object);
407 from_caustic_receiver = (object_flags & SD_OBJECT_CAUSTICS_RECEIVER);
408 from_caustic_caster = (object_flags & SD_OBJECT_CAUSTICS_CASTER);
409 }
410
411 const bool has_receiver_ancestor = INTEGRATOR_STATE(state, path, mnee) &
414 if (from_caustic_caster && has_receiver_ancestor) {
416 }
417 if (from_caustic_receiver) {
419 }
420 }
421#endif /* __MNEE__ */
422
423 /* Light intersection for MIS. */
424 if (kernel_data.integrator.use_light_mis && !integrator_intersect_skip_lights(kg, state)) {
425 /* NOTE: if we make lights visible to camera rays, we'll need to initialize
426 * these in the path_state_init. */
427 const int last_type = INTEGRATOR_STATE(state, isect, type);
428 hit = lights_intersect(kg,
429 state,
430 &ray,
431 &isect,
432 last_isect_prim,
433 last_isect_object,
434 last_type,
435 path_visibility,
436 path_flag) ||
437 hit;
438 }
439
440 /* Write intersection result into global integrator state memory. */
442
443 /* Setup up next kernel to be executed. */
445 kg, state, &isect, render_buffer, hit);
446}
447
ccl_device_forceinline uint intersection_get_object_flags(KernelGlobals kg, const ccl_private Intersection *ccl_restrict isect)
ccl_device_forceinline int intersection_get_shader(KernelGlobals kg, const ccl_private Intersection *ccl_restrict isect)
#define kernel_assert(cond)
#define kernel_data
#define PASS_UNUSED
#define ccl_restrict
#define ccl_device_forceinline
#define ccl_optional_struct_init
#define kernel_data_fetch(name, index)
#define SHADER_NONE
#define PRIM_NONE
#define OBJECT_NONE
#define ccl_private
#define ccl_global
#define CCL_NAMESPACE_END
ccl_device_forceinline void integrator_intersect_next_kernel_after_volume(KernelGlobals kg, IntegratorState state, const ccl_private Intersection *ccl_restrict isect, ccl_global float *ccl_restrict render_buffer)
CCL_NAMESPACE_BEGIN ccl_device_forceinline bool integrator_intersect_skip_lights(KernelGlobals kg, IntegratorState state)
ccl_device void integrator_intersect_closest(KernelGlobals kg, IntegratorState state, ccl_global float *ccl_restrict render_buffer)
ccl_device_forceinline bool integrator_intersect_terminate(KernelGlobals kg, IntegratorState state, const int shader_flags)
ccl_device_forceinline void integrator_intersect_next_kernel(KernelGlobals kg, IntegratorState state, const ccl_private Intersection *ccl_restrict isect, ccl_global float *ccl_restrict render_buffer, const bool hit)
ccl_device_intersect bool scene_intersect(KernelGlobals kg, const ccl_private Ray *ray, const uint visibility, ccl_private Intersection *isect)
const int state
ccl_gpu_kernel_postfix ccl_global KernelWorkTile const int ccl_global float * render_buffer
ccl_device_forceinline void guiding_record_continuation_probability(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const float continuation_probability)
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)
uint32_t PathRayVisibility
@ PATH_MNEE_RECEIVER_ANCESTOR
@ PATH_MNEE_CULL_LIGHT_CONNECTION
@ FILTER_CLOSURE_DIRECT_LIGHT
@ SD_HAS_TRANSPARENT_SHADOW
@ SD_HAS_EMISSION
@ SD_HAS_RAYTRACE
@ PRIMITIVE_LAMP
@ PRNG_TERMINATE
@ PATH_RAY_SHADOW_CATCHER_HIT
@ PATH_RAY_TERMINATE_AFTER_TRANSPARENT
@ PATH_RAY_TERMINATE_AFTER_VOLUME
@ PATH_RAY_SHADOW_CATCHER_PASS
@ PATH_RAY_TERMINATE_IN_NEXT_VOLUME
@ PATH_RAY_SHADOW_CATCHER_BACKGROUND
@ PATH_RAY_TERMINATE_ON_NEXT_SURFACE
@ PATH_RAY_VISIBILITY_CAMERA
@ SD_OBJECT_CAUSTICS_RECEIVER
@ SD_OBJECT_CAUSTICS_CASTER
@ DEVICE_KERNEL_INTEGRATOR_SHADE_SURFACE
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_MNEE
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_VOLUME_STACK
@ DEVICE_KERNEL_INTEGRATOR_SHADE_SURFACE_RAYTRACE
@ DEVICE_KERNEL_INTEGRATOR_SHADE_LIGHT_FORWARD
@ DEVICE_KERNEL_INTEGRATOR_SHADE_VOLUME_RAY_MARCHING
@ DEVICE_KERNEL_INTEGRATOR_SHADE_VOLUME
@ DEVICE_KERNEL_INTEGRATOR_SHADE_BACKGROUND
#define PROFILING_INIT(kg, event)
unsigned int uint
Definition BKE_text.h:55
ccl_device_inline float path_state_continuation_probability(KernelGlobals kg, ConstIntegratorState state, const uint32_t path_flag)
Definition path_state.h:279
ccl_device_inline bool path_state_ao_bounce(KernelGlobals kg, ConstIntegratorState state)
Definition path_state.h:309
ccl_device_inline float path_state_rng_1D(KernelGlobals kg, const ccl_private RNGState *rng_state, const int dimension)
Definition path_state.h:361
ccl_device_inline void path_state_rng_load(ConstIntegratorState state, ccl_private RNGState *rng_state)
Definition path_state.h:337
ccl_device_inline PathRayVisibility path_state_ray_visibility(ConstIntegratorState state)
Definition path_state.h:264
@ PROFILING_INTERSECT_CLOSEST
Definition profiling.h:19
#define ccl_device
CCL_NAMESPACE_BEGIN ccl_device_inline bool kernel_shadow_catcher_is_path_split_bounce(KernelGlobals kg, IntegratorState state, const uint object_flag)
#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_path_next_sorted(KernelGlobals, IntegratorState state, const DeviceKernel current_kernel, const DeviceKernel next_kernel, const uint32_t key)
Definition state_flow.h:250
ccl_device_forceinline void integrator_path_init_sorted(KernelGlobals, IntegratorState state, const DeviceKernel next_kernel, const uint32_t key)
Definition state_flow.h:210
ccl_device_forceinline void integrator_path_init(IntegratorState state, const DeviceKernel next_kernel)
Definition state_flow.h:204
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_state_read_ray(ConstIntegratorState state, ccl_private Ray *ccl_restrict ray)
Definition state_util.h:49
ccl_device_inline IntegratorState integrator_state_shadow_catcher_split(KernelGlobals kg, IntegratorState state)
Definition state_util.h:634
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_write_isect(IntegratorState state, const ccl_private Intersection *ccl_restrict isect)
Definition state_util.h:149