Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apple: MaterialX on Metal #2324

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build_scripts/build_usd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1574,11 +1574,12 @@ def InstallDraco(context, force, buildArgs):
############################################################
# MaterialX

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

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

cmakeOptions += buildArgs;

Expand Down
1 change: 1 addition & 0 deletions pxr/imaging/hdSt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ if (${PXR_ENABLE_MATERIALX_SUPPORT})
MaterialXCore
MaterialXFormat
MaterialXGenGlsl
MaterialXGenMsl
hdMtlx
)
list(APPEND optionalPrivateClasses
Expand Down
38 changes: 32 additions & 6 deletions pxr/imaging/hdSt/materialXFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "pxr/imaging/hdSt/package.h"
#include "pxr/imaging/hdSt/resourceRegistry.h"
#include "pxr/imaging/hdMtlx/hdMtlx.h"
#include "pxr/imaging/hgi/tokens.h"

#include "pxr/usd/sdr/registry.h"
#include "pxr/imaging/hio/glslfx.h"
Expand Down Expand Up @@ -103,16 +104,37 @@ R"(
</materialx>
)";

mx::GenContext createMaterialXContext(
HdSt_MxShaderGenInfo const& mxHdInfo,
std::string const& apiName)
{
if(apiName == HgiTokens->Metal)
return HdStMaterialXMslShaderGen::create(mxHdInfo);
if(apiName == HgiTokens->OpenGL)
return HdStMaterialXGlslShaderGen::create(mxHdInfo);
else
{
TF_CODING_ERROR(
"MaterialX Shader Generator doesn't support %s API.",
apiName.c_str());
//return mx::GenContext();
}
}

// Use the given mxDocument to generate the corresponding glsl shader
// Based on MaterialXViewer Viewer::loadDocument()
mx::ShaderPtr
HdSt_GenMaterialXShader(
mx::DocumentPtr const& mxDoc,
mx::FileSearchPath const& searchPath,
HdSt_MxShaderGenInfo const& mxHdInfo)
HdSt_MxShaderGenInfo const& mxHdInfo,
std::string const& apiName)
{
// Initialize the Context for shaderGen.
mx::GenContext mxContext = HdStMaterialXShaderGen::create(mxHdInfo);
mx::GenContext mxContext = createMaterialXContext(mxHdInfo, apiName);

// USD expects transmission Opacity. Default is changed to TRANSMISSION_REFRACTION
mxContext.getOptions().hwTransmissionRenderMethod = mx::TRANSMISSION_OPACITY;

#if MATERIALX_MAJOR_VERSION == 1 && MATERIALX_MINOR_VERSION == 38 && MATERIALX_BUILD_VERSION == 3
mxContext.registerSourceCodeSearchPath(searchPath);
Expand All @@ -122,6 +144,7 @@ HdSt_GenMaterialXShader(
for (const mx::FilePath &path : searchPath) {
if (path.getBaseName() == "libraries") {
libSearchPaths.append(path.getParentPath());
libSearchPaths.append(path);
}
else {
libSearchPaths.append(path);
Expand Down Expand Up @@ -863,6 +886,7 @@ _GenerateMaterialXShader(
HdMaterialNode2 const& terminalNode,
SdfPath const& terminalNodePath,
TfToken const& materialTagToken,
TfToken const& apiName,
bool const bindlessTexturesEnabled)
{
// Load Standard Libraries/setup SearchPaths (for mxDoc and mxShaderGen)
Expand Down Expand Up @@ -901,7 +925,7 @@ _GenerateMaterialXShader(
mxHdInfo.bindlessTexturesEnabled = bindlessTexturesEnabled;

// Generate the glslfx source code from the mtlxDoc
return HdSt_GenMaterialXShader(mtlxDoc, searchPath, mxHdInfo);
return HdSt_GenMaterialXShader(mtlxDoc, searchPath, mxHdInfo, apiName.GetString());
}

void
Expand All @@ -927,6 +951,8 @@ HdSt_ApplyMaterialXFilter(
resourceRegistry->GetHgi()->GetCapabilities()->IsSet(
HgiDeviceCapabilitiesBitsBindlessTextures);

const TfToken apiName = resourceRegistry->GetHgi()->GetAPIName();

// If the MaterialNetwork has just a terminal node, utilize the
// Resource Registry to cache the generated MaterialX glslfx Shader
if (hdNetwork->nodes.size() == 1) {
Expand All @@ -942,7 +968,7 @@ HdSt_ApplyMaterialXFilter(
// Generate the MaterialX glslfx ShaderPtr
glslfxShader = _GenerateMaterialXShader(
hdNetwork, materialPath, terminalNode, terminalNodePath,
materialTagToken, bindlessTexturesEnabled);
materialTagToken, apiName, bindlessTexturesEnabled);

// Store the mx::ShaderPtr
glslfxInstance.SetValue(glslfxShader);
Expand All @@ -965,8 +991,8 @@ HdSt_ApplyMaterialXFilter(
else {
// Process the network and generate the MaterialX glslfx ShaderPtr
glslfxShader = _GenerateMaterialXShader(
hdNetwork, materialPath, terminalNode, terminalNodePath,
materialTagToken, bindlessTexturesEnabled);
hdNetwork, materialPath, terminalNode, terminalNodePath,
materialTagToken, apiName, bindlessTexturesEnabled);

// Add material parameters from the glslfxShader
_AddMaterialXParams(glslfxShader, materialParams);
Expand Down
3 changes: 2 additions & 1 deletion pxr/imaging/hdSt/materialXFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ void HdSt_ApplyMaterialXFilter(
MaterialX::ShaderPtr HdSt_GenMaterialXShader(
MaterialX::DocumentPtr const& mxDoc,
MaterialX::FileSearchPath const& searchPath,
HdSt_MxShaderGenInfo const& mxHdInfo=HdSt_MxShaderGenInfo());
HdSt_MxShaderGenInfo const& mxHdInfo=HdSt_MxShaderGenInfo(),
std::string const& apiName="");

PXR_NAMESPACE_CLOSE_SCOPE

Expand Down
Loading