Blender V5.0
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;
25
26/* Shader Manager */
27
29 public:
32
33 void device_update_specific(Device *device,
34 DeviceScene *dscene,
35 Scene *scene,
36 Progress &progress) override;
37 void device_free(Device *device, DeviceScene *dscene, Scene *scene) override;
38
39 protected:
40 void device_update_shader(Scene *scene,
41 Shader *shader,
42 Progress &progress,
43 array<int4> *svm_nodes);
44};
45
46/* Graph Compiler */
47
49 public:
50 struct Summary {
51 Summary();
52
53 /* Number of SVM nodes shader was compiled into. */
55
56 /* Peak stack usage during shader evaluation. */
58
59 /* Time spent on generating SVM nodes for surface shader. */
61
62 /* Time spent on generating SVM nodes for bump shader. */
64
65 /* Time spent on generating SVM nodes for volume shader. */
67
68 /* Time spent on generating SVM nodes for displacement shader. */
70
71 /* Total time spent on all routines. */
72 double time_total;
73
74 /* A full multi-line description of the state of the compiler after compilation. */
75 string full_report() const;
76 };
77
79 void compile(Shader *shader,
80 array<int4> &svm_nodes,
81 const int index,
82 Summary *summary = nullptr);
83
89 int stack_assign_if_not_equal(ShaderInput *input, const float value);
91 int stack_find_offset(const int size);
93 void stack_clear_offset(SocketType::Type type, const int offset);
95
96 void add_node(ShaderNodeType type, const int a = 0, const int b = 0, const int c = 0);
97 void add_node(const int a = 0, const int b = 0, const int c = 0, const int d = 0);
98 void add_node(ShaderNodeType type, const float3 &f);
99 void add_node(const float4 &f);
100 uint attribute(ustring name);
103 uint encode_uchar4(const uint x, const uint y = 0, const uint z = 0, const uint w = 0);
112
114 {
115 return current_type;
116 }
117
121
122 protected:
123 /* stack */
124 struct Stack {
126 {
127 memset(users, 0, sizeof(users));
128 }
129 Stack(const Stack &other)
130 {
131 memcpy(users, other.users, sizeof(users));
132 }
133 Stack &operator=(const Stack &other)
134 {
135 memcpy(users, other.users, sizeof(users));
136 return *this;
137 }
138
139 bool empty()
140 {
141 for (int i = 0; i < SVM_STACK_SIZE; i++) {
142 if (users[i]) {
143 return false;
144 }
145 }
146
147 return true;
148 }
149
150 void print()
151 {
152 printf("stack <");
153
154 for (int i = 0; i < SVM_STACK_SIZE; i++) {
155 printf((users[i]) ? "*" : " ");
156 }
157
158 printf(">\n");
159 }
160
162 };
163
164 /* Global state of the compiler accessible from the compilation routines. */
166 explicit CompilerState(ShaderGraph *graph);
167
168 /* ** Global state, used by various compilation steps. ** */
169
170 /* Set of nodes which were already compiled. */
172
173 /* Set of closures which were already compiled. */
175
176 /* Set of nodes used for writing AOVs. */
178
179 /* ** SVM nodes generation state ** */
180
181 /* Flag whether the node with corresponding ID was already compiled or
182 * not. Array element with index i corresponds to a node with such if.
183 *
184 * TODO(sergey): This is actually a copy of nodes_done just in another
185 * notation. We can de-duplicate this storage actually after switching
186 * all areas to use this flags array.
187 */
189
190 /* Node features that can be compiled. */
192 };
193
196 void stack_clear_users(ShaderNode *node, ShaderNodeSet &done);
197
198 /* single closure */
199 void find_dependencies(ShaderNodeSet &dependencies,
200 const ShaderNodeSet &done,
202 ShaderNode *skip_node = nullptr);
204 ShaderGraph *graph,
206 void generate_node(ShaderNode *node, ShaderNodeSet &done);
210 ShaderNode *node,
212 const ShaderNodeSet &shared);
214
215 /* multi closure */
217
218 /* compile */
219 void compile_type(Shader *shader, ShaderGraph *graph, ShaderType type);
220
221 std::atomic_int *svm_node_types_used;
230};
231
unsigned int uint
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
ShaderGraph * current_graph
Definition scene/svm.h:119
void compile_type(Shader *shader, ShaderGraph *graph, ShaderType type)
Definition svm.cpp:753
int max_stack_use
Definition scene/svm.h:226
void find_aov_nodes_and_dependencies(ShaderNodeSet &aov_nodes, ShaderGraph *graph, CompilerState *state)
Definition svm.cpp:580
bool background
Definition scene/svm.h:120
SVMCompiler(Scene *scene)
Definition svm.cpp:152
int stack_assign_if_not_equal(ShaderInput *input, const float value)
Definition svm.cpp:313
array< int4 > current_svm_nodes
Definition scene/svm.h:222
int stack_find_offset(const int size)
Definition svm.cpp:193
void generate_closure_node(ShaderNode *node, CompilerState *state)
Definition svm.cpp:512
ShaderType current_type
Definition scene/svm.h:223
uint bump_state_offset
Definition scene/svm.h:228
void compile(Shader *shader, array< int4 > &svm_nodes, const int index, Summary *summary=nullptr)
Definition svm.cpp:884
void generate_node(ShaderNode *node, ShaderNodeSet &done)
Definition svm.cpp:457
uint encode_uchar4(const uint x, const uint y=0, const uint z=0, const uint w=0)
Definition svm.cpp:391
int stack_assign_if_linked(ShaderInput *input)
Definition svm.cpp:295
Shader * current_shader
Definition scene/svm.h:224
void stack_clear_users(ShaderNode *node, ShaderNodeSet &done)
Definition svm.cpp:347
int stack_size(SocketType::Type type)
Definition svm.cpp:167
void find_dependencies(ShaderNodeSet &dependencies, const ShaderNodeSet &done, ShaderInput *input, ShaderNode *skip_node=nullptr)
Definition svm.cpp:441
void stack_clear_temporary(ShaderNode *node)
Definition svm.cpp:381
uint attribute_standard(ustring name)
Definition svm.cpp:435
void add_node(ShaderNodeType type, const int a=0, const int b=0, const int c=0)
Definition svm.cpp:406
Stack active_stack
Definition scene/svm.h:225
void stack_link(ShaderInput *input, ShaderOutput *output)
Definition svm.cpp:331
void generate_aov_node(ShaderNode *node, CompilerState *state)
ShaderType output_type()
Definition scene/svm.h:113
void generate_svm_nodes(const ShaderNodeSet &nodes, CompilerState *state)
Definition svm.cpp:481
uint closure_mix_weight_offset()
Definition scene/svm.h:104
bool is_linked(ShaderInput *input)
Definition svm.cpp:290
void generate_multi_closure(ShaderNode *root_node, ShaderNode *node, CompilerState *state)
Definition svm.cpp:599
uint get_bump_state_offset()
Definition scene/svm.h:108
uint attribute(ustring name)
Definition svm.cpp:425
void stack_clear_offset(SocketType::Type type, const int offset)
Definition svm.cpp:232
std::atomic_int * svm_node_types_used
Definition scene/svm.h:221
Scene * scene
Definition scene/svm.h:118
uint mix_weight_offset
Definition scene/svm.h:227
bool compile_failed
Definition scene/svm.h:229
void generated_shared_closure_nodes(ShaderNode *root_node, ShaderNode *node, CompilerState *state, const ShaderNodeSet &shared)
Definition svm.cpp:563
int stack_assign(ShaderOutput *output)
Definition svm.cpp:280
void device_update_shader(Scene *scene, Shader *shader, Progress &progress, array< int4 > *svm_nodes)
Definition svm.cpp:31
void device_free(Device *device, DeviceScene *dscene, Scene *scene) override
Definition svm.cpp:143
void device_update_specific(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress) override
Definition svm.cpp:51
~SVMShaderManager() override
#define SVM_STACK_SIZE
#define CCL_NAMESPACE_END
#define input
#define printf(...)
#define output
#define shared
ShaderNodeType
AttributeStandard
static ulong state[N]
const char * name
set< ShaderNode *, ShaderNodeIDComparator > ShaderNodeSet
ShaderNodeSet closure_done
Definition scene/svm.h:174
vector< bool > nodes_done_flag
Definition scene/svm.h:188
CompilerState(ShaderGraph *graph)
Definition svm.cpp:982
Stack & operator=(const Stack &other)
Definition scene/svm.h:133
int users[SVM_STACK_SIZE]
Definition scene/svm.h:161
Stack(const Stack &other)
Definition scene/svm.h:129
double time_generate_volume
Definition scene/svm.h:66
double time_generate_surface
Definition scene/svm.h:60
double time_generate_bump
Definition scene/svm.h:63
string full_report() const
Definition svm.cpp:962
double time_generate_displacement
Definition scene/svm.h:69
i
Definition text_draw.cc:230