Skip to content

Commit

Permalink
Further refine DataLibrary class
Browse files Browse the repository at this point in the history
Clean up need for datalibrary in ColorManagement and Unit systems.
  • Loading branch information
ashwinbhat committed Oct 2, 2024
1 parent 3ea61f1 commit 62434e7
Show file tree
Hide file tree
Showing 15 changed files with 29 additions and 75 deletions.
2 changes: 1 addition & 1 deletion source/MaterialXCore/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ MATERIALX_NAMESPACE_BEGIN
const string Document::CMS_ATTRIBUTE = "cms";
const string Document::CMS_CONFIG_ATTRIBUTE = "cmsconfig";

DataLibraryPtr globalDataLibrary = nullptr;
const DataLibraryPtr standardDataLibrary = DataLibrary::create();

// use loadDocuments to build this vector
void DataLibrary::loadDataLibrary(vector<DocumentPtr>& librarydocuments)
Expand Down
19 changes: 8 additions & 11 deletions source/MaterialXCore/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -696,32 +696,29 @@ class MX_CORE_API Document : public GraphElement

class MX_CORE_API DataLibrary
{

public:
/// Gets the registered node library
DocumentPtr getLibrary()
public:
ConstDocumentPtr dataLibrary()
{
return _datalibrary;
}

/// Returns true if a node libary is registered.
bool hasNodeLibrary()
void loadDataLibrary(vector<DocumentPtr>& documents);

static DataLibraryPtr create()
{
return (_datalibrary!= nullptr);
return std::make_shared<DataLibrary>();
}

private:
// Shared node library used across documents.
DocumentPtr _datalibrary;

void loadDataLibrary(vector<DocumentPtr>& documents);

};

/// Create a new Document.
/// @relates Document
MX_CORE_API DocumentPtr createDocument();

extern MX_CORE_API DataLibraryPtr globalDataLibrary;
extern MX_CORE_API const DataLibraryPtr standardDataLibrary;

MATERIALX_NAMESPACE_END

Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXCore/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ NodeDefPtr Node::getNodeDef(const string& target, bool allowRoughMatch) const

// AB: Should getNodeDef be part of DataLibrary?
// If a nodelibrary is not registered, use the document to locate nodedefs
ConstDocumentPtr datalibrarydoc = globalDataLibrary ? globalDataLibrary->getLibrary(): getDocument();
ConstDocumentPtr datalibrarydoc = standardDataLibrary ? standardDataLibrary->dataLibrary() : getDocument();
vector<NodeDefPtr> nodeDefs = datalibrarydoc->getMatchingNodeDefs(getQualifiedName(getCategory()));
vector<NodeDefPtr> secondary = datalibrarydoc->getMatchingNodeDefs(getCategory());
vector<NodeDefPtr> roughMatches;
Expand Down
9 changes: 0 additions & 9 deletions source/MaterialXGenShader/ColorManagementSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,8 @@ ColorManagementSystem::ColorManagementSystem()
{
}

void ColorManagementSystem::loadLibrary(DocumentPtr document)
{
_document = document;
}

bool ColorManagementSystem::supportsTransform(const ColorSpaceTransform& transform) const
{
if (!_document)
{
throw ExceptionShaderGenError("No library loaded for color management system");
}
return getNodeDef(transform) != nullptr;
}

Expand Down
6 changes: 0 additions & 6 deletions source/MaterialXGenShader/ColorManagementSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ class MX_GENSHADER_API ColorManagementSystem
/// Return the ColorManagementSystem name
virtual const string& getName() const = 0;

/// Load a library of implementations from the provided document,
/// replacing any previously loaded content.
virtual void loadLibrary(DocumentPtr document);

/// Returns whether this color management system supports a provided transform
bool supportsTransform(const ColorSpaceTransform& transform) const;

Expand All @@ -71,8 +67,6 @@ class MX_GENSHADER_API ColorManagementSystem
/// Returns a nodedef for a given transform
virtual NodeDefPtr getNodeDef(const ColorSpaceTransform& transform) const = 0;

protected:
DocumentPtr _document;
};

MATERIALX_NAMESPACE_END
Expand Down
7 changes: 1 addition & 6 deletions source/MaterialXGenShader/DefaultColorManagementSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,11 @@ const string& DefaultColorManagementSystem::getName() const

NodeDefPtr DefaultColorManagementSystem::getNodeDef(const ColorSpaceTransform& transform) const
{
if (!_document)
{
throw ExceptionShaderGenError("No library loaded for color management system");
}

string sourceSpace = COLOR_SPACE_REMAP.count(transform.sourceSpace) ? COLOR_SPACE_REMAP.at(transform.sourceSpace) : transform.sourceSpace;
string targetSpace = COLOR_SPACE_REMAP.count(transform.targetSpace) ? COLOR_SPACE_REMAP.at(transform.targetSpace) : transform.targetSpace;
string nodeName = sourceSpace + "_to_" + targetSpace;

for (NodeDefPtr nodeDef : _document->getMatchingNodeDefs(nodeName))
for (NodeDefPtr nodeDef : standardDataLibrary->dataLibrary()->getMatchingNodeDefs(nodeName))
{
for (OutputPtr output : nodeDef->getOutputs())
{
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXGenShader/ShaderGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ void ShaderGraph::addDefaultGeomNode(ShaderInput* input, const GeomPropDef& geom
const string geomNodeName = "geomprop_" + geomprop.getName();
ShaderNode* node = getNode(geomNodeName);

ConstDocumentPtr datalibrarydoc = globalDataLibrary ? globalDataLibrary->getLibrary() : _document;
ConstDocumentPtr datalibrarydoc = standardDataLibrary ? standardDataLibrary->dataLibrary() : _document;
if (!node)
{
// Find the nodedef for the geometric node referenced by the geomprop. Use the type of the
Expand Down
14 changes: 2 additions & 12 deletions source/MaterialXGenShader/UnitSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,6 @@ UnitSystem::UnitSystem(const string& target) :
{
}

void UnitSystem::loadLibrary(DocumentPtr document)
{
_document = document;
}

void UnitSystem::setUnitConverterRegistry(UnitConverterRegistryPtr registry)
{
_unitRegistry = registry;
Expand All @@ -145,13 +140,8 @@ UnitSystemPtr UnitSystem::create(const string& language)

NodeDefPtr UnitSystem::getNodeDef(const UnitTransform& transform) const
{
if (!_document)
{
throw ExceptionShaderGenError("No library loaded for unit system");
}

const string MULTIPLY_NODE_NAME = "multiply";
for (NodeDefPtr nodeDef : _document->getMatchingNodeDefs(MULTIPLY_NODE_NAME))
for (NodeDefPtr nodeDef : standardDataLibrary->dataLibrary()->getMatchingNodeDefs(MULTIPLY_NODE_NAME))
{
for (OutputPtr output : nodeDef->getOutputs())
{
Expand Down Expand Up @@ -183,7 +173,7 @@ ShaderNodePtr UnitSystem::createNode(ShaderGraph* parent, const UnitTransform& t
}

// Scalar unit conversion
UnitTypeDefPtr scalarTypeDef = _document->getUnitTypeDef(transform.unitType);
UnitTypeDefPtr scalarTypeDef = standardDataLibrary->dataLibrary()->getUnitTypeDef(transform.unitType);
if (!_unitRegistry || !_unitRegistry->getUnitConverter(scalarTypeDef))
{
throw ExceptionTypeError("Unit registry unavaliable or undefined unit converter for: " + transform.unitType);
Expand Down
4 changes: 0 additions & 4 deletions source/MaterialXGenShader/UnitSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ class MX_GENSHADER_API UnitSystem
/// Returns the currently assigned unit converter registry
virtual UnitConverterRegistryPtr getUnitConverterRegistry() const;

/// assign document with unit implementations replacing any previously loaded content.
virtual void loadLibrary(DocumentPtr document);

/// Returns whether this unit system supports a provided transform
bool supportsTransform(const UnitTransform& transform) const;

Expand All @@ -89,7 +86,6 @@ class MX_GENSHADER_API UnitSystem

protected:
UnitConverterRegistryPtr _unitRegistry;
DocumentPtr _document;
string _target;
};

Expand Down
2 changes: 0 additions & 2 deletions source/MaterialXRender/TextureBaker.inl
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,6 @@ DocumentPtr TextureBaker<Renderer, ShaderGen>::bakeMaterialToDoc(DocumentPtr doc
genContext.getOptions().targetDistanceUnit = _distanceUnit;

DefaultColorManagementSystemPtr cms = DefaultColorManagementSystem::create(genContext.getShaderGenerator().getTarget());
cms->loadLibrary(doc);
genContext.registerSourceCodeSearchPath(searchPath);
genContext.getShaderGenerator().setColorManagementSystem(cms);

Expand Down Expand Up @@ -644,7 +643,6 @@ void TextureBaker<Renderer, ShaderGen>::setupUnitSystem(DocumentPtr unitDefiniti
UnitConverterRegistryPtr registry = UnitConverterRegistry::create();
registry->addUnitConverter(distanceTypeDef, LinearUnitConverter::create(distanceTypeDef));
registry->addUnitConverter(angleTypeDef, LinearUnitConverter::create(angleTypeDef));
_generator->getUnitSystem()->loadLibrary(unitDefinitions);
_generator->getUnitSystem()->setUnitConverterRegistry(registry);
}

Expand Down
31 changes: 15 additions & 16 deletions source/MaterialXTest/MaterialXGenShader/GenShaderUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,50 +312,51 @@ void testUniqueNames(mx::GenContext& context, const std::string& stage)
REQUIRE(sgNode1->getOutput()->getVariable() == "unique_names_out");
}

// Test ShaderGen performance
void shaderGenPerformanceTest(mx::GenContext& context)
void loadDefaultDataLibrary()
{
mx::globalDataLibrary = std::shared_ptr<mx::DataLibrary>(new mx::DataLibrary());
const mx::FileSearchPath libSearchPath(mx::getDefaultDataSearchPath());

// Load the standard libraries.

mx::FilePath libraryroot(libSearchPath.asString() + "/libraries");
std::vector<mx::DocumentPtr> documentList;
mx::StringVec libdocumentsPaths;
mx::StringVec liberrorLog;
mx::loadDocuments(libraryroot, libSearchPath, {}, {}, documentList, libdocumentsPaths, nullptr, &liberrorLog);
mx::globalDataLibrary->loadDataLibrary(documentList);

if (liberrorLog.size() == 0)
mx::standardDataLibrary->loadDataLibrary(documentList);
}

// Test ShaderGen performance
void shaderGenPerformanceTest(mx::GenContext& context)
{
const mx::FileSearchPath libSearchPath(mx::getDefaultDataSearchPath());

// Load the standard libraries.
loadDefaultDataLibrary();

//loadLibraries({ "libraries" }, libSearchPath, nodeLibrary);
context.registerSourceCodeSearchPath(libSearchPath);

// Register node library
//mx::Document::setNodeLibrary(nodeLibrary);

// Enable Color Management
mx::ColorManagementSystemPtr colorManagementSystem =
mx::DefaultColorManagementSystem::create(context.getShaderGenerator().getTarget());

REQUIRE(colorManagementSystem);
if (colorManagementSystem)
{
context.getShaderGenerator().setColorManagementSystem(colorManagementSystem);
colorManagementSystem->loadLibrary(mx::globalDataLibrary->getLibrary());
}

// Enable Unit System
mx::UnitSystemPtr unitSystem = mx::UnitSystem::create(context.getShaderGenerator().getTarget());
REQUIRE(unitSystem);
if (unitSystem)
{
context.getShaderGenerator().setUnitSystem(unitSystem);
unitSystem->loadLibrary(mx::globalDataLibrary->getLibrary());
// Setup Unit converters
unitSystem->setUnitConverterRegistry(mx::UnitConverterRegistry::create());
mx::UnitTypeDefPtr distanceTypeDef = mx::globalDataLibrary->getLibrary()->getUnitTypeDef("distance");
mx::UnitTypeDefPtr distanceTypeDef = mx::standardDataLibrary->dataLibrary()->getUnitTypeDef("distance");
unitSystem->getUnitConverterRegistry()->addUnitConverter(distanceTypeDef, mx::LinearUnitConverter::create(distanceTypeDef));
mx::UnitTypeDefPtr angleTypeDef = mx::globalDataLibrary->getLibrary()->getUnitTypeDef("angle");
mx::UnitTypeDefPtr angleTypeDef = mx::standardDataLibrary->dataLibrary()->getUnitTypeDef("angle");
unitSystem->getUnitConverterRegistry()->addUnitConverter(angleTypeDef, mx::LinearUnitConverter::create(angleTypeDef));
context.getOptions().targetDistanceUnit = "meter";
}
Expand Down Expand Up @@ -529,7 +530,6 @@ void ShaderGeneratorTester::addColorManagement()
else
{
_shaderGenerator->setColorManagementSystem(_colorManagementSystem);
_colorManagementSystem->loadLibrary(_dependLib);
}
}
}
Expand All @@ -547,7 +547,6 @@ void ShaderGeneratorTester::addUnitSystem()
else
{
_shaderGenerator->setUnitSystem(_unitSystem);
_unitSystem->loadLibrary(_dependLib);
_unitSystem->setUnitConverterRegistry(mx::UnitConverterRegistry::create());
mx::UnitTypeDefPtr distanceTypeDef = _dependLib->getUnitTypeDef("distance");
_unitSystem->getUnitConverterRegistry()->addUnitConverter(distanceTypeDef, mx::LinearUnitConverter::create(distanceTypeDef));
Expand Down
2 changes: 0 additions & 2 deletions source/MaterialXTest/MaterialXRender/RenderUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ bool ShaderRenderTester::validate(const mx::FilePath optionsFilePath)
createRenderer(log);

mx::ColorManagementSystemPtr colorManagementSystem = mx::DefaultColorManagementSystem::create(_shaderGenerator->getTarget());
colorManagementSystem->loadLibrary(dependLib);
_shaderGenerator->setColorManagementSystem(colorManagementSystem);

// Setup Unit system and working space
Expand All @@ -154,7 +153,6 @@ bool ShaderRenderTester::validate(const mx::FilePath optionsFilePath)
registry->addUnitConverter(distanceTypeDef, mx::LinearUnitConverter::create(distanceTypeDef));
mx::UnitTypeDefPtr angleTypeDef = dependLib->getUnitTypeDef("angle");
registry->addUnitConverter(angleTypeDef, mx::LinearUnitConverter::create(angleTypeDef));
_shaderGenerator->getUnitSystem()->loadLibrary(dependLib);
_shaderGenerator->getUnitSystem()->setUnitConverterRegistry(registry);

mx::GenContext context(_shaderGenerator);
Expand Down
2 changes: 0 additions & 2 deletions source/MaterialXView/Viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1671,12 +1671,10 @@ void Viewer::initContext(mx::GenContext& context)

// Initialize color management.
mx::DefaultColorManagementSystemPtr cms = mx::DefaultColorManagementSystem::create(context.getShaderGenerator().getTarget());
cms->loadLibrary(_stdLib);
context.getShaderGenerator().setColorManagementSystem(cms);

// Initialize unit management.
mx::UnitSystemPtr unitSystem = mx::UnitSystem::create(context.getShaderGenerator().getTarget());
unitSystem->loadLibrary(_stdLib);
unitSystem->setUnitConverterRegistry(_unitRegistry);
context.getShaderGenerator().setUnitSystem(unitSystem);
context.getOptions().targetDistanceUnit = "meter";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ void bindPyColorManagement(py::module& mod)
py::class_<mx::ColorManagementSystem, PyColorManagementSystem, mx::ColorManagementSystemPtr>(mod, "ColorManagementSystem")
.def(py::init<>())
.def("getName", &mx::ColorManagementSystem::getName)
.def("loadLibrary", &mx::ColorManagementSystem::loadLibrary)
.def("supportsTransform", &mx::ColorManagementSystem::supportsTransform);

py::class_<mx::DefaultColorManagementSystem, mx::DefaultColorManagementSystemPtr, mx::ColorManagementSystem>(mod, "DefaultColorManagementSystem")
Expand Down
1 change: 0 additions & 1 deletion source/PyMaterialX/PyMaterialXGenShader/PyUnitSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ void bindPyUnitSystem(py::module& mod)
py::class_<mx::UnitSystem, mx::UnitSystemPtr>(mod, "UnitSystem")
.def_static("create", &mx::UnitSystem::create)
.def("getName", &mx::UnitSystem::getName)
.def("loadLibrary", &mx::UnitSystem::loadLibrary)
.def("supportsTransform", &mx::UnitSystem::supportsTransform)
.def("setUnitConverterRegistry", &mx::UnitSystem::setUnitConverterRegistry)
.def("getUnitConverterRegistry", &mx::UnitSystem::getUnitConverterRegistry);
Expand Down

0 comments on commit 62434e7

Please sign in to comment.