-
Notifications
You must be signed in to change notification settings - Fork 422
Expand file tree
/
Copy pathGenContext.cpp
More file actions
128 lines (104 loc) · 2.85 KB
/
GenContext.cpp
File metadata and controls
128 lines (104 loc) · 2.85 KB
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
//
// Copyright Contributors to the MaterialX Project
// SPDX-License-Identifier: Apache-2.0
//
#include <MaterialXGenShader/Exception.h>
#include <MaterialXGenShader/GenContext.h>
MATERIALX_NAMESPACE_BEGIN
//
// GenContext methods
//
GenContext::GenContext(ShaderGeneratorPtr sg) :
_sg(sg)
{
if (!_sg)
{
throw ExceptionShaderGenError("GenContext must have a valid shader generator");
}
// Apply the generator's default options for its target.
_sg->applyDefaultOptions(_options);
// Collect and cache reserved words from the shader generator
StringSet reservedWords;
// Add reserved words from the syntax
reservedWords = _sg->getSyntax().getReservedWords();
// Add token substitution identifiers
for (const auto& it : _sg->getTokenSubstitutions())
{
if (!it.second.empty())
{
reservedWords.insert(it.second);
}
}
addReservedWords(reservedWords);
_applicationVariableHandler = nullptr;
}
void GenContext::addNodeImplementation(const string& name, ShaderNodeImplPtr impl)
{
_nodeImpls[name] = impl;
}
ShaderNodeImplPtr GenContext::findNodeImplementation(const string& name) const
{
auto it = _nodeImpls.find(name);
return it != _nodeImpls.end() ? it->second : nullptr;
}
void GenContext::getNodeImplementationNames(StringSet& names)
{
for (const auto& it : _nodeImpls)
{
names.insert(it.first);
}
}
void GenContext::clearNodeImplementations()
{
_nodeImpls.clear();
_nodeGraphTopologyCache.clear();
}
void GenContext::clearUserData()
{
_userData.clear();
}
void GenContext::addInputSuffix(const ShaderInput* input, const string& suffix)
{
_inputSuffix[input] = suffix;
}
void GenContext::removeInputSuffix(const ShaderInput* input)
{
_inputSuffix.erase(input);
}
void GenContext::getInputSuffix(const ShaderInput* input, string& suffix) const
{
suffix.clear();
std::unordered_map<const ShaderInput*, string>::const_iterator iter = _inputSuffix.find(input);
if (iter != _inputSuffix.end())
{
suffix = iter->second;
}
}
void GenContext::addOutputSuffix(const ShaderOutput* output, const string& suffix)
{
_outputSuffix[output] = suffix;
}
void GenContext::removeOutputSuffix(const ShaderOutput* output)
{
_outputSuffix.erase(output);
}
void GenContext::getOutputSuffix(const ShaderOutput* output, string& suffix) const
{
suffix.clear();
std::unordered_map<const ShaderOutput*, string>::const_iterator iter = _outputSuffix.find(output);
if (iter != _outputSuffix.end())
{
suffix = iter->second;
}
}
ScopedSetVariableName::ScopedSetVariableName(const string& name, ShaderPort* port) :
_port(port),
_oldName(port->getVariable())
{
_port->setVariable(name);
}
ScopedSetVariableName::~ScopedSetVariableName()
{
_port->setVariable(_oldName);
}
MATERIALX_NAMESPACE_END