MaterialX 1.38.10
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
18
20
22
23MATERIALX_NAMESPACE_BEGIN
24
30class MX_GENSHADER_API ShaderGenerator
31{
32 public:
34 virtual ~ShaderGenerator() { }
35
37 virtual const string& getTarget() const
38 {
39 return EMPTY_STRING;
40 }
41
44 virtual ShaderPtr generate(const string&, ElementPtr, GenContext&) const
45 {
46 return nullptr;
47 }
48
50 virtual void emitScopeBegin(ShaderStage& stage, Syntax::Punctuation punc = Syntax::CURLY_BRACKETS) const;
51
53 virtual void emitScopeEnd(ShaderStage& stage, bool semicolon = false, bool newline = true) const;
54
56 virtual void emitLineBegin(ShaderStage& stage) const;
57
59 virtual void emitLineEnd(ShaderStage& stage, bool semicolon = true) const;
60
62 virtual void emitLineBreak(ShaderStage& stage) const;
63
65 virtual void emitString(const string& str, ShaderStage& stage) const;
66
68 virtual void emitLine(const string& str, ShaderStage& stage, bool semicolon = true) const;
69
71 virtual void emitComment(const string& str, ShaderStage& stage) const;
72
74 virtual void emitBlock(const string& str, const FilePath& sourceFilename, GenContext& context, ShaderStage& stage) const;
75
79 virtual void emitLibraryInclude(const FilePath& filename, GenContext& context, ShaderStage& stage) const;
80
82 template <typename T>
83 void emitValue(const T& value, ShaderStage& stage) const
84 {
85 stage.addValue<T>(value);
86 }
87
89 virtual void emitFunctionDefinition(const ShaderNode& node, GenContext& context, ShaderStage& stage) const;
90
92 virtual void emitFunctionDefinitions(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
93
95 virtual void emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const;
96 [[deprecated]] virtual void emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage, bool checkScope) const;
97
100 virtual void emitFunctionCalls(const ShaderGraph& graph, GenContext& context, ShaderStage& stage, uint32_t classification = 0u) const;
101
105 virtual void emitDependentFunctionCalls(const ShaderNode& node, GenContext& context, ShaderStage& stage, uint32_t classification = 0u) const;
106
108 virtual void emitFunctionBodyBegin(const ShaderNode& node, GenContext& context, ShaderStage& stage, Syntax::Punctuation punc = Syntax::CURLY_BRACKETS) const;
109
111 virtual void emitFunctionBodyEnd(const ShaderNode& node, GenContext& context, ShaderStage& stage) const;
112
114 virtual void emitTypeDefinitions(GenContext& context, ShaderStage& stage) const;
115
118 virtual void emitInput(const ShaderInput* input, GenContext& context, ShaderStage& stage) const;
119
122 virtual void emitOutput(const ShaderOutput* output, bool includeType, bool assignValue, GenContext& context, ShaderStage& stage) const;
123
131 virtual void emitVariableDeclarations(const VariableBlock& block, const string& qualifier, const string& separator, GenContext& context, ShaderStage& stage,
132 bool assignValue = true) const;
133
140 virtual void emitVariableDeclaration(const ShaderPort* variable, const string& qualifier, GenContext& context, ShaderStage& stage,
141 bool assignValue = true) const;
142
144 virtual void getClosureContexts(const ShaderNode& node, vector<ClosureContext*>& cct) const;
145
147 virtual string getUpstreamResult(const ShaderInput* input, GenContext& context) const;
148
150 const Syntax& getSyntax() const { return *_syntax; }
151
153 void registerImplementation(const string& name, CreatorFunction<ShaderNodeImpl> creator);
154
156 void registerImplementation(const StringVec& nameVec, CreatorFunction<ShaderNodeImpl> creator);
157
159 bool implementationRegistered(const string& name) const;
160
162 virtual ShaderNodeImplPtr getImplementation(const NodeDef& nodedef, GenContext& context) const;
163
166 {
167 _colorManagementSystem = colorManagementSystem;
168 }
169
172 {
173 return _colorManagementSystem;
174 }
175
178 {
179 _unitSystem = unitSystem;
180 }
181
184 {
185 return _unitSystem;
186 }
187
190 {
191 return _tokenSubstitutions;
192 }
193
203 virtual void registerShaderMetadata(const DocumentPtr& doc, GenContext& context) const;
204
205 protected:
208
210 virtual ShaderStagePtr createStage(const string& name, Shader& shader) const;
211
213 void setFunctionName(const string& functionName, ShaderStage& stage) const
214 {
215 stage.setFunctionName(functionName);
216 }
217
219 void replaceTokens(const StringMap& substitutions, ShaderStage& stage) const;
220
223 void createVariables(ShaderGraphPtr graph, GenContext& context, Shader& shader) const;
224
225 protected:
226 static const string T_FILE_TRANSFORM_UV;
227
228 SyntaxPtr _syntax;
229 Factory<ShaderNodeImpl> _implFactory;
230 ColorManagementSystemPtr _colorManagementSystem;
231 UnitSystemPtr _unitSystem;
232 mutable StringMap _tokenSubstitutions;
233
234 friend ShaderGraph;
235};
236
239class MX_GENSHADER_API ExceptionShaderGenError : public Exception
240{
241 public:
242 using Exception::Exception;
243};
244
245MATERIALX_NAMESPACE_END
246
247#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
Base exception classes.
Class instantiator factory helper class.
Cross-platform support for file and search paths.
vector< string > StringVec
A vector of strings.
Definition: Library.h:57
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition: Library.h:59
Macros for declaring imported and exported symbols.
shared_ptr< ShaderStage > ShaderStagePtr
Shared pointer to a ShaderStage.
Definition: Library.h:36
shared_ptr< Shader > ShaderPtr
Shared pointer to a Shader.
Definition: Library.h:34
shared_ptr< ShaderNodeImpl > ShaderNodeImplPtr
Shared pointer to a ShaderNodeImpl.
Definition: Library.h:40
shared_ptr< class ShaderGraph > ShaderGraphPtr
A shared pointer to a shader graph.
Definition: ShaderGraph.h:39
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:26
The base class for exceptions that are propagated from the MaterialX library to the client applicatio...
Definition: Exception.h:22
An exception that is thrown when shader generation fails.
Definition: ShaderGenerator.h:240
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:31
A node definition element within a Document.
Definition: Definition.h:82
Base class for shader generators All third-party shader generators should derive from this class.
Definition: ShaderGenerator.h:31
ColorManagementSystemPtr getColorManagementSystem() const
Returns the color management system.
Definition: ShaderGenerator.h:171
const StringMap & getTokenSubstitutions() const
Return the map of token substitutions used by the generator.
Definition: ShaderGenerator.h:189
void setUnitSystem(UnitSystemPtr unitSystem)
Sets the unit system.
Definition: ShaderGenerator.h:177
void emitValue(const T &value, ShaderStage &stage) const
Add a value.
Definition: ShaderGenerator.h:83
virtual const string & getTarget() const
Return the name of the target this generator is for.
Definition: ShaderGenerator.h:37
void setColorManagementSystem(ColorManagementSystemPtr colorManagementSystem)
Sets the color management system.
Definition: ShaderGenerator.h:165
const Syntax & getSyntax() const
Return the syntax object for the language used by the code generator.
Definition: ShaderGenerator.h:150
virtual ~ShaderGenerator()
Destructor.
Definition: ShaderGenerator.h:34
void setFunctionName(const string &functionName, ShaderStage &stage) const
Set function name for a stage.
Definition: ShaderGenerator.h:213
UnitSystemPtr getUnitSystem() const
Returns the unit system.
Definition: ShaderGenerator.h:183
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:44
Class representing a graph (DAG) for shader generation.
Definition: ShaderGraph.h:44
Class containing all data needed during shader generation.
Definition: Shader.h:33
An input on a ShaderNode.
Definition: ShaderNode.h:264
Class representing a node in the shader generation DAG.
Definition: ShaderNode.h:326
An output on a ShaderNode.
Definition: ShaderNode.h:301
An input or output port on a ShaderNode.
Definition: ShaderNode.h:123
A shader stage, containing the state and resulting source code for the stage.
Definition: ShaderStage.h:139
void setFunctionName(const string &functionName)
Set stage function name.
Definition: ShaderStage.h:284
void addValue(const T &value)
Add a value.
Definition: ShaderStage.h:264
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:61