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

Apple : Compilation support for MaterialX 1.38.X and 1.39.X #3159

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 63 additions & 56 deletions pxr/imaging/hdSt/materialXFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,9 @@ _UpdatePrimvarNodes(
(*mxHdPrimvarDefaultValueMap)[primvarName] = defaultPrimvarValue;
}

// Texcoord nodes will have an index parameter set
primvarNameIt = hdPrimvarNode.parameters.find(_tokens->index);
if (primvarNameIt != hdPrimvarNode.parameters.end()) {
// Get the sdr node for the texcoord node
else {
// Other primvar nodes will be either a texcoord node or a
// custom node that uses a texcoord node.
SdrRegistry &sdrRegistry = SdrRegistry::GetInstance();
const SdrShaderNodeConstPtr sdrTexCoordNode =
sdrRegistry.GetShaderNodeByIdentifierAndType(
Expand All @@ -511,12 +510,7 @@ _UpdatePrimvarNodes(
texCoordName = metadata[SdrNodeMetadata->Primvars];
}

// Figure out the mx typename
mx::NodeDefPtr mxNodeDef = mxDoc->getNodeDef(
hdPrimvarNode.nodeTypeId.GetString());
if (mxNodeDef) {
(*mxHdPrimvarMap)[texCoordName] = mxNodeDef->getType();
}
(*mxHdPrimvarMap)[texCoordName] = getVector2Name();
}
}
}
Expand Down Expand Up @@ -634,26 +628,41 @@ _GetGlTFSurfaceMaterialTag(HdMaterialNode2 const& terminal)
return materialToken.GetString();
}

static const mx::TypeDesc*
static const mx::TypeDesc
_GetMxTypeDescription(std::string const& typeName)
{
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 38
using MxTypeDesc = const mx::TypeDesc*;
#else
using MxTypeDesc = const mx::TypeDesc;
#endif

// Add whatever is necessary for current codebase:
static const auto _typeLibrary =
std::map<std::string, const mx::TypeDesc*>{
{"float", mx::Type::FLOAT},
{"color3", mx::Type::COLOR3},
{"color4", mx::Type::COLOR4},
{"vector2", mx::Type::VECTOR2},
{"vector3", mx::Type::VECTOR3},
{"vector4", mx::Type::VECTOR4},
{"surfaceshader", mx::Type::SURFACESHADER}
};
static const auto _typeLibrary =
std::map<std::string, MxTypeDesc>{
{"float", mx::Type::FLOAT},
{"color3", mx::Type::COLOR3},
{"color4", mx::Type::COLOR4},
{"vector2", mx::Type::VECTOR2},
{"vector3", mx::Type::VECTOR3},
{"vector4", mx::Type::VECTOR4},
{"surfaceshader", mx::Type::SURFACESHADER}
};

const auto typeDescIt = _typeLibrary.find(typeName);
if (typeDescIt != _typeLibrary.end()) {
return typeDescIt->second;
}
return nullptr;
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 38
return *typeDescIt->second;
#else
return typeDescIt->second;
#endif
}

#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 38
return *mx::Type::NONE;
#else
return mx::Type::NONE;
#endif
}

// This function adds a stripped down version of the surfaceshader node to the
Expand All @@ -678,14 +687,14 @@ _AddStrippedSurfaceNode(
if (!mxInputDef) {
continue;
}
auto const* mxTypeDesc = _GetMxTypeDescription(mxInputDef->getType());
if (!mxTypeDesc) {
auto const mxTypeDesc = _GetMxTypeDescription(mxInputDef->getType());
if (mxTypeIsNone(mxTypeDesc)) {
continue;
}
// If hdNode is connected to the surfaceshader node, recursively call
// this function to make sure that surfaceshader node is added to
// the mxDocument
if (mxTypeDesc == mx::Type::SURFACESHADER) {
if (mxTypeIsSurfaceShader(mxTypeDesc)) {
auto const& hdConnectedPath = connIt.second.front().upstreamNode;
auto const& hdConnectedNode = hdNetwork.nodes.at(hdConnectedPath);
mx::NodePtr mxConnectedNode =
Expand All @@ -696,14 +705,13 @@ _AddStrippedSurfaceNode(
mxInput->setConnectedNode(mxConnectedNode);
}
// Add the connection as an input with each component set to 0.5
else if (mxTypeDesc->getBaseType() == mx::TypeDesc::BASETYPE_FLOAT &&
mxTypeDesc->getSemantic() != mx::TypeDesc::SEMANTIC_MATRIX) {
else if (mxTypeDesc.getBaseType() == mx::TypeDesc::BASETYPE_FLOAT &&
mxTypeDesc.getSemantic() != mx::TypeDesc::SEMANTIC_MATRIX) {
std::string valueStr = "0.5";
for (size_t i = 1; i < mxTypeDesc->getSize(); ++i) {
for (size_t i = 1; i < mxTypeDesc.getSize(); ++i) {
valueStr += ", 0.5";
}
mx::InputPtr mxInput =
mxNode->addInput(mxInputDef->getName(), mxInputDef->getType());
mx::InputPtr mxInput = mxNode->addInputFromNodeDef(mxInputDef->getName());
mxInput->setValueString(valueStr);
}
}
Expand All @@ -715,16 +723,15 @@ _AddStrippedSurfaceNode(
if (!mxInputDef) {
continue;
}
auto const* mxTypeDesc = _GetMxTypeDescription(mxInputDef->getType());
if (!mxTypeDesc) {
auto const mxTypeDesc = _GetMxTypeDescription(mxInputDef->getType());
if (mxTypeIsNone(mxTypeDesc)) {
continue;
}

if (mxTypeDesc->getBaseType() == mx::TypeDesc::BASETYPE_FLOAT &&
mxTypeDesc->getSemantic() != mx::TypeDesc::SEMANTIC_MATRIX) {
if (mxTypeDesc.getBaseType() == mx::TypeDesc::BASETYPE_FLOAT &&
mxTypeDesc.getSemantic() != mx::TypeDesc::SEMANTIC_MATRIX) {
// Add the parameter as an input to the mxNode in the mx Document
mx::InputPtr mxInput =
mxNode->addInput(mxInputDef->getName(), mxInputDef->getType());
mx::InputPtr mxInput = mxNode->addInputFromNodeDef(mxInputDef->getName());
mxInput->setValueString(HdMtlxConvertToString(paramIt.second));
}
}
Expand Down Expand Up @@ -787,9 +794,9 @@ _GetMaterialTag(
// Outputting anything that is not a surfaceshader will be
// considered opaque, unless outputting a color4 or vector4.
// XXX This is not fully per USD specs, but is supported by MaterialX.
auto const* typeDesc =
auto const typeDesc =
_GetMxTypeDescription(activeOutputs.back()->getType());
if (typeDesc == mx::Type::COLOR4 || typeDesc == mx::Type::VECTOR4) {
if (typeDesc.isFloat4()) {
return HdStMaterialTagTokens->translucent.GetString();
}
return HdStMaterialTagTokens->defaultMaterialTag.GetString();
Expand Down Expand Up @@ -1100,7 +1107,7 @@ _AddMaterialXParams(

// MaterialX parameter Information
const auto* variable = paramsBlock[i];
const auto varType = variable->getType();
const auto varType = getMxTypeDesc(variable);

// Create a corresponding HdSt_MaterialParam
HdSt_MaterialParam param;
Expand All @@ -1111,9 +1118,9 @@ _AddMaterialXParams(
const auto paramValueIt =
mxParamNameToValue.find(variable->getVariable());
if (paramValueIt != mxParamNameToValue.end()) {
if (varType->getBaseType() == mx::TypeDesc::BASETYPE_BOOLEAN ||
varType->getBaseType() == mx::TypeDesc::BASETYPE_FLOAT ||
varType->getBaseType() == mx::TypeDesc::BASETYPE_INTEGER) {
if (varType.getBaseType() == mx::TypeDesc::BASETYPE_BOOLEAN ||
varType.getBaseType() == mx::TypeDesc::BASETYPE_FLOAT ||
varType.getBaseType() == mx::TypeDesc::BASETYPE_INTEGER) {
param.fallbackValue = paramValueIt->second;
}
}
Expand All @@ -1124,52 +1131,52 @@ _AddMaterialXParams(
const auto varValue = variable->getValue();
std::istringstream valueStream(varValue
? varValue->getValueString() : std::string());
if (varType->getBaseType() == mx::TypeDesc::BASETYPE_BOOLEAN) {
if (varType.getBaseType() == mx::TypeDesc::BASETYPE_BOOLEAN) {
const bool val = valueStream.str() == "true";
param.fallbackValue = VtValue(val);
}
else if (varType->getBaseType() == mx::TypeDesc::BASETYPE_FLOAT) {
if (varType->getSize() == 1) {
else if (varType.getBaseType() == mx::TypeDesc::BASETYPE_FLOAT) {
if (varType.getSize() == 1) {
float val;
valueStream >> val;
param.fallbackValue = VtValue(val);
}
else if (varType->getSize() == 2) {
else if (varType.getSize() == 2) {
GfVec2f val;
valueStream >> val[0] >> separator >> val[1];
param.fallbackValue = VtValue(val);
}
else if (varType->getSize() == 3) {
else if (varType.getSize() == 3) {
GfVec3f val;
valueStream >> val[0] >> separator >> val[1] >> separator
>> val[2];
param.fallbackValue = VtValue(val);
}
else if (varType->getSize() == 4) {
else if (varType.getSize() == 4) {
GfVec4f val;
valueStream >> val[0] >> separator >> val[1] >> separator
>> val[2] >> separator >> val[3];
param.fallbackValue = VtValue(val);
}
}
else if (varType->getBaseType() == mx::TypeDesc::BASETYPE_INTEGER) {
if (varType->getSize() == 1) {
else if (varType.getBaseType() == mx::TypeDesc::BASETYPE_INTEGER) {
if (varType.getSize() == 1) {
int val;
valueStream >> val;
param.fallbackValue = VtValue(val);
}
else if (varType->getSize() == 2) {
else if (varType.getSize() == 2) {
GfVec2i val;
valueStream >> val[0] >> separator >> val[1];
param.fallbackValue = VtValue(val);
}
else if (varType->getSize() == 3) {
else if (varType.getSize() == 3) {
GfVec3i val;
valueStream >> val[0] >> separator >> val[1] >> separator
>> val[2];
param.fallbackValue = VtValue(val);
}
else if (varType->getSize() == 4) {
else if (varType.getSize() == 4) {
GfVec4i val;
valueStream >> val[0] >> separator >> val[1] >> separator
>> val[2] >> separator >> val[3];
Expand All @@ -1183,7 +1190,7 @@ _AddMaterialXParams(
}

// For filename inputs, manage the associated texture node
if (varType->getSemantic() == mx::TypeDesc::SEMANTIC_FILENAME) {
if (varType.getSemantic() == mx::TypeDesc::SEMANTIC_FILENAME) {
// Get the anonymized MaterialX node name from the param name
// annonNodeName_paramName -> annonNodeName
std::string mxNodeName = variable->getVariable();
Expand Down
Loading