2

我有一个线程,其中我有 GL 上下文,并且我制作了所有渲染内容。在另一个线程中,我正在运行 OpenCL 程序。这两个任务必须与浮点值的缓冲区进行交换。

现在,这个缓冲区是 OpenGL 2D 纹理(我想使用 3D,它会很棒,但大多数设备不像cl_khr_3d_image_writes我的那样支持)。正如预期的那样,因为纹理是在 GL 线程中创建的,所以当我尝试在另一个线程的 CL 程序中使用它时,应用程序失败(没有 GL 或 CL 错误,只是应用程序崩溃)。

是否有可能以某种方式使用两个线程和 CL-GL 互操作?

4

1 回答 1

1

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

于 2014-08-08T09:55:04.633 回答