From ac3ca253e643a095e0985264238be28b95109a44 Mon Sep 17 00:00:00 2001 From: Jerry Gamache Date: Tue, 14 Dec 2021 16:18:23 -0500 Subject: [PATCH 1/4] Update to MaterialX v1.38.3 - Use CMake config directly from MaterialX install - Update for API and library changes in v1.38.3 - Strenghten NodeDef discovery This is the last update that is not version tagged in the C++ code. Future updates will be bracketed using MATERIALX_MINOR_VERSION defines introduced in v1.38.3. --- BUILDING.md | 6 +- build_scripts/build_usd.py | 35 ++++- cmake/modules/FindMaterialX.cmake | 139 ------------------ pxr/imaging/hdMtlx/CMakeLists.txt | 6 +- pxr/imaging/hdMtlx/hdMtlx.h | 6 +- pxr/imaging/hdSt/CMakeLists.txt | 10 +- pxr/imaging/hdSt/materialXFilter.cpp | 4 +- pxr/imaging/hdSt/materialXShaderGen.cpp | 15 +- pxr/usd/usdMtlx/CMakeLists.txt | 6 +- pxr/usd/usdMtlx/reader.cpp | 19 ++- pxr/usd/usdMtlx/utils.cpp | 2 +- pxr/usdImaging/bin/usdBakeMtlx/CMakeLists.txt | 5 +- .../bin/usdBakeMtlx/bakeMaterialX.h | 6 +- .../plugin/hdPrman/CMakeLists.txt | 10 +- 14 files changed, 90 insertions(+), 179 deletions(-) delete mode 100644 cmake/modules/FindMaterialX.cmake diff --git a/BUILDING.md b/BUILDING.md index d62efeb53b..7105213ff8 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -273,9 +273,9 @@ Linux and MacOS. The additional dependencies that must be supplied when invoking cmake are: -| Dependency Name | Description | -| ------------------ |---------------------------------------- | -| MATERIALX_ROOT | The root path to a MaterialX SDK install.| +| Dependency Name | Description | +| ------------------ |----------------------------------------------------------- | +| MaterialX_DIR | Path to the CMake package config of a MaterialX SDK install.| See [3rd Party Library and Application Versions](VERSIONS.md) for version information. diff --git a/build_scripts/build_usd.py b/build_scripts/build_usd.py index 7c27a2aa33..80115c9482 100644 --- a/build_scripts/build_usd.py +++ b/build_scripts/build_usd.py @@ -1349,20 +1349,43 @@ def InstallDraco(context, force, buildArgs): ############################################################ # MaterialX -MATERIALX_URL = "https://github.com/materialx/MaterialX/archive/v1.38.0.zip" +MATERIALX_URL = "https://github.com/materialx/MaterialX/archive/v1.38.3.zip" def InstallMaterialX(context, force, buildArgs): with CurrentWorkingDirectory(DownloadURL(MATERIALX_URL, context, force)): - # USD requires MaterialX to be built as a shared library on Linux and MacOS - # Currently MaterialX does not support shared builds on Windows - cmakeOptions = [] - if Linux() or MacOS(): - cmakeOptions += ['-DMATERIALX_BUILD_SHARED_LIBS=ON'] + cmakeOptions = ['-DMATERIALX_BUILD_SHARED_LIBS=ON'] cmakeOptions += buildArgs; + if "v1.38.3.zip" in MATERIALX_URL: + # This will be fixed in v1.38.4: + PatchFile(os.path.join('cmake', 'modules', 'MaterialXConfig.cmake.in'), + [('set_and_check(MATERIALX_PYTHON_DIR "@PACKAGE_CMAKE_INSTALL_PREFIX@/python")\n', + 'IF (@MATERIALX_BUILD_PYTHON@ AND @MATERIALX_INSTALL_PYTHON@)\n' + ' set_and_check(MATERIALX_PYTHON_DIR "@PACKAGE_CMAKE_INSTALL_PREFIX@/python")\n' + 'ENDIF()\n')], + multiLineMatches=True) + RunCMake(context, force, cmakeOptions) + if "v1.38.3.zip" in MATERIALX_URL: + # This will be fixed in v1.38.4: + # Post-build fix. Make sure MaterialX config is relocatable: + filename = os.path.join(context.instDir, 'lib', 'cmake', 'MaterialX', 'MaterialXTargets.cmake') + oldLines = open(filename, 'r').readlines() + newLines = [] + for s in oldLines: + if s.startswith('set(_IMPORT_PREFIX '): + pathBegin = s.find(' ') + 1 + pathEnd = s.rfind(')') + s = s[:pathBegin] + '${PACKAGE_PREFIX_DIR}' + s[pathEnd:] + newLines.append(s) + if newLines != oldLines: + PrintInfo("Patching file {filename} (original in {oldFilename})..." + .format(filename=filename, oldFilename=filename + ".old")) + shutil.copy(filename, filename + ".old") + open(filename, 'w').writelines(newLines) + MATERIALX = Dependency("MaterialX", InstallMaterialX, "include/MaterialXCore/Library.h") ############################################################ diff --git a/cmake/modules/FindMaterialX.cmake b/cmake/modules/FindMaterialX.cmake deleted file mode 100644 index 3780834143..0000000000 --- a/cmake/modules/FindMaterialX.cmake +++ /dev/null @@ -1,139 +0,0 @@ -# -# Copyright 2018 Pixar -# -# Licensed under the Apache License, Version 2.0 (the "Apache License") -# with the following modification; you may not use this file except in -# compliance with the Apache License and the following modification to it: -# Section 6. Trademarks. is deleted and replaced with: -# -# 6. Trademarks. This License does not grant permission to use the trade -# names, trademarks, service marks, or product names of the Licensor -# and its affiliates, except as required to comply with Section 4(c) of -# the License and to reproduce the content of the NOTICE file. -# -# You may obtain a copy of the Apache License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the Apache License with the above modification is -# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the Apache License for the specific -# language governing permissions and limitations under the Apache License. -# -# - Find MaterialX Development Kit -# -# Finds an installed MaterialX Development Kit -# -# Variables that will be defined: -# MATERIALX_FOUND Defined if MaterialX has been detected -# MATERIALX_BASE_DIR Path to the root of the MaterialX installation -# MATERIALX_INCLUDE_DIRS Path to the MaterialX include directories -# MATERIALX_LIB_DIRS Path to the MaterialX library directories -# MATERIALX_STDLIB_DIR Path to the MaterialX standard library directory -# MATERIALX_LIBRARIES List of MaterialX libraries - -# -# In: -# MATERIALX_ROOT Path to the root of the MaterialX installation -# MATERIALX_DATA_ROOT Path where MaterialX data files (libraries and -# resources) are installed, if different from -# MATERIALX_ROOT -# -# Out: -# MATERIALX_FOUND -# MATERIALX_INCLUDE_DIRS -# MATERIALX_LIB_DIRS -# MATERIALX_LIBRARIES - -find_path(MATERIALX_BASE_DIR - NAMES - include/MaterialXCore/Library.h - HINTS - "${MATERIALX_ROOT}" - "$ENV{MATERIALX_ROOT}" - ) - -find_path(MATERIALX_INCLUDE_DIRS - MaterialXCore/Library.h - HINTS - "${MATERIALX_ROOT}" - "$ENV{MATERIALX_ROOT}" - "${MATERIALX_BASE_DIR}" - PATH_SUFFIXES - include - DOC - "MaterialX Header Path" -) - -if (WIN32) - set(MATERIALX_CORE_STATIC_LIB_NAME MaterialXCore.lib) - set(MATERIALX_CORE_DYNAMIC_LIB_NAME MaterialXCore.dll) -else() - set(MATERIALX_CORE_STATIC_LIB_NAME libMaterialXCore.a) - if (APPLE) - set(MATERIALX_CORE_DYNAMIC_LIB_NAME libMaterialXCore.dylib) - else() - set(MATERIALX_CORE_DYNAMIC_LIB_NAME libMaterialXCore.so) - endif() -endif() - -find_path(MATERIALX_LIB_DIRS - "${MATERIALX_CORE_STATIC_LIB_NAME}" - "${MATERIALX_CORE_DYNAMIC_LIB_NAME}" - HINTS - "${MATERIALX_ROOT}" - "$ENV{MATERIALX_ROOT}" - "${MATERIALX_BASE_DIR}" - PATH_SUFFIXES - lib - DOC - "MaterialX Library Path" -) - -find_path(MATERIALX_STDLIB_DIR - stdlib/stdlib_defs.mtlx - HINTS - "${MATERIALX_ROOT}" - "$ENV{MATERIALX_ROOT}" - "${MATERIALX_BASE_DIR}" - "${MATERIALX_DATA_ROOT}" - PATH_SUFFIXES - libraries - DOC - "MaterialX Standard Libraries Path" -) - -foreach(MATERIALX_LIB - Core - Format - GenGlsl - GenOsl - GenShader - Render - RenderGlsl - RenderHw - RenderOsl - ) - find_library(MATERIALX_${MATERIALX_LIB}_LIBRARY - MaterialX${MATERIALX_LIB} - HINTS - "${MATERIALX_LIB_DIRS}" - DOC - "MaterialX's ${MATERIALX_LIB} library path" - NO_CMAKE_SYSTEM_PATH - ) - - if (MATERIALX_${MATERIALX_LIB}_LIBRARY) - list(APPEND MATERIALX_LIBRARIES ${MATERIALX_${MATERIALX_LIB}_LIBRARY}) - endif () -endforeach() - -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(MaterialX - REQUIRED_VARS - MATERIALX_BASE_DIR - MATERIALX_INCLUDE_DIRS - MATERIALX_LIB_DIRS - MATERIALX_STDLIB_DIR -) diff --git a/pxr/imaging/hdMtlx/CMakeLists.txt b/pxr/imaging/hdMtlx/CMakeLists.txt index 37fce0b0a2..1947b42f13 100644 --- a/pxr/imaging/hdMtlx/CMakeLists.txt +++ b/pxr/imaging/hdMtlx/CMakeLists.txt @@ -11,10 +11,8 @@ pxr_library(hdMtlx trace usdMtlx vt - ${MATERIALX_LIBRARIES} - - INCLUDE_DIRS - ${MATERIALX_INCLUDE_DIRS} + MaterialXCore + MaterialXFormat PUBLIC_CLASSES hdMtlx diff --git a/pxr/imaging/hdMtlx/hdMtlx.h b/pxr/imaging/hdMtlx/hdMtlx.h index ca2281f03a..552d039f9d 100644 --- a/pxr/imaging/hdMtlx/hdMtlx.h +++ b/pxr/imaging/hdMtlx/hdMtlx.h @@ -31,11 +31,13 @@ #include #include -namespace MaterialX { +#include + +MATERIALX_NAMESPACE_BEGIN class FileSearchPath; using DocumentPtr = std::shared_ptr; using StringMap = std::unordered_map; -} +MATERIALX_NAMESPACE_END PXR_NAMESPACE_OPEN_SCOPE diff --git a/pxr/imaging/hdSt/CMakeLists.txt b/pxr/imaging/hdSt/CMakeLists.txt index 2fde872ba0..8e40f6bbd0 100644 --- a/pxr/imaging/hdSt/CMakeLists.txt +++ b/pxr/imaging/hdSt/CMakeLists.txt @@ -14,8 +14,14 @@ set(optionalIncludeDirs "") set(optionalPublicClasses "") set(optionalPrivateClasses "") if (${PXR_ENABLE_MATERIALX_SUPPORT}) - list(APPEND optionalLibs ${MATERIALX_LIBRARIES} hdMtlx) - list(APPEND optionalIncludeDirs ${MATERIALX_INCLUDE_DIRS}) + list(APPEND optionalLibs + MaterialXGenShader + MaterialXRender + MaterialXCore + MaterialXFormat + MaterialXGenGlsl + hdMtlx + ) list(APPEND optionalPrivateClasses materialXFilter materialXShaderGen diff --git a/pxr/imaging/hdSt/materialXFilter.cpp b/pxr/imaging/hdSt/materialXFilter.cpp index 7b18ca7fcc..788e774133 100644 --- a/pxr/imaging/hdSt/materialXFilter.cpp +++ b/pxr/imaging/hdSt/materialXFilter.cpp @@ -68,8 +68,8 @@ TF_DEFINE_PRIVATE_TOKENS( static mx::ShaderPtr _GenMaterialXShader(mx::GenContext & mxContext, mx::ElementPtr const& mxElem) { - bool hasTransparency = mx::isTransparentSurface(mxElem, - mxContext.getShaderGenerator()); + bool hasTransparency = mx::isTransparentSurface(mxElem, + mxContext.getShaderGenerator().getTarget()); mx::GenContext materialContext = mxContext; materialContext.getOptions().hwTransparency = hasTransparency; diff --git a/pxr/imaging/hdSt/materialXShaderGen.cpp b/pxr/imaging/hdSt/materialXShaderGen.cpp index c8232bd0b9..85f6eb9531 100644 --- a/pxr/imaging/hdSt/materialXShaderGen.cpp +++ b/pxr/imaging/hdSt/materialXShaderGen.cpp @@ -273,8 +273,8 @@ HdStMaterialXShaderGen::_EmitMxFunctions( mx::ShaderStage& mxStage) const { // Add global constants and type definitions - emitInclude("pbrlib/" + mx::GlslShaderGenerator::TARGET - + "/lib/mx_defines.glsl", mxContext, mxStage); + emitInclude("stdlib/" + mx::GlslShaderGenerator::TARGET + + "/lib/mx_math.glsl", mxContext, mxStage); emitLine("#if NUM_LIGHTS > 0", mxStage, false); emitLine("#define MAX_LIGHT_SOURCES NUM_LIGHTS", mxStage, false); emitLine("#else", mxStage, false); @@ -386,11 +386,6 @@ HdStMaterialXShaderGen::_EmitMxFunctions( _EmitMxInitFunction(vertexData, mxStage); } - // Emit common math functions - emitInclude("pbrlib/" + mx::GlslShaderGenerator::TARGET - + "/lib/mx_math.glsl", mxContext, mxStage); - emitLineBreak(mxStage); - // Emit lighting and shadowing code if (lighting) { emitSpecularEnvironment(mxContext, mxStage); @@ -427,6 +422,9 @@ HdStMaterialXShaderGen::_EmitMxFunctions( emitInclude(ShaderGenerator::T_FILE_TRANSFORM_UV, mxContext, mxStage); } + // Add light sampling functions + emitLightFunctionDefinitions(mxGraph, mxContext, mxStage); + // Add all functions for node implementations emitFunctionDefinitions(mxGraph, mxContext, mxStage); } @@ -448,7 +446,8 @@ HdStMaterialXShaderGen::_EmitMxSurfaceShader( emitLine("mxInit(Peye, Neye)", mxStage); const mx::ShaderGraphOutputSocket* outputSocket = mxGraph.getOutputSocket(); - if (mxGraph.hasClassification(mx::ShaderNode::Classification::CLOSURE)) { + if (mxGraph.hasClassification(mx::ShaderNode::Classification::CLOSURE) && + !mxGraph.hasClassification(mx::ShaderNode::Classification::SHADER)) { // Handle the case where the mxGraph is a direct closure. // We don't support rendering closures without attaching // to a surface shader, so just output black. diff --git a/pxr/usd/usdMtlx/CMakeLists.txt b/pxr/usd/usdMtlx/CMakeLists.txt index c4504abcd2..5d919832bc 100644 --- a/pxr/usd/usdMtlx/CMakeLists.txt +++ b/pxr/usd/usdMtlx/CMakeLists.txt @@ -19,10 +19,8 @@ pxr_library(usdMtlx usdShade usdUI usdUtils - ${MATERIALX_LIBRARIES} - - INCLUDE_DIRS - ${MATERIALX_INCLUDE_DIRS} + MaterialXCore + MaterialXFormat CPPFILES debugCodes.cpp diff --git a/pxr/usd/usdMtlx/reader.cpp b/pxr/usd/usdMtlx/reader.cpp index d99b7d0dbe..c5cbd1793c 100644 --- a/pxr/usd/usdMtlx/reader.cpp +++ b/pxr/usd/usdMtlx/reader.cpp @@ -288,6 +288,7 @@ _FindMatchingNodeDef( const mx::ConstDocumentPtr& mtlxDocument, const mx::ConstInterfaceElementPtr& mtlxInterface, const std::string& family, + const std::string& type, const NdrVersion& version, const std::string& target) { @@ -300,7 +301,11 @@ _FindMatchingNodeDef( } // Filter by types. - if (mtlxInterface && !mtlxInterface->isTypeCompatible(mtlxNodeDef)) { + if (mtlxInterface && !mtlxInterface->hasExactInputMatch(mtlxNodeDef)) { + continue; + } + + if (mtlxNodeDef->getType() != type) { continue; } @@ -347,6 +352,7 @@ _FindMatchingNodeDef( auto nodeDef = _FindMatchingNodeDef(mtlxShaderNode->getDocument(), mtlxInterface, mtlxShaderNode->getCategory(), + mtlxShaderNode->getType(), UsdMtlxGetVersion(mtlxShaderNode), mtlxShaderNode->getTarget()); if (nodeDef) { @@ -355,9 +361,18 @@ _FindMatchingNodeDef( // Get the standard library document and check that. static auto standardLibraryDocument = UsdMtlxGetDocument(""); + + if (mtlxShaderNode->hasNodeDefString()) { + nodeDef = standardLibraryDocument->getNodeDef(mtlxShaderNode->getNodeDefString()); + if (nodeDef) { + return nodeDef; + } + } + return _FindMatchingNodeDef(standardLibraryDocument, mtlxInterface, mtlxShaderNode->getCategory(), + mtlxShaderNode->getType(), UsdMtlxGetVersion(mtlxShaderNode), mtlxShaderNode->getTarget()); } @@ -1316,7 +1331,7 @@ _Context::AddShaderNode(const mx::ConstNodePtr& mtlxShaderNode) // Get the nodeDef for this shaderNode. mx::ConstNodeDefPtr mtlxNodeDef = mtlxShaderNode->getNodeDef(); - if (mtlxShaderNode->getNodeDefString().empty()) { + if (!mtlxNodeDef) { // The shaderNode specified a node instead of a nodeDef. Find // the best matching nodedef since the MaterialX API doesn't. mtlxNodeDef = diff --git a/pxr/usd/usdMtlx/utils.cpp b/pxr/usd/usdMtlx/utils.cpp index 943494e3c1..9701fd710f 100644 --- a/pxr/usd/usdMtlx/utils.cpp +++ b/pxr/usd/usdMtlx/utils.cpp @@ -311,7 +311,7 @@ _ReadFromAsset(mx::DocumentPtr doc, const ArResolvedPath& resolvedPath, newReadOptions); }; - mx::readFromXmlString(doc, s, &readOptions); + mx::readFromXmlString(doc, s, searchPath, &readOptions); } #endif diff --git a/pxr/usdImaging/bin/usdBakeMtlx/CMakeLists.txt b/pxr/usdImaging/bin/usdBakeMtlx/CMakeLists.txt index 2b60220580..96466929a7 100644 --- a/pxr/usdImaging/bin/usdBakeMtlx/CMakeLists.txt +++ b/pxr/usdImaging/bin/usdBakeMtlx/CMakeLists.txt @@ -15,11 +15,12 @@ pxr_library(usdBakeMtlx hd hdMtlx usdImaging - ${MATERIALX_LIBRARIES} + MaterialXCore + MaterialXFormat + MaterialXRenderGlsl INCLUDE_DIRS ${Boost_INCLUDE_DIRS} - ${MATERIALX_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS} PUBLIC_CLASSES diff --git a/pxr/usdImaging/bin/usdBakeMtlx/bakeMaterialX.h b/pxr/usdImaging/bin/usdBakeMtlx/bakeMaterialX.h index c14437bf7e..201fb4b419 100644 --- a/pxr/usdImaging/bin/usdBakeMtlx/bakeMaterialX.h +++ b/pxr/usdImaging/bin/usdBakeMtlx/bakeMaterialX.h @@ -30,13 +30,15 @@ #include "pxr/imaging/hd/material.h" #include "pxr/usd/usdShade/material.h" +#include + #include "pxr/base/tf/declarePtrs.h" #include -namespace MaterialX { +MATERIALX_NAMESPACE_BEGIN using FilePathVec = std::vector; class FileSearchPath; -} +MATERIALX_NAMESPACE_END PXR_NAMESPACE_OPEN_SCOPE diff --git a/third_party/renderman-24/plugin/hdPrman/CMakeLists.txt b/third_party/renderman-24/plugin/hdPrman/CMakeLists.txt index 6424e4113d..dee21d5a5f 100644 --- a/third_party/renderman-24/plugin/hdPrman/CMakeLists.txt +++ b/third_party/renderman-24/plugin/hdPrman/CMakeLists.txt @@ -22,8 +22,14 @@ endif() set(optionalIncludeDirs "") set(optionalPublicClasses "") if (${PXR_ENABLE_MATERIALX_SUPPORT}) - list(APPEND optionalLibs ${MATERIALX_LIBRARIES} hdMtlx) - list(APPEND optionalIncludeDirs ${MATERIALX_INCLUDE_DIRS}) + list(APPEND optionalLibs + MaterialXCore + MaterialXFormat + MaterialXGenShader + MaterialXGenOsl + MaterialXRender + hdMtlx + ) list(APPEND optionalPublicClasses matfiltMaterialX) if (${PXR_ENABLE_OSL_SUPPORT}) From 034df39a78a5fc4cada49b545ad2a87fd6666e1d Mon Sep 17 00:00:00 2001 From: Sean Donnelly <23455376+seando-adsk@users.noreply.github.com> Date: Tue, 19 Apr 2022 14:21:01 -0400 Subject: [PATCH 2/4] Update to MaterialX v1.38.4 --- build_scripts/build_usd.py | 29 +----------------- pxr/imaging/hdSt/materialXFilter.cpp | 15 ++++++++-- pxr/imaging/hdSt/materialXShaderGen.cpp | 30 +++++++++++++++---- .../plugin/hdPrman/matfiltMaterialX.cpp | 12 +++++++- 4 files changed, 50 insertions(+), 36 deletions(-) diff --git a/build_scripts/build_usd.py b/build_scripts/build_usd.py index 80115c9482..6421f9b3fd 100644 --- a/build_scripts/build_usd.py +++ b/build_scripts/build_usd.py @@ -1349,7 +1349,7 @@ def InstallDraco(context, force, buildArgs): ############################################################ # MaterialX -MATERIALX_URL = "https://github.com/materialx/MaterialX/archive/v1.38.3.zip" +MATERIALX_URL = "https://github.com/materialx/MaterialX/archive/v1.38.4.zip" def InstallMaterialX(context, force, buildArgs): with CurrentWorkingDirectory(DownloadURL(MATERIALX_URL, context, force)): @@ -1357,35 +1357,8 @@ def InstallMaterialX(context, force, buildArgs): cmakeOptions += buildArgs; - if "v1.38.3.zip" in MATERIALX_URL: - # This will be fixed in v1.38.4: - PatchFile(os.path.join('cmake', 'modules', 'MaterialXConfig.cmake.in'), - [('set_and_check(MATERIALX_PYTHON_DIR "@PACKAGE_CMAKE_INSTALL_PREFIX@/python")\n', - 'IF (@MATERIALX_BUILD_PYTHON@ AND @MATERIALX_INSTALL_PYTHON@)\n' - ' set_and_check(MATERIALX_PYTHON_DIR "@PACKAGE_CMAKE_INSTALL_PREFIX@/python")\n' - 'ENDIF()\n')], - multiLineMatches=True) - RunCMake(context, force, cmakeOptions) - if "v1.38.3.zip" in MATERIALX_URL: - # This will be fixed in v1.38.4: - # Post-build fix. Make sure MaterialX config is relocatable: - filename = os.path.join(context.instDir, 'lib', 'cmake', 'MaterialX', 'MaterialXTargets.cmake') - oldLines = open(filename, 'r').readlines() - newLines = [] - for s in oldLines: - if s.startswith('set(_IMPORT_PREFIX '): - pathBegin = s.find(' ') + 1 - pathEnd = s.rfind(')') - s = s[:pathBegin] + '${PACKAGE_PREFIX_DIR}' + s[pathEnd:] - newLines.append(s) - if newLines != oldLines: - PrintInfo("Patching file {filename} (original in {oldFilename})..." - .format(filename=filename, oldFilename=filename + ".old")) - shutil.copy(filename, filename + ".old") - open(filename, 'w').writelines(newLines) - MATERIALX = Dependency("MaterialX", InstallMaterialX, "include/MaterialXCore/Library.h") ############################################################ diff --git a/pxr/imaging/hdSt/materialXFilter.cpp b/pxr/imaging/hdSt/materialXFilter.cpp index 788e774133..0bea302121 100644 --- a/pxr/imaging/hdSt/materialXFilter.cpp +++ b/pxr/imaging/hdSt/materialXFilter.cpp @@ -103,8 +103,19 @@ HdSt_GenMaterialXShader( { // Initialize the Context for shaderGen. mx::GenContext mxContext = HdStMaterialXShaderGen::create(mxHdInfo); - mxContext.registerSourceCodeSearchPath(searchPath); - + + // Starting from MaterialX 1.38.4 at PR 877, we must remove the "libraries" part: + mx::FileSearchPath libSearchPaths; + for (const mx::FilePath &path : searchPath) { + if (path.getBaseName() == "libraries") { + libSearchPaths.append(path.getParentPath()); + } + else { + libSearchPaths.append(path); + } + } + mxContext.registerSourceCodeSearchPath(libSearchPaths); + // Add the Direct Light mtlx file to the mxDoc mx::DocumentPtr lightDoc = mx::createDocument(); mx::readFromXmlString(lightDoc, mxDirectLightString); diff --git a/pxr/imaging/hdSt/materialXShaderGen.cpp b/pxr/imaging/hdSt/materialXShaderGen.cpp index 85f6eb9531..7eff2fde05 100644 --- a/pxr/imaging/hdSt/materialXShaderGen.cpp +++ b/pxr/imaging/hdSt/materialXShaderGen.cpp @@ -273,7 +273,8 @@ HdStMaterialXShaderGen::_EmitMxFunctions( mx::ShaderStage& mxStage) const { // Add global constants and type definitions - emitInclude("stdlib/" + mx::GlslShaderGenerator::TARGET + // Starting from MaterialX 1.38.4 at PR 877, we must add the "libraries" part: + emitInclude("libraries/stdlib/" + mx::GlslShaderGenerator::TARGET + "/lib/mx_math.glsl", mxContext, mxStage); emitLine("#if NUM_LIGHTS > 0", mxStage, false); emitLine("#define MAX_LIGHT_SOURCES NUM_LIGHTS", mxStage, false); @@ -391,7 +392,8 @@ HdStMaterialXShaderGen::_EmitMxFunctions( emitSpecularEnvironment(mxContext, mxStage); } if (shadowing) { - emitInclude("pbrlib/" + mx::GlslShaderGenerator::TARGET + // Starting from MaterialX 1.38.4 at PR 877, we must add the "libraries" part: + emitInclude("libraries/pbrlib/" + mx::GlslShaderGenerator::TARGET + "/lib/mx_shadow.glsl", mxContext, mxStage); } @@ -399,7 +401,8 @@ HdStMaterialXShaderGen::_EmitMxFunctions( if (mxContext.getOptions().hwDirectionalAlbedoMethod == mx::HwDirectionalAlbedoMethod::DIRECTIONAL_ALBEDO_TABLE || mxContext.getOptions().hwWriteAlbedoTable) { - emitInclude("pbrlib/" + mx::GlslShaderGenerator::TARGET + // Starting from MaterialX 1.38.4 at PR 877, we must add the "libraries" part: + emitInclude("libraries/pbrlib/" + mx::GlslShaderGenerator::TARGET + "/lib/mx_table.glsl", mxContext, mxStage); emitLineBreak(mxStage); } @@ -464,8 +467,25 @@ HdStMaterialXShaderGen::_EmitMxSurfaceShader( mxStage); } else { - // Add all function calls - emitFunctionCalls(mxGraph, mxContext, mxStage); + // Surface shaders need special handling. + if (mxGraph.hasClassification(mx::ShaderNode::Classification::SHADER | + mx::ShaderNode::Classification::SURFACE)) + { + // Emit all texturing nodes. These are inputs to any + // closure/shader nodes and need to be emitted first. + emitFunctionCalls(mxGraph, mxContext, mxStage, mx::ShaderNode::Classification::TEXTURE); + + // Emit function calls for all surface shader nodes. + // These will internally emit their closure function calls. + emitFunctionCalls(mxGraph, mxContext, mxStage, mx::ShaderNode::Classification::SHADER | + mx::ShaderNode::Classification::SURFACE); + } + else + { + // No surface shader graph so just generate all + // function calls in order. + emitFunctionCalls(mxGraph, mxContext, mxStage); + } // Emit final output std::string finalOutputReturn = "vec4 mxOut = " ; diff --git a/third_party/renderman-24/plugin/hdPrman/matfiltMaterialX.cpp b/third_party/renderman-24/plugin/hdPrman/matfiltMaterialX.cpp index b1da731902..50001c4c0d 100644 --- a/third_party/renderman-24/plugin/hdPrman/matfiltMaterialX.cpp +++ b/third_party/renderman-24/plugin/hdPrman/matfiltMaterialX.cpp @@ -103,7 +103,17 @@ _GenMaterialXShaderCode( { // Initialize the Context for shaderGen mx::GenContext mxContext = mx::OslShaderGenerator::create(); - mxContext.registerSourceCodeSearchPath(searchPath); + // Starting from MaterialX 1.38.4 at PR 877, we must remove the "libraries" part: + mx::FileSearchPath libSearchPaths; + for (const mx::FilePath &path : searchPath) { + if (path.getBaseName() == "libraries") { + libSearchPaths.append(path.getParentPath()); + } + else { + libSearchPaths.append(path); + } + } + mxContext.registerSourceCodeSearchPath(libSearchPaths); mxContext.getOptions().fileTextureVerticalFlip = false; // Get the Node from the Nodegraph/mxDoc From aaf20de564e29c3e59bcd32b3539add08b7597c6 Mon Sep 17 00:00:00 2001 From: Sean Donnelly <23455376+seando-adsk@users.noreply.github.com> Date: Tue, 19 Apr 2022 16:22:11 -0400 Subject: [PATCH 3/4] Add support back in for MaterialX v1.38.3 --- pxr/imaging/hdSt/materialXFilter.cpp | 4 ++++ pxr/imaging/hdSt/materialXShaderGen.cpp | 19 +++++++++++-------- .../plugin/hdPrman/matfiltMaterialX.cpp | 4 ++++ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/pxr/imaging/hdSt/materialXFilter.cpp b/pxr/imaging/hdSt/materialXFilter.cpp index 0bea302121..3bc8e36c12 100644 --- a/pxr/imaging/hdSt/materialXFilter.cpp +++ b/pxr/imaging/hdSt/materialXFilter.cpp @@ -104,6 +104,9 @@ HdSt_GenMaterialXShader( // Initialize the Context for shaderGen. mx::GenContext mxContext = HdStMaterialXShaderGen::create(mxHdInfo); +#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION == 38 && MATERIALX_BUILD_VERSION == 3 + mxContext.registerSourceCodeSearchPath(searchPath); +#else // Starting from MaterialX 1.38.4 at PR 877, we must remove the "libraries" part: mx::FileSearchPath libSearchPaths; for (const mx::FilePath &path : searchPath) { @@ -115,6 +118,7 @@ HdSt_GenMaterialXShader( } } mxContext.registerSourceCodeSearchPath(libSearchPaths); +#endif // Add the Direct Light mtlx file to the mxDoc mx::DocumentPtr lightDoc = mx::createDocument(); diff --git a/pxr/imaging/hdSt/materialXShaderGen.cpp b/pxr/imaging/hdSt/materialXShaderGen.cpp index 7eff2fde05..69f3abf8f2 100644 --- a/pxr/imaging/hdSt/materialXShaderGen.cpp +++ b/pxr/imaging/hdSt/materialXShaderGen.cpp @@ -272,9 +272,14 @@ HdStMaterialXShaderGen::_EmitMxFunctions( mx::GenContext& mxContext, mx::ShaderStage& mxStage) const { - // Add global constants and type definitions +#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION == 38 && MATERIALX_BUILD_VERSION == 3 + std::string libPath; +#else // Starting from MaterialX 1.38.4 at PR 877, we must add the "libraries" part: - emitInclude("libraries/stdlib/" + mx::GlslShaderGenerator::TARGET + std::string libPath = "libraries/"; +#endif + // Add global constants and type definitions + emitInclude(libPath + "stdlib/" + mx::GlslShaderGenerator::TARGET + "/lib/mx_math.glsl", mxContext, mxStage); emitLine("#if NUM_LIGHTS > 0", mxStage, false); emitLine("#define MAX_LIGHT_SOURCES NUM_LIGHTS", mxStage, false); @@ -392,8 +397,7 @@ HdStMaterialXShaderGen::_EmitMxFunctions( emitSpecularEnvironment(mxContext, mxStage); } if (shadowing) { - // Starting from MaterialX 1.38.4 at PR 877, we must add the "libraries" part: - emitInclude("libraries/pbrlib/" + mx::GlslShaderGenerator::TARGET + emitInclude(libPath + "pbrlib/" + mx::GlslShaderGenerator::TARGET + "/lib/mx_shadow.glsl", mxContext, mxStage); } @@ -401,8 +405,7 @@ HdStMaterialXShaderGen::_EmitMxFunctions( if (mxContext.getOptions().hwDirectionalAlbedoMethod == mx::HwDirectionalAlbedoMethod::DIRECTIONAL_ALBEDO_TABLE || mxContext.getOptions().hwWriteAlbedoTable) { - // Starting from MaterialX 1.38.4 at PR 877, we must add the "libraries" part: - emitInclude("libraries/pbrlib/" + mx::GlslShaderGenerator::TARGET + emitInclude(libPath + "pbrlib/" + mx::GlslShaderGenerator::TARGET + "/lib/mx_table.glsl", mxContext, mxStage); emitLineBreak(mxStage); } @@ -411,12 +414,12 @@ HdStMaterialXShaderGen::_EmitMxFunctions( // depending on the vertical flip flag. if (mxContext.getOptions().fileTextureVerticalFlip) { _tokenSubstitutions[mx::ShaderGenerator::T_FILE_TRANSFORM_UV] = - "stdlib/" + mx::GlslShaderGenerator::TARGET + + libPath + "stdlib/" + mx::GlslShaderGenerator::TARGET + "/lib/mx_transform_uv_vflip.glsl"; } else { _tokenSubstitutions[mx::ShaderGenerator::T_FILE_TRANSFORM_UV] = - "stdlib/" + mx::GlslShaderGenerator::TARGET + + libPath + "stdlib/" + mx::GlslShaderGenerator::TARGET + "/lib/mx_transform_uv.glsl"; } diff --git a/third_party/renderman-24/plugin/hdPrman/matfiltMaterialX.cpp b/third_party/renderman-24/plugin/hdPrman/matfiltMaterialX.cpp index 50001c4c0d..75936f3022 100644 --- a/third_party/renderman-24/plugin/hdPrman/matfiltMaterialX.cpp +++ b/third_party/renderman-24/plugin/hdPrman/matfiltMaterialX.cpp @@ -103,6 +103,9 @@ _GenMaterialXShaderCode( { // Initialize the Context for shaderGen mx::GenContext mxContext = mx::OslShaderGenerator::create(); +#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION == 38 && MATERIALX_BUILD_VERSION == 3 + mxContext.registerSourceCodeSearchPath(searchPath); +#else // Starting from MaterialX 1.38.4 at PR 877, we must remove the "libraries" part: mx::FileSearchPath libSearchPaths; for (const mx::FilePath &path : searchPath) { @@ -114,6 +117,7 @@ _GenMaterialXShaderCode( } } mxContext.registerSourceCodeSearchPath(libSearchPaths); +#endif mxContext.getOptions().fileTextureVerticalFlip = false; // Get the Node from the Nodegraph/mxDoc From e6edb7e8fd74f3f0d5cd981450df0c937a809257 Mon Sep 17 00:00:00 2001 From: Sean Donnelly <23455376+seando-adsk@users.noreply.github.com> Date: Wed, 4 May 2022 13:22:27 -0400 Subject: [PATCH 4/4] Fix incorrect MaterialX cmake variable. --- pxr/usdImaging/usdImagingGL/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pxr/usdImaging/usdImagingGL/CMakeLists.txt b/pxr/usdImaging/usdImagingGL/CMakeLists.txt index fcfe67696e..f7a59c0cb6 100644 --- a/pxr/usdImaging/usdImagingGL/CMakeLists.txt +++ b/pxr/usdImaging/usdImagingGL/CMakeLists.txt @@ -3140,7 +3140,7 @@ pxr_register_test(testUsdImagingGLMaterialTag TESTENV testUsdImagingGLMaterialTag ) -if (MATERIALX_FOUND AND ${PXR_ENABLE_MATERIALX_SUPPORT}) +if (MaterialX_FOUND AND ${PXR_ENABLE_MATERIALX_SUPPORT}) pxr_install_test_dir( SRC testenv/testUsdImagingGLMaterialX DEST testUsdImagingGLMaterialX