Newbie Questions: Trying to understand glfwSwapInterval

Hello everybody, new to the forums! I’m having trouble understanding the nature of glfwSwapInterval :confused:

Question #1

If I don’t call this function at any point of my program, I get a lot of stuttering in my framerate. On the other hand if I call the function and set it to 1, there’s pretty much no stuttering. Maybe a couple of framerate dips here and there, but nothing very noticeable.

If I set the interval to 0, I get the smoothest framerate out of all situations! But, my CPU usage rises, which makes my processor fans spin more. Keep in mind I’m calling glfwSwapInterval once before the game loop in all situations. I read I shouldn’t be setting the interval every frame.

What are the best practices I should take?

Question #2
What are the differences between setting the interval to 1, 0, or just not calling the function?

Question #3
I understand that setting the interval to 1 makes glfw wait for the next screen refresh so that glfwSwapBuffers returns when that happens. Which is also why my profiler timed the SwapBuffers function at 15.x ms, but when the interval is set to 0 …swap buffers takes only 0.2ms.

What if I were rendering a complicated scene with the interval set to 1, does SwapBuffers take those 15.x ms away from my game, or it only uses a the remaining time available? I also thought it could be due to the fact that my scene is very simple and doesn’t get close to 15ms. I’m confused.

I hope I made sense, thank you very much if you read this far. By the way I’ll detail what my scene consist of below.

My scene is simple:

  • one quad.
  • I bind a texture and shader only once before the update loop.
  • I only call the following glfw functions in my update loop: glfwGetTime, glfwSwapBuffers, and glfwPollEvents.

Thanks for reading :slight_smile:

The documentation on glfwSwapInterval and buffer swapping should hopefully answers most of your questions.

Some brief answers to your specific questions, but please read the above in detail and also search and read about vertical synchronization or vsync.

Question #1 What are the best practices I should take?
The normal best practice is to let your end user choose, and default to 1.

Question #2 What are the differences between setting the interval to 1, 0, or just not calling the function?

  • Not calling: driver default
  • 0: do not wait for vsync (may be overridden by driver/driver settings)
  • 1: wait for 1st vsync (may be overridden by driver/driver settings)

Question #3 What happens during glfwSwapBuffers when interval is set to 1?
If you set the swap interval to 1, and the driver does not override this, then calling glfwSwapBuffers will call the opengl swap buffer implementation which will then wait until the buffer has swapped. You won’t be able to do anything with this thread until then, but you can use multithreading to do other work.


Hey Doug, I appreciate your answer it helped a lot. I took a look at the documentation, the buffer swapping link was very useful.

Thanks again!