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  /// Return an identifier for the target used by this implementation.
37  /// By default an empty string is returned, representing all targets.
38  /// Only override this method if your derived node implementation class
39  /// is for a specific target.
40  virtual const string& getTarget() const { return EMPTY_STRING; }
41 
42  /// Initialize with the given implementation element.
43  /// Initialization must set the name and hash for the implementation,
44  /// as well as any other data needed to emit code for the node.
45  virtual void initialize(const InterfaceElement& element, GenContext& context);
46 
47  /// Return the name of this implementation.
48  const string& getName() const
49  {
50  return _name;
51  }
52 
53  /// Return a hash for this implementation.
54  /// The hash should correspond to the function signature generated for the node,
55  /// and can be used to compare implementations, e.g. to query if an identical
56  /// function has already been emitted during shader generation.
57  size_t getHash() const
58  {
59  return _hash;
60  }
61 
62  /// Add additional inputs on a node.
63  virtual void addInputs(ShaderNode& node, GenContext& context) const;
64 
65  /// Set values for additional inputs on a node.
66  virtual void setValues(const Node& node, ShaderNode& shaderNode, GenContext& context) const;
67 
68  /// Add additional classifications on a node.
69  virtual void addClassification(ShaderNode& node) const;
70 
71  /// Create shader variables needed for the implementation of this node (e.g. uniforms, inputs and outputs).
72  /// Used if the node requires input data from the application.
73  virtual void createVariables(const ShaderNode& node, GenContext& context, Shader& shader) const;
74 
75  /// Emit function definition for the given node instance.
76  virtual void emitFunctionDefinition(const ShaderNode& node, GenContext& context, ShaderStage& stage) const;
77 
78  /// Emit the function call or inline source code for given node instance in the given context.
79  virtual void emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const;
80 
81  /// Emit declaration and initialization of output variables to use in a function call.
82  virtual void emitOutputVariables(const ShaderNode& node, GenContext& context, ShaderStage& stage) const;
83 
84  /// Return a pointer to the graph if this implementation is using a graph,
85  /// or returns nullptr otherwise.
86  virtual ShaderGraph* getGraph() const;
87 
88  /// Returns true if an input is editable by users.
89  /// Editable inputs are allowed to be published as shader uniforms
90  /// and hence must be presentable in a user interface.
91  /// By default all inputs are considered to be editable.
92  virtual bool isEditable(const ShaderInput& /*input*/) const
93  {
94  return true;
95  }
96 
97  /// Returns true if a graph input is accessible by users.
98  /// Accessible inputs are allowed to be published as shader uniforms
99  /// and hence must be presentable in a user interface.
100  /// By default all graph inputs are considered to be accessible.
101  virtual bool isEditable(const ShaderGraphInputSocket& /*input*/) const
102  {
103  return true;
104  }
105 
106  protected:
107  /// Protected constructor
108  ShaderNodeImpl();
109 
110  protected:
111  string _name;
112  size_t _hash;
113 };
114 
115 /// A no operation node, to be used for organizational nodes that has no code to execute.
117 {
118  public:
119  static ShaderNodeImplPtr create();
120 };
121 
123 
124 #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
MATERIALX_NAMESPACE_BEGIN MX_CORE_API const string EMPTY_STRING
#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
virtual const string & getTarget() 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