Skip to content

Commit 17e52e3

Browse files
authored
Adsk Contrib - Improve the OSL unit test framework (#1514) (#1524)
* Adsk Contrib - Improve the OSL unit test framework Signed-off-by: Patrick Hodoul <[email protected]> * Fix a Linux build break Signed-off-by: Patrick Hodoul <[email protected]> * Fix a 'git rebase' break Signed-off-by: Patrick Hodoul <[email protected]> * Some improvements Signed-off-by: Patrick Hodoul <[email protected]> * Workaround to sucesfully link with OSL Signed-off-by: Patrick Hodoul <[email protected]> * Found the right fix Signed-off-by: Patrick Hodoul <[email protected]> * Add more tests for Grading ops Signed-off-by: Patrick Hodoul <[email protected]> * Improve OSL library find Signed-off-by: Patrick Hodoul <[email protected]> * Fix with OpenImageIO version requires C++14 Signed-off-by: Patrick Hodoul <[email protected]> * Another OSL cmake improvement Signed-off-by: Patrick Hodoul <[email protected]>
1 parent 7360206 commit 17e52e3

37 files changed

+3427
-638
lines changed

share/cmake/modules/FindImath.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ if(NOT Imath_FOUND)
203203
)
204204

205205
add_dependencies(Imath::Imath imath_install)
206+
207+
# Some Imath versions define a second target.
208+
add_library(Imath::ImathConfig ALIAS Imath::Imath)
209+
206210
message(STATUS "Installing Imath: ${Imath_LIBRARY} (version \"${Imath_VERSION}\")")
207211
endif()
208212
endif()

share/cmake/modules/FindOpenImageIO.cmake

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
# OPENIMAGEIO_VERSION_MINOR Version minor number
2222
# OPENIMAGEIO_VERSION_PATCH Version minor patch
2323
# OPENIMAGEIO_VERSION_TWEAK Version minor tweak
24-
# OIIOTOOL_BIN Path to oiiotool executable
2524
#
2625
# Imported targets:
2726
# OpenImageIO::OpenImageIO The libOpenImageIO library.
@@ -66,9 +65,6 @@ find_library ( OPENIMAGEIO_UTIL_LIBRARY
6665
find_path ( OPENIMAGEIO_INCLUDE_DIR
6766
NAMES OpenImageIO/imageio.h
6867
HINTS ${OPENIMAGEIO_ROOT_DIR} )
69-
find_program ( OIIOTOOL_BIN
70-
NAMES oiiotool
71-
HINTS ${OPENIMAGEIO_ROOT_DIR} )
7268

7369
# Try to figure out version number
7470
set (OIIO_VERSION_HEADER "${OPENIMAGEIO_INCLUDE_DIR}/OpenImageIO/oiioversion.h")

share/cmake/modules/FindOpenShadingLanguage.cmake

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,59 @@
77
# variable to tell CMake where to find it.
88
#
99

10-
# TODO: OSL: Use "find_package(OSL 1.11 CONFIG)" directly instead of this file!
11-
1210

1311
if(NOT TARGET osl::osl)
14-
add_library(osl::osl UNKNOWN IMPORTED GLOBAL)
12+
add_library(osl::osl INTERFACE IMPORTED GLOBAL)
1513
set(OSL_FOUND OFF)
1614
endif()
1715

1816
###############################################################################
1917
### Try to find package ###
2018

21-
if(DEFINED OSL_ROOT)
19+
if(NOT DEFINED OSL_ROOT)
20+
21+
find_package(OSL ${OpenShadingLanguage_FIND_VERSION} CONFIG)
22+
23+
set(OpenShadingLanguage_VERSION ${OSL_VERSION})
24+
25+
# TODO: No variable to have the share directory?
26+
27+
set(OSL_SHADERS_INCLUDE_DIR ${OSL_INCLUDE_DIR}/../share)
28+
29+
# Variable used by the OSL unit tests.
30+
set(OSL_SHADERS_DIR ${OSL_SHADERS_INCLUDE_DIR}/OSL/shaders)
31+
32+
include (FindPackageHandleStandardArgs)
33+
find_package_handle_standard_args (OpenShadingLanguage
34+
FOUND_VAR OpenShadingLanguage_FOUND
35+
REQUIRED_VARS OSL_INCLUDE_DIR OSL_LIB_DIR OpenShadingLanguage_VERSION
36+
VERSION_VAR OpenShadingLanguage_VERSION
37+
)
38+
39+
set(OSL_FOUND ${OpenShadingLanguage_FOUND})
40+
41+
else()
2242

2343
set(OSL_INCLUDE_DIR ${OSL_ROOT}/include)
2444

25-
if(EXISTS "${OSL_INCLUDE_DIR}/OSL/oslversion.h")
45+
set(OSL_VERSION_HEADER "${OSL_INCLUDE_DIR}/OSL/oslversion.h")
46+
47+
if(EXISTS "${OSL_VERSION_HEADER}")
48+
49+
# Try to figure out version number
50+
file (STRINGS "${OSL_VERSION_HEADER}" TMP REGEX "^#define OSL_LIBRARY_VERSION_MAJOR .*$")
51+
string (REGEX MATCHALL "[0-9]+" OSL_VERSION_MAJOR ${TMP})
52+
file (STRINGS "${OSL_VERSION_HEADER}" TMP REGEX "^#define OSL_LIBRARY_VERSION_MINOR .*$")
53+
string (REGEX MATCHALL "[0-9]+" OSL_VERSION_MINOR ${TMP})
54+
file (STRINGS "${OSL_VERSION_HEADER}" TMP REGEX "^#define OSL_LIBRARY_VERSION_PATCH .*$")
55+
string (REGEX MATCHALL "[0-9]+" OSL_VERSION_PATCH ${TMP})
56+
file (STRINGS "${OSL_VERSION_HEADER}" TMP REGEX "^#define OSL_LIBRARY_VERSION_TWEAK .*$")
57+
if (TMP)
58+
string (REGEX MATCHALL "[0-9]+" OSL_VERSION_TWEAK ${TMP})
59+
else ()
60+
set (OSL_VERSION_TWEAK 0)
61+
endif ()
62+
set (OpenShadingLanguage_VERSION "${OSL_VERSION_MAJOR}.${OSL_VERSION_MINOR}.${OSL_VERSION_PATCH}.${OSL_VERSION_TWEAK}")
2663

2764
# Find the oslcomp library.
2865
find_library(oslcomp_LIBRARY
@@ -34,6 +71,11 @@ if(DEFINED OSL_ROOT)
3471
lib
3572
)
3673

74+
add_library(OSL::oslcomp SHARED IMPORTED)
75+
set_target_properties(OSL::oslcomp PROPERTIES
76+
IMPORTED_LOCATION ${oslcomp_LIBRARY}
77+
)
78+
3779
# Find the oslexec library.
3880
find_library(oslexec_LIBRARY
3981
NAMES
@@ -44,8 +86,15 @@ if(DEFINED OSL_ROOT)
4486
lib
4587
)
4688

89+
add_library(OSL::oslexec SHARED IMPORTED)
90+
set_target_properties(OSL::oslexec PROPERTIES
91+
IMPORTED_LOCATION ${oslexec_LIBRARY}
92+
)
93+
94+
set(OSL_SHADERS_INCLUDE_DIR ${OSL_ROOT}/share)
95+
4796
# Variable used by the OSL unit tests.
48-
set(OSL_SHADERS_DIR ${OSL_ROOT}/share/OSL/shaders)
97+
set(OSL_SHADERS_DIR ${OSL_SHADERS_INCLUDE_DIR}/OSL/shaders)
4998

5099
if(EXISTS "${OSL_SHADERS_DIR}")
51100

@@ -56,6 +105,15 @@ if(DEFINED OSL_ROOT)
56105

57106
endif()
58107

108+
include (FindPackageHandleStandardArgs)
109+
find_package_handle_standard_args (OpenShadingLanguage
110+
FOUND_VAR OpenShadingLanguage_FOUND
111+
REQUIRED_VARS OSL_INCLUDE_DIR oslcomp_LIBRARY oslexec_LIBRARY OpenShadingLanguage_VERSION
112+
VERSION_VAR OpenShadingLanguage_VERSION
113+
)
114+
115+
set(OSL_FOUND ${OpenShadingLanguage_FOUND})
116+
59117
endif()
60118

61119
###############################################################################
@@ -64,16 +122,21 @@ endif()
64122
if(OSL_FOUND)
65123

66124
if (NOT OSL_FIND_QUIETLY)
67-
message(STATUS "OpenShadingLanguage includes = ${OSL_INCLUDE_DIR}")
68-
message(STATUS "OpenShadingLanguage oslcomp library = ${oslcomp_LIBRARY}")
69-
message(STATUS "OpenShadingLanguage oslexec library = ${oslexec_LIBRARY}")
125+
message(STATUS "OpenShadingLanguage includes = ${OSL_INCLUDE_DIR}")
126+
message(STATUS "OpenShadingLanguage shaders = ${OSL_SHADERS_DIR}")
127+
message(STATUS "OpenShadingLanguage library dir = ${OSL_LIB_DIR}")
70128
endif ()
71129

72-
set_target_properties(osl::osl PROPERTIES
73-
IMPORTED_LOCATION ${oslcomp_LIBRARY}
74-
IMPORTED_LOCATION ${oslexec_LIBRARY}
75-
INTERFACE_INCLUDE_DIRECTORIES ${OSL_INCLUDE_DIR}
76-
)
130+
list(APPEND LIB_INCLUDE_DIRS ${OSL_INCLUDE_DIR})
131+
list(APPEND LIB_INCLUDE_DIRS ${OSL_SHADERS_INCLUDE_DIR})
132+
133+
target_include_directories(osl::osl INTERFACE "${LIB_INCLUDE_DIRS}")
134+
target_link_libraries(osl::osl INTERFACE OSL::oslcomp OSL::oslexec)
135+
136+
if (${OpenShadingLanguage_VERSION} VERSION_GREATER_EQUAL "1.12" AND ${CMAKE_CXX_STANDARD} LESS_EQUAL 11)
137+
set(OSL_FOUND OFF)
138+
message(WARNING "Need C++14 or higher to compile OpenShadingLanguage. Skipping build the OSL unit tests")
139+
endif()
77140

78141
mark_as_advanced(OSL_INCLUDE_DIR
79142
oslcomp_LIBRARY oslcomp_FOUND

src/OpenColorIO/GpuShaderDesc.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,15 @@ void GpuShaderCreator::finalize()
308308

309309
kw.newLine() << "";
310310
kw.newLine() << "/* All the generic helper methods */";
311+
311312
kw.newLine() << "";
312-
kw.newLine() << "vector4 __operator__mul__(vector4 v, matrix m)";
313+
kw.newLine() << "vector4 __operator__mul__(matrix m, vector4 v)";
313314
kw.newLine() << "{";
314315
kw.indent();
315-
kw.newLine() << "return vector4(v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + v.w * m[3][0], "\
316-
"v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + v.w * m[3][1], "\
317-
"v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + v.w * m[3][2], "\
318-
"v.x * m[0][3] + v.y * m[1][3] + v.z * m[2][3] + v.w * m[3][3]);";
316+
kw.newLine() << "return vector4(v.x * m[0][0] + v.y * m[0][1] + v.z * m[0][2] + v.w * m[0][3], ";
317+
kw.newLine() << " v.x * m[1][0] + v.y * m[1][1] + v.z * m[1][2] + v.w * m[1][3], ";
318+
kw.newLine() << " v.x * m[2][0] + v.y * m[2][1] + v.z * m[2][2] + v.w * m[2][3], ";
319+
kw.newLine() << " v.x * m[3][0] + v.y * m[3][1] + v.z * m[3][2] + v.w * m[3][3]);";
319320
kw.dedent();
320321
kw.newLine() << "}";
321322

@@ -344,28 +345,32 @@ void GpuShaderCreator::finalize()
344345
kw.newLine() << "}";
345346

346347
kw.newLine() << "";
347-
kw.newLine() << "vector4 __operator__add__(vector4 v, color4 c) {";
348+
kw.newLine() << "vector4 __operator__add__(vector4 v, color4 c)";
349+
kw.newLine() << "{";
348350
kw.indent();
349351
kw.newLine() << "return v + vector4(c.rgb.r, c.rgb.g, c.rgb.b, c.a);";
350352
kw.dedent();
351353
kw.newLine() << "}";
352354

353355
kw.newLine() << "";
354-
kw.newLine() << "vector4 __operator__add__(color4 c, vector4 v) {";
356+
kw.newLine() << "vector4 __operator__add__(color4 c, vector4 v)";
357+
kw.newLine() << "{";
355358
kw.indent();
356359
kw.newLine() << "return vector4(c.rgb.r, c.rgb.g, c.rgb.b, c.a) + v;";
357360
kw.dedent();
358361
kw.newLine() << "}";
359362

360363
kw.newLine() << "";
361-
kw.newLine() << "vector4 pow(color4 c, vector4 v) {";
364+
kw.newLine() << "vector4 pow(color4 c, vector4 v)";
365+
kw.newLine() << "{";
362366
kw.indent();
363367
kw.newLine() << "return pow(vector4(c.rgb.r, c.rgb.g, c.rgb.b, c.a), v);";
364368
kw.dedent();
365369
kw.newLine() << "}";
366370

367371
kw.newLine() << "";
368-
kw.newLine() << "vector4 max(vector4 v, color4 c) {";
372+
kw.newLine() << "vector4 max(vector4 v, color4 c)";
373+
kw.newLine() << "{";
369374
kw.indent();
370375
kw.newLine() << "return max(v, vector4(c.rgb.r, c.rgb.g, c.rgb.b, c.a));";
371376
kw.dedent();

src/OpenColorIO/GpuShaderUtils.cpp

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,7 @@ void GpuShaderText::flushLine()
307307
m_ossLine.clear();
308308
}
309309

310-
std::string GpuShaderText::floatKeyword() const
311-
{
312-
return (m_lang == GPU_LANGUAGE_CG ? "half" : "float");
313-
}
314-
315-
std::string GpuShaderText::floatKeywordConst() const
310+
std::string GpuShaderText::constKeyword() const
316311
{
317312
std::string str;
318313

@@ -334,6 +329,19 @@ std::string GpuShaderText::floatKeywordConst() const
334329
break;
335330
}
336331

332+
return str;
333+
}
334+
335+
std::string GpuShaderText::floatKeyword() const
336+
{
337+
return (m_lang == GPU_LANGUAGE_CG ? "half" : "float");
338+
}
339+
340+
std::string GpuShaderText::floatKeywordConst() const
341+
{
342+
std::string str;
343+
344+
str += constKeyword();
337345
str += floatKeyword();
338346

339347
return str;
@@ -364,10 +372,20 @@ std::string GpuShaderText::colorDecl(const std::string & name) const
364372
return (m_lang==LANGUAGE_OSL_1 ? "color" : float3Keyword()) + " " + name;
365373
}
366374

375+
void GpuShaderText::declareVarConst(const std::string & name, float v)
376+
{
377+
newLine() << constKeyword() << declareVarStr(name, v) << ";";
378+
}
379+
380+
void GpuShaderText::declareVar(const std::string & name, float v)
381+
{
382+
newLine() << declareVarStr(name, v) << ";";
383+
}
384+
367385
// TODO: OSL: The method only solves the problem for constant float values. The code must also
368386
// support the in-place declarations (like res = t + vec3(...) for example).
369387

370-
void GpuShaderText::declareVar(const std::string & name, float v)
388+
std::string GpuShaderText::declareVarStr(const std::string & name, float v)
371389
{
372390
if (name.empty())
373391
{
@@ -393,21 +411,37 @@ void GpuShaderText::declareVar(const std::string & name, float v)
393411
oss.precision(std::numeric_limits<float>::max_digits10);
394412
oss << newVal;
395413

396-
newLine() << floatDecl(name) << " = " << oss.str() << ";";
397-
398-
return;
414+
return floatDecl(name) + " = " + oss.str();
399415
}
400416

401-
newLine() << floatDecl(name) << " = " << getFloatString(v, m_lang) << ";";
417+
return floatDecl(name) + " = " + getFloatString(v, m_lang);
418+
}
419+
420+
void GpuShaderText::declareVarConst(const std::string & name, bool v)
421+
{
422+
newLine() << constKeyword() << declareVarStr(name, v) << ";";
402423
}
403424

404425
void GpuShaderText::declareVar(const std::string & name, bool v)
426+
{
427+
newLine() << declareVarStr(name, v) << ";";
428+
}
429+
430+
std::string GpuShaderText::declareVarStr(const std::string & name, bool v)
405431
{
406432
if (name.empty())
407433
{
408434
throw Exception("GPU variable name is empty.");
409435
}
410-
newLine() << "bool " << name << " = " << (v ? "true;" : "false;");
436+
437+
if (m_lang==LANGUAGE_OSL_1)
438+
{
439+
return intKeyword() + " " + name + " = " + (v ? "1" : "0");
440+
}
441+
else
442+
{
443+
return "bool " + name + " = " + (v ? "true" : "false");
444+
}
411445
}
412446

413447
void GpuShaderText::declareFloatArrayConst(const std::string & name, int size, const float * v)
@@ -848,12 +882,7 @@ std::string matrix4Mul(const T * m4x4, const std::string & vecName, GpuLanguage
848882
}
849883
case LANGUAGE_OSL_1:
850884
{
851-
kw << vecName << " * matrix("
852-
<< m4x4[ 0] << ", " << m4x4[ 1] << ", " << m4x4[ 2] << ", " << m4x4[ 3] << ", "
853-
<< m4x4[ 4] << ", " << m4x4[ 5] << ", " << m4x4[ 6] << ", " << m4x4[ 7] << ", "
854-
<< m4x4[ 8] << ", " << m4x4[ 9] << ", " << m4x4[10] << ", " << m4x4[11] << ", "
855-
<< m4x4[12] << ", " << m4x4[13] << ", " << m4x4[14] << ", " << m4x4[15]
856-
<< ")";
885+
kw << "matrix(" << getMatrixValues<T, 4>(m4x4, lang, false) << ") * " << vecName;
857886
break;
858887
}
859888

@@ -964,7 +993,7 @@ std::string GpuShaderText::float4GreaterThan(const std::string & a,
964993
kw << float4Keyword() << "("
965994
<< "(" << a << "[0] > " << b << "[0]) ? 1.0 : 0.0, "
966995
<< "(" << a << "[1] > " << b << "[1]) ? 1.0 : 0.0, "
967-
<< "(" << a << "[2] > " << b << "[2]) ? 1.0 : 0.0) "
996+
<< "(" << a << "[2] > " << b << "[2]) ? 1.0 : 0.0, "
968997
<< "(" << a << "[3] > " << b << "[3]) ? 1.0 : 0.0)";
969998
break;
970999
}
@@ -974,7 +1003,7 @@ std::string GpuShaderText::float4GreaterThan(const std::string & a,
9741003
<< "(" << a << ".rgb.r > " << b << ".x) ? 1.0 : 0.0, "
9751004
<< "(" << a << ".rgb.g > " << b << ".y) ? 1.0 : 0.0, "
9761005
<< "(" << a << ".rgb.b > " << b << ".z) ? 1.0 : 0.0, "
977-
<< "(" << a << ".a > " << b << ".w) ? 1.0 : 0.0)";
1006+
<< "(" << a << ".a > " << b << ".w) ? 1.0 : 0.0)";
9781007
break;
9791008
}
9801009

@@ -1003,13 +1032,17 @@ std::string GpuShaderText::atan2(const std::string & y,
10031032
kw << "atan(" << y << ", " << x << ")";
10041033
break;
10051034
}
1006-
case LANGUAGE_OSL_1:
10071035
case GPU_LANGUAGE_HLSL_DX11:
10081036
{
10091037
// note: operand order is swapped in HLSL
10101038
kw << "atan2(" << x << ", " << y << ")";
10111039
break;
10121040
}
1041+
case LANGUAGE_OSL_1:
1042+
{
1043+
kw << "atan2(" << y << ", " << x << ")";
1044+
break;
1045+
}
10131046

10141047
default:
10151048
{
@@ -1037,7 +1070,6 @@ std::string GpuShaderText::sign(const std::string & v) const
10371070
}
10381071
case LANGUAGE_OSL_1:
10391072
{
1040-
// The challenge is only to return a vector4 type instead of a color4.
10411073
kw << "sign(" << float4Const(v + ".rgb.r", v + ".rgb.g",
10421074
v + ".rgb.b", v + ".a") << ");";
10431075
break;

0 commit comments

Comments
 (0)