Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] How to render from another thread?

This thread is locked; no one can reply to it. rss feed Print
[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.
I use std::thread in C++11, and I only need OpenGL rendering (no need D3D).

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.

--
"Either help out or stop whining" - Evert

wqking
Member #16,641
February 2017

Can you explain "For platform compatibility"?
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.

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
avatar

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
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

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.

--
"Either help out or stop whining" - Evert

Chris Katko
Member #1,881
January 2002
avatar

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:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

wqking
Member #16,641
February 2017

@All:
Now I create the display from the rendering thread, and all works perfect.
I have another question on memory bitmap but I will start a new thread.

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:
WebGL maybe different, but Android should support multi threaded rendering well. I tried multi threaded rendering with SFML on Android and it works well (but I didn't try orientation change), I will try it with Allegro after I have setup Allegro Android.
Also I believe multi threaded rendering is the future of native high performance games.

@Chris:
What I'm making is a game framework (hobby project), I would like the user not aware of the existing of any threads. Thus the user will work as if there is no multi thread. That's why I would like to keep any logic in the main thread. I would like that multi threaded rendering only increase performance and not increase the work load. :D

Go to: