Skip to content

Commit

Permalink
Merge pull request #1792 from seando-adsk/adsk/update_to_materialx_1.…
Browse files Browse the repository at this point in the history
…38.3

Update to MaterialX v1.38.3

Also adds support for v1.38.4 and makes it the version built via
build_usd.py.

(Internal change: 2230560)
  • Loading branch information
pixar-oss committed May 16, 2022
2 parents a02f4f3 + e6edb7e commit 02a9843
Show file tree
Hide file tree
Showing 25 changed files with 123 additions and 187 deletions.
6 changes: 3 additions & 3 deletions BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
8 changes: 2 additions & 6 deletions build_scripts/build_usd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1354,15 +1354,11 @@ def InstallDraco(context, force, buildArgs):
############################################################
# MaterialX

MATERIALX_URL = "https:/materialx/MaterialX/archive/v1.38.0.zip"
MATERIALX_URL = "https:/materialx/MaterialX/archive/v1.38.4.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;

Expand Down
139 changes: 0 additions & 139 deletions cmake/modules/FindMaterialX.cmake

This file was deleted.

6 changes: 2 additions & 4 deletions pxr/imaging/hdMtlx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ pxr_library(hdMtlx
trace
usdMtlx
vt
${MATERIALX_LIBRARIES}

INCLUDE_DIRS
${MATERIALX_INCLUDE_DIRS}
MaterialXCore
MaterialXFormat

PUBLIC_CLASSES
hdMtlx
Expand Down
6 changes: 4 additions & 2 deletions pxr/imaging/hdMtlx/hdMtlx.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
#include <set>
#include <unordered_map>

namespace MaterialX {
#include <MaterialXCore/Library.h>

MATERIALX_NAMESPACE_BEGIN
class FileSearchPath;
using DocumentPtr = std::shared_ptr<class Document>;
using StringMap = std::unordered_map<std::string, std::string>;
}
MATERIALX_NAMESPACE_END

PXR_NAMESPACE_OPEN_SCOPE

Expand Down
10 changes: 8 additions & 2 deletions pxr/imaging/hdSt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 18 additions & 3 deletions pxr/imaging/hdSt/materialXFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,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;
Expand Down Expand Up @@ -104,8 +104,23 @@ 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) {
if (path.getBaseName() == "libraries") {
libSearchPaths.append(path.getParentPath());
}
else {
libSearchPaths.append(path);
}
}
mxContext.registerSourceCodeSearchPath(libSearchPaths);
#endif

// Add the Direct Light mtlx file to the mxDoc
mx::DocumentPtr lightDoc = mx::createDocument();
mx::readFromXmlString(lightDoc, mxDirectLightString);
Expand Down
50 changes: 36 additions & 14 deletions pxr/imaging/hdSt/materialXShaderGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,15 @@ HdStMaterialXShaderGen::_EmitMxFunctions(
mx::GenContext& mxContext,
mx::ShaderStage& mxStage) const
{
#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:
std::string libPath = "libraries/";
#endif
// Add global constants and type definitions
emitInclude("pbrlib/" + mx::GlslShaderGenerator::TARGET
+ "/lib/mx_defines.glsl", mxContext, mxStage);
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);
emitLine("#else", mxStage, false);
Expand Down Expand Up @@ -386,25 +392,20 @@ 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);
}
if (shadowing) {
emitInclude("pbrlib/" + mx::GlslShaderGenerator::TARGET
emitInclude(libPath + "pbrlib/" + mx::GlslShaderGenerator::TARGET
+ "/lib/mx_shadow.glsl", mxContext, mxStage);
}

// Emit directional albedo table code.
if (mxContext.getOptions().hwDirectionalAlbedoMethod ==
mx::HwDirectionalAlbedoMethod::DIRECTIONAL_ALBEDO_TABLE ||
mxContext.getOptions().hwWriteAlbedoTable) {
emitInclude("pbrlib/" + mx::GlslShaderGenerator::TARGET
emitInclude(libPath + "pbrlib/" + mx::GlslShaderGenerator::TARGET
+ "/lib/mx_table.glsl", mxContext, mxStage);
emitLineBreak(mxStage);
}
Expand All @@ -413,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";
}

Expand All @@ -427,6 +428,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);
}
Expand All @@ -448,7 +452,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.
Expand All @@ -465,8 +470,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 = " ;
Expand Down
6 changes: 2 additions & 4 deletions pxr/usd/usdMtlx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ pxr_library(usdMtlx
usdShade
usdUI
usdUtils
${MATERIALX_LIBRARIES}

INCLUDE_DIRS
${MATERIALX_INCLUDE_DIRS}
MaterialXCore
MaterialXFormat

CPPFILES
debugCodes.cpp
Expand Down
Loading

0 comments on commit 02a9843

Please sign in to comment.