Skip to content

Commit

Permalink
Testcase for Shader reflection with USD
Browse files Browse the repository at this point in the history
In this test we use a materialx document with two surfacematerial. We generate shaders for each of the surfaces and use reflection to report the input paths.
Notice that the input paths for both surface materials are identical though the generated shader code isn't
  • Loading branch information
ashwinbhat committed Sep 6, 2023
1 parent f822dc9 commit ab218c0
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
18 changes: 18 additions & 0 deletions resources/Materials/TestSuite/usd_usage/surfacematerial.mtlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<materialx version="1.38" colorspace="lin_rec709">
<!-- node shared between two surface materials-->
<UsdPreviewSurface name="SR_plastic" type="surfaceshader">
<input name="diffuseColor" type="color3" value="0.10470402, 0.24188282, 0.81800002" />
<input name="roughness" type="float" value="0.32467532157897949" />
<input name="ior" type="float" value="1.5" />
</UsdPreviewSurface>

<!-- 1st surfacematerial -->
<surfacematerial name="USD_PlasticOne" type="material">
<input name="surfaceshader" type="surfaceshader" nodename="SR_plastic" />
</surfacematerial>

<!-- 2nd surfacematerial -->
<surfacematerial name="USD_PlasticTwo" type="material">
<input name="surfaceshader" type="surfaceshader" nodename="SR_plastic" />
</surfacematerial>
</materialx>
73 changes: 73 additions & 0 deletions source/MaterialXTest/MaterialXGenShader/GenShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,79 @@ TEST_CASE("GenShader: Deterministic Generation", "[genshader]")
#endif
}

void testShaderReflection(mx::DocumentPtr libraries, mx::GenContext& context)
{
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
mx::FilePath testFile = searchPath.find("resources/Materials/TestSuite/usd_usage/surfacematerial.mtlx");

mx::StringVec sourceCode(2);

mx::DocumentPtr testDoc = mx::createDocument();
mx::readFromXmlFile(testDoc, testFile);
testDoc->importLibrary(libraries);

mx::string testSurfaceOne = "USD_PlasticOne";
mx::ElementPtr element = testDoc->getChild(testSurfaceOne);
CHECK(element);

mx::ShaderPtr shaderOne = context.getShaderGenerator().generate(testSurfaceOne, element, context);
sourceCode[0] = shaderOne->getSourceCode();

mx::string testSurfaceTwo = "USD_PlasticTwo";
element = testDoc->getChild(testSurfaceTwo);
CHECK(element);

mx::ShaderPtr shaderTwo = context.getShaderGenerator().generate(testSurfaceTwo, element, context);
sourceCode[1] = shaderTwo->getSourceCode();

// Shader code is different
CHECK(sourceCode[0] != sourceCode[1]);

// Check paths
mx::ShaderStage& stage1 = shaderOne->getStage(mx::Stage::PIXEL);
mx::VariableBlock& block1 = stage1.getUniformBlock(mx::HW::PUBLIC_UNIFORMS);

printf("Shader one input paths:\n");
for (mx::ShaderPort* variable : block1.getVariableOrder())
{
std::string parameterPath = variable->getPath();
if (!parameterPath.empty())
printf("%s\n", parameterPath.c_str());
}

mx::ShaderStage& stage2 = shaderTwo->getStage(mx::Stage::PIXEL);
mx::VariableBlock& block2 = stage2.getUniformBlock(mx::HW::PUBLIC_UNIFORMS);
printf("Shader two input paths:\n");
for (mx::ShaderPort* variable : block2.getVariableOrder())
{
std::string parameterPath = variable->getPath();
if (!parameterPath.empty())
printf("%s\n", parameterPath.c_str());
}
}

TEST_CASE("GenShader: Shader reflection", "[genshader]")
{
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
mx::DocumentPtr libraries = mx::createDocument();
mx::loadLibraries({ "libraries" }, searchPath, libraries);

#ifdef MATERIALX_BUILD_GEN_GLSL
{
mx::GenContext context(mx::GlslShaderGenerator::create());
context.registerSourceCodeSearchPath(searchPath);
testShaderReflection(libraries, context);
}
#endif
#ifdef MATERIALX_BUILD_GEN_MSL
{
mx::GenContext context(mx::MslShaderGenerator::create());
context.registerSourceCodeSearchPath(searchPath);
testShaderReflection(libraries, context);
}
#endif
}

void checkPixelDependencies(mx::DocumentPtr libraries, mx::GenContext& context)
{
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
Expand Down

0 comments on commit ab218c0

Please sign in to comment.