Reputation: 719
I want the output of my OpenCL program to be drawn directly on the screen, without being downloaded from the GPU to then be uploaded again. How would I put data into my vertex array from the kernel? I'm using glfw3 and glew together with the default OpenCL library by nVidia in c++.
Upvotes: 0
Views: 408
Reputation: 8410
This example may help: http://enja.org/2010/08/27/adventures-in-opencl-part-2-particles-with-opengl/
The key requirement is to set up the CL/GL shared context. Depending on your GL host, you need to set different properties to the CL context:
Apple:
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
};
Other:
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
};
Then, you create the context, and use the GL buffers (acquiring and releasing them each time):
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);
}
Upvotes: 2