![]() |
|
[A5] How to render from another thread? |
wqking
Member #16,641
February 2017
|
What I want to do is, run all game logic from the main thread, send all render command to a queue, then in another thread, the thread render the queue on the screen. How to do it properly? My current problem is, Allegro uses TLS for render context, so in a new thread, there is no render context (the display, etc), how can I pass the render context to a new thread from the main thread? Thanks |
Elias
Member #358
May 2000
|
OpenGL can only be called from the same thread which calls al_create_display. Also, all bitmaps need to be created on that thread. For platform compatibility, that thread also should be the main thread. That said, technically you can change to a different thread by calling al_set_target_bitmap. I think ex_threads or ex_threads2 do that. -- |
wqking
Member #16,641
February 2017
|
Can you explain "For platform compatibility"? Also, I believe we can transfer the GL context between threads as long as there is only one context active among all the threads. For example, in SFML, the window can be create in the main thread, then deactivate the window, and re-activate it from another thread. Is Allegro different? Thanks |
SiegeLord
Member #7,827
October 2006
![]() |
Elias said: For platform compatibility, that thread also should be the main thread. ex_threads definitely creates the new displays in each separate thread. What platforms does that not work on? "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
Elias
Member #358
May 2000
|
I ran into troubles when porting my games to Android as well as WebGL when doing anything involving changing the thread with the OpenGL context. Our Android port has some serious bugs (none of my games support orientation change without a special setting in the manifest) - so probably once that's fixed it might work. I really need to get back to that. And WebGL support is not really existing right now with multiple threads - so I guess in general yeah, it should at least work in Linux/OSX/Windows. -- |
Chris Katko
Member #1,881
January 2002
![]() |
wqking said: I think it's more nature to put rendering to another thread and let main thread deal with the logic because the whole application is created from the main thread. Is that a real difference though? Is it really harder / more complex to have logic in a thread spawned from main() instead of inside main() (but still spawning a thread for graphics). -----sig: |
wqking
Member #16,641
February 2017
|
@All: Update: I just found that we can create the display from the main thread, then call al_set_target_bitmap(nullptr) to release the context, and in render thread call al_set_target_backbuffer(this->window) to take the context. OT: can any one tell me how to quote other's message? I can't see any quote button. @Elias: @Chris: |
|