MaterialX 1.38.2
GenContext.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_GENCONTEXT_H
7#define MATERIALX_GENCONTEXT_H
8
11
13
17
19
20namespace MaterialX
21{
22
26class MX_GENSHADER_API GenContext
27{
28 public:
31
34 {
35 return *_sg;
36 }
37
40 {
41 return _options;
42 }
43
45 const GenOptions& getOptions() const
46 {
47 return _options;
48 }
49
52 {
53 _sourceCodeSearchPath.append(path);
54 }
55
58 {
59 _sourceCodeSearchPath.append(path);
60 }
61
63 FilePath resolveSourceFile(const FilePath& filename) const
64 {
65 return _sourceCodeSearchPath.find(filename);
66 }
67
70 void addReservedWords(const StringSet& names)
71 {
72 _reservedWords.insert(names.begin(), names.end());
73 }
74
78 {
79 return _reservedWords;
80 }
81
83 void addNodeImplementation(const string& name, ShaderNodeImplPtr impl);
84
87 ShaderNodeImplPtr findNodeImplementation(const string& name) const;
88
90 void getNodeImplementationNames(StringSet& names);
91
93 void clearNodeImplementations();
94
97 void pushUserData(const string& name, GenUserDataPtr data)
98 {
99 auto it = _userData.find(name);
100 if (it != _userData.end())
101 {
102 it->second.push_back(data);
103 }
104 else
105 {
106 _userData[name] = { data };
107 }
108 }
109
111 void popUserData(const string& name)
112 {
113 auto it = _userData.find(name);
114 if (it != _userData.end())
115 {
116 it->second.pop_back();
117 }
118 }
119
121 void clearUserData();
122
125 template<class T>
126 std::shared_ptr<T> getUserData(const string& name)
127 {
128 auto it = _userData.find(name);
129 return it != _userData.end() && !it->second.empty() ? it->second.back()->asA<T>() : nullptr;
130 }
131
135 void addInputSuffix(const ShaderInput* input, const string& suffix);
136
139 void removeInputSuffix(const ShaderInput* input);
140
144 void getInputSuffix(const ShaderInput* input, string& suffix) const;
145
149 void addOutputSuffix(const ShaderOutput* output, const string& suffix);
150
153 void removeOutputSuffix(const ShaderOutput* output);
154
158 void getOutputSuffix(const ShaderOutput* output, string& suffix) const;
159
160 protected:
161 GenContext() = delete;
162
163 // Shader generator.
165
166 // Generation options.
167 GenOptions _options;
168
169 // Search path for finding source files.
170 FileSearchPath _sourceCodeSearchPath;
171
172 // Set of globally reserved words.
173 StringSet _reservedWords;
174
175 // Cached shader node implementations.
176 std::unordered_map<string, ShaderNodeImplPtr> _nodeImpls;
177
178 // User data
179 std::unordered_map<string, vector<GenUserDataPtr>> _userData;
180
181 // List of input suffixes
182 std::unordered_map<const ShaderInput*, string> _inputSuffix;
183
184 // List of output suffixes
185 std::unordered_map<const ShaderOutput*, string> _outputSuffix;
186};
187
188} // namespace MaterialX
189
190#endif // MATERIALX_GENCONTEXT_H
Cross-platform support for file and search paths.
Shader generation options class.
User data base class for shader generation.
std::shared_ptr< GenUserData > GenUserDataPtr
Shared pointer to a GenUserData.
Definition: GenUserData.h:20
std::set< string > StringSet
A set of strings.
Definition: Library.h:60
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.
A generic file path, supporting both syntactic and file system operations.
Definition: File.h:28
A sequence of file paths, which may be queried to find the first instance of a given filename on the ...
Definition: File.h:213
A context class for shader generation.
Definition: GenContext.h:27
void registerSourceCodeSearchPath(const FilePath &path)
Add to the search path used for finding source code.
Definition: GenContext.h:51
GenOptions & getOptions()
Return shader generation options.
Definition: GenContext.h:39
ShaderGenerator & getShaderGenerator()
Return shader generatior.
Definition: GenContext.h:33
void popUserData(const string &name)
Remove user data from the context.
Definition: GenContext.h:111
void addReservedWords(const StringSet &names)
Add reserved words that should not be used as identifiers during code generation.
Definition: GenContext.h:70
void pushUserData(const string &name, GenUserDataPtr data)
Add user data to the context to make it available during shader generator.
Definition: GenContext.h:97
FilePath resolveSourceFile(const FilePath &filename) const
Resolve a file using the registered search paths.
Definition: GenContext.h:63
void registerSourceCodeSearchPath(const FileSearchPath &path)
Add to the search path used for finding source code.
Definition: GenContext.h:57
const GenOptions & getOptions() const
Return shader generation options.
Definition: GenContext.h:45
std::shared_ptr< T > getUserData(const string &name)
Return user data with given name, or nullptr if no data is found.
Definition: GenContext.h:126
const StringSet & getReservedWords() const
Return the set of reserved words that should not be used as identifiers during code generation.
Definition: GenContext.h:77
Class holding options to configure shader generation.
Definition: GenOptions.h:65
Base class for shader generators All third-party shader generators should derive from this class.
Definition: ShaderGenerator.h:30
An input on a ShaderNode.
Definition: ShaderNode.h:256
An output on a ShaderNode.
Definition: ShaderNode.h:289