Blender V4.5
intersect_shadow.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
7#include "kernel/globals.h"
8#include "kernel/types.h"
9
10#include "kernel/bvh/bvh.h"
11
15
17
18/* Visibility for the shadow ray. */
21{
22 uint visibility = PATH_RAY_SHADOW;
23
24#ifdef __SHADOW_CATCHER__
25 const uint32_t path_flag = INTEGRATOR_STATE(state, shadow_path, flag);
26 visibility = SHADOW_CATCHER_PATH_VISIBILITY(path_flag, visibility);
27#endif
28
29 return visibility;
30}
31
34 const ccl_private Ray *ray,
35 const uint visibility)
36{
37 /* Mask which will pick only opaque visibility bits from the `visibility`.
38 * Calculate the mask at compile time: the visibility will either be a high bits for the shadow
39 * catcher objects, or lower bits for the regular objects (there is no need to check the path
40 * state here again). */
41 constexpr const uint opaque_mask = SHADOW_CATCHER_VISIBILITY_SHIFT(PATH_RAY_SHADOW_OPAQUE) |
43
44 const bool opaque_hit = scene_intersect_shadow(kg, ray, visibility & opaque_mask);
45
46 /* Only record the number of hits if nothing was hit, so that the shadow shading kernel does not
47 * consider any intersections. There is no need to write anything to the state if the hit is
48 * opaque because in this case the path is terminated. */
49 if (!opaque_hit) {
50 INTEGRATOR_STATE_WRITE(state, shadow_path, num_hits) = 0;
51 }
52
53 return opaque_hit;
54}
55
58{
59 const int transparent_max_bounce = kernel_data.integrator.transparent_max_bounce;
60 const int transparent_bounce = INTEGRATOR_STATE(state, shadow_path, transparent_bounce);
61
62 return max(transparent_max_bounce - transparent_bounce - 1, 0);
63}
64
65#ifdef __TRANSPARENT_SHADOWS__
66# ifndef __KERNEL_GPU__
67ccl_device int shadow_intersections_compare(const void *a, const void *b)
68{
69 const Intersection *isect_a = (const Intersection *)a;
70 const Intersection *isect_b = (const Intersection *)b;
71
72 if (isect_a->t < isect_b->t) {
73 return -1;
74 }
75 if (isect_a->t > isect_b->t) {
76 return 1;
77 }
78 return 0;
79}
80# endif
81
82ccl_device_inline void sort_shadow_intersections(IntegratorShadowState state, uint num_hits)
83{
84 kernel_assert(num_hits > 0);
85
86# ifdef __KERNEL_GPU__
87 /* Use bubble sort which has more friendly memory pattern on GPU. */
88 bool swapped;
89 do {
90 swapped = false;
91 for (int j = 0; j < num_hits - 1; ++j) {
92 if (INTEGRATOR_STATE_ARRAY(state, shadow_isect, j, t) >
93 INTEGRATOR_STATE_ARRAY(state, shadow_isect, j + 1, t))
94 {
101 swapped = true;
102 }
103 }
104 --num_hits;
105 } while (swapped);
106# else
107 Intersection *isect_array = (Intersection *)state->shadow_isect;
108 qsort(isect_array, num_hits, sizeof(Intersection), shadow_intersections_compare);
109# endif
110}
111
112ccl_device bool integrate_intersect_shadow_transparent(KernelGlobals kg,
114 const ccl_private Ray *ray,
115 const uint visibility)
116{
117 /* Limit the number hits to the max transparent bounces allowed and the size that we
118 * have available in the integrator state. */
120 uint num_hits = 0;
121 float throughput = 1.0f;
122 bool opaque_hit = scene_intersect_shadow_all(
123 kg, state, ray, visibility, max_hits, &num_hits, &throughput);
124
125 /* Computed throughput from baked shadow transparency, where we can bypass recording
126 * intersections and shader evaluation. */
127 if (throughput != 1.0f) {
128 INTEGRATOR_STATE_WRITE(state, shadow_path, throughput) *= throughput;
129 }
130
131 /* If number of hits exceed the transparent bounces limit, make opaque. */
132 if (num_hits > max_hits) {
133 opaque_hit = true;
134 }
135
136 if (!opaque_hit) {
137 const uint num_recorded_hits = min(num_hits,
139
140 if (num_recorded_hits > 0) {
141 sort_shadow_intersections(state, num_recorded_hits);
142 }
143
144 INTEGRATOR_STATE_WRITE(state, shadow_path, num_hits) = num_hits;
145 }
146 else {
147 INTEGRATOR_STATE_WRITE(state, shadow_path, num_hits) = 0;
148 }
149
150 return opaque_hit;
151}
152#endif
153
155{
157
158 /* Read ray from integrator state into local memory. */
162 /* Compute visibility. */
163 const uint visibility = integrate_intersect_shadow_visibility(kg, state);
164
165#ifdef __TRANSPARENT_SHADOWS__
166 /* TODO: compile different kernels depending on this? Especially for OptiX
167 * conditional trace calls are bad. */
168 const bool opaque_hit = (kernel_data.integrator.transparent_shadows) ?
169 integrate_intersect_shadow_transparent(kg, state, &ray, visibility) :
170 integrate_intersect_shadow_opaque(kg, state, &ray, visibility);
171#else
172 const bool opaque_hit = integrate_intersect_shadow_opaque(kg, state, &ray, visibility);
173#endif
174
175 if (opaque_hit) {
176 /* Hit an opaque surface, shadow path ends here. */
178 return;
179 }
180
181 /* Hit nothing or transparent surfaces, continue to shadow kernel
182 * for shading and render buffer output.
183 *
184 * TODO: could also write to render buffer directly if no transparent shadows?
185 * Could save a kernel execution for the common case. */
188}
189
unsigned int uint
#define kernel_assert(cond)
#define kernel_data
#define ccl_device_forceinline
#define ccl_optional_struct_init
#define SHADOW_CATCHER_VISIBILITY_SHIFT(visibility)
#define INTEGRATOR_SHADOW_ISECT_SIZE
#define ccl_device
#define ccl_private
const ThreadKernelGlobalsCPU * KernelGlobals
#define ccl_device_inline
#define SHADOW_CATCHER_PATH_VISIBILITY(path_flag, visibility)
#define CCL_NAMESPACE_END
ccl_device_forceinline int integrate_shadow_max_transparent_hits(KernelGlobals kg, ConstIntegratorShadowState state)
ccl_device void integrator_intersect_shadow(KernelGlobals kg, IntegratorShadowState state)
ccl_device bool integrate_intersect_shadow_opaque(KernelGlobals kg, IntegratorShadowState state, const ccl_private Ray *ray, const uint visibility)
CCL_NAMESPACE_BEGIN ccl_device_forceinline uint integrate_intersect_shadow_visibility(KernelGlobals kg, ConstIntegratorShadowState state)
ccl_device_intersect bool scene_intersect_shadow(KernelGlobals kg, const ccl_private Ray *ray, const uint visibility)
@ PATH_RAY_SHADOW
@ PATH_RAY_SHADOW_OPAQUE
@ DEVICE_KERNEL_INTEGRATOR_SHADE_SHADOW
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_SHADOW
static ulong state[N]
#define PROFILING_INIT(kg, event)
Definition profiler.h:14
@ PROFILING_INTERSECT_SHADOW
Definition profiling.h:21
#define min(a, b)
Definition sort.cc:36
IntegratorShadowStateCPU * IntegratorShadowState
Definition state.h:230
#define INTEGRATOR_STATE_WRITE(state, nested_struct, member)
Definition state.h:236
#define INTEGRATOR_STATE(state, nested_struct, member)
Definition state.h:235
const IntegratorShadowStateCPU * ConstIntegratorShadowState
Definition state.h:231
#define INTEGRATOR_STATE_ARRAY(state, nested_struct, array_index, member)
Definition state.h:238
ccl_device_forceinline void integrator_shadow_path_terminate(KernelGlobals kg, IntegratorShadowState state, const DeviceKernel current_kernel)
Definition state_flow.h:221
ccl_device_forceinline void integrator_shadow_path_next(KernelGlobals kg, IntegratorShadowState state, const DeviceKernel current_kernel, const DeviceKernel next_kernel)
Definition state_flow.h:212
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_write_shadow_isect(IntegratorShadowState state, const ccl_private Intersection *ccl_restrict isect, const int index)
Definition state_util.h:284
ccl_device_forceinline void integrator_state_read_shadow_ray_self(KernelGlobals kg, ConstIntegratorShadowState state, ccl_private Ray *ccl_restrict ray)
Definition state_util.h:136
ccl_device_forceinline void integrator_state_read_shadow_isect(ConstIntegratorShadowState state, ccl_private Intersection *ccl_restrict isect, const int index)
Definition state_util.h:297
max
Definition text_draw.cc:251
uint8_t flag
Definition wm_window.cc:139