Skip to content

Commit

Permalink
Simplify OSL test setup (#1868)
Browse files Browse the repository at this point in the history
Use the OSL cmake exports to configure the variables needed to enable OSL testing.

Also remove the need for specifying the standard OSL include path, an oslc install will already know where these are.
  • Loading branch information
ld-kerley committed Aug 3, 2024
1 parent 1775a6d commit dd075c0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
19 changes: 16 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,23 @@ if (MATERIALX_BUILD_USE_CCACHE)
endif()
endif()

# Attempt to configure OSL testing if it can be found by cmake.
# This will not override any explicitly provided oslc and testrender
if(MATERIALX_BUILD_RENDER AND MATERIALX_BUILD_GEN_OSL AND MATERIALX_BUILD_TESTS)
# We currently only need the actual OSL binaries if we're running tests with Render and GenOSL enabled.
find_package(OSL QUIET)
if(OSL_FOUND)
if(NOT MATERIALX_OSL_BINARY_OSLC)
set(MATERIALX_OSL_BINARY_OSLC $<TARGET_FILE:OSL::oslc>)
endif()
if(NOT MATERIALX_OSL_BINARY_TESTRENDER)
# currently OSL does not export a cmake target for testrender, but once that's added this can be simplified.
set(MATERIALX_OSL_BINARY_TESTRENDER $<TARGET_FILE_DIR:OSL::oslc>/testrender)
endif()
endif()
endif()

# Add global definitions
add_definitions(-DMATERIALX_OSL_BINARY_OSLC=\"${MATERIALX_OSL_BINARY_OSLC}\")
add_definitions(-DMATERIALX_OSL_BINARY_TESTRENDER=\"${MATERIALX_OSL_BINARY_TESTRENDER}\")
add_definitions(-DMATERIALX_OSL_INCLUDE_PATH=\"${MATERIALX_OSL_INCLUDE_PATH}\")
if (MATERIALX_OSL_LEGACY_CLOSURES)
add_definitions(-DMATERIALX_OSL_LEGACY_CLOSURES)
endif()
Expand Down
12 changes: 4 additions & 8 deletions source/MaterialXRenderOsl/OslRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ void OslRenderer::setSize(unsigned int width, unsigned int height)

void OslRenderer::initialize(RenderContextHandle)
{
if (_oslIncludePath.isEmpty())
{
throw ExceptionRenderError("OSL validation include path is empty");
}
if (_oslTestShadeExecutable.isEmpty() && _oslCompilerExecutable.isEmpty())
{
throw ExceptionRenderError("OSL validation executables not set");
Expand All @@ -61,7 +57,7 @@ void OslRenderer::initialize(RenderContextHandle)
void OslRenderer::renderOSL(const FilePath& dirPath, const string& shaderName, const string& outputName)
{
// If command options missing, skip testing.
if (_oslTestRenderExecutable.isEmpty() || _oslIncludePath.isEmpty() ||
if (_oslTestRenderExecutable.isEmpty() ||
_oslTestRenderSceneTemplateFile.isEmpty() || _oslUtilityOSOPath.isEmpty())
{
throw ExceptionRenderError("Command input arguments are missing");
Expand Down Expand Up @@ -218,7 +214,7 @@ void OslRenderer::renderOSL(const FilePath& dirPath, const string& shaderName, c
void OslRenderer::shadeOSL(const FilePath& dirPath, const string& shaderName, const string& outputName)
{
// If no command and include path specified then skip checking.
if (_oslTestShadeExecutable.isEmpty() || _oslIncludePath.isEmpty())
if (_oslTestShadeExecutable.isEmpty())
{
return;
}
Expand Down Expand Up @@ -279,7 +275,7 @@ void OslRenderer::shadeOSL(const FilePath& dirPath, const string& shaderName, co
void OslRenderer::compileOSL(const FilePath& oslFilePath)
{
// If no command and include path specified then skip checking.
if (_oslCompilerExecutable.isEmpty() || _oslIncludePath.isEmpty())
if (_oslCompilerExecutable.isEmpty())
{
return;
}
Expand Down Expand Up @@ -333,7 +329,7 @@ void OslRenderer::createProgram(const StageMap& stages)
throw ExceptionRenderError("No shader code to validate");
}

bool haveCompiler = !_oslCompilerExecutable.isEmpty() && !_oslIncludePath.isEmpty();
bool haveCompiler = !_oslCompilerExecutable.isEmpty();
if (!haveCompiler)
{
throw ExceptionRenderError("No OSL compiler specified for validation");
Expand Down
5 changes: 5 additions & 0 deletions source/MaterialXTest/MaterialXRenderOsl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h")

target_sources(MaterialXTest PUBLIC ${source} ${headers})

target_compile_definitions(MaterialXTest PRIVATE
MATERIALX_OSL_BINARY_OSLC=\"${MATERIALX_OSL_BINARY_OSLC}\"
MATERIALX_OSL_BINARY_TESTRENDER=\"${MATERIALX_OSL_BINARY_TESTRENDER}\"
)

add_tests("${source}")

assign_source_group("Source Files" ${source})
Expand Down
1 change: 0 additions & 1 deletion source/MaterialXTest/MaterialXRenderOsl/GenReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ TEST_CASE("GenReference: OSL Reference", "[genreference]")
oslRenderer = mx::OslRenderer::create();
oslRenderer->setOslCompilerExecutable(MATERIALX_OSL_BINARY_OSLC);
mx::FileSearchPath oslIncludePaths;
oslIncludePaths.append(mx::FilePath(MATERIALX_OSL_INCLUDE_PATH));
// Add in library include path for compile testing as the generated
// shader's includes are not added with absolute paths.
oslIncludePaths.append(searchPath.find("libraries/stdlib/genosl/include"));
Expand Down
1 change: 0 additions & 1 deletion source/MaterialXTest/MaterialXRenderOsl/RenderOsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ void OslShaderRenderTester::createRenderer(std::ostream& log)
_renderer->setOslCompilerExecutable(oslcExecutable);
const std::string testRenderExecutable(MATERIALX_OSL_BINARY_TESTRENDER);
_renderer->setOslTestRenderExecutable(testRenderExecutable);
_renderer->setOslIncludePath(mx::FileSearchPath(MATERIALX_OSL_INCLUDE_PATH));

try
{
Expand Down

0 comments on commit dd075c0

Please sign in to comment.