Search code examples
parallel-processinglibgdxrenderinggame-loop

Render loop - maximum parallelization


Below is a UML sequence diagram showing processing time on my understanding of the game loop in the library libGDX. I think it should be the same architecture for every other game library. I am not sure if I understood it correctly. sequence diagram CP and GPU serial In theory CPU and GPU work in parallel. When the CPU waits until the GPU is finished for the buffer change this makes it a serial process. How can make my game loop work in parallel or is my understanding wrong?

Now image we want to have parallelisation and that the GPU is slower than the CPU and the CPU continues with the next frame while the GPU is rendering. We have a second thread waiting for the GPU to finish. Once the GPU is done the next image is calculated. Where are the OpenGL state changes and the draw commands going now? The GPU is busy now. This leads me to the conclusion that I am missing something. sequence diagram parallel Cpu and GPU

EDIT:

Suggested by Ross Vander: suggested by Vander


Solution

  • One problem I see with the second diagram which may be where you're going wrong is that the GPU seems to return to CPU thread 2 even though it was CPU thread 1 that sent data to the GPU and started blocking on it. Swapping two references for the front and back buffer doesn't change which thread is blocking on the GPU. I think the order of events should be more like this: CPU thread 1 sends data from the front buffer to the GPU to render. Simultaneously, thread 2 is writing to the back buffer. When the GPU finishes, thread 1 is free to swap the front and back buffers (assuming thread 2 is done) and then send the data to the GPU. Thread 2 writes to the back buffer again while the GPU is working.

    Update: taken from https://github.com/libgdx/libgdx/wiki/Threading

    Any graphics operations directly involving OpenGL need to be executed on the rendering thread. Doing so on a different thread results in undefined behaviour. This is due to the OpenGL context only being active on the rendering thread. Making the context current on another thread has its problems on a lot of Android devices, hence it is unsupported.