Merge pull request #4127 from avershov:opengl-opencl-sample

This commit is contained in:
Alexander Alekhin
2015-07-02 11:24:10 +00:00
18 changed files with 1471 additions and 13 deletions
@@ -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 <int ID, typename _R>
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 <int ID, typename _R, typename _T1>
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 <int ID, typename _R, typename _T1, typename _T2>
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 <int ID, typename _R, typename _T1, typename _T2, typename _T3>
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 <int ID, typename _R, typename _T1, typename _T2, typename _T3, typename _T4>
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 <int ID, typename _R, typename _T1, typename _T2, typename _T3, typename _T4, typename _T5>
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 <int ID, typename _R, typename _T1, typename _T2, typename _T3, typename _T4, typename _T5, typename _T6>
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 <int ID, typename _R, typename _T1, typename _T2, typename _T3, typename _T4, typename _T5, typename _T6, typename _T7>
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 <int ID, typename _R, typename _T1, typename _T2, typename _T3, typename _T4, typename _T5, typename _T6, typename _T7, typename _T8>
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 <int ID, typename _R, typename _T1, typename _T2, typename _T3, typename _T4, typename _T5, typename _T6, typename _T7, typename _T8, typename _T9>
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 <int ID, typename _R, typename _T1, typename _T2, typename _T3, typename _T4, typename _T5, typename _T6, typename _T7, typename _T8, typename _T9, typename _T10>
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 <int ID, typename _R, typename _T1, typename _T2, typename _T3, typename _T4, typename _T5, typename _T6, typename _T7, typename _T8, typename _T9, typename _T10, typename _T11>
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 <int ID, typename _R, typename _T1, typename _T2, typename _T3, typename _T4, typename _T5, typename _T6, typename _T7, typename _T8, typename _T9, typename _T10, typename _T11, typename _T12>
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 <int ID, typename _R, typename _T1, typename _T2, typename _T3, typename _T4, typename _T5, typename _T6, typename _T7, typename _T8, typename _T9, typename _T10, typename _T11, typename _T12, typename _T13>
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 <int ID, typename _R, typename _T1, typename _T2, typename _T3, typename _T4, typename _T5, typename _T6, typename _T7, typename _T8, typename _T9, typename _T10, typename _T11, typename _T12, typename _T13, typename _T14>
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<OPENCL_GL_FN_clCreateFromGLBuffer, cl_mem, cl_context, cl_mem_flags, cl_GLuint, int*>::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<OPENCL_GL_FN_clCreateFromGLRenderbuffer, cl_mem, cl_context, cl_mem_flags, cl_GLuint, cl_int*>::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<OPENCL_GL_FN_clCreateFromGLTexture, cl_mem, cl_context, cl_mem_flags, cl_GLenum, cl_GLint, cl_GLuint, cl_int*>::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<OPENCL_GL_FN_clCreateFromGLTexture2D, cl_mem, cl_context, cl_mem_flags, cl_GLenum, cl_GLint, cl_GLuint, cl_int*>::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<OPENCL_GL_FN_clCreateFromGLTexture3D, cl_mem, cl_context, cl_mem_flags, cl_GLenum, cl_GLint, cl_GLuint, cl_int*>::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<OPENCL_GL_FN_clEnqueueAcquireGLObjects, cl_int, cl_command_queue, cl_uint, const cl_mem*, cl_uint, const cl_event*, cl_event*>::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<OPENCL_GL_FN_clEnqueueReleaseGLObjects, cl_int, cl_command_queue, cl_uint, const cl_mem*, cl_uint, const cl_event*, cl_event*>::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<OPENCL_GL_FN_clGetGLContextInfoKHR, cl_int, const cl_context_properties*, cl_gl_context_info, size_t, void*, size_t*>::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<OPENCL_GL_FN_clGetGLObjectInfo, cl_int, cl_mem, cl_gl_object_type*, cl_GLuint*>::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<OPENCL_GL_FN_clGetGLTextureInfo, cl_int, cl_mem, cl_gl_texture_info, size_t, void*, size_t*>::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
@@ -0,0 +1,11 @@
clCreateFromGLBuffer
clCreateFromGLRenderbuffer
clCreateFromGLTexture
clCreateFromGLTexture2D
clCreateFromGLTexture3D
clEnqueueAcquireGLObjects
clEnqueueReleaseGLObjects
clGetGLContextInfoKHR
clGetGLObjectInfo
clGetGLTextureInfo
#total 10
@@ -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"
@@ -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)
ProcessTemplate('template/%s_wrappers.hpp.in' % module_name, 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')
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)
@@ -0,0 +1,15 @@
#ifndef __OPENCV_CORE_OCL_RUNTIME_OPENCL_GL_HPP__
#error "Invalid usage"
#endif
@CL_REMAP_ORIGIN@
#if defined __APPLE__
#include <OpenCL/cl_gl.h>
#else
#include <CL/cl_gl.h>
#endif
@CL_REMAP_DYNAMIC@
@CL_FN_DECLARATIONS@
@@ -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@
@@ -0,0 +1,5 @@
#ifndef __OPENCV_CORE_OCL_RUNTIME_OPENCL_GL_WRAPPERS_HPP__
#error "Invalid usage"
#endif
@CL_FN_INLINE_WRAPPERS@
@@ -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
+234 -1
View File
@@ -47,7 +47,9 @@
# ifdef HAVE_CUDA
# include <cuda_gl_interop.h>
# 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 <EGL/egl.h>
# elif defined(__linux__)
# include <GL/glx.h>
# 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<cl_platform_id> 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<char> 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