1919#include < boost/compute/exception/unsupported_extension_error.hpp>
2020#include < boost/compute/interop/opengl/cl_gl.hpp>
2121
22+ #ifdef __APPLE__
23+ #include < OpenCL/cl_gl_ext.h>
24+ #include < OpenGL/OpenGL.h>
25+ #endif
26+
2227#ifdef __linux__
2328#include < GL/glx.h>
2429#endif
@@ -37,12 +42,31 @@ namespace compute {
3742inline context opengl_create_shared_context ()
3843{
3944 // name of the OpenGL sharing extension for the system
40- #if defined(__APPLE__)
45+ #if defined(__APPLE__)
4146 const char *cl_gl_sharing_extension = " cl_APPLE_gl_sharing" ;
42- #else
47+ #else
4348 const char *cl_gl_sharing_extension = " cl_khr_gl_sharing" ;
44- #endif
49+ #endif
50+
51+ #if defined(__APPLE__)
52+ // get OpenGL share group
53+ CGLContextObj cgl_current_context = CGLGetCurrentContext ();
54+ CGLShareGroupObj cgl_share_group = CGLGetShareGroup (cgl_current_context);
55+
56+ cl_context_properties properties[] = {
57+ CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
58+ (cl_context_properties) cgl_share_group,
59+ 0
60+ };
61+
62+ cl_int error = 0 ;
63+ cl_context cl_gl_context = clCreateContext (properties, 0 , 0 , 0 , 0 , &error);
64+ if (!cl_gl_context){
65+ BOOST_THROW_EXCEPTION (opencl_error (error));
66+ }
4567
68+ return context (cl_gl_context, false );
69+ #else
4670 typedef cl_int (*GetGLContextInfoKHRFunction)(
4771 const cl_context_properties*, cl_gl_context_info, size_t , void *, size_t *
4872 );
@@ -62,21 +86,12 @@ inline context opengl_create_shared_context()
6286 continue ;
6387 }
6488
65- // get OpenGL share group (needed for Apple)
66- #ifdef __APPLE__
67- CGLContextObj cgl_current_context = CGLGetCurrentContext ();
68- CGLShareGroupObj cgl_share_group = CGLGetShareGroup (cgl_current_context);
69- #endif
70-
7189 // create context properties listing the platform and current OpenGL display
7290 cl_context_properties properties[] = {
7391 CL_CONTEXT_PLATFORM, (cl_context_properties) platform.id (),
7492 #if defined(__linux__)
7593 CL_GL_CONTEXT_KHR, (cl_context_properties) glXGetCurrentContext (),
7694 CL_GLX_DISPLAY_KHR, (cl_context_properties) glXGetCurrentDisplay (),
77- #elif defined(__APPLE__)
78- CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
79- (cl_context_properties) cgl_share_group,
8095 #elif defined(WIN32)
8196 CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext (),
8297 CL_WGL_HDC_KHR, (cl_context_properties) wglGetCurrentDC (),
@@ -106,6 +121,7 @@ inline context opengl_create_shared_context()
106121 // return CL-GL sharing context
107122 return context (gpu, properties);
108123 }
124+ #endif
109125
110126 // no CL-GL sharing capable devices found
111127 BOOST_THROW_EXCEPTION (
0 commit comments