Blender  V3.3
scene/svm.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #ifndef __SVM_H__
5 #define __SVM_H__
6 
7 #include "scene/attribute.h"
8 #include "scene/shader.h"
9 #include "scene/shader_graph.h"
10 
11 #include "util/array.h"
12 #include "util/set.h"
13 #include "util/string.h"
14 #include "util/thread.h"
15 
17 
18 class Device;
19 class DeviceScene;
20 class ImageManager;
21 class Scene;
22 class ShaderGraph;
23 class ShaderInput;
24 class ShaderNode;
25 class ShaderOutput;
26 
27 /* Shader Manager */
28 
30  public:
33 
34  void reset(Scene *scene) override;
35 
36  void device_update_specific(Device *device,
37  DeviceScene *dscene,
38  Scene *scene,
39  Progress &progress) override;
40  void device_free(Device *device, DeviceScene *dscene, Scene *scene) override;
41 
42  protected:
44  Shader *shader,
45  Progress *progress,
46  array<int4> *svm_nodes);
47 };
48 
49 /* Graph Compiler */
50 
51 class SVMCompiler {
52  public:
53  struct Summary {
54  Summary();
55 
56  /* Number of SVM nodes shader was compiled into. */
58 
59  /* Peak stack usage during shader evaluation. */
61 
62  /* Time spent on surface graph finalization. */
63  double time_finalize;
64 
65  /* Time spent on generating SVM nodes for surface shader. */
67 
68  /* Time spent on generating SVM nodes for bump shader. */
70 
71  /* Time spent on generating SVM nodes for volume shader. */
73 
74  /* Time spent on generating SVM nodes for displacement shader. */
76 
77  /* Total time spent on all routines. */
78  double time_total;
79 
80  /* A full multi-line description of the state of the compiler after compilation. */
81  string full_report() const;
82  };
83 
85  void compile(Shader *shader, array<int4> &svm_nodes, int index, Summary *summary = NULL);
86 
91  int stack_find_offset(int size);
95 
96  void add_node(ShaderNodeType type, int a = 0, int b = 0, int c = 0);
97  void add_node(int a = 0, int b = 0, int c = 0, int d = 0);
98  void add_node(ShaderNodeType type, const float3 &f);
99  void add_node(const float4 &f);
100  uint attribute(ustring name);
102  uint attribute_standard(ustring name);
103  uint encode_uchar4(uint x, uint y = 0, uint z = 0, uint w = 0);
105  {
106  return mix_weight_offset;
107  }
108 
110  {
111  return current_type;
112  }
113 
117 
118  protected:
119  /* stack */
120  struct Stack {
122  {
123  memset(users, 0, sizeof(users));
124  }
125  Stack(const Stack &other)
126  {
127  memcpy(users, other.users, sizeof(users));
128  }
129  Stack &operator=(const Stack &other)
130  {
131  memcpy(users, other.users, sizeof(users));
132  return *this;
133  }
134 
135  bool empty()
136  {
137  for (int i = 0; i < SVM_STACK_SIZE; i++)
138  if (users[i])
139  return false;
140 
141  return true;
142  }
143 
144  void print()
145  {
146  printf("stack <");
147 
148  for (int i = 0; i < SVM_STACK_SIZE; i++)
149  printf((users[i]) ? "*" : " ");
150 
151  printf(">\n");
152  }
153 
155  };
156 
157  /* Global state of the compiler accessible from the compilation routines. */
158  struct CompilerState {
159  explicit CompilerState(ShaderGraph *graph);
160 
161  /* ** Global state, used by various compilation steps. ** */
162 
163  /* Set of nodes which were already compiled. */
165 
166  /* Set of closures which were already compiled. */
168 
169  /* Set of nodes used for writing AOVs. */
171 
172  /* ** SVM nodes generation state ** */
173 
174  /* Flag whether the node with corresponding ID was already compiled or
175  * not. Array element with index i corresponds to a node with such if.
176  *
177  * TODO(sergey): This is actually a copy of nodes_done just in another
178  * notation. We can de-duplicate this storage actually after switching
179  * all areas to use this flags array.
180  */
182 
183  /* Node features that can be compiled. */
185  };
186 
190 
191  /* single closure */
192  void find_dependencies(ShaderNodeSet &dependencies,
193  const ShaderNodeSet &done,
195  ShaderNode *skip_node = NULL);
203  ShaderNode *node,
205  const ShaderNodeSet &shared);
207 
208  /* multi closure */
210 
211  /* compile */
213 
214  std::atomic_int *svm_node_types_used;
222 };
223 
225 
226 #endif /* __SVM_H__ */
unsigned int uint
Definition: BLI_sys_types.h:67
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble z
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
float float4[4]
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
ShaderGraph * current_graph
Definition: scene/svm.h:115
void compile_type(Shader *shader, ShaderGraph *graph, ShaderType type)
Definition: svm.cpp:717
int max_stack_use
Definition: scene/svm.h:219
void find_aov_nodes_and_dependencies(ShaderNodeSet &aov_nodes, ShaderGraph *graph, CompilerState *state)
Definition: svm.cpp:550
void add_node(ShaderNodeType type, int a=0, int b=0, int c=0)
Definition: svm.cpp:380
bool background
Definition: scene/svm.h:116
SVMCompiler(Scene *scene)
Definition: svm.cpp:162
array< int4 > current_svm_nodes
Definition: scene/svm.h:215
void generate_closure_node(ShaderNode *node, CompilerState *state)
Definition: svm.cpp:485
void stack_clear_offset(SocketType::Type type, int offset)
Definition: svm.cpp:239
ShaderType current_type
Definition: scene/svm.h:216
void generate_node(ShaderNode *node, ShaderNodeSet &done)
Definition: svm.cpp:430
int stack_assign_if_linked(ShaderInput *input)
Definition: svm.cpp:293
Shader * current_shader
Definition: scene/svm.h:217
void stack_clear_users(ShaderNode *node, ShaderNodeSet &done)
Definition: svm.cpp:324
int stack_size(SocketType::Type type)
Definition: svm.cpp:176
void stack_clear_temporary(ShaderNode *node)
Definition: svm.cpp:355
uint encode_uchar4(uint x, uint y=0, uint z=0, uint w=0)
Definition: svm.cpp:365
uint attribute_standard(ustring name)
Definition: svm.cpp:409
Stack active_stack
Definition: scene/svm.h:218
void stack_link(ShaderInput *input, ShaderOutput *output)
Definition: svm.cpp:309
void find_dependencies(ShaderNodeSet &dependencies, const ShaderNodeSet &done, ShaderInput *input, ShaderNode *skip_node=NULL)
Definition: svm.cpp:415
void generate_aov_node(ShaderNode *node, CompilerState *state)
ShaderType output_type()
Definition: scene/svm.h:109
void generate_svm_nodes(const ShaderNodeSet &nodes, CompilerState *state)
Definition: svm.cpp:454
uint closure_mix_weight_offset()
Definition: scene/svm.h:104
void generate_multi_closure(ShaderNode *root_node, ShaderNode *node, CompilerState *state)
Definition: svm.cpp:569
uint attribute(ustring name)
Definition: svm.cpp:399
std::atomic_int * svm_node_types_used
Definition: scene/svm.h:214
Scene * scene
Definition: scene/svm.h:114
uint mix_weight_offset
Definition: scene/svm.h:220
bool compile_failed
Definition: scene/svm.h:221
int stack_find_offset(int size)
Definition: svm.cpp:202
void compile(Shader *shader, array< int4 > &svm_nodes, int index, Summary *summary=NULL)
Definition: svm.cpp:850
void generated_shared_closure_nodes(ShaderNode *root_node, ShaderNode *node, CompilerState *state, const ShaderNodeSet &shared)
Definition: svm.cpp:534
int stack_assign(ShaderOutput *output)
Definition: svm.cpp:284
void reset(Scene *scene) override
Definition: svm.cpp:33
void device_free(Device *device, DeviceScene *dscene, Scene *scene) override
Definition: svm.cpp:153
SVMShaderManager()
Definition: svm.cpp:25
~SVMShaderManager()
Definition: svm.cpp:29
void device_update_specific(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress) override
Definition: svm.cpp:57
void device_update_shader(Scene *scene, Shader *shader, Progress *progress, array< int4 > *svm_nodes)
Definition: svm.cpp:37
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
OperationNode * node
Depsgraph * graph
Scene scene
ccl_global KernelShaderEvalInput ccl_global float * output
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
ccl_global KernelShaderEvalInput * input
const int state
#define SVM_STACK_SIZE
ShaderNodeType
ShaderType
AttributeStandard
Definition: kernel/types.h:612
static unsigned c
Definition: RandGen.cpp:83
static unsigned a[3]
Definition: RandGen.cpp:78
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
set< ShaderNode *, ShaderNodeIDComparator > ShaderNodeSet
Definition: shader_graph.h:289
ShaderNodeSet nodes_done
Definition: scene/svm.h:164
ShaderNodeSet closure_done
Definition: scene/svm.h:167
vector< bool > nodes_done_flag
Definition: scene/svm.h:181
ShaderNodeSet aov_nodes
Definition: scene/svm.h:170
CompilerState(ShaderGraph *graph)
Definition: svm.cpp:969
Stack & operator=(const Stack &other)
Definition: scene/svm.h:129
int users[SVM_STACK_SIZE]
Definition: scene/svm.h:154
Stack(const Stack &other)
Definition: scene/svm.h:125
double time_generate_volume
Definition: scene/svm.h:72
double time_generate_surface
Definition: scene/svm.h:66
double time_generate_bump
Definition: scene/svm.h:69
string full_report() const
Definition: svm.cpp:947
double time_generate_displacement
Definition: scene/svm.h:75