HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ShaderNodeImpl.h
Go to the documentation of this file.
1 //
2 // Copyright Contributors to the MaterialX Project
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 
6 #ifndef MATERIALX_SHADERNODEIMPL_H
7 #define MATERIALX_SHADERNODEIMPL_H
8 
9 /// @file
10 /// Base class for shader node implementations
11 
13 
16 #include <MaterialXCore/Util.h>
17 
19 
20 class InterfaceElement;
21 class Node;
23 
24 /// Shared pointer to a ShaderNodeImpl
25 using ShaderNodeImplPtr = shared_ptr<class ShaderNodeImpl>;
26 
27 /// @class ShaderNodeImpl
28 /// Class handling the shader generation implementation for a node.
29 /// Responsible for emitting the function definition and function call
30 /// that is the node implementation.
32 {
33  public:
34  virtual ~ShaderNodeImpl() { }
35 
36  /// Initialize with the given implementation element.
37  /// Initialization must set the name and hash for the implementation,
38  /// as well as any other data needed to emit code for the node.
39  virtual void initialize(const InterfaceElement& element, GenContext& context);
40 
41  /// Return the name of this implementation.
42  const string& getName() const
43  {
44  return _name;
45  }
46 
47  /// Return a hash for this implementation.
48  /// The hash should correspond to the function signature generated for the node,
49  /// and can be used to compare implementations, e.g. to query if an identical
50  /// function has already been emitted during shader generation.
51  size_t getHash() const
52  {
53  return _hash;
54  }
55 
56  /// Add additional inputs on a node.
57  virtual void addInputs(ShaderNode& node, GenContext& context) const;
58 
59  /// Set values for additional inputs on a node.
60  virtual void setValues(const Node& node, ShaderNode& shaderNode, GenContext& context) const;
61 
62  /// Add additional classifications on a node.
63  virtual void addClassification(ShaderNode& node) const;
64 
65  /// Create shader variables needed for the implementation of this node (e.g. uniforms, inputs and outputs).
66  /// Used if the node requires input data from the application.
67  virtual void createVariables(const ShaderNode& node, GenContext& context, Shader& shader) const;
68 
69  /// Emit function definition for the given node instance.
70  virtual void emitFunctionDefinition(const ShaderNode& node, GenContext& context, ShaderStage& stage) const;
71 
72  /// Emit the function call or inline source code for given node instance in the given context.
73  virtual void emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const;
74 
75  /// Emit declaration and initialization of output variables to use in a function call.
76  virtual void emitOutputVariables(const ShaderNode& node, GenContext& context, ShaderStage& stage) const;
77 
78  /// Return a pointer to the graph if this implementation is using a graph,
79  /// or returns nullptr otherwise.
80  virtual ShaderGraph* getGraph() const;
81 
82  /// Returns true if an input is editable by users.
83  /// Editable inputs are allowed to be published as shader uniforms
84  /// and hence must be presentable in a user interface.
85  /// By default all inputs are considered to be editable.
86  virtual bool isEditable(const ShaderInput& /*input*/) const
87  {
88  return true;
89  }
90 
91  /// Returns true if a graph input is accessible by users.
92  /// Accessible inputs are allowed to be published as shader uniforms
93  /// and hence must be presentable in a user interface.
94  /// By default all graph inputs are considered to be accessible.
95  virtual bool isEditable(const ShaderGraphInputSocket& /*input*/) const
96  {
97  return true;
98  }
99 
100  protected:
101  /// Protected constructor
102  ShaderNodeImpl();
103 
104  /// Returns true if the first output of the node is a closure
105  /// This is used by SourceCodeNode and CompoundNode to generate
106  /// the appropriate shader code.
107  bool nodeOutputIsClosure(const ShaderNode& node) const;
108 
109  protected:
110  string _name;
111  size_t _hash;
112 };
113 
114 /// A no operation node, to be used for organizational nodes that has no code to execute.
116 {
117  public:
118  static ShaderNodeImplPtr create();
119 };
120 
122 
123 #endif
virtual bool isEditable(const ShaderGraphInputSocket &) const
shared_ptr< ShaderNodeImpl > ShaderNodeImplPtr
Shared pointer to a ShaderNodeImpl.
Definition: Library.h:39
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
Definition: Node.h:52
#define MX_GENSHADER_API
Definition: Export.h:18
virtual bool isEditable(const ShaderInput &) const
A no operation node, to be used for organizational nodes that has no code to execute.
virtual ~ShaderNodeImpl()
size_t getHash() const
OPENVDB_API void initialize()
Global registration of native Grid, Transform, Metadata and Point attribute types. Also initializes blosc (if enabled).
Definition: logging.h:294
const string & getName() const
Return the name of this implementation.
GLuint shader
Definition: glcorearb.h:785
Definition: Shader.h:32
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26