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

MaterialX 1.39.x support #4134

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
56 changes: 53 additions & 3 deletions lib/mayaUsd/render/MaterialXGenOgsXml/GlslFragmentGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ const string& HwSpecularEnvironmentSamples::name()

string GlslFragmentSyntax::getVariableName(
const string& name,
#if MX_COMBINED_VERSION < 13900
const TypeDesc* type,
#else
TypeDesc type,
#endif
IdentifierMap& identifiers) const
{
string variable = GlslSyntax::getVariableName(name, type, identifiers);
Expand Down Expand Up @@ -445,7 +449,11 @@ ShaderPtr GlslFragmentGenerator::createShader(
for (ShaderNode* node : g->getNodes()) {
if (node->hasClassification(ShaderNode::Classification::FILETEXTURE)) {
for (ShaderInput* input : node->getInputs()) {
#if MX_COMBINED_VERSION < 13900
if (!input->getConnection() && *input->getType() == *Type::FILENAME) {
#else
if (!input->getConnection() && input->getType() == Type::FILENAME) {
#endif
// Create the uniform using the filename type to make this uniform into a
// texture sampler.
ShaderPort* filename = psPublicUniforms->add(
Expand Down Expand Up @@ -782,12 +790,13 @@ ShaderPtr GlslFragmentGenerator::generate(
//
if (const ShaderOutput* const outputConnection = outputSocket->getConnection()) {
string finalOutput = outputConnection->getVariable();
#if MX_COMBINED_VERSION < 13900
const string& channels = outputSocket->getChannels();
if (!channels.empty()) {
finalOutput = _syntax->getSwizzledVariable(
finalOutput, outputConnection->getType(), channels, outputSocket->getType());
}

#endif
if (graph.hasClassification(ShaderNode::Classification::SURFACE)) {
if (context.getOptions().hwTransparency) {
emitLine(
Expand All @@ -798,20 +807,34 @@ ShaderPtr GlslFragmentGenerator::generate(
emitLine("return " + finalOutput + ".color", pixelStage);
}
} else {
#if MX_COMBINED_VERSION < 13900
if (context.getOptions().hwTransparency && !outputSocket->getType()->isFloat4()) {
toVec4(outputSocket->getType(), finalOutput);
} else if (
!context.getOptions().hwTransparency && !outputSocket->getType()->isFloat3()) {
toVec3(outputSocket->getType(), finalOutput);
}
#else
if (context.getOptions().hwTransparency && !outputSocket->getType().isFloat4()) {
toVec4(outputSocket->getType(), finalOutput);
} else if (
!context.getOptions().hwTransparency && !outputSocket->getType().isFloat3()) {
toVec3(outputSocket->getType(), finalOutput);
}
#endif
emitLine("return " + finalOutput, pixelStage);
}
} else {
const string outputValue = outputSocket->getValue()
? _syntax->getValue(outputSocket->getType(), *outputSocket->getValue())
: _syntax->getDefaultValue(outputSocket->getType());

if (!context.getOptions().hwTransparency && !outputSocket->getType()->isFloat3()) {
if (!context.getOptions().hwTransparency &&
#if MX_COMBINED_VERSION < 13900
!outputSocket->getType()->isFloat3()) {
#else
!outputSocket->getType().isFloat3()) {
#endif
string finalOutput = outputSocket->getVariable() + "_tmp";
emitLine(
_syntax->getTypeName(outputSocket->getType()) + " " + finalOutput + " = "
Expand All @@ -820,7 +843,12 @@ ShaderPtr GlslFragmentGenerator::generate(
toVec3(outputSocket->getType(), finalOutput);
emitLine("return " + finalOutput, pixelStage);
} else if (
context.getOptions().hwTransparency && !outputSocket->getType()->isFloat4()) {
context.getOptions().hwTransparency &&
#if MX_COMBINED_VERSION < 13900
!outputSocket->getType()->isFloat4()) {
#else
!outputSocket->getType().isFloat4()) {
#endif
string finalOutput = outputSocket->getVariable() + "_tmp";
emitLine(
_syntax->getTypeName(outputSocket->getType()) + " " + finalOutput + " = "
Expand Down Expand Up @@ -896,12 +924,21 @@ ShaderPtr GlslFragmentGenerator::generate(
return shader;
}

#if MX_COMBINED_VERSION < 13900
void GlslFragmentGenerator::toVec3(const TypeDesc* type, string& variable)
{
if (type->isFloat2()) {
variable = "vec3(" + variable + ", 0.0)";
} else if (type->isFloat4()) {
variable = variable + ".xyz";
#else
void GlslFragmentGenerator::toVec3(const TypeDesc& type, string& variable)
{
if (type.isFloat2()) {
variable = "vec3(" + variable + ", 0.0)";
} else if (type.isFloat4()) {
variable = variable + ".xyz";
#endif
} else if (type == Type::FLOAT || type == Type::INTEGER) {
variable = "vec3(" + variable + ", " + variable + ", " + variable + ")";
} else if (type == Type::BSDF || type == Type::EDF) {
Expand Down Expand Up @@ -954,10 +991,19 @@ GlslFragmentGenerator::getImplementation(const NodeDef& nodedef, GenContext& con
throw ExceptionShaderGenError("NodeDef '" + nodedef.getName() + "' has no outputs defined");
}

#if MX_COMBINED_VERSION < 13900
const TypeDesc* outputType = TypeDesc::get(outputs[0]->getType());
#else
const TypeDesc outputType = TypeDesc::get(outputs[0]->getType());
#endif

#if MX_COMBINED_VERSION < 13900
if (implElement->isA<NodeGraph>() && outputType->getName() != Type::LIGHTSHADER->getName()
&& !outputType->isClosure()) {
#else
if (implElement->isA<NodeGraph>() && outputType.getName() != Type::LIGHTSHADER.getName()
&& !outputType.isClosure()) {
#endif
// Use a compound implementation that can propagate UDIM inputs:
impl = MayaCompoundNode::create();
impl->initialize(*implElement, context);
Expand All @@ -968,7 +1014,11 @@ GlslFragmentGenerator::getImplementation(const NodeDef& nodedef, GenContext& con
return impl;
} else if (
implElement->isA<Implementation>() && !_implFactory.classRegistered(name)
#if MX_COMBINED_VERSION < 13900
&& !outputType->isClosure()) {
#else
&& !outputType.isClosure()) {
#endif
// Backporting 1.39 fix done in
// https://github.com/AcademySoftwareFoundation/MaterialX/pull/1754
impl = MayaSourceCodeNode::create();
Expand Down
10 changes: 10 additions & 0 deletions lib/mayaUsd/render/MaterialXGenOgsXml/GlslFragmentGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/// @file
/// GLSL fragment generator

#include <mayaUsd/render/MaterialXGenOgsXml/CombinedMaterialXVersion.h>

#include <MaterialXGenGlsl/GlslShaderGenerator.h>
#include <MaterialXGenGlsl/GlslSyntax.h>
#include <MaterialXGenShader/GenUserData.h>
Expand Down Expand Up @@ -50,7 +52,11 @@ class HwSpecularEnvironmentSamples : public GenUserData
class GlslFragmentSyntax : public GlslSyntax
{
public:
#if MX_COMBINED_VERSION < 13900
string getVariableName(const string& name, const TypeDesc* type, IdentifierMap& identifiers)
#else
string getVariableName(const string& name, TypeDesc type, IdentifierMap& identifiers)
#endif
const override;
};

Expand Down Expand Up @@ -84,7 +90,11 @@ class GlslFragmentGenerator : public GlslShaderGenerator
#endif

protected:
#if MX_COMBINED_VERSION < 13900
static void toVec3(const TypeDesc* type, string& variable);
#else
static void toVec3(const TypeDesc& type, string& variable);
#endif
};

MATERIALX_NAMESPACE_END
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,13 @@ MayaShaderGraph::MayaShaderGraph(
ValuePtr value = nodeInput->getResolvedValue();
if (value) {
const string& valueString = value->getValueString();
#if MX_COMBINED_VERSION < 13900
std::pair<const TypeDesc*, ValuePtr> enumResult;
const TypeDesc* type = TypeDesc::get(nodedefInput->getType());
#else
std::pair<TypeDesc, ValuePtr> enumResult;
TypeDesc type = TypeDesc::get(nodedefInput->getType());
#endif
const string& enumNames
= nodedefInput->getAttribute(ValueElement::ENUM_ATTRIBUTE);
if (context.getShaderGenerator().getSyntax().remapEnumeration(
Expand Down
8 changes: 8 additions & 0 deletions lib/mayaUsd/render/MaterialXGenOgsXml/OgsFragment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,11 @@ OgsFragment::OgsFragment(mx::ElementPtr element, GLSL_GENERATOR_WRAPPER&& glslGe
for (size_t i = 0; i < uniforms.size(); ++i) {
const mx::ShaderPort* const port = uniforms[i];
if (!port->getNode()) {
#if MX_COMBINED_VERSION < 13900
if (port->getType()->getSemantic() == mx::TypeDesc::SEMANTIC_FILENAME) {
#else
if (port->getType().getSemantic() == mx::TypeDesc::SEMANTIC_FILENAME) {
#endif
// Might be an embedded texture. Retrieve the filename.
std::string textureName
= mx::OgsXmlGenerator::samplerToTextureName(port->getVariable());
Expand Down Expand Up @@ -675,7 +679,11 @@ OgsFragment::OgsFragment(mx::ElementPtr element, GLSL_GENERATOR_WRAPPER&& glslGe
path += originalName;
}
if (!path.empty()) {
#if MX_COMBINED_VERSION < 13900
if (port->getType()->getSemantic() == mx::TypeDesc::SEMANTIC_FILENAME) {
#else
if (port->getType().getSemantic() == mx::TypeDesc::SEMANTIC_FILENAME) {
#endif
std::string textureName
= mx::OgsXmlGenerator::samplerToTextureName(variableName);
if (!textureName.empty()) {
Expand Down
48 changes: 48 additions & 0 deletions lib/mayaUsd/render/MaterialXGenOgsXml/OgsXmlGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ MATERIALX_NAMESPACE_BEGIN
namespace {
typedef std::unordered_map<string, const pugi::char_t*> OGS_TYPE_MAP_T;

#if MX_COMBINED_VERSION < 13900
const OGS_TYPE_MAP_T& getOgsTypeMap()
{
// Data types used by OGS
Expand All @@ -34,6 +35,22 @@ const OGS_TYPE_MAP_T& getOgsTypeMap()
{ Type::MATRIX44->getName(), "float4x4" } };
return OGS_TYPE_MAP;
}
#else
const OGS_TYPE_MAP_T& getOgsTypeMap()
{
// Data types used by OGS
// Delayed initialization to survive C++ init order fiasco.
// Keyed by string to survive multiple redefinitions of global symbols.
static const OGS_TYPE_MAP_T OGS_TYPE_MAP
= { { Type::BOOLEAN.getName(), "bool" }, { Type::FLOAT.getName(), "float" },
{ Type::INTEGER.getName(), "int" }, { Type::STRING.getName(), "int" },
{ Type::COLOR3.getName(), "float3" }, { Type::COLOR4.getName(), "float4" },
{ Type::VECTOR2.getName(), "float2" }, { Type::VECTOR3.getName(), "float3" },
{ Type::VECTOR4.getName(), "float4" }, { Type::MATRIX33.getName(), "float4x4" },
{ Type::MATRIX44.getName(), "float4x4" } };
return OGS_TYPE_MAP;
}
#endif

// Semantics used by OGS
static const std::unordered_map<string, const pugi::char_t*> OGS_SEMANTICS_MAP = {
Expand Down Expand Up @@ -230,7 +247,11 @@ void xmlAddProperties(
sampler, shaderPort->getName(), samplerName, parameterFlags, refNode);
}
} else {
#if MX_COMBINED_VERSION < 13900
const auto type = getOgsTypeMap().find(shaderPort->getType()->getName());
#else
const auto type = getOgsTypeMap().find(shaderPort->getType().getName());
#endif
if (type != getOgsTypeMap().end()) {
pugi::xml_node prop = parent.append_child(type->second);
if (shaderPort->getType() == Type::MATRIX33) {
Expand Down Expand Up @@ -262,7 +283,11 @@ void xmlAddValues(pugi::xml_node& parent, const VariableBlock& block, bool skipL
continue;
}
if (p->getValue()) {
#if MX_COMBINED_VERSION < 13900
auto type = getOgsTypeMap().find(p->getType()->getName());
#else
auto type = getOgsTypeMap().find(p->getType().getName());
#endif
if (type != getOgsTypeMap().end()) {
pugi::xml_node val = parent.append_child(type->second);
if (p->getType() == Type::MATRIX33) {
Expand Down Expand Up @@ -415,8 +440,13 @@ string OgsXmlGenerator::generate(
throw ExceptionShaderGenError("Shader stage has no output");
}
pugi::xml_node xmlOutputs = xmlRoot.append_child(OUTPUTS);
#if MX_COMBINED_VERSION < 13900
pugi::xml_node xmlOut = xmlOutputs.append_child(
getOgsTypeMap().at(hwTransparency ? Type::COLOR4->getName() : Type::COLOR3->getName()));
#else
pugi::xml_node xmlOut = xmlOutputs.append_child(
getOgsTypeMap().at(hwTransparency ? Type::COLOR4.getName() : Type::COLOR3.getName()));
#endif
xmlOut.append_attribute(NAME) = OUTPUT_NAME.c_str();

// Add implementations
Expand Down Expand Up @@ -499,15 +529,24 @@ string OgsXmlGenerator::generateLightRig(
baseShaderName.c_str());

// Add Light Rig properties:
#if MX_COMBINED_VERSION < 13900
auto vec3OGSType = getOgsTypeMap().find(Type::VECTOR3->getName())->second;
auto intOGSType = getOgsTypeMap().find(Type::INTEGER->getName())->second;
#else
auto vec3OGSType = getOgsTypeMap().find(Type::VECTOR3.getName())->second;
auto intOGSType = getOgsTypeMap().find(Type::INTEGER.getName())->second;
#endif
pugi::xml_node xmlLightProp = xmlProperties.append_child(vec3OGSType);
xmlLightProp.append_attribute(NAME) = IRRADIANCEENV;
xmlLightProp.append_attribute(REF) = DOT_COMBINE(baseShaderName.c_str(), DIFFUSEI).c_str();
xmlLightProp = xmlProperties.append_child(vec3OGSType);
xmlLightProp.append_attribute(NAME) = SPECULARENV;
xmlLightProp.append_attribute(REF) = DOT_COMBINE(baseShaderName.c_str(), SPECULARI).c_str();
#if MX_COMBINED_VERSION < 13900
xmlLightProp = xmlProperties.append_child(Type::STRING->getName().c_str());
#else
xmlLightProp = xmlProperties.append_child(Type::STRING.getName().c_str());
#endif
xmlLightProp.append_attribute(NAME) = SELECTOR;
xmlLightProp.append_attribute(REF) = DOT_COMBINE(LIGHT_ACCUM, SELECTOR).c_str();
xmlLightProp = xmlProperties.append_child(intOGSType);
Expand Down Expand Up @@ -536,7 +575,11 @@ string OgsXmlGenerator::generateLightRig(
xmlLightValue = xmlValues.append_child(vec3OGSType);
xmlLightValue.append_attribute(NAME) = SPECULARI;
xmlLightValue.append_attribute(VALUE) = "0, 0, 0";
#if MX_COMBINED_VERSION < 13900
xmlLightValue = xmlValues.append_child(Type::STRING->getName().c_str());
#else
xmlLightValue = xmlValues.append_child(Type::STRING.getName().c_str());
#endif
xmlLightValue.append_attribute(NAME) = SELECTOR;
xmlLightValue.append_attribute(VALUE) = LIGHT_SELECTOR;

Expand All @@ -547,8 +590,13 @@ string OgsXmlGenerator::generateLightRig(
}
pugi::xml_node xmlOutputs = xmlRoot.append_child(OUTPUTS);
const bool hwTransparency = glslShader.hasAttribute(HW::ATTR_TRANSPARENT);
#if MX_COMBINED_VERSION < 13900
pugi::xml_node xmlOut = xmlOutputs.append_child(
getOgsTypeMap().at(hwTransparency ? Type::COLOR4->getName() : Type::COLOR3->getName()));
#else
pugi::xml_node xmlOut = xmlOutputs.append_child(
getOgsTypeMap().at(hwTransparency ? Type::COLOR4.getName() : Type::COLOR3.getName()));
#endif
xmlOut.append_attribute(NAME) = OUTPUT_NAME.c_str();
xmlOut.append_attribute(REF)
= DOT_COMBINE(baseShaderName.c_str(), OgsXmlGenerator::OUTPUT_NAME.c_str()).c_str();
Expand Down
16 changes: 16 additions & 0 deletions lib/mayaUsd/render/MaterialXGenOgsXml/ShaderGenUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,11 @@ std::string TopoNeutralGraph::gatherChannels(const mx::Input& input)
}
const auto& ngInput = interfaceInput ? *interfaceInput : input;

#if MX_COMBINED_VERSION < 13900
std::string channelInfo = ngInput.getChannels();
#else
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the #else part. Channels are gone, so we should not see any here. And if we see some, it will be because we forgot to upgrade the document from 1.38 to 1.39, which might be needed in a few places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this one, do you think gatherChannels could just be bypassed entirely, or return an empty string?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please bypass it entirely on 1.39.

std::string channelInfo = ngInput.getAttribute("channels");
#endif

if (!ngInput.hasNodeGraphString()) {
if (ngInput.hasNodeName()) {
Expand All @@ -332,7 +336,11 @@ std::string TopoNeutralGraph::gatherChannels(const mx::Input& input)
throw mx::Exception("Could not find nodegraph");
}

#if MX_COMBINED_VERSION < 13900
const std::string outputChannels = output->getChannels();
#else
const std::string outputChannels = output->getAttribute("channels");
#endif
if (outputChannels.empty()) {
return channelInfo;
} else if (channelInfo.empty()) {
Expand Down Expand Up @@ -425,7 +433,11 @@ void TopoNeutralGraph::cloneConnection(
auto destInput = destNode.addInput(sourceInput.getName(), sourceInput.getType());
destInput->setConnectedNode(destConnectedNode);
if (!channelInfo.empty()) {
#if MX_COMBINED_VERSION < 13900
destInput->setChannels(channelInfo);
#else
destInput->setAttribute("channels", channelInfo);
#endif
}
if (!output.empty()) {
destInput->setOutputString(output);
Expand All @@ -449,7 +461,11 @@ void TopoNeutralGraph::cloneNodeGraphConnection(
graphOutput
= getNodeGraph()->addOutput("O" + std::to_string(_outputIndex), sourceInput.getType());
if (!channelInfo.empty()) {
#if MX_COMBINED_VERSION < 13900
graphOutput->setChannels(channelInfo);
#else
graphOutput->setAttribute("channels", channelInfo);
#endif
}
if (!output.empty()) {
graphOutput->setOutputString(output);
Expand Down