Reputation: 3032
I have one thread where I have GL context and I make all my rendering stuff. In another thread I am running OpenCL program.This two task must exchange with a buffer of float values.
Now, this buffer is OpenGL 2D texture (I wanted to use 3D, it would be excellent, but most of devices does not support cl_khr_3d_image_writes
as well as mine). As it could be expected since the texture was created in GL thread, then when I try to use it in CL program in another thread the application fails (without GL or CL errors, just application crush).
Is it possible somehow to use two threads and CL-GL interoperation?
Upvotes: 2
Views: 382
Reputation: 3406
It is entirely possible to use two threads in this way. However, you must explicitly take care of the syncronization of the buffer. See Appendix D ("Shared Objects and Multiple Contexts") of the OpenGL Specification..
The rough flow is:
1) On your GL thread do a glFenceSync() to create a GLsync object (ARB_sync extension).
2) On either thread (OpenCL is thread safe) use clCreateEventFromGLsyncKHR() to create a cl_event from the GLsync (cl_khr_gl_event extension).
3) On your CL thread use clEnqueueAcquireGLObjects() passing in your cl_event from step 2 as an event in the wait list (cl_khr_gl_sharing extension). Possibly keep hold of the cl_event created.
4) Go ahead and do your CL processing. If you are using an out of order queue make sure you make use of the cl_event created by clEnqueueAcquireGLObjects() in step 2.
5) On your CL thread use clEnqueueReleaseGLObjects() to create a cl_event (cl_khr_gl_sharing extension).
6) On your GL thread (OpenGL is not thread safe) use glCreateSyncFromCLeventARB() to create a GLsync object from the cl_event from step 5 (GL_ARB_cl_event extension).
7) Back on your GL thread use glWaitSync() to wait for the GLsync object (ARB_sync extension extension).
8) Go ahead and do your GL processing.
9) Go back to step 1.
It's just about creating sync object and passing them between the two APIs :)
Upvotes: 1