Is OpenGL threadsafe for multiple threads with distinct contexts?

旧时模样 提交于 2019-12-20 10:55:35

问题


I know that sharing a single context between threads is bad news. I know that I can safely create and use a context with an offscreen framebuffer on a secondary thread when nothing is happening with GL on the main thread.

I haven't yet been able to find a definitive answer to the question of whether I can safely create two contexts on two different threads (say, a main thread drawing to the screen, and a secondary thread doing offscreen drawing work) and have them both making GL function calls simultaneously.

In other words, as long as the contexts are different, can two threads "share" the C API and thus the GPU? Or is that inherently something that is unshareable? Or is this implementation-specific?

Asking specifically for OpenGL ES on iOS, but it's probably a general GL question.


回答1:


Yes, you need to use one context for each thread you want to use OpenGL with, also you can share objects between the contexts. This is the way to go :)




回答2:


Option 1: If you don't use the context by two threads simultaneously, one context is enough.

Option 2: If you need to use OpenGL by several threads simultaneously, you need more than one context. Then, if the contexts share their Sharegroup, they share their OpenGL content like textures. This way you can load textures or do heavy framebuffer processing on a background thread.

Have a look at the last section about Sharegroups here: http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/WorkingwithOpenGLESContexts/WorkingwithOpenGLESContexts.html

Option 3: GLKit provides some built-in background processing, for example asynchronous texture loading via GLKTextureLoaders - textureWithContentsOfFile. I don't know all the options, but it definitely simplifies some of uses cases of asynchronous OpenGL.



来源:https://stackoverflow.com/questions/3937257/is-opengl-threadsafe-for-multiple-threads-with-distinct-contexts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!