Skip to content

Commit e87f5a6

Browse files
committed
opencl: Improve compatibility
Add a copy FindOpenCL.cmake from CMake 3.1.0 verbatim except the CMake BSD license header, and a path edit. Check if libOpenCL.so is compatible with CL/cl.h. If not, issue a warning, and revert to OpenCL 1.1 for the processor. Otherwise use OpenCL 1.2. This should provide a proper solution to the issue in #167.
1 parent 9ef2149 commit e87f5a6

File tree

5 files changed

+204
-79
lines changed

5 files changed

+204
-79
lines changed

CMakeLists.txt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,23 @@ ENDIF(ENABLE_OPENGL)
166166
IF(ENABLE_OPENCL)
167167
FIND_PACKAGE(OpenCL)
168168

169-
IF(OPENCL_FOUND)
169+
IF(OpenCL_FOUND)
170+
IF(UNIX AND NOT APPLE)
171+
INCLUDE(CheckOpenCLICDLoader)
172+
IF(OpenCL_C_WORKS AND NOT OpenCL_CXX_WORKS)
173+
SET(LIBFREENECT2_OPENCL_ICD_LOADER_IS_OLD 1)
174+
MESSAGE(WARNING "Your libOpenCL.so is incompatible with CL/cl.h. Install ocl-icd-opencl-dev to update libOpenCL.so?")
175+
ENDIF()
176+
ENDIF()
170177
SET(LIBFREENECT2_WITH_OPENCL_SUPPORT 1)
171-
INCLUDE_DIRECTORIES(${OPENCL_INCLUDE_DIRS})
178+
INCLUDE_DIRECTORIES(${OpenCL_INCLUDE_DIRS})
172179

173180
LIST(APPEND SOURCES
174181
src/opencl_depth_packet_processor.cpp
175182
)
176183

177184
LIST(APPEND LIBRARIES
178-
${OPENCL_LIBRARIES}
185+
${OpenCL_LIBRARIES}
179186
)
180187

181188
LIST(APPEND RESOURCES
@@ -188,7 +195,7 @@ IF(ENABLE_OPENCL)
188195
IF(UNIX AND NOT APPLE)
189196
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
190197
ENDIF()
191-
ENDIF(OPENCL_FOUND)
198+
ENDIF(OpenCL_FOUND)
192199
ENDIF(ENABLE_OPENCL)
193200

194201
# RPATH handling for private libusb copies
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
INCLUDE(CheckCXXSourceCompiles)
2+
INCLUDE(CheckCSourceCompiles)
3+
4+
SET(CMAKE_REQUIRED_INCLUDES "${MY_DIR}/include/internal" ${OpenCL_INCLUDE_DIRS})
5+
SET(CMAKE_REQUIRED_LIBRARIES ${OpenCL_LIBRARIES})
6+
CHECK_C_SOURCE_COMPILES("
7+
#include <CL/cl.h>
8+
int main() {
9+
clGetPlatformIDs(0, 0, 0);
10+
return 0;
11+
}" OpenCL_C_WORKS)
12+
CHECK_CXX_SOURCE_COMPILES("
13+
#include <CL/cl.hpp>
14+
int main() {
15+
cl::Context context;
16+
cl::Platform platform;
17+
cl::Device device;
18+
return 0;
19+
}" OpenCL_CXX_WORKS)
20+
SET(CMAKE_REQUIRED_INCLUDES)
21+
SET(CMAKE_REQUIRED_LIBRARIES)

cmake_modules/FindOpenCL.cmake

Lines changed: 151 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,155 @@
1-
# - Try to find OpenCL
2-
# This module tries to find an OpenCL implementation on your system. It supports
3-
# AMD / ATI, Apple and NVIDIA implementations, but should work, too.
1+
#.rst:
2+
# FindOpenCL
3+
# ----------
44
#
5-
# To set manually the paths, define these environment variables:
6-
# OpenCL_INCPATH - Include path (e.g. OpenCL_INCPATH=/opt/cuda/4.0/cuda/include)
7-
# OpenCL_LIBPATH - Library path (e.h. OpenCL_LIBPATH=/usr/lib64/nvidia)
5+
# Try to find OpenCL
86
#
9-
# Once done this will define
10-
# OPENCL_FOUND - system has OpenCL
11-
# OPENCL_INCLUDE_DIRS - the OpenCL include directory
12-
# OPENCL_LIBRARIES - link these to use OpenCL
7+
# Once done this will define::
8+
#
9+
# OpenCL_FOUND - True if OpenCL was found
10+
# OpenCL_INCLUDE_DIRS - include directories for OpenCL
11+
# OpenCL_LIBRARIES - link against this library to use OpenCL
12+
# OpenCL_VERSION_STRING - Highest supported OpenCL version (eg. 1.2)
13+
# OpenCL_VERSION_MAJOR - The major version of the OpenCL implementation
14+
# OpenCL_VERSION_MINOR - The minor version of the OpenCL implementation
15+
#
16+
# The module will also define two cache variables::
17+
#
18+
# OpenCL_INCLUDE_DIR - the OpenCL include directory
19+
# OpenCL_LIBRARY - the path to the OpenCL library
1320
#
1421

15-
FIND_PACKAGE(PackageHandleStandardArgs)
16-
17-
SET (OPENCL_VERSION_STRING "0.1.0")
18-
SET (OPENCL_VERSION_MAJOR 0)
19-
SET (OPENCL_VERSION_MINOR 1)
20-
SET (OPENCL_VERSION_PATCH 0)
21-
22-
IF (APPLE)
23-
24-
FIND_LIBRARY(OPENCL_LIBRARIES OpenCL DOC "OpenCL lib for OSX")
25-
FIND_PATH(OPENCL_INCLUDE_DIRS OpenCL/cl.h DOC "Include for OpenCL on OSX")
26-
FIND_PATH(_OPENCL_CPP_INCLUDE_DIRS OpenCL/cl.hpp DOC "Include for OpenCL CPP bindings on OSX")
27-
28-
ELSE (APPLE)
29-
30-
IF (WIN32)
31-
# The AMD SDK currently installs both x86 and x86_64 libraries
32-
# This is only a hack to find out architecture
33-
IF( ${CMAKE_SIZEOF_VOID_P} EQUAL 8 )
34-
FIND_LIBRARY(OPENCL_LIBRARIES OpenCL.lib PATHS "$ENV{ATISTREAMSDKROOT}" "$ENV{AMDAPPSDKROOT}" "$ENV{INTELOCLSDKROOT}" PATH_SUFFIXES "/lib/x86_64" "/lib/x64")
35-
ELSE (${CMAKE_SIZEOF_VOID_P} EQUAL 8)
36-
FIND_LIBRARY(OPENCL_LIBRARIES OpenCL.lib PATHS "$ENV{ATISTREAMSDKROOT}" "$ENV{AMDAPPSDKROOT}" "$ENV{INTELOCLSDKROOT}" PATH_SUFFIXES "/lib/x86")
37-
ENDIF( ${CMAKE_SIZEOF_VOID_P} EQUAL 8 )
38-
39-
# On Win32 search relative to the library
40-
FIND_PATH(OPENCL_INCLUDE_DIRS CL/cl.h PATHS "$ENV{ATISTREAMSDKROOT}" "$ENV{AMDAPPSDKROOT}" "$ENV{INTELOCLSDKROOT}" PATH_SUFFIXES "/include")
41-
FIND_PATH(_OPENCL_CPP_INCLUDE_DIRS CL/cl.hpp PATHS "$ENV{ATISTREAMSDKROOT}" "$ENV{AMDAPPSDKROOT}" "$ENV{INTELOCLSDKROOT}" PATH_SUFFIXES "/include")
42-
43-
ELSE (WIN32)
44-
45-
# Unix style platforms
46-
FIND_LIBRARY(OPENCL_LIBRARIES OpenCL
47-
PATHS ENV LD_LIBRARY_PATH ENV OpenCL_LIBPATH
48-
)
49-
50-
GET_FILENAME_COMPONENT(OPENCL_LIB_DIR ${OPENCL_LIBRARIES} PATH)
51-
GET_FILENAME_COMPONENT(_OPENCL_INC_CAND ${OPENCL_LIB_DIR}/../../include ABSOLUTE)
52-
53-
# The AMD SDK currently does not place its headers
54-
# in /usr/include, therefore also search relative
55-
# to the library
56-
FIND_PATH(OPENCL_INCLUDE_DIRS CL/cl.h PATHS ${_OPENCL_INC_CAND} "/usr/local/cuda/include" "/opt/AMDAPP/include" ENV OpenCL_INCPATH)
57-
FIND_PATH(_OPENCL_CPP_INCLUDE_DIRS CL/cl.hpp PATHS ${_OPENCL_INC_CAND} "/usr/local/cuda/include" "/opt/AMDAPP/include" ENV OpenCL_INCPATH)
58-
59-
ENDIF (WIN32)
60-
61-
ENDIF (APPLE)
62-
63-
FIND_PACKAGE_HANDLE_STANDARD_ARGS(OpenCL DEFAULT_MSG OPENCL_LIBRARIES OPENCL_INCLUDE_DIRS)
64-
65-
IF(_OPENCL_CPP_INCLUDE_DIRS)
66-
SET( OPENCL_HAS_CPP_BINDINGS TRUE )
67-
LIST( APPEND OPENCL_INCLUDE_DIRS ${_OPENCL_CPP_INCLUDE_DIRS} )
68-
# This is often the same, so clean up
69-
LIST( REMOVE_DUPLICATES OPENCL_INCLUDE_DIRS )
70-
ENDIF(_OPENCL_CPP_INCLUDE_DIRS)
71-
72-
MARK_AS_ADVANCED(
73-
OPENCL_INCLUDE_DIRS
74-
)
75-
22+
#=============================================================================
23+
# Copyright 2014 Matthaeus G. Chajdas
24+
#
25+
# Redistribution and use in source and binary forms, with or without
26+
# modification, are permitted provided that the following conditions
27+
# are met:
28+
#
29+
# * Redistributions of source code must retain the above copyright
30+
# notice, this list of conditions and the following disclaimer.
31+
#
32+
# * Redistributions in binary form must reproduce the above copyright
33+
# notice, this list of conditions and the following disclaimer in the
34+
# documentation and/or other materials provided with the distribution.
35+
#
36+
# * Neither the names of Kitware, Inc., the Insight Software Consortium,
37+
# nor the names of their contributors may be used to endorse or promote
38+
# products derived from this software without specific prior written
39+
# permission.
40+
#
41+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
42+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
43+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
44+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
45+
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
48+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
49+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
51+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52+
#=============================================================================
53+
54+
function(_FIND_OPENCL_VERSION)
55+
include(CheckSymbolExists)
56+
include(CMakePushCheckState)
57+
set(CMAKE_REQUIRED_QUIET ${OpenCL_FIND_QUIETLY})
58+
59+
CMAKE_PUSH_CHECK_STATE()
60+
foreach(VERSION "2_0" "1_2" "1_1" "1_0")
61+
set(CMAKE_REQUIRED_INCLUDES "${OpenCL_INCLUDE_DIR}")
62+
63+
if(APPLE)
64+
CHECK_SYMBOL_EXISTS(
65+
CL_VERSION_${VERSION}
66+
"${OpenCL_INCLUDE_DIR}/OpenCL/cl.h"
67+
OPENCL_VERSION_${VERSION})
68+
else()
69+
CHECK_SYMBOL_EXISTS(
70+
CL_VERSION_${VERSION}
71+
"${OpenCL_INCLUDE_DIR}/CL/cl.h"
72+
OPENCL_VERSION_${VERSION})
73+
endif()
74+
75+
if(OPENCL_VERSION_${VERSION})
76+
string(REPLACE "_" "." VERSION "${VERSION}")
77+
set(OpenCL_VERSION_STRING ${VERSION} PARENT_SCOPE)
78+
string(REGEX MATCHALL "[0-9]+" version_components "${VERSION}")
79+
list(GET version_components 0 major_version)
80+
list(GET version_components 1 minor_version)
81+
set(OpenCL_VERSION_MAJOR ${major_version} PARENT_SCOPE)
82+
set(OpenCL_VERSION_MINOR ${minor_version} PARENT_SCOPE)
83+
break()
84+
endif()
85+
endforeach()
86+
CMAKE_POP_CHECK_STATE()
87+
endfunction()
88+
89+
find_path(OpenCL_INCLUDE_DIR
90+
NAMES
91+
CL/cl.h OpenCL/cl.h
92+
PATHS
93+
ENV "PROGRAMFILES(X86)"
94+
ENV AMDAPPSDKROOT
95+
ENV INTELOCLSDKROOT
96+
ENV NVSDKCOMPUTE_ROOT
97+
ENV CUDA_PATH
98+
ENV ATISTREAMSDKROOT
99+
PATH_SUFFIXES
100+
include
101+
OpenCL/common/inc
102+
"AMD APP/include")
103+
104+
_FIND_OPENCL_VERSION()
105+
106+
if(WIN32)
107+
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
108+
find_library(OpenCL_LIBRARY
109+
NAMES OpenCL
110+
PATHS
111+
ENV "PROGRAMFILES(X86)"
112+
ENV AMDAPPSDKROOT
113+
ENV INTELOCLSDKROOT
114+
ENV CUDA_PATH
115+
ENV NVSDKCOMPUTE_ROOT
116+
ENV ATISTREAMSDKROOT
117+
PATH_SUFFIXES
118+
"AMD APP/lib/x86"
119+
lib/x86
120+
lib/Win32
121+
OpenCL/common/lib/Win32)
122+
elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
123+
find_library(OpenCL_LIBRARY
124+
NAMES OpenCL
125+
PATHS
126+
ENV "PROGRAMFILES(X86)"
127+
ENV AMDAPPSDKROOT
128+
ENV INTELOCLSDKROOT
129+
ENV CUDA_PATH
130+
ENV NVSDKCOMPUTE_ROOT
131+
ENV ATISTREAMSDKROOT
132+
PATH_SUFFIXES
133+
"AMD APP/lib/x86_64"
134+
lib/x86_64
135+
lib/x64
136+
OpenCL/common/lib/x64)
137+
endif()
138+
else()
139+
find_library(OpenCL_LIBRARY
140+
NAMES OpenCL)
141+
endif()
142+
143+
set(OpenCL_LIBRARIES ${OpenCL_LIBRARY})
144+
set(OpenCL_INCLUDE_DIRS ${OpenCL_INCLUDE_DIR})
145+
146+
include(FindPackageHandleStandardArgs)
147+
find_package_handle_standard_args(
148+
OpenCL
149+
FOUND_VAR OpenCL_FOUND
150+
REQUIRED_VARS OpenCL_LIBRARY OpenCL_INCLUDE_DIR
151+
VERSION_VAR OpenCL_VERSION_STRING)
152+
153+
mark_as_advanced(
154+
OpenCL_INCLUDE_DIR
155+
OpenCL_LIBRARY)

include/libfreenect2/config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#cmakedefine LIBFREENECT2_WITH_OPENGL_SUPPORT
4040

4141
#cmakedefine LIBFREENECT2_WITH_OPENCL_SUPPORT
42+
#cmakedefine LIBFREENECT2_OPENCL_ICD_LOADER_IS_OLD
4243

4344
#cmakedefine LIBFREENECT2_THREADING_STDLIB
4445

src/opencl_depth_packet_processor.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@
3636
#define _USE_MATH_DEFINES
3737
#include <math.h>
3838

39-
#ifdef __APPLE__
40-
#include <OpenCL/cl.hpp>
41-
#else
39+
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
40+
#define CL_USE_DEPRECATED_OPENCL_2_0_APIS
41+
42+
#ifdef LIBFREENECT2_OPENCL_ICD_LOADER_IS_OLD
4243
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
4344
#include <CL/cl.h>
45+
#ifdef CL_VERSION_1_2
4446
#undef CL_VERSION_1_2
47+
#endif //CL_VERSION_1_2
48+
#endif //LIBFREENECT2_OPENCL_ICD_LOADER_IS_OLD
49+
4550
#include <CL/cl.hpp>
46-
#endif
4751

4852
#ifndef REG_OPENCL_FILE
4953
#define REG_OPENCL_FILE ""
@@ -146,7 +150,19 @@ class OpenCLDepthPacketProcessorImpl: public WithPerfLogging
146150

147151
const int CL_ICDL_VERSION = 2;
148152
typedef cl_int (*icdloader_func)(int, size_t, void*, size_t*);
153+
#ifdef _MSC_VER
154+
#pragma warning(push)
155+
#pragma warning(disable : 4996)
156+
#else
157+
#pragma GCC diagnostic push
158+
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
159+
#endif
149160
icdloader_func clGetICDLoaderInfoOCLICD = (icdloader_func)clGetExtensionFunctionAddress("clGetICDLoaderInfoOCLICD");
161+
#ifdef _MSC_VER
162+
#pragma warning(pop)
163+
#else
164+
#pragma GCC diagnostic pop
165+
#endif
150166
if (clGetICDLoaderInfoOCLICD != NULL)
151167
{
152168
char buf[16];

0 commit comments

Comments
 (0)