Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix swizzle edge cases in version upgrade #1957

Merged
26 changes: 16 additions & 10 deletions source/MaterialXCore/Version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,16 +992,19 @@ void Document::upgradeVersion()
{ "color3", 3 }, { "color4", 4 },
{ "vector2", 2 }, { "vector3", 3 }, { "vector4", 4 }
};
const StringSet CHANNEL_CONVERT_PATTERNS =
{
"rgb", "rgba", "xyz", "xyzw", "rrr", "xxx"
};
const vector<std::pair<StringSet, string>> CHANNEL_ATTRIBUTE_PATTERNS =
{
const std::array<std::pair<string, size_t>, 10> CHANNEL_CONVERT_PATTERNS =
{ {
{ "rgb", 3 }, { "rgb", 4 }, { "rgba", 4 },
{ "xyz", 3 }, { "xyz", 4 }, { "xyzw", 4 },
{ "rr", 1 }, { "rrr", 1 },
{ "xx", 1 }, { "xxx", 1 }
} };
const std::array<std::pair<StringSet, string>, 3> CHANNEL_ATTRIBUTE_PATTERNS =
{ {
{ { "xx", "xxx", "xxxx" }, "float" },
{ { "xyz", "x", "y", "z" }, "vector3" },
{ { "rgba", "a" }, "color4" }
};
} };

// Convert channels attributes to legacy swizzle nodes, which are then converted
// to modern nodes in a second pass.
Expand Down Expand Up @@ -1169,8 +1172,10 @@ void Document::upgradeVersion()
CHANNEL_COUNT_MAP.count(node->getType()))
{
string channelString = channelsInput ? channelsInput->getValueString() : EMPTY_STRING;
size_t sourceChannelCount = CHANNEL_COUNT_MAP.at(inInput->getType());
size_t destChannelCount = CHANNEL_COUNT_MAP.at(node->getType());
string sourceType = inInput->getType();
string destType = node->getType();
size_t sourceChannelCount = CHANNEL_COUNT_MAP.at(sourceType);
size_t destChannelCount = CHANNEL_COUNT_MAP.at(destType);

// Resolve the invalid case of having both a connection and a value
// by removing the value attribute.
Expand Down Expand Up @@ -1228,7 +1233,8 @@ void Document::upgradeVersion()
node->setInputValue("index", (int) CHANNEL_INDEX_MAP.at(channelString[0]));
}
}
else if (CHANNEL_CONVERT_PATTERNS.count(channelString))
else if (sourceType != destType && std::find(CHANNEL_CONVERT_PATTERNS.begin(), CHANNEL_CONVERT_PATTERNS.end(),
std::make_pair(channelString, sourceChannelCount)) != CHANNEL_CONVERT_PATTERNS.end())
{
// Replace swizzle with convert.
node->setCategory("convert");
Expand Down