Blender  V3.3
state.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 /* Integrator State
5  *
6  * This file defines the data structures that define the state of a path. Any state that is
7  * preserved and passed between kernel executions is part of this.
8  *
9  * The size of this state must be kept as small as possible, to reduce cache misses and keep memory
10  * usage under control on GPUs that may execute millions of kernels.
11  *
12  * Memory may be allocated and passed along in different ways depending on the device. There may
13  * be a scalar layout, or AoS or SoA layout for batches. The state may be passed along as a pointer
14  * to every kernel, or the pointer may exist at program scope or in constant memory. To abstract
15  * these differences between devices and experiment with different layouts, macros are used.
16  *
17  * Use IntegratorState to pass a reference to the integrator state for the current path. These are
18  * defined differently on the CPU and GPU. Use ConstIntegratorState instead of const
19  * IntegratorState for passing state as read-only, to avoid oddities in typedef behavior.
20  *
21  * INTEGRATOR_STATE(state, x, y): read nested struct member x.y of IntegratorState
22  * INTEGRATOR_STATE_WRITE(state, x, y): write to nested struct member x.y of IntegratorState
23  *
24  * INTEGRATOR_STATE_ARRAY(state, x, index, y): read x[index].y
25  * INTEGRATOR_STATE_ARRAY_WRITE(state, x, index, y): write x[index].y
26  *
27  * INTEGRATOR_STATE_NULL: use to pass empty state to other functions.
28  */
29 
30 #include "kernel/types.h"
31 
32 #include "util/types.h"
33 
34 #pragma once
35 
37 
38 /* Data structures */
39 
40 /* Integrator State
41  *
42  * CPU rendering path state with AoS layout. */
43 typedef struct IntegratorShadowStateCPU {
44 #define KERNEL_STRUCT_BEGIN(name) struct {
45 #define KERNEL_STRUCT_MEMBER(parent_struct, type, name, feature) type name;
46 #define KERNEL_STRUCT_ARRAY_MEMBER KERNEL_STRUCT_MEMBER
47 #define KERNEL_STRUCT_END(name) \
48  } \
49  name;
50 #define KERNEL_STRUCT_END_ARRAY(name, cpu_size, gpu_size) \
51  } \
52  name[cpu_size];
53 #define KERNEL_STRUCT_VOLUME_STACK_SIZE MAX_VOLUME_STACK_SIZE
55 #undef KERNEL_STRUCT_BEGIN
56 #undef KERNEL_STRUCT_MEMBER
57 #undef KERNEL_STRUCT_ARRAY_MEMBER
58 #undef KERNEL_STRUCT_END
59 #undef KERNEL_STRUCT_END_ARRAY
61 
62 typedef struct IntegratorStateCPU {
63 #define KERNEL_STRUCT_BEGIN(name) struct {
64 #define KERNEL_STRUCT_MEMBER(parent_struct, type, name, feature) type name;
65 #define KERNEL_STRUCT_ARRAY_MEMBER KERNEL_STRUCT_MEMBER
66 #define KERNEL_STRUCT_END(name) \
67  } \
68  name;
69 #define KERNEL_STRUCT_END_ARRAY(name, cpu_size, gpu_size) \
70  } \
71  name[cpu_size];
72 #define KERNEL_STRUCT_VOLUME_STACK_SIZE MAX_VOLUME_STACK_SIZE
74 #undef KERNEL_STRUCT_BEGIN
75 #undef KERNEL_STRUCT_MEMBER
76 #undef KERNEL_STRUCT_ARRAY_MEMBER
77 #undef KERNEL_STRUCT_END
78 #undef KERNEL_STRUCT_END_ARRAY
79 #undef KERNEL_STRUCT_VOLUME_STACK_SIZE
80 
84 
85 /* Path Queue
86  *
87  * Keep track of which kernels are queued to be executed next in the path
88  * for GPU rendering. */
89 typedef struct IntegratorQueueCounter {
92 
93 /* Integrator State GPU
94  *
95  * GPU rendering path state with SoA layout. */
96 typedef struct IntegratorStateGPU {
97 #define KERNEL_STRUCT_BEGIN(name) struct {
98 #define KERNEL_STRUCT_MEMBER(parent_struct, type, name, feature) ccl_global type *name;
99 #define KERNEL_STRUCT_ARRAY_MEMBER KERNEL_STRUCT_MEMBER
100 #define KERNEL_STRUCT_END(name) \
101  } \
102  name;
103 #define KERNEL_STRUCT_END_ARRAY(name, cpu_size, gpu_size) \
104  } \
105  name[gpu_size];
106 #define KERNEL_STRUCT_VOLUME_STACK_SIZE MAX_VOLUME_STACK_SIZE
107 
109 
111 
112 #undef KERNEL_STRUCT_BEGIN
113 #undef KERNEL_STRUCT_MEMBER
114 #undef KERNEL_STRUCT_ARRAY_MEMBER
115 #undef KERNEL_STRUCT_END
116 #undef KERNEL_STRUCT_END_ARRAY
117 #undef KERNEL_STRUCT_VOLUME_STACK_SIZE
118 
119  /* Count number of queued kernels. */
121 
122  /* Count number of kernels queued for specific shaders. */
124 
125  /* Index of shadow path which will be used by a next shadow path. */
127 
128  /* Index of main path which will be used by a next shadow catcher split. */
130 
131  /* Divisor used to partition active indices by locality when sorting by material. */
134 
135 /* Abstraction
136  *
137  * Macros to access data structures on different devices.
138  *
139  * Note that there is a special access function for the shadow catcher state. This access is to
140  * happen from a kernel which operates on a "main" path. Attempt to use shadow catcher accessors
141  * from a kernel which operates on a shadow catcher state will cause bad memory access. */
142 
143 #ifndef __KERNEL_GPU__
144 
145 /* Scalar access on CPU. */
146 
151 
152 # define INTEGRATOR_STATE_NULL nullptr
153 
154 # define INTEGRATOR_STATE(state, nested_struct, member) ((state)->nested_struct.member)
155 # define INTEGRATOR_STATE_WRITE(state, nested_struct, member) ((state)->nested_struct.member)
156 
157 # define INTEGRATOR_STATE_ARRAY(state, nested_struct, array_index, member) \
158  ((state)->nested_struct[array_index].member)
159 # define INTEGRATOR_STATE_ARRAY_WRITE(state, nested_struct, array_index, member) \
160  ((state)->nested_struct[array_index].member)
161 
162 #else /* !__KERNEL_GPU__ */
163 
164 /* Array access on GPU with Structure-of-Arrays. */
165 
166 typedef int IntegratorState;
167 typedef int ConstIntegratorState;
168 typedef int IntegratorShadowState;
169 typedef int ConstIntegratorShadowState;
170 
171 # define INTEGRATOR_STATE_NULL -1
172 
173 # define INTEGRATOR_STATE(state, nested_struct, member) \
174  kernel_integrator_state.nested_struct.member[state]
175 # define INTEGRATOR_STATE_WRITE(state, nested_struct, member) \
176  INTEGRATOR_STATE(state, nested_struct, member)
177 
178 # define INTEGRATOR_STATE_ARRAY(state, nested_struct, array_index, member) \
179  kernel_integrator_state.nested_struct[array_index].member[state]
180 # define INTEGRATOR_STATE_ARRAY_WRITE(state, nested_struct, array_index, member) \
181  INTEGRATOR_STATE_ARRAY(state, nested_struct, array_index, member)
182 
183 #endif /* !__KERNEL_GPU__ */
184 
unsigned int uint
Definition: BLI_sys_types.h:67
#define ccl_restrict
Definition: cuda/compat.h:50
#define ccl_global
Definition: cuda/compat.h:43
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
@ DEVICE_KERNEL_INTEGRATOR_NUM
const IntegratorShadowStateCPU *ccl_restrict ConstIntegratorShadowState
Definition: state.h:150
CCL_NAMESPACE_BEGIN struct IntegratorShadowStateCPU IntegratorShadowStateCPU
struct IntegratorQueueCounter IntegratorQueueCounter
struct IntegratorStateGPU IntegratorStateGPU
IntegratorStateCPU *ccl_restrict IntegratorState
Definition: state.h:147
const IntegratorStateCPU *ccl_restrict ConstIntegratorState
Definition: state.h:148
IntegratorShadowStateCPU *ccl_restrict IntegratorShadowState
Definition: state.h:149
struct IntegratorStateCPU IntegratorStateCPU
int num_queued[DEVICE_KERNEL_INTEGRATOR_NUM]
Definition: state.h:90
IntegratorShadowStateCPU shadow
Definition: state.h:81
IntegratorShadowStateCPU ao
Definition: state.h:82
ccl_global IntegratorQueueCounter * queue_counter
Definition: state.h:120
ccl_global int * next_shadow_path_index
Definition: state.h:126
ccl_global int * next_main_path_index
Definition: state.h:129
ccl_global int * sort_key_counter[DEVICE_KERNEL_INTEGRATOR_NUM]
Definition: state.h:123
uint sort_partition_divisor
Definition: state.h:132