Blender V5.3
shader_graph.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 "graph/node.h"
8#include "graph/node_type.h"
9
10#include "kernel/svm/types.h"
11#include "kernel/types.h"
12
13#include "util/map.h"
14#include "util/param.h"
15#include "util/set.h"
16#include "util/string.h"
17#include "util/types.h"
19#include "util/vector.h"
20
22
24class Scene;
25class Shader;
26class ShaderInput;
27class ShaderOutput;
28class ShaderNode;
29class ShaderGraph;
30class SVMCompiler;
31class OSLCompiler;
32class OutputNode;
33class ConstantFolder;
34class MD5Hash;
35
36/* Bump
37 *
38 * For bump mapping, a node may be evaluated multiple times, using different
39 * samples to reconstruct the normal, this indicates the sample position */
40
42
43/* Identifiers for some special node types.
44 *
45 * The graph needs to identify these in the clean function.
46 * Cannot use dynamic_cast, as this is disabled for OSL. */
47
63
64/* Base class for ShaderInput and ShaderOutput. */
65
66class ShaderIO {
67 public:
68 ShaderIO(const SocketType &socket_type_, ShaderNode *parent_)
69 : socket_type(socket_type_), parent(parent_)
70
71 {
72 }
73
74 ustring name() const
75 {
76 return socket_type.ui_name;
77 }
79 {
80 return socket_type.type;
81 }
82
85
86 virtual void disconnect() {};
87
88 int stack_offset = SVM_STACK_INVALID; /* for SVM compiler */
89};
90
91/* Input
92 *
93 * Input socket for a shader node. May be linked to an output or not. If not
94 * linked, it will either get a fixed default value, or e.g. a texture
95 * coordinate. */
96
97class ShaderInput final : public ShaderIO {
98 public:
100
101 int flags() const
102 {
103 return socket_type.flags;
104 }
105 void set(const float f)
106 {
107 ((Node *)parent)->set(socket_type, f);
108 }
109 void set(const float3 f)
110 {
111 ((Node *)parent)->set(socket_type, f);
112 }
113 void set(const int f)
114 {
115 ((Node *)parent)->set(socket_type, f);
116 }
117
118 void disconnect() override;
119
120 ShaderOutput *link = nullptr;
121
122 /* Keeps track of whether a constant was folded in this socket, to avoid over-optimizing when the
123 * link is null. */
124 bool constant_folded_in = false;
125};
126
127/* Output
128 *
129 * Output socket for a shader node. */
130
132 public:
133 using ShaderIO::ShaderIO;
134
135 void disconnect() override;
136
138};
139
140/* Node
141 *
142 * Shader node in graph, with input and output sockets. This is the virtual
143 * base class for all node types. */
144
145class ShaderNode : public Node {
146 public:
147 explicit ShaderNode(const NodeType *type);
148 ShaderNode(const ShaderNode &other);
149
152
153 ShaderInput *input(const char *name);
154 ShaderOutput *output(const char *name);
155 ShaderInput *input(ustring name);
156 ShaderOutput *output(ustring name);
157
158 virtual ShaderNode *clone(ShaderGraph *graph) const = 0;
159 virtual void attributes(Shader *shader, AttributeRequestSet *attributes);
161 virtual void compile(SVMCompiler &compiler) = 0;
162 virtual void compile(OSLCompiler &compiler) = 0;
163
164 /* Expand node into additional nodes. */
165 virtual void expand(ShaderGraph * /* graph */) {}
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 /* Simplify settings used by artists to the ones which are simpler to
172 * evaluate in the kernel but keep the final result unchanged.
173 */
174 virtual void simplify_settings(Scene * /*scene*/) {};
175
176 virtual bool has_surface_emission()
177 {
178 return false;
179 }
181 {
182 return false;
183 }
184 virtual bool has_surface_bssrdf()
185 {
186 return false;
187 }
188 virtual bool has_dispersion()
189 {
190 return false;
191 }
192 virtual bool has_bump()
193 {
194 return false;
195 }
196 virtual bool has_bssrdf_bump()
197 {
198 return false;
199 }
200 virtual bool has_spatial_varying()
201 {
202 return false;
203 }
205 {
206 return false;
207 }
208 virtual bool has_volume_support()
209 {
210 return false;
211 }
212 /* True if the node only multiplies or adds a constant values. */
213 virtual bool is_linear_operation()
214 {
215 return false;
216 }
218 {
219 return NODE_NONE;
220 }
221 virtual bool is_texture_node_and_needs_derivatives(const SVMCompiler & /*compiler*/)
222 {
223 return false;
224 }
225 bool need_derivatives() const
226 {
227 return need_derivatives_;
228 }
233
236
237 /* index in graph node array */
238 int id = -1;
239 /* for bump mapping utility */
241 float bump_filter_width = 0.0f;
242 /* special node type */
244
245 /* ** Selective nodes compilation ** */
246
247 /* TODO(sergey): More explicitly mention in the function names
248 * that those functions are for selective compilation only?
249 */
250
251 /* Node feature are used to disable huge nodes inside the group,
252 * so it's possible to disable huge nodes inside of the required
253 * nodes group.
254 */
256 {
258 }
259
260 /* Get closure ID to which the node compiles into. */
262 {
263 return CLOSURE_NONE_ID;
264 }
265
266 /* Check whether settings of the node equals to another one.
267 *
268 * This is mainly used to check whether two nodes can be merged
269 * together. Meaning, runtime stuff like node id and unbound slots
270 * will be ignored for comparison.
271 *
272 * NOTE: If some node can't be de-duplicated for whatever reason it
273 * is to be handled in the subclass.
274 */
275 virtual bool equals(const ShaderNode &other);
276
277 /* Helps to avoid adding input link to the node after the node is already added to SVM. */
278 bool added_to_svm = false;
279
280 protected:
281 bool need_derivatives_ = false;
282
283 /* Disconnect the input with the given name if it is connected.
284 * Used to optimize away unused inputs. */
285 void disconnect_unused_input(const char *name);
286};
287
288/* Node definition utility macros */
289
290#define SHADER_NODE_CLASS(type) \
291 NODE_DECLARE \
292 type(); \
293 ShaderNode *clone(ShaderGraph *graph) const override \
294 { \
295 return graph->create_node<type>(*this); \
296 } \
297 void compile(SVMCompiler &compiler) override; \
298 void compile(OSLCompiler &compiler) override;
299
300#define SHADER_NODE_NO_CLONE_CLASS(type) \
301 NODE_DECLARE \
302 type(); \
303 void compile(SVMCompiler &compiler) override; \
304 void compile(OSLCompiler &compiler) override;
305
306#define SHADER_NODE_BASE_CLASS(type) \
307 ShaderNode *clone(ShaderGraph *graph) const override \
308 { \
309 return graph->create_node<type>(*this); \
310 } \
311 void compile(SVMCompiler &compiler) override; \
312 void compile(OSLCompiler &compiler) override;
313
315 public:
316 bool operator()(const ShaderNode *n1, const ShaderNode *n2) const
317 {
318 return n1->id < n2->id;
319 }
320};
321
323 public:
324 bool operator()(const std::pair<ShaderNode *, bool> p1,
325 const std::pair<ShaderNode *, bool> p2) const
326 {
327 return p1.first->id < p2.first->id || (p1.first->id == p2.first->id && p1.second < p2.second);
328 }
329};
330
331using ShaderNodeSet = set<ShaderNode *, ShaderNodeIDComparator>;
332using ShaderNodeMap = map<ShaderNode *, ShaderNode *, ShaderNodeIDComparator>;
333
334/* Graph
335 *
336 * Shader graph of nodes. Also does graph manipulations for default inputs,
337 * bump mapping from displacement, and possibly other things in the future. */
338
339class ShaderGraph : public NodeOwner {
340 public:
346
347 ShaderGraph();
348 ~ShaderGraph() override;
349
351
352 void connect(ShaderOutput *from, ShaderInput *to);
353 void disconnect(ShaderOutput *from);
354 void disconnect(ShaderInput *to);
355 void relink(ShaderInput *from, ShaderInput *to);
356 void relink(ShaderOutput *from, ShaderOutput *to);
358
359 void remove_proxy_nodes();
361 void simplify(Scene *scene);
362 void finalize(Scene *scene, bool do_bump = false, bool bump_in_object_space = false);
363
364 int get_num_closures();
365
366 void dump_graph(const char *filename);
367
368 /* Create node from class and add it to the shader graph. */
369 template<typename T, typename... Args> T *create_node(Args &&...args)
370 {
371 unique_ptr<T> node = make_unique<T>(args...);
372 T *node_ptr = node.get();
373 this->add_node(std::move(node));
374 return node_ptr;
375 }
376
377 /* Create OSL node from class and add it to the shader graph. */
378 template<typename T, typename... Args> T *create_osl_node(void *node_memory, Args &&...args)
379 {
380 T *node_ptr = new (node_memory) T(args...);
381 unique_ptr<T> node(node_ptr);
382 this->add_node(std::move(node));
383 return node_ptr;
384 }
385
386 /* Create node from node type and add it to the shader graph. */
387 ShaderNode *create_node(const NodeType *node_type)
388 {
389 unique_ptr<Node> node = node_type->create(node_type);
390 unique_ptr<ShaderNode> shader_node(static_cast<ShaderNode *>(node.release()));
391 ShaderNode *shader_node_ptr = shader_node.get();
392 this->add_node(std::move(shader_node));
393 return shader_node_ptr;
394 }
395
396 protected:
397 using NodePair = pair<ShaderNode *const, ShaderNode *>;
398
400
401 void find_dependencies(ShaderNodeSet &dependencies, ShaderInput *input);
402 void clear_nodes();
404
405 void break_cycles(ShaderNode *node, vector<bool> &visited, vector<bool> &on_stack);
406 void bump_from_displacement(bool use_object_space);
407 void refine_bump_nodes();
408 void expand();
409 void default_inputs(bool do_osl);
410 void transform_multi_closure(ShaderNode *node, ShaderOutput *weight_out, bool volume);
411
412 /* Graph simplification routines. */
413 void clean(Scene *scene);
414 void constant_fold(Scene *scene);
415 void simplify_settings(Scene *scene);
416 void deduplicate_nodes();
418};
419
#define final(a, b, c)
Definition BLI_hash_c.hh:21
unsigned long long int uint64_t
Definition md5.h:19
Definition scene.h:133
void find_dependencies(ShaderNodeSet &dependencies, ShaderInput *input)
string displacement_hash
OutputNode * output()
unique_ptr_vector< ShaderNode > nodes
void simplify(Scene *scene)
T * create_node(Args &&...args)
void add_node(unique_ptr< ShaderNode > &&node)
void disconnect(ShaderOutput *from)
size_t num_node_ids
void break_cycles(ShaderNode *node, vector< bool > &visited, vector< bool > &on_stack)
void copy_nodes(ShaderNodeSet &nodes, ShaderNodeMap &nnodemap)
void clean(Scene *scene)
void finalize(Scene *scene, bool do_bump=false, bool bump_in_object_space=false)
void compute_displacement_hash()
void default_inputs(bool do_osl)
ShaderNode * create_node(const NodeType *node_type)
void connect(ShaderOutput *from, ShaderInput *to)
void relink(ShaderInput *from, ShaderInput *to)
T * create_osl_node(void *node_memory, Args &&...args)
void optimize_volume_output()
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)
pair< ShaderNode *const, ShaderNode * > NodePair
void refine_bump_nodes()
void dump_graph(const char *filename)
ustring name() const
ShaderIO(const SocketType &socket_type_, ShaderNode *parent_)
const SocketType & socket_type
ShaderNode * parent
int stack_offset
SocketType::Type type() const
virtual void disconnect()
void disconnect() override
ShaderOutput * link
ShaderIO(const SocketType &socket_type_, ShaderNode *parent_)
bool constant_folded_in
void set(const float3 f)
void set(const int f)
void set(const float f)
int flags() const
bool operator()(const std::pair< ShaderNode *, bool > p1, const std::pair< ShaderNode *, bool > p2) const
bool operator()(const ShaderNode *n1, const ShaderNode *n2) const
ShaderInput * input(const char *name)
virtual bool has_surface_transparent()
virtual bool has_surface_emission()
bool need_derivatives() const
void remove_input(ShaderInput *input)
virtual bool has_dispersion()
ShaderNodeSpecialType special_type
virtual void simplify_settings(Scene *)
virtual bool has_bssrdf_bump()
virtual bool has_spatial_varying()
virtual bool has_volume_support()
void set_need_derivatives(const bool need_derivatives=true)
virtual ClosureType get_closure_type()
bool need_derivatives_
virtual uint64_t get_feature()
virtual void global_attributes(Shader *shader, AttributeRequestSet *attributes)
bool added_to_svm
virtual ShaderNode * clone(ShaderGraph *graph) const =0
virtual ShaderNodeType shader_node_type() const
virtual bool equals(const ShaderNode &other)
float bump_filter_width
void disconnect_unused_input(const char *name)
virtual bool has_attribute_dependency()
virtual void constant_fold(const ConstantFolder &)
virtual bool has_bump()
virtual bool is_linear_operation()
virtual void expand(ShaderGraph *)
virtual bool is_texture_node_and_needs_derivatives(const SVMCompiler &)
ShaderNode(const NodeType *type)
void create_inputs_outputs(const NodeType *type)
unique_ptr_vector< ShaderInput > inputs
ShaderBump bump
ShaderOutput * output(const char *name)
virtual bool has_surface_bssrdf()
virtual void compile(OSLCompiler &compiler)=0
virtual void compile(SVMCompiler &compiler)=0
virtual void attributes(Shader *shader, AttributeRequestSet *attributes)
unique_ptr_vector< ShaderOutput > outputs
void disconnect() override
vector< ShaderInput * > links
ShaderIO(const SocketType &socket_type_, ShaderNode *parent_)
#define SVM_STACK_INVALID
#define KERNEL_FEATURE_NODE_BUMP
#define CCL_NAMESPACE_END
ShaderNodeType
ClosureType
@ CLOSURE_NONE_ID
#define T
ShaderNodeSpecialType
@ SHADER_SPECIAL_TYPE_LIGHT_PATH
@ SHADER_SPECIAL_TYPE_PROXY
@ SHADER_SPECIAL_TYPE_IMAGE_SLOT
@ SHADER_SPECIAL_TYPE_GEOMETRY
@ SHADER_SPECIAL_TYPE_OUTPUT_AOV
@ SHADER_SPECIAL_TYPE_COMBINE_CLOSURE
@ SHADER_SPECIAL_TYPE_BUMP
@ SHADER_SPECIAL_TYPE_AUTOCONVERT
@ SHADER_SPECIAL_TYPE_NONE
@ SHADER_SPECIAL_TYPE_SCENE_TIME
@ SHADER_SPECIAL_TYPE_OUTPUT
@ SHADER_SPECIAL_TYPE_CLOSURE
@ SHADER_SPECIAL_TYPE_OSL
set< ShaderNode *, ShaderNodeIDComparator > ShaderNodeSet
map< ShaderNode *, ShaderNode *, ShaderNodeIDComparator > ShaderNodeMap
ShaderBump
@ SHADER_BUMP_CENTER
@ SHADER_BUMP_DX
@ SHADER_BUMP_DY
@ SHADER_BUMP_NONE
CreateFunc create
Definition node_type.h:133
const NodeType * type
Definition graph/node.h:179
ustring name
Definition graph/node.h:178
Node(const NodeType *type, ustring name=ustring())
ustring ui_name
Definition node_type.h:91
Type type
Definition node_type.h:85