forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenContext.h
227 lines (180 loc) · 6.27 KB
/
GenContext.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#ifndef MATERIALX_GENCONTEXT_H
#define MATERIALX_GENCONTEXT_H
/// @file
/// Context classes for shader generation
#include <MaterialXGenShader/Library.h>
#include <MaterialXGenShader/GenOptions.h>
#include <MaterialXGenShader/ShaderNode.h>
#include <MaterialXFormat/File.h>
namespace MaterialX
{
class GenUserData;
/// Shared pointer to a GenUserData
using GenUserDataPtr = std::shared_ptr<GenUserData>;
/// Shared pointer to a constant GenUserData
using ConstGenUserDataPtr = std::shared_ptr<const GenUserData>;
/// @class GenUserData
/// Base class for custom user data needed during shader generation.
class GenUserData : public std::enable_shared_from_this<GenUserData>
{
public:
virtual ~GenUserData() { }
/// Return a shared pointer for this object.
GenUserDataPtr getSelf()
{
return shared_from_this();
}
/// Return a shared pointer for this object.
ConstGenUserDataPtr getSelf() const
{
return shared_from_this();
}
/// Return this object cast to a templated type.
template<class T> shared_ptr<T> asA()
{
return std::dynamic_pointer_cast<T>(getSelf());
}
/// Return this object cast to a templated type.
template<class T> shared_ptr<const T> asA() const
{
return std::dynamic_pointer_cast<const T>(getSelf());
}
protected:
GenUserData() { }
};
/// @class GenContext
/// A context class for shader generation.
/// Used for thread local storage of data needed during shader generation.
class GenContext
{
public:
/// Constructor.
GenContext(ShaderGeneratorPtr sg);
/// Return shader generatior.
ShaderGenerator& getShaderGenerator()
{
return *_sg;
}
/// Return shader generation options.
GenOptions& getOptions()
{
return _options;
}
/// Return shader generation options.
const GenOptions& getOptions() const
{
return _options;
}
/// Add to the search path used for finding source code.
void registerSourceCodeSearchPath(const FilePath& path)
{
_sourceCodeSearchPath.append(path);
}
/// Add to the search path used for finding source code.
void registerSourceCodeSearchPath(const FileSearchPath& path)
{
_sourceCodeSearchPath.append(path);
}
/// Resolve a file using the registered search paths.
FilePath resolveSourceFile(const FilePath& filename) const
{
return _sourceCodeSearchPath.find(filename);
}
/// Add reserved words that should not be used as
/// identifiers during code generation.
void addReservedWords(const StringSet& names)
{
_reservedWords.insert(names.begin(), names.end());
}
/// Return the set of reserved words that should not be used
/// as identifiers during code generation.
const StringSet& getReservedWords() const
{
return _reservedWords;
}
/// Cache a shader node implementation.
void addNodeImplementation(const string& name, ShaderNodeImplPtr impl);
/// Find and return a cached shader node implementation,
/// or return nullptr if no implementation is found.
ShaderNodeImplPtr findNodeImplementation(const string& name);
/// Clear all cached shader node implementation.
void clearNodeImplementations();
/// Add user data to the context to make it
/// available during shader generator.
void pushUserData(const string& name, GenUserDataPtr data)
{
auto it = _userData.find(name);
if (it != _userData.end())
{
it->second.push_back(data);
}
else
{
_userData[name] = { data };
}
}
/// Remove user data from the context.
void popUserData(const string& name)
{
auto it = _userData.find(name);
if (it != _userData.end())
{
it->second.pop_back();
}
}
/// Clear all user data from the context.
void clearUserData();
/// Return user data with given name,
/// or nullptr if no data is found.
template<class T>
std::shared_ptr<T> getUserData(const string& name)
{
auto it = _userData.find(name);
return it != _userData.end() && !it->second.empty() ? it->second.back()->asA<T>() : nullptr;
}
/// Add an input suffix to be used for the input in this context.
/// @param input Node input
/// @param suffix Suffix string
void addInputSuffix(const ShaderInput* input, const string& suffix);
/// Remove an input suffix to be used for the input in this context.
/// @param input Node input
void removeInputSuffix(const ShaderInput* input);
/// Get an input suffix to be used for the input in this context.
/// @param input Node input
/// @param suffix Suffix string returned. Is empty if not found.
void getInputSuffix(const ShaderInput* input, string& suffix) const;
/// Add an output suffix to be used for the output in this context.
/// @param output Node output
/// @param suffix Suffix string
void addOutputSuffix(const ShaderOutput* output, const string& suffix);
/// Remove an output suffix to be used for the output in this context.
/// @param output Node output
void removeOutputSuffix(const ShaderOutput* output);
/// Get an output suffix to be used for the output in this context.
/// @param output Node output
/// @param suffix Suffix string returned. Is empty if not found.
void getOutputSuffix(const ShaderOutput* output, string& suffix) const;
protected:
// Shader generator.
ShaderGeneratorPtr _sg;
// Generation options.
GenOptions _options;
// Search path for finding source files.
FileSearchPath _sourceCodeSearchPath;
// Set of globally reserved words.
StringSet _reservedWords;
// Cached shader node implementations.
std::unordered_map<string, ShaderNodeImplPtr> _nodeImpls;
// User data
std::unordered_map<string, vector<GenUserDataPtr>> _userData;
// List of input suffixes
std::unordered_map<const ShaderInput*, string> _inputSuffix;
// List of output suffixes
std::unordered_map<const ShaderOutput*, string> _outputSuffix;
};
} // namespace MaterialX
#endif // MATERIALX_GENCONTEXT_H