HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ShaderGenerator.h
Go to the documentation of this file.
1 //
2 // TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3 // All rights reserved. See LICENSE.txt for license.
4 //
5 
6 #ifndef MATERIALX_SHADERGENERATOR_H
7 #define MATERIALX_SHADERGENERATOR_H
8 
9 /// @file
10 /// Base shader generator class
11 
13 
18 
20 
22 
23 /// @class ShaderGenerator
24 /// Base class for shader generators
25 /// All third-party shader generators should derive from this class.
26 /// Derived classes should use DECLARE_SHADER_GENERATOR / DEFINE_SHADER_GENERATOR
27 /// in their declaration / definition, and register with the Registry class.
29 {
30  public:
31  /// Destructor
32  virtual ~ShaderGenerator() { }
33 
34  /// Return the name of the target this generator is for.
35  virtual const string& getTarget() const = 0;
36 
37  /// Generate a shader starting from the given element, translating
38  /// the element and all dependencies upstream into shader code.
39  virtual ShaderPtr generate(const string& name, ElementPtr element, GenContext& context) const = 0;
40 
41  /// Start a new scope using the given bracket type.
42  virtual void emitScopeBegin(ShaderStage& stage, Syntax::Punctuation punc = Syntax::CURLY_BRACKETS) const;
43 
44  /// End the current scope.
45  virtual void emitScopeEnd(ShaderStage& stage, bool semicolon = false, bool newline = true) const;
46 
47  /// Start a new line.
48  virtual void emitLineBegin(ShaderStage& stage) const;
49 
50  /// End the current line.
51  virtual void emitLineEnd(ShaderStage& stage, bool semicolon = true) const;
52 
53  /// Add a line break.
54  virtual void emitLineBreak(ShaderStage& stage) const;
55 
56  /// Add a string.
57  virtual void emitString(const string& str, ShaderStage& stage) const;
58 
59  /// Add a single line of code, optionally appending a semicolon.
60  virtual void emitLine(const string& str, ShaderStage& stage, bool semicolon = true) const;
61 
62  /// Add a single line code comment.
63  virtual void emitComment(const string& str, ShaderStage& stage) const;
64 
65  /// Add a block of code.
66  virtual void emitBlock(const string& str, GenContext& context, ShaderStage& stage) const;
67 
68  /// Add the contents of an include file. Making sure it is
69  /// only included once for the shader stage.
70  virtual void emitInclude(const string& file, GenContext& context, ShaderStage& stage) const;
71 
72  /// Add a value.
73  template<typename T>
74  void emitValue(const T& value, ShaderStage& stage) const
75  {
76  stage.addValue<T>(value);
77  }
78 
79  /// Add the function definition for a single node.
80  virtual void emitFunctionDefinition(const ShaderNode& node, GenContext& context, ShaderStage& stage) const;
81 
82  /// Add all function definitions for a graph.
83  virtual void emitFunctionDefinitions(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
84 
85  /// Add the function call for a single node.
86  virtual void emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage, bool checkScope = true) const;
87 
88  /// Add all function calls for a graph. If a classification mask is given only functions for
89  /// nodes matching this classification will be emitted.
90  virtual void emitFunctionCalls(const ShaderGraph& graph, GenContext& context, ShaderStage& stage, uint32_t classification = 0u) const;
91 
92  /// Add function calls for nodes connected directly upstream from the given node.
93  /// If a classification mask is given only functions for nodes matching this classification
94  /// will be emitted.
95  virtual void emitDependentFunctionCalls(const ShaderNode& node, GenContext& context, ShaderStage& stage, uint32_t classification = 0u) const;
96 
97  /// Emit code for starting a new function body.
98  virtual void emitFunctionBodyBegin(const ShaderNode& node, GenContext& context, ShaderStage& stage, Syntax::Punctuation punc = Syntax::CURLY_BRACKETS) const;
99 
100  /// Emit code for ending a function body.
101  virtual void emitFunctionBodyEnd(const ShaderNode& node, GenContext& context, ShaderStage& stage) const;
102 
103  /// Emit type definitions for all data types that needs it.
104  virtual void emitTypeDefinitions(GenContext& context, ShaderStage& stage) const;
105 
106  /// Emit the connected variable name for an input,
107  /// or constant value if the port is not connected
108  virtual void emitInput(const ShaderInput* input, GenContext& context, ShaderStage& stage) const;
109 
110  /// Emit the output variable name for an output, optionally including it's type
111  /// and default value assignment.
112  virtual void emitOutput(const ShaderOutput* output, bool includeType, bool assignValue, GenContext& context, ShaderStage& stage) const;
113 
114  /// Emit definitions for all shader variables in a block.
115  /// @param block Block to emit.
116  /// @param qualifier Optional qualifier to add before the variable declaration.
117  /// @param separator Separator to use between the declarations.
118  /// @param context Context for generation.
119  /// @param stage The stage to emit code into.
120  /// @param assignValue If true the variables are initialized with their value.
121  virtual void emitVariableDeclarations(const VariableBlock& block, const string& qualifier, const string& separator, GenContext& context, ShaderStage& stage,
122  bool assignValue = true) const;
123 
124  /// Emit definition of a single shader variable.
125  /// @param variable Shader port representing the variable.
126  /// @param qualifier Optional qualifier to add before the variable declaration.
127  /// @param context Context for generation.
128  /// @param stage The stage to emit code into.
129  /// @param assignValue If true the variable is initialized with its value.
130  virtual void emitVariableDeclaration(const ShaderPort* variable, const string& qualifier, GenContext& context, ShaderStage& stage,
131  bool assignValue = true) const;
132 
133  /// Return the closure contexts defined for the given node.
134  virtual void getClosureContexts(const ShaderNode& node, vector<ClosureContext*>& cct) const;
135 
136  /// Return the result of an upstream connection or value for an input.
137  virtual string getUpstreamResult(const ShaderInput* input, GenContext& context) const;
138 
139  /// Return the syntax object for the language used by the code generator
140  const Syntax& getSyntax() const { return *_syntax; }
141 
142  /// Register a shader node implementation for a given implementation element name
143  void registerImplementation(const string& name, CreatorFunction<ShaderNodeImpl> creator);
144 
145  /// Determine if a shader node implementation has been registered for a given implementation element name
146  bool implementationRegistered(const string& name) const;
147 
148  /// Return a registered shader node implementation for the given nodedef.
149  virtual ShaderNodeImplPtr getImplementation(const NodeDef& nodedef, GenContext& context) const;
150 
151  /// Sets the color management system
153  {
154  _colorManagementSystem = colorManagementSystem;
155  }
156 
157  /// Returns the color management system
159  {
160  return _colorManagementSystem;
161  }
162 
163  /// Sets the unit system
164  void setUnitSystem(UnitSystemPtr unitSystem)
165  {
166  _unitSystem = unitSystem;
167  }
168 
169  /// Returns the unit system
171  {
172  return _unitSystem;
173  }
174 
175  /// Return the map of token substitutions used by the generator.
177  {
178  return _tokenSubstitutions;
179  }
180 
181  /// Register metadata that should be exported to the generated shaders.
182  /// Supported metadata includes standard UI attributes like "uiname", "uifolder",
183  /// "uimin", "uimax", etc.
184  /// But it is also extendable by defining custom attributes using AttributeDefs.
185  /// Any AttributeDef in the given document with exportable="true" will be
186  /// exported as shader metadata when found on nodes during shader generation.
187  /// Derived shader generators may override this method to change the registration.
188  /// Applications must explicitly call this method before shader generation to enable
189  /// export of metadata.
190  virtual void registerShaderMetadata(const DocumentPtr& doc, GenContext& context) const;
191 
192  protected:
193  /// Protected constructor
194  ShaderGenerator(SyntaxPtr syntax);
195 
196  /// Create a new stage in a shader.
197  virtual ShaderStagePtr createStage(const string& name, Shader& shader) const;
198 
199  /// Set function name for a stage.
200  void setFunctionName(const string& functionName, ShaderStage& stage) const
201  {
202  stage.setFunctionName(functionName);
203  }
204 
205  /// Replace tokens with identifiers according to the given substitutions map.
206  void replaceTokens(const StringMap& substitutions, ShaderStage& stage) const;
207 
208  protected:
209  static const string T_FILE_TRANSFORM_UV;
210 
216 
217  friend ShaderGraph;
218 };
219 
220 /// @class ExceptionShaderGenError
221 /// An exception that is thrown when shader generation fails.
223 {
224  public:
225  using Exception::Exception;
226 };
227 
229 
230 #endif // MATERIALX_SHADERGENERATOR_H
ColorManagementSystemPtr _colorManagementSystem
shared_ptr< ShaderNodeImpl > ShaderNodeImplPtr
Shared pointer to a ShaderNodeImpl.
Definition: Library.h:40
void setFunctionName(const string &functionName, ShaderStage &stage) const
Set function name for a stage.
Factory< ShaderNodeImpl > _implFactory
void addValue(const T &value)
Add a value.
Definition: ShaderStage.h:233
void setColorManagementSystem(ColorManagementSystemPtr colorManagementSystem)
Sets the color management system.
static const string T_FILE_TRANSFORM_UV
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:23
const Syntax & getSyntax() const
Return the syntax object for the language used by the code generator.
GLenum GLenum GLenum input
Definition: glew.h:14162
shared_ptr< class UnitSystem > UnitSystemPtr
A shared pointer to a UnitSystem.
Definition: UnitSystem.h:26
shared_ptr< Syntax > SyntaxPtr
Shared pointer to a Syntax.
Definition: Syntax.h:26
#define MX_GENSHADER_API
Definition: Export.h:18
GLuint const GLchar * name
Definition: glcorearb.h:786
void setUnitSystem(UnitSystemPtr unitSystem)
Sets the unit system.
const StringMap & getTokenSubstitutions() const
Return the map of token substitutions used by the generator.
shared_ptr< class ColorManagementSystem > ColorManagementSystemPtr
A shared pointer to a ColorManagementSystem.
void emitValue(const T &value, ShaderStage &stage) const
Add a value.
UnitSystemPtr _unitSystem
shared_ptr< Document > DocumentPtr
A shared pointer to a Document.
Definition: Document.h:22
GLenum GLenum variable
Definition: glew.h:14162
void setFunctionName(const string &functionName)
Set stage function name.
Definition: ShaderStage.h:250
ColorManagementSystemPtr getColorManagementSystem() const
Returns the color management system.
GLuint shader
Definition: glcorearb.h:785
Definition: Shader.h:32
Exception(const string &msg)
Definition: Exception.h:24
shared_ptr< ShaderStage > ShaderStagePtr
Shared pointer to a ShaderStage.
Definition: Library.h:36
Punctuation
Punctuation types.
Definition: Syntax.h:43
UnitSystemPtr getUnitSystem() const
Returns the unit system.
GLsizei const GLfloat * value
Definition: glcorearb.h:824
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition: Library.h:59
shared_ptr< Shader > ShaderPtr
Shared pointer to a Shader.
Definition: Library.h:34
Definition: core.h:1131
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:30
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:24
virtual ~ShaderGenerator()
Destructor.
Definition: Syntax.h:39
StringMap _tokenSubstitutions