Whatlahuhu
Whatlahuhu

Reputation: 19

How to create OpenCL context based on the openGL context on mac os x

I am creating a project about particle system on mac os x. I found similar questions on Internet, and my question is the same as How can I create an shared context between OpenGL and OpenCL with glfw3 on OSX? ,but I still haven't solved my problem yet. Please help me, thank you.

This is a part of my code:

    CGLContextObj glContext = CGLGetCurrentContext();
    CGLShareGroupObj shareGroup = CGLGetShareGroup(glContext);

    cl_context_properties props[] =
    {
      CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
      (cl_context_properties)kCGLShareGroup,`
      0
    };

my error messages are :

particles.cpp:522:2: error: ‘CGLContextObj’ was not declared in this scope CGLContextObj glContext = CGLGetCurrentContext();

particles.cpp:523:2: error: ‘CGLShareGroupObj’ was not declared in this scope CGLShareGroupObj shareGroup = CGLGetShareGroup(glContext);

particles.cpp:527:2: error: ‘CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE’ was not declared in this scope CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,

particles.cpp:528:25: error: ‘kCGLShareGroup’ was not declared in this scope (cl_context_properties)kCGLShareGroup,0

Upvotes: 1

Views: 1758

Answers (1)

sarasvati
sarasvati

Reputation: 792

What header files do you include? Location of symbols in header files:

  • #include <OpenCL/cl_gl_ext.h> for CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE
  • #include <OpenGL/CGLDevice.h> for CGLGetShareGroup()
  • #include <OpenGL/CGLCurrent.h> for CGLGetCurrentContext()

Although you could include the header files above, I find it more convenient to just include the following 2 header files:

#include <OpenCL/opencl.h>
#include <OpenGL/OpenGL.h>

Example code:

CGLContextObj    gl_ctx        = CGLGetCurrentContext();
CGLShareGroupObj gl_sharegroup = CGLGetShareGroup(gl_ctx);

cl_context default_ctx;
cl_context_properties properties[] = {
        CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties) gl_sharegroup,
        0
};

cl_int err_code;
default_ctx = clCreateContext(  properties,
                                1,
                                &device, /* cl_device */
                                [](const char* errinfo, const void* private_info, size_t cb, void* user_data) -> void {
                                    /* context-creation and runtime error handler */
                                    cout << "Context error: " << errinfo << endl;
                                }, /* C++11, this parameter can be nullptr */
                                nullptr /*user_data*/,
                                &err_code);

Upvotes: 4

Related Questions