Domanda

I'm writing single threaded game engine for 2D games. Right now my game loop looks rougly like this:

  1. Update game logic
  2. Update physics
  3. Handle collisions
  4. Render scene

I'm using OpenGL for rendering. I'm wondering about 'Render scene' step position in game loop. There is a SwapBuffers() call on the end of the 'Render scene' step, so CPU is blocked until all rendering is finished. What do you think about this game loop:

  1. Render scene (previous frame)
  2. Logic, physics, collisions
  3. Swap buffers

This approach will bring better parallelism between CPU and GPU. Am I right?

È stato utile?

Soluzione

I'm thinking that since rendering occurs asynchronously in OpenGL, on the GPU, it should certainly be possible that the GPU finishes rendering while the CPU updates logic/physics/etc. Swapping buffers waits until all commands are executed, so doing that last might give better performance.

The only way to be sure is to try it, however. If the CPU is the bottleneck it won't matter anyway. (The GPU renders faster than the CPU sends commands - this can occur with high FPS, and in that case it normally isn't called a 'bottleneck' for gamers, since the FPS is acceptable anyway)

Altri suggerimenti

I know this has been answered already, but I have something to add.

What you mention here

Render scene (previous frame)
Logic, physics, collisions
Swap buffers

may increase the framerate, but it also increases the latency a lot. When the user presses a button, the result will first be seen in the best case more than two frames later, at worst up to 4 frames later.

A better game loop is:

-Handle Input, move character etc. (This takes virtually no time)
-Render
-Do the heavy calculations (AI, physics)
-Swap Buffers

Actually it doesn't make so much difference. There are a lot of ways you can make your game loop, the most important thing is just to keep the 3 main topics separated.

-Input
-Logic
-Render

The time between each one is always gonna be the same, since they are not being executted in parallel.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top