Blender V5.3
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. */
20{
21 uint visibility = PATH_RAY_VISIBILITY_SHADOW;
22
23#ifdef __SHADOW_CATCHER__
24 const uint32_t path_flag = INTEGRATOR_STATE(state, shadow_path, flag);
25 visibility = SHADOW_CATCHER_PATH_VISIBILITY(path_flag, visibility);
26#endif
27
28 return visibility;
29}
30
33 const ccl_private Ray *ray,
34 const uint visibility)
35{
36 /* Mask which will pick only opaque visibility bits from the `visibility`.
37 * Calculate the mask at compile time: the visibility will either be a high bits for the shadow
38 * catcher objects, or lower bits for the regular objects (there is no need to check the path
39 * state here again). */
40 constexpr const uint opaque_mask = SHADOW_CATCHER_VISIBILITY_SHIFT(
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, packed_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, 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. */
119 const uint max_transparent_hits = integrate_shadow_max_transparent_hits(kg, state);
120 uint num_hits = 0;
121 float3 throughput = one_float3();
122 scene_intersect_shadow_all(
123 kg, state, ray, visibility, max_transparent_hits, &num_hits, &throughput);
124
125 const bool opaque_hit = is_zero(throughput);
126
127 /* Computed throughput from baked shadow transparency, where we can bypass recording
128 * intersections and shader evaluation.
129 * The hypothesis here is that the majority of the shadow rays do not hit anything, and the
130 * kernel is memory-bound. Try to avoid unnecessary access and writes to the global memory. */
131 if (!isequal(throughput, one_float3())) {
132 INTEGRATOR_STATE_WRITE(state, shadow_path, throughput) *= throughput;
133 }
134
135 if (!opaque_hit) {
136 const uint num_recorded_hits = min(num_hits, (uint)INTEGRATOR_SHADOW_ISECT_SIZE);
137
138 if (num_recorded_hits > 0) {
139 sort_shadow_intersections(state, num_recorded_hits);
140 }
141
142 INTEGRATOR_STATE_WRITE(state, shadow_path, packed_num_hits) = num_hits;
143 }
144 else {
145 INTEGRATOR_STATE_WRITE(state, shadow_path, packed_num_hits) = 0;
146 }
147
148 return opaque_hit;
149}
150#endif
151
153{
155
156 /* Read ray from integrator state into local memory. */
160 /* Compute visibility. */
161 const uint visibility = integrate_intersect_shadow_visibility(state);
162
163#ifdef __TRANSPARENT_SHADOWS__
164 /* TODO: compile different kernels depending on this? Especially for OptiX
165 * conditional trace calls are bad. */
166 const bool opaque_hit = (kernel_data.integrator.transparent_shadows) ?
167 integrate_intersect_shadow_transparent(kg, state, &ray, visibility) :
168 integrate_intersect_shadow_opaque(kg, state, &ray, visibility);
169#else
170 const bool opaque_hit = integrate_intersect_shadow_opaque(kg, state, &ray, visibility);
171#endif
172
173 if (opaque_hit) {
174 /* Hit an opaque surface, shadow path ends here. */
176 return;
177 }
178
179 /* Hit nothing or transparent surfaces, continue to shadow kernel
180 * for shading and render buffer output.
181 *
182 * TODO: could also write to render buffer directly if no transparent shadows?
183 * Could save a kernel execution for the common case. */
186}
187
btScalar max
#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_private
#define ccl_device_inline
#define SHADOW_CATCHER_PATH_VISIBILITY(path_flag, visibility)
#define CCL_NAMESPACE_END
bool is_zero(float3x3 a)
ccl_device_forceinline int integrate_shadow_max_transparent_hits(KernelGlobals kg, ConstIntegratorShadowState state)
CCL_NAMESPACE_BEGIN ccl_device_forceinline uint integrate_intersect_shadow_visibility(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_device_intersect bool scene_intersect_shadow(KernelGlobals kg, const ccl_private Ray *ray, const uint visibility)
const int state
@ PATH_RAY_VISIBILITY_SHADOW
@ PATH_RAY_VISIBILITY_SHADOW_OPAQUE
@ DEVICE_KERNEL_INTEGRATOR_SHADE_SHADOW
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_SHADOW
ccl_device_inline bool isequal(const float2 a, const float2 b)
ccl_device_inline float3 one_float3()
Definition math_float3.h:26
#define PROFILING_INIT(kg, event)
unsigned int uint
Definition BKE_text.h:55
@ PROFILING_INTERSECT_SHADOW
Definition profiling.h:21
#define ccl_device
#define min(a, b)
Definition sort.cc:38
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
const IntegratorShadowStateCPU * ConstIntegratorShadowState
Definition state.h:230
#define INTEGRATOR_STATE_ARRAY(state, nested_struct, array_index, member)
Definition state.h:237
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_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_isect(ConstIntegratorShadowState state, ccl_private Intersection *ccl_restrict isect, const int index)
Definition state_util.h:297
ccl_device_forceinline void integrator_state_read_shadow_ray_self(ConstIntegratorShadowState state, ccl_private Ray *ccl_restrict ray)
Definition state_util.h:136