MaterialX 1.39.1
Loading...
Searching...
No Matches
GenContext.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_GENCONTEXT_H
7#define MATERIALX_GENCONTEXT_H
8
11
13
17
19
20MATERIALX_NAMESPACE_BEGIN
21
22class ClosureContext;
23
25using ApplicationVariableHandler = std::function<void(ShaderNode*, GenContext&)>;
26
30class MX_GENSHADER_API GenContext
31{
32 public:
35
38 {
39 return *_sg;
40 }
41
44 {
45 return _options;
46 }
47
49 const GenOptions& getOptions() const
50 {
51 return _options;
52 }
53
57 {
58 _sourceCodeSearchPath.append(path);
59 }
60
64 {
65 _sourceCodeSearchPath.append(path);
66 }
67
70 FilePath resolveSourceFile(const FilePath& filename, const FilePath& localPath) const
71 {
72 FileSearchPath searchPath = _sourceCodeSearchPath;
73 if (!localPath.isEmpty())
74 {
75 searchPath.prepend(localPath);
76 }
77 return searchPath.find(filename).getNormalized();
78 }
79
82 void addReservedWords(const StringSet& names)
83 {
84 _reservedWords.insert(names.begin(), names.end());
85 }
86
90 {
91 return _reservedWords;
92 }
93
95 void addNodeImplementation(const string& name, ShaderNodeImplPtr impl);
96
99 ShaderNodeImplPtr findNodeImplementation(const string& name) const;
100
103
106
109 {
110 _closureContexts.push_back(cct);
111 }
112
115 {
116 if (_closureContexts.size())
117 {
118 _closureContexts.pop_back();
119 }
120 }
121
124 {
125 return _closureContexts.size() ? _closureContexts.back() : nullptr;
126 }
127
130 {
131 _parentNodes.push_back(node);
132 }
133
136 {
137 _parentNodes.pop_back();
138 }
139
141 const vector<ConstNodePtr>& getParentNodes()
142 {
143 return _parentNodes;
144 }
145
148 void pushUserData(const string& name, GenUserDataPtr data)
149 {
150 auto it = _userData.find(name);
151 if (it != _userData.end())
152 {
153 it->second.push_back(data);
154 }
155 else
156 {
157 _userData[name] = { data };
158 }
159 }
160
162 void popUserData(const string& name)
163 {
164 auto it = _userData.find(name);
165 if (it != _userData.end())
166 {
167 it->second.pop_back();
168 }
169 }
170
173
176 template <class T>
177 std::shared_ptr<T> getUserData(const string& name)
178 {
179 auto it = _userData.find(name);
180 return it != _userData.end() && !it->second.empty() ? it->second.back()->asA<T>() : nullptr;
181 }
182
186 void addInputSuffix(const ShaderInput* input, const string& suffix);
187
190 void removeInputSuffix(const ShaderInput* input);
191
195 void getInputSuffix(const ShaderInput* input, string& suffix) const;
196
200 void addOutputSuffix(const ShaderOutput* output, const string& suffix);
201
204 void removeOutputSuffix(const ShaderOutput* output);
205
209 void getOutputSuffix(const ShaderOutput* output, string& suffix) const;
210
213 {
214 _applicationVariableHandler = handler;
215 }
216
219 {
220 return _applicationVariableHandler;
221 }
222
223 protected:
224 GenContext() = delete;
225
227 GenOptions _options;
228 FileSearchPath _sourceCodeSearchPath;
229 StringSet _reservedWords;
230
231 std::unordered_map<string, ShaderNodeImplPtr> _nodeImpls;
232 std::unordered_map<string, vector<GenUserDataPtr>> _userData;
233 std::unordered_map<const ShaderInput*, string> _inputSuffix;
234 std::unordered_map<const ShaderOutput*, string> _outputSuffix;
235
236 vector<ClosureContext*> _closureContexts;
237 vector<ConstNodePtr> _parentNodes;
238
239 ApplicationVariableHandler _applicationVariableHandler;
240};
241
247class MX_GENSHADER_API ClosureContext
248{
249 public:
253 using Argument = std::pair<TypeDesc, string>;
255 using Arguments = vector<Argument>;
256
258 using ClosureParams = std::unordered_map<string, const ShaderInput*>;
259
261 ClosureContext(int type = 0) :
262 _type(type) { }
263
265 int getType() const { return _type; }
266
268 void addArgument(TypeDesc nodeType, const Argument& arg)
269 {
270 _arguments[nodeType].push_back(arg);
271 }
272 [[deprecated]] void addArgument(const TypeDesc* nodeType, const Argument& arg) { addArgument(*nodeType, arg); }
273
275 const Arguments& getArguments(TypeDesc nodeType) const
276 {
277 auto it = _arguments.find(nodeType);
278 return it != _arguments.end() ? it->second : EMPTY_ARGUMENTS;
279 }
280 [[deprecated]] const Arguments& getArguments(const TypeDesc* nodeType) const { return getArguments(*nodeType); }
281
283 void setSuffix(TypeDesc nodeType, const string& suffix)
284 {
285 _suffix[nodeType] = suffix;
286 }
287 [[deprecated]] void setSuffix(const TypeDesc* nodeType, const string& suffix) { setSuffix(*nodeType, suffix); }
288
290 const string& getSuffix(TypeDesc nodeType) const
291 {
292 auto it = _suffix.find(nodeType);
293 return it != _suffix.end() ? it->second : EMPTY_STRING;
294 }
295 [[deprecated]] const string& getSuffix(const TypeDesc* nodeType) const { return getSuffix(*nodeType); }
296
298 void setClosureParams(const ShaderNode* closure, const ClosureParams* params)
299 {
300 if (params)
301 {
302 _params[closure] = params;
303 }
304 else
305 {
306 _params.erase(closure);
307 }
308 }
309
312 const ClosureParams* getClosureParams(const ShaderNode* closure) const
313 {
314 auto it = _params.find(closure);
315 return it != _params.end() ? it->second : nullptr;
316 }
317
318 protected:
319 const int _type;
320 std::unordered_map<TypeDesc, Arguments, TypeDesc::Hasher> _arguments;
321 std::unordered_map<TypeDesc, string, TypeDesc::Hasher> _suffix;
322 std::unordered_map<const ShaderNode*, const ClosureParams*> _params;
323
324 static const Arguments EMPTY_ARGUMENTS;
325};
326
329class MX_GENSHADER_API ScopedSetClosureParams
330{
331 public:
334
336 ScopedSetClosureParams(const ShaderNode* fromNode, const ShaderNode* toNode, ClosureContext* cct);
337
340
341 private:
342 ClosureContext* _cct;
343 const ShaderNode* _node;
344 const ClosureContext::ClosureParams* _oldParams;
345};
346
348class MX_GENSHADER_API ScopedSetVariableName
349{
350 public:
352 ScopedSetVariableName(const string& name, ShaderPort* port);
353
356
357 private:
358 ShaderPort* _port;
359 string _oldName;
360};
361
362MATERIALX_NAMESPACE_END
363
364#endif // MATERIALX_GENCONTEXT_H
Cross-platform support for file and search paths.
std::function< void(ShaderNode *, GenContext &)> ApplicationVariableHandler
A standard function to allow for handling of application variables for a given node.
Definition: GenContext.h:25
Shader generation options class.
User data base class for shader generation.
std::shared_ptr< GenUserData > GenUserDataPtr
Shared pointer to a GenUserData.
Definition: GenUserData.h:19
std::set< string > StringSet
A set of strings.
Definition: Library.h:59
Macros for declaring imported and exported symbols.
shared_ptr< ShaderNodeImpl > ShaderNodeImplPtr
Shared pointer to a ShaderNodeImpl.
Definition: Library.h:40
shared_ptr< ShaderGenerator > ShaderGeneratorPtr
Shared pointer to a ShaderGenerator.
Definition: Library.h:38
shared_ptr< const Node > ConstNodePtr
A shared pointer to a const Node.
Definition: Node.h:26
Classes for nodes created during shader generation.
Class representing a context for closure evaluation.
Definition: GenContext.h:248
void setClosureParams(const ShaderNode *closure, const ClosureParams *params)
Set extra parameters to use for evaluating a closure.
Definition: GenContext.h:298
vector< Argument > Arguments
An array of arguments.
Definition: GenContext.h:255
const string & getSuffix(TypeDesc nodeType) const
Return the function name suffix to be used for the given node in this context.
Definition: GenContext.h:290
ClosureContext(int type=0)
Constructor.
Definition: GenContext.h:261
void addArgument(TypeDesc nodeType, const Argument &arg)
For the given node type add an extra argument to be used for the function in this context.
Definition: GenContext.h:268
void setSuffix(TypeDesc nodeType, const string &suffix)
For the given node type set a function name suffix to be used for the function in this context.
Definition: GenContext.h:283
const Arguments & getArguments(TypeDesc nodeType) const
Return a list of extra argument to be used for the given node in this context.
Definition: GenContext.h:275
std::unordered_map< string, const ShaderInput * > ClosureParams
Extra parameters for closure evaluation.
Definition: GenContext.h:258
int getType() const
Return the identifier for this context.
Definition: GenContext.h:265
std::pair< TypeDesc, string > Argument
An extra argument for closure functions.
Definition: GenContext.h:253
const ClosureParams * getClosureParams(const ShaderNode *closure) const
Return extra parameters to use for evaluating a closure.
Definition: GenContext.h:312
A generic file path, supporting both syntactic and file system operations.
Definition: File.h:27
bool isEmpty() const
Return true if the given path is empty.
Definition: File.h:92
FilePath getNormalized() const
Return a normalized version of the given path, collapsing current path and parent path references so ...
A sequence of file paths, which may be queried to find the first instance of a given filename on the ...
Definition: File.h:219
void prepend(const FilePath &path)
Prepend the given path to the sequence.
Definition: File.h:274
FilePath find(const FilePath &filename) const
Given an input filename, iterate through each path in this sequence, returning the first combined pat...
Definition: File.h:313
A context class for shader generation.
Definition: GenContext.h:31
void registerSourceCodeSearchPath(const FilePath &path)
Register a user search path for finding source code during code generation.
Definition: GenContext.h:56
void popParentNode()
Pop the current parent node from the stack.
Definition: GenContext.h:135
GenOptions & getOptions()
Return shader generation options.
Definition: GenContext.h:43
const vector< ConstNodePtr > & getParentNodes()
Return the current stack of parent nodes.
Definition: GenContext.h:141
void setApplicationVariableHandler(ApplicationVariableHandler handler)
Set handler for application variables.
Definition: GenContext.h:212
ShaderGenerator & getShaderGenerator()
Return shader generatior.
Definition: GenContext.h:37
void getInputSuffix(const ShaderInput *input, string &suffix) const
Get an input suffix to be used for the input in this context.
void clearUserData()
Clear all user data from the context.
void clearNodeImplementations()
Clear all cached shader node implementation.
void popUserData(const string &name)
Remove user data from the context.
Definition: GenContext.h:162
ShaderNodeImplPtr findNodeImplementation(const string &name) const
Find and return a cached shader node implementation, or return nullptr if no implementation is found.
void removeInputSuffix(const ShaderInput *input)
Remove an input suffix to be used for the input in this context.
void getNodeImplementationNames(StringSet &names)
Get the names of all cached node implementations.
void addInputSuffix(const ShaderInput *input, const string &suffix)
Add an input suffix to be used for the input in this context.
void addOutputSuffix(const ShaderOutput *output, const string &suffix)
Add an output suffix to be used for the output in this context.
void removeOutputSuffix(const ShaderOutput *output)
Remove an output suffix to be used for the output in this context.
void pushClosureContext(ClosureContext *cct)
Push a new closure context to use for closure evaluation.
Definition: GenContext.h:108
void addNodeImplementation(const string &name, ShaderNodeImplPtr impl)
Cache a shader node implementation.
void popClosureContext()
Pop the current closure context.
Definition: GenContext.h:114
void addReservedWords(const StringSet &names)
Add reserved words that should not be used as identifiers during code generation.
Definition: GenContext.h:82
void pushUserData(const string &name, GenUserDataPtr data)
Add user data to the context to make it available during shader generator.
Definition: GenContext.h:148
GenContext(ShaderGeneratorPtr sg)
Constructor.
void registerSourceCodeSearchPath(const FileSearchPath &path)
Register a user search path for finding source code during code generation.
Definition: GenContext.h:63
FilePath resolveSourceFile(const FilePath &filename, const FilePath &localPath) const
Resolve a source code filename, first checking the given local path then checking any file paths regi...
Definition: GenContext.h:70
ApplicationVariableHandler getApplicationVariableHandler() const
Get handler for application variables.
Definition: GenContext.h:218
void pushParentNode(ConstNodePtr node)
Push a parent node onto the stack.
Definition: GenContext.h:129
ClosureContext * getClosureContext()
Return the current closure context.
Definition: GenContext.h:123
const GenOptions & getOptions() const
Return shader generation options.
Definition: GenContext.h:49
void getOutputSuffix(const ShaderOutput *output, string &suffix) const
Get an output suffix to be used for the output in this context.
std::shared_ptr< T > getUserData(const string &name)
Return user data with given name, or nullptr if no data is found.
Definition: GenContext.h:177
const StringSet & getReservedWords() const
Return the set of reserved words that should not be used as identifiers during code generation.
Definition: GenContext.h:89
Class holding options to configure shader generation.
Definition: GenOptions.h:76
A RAII class for setting extra parameters for closure evaluation, stored in the closure context.
Definition: GenContext.h:330
ScopedSetClosureParams(const ClosureContext::ClosureParams *params, const ShaderNode *node, ClosureContext *cct)
Constructor for setting explicit parameters for a closure node.
~ScopedSetClosureParams()
Destructor restoring the closure parameter state.
ScopedSetClosureParams(const ShaderNode *fromNode, const ShaderNode *toNode, ClosureContext *cct)
Constructor for setting parameters from one closure node to another.
A RAII class for overriding port variable names.
Definition: GenContext.h:349
~ScopedSetVariableName()
Destructor restoring the original variable name.
ScopedSetVariableName(const string &name, ShaderPort *port)
Constructor for setting a new variable name for a port.
Base class for shader generators All third-party shader generators should derive from this class.
Definition: ShaderGenerator.h:31
An input on a ShaderNode.
Definition: ShaderNode.h:264
Class representing a node in the shader generation DAG.
Definition: ShaderNode.h:320
An output on a ShaderNode.
Definition: ShaderNode.h:295
An input or output port on a ShaderNode.
Definition: ShaderNode.h:123
A type descriptor for MaterialX data types.
Definition: TypeDesc.h:35