Skip to content

Commit

Permalink
Merge branch 'main' into feldstj/ramp_node
Browse files Browse the repository at this point in the history
  • Loading branch information
jstone-lucasfilm authored Dec 10, 2024
2 parents abe8efa + 31d8240 commit 92646bb
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 150 deletions.
68 changes: 38 additions & 30 deletions source/MaterialXCore/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ const string ValueElement::UI_ADVANCED_ATTRIBUTE = "uiadvanced";
const string ValueElement::UNIT_ATTRIBUTE = "unit";
const string ValueElement::UNITTYPE_ATTRIBUTE = "unittype";
const string ValueElement::UNIFORM_ATTRIBUTE = "uniform";
const string ElementEquivalenceResult::ATTRIBUTE = "attribute";
const string ElementEquivalenceResult::ATTRIBUTE_NAMES = "attribute names";
const string ElementEquivalenceResult::CHILD_COUNT = "child count";
const string ElementEquivalenceResult::CHILD_NAME = "child name";
const string ElementEquivalenceResult::NAME = "name";
const string ElementEquivalenceResult::CATEGORY = "category";

Element::CreatorMap Element::_creatorMap;

Expand Down Expand Up @@ -375,19 +369,22 @@ bool Element::hasInheritanceCycle() const
return false;
}

bool Element::isEquivalent(ConstElementPtr rhs, const ElementEquivalenceOptions& options,
ElementEquivalenceResultVec* results) const
bool Element::isEquivalent(ConstElementPtr rhs, const ElementEquivalenceOptions& options, string* message) const
{
if (getName() != rhs->getName())
{
if (results)
results->push_back(ElementEquivalenceResult(getNamePath(), rhs->getNamePath(), ElementEquivalenceResult::NAME));
if (message)
{
*message += "Name of " + getNamePath() + " differs from " + rhs->getNamePath() + "\n";
}
return false;
}
if (getCategory() != rhs->getCategory())
{
if (results)
results->push_back(ElementEquivalenceResult(getNamePath(), rhs->getNamePath(), ElementEquivalenceResult::CATEGORY));
if (message)
{
*message += "Category of " + getNamePath() + " differs from " + rhs->getNamePath() + "\n";
}
return false;
}

Expand All @@ -413,14 +410,16 @@ bool Element::isEquivalent(ConstElementPtr rhs, const ElementEquivalenceOptions&

if (attributeNames != rhsAttributeNames)
{
if (results)
results->push_back(ElementEquivalenceResult(getNamePath(), rhs->getNamePath(), ElementEquivalenceResult::ATTRIBUTE_NAMES));
if (message)
{
*message += "Attribute names of '" + getNamePath() + "' differ from '" + rhs->getNamePath() + "\n";
}
return false;
}

for (const string& attr : rhsAttributeNames)
{
if (!isAttributeEquivalent(rhs, attr, options, results))
if (!isAttributeEquivalent(rhs, attr, options, message))
{
return false;
}
Expand All @@ -447,8 +446,10 @@ bool Element::isEquivalent(ConstElementPtr rhs, const ElementEquivalenceOptions&
}
if (children.size() != rhsChildren.size())
{
if (results)
results->push_back(ElementEquivalenceResult(getNamePath(), rhs->getNamePath(), ElementEquivalenceResult::CHILD_COUNT));
if (message)
{
*message += "Child count of " + getNamePath() + " differs from " + rhs->getNamePath() + "\n";
}
return false;
}
for (size_t i = 0; i < children.size(); i++)
Expand All @@ -468,26 +469,29 @@ bool Element::isEquivalent(ConstElementPtr rhs, const ElementEquivalenceOptions&
rhsElement = rhs->getChild(childName);
if (!rhsElement)
{
if (results)
results->push_back(ElementEquivalenceResult(children[i]->getNamePath(), "<NONE>",
ElementEquivalenceResult::CHILD_NAME, childName));
if (message)
{
*message += "Child name `" + childName + "` of " + getNamePath() + " differs from " + rhs->getNamePath() + "\n";
}
return false;
}
}
}
if (!children[i]->isEquivalent(rhsElement, options, results))
if (!children[i]->isEquivalent(rhsElement, options, message))
return false;
}
return true;
}

bool Element::isAttributeEquivalent(ConstElementPtr rhs, const string& attributeName,
const ElementEquivalenceOptions& /*options*/, ElementEquivalenceResultVec* results) const
const ElementEquivalenceOptions& /*options*/, string* message) const
{
if (getAttribute(attributeName) != rhs->getAttribute(attributeName))
{
if (results)
results->push_back(ElementEquivalenceResult(getNamePath(), rhs->getNamePath(), ElementEquivalenceResult::ATTRIBUTE, attributeName));
if (message)
{
*message += "Attribute name `" + attributeName + "` of " + getNamePath() + " differs from " + rhs->getNamePath() + "\n";
}
return false;
}
return true;
Expand Down Expand Up @@ -710,7 +714,7 @@ const string& ValueElement::getActiveUnit() const
}

bool ValueElement::isAttributeEquivalent(ConstElementPtr rhs, const string& attributeName,
const ElementEquivalenceOptions& options, ElementEquivalenceResultVec* results) const
const ElementEquivalenceOptions& options, string* message) const
{
// Perform value comparisons
bool performedValueComparison = false;
Expand All @@ -737,8 +741,10 @@ bool ValueElement::isAttributeEquivalent(ConstElementPtr rhs, const string& attr
{
if (thisValue->getValueString() != rhsValue->getValueString())
{
if (results)
results->push_back(ElementEquivalenceResult(getNamePath(), rhs->getNamePath(), ElementEquivalenceResult::ATTRIBUTE, attributeName));
if (message)
{
*message += "Attribute `" + attributeName + "` of " + getNamePath() + " differs from " + rhs->getNamePath() + "\n";
}
return false;
}
}
Expand All @@ -756,8 +762,10 @@ bool ValueElement::isAttributeEquivalent(ConstElementPtr rhs, const string& attr
{
if (uiValue->getValueString() != rhsUiValue->getValueString())
{
if (results)
results->push_back(ElementEquivalenceResult(getNamePath(), rhs->getNamePath(), ElementEquivalenceResult::ATTRIBUTE, attributeName));
if (message)
{
*message += "Attribute `" + attributeName + "` of " + getNamePath() + " differs from " + rhs->getNamePath() + "\n";
}
return false;
}
}
Expand All @@ -769,7 +777,7 @@ bool ValueElement::isAttributeEquivalent(ConstElementPtr rhs, const string& attr
// If did not peform a value comparison, perform the default comparison
if (!performedValueComparison)
{
return Element::isAttributeEquivalent(rhs, attributeName, options, results);
return Element::isAttributeEquivalent(rhs, attributeName, options, message);
}

return true;
Expand Down
45 changes: 7 additions & 38 deletions source/MaterialXCore/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ using ElementMap = std::unordered_map<string, ElementPtr>;
using ElementPredicate = std::function<bool(ConstElementPtr)>;

class ElementEquivalenceOptions;
class ElementEquivalenceResult;
using ElementEquivalenceResultVec = vector<ElementEquivalenceResult>;

/// @class Element
/// The base class for MaterialX elements.
Expand Down Expand Up @@ -608,21 +606,21 @@ class MX_CORE_API Element : public std::enable_shared_from_this<Element>
/// criteria provided.
/// @param rhs Element to compare against
/// @param options Equivalence criteria
/// @param results Results of comparison if argument is specified.
/// @param message Optional text description of differences
/// @return True if the elements are equivalent. False otherwise.
bool isEquivalent(ConstElementPtr rhs, const ElementEquivalenceOptions& options,
ElementEquivalenceResultVec* results = nullptr) const;
bool isEquivalent(ConstElementPtr rhs, const ElementEquivalenceOptions& options,
string* message = nullptr) const;

/// Return true if the attribute on a given element is equivalent
/// based on the equivalence criteria provided.
/// @param rhs Element to compare against
/// @param attributeName Name of attribute to compare
/// @param options Equivalence criteria
/// @param results Results of comparison if argument is specified.
/// @param message Optional text description of differences
/// @return True if the attribute on the elements are equivalent. False otherwise.
virtual bool isAttributeEquivalent(ConstElementPtr rhs, const string& attributeName,
const ElementEquivalenceOptions& options,
ElementEquivalenceResultVec* results = nullptr) const;
string* message = nullptr) const;

/// @}
/// @name Traversal
Expand Down Expand Up @@ -1125,11 +1123,11 @@ class MX_CORE_API ValueElement : public TypedElement
/// @param rhs Element to compare against
/// @param attributeName Name of attribute to compare
/// @param options Equivalence criteria
/// @param results Results of comparison if argument is specified.
/// @param message Optional text description of differences
/// @return True if the attribute on the elements are equivalent. False otherwise.
bool isAttributeEquivalent(ConstElementPtr rhs, const string& attributeName,
const ElementEquivalenceOptions& options,
ElementEquivalenceResultVec* results = nullptr) const override;
string* message = nullptr) const override;

/// @}
/// @name Validation
Expand Down Expand Up @@ -1353,35 +1351,6 @@ class MX_CORE_API StringResolver
StringMap _geomNameMap;
};

/// @class ElementEquivalenceResult
/// A comparison result for the functional equivalence of two elements.
class MX_CORE_API ElementEquivalenceResult
{
public:
ElementEquivalenceResult(const string& p1, const string& p2, const string& type,
const string& attrName = EMPTY_STRING)
{
path1 = p1;
path2 = p2;
differenceType = type;
attributeName = attrName;
}
ElementEquivalenceResult() = delete;
~ElementEquivalenceResult() = default;

string path1;
string path2;
string differenceType;
string attributeName;

static const string ATTRIBUTE;
static const string ATTRIBUTE_NAMES;
static const string CHILD_COUNT;
static const string CHILD_NAME;
static const string NAME;
static const string CATEGORY;
};

/// @class ElementEquivalenceOptions
/// A set of options for comparing the functional equivalence of elements.
class MX_CORE_API ElementEquivalenceOptions
Expand Down
28 changes: 18 additions & 10 deletions source/MaterialXGenShader/ShaderGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,27 @@ void ShaderGraph::createConnectedNodes(const ElementPtr& downstreamElement,
{
// We have a node downstream
ShaderNode* downstream = getNode(downstreamNode->getName());
if (downstream && connectingElement)
if (downstream)
{
ShaderInput* input = downstream->getInput(connectingElement->getName());
if (!input)
if (downstream == newNode)
{
throw ExceptionShaderGenError("Could not find an input named '" + connectingElement->getName() +
"' on downstream node '" + downstream->getName() + "'");
throw ExceptionShaderGenError("Upstream node '" + downstream->getName() + "' has itself as downstream node, creating a loop");
}

if (connectingElement)
{
ShaderInput* input = downstream->getInput(connectingElement->getName());
if (!input)
{
throw ExceptionShaderGenError("Could not find an input named '" + connectingElement->getName() +
"' on downstream node '" + downstream->getName() + "'");
}
input->makeConnection(output);
}
else
{
throw ExceptionShaderGenError("Could not find downstream node ' " + downstreamNode->getName() + "'");
}
input->makeConnection(output);
}
else
{
throw ExceptionShaderGenError("Could not find downstream node ' " + downstreamNode->getName() + "'");
}
}
else
Expand Down
7 changes: 7 additions & 0 deletions source/MaterialXGenShader/ShaderNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ void ShaderInput::makeConnection(ShaderOutput* src)
if (src)
{
// Make the new connection.
if (src->getNode() == getNode() && !getNode()->isAGraph())
{
throw ExceptionShaderGenError(
"Tried to create looping connection on node " + getNode()->getName()
+ " from output: " + src->getFullName() + " to input: " + getFullName());
}

_connection = src;
src->_connections.push_back(this);
}
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXGenShader/TypeDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ TYPEDESC_REGISTER_TYPE(MATERIAL, "material")
// StructTypeDesc methods
//

void StructTypeDesc::addMember(const string& name, TypeDesc type, string defaultValueStr)
void StructTypeDesc::addMember(const string& name, TypeDesc type, const string& defaultValueStr)
{
_members.emplace_back(StructTypeDesc::StructMemberTypeDesc(name, type, defaultValueStr));
}
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXGenShader/TypeDesc.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class MX_GENSHADER_API StructTypeDesc
/// Empty constructor.
StructTypeDesc() noexcept{}

void addMember(const string& name, TypeDesc type, string defaultValueStr);
void addMember(const string& name, TypeDesc type, const string& defaultValueStr);
void setTypeDesc(TypeDesc typedesc) { _typedesc = typedesc; }

/// Return a type description by index.
Expand Down
Loading

0 comments on commit 92646bb

Please sign in to comment.