HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
9 /// @file
10 /// Context classes for shader generation
11 
13 
18 
19 #include <MaterialXFormat/File.h>
20 
22 
23 /// A standard function to allow for handling of application variables for a given node
24 using ApplicationVariableHandler = std::function<void(ShaderNode*, GenContext&)>;
25 
26 /// @class GenContext
27 /// A context class for shader generation.
28 /// Used for thread local storage of data needed during shader generation.
30 {
31  public:
32  /// Constructor.
34 
35  /// Return shader generatior.
37  {
38  return *_sg;
39  }
40 
41  /// Return shader generation options.
43  {
44  return _options;
45  }
46 
47  /// Return shader generation options.
48  const GenOptions& getOptions() const
49  {
50  return _options;
51  }
52 
53  /// Return a TypeDesc for the given type name.
54  TypeDesc getTypeDesc(const string& name) const
55  {
56  return _sg->getTypeSystem()->getType(name);
57  }
58 
59  /// Register a user search path for finding source code during
60  /// code generation.
62  {
63  _sourceCodeSearchPath.append(path);
64  }
65 
66  /// Register a user search path for finding source code during
67  /// code generation.
69  {
70  _sourceCodeSearchPath.append(path);
71  }
72 
73  /// Resolve a source code filename, first checking the given local path
74  /// then checking any file paths registered by the user.
75  FilePath resolveSourceFile(const FilePath& filename, const FilePath& localPath) const
76  {
77  FileSearchPath searchPath = _sourceCodeSearchPath;
78  if (!localPath.isEmpty())
79  {
80  searchPath.prepend(localPath);
81  }
82  return searchPath.find(filename).getNormalized();
83  }
84 
85  /// Add reserved words that should not be used as
86  /// identifiers during code generation.
87  void addReservedWords(const StringSet& names)
88  {
89  _reservedWords.insert(names.begin(), names.end());
90  }
91 
92  /// Return the set of reserved words that should not be used
93  /// as identifiers during code generation.
94  const StringSet& getReservedWords() const
95  {
96  return _reservedWords;
97  }
98 
99  /// Cache a shader node implementation.
100  void addNodeImplementation(const string& name, ShaderNodeImplPtr impl);
101 
102  /// Find and return a cached shader node implementation,
103  /// or return nullptr if no implementation is found.
104  ShaderNodeImplPtr findNodeImplementation(const string& name) const;
105 
106  /// Get the names of all cached node implementations.
107  void getNodeImplementationNames(StringSet& names);
108 
109  /// Clear all cached shader node implementation.
110  void clearNodeImplementations();
111 
112  /// Push a parent node onto the stack
114  {
115  _parentNodes.push_back(node);
116  }
117 
118  /// Pop the current parent node from the stack.
120  {
121  _parentNodes.pop_back();
122  }
123 
124  /// Return the current stack of parent nodes.
125  const vector<ConstNodePtr>& getParentNodes()
126  {
127  return _parentNodes;
128  }
129 
130  /// Add user data to the context to make it
131  /// available during shader generator.
132  void pushUserData(const string& name, GenUserDataPtr data)
133  {
134  auto it = _userData.find(name);
135  if (it != _userData.end())
136  {
137  it->second.push_back(data);
138  }
139  else
140  {
141  _userData[name] = { data };
142  }
143  }
144 
145  /// Remove user data from the context.
146  void popUserData(const string& name)
147  {
148  auto it = _userData.find(name);
149  if (it != _userData.end())
150  {
151  it->second.pop_back();
152  }
153  }
154 
155  /// Clear all user data from the context.
156  void clearUserData();
157 
158  /// Return user data with given name,
159  /// or nullptr if no data is found.
160  template <class T>
161  std::shared_ptr<T> getUserData(const string& name)
162  {
163  auto it = _userData.find(name);
164  return it != _userData.end() && !it->second.empty() ? it->second.back()->asA<T>() : nullptr;
165  }
166 
167  /// Add an input suffix to be used for the input in this context.
168  /// @param input Node input
169  /// @param suffix Suffix string
170  void addInputSuffix(const ShaderInput* input, const string& suffix);
171 
172  /// Remove an input suffix to be used for the input in this context.
173  /// @param input Node input
174  void removeInputSuffix(const ShaderInput* input);
175 
176  /// Get an input suffix to be used for the input in this context.
177  /// @param input Node input
178  /// @param suffix Suffix string returned. Is empty if not found.
179  void getInputSuffix(const ShaderInput* input, string& suffix) const;
180 
181  /// Add an output suffix to be used for the output in this context.
182  /// @param output Node output
183  /// @param suffix Suffix string
184  void addOutputSuffix(const ShaderOutput* output, const string& suffix);
185 
186  /// Remove an output suffix to be used for the output in this context.
187  /// @param output Node output
188  void removeOutputSuffix(const ShaderOutput* output);
189 
190  /// Get an output suffix to be used for the output in this context.
191  /// @param output Node output
192  /// @param suffix Suffix string returned. Is empty if not found.
193  void getOutputSuffix(const ShaderOutput* output, string& suffix) const;
194 
195  /// Set handler for application variables
197  {
198  _applicationVariableHandler = handler;
199  }
200 
201  /// Get handler for application variables
203  {
204  return _applicationVariableHandler;
205  }
206 
207  protected:
208  GenContext() = delete;
209 
214 
215  std::unordered_map<string, ShaderNodeImplPtr> _nodeImpls;
216  std::unordered_map<string, vector<GenUserDataPtr>> _userData;
217  std::unordered_map<const ShaderInput*, string> _inputSuffix;
218  std::unordered_map<const ShaderOutput*, string> _outputSuffix;
219 
220  vector<ConstNodePtr> _parentNodes;
221 
223 };
224 
225 /// A RAII class for overriding port variable names.
227 {
228  public:
229  /// Constructor for setting a new variable name for a port.
230  ScopedSetVariableName(const string& name, ShaderPort* port);
231 
232  /// Destructor restoring the original variable name.
234 
235  private:
236  ShaderPort* _port;
237  string _oldName;
238 };
239 
241 
242 #endif // MATERIALX_GENCONTEXT_H
FileSearchPath _sourceCodeSearchPath
Definition: GenContext.h:212
vector< ConstNodePtr > _parentNodes
Definition: GenContext.h:220
GT_API const UT_StringHolder filename
FilePath getNormalized() const
shared_ptr< ShaderNodeImpl > ShaderNodeImplPtr
Shared pointer to a ShaderNodeImpl.
Definition: Library.h:39
Definition: File.h:26
const vector< ConstNodePtr > & getParentNodes()
Return the current stack of parent nodes.
Definition: GenContext.h:125
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
StringSet _reservedWords
Definition: GenContext.h:213
A RAII class for overriding port variable names.
Definition: GenContext.h:226
const StringSet & getReservedWords() const
Definition: GenContext.h:94
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
void addReservedWords(const StringSet &names)
Definition: GenContext.h:87
shared_ptr< const Node > ConstNodePtr
A shared pointer to a const Node.
Definition: Node.h:26
std::unordered_map< const ShaderInput *, string > _inputSuffix
Definition: GenContext.h:217
std::function< void(ShaderNode *, GenContext &)> ApplicationVariableHandler
A standard function to allow for handling of application variables for a given node.
Definition: GenContext.h:24
#define MX_GENSHADER_API
Definition: Export.h:18
void popUserData(const string &name)
Remove user data from the context.
Definition: GenContext.h:146
const GenOptions & getOptions() const
Return shader generation options.
Definition: GenContext.h:48
bool isEmpty() const
Return true if the given path is empty.
Definition: File.h:92
ShaderGenerator & getShaderGenerator()
Return shader generatior.
Definition: GenContext.h:36
ShaderGeneratorPtr _sg
Definition: GenContext.h:210
std::unordered_map< string, ShaderNodeImplPtr > _nodeImpls
Definition: GenContext.h:215
ApplicationVariableHandler getApplicationVariableHandler() const
Get handler for application variables.
Definition: GenContext.h:202
std::shared_ptr< GenUserData > GenUserDataPtr
Shared pointer to a GenUserData.
Definition: GenUserData.h:19
TypeDesc getTypeDesc(const string &name) const
Return a TypeDesc for the given type name.
Definition: GenContext.h:54
ApplicationVariableHandler _applicationVariableHandler
Definition: GenContext.h:222
GLuint const GLchar * name
Definition: glcorearb.h:786
std::unordered_map< string, vector< GenUserDataPtr > > _userData
Definition: GenContext.h:216
std::shared_ptr< T > getUserData(const string &name)
Definition: GenContext.h:161
void registerSourceCodeSearchPath(const FileSearchPath &path)
Definition: GenContext.h:68
void popParentNode()
Pop the current parent node from the stack.
Definition: GenContext.h:119
void pushUserData(const string &name, GenUserDataPtr data)
Definition: GenContext.h:132
void setApplicationVariableHandler(ApplicationVariableHandler handler)
Set handler for application variables.
Definition: GenContext.h:196
void prepend(const FilePath &path)
Prepend the given path to the sequence.
Definition: File.h:274
GenOptions & getOptions()
Return shader generation options.
Definition: GenContext.h:42
FilePath resolveSourceFile(const FilePath &filename, const FilePath &localPath) const
Definition: GenContext.h:75
std::unordered_map< const ShaderOutput *, string > _outputSuffix
Definition: GenContext.h:218
FilePath find(const FilePath &filename) const
Definition: File.h:313
std::set< string > StringSet
A set of strings.
Definition: Library.h:64
void pushParentNode(ConstNodePtr node)
Push a parent node onto the stack.
Definition: GenContext.h:113
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
shared_ptr< ShaderGenerator > ShaderGeneratorPtr
Shared pointer to a ShaderGenerator.
Definition: Library.h:37
GenOptions _options
Definition: GenContext.h:211
Definition: format.h:1821
void registerSourceCodeSearchPath(const FilePath &path)
Definition: GenContext.h:61