forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitSystem.cpp
249 lines (211 loc) · 9.04 KB
/
UnitSystem.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXGenShader/UnitSystem.h>
#include <MaterialXGenShader/GenContext.h>
#include <MaterialXGenShader/ShaderGenerator.h>
#include <MaterialXGenShader/ShaderStage.h>
#include <MaterialXGenShader/Shader.h>
#include <MaterialXGenShader/Nodes/SourceCodeNode.h>
namespace MaterialX
{
class ScalarUnitNode : public ShaderNodeImpl
{
public:
explicit ScalarUnitNode(LinearUnitConverterPtr scalarUnitConverter) :
_scalarUnitConverter(scalarUnitConverter),
_unitRatioFunctionName("mx_" + _scalarUnitConverter->getUnitType() + "_unit_ratio")
{
}
static ShaderNodeImplPtr create(LinearUnitConverterPtr scalarUnitConverter);
void initialize(const InterfaceElement& element, GenContext& context) override;
void emitFunctionDefinition(const ShaderNode& node, GenContext& context, ShaderStage& stage) const override;
void emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const override;
protected:
LinearUnitConverterPtr _scalarUnitConverter;
const string _unitRatioFunctionName;
};
ShaderNodeImplPtr ScalarUnitNode::create(LinearUnitConverterPtr scalarUnitConverter)
{
return std::make_shared<ScalarUnitNode>(scalarUnitConverter);
}
void ScalarUnitNode::initialize(const InterfaceElement& element, GenContext& /*context*/)
{
_name = element.getName();
// Use the unit ratio function name has hash to make sure this function
// is shared, and only emitted once, for all units of the same unit type.
_hash = std::hash<string>{}(_unitRatioFunctionName);
}
void ScalarUnitNode::emitFunctionDefinition(const ShaderNode& /*node*/, GenContext& context, ShaderStage& stage) const
{
BEGIN_SHADER_STAGE(stage, Stage::PIXEL)
// Emit the helper funtion mx_<unittype>_unit_ratio that embeds a look up table for unit scale
vector<float> unitScales;
unitScales.reserve(_scalarUnitConverter->getUnitScale().size());
auto unitScaleMap = _scalarUnitConverter->getUnitScale();
unitScales.resize(unitScaleMap.size());
for (auto unitScale : unitScaleMap)
{
int location = _scalarUnitConverter->getUnitAsInteger(unitScale.first);
unitScales[location] = unitScale.second;
}
// See stdlib/gen*/mx_<unittype>_unit. This helper function is called by these shaders.
const string VAR_UNIT_SCALE = "u_" + _scalarUnitConverter->getUnitType() + "_unit_scales";
VariableBlock unitLUT("unitLUT", EMPTY_STRING);
ScopedFloatFormatting fmt(Value::FloatFormatFixed, 15);
unitLUT.add(Type::FLOATARRAY, VAR_UNIT_SCALE, Value::createValue<vector<float>>(unitScales));
const ShaderGenerator& shadergen = context.getShaderGenerator();
shadergen.emitLine("float " + _unitRatioFunctionName + "(int unit_from, int unit_to)", stage, false);
shadergen.emitScopeBegin(stage);
shadergen.emitVariableDeclarations(unitLUT, shadergen.getSyntax().getConstantQualifier(), ";", context, stage, true);
shadergen.emitLine("return ("+ VAR_UNIT_SCALE + "[unit_from] / " + VAR_UNIT_SCALE + "[unit_to])", stage);
shadergen.emitScopeEnd(stage);
shadergen.emitLineBreak(stage);
END_SHADER_STAGE(shader, Stage::PIXEL)
}
void ScalarUnitNode::emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const
{
BEGIN_SHADER_STAGE(stage, Stage::PIXEL)
const ShaderGenerator& shadergen = context.getShaderGenerator();
const ShaderInput* in = node.getInput(0);
const ShaderInput* from = node.getInput(1);
const ShaderInput* to = node.getInput(2);
shadergen.emitLineBegin(stage);
shadergen.emitOutput(node.getOutput(), true, false, context, stage);
shadergen.emitString(" = ", stage);
shadergen.emitInput(in, context, stage);
shadergen.emitString(" * ", stage);
shadergen.emitString(_unitRatioFunctionName + "(", stage);
shadergen.emitInput(from, context, stage);
shadergen.emitString(", ", stage);
shadergen.emitInput(to, context, stage);
shadergen.emitString(")", stage);
shadergen.emitLineEnd(stage);
END_SHADER_STAGE(shader, Stage::PIXEL)
}
//
// Unit transform methods
//
UnitTransform::UnitTransform(const string& ss, const string& ts, const TypeDesc* t, const string& unittype) :
sourceUnit(ss),
targetUnit(ts),
type(t),
unitType(unittype)
{
if (type != Type::FLOAT && type != Type::VECTOR2 && type != Type::VECTOR3 && type != Type::VECTOR4)
{
throw ExceptionShaderGenError("Unit space transform can only be a float or vectors");
}
}
const string UnitSystem::UNITSYTEM_NAME = "default_unit_system";
UnitSystem::UnitSystem(const string& language)
{
_language = createValidName(language);
}
void UnitSystem::loadLibrary(DocumentPtr document)
{
_document = document;
}
void UnitSystem::setUnitConverterRegistry(UnitConverterRegistryPtr registry)
{
_unitRegistry = registry;
}
UnitConverterRegistryPtr UnitSystem::getUnitConverterRegistry() const
{
return _unitRegistry;
}
UnitSystemPtr UnitSystem::create(const string& language)
{
return UnitSystemPtr(new UnitSystem(language));
}
string UnitSystem::getImplementationName(const UnitTransform& transform, const string& unitname) const
{
return "IM_" + unitname + "_unit_" + transform.type->getName() + "_" + _language;
}
bool UnitSystem::supportsTransform(const UnitTransform& transform) const
{
const string implName = getImplementationName(transform, transform.unitType);
ImplementationPtr impl = _document->getImplementation(implName);
return impl != nullptr;
}
ShaderNodePtr UnitSystem::createNode(ShaderGraph* parent, const UnitTransform& transform, const string& name,
GenContext& context) const
{
const string implName = getImplementationName(transform, transform.unitType);
ImplementationPtr impl = _document->getImplementation(implName);
if (!impl)
{
throw ExceptionShaderGenError("No implementation found for transform: ('" + transform.sourceUnit + "', '" + transform.targetUnit + "').");
}
// Scalar unit conversion
UnitTypeDefPtr scalarTypeDef = _document->getUnitTypeDef(transform.unitType);
if (!_unitRegistry || !_unitRegistry->getUnitConverter(scalarTypeDef))
{
throw ExceptionTypeError("Unit registry unavaliable or undefined unit converter for: " + transform.unitType);
}
LinearUnitConverterPtr scalarConverter = std::dynamic_pointer_cast<LinearUnitConverter>(_unitRegistry->getUnitConverter(scalarTypeDef));
// Check if it's created and cached already,
// otherwise create and cache it.
ShaderNodeImplPtr nodeImpl = context.findNodeImplementation(implName);
if (!nodeImpl)
{
nodeImpl = ScalarUnitNode::create(scalarConverter);
nodeImpl->initialize(*impl, context);
context.addNodeImplementation(implName, nodeImpl);
}
// Create the node.
ShaderNodePtr shaderNode = ShaderNode::create(parent, name, nodeImpl, ShaderNode::Classification::TEXTURE);
// Create ports on the node.
ShaderInput* input = shaderNode->addInput("in", transform.type);
if (transform.type == Type::FLOAT)
{
input->setValue(Value::createValue(1.0));
}
else if (transform.type == Type::VECTOR2)
{
input->setValue(Value::createValue(Vector2(1.0f, 1.0)));
}
else if (transform.type == Type::VECTOR3)
{
input->setValue(Value::createValue(Vector3(1.0f, 1.0, 1.0)));
}
else if (transform.type == Type::VECTOR4)
{
input->setValue(Value::createValue(Vector4(1.0f, 1.0, 1.0, 1.0)));
}
else
{
throw ExceptionShaderGenError("Invalid type specified to unitTransform: '" + transform.type->getName() + "'");
}
// Add the conversion code
{
int value = scalarConverter->getUnitAsInteger(transform.sourceUnit);
if (value < 0)
{
throw ExceptionTypeError("Unrecognized source unit: " + transform.sourceUnit);
}
ShaderInput* convertFrom = shaderNode->addInput("unit_from", Type::INTEGER);
convertFrom->setValue(Value::createValue(value));
}
{
int value = scalarConverter->getUnitAsInteger(transform.targetUnit);
if (value < 0)
{
throw ExceptionTypeError("Unrecognized target unit: " + transform.targetUnit);
}
ShaderInput* convertTo = shaderNode->addInput("unit_to", Type::INTEGER);
// Create a graph input to connect to the "unit_to" if it does not already exist.
const string UNIT_TARGET_NAME = "u_" + transform.unitType + "UnitTarget";
ShaderGraphInputSocket* globalInput = parent->getInputSocket(UNIT_TARGET_NAME);
if (!globalInput)
{
globalInput = parent->addInputSocket(UNIT_TARGET_NAME, Type::INTEGER);
}
globalInput->setValue(Value::createValue(value));
convertTo->makeConnection(globalInput);
}
shaderNode->addOutput("out", transform.type);
return shaderNode;
}
} // namespace MaterialX