Blender V5.3
state_flow.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/film/write.h"
11
13
14#ifdef __KERNEL_GPU__
15# include "util/atomic.h"
16#endif
17
19
20/* Control Flow
21 *
22 * Utilities for control flow between kernels. The implementation is different between CPU and
23 * GPU devices. For the latter part of the logic is handled on the host side with wavefronts.
24 *
25 * There is a main path for regular path tracing camera for path tracing. Shadows for next
26 * event estimation branch off from this into their own path, that may be computed in
27 * parallel while the main path continues. Additionally, shading kernels are sorted using
28 * a key for coherence.
29 *
30 * Each kernel on the main path must call one of these functions. These may not be called
31 * multiple times from the same kernel.
32 *
33 * integrator_path_init(state, next_kernel)
34 * integrator_path_next(state, current_kernel, next_kernel)
35 * integrator_path_terminate(state, current_kernel)
36 *
37 * For the shadow path similar functions are used, and again each shadow kernel must call
38 * one of them, and only once.
39 */
40
45
50
54{
55 if (!render_buffer) {
56 return;
57 }
58
60 kernel_assert(kernel_data.film.pass_volume_majorant != PASS_UNUSED);
61
62 const float optical_depth = INTEGRATOR_STATE(state, path, optical_depth);
64 film_write_pass_float(buffer + kernel_data.film.pass_volume_majorant, optical_depth);
65 film_write_pass_float(buffer + kernel_data.film.pass_volume_majorant_sample_count, 1.0f);
66 }
67}
68
69#ifdef __KERNEL_GPU__
70
72 const DeviceKernel next_kernel)
73{
74 atomic_fetch_and_add_uint32(&kernel_integrator_state.queue_counter->num_queued[next_kernel], 1);
75 INTEGRATOR_STATE_WRITE(state, path, queued_kernel) = next_kernel;
76}
77
79 const DeviceKernel current_kernel,
80 const DeviceKernel next_kernel)
81{
82 atomic_fetch_and_sub_uint32(&kernel_integrator_state.queue_counter->num_queued[current_kernel],
83 1);
84 atomic_fetch_and_add_uint32(&kernel_integrator_state.queue_counter->num_queued[next_kernel], 1);
85 INTEGRATOR_STATE_WRITE(state, path, queued_kernel) = next_kernel;
86}
87
91 const DeviceKernel current_kernel)
92{
94
95 atomic_fetch_and_sub_uint32(&kernel_integrator_state.queue_counter->num_queued[current_kernel],
96 1);
97 INTEGRATOR_STATE_WRITE(state, path, queued_kernel) = 0;
98}
99
101 const DeviceKernel /*current_kernel*/)
102{
103 /* Queued kernel and counter is unmodified, so it will be re-executed. */
104 kernel_integrator_state.queue_counter->cache_miss = true;
105}
106
108 const DeviceKernel current_kernel)
109{
110 /* Queued kernel and counter is unmodified, so it will be re-executed. */
111 kernel_integrator_state.queue_counter->cache_miss = true;
112
113 if (!kernel_integrator_state.sort_key_counter[current_kernel]) {
114 return;
115 }
116
117 const int key_ = INTEGRATOR_STATE_WRITE(state, path, shader_sort_key);
118 atomic_fetch_and_add_uint32(&kernel_integrator_state.sort_key_counter[current_kernel][key_], 1);
119}
120
122 KernelGlobals kg, IntegratorState state, const DeviceKernel next_kernel, const bool is_ao)
123{
125 &kernel_integrator_state.next_shadow_path_index[0], 1);
126 atomic_fetch_and_add_uint32(&kernel_integrator_state.queue_counter->num_queued[next_kernel], 1);
127 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, queued_kernel) = next_kernel;
128# if defined(__PATH_GUIDING__)
129 if ((kernel_data.kernel_features & KERNEL_FEATURE_PATH_GUIDING)) {
130 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, path_segment) = nullptr;
131 }
132# endif
133 return shadow_state;
134}
135
137 const DeviceKernel current_kernel,
138 const DeviceKernel next_kernel)
139{
140 atomic_fetch_and_sub_uint32(&kernel_integrator_state.queue_counter->num_queued[current_kernel],
141 1);
142 atomic_fetch_and_add_uint32(&kernel_integrator_state.queue_counter->num_queued[next_kernel], 1);
143 INTEGRATOR_STATE_WRITE(state, shadow_path, queued_kernel) = next_kernel;
144}
145
147 const DeviceKernel current_kernel)
148{
149 atomic_fetch_and_sub_uint32(&kernel_integrator_state.queue_counter->num_queued[current_kernel],
150 1);
151 INTEGRATOR_STATE_WRITE(state, shadow_path, queued_kernel) = 0;
152}
153
155 IntegratorShadowState state, const DeviceKernel /*current_kernel*/)
156{
157 /* Queued kernel and counter is unmodified, so it will be re-executed. */
158 kernel_integrator_state.queue_counter->cache_miss = true;
159}
160
161/* Sort first by truncated state index (for good locality), then by key (for good coherence). */
162# define INTEGRATOR_SORT_KEY(key, state) \
163 (key + kernel_data.max_shaders * (state / kernel_integrator_state.sort_partition_divisor))
164
167 const DeviceKernel next_kernel,
168 const uint32_t key)
169{
170 const int key_ = INTEGRATOR_SORT_KEY(key, state);
171 atomic_fetch_and_add_uint32(&kernel_integrator_state.queue_counter->num_queued[next_kernel], 1);
172 INTEGRATOR_STATE_WRITE(state, path, queued_kernel) = next_kernel;
173 INTEGRATOR_STATE_WRITE(state, path, shader_sort_key) = key_;
174
175 if (!kernel_integrator_state.sort_key_counter[next_kernel]) {
176 return;
177 }
178
179 atomic_fetch_and_add_uint32(&kernel_integrator_state.sort_key_counter[next_kernel][key_], 1);
180}
181
184 const DeviceKernel current_kernel,
185 const DeviceKernel next_kernel,
186 const uint32_t key)
187{
188 const int key_ = INTEGRATOR_SORT_KEY(key, state);
189 atomic_fetch_and_sub_uint32(&kernel_integrator_state.queue_counter->num_queued[current_kernel],
190 1);
191 atomic_fetch_and_add_uint32(&kernel_integrator_state.queue_counter->num_queued[next_kernel], 1);
192 INTEGRATOR_STATE_WRITE(state, path, queued_kernel) = next_kernel;
193 INTEGRATOR_STATE_WRITE(state, path, shader_sort_key) = key_;
194
195 if (!kernel_integrator_state.sort_key_counter[next_kernel]) {
196 return;
197 }
198
199 atomic_fetch_and_add_uint32(&kernel_integrator_state.sort_key_counter[next_kernel][key_], 1);
200}
201
202#else
203
205 const DeviceKernel next_kernel)
206{
207 INTEGRATOR_STATE_WRITE(state, path, queued_kernel) = next_kernel;
208}
209
212 const DeviceKernel next_kernel,
213 const uint32_t key)
214{
215 INTEGRATOR_STATE_WRITE(state, path, queued_kernel) = next_kernel;
216 (void)key;
217}
218
220 const DeviceKernel current_kernel,
221 const DeviceKernel next_kernel)
222{
223 INTEGRATOR_STATE_WRITE(state, path, queued_kernel) = next_kernel;
224 (void)current_kernel;
225}
226
230 const DeviceKernel current_kernel)
231{
233
234 INTEGRATOR_STATE_WRITE(state, path, queued_kernel) = 0;
235 (void)current_kernel;
236}
237
239 const DeviceKernel /*current_kernel*/)
240{
241 kernel_assert(!"CPU kernel does not use texture cache miss mechanism");
242}
243
245 IntegratorState /*state*/, const DeviceKernel /*current_kernel*/)
246{
247 kernel_assert(!"CPU kernel does not use texture cache miss mechanism");
248}
249
252 const DeviceKernel current_kernel,
253 const DeviceKernel next_kernel,
254 const uint32_t key)
255{
256 INTEGRATOR_STATE_WRITE(state, path, queued_kernel) = next_kernel;
257 (void)key;
258 (void)current_kernel;
259}
260
264 const DeviceKernel next_kernel,
265 const bool is_ao)
266{
267 IntegratorShadowState shadow_state = (is_ao) ? &state->ao : &state->shadow;
268 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, queued_kernel) = next_kernel;
269# if defined(__PATH_GUIDING__)
270 if ((kernel_data.kernel_features & KERNEL_FEATURE_PATH_GUIDING)) {
271 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, path_segment) = nullptr;
272 }
273# else
274 (void)kg;
275# endif
276 return shadow_state;
277}
278
280 const DeviceKernel current_kernel,
281 const DeviceKernel next_kernel)
282{
283 INTEGRATOR_STATE_WRITE(state, shadow_path, queued_kernel) = next_kernel;
284 (void)current_kernel;
285}
286
288 const DeviceKernel current_kernel)
289{
290 INTEGRATOR_STATE_WRITE(state, shadow_path, queued_kernel) = 0;
291 (void)current_kernel;
292}
293
295 IntegratorShadowState /*state*/, const DeviceKernel /*current_kernel*/)
296{
297 kernel_assert(!"CPU kernel does not use texture cache miss mechanism");
298}
299
300#endif
301
ATOMIC_INLINE uint32_t atomic_fetch_and_add_uint32(uint32_t *p, uint32_t x)
#define kernel_assert(cond)
#define kernel_data
#define ccl_attr_maybe_unused
#define PASS_UNUSED
#define ccl_restrict
#define ccl_device_forceinline
#define KERNEL_FEATURE_PATH_GUIDING
#define ccl_device_inline
#define ccl_global
#define CCL_NAMESPACE_END
#define kernel_integrator_state
const int state
ccl_gpu_kernel_postfix ccl_global KernelWorkTile const int ccl_global float * render_buffer
@ PATH_RAY_VOLUME_PRIMARY_TRANSMIT
DeviceKernel
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
IntegratorStateCPU * IntegratorState
Definition state.h:227
const IntegratorStateCPU * ConstIntegratorState
Definition state.h:228
ccl_device_forceinline void integrator_shadow_path_terminate(IntegratorShadowState state, const DeviceKernel current_kernel)
Definition state_flow.h:287
ccl_device_forceinline void integrator_path_next_sorted(KernelGlobals, IntegratorState state, const DeviceKernel current_kernel, const DeviceKernel next_kernel, const uint32_t key)
Definition state_flow.h:250
CCL_NAMESPACE_BEGIN ccl_device_forceinline bool integrator_path_is_terminated(ConstIntegratorState state)
Definition state_flow.h:41
ccl_device_forceinline void integrator_path_init_sorted(KernelGlobals, IntegratorState state, const DeviceKernel next_kernel, const uint32_t key)
Definition state_flow.h:210
ccl_device_inline void write_optical_depth(KernelGlobals kg, IntegratorState state, ccl_global float *ccl_restrict render_buffer)
Definition state_flow.h:51
ccl_device_forceinline bool integrator_shadow_path_is_terminated(ConstIntegratorShadowState state)
Definition state_flow.h:46
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_path_init(IntegratorState state, const DeviceKernel next_kernel)
Definition state_flow.h:204
ccl_device_forceinline void integrator_path_cache_miss(IntegratorState, const DeviceKernel)
Definition state_flow.h:238
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_path_terminate(KernelGlobals kg, IntegratorState state, ccl_global float *ccl_restrict render_buffer, const DeviceKernel current_kernel)
Definition state_flow.h:227
ccl_device_forceinline void integrator_path_cache_miss_sorted(IntegratorState, const DeviceKernel)
Definition state_flow.h:244
ccl_device_forceinline void integrator_shadow_path_cache_miss(IntegratorShadowState, const DeviceKernel)
Definition state_flow.h:294
ccl_device_forceinline IntegratorShadowState integrator_shadow_path_init(ccl_attr_maybe_unused KernelGlobals kg, IntegratorState state, const DeviceKernel next_kernel, const bool is_ao)
Definition state_flow.h:262
ccl_device_inline void film_write_pass_float(ccl_global float *ccl_restrict buffer, const float value)
Definition write.h:58
CCL_NAMESPACE_BEGIN ccl_device_forceinline ccl_global float * film_pass_pixel_render_buffer(KernelGlobals kg, ConstIntegratorState state, ccl_global float *ccl_restrict render_buffer)
Definition write.h:24