Skip to content

Commit

Permalink
Handle older MaterialX versions.
Browse files Browse the repository at this point in the history
Also handle USDMTLX_PRIMARY_UV_NAME env var.
  • Loading branch information
JGamache-autodesk committed Aug 28, 2023
1 parent 39fb4e7 commit 5c162e1
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 23 deletions.
1 change: 1 addition & 0 deletions lib/mayaUsd/render/MaterialXGenOgsXml/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ target_sources(${PROJECT_NAME}
)

set(HEADERS
CombinedMaterialXVersion.h
GlslFragmentGenerator.h
GlslOcioNodeImpl.h
OgsFragment.h
Expand Down
24 changes: 24 additions & 0 deletions lib/mayaUsd/render/MaterialXGenOgsXml/CombinedMaterialXVersion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//

#ifndef MATERIALX_COMBINEDVERSION_H
#define MATERIALX_COMBINEDVERSION_H

/// @file
/// GLSL fragment generator

#include <MaterialXCore/Generated.h>

#if !defined(MATERIALX_NAMESPACE_BEGIN)
// The above define was introduced in MaterialX 1.38.3. If it does not exist, we know we are on an
// earlier 1.38 MaterialX version that can use the same code we use for 1.38.3
#define MX_COMBINED_VERSION 13803
#else
#define MX_COMBINED_VERSION \
((MATERIALX_MAJOR_VERSION * 100 * 100) + (MATERIALX_MINOR_VERSION * 100) \
+ MATERIALX_BUILD_VERSION)
#endif

#endif
11 changes: 7 additions & 4 deletions lib/mayaUsd/render/MaterialXGenOgsXml/GlslFragmentGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Nodes/SurfaceNodeMaya.h"
#include "Nodes/TexcoordNodeMaya.h"

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

Expand Down Expand Up @@ -165,10 +166,12 @@ GlslFragmentGenerator::GlslFragmentGenerator()
_tokenSubstitutions[HW::T_NUM_ACTIVE_LIGHT_SOURCES] = "g_numActiveLightSources";
}

registerImplementation(
"IM_texcoord_vector2_" + GlslShaderGenerator::TARGET, TexcoordNodeGlslMaya::create);
registerImplementation(
"IM_texcoord_vector3_" + GlslShaderGenerator::TARGET, TexcoordNodeGlslMaya::create);
if (!OgsXmlGenerator::getPrimaryUVSetName().empty()) {
registerImplementation(
"IM_texcoord_vector2_" + GlslShaderGenerator::TARGET, TexcoordNodeGlslMaya::create);
registerImplementation(
"IM_texcoord_vector3_" + GlslShaderGenerator::TARGET, TexcoordNodeGlslMaya::create);
}

for (auto&& implName : GlslOcioNodeImpl::getOCIOImplementations()) {
registerImplementation(implName, GlslOcioNodeImpl::create);
Expand Down
10 changes: 0 additions & 10 deletions lib/mayaUsd/render/MaterialXGenOgsXml/GlslFragmentGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@

MATERIALX_NAMESPACE_BEGIN

#if !defined(MATERIALX_NAMESPACE_BEGIN)
// The above define was introduced in MaterialX 1.38.3. If it does not exist, we know we are on an
// earlier 1.38 MaterialX version that can use the same code we use for 1.38.3
#define MX_COMBINED_VERSION 13803
#else
#define MX_COMBINED_VERSION \
((MATERIALX_MAJOR_VERSION * 100 * 100) + (MATERIALX_MINOR_VERSION * 100) \
+ MATERIALX_BUILD_VERSION)
#endif

#define MX_REFRACTION_SUBSTITUTION "(mayaGetSpecularEnvironmentNumLOD() > 0)"

namespace Stage {
Expand Down
65 changes: 56 additions & 9 deletions lib/mayaUsd/render/MaterialXGenOgsXml/Nodes/TexcoordNodeMaya.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,47 @@

#include "TexcoordNodeMaya.h"

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

#include <MaterialXGenShader/Shader.h>

MATERIALX_NAMESPACE_BEGIN

namespace {
std::string geomNameFromIndex(const std::string& index)
{
std::string geomname = OgsXmlGenerator::getPrimaryUVSetName();

// The code below which is trying to handle non-zero indices has little chance of working. Main
// reason is that our main client (USD) will only handle UV0 by adding an extra primvar in the
// set of required primvars. Since this is the set that will be used by the mesh hydra scene
// delegate, the primvars for non-zero index will end up missing in the cached geometry data and
// prevent copying all necessary buffers to render the material correctly.

// Fixing this would require extending USD's GetAdditionalPrimvarProperties() to return a
// functor instead of a hardcoded Token. This way we could add the equivalent of this code as a
// functor in the MaterialX parser, and the _ExtractPrimvarsFromNode function found in
// pxr\usdImaging\usdImaging\materialParamUtils.cpp would then be able to handle indexed values.
#if 0
int i = fromValueString<int>(index);
if (i < 0 || i < 9) {
i = 0;
}
if (i > 0) {
if (geomname.back() == '0') {
// uv0 -> uvX
geomname.back() = '0' + i;
} else {
// st -> stX
geomname = geomname + std::to_string(i);
}
}
#endif
return geomname;
}
} // namespace

ShaderNodeImplPtr TexcoordNodeGlslMaya::create()
{
return std::make_shared<TexcoordNodeGlslMaya>();
Expand All @@ -24,11 +61,8 @@ void TexcoordNodeGlslMaya::createVariables(const ShaderNode& node, GenContext&,
+ "'. Don't know what property to bind");
}
// Use the standard USD convention for texcoord primvar names:
const string index = indexInput ? indexInput->getValue()->getValueString() : "0";
string geomProp = "st";
if (index != "0") {
geomProp += index;
};
const string index = indexInput ? indexInput->getValue()->getValueString() : "0";
const string geomProp = geomNameFromIndex(index);
const ShaderOutput* output = node.getOutput();

ShaderStage& vs = shader.getStage(Stage::VERTEX);
Expand All @@ -55,14 +89,15 @@ void TexcoordNodeGlslMaya::emitFunctionCall(
}
// Use the standard USD convention for texcoord primvar names:
const string index = indexInput ? indexInput->getValue()->getValueString() : "0";
string geomname = "st";
if (index != "0") {
geomname += index;
};
const string geomname = geomNameFromIndex(index);
const string variable = HW::T_IN_GEOMPROP + "_" + geomname;

#if MX_COMBINED_VERSION >= 13807
DEFINE_SHADER_STAGE(stage, Stage::VERTEX)
{
#else
BEGIN_SHADER_STAGE(stage, Stage::VERTEX)
#endif
VariableBlock& vertexData = stage.getOutputBlock(HW::VERTEX_DATA);
const string prefix = shadergen.getVertexDataPrefix(vertexData);
ShaderPort* geomprop = vertexData[variable];
Expand All @@ -72,18 +107,30 @@ void TexcoordNodeGlslMaya::emitFunctionCall(
stage);
geomprop->setEmitted();
}
#if MX_COMBINED_VERSION >= 13807
}
#else
END_SHADER_STAGE(shader, Stage::VERTEX)
#endif

#if MX_COMBINED_VERSION >= 13807
DEFINE_SHADER_STAGE(stage, Stage::PIXEL)
{
#else
BEGIN_SHADER_STAGE(stage, Stage::PIXEL)
#endif
VariableBlock& vertexData = stage.getInputBlock(HW::VERTEX_DATA);
const string prefix = shadergen.getVertexDataPrefix(vertexData);
ShaderPort* geomprop = vertexData[variable];
shadergen.emitLineBegin(stage);
shadergen.emitOutput(node.getOutput(), true, false, context, stage);
shadergen.emitString(" = " + prefix + geomprop->getVariable(), stage);
shadergen.emitLineEnd(stage);
#if MX_COMBINED_VERSION >= 13807
}
#else
END_SHADER_STAGE(shader, Stage::PIXEL)
#endif
}

MATERIALX_NAMESPACE_END
1 change: 1 addition & 0 deletions lib/mayaUsd/render/MaterialXGenOgsXml/OgsFragment.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "OgsFragment.h"

#include <mayaUsd/render/MaterialXGenOgsXml/CombinedMaterialXVersion.h>
#include <mayaUsd/render/MaterialXGenOgsXml/GlslFragmentGenerator.h>
#include <mayaUsd/render/MaterialXGenOgsXml/GlslOcioNodeImpl.h>
#include <mayaUsd/render/MaterialXGenOgsXml/OgsXmlGenerator.h>
Expand Down
5 changes: 5 additions & 0 deletions lib/mayaUsd/render/MaterialXGenOgsXml/OgsXmlGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,4 +563,9 @@ int OgsXmlGenerator::sUseLightAPI = 1;
int OgsXmlGenerator::useLightAPI() { return sUseLightAPI; }
void OgsXmlGenerator::setUseLightAPI(int val) { sUseLightAPI = val; }

string OgsXmlGenerator::sPrimaryUVSetName;

const string& OgsXmlGenerator::getPrimaryUVSetName() { return sPrimaryUVSetName; }
void OgsXmlGenerator::setPrimaryUVSetName(const string& val) { sPrimaryUVSetName = val; }

MATERIALX_NAMESPACE_END
6 changes: 6 additions & 0 deletions lib/mayaUsd/render/MaterialXGenOgsXml/OgsXmlGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ class OgsXmlGenerator
static int useLightAPI();
static void setUseLightAPI(int);

/// Replace every texcoord use with this UV set name (optional):
/// Empty string will let texcoord generate their usual code.
static const string& getPrimaryUVSetName();
static void setPrimaryUVSetName(const string& mainUvSetName);

private:
static const string SAMPLER_SUFFIX;
static const string OCIO_SAMPLER_SUFFIX;
static const string OCIO_SAMPLER_PREFIX;
static int sUseLightAPI;
static string sPrimaryUVSetName;
};

MATERIALX_NAMESPACE_END
Expand Down
7 changes: 7 additions & 0 deletions lib/mayaUsd/render/vp2RenderDelegate/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <pxr/usd/sdf/assetPath.h>
#include <pxr/usd/sdr/registry.h>
#include <pxr/usd/usdHydra/tokens.h>
#include <pxr/usd/usdUtils/pipeline.h>
#include <pxr/usdImaging/usdImaging/textureUtils.h>
#include <pxr/usdImaging/usdImaging/tokens.h>

Expand Down Expand Up @@ -475,6 +476,12 @@ struct _MaterialXData
_FixLibraryTangentInputs(_mtlxLibrary);

mx::OgsXmlGenerator::setUseLightAPI(MAYA_LIGHTAPI_VERSION_2);

// This environment variable is defined in USD: pxr\usd\usdMtlx\parser.cpp
static const std::string env = TfGetenv("USDMTLX_PRIMARY_UV_NAME");
std::string mainUvSetName = env.empty() ? UsdUtilsGetPrimaryUVSetName().GetString() : env;

mx::OgsXmlGenerator::setPrimaryUVSetName(mainUvSetName);
}
MaterialX::FileSearchPath _mtlxSearchPath; //!< MaterialX library search path
MaterialX::DocumentPtr _mtlxLibrary; //!< MaterialX library
Expand Down

0 comments on commit 5c162e1

Please sign in to comment.