Skip to content

Commit 1cc5171

Browse files
author
Thor Hjalmarsson
committed
MaterialX on Metal
1 parent 471c89b commit 1cc5171

11 files changed

Lines changed: 613 additions & 146 deletions

File tree

build_scripts/build_usd.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1574,11 +1574,12 @@ def InstallDraco(context, force, buildArgs):
15741574
############################################################
15751575
# MaterialX
15761576

1577-
MATERIALX_URL = "https://github.com/materialx/MaterialX/archive/v1.38.4.zip"
1577+
MATERIALX_URL = "https://github.com/apple/MaterialX/archive/refs/tags/v1.38.6_MetalShadingLaguage.zip"
15781578

15791579
def InstallMaterialX(context, force, buildArgs):
15801580
with CurrentWorkingDirectory(DownloadURL(MATERIALX_URL, context, force)):
15811581
cmakeOptions = ['-DMATERIALX_BUILD_SHARED_LIBS=ON']
1582+
cmakeOptions += ['-DMATERIALX_BUILD_TESTS=OFF']
15821583

15831584
cmakeOptions += buildArgs;
15841585

pxr/imaging/hdSt/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ if (${PXR_ENABLE_MATERIALX_SUPPORT})
2020
MaterialXCore
2121
MaterialXFormat
2222
MaterialXGenGlsl
23+
MaterialXGenMsl
2324
hdMtlx
2425
)
2526
list(APPEND optionalPrivateClasses

pxr/imaging/hdSt/materialXFilter.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "pxr/imaging/hdSt/package.h"
2828
#include "pxr/imaging/hdSt/resourceRegistry.h"
2929
#include "pxr/imaging/hdMtlx/hdMtlx.h"
30+
#include "pxr/imaging/hgi/tokens.h"
3031

3132
#include "pxr/usd/sdr/registry.h"
3233
#include "pxr/imaging/hio/glslfx.h"
@@ -103,16 +104,37 @@ R"(
103104
</materialx>
104105
)";
105106

107+
mx::GenContext createMaterialXContext(
108+
HdSt_MxShaderGenInfo const& mxHdInfo,
109+
std::string const& apiName)
110+
{
111+
if(apiName == HgiTokens->Metal)
112+
return HdStMaterialXMslShaderGen::create(mxHdInfo);
113+
if(apiName == HgiTokens->OpenGL)
114+
return HdStMaterialXGlslShaderGen::create(mxHdInfo);
115+
else
116+
{
117+
TF_CODING_ERROR(
118+
"MaterialX Shader Generator doesn't support %s API.",
119+
apiName.c_str());
120+
//return mx::GenContext();
121+
}
122+
}
123+
106124
// Use the given mxDocument to generate the corresponding glsl shader
107125
// Based on MaterialXViewer Viewer::loadDocument()
108126
mx::ShaderPtr
109127
HdSt_GenMaterialXShader(
110128
mx::DocumentPtr const& mxDoc,
111129
mx::FileSearchPath const& searchPath,
112-
HdSt_MxShaderGenInfo const& mxHdInfo)
130+
HdSt_MxShaderGenInfo const& mxHdInfo,
131+
std::string const& apiName)
113132
{
114133
// Initialize the Context for shaderGen.
115-
mx::GenContext mxContext = HdStMaterialXShaderGen::create(mxHdInfo);
134+
mx::GenContext mxContext = createMaterialXContext(mxHdInfo, apiName);
135+
136+
// USD expects transmission Opacity. Default is changed to TRANSMISSION_REFRACTION
137+
mxContext.getOptions().hwTransmissionRenderMethod = mx::TRANSMISSION_OPACITY;
116138

117139
#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION == 38 && MATERIALX_BUILD_VERSION == 3
118140
mxContext.registerSourceCodeSearchPath(searchPath);
@@ -122,6 +144,7 @@ HdSt_GenMaterialXShader(
122144
for (const mx::FilePath &path : searchPath) {
123145
if (path.getBaseName() == "libraries") {
124146
libSearchPaths.append(path.getParentPath());
147+
libSearchPaths.append(path);
125148
}
126149
else {
127150
libSearchPaths.append(path);
@@ -863,6 +886,7 @@ _GenerateMaterialXShader(
863886
HdMaterialNode2 const& terminalNode,
864887
SdfPath const& terminalNodePath,
865888
TfToken const& materialTagToken,
889+
TfToken const& apiName,
866890
bool const bindlessTexturesEnabled)
867891
{
868892
// Load Standard Libraries/setup SearchPaths (for mxDoc and mxShaderGen)
@@ -901,7 +925,7 @@ _GenerateMaterialXShader(
901925
mxHdInfo.bindlessTexturesEnabled = bindlessTexturesEnabled;
902926

903927
// Generate the glslfx source code from the mtlxDoc
904-
return HdSt_GenMaterialXShader(mtlxDoc, searchPath, mxHdInfo);
928+
return HdSt_GenMaterialXShader(mtlxDoc, searchPath, mxHdInfo, apiName.GetString());
905929
}
906930

907931
void
@@ -927,6 +951,8 @@ HdSt_ApplyMaterialXFilter(
927951
resourceRegistry->GetHgi()->GetCapabilities()->IsSet(
928952
HgiDeviceCapabilitiesBitsBindlessTextures);
929953

954+
const TfToken apiName = resourceRegistry->GetHgi()->GetAPIName();
955+
930956
// If the MaterialNetwork has just a terminal node, utilize the
931957
// Resource Registry to cache the generated MaterialX glslfx Shader
932958
if (hdNetwork->nodes.size() == 1) {
@@ -942,7 +968,7 @@ HdSt_ApplyMaterialXFilter(
942968
// Generate the MaterialX glslfx ShaderPtr
943969
glslfxShader = _GenerateMaterialXShader(
944970
hdNetwork, materialPath, terminalNode, terminalNodePath,
945-
materialTagToken, bindlessTexturesEnabled);
971+
materialTagToken, apiName, bindlessTexturesEnabled);
946972

947973
// Store the mx::ShaderPtr
948974
glslfxInstance.SetValue(glslfxShader);
@@ -965,8 +991,8 @@ HdSt_ApplyMaterialXFilter(
965991
else {
966992
// Process the network and generate the MaterialX glslfx ShaderPtr
967993
glslfxShader = _GenerateMaterialXShader(
968-
hdNetwork, materialPath, terminalNode, terminalNodePath,
969-
materialTagToken, bindlessTexturesEnabled);
994+
hdNetwork, materialPath, terminalNode, terminalNodePath,
995+
materialTagToken, apiName, bindlessTexturesEnabled);
970996

971997
// Add material parameters from the glslfxShader
972998
_AddMaterialXParams(glslfxShader, materialParams);

pxr/imaging/hdSt/materialXFilter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ void HdSt_ApplyMaterialXFilter(
6666
MaterialX::ShaderPtr HdSt_GenMaterialXShader(
6767
MaterialX::DocumentPtr const& mxDoc,
6868
MaterialX::FileSearchPath const& searchPath,
69-
HdSt_MxShaderGenInfo const& mxHdInfo=HdSt_MxShaderGenInfo());
69+
HdSt_MxShaderGenInfo const& mxHdInfo=HdSt_MxShaderGenInfo(),
70+
std::string const& apiName="");
7071

7172
PXR_NAMESPACE_CLOSE_SCOPE
7273

0 commit comments

Comments
 (0)