Skip to content

Commit 0843b61

Browse files
committed
Merge pull request #425 from kylelutz/opengl-osx
Add support for OpenCL-OpenCL sharing on Mac OS X
2 parents d035cf9 + d4adfc5 commit 0843b61

File tree

5 files changed

+53
-14
lines changed

5 files changed

+53
-14
lines changed

example/CMakeLists.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ if(${BOOST_COMPUTE_HAVE_VTK})
8686
find_package(VTK REQUIRED)
8787
include(${VTK_USE_FILE})
8888
add_executable(opengl_sphere opengl_sphere.cpp)
89-
target_link_libraries(opengl_sphere ${OPENCL_LIBRARIES} ${Boost_LIBRARIES} ${VTK_LIBRARIES} GL)
89+
target_link_libraries(opengl_sphere ${OPENCL_LIBRARIES} ${Boost_LIBRARIES} ${VTK_LIBRARIES})
90+
if(APPLE)
91+
target_link_libraries(opengl_sphere "-framework OpenGL")
92+
elseif(UNIX)
93+
target_link_libraries(opengl_sphere GL)
94+
endif()
9095
endif()
9196

9297
# qt examples
@@ -134,6 +139,11 @@ if(${BOOST_COMPUTE_HAVE_QT})
134139
)
135140
foreach(EXAMPLE ${QT_OPENGL_EXAMPLES})
136141
add_executable(${EXAMPLE} ${EXAMPLE}.cpp)
137-
target_link_libraries(${EXAMPLE} ${OPENCL_LIBRARIES} ${Boost_LIBRARIES} ${QT_LIBRARIES} GL)
142+
target_link_libraries(${EXAMPLE} ${OPENCL_LIBRARIES} ${Boost_LIBRARIES} ${QT_LIBRARIES})
143+
if(APPLE)
144+
target_link_libraries(${EXAMPLE} "-framework OpenGL")
145+
elseif(UNIX)
146+
target_link_libraries(${EXAMPLE} GL)
147+
endif()
138148
endforeach()
139149
endif()

example/mandelbrot.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ void MandelbrotWidget::initializeGL()
127127

128128
void MandelbrotWidget::resizeGL(int width, int height)
129129
{
130+
// scale height/width based on device pixel ratio
131+
width /= windowHandle()->devicePixelRatio();
132+
height /= windowHandle()->devicePixelRatio();
133+
130134
// resize viewport
131135
glViewport(0, 0, width, height);
132136

example/nbody.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111
#include <iostream>
1212

1313
#define GL_GLEXT_PROTOTYPES
14+
#ifdef __APPLE__
15+
#include <OpenGL/gl.h>
16+
#include <OpenGL/glext.h>
17+
#else
1418
#include <GL/gl.h>
1519
#include <GL/glext.h>
20+
#endif
1621

1722
#include <QtGlobal>
1823
#if QT_VERSION >= 0x050000

example/resize_image.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ void ImageWidget::initializeGL()
127127

128128
void ImageWidget::resizeGL(int width, int height)
129129
{
130+
// scale height/width based on device pixel ratio
131+
width /= windowHandle()->devicePixelRatio();
132+
height /= windowHandle()->devicePixelRatio();
133+
130134
// resize viewport
131135
glViewport(0, 0, width, height);
132136

include/boost/compute/interop/opengl/context.hpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
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 {
3742
inline 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

Comments
 (0)