从 OpenCL 内核修改 VBO 数据

Modify VBO data from OpenCL kernel

我希望我的 OpenCL 程序的输出直接绘制在屏幕上,而不是从 GPU 下载然后再次上传。我如何将数据从内核放入我的顶点数组?我正在使用 glfw3 和 glew 以及 nVidia 在 c++ 中的默认 OpenCL 库。

这个例子可能有帮助:http://enja.org/2010/08/27/adventures-in-opencl-part-2-particles-with-opengl/

关键要求是设置 CL/GL 共享上下文。根据您的 GL 主机,您需要为 CL 上下文设置不同的属性:

苹果:

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

Windows:

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)(platforms[0])(),
            0
        };

其他:

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)(platforms[0])(),
            0
        };

然后,您创建上下文,并使用 GL 缓冲区(每次获取和释放它们):

context = cl::Context(CL_DEVICE_TYPE_GPU, props);
cl::BufferGL glbuffer(context, CL_MEM_READ_WRITE, myvbo, &err);

for (each frame){
     queue.enqueueAcquireGLObjects(&glbuffer);
     //use glbuffer as if it is a clbuffer
     queue.enqueueReleaseGLObjects(&glbuffer);
}