Blender V5.3
shade_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
11
13
15
21
22#ifdef __KERNEL_GPU__
23/* Assume we can pack num hits into 12 bits, so that we can also store resume hits
24 * and skip volume in the 16 bit num_hits. */
25# define SHADOW_HIT_COUNT_BITS_GPU 12
26# define SHADOW_HIT_COUNT_MASK_GPU ((1 << SHADOW_HIT_COUNT_BITS_GPU) - 1)
27# define SHADOW_RESUME_BITS_GPU 3
28# define SHADOW_RESUME_MASK_GPU ((1 << SHADOW_RESUME_BITS_GPU) - 1)
29
30/* +1 is for the extra loop iteration for the final volume segment. */
31static_assert(INTEGRATOR_SHADOW_ISECT_SIZE_GPU + 1 < (1 << SHADOW_RESUME_BITS_GPU),
32 "INTEGRATOR_SHADOW_ISECT_SIZE_GPU too large for resume_hit bits");
33#endif
34
35ccl_device_inline uint shadow_num_hits_get(const uint packed_num_hits)
36{
37#ifdef __KERNEL_GPU__
38 return packed_num_hits & SHADOW_HIT_COUNT_MASK_GPU;
39#else
40 return packed_num_hits;
41#endif
42}
43
44ccl_device_inline uint shadow_resume_hit_get(const uint packed_num_hits)
45{
46#ifdef __KERNEL_GPU__
47 return (packed_num_hits >> SHADOW_HIT_COUNT_BITS_GPU) & SHADOW_RESUME_MASK_GPU;
48#else
49 (void)packed_num_hits;
50 return 0;
51#endif
52}
53
54ccl_device_inline bool shadow_surface_cache_miss_get(const uint packed_num_hits)
55{
56#ifdef __KERNEL_GPU__
57 return (packed_num_hits >> (SHADOW_HIT_COUNT_BITS_GPU + SHADOW_RESUME_BITS_GPU)) & 1;
58#else
59 (void)packed_num_hits;
60 return false;
61#endif
62}
63
64ccl_device_inline uint shadow_num_hits_pack(const uint num_hits,
65 const uint resume_hit,
66 const bool surface_hit)
67{
68#ifdef __KERNEL_GPU__
69 return (num_hits & SHADOW_HIT_COUNT_MASK_GPU) |
70 ((resume_hit & SHADOW_RESUME_MASK_GPU) << SHADOW_HIT_COUNT_BITS_GPU) |
71 ((surface_hit ? 1u : 0u) << (SHADOW_HIT_COUNT_BITS_GPU + SHADOW_RESUME_BITS_GPU));
72#else
73 /* Cache miss resume not supported on CPU. */
74 kernel_assert(resume_hit == 0 && !surface_hit);
75 (void)resume_hit;
76 (void)surface_hit;
77 return num_hits;
78#endif
79}
80
82{
83 return shadow_num_hits_get(packed_num_hits) >= INTEGRATOR_SHADOW_ISECT_SIZE;
84}
85
86#ifdef __TRANSPARENT_SHADOWS__
87ccl_device_inline TransparentShadowEvalResult integrate_transparent_surface_shadow(
88 KernelGlobals kg, IntegratorShadowState state, const int num_hits, const int hit)
89{
91
92 /* TODO: does aliasing like this break automatic SoA in CUDA?
93 * Should we instead store closures separate from ShaderData?
94 *
95 * TODO: is it better to declare this outside the loop or keep it local
96 * so the compiler can see there is no dependency between iterations? */
97 ShaderDataTinyStorage shadow_sd_storage;
98 ccl_private ShaderData *shadow_sd = AS_SHADER_DATA(&shadow_sd_storage);
99
100 /* Setup shader data at surface. */
103
106
107 shader_setup_from_ray(kg, shadow_sd, &ray, &isect);
108
109 /* Evaluate shader. */
110 if (!(shadow_sd->shader_flag & SD_HAS_ONLY_VOLUME)) {
112 kg, state, shadow_sd, nullptr, PATH_RAY_VISIBILITY_SHADOW, PATH_RAY_FLAG_NONE);
113 if (shadow_sd->runtime_flag & SR_CACHE_MISS) {
114 /* Store resume state at current hit. */
115 INTEGRATOR_STATE_WRITE(state, shadow_path, packed_num_hits) = shadow_num_hits_pack(
116 num_hits, hit, true);
118 }
119 }
120 else {
121 INTEGRATOR_STATE_WRITE(state, shadow_path, volume_bounds_bounce) += 1;
122 if (INTEGRATOR_STATE(state, shadow_path, volume_bounds_bounce) > VOLUME_BOUNDS_MAX) {
124 }
125 }
126
127# ifdef __VOLUME__
128 /* Exit/enter volume. */
129 volume_stack_enter_exit<true>(kg, state, shadow_sd);
130# endif
131
132 /* Disable transparent shadows for ray portals */
133 if (shadow_sd->runtime_flag & SR_RAY_PORTAL) {
135 }
136
137 /* Compute transparency from closures. */
138 const Spectrum transparency = surface_shader_transparency(shadow_sd);
139 const Spectrum throughput = INTEGRATOR_STATE(state, shadow_path, throughput) * transparency;
140
141 if (is_zero(throughput)) {
143 }
144
145 INTEGRATOR_STATE_WRITE(state, shadow_path, throughput) = throughput;
146 INTEGRATOR_STATE_WRITE(state, shadow_path, transparent_bounce) += 1;
147 INTEGRATOR_STATE_WRITE(state, shadow_path, rng_offset) += PRNG_BOUNCE_NUM;
148
150}
151
152# ifdef __VOLUME__
154integrate_transparent_volume_shadow(KernelGlobals kg,
156 const int num_hits,
157 const int hit,
158 const int num_recorded_hits,
160{
162
163 /* TODO: deduplicate with surface, or does it not matter for memory usage? */
164 ShaderDataTinyStorage shadow_sd_storage;
165 ccl_private ShaderData *shadow_sd = AS_SHADER_DATA(&shadow_sd_storage);
166
167 /* Setup shader data. */
170 ray.self.object = OBJECT_NONE;
171 ray.self.prim = PRIM_NONE;
172 ray.self.light_object = OBJECT_NONE;
173 ray.self.light_prim = PRIM_NONE;
174 /* Modify ray position and length to match current segment. */
175 ray.tmin = (hit == 0) ? ray.tmin : INTEGRATOR_STATE_ARRAY(state, shadow_isect, hit - 1, t);
176 ray.tmax = (hit < num_recorded_hits) ? INTEGRATOR_STATE_ARRAY(state, shadow_isect, hit, t) :
177 ray.tmax;
178
179 /* `object` is only needed for light tree with light linking, it is irrelevant for shadow. */
180 shader_setup_from_volume(shadow_sd, &ray, OBJECT_NONE);
181
182 if (kernel_data.integrator.volume_ray_marching) {
183 const float step_size = volume_stack_step_size<true>(kg, state);
184 volume_shadow_ray_marching(kg, state, &ray, shadow_sd, throughput, step_size);
185 }
186 else {
187 volume_shadow_null_scattering(kg, state, &ray, shadow_sd, throughput);
188 }
189
190 if (is_zero(*throughput)) {
192 }
193
194 if (shadow_sd->runtime_flag & SR_CACHE_MISS) {
195 /* Store resume state at current hit. */
196 INTEGRATOR_STATE_WRITE(state, shadow_path, packed_num_hits) = shadow_num_hits_pack(
197 num_hits, hit, false);
199 }
200
202}
203# endif
204
205ccl_device_inline TransparentShadowEvalResult integrate_transparent_shadow(
206 KernelGlobals kg, IntegratorShadowState state, const uint packed_num_hits)
207{
208 /* Accumulate shadow for transparent surfaces. */
209 const uint num_hits = shadow_num_hits_get(packed_num_hits);
210 const uint num_recorded_hits = min(num_hits, (uint)INTEGRATOR_SHADOW_ISECT_SIZE);
211
212 /* Resume state from previous cache miss. */
213 const uint resume_hit = shadow_resume_hit_get(packed_num_hits);
214 const bool cache_miss_surface = shadow_surface_cache_miss_get(packed_num_hits);
215
216 /* Plus one to account for world volume, which has no boundary to hit but casts shadows. */
217 for (uint hit = resume_hit; hit < num_recorded_hits + 1; hit++) {
218 /* Skip volume if resuming after volume completed but surface had cache miss. */
219 const bool skip_volume = (hit == resume_hit) && cache_miss_surface;
220
221 /* Volume shaders. */
222 if (!skip_volume &&
223 (hit < num_recorded_hits || !shadow_intersections_has_remaining(packed_num_hits)))
224 {
225# ifdef __VOLUME__
226 if (!integrator_state_shadow_volume_stack_is_empty(kg, state)) {
227 Spectrum throughput = INTEGRATOR_STATE(state, shadow_path, throughput);
228 const TransparentShadowEvalResult result = integrate_transparent_volume_shadow(
229 kg, state, num_hits, hit, num_recorded_hits, &throughput);
230 if (result != TRANSPARENT_SHADOW_EVAL_CONTINUE) {
231 return result;
232 }
233
234 INTEGRATOR_STATE_WRITE(state, shadow_path, throughput) = throughput;
235 }
236# endif
237 }
238
239 /* Surface shaders. */
240 if (hit < num_recorded_hits) {
241 const TransparentShadowEvalResult result = integrate_transparent_surface_shadow(
242 kg, state, num_hits, hit);
243 if (result != TRANSPARENT_SHADOW_EVAL_CONTINUE) {
244 return result;
245 }
246 }
247 /* Note we do not need to check max_transparent_bounce here, the number
248 * of intersections is already limited and made opaque in the
249 * INTERSECT_SHADOW kernel. */
250 }
251
252 if (shadow_intersections_has_remaining(packed_num_hits)) {
253 /* There are more hits that we could not recorded due to memory usage,
254 * adjust ray to intersect again from the last hit. */
255 const float last_hit_t = INTEGRATOR_STATE_ARRAY(state, shadow_isect, num_recorded_hits - 1, t);
256 INTEGRATOR_STATE_WRITE(state, shadow_ray, tmin) = intersection_t_offset(last_hit_t);
257 }
258
260}
261#endif /* __TRANSPARENT_SHADOWS__ */
262
266{
268 const uint packed_num_hits = INTEGRATOR_STATE(state, shadow_path, packed_num_hits);
269
270#ifdef __TRANSPARENT_SHADOWS__
271 /* Evaluate transparent shadows. */
272 const TransparentShadowEvalResult result = integrate_transparent_shadow(
273 kg, state, packed_num_hits);
276 return;
277 }
278 if (result == TRANSPARENT_SHADOW_EVAL_OPAQUE) {
280 return;
281 }
282
284#endif
285
286 if (shadow_intersections_has_remaining(packed_num_hits)) {
287 /* More intersections to find, continue shadow ray. */
290 return;
291 }
292
296}
297
ccl_device_forceinline float intersection_t_offset(const float t)
#define kernel_assert(cond)
#define kernel_data
#define ccl_restrict
#define VOLUME_BOUNDS_MAX
#define ccl_optional_struct_init
#define AS_SHADER_DATA(shader_data_tiny_storage)
#define PRIM_NONE
#define INTEGRATOR_SHADOW_ISECT_SIZE
#define INTEGRATOR_SHADOW_ISECT_SIZE_GPU
#define OBJECT_NONE
#define ccl_private
#define ccl_device_inline
#define ccl_global
#define CCL_NAMESPACE_END
bool is_zero(float3x3 a)
const int state
ccl_gpu_kernel_postfix ccl_global KernelWorkTile const int ccl_global float * render_buffer
ccl_device_forceinline void guiding_record_direct_light(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorShadowState state)
@ SR_RAY_PORTAL
@ SR_CACHE_MISS
@ SD_HAS_ONLY_VOLUME
ShaderData ShaderDataTinyStorage
@ PRNG_BOUNCE_NUM
@ PATH_RAY_FLAG_NONE
@ PATH_RAY_VISIBILITY_SHADOW
@ DEVICE_KERNEL_INTEGRATOR_SHADE_SHADOW
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_SHADOW
ccl_device_inline void film_write_direct_light(KernelGlobals kg, ConstIntegratorShadowState state, ccl_global float *ccl_restrict render_buffer)
#define PROFILING_INIT(kg, event)
unsigned int uint
Definition BKE_text.h:55
@ PROFILING_SHADE_SHADOW_SURFACE
Definition profiling.h:39
@ PROFILING_SHADE_SHADOW_VOLUME
Definition profiling.h:40
@ PROFILING_SHADE_SHADOW_SETUP
Definition profiling.h:38
#define ccl_device
ccl_device_inline bool shadow_intersections_has_remaining(const uint packed_num_hits)
ccl_device void integrator_shade_shadow(KernelGlobals kg, IntegratorShadowState state, ccl_global float *ccl_restrict render_buffer)
ccl_device_inline uint shadow_num_hits_pack(const uint num_hits, const uint resume_hit, const bool surface_hit)
ccl_device_inline uint shadow_num_hits_get(const uint packed_num_hits)
TransparentShadowEvalResult
@ TRANSPARENT_SHADOW_EVAL_OPAQUE
@ TRANSPARENT_SHADOW_EVAL_CACHE_MISS
@ TRANSPARENT_SHADOW_EVAL_CONTINUE
ccl_device_inline bool shadow_surface_cache_miss_get(const uint packed_num_hits)
ccl_device_inline uint shadow_resume_hit_get(const uint packed_num_hits)
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
#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
#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_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_shadow_isect(ConstIntegratorShadowState state, ccl_private Intersection *ccl_restrict isect, const int index)
Definition state_util.h:297
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_transparency(const ccl_private ShaderData *sd)
float3 Spectrum