Blender V5.3
scene/svm.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 <atomic>
8
9#include "scene/shader.h"
10#include "scene/shader_graph.h"
11
12#include "util/array.h"
13#include "util/string.h"
14
16
17class Device;
18class DeviceScene;
19class ImageManager;
20class Scene;
21class ShaderGraph;
22class ShaderInput;
23class ShaderNode;
24class ShaderOutput;
26
27/* Shader Manager */
28
30 public:
33
34 void device_update_specific(Device *device,
35 DeviceScene *dscene,
36 Scene *scene,
37 Progress &progress) override;
38 void device_free(Device *device, DeviceScene *dscene, Scene *scene) override;
39
40 protected:
41 void device_update_shader(Scene *scene,
42 Shader *shader,
43 Progress &progress,
44 array<int> *svm_nodes);
45};
46
47/* Graph Compiler */
48
50 public:
51 struct Summary {
52 Summary();
53
54 /* Number of SVM nodes shader was compiled into. */
56
57 /* Peak stack usage during shader evaluation. */
59
60 /* Time spent on generating SVM nodes for surface shader. */
62
63 /* Time spent on generating SVM nodes for bump shader. */
65
66 /* Time spent on generating SVM nodes for volume shader. */
68
69 /* Time spent on generating SVM nodes for displacement shader. */
71
72 /* Total time spent on all routines. */
73 double time_total;
74
75 /* A full multi-line description of the state of the compiler after compilation. */
76 string full_report() const;
77 };
78
80 void compile(Shader *shader, array<int> &svm_nodes, const int index, Summary *summary = nullptr);
81
82 /* Create input and output node parameters for struct T passed to add_node. */
83 SVMInputInt input_int(const char *name);
84 SVMInputFloat input_float(const char *name);
85 SVMInputFloat3 input_float3(const char *name);
87 SVMStackOffset input_link(const char *name);
88 SVMStackOffset output(const char *name);
89 SVMStackOffset output(ShaderOutput *shader_output);
90
91 /* Add simple SVM node without parameters. */
92 void add_node(ShaderNodeType type);
93
94 /* Add SVM node with parameters in struct T. */
95 template<typename T>
96 void add_node(ShaderNode *shader_node,
97 const ShaderNodeType type,
98 const T &node,
99 const bool use_derivatives = false)
100 requires(std::is_class_v<T> && sizeof(T) % sizeof(int) == 0 && alignof(T) <= sizeof(uint))
101 {
102 const ShaderNodeType resolved_type = node_type(shader_node, type, use_derivatives);
103 current_svm_nodes.push_back_slow(resolved_type);
104 const int *data = reinterpret_cast<const int *>(&node);
105 svm_node_types_used[resolved_type] = true;
106 for (size_t i = 0; i < sizeof(T) / sizeof(int); i++) {
108 }
109 if (shader_node) {
110 shader_node->added_to_svm = true;
111 }
112 }
113
114 /* Add value node. */
115 void add_value_node(ShaderNode *shader_node, const float value, const int stack_offset);
116 void add_value_node(ShaderNode *shader_node, const float3 &value, const int stack_offset);
117
118 /* Add BSDF node. */
119 template<typename T> void add_bsdf_node(const SVMNodeClosureBsdf &node, const T &data)
120 {
121 assert(current_node->shader_node_type() == NODE_CLOSURE_BSDF);
122 add_node(current_node, NODE_CLOSURE_BSDF, node);
123 add_node_data(data);
124 }
125
126 /* Add extra node data following add_node. */
127 template<typename T>
128 void add_node_data(const T &data)
129 requires(std::is_class_v<T> && sizeof(T) % sizeof(int) == 0 && alignof(T) <= sizeof(uint))
130 {
131 const int *ptr = reinterpret_cast<const int *>(&data);
132 for (size_t i = 0; i < sizeof(T) / sizeof(int); i++) {
134 }
135 }
136 void add_node_data_float4(const float4 &f);
137 void add_node_data_float(const float f);
138
139 /* Low level input and output handling for some special nodes. Usually the functions
140 * above should be used instead of these. */
143 void stack_clear_offset(const ShaderIO *io, const SVMStackOffset offset);
145
146 uint attribute(ustring name);
148 uint attribute_standard(ustring name);
157
159 {
160 return current_type;
161 }
162
168
169 protected:
170 /* stack */
171 struct Stack {
173 {
174 memset(users, 0, sizeof(users));
175 }
176 Stack(const Stack &other)
177 {
178 memcpy(users, other.users, sizeof(users));
179 }
180 Stack &operator=(const Stack &other)
181 {
182 memcpy(users, other.users, sizeof(users));
183 return *this;
184 }
185
186 bool empty()
187 {
188 for (int i = 0; i < SVM_STACK_SIZE; i++) {
189 if (users[i]) {
190 return false;
191 }
192 }
193
194 return true;
195 }
196
197 void print()
198 {
199 printf("stack <");
200
201 for (int i = 0; i < SVM_STACK_SIZE; i++) {
202 printf((users[i]) ? "*" : " ");
203 }
204
205 printf(">\n");
206 }
207
209 };
210
211 /* Global state of the compiler accessible from the compilation routines. */
213 explicit CompilerState(ShaderGraph *graph);
214
215 /* ** Global state, used by various compilation steps. ** */
216
217 /* Set of nodes which were already compiled. */
219
220 /* Set of closures which were already compiled. */
222
223 /* Set of nodes used for writing AOVs. */
225
226 /* ** SVM nodes generation state ** */
227
228 /* Flag whether the node with corresponding ID was already compiled or
229 * not. Array element with index i corresponds to a node with such if.
230 *
231 * TODO(sergey): This is actually a copy of nodes_done just in another
232 * notation. We can de-duplicate this storage actually after switching
233 * all areas to use this flags array.
234 */
236
237 /* Node features that can be compiled. */
239 };
240
241 ShaderNodeType node_type(const ShaderNode *shader_node,
242 const ShaderNodeType type,
243 const bool use_derivatives);
244
246 SVMStackOffset stack_find_offset(const int size);
247
250 int stack_size(const ShaderIO *io);
252 bool is_sole_user(const ShaderNode *node, const ShaderOutput *output, const ShaderNodeSet &done);
254
255 /* Stack size that will be allocated for the outputs of this node. */
257
258 /* single closure */
259 void find_dependencies(ShaderNodeSet &dependencies,
260 const ShaderNodeSet &done,
261 ShaderInput *input,
262 ShaderNode *skip_node = nullptr);
264 ShaderGraph *graph,
272 const ShaderNodeSet &shared);
274
275 /* multi closure */
277
278 /* compile */
279 void compile_type(Shader *shader, ShaderGraph *graph, ShaderType type);
280
281 std::atomic_int *svm_node_types_used;
290};
291
unsigned long long int uint64_t
#define output
ShaderGraph * current_graph
Definition scene/svm.h:165
Progress & progress
Definition scene/svm.h:164
void compile_type(Shader *shader, ShaderGraph *graph, ShaderType type)
Definition svm.cpp:1039
int max_stack_use
Definition scene/svm.h:286
SVMStackOffset input_link(const char *name)
Definition svm.cpp:348
ShaderNodeType node_type(const ShaderNode *shader_node, const ShaderNodeType type, const bool use_derivatives)
Definition svm.cpp:455
void find_aov_nodes_and_dependencies(ShaderNodeSet &aov_nodes, ShaderGraph *graph, CompilerState *state)
Definition svm.cpp:829
SVMCompiler(Scene *scene, Progress &progress)
Definition svm.cpp:157
bool background
Definition scene/svm.h:167
SVMStackOffset output(const char *name)
Definition svm.cpp:360
void compile(Shader *shader, array< int > &svm_nodes, const int index, Summary *summary=nullptr)
Definition svm.cpp:1177
SVMInputFloat input_float(const char *name)
Definition svm.cpp:310
void generate_closure_node(ShaderNode *node, CompilerState *state)
Definition svm.cpp:758
void add_value_node(ShaderNode *shader_node, const float value, const int stack_offset)
Definition svm.cpp:480
ShaderType current_type
Definition scene/svm.h:283
SVMStackOffset bump_state_offset
Definition scene/svm.h:288
void generate_node(ShaderNode *node, ShaderNodeSet &done)
Definition svm.cpp:567
void add_node_data(const T &data)
Definition scene/svm.h:128
void stack_zero_incomplete_derivatives(const ShaderNode *node)
Definition svm.cpp:504
void add_bsdf_node(const SVMNodeClosureBsdf &node, const T &data)
Definition scene/svm.h:119
SVMStackOffset stack_find_offset(const ShaderIO *io)
Definition svm.cpp:240
Shader * current_shader
Definition scene/svm.h:284
void stack_clear_users(ShaderNode *node, ShaderNodeSet &done)
Definition svm.cpp:402
int stack_size(SocketType::Type type)
Definition svm.cpp:172
int stack_node_output_size(const ShaderNode *node)
Definition svm.cpp:594
void add_node(ShaderNodeType type)
Definition svm.cpp:434
void find_dependencies(ShaderNodeSet &dependencies, const ShaderNodeSet &done, ShaderInput *input, ShaderNode *skip_node=nullptr)
Definition svm.cpp:552
void stack_clear_temporary(ShaderNode *node)
Definition svm.cpp:424
SVMStackOffset closure_mix_weight_offset()
Definition scene/svm.h:149
uint attribute_standard(ustring name)
Definition svm.cpp:546
Stack active_stack
Definition scene/svm.h:285
bool is_sole_user(const ShaderNode *node, const ShaderOutput *output, const ShaderNodeSet &done)
Definition svm.cpp:386
void stack_link(ShaderInput *input, ShaderOutput *output)
Definition svm.cpp:371
void generate_aov_node(ShaderNode *node, CompilerState *state)
ShaderType output_type()
Definition scene/svm.h:158
ShaderNode * current_node
Definition scene/svm.h:166
void add_node_data_float(const float f)
Definition svm.cpp:475
SVMStackOffset mix_weight_offset
Definition scene/svm.h:287
void generate_svm_nodes(const ShaderNodeSet &nodes, CompilerState *state)
Definition svm.cpp:606
array< int > current_svm_nodes
Definition scene/svm.h:282
void generate_multi_closure(ShaderNode *root_node, ShaderNode *node, CompilerState *state)
Definition svm.cpp:848
void add_node_data_float4(const float4 &f)
Definition svm.cpp:467
SVMInputInt input_int(const char *name)
Definition svm.cpp:300
SVMStackOffset get_bump_state_offset()
Definition scene/svm.h:153
SVMStackOffset stack_assign(ShaderInput *input)
Definition svm.cpp:254
std::atomic_int * svm_node_types_used
Definition scene/svm.h:281
Scene * scene
Definition scene/svm.h:163
SVMInputFloat3 input_float3(const char *name)
Definition svm.cpp:324
bool compile_failed
Definition scene/svm.h:289
void stack_clear_offset(const ShaderIO *io, const SVMStackOffset offset)
Definition svm.cpp:245
void generated_shared_closure_nodes(ShaderNode *root_node, ShaderNode *node, CompilerState *state, const ShaderNodeSet &shared)
Definition svm.cpp:812
void add_node(ShaderNode *shader_node, const ShaderNodeType type, const T &node, const bool use_derivatives=false)
Definition scene/svm.h:96
SVMInputFloat3 input_float3_from_offset(SVMStackOffset offset)
Definition svm.cpp:343
void device_free(Device *device, DeviceScene *dscene, Scene *scene) override
Definition svm.cpp:148
void device_update_specific(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress) override
Definition svm.cpp:57
~SVMShaderManager() override
void device_update_shader(Scene *scene, Shader *shader, Progress &progress, array< int > *svm_nodes)
Definition svm.cpp:37
Definition scene.h:133
virtual ShaderNodeType shader_node_type() const
void push_back_slow(const T &t)
#define printf
#define SVM_STACK_SIZE
#define CCL_NAMESPACE_END
#define assert(assertion)
#define shared
#define index(i)
#define attribute(slot)
const int state
ShaderNodeType
uint8_t SVMStackOffset
AttributeStandard
#define T
set< ShaderNode *, ShaderNodeIDComparator > ShaderNodeSet
ShaderNodeSet closure_done
Definition scene/svm.h:221
vector< bool > nodes_done_flag
Definition scene/svm.h:235
CompilerState(ShaderGraph *graph)
Definition svm.cpp:1274
Stack & operator=(const Stack &other)
Definition scene/svm.h:180
int users[SVM_STACK_SIZE]
Definition scene/svm.h:208
Stack(const Stack &other)
Definition scene/svm.h:176
double time_generate_volume
Definition scene/svm.h:67
double time_generate_surface
Definition scene/svm.h:61
double time_generate_bump
Definition scene/svm.h:64
string full_report() const
Definition svm.cpp:1254
double time_generate_displacement
Definition scene/svm.h:70