MaterialX 1.38.2
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
11
13
18
20
21namespace MaterialX
22{
23
29class MX_GENSHADER_API ShaderGenerator
30{
31 public:
33 virtual ~ShaderGenerator() { }
34
36 virtual const string& getTarget() const = 0;
37
40 virtual ShaderPtr generate(const string& name, ElementPtr element, GenContext& context) const = 0;
41
43 virtual void emitScopeBegin(ShaderStage& stage, Syntax::Punctuation punc = Syntax::CURLY_BRACKETS) const;
44
46 virtual void emitScopeEnd(ShaderStage& stage, bool semicolon = false, bool newline = true) const;
47
49 virtual void emitLineBegin(ShaderStage& stage) const;
50
52 virtual void emitLineEnd(ShaderStage& stage, bool semicolon = true) const;
53
55 virtual void emitLineBreak(ShaderStage& stage) const;
56
58 virtual void emitString(const string& str, ShaderStage& stage) const;
59
61 virtual void emitLine(const string& str, ShaderStage& stage, bool semicolon = true) const;
62
64 virtual void emitComment(const string& str, ShaderStage& stage) const;
65
67 virtual void emitBlock(const string& str, GenContext& context, ShaderStage& stage) const;
68
71 virtual void emitInclude(const string& file, GenContext& context, ShaderStage& stage) const;
72
74 template<typename T>
75 void emitValue(const T& value, ShaderStage& stage) const
76 {
77 stage.addValue<T>(value);
78 }
79
81 virtual void emitFunctionDefinition(const ShaderNode& node, GenContext& context, ShaderStage& stage) const;
82
84 virtual void emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage,
85 bool checkScope = true) const;
86
88 virtual void emitFunctionDefinitions(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
89
91 virtual void emitFunctionCalls(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
92
94 virtual void emitTypeDefinitions(GenContext& context, ShaderStage& stage) const;
95
98 virtual void emitInput(const ShaderInput* input, GenContext& context, ShaderStage& stage) const;
99
102 virtual void emitOutput(const ShaderOutput* output, bool includeType, bool assignValue, GenContext& context, ShaderStage& stage) const;
103
111 virtual void emitVariableDeclarations(const VariableBlock& block, const string& qualifier, const string& separator, GenContext& context, ShaderStage& stage,
112 bool assignValue = true) const;
113
120 virtual void emitVariableDeclaration(const ShaderPort* variable, const string& qualifier, GenContext& context, ShaderStage& stage,
121 bool assignValue = true) const;
122
124 virtual string getUpstreamResult(const ShaderInput* input, GenContext& context) const;
125
127 const Syntax& getSyntax() const { return *_syntax; }
128
130 void registerImplementation(const string& name, CreatorFunction<ShaderNodeImpl> creator);
131
133 bool implementationRegistered(const string& name) const;
134
137 {
138 _colorManagementSystem = colorManagementSystem;
139 }
140
143 {
144 return _colorManagementSystem;
145 }
146
149 {
150 _unitSystem = unitSystem;
151 }
152
155 {
156 return _unitSystem;
157 }
158
163 ShaderNodeImplPtr getImplementation(const InterfaceElement& element, GenContext& context) const;
164
167 {
168 return _tokenSubstitutions;
169 }
170
180 virtual void registerShaderMetadata(const DocumentPtr& doc, GenContext& context) const;
181
182 protected:
185
187 virtual ShaderStagePtr createStage(const string& name, Shader& shader) const;
188
192 virtual ShaderNodeImplPtr createSourceCodeImplementation(const Implementation& impl) const;
193
197 virtual ShaderNodeImplPtr createCompoundImplementation(const NodeGraph& impl) const;
198
202 virtual void finalizeShaderGraph(ShaderGraph& graph);
203
205 void setFunctionName(const string& functionName, ShaderStage& stage) const
206 {
207 stage.setFunctionName(functionName);
208 }
209
211 void replaceTokens(const StringMap& substitutions, ShaderStage& stage) const;
212
213 protected:
214 static const string T_FILE_TRANSFORM_UV;
215
216 SyntaxPtr _syntax;
217 Factory<ShaderNodeImpl> _implFactory;
218 ColorManagementSystemPtr _colorManagementSystem;
219 UnitSystemPtr _unitSystem;
220 mutable StringMap _tokenSubstitutions;
221
222 friend ShaderGraph;
223};
224
227class MX_GENSHADER_API ExceptionShaderGenError : public Exception
228{
229 public:
230 using Exception::Exception;
231};
232
233} // namespace MaterialX
234
235#endif // MATERIALX_SHADERGENERATOR_H
Color management system classes.
shared_ptr< class ColorManagementSystem > ColorManagementSystemPtr
A shared pointer to a ColorManagementSystem.
Definition: ColorManagementSystem.h:26
shared_ptr< Document > DocumentPtr
A shared pointer to a Document.
Definition: Document.h:23
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:31
Base exception classes.
Class instantiator factory helper class.
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition: Library.h:58
Macros for declaring imported and exported symbols.
shared_ptr< Shader > ShaderPtr
Shared pointer to a Shader.
Definition: Library.h:34
shared_ptr< ShaderStage > ShaderStagePtr
Shared pointer to a ShaderStage.
Definition: Library.h:36
shared_ptr< ShaderNodeImpl > ShaderNodeImplPtr
Shared pointer to a ShaderNodeImpl.
Definition: Library.h:40
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:26
shared_ptr< class UnitSystem > UnitSystemPtr
A shared pointer to a UnitSystem.
Definition: UnitSystem.h:27
The base class for exceptions that are propagated from the MaterialX library to the client applicatio...
Definition: Exception.h:23
An exception that is thrown when shader generation fails.
Definition: ShaderGenerator.h:228
Factory class for creating instances of classes given their type name.
Definition: Factory.h:20
A context class for shader generation.
Definition: GenContext.h:27
An implementation element within a Document.
Definition: Definition.h:194
The base class for interface elements such as Node, NodeDef, and NodeGraph.
Definition: Interface.h:317
A node graph element within a Document.
Definition: Node.h:316
Base class for shader generators All third-party shader generators should derive from this class.
Definition: ShaderGenerator.h:30
ColorManagementSystemPtr getColorManagementSystem() const
Returns the color management system.
Definition: ShaderGenerator.h:142
virtual const string & getTarget() const =0
Return the name of the target this generator is for.
const StringMap & getTokenSubstitutions() const
Return the map of token substitutions used by the generator.
Definition: ShaderGenerator.h:166
void setUnitSystem(UnitSystemPtr unitSystem)
Sets the unit system.
Definition: ShaderGenerator.h:148
void emitValue(const T &value, ShaderStage &stage) const
Add a value.
Definition: ShaderGenerator.h:75
void setColorManagementSystem(ColorManagementSystemPtr colorManagementSystem)
Sets the color management system.
Definition: ShaderGenerator.h:136
const Syntax & getSyntax() const
Return the syntax object for the language used by the code generator.
Definition: ShaderGenerator.h:127
virtual ~ShaderGenerator()
Destructor.
Definition: ShaderGenerator.h:33
void setFunctionName(const string &functionName, ShaderStage &stage) const
Set function name for a stage.
Definition: ShaderGenerator.h:205
virtual ShaderPtr generate(const string &name, ElementPtr element, GenContext &context) const =0
Generate a shader starting from the given element, translating the element and all dependencies upstr...
UnitSystemPtr getUnitSystem() const
Returns the unit system.
Definition: ShaderGenerator.h:154
Class representing a graph (DAG) for shader generation.
Definition: ShaderGraph.h:45
Class containing all data needed during shader generation.
Definition: Shader.h:34
An input on a ShaderNode.
Definition: ShaderNode.h:256
Class representing a node in the shader generation DAG.
Definition: ShaderNode.h:326
An output on a ShaderNode.
Definition: ShaderNode.h:289
An input or output port on a ShaderNode.
Definition: ShaderNode.h:125
A shader stage, containing the state and resulting source code for the stage.
Definition: ShaderStage.h:124
void setFunctionName(const string &functionName)
Set stage function name.
Definition: ShaderStage.h:234
void addValue(const T &value)
Add a value.
Definition: ShaderStage.h:223
Base class for syntax objects used by shader generators to emit code with correct syntax for each lan...
Definition: Syntax.h:40
Punctuation
Punctuation types.
Definition: Syntax.h:44
A block of variables in a shader stage.
Definition: ShaderStage.h:53