forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderTranslator.cpp
201 lines (177 loc) · 7.14 KB
/
ShaderTranslator.cpp
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
//
// TM & (c) 2020 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXGenShader/ShaderTranslator.h>
#include <MaterialXCore/Material.h>
MATERIALX_NAMESPACE_BEGIN
//
// ShaderTranslator methods
//
void ShaderTranslator::connectTranslationInputs(NodePtr shader, NodeDefPtr translationNodeDef)
{
vector<InputPtr> origInputs = shader->getInputs();
std::set<OutputPtr> origOutputs;
for (InputPtr shaderInput : origInputs)
{
if (translationNodeDef->getInput(shaderInput->getName()))
{
InputPtr input = _translationNode->addInput(shaderInput->getName(), shaderInput->getType());
OutputPtr connectedOutput = shaderInput->getConnectedOutput();
if (connectedOutput)
{
NodePtr connectedNode = connectedOutput->getConnectedNode();
// Nodes with world-space outputs are skipped, with translation being applied to
// the node directly upstream.
NodePtr worldSpaceNode = connectsToWorldSpaceNode(connectedOutput);
if (worldSpaceNode)
{
NodePtr upstreamNode = worldSpaceNode->getConnectedNode("in");
if (upstreamNode)
{
connectedNode = upstreamNode;
}
}
input->setConnectedNode(connectedNode);
origOutputs.insert(connectedOutput);
}
else if (shaderInput->hasValueString())
{
input->setValueString(shaderInput->getValueString());
}
else
{
throw Exception("Shader input has no associated output or value " + shaderInput->getName());
}
if (shaderInput->hasColorSpace())
{
input->setColorSpace(shaderInput->getColorSpace());
}
if (shaderInput->hasUnit())
{
input->setUnit(shaderInput->getUnit());
input->setUnitType(shaderInput->getUnitType());
}
}
}
for (InputPtr input : origInputs)
{
shader->removeInput(input->getName());
}
for (OutputPtr output : origOutputs)
{
_graph->removeOutput(output->getName());
}
}
void ShaderTranslator::connectTranslationOutputs(NodePtr shader)
{
DocumentPtr doc = shader->getDocument();
InterfaceElementPtr implement = _translationNode->getImplementation();
NodeGraphPtr translationGraph = implement ? implement->asA<NodeGraph>() : nullptr;
if (!translationGraph)
{
throw Exception("No graph implementation for " + _translationNode->getCategory() + " was found");
}
// Iterate through outputs of the translation graph.
for (OutputPtr translationGraphOutput : translationGraph->getOutputs())
{
// Convert output name to input name, using a hardcoded naming convention for now.
string outputName = translationGraphOutput->getName();
size_t pos = outputName.find("_out");
if (pos == string::npos)
{
throw Exception("Translation graph output " + outputName + " does not end with '_out'");
}
string inputName = outputName.substr(0, pos);
// Determine the node and output representing this translated stream.
NodePtr translatedStreamNode = _translationNode;
string translatedStreamOutput = outputName;
// Nodes with world-space outputs are moved outside of their containing graph,
// providing greater flexibility in texture baking.
NodePtr worldSpaceNode = connectsToWorldSpaceNode(translationGraphOutput);
if (worldSpaceNode)
{
InputPtr nodeInput = worldSpaceNode->getInput("in");
if (nodeInput && nodeInput->hasInterfaceName())
{
InputPtr interfaceInput = _translationNode->getInput(nodeInput->getInterfaceName());
NodePtr sourceNode = interfaceInput ? interfaceInput->getConnectedNode() : nullptr;
if (!sourceNode)
{
continue;
}
translatedStreamNode = _graph->addNode(worldSpaceNode->getCategory(), worldSpaceNode->getName(), worldSpaceNode->getType());
translatedStreamNode->setConnectedNode("in", sourceNode);
translatedStreamOutput = EMPTY_STRING;
}
}
// Create translated output.
OutputPtr translatedOutput = _graph->getOutput(outputName);
if (!translatedOutput)
{
translatedOutput = _graph->addOutput(outputName, translationGraphOutput->getType());
}
translatedOutput->setConnectedNode(translatedStreamNode);
if (!translatedStreamOutput.empty())
{
translatedOutput->setOutputString(translatedStreamOutput);
}
// Add translated shader input.
InputPtr translatedShaderInput = shader->getInput(inputName);
if (!translatedShaderInput)
{
translatedShaderInput = shader->addInput(inputName, translationGraphOutput->getType());
}
translatedShaderInput->setConnectedOutput(translatedOutput);
}
}
void ShaderTranslator::translateShader(NodePtr shader, const string& destCategory)
{
_graph = nullptr;
_translationNode = nullptr;
if (!shader)
{
return;
}
const string& sourceCategory = shader->getCategory();
if (sourceCategory == destCategory)
{
return;
}
DocumentPtr doc = shader->getDocument();
vector<OutputPtr> referencedOutputs = getConnectedOutputs(shader);
ElementPtr referencedParent = !referencedOutputs.empty() ? referencedOutputs[0]->getParent() : nullptr;
NodeGraphPtr referencedGraph = referencedParent ? referencedParent->asA<NodeGraph>() : nullptr;
_graph = referencedGraph ? referencedGraph : doc->addNodeGraph();
string translateNodeString = sourceCategory + "_to_" + destCategory;
vector<NodeDefPtr> matchingNodeDefs = doc->getMatchingNodeDefs(translateNodeString);
if (matchingNodeDefs.empty())
{
throw Exception("Shader translation requires a translation nodedef named " + translateNodeString);
}
NodeDefPtr translationNodeDef = matchingNodeDefs[0];
_translationNode = _graph->addNodeInstance(translationNodeDef);
connectTranslationInputs(shader, translationNodeDef);
shader->setCategory(destCategory);
shader->removeAttribute(InterfaceElement::NODE_DEF_ATTRIBUTE);
connectTranslationOutputs(shader);
}
void ShaderTranslator::translateAllMaterials(DocumentPtr doc, const string& destCategory)
{
vector<TypedElementPtr> materialNodes;
std::unordered_set<ElementPtr> shaderOutputs;
findRenderableMaterialNodes(doc, materialNodes, false, shaderOutputs);
for (auto elem : materialNodes)
{
NodePtr materialNode = elem->asA<Node>();
if (!materialNode)
{
continue;
}
for (NodePtr shaderNode : getShaderNodes(materialNode))
{
translateShader(shaderNode, destCategory);
}
}
}
MATERIALX_NAMESPACE_END