From dceeb47cd3c7605fa81dff78c313ded3b80880c5 Mon Sep 17 00:00:00 2001 From: kallaballa Date: Thu, 10 Oct 2024 00:02:56 +0200 Subject: [PATCH 1/7] rewrote clgl device discovery --- modules/core/src/opengl.cpp | 153 ++++++++++++++++++++++-------------- 1 file changed, 96 insertions(+), 57 deletions(-) diff --git a/modules/core/src/opengl.cpp b/modules/core/src/opengl.cpp index 45aa121a4a..6e3ea6aed4 100644 --- a/modules/core/src/opengl.cpp +++ b/modules/core/src/opengl.cpp @@ -42,6 +42,12 @@ #include "precomp.hpp" +#if defined (__APPLE__) || defined(MACOSX) + #define GL_SHARING_EXTENSION "cl_APPLE_gl_sharing" +#else + #define GL_SHARING_EXTENSION "cl_khr_gl_sharing" +#endif + #ifdef HAVE_OPENGL # include "gl_core_3_1.hpp" # ifdef HAVE_CUDA @@ -1636,6 +1642,11 @@ Context& initializeContextFromGL() NO_OPENCL_SHARING_ERROR; #else cl_uint numPlatforms; + cl_device_id* devices = new cl_device_id[256]; + cl_uint devCnt; + cl_uint devUsed; + cl_context context; + cl_int status = clGetPlatformIDs(0, NULL, &numPlatforms); if (status != CL_SUCCESS) CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: Can't get number of platforms: %d", status)); @@ -1645,84 +1656,112 @@ Context& initializeContextFromGL() 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: %d", status)); + CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: Can't get platforms: %d", status)); + // TODO Filter platforms by name from OPENCV_OPENCL_DEVICE + bool sharingSupported = false; - int found = -1; - cl_device_id device = NULL; - cl_context context = NULL; + for (unsigned int i = 0; (!sharingSupported && (i < numPlatforms)); ++i) { + status = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_GPU, 0, NULL, &devCnt); + if (status != CL_SUCCESS) + CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: No devices available: %d", status)); - for (int i = 0; i < (int)numPlatforms; i++) - { - // query platform extension: presence of "cl_khr_gl_sharing" extension is required - { - AutoBuffer extensionStr; + status = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_GPU, devCnt, devices, NULL); + if (status != CL_SUCCESS) + CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: Can't get platform devices: %d", status)); + for (unsigned int j = 0; (!sharingSupported && (j < devCnt)); ++j) { 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.data(), NULL); - } + status = clGetDeviceInfo(devices[j], CL_DEVICE_EXTENSIONS, 0, NULL, &extensionSize ); if (status != CL_SUCCESS) - CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: Can't get platform extension string: %d", status)); + CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: No devices available: %d", status)); - if (!strstr((const char*)extensionStr.data(), "cl_khr_gl_sharing")) - continue; + if(extensionSize > 0) + { + char* extensions = (char*)malloc(extensionSize); + status = clGetDeviceInfo(devices[j], CL_DEVICE_EXTENSIONS, extensionSize, extensions, &extensionSize); + if (status != CL_SUCCESS) + continue; + + std::string stdDevString(extensions); + free(extensions); + + + size_t szOldPos = 0; + size_t szSpacePos = stdDevString.find(' ', szOldPos); // extensions string is space delimited + while (szSpacePos != stdDevString.npos) { + if (strcmp(GL_SHARING_EXTENSION, + stdDevString.substr(szOldPos, szSpacePos - szOldPos).c_str()) + == 0) { + // Device supports context sharing with OpenGL + devUsed = i; + sharingSupported = true; + break; + } + do { + szOldPos = szSpacePos + 1; + szSpacePos = stdDevString.find(' ', szOldPos); + } while (szSpacePos == szOldPos); + } + } } - clGetGLContextInfoKHR_fn clGetGLContextInfoKHR = (clGetGLContextInfoKHR_fn) - clGetExtensionFunctionAddressForPlatform(platforms[i], "clGetGLContextInfoKHR"); - if (!clGetGLContextInfoKHR) - continue; + if (!sharingSupported) + CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: OpenGL sharing not supported: %d", status)); - cl_context_properties properties[] = - { -#if 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; + // Define OS-specific context properties and create the OpenCL context + #if defined (__APPLE__) + CGLContextObj kCGLContext = CGLGetCurrentContext(); + CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext); + cl_context_properties props[] = + { + CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup, + 0 + }; + context = clCreateContext(props, 0,0, NULL, NULL, &ciErrNum); + #elif defined(__ANDROID__) + cl_context_properties props[] = + { + CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(), + CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(), + CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i], + 0 + }; + context = clCreateContext(props, 1, &devices[devUsed], NULL, NULL, &status); + #elif defined(_WIN32) + cl_context_properties props[] = + { + CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(), + CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(), + CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i], + 0 + }; + context = clCreateContext(props, 1, &devices[devUsed], NULL, NULL, &status); + #elif defined(__linux__) + cl_context_properties props[] = + { + CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(), + CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(), + CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i], + 0 + }; + context = clCreateContext(props, 1, &devices[devUsed], NULL, NULL, &status); + #endif - // create context - context = clCreateContext(properties, 1, &device, NULL, NULL, &status); if (status != CL_SUCCESS) - { - clReleaseDevice(device); - } + CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: Can't create context for OpenGL interop: %d", status)); else - { - found = i; break; - } } - if (found < 0) - CV_Error(cv::Error::OpenCLInitError, "OpenCL: Can't create context for OpenGL interop"); - cl_platform_id platform = platforms[found]; + cl_platform_id platform = platforms[devUsed]; std::string platformName = PlatformInfo(&platform).name(); - OpenCLExecutionContext clExecCtx = OpenCLExecutionContext::create(platformName, platform, context, device); - clReleaseDevice(device); + OpenCLExecutionContext clExecCtx = OpenCLExecutionContext::create(platformName, platform, context, devices[devUsed]); + clReleaseDevice(devices[devUsed]); clReleaseContext(context); clExecCtx.bind(); return const_cast(clExecCtx.getContext()); From 885bbc643f73a2855e5396ea8e4f3ca081ca4562 Mon Sep 17 00:00:00 2001 From: kallaballa Date: Thu, 10 Oct 2024 06:30:33 +0200 Subject: [PATCH 2/7] renaming --- modules/core/src/opengl.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/modules/core/src/opengl.cpp b/modules/core/src/opengl.cpp index 6e3ea6aed4..2de0efcfc6 100644 --- a/modules/core/src/opengl.cpp +++ b/modules/core/src/opengl.cpp @@ -1684,15 +1684,15 @@ Context& initializeContextFromGL() if (status != CL_SUCCESS) continue; - std::string stdDevString(extensions); + std::string devString(extensions); free(extensions); - size_t szOldPos = 0; - size_t szSpacePos = stdDevString.find(' ', szOldPos); // extensions string is space delimited - while (szSpacePos != stdDevString.npos) { + size_t oldPos = 0; + size_t spacePos = devString.find(' ', oldPos); // extensions string is space delimited + while (spacePos != devString.npos) { if (strcmp(GL_SHARING_EXTENSION, - stdDevString.substr(szOldPos, szSpacePos - szOldPos).c_str()) + devString.substr(oldPos, spacePos - oldPos).c_str()) == 0) { // Device supports context sharing with OpenGL devUsed = i; @@ -1700,9 +1700,9 @@ Context& initializeContextFromGL() break; } do { - szOldPos = szSpacePos + 1; - szSpacePos = stdDevString.find(' ', szOldPos); - } while (szSpacePos == szOldPos); + oldPos = spacePos + 1; + spacePos = devString.find(' ', oldPos); + } while (spacePos == oldPos); } } } @@ -1713,11 +1713,11 @@ Context& initializeContextFromGL() // Define OS-specific context properties and create the OpenCL context #if defined (__APPLE__) - CGLContextObj kCGLContext = CGLGetCurrentContext(); - CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext); + CGLContextObj cglContext = CGLGetCurrentContext(); + CGLShareGroupObj cglShareGroup = CGLGetShareGroup(cglContext); cl_context_properties props[] = { - CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup, + CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)cglShareGroup, 0 }; context = clCreateContext(props, 0,0, NULL, NULL, &ciErrNum); From 8ba7389b21daa69705949a78320a4854b737e3a2 Mon Sep 17 00:00:00 2001 From: kallaballa Date: Thu, 10 Oct 2024 06:32:22 +0200 Subject: [PATCH 3/7] properly size the devices array --- modules/core/src/opengl.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/core/src/opengl.cpp b/modules/core/src/opengl.cpp index 2de0efcfc6..c249ce715b 100644 --- a/modules/core/src/opengl.cpp +++ b/modules/core/src/opengl.cpp @@ -1642,8 +1642,8 @@ Context& initializeContextFromGL() NO_OPENCL_SHARING_ERROR; #else cl_uint numPlatforms; - cl_device_id* devices = new cl_device_id[256]; cl_uint devCnt; + cl_device_id* devices; cl_uint devUsed; cl_context context; @@ -1667,6 +1667,8 @@ Context& initializeContextFromGL() if (status != CL_SUCCESS) CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: No devices available: %d", status)); + cl_device_id* devices = new cl_device_id[devCnt]; + status = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_GPU, devCnt, devices, NULL); if (status != CL_SUCCESS) CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: Can't get platform devices: %d", status)); From 63b5dee27484fdae081869d87de1661c97d3b849 Mon Sep 17 00:00:00 2001 From: kallaballa Date: Thu, 10 Oct 2024 06:35:42 +0200 Subject: [PATCH 4/7] fixed bug: variable shadowing --- modules/core/src/opengl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/core/src/opengl.cpp b/modules/core/src/opengl.cpp index c249ce715b..f471a861cd 100644 --- a/modules/core/src/opengl.cpp +++ b/modules/core/src/opengl.cpp @@ -1667,7 +1667,7 @@ Context& initializeContextFromGL() if (status != CL_SUCCESS) CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: No devices available: %d", status)); - cl_device_id* devices = new cl_device_id[devCnt]; + devices = new cl_device_id[devCnt]; status = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_GPU, devCnt, devices, NULL); if (status != CL_SUCCESS) From 50f6d54f877c655f208a8dcf498ad01f6a7f416a Mon Sep 17 00:00:00 2001 From: kallaballa Date: Thu, 10 Oct 2024 14:48:49 +0200 Subject: [PATCH 5/7] renaming --- modules/core/src/opengl.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/core/src/opengl.cpp b/modules/core/src/opengl.cpp index f471a861cd..5610b97ce8 100644 --- a/modules/core/src/opengl.cpp +++ b/modules/core/src/opengl.cpp @@ -1641,20 +1641,20 @@ Context& initializeContextFromGL() #elif !defined(HAVE_OPENCL_OPENGL_SHARING) NO_OPENCL_SHARING_ERROR; #else - cl_uint numPlatforms; + cl_uint platformsCnt; cl_uint devCnt; cl_device_id* devices; cl_uint devUsed; cl_context context; - cl_int status = clGetPlatformIDs(0, NULL, &numPlatforms); + cl_int status = clGetPlatformIDs(0, NULL, &platformsCnt); if (status != CL_SUCCESS) CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: Can't get number of platforms: %d", status)); - if (numPlatforms == 0) + if (platformsCnt == 0) CV_Error(cv::Error::OpenCLInitError, "OpenCL: No available platforms"); - std::vector platforms(numPlatforms); - status = clGetPlatformIDs(numPlatforms, &platforms[0], NULL); + std::vector platforms(platformsCnt); + status = clGetPlatformIDs(platformsCnt, &platforms[0], NULL); if (status != CL_SUCCESS) CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: Can't get platforms: %d", status)); @@ -1662,7 +1662,7 @@ Context& initializeContextFromGL() // TODO Filter platforms by name from OPENCV_OPENCL_DEVICE bool sharingSupported = false; - for (unsigned int i = 0; (!sharingSupported && (i < numPlatforms)); ++i) { + for (unsigned int i = 0; (!sharingSupported && (i < platformsCnt)); ++i) { status = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_GPU, 0, NULL, &devCnt); if (status != CL_SUCCESS) CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: No devices available: %d", status)); From 4cbb96b396f9423fdc83cc8a927044de3973898c Mon Sep 17 00:00:00 2001 From: kallaballa Date: Thu, 10 Oct 2024 15:14:58 +0200 Subject: [PATCH 6/7] use new instead of malloc and guard it --- modules/core/src/opengl.cpp | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/modules/core/src/opengl.cpp b/modules/core/src/opengl.cpp index 5610b97ce8..22173d9495 100644 --- a/modules/core/src/opengl.cpp +++ b/modules/core/src/opengl.cpp @@ -1681,14 +1681,27 @@ Context& initializeContextFromGL() if(extensionSize > 0) { - char* extensions = (char*)malloc(extensionSize); - status = clGetDeviceInfo(devices[j], CL_DEVICE_EXTENSIONS, extensionSize, extensions, &extensionSize); - if (status != CL_SUCCESS) - continue; + char* extensions = nullptr; - std::string devString(extensions); - free(extensions); + try { + extensions = new char[extensionSize]; + status = clGetDeviceInfo(devices[j], CL_DEVICE_EXTENSIONS, extensionSize, extensions, &extensionSize); + if (status != CL_SUCCESS) + continue; + } catch(std::exception& ex) { + CV_Error(cv::Error::OpenCLInitError, "OpenCL: Exception thrown during device extensions gathering"); + } + + std::string devString; + + if(extensions != nullptr) { + devString = extensions; + delete[] extensions; + } + else { + CV_Error(cv::Error::OpenCLInitError, "OpenCL: Unexpected error during device extensions gathering"); + } size_t oldPos = 0; size_t spacePos = devString.find(' ', oldPos); // extensions string is space delimited @@ -1710,8 +1723,7 @@ Context& initializeContextFromGL() } if (!sharingSupported) - CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: OpenGL sharing not supported: %d", status)); - + continue; // Define OS-specific context properties and create the OpenCL context #if defined (__APPLE__) From 3edcf410b6f485aa0cca51ffa21a7ce894ac6042 Mon Sep 17 00:00:00 2001 From: kallaballa Date: Fri, 11 Oct 2024 02:18:14 +0200 Subject: [PATCH 7/7] more guarding --- modules/core/src/opengl.cpp | 189 +++++++++++++++++++----------------- 1 file changed, 98 insertions(+), 91 deletions(-) diff --git a/modules/core/src/opengl.cpp b/modules/core/src/opengl.cpp index 22173d9495..83be34f147 100644 --- a/modules/core/src/opengl.cpp +++ b/modules/core/src/opengl.cpp @@ -1641,11 +1641,11 @@ Context& initializeContextFromGL() #elif !defined(HAVE_OPENCL_OPENGL_SHARING) NO_OPENCL_SHARING_ERROR; #else - cl_uint platformsCnt; - cl_uint devCnt; - cl_device_id* devices; - cl_uint devUsed; - cl_context context; + cl_uint platformsCnt = 0; + cl_uint devCnt = 0; + cl_device_id* devices = nullptr; + cl_uint devUsed = 0; + cl_context context = nullptr; cl_int status = clGetPlatformIDs(0, NULL, &platformsCnt); if (status != CL_SUCCESS) @@ -1667,102 +1667,109 @@ Context& initializeContextFromGL() if (status != CL_SUCCESS) CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: No devices available: %d", status)); - devices = new cl_device_id[devCnt]; + try { + devices = new cl_device_id[devCnt]; - status = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_GPU, devCnt, devices, NULL); - if (status != CL_SUCCESS) - CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: Can't get platform devices: %d", status)); - - for (unsigned int j = 0; (!sharingSupported && (j < devCnt)); ++j) { - size_t extensionSize; - status = clGetDeviceInfo(devices[j], CL_DEVICE_EXTENSIONS, 0, NULL, &extensionSize ); + status = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_GPU, devCnt, devices, NULL); if (status != CL_SUCCESS) - CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: No devices available: %d", status)); - - if(extensionSize > 0) - { - char* extensions = nullptr; - - try { - extensions = new char[extensionSize]; - - status = clGetDeviceInfo(devices[j], CL_DEVICE_EXTENSIONS, extensionSize, extensions, &extensionSize); - if (status != CL_SUCCESS) - continue; - } catch(std::exception& ex) { - CV_Error(cv::Error::OpenCLInitError, "OpenCL: Exception thrown during device extensions gathering"); - } + CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: Can't get platform devices: %d", status)); + + for (unsigned int j = 0; (!sharingSupported && (j < devCnt)); ++j) { + size_t extensionSize; + status = clGetDeviceInfo(devices[j], CL_DEVICE_EXTENSIONS, 0, NULL, &extensionSize ); + if (status != CL_SUCCESS) + CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: No devices available: %d", status)); + + if(extensionSize > 0) + { + char* extensions = nullptr; + + try { + extensions = new char[extensionSize]; + + status = clGetDeviceInfo(devices[j], CL_DEVICE_EXTENSIONS, extensionSize, extensions, &extensionSize); + if (status != CL_SUCCESS) + continue; + } catch(...) { + CV_Error(cv::Error::OpenCLInitError, "OpenCL: Exception thrown during device extensions gathering"); + } - std::string devString; + std::string devString; - if(extensions != nullptr) { - devString = extensions; - delete[] extensions; - } - else { - CV_Error(cv::Error::OpenCLInitError, "OpenCL: Unexpected error during device extensions gathering"); - } + if(extensions != nullptr) { + devString = extensions; + delete[] extensions; + } + else { + CV_Error(cv::Error::OpenCLInitError, "OpenCL: Unexpected error during device extensions gathering"); + } - size_t oldPos = 0; - size_t spacePos = devString.find(' ', oldPos); // extensions string is space delimited - while (spacePos != devString.npos) { - if (strcmp(GL_SHARING_EXTENSION, - devString.substr(oldPos, spacePos - oldPos).c_str()) - == 0) { - // Device supports context sharing with OpenGL - devUsed = i; - sharingSupported = true; - break; + size_t oldPos = 0; + size_t spacePos = devString.find(' ', oldPos); // extensions string is space delimited + while (spacePos != devString.npos) { + if (strcmp(GL_SHARING_EXTENSION, + devString.substr(oldPos, spacePos - oldPos).c_str()) + == 0) { + // Device supports context sharing with OpenGL + devUsed = i; + sharingSupported = true; + break; + } + do { + oldPos = spacePos + 1; + spacePos = devString.find(' ', oldPos); + } while (spacePos == oldPos); } - do { - oldPos = spacePos + 1; - spacePos = devString.find(' ', oldPos); - } while (spacePos == oldPos); } } - } - - if (!sharingSupported) + } catch(...) { + CV_Error(cv::Error::OpenCLInitError, "OpenCL: Exception thrown during device information gathering"); + if(devices != nullptr) { + delete[] devices; + } continue; + } - // Define OS-specific context properties and create the OpenCL context - #if defined (__APPLE__) - CGLContextObj cglContext = CGLGetCurrentContext(); - CGLShareGroupObj cglShareGroup = CGLGetShareGroup(cglContext); - cl_context_properties props[] = - { - CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)cglShareGroup, - 0 - }; - context = clCreateContext(props, 0,0, NULL, NULL, &ciErrNum); - #elif defined(__ANDROID__) - cl_context_properties props[] = - { - CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(), - CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(), - CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i], - 0 - }; - context = clCreateContext(props, 1, &devices[devUsed], NULL, NULL, &status); - #elif defined(_WIN32) - cl_context_properties props[] = - { - CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(), - CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(), - CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i], - 0 - }; - context = clCreateContext(props, 1, &devices[devUsed], NULL, NULL, &status); - #elif defined(__linux__) - cl_context_properties props[] = - { - CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(), - CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(), - CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i], - 0 - }; - context = clCreateContext(props, 1, &devices[devUsed], NULL, NULL, &status); - #endif + if (sharingSupported) { + // Define OS-specific context properties and create the OpenCL context +#if defined (__APPLE__) + CGLContextObj cglContext = CGLGetCurrentContext(); + CGLShareGroupObj cglShareGroup = CGLGetShareGroup(cglContext); + cl_context_properties props[] = + { + CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)cglShareGroup, + 0 + }; + context = clCreateContext(props, 0,0, NULL, NULL, &ciErrNum); +#elif defined(__ANDROID__) + cl_context_properties props[] = + { + CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(), + CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(), + CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i], + 0 + }; + context = clCreateContext(props, 1, &devices[devUsed], NULL, NULL, &status); +#elif defined(_WIN32) + cl_context_properties props[] = + { + CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(), + CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(), + CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i], + 0 + }; + context = clCreateContext(props, 1, &devices[devUsed], NULL, NULL, &status); +#elif defined(__linux__) + cl_context_properties props[] = + { + CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(), + CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(), + CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i], + 0 + }; + context = clCreateContext(props, 1, &devices[devUsed], NULL, NULL, &status); +#endif + } if (status != CL_SUCCESS) CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: Can't create context for OpenGL interop: %d", status));