Blender  V3.3
shade_shadow.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #pragma once
5 
9 
11 
13 {
14  return num_hits >= INTEGRATOR_SHADOW_ISECT_SIZE;
15 }
16 
17 #ifdef __TRANSPARENT_SHADOWS__
18 ccl_device_inline float3 integrate_transparent_surface_shadow(KernelGlobals kg,
20  const int hit)
21 {
23 
24  /* TODO: does aliasing like this break automatic SoA in CUDA?
25  * Should we instead store closures separate from ShaderData?
26  *
27  * TODO: is it better to declare this outside the loop or keep it local
28  * so the compiler can see there is no dependency between iterations? */
29  ShaderDataTinyStorage shadow_sd_storage;
30  ccl_private ShaderData *shadow_sd = AS_SHADER_DATA(&shadow_sd_storage);
31 
32  /* Setup shader data at surface. */
35 
38 
39  shader_setup_from_ray(kg, shadow_sd, &ray, &isect);
40 
41  /* Evaluate shader. */
42  if (!(shadow_sd->flag & SD_HAS_ONLY_VOLUME)) {
43  shader_eval_surface<KERNEL_FEATURE_NODE_MASK_SURFACE_SHADOW>(
44  kg, state, shadow_sd, NULL, PATH_RAY_SHADOW);
45  }
46 
47 # ifdef __VOLUME__
48  /* Exit/enter volume. */
49  shadow_volume_stack_enter_exit(kg, state, shadow_sd);
50 # endif
51 
52  /* Compute transparency from closures. */
53  return shader_bsdf_transparency(kg, shadow_sd);
54 }
55 
56 # ifdef __VOLUME__
57 ccl_device_inline void integrate_transparent_volume_shadow(KernelGlobals kg,
59  const int hit,
60  const int num_recorded_hits,
62  throughput)
63 {
65 
66  /* TODO: deduplicate with surface, or does it not matter for memory usage? */
67  ShaderDataTinyStorage shadow_sd_storage;
68  ccl_private ShaderData *shadow_sd = AS_SHADER_DATA(&shadow_sd_storage);
69 
70  /* Setup shader data. */
73  ray.self.object = OBJECT_NONE;
74  ray.self.prim = PRIM_NONE;
75  ray.self.light_object = OBJECT_NONE;
76  ray.self.light_prim = PRIM_NONE;
77  /* Modify ray position and length to match current segment. */
78  ray.tmin = (hit == 0) ? ray.tmin : INTEGRATOR_STATE_ARRAY(state, shadow_isect, hit - 1, t);
79  ray.tmax = (hit < num_recorded_hits) ? INTEGRATOR_STATE_ARRAY(state, shadow_isect, hit, t) :
80  ray.tmax;
81 
82  shader_setup_from_volume(kg, shadow_sd, &ray);
83 
85  const float step_size = volume_stack_step_size(kg, volume_read_lambda_pass);
86 
87  volume_shadow_heterogeneous(kg, state, &ray, shadow_sd, throughput, step_size);
88 }
89 # endif
90 
91 ccl_device_inline bool integrate_transparent_shadow(KernelGlobals kg,
93  const uint num_hits)
94 {
95  /* Accumulate shadow for transparent surfaces. */
96  const uint num_recorded_hits = min(num_hits, INTEGRATOR_SHADOW_ISECT_SIZE);
97 
98  for (uint hit = 0; hit < num_recorded_hits + 1; hit++) {
99  /* Volume shaders. */
100  if (hit < num_recorded_hits || !shadow_intersections_has_remaining(num_hits)) {
101 # ifdef __VOLUME__
103  float3 throughput = INTEGRATOR_STATE(state, shadow_path, throughput);
104  integrate_transparent_volume_shadow(kg, state, hit, num_recorded_hits, &throughput);
105  if (is_zero(throughput)) {
106  return true;
107  }
108 
109  INTEGRATOR_STATE_WRITE(state, shadow_path, throughput) = throughput;
110  }
111 # endif
112  }
113 
114  /* Surface shaders. */
115  if (hit < num_recorded_hits) {
116  const float3 shadow = integrate_transparent_surface_shadow(kg, state, hit);
117  const float3 throughput = INTEGRATOR_STATE(state, shadow_path, throughput) * shadow;
118  if (is_zero(throughput)) {
119  return true;
120  }
121 
122  INTEGRATOR_STATE_WRITE(state, shadow_path, throughput) = throughput;
123  INTEGRATOR_STATE_WRITE(state, shadow_path, transparent_bounce) += 1;
124  INTEGRATOR_STATE_WRITE(state, shadow_path, rng_offset) += PRNG_BOUNCE_NUM;
125  }
126 
127  /* Note we do not need to check max_transparent_bounce here, the number
128  * of intersections is already limited and made opaque in the
129  * INTERSECT_SHADOW kernel. */
130  }
131 
132  if (shadow_intersections_has_remaining(num_hits)) {
133  /* There are more hits that we could not recorded due to memory usage,
134  * adjust ray to intersect again from the last hit. */
135  const float last_hit_t = INTEGRATOR_STATE_ARRAY(state, shadow_isect, num_recorded_hits - 1, t);
136  INTEGRATOR_STATE_WRITE(state, shadow_ray, tmin) = intersection_t_offset(last_hit_t);
137  }
138 
139  return false;
140 }
141 #endif /* __TRANSPARENT_SHADOWS__ */
142 
146 {
148  const uint num_hits = INTEGRATOR_STATE(state, shadow_path, num_hits);
149 
150 #ifdef __TRANSPARENT_SHADOWS__
151  /* Evaluate transparent shadows. */
152  const bool opaque = integrate_transparent_shadow(kg, state, num_hits);
153  if (opaque) {
155  return;
156  }
157 #endif
158 
159  if (shadow_intersections_has_remaining(num_hits)) {
160  /* More intersections to find, continue shadow ray. */
162  state,
165  return;
166  }
167  else {
170  return;
171  }
172 }
173 
unsigned int uint
Definition: BLI_sys_types.h:67
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
ccl_device_inline void kernel_accum_light(KernelGlobals kg, ConstIntegratorShadowState state, ccl_global float *ccl_restrict render_buffer)
Definition: accumulate.h:418
#define ccl_restrict
Definition: cuda/compat.h:50
#define ccl_optional_struct_init
Definition: cuda/compat.h:53
#define ccl_device
Definition: cuda/compat.h:32
#define ccl_private
Definition: cuda/compat.h:48
#define ccl_device_inline
Definition: cuda/compat.h:34
#define ccl_global
Definition: cuda/compat.h:43
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
ccl_device_forceinline float intersection_t_offset(const float t)
const KernelGlobalsCPU *ccl_restrict KernelGlobals
ccl_gpu_kernel_postfix ccl_global KernelWorkTile const int ccl_global float * render_buffer
const int state
ccl_device float3 shader_bsdf_transparency(KernelGlobals kg, ccl_private const ShaderData *sd)
@ SD_HAS_ONLY_VOLUME
Definition: kernel/types.h:770
#define AS_SHADER_DATA(shader_data_tiny_storage)
Definition: kernel/types.h:943
@ PRNG_BOUNCE_NUM
Definition: kernel/types.h:172
#define PRIM_NONE
Definition: kernel/types.h:41
@ PATH_RAY_SHADOW
Definition: kernel/types.h:206
#define INTEGRATOR_SHADOW_ISECT_SIZE
Definition: kernel/types.h:53
#define OBJECT_NONE
Definition: kernel/types.h:40
ShaderDataTinyStorage
Definition: kernel/types.h:933
ShaderData
Definition: kernel/types.h:925
@ DEVICE_KERNEL_INTEGRATOR_SHADE_SHADOW
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_SHADOW
#define PROFILING_INIT(kg, event)
#define VOLUME_READ_LAMBDA(function_call)
Definition: metal/compat.h:156
bool is_zero(const T &a)
ccl_device void integrator_shade_shadow(KernelGlobals kg, IntegratorShadowState state, ccl_global float *ccl_restrict render_buffer)
Definition: shade_shadow.h:143
CCL_NAMESPACE_BEGIN ccl_device_inline bool shadow_intersections_has_remaining(const uint num_hits)
Definition: shade_shadow.h:12
CCL_NAMESPACE_BEGIN ccl_device_inline void shader_setup_from_ray(KernelGlobals kg, ccl_private ShaderData *ccl_restrict sd, ccl_private const Ray *ccl_restrict ray, ccl_private const Intersection *ccl_restrict isect)
Definition: shader_data.h:28
#define min(a, b)
Definition: sort.c:35
#define INTEGRATOR_STATE_WRITE(state, nested_struct, member)
Definition: state.h:155
#define INTEGRATOR_STATE(state, nested_struct, member)
Definition: state.h:154
IntegratorShadowStateCPU *ccl_restrict IntegratorShadowState
Definition: state.h:149
#define INTEGRATOR_STATE_ARRAY(state, nested_struct, array_index, member)
Definition: state.h:157
ccl_device_forceinline void integrator_shadow_path_terminate(KernelGlobals kg, IntegratorShadowState state, const DeviceKernel current_kernel)
Definition: state_flow.h:196
ccl_device_forceinline void integrator_shadow_path_next(KernelGlobals kg, IntegratorShadowState state, const DeviceKernel current_kernel, const DeviceKernel next_kernel)
Definition: state_flow.h:187
ccl_device_forceinline void integrator_state_read_shadow_ray(KernelGlobals kg, ConstIntegratorShadowState state, ccl_private Ray *ccl_restrict ray)
Definition: state_util.h:53
ccl_device_forceinline VolumeStack integrator_state_read_shadow_volume_stack(ConstIntegratorShadowState state, int i)
Definition: state_util.h:180
ccl_device_forceinline void integrator_state_read_shadow_isect(ConstIntegratorShadowState state, ccl_private Intersection *ccl_restrict isect, const int index)
Definition: state_util.h:129
ccl_device_forceinline bool integrator_state_shadow_volume_stack_is_empty(KernelGlobals kg, ConstIntegratorShadowState state)
Definition: state_util.h:187
@ PROFILING_SHADE_SHADOW_SURFACE
@ PROFILING_SHADE_SHADOW_VOLUME
@ PROFILING_SHADE_SHADOW_SETUP
ccl_device float volume_stack_step_size(KernelGlobals kg, StackReadOp stack_read)
Definition: volume_stack.h:126
ccl_device void shadow_volume_stack_enter_exit(KernelGlobals kg, IntegratorShadowState state, ccl_private const ShaderData *sd)
Definition: volume_stack.h:91