Blender  V3.3
shader_graph.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #ifndef __GRAPH_H__
5 #define __GRAPH_H__
6 
7 #include "graph/node.h"
8 #include "graph/node_type.h"
9 
10 #include "kernel/types.h"
11 
12 #include "util/list.h"
13 #include "util/map.h"
14 #include "util/param.h"
15 #include "util/set.h"
16 #include "util/types.h"
17 #include "util/vector.h"
18 
20 
22 class Scene;
23 class Shader;
24 class ShaderInput;
25 class ShaderOutput;
26 class ShaderNode;
27 class ShaderGraph;
28 class SVMCompiler;
29 class OSLCompiler;
30 class OutputNode;
31 class ConstantFolder;
32 class MD5Hash;
33 
34 /* Bump
35  *
36  * For bump mapping, a node may be evaluated multiple times, using different
37  * samples to reconstruct the normal, this indicates the sample position */
38 
40 
41 /* Identifiers for some special node types.
42  *
43  * The graph needs to identify these in the clean function.
44  * Cannot use dynamic_cast, as this is disabled for OSL. */
45 
58 };
59 
60 /* Input
61  *
62  * Input socket for a shader node. May be linked to an output or not. If not
63  * linked, it will either get a fixed default value, or e.g. a texture
64  * coordinate. */
65 
66 class ShaderInput {
67  public:
68  ShaderInput(const SocketType &socket_type_, ShaderNode *parent_)
69  : socket_type(socket_type_),
70  parent(parent_),
71  link(NULL),
73  constant_folded_in(false)
74  {
75  }
76 
77  ustring name()
78  {
79  return socket_type.ui_name;
80  }
81  int flags()
82  {
83  return socket_type.flags;
84  }
86  {
87  return socket_type.type;
88  }
89 
90  void set(float f)
91  {
92  ((Node *)parent)->set(socket_type, f);
93  }
94  void set(float3 f)
95  {
96  ((Node *)parent)->set(socket_type, f);
97  }
98 
99  void disconnect();
100 
104  int stack_offset; /* for SVM compiler */
105 
106  /* Keeps track of whether a constant was folded in this socket, to avoid over-optimizing when the
107  * link is null. */
109 };
110 
111 /* Output
112  *
113  * Output socket for a shader node. */
114 
116  public:
117  ShaderOutput(const SocketType &socket_type_, ShaderNode *parent_)
118  : socket_type(socket_type_), parent(parent_), stack_offset(SVM_STACK_INVALID)
119  {
120  }
121 
122  ustring name()
123  {
124  return socket_type.ui_name;
125  }
127  {
128  return socket_type.type;
129  }
130 
131  void disconnect();
132 
136  int stack_offset; /* for SVM compiler */
137 };
138 
139 /* Node
140  *
141  * Shader node in graph, with input and output sockets. This is the virtual
142  * base class for all node types. */
143 
144 class ShaderNode : public Node {
145  public:
146  explicit ShaderNode(const NodeType *type);
147  virtual ~ShaderNode();
148 
149  void create_inputs_outputs(const NodeType *type);
151 
152  ShaderInput *input(const char *name);
153  ShaderOutput *output(const char *name);
154  ShaderInput *input(ustring name);
155  ShaderOutput *output(ustring name);
156 
157  virtual ShaderNode *clone(ShaderGraph *graph) const = 0;
158  virtual void attributes(Shader *shader, AttributeRequestSet *attributes);
159  virtual void compile(SVMCompiler &compiler) = 0;
160  virtual void compile(OSLCompiler &compiler) = 0;
161 
162  /* Expand node into additional nodes. */
163  virtual void expand(ShaderGraph * /* graph */)
164  {
165  }
166 
167  /* ** Node optimization ** */
168  /* Check whether the node can be replaced with single constant. */
169  virtual void constant_fold(const ConstantFolder & /*folder*/)
170  {
171  }
172 
173  /* Simplify settings used by artists to the ones which are simpler to
174  * evaluate in the kernel but keep the final result unchanged.
175  */
176  virtual void simplify_settings(Scene * /*scene*/){};
177 
178  virtual bool has_surface_emission()
179  {
180  return false;
181  }
182  virtual bool has_surface_transparent()
183  {
184  return false;
185  }
186  virtual bool has_surface_bssrdf()
187  {
188  return false;
189  }
190  virtual bool has_bump()
191  {
192  return false;
193  }
194  virtual bool has_bssrdf_bump()
195  {
196  return false;
197  }
198  virtual bool has_spatial_varying()
199  {
200  return false;
201  }
203  {
204  return false;
205  }
207  {
208  return false;
209  }
210  virtual bool has_volume_support()
211  {
212  return false;
213  }
216 
217  int id; /* index in graph node array */
218  ShaderBump bump; /* for bump mapping utility */
219 
220  ShaderNodeSpecialType special_type; /* special node type */
221 
222  /* ** Selective nodes compilation ** */
223 
224  /* TODO(sergey): More explicitly mention in the function names
225  * that those functions are for selective compilation only?
226  */
227 
228  /* Node feature are used to disable huge nodes inside the group,
229  * so it's possible to disable huge nodes inside of the required
230  * nodes group.
231  */
232  virtual int get_feature()
233  {
235  }
236 
237  /* Get closure ID to which the node compiles into. */
239  {
240  return CLOSURE_NONE_ID;
241  }
242 
243  /* Check whether settings of the node equals to another one.
244  *
245  * This is mainly used to check whether two nodes can be merged
246  * together. Meaning, runtime stuff like node id and unbound slots
247  * will be ignored for comparison.
248  *
249  * NOTE: If some node can't be de-duplicated for whatever reason it
250  * is to be handled in the subclass.
251  */
252  virtual bool equals(const ShaderNode &other);
253 };
254 
255 /* Node definition utility macros */
256 
257 #define SHADER_NODE_CLASS(type) \
258  NODE_DECLARE \
259  type(); \
260  virtual ShaderNode *clone(ShaderGraph *graph) const \
261  { \
262  return graph->create_node<type>(*this); \
263  } \
264  virtual void compile(SVMCompiler &compiler); \
265  virtual void compile(OSLCompiler &compiler);
266 
267 #define SHADER_NODE_NO_CLONE_CLASS(type) \
268  NODE_DECLARE \
269  type(); \
270  virtual void compile(SVMCompiler &compiler); \
271  virtual void compile(OSLCompiler &compiler);
272 
273 #define SHADER_NODE_BASE_CLASS(type) \
274  virtual ShaderNode *clone(ShaderGraph *graph) const \
275  { \
276  return graph->create_node<type>(*this); \
277  } \
278  virtual void compile(SVMCompiler &compiler); \
279  virtual void compile(OSLCompiler &compiler);
280 
282  public:
283  bool operator()(const ShaderNode *n1, const ShaderNode *n2) const
284  {
285  return n1->id < n2->id;
286  }
287 };
288 
289 typedef set<ShaderNode *, ShaderNodeIDComparator> ShaderNodeSet;
290 typedef map<ShaderNode *, ShaderNode *, ShaderNodeIDComparator> ShaderNodeMap;
291 
292 /* Graph
293  *
294  * Shader graph of nodes. Also does graph manipulations for default inputs,
295  * bump mapping from displacement, and possibly other things in the future. */
296 
297 class ShaderGraph : public NodeOwner {
298  public:
299  list<ShaderNode *> nodes;
300  size_t num_node_ids;
301  bool finalized;
304 
305  ShaderGraph();
306  ~ShaderGraph();
307 
309  OutputNode *output();
310 
311  void connect(ShaderOutput *from, ShaderInput *to);
313  void disconnect(ShaderInput *to);
314  void relink(ShaderInput *from, ShaderInput *to);
315  void relink(ShaderOutput *from, ShaderOutput *to);
317 
318  void remove_proxy_nodes();
320  void simplify(Scene *scene);
321  void finalize(Scene *scene,
322  bool do_bump = false,
323  bool do_simplify = false,
324  bool bump_in_object_space = false);
325 
326  int get_num_closures();
327 
328  void dump_graph(const char *filename);
329 
330  /* This function is used to create a node of a specified type instead of
331  * calling 'new', and sets the graph as the owner of the node.
332  */
333  template<typename T, typename... Args> T *create_node(Args &&...args)
334  {
335  T *node = new T(args...);
336  node->set_owner(this);
337  return node;
338  }
339 
340  /* This function is used to delete a node created and owned by the graph.
341  */
342  template<typename T> void delete_node(T *node)
343  {
344  assert(node->get_owner() == this);
345  delete node;
346  }
347 
348  protected:
349  typedef pair<ShaderNode *const, ShaderNode *> NodePair;
350 
351  void find_dependencies(ShaderNodeSet &dependencies, ShaderInput *input);
352  void clear_nodes();
353  void copy_nodes(ShaderNodeSet &nodes, ShaderNodeMap &nnodemap);
354 
356  void bump_from_displacement(bool use_object_space);
357  void refine_bump_nodes();
358  void expand();
359  void default_inputs(bool do_osl);
360  void transform_multi_closure(ShaderNode *node, ShaderOutput *weight_out, bool volume);
361 
362  /* Graph simplification routines. */
363  void clean(Scene *scene);
364  void constant_fold(Scene *scene);
366  void deduplicate_nodes();
367  void verify_volume_output();
368 };
369 
371 
372 #endif /* __GRAPH_H__ */
Definition: md5.h:20
void find_dependencies(ShaderNodeSet &dependencies, ShaderInput *input)
string displacement_hash
Definition: shader_graph.h:303
list< ShaderNode * > nodes
Definition: shader_graph.h:299
OutputNode * output()
void verify_volume_output()
void simplify(Scene *scene)
void disconnect(ShaderOutput *from)
void delete_node(T *node)
Definition: shader_graph.h:342
size_t num_node_ids
Definition: shader_graph.h:300
void break_cycles(ShaderNode *node, vector< bool > &visited, vector< bool > &on_stack)
void copy_nodes(ShaderNodeSet &nodes, ShaderNodeMap &nnodemap)
void clean(Scene *scene)
pair< ShaderNode *const, ShaderNode * > NodePair
Definition: shader_graph.h:349
void compute_displacement_hash()
void default_inputs(bool do_osl)
void connect(ShaderOutput *from, ShaderInput *to)
void relink(ShaderInput *from, ShaderInput *to)
void clear_nodes()
T * create_node(Args &&...args)
Definition: shader_graph.h:333
void constant_fold(Scene *scene)
void transform_multi_closure(ShaderNode *node, ShaderOutput *weight_out, bool volume)
void deduplicate_nodes()
void remove_proxy_nodes()
void simplify_settings(Scene *scene)
void bump_from_displacement(bool use_object_space)
int get_num_closures()
void finalize(Scene *scene, bool do_bump=false, bool do_simplify=false, bool bump_in_object_space=false)
ShaderNode * add(ShaderNode *node)
void refine_bump_nodes()
void dump_graph(const char *filename)
ShaderInput(const SocketType &socket_type_, ShaderNode *parent_)
Definition: shader_graph.h:68
void set(float f)
Definition: shader_graph.h:90
ShaderOutput * link
Definition: shader_graph.h:103
bool constant_folded_in
Definition: shader_graph.h:108
void disconnect()
ShaderNode * parent
Definition: shader_graph.h:102
void set(float3 f)
Definition: shader_graph.h:94
SocketType::Type type()
Definition: shader_graph.h:85
ustring name()
Definition: shader_graph.h:77
const SocketType & socket_type
Definition: shader_graph.h:101
bool operator()(const ShaderNode *n1, const ShaderNode *n2) const
Definition: shader_graph.h:283
virtual ShaderNode * clone(ShaderGraph *graph) const =0
ShaderInput * input(const char *name)
virtual bool has_surface_transparent()
Definition: shader_graph.h:182
vector< ShaderOutput * > outputs
Definition: shader_graph.h:215
virtual bool has_surface_emission()
Definition: shader_graph.h:178
void remove_input(ShaderInput *input)
ShaderNodeSpecialType special_type
Definition: shader_graph.h:220
virtual void simplify_settings(Scene *)
Definition: shader_graph.h:176
virtual bool has_bssrdf_bump()
Definition: shader_graph.h:194
virtual bool has_spatial_varying()
Definition: shader_graph.h:198
vector< ShaderInput * > inputs
Definition: shader_graph.h:214
virtual bool has_volume_support()
Definition: shader_graph.h:210
virtual ~ShaderNode()
virtual ClosureType get_closure_type()
Definition: shader_graph.h:238
virtual bool equals(const ShaderNode &other)
virtual int get_feature()
Definition: shader_graph.h:232
virtual bool has_attribute_dependency()
Definition: shader_graph.h:202
virtual void constant_fold(const ConstantFolder &)
Definition: shader_graph.h:169
virtual bool has_bump()
Definition: shader_graph.h:190
virtual void expand(ShaderGraph *)
Definition: shader_graph.h:163
virtual bool has_integrator_dependency()
Definition: shader_graph.h:206
ShaderNode(const NodeType *type)
void create_inputs_outputs(const NodeType *type)
ShaderBump bump
Definition: shader_graph.h:218
ShaderOutput * output(const char *name)
virtual bool has_surface_bssrdf()
Definition: shader_graph.h:186
virtual void compile(OSLCompiler &compiler)=0
virtual void compile(SVMCompiler &compiler)=0
virtual void attributes(Shader *shader, AttributeRequestSet *attributes)
ustring name()
Definition: shader_graph.h:122
SocketType::Type type()
Definition: shader_graph.h:126
vector< ShaderInput * > links
Definition: shader_graph.h:135
ShaderOutput(const SocketType &socket_type_, ShaderNode *parent_)
Definition: shader_graph.h:117
const SocketType & socket_type
Definition: shader_graph.h:133
void disconnect()
ShaderNode * parent
Definition: shader_graph.h:134
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
OperationNode * node
Depsgraph * graph
StackEntry * from
Scene scene
Set< ComponentNode * > visited
ccl_global KernelShaderEvalInput * input
#define SVM_STACK_INVALID
ClosureType
@ CLOSURE_NONE_ID
@ KERNEL_FEATURE_NODE_BUMP
#define T
ShaderNodeSpecialType
Definition: shader_graph.h:46
@ SHADER_SPECIAL_TYPE_PROXY
Definition: shader_graph.h:48
@ SHADER_SPECIAL_TYPE_IMAGE_SLOT
Definition: shader_graph.h:52
@ SHADER_SPECIAL_TYPE_GEOMETRY
Definition: shader_graph.h:50
@ SHADER_SPECIAL_TYPE_OUTPUT_AOV
Definition: shader_graph.h:57
@ SHADER_SPECIAL_TYPE_COMBINE_CLOSURE
Definition: shader_graph.h:54
@ SHADER_SPECIAL_TYPE_BUMP
Definition: shader_graph.h:56
@ SHADER_SPECIAL_TYPE_AUTOCONVERT
Definition: shader_graph.h:49
@ SHADER_SPECIAL_TYPE_NONE
Definition: shader_graph.h:47
@ SHADER_SPECIAL_TYPE_OUTPUT
Definition: shader_graph.h:55
@ SHADER_SPECIAL_TYPE_CLOSURE
Definition: shader_graph.h:53
@ SHADER_SPECIAL_TYPE_OSL
Definition: shader_graph.h:51
set< ShaderNode *, ShaderNodeIDComparator > ShaderNodeSet
Definition: shader_graph.h:289
ShaderBump
Definition: shader_graph.h:39
@ SHADER_BUMP_CENTER
Definition: shader_graph.h:39
@ SHADER_BUMP_DX
Definition: shader_graph.h:39
@ SHADER_BUMP_DY
Definition: shader_graph.h:39
@ SHADER_BUMP_NONE
Definition: shader_graph.h:39
map< ShaderNode *, ShaderNode *, ShaderNodeIDComparator > ShaderNodeMap
Definition: shader_graph.h:290
const NodeType * type
Definition: graph/node.h:175
ustring name
Definition: graph/node.h:174
ustring ui_name
Definition: node_type.h:79
Type type
Definition: node_type.h:73
int flags
Definition: node_type.h:78