diff --git a/doc/source/runtime.rst b/doc/source/runtime.rst index 502f215a55784130ae7b55869a0b8aa32574d2b9..d301f44032a99d901aac1f15ce2ea1471a76b729 100644 --- a/doc/source/runtime.rst +++ b/doc/source/runtime.rst @@ -127,6 +127,7 @@ Platforms, Devices and Contexts :attr:`context_properties.CL_GLX_DISPLAY_KHR`, :attr:`context_properties.CL_WGL_HDC_KHR`, and :attr:`context_properties.CL_CGL_SHAREGROUP_KHR` + :attr:`context_properties.CL_CGL_SHAREGROUP_APPLE` the value in the key-value pair is a PyOpenGL context or display instance. @@ -709,6 +710,14 @@ with GL support. See :func:`have_gl`. Return *True* if PyOpenCL was compiled with OpenGL interoperability, otherwise *False*. +.. function:: get_apple_cgl_share_group() + + Get share group handle for current CGL context. + + Apple OS X only. + + ..versionadded:: 2011.1 + .. class:: GLBuffer(context, flags, bufobj) :class:`GLBuffer` is a subclass of :class:`MemoryObject`. diff --git a/examples/gl_interop_demo.py b/examples/gl_interop_demo.py index 09a0e65cfc679440ad04433f03d38def3fdcb944..f65c3ef09def48f7da7aa7dc1b18a3fb1eebb65f 100644 --- a/examples/gl_interop_demo.py +++ b/examples/gl_interop_demo.py @@ -1,7 +1,6 @@ from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.raw.GL.VERSION.GL_1_5 import glBufferData as rawGlBufferData -from OpenGL import platform, GLX, WGL import pyopencl as cl @@ -23,30 +22,11 @@ __kernel void generate_sin(__global float2* a) def initialize(): plats = cl.get_platforms() - ctx_props = cl.context_properties - props = [(ctx_props.PLATFORM, plats[0])] - - import sys - if sys.platform == "linux2": - props.append( - (ctx_props.GL_CONTEXT_KHR, platform.GetCurrentContext())) - props.append( - (ctx_props.GLX_DISPLAY_KHR, - GLX.glXGetCurrentDisplay())) - elif sys.platform == "win32": - props.append( - (ctx_props.GL_CONTEXT_KHR, platform.GetCurrentContext())) - props.append( - (ctx_props.WGL_HDC_KHR, - WGL.wglGetCurrentDC())) - elif sys.platform == "darwin": - pass - else: - raise NotImplementedError("platform '%s' not yet supported" - % sys.platform) - - ctx = cl.Context(properties=props) + from pyopencl.tools import get_gl_sharing_context_properties + ctx = cl.Context(properties=props + [(ctx_props.PLATFORM, plats[0])] + + get_gl_sharing_context_properties()) glClearColor(1, 1, 1, 1) glColor(0, 0, 1) diff --git a/pyopencl/tools.py b/pyopencl/tools.py index d3ac00ee162a2df06fdf9e21026ca716dd881767..40e2b41122f052160f175bd06c5841ae389347a8 100644 --- a/pyopencl/tools.py +++ b/pyopencl/tools.py @@ -249,4 +249,35 @@ def has_double_support(dev): +def get_gl_sharing_context_properties(): + ctx_props = cl.context_properties + + from OpenGL import platform as gl_platform, GLX, WGL + + props = [] + + import sys + if sys.platform == "linux2": + props.append( + (ctx_props.GL_CONTEXT_KHR, gl_platform.GetCurrentContext())) + props.append( + (ctx_props.GLX_DISPLAY_KHR, + GLX.glXGetCurrentDisplay())) + elif sys.platform == "win32": + props.append( + (ctx_props.GL_CONTEXT_KHR, gl_platform.GetCurrentContext())) + props.append( + (ctx_props.WGL_HDC_KHR, + WGL.wglGetCurrentDC())) + elif sys.platform == "darwin": + props.append( + (ctx_props.USE_CGL_SHAREGROUP_APPLE, cl. get_apple_cgl_share_group())) + else: + raise NotImplementedError("platform '%s' not yet supported" + % sys.platform) + + return props + + + # vim: foldmethod=marker diff --git a/src/wrapper/wrap_cl.hpp b/src/wrapper/wrap_cl.hpp index 533074c42be3322b8e112b62b653373a55508408..2460b497494b8d42898c64de1aa4a16e68c7fad7 100644 --- a/src/wrapper/wrap_cl.hpp +++ b/src/wrapper/wrap_cl.hpp @@ -787,20 +787,6 @@ namespace pyopencl // from dev_type else { -#ifdef __APPLE__ - //If the user passes in no devices we assume GL context sharing for Apple - CGLContextObj kCGLContext = CGLGetCurrentContext(); - CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext); - cl_context_properties propsarr[] = - { - CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup, - 0 - }; - - ctx = clCreateContext(propsarr, 0, 0, NULL, NULL, &status_code); - - PYOPENCL_PRINT_CALL_TRACE("clCreateContext Apple GL Sharing"); -#else cl_device_type dev_type = CL_DEVICE_TYPE_DEFAULT; if (py_dev_type.ptr() != Py_None) dev_type = py::extract<cl_device_type>(py_dev_type)(); @@ -808,7 +794,6 @@ namespace pyopencl ctx = clCreateContextFromType(props_ptr, dev_type, 0, 0, &status_code); PYOPENCL_PRINT_CALL_TRACE("clCreateContextFromType"); -#endif } if (status_code != CL_SUCCESS) @@ -825,6 +810,10 @@ namespace pyopencl } } + + + + // }}} // {{{ command_queue @@ -2805,6 +2794,21 @@ namespace pyopencl #ifdef HAVE_GL + +#ifdef __APPLE__ + inline + cl_context_properties get_apple_cgl_share_group() + { + CGLContextObj kCGLContext = CGLGetCurrentContext(); + CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext); + + return (cl_context_properties) kCGLShareGroup; + } +#endif /* __APPLE__ */ + + + + class gl_buffer : public memory_object { public: diff --git a/src/wrapper/wrap_cl_part_2.cpp b/src/wrapper/wrap_cl_part_2.cpp index 34a20110a279ae0e6f1408c945e6187a4a64140f..899db5b893af39e7f7ea3779e4934dd366b48dca 100644 --- a/src/wrapper/wrap_cl_part_2.cpp +++ b/src/wrapper/wrap_cl_part_2.cpp @@ -182,6 +182,11 @@ void pyopencl_expose_part_2() DEF_SIMPLE_FUNCTION(have_gl); #ifdef HAVE_GL + +#ifdef __APPLE__ + DEF_SIMPLE_FUNCTION(get_apple_cgl_share_group); +#endif /* __APPLE__ */ + { typedef gl_buffer cls; py::class_<cls, py::bases<memory_object>, boost::noncopyable>( diff --git a/src/wrapper/wrap_constants.cpp b/src/wrapper/wrap_constants.cpp index a81adc9b111a4c6253635482943186b8ebc14b18..9104618efa81ded6325faa54a00a6f5067ac4ad6 100644 --- a/src/wrapper/wrap_constants.cpp +++ b/src/wrapper/wrap_constants.cpp @@ -262,6 +262,9 @@ void pyopencl_expose_constants() ADD_ATTR( ,GLX_DISPLAY_KHR); ADD_ATTR( ,WGL_HDC_KHR); ADD_ATTR( ,CGL_SHAREGROUP_KHR); +#ifdef __APPLE__ + ADD_ATTR( ,CGL_SHAREGROUP_APPLE); +#endif /* __APPLE__ */ #endif }