Skip to content

Commit

Permalink
Allow for usages where datalibrary and local library are both supported.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwinbhat committed Oct 23, 2023
1 parent a7f3d5f commit 1a3615a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
20 changes: 17 additions & 3 deletions source/MaterialXCore/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,24 @@ NodeDefPtr Node::getNodeDef(const string& target, bool allowRoughMatch) const
{
return resolveNameReference<NodeDef>(getNodeDefString());
}
vector<NodeDefPtr> nodeDefs = getDocument()->getMatchingNodeDefs(getQualifiedName(getCategory()));
vector<NodeDefPtr> secondary = getDocument()->getMatchingNodeDefs(getCategory());
vector<NodeDefPtr> roughMatches;

// If a datalibrary is not registered, use the document to locate nodedefs
ConstDocumentPtr datalibrary = Document::hasDataLibrary() ? Document::getDataLibrary() : getDocument();
vector<NodeDefPtr> nodeDefs = datalibrary->getMatchingNodeDefs(getQualifiedName(getCategory()));
vector<NodeDefPtr> secondary = datalibrary->getMatchingNodeDefs(getCategory());
nodeDefs.insert(nodeDefs.end(), secondary.begin(), secondary.end());

// NEED CLARIFICATION: It is possible that the node is defined in the document :-<
// data library takes presedence over local (?)
if (nodeDefs.empty())
{
nodeDefs = getDocument()->getMatchingNodeDefs(getQualifiedName(getCategory()));
secondary = getDocument()->getMatchingNodeDefs(getCategory());
nodeDefs.insert(nodeDefs.end(), secondary.begin(), secondary.end());
}

vector<NodeDefPtr> roughMatches;

for (NodeDefPtr nodeDef : nodeDefs)
{
if (!targetStringsMatch(nodeDef->getTarget(), target) ||
Expand Down
22 changes: 12 additions & 10 deletions source/MaterialXTest/MaterialXRender/RenderUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ bool ShaderRenderTester::validate(const mx::FilePath optionsFilePath)
// Load in the library dependencies once
// This will be imported in each test document below
ioTimer.startTimer();
mx::DocumentPtr dependLib;
loadDependentLibraries(options, searchPath, dependLib);
mx::DocumentPtr dataLibrary;
loadDependentLibraries(options, searchPath, dataLibrary);
ioTimer.endTimer();

// Create renderers and generators
Expand All @@ -144,18 +144,18 @@ bool ShaderRenderTester::validate(const mx::FilePath optionsFilePath)
createRenderer(log);

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

// Setup Unit system and working space
mx::UnitSystemPtr unitSystem = mx::UnitSystem::create(_shaderGenerator->getTarget());
_shaderGenerator->setUnitSystem(unitSystem);
mx::UnitConverterRegistryPtr registry = mx::UnitConverterRegistry::create();
mx::UnitTypeDefPtr distanceTypeDef = dependLib->getUnitTypeDef("distance");
mx::UnitTypeDefPtr distanceTypeDef = dataLibrary->getUnitTypeDef("distance");
registry->addUnitConverter(distanceTypeDef, mx::LinearUnitConverter::create(distanceTypeDef));
mx::UnitTypeDefPtr angleTypeDef = dependLib->getUnitTypeDef("angle");
mx::UnitTypeDefPtr angleTypeDef = dataLibrary->getUnitTypeDef("angle");
registry->addUnitConverter(angleTypeDef, mx::LinearUnitConverter::create(angleTypeDef));
_shaderGenerator->getUnitSystem()->loadLibrary(dependLib);
_shaderGenerator->getUnitSystem()->loadLibrary(dataLibrary);
_shaderGenerator->getUnitSystem()->setUnitConverterRegistry(registry);

mx::GenContext context(_shaderGenerator);
Expand All @@ -169,15 +169,18 @@ bool ShaderRenderTester::validate(const mx::FilePath optionsFilePath)
context.getOptions().emitColorTransforms = _emitColorTransforms;

// Register shader metadata defined in the libraries.
_shaderGenerator->registerShaderMetadata(dependLib, context);
_shaderGenerator->registerShaderMetadata(dataLibrary, context);

setupTime.endTimer();

if (!options.enableDirectLighting)
{
context.getOptions().hwMaxActiveLightSources = 0;
}
registerLights(dependLib, options, context);
registerLights(dataLibrary, options, context);

// Register the data library
mx::Document::setDataLibrary(dataLibrary);

// Map to replace "/" in Element path and ":" in namespaced names with "_".
mx::StringMap pathMap;
Expand Down Expand Up @@ -227,7 +230,6 @@ bool ShaderRenderTester::validate(const mx::FilePath optionsFilePath)
// colliding with implementations in previous test cases.
context.clearNodeImplementations();

doc->importLibrary(dependLib);
ioTimer.endTimer();

validateTimer.startTimer();
Expand Down Expand Up @@ -403,7 +405,7 @@ bool ShaderRenderTester::validate(const mx::FilePath optionsFilePath)

// Dump out profiling information
totalTime.endTimer();
printRunLog(profileTimes, options, profilingLog, dependLib);
printRunLog(profileTimes, options, profilingLog, dataLibrary);

return true;
}
Expand Down

0 comments on commit 1a3615a

Please sign in to comment.