Blender V5.3
state.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/* Integrator State
6 *
7 * This file defines the data structures that define the state of a path. Any state that is
8 * preserved and passed between kernel executions is part of this.
9 *
10 * The size of this state must be kept as small as possible, to reduce cache misses and keep memory
11 * usage under control on GPUs that may execute millions of kernels.
12 *
13 * Memory may be allocated and passed along in different ways depending on the device. There may
14 * be a scalar layout, or AoS or SoA layout for batches. The state may be passed along as a pointer
15 * to every kernel, or the pointer may exist at program scope or in constant memory. To abstract
16 * these differences between devices and experiment with different layouts, macros are used.
17 *
18 * Use IntegratorState to pass a reference to the integrator state for the current path. These are
19 * defined differently on the CPU and GPU. Use ConstIntegratorState instead of const
20 * IntegratorState for passing state as read-only, to avoid oddities in typedef behavior.
21 *
22 * INTEGRATOR_STATE(state, x, y): read nested struct member x.y of IntegratorState
23 * INTEGRATOR_STATE_WRITE(state, x, y): write to nested struct member x.y of IntegratorState
24 *
25 * INTEGRATOR_STATE_ARRAY(state, x, index, y): read x[index].y
26 * INTEGRATOR_STATE_ARRAY_WRITE(state, x, index, y): write x[index].y
27 */
28
29#include "kernel/types.h"
30
31#include "util/types.h"
32
33#if defined(__PATH_GUIDING__)
34# include "util/guiding.h" // IWYU pragma: keep
35#endif
36
37#pragma once
38
41/* Data structures */
42
43/* Integrator State
44 *
45 * CPU rendering path state with AoS layout. */
47#define KERNEL_STRUCT_BEGIN(name) struct {
48#define KERNEL_STRUCT_BEGIN_PACKED(parent_struct, feature) struct {
49#define KERNEL_STRUCT_MEMBER(parent_struct, type, name, feature) type name;
50#define KERNEL_STRUCT_MEMBER_PACKED KERNEL_STRUCT_MEMBER
51#define KERNEL_STRUCT_ARRAY_MEMBER KERNEL_STRUCT_MEMBER
52#define KERNEL_STRUCT_END(name) \
53 } \
54 name;
55#define KERNEL_STRUCT_END_ARRAY(name, cpu_size, gpu_size) \
56 } \
57 name[cpu_size];
58#define KERNEL_STRUCT_VOLUME_STACK_SIZE MAX_VOLUME_STACK_SIZE
60#undef KERNEL_STRUCT_BEGIN
61#undef KERNEL_STRUCT_BEGIN_PACKED
62#undef KERNEL_STRUCT_MEMBER
63#undef KERNEL_STRUCT_MEMBER_PACKED
64#undef KERNEL_STRUCT_ARRAY_MEMBER
65#undef KERNEL_STRUCT_END
66#undef KERNEL_STRUCT_END_ARRAY
67};
68
70#define KERNEL_STRUCT_BEGIN(name) struct {
71#define KERNEL_STRUCT_BEGIN_PACKED(parent_struct, feature) struct {
72#define KERNEL_STRUCT_MEMBER(parent_struct, type, name, feature) type name;
73#define KERNEL_STRUCT_MEMBER_PACKED KERNEL_STRUCT_MEMBER
74#define KERNEL_STRUCT_ARRAY_MEMBER KERNEL_STRUCT_MEMBER
75#define KERNEL_STRUCT_END(name) \
76 } \
77 name;
78#define KERNEL_STRUCT_END_ARRAY(name, cpu_size, gpu_size) \
79 } \
80 name[cpu_size];
81#define KERNEL_STRUCT_VOLUME_STACK_SIZE MAX_VOLUME_STACK_SIZE
83#undef KERNEL_STRUCT_BEGIN
84#undef KERNEL_STRUCT_BEGIN_PACKED
85#undef KERNEL_STRUCT_MEMBER
86#undef KERNEL_STRUCT_MEMBER_PACKED
87#undef KERNEL_STRUCT_ARRAY_MEMBER
88#undef KERNEL_STRUCT_END
89#undef KERNEL_STRUCT_END_ARRAY
90#undef KERNEL_STRUCT_VOLUME_STACK_SIZE
91
94};
95
96/* Path Queue
97 *
98 * Keep track of which kernels are queued to be executed next in the path
99 * for GPU rendering. */
104
105#if defined(__INTEGRATOR_GPU_PACKED_STATE__) && defined(__KERNEL_GPU__)
106
107/* Generate wrapper structs for all integrator state fields. This allows us to access state
108 * uniformly, regardless of whether it stored in a packed struct or separate arrays. */
109# define KERNEL_STRUCT_BEGIN(name)
110# define KERNEL_STRUCT_MEMBER(parent_struct, type, name, feature) \
111 struct Wrapped_##parent_struct##_##name { \
112 type name; \
113 };
114# define KERNEL_STRUCT_MEMBER_PACKED KERNEL_STRUCT_MEMBER
115# define KERNEL_STRUCT_BEGIN_PACKED(parent_struct, feature) \
116 KERNEL_STRUCT_BEGIN(parent_struct) \
117 KERNEL_STRUCT_MEMBER(parent_struct, packed_##parent_struct, packed, feature)
118# define KERNEL_STRUCT_ARRAY_MEMBER KERNEL_STRUCT_MEMBER
119# define KERNEL_STRUCT_END(name)
120# define KERNEL_STRUCT_END_ARRAY(name, cpu_size, gpu_size)
121# define KERNEL_STRUCT_VOLUME_STACK_SIZE MAX_VOLUME_STACK_SIZE
122
125
126# undef KERNEL_STRUCT_BEGIN
127# undef KERNEL_STRUCT_BEGIN_PACKED
128# undef KERNEL_STRUCT_MEMBER
129# undef KERNEL_STRUCT_MEMBER_PACKED
130# undef KERNEL_STRUCT_ARRAY_MEMBER
131# undef KERNEL_STRUCT_END
132# undef KERNEL_STRUCT_END_ARRAY
133# undef KERNEL_STRUCT_VOLUME_STACK_SIZE
134
135#endif
136
137/* Integrator State GPU
138 *
139 * GPU rendering path state with SoA layout. */
141#define KERNEL_STRUCT_BEGIN(name) struct {
142
143#ifdef __INTEGRATOR_GPU_PACKED_STATE__
144
145# ifdef __KERNEL_GPU__
146
147/* If we've opted in to packed layouts, generate member functions that return a pointer to a
148 * wrapper type so we can access state using uniform syntax. */
149# define KERNEL_STRUCT_MEMBER(parent_struct, type, name, feature) \
150 ccl_global Wrapped_##parent_struct##_##name *name; \
151 ccl_device_inline ccl_global Wrapped_##parent_struct##_##name *name##_fn() ccl_constant \
152 { \
153 return (ccl_global Wrapped_##parent_struct##_##name *)name; \
154 }
155# define KERNEL_STRUCT_MEMBER_PACKED(parent_struct, type, name, feature) \
156 ccl_device_inline ccl_global packed_##parent_struct *name##_fn() ccl_constant \
157 { \
158 return (ccl_global packed_##parent_struct *)packed; \
159 }
160# else
161# define KERNEL_STRUCT_MEMBER(parent_struct, type, name, feature) ccl_global type *name;
162# define KERNEL_STRUCT_MEMBER_PACKED(parent_struct, type, name, feature)
163# endif
164
165# define KERNEL_STRUCT_BEGIN_PACKED(parent_struct, feature) \
166 KERNEL_STRUCT_BEGIN(parent_struct) \
167 KERNEL_STRUCT_MEMBER(parent_struct, packed_##parent_struct, packed, feature)
168
169#else
170# define KERNEL_STRUCT_MEMBER(parent_struct, type, name, feature) ccl_global type *name;
171# define KERNEL_STRUCT_MEMBER_PACKED KERNEL_STRUCT_MEMBER
172# define KERNEL_STRUCT_BEGIN_PACKED(parent_struct, feature) KERNEL_STRUCT_BEGIN(parent_struct)
173#endif
174#define KERNEL_STRUCT_ARRAY_MEMBER KERNEL_STRUCT_MEMBER
175#define KERNEL_STRUCT_END(name) \
176 } \
177 name;
178#define KERNEL_STRUCT_END_ARRAY(name, cpu_size, gpu_size) \
179 } \
180 name[gpu_size];
181#define KERNEL_STRUCT_VOLUME_STACK_SIZE MAX_VOLUME_STACK_SIZE
182
184
186
187#undef KERNEL_STRUCT_BEGIN
188#undef KERNEL_STRUCT_BEGIN_PACKED
189#undef KERNEL_STRUCT_MEMBER
190#undef KERNEL_STRUCT_MEMBER_PACKED
191#undef KERNEL_STRUCT_ARRAY_MEMBER
192#undef KERNEL_STRUCT_END
193#undef KERNEL_STRUCT_END_ARRAY
194#undef KERNEL_STRUCT_VOLUME_STACK_SIZE
195
196 /* Count number of queued kernels. */
197 ccl_global IntegratorQueueCounter *queue_counter;
198
199 /* Count number of kernels queued for specific shaders. */
201
202 /* Index of shadow path which will be used by a next shadow path. */
204
205 /* Index of main path which will be used by a next shadow catcher split. */
207
208 /* Partition/key offsets used when writing sorted active indices. */
210
211 /* Divisor used to partition active indices by locality when sorting by material. */
213};
214
215/* Abstraction
216 *
217 * Macros to access data structures on different devices.
218 *
219 * Note that there is a special access function for the shadow catcher state. This access is to
220 * happen from a kernel which operates on a "main" path. Attempt to use shadow catcher accessors
221 * from a kernel which operates on a shadow catcher state will cause bad memory access. */
222
223#ifndef __KERNEL_GPU__
224
225/* Scalar access on CPU. */
226
233
234# define INTEGRATOR_STATE(state, nested_struct, member) ((state)->nested_struct.member)
235# define INTEGRATOR_STATE_WRITE(state, nested_struct, member) ((state)->nested_struct.member)
236
237# define INTEGRATOR_STATE_ARRAY(state, nested_struct, array_index, member) \
238 ((state)->nested_struct[array_index].member)
239# define INTEGRATOR_STATE_ARRAY_WRITE(state, nested_struct, array_index, member) \
240 ((state)->nested_struct[array_index].member)
242#else /* !__KERNEL_GPU__ */
243
244/* Array access on GPU with Structure-of-Arrays. */
245
246using IntegratorState = int;
247using ConstIntegratorState = int;
248
249/* Shadow state is wrapped in a struct to support function overloading and templates. */
253 ccl_device_inline_method operator int() const
254 {
255 return state;
256 }
257 int state;
258};
260
261struct IntegratorBakeState {};
263
264# ifdef __INTEGRATOR_GPU_PACKED_STATE__
266/* If we've opted in to packed layouts, we use the generated accessor functions (member##_fn) to
267 * resolve different layouts (packed vs separate). */
268# define INTEGRATOR_STATE(state, nested_struct, member) \
269 kernel_integrator_state.nested_struct.member##_fn()[state].member
270# define INTEGRATOR_STATE_ARRAY(state, nested_struct, array_index, member) \
271 kernel_integrator_state.nested_struct[array_index].member##_fn()[state].member
272# else
273# define INTEGRATOR_STATE(state, nested_struct, member) \
274 kernel_integrator_state.nested_struct.member[state]
275# define INTEGRATOR_STATE_ARRAY(state, nested_struct, array_index, member) \
276 kernel_integrator_state.nested_struct[array_index].member[state]
277# endif
278
279# define INTEGRATOR_STATE_WRITE(state, nested_struct, member) \
280 INTEGRATOR_STATE(state, nested_struct, member)
281# define INTEGRATOR_STATE_ARRAY_WRITE(state, nested_struct, array_index, member) \
282 INTEGRATOR_STATE_ARRAY(state, nested_struct, array_index, member)
283
284#endif /* !__KERNEL_GPU__ */
285
#define ccl_global
#define ccl_device_inline_method
#define CCL_NAMESPACE_END
const int state
@ DEVICE_GPU_KERNEL_INTEGRATOR_NUM
IntegratorShadowStateCPU * IntegratorShadowState
Definition state.h:229
IntegratorBakeState ConstIntegratorBakeState
Definition state.h:232
const IntegratorShadowStateCPU * ConstIntegratorShadowState
Definition state.h:230
IntegratorStateCPU * IntegratorState
Definition state.h:227
const IntegratorStateCPU * ConstIntegratorState
Definition state.h:228
int num_queued[DEVICE_GPU_KERNEL_INTEGRATOR_NUM]
Definition state.h:101
IntegratorShadowStateCPU ao
Definition state.h:200
ccl_global int * next_shadow_path_index
Definition state.h:331
ccl_global int * next_main_path_index
Definition state.h:334
ccl_global int * sort_partition_key_offsets
Definition state.h:337
uint sort_partition_divisor
Definition state.h:340
ccl_global int * sort_key_counter[DEVICE_GPU_KERNEL_INTEGRATOR_NUM]
Definition state.h:328