Blender V5.3
intersect_volume_stack.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/bvh/bvh.h"
8
10
13
15
18 const float3 from_P,
19 const float3 to_P)
20{
21#ifdef __VOLUME__
23
24 ShaderDataTinyStorage stack_sd_storage;
25 ccl_private ShaderData *stack_sd = AS_SHADER_DATA(&stack_sd_storage);
26
27 kernel_assert(kernel_data.integrator.use_volumes);
28
30 volume_ray.P = from_P;
31 volume_ray.D = safe_normalize_len(to_P - from_P, &volume_ray.tmax);
32 volume_ray.tmin = 0.0f;
33 volume_ray.self.object = INTEGRATOR_STATE(state, isect, object);
34 volume_ray.self.prim = INTEGRATOR_STATE(state, isect, prim);
35 volume_ray.self.light_object = OBJECT_NONE;
36 volume_ray.self.light_prim = PRIM_NONE;
37 /* Store to avoid global fetches on every intersection step. */
38 const uint volume_stack_size = kernel_data.volume_stack_size;
39
40 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
41 const PathRayVisibility visibility = SHADOW_CATCHER_PATH_VISIBILITY(path_flag,
43
44# ifdef __VOLUME_RECORD_ALL__
46 const uint num_hits = scene_intersect_volume(
47 kg, &volume_ray, hits, 2 * volume_stack_size, visibility);
48 if (num_hits > 0) {
49 Intersection *isect = hits;
50
51 qsort(hits, num_hits, sizeof(Intersection), intersections_compare);
52
53 for (uint hit = 0; hit < num_hits; ++hit, ++isect) {
54 /* Ignore self, SSS itself already enters and exits the object. */
55 if (isect->object == volume_ray.self.object) {
56 continue;
57 }
58 shader_setup_from_ray(kg, stack_sd, &volume_ray, isect);
59 volume_stack_enter_exit<false>(kg, state, stack_sd);
60 }
61 }
62# else
63 Intersection isect;
64 int step = 0;
65 while (step < 2 * volume_stack_size &&
66 scene_intersect_volume(kg, &volume_ray, &isect, visibility))
67 {
68 /* Ignore self, SSS itself already enters and exits the object. */
69 if (isect.object != volume_ray.self.object) {
70 shader_setup_from_ray(kg, stack_sd, &volume_ray, &isect);
71 volume_stack_enter_exit<false>(kg, state, stack_sd);
72 }
73 /* Move ray forward. */
74 volume_ray.tmin = intersection_t_offset(isect.t);
75 volume_ray.self.object = isect.object;
76 volume_ray.self.prim = isect.prim;
77 ++step;
78 }
79# endif
80}
81
82ccl_device void integrator_volume_stack_init(KernelGlobals kg, IntegratorState state)
83{
85
86 ShaderDataTinyStorage stack_sd_storage;
87 ccl_private ShaderData *stack_sd = AS_SHADER_DATA(&stack_sd_storage);
88
90 integrator_state_read_ray(state, &volume_ray);
91
92 /* Trace ray in random direction. Any direction works, Z up is a guess to get the
93 * fewest hits. */
94 volume_ray.D = make_float3(0.0f, 0.0f, 1.0f);
95 volume_ray.tmin = 0.0f;
96 volume_ray.tmax = FLT_MAX;
97 volume_ray.self.object = OBJECT_NONE;
98 volume_ray.self.prim = PRIM_NONE;
99 volume_ray.self.light_object = OBJECT_NONE;
100 volume_ray.self.light_prim = PRIM_NONE;
101
102 int stack_index = 0;
103 int enclosed_index = 0;
104
105 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
108
109 /* Initialize volume stack with background volume For shadow catcher the
110 * background volume is always assumed to be CG. */
111 if (kernel_data.background.volume_shader != SHADER_NONE) {
112 if (!(path_flag & PATH_RAY_SHADOW_CATCHER_PASS)) {
114 state, volume_stack, stack_index, object) = kernel_data.background.object_index;
116 state, volume_stack, stack_index, shader) = kernel_data.background.volume_shader;
117 stack_index++;
118 }
119 }
120
121 /* Store to avoid global fetches on every intersection step. */
122 const uint volume_stack_size = kernel_data.volume_stack_size;
123
124# ifdef __VOLUME_RECORD_ALL__
126 const uint num_hits = scene_intersect_volume(
127 kg, &volume_ray, hits, 2 * volume_stack_size, visibility);
128 if (num_hits > 0) {
129 int enclosed_volumes[MAX_VOLUME_STACK_SIZE];
130 Intersection *isect = hits;
131
132 qsort(hits, num_hits, sizeof(Intersection), intersections_compare);
133
134 for (uint hit = 0; hit < num_hits; ++hit, ++isect) {
135 shader_setup_from_ray(kg, stack_sd, &volume_ray, isect);
136 if (stack_sd->runtime_flag & SR_BACKFACING) {
137 bool need_add = true;
138 for (int i = 0; i < enclosed_index && need_add; ++i) {
139 /* If ray exited the volume and never entered to that volume
140 * it means that camera is inside such a volume.
141 */
142 if (enclosed_volumes[i] == stack_sd->object) {
143 need_add = false;
144 }
145 }
146 for (int i = 0; i < stack_index && need_add; ++i) {
147 /* Don't add intersections twice. */
148 const VolumeStack entry = integrator_state_read_volume_stack(state, i);
149 if (entry.object == stack_sd->object) {
150 need_add = false;
151 break;
152 }
153 }
154 if (need_add && stack_index < volume_stack_size - 1) {
155 const VolumeStack new_entry = {stack_sd->object, stack_sd->shader};
156 integrator_state_write_volume_stack(state, stack_index, new_entry);
157 ++stack_index;
158 }
159 }
160 else {
161 /* If ray from camera enters the volume, this volume shouldn't
162 * be added to the stack on exit.
163 */
164 enclosed_volumes[enclosed_index++] = stack_sd->object;
165 }
166 }
167 }
168# else
169 /* CUDA does not support definition of a variable size arrays, so use the maximum possible. */
170 int enclosed_volumes[MAX_VOLUME_STACK_SIZE];
171 int step = 0;
172
173 while (stack_index < volume_stack_size - 1 && enclosed_index < MAX_VOLUME_STACK_SIZE - 1 &&
174 step < 2 * volume_stack_size)
175 {
176 Intersection isect;
177 if (!scene_intersect_volume(kg, &volume_ray, &isect, visibility)) {
178 break;
179 }
180
181 shader_setup_from_ray(kg, stack_sd, &volume_ray, &isect);
182 if (stack_sd->runtime_flag & SR_BACKFACING) {
183 /* If ray exited the volume and never entered to that volume
184 * it means that camera is inside such a volume.
185 */
186 bool need_add = true;
187 for (int i = 0; i < enclosed_index && need_add; ++i) {
188 /* If ray exited the volume and never entered to that volume
189 * it means that camera is inside such a volume.
190 */
191 if (enclosed_volumes[i] == stack_sd->object) {
192 need_add = false;
193 }
194 }
195 for (int i = 0; i < stack_index && need_add; ++i) {
196 /* Don't add intersections twice. */
197 VolumeStack entry = integrator_state_read_volume_stack(state, i);
198 if (entry.object == stack_sd->object) {
199 need_add = false;
200 break;
201 }
202 }
203 if (need_add) {
204 const VolumeStack new_entry = {stack_sd->object, stack_sd->shader};
205 integrator_state_write_volume_stack(state, stack_index, new_entry);
206 ++stack_index;
207 }
208 }
209 else {
210 /* If ray from camera enters the volume, this volume shouldn't
211 * be added to the stack on exit.
212 */
213 enclosed_volumes[enclosed_index++] = stack_sd->object;
214 }
215
216 /* Move ray forward. */
217 volume_ray.tmin = intersection_t_offset(isect.t);
218 volume_ray.self.object = isect.object;
219 volume_ray.self.prim = isect.prim;
220 ++step;
221 }
222# endif
223
224 /* Write terminator. */
225 const VolumeStack new_entry = {OBJECT_NONE, SHADER_NONE};
226 integrator_state_write_volume_stack(state, stack_index, new_entry);
227#endif
228}
229
231{
232#ifdef __VOLUME__
233 integrator_volume_stack_init(kg, state);
234
235# ifdef __SHADOW_CATCHER__
237 /* Volume stack re-init for shadow catcher, continue with shading of hit. */
238 integrator_intersect_next_kernel_after_shadow_catcher_volume<
240 }
241 else
242# endif
243 {
244 /* Volume stack init for camera rays, continue with intersection of camera ray. */
248 }
249#endif
250}
251
ccl_device_forceinline float intersection_t_offset(const float t)
ccl_device int intersections_compare(const void *a, const void *b)
#define kernel_assert(cond)
#define kernel_data
#define MAX_VOLUME_STACK_SIZE
#define ccl_optional_struct_init
#define AS_SHADER_DATA(shader_data_tiny_storage)
#define SHADER_NONE
#define PRIM_NONE
#define OBJECT_NONE
#define ccl_private
#define SHADOW_CATCHER_PATH_VISIBILITY(path_flag, visibility)
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define FLT_MAX
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
ccl_device void integrator_intersect_volume_stack(KernelGlobals kg, IntegratorState state)
CCL_NAMESPACE_BEGIN ccl_device void integrator_volume_stack_update_for_subsurface(KernelGlobals kg, IntegratorState state, const float3 from_P, const float3 to_P)
const int state
uint32_t PathRayVisibility
@ SR_BACKFACING
ShaderData ShaderDataTinyStorage
@ PATH_RAY_SHADOW_CATCHER_PASS
@ PATH_RAY_VISIBILITY_CAMERA
@ PATH_RAY_VISIBILITY_ALL
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_VOLUME_STACK
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_CLOSEST
ccl_device_inline float3 safe_normalize_len(const float3 a, ccl_private float *t)
#define PROFILING_INIT(kg, event)
unsigned int uint
Definition BKE_text.h:55
unsigned int uint32_t
Definition BKE_text.h:81
void visibility(Visibility &srt, const ViewCulling &culling, const uint3 global_id, const uint3 local_id, const uint3 workgroup_id)
@ PROFILING_INTERSECT_VOLUME_STACK
Definition profiling.h:22
#define ccl_device
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 INTEGRATOR_STATE_ARRAY_WRITE(state, nested_struct, array_index, member)
Definition state.h:239
#define INTEGRATOR_STATE(state, nested_struct, member)
Definition state.h:234
IntegratorStateCPU * IntegratorState
Definition state.h:227
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_state_read_ray(ConstIntegratorState state, ccl_private Ray *ccl_restrict ray)
Definition state_util.h:49