MaterialX 1.39.5
Loading...
Searching...
No Matches
ShaderGenerator.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_SHADERGENERATOR_H
7#define MATERIALX_SHADERGENERATOR_H
8
11
13
20
22
23
24MATERIALX_NAMESPACE_BEGIN
25
31class MX_GENSHADER_API ShaderGenerator
32{
33 public:
35 virtual ~ShaderGenerator() { }
36
38 virtual const string& getTarget() const
39 {
40 return EMPTY_STRING;
41 }
42
45 virtual ShaderPtr generate(const string&, ElementPtr, GenContext&) const
46 {
47 return nullptr;
48 }
49
51 virtual void emitScopeBegin(ShaderStage& stage, Syntax::Punctuation punc = Syntax::CURLY_BRACKETS) const;
52
54 virtual void emitScopeEnd(ShaderStage& stage, bool semicolon = false, bool newline = true) const;
55
57 virtual void emitLineBegin(ShaderStage& stage) const;
58
60 virtual void emitLineEnd(ShaderStage& stage, bool semicolon = true) const;
61
63 virtual void emitLineBreak(ShaderStage& stage) const;
64
66 virtual void emitString(const string& str, ShaderStage& stage) const;
67
69 virtual void emitLine(const string& str, ShaderStage& stage, bool semicolon = true) const;
70
72 virtual void emitComment(const string& str, ShaderStage& stage) const;
73
75 virtual void emitBlock(const string& str, const FilePath& sourceFilename, GenContext& context, ShaderStage& stage) const;
76
80 virtual void emitLibraryInclude(const FilePath& filename, GenContext& context, ShaderStage& stage) const;
81
83 template <typename T>
84 void emitValue(const T& value, ShaderStage& stage) const
85 {
86 stage.addValue<T>(value);
87 }
88
90 virtual void emitFunctionDefinition(const ShaderNode& node, GenContext& context, ShaderStage& stage) const;
91
92 // Emit the connected variable name for an input and output in a function definition (Used by CompoundNode)
93 virtual void emitFunctionDefinitionParameter(const ShaderPort* shaderPort, bool isOutput, GenContext& context, ShaderStage& stage) const;
94
96 virtual void emitFunctionDefinitions(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
97
99 virtual void emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const;
100 [[deprecated]] virtual void emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage, bool checkScope) const;
101
104 virtual void emitFunctionCalls(const ShaderGraph& graph, GenContext& context, ShaderStage& stage, uint32_t classification = 0u) const;
105
109 virtual void emitDependentFunctionCalls(const ShaderNode& node, GenContext& context, ShaderStage& stage, uint32_t classification = 0u) const;
110
112 virtual void emitFunctionBodyBegin(const ShaderNode& node, GenContext& context, ShaderStage& stage, Syntax::Punctuation punc = Syntax::CURLY_BRACKETS) const;
113
115 virtual void emitFunctionBodyEnd(const ShaderNode& node, GenContext& context, ShaderStage& stage) const;
116
118 virtual void emitTypeDefinitions(GenContext& context, ShaderStage& stage) const;
119
122 virtual void emitInput(const ShaderInput* input, GenContext& context, ShaderStage& stage) const;
123
126 virtual void emitOutput(const ShaderOutput* output, bool includeType, bool assignValue, GenContext& context, ShaderStage& stage) const;
127
135 virtual void emitVariableDeclarations(const VariableBlock& block, const string& qualifier, const string& separator, GenContext& context, ShaderStage& stage,
136 bool assignValue = true) const;
137
144 virtual void emitVariableDeclaration(const ShaderPort* variable, const string& qualifier, GenContext& context, ShaderStage& stage,
145 bool assignValue = true) const;
146
148 virtual bool nodeNeedsClosureData(const ShaderNode& /*node*/) const { return false; }
149
152 virtual void emitClosureDataArg(const ShaderNode& /*node*/, GenContext& /*context*/, ShaderStage& /*stage*/) const {}
153
156 virtual void emitClosureDataParameter(const ShaderNode& /*node*/, GenContext& /*context*/, ShaderStage& /*stage*/) const {}
157
159 virtual string getUpstreamResult(const ShaderInput* input, GenContext& context) const;
160
162 const Syntax& getSyntax() const { return *_syntax; }
163
165 void registerImplementation(const string& name, CreatorFunction<ShaderNodeImpl> creator);
166
168 void registerImplementation(const StringVec& nameVec, CreatorFunction<ShaderNodeImpl> creator);
169
171 bool implementationRegistered(const string& name) const;
172
175
178
180 virtual ShaderNodeImplPtr getImplementation(const NodeDef& nodedef, GenContext& context) const;
181
184 {
185 _colorManagementSystem = colorManagementSystem;
186 }
187
190 {
191 return _colorManagementSystem;
192 }
193
196 {
197 _unitSystem = unitSystem;
198 }
199
202 {
203 return _unitSystem;
204 }
205
207 TypeSystemPtr getTypeSystem() const
208 {
209 return _typeSystem;
210 }
211
214 {
215 return _tokenSubstitutions;
216 }
217
220 {
221 _refactors.push_back(refactor);
222 }
223
225 const vector<ShaderGraphRefactorPtr>& getRefactors() const
226 {
227 return _refactors;
228 }
229
231 virtual void applyDefaultOptions(GenOptions& options) const;
232
234 virtual void registerTypeDefs(const DocumentPtr& doc);
235
245 virtual void registerShaderMetadata(const DocumentPtr& doc, GenContext& context) const;
246
247 // Retrieve the string used for the LightData.type member variable
248 virtual const string& getLightDataTypevarString() const { return LIGHTDATA_TYPEVAR_STRING; }
249
250 protected:
252 ShaderGenerator(TypeSystemPtr typeSystem, SyntaxPtr syntax);
253
255 virtual ShaderStagePtr createStage(const string& name, Shader& shader) const;
256
258 void setFunctionName(const string& functionName, ShaderStage& stage) const
259 {
260 stage.setFunctionName(functionName);
261 }
262
264 void replaceTokens(const StringMap& substitutions, ShaderStage& stage) const;
265
268 void createVariables(ShaderGraphPtr graph, GenContext& context, Shader& shader) const;
269
270 protected:
271 static const string T_FILE_TRANSFORM_UV;
272 static const string LIGHTDATA_TYPEVAR_STRING;
273
274 TypeSystemPtr _typeSystem;
275 SyntaxPtr _syntax;
276 Factory<ShaderNodeImpl> _implFactory;
277 ColorManagementSystemPtr _colorManagementSystem;
278 UnitSystemPtr _unitSystem;
279 mutable StringMap _tokenSubstitutions;
280 vector<ShaderGraphRefactorPtr> _refactors;
281
282 friend ShaderGraph;
283};
284
285MATERIALX_NAMESPACE_END
286
287#endif // MATERIALX_SHADERGENERATOR_H
Color management system classes.
shared_ptr< class ColorManagementSystem > ColorManagementSystemPtr
A shared pointer to a ColorManagementSystem.
Definition ColorManagementSystem.h:25
shared_ptr< Document > DocumentPtr
A shared pointer to a Document.
Definition Document.h:22
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition Element.h:31
Class instantiator factory helper class.
Cross-platform support for file and search paths.
vector< string > StringVec
A vector of strings.
Definition Library.h:61
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition Library.h:63
Base shader generator exception class.
Macros for declaring imported and exported symbols.
shared_ptr< ShaderStage > ShaderStagePtr
Shared pointer to a ShaderStage.
Definition Library.h:35
shared_ptr< Shader > ShaderPtr
Shared pointer to a Shader.
Definition Library.h:33
shared_ptr< ShaderNodeImpl > ShaderNodeImplPtr
Shared pointer to a ShaderNodeImpl.
Definition Library.h:39
shared_ptr< class ShaderGraph > ShaderGraphPtr
A shared pointer to a shader graph.
Definition ShaderGraph.h:40
Shader graph refactoring passes.
shared_ptr< class ShaderGraphRefactor > ShaderGraphRefactorPtr
A shared pointer to a shader graph refactor.
Definition ShaderGraphRefactor.h:20
Class related to holding information for shader stages.
Base class for syntax handling for shader generators.
shared_ptr< Syntax > SyntaxPtr
Shared pointer to a Syntax.
Definition Syntax.h:28
shared_ptr< class UnitSystem > UnitSystemPtr
A shared pointer to a UnitSystem.
Definition UnitSystem.h:26
Factory class for creating instances of classes given their type name.
Definition Factory.h:19
A generic file path, supporting both syntactic and file system operations.
Definition File.h:27
A context class for shader generation.
Definition GenContext.h:30
Class holding options to configure shader generation.
Definition GenOptions.h:76
An implementation element within a Document.
Definition Definition.h:204
A node definition element within a Document.
Definition Definition.h:82
A node graph element within a Document.
Definition Node.h:332
virtual void emitScopeBegin(ShaderStage &stage, Syntax::Punctuation punc=Syntax::CURLY_BRACKETS) const
Start a new scope using the given bracket type.
bool implementationRegistered(const string &name) const
Determine if a shader node implementation has been registered for a given implementation element name...
const vector< ShaderGraphRefactorPtr > & getRefactors() const
Return the registered graph refactoring passes.
Definition ShaderGenerator.h:225
virtual void emitVariableDeclaration(const ShaderPort *variable, const string &qualifier, GenContext &context, ShaderStage &stage, bool assignValue=true) const
Emit definition of a single shader variable.
virtual void emitDependentFunctionCalls(const ShaderNode &node, GenContext &context, ShaderStage &stage, uint32_t classification=0u) const
Add function calls for nodes connected directly upstream from the given node.
virtual void emitBlock(const string &str, const FilePath &sourceFilename, GenContext &context, ShaderStage &stage) const
Add a block of code.
void registerRefactor(ShaderGraphRefactorPtr refactor)
Register a shader graph refactoring pass.
Definition ShaderGenerator.h:219
void createVariables(ShaderGraphPtr graph, GenContext &context, Shader &shader) const
Create shader variables (e.g.
virtual void emitVariableDeclarations(const VariableBlock &block, const string &qualifier, const string &separator, GenContext &context, ShaderStage &stage, bool assignValue=true) const
Emit definitions for all shader variables in a block.
virtual void emitOutput(const ShaderOutput *output, bool includeType, bool assignValue, GenContext &context, ShaderStage &stage) const
Emit the output variable name for an output, optionally including it's type and default value assignm...
virtual void emitClosureDataArg(const ShaderNode &, GenContext &, ShaderStage &) const
Emit the closure data argument if required Note this is an affordance for HwShaderGenerator.
Definition ShaderGenerator.h:152
virtual ShaderNodeImplPtr createShaderNodeImplForNodeGraph(const NodeGraph &nodegraph) const
Create the shader node implementation for a NodeGraph implementation.
virtual void emitComment(const string &str, ShaderStage &stage) const
Add a single line code comment.
virtual void emitString(const string &str, ShaderStage &stage) const
Add a string.
ColorManagementSystemPtr getColorManagementSystem() const
Returns the color management system.
Definition ShaderGenerator.h:189
const StringMap & getTokenSubstitutions() const
Return the map of token substitutions used by the generator.
Definition ShaderGenerator.h:213
virtual string getUpstreamResult(const ShaderInput *input, GenContext &context) const
Return the result of an upstream connection or value for an input.
void registerImplementation(const StringVec &nameVec, CreatorFunction< ShaderNodeImpl > creator)
Register a shader node implementation for a given set of implementation element names.
virtual void emitFunctionBodyBegin(const ShaderNode &node, GenContext &context, ShaderStage &stage, Syntax::Punctuation punc=Syntax::CURLY_BRACKETS) const
Emit code for starting a new function body.
void setUnitSystem(UnitSystemPtr unitSystem)
Sets the unit system.
Definition ShaderGenerator.h:195
virtual void emitFunctionBodyEnd(const ShaderNode &node, GenContext &context, ShaderStage &stage) const
Emit code for ending a function body.
void registerImplementation(const string &name, CreatorFunction< ShaderNodeImpl > creator)
Register a shader node implementation for a given implementation element name.
virtual void emitLibraryInclude(const FilePath &filename, GenContext &context, ShaderStage &stage) const
Add the contents of a standard library include file if not already present.
virtual void registerShaderMetadata(const DocumentPtr &doc, GenContext &context) const
Register metadata that should be exported to the generated shaders.
virtual void emitTypeDefinitions(GenContext &context, ShaderStage &stage) const
Emit type definitions for all data types that needs it.
void emitValue(const T &value, ShaderStage &stage) const
Add a value.
Definition ShaderGenerator.h:84
void replaceTokens(const StringMap &substitutions, ShaderStage &stage) const
Replace tokens with identifiers according to the given substitutions map.
ShaderGenerator(TypeSystemPtr typeSystem, SyntaxPtr syntax)
Protected constructor.
virtual const string & getTarget() const
Return the name of the target this generator is for.
Definition ShaderGenerator.h:38
virtual void emitFunctionCalls(const ShaderGraph &graph, GenContext &context, ShaderStage &stage, uint32_t classification=0u) const
Add all function calls for a graph.
virtual void applyDefaultOptions(GenOptions &options) const
Apply the default GenOptions for this generator's target.
virtual ShaderNodeImplPtr getImplementation(const NodeDef &nodedef, GenContext &context) const
Return a registered shader node implementation for the given nodedef.
virtual ShaderStagePtr createStage(const string &name, Shader &shader) const
Create a new stage in a shader.
TypeSystemPtr getTypeSystem() const
Returns the type system.
Definition ShaderGenerator.h:207
virtual void emitLineBegin(ShaderStage &stage) const
Start a new line.
virtual void emitClosureDataParameter(const ShaderNode &, GenContext &, ShaderStage &) const
Emit the closure data parameter if required.
Definition ShaderGenerator.h:156
void setColorManagementSystem(ColorManagementSystemPtr colorManagementSystem)
Sets the color management system.
Definition ShaderGenerator.h:183
const Syntax & getSyntax() const
Return the syntax object for the language used by the code generator.
Definition ShaderGenerator.h:162
virtual void emitLine(const string &str, ShaderStage &stage, bool semicolon=true) const
Add a single line of code, optionally appending a semicolon.
virtual bool nodeNeedsClosureData(const ShaderNode &) const
Return true if the node needs the additional ClosureData added.
Definition ShaderGenerator.h:148
virtual ~ShaderGenerator()
Destructor.
Definition ShaderGenerator.h:35
virtual void emitFunctionDefinitions(const ShaderGraph &graph, GenContext &context, ShaderStage &stage) const
Add all function definitions for a graph.
virtual void emitLineBreak(ShaderStage &stage) const
Add a line break.
virtual void emitFunctionDefinition(const ShaderNode &node, GenContext &context, ShaderStage &stage) const
Add the function definition for a single node.
void setFunctionName(const string &functionName, ShaderStage &stage) const
Set function name for a stage.
Definition ShaderGenerator.h:258
virtual void registerTypeDefs(const DocumentPtr &doc)
Register type definitions from the document.
virtual void emitInput(const ShaderInput *input, GenContext &context, ShaderStage &stage) const
Emit the connected variable name for an input, or constant value if the port is not connected.
virtual ShaderNodeImplPtr createShaderNodeImplForImplementation(const Implementation &implementation) const
Create the shader node implementation for an Implementation implementation.
virtual void emitLineEnd(ShaderStage &stage, bool semicolon=true) const
End the current line.
UnitSystemPtr getUnitSystem() const
Returns the unit system.
Definition ShaderGenerator.h:201
virtual void emitFunctionCall(const ShaderNode &node, GenContext &context, ShaderStage &stage) const
Add the function call for a single node.
virtual void emitScopeEnd(ShaderStage &stage, bool semicolon=false, bool newline=true) const
End the current scope.
virtual ShaderPtr generate(const string &, ElementPtr, GenContext &) const
Generate a shader starting from the given element, translating the element and all dependencies upstr...
Definition ShaderGenerator.h:45
Class containing all data needed during shader generation.
Definition Shader.h:33
An input on a ShaderNode.
Definition ShaderNode.h:272
Class representing a node in the shader generation DAG.
Definition ShaderNode.h:328
An output on a ShaderNode.
Definition ShaderNode.h:303
An input or output port on a ShaderNode.
Definition ShaderNode.h:124
A shader stage, containing the state and resulting source code for the stage.
Definition ShaderStage.h:145
void setFunctionName(const string &functionName)
Set stage function name.
Definition ShaderStage.h:293
void addValue(const T &value)
Add a value.
Definition ShaderStage.h:273
Base class for syntax objects used by shader generators to emit code with correct syntax for each lan...
Definition Syntax.h:44
Punctuation
Punctuation types.
Definition Syntax.h:48
A block of variables in a shader stage.
Definition ShaderStage.h:67