diff --git a/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_gl.hpp b/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_gl.hpp new file mode 100644 index 0000000000000000000000000000000000000000..f37ad158e12e1fe91ad545d4dd7fb496adf84ead --- /dev/null +++ b/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_gl.hpp @@ -0,0 +1,58 @@ +// +// AUTOGENERATED, DO NOT EDIT +// +#ifndef __OPENCV_CORE_OCL_RUNTIME_OPENCL_GL_HPP__ +#error "Invalid usage" +#endif + +// generated by parser_cl.py +#define clCreateFromGLBuffer clCreateFromGLBuffer_ +#define clCreateFromGLRenderbuffer clCreateFromGLRenderbuffer_ +#define clCreateFromGLTexture clCreateFromGLTexture_ +#define clCreateFromGLTexture2D clCreateFromGLTexture2D_ +#define clCreateFromGLTexture3D clCreateFromGLTexture3D_ +#define clEnqueueAcquireGLObjects clEnqueueAcquireGLObjects_ +#define clEnqueueReleaseGLObjects clEnqueueReleaseGLObjects_ +#define clGetGLContextInfoKHR clGetGLContextInfoKHR_ +#define clGetGLObjectInfo clGetGLObjectInfo_ +#define clGetGLTextureInfo clGetGLTextureInfo_ + +#if defined __APPLE__ +#include +#else +#include +#endif + +// generated by parser_cl.py +#undef clCreateFromGLBuffer +#define clCreateFromGLBuffer clCreateFromGLBuffer_pfn +#undef clCreateFromGLRenderbuffer +#define clCreateFromGLRenderbuffer clCreateFromGLRenderbuffer_pfn +#undef clCreateFromGLTexture +#define clCreateFromGLTexture clCreateFromGLTexture_pfn +#undef clCreateFromGLTexture2D +#define clCreateFromGLTexture2D clCreateFromGLTexture2D_pfn +#undef clCreateFromGLTexture3D +#define clCreateFromGLTexture3D clCreateFromGLTexture3D_pfn +#undef clEnqueueAcquireGLObjects +#define clEnqueueAcquireGLObjects clEnqueueAcquireGLObjects_pfn +#undef clEnqueueReleaseGLObjects +#define clEnqueueReleaseGLObjects clEnqueueReleaseGLObjects_pfn +#undef clGetGLContextInfoKHR +#define clGetGLContextInfoKHR clGetGLContextInfoKHR_pfn +#undef clGetGLObjectInfo +#define clGetGLObjectInfo clGetGLObjectInfo_pfn +#undef clGetGLTextureInfo +#define clGetGLTextureInfo clGetGLTextureInfo_pfn + +// generated by parser_cl.py +extern CL_RUNTIME_EXPORT cl_mem (CL_API_CALL*clCreateFromGLBuffer)(cl_context, cl_mem_flags, cl_GLuint, int*); +extern CL_RUNTIME_EXPORT cl_mem (CL_API_CALL*clCreateFromGLRenderbuffer)(cl_context, cl_mem_flags, cl_GLuint, cl_int*); +extern CL_RUNTIME_EXPORT cl_mem (CL_API_CALL*clCreateFromGLTexture)(cl_context, cl_mem_flags, cl_GLenum, cl_GLint, cl_GLuint, cl_int*); +extern CL_RUNTIME_EXPORT cl_mem (CL_API_CALL*clCreateFromGLTexture2D)(cl_context, cl_mem_flags, cl_GLenum, cl_GLint, cl_GLuint, cl_int*); +extern CL_RUNTIME_EXPORT cl_mem (CL_API_CALL*clCreateFromGLTexture3D)(cl_context, cl_mem_flags, cl_GLenum, cl_GLint, cl_GLuint, cl_int*); +extern CL_RUNTIME_EXPORT cl_int (CL_API_CALL*clEnqueueAcquireGLObjects)(cl_command_queue, cl_uint, const cl_mem*, cl_uint, const cl_event*, cl_event*); +extern CL_RUNTIME_EXPORT cl_int (CL_API_CALL*clEnqueueReleaseGLObjects)(cl_command_queue, cl_uint, const cl_mem*, cl_uint, const cl_event*, cl_event*); +extern CL_RUNTIME_EXPORT cl_int (CL_API_CALL*clGetGLContextInfoKHR)(const cl_context_properties*, cl_gl_context_info, size_t, void*, size_t*); +extern CL_RUNTIME_EXPORT cl_int (CL_API_CALL*clGetGLObjectInfo)(cl_mem, cl_gl_object_type*, cl_GLuint*); +extern CL_RUNTIME_EXPORT cl_int (CL_API_CALL*clGetGLTextureInfo)(cl_mem, cl_gl_texture_info, size_t, void*, size_t*); diff --git a/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_gl_wrappers.hpp b/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_gl_wrappers.hpp new file mode 100644 index 0000000000000000000000000000000000000000..105867f47aa7bd2d619bdd9ab0c4a7bcee087307 --- /dev/null +++ b/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_gl_wrappers.hpp @@ -0,0 +1,38 @@ +// +// AUTOGENERATED, DO NOT EDIT +// +#ifndef __OPENCV_CORE_OCL_RUNTIME_OPENCL_GL_WRAPPERS_HPP__ +#error "Invalid usage" +#endif + +// generated by parser_cl.py +#undef clCreateFromGLBuffer +#define clCreateFromGLBuffer clCreateFromGLBuffer_fn +inline cl_mem clCreateFromGLBuffer(cl_context p0, cl_mem_flags p1, cl_GLuint p2, int* p3) { return clCreateFromGLBuffer_pfn(p0, p1, p2, p3); } +#undef clCreateFromGLRenderbuffer +#define clCreateFromGLRenderbuffer clCreateFromGLRenderbuffer_fn +inline cl_mem clCreateFromGLRenderbuffer(cl_context p0, cl_mem_flags p1, cl_GLuint p2, cl_int* p3) { return clCreateFromGLRenderbuffer_pfn(p0, p1, p2, p3); } +#undef clCreateFromGLTexture +#define clCreateFromGLTexture clCreateFromGLTexture_fn +inline cl_mem clCreateFromGLTexture(cl_context p0, cl_mem_flags p1, cl_GLenum p2, cl_GLint p3, cl_GLuint p4, cl_int* p5) { return clCreateFromGLTexture_pfn(p0, p1, p2, p3, p4, p5); } +#undef clCreateFromGLTexture2D +#define clCreateFromGLTexture2D clCreateFromGLTexture2D_fn +inline cl_mem clCreateFromGLTexture2D(cl_context p0, cl_mem_flags p1, cl_GLenum p2, cl_GLint p3, cl_GLuint p4, cl_int* p5) { return clCreateFromGLTexture2D_pfn(p0, p1, p2, p3, p4, p5); } +#undef clCreateFromGLTexture3D +#define clCreateFromGLTexture3D clCreateFromGLTexture3D_fn +inline cl_mem clCreateFromGLTexture3D(cl_context p0, cl_mem_flags p1, cl_GLenum p2, cl_GLint p3, cl_GLuint p4, cl_int* p5) { return clCreateFromGLTexture3D_pfn(p0, p1, p2, p3, p4, p5); } +#undef clEnqueueAcquireGLObjects +#define clEnqueueAcquireGLObjects clEnqueueAcquireGLObjects_fn +inline cl_int clEnqueueAcquireGLObjects(cl_command_queue p0, cl_uint p1, const cl_mem* p2, cl_uint p3, const cl_event* p4, cl_event* p5) { return clEnqueueAcquireGLObjects_pfn(p0, p1, p2, p3, p4, p5); } +#undef clEnqueueReleaseGLObjects +#define clEnqueueReleaseGLObjects clEnqueueReleaseGLObjects_fn +inline cl_int clEnqueueReleaseGLObjects(cl_command_queue p0, cl_uint p1, const cl_mem* p2, cl_uint p3, const cl_event* p4, cl_event* p5) { return clEnqueueReleaseGLObjects_pfn(p0, p1, p2, p3, p4, p5); } +#undef clGetGLContextInfoKHR +#define clGetGLContextInfoKHR clGetGLContextInfoKHR_fn +inline cl_int clGetGLContextInfoKHR(const cl_context_properties* p0, cl_gl_context_info p1, size_t p2, void* p3, size_t* p4) { return clGetGLContextInfoKHR_pfn(p0, p1, p2, p3, p4); } +#undef clGetGLObjectInfo +#define clGetGLObjectInfo clGetGLObjectInfo_fn +inline cl_int clGetGLObjectInfo(cl_mem p0, cl_gl_object_type* p1, cl_GLuint* p2) { return clGetGLObjectInfo_pfn(p0, p1, p2); } +#undef clGetGLTextureInfo +#define clGetGLTextureInfo clGetGLTextureInfo_fn +inline cl_int clGetGLTextureInfo(cl_mem p0, cl_gl_texture_info p1, size_t p2, void* p3, size_t* p4) { return clGetGLTextureInfo_pfn(p0, p1, p2, p3, p4); } diff --git a/modules/core/include/opencv2/core/opencl/runtime/opencl_gl.hpp b/modules/core/include/opencv2/core/opencl/runtime/opencl_gl.hpp new file mode 100644 index 0000000000000000000000000000000000000000..7c7a82e9ea5ff4814c01bac988d93081d1429be4 --- /dev/null +++ b/modules/core/include/opencv2/core/opencl/runtime/opencl_gl.hpp @@ -0,0 +1,65 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the OpenCV Foundation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef __OPENCV_CORE_OCL_RUNTIME_OPENCL_GL_HPP__ +#define __OPENCV_CORE_OCL_RUNTIME_OPENCL_GL_HPP__ + +#if defined HAVE_OPENCL && defined HAVE_OPENGL + +#include "opencl_core.hpp" + +#if defined(HAVE_OPENCL_STATIC) + +#if defined __APPLE__ +#include +#else +#include +#endif + +#else // HAVE_OPENCL_STATIC + +#include "autogenerated/opencl_gl.hpp" + +#endif // HAVE_OPENCL_STATIC + +#endif // defined HAVE_OPENCL && defined HAVE_OPENGL + +#endif // __OPENCV_CORE_OCL_RUNTIME_OPENCL_GL_HPP__ diff --git a/modules/core/include/opencv2/core/opencl/runtime/opencl_gl_wrappers.hpp b/modules/core/include/opencv2/core/opencl/runtime/opencl_gl_wrappers.hpp new file mode 100644 index 0000000000000000000000000000000000000000..9327d2ede684ff543eb0eaba1489b7fa5c865b39 --- /dev/null +++ b/modules/core/include/opencv2/core/opencl/runtime/opencl_gl_wrappers.hpp @@ -0,0 +1,47 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the OpenCV Foundation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef __OPENCV_CORE_OCL_RUNTIME_OPENCL_GL_WRAPPERS_HPP__ +#define __OPENCV_CORE_OCL_RUNTIME_OPENCL_GL_WRAPPERS_HPP__ + +#include "autogenerated/opencl_gl_wrappers.hpp" + +#endif // __OPENCV_CORE_OCL_RUNTIME_OPENCL_GL_WRAPPERS_HPP__ diff --git a/modules/core/include/opencv2/core/opengl.hpp b/modules/core/include/opencv2/core/opengl.hpp index 15c635c8804b1b180ce17522c846f2d8be4ad906..8c3235f5a36269c5b5b85b57256b67212e46f7dc 100644 --- a/modules/core/include/opencv2/core/opengl.hpp +++ b/modules/core/include/opencv2/core/opengl.hpp @@ -48,6 +48,7 @@ #endif #include "opencv2/core.hpp" +#include "ocl.hpp" namespace cv { namespace ogl { @@ -511,7 +512,30 @@ CV_EXPORTS void render(const Arrays& arr, int mode = POINTS, Scalar color = Scal */ CV_EXPORTS void render(const Arrays& arr, InputArray indices, int mode = POINTS, Scalar color = Scalar::all(255)); -//! @} core_opengl +/////////////////// CL-GL Interoperability Functions /////////////////// + +namespace ocl { +using namespace cv::ocl; + +// TODO static functions in the Context class +/** @brief Creates OpenCL context from GL. +@return Returns reference to OpenCL Context + */ +CV_EXPORTS Context& initializeContextFromGL(); + +} // namespace cv::ogl::ocl + +/** @brief Converts InputArray to Texture2D object. +@param src - source InputArray. +@param texture - destination Texture2D object. + */ +CV_EXPORTS void convertToGLTexture2D(InputArray src, Texture2D& texture); + +/** @brief Converts Texture2D object to OutputArray. +@param texture - source Texture2D object. +@param dst - destination OutputArray. + */ +CV_EXPORTS void convertFromGLTexture2D(const Texture2D& texture, OutputArray dst); }} // namespace cv::ogl diff --git a/modules/core/src/opencl/runtime/autogenerated/opencl_gl_impl.hpp b/modules/core/src/opencl/runtime/autogenerated/opencl_gl_impl.hpp new file mode 100644 index 0000000000000000000000000000000000000000..6d9718050843ff0bdb7b63b9b41239a857e949cc --- /dev/null +++ b/modules/core/src/opencl/runtime/autogenerated/opencl_gl_impl.hpp @@ -0,0 +1,198 @@ +// +// AUTOGENERATED, DO NOT EDIT +// +// generated by parser_cl.py +enum OPENCL_GL_FN_ID { + OPENCL_GL_FN_clCreateFromGLBuffer = 0, + OPENCL_GL_FN_clCreateFromGLRenderbuffer = 1, + OPENCL_GL_FN_clCreateFromGLTexture = 2, + OPENCL_GL_FN_clCreateFromGLTexture2D = 3, + OPENCL_GL_FN_clCreateFromGLTexture3D = 4, + OPENCL_GL_FN_clEnqueueAcquireGLObjects = 5, + OPENCL_GL_FN_clEnqueueReleaseGLObjects = 6, + OPENCL_GL_FN_clGetGLContextInfoKHR = 7, + OPENCL_GL_FN_clGetGLObjectInfo = 8, + OPENCL_GL_FN_clGetGLTextureInfo = 9, +}; + +namespace { +// generated by parser_cl.py +template +struct opencl_gl_fn0 +{ + typedef _R (CL_API_CALL*FN)(); + static _R CL_API_CALL switch_fn() + { return ((FN)opencl_gl_check_fn(ID))(); } +}; + +template +struct opencl_gl_fn1 +{ + typedef _R (CL_API_CALL*FN)(_T1); + static _R CL_API_CALL switch_fn(_T1 p1) + { return ((FN)opencl_gl_check_fn(ID))(p1); } +}; + +template +struct opencl_gl_fn2 +{ + typedef _R (CL_API_CALL*FN)(_T1, _T2); + static _R CL_API_CALL switch_fn(_T1 p1, _T2 p2) + { return ((FN)opencl_gl_check_fn(ID))(p1, p2); } +}; + +template +struct opencl_gl_fn3 +{ + typedef _R (CL_API_CALL*FN)(_T1, _T2, _T3); + static _R CL_API_CALL switch_fn(_T1 p1, _T2 p2, _T3 p3) + { return ((FN)opencl_gl_check_fn(ID))(p1, p2, p3); } +}; + +template +struct opencl_gl_fn4 +{ + typedef _R (CL_API_CALL*FN)(_T1, _T2, _T3, _T4); + static _R CL_API_CALL switch_fn(_T1 p1, _T2 p2, _T3 p3, _T4 p4) + { return ((FN)opencl_gl_check_fn(ID))(p1, p2, p3, p4); } +}; + +template +struct opencl_gl_fn5 +{ + typedef _R (CL_API_CALL*FN)(_T1, _T2, _T3, _T4, _T5); + static _R CL_API_CALL switch_fn(_T1 p1, _T2 p2, _T3 p3, _T4 p4, _T5 p5) + { return ((FN)opencl_gl_check_fn(ID))(p1, p2, p3, p4, p5); } +}; + +template +struct opencl_gl_fn6 +{ + typedef _R (CL_API_CALL*FN)(_T1, _T2, _T3, _T4, _T5, _T6); + static _R CL_API_CALL switch_fn(_T1 p1, _T2 p2, _T3 p3, _T4 p4, _T5 p5, _T6 p6) + { return ((FN)opencl_gl_check_fn(ID))(p1, p2, p3, p4, p5, p6); } +}; + +template +struct opencl_gl_fn7 +{ + typedef _R (CL_API_CALL*FN)(_T1, _T2, _T3, _T4, _T5, _T6, _T7); + static _R CL_API_CALL switch_fn(_T1 p1, _T2 p2, _T3 p3, _T4 p4, _T5 p5, _T6 p6, _T7 p7) + { return ((FN)opencl_gl_check_fn(ID))(p1, p2, p3, p4, p5, p6, p7); } +}; + +template +struct opencl_gl_fn8 +{ + typedef _R (CL_API_CALL*FN)(_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8); + static _R CL_API_CALL switch_fn(_T1 p1, _T2 p2, _T3 p3, _T4 p4, _T5 p5, _T6 p6, _T7 p7, _T8 p8) + { return ((FN)opencl_gl_check_fn(ID))(p1, p2, p3, p4, p5, p6, p7, p8); } +}; + +template +struct opencl_gl_fn9 +{ + typedef _R (CL_API_CALL*FN)(_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9); + static _R CL_API_CALL switch_fn(_T1 p1, _T2 p2, _T3 p3, _T4 p4, _T5 p5, _T6 p6, _T7 p7, _T8 p8, _T9 p9) + { return ((FN)opencl_gl_check_fn(ID))(p1, p2, p3, p4, p5, p6, p7, p8, p9); } +}; + +template +struct opencl_gl_fn10 +{ + typedef _R (CL_API_CALL*FN)(_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10); + static _R CL_API_CALL switch_fn(_T1 p1, _T2 p2, _T3 p3, _T4 p4, _T5 p5, _T6 p6, _T7 p7, _T8 p8, _T9 p9, _T10 p10) + { return ((FN)opencl_gl_check_fn(ID))(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); } +}; + +template +struct opencl_gl_fn11 +{ + typedef _R (CL_API_CALL*FN)(_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10, _T11); + static _R CL_API_CALL switch_fn(_T1 p1, _T2 p2, _T3 p3, _T4 p4, _T5 p5, _T6 p6, _T7 p7, _T8 p8, _T9 p9, _T10 p10, _T11 p11) + { return ((FN)opencl_gl_check_fn(ID))(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11); } +}; + +template +struct opencl_gl_fn12 +{ + typedef _R (CL_API_CALL*FN)(_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10, _T11, _T12); + static _R CL_API_CALL switch_fn(_T1 p1, _T2 p2, _T3 p3, _T4 p4, _T5 p5, _T6 p6, _T7 p7, _T8 p8, _T9 p9, _T10 p10, _T11 p11, _T12 p12) + { return ((FN)opencl_gl_check_fn(ID))(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12); } +}; + +template +struct opencl_gl_fn13 +{ + typedef _R (CL_API_CALL*FN)(_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10, _T11, _T12, _T13); + static _R CL_API_CALL switch_fn(_T1 p1, _T2 p2, _T3 p3, _T4 p4, _T5 p5, _T6 p6, _T7 p7, _T8 p8, _T9 p9, _T10 p10, _T11 p11, _T12 p12, _T13 p13) + { return ((FN)opencl_gl_check_fn(ID))(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13); } +}; + +template +struct opencl_gl_fn14 +{ + typedef _R (CL_API_CALL*FN)(_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10, _T11, _T12, _T13, _T14); + static _R CL_API_CALL switch_fn(_T1 p1, _T2 p2, _T3 p3, _T4 p4, _T5 p5, _T6 p6, _T7 p7, _T8 p8, _T9 p9, _T10 p10, _T11 p11, _T12 p12, _T13 p13, _T14 p14) + { return ((FN)opencl_gl_check_fn(ID))(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14); } +}; + +} // anonymous namespace + +// generated by parser_cl.py +cl_mem (CL_API_CALL*clCreateFromGLBuffer)(cl_context, cl_mem_flags, cl_GLuint, int*) = + opencl_gl_fn4::switch_fn; +static const struct DynamicFnEntry clCreateFromGLBuffer_definition = { "clCreateFromGLBuffer", (void**)&clCreateFromGLBuffer}; + +cl_mem (CL_API_CALL*clCreateFromGLRenderbuffer)(cl_context, cl_mem_flags, cl_GLuint, cl_int*) = + opencl_gl_fn4::switch_fn; +static const struct DynamicFnEntry clCreateFromGLRenderbuffer_definition = { "clCreateFromGLRenderbuffer", (void**)&clCreateFromGLRenderbuffer}; + +cl_mem (CL_API_CALL*clCreateFromGLTexture)(cl_context, cl_mem_flags, cl_GLenum, cl_GLint, cl_GLuint, cl_int*) = + opencl_gl_fn6::switch_fn; +static const struct DynamicFnEntry clCreateFromGLTexture_definition = { "clCreateFromGLTexture", (void**)&clCreateFromGLTexture}; + +cl_mem (CL_API_CALL*clCreateFromGLTexture2D)(cl_context, cl_mem_flags, cl_GLenum, cl_GLint, cl_GLuint, cl_int*) = + opencl_gl_fn6::switch_fn; +static const struct DynamicFnEntry clCreateFromGLTexture2D_definition = { "clCreateFromGLTexture2D", (void**)&clCreateFromGLTexture2D}; + +cl_mem (CL_API_CALL*clCreateFromGLTexture3D)(cl_context, cl_mem_flags, cl_GLenum, cl_GLint, cl_GLuint, cl_int*) = + opencl_gl_fn6::switch_fn; +static const struct DynamicFnEntry clCreateFromGLTexture3D_definition = { "clCreateFromGLTexture3D", (void**)&clCreateFromGLTexture3D}; + +cl_int (CL_API_CALL*clEnqueueAcquireGLObjects)(cl_command_queue, cl_uint, const cl_mem*, cl_uint, const cl_event*, cl_event*) = + opencl_gl_fn6::switch_fn; +static const struct DynamicFnEntry clEnqueueAcquireGLObjects_definition = { "clEnqueueAcquireGLObjects", (void**)&clEnqueueAcquireGLObjects}; + +cl_int (CL_API_CALL*clEnqueueReleaseGLObjects)(cl_command_queue, cl_uint, const cl_mem*, cl_uint, const cl_event*, cl_event*) = + opencl_gl_fn6::switch_fn; +static const struct DynamicFnEntry clEnqueueReleaseGLObjects_definition = { "clEnqueueReleaseGLObjects", (void**)&clEnqueueReleaseGLObjects}; + +cl_int (CL_API_CALL*clGetGLContextInfoKHR)(const cl_context_properties*, cl_gl_context_info, size_t, void*, size_t*) = + opencl_gl_fn5::switch_fn; +static const struct DynamicFnEntry clGetGLContextInfoKHR_definition = { "clGetGLContextInfoKHR", (void**)&clGetGLContextInfoKHR}; + +cl_int (CL_API_CALL*clGetGLObjectInfo)(cl_mem, cl_gl_object_type*, cl_GLuint*) = + opencl_gl_fn3::switch_fn; +static const struct DynamicFnEntry clGetGLObjectInfo_definition = { "clGetGLObjectInfo", (void**)&clGetGLObjectInfo}; + +cl_int (CL_API_CALL*clGetGLTextureInfo)(cl_mem, cl_gl_texture_info, size_t, void*, size_t*) = + opencl_gl_fn5::switch_fn; +static const struct DynamicFnEntry clGetGLTextureInfo_definition = { "clGetGLTextureInfo", (void**)&clGetGLTextureInfo}; + + +// generated by parser_cl.py +static const struct DynamicFnEntry* opencl_gl_fn_list[] = { + &clCreateFromGLBuffer_definition, + &clCreateFromGLRenderbuffer_definition, + &clCreateFromGLTexture_definition, + &clCreateFromGLTexture2D_definition, + &clCreateFromGLTexture3D_definition, + &clEnqueueAcquireGLObjects_definition, + &clEnqueueReleaseGLObjects_definition, + &clGetGLContextInfoKHR_definition, + &clGetGLObjectInfo_definition, + &clGetGLTextureInfo_definition, +}; + +// number of enabled functions: 10 diff --git a/modules/core/src/opencl/runtime/generator/filter/opencl_gl_functions.list b/modules/core/src/opencl/runtime/generator/filter/opencl_gl_functions.list new file mode 100644 index 0000000000000000000000000000000000000000..b2f9e621aaede0b4556fe4004857c8b8c91b066a --- /dev/null +++ b/modules/core/src/opencl/runtime/generator/filter/opencl_gl_functions.list @@ -0,0 +1,11 @@ +clCreateFromGLBuffer +clCreateFromGLRenderbuffer +clCreateFromGLTexture +clCreateFromGLTexture2D +clCreateFromGLTexture3D +clEnqueueAcquireGLObjects +clEnqueueReleaseGLObjects +clGetGLContextInfoKHR +clGetGLObjectInfo +clGetGLTextureInfo +#total 10 diff --git a/modules/core/src/opencl/runtime/generator/generate.sh b/modules/core/src/opencl/runtime/generator/generate.sh index 8649e998436872e9625f403eba2bb77cfe95e6b1..d9d6f0e3e8d39856fc8e156862672594cbb92013 100644 --- a/modules/core/src/opencl/runtime/generator/generate.sh +++ b/modules/core/src/opencl/runtime/generator/generate.sh @@ -3,4 +3,6 @@ echo "Generate files for CL runtime..." python parser_cl.py opencl_core < sources/cl.h python parser_clamdblas.py < sources/clAmdBlas.h python parser_clamdfft.py < sources/clAmdFft.h + +python parser_cl.py opencl_gl < sources/cl_gl.h echo "Generate files for CL runtime... Done" diff --git a/modules/core/src/opencl/runtime/generator/parser_cl.py b/modules/core/src/opencl/runtime/generator/parser_cl.py index 87eeb27236973df5c343746f59b2921be4425f6b..e6c738bef76b317d00db11dffc7d265c5076be53 100644 --- a/modules/core/src/opencl/runtime/generator/parser_cl.py +++ b/modules/core/src/opencl/runtime/generator/parser_cl.py @@ -8,9 +8,10 @@ from common import remove_comments, getTokens, getParameters, postProcessParamet try: if len(sys.argv) > 1: - outfile = open('../../../../include/opencv2/core/opencl/runtime/autogenerated/' + sys.argv[1] + '.hpp', 'wb') - outfile_impl = open('../autogenerated/' + sys.argv[1] + '_impl.hpp', 'wb') - outfile_wrappers = open('../../../../include/opencv2/core/opencl/runtime/autogenerated/' + sys.argv[1] + '_wrappers.hpp', 'wb') + module_name = sys.argv[1] + outfile = open('../../../../include/opencv2/core/opencl/runtime/autogenerated/%s.hpp' % module_name, 'wb') + outfile_impl = open('../autogenerated/%s_impl.hpp' % module_name, 'wb') + outfile_wrappers = open('../../../../include/opencv2/core/opencl/runtime/autogenerated/%s_wrappers.hpp' % module_name, 'wb') if len(sys.argv) > 2: f = open(sys.argv[2], "r") else: @@ -95,7 +96,7 @@ pprint(fns) from common import * -filterFileName='./filter/opencl_core_functions.list' +filterFileName = './filter/%s_functions.list' % module_name numEnabled = readFunctionFilter(fns, filterFileName) functionsFilter = generateFilterNames(fns) @@ -108,18 +109,27 @@ ctx['CL_REMAP_DYNAMIC'] = generateRemapDynamic(fns) ctx['CL_FN_DECLARATIONS'] = generateFnDeclaration(fns) sys.stdout = outfile -ProcessTemplate('template/opencl_core.hpp.in', ctx) +ProcessTemplate('template/%s.hpp.in' % module_name, ctx) ctx['CL_FN_INLINE_WRAPPERS'] = generateInlineWrappers(fns) sys.stdout = outfile_wrappers -ProcessTemplate('template/opencl_core_wrappers.hpp.in', ctx) - -ctx['CL_FN_ENTRY_DEFINITIONS'] = generateStructDefinitions(fns) -ctx['CL_FN_ENTRY_LIST'] = generateListOfDefinitions(fns) -ctx['CL_FN_ENUMS'] = generateEnums(fns) -ctx['CL_FN_SWITCH'] = generateTemplates(15, 'opencl_fn', 'opencl_check_fn', 'CL_API_CALL') +ProcessTemplate('template/%s_wrappers.hpp.in' % module_name, ctx) + +if module_name == 'opencl_core': + ctx['CL_FN_ENTRY_DEFINITIONS'] = generateStructDefinitions(fns) + ctx['CL_FN_ENTRY_LIST'] = generateListOfDefinitions(fns) + ctx['CL_FN_ENUMS'] = generateEnums(fns) + ctx['CL_FN_SWITCH'] = generateTemplates(15, 'opencl_fn', 'opencl_check_fn', 'CL_API_CALL') +else: + lprefix = module_name + '_fn' + enumprefix = module_name.upper() + '_FN' + fn_list_name = module_name + '_fn_list' + ctx['CL_FN_ENTRY_DEFINITIONS'] = generateStructDefinitions(fns, lprefix=lprefix, enumprefix=enumprefix) + ctx['CL_FN_ENTRY_LIST'] = generateListOfDefinitions(fns, fn_list_name) + ctx['CL_FN_ENUMS'] = generateEnums(fns, prefix=enumprefix) + ctx['CL_FN_SWITCH'] = generateTemplates(15, lprefix, '%s_check_fn' % module_name, 'CL_API_CALL') ctx['CL_NUMBER_OF_ENABLED_FUNCTIONS'] = '// number of enabled functions: %d' % (numEnabled) sys.stdout = outfile_impl -ProcessTemplate('template/opencl_core_impl.hpp.in', ctx) +ProcessTemplate('template/%s_impl.hpp.in' % module_name, ctx) diff --git a/modules/core/src/opencl/runtime/generator/template/opencl_gl.hpp.in b/modules/core/src/opencl/runtime/generator/template/opencl_gl.hpp.in new file mode 100644 index 0000000000000000000000000000000000000000..24434d2de7c7aa4d4187f63aa813d3c5202ab403 --- /dev/null +++ b/modules/core/src/opencl/runtime/generator/template/opencl_gl.hpp.in @@ -0,0 +1,15 @@ +#ifndef __OPENCV_CORE_OCL_RUNTIME_OPENCL_GL_HPP__ +#error "Invalid usage" +#endif + +@CL_REMAP_ORIGIN@ + +#if defined __APPLE__ +#include +#else +#include +#endif + +@CL_REMAP_DYNAMIC@ + +@CL_FN_DECLARATIONS@ diff --git a/modules/core/src/opencl/runtime/generator/template/opencl_gl_impl.hpp.in b/modules/core/src/opencl/runtime/generator/template/opencl_gl_impl.hpp.in new file mode 100644 index 0000000000000000000000000000000000000000..14586017a43f3f3e8c3dc14074af4cf65d861391 --- /dev/null +++ b/modules/core/src/opencl/runtime/generator/template/opencl_gl_impl.hpp.in @@ -0,0 +1,11 @@ +@CL_FN_ENUMS@ + +namespace { +@CL_FN_SWITCH@ +} // anonymous namespace + +@CL_FN_ENTRY_DEFINITIONS@ + +@CL_FN_ENTRY_LIST@ + +@CL_NUMBER_OF_ENABLED_FUNCTIONS@ diff --git a/modules/core/src/opencl/runtime/generator/template/opencl_gl_wrappers.hpp.in b/modules/core/src/opencl/runtime/generator/template/opencl_gl_wrappers.hpp.in new file mode 100644 index 0000000000000000000000000000000000000000..0aeefb4f44d000415696b5a36e8ec75b54fd4dd5 --- /dev/null +++ b/modules/core/src/opencl/runtime/generator/template/opencl_gl_wrappers.hpp.in @@ -0,0 +1,5 @@ +#ifndef __OPENCV_CORE_OCL_RUNTIME_OPENCL_GL_WRAPPERS_HPP__ +#error "Invalid usage" +#endif + +@CL_FN_INLINE_WRAPPERS@ diff --git a/modules/core/src/opencl/runtime/opencl_core.cpp b/modules/core/src/opencl/runtime/opencl_core.cpp index 43f6b13b6e7bb2835a3c2a2dd884a2725dbc652e..971c0770e5c832ae616c8a6291b68e8193dca4ae 100644 --- a/modules/core/src/opencl/runtime/opencl_core.cpp +++ b/modules/core/src/opencl/runtime/opencl_core.cpp @@ -279,4 +279,30 @@ static void* opencl_check_fn(int ID) return func; } +#ifdef HAVE_OPENGL + +#include "opencv2/core/opencl/runtime/opencl_gl.hpp" + +static void* opencl_gl_check_fn(int ID); + +#include "autogenerated/opencl_gl_impl.hpp" + +static void* opencl_gl_check_fn(int ID) +{ + const struct DynamicFnEntry* e = NULL; + assert(ID >= 0 && ID < (int)(sizeof(opencl_gl_fn_list)/sizeof(opencl_gl_fn_list[0]))); + e = opencl_gl_fn_list[ID]; + void* func = CV_CL_GET_PROC_ADDRESS(e->fnName); + if (!func) + { + throw cv::Exception(cv::Error::OpenCLApiCallError, + cv::format("OpenCL function is not available: [%s]", e->fnName), + CV_Func, __FILE__, __LINE__); + } + *(e->ppFn) = func; + return func; +} + +#endif // HAVE_OPENGL + #endif diff --git a/modules/core/src/opengl.cpp b/modules/core/src/opengl.cpp index 00a7f6666231a872b91549c331387a9a05ae857c..3c3c6d2ab24d9fd34039ccde7d7e96a1c38a4c67 100644 --- a/modules/core/src/opengl.cpp +++ b/modules/core/src/opengl.cpp @@ -47,7 +47,9 @@ # ifdef HAVE_CUDA # include # endif -#endif +#else // HAVE_OPENGL +# define NO_OPENGL_SUPPORT_ERROR CV_ErrorNoReturn(cv::Error::StsBadFunc, "OpenCV was build without OpenGL support") +#endif // HAVE_OPENGL using namespace cv; using namespace cv::cuda; @@ -1572,3 +1574,234 @@ void cv::ogl::render(const ogl::Arrays& arr, InputArray indices, int mode, Scala } #endif } + +//////////////////////////////////////////////////////////////////////// +// CL-GL Interoperability + +#ifdef HAVE_OPENCL +# include "opencv2/core/opencl/runtime/opencl_gl.hpp" +#else // HAVE_OPENCL +# define NO_OPENCL_SUPPORT_ERROR CV_ErrorNoReturn(cv::Error::StsBadFunc, "OpenCV was build without OpenCL support") +#endif // HAVE_OPENCL + +#if defined(HAVE_OPENGL) +# if defined(ANDROID) +# include +# elif defined(__linux__) +# include +# endif +#endif // HAVE_OPENGL + +namespace cv { namespace ogl { + +namespace ocl { + +Context& initializeContextFromGL() +{ +#if !defined(HAVE_OPENGL) + NO_OPENGL_SUPPORT_ERROR; +#elif !defined(HAVE_OPENCL) + NO_OPENCL_SUPPORT_ERROR; +#else + cl_uint numPlatforms; + cl_int status = clGetPlatformIDs(0, NULL, &numPlatforms); + if (status != CL_SUCCESS) + CV_Error(cv::Error::OpenCLInitError, "OpenCL: Can't get number of platforms"); + if (numPlatforms == 0) + CV_Error(cv::Error::OpenCLInitError, "OpenCL: No available platforms"); + + std::vector platforms(numPlatforms); + status = clGetPlatformIDs(numPlatforms, &platforms[0], NULL); + if (status != CL_SUCCESS) + CV_Error(cv::Error::OpenCLInitError, "OpenCL: Can't get number of platforms"); + + // TODO Filter platforms by name from OPENCV_OPENCL_DEVICE + + int found = -1; + cl_device_id device = NULL; + cl_context context = NULL; + + for (int i = 0; i < (int)numPlatforms; i++) + { + // query platform extension: presence of "cl_khr_gl_sharing" extension is requred + { + AutoBuffer extensionStr; + + size_t extensionSize; + status = clGetPlatformInfo(platforms[i], CL_PLATFORM_EXTENSIONS, 0, NULL, &extensionSize); + if (status == CL_SUCCESS) + { + extensionStr.allocate(extensionSize+1); + status = clGetPlatformInfo(platforms[i], CL_PLATFORM_EXTENSIONS, extensionSize, (char*)extensionStr, NULL); + } + if (status != CL_SUCCESS) + CV_Error(cv::Error::OpenCLInitError, "OpenCL: Can't get platform extension string"); + + if (!strstr((const char*)extensionStr, "cl_khr_gl_sharing")) + continue; + } + + clGetGLContextInfoKHR_fn clGetGLContextInfoKHR = (clGetGLContextInfoKHR_fn) + clGetExtensionFunctionAddressForPlatform(platforms[i], "clGetGLContextInfoKHR"); + if (!clGetGLContextInfoKHR) + continue; + + cl_context_properties properties[] = + { +#if defined(WIN32) || defined(_WIN32) + CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i], + CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(), + CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(), +#elif defined(ANDROID) + CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i], + CL_GL_CONTEXT_KHR, (cl_context_properties)eglGetCurrentContext(), + CL_EGL_DISPLAY_KHR, (cl_context_properties)eglGetCurrentDisplay(), +#elif defined(__linux__) + CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i], + CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(), + CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(), +#endif + 0 + }; + + // query device + device = NULL; + status = clGetGLContextInfoKHR(properties, CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR, sizeof(cl_device_id), (void*)&device, NULL); + if (status != CL_SUCCESS) + continue; + + // create context + context = clCreateContext(properties, 1, &device, NULL, NULL, &status); + if (status != CL_SUCCESS) + { + clReleaseDevice(device); + } + else + { + found = i; + break; + } + } + + if (found < 0) + CV_Error(cv::Error::OpenCLInitError, "OpenCL: Can't create context for OpenGL interop"); + + Context& ctx = Context::getDefault(false); + initializeContextFromHandle(ctx, platforms[found], context, device); + return ctx; +#endif +} + +} // namespace cv::ogl::ocl + +void convertToGLTexture2D(InputArray src, Texture2D& texture) +{ + (void)src; (void)texture; +#if !defined(HAVE_OPENGL) + NO_OPENGL_SUPPORT_ERROR; +#elif !defined(HAVE_OPENCL) + NO_OPENCL_SUPPORT_ERROR; +#else + Size srcSize = src.size(); + CV_Assert(srcSize.width == (int)texture.cols() && srcSize.height == (int)texture.rows()); + + using namespace cv::ocl; + Context& ctx = Context::getDefault(); + cl_context context = (cl_context)ctx.ptr(); + + UMat u = src.getUMat(); + + // TODO Add support for roi + CV_Assert(u.offset == 0); + CV_Assert(u.isContinuous()); + + cl_int status = 0; + cl_mem clImage = clCreateFromGLTexture(context, CL_MEM_WRITE_ONLY, gl::TEXTURE_2D, 0, texture.texId(), &status); + if (status != CL_SUCCESS) + CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clCreateFromGLTexture failed"); + + cl_mem clBuffer = (cl_mem)u.handle(ACCESS_READ); + + cl_command_queue q = (cl_command_queue)Queue::getDefault().ptr(); + status = clEnqueueAcquireGLObjects(q, 1, &clImage, 0, NULL, NULL); + if (status != CL_SUCCESS) + CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clEnqueueAcquireGLObjects failed"); + size_t offset = 0; // TODO + size_t dst_origin[3] = {0, 0, 0}; + size_t region[3] = {u.cols, u.rows, 1}; + status = clEnqueueCopyBufferToImage(q, clBuffer, clImage, offset, dst_origin, region, 0, NULL, NULL); + if (status != CL_SUCCESS) + CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clEnqueueCopyBufferToImage failed"); + status = clEnqueueReleaseGLObjects(q, 1, &clImage, 0, NULL, NULL); + if (status != CL_SUCCESS) + CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clEnqueueReleaseGLObjects failed"); + + status = clFinish(q); // TODO Use events + if (status != CL_SUCCESS) + CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clFinish failed"); + + status = clReleaseMemObject(clImage); // TODO RAII + if (status != CL_SUCCESS) + CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clReleaseMemObject failed"); +#endif +} + +void convertFromGLTexture2D(const Texture2D& texture, OutputArray dst) +{ + (void)texture; (void)dst; +#if !defined(HAVE_OPENGL) + NO_OPENGL_SUPPORT_ERROR; +#elif !defined(HAVE_OPENCL) + NO_OPENCL_SUPPORT_ERROR; +#else + // check texture format + const int dtype = CV_8UC4; + CV_Assert(texture.format() == Texture2D::RGBA); + + int textureType = dtype; + CV_Assert(textureType >= 0); + + using namespace cv::ocl; + Context& ctx = Context::getDefault(); + cl_context context = (cl_context)ctx.ptr(); + + // TODO Need to specify ACCESS_WRITE here somehow to prevent useless data copying! + dst.create(texture.size(), textureType); + UMat u = dst.getUMat(); + + // TODO Add support for roi + CV_Assert(u.offset == 0); + CV_Assert(u.isContinuous()); + + cl_int status = 0; + cl_mem clImage = clCreateFromGLTexture(context, CL_MEM_READ_ONLY, gl::TEXTURE_2D, 0, texture.texId(), &status); + if (status != CL_SUCCESS) + CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clCreateFromGLTexture failed"); + + cl_mem clBuffer = (cl_mem)u.handle(ACCESS_READ); + + cl_command_queue q = (cl_command_queue)Queue::getDefault().ptr(); + status = clEnqueueAcquireGLObjects(q, 1, &clImage, 0, NULL, NULL); + if (status != CL_SUCCESS) + CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clEnqueueAcquireGLObjects failed"); + size_t offset = 0; // TODO + size_t src_origin[3] = {0, 0, 0}; + size_t region[3] = {u.cols, u.rows, 1}; + status = clEnqueueCopyImageToBuffer(q, clImage, clBuffer, src_origin, region, offset, 0, NULL, NULL); + if (status != CL_SUCCESS) + CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clEnqueueCopyImageToBuffer failed"); + status = clEnqueueReleaseGLObjects(q, 1, &clImage, 0, NULL, NULL); + if (status != CL_SUCCESS) + CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clEnqueueReleaseGLObjects failed"); + + status = clFinish(q); // TODO Use events + if (status != CL_SUCCESS) + CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clFinish failed"); + + status = clReleaseMemObject(clImage); // TODO RAII + if (status != CL_SUCCESS) + CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clReleaseMemObject failed"); +#endif +} + +}} // namespace cv::ogl diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index ef6cd772f43e3e259adfeac05165224eec5abcc9..31d7d8021a05e0ac7097d4bc76c45a94c04d000a 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -18,6 +18,10 @@ if(WIN32 AND HAVE_DIRECTX) add_subdirectory(directx) endif() +if((NOT ANDROID) AND HAVE_OPENGL) + add_subdirectory(opengl) +endif() + if(ANDROID AND BUILD_ANDROID_EXAMPLES) add_subdirectory(android) endif() diff --git a/samples/opengl/CMakeLists.txt b/samples/opengl/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..4bf48925e307e296a836852fa116c69b59cfb5b8 --- /dev/null +++ b/samples/opengl/CMakeLists.txt @@ -0,0 +1,45 @@ +SET(OPENCV_OPENGL_SAMPLES_REQUIRED_DEPS opencv_core opencv_imgproc opencv_imgcodecs opencv_videoio opencv_highgui) + +ocv_check_dependencies(${OPENCV_OPENGL_SAMPLES_REQUIRED_DEPS}) + +if(BUILD_EXAMPLES AND OCV_DEPENDENCIES_FOUND) + set(project "opengl") + string(TOUPPER "${project}" project_upper) + + project("${project}_samples") + + ocv_include_modules_recurse(${OPENCV_OPENGL_SAMPLES_REQUIRED_DEPS}) + + # --------------------------------------------- + # Define executable targets + # --------------------------------------------- + MACRO(OPENCV_DEFINE_OPENGL_EXAMPLE name srcs) + set(the_target "example_${project}_${name}") + add_executable(${the_target} ${srcs}) + + ocv_target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS} ${OPENCV_OPENGL_SAMPLES_REQUIRED_DEPS}) + + set_target_properties(${the_target} PROPERTIES + OUTPUT_NAME "${project}-example-${name}" + PROJECT_LABEL "(EXAMPLE_${project_upper}) ${name}") + + if(ENABLE_SOLUTION_FOLDERS) + set_target_properties(${the_target} PROPERTIES FOLDER "samples//${project}") + endif() + + if(WIN32) + if(MSVC AND NOT BUILD_SHARED_LIBS) + set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /DEBUG") + endif() + install(TARGETS ${the_target} RUNTIME DESTINATION "${OPENCV_SAMPLES_BIN_INSTALL_PATH}/${project}" COMPONENT samples) + endif() + ENDMACRO() + + file(GLOB all_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) + + foreach(sample_filename ${all_samples}) + get_filename_component(sample ${sample_filename} NAME_WE) + file(GLOB sample_srcs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${sample}.*) + OPENCV_DEFINE_OPENGL_EXAMPLE(${sample} ${sample_srcs}) + endforeach() +endif() diff --git a/samples/opengl/opengl_interop.cpp b/samples/opengl/opengl_interop.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00a8a2684388a7954c786157fe5da7fa0ba8ba80 --- /dev/null +++ b/samples/opengl/opengl_interop.cpp @@ -0,0 +1,445 @@ +#if defined(WIN32) || defined(_WIN32) +# define WIN32_LEAN_AND_MEAN +# include +#elif defined(__linux__) +# include +# include +#endif + +#include +#include +#include + +#include + +#include "opencv2/core.hpp" +#include "opencv2/core/opengl.hpp" +#include "opencv2/core/ocl.hpp" +#include "opencv2/imgproc.hpp" +#include "opencv2/videoio.hpp" + +#include "winapp.hpp" + +#if defined(WIN32) || defined(_WIN32) +# pragma comment(lib, "opengl32.lib") +# pragma comment(lib, "glu32.lib") +#endif + +class GLWinApp : public WinApp +{ +public: + GLWinApp(int width, int height, std::string& window_name, cv::VideoCapture& cap) : + WinApp(width, height, window_name) + { + m_shutdown = false; + m_mode = 0; + m_modeStr[0] = cv::String("No processing"); + m_modeStr[1] = cv::String("Processing on CPU"); + m_modeStr[2] = cv::String("Processing on GPU"); + m_disableProcessing = false; + m_cap = cap; + } + + ~GLWinApp() {} + + virtual void cleanup() + { + m_shutdown = true; +#if defined(__linux__) + glXMakeCurrent(m_display, None, NULL); + glXDestroyContext(m_display, m_glctx); +#endif + WinApp::cleanup(); + } + +#if defined(WIN32) || defined(_WIN32) + virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) + { + switch (message) + { + case WM_CHAR: + if (wParam >= '0' && wParam <= '2') + { + m_mode = (char)wParam - '0'; + return 0; + } + else if (wParam == VK_SPACE) + { + m_disableProcessing = !m_disableProcessing; + return 0; + } + else if (wParam == VK_ESCAPE) + { + cleanup(); + return 0; + } + break; + + case WM_CLOSE: + cleanup(); + return 0; + + case WM_DESTROY: + ::PostQuitMessage(0); + return 0; + } + + return ::DefWindowProc(hWnd, message, wParam, lParam); + } +#endif + + static float getFps() + { + static std::queue time_queue; + + int64 now = cv::getTickCount(); + int64 then = 0; + time_queue.push(now); + + if (time_queue.size() >= 2) + then = time_queue.front(); + + if (time_queue.size() >= 25) + time_queue.pop(); + + return time_queue.size() * (float)cv::getTickFrequency() / (now - then); + } + +#if defined(__linux__) + int handle_event(XEvent& e) + { + switch(e.type) + { + case ClientMessage: + if ((Atom)e.xclient.data.l[0] == m_WM_DELETE_WINDOW) + { + m_end_loop = true; + cleanup(); + } + else + { + return 0; + } + break; + case Expose: + render(); + break; + case KeyPress: + switch(keycode_to_keysym(e.xkey.keycode)) + { + case XK_space: + m_disableProcessing = !m_disableProcessing; + break; + case XK_0: + m_mode = 0; + break; + case XK_1: + m_mode = 1; + break; + case XK_2: + m_mode = 2; + break; + case XK_Escape: + m_end_loop = true; + cleanup(); + break; + } + break; + default: + return 0; + } + return 1; + } +#endif + + int init() + { +#if defined(WIN32) || defined(_WIN32) + m_hDC = GetDC(m_hWnd); + + if (setup_pixel_format() != 0) + { + std::cerr << "Can't setup pixel format" << std::endl; + return -1; + } + + m_hRC = wglCreateContext(m_hDC); + wglMakeCurrent(m_hDC, m_hRC); +#elif defined(__linux__) + m_glctx = glXCreateContext(m_display, m_visual_info, NULL, GL_TRUE); + glXMakeCurrent(m_display, m_window, m_glctx); +#endif + + glEnable(GL_TEXTURE_2D); + glEnable(GL_DEPTH_TEST); + + glViewport(0, 0, m_width, m_height); + + if (cv::ocl::haveOpenCL()) + { + (void) cv::ogl::ocl::initializeContextFromGL(); + } + + m_oclDevName = cv::ocl::useOpenCL() ? + cv::ocl::Context::getDefault().device(0).name() : + (char*) "No OpenCL device"; + + return 0; + } // init() + + int get_texture(cv::ogl::Texture2D& texture) + { + if (!m_cap.read(m_frame_bgr)) + return -1; + + cv::cvtColor(m_frame_bgr, m_frame_rgba, CV_RGB2RGBA); + + texture.copyFrom(m_frame_rgba); + + return 0; + } + + void print_info(int mode, float fps, cv::String oclDevName) + { +#if defined(WIN32) || defined(_WIN32) + HDC hDC = m_hDC; + + HFONT hFont = (HFONT)::GetStockObject(SYSTEM_FONT); + + HFONT hOldFont = (HFONT)::SelectObject(hDC, hFont); + + if (hOldFont) + { + TEXTMETRIC tm; + ::GetTextMetrics(hDC, &tm); + + char buf[256+1]; + int y = 0; + + buf[0] = 0; + sprintf_s(buf, sizeof(buf)-1, "Mode: %s", m_modeStr[mode].c_str()); + ::TextOut(hDC, 0, y, buf, (int)strlen(buf)); + + y += tm.tmHeight; + buf[0] = 0; + sprintf_s(buf, sizeof(buf)-1, "FPS: %2.1f", fps); + ::TextOut(hDC, 0, y, buf, (int)strlen(buf)); + + y += tm.tmHeight; + buf[0] = 0; + sprintf_s(buf, sizeof(buf)-1, "OpenCL device: %s", oclDevName.c_str()); + ::TextOut(hDC, 0, y, buf, (int)strlen(buf)); + + ::SelectObject(hDC, hOldFont); + } +#elif defined(__linux__) + + char buf[256+1]; + snprintf(buf, sizeof(buf)-1, "FPS: %2.1f Mode: %s Device: %s", fps, m_modeStr[mode].c_str(), oclDevName.c_str()); + XStoreName(m_display, m_window, buf); +#endif + } + + void idle() + { + render(); + } + + int render() + { + try + { + if (m_shutdown) + return 0; + + int r; + cv::ogl::Texture2D texture; + + r = get_texture(texture); + if (r != 0) + { + return -1; + } + + switch (m_mode) + { + case 0: + // no processing + break; + + case 1: + { + // process video frame on CPU + cv::Mat m(m_height, m_width, CV_8UC4); + + texture.copyTo(m); + if (!m_disableProcessing) + { + // blur texture image with OpenCV on CPU + cv::blur(m, m, cv::Size(15, 15), cv::Point(-7, -7)); + } + texture.copyFrom(m); + + break; + } + + case 2: + { + // process video frame on GPU + cv::UMat u; + + cv::ogl::convertFromGLTexture2D(texture, u); + if (!m_disableProcessing) + { + // blur texture image with OpenCV on GPU with OpenCL + cv::blur(u, u, cv::Size(15, 15), cv::Point(-7, -7)); + } + cv::ogl::convertToGLTexture2D(u, texture); + + break; + } + + } // switch + +#if defined(__linux__) + XWindowAttributes window_attributes; + XGetWindowAttributes(m_display, m_window, &window_attributes); + glViewport(0, 0, window_attributes.width, window_attributes.height); +#endif + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glLoadIdentity(); + glEnable(GL_TEXTURE_2D); + + texture.bind(); + + glBegin(GL_QUADS); + glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 0.1f); + glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 0.1f); + glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, -1.0f, 0.1f); + glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 0.1f); + glEnd(); + +#if defined(WIN32) || defined(_WIN32) + SwapBuffers(m_hDC); +#elif defined(__linux__) + glXSwapBuffers(m_display, m_window); +#endif + + print_info(m_mode, getFps(), m_oclDevName); + } + + + catch (cv::Exception& e) + { + std::cerr << "Exception: " << e.what() << std::endl; + return 10; + } + + return 0; + } + +protected: + +#if defined(WIN32) || defined(_WIN32) + int setup_pixel_format() + { + PIXELFORMATDESCRIPTOR pfd; + + pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); + pfd.nVersion = 1; + pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; + pfd.iPixelType = PFD_TYPE_RGBA; + pfd.cColorBits = 24; + pfd.cRedBits = 8; + pfd.cRedShift = 0; + pfd.cGreenBits = 8; + pfd.cGreenShift = 0; + pfd.cBlueBits = 8; + pfd.cBlueShift = 0; + pfd.cAlphaBits = 8; + pfd.cAlphaShift = 0; + pfd.cAccumBits = 0; + pfd.cAccumRedBits = 0; + pfd.cAccumGreenBits = 0; + pfd.cAccumBlueBits = 0; + pfd.cAccumAlphaBits = 0; + pfd.cDepthBits = 24; + pfd.cStencilBits = 8; + pfd.cAuxBuffers = 0; + pfd.iLayerType = PFD_MAIN_PLANE; + pfd.bReserved = 0; + pfd.dwLayerMask = 0; + pfd.dwVisibleMask = 0; + pfd.dwDamageMask = 0; + + int pfmt = ChoosePixelFormat(m_hDC, &pfd); + if (pfmt == 0) + return -1; + if (SetPixelFormat(m_hDC, pfmt, &pfd) == 0) + return -2; + return 0; + } +#endif + +#if defined(__linux__) + KeySym keycode_to_keysym(unsigned keycode) + { // note that XKeycodeToKeysym() is considered deprecated + int keysyms_per_keycode_return = 0; + KeySym *keysyms = XGetKeyboardMapping(m_display, keycode, 1, &keysyms_per_keycode_return); + KeySym keysym = keysyms[0]; + XFree(keysyms); + return keysym; + } +#endif + +private: + bool m_shutdown; + int m_mode; + cv::String m_modeStr[3]; + int m_disableProcessing; +#if defined(WIN32) || defined(_WIN32) + HDC m_hDC; + HGLRC m_hRC; +#elif defined(__linux__) + GLXContext m_glctx; +#endif + cv::VideoCapture m_cap; + cv::Mat m_frame_bgr; + cv::Mat m_frame_rgba; + cv::String m_oclDevName; +}; + +using namespace cv; + +int main(int argc, char** argv) +{ + cv::VideoCapture cap; + + if (argc > 1) + cap.open(argv[1]); + else + cap.open(0); + + int width = (int)cap.get(CAP_PROP_FRAME_WIDTH); + int height = (int)cap.get(CAP_PROP_FRAME_HEIGHT); + std::string wndname = "WGL Window"; + + GLWinApp app(width, height, wndname, cap); + + try + { + app.create(); + return app.run(); + } + catch (cv::Exception& e) + { + std::cerr << "Exception: " << e.what() << std::endl; + return 10; + } + catch (...) + { + std::cerr << "FATAL ERROR: Unknown exception" << std::endl; + return 11; + } +} diff --git a/samples/opengl/winapp.hpp b/samples/opengl/winapp.hpp new file mode 100644 index 0000000000000000000000000000000000000000..b70dfa0fe900e72328388008d2d924ae4642068c --- /dev/null +++ b/samples/opengl/winapp.hpp @@ -0,0 +1,221 @@ +#if defined(WIN32) || defined(_WIN32) +# define WIN32_LEAN_AND_MEAN +# include +#elif defined(__linux__) +# include +# include +# include +#endif + +#include + +#include +#if defined(WIN32) || defined(_WIN32) +# include +#elif defined(__linux__) +# include +#endif + +#if defined(WIN32) || defined(_WIN32) +# define WINCLASS "WinAppWnd" +#endif + +#define SAFE_RELEASE(p) if (p) { p->Release(); p = NULL; } + +class WinApp +{ +public: + WinApp(int width, int height, std::string& window_name) + { + m_width = width; + m_height = height; + m_window_name = window_name; +#if defined(WIN32) || defined(_WIN32) + m_hInstance = ::GetModuleHandle(NULL); +#endif + } + + virtual ~WinApp() + { +#if defined(WIN32) || defined(_WIN32) + ::UnregisterClass(WINCLASS, m_hInstance); +#endif + } + + int create() + { +#if defined(WIN32) || defined(_WIN32) + WNDCLASSEX wcex; + + wcex.cbSize = sizeof(WNDCLASSEX); + wcex.style = CS_HREDRAW | CS_VREDRAW; + wcex.lpfnWndProc = &WinApp::StaticWndProc; + wcex.cbClsExtra = 0; + wcex.cbWndExtra = 0; + wcex.hInstance = m_hInstance; + wcex.hIcon = LoadIcon(0, IDI_APPLICATION); + wcex.hCursor = LoadCursor(0, IDC_ARROW); + wcex.hbrBackground = 0; + wcex.lpszMenuName = 0L; + wcex.lpszClassName = WINCLASS; + wcex.hIconSm = 0; + + ATOM wc = ::RegisterClassEx(&wcex); + + RECT rc = { 0, 0, m_width, m_height }; + ::AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, false); + + m_hWnd = ::CreateWindow( + (LPCTSTR)wc, m_window_name.c_str(), + WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, + rc.right - rc.left, rc.bottom - rc.top, + NULL, NULL, m_hInstance, (void*)this); + + if (!m_hWnd) + return -1; + + ::ShowWindow(m_hWnd, SW_SHOW); + ::UpdateWindow(m_hWnd); + ::SetFocus(m_hWnd); +#elif defined(__linux__) + m_display = XOpenDisplay(NULL); + + if (m_display == NULL) + { + return -1; + } + + m_WM_DELETE_WINDOW = XInternAtom(m_display, "WM_DELETE_WINDOW", False); + + static GLint visual_attributes[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None }; + m_visual_info = glXChooseVisual(m_display, 0, visual_attributes); + + if (m_visual_info == NULL) + { + XCloseDisplay(m_display); + return -2; + } + + Window root = DefaultRootWindow(m_display); + + m_event_mask = ExposureMask | KeyPressMask; + + XSetWindowAttributes window_attributes; + window_attributes.colormap = XCreateColormap(m_display, root, m_visual_info->visual, AllocNone); + window_attributes.event_mask = m_event_mask; + + m_window = XCreateWindow( + m_display, root, 0, 0, m_width, m_height, 0, m_visual_info->depth, + InputOutput, m_visual_info->visual, CWColormap | CWEventMask, &window_attributes); + + XMapWindow(m_display, m_window); + XSetWMProtocols(m_display, m_window, &m_WM_DELETE_WINDOW, 1); + XStoreName(m_display, m_window, m_window_name.c_str()); +#endif + + return init(); + } + + virtual void cleanup() + { +#if defined(WIN32) || defined(_WIN32) + ::DestroyWindow(m_hWnd); +#elif defined(__linux__) + XDestroyWindow(m_display, m_window); + XCloseDisplay(m_display); +#endif + } + +#if defined(WIN32) || defined(_WIN32) + virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) = 0; +#endif + + int run() + { +#if defined(WIN32) || defined(_WIN32) + MSG msg; + + ::ZeroMemory(&msg, sizeof(msg)); + + while (msg.message != WM_QUIT) + { + if (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) + { + ::TranslateMessage(&msg); + ::DispatchMessage(&msg); + } + else + { + idle(); + } + } + + return static_cast(msg.wParam); +#elif defined(__linux__) + m_end_loop = false; + + do { + XEvent e; + + if (!XCheckWindowEvent(m_display, m_window, m_event_mask, &e) || !handle_event(e)) + { + idle(); + } + } while (!m_end_loop); +#endif + + return 0; + } + +protected: + +#if defined(WIN32) || defined(_WIN32) + static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) + { + WinApp* pWnd; + + if (message == WM_NCCREATE) + { + LPCREATESTRUCT pCreateStruct = ((LPCREATESTRUCT)lParam); + pWnd = (WinApp*)(pCreateStruct->lpCreateParams); + ::SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pWnd); + } + + pWnd = GetObjectFromWindow(hWnd); + + if (pWnd) + return pWnd->WndProc(hWnd, message, wParam, lParam); + else + return ::DefWindowProc(hWnd, message, wParam, lParam); + } + + inline static WinApp* GetObjectFromWindow(HWND hWnd) + { + return (WinApp*)::GetWindowLongPtr(hWnd, GWLP_USERDATA); + } +#endif + +#if defined(__linux__) + virtual int handle_event(XEvent& e) = 0; +#endif + + virtual int init() = 0; + virtual int render() = 0; + + virtual void idle() = 0; + +#if defined(WIN32) || defined(_WIN32) + HINSTANCE m_hInstance; + HWND m_hWnd; +#elif defined(__linux__) + Display* m_display; + XVisualInfo* m_visual_info; + Window m_window; + long m_event_mask; + Atom m_WM_DELETE_WINDOW; + bool m_end_loop; +#endif + int m_width; + int m_height; + std::string m_window_name; +};