Skip to content

Commit 6e58ab1

Browse files
klucknavpixar-oss
authored andcommitted
Update MaterialX version checks
This adds a version calculation that combines the major/minor/build versions into one value for easier version comparisons. Similar functionality was added to MaterialX in v1.39.4 however we would need to wait until that version is the minimum version to be able to use it. This change was inspired by PR PixarAnimationStudios#3653 by @scotbrew Fixes PixarAnimationStudios#3651 Closes PixarAnimationStudios#3653 (Internal change: 2379338) (Internal change: 2379370)
1 parent ae234da commit 6e58ab1

File tree

7 files changed

+49
-63
lines changed

7 files changed

+49
-63
lines changed

pxr/imaging/hdMtlx/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ pxr_library(hdMtlx
2121

2222
PUBLIC_HEADERS
2323
api.h
24+
combinedMtlxVersion.h
2425
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// Copyright 2025 Pixar
3+
//
4+
// Licensed under the terms set forth in the LICENSE.txt file available at
5+
// https://openusd.org/license.
6+
//
7+
8+
#ifndef PXR_IMAGING_HD_MTLX_COMBINEDVERSION_H
9+
#define PXR_IMAGING_HD_MTLX_COMBINEDVERSION_H
10+
11+
#include <MaterialXCore/Generated.h>
12+
13+
#define MTLX_COMBINED_VERSION \
14+
((MATERIALX_MAJOR_VERSION * 100 * 100) + (MATERIALX_MINOR_VERSION * 100) \
15+
+ MATERIALX_BUILD_VERSION)
16+
#endif

pxr/imaging/hdMtlx/hdMtlx.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// https://openusd.org/license.
66
//
77
#include "pxr/imaging/hdMtlx/hdMtlx.h"
8+
#include "pxr/imaging/hdMtlx/combinedMtlxVersion.h"
89
#include "pxr/imaging/hdMtlx/debugCodes.h"
910
#include "pxr/imaging/hdMtlx/tokens.h"
1011
#include "pxr/imaging/hd/material.h"
@@ -275,7 +276,7 @@ HdMtlxGetNodeDefName(std::string const& prevMxNodeDefName)
275276
{
276277
std::string mxNodeDefName = prevMxNodeDefName;
277278
// For nodeDef name changes between MaterialX v1.38 and the current version
278-
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION >= 39
279+
#if MTLX_COMBINED_VERSION >= 13900
279280
// The normalmap nodeDef name changed in v1.39
280281
if (prevMxNodeDefName == "ND_normalmap") {
281282
mxNodeDefName = "ND_normalmap_float";
@@ -326,7 +327,7 @@ HdMtlxGetNodeDef(TfToken const& hdNodeType, mx::DocumentPtr const& mxDoc)
326327
const std::string nodeTypeStr = hdNodeType.GetString();
327328

328329
// For node removals between MaterialX v1.38 and the current version
329-
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION >= 39
330+
#if MTLX_COMBINED_VERSION >= 13900
330331
// Swizzle nodes were deleted in v1.39, return a temporary NodeDef
331332
std::smatch match;
332333
static const auto swizzleRegex = std::regex("ND_swizzle_([^_]+)_([^_]+)");

pxr/imaging/hdSt/materialXFilter.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "pxr/imaging/hdSt/materialXShaderGen.h"
1111
#include "pxr/imaging/hdSt/package.h"
1212
#include "pxr/imaging/hdSt/resourceRegistry.h"
13+
#include "pxr/imaging/hdMtlx/combinedMtlxVersion.h"
1314
#include "pxr/imaging/hdMtlx/hdMtlx.h"
1415
#include "pxr/imaging/hdMtlx/tokens.h"
1516
#include "pxr/imaging/hgi/tokens.h"
@@ -626,7 +627,7 @@ _GetGlTFSurfaceMaterialTag(HdMaterialNode2 const& terminal)
626627
static const mx::TypeDesc
627628
_GetMxTypeDescription(std::string const& typeName)
628629
{
629-
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 38
630+
#if MTLX_COMBINED_VERSION < 13900
630631
using MxTypeDesc = const mx::TypeDesc*;
631632
#else
632633
using MxTypeDesc = const mx::TypeDesc;
@@ -646,14 +647,14 @@ _GetMxTypeDescription(std::string const& typeName)
646647

647648
const auto typeDescIt = _typeLibrary.find(typeName);
648649
if (typeDescIt != _typeLibrary.end()) {
649-
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 38
650+
#if MTLX_COMBINED_VERSION < 13900
650651
return *typeDescIt->second;
651652
#else
652653
return typeDescIt->second;
653654
#endif
654655
}
655656

656-
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 38
657+
#if MTLX_COMBINED_VERSION < 13900
657658
return *mx::Type::NONE;
658659
#else
659660
return mx::Type::NONE;

pxr/imaging/hdSt/materialXShaderGen.cpp

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
#include "pxr/imaging/hdSt/materialXShaderGen.h"
88
#include "pxr/imaging/hdSt/materialXFilter.h"
9+
#include "pxr/imaging/hdMtlx/combinedMtlxVersion.h"
910
#include "pxr/base/tf/stringUtils.h"
1011

1112
#include <MaterialXCore/Value.h>
@@ -263,15 +264,6 @@ HdStMaterialXShaderGen<Base>::_EmitMxSurfaceShader(
263264
// closure/shader nodes and need to be emitted first.
264265
Base::emitFunctionCalls(mxGraph, mxContext, mxStage,
265266
mx::ShaderNode::Classification::TEXTURE);
266-
267-
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION == 38 && \
268-
MATERIALX_BUILD_VERSION <= 4
269-
// Emit function calls for all surface shader nodes.
270-
// These will internally emit their closure function calls.
271-
Base::emitFunctionCalls(mxGraph, mxContext, mxStage,
272-
mx::ShaderNode::Classification::SHADER |
273-
mx::ShaderNode::Classification::SURFACE);
274-
#else
275267
// Emit function calls for "root" closure/shader nodes.
276268
// These will internally emit function calls for any dependent
277269
// closure nodes upstream.
@@ -289,7 +281,6 @@ HdStMaterialXShaderGen<Base>::_EmitMxSurfaceShader(
289281
}
290282
}
291283
}
292-
#endif
293284
}
294285
else {
295286
// No surface shader graph so just generate all
@@ -303,7 +294,7 @@ HdStMaterialXShaderGen<Base>::_EmitMxSurfaceShader(
303294
if (outputConnection) {
304295

305296
std::string finalOutput = outputConnection->getVariable();
306-
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 38
297+
#if MTLX_COMBINED_VERSION < 13900
307298
// channels feature removed in MaterialX 1.39
308299
const std::string& channels = outputSocket->getChannels();
309300
if (!channels.empty()) {
@@ -312,7 +303,6 @@ HdStMaterialXShaderGen<Base>::_EmitMxSurfaceShader(
312303
channels, outputSocket->getType());
313304
}
314305
#endif
315-
316306
if (mxGraph.hasClassification(
317307
mx::ShaderNode::Classification::SURFACE)) {
318308
if (mxContext.getOptions().hwTransparency) {
@@ -840,19 +830,6 @@ HdStMaterialXShaderGen<Base>::_EmitDataStructsAndFunctionDefinitions(
840830
mxContext, mxStage);
841831
}
842832

843-
// Prior to MaterialX 1.38.5 the token substitutions need to
844-
// include the full path to the .glsl files, so we prepend that
845-
// here.
846-
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION == 38
847-
#if MATERIALX_BUILD_VERSION < 4
848-
(*tokenSubstitutions)[mx::ShaderGenerator::T_FILE_TRANSFORM_UV].insert(
849-
0, "stdlib/" + Base::TARGET + "/lib/");
850-
#elif MATERIALX_BUILD_VERSION == 4
851-
(*tokenSubstitutions)[mx::ShaderGenerator::T_FILE_TRANSFORM_UV].insert(
852-
0, "libraries/stdlib/" + Base::TARGET + "/lib/");
853-
#endif
854-
#endif
855-
856833
// Add light sampling functions
857834
Base::emitLightFunctionDefinitions(mxGraph, mxContext, mxStage);
858835

@@ -895,9 +872,7 @@ namespace {
895872
template<>
896873
HdStMaterialXShaderGen<mx::GlslShaderGenerator>::HdStMaterialXShaderGen(
897874
HdSt_MxShaderGenInfo const& mxHdInfo)
898-
#if (MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 38) || \
899-
(MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 39 && \
900-
MATERIALX_BUILD_VERSION <= 2)
875+
#if MTLX_COMBINED_VERSION <= 13902
901876
: mx::GlslShaderGenerator(),
902877
#else
903878
: mx::GlslShaderGenerator(mx::TypeSystem::create()),
@@ -1052,9 +1027,7 @@ namespace {
10521027
template<>
10531028
HdStMaterialXShaderGen<mx::VkShaderGenerator>::HdStMaterialXShaderGen(
10541029
HdSt_MxShaderGenInfo const& mxHdInfo)
1055-
#if (MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 38) || \
1056-
(MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 39 && \
1057-
MATERIALX_BUILD_VERSION <= 2)
1030+
#if MTLX_COMBINED_VERSION <= 13902
10581031
: mx::VkShaderGenerator(),
10591032
#else
10601033
: mx::VkShaderGenerator(mx::TypeSystem::create()),
@@ -1208,9 +1181,7 @@ namespace {
12081181
template<>
12091182
HdStMaterialXShaderGen<mx::MslShaderGenerator>::HdStMaterialXShaderGen(
12101183
HdSt_MxShaderGenInfo const& mxHdInfo)
1211-
#if (MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 38) || \
1212-
(MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 39 && \
1213-
MATERIALX_BUILD_VERSION <= 2)
1184+
#if MTLX_COMBINED_VERSION <= 13902
12141185
: mx::MslShaderGenerator(),
12151186
#else
12161187
: mx::MslShaderGenerator(mx::TypeSystem::create()),
@@ -1398,7 +1369,7 @@ HdStMaterialXShaderGenMsl::_EmitMxFunctions(
13981369
bool
13991370
HdStMaterialXHelpers::MxTypeIsNone(mx::TypeDesc typeDesc)
14001371
{
1401-
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 38
1372+
#if MTLX_COMBINED_VERSION < 13900
14021373
return typeDesc == *mx::Type::NONE;
14031374
#else
14041375
return typeDesc == mx::Type::NONE;
@@ -1408,7 +1379,7 @@ HdStMaterialXHelpers::MxTypeIsNone(mx::TypeDesc typeDesc)
14081379
bool
14091380
HdStMaterialXHelpers::MxTypeIsSurfaceShader(mx::TypeDesc typeDesc)
14101381
{
1411-
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 38
1382+
#if MTLX_COMBINED_VERSION < 13900
14121383
return typeDesc == *mx::Type::SURFACESHADER;
14131384
#else
14141385
return typeDesc == mx::Type::SURFACESHADER;
@@ -1418,7 +1389,7 @@ HdStMaterialXHelpers::MxTypeIsSurfaceShader(mx::TypeDesc typeDesc)
14181389
bool
14191390
HdStMaterialXHelpers::MxTypeDescIsFilename(const mx::TypeDesc typeDesc)
14201391
{
1421-
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 38
1392+
#if MTLX_COMBINED_VERSION < 13900
14221393
return typeDesc == *mx::Type::FILENAME;
14231394
#else
14241395
return typeDesc == mx::Type::FILENAME;
@@ -1428,7 +1399,7 @@ HdStMaterialXHelpers::MxTypeDescIsFilename(const mx::TypeDesc typeDesc)
14281399
const mx::TypeDesc
14291400
HdStMaterialXHelpers::GetMxTypeDesc(const mx::ShaderPort* port)
14301401
{
1431-
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 38
1402+
#if MTLX_COMBINED_VERSION < 13900
14321403
return port->getType() ? *(port->getType()) : *mx::Type::NONE;
14331404
#else
14341405
return port->getType();
@@ -1441,13 +1412,12 @@ HdStMaterialXHelpers::MxGetTypeString(
14411412
const mx::GenContext& mxContext,
14421413
const std::string& typeName)
14431414
{
1444-
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 38
1415+
#if MTLX_COMBINED_VERSION < 13900
14451416
const mx::TypeDesc* mxType = mx::TypeDesc::get(typeName);
14461417
if (!mxType) {
14471418
return mx::Type::NONE->getName();
14481419
}
1449-
#elif MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION == 39 && \
1450-
MATERIALX_BUILD_VERSION <=2
1420+
#elif MTLX_COMBINED_VERSION <= 13902
14511421
const mx::TypeDesc mxType = mx::TypeDesc::get(typeName);
14521422
#else
14531423
const mx::TypeDesc mxType = mxContext.getTypeDesc(typeName);
@@ -1458,7 +1428,7 @@ HdStMaterialXHelpers::MxGetTypeString(
14581428
const std::string&
14591429
HdStMaterialXHelpers::GetVector2Name()
14601430
{
1461-
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 38
1431+
#if MTLX_COMBINED_VERSION < 13900
14621432
return mx::Type::VECTOR2->getName();
14631433
#else
14641434
return mx::Type::VECTOR2.getName();

pxr/usdImaging/bin/usdBakeMtlx/bakeMaterialX.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "pxr/usd/usdShade/shader.h"
2121

2222
#include <MaterialXCore/Document.h>
23-
#include <MaterialXCore/Generated.h>
2423
#include <MaterialXCore/Node.h>
2524
#include <MaterialXFormat/Util.h>
2625
#include <MaterialXFormat/XmlIo.h>
@@ -128,14 +127,8 @@ void _BakeMtlxDocument(
128127
: mx::Image::BaseType::UINT8;
129128

130129
// Construct a Texture Baker.
131-
#if MATERIALX_MAJOR_VERSION <= 1 && MATERIALX_MINOR_VERSION <= 38 && \
132-
MATERIALX_BUILD_VERSION <= 6
133-
mx::TextureBakerPtr baker = mx::TextureBaker::create(
134-
textureWidth, textureHeight, baseType);
135-
#else
136130
mx::TextureBakerPtr baker = mx::TextureBakerGlsl::create(
137131
textureWidth, textureHeight, baseType);
138-
#endif
139132
baker->setupUnitSystem(stdLibraries);
140133
baker->setAverageImages(bakeAverage);
141134

third_party/renderman-26/plugin/hdPrman/matfiltMaterialX.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@
3030
#include "pxr/imaging/hdMtlx/hdMtlx.h"
3131
#endif
3232

33+
#include "pxr/pxr.h"
34+
#if PXR_VERSION >= 2511
35+
#include "pxr/imaging/hdMtlx/combinedMtlxVersion.h"
36+
#else
37+
#include <MaterialXCore/Generated.h>
38+
#define MTLX_COMBINED_VERSION \
39+
((MATERIALX_MAJOR_VERSION * 100 * 100) + (MATERIALX_MINOR_VERSION * 100) \
40+
+ MATERIALX_BUILD_VERSION)
41+
#endif
42+
3343
#include <MaterialXCore/Node.h>
3444
#include <MaterialXCore/Document.h>
3545
#include <MaterialXFormat/Environ.h>
@@ -424,9 +434,7 @@ _CompileOslSource(
424434
// Include the filepath to the MaterialX OSL directory containing mx_funcs.h
425435
std::vector<std::string> oslArgs;
426436
oslArgs.reserve(searchPaths.size());
427-
#if MATERIALX_MAJOR_VERSION == 1 && \
428-
MATERIALX_MINOR_VERSION == 38 && \
429-
MATERIALX_BUILD_VERSION == 3
437+
#if MTLX_COMBINED_VERSION == 13803
430438
static const mx::FilePath stdlibOslPath = "stdlib/osl";
431439
#else
432440
// MaterialX v1.38.4 restructured the OSL files and moved mx_funcs.h
@@ -438,11 +446,7 @@ _CompileOslSource(
438446
: "-I\"" + path.asString() + "\"");
439447
}
440448

441-
#if MATERIALX_MAJOR_VERSION == 1 && \
442-
MATERIALX_MINOR_VERSION == 38 && \
443-
MATERIALX_BUILD_VERSION == 3
444-
// Nothing
445-
#else
449+
#if MTLX_COMBINED_VERSION >= 13804
446450
// MaterialX 1.38.4 removed its copy of stdosl.h and other OSL headers
447451
// and requires it to be included from the OSL installation itself.
448452
oslArgs.push_back(std::string("-I\"") + TfGetenv("RMANTREE") + "lib/osl\"");
@@ -875,7 +879,7 @@ _NodeHasTextureCoordPrimvar(
875879
// for texture coordinates.
876880
auto geompropvalueNodes = nodegraph->getNodes(_tokens->geompropvalue);
877881
for (const mx::NodePtr& mxGeomPropNode : geompropvalueNodes) {
878-
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION <= 38
882+
#if MTLX_COMBINED_VERSION < 13900
879883
if (mxGeomPropNode->getType() == mx::Type::VECTOR2->getName()) {
880884
#else
881885
if (mxGeomPropNode->getType() == mx::Type::VECTOR2.getName()) {

0 commit comments

Comments
 (0)