Is there a way to GL render 3d data directly from CL memory?

Anybody know if there is any way to render 3d vertex data directly from a CL memory buffer, without copying to host memory and back?

For example, if I compute a surface of 3d coordinates using OpenCL, and then want to visualize this using GL with lighting, camera, etc.

I can copy the data to the host and then feed it back via individual GL vertex calls, but it seems if the data could remain on the graphics card this could be much faster. There is a way to move CL memory directly to GL texture memory, so I’m hoping something like that might exist for vertex/polygon data as well…


I vaguely remember NVIDIA having an example that did this. It moved points in a sin-wave and rendered using OpenGL. In any case, it certainly is possible and you should read sections 9.11 and 9.12 in the OpenCL specification.

I don’t have enough experience with sharing graphics contexts with OpenCL, but with CUDA what you do is share a vertex buffer object (VBO) and write the vertices to the VBO which you can then send to OpenGL for rendering instead of passing the vertices one by one from the CPU.

Refer to section 9.7 of the 1.1 spec which describes how to create a CL context for sharing with GL. You can pass a GLcontext or a sharegroup (on Mac OS X) to cl_context_properties argument in clCreateContext.

Once you have a CL context that is associated with a GL context or sharegroup, then to share a VBO, renderbuffer or texture, all you do is create the cl_mem object using the clCreateFromGLBuffer, clCreateFromGLRenderbuffer or clCreateFromGLTexture{2D | 3D} APIs. The cl_mem object and the GL object both share the same data so there are no copies.

To use a cl_mem object created from a GL object inside CL, you need to call clEnqueueAcquireGLObjects and once you are doing using these objects you call clEnqueueReleaseGLObjects. You can also do fine grained sharing by creating a cl_event from a GLsync object and vice-versa. All of this is described in sections 9.8 and 9.9 of the 1.1 spec.