MaterialX 1.38.8
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
102 void getNodeImplementationNames(StringSet& names);
103
105 void clearNodeImplementations();
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 void pushUserData(const string& name, GenUserDataPtr data)
131 {
132 auto it = _userData.find(name);
133 if (it != _userData.end())
134 {
135 it->second.push_back(data);
136 }
137 else
138 {
139 _userData[name] = { data };
140 }
141 }
142
144 void popUserData(const string& name)
145 {
146 auto it = _userData.find(name);
147 if (it != _userData.end())
148 {
149 it->second.pop_back();
150 }
151 }
152
154 void clearUserData();
155
158 template <class T>
159 std::shared_ptr<T> getUserData(const string& name)
160 {
161 auto it = _userData.find(name);
162 return it != _userData.end() && !it->second.empty() ? it->second.back()->asA<T>() : nullptr;
163 }
164
168 void addInputSuffix(const ShaderInput* input, const string& suffix);
169
172 void removeInputSuffix(const ShaderInput* input);
173
177 void getInputSuffix(const ShaderInput* input, string& suffix) const;
178
182 void addOutputSuffix(const ShaderOutput* output, const string& suffix);
183
186 void removeOutputSuffix(const ShaderOutput* output);
187
191 void getOutputSuffix(const ShaderOutput* output, string& suffix) const;
192
195 {
196 _applicationVariableHandler = handler;
197 }
198
201 {
202 return _applicationVariableHandler;
203 }
204
205 protected:
206 GenContext() = delete;
207
209 GenOptions _options;
210 FileSearchPath _sourceCodeSearchPath;
211 StringSet _reservedWords;
212
213 std::unordered_map<string, ShaderNodeImplPtr> _nodeImpls;
214 std::unordered_map<string, vector<GenUserDataPtr>> _userData;
215 std::unordered_map<const ShaderInput*, string> _inputSuffix;
216 std::unordered_map<const ShaderOutput*, string> _outputSuffix;
217
218 vector<ClosureContext*> _closureContexts;
219
220 ApplicationVariableHandler _applicationVariableHandler;
221};
222
228class MX_GENSHADER_API ClosureContext
229{
230 public:
234 using Argument = std::pair<const TypeDesc*, string>;
236 using Arguments = vector<Argument>;
237
239 using ClosureParams = std::unordered_map<string, const ShaderInput*>;
240
242 ClosureContext(int type = 0) :
243 _type(type) { }
244
246 int getType() const { return _type; }
247
249 void addArgument(const TypeDesc* nodeType, const Argument& arg)
250 {
251 _arguments[nodeType].push_back(arg);
252 }
253
255 const Arguments& getArguments(const TypeDesc* nodeType) const
256 {
257 auto it = _arguments.find(nodeType);
258 return it != _arguments.end() ? it->second : EMPTY_ARGUMENTS;
259 }
260
262 void setSuffix(const TypeDesc* nodeType, const string& suffix)
263 {
264 _suffix[nodeType] = suffix;
265 }
266
268 const string& getSuffix(const TypeDesc* nodeType) const
269 {
270 auto it = _suffix.find(nodeType);
271 return it != _suffix.end() ? it->second : EMPTY_STRING;
272 }
273
275 void setClosureParams(const ShaderNode* closure, const ClosureParams* params)
276 {
277 if (params)
278 {
279 _params[closure] = params;
280 }
281 else
282 {
283 _params.erase(closure);
284 }
285 }
286
289 const ClosureParams* getClosureParams(const ShaderNode* closure) const
290 {
291 auto it = _params.find(closure);
292 return it != _params.end() ? it->second : nullptr;
293 }
294
295 protected:
296 const int _type;
297 std::unordered_map<const TypeDesc*, Arguments> _arguments;
298 std::unordered_map<const TypeDesc*, string> _suffix;
299 std::unordered_map<const ShaderNode*, const ClosureParams*> _params;
300
301 static const Arguments EMPTY_ARGUMENTS;
302};
303
306class MX_GENSHADER_API ScopedSetClosureParams
307{
308 public:
311
313 ScopedSetClosureParams(const ShaderNode* fromNode, const ShaderNode* toNode, ClosureContext* cct);
314
317
318 private:
319 ClosureContext* _cct;
320 const ShaderNode* _node;
321 const ClosureContext::ClosureParams* _oldParams;
322};
323
325class MX_GENSHADER_API ScopedSetVariableName
326{
327 public:
329 ScopedSetVariableName(const string& name, ShaderPort* port);
330
333
334 private:
335 ShaderPort* _port;
336 string _oldName;
337};
338
339MATERIALX_NAMESPACE_END
340
341#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:61
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
Classes for nodes created during shader generation.
Class representing a context for closure evaluation.
Definition: GenContext.h:229
void setClosureParams(const ShaderNode *closure, const ClosureParams *params)
Set extra parameters to use for evaluating a closure.
Definition: GenContext.h:275
const string & getSuffix(const TypeDesc *nodeType) const
Return the function name suffix to be used for the given node in this context.
Definition: GenContext.h:268
vector< Argument > Arguments
An array of arguments.
Definition: GenContext.h:236
ClosureContext(int type=0)
Constructor.
Definition: GenContext.h:242
void setSuffix(const 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:262
void addArgument(const 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:249
std::pair< const TypeDesc *, string > Argument
An extra argument for closure functions.
Definition: GenContext.h:234
std::unordered_map< string, const ShaderInput * > ClosureParams
Extra parameters for closure evaluation.
Definition: GenContext.h:239
const Arguments & getArguments(const TypeDesc *nodeType) const
Return a list of extra argument to be used for the given node in this context.
Definition: GenContext.h:255
int getType() const
Return the identifier for this context.
Definition: GenContext.h:246
const ClosureParams * getClosureParams(const ShaderNode *closure) const
Return extra parameters to use for evaluating a closure.
Definition: GenContext.h:289
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 ...
Definition: File.cpp:136
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
GenOptions & getOptions()
Return shader generation options.
Definition: GenContext.h:43
void setApplicationVariableHandler(ApplicationVariableHandler handler)
Set handler for application variables.
Definition: GenContext.h:194
ShaderGenerator & getShaderGenerator()
Return shader generatior.
Definition: GenContext.h:37
void popUserData(const string &name)
Remove user data from the context.
Definition: GenContext.h:144
void pushClosureContext(ClosureContext *cct)
Push a new closure context to use for closure evaluation.
Definition: GenContext.h:108
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:130
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:200
ClosureContext * getClosureContext()
Return the current closure context.
Definition: GenContext.h:123
const GenOptions & getOptions() const
Return shader generation options.
Definition: GenContext.h:49
std::shared_ptr< T > getUserData(const string &name)
Return user data with given name, or nullptr if no data is found.
Definition: GenContext.h:159
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:307
A RAII class for overriding port variable names.
Definition: GenContext.h:326
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:261
Class representing a node in the shader generation DAG.
Definition: ShaderNode.h:323
An output on a ShaderNode.
Definition: ShaderNode.h:298
An input or output port on a ShaderNode.
Definition: ShaderNode.h:123
A type descriptor for MaterialX data types.
Definition: TypeDesc.h:28