iDevGames Forums

Full Version: How do you synchronize data between your game and render thread?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is kind of a generic architecture question but I currently have my game and render loop on separate threads. What method(s) have you used come time to populate your renderer with render data/commands from your game loop?

I would like to avoid locks as much as possible, and memory use is not much of a concern at the moment. Also, the game thread will create and destroy objects at will and the render thread will simply read data.

Initially what comes to mind is triple buffering my render data. For example my game thread has an array of all my objects which need to be rendered (the first "buffer"). The render has two buffers, the current working copy of data being sent to the GPU (the second buffer), and a another buffer to be filled by the game loop (the third buffer). When the second buffer has been drawn and the third buffer is full they are swapped so the third buffer is sent to the GPU and the second buffer is filled by the game loop. I've found this implementation which seems akin to what I want, I don't like the crazy use of macros though...

Another thought is a carefully planned ring buffer, but I have not thought of the exact details for this.

Also, this person asked a similar question without much consensus on a good answer in the end.
I have triple buffered game state and used CAS to exchange the buffers, to do this in a lock-free manner which allows the physics and render threads to run at entirely independent framerates. No idea if the source builds any more, but
Thank you for the reply OneSadCookie. That is good to know this structure has worked for you. Thanks for the sample too, I'll read through it on my computer this weekend. Looks clear and consise on my phone. Also, compare and swap seems like the best mechanism for me to maintain thread safety.
I wrote a very basic multi-threaded renderer for our Chipmunk Showcase iOS app recently. It's somewhat basic as I didn't want to spend too much time on it. The main focus was moving the draw calls off of the main thread to avoid input latency. I still generate the VBO data on the main thread, but it's uploaded and drawn on the secondary thread. It helped significantly with the input latency and the implementation is simple, but I don't think it increases performance that much.

Probably not really what you are looking for, but the source is here if you are interested:
Thank you Skorche. Any examples are always helpful!

OneSadCookie, I read though your sample over the weekend. It was good to see your solution. On a side note if anyone is looking for what atomic operations are available for OSX and iOS here is the Apple Doc.
Reference URL's