Skip to content

Instantly share code, notes, and snippets.

@ugovaretto
Last active December 18, 2015 15:28
Show Gist options
  • Save ugovaretto/5804278 to your computer and use it in GitHub Desktop.
Save ugovaretto/5804278 to your computer and use it in GitHub Desktop.
Create OpenCL context for OpenGL interoperability
#include <vector>
#include <stdexcept>
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#else
#ifdef WIN32
#include <wingdi.h>
#else
#include <GL/glx.h>
#endif
#endif
#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
typedef std::vector< cl_context_properties > CLContextProperties;
CLContextProperties create_cl_gl_interop_properties(cl_platform_id platform = 0) {
#if defined (__APPLE__) || defined(MACOSX)
CGLContextObj kCGLContext = CGLGetCurrentContext();
CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext);
cl_context_properties props[] =
{
CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup,
0
};
return CLProperties(props, props + sizeof(props) / sizeof(cl_context_properties));
#else
#if 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)platform,
0
};
return CLProperties(props, props + sizeof(props) / sizeof(cl_context_properties));
#else
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)platform,
0
};
return CLProperties(props, props + sizeof(props) / sizeof(cl_context_properties));
#endif
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment