Blender V4.5
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,
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_find_offset(const int size);
91 void stack_clear_offset(SocketType::Type type, const int offset);
93
94 void add_node(ShaderNodeType type, const int a = 0, const int b = 0, const int c = 0);
95 void add_node(const int a = 0, const int b = 0, const int c = 0, const int d = 0);
96 void add_node(ShaderNodeType type, const float3 &f);
97 void add_node(const float4 &f);
98 uint attribute(ustring name);
100 uint attribute_standard(ustring name);
101 uint encode_uchar4(const uint x, const uint y = 0, const uint z = 0, const uint w = 0);
110
112 {
113 return current_type;
114 }
115
119
120 protected:
121 /* stack */
122 struct Stack {
124 {
125 memset(users, 0, sizeof(users));
126 }
127 Stack(const Stack &other)
128 {
129 memcpy(users, other.users, sizeof(users));
130 }
131 Stack &operator=(const Stack &other)
132 {
133 memcpy(users, other.users, sizeof(users));
134 return *this;
135 }
136
137 bool empty()
138 {
139 for (int i = 0; i < SVM_STACK_SIZE; i++) {
140 if (users[i]) {
141 return false;
142 }
143 }
144
145 return true;
146 }
147
148 void print()
149 {
150 printf("stack <");
151
152 for (int i = 0; i < SVM_STACK_SIZE; i++) {
153 printf((users[i]) ? "*" : " ");
154 }
155
156 printf(">\n");
157 }
158
160 };
161
162 /* Global state of the compiler accessible from the compilation routines. */
164 explicit CompilerState(ShaderGraph *graph);
165
166 /* ** Global state, used by various compilation steps. ** */
167
168 /* Set of nodes which were already compiled. */
170
171 /* Set of closures which were already compiled. */
173
174 /* Set of nodes used for writing AOVs. */
176
177 /* ** SVM nodes generation state ** */
178
179 /* Flag whether the node with corresponding ID was already compiled or
180 * not. Array element with index i corresponds to a node with such if.
181 *
182 * TODO(sergey): This is actually a copy of nodes_done just in another
183 * notation. We can de-duplicate this storage actually after switching
184 * all areas to use this flags array.
185 */
187
188 /* Node features that can be compiled. */
190 };
191
194 void stack_clear_users(ShaderNode *node, ShaderNodeSet &done);
195
196 /* single closure */
197 void find_dependencies(ShaderNodeSet &dependencies,
198 const ShaderNodeSet &done,
200 ShaderNode *skip_node = nullptr);
202 ShaderGraph *graph,
204 void generate_node(ShaderNode *node, ShaderNodeSet &done);
208 ShaderNode *node,
210 const ShaderNodeSet &shared);
212
213 /* multi closure */
215
216 /* compile */
217 void compile_type(Shader *shader, ShaderGraph *graph, ShaderType type);
218
219 std::atomic_int *svm_node_types_used;
228};
229
unsigned int uint
float progress
Definition WM_types.hh:1019
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:117
void compile_type(Shader *shader, ShaderGraph *graph, ShaderType type)
Definition svm.cpp:736
int max_stack_use
Definition scene/svm.h:224
void find_aov_nodes_and_dependencies(ShaderNodeSet &aov_nodes, ShaderGraph *graph, CompilerState *state)
Definition svm.cpp:563
bool background
Definition scene/svm.h:118
SVMCompiler(Scene *scene)
Definition svm.cpp:152
array< int4 > current_svm_nodes
Definition scene/svm.h:220
int stack_find_offset(const int size)
Definition svm.cpp:193
void generate_closure_node(ShaderNode *node, CompilerState *state)
Definition svm.cpp:495
ShaderType current_type
Definition scene/svm.h:221
uint bump_state_offset
Definition scene/svm.h:226
void compile(Shader *shader, array< int4 > &svm_nodes, const int index, Summary *summary=nullptr)
Definition svm.cpp:870
void generate_node(ShaderNode *node, ShaderNodeSet &done)
Definition svm.cpp:440
uint encode_uchar4(const uint x, const uint y=0, const uint z=0, const uint w=0)
Definition svm.cpp:374
int stack_assign_if_linked(ShaderInput *input)
Definition svm.cpp:296
Shader * current_shader
Definition scene/svm.h:222
void stack_clear_users(ShaderNode *node, ShaderNodeSet &done)
Definition svm.cpp:330
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:424
void stack_clear_temporary(ShaderNode *node)
Definition svm.cpp:364
uint attribute_standard(ustring name)
Definition svm.cpp:418
void add_node(ShaderNodeType type, const int a=0, const int b=0, const int c=0)
Definition svm.cpp:389
Stack active_stack
Definition scene/svm.h:223
void stack_link(ShaderInput *input, ShaderOutput *output)
Definition svm.cpp:314
void generate_aov_node(ShaderNode *node, CompilerState *state)
ShaderType output_type()
Definition scene/svm.h:111
void generate_svm_nodes(const ShaderNodeSet &nodes, CompilerState *state)
Definition svm.cpp:464
uint closure_mix_weight_offset()
Definition scene/svm.h:102
bool is_linked(ShaderInput *input)
Definition svm.cpp:291
void generate_multi_closure(ShaderNode *root_node, ShaderNode *node, CompilerState *state)
Definition svm.cpp:582
uint get_bump_state_offset()
Definition scene/svm.h:106
uint attribute(ustring name)
Definition svm.cpp:408
void stack_clear_offset(SocketType::Type type, const int offset)
Definition svm.cpp:233
std::atomic_int * svm_node_types_used
Definition scene/svm.h:219
Scene * scene
Definition scene/svm.h:116
uint mix_weight_offset
Definition scene/svm.h:225
bool compile_failed
Definition scene/svm.h:227
void generated_shared_closure_nodes(ShaderNode *root_node, ShaderNode *node, CompilerState *state, const ShaderNodeSet &shared)
Definition svm.cpp:546
int stack_assign(ShaderOutput *output)
Definition svm.cpp:281
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
VecBase< float, 4 > float4
#define printf(...)
#define output
#define shared
ShaderNodeType
AttributeStandard
static ulong state[N]
set< ShaderNode *, ShaderNodeIDComparator > ShaderNodeSet
ShaderNodeSet closure_done
Definition scene/svm.h:172
vector< bool > nodes_done_flag
Definition scene/svm.h:186
CompilerState(ShaderGraph *graph)
Definition svm.cpp:979
Stack & operator=(const Stack &other)
Definition scene/svm.h:131
int users[SVM_STACK_SIZE]
Definition scene/svm.h:159
Stack(const Stack &other)
Definition scene/svm.h:127
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:959
double time_generate_displacement
Definition scene/svm.h:69
i
Definition text_draw.cc:230