Blender V5.3
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
8
9#ifdef __VOLUME__
10
11/* Volume Stack
12 *
13 * This is an array of object/shared ID's that the current segment of the path
14 * is inside of. */
15
16template<const bool shadow, typename IntegratorGenericState>
17ccl_device_forceinline VolumeStack volume_stack_read(const IntegratorGenericState state,
18 const int i)
19{
20 if constexpr (shadow) {
21 return integrator_state_read_shadow_volume_stack(state, i);
22 }
23 else {
24 return integrator_state_read_volume_stack(state, i);
25 }
26
27# ifdef __KERNEL_GPU__
28 /* Silence false positive warning with some GPU compilers. */
29 VolumeStack stack = {};
30 return stack;
31# endif
32}
33
34template<const bool shadow, typename IntegratorGenericState>
35ccl_device_forceinline void volume_stack_write(IntegratorGenericState state,
36 const int i,
37 const VolumeStack entry)
38{
39 if constexpr (shadow) {
40 integrator_state_write_shadow_volume_stack(state, i, entry);
41 }
42 else {
43 integrator_state_write_volume_stack(state, i, entry);
44 }
45}
46
47template<const bool shadow, typename IntegratorGenericState>
48ccl_device_inline void volume_stack_exit(IntegratorGenericState state, const int exit_i)
49{
50 /* Get size of the volume stack. */
51 /* TODO(weizhen): instead looping until SHADER_NONE, can we store the size in the first entry? */
52 int i = exit_i;
53 for (;; i++) {
54 if (volume_stack_read<shadow>(state, i + 1).shader == SHADER_NONE) {
55 break;
56 }
57 }
58
59 if (exit_i != i) {
60 /* Swap the last entry and the exit entry. */
61 volume_stack_write<shadow>(state, exit_i, volume_stack_read<shadow>(state, i));
62 }
63
64 /* Write sentinel value. */
65 volume_stack_write<shadow>(state, i, {OBJECT_NONE, SHADER_NONE});
66}
67
68template<const bool shadow, typename IntegratorGenericState>
69ccl_device void volume_stack_enter_exit(KernelGlobals kg,
70 IntegratorGenericState state,
71 const ccl_private ShaderData *sd)
72{
73# ifdef __KERNEL_USE_DATA_CONSTANTS__
74 /* If we're using data constants, this fetch disappears.
75 * On Apple GPUs, scenes without volumetric features can render 1 or 2% faster by dead-stripping
76 * this function. */
77 if (!(kernel_data.kernel_features & KERNEL_FEATURE_VOLUME)) {
78 return;
79 }
80# endif
81
82 /* todo: we should have some way for objects to indicate if they want the
83 * world shader to work inside them. excluding it by default is problematic
84 * because non-volume objects can't be assumed to be closed manifolds */
85 if (!(sd->shader_flag & SD_HAS_VOLUME)) {
86 return;
87 }
88
89 if (sd->runtime_flag & SR_BACKFACING) {
90 /* Exit volume object: remove from stack. */
91 for (int i = 0;; i++) {
92 const VolumeStack entry = volume_stack_read<shadow>(state, i);
93 if (entry.shader == SHADER_NONE) {
94 break;
95 }
96
97 if (entry.object == sd->object && entry.shader == sd->shader) {
98 volume_stack_exit<shadow>(state, i);
99 return;
100 }
101 }
102 }
103 else {
104 /* Enter volume object: add to stack. */
105 uint i;
106 for (i = 0;; i++) {
107 const VolumeStack entry = volume_stack_read<shadow>(state, i);
108 if (entry.shader == SHADER_NONE) {
109 break;
110 }
111
112 /* Already in the stack? then we have nothing to do. */
113 if (entry.object == sd->object && entry.shader == sd->shader) {
114 return;
115 }
116 }
117
118 /* If we exceed the stack limit, ignore. */
119 if (i >= kernel_data.volume_stack_size - 1) {
120 return;
121 }
122
123 /* Add to the end of the stack. */
124 const VolumeStack new_entry = {sd->object, sd->shader};
125 const VolumeStack empty_entry = {OBJECT_NONE, SHADER_NONE};
126 volume_stack_write<shadow>(state, i, new_entry);
127 volume_stack_write<shadow>(state, i + 1, empty_entry);
128 }
129}
130
131/* Clean stack after the last bounce.
132 *
133 * It is expected that all volumes are closed manifolds, so at the time when ray
134 * hits nothing (for example, it is a last bounce which goes to environment) the
135 * only expected volume in the stack is the world's one. All the rest volume
136 * entries should have been exited already.
137 *
138 * This isn't always true because of ray intersection precision issues, which
139 * could lead us to an infinite non-world volume in the stack, causing render
140 * artifacts.
141 *
142 * Use this function after the last bounce to get rid of all volumes apart from
143 * the world's one after the last bounce to avoid render artifacts.
144 */
145ccl_device_inline void volume_stack_clean(KernelGlobals kg, IntegratorState state)
146{
147 if (kernel_data.background.volume_shader != SHADER_NONE) {
148 /* Keep the world's volume in stack. */
149 INTEGRATOR_STATE_ARRAY_WRITE(state, volume_stack, 1, shader) = SHADER_NONE;
150 }
151 else {
152 INTEGRATOR_STATE_ARRAY_WRITE(state, volume_stack, 0, shader) = SHADER_NONE;
153 }
154}
155
156/* Check if the volume is homogeneous by checking if the shader flag is set or if volume attributes
157 * are needed. */
158ccl_device_inline bool volume_is_homogeneous(KernelGlobals kg,
159 const ccl_private VolumeStack &entry)
160{
161 const int shader_flag = kernel_data_fetch(shaders, (entry.shader & SHADER_MASK)).flags;
162
163 if (shader_flag & SD_HETEROGENEOUS_VOLUME) {
164 return false;
165 }
166
167 if (shader_flag & SD_NEED_VOLUME_ATTRIBUTES) {
168 const int object = entry.object;
169 if (object == kernel_data.background.object_index) {
170 /* Volume attributes for world is not supported. */
171 return true;
172 }
173
174 const uint object_flag = kernel_data_fetch(object_flag, object);
175 if (object_flag & SD_OBJECT_HAS_VOLUME_ATTRIBUTES) {
176 /* If both the shader and the object needs volume attributes, the volume is heterogeneous. */
177 return false;
178 }
179 }
180
181 return true;
182}
183
184template<const bool shadow, typename IntegratorGenericState>
185ccl_device_inline bool volume_is_homogeneous(KernelGlobals kg, const IntegratorGenericState state)
186{
187 for (int i = 0;; i++) {
188 const VolumeStack entry = volume_stack_read<shadow>(state, i);
189
190 if (entry.shader == SHADER_NONE) {
191 return true;
192 }
193
194 if (!volume_is_homogeneous(kg, entry)) {
195 return false;
196 }
197 }
198
199 kernel_assert(false);
200 return false;
201}
202
203template<const bool shadow, typename IntegratorGenericState>
204ccl_device float volume_stack_step_size(KernelGlobals kg, const IntegratorGenericState state)
205{
206 kernel_assert(kernel_data.integrator.volume_ray_marching);
207
208 float step_size = FLT_MAX;
209
210 for (int i = 0;; i++) {
211 const VolumeStack entry = volume_stack_read<shadow>(state, i);
212 if (entry.shader == SHADER_NONE) {
213 break;
214 }
215
216 if (!volume_is_homogeneous(kg, entry)) {
217 const float object_step_size = kernel_data_fetch(volume_step_size, entry.object);
218 step_size = fminf(object_step_size, step_size);
219 }
220 }
221
222 return step_size;
223}
224
225enum VolumeSampleMethod {
226 VOLUME_SAMPLE_NONE = 0,
227 VOLUME_SAMPLE_DISTANCE = (1 << 0),
228 VOLUME_SAMPLE_EQUIANGULAR = (1 << 1),
229 VOLUME_SAMPLE_MIS = (VOLUME_SAMPLE_DISTANCE | VOLUME_SAMPLE_EQUIANGULAR),
230};
231
232ccl_device VolumeSampleMethod volume_stack_sample_method(KernelGlobals kg, IntegratorState state)
233{
234 VolumeSampleMethod method = VOLUME_SAMPLE_NONE;
235
236 for (int i = 0;; i++) {
237 VolumeStack entry = integrator_state_read_volume_stack(state, i);
238 if (entry.shader == SHADER_NONE) {
239 break;
240 }
241
242 int shader_flag = kernel_data_fetch(shaders, (entry.shader & SHADER_MASK)).flags;
243
244 if (shader_flag & SD_VOLUME_MIS) {
245 /* Multiple importance sampling. */
246 return VOLUME_SAMPLE_MIS;
247 }
248 else if (shader_flag & SD_VOLUME_EQUIANGULAR) {
249 /* Distance + equiangular sampling -> multiple importance sampling. */
250 if (method == VOLUME_SAMPLE_DISTANCE) {
251 return VOLUME_SAMPLE_MIS;
252 }
253
254 /* Only equiangular sampling. */
255 method = VOLUME_SAMPLE_EQUIANGULAR;
256 }
257 else {
258 /* Distance + equiangular sampling -> multiple importance sampling. */
259 if (method == VOLUME_SAMPLE_EQUIANGULAR) {
260 return VOLUME_SAMPLE_MIS;
261 }
262
263 /* Distance sampling only. */
264 method = VOLUME_SAMPLE_DISTANCE;
265 }
266 }
267
268 return method;
269}
270
271#endif /* __VOLUME__ */
272
#define kernel_assert(cond)
#define KERNEL_FEATURE_VOLUME
#define kernel_data
#define ccl_device_forceinline
#define kernel_data_fetch(name, index)
#define SHADER_NONE
#define OBJECT_NONE
#define ccl_private
#define ccl_device_inline
#define CCL_NAMESPACE_END
#define FLT_MAX
const int state
@ SR_BACKFACING
@ SD_VOLUME_MIS
@ SD_VOLUME_EQUIANGULAR
@ SD_HAS_VOLUME
@ SD_NEED_VOLUME_ATTRIBUTES
@ SD_HETEROGENEOUS_VOLUME
@ SD_OBJECT_HAS_VOLUME_ATTRIBUTES
@ SHADER_MASK
unsigned int uint
Definition BKE_text.h:55
#define ccl_device
#define fminf
#define INTEGRATOR_STATE_ARRAY_WRITE(state, nested_struct, array_index, member)
Definition state.h:239
IntegratorStateCPU * IntegratorState
Definition state.h:227