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

倖福魔咒の 提交于 2019-12-13 02:13:25

问题


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


回答1:


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);


来源:https://stackoverflow.com/questions/37609454/how-to-create-opencl-context-based-on-the-opengl-context-on-mac-os-x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!