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

Autodesk: Cache materialX::LoadLibraries step to be done once #2666

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
1 change: 1 addition & 0 deletions pxr/imaging/hdMtlx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pxr_library(hdMtlx

PUBLIC_CLASSES
hdMtlx
hdMtlxLibsRegistry

PUBLIC_HEADERS
api.h
Expand Down
1 change: 1 addition & 0 deletions pxr/imaging/hdMtlx/hdMtlx.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "pxr/pxr.h"
#include "pxr/base/tf/token.h"
#include "pxr/usd/sdf/path.h"
#include "pxr/imaging/hdMtlx/api.h"
#include <memory>
#include <set>
Expand Down
50 changes: 50 additions & 0 deletions pxr/imaging/hdMtlx/hdMtlxLibsRegistry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Copyright 2023 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.
//
#include "hdMtlxLibsRegistry.h"
#include "hdMtlx.h"
#include "pxr/base/tf/instantiateSingleton.h"
#include "pxr/usd/usdMtlx/utils.h"
#include <MaterialXCore/Document.h>
#include <MaterialXFormat/Util.h>



PXR_NAMESPACE_OPEN_SCOPE
TF_INSTANTIATE_SINGLETON(MtlxLibsRegistry);

MtlxLibsRegistry::MtlxLibsRegistry()
{
_stdLibraries = MaterialX::createDocument();
_searchPaths = HdMtlxSearchPaths();

mx::FilePathVec libraryFolders;
mx::loadLibraries(libraryFolders, _searchPaths, _stdLibraries);
};

MtlxLibsRegistry::~MtlxLibsRegistry()
{
}
PXR_NAMESPACE_CLOSE_SCOPE


68 changes: 68 additions & 0 deletions pxr/imaging/hdMtlx/hdMtlxLibsRegistry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// Copyright 2023 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.
//
#ifndef PXR_IMAGING_HD_MTLX_HDMTLXLIBSREGISTRY_H
#define PXR_IMAGING_HD_MTLX_HDMTLXLIBSREGISTRY_H

#include "pxr/pxr.h"
#include "pxr/base/tf/singleton.h"
#include <boost/noncopyable.hpp>

#include "pxr/imaging/hdMtlx/api.h"

#include <MaterialXCore/Library.h>
#include <MaterialXFormat/File.h>
#include <cstddef>

MATERIALX_NAMESPACE_BEGIN
class FileSearchPath;
using DocumentPtr = std::shared_ptr<class Document>;
MATERIALX_NAMESPACE_END

PXR_NAMESPACE_OPEN_SCOPE
namespace mx = MaterialX;
class MtlxLibsRegistry : boost::noncopyable
{
public:
HDMTLX_API
static MtlxLibsRegistry& GetInstance() {
return TfSingleton<MtlxLibsRegistry>::GetInstance();
};

HDMTLX_API
MaterialX::DocumentPtr stdLibraries() const { return _stdLibraries; };
HDMTLX_API
const MaterialX::FileSearchPath& searchPaths() const {return _searchPaths;};
private:

MtlxLibsRegistry();
~MtlxLibsRegistry();
friend class TfSingleton<MtlxLibsRegistry>;
private:
MaterialX::FileSearchPath _searchPaths;
MaterialX::DocumentPtr _stdLibraries = NULL;
};

PXR_NAMESPACE_CLOSE_SCOPE

#endif
9 changes: 5 additions & 4 deletions pxr/imaging/hdSt/materialXFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "pxr/imaging/hdSt/resourceRegistry.h"
#include "pxr/imaging/hdMtlx/hdMtlx.h"
#include "pxr/imaging/hgi/tokens.h"
#include "pxr/imaging/hdMtlx/hdMtlxLibsRegistry.h"

#include "pxr/usd/sdr/registry.h"
#include "pxr/imaging/hio/glslfx.h"
Expand Down Expand Up @@ -903,10 +904,10 @@ _GenerateMaterialXShader(
bool const bindlessTexturesEnabled)
{
// Load Standard Libraries/setup SearchPaths (for mxDoc and mxShaderGen)
mx::FilePathVec libraryFolders;
mx::FileSearchPath searchPath = HdMtlxSearchPaths();
mx::DocumentPtr stdLibraries = mx::createDocument();
mx::loadLibraries(libraryFolders, searchPath, stdLibraries);
//
MtlxLibsRegistry& mtlxLibsRegistry = MtlxLibsRegistry::GetInstance();
mx::DocumentPtr stdLibraries = mtlxLibsRegistry.stdLibraries();
mx::FileSearchPath searchPath = mtlxLibsRegistry.searchPaths();

// Create the MaterialX Document from the HdMaterialNetwork
HdSt_MxShaderGenInfo mxHdInfo;
Expand Down