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 // 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 
9 /// @file
10 /// Context classes for shader generation
11 
13 
17 
18 #include <MaterialXFormat/File.h>
19 
21 
22 class ClosureContext;
23 
24 /// @class GenContext
25 /// A context class for shader generation.
26 /// Used for thread local storage of data needed during shader generation.
28 {
29  public:
30  /// Constructor.
32 
33  /// Return shader generatior.
35  {
36  return *_sg;
37  }
38 
39  /// Return shader generation options.
41  {
42  return _options;
43  }
44 
45  /// Return shader generation options.
46  const GenOptions& getOptions() const
47  {
48  return _options;
49  }
50 
51  /// Add to the search path used for finding source code.
53  {
54  _sourceCodeSearchPath.append(path);
55  }
56 
57  /// Add to the search path used for finding source code.
59  {
60  _sourceCodeSearchPath.append(path);
61  }
62 
63  /// Resolve a file using the registered search paths.
65  {
66  return _sourceCodeSearchPath.find(filename);
67  }
68 
69  /// Add reserved words that should not be used as
70  /// identifiers during code generation.
72  {
73  _reservedWords.insert(names.begin(), names.end());
74  }
75 
76  /// Return the set of reserved words that should not be used
77  /// as identifiers during code generation.
78  const StringSet& getReservedWords() const
79  {
80  return _reservedWords;
81  }
82 
83  /// Cache a shader node implementation.
84  void addNodeImplementation(const string& name, ShaderNodeImplPtr impl);
85 
86  /// Find and return a cached shader node implementation,
87  /// or return nullptr if no implementation is found.
88  ShaderNodeImplPtr findNodeImplementation(const string& name) const;
89 
90  /// Get the names of all cached node implementations.
91  void getNodeImplementationNames(StringSet& names);
92 
93  /// Clear all cached shader node implementation.
94  void clearNodeImplementations();
95 
96  /// Push a new closure context to use for closure evaluation.
98  {
99  _closureContexts.push_back(cct);
100  }
101 
102  /// Pop the current closure context.
104  {
105  if (_closureContexts.size())
106  {
107  _closureContexts.pop_back();
108  }
109  }
110 
111  /// Return the current closure context.
113  {
114  return _closureContexts.size() ? _closureContexts.back() : nullptr;
115  }
116 
117  /// Add user data to the context to make it
118  /// available during shader generator.
119  void pushUserData(const string& name, GenUserDataPtr data)
120  {
121  auto it = _userData.find(name);
122  if (it != _userData.end())
123  {
124  it->second.push_back(data);
125  }
126  else
127  {
128  _userData[name] = { data };
129  }
130  }
131 
132  /// Remove user data from the context.
133  void popUserData(const string& name)
134  {
135  auto it = _userData.find(name);
136  if (it != _userData.end())
137  {
138  it->second.pop_back();
139  }
140  }
141 
142  /// Clear all user data from the context.
143  void clearUserData();
144 
145  /// Return user data with given name,
146  /// or nullptr if no data is found.
147  template<class T>
148  std::shared_ptr<T> getUserData(const string& name)
149  {
150  auto it = _userData.find(name);
151  return it != _userData.end() && !it->second.empty() ? it->second.back()->asA<T>() : nullptr;
152  }
153 
154  /// Add an input suffix to be used for the input in this context.
155  /// @param input Node input
156  /// @param suffix Suffix string
157  void addInputSuffix(const ShaderInput* input, const string& suffix);
158 
159  /// Remove an input suffix to be used for the input in this context.
160  /// @param input Node input
161  void removeInputSuffix(const ShaderInput* input);
162 
163  /// Get an input suffix to be used for the input in this context.
164  /// @param input Node input
165  /// @param suffix Suffix string returned. Is empty if not found.
166  void getInputSuffix(const ShaderInput* input, string& suffix) const;
167 
168  /// Add an output suffix to be used for the output in this context.
169  /// @param output Node output
170  /// @param suffix Suffix string
171  void addOutputSuffix(const ShaderOutput* output, const string& suffix);
172 
173  /// Remove an output suffix to be used for the output in this context.
174  /// @param output Node output
175  void removeOutputSuffix(const ShaderOutput* output);
176 
177  /// Get an output suffix to be used for the output in this context.
178  /// @param output Node output
179  /// @param suffix Suffix string returned. Is empty if not found.
180  void getOutputSuffix(const ShaderOutput* output, string& suffix) const;
181 
182  protected:
183  GenContext() = delete;
184 
185  // Shader generator.
187 
188  // Generation options.
190 
191  // Search path for finding source files.
193 
194  // Set of globally reserved words.
196 
197  // Cached shader node implementations.
198  std::unordered_map<string, ShaderNodeImplPtr> _nodeImpls;
199 
200  // User data
201  std::unordered_map<string, vector<GenUserDataPtr>> _userData;
202 
203  // List of input suffixes
204  std::unordered_map<const ShaderInput*, string> _inputSuffix;
205 
206  // List of output suffixes
207  std::unordered_map<const ShaderOutput*, string> _outputSuffix;
208 
209  // Contexts for closure evaluation.
210  vector<ClosureContext*> _closureContexts;
211 };
212 
213 
214 /// @class ClosureContext
215 /// Class representing a context for closure evaluation.
216 /// On hardware BSDF closures are evaluated differently in reflection, transmission
217 /// or environment/indirect contexts. This class represents the context we are in
218 /// and if extra arguments and function decorators are needed for that context.
220 {
221 public:
222  /// An extra argument for closure functions.
223  /// An argument is a pair of strings holding the
224  /// 'type' and 'name' of the argument.
225  using Argument = std::pair<const TypeDesc*, string>;
226  /// An array of arguments
227  using Arguments = vector<Argument>;
228 
229  /// Extra parameters for closure evaluation.
230  using ClosureParams = std::unordered_map<string, const ShaderInput*>;
231 
232  /// Constructor
233  ClosureContext(int type = 0) : _type(type) {}
234 
235  /// Return the identifier for this context.
236  int getType() const { return _type; }
237 
238  /// For the given node type add an extra argument to be used for the function in this context.
239  void addArgument(const TypeDesc* nodeType, const Argument& arg)
240  {
241  _arguments[nodeType].push_back(arg);
242  }
243 
244  /// Return a list of extra argument to be used for the given node in this context.
245  const Arguments& getArguments(const TypeDesc* nodeType) const
246  {
247  auto it = _arguments.find(nodeType);
248  return it != _arguments.end() ? it->second : EMPTY_ARGUMENTS;
249  }
250 
251  /// For the given node type set a function name suffix to be used for the function in this context.
252  void setSuffix(const TypeDesc* nodeType, const string& suffix)
253  {
254  _suffix[nodeType] = suffix;
255  }
256 
257  /// Return the function name suffix to be used for the given node in this context.
258  const string& getSuffix(const TypeDesc* nodeType) const
259  {
260  auto it = _suffix.find(nodeType);
261  return it != _suffix.end() ? it->second : EMPTY_STRING;
262  }
263 
264  /// Set extra parameters to use for evaluating a closure.
265  void setClosureParams(const ShaderNode* closure, const ClosureParams* params)
266  {
267  if (params)
268  {
269  _params[closure] = params;
270  }
271  else
272  {
273  _params.erase(closure);
274  }
275  }
276 
277  /// Return extra parameters to use for evaluating a closure. Or return
278  /// nullptr if no parameters have been set for the given closure.
279  const ClosureParams* getClosureParams(const ShaderNode* closure) const
280  {
281  auto it = _params.find(closure);
282  return it != _params.end() ? it->second : nullptr;
283  }
284 
285 protected:
286  const int _type;
287  std::unordered_map<const TypeDesc*, Arguments> _arguments;
288  std::unordered_map<const TypeDesc*, string> _suffix;
289  std::unordered_map<const ShaderNode*, const ClosureParams*> _params;
290 
292 };
293 
294 /// A RAII class for setting extra parameters for closure evaluation,
295 /// stored in the closure context.
297 {
298 public:
299  /// Constructor for setting explicit parameters for a closure node.
301 
302  /// Constructor for setting parameters from one closure node to another.
303  ScopedSetClosureParams(const ShaderNode* fromNode, const ShaderNode* toNode, ClosureContext* cct);
304 
305  /// Destructor restoring the closure parameter state.
307 
308 private:
309  ClosureContext* _cct;
310  const ShaderNode* _node;
311  const ClosureContext::ClosureParams* _oldParams;
312 };
313 
314 /// A RAII class for overriding port variable names.
316 {
317 public:
318  /// Constructor for setting a new variable name for a port.
319  ScopedSetVariableName(const string& name, ShaderPort* port);
320 
321  /// Destructor restoring the original variable name.
323 
324 private:
325  ShaderPort* _port;
326  string _oldName;
327 };
328 
330 
331 #endif // MATERIALX_GENCONTEXT_H
FileSearchPath _sourceCodeSearchPath
Definition: GenContext.h:192
GT_API const UT_StringHolder filename
shared_ptr< ShaderNodeImpl > ShaderNodeImplPtr
Shared pointer to a ShaderNodeImpl.
Definition: Library.h:40
Definition: File.h:26
static const Arguments EMPTY_ARGUMENTS
Definition: GenContext.h:291
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:23
StringSet _reservedWords
Definition: GenContext.h:195
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:245
A RAII class for overriding port variable names.
Definition: GenContext.h:315
const StringSet & getReservedWords() const
Definition: GenContext.h:78
ClosureContext(int type=0)
Constructor.
Definition: GenContext.h:233
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
GLenum GLenum GLenum input
Definition: glew.h:14162
void addReservedWords(const StringSet &names)
Definition: GenContext.h:71
MATERIALX_NAMESPACE_BEGIN MX_CORE_API const string EMPTY_STRING
std::unordered_map< const ShaderInput *, string > _inputSuffix
Definition: GenContext.h:204
#define MX_GENSHADER_API
Definition: Export.h:18
GLuint const GLchar * name
Definition: glcorearb.h:786
void popUserData(const string &name)
Remove user data from the context.
Definition: GenContext.h:133
vector< ClosureContext * > _closureContexts
Definition: GenContext.h:210
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1736
const GenOptions & getOptions() const
Return shader generation options.
Definition: GenContext.h:46
ShaderGenerator & getShaderGenerator()
Return shader generatior.
Definition: GenContext.h:34
ShaderGeneratorPtr _sg
Definition: GenContext.h:186
int getType() const
Return the identifier for this context.
Definition: GenContext.h:236
std::unordered_map< string, ShaderNodeImplPtr > _nodeImpls
Definition: GenContext.h:198
FilePath resolveSourceFile(const FilePath &filename) const
Resolve a file using the registered search paths.
Definition: GenContext.h:64
std::shared_ptr< GenUserData > GenUserDataPtr
Shared pointer to a GenUserData.
Definition: GenUserData.h:19
std::pair< const TypeDesc *, string > Argument
Definition: GenContext.h:225
GLuint const GLuint * names
Definition: glew.h:2695
std::unordered_map< string, vector< GenUserDataPtr > > _userData
Definition: GenContext.h:201
std::shared_ptr< T > getUserData(const string &name)
Definition: GenContext.h:148
const ClosureParams * getClosureParams(const ShaderNode *closure) const
Definition: GenContext.h:279
std::unordered_map< string, const ShaderInput * > ClosureParams
Extra parameters for closure evaluation.
Definition: GenContext.h:230
void registerSourceCodeSearchPath(const FileSearchPath &path)
Add to the search path used for finding source code.
Definition: GenContext.h:58
void popClosureContext()
Pop the current closure context.
Definition: GenContext.h:103
void pushUserData(const string &name, GenUserDataPtr data)
Definition: GenContext.h:119
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:258
ClosureContext * getClosureContext()
Return the current closure context.
Definition: GenContext.h:112
GenOptions & getOptions()
Return shader generation options.
Definition: GenContext.h:40
const int _type
Definition: GenContext.h:286
vector< Argument > Arguments
An array of arguments.
Definition: GenContext.h:227
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:239
std::unordered_map< const TypeDesc *, string > _suffix
Definition: GenContext.h:288
std::unordered_map< const ShaderOutput *, string > _outputSuffix
Definition: GenContext.h:207
std::unordered_map< const ShaderNode *, const ClosureParams * > _params
Definition: GenContext.h:289
GLenum const GLfloat * params
Definition: glcorearb.h:105
std::set< string > StringSet
A set of strings.
Definition: Library.h:61
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:24
void pushClosureContext(ClosureContext *cct)
Push a new closure context to use for closure evaluation.
Definition: GenContext.h:97
type
Definition: core.h:1059
shared_ptr< ShaderGenerator > ShaderGeneratorPtr
Shared pointer to a ShaderGenerator.
Definition: Library.h:38
GenOptions _options
Definition: GenContext.h:189
void setClosureParams(const ShaderNode *closure, const ClosureParams *params)
Set extra parameters to use for evaluating a closure.
Definition: GenContext.h:265
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:252
Definition: format.h:895
std::unordered_map< const TypeDesc *, Arguments > _arguments
Definition: GenContext.h:287
void registerSourceCodeSearchPath(const FilePath &path)
Add to the search path used for finding source code.
Definition: GenContext.h:52