Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adsk Contrib - Improve the OSL unit test framework #1514

4 changes: 4 additions & 0 deletions share/cmake/modules/FindImath.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ if(NOT Imath_FOUND)
)

add_dependencies(Imath::Imath imath_install)

# Some Imath versions define a second target.
add_library(Imath::ImathConfig ALIAS Imath::Imath)

message(STATUS "Installing Imath: ${Imath_LIBRARY} (version \"${Imath_VERSION}\")")
endif()
endif()
Expand Down
57 changes: 44 additions & 13 deletions share/cmake/modules/FindOpenShadingLanguage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@
# variable to tell CMake where to find it.
#

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


if(NOT TARGET osl::osl)
add_library(osl::osl UNKNOWN IMPORTED GLOBAL)
add_library(osl::osl INTERFACE IMPORTED GLOBAL)
set(OSL_FOUND OFF)
endif()

###############################################################################
### Try to find package ###

if(DEFINED OSL_ROOT)
if(NOT DEFINED OSL_ROOT)

find_package(OSL ${OpenShaderLanguage_VERSION} CONFIG)

# TODO: No variable to have the share directory?

set(OSL_SHADERS_INCLUDE_DIR ${OSL_INCLUDE_DIR}/../share)

# Variable used by the OSL unit tests.
set(OSL_SHADERS_DIR ${OSL_SHADERS_INCLUDE_DIR}/OSL/shaders)

else()

set(OSL_INCLUDE_DIR ${OSL_ROOT}/include)

Expand All @@ -34,6 +43,11 @@ if(DEFINED OSL_ROOT)
lib
)

add_library(OSL::oslcomp SHARED IMPORTED)
set_target_properties(OSL::oslcomp PROPERTIES
IMPORTED_LOCATION ${oslcomp_LIBRARY}
)

# Find the oslexec library.
find_library(oslexec_LIBRARY
NAMES
Expand All @@ -44,8 +58,15 @@ if(DEFINED OSL_ROOT)
lib
)

add_library(OSL::oslexec SHARED IMPORTED)
set_target_properties(OSL::oslexec PROPERTIES
IMPORTED_LOCATION ${oslexec_LIBRARY}
)

set(OSL_SHADERS_INCLUDE_DIR ${OSL_ROOT}/share)

# Variable used by the OSL unit tests.
set(OSL_SHADERS_DIR ${OSL_ROOT}/share/OSL/shaders)
set(OSL_SHADERS_DIR ${OSL_SHADERS_INCLUDE_DIR}/OSL/shaders)

if(EXISTS "${OSL_SHADERS_DIR}")

Expand All @@ -58,22 +79,32 @@ if(DEFINED OSL_ROOT)

endif()

###############################################################################
### Check the C++ version ###

# TODO: Which version starts to impose C++14?

if(${CMAKE_CXX_STANDARD} LESS_EQUAL 11)
set(OSL_FOUND OFF)
message(WARNING "Need C++14 or higher to compile OpenShadingLanguage. Skipping build the OSL unit tests")
endif()

###############################################################################
### Configure target ###

if(OSL_FOUND)

if (NOT OSL_FIND_QUIETLY)
message(STATUS "OpenShadingLanguage includes = ${OSL_INCLUDE_DIR}")
message(STATUS "OpenShadingLanguage oslcomp library = ${oslcomp_LIBRARY}")
message(STATUS "OpenShadingLanguage oslexec library = ${oslexec_LIBRARY}")
message(STATUS "OpenShadingLanguage includes = ${OSL_INCLUDE_DIR}")
message(STATUS "OpenShadingLanguage shaders = ${OSL_SHADERS_DIR}")
message(STATUS "OpenShadingLanguage library dir = ${OSL_LIB_DIR}")
endif ()

set_target_properties(osl::osl PROPERTIES
IMPORTED_LOCATION ${oslcomp_LIBRARY}
IMPORTED_LOCATION ${oslexec_LIBRARY}
INTERFACE_INCLUDE_DIRECTORIES ${OSL_INCLUDE_DIR}
)
list(APPEND LIB_INCLUDE_DIRS ${OSL_INCLUDE_DIR})
list(APPEND LIB_INCLUDE_DIRS ${OSL_SHADERS_INCLUDE_DIR})

target_include_directories(osl::osl INTERFACE "${LIB_INCLUDE_DIRS}")
target_link_libraries(osl::osl INTERFACE OSL::oslcomp OSL::oslexec)

mark_as_advanced(OSL_INCLUDE_DIR
oslcomp_LIBRARY oslcomp_FOUND
Expand Down
23 changes: 14 additions & 9 deletions src/OpenColorIO/GpuShaderDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,15 @@ void GpuShaderCreator::finalize()

kw.newLine() << "";
kw.newLine() << "/* All the generic helper methods */";

kw.newLine() << "";
kw.newLine() << "vector4 __operator__mul__(vector4 v, matrix m)";
kw.newLine() << "vector4 __operator__mul__(matrix m, vector4 v)";
kw.newLine() << "{";
kw.indent();
kw.newLine() << "return vector4(v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + v.w * m[3][0], "\
"v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + v.w * m[3][1], "\
"v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + v.w * m[3][2], "\
"v.x * m[0][3] + v.y * m[1][3] + v.z * m[2][3] + v.w * m[3][3]);";
kw.newLine() << "return vector4(v.x * m[0][0] + v.y * m[0][1] + v.z * m[0][2] + v.w * m[0][3], ";
kw.newLine() << " v.x * m[1][0] + v.y * m[1][1] + v.z * m[1][2] + v.w * m[1][3], ";
kw.newLine() << " v.x * m[2][0] + v.y * m[2][1] + v.z * m[2][2] + v.w * m[2][3], ";
kw.newLine() << " v.x * m[3][0] + v.y * m[3][1] + v.z * m[3][2] + v.w * m[3][3]);";
kw.dedent();
kw.newLine() << "}";

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

kw.newLine() << "";
kw.newLine() << "vector4 __operator__add__(vector4 v, color4 c) {";
kw.newLine() << "vector4 __operator__add__(vector4 v, color4 c)";
kw.newLine() << "{";
kw.indent();
kw.newLine() << "return v + vector4(c.rgb.r, c.rgb.g, c.rgb.b, c.a);";
kw.dedent();
kw.newLine() << "}";

kw.newLine() << "";
kw.newLine() << "vector4 __operator__add__(color4 c, vector4 v) {";
kw.newLine() << "vector4 __operator__add__(color4 c, vector4 v)";
kw.newLine() << "{";
kw.indent();
kw.newLine() << "return vector4(c.rgb.r, c.rgb.g, c.rgb.b, c.a) + v;";
kw.dedent();
kw.newLine() << "}";

kw.newLine() << "";
kw.newLine() << "vector4 pow(color4 c, vector4 v) {";
kw.newLine() << "vector4 pow(color4 c, vector4 v)";
kw.newLine() << "{";
kw.indent();
kw.newLine() << "return pow(vector4(c.rgb.r, c.rgb.g, c.rgb.b, c.a), v);";
kw.dedent();
kw.newLine() << "}";

kw.newLine() << "";
kw.newLine() << "vector4 max(vector4 v, color4 c) {";
kw.newLine() << "vector4 max(vector4 v, color4 c)";
kw.newLine() << "{";
kw.indent();
kw.newLine() << "return max(v, vector4(c.rgb.r, c.rgb.g, c.rgb.b, c.a));";
kw.dedent();
Expand Down
76 changes: 54 additions & 22 deletions src/OpenColorIO/GpuShaderUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,7 @@ void GpuShaderText::flushLine()
m_ossLine.clear();
}

std::string GpuShaderText::floatKeyword() const
{
return (m_lang == GPU_LANGUAGE_CG ? "half" : "float");
}

std::string GpuShaderText::floatKeywordConst() const
std::string GpuShaderText::constKeyword() const
{
std::string str;

Expand All @@ -334,6 +329,19 @@ std::string GpuShaderText::floatKeywordConst() const
break;
}

return str;
}

std::string GpuShaderText::floatKeyword() const
{
return (m_lang == GPU_LANGUAGE_CG ? "half" : "float");
}

std::string GpuShaderText::floatKeywordConst() const
{
std::string str;

str += constKeyword();
str += floatKeyword();

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

void GpuShaderText::declareVarConst(const std::string & name, float v)
{
newLine() << constKeyword() << declareVarStr(name, v) << ";";
}

void GpuShaderText::declareVar(const std::string & name, float v)
{
newLine() << declareVarStr(name, v) << ";";
}

// TODO: OSL: The method only solves the problem for constant float values. The code must also
// support the in-place declarations (like res = t + vec3(...) for example).

void GpuShaderText::declareVar(const std::string & name, float v)
std::string GpuShaderText::declareVarStr(const std::string & name, float v)
{
if (name.empty())
{
Expand All @@ -393,21 +411,37 @@ void GpuShaderText::declareVar(const std::string & name, float v)
oss.precision(std::numeric_limits<float>::max_digits10);
oss << newVal;

newLine() << floatDecl(name) << " = " << oss.str() << ";";

return;
return floatDecl(name) + " = " + oss.str();
}

newLine() << floatDecl(name) << " = " << getFloatString(v, m_lang) << ";";
return floatDecl(name) + " = " + getFloatString(v, m_lang);
}

void GpuShaderText::declareVarConst(const std::string & name, bool v)
{
newLine() << constKeyword() << declareVarStr(name, v) << ";";
}

void GpuShaderText::declareVar(const std::string & name, bool v)
{
newLine() << declareVarStr(name, v) << ";";
}

std::string GpuShaderText::declareVarStr(const std::string & name, bool v)
{
if (name.empty())
{
throw Exception("GPU variable name is empty.");
}
newLine() << "bool " << name << " = " << (v ? "true;" : "false;");

if (m_lang==LANGUAGE_OSL_1)
{
return intKeyword() + " " + name + " = " + (v ? "1" : "0");
}
else
{
return "bool " + name + " = " + (v ? "true" : "false");
}
}

void GpuShaderText::declareFloatArrayConst(const std::string & name, int size, const float * v)
Expand Down Expand Up @@ -848,12 +882,7 @@ std::string matrix4Mul(const T * m4x4, const std::string & vecName, GpuLanguage
}
case LANGUAGE_OSL_1:
{
kw << vecName << " * matrix("
<< m4x4[ 0] << ", " << m4x4[ 1] << ", " << m4x4[ 2] << ", " << m4x4[ 3] << ", "
<< m4x4[ 4] << ", " << m4x4[ 5] << ", " << m4x4[ 6] << ", " << m4x4[ 7] << ", "
<< m4x4[ 8] << ", " << m4x4[ 9] << ", " << m4x4[10] << ", " << m4x4[11] << ", "
<< m4x4[12] << ", " << m4x4[13] << ", " << m4x4[14] << ", " << m4x4[15]
<< ")";
kw << "matrix(" << getMatrixValues<T, 4>(m4x4, lang, false) << ") * " << vecName;
break;
}

Expand Down Expand Up @@ -964,7 +993,7 @@ std::string GpuShaderText::float4GreaterThan(const std::string & a,
kw << float4Keyword() << "("
<< "(" << a << "[0] > " << b << "[0]) ? 1.0 : 0.0, "
<< "(" << a << "[1] > " << b << "[1]) ? 1.0 : 0.0, "
<< "(" << a << "[2] > " << b << "[2]) ? 1.0 : 0.0) "
<< "(" << a << "[2] > " << b << "[2]) ? 1.0 : 0.0, "
<< "(" << a << "[3] > " << b << "[3]) ? 1.0 : 0.0)";
break;
}
Expand All @@ -974,7 +1003,7 @@ std::string GpuShaderText::float4GreaterThan(const std::string & a,
<< "(" << a << ".rgb.r > " << b << ".x) ? 1.0 : 0.0, "
<< "(" << a << ".rgb.g > " << b << ".y) ? 1.0 : 0.0, "
<< "(" << a << ".rgb.b > " << b << ".z) ? 1.0 : 0.0, "
<< "(" << a << ".a > " << b << ".w) ? 1.0 : 0.0)";
<< "(" << a << ".a > " << b << ".w) ? 1.0 : 0.0)";
break;
}

Expand Down Expand Up @@ -1003,13 +1032,17 @@ std::string GpuShaderText::atan2(const std::string & y,
kw << "atan(" << y << ", " << x << ")";
break;
}
case LANGUAGE_OSL_1:
case GPU_LANGUAGE_HLSL_DX11:
{
// note: operand order is swapped in HLSL
kw << "atan2(" << x << ", " << y << ")";
break;
}
case LANGUAGE_OSL_1:
{
kw << "atan2(" << y << ", " << x << ")";
break;
}

default:
{
Expand Down Expand Up @@ -1037,7 +1070,6 @@ std::string GpuShaderText::sign(const std::string & v) const
}
case LANGUAGE_OSL_1:
{
// The challenge is only to return a vector4 type instead of a color4.
kw << "sign(" << float4Const(v + ".rgb.r", v + ".rgb.g",
v + ".rgb.b", v + ".a") << ");";
break;
Expand Down
15 changes: 9 additions & 6 deletions src/OpenColorIO/GpuShaderUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class GpuShaderText
// Basic types.
//

std::string constKeyword() const;

std::string floatKeyword() const;
std::string floatKeywordConst() const;
std::string floatDecl(const std::string& name) const;
Expand All @@ -80,10 +82,14 @@ class GpuShaderText
//

// Declare a float variable.
std::string declareVarStr(const std::string & name, float v);
hodoulp marked this conversation as resolved.
Show resolved Hide resolved
void declareVar(const std::string & name, float v);
void declareVarConst(const std::string & name, float v);

// Declare a bool variable.
std::string declareVarStr(const std::string & name, bool v);
void declareVar(const std::string & name, bool v);
void declareVarConst(const std::string & name, bool v);

// Declare a float array variable.
void declareFloatArrayConst(const std::string & name, int size, const float * v);
Expand All @@ -108,8 +114,7 @@ class GpuShaderText
std::string float3Const(float x, float y, float z) const;
std::string float3Const(double x, double y, double z) const;
// Get the string for creating constant vector with three elements
std::string float3Const(const std::string& x, const std::string& y,
const std::string& z) const;
std::string float3Const(const std::string& x, const std::string& y, const std::string& z) const;
// Get the string for creating constant vector with three elements
std::string float3Const(float v) const;
std::string float3Const(double v) const;
Expand All @@ -119,11 +124,9 @@ class GpuShaderText
std::string float3Decl(const std::string& name) const;

// Declare and initialize a vector with three elements
void declareFloat3(const std::string& name,
float x, float y, float z);
void declareFloat3(const std::string& name, float x, float y, float z);
void declareFloat3(const std::string& name, const Float3 & vec3);
void declareFloat3(const std::string& name,
double x, double y, double z);
void declareFloat3(const std::string& name, double x, double y, double z);
// Declare and initialize a vector with three elements
void declareFloat3(const std::string& name,
const std::string& x, const std::string& y, const std::string& z);
Expand Down
Loading