Skip to content

Commit

Permalink
Fix swizzle edge cases in version upgrade (#1957)
Browse files Browse the repository at this point in the history
This PR fixes 2 edge cases when upgrading materials from version 1.38 to 1.39 involving swizzle nodes:
- swizzle xyz -> xyz, rgb -> rgb wasn't handled properly, because convert nodedefs were missing for such cases
- swizzle xy -> xxx is incorrectly changed to convert node
  • Loading branch information
nadult authored Aug 8, 2024
1 parent 67d2e28 commit 3ec6e87
Showing 1 changed file with 16 additions and 10 deletions.
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

0 comments on commit 3ec6e87

Please sign in to comment.