I have written a basic render loop following principles from the “Fix Your Timestep!” article. In a basic form: it polls for input, updates all object positions as many times as required, issues GL commands then swaps the buffers. Pretty standard, nothing too special.
When rendering a simple square with 6 vertices (with glfwSwapInterval
enabled), it maintains 60 frames per second, then quickly dips down locking at 30 for a few seconds, before finally going back to 60. This happens randomly without warning or cause.
TL;DR: I initially thought that I had a bug in my rendering code, but after ensuring all commands were synced and flushed, I found that glfwSwapBuffers
was taking 16.51 ms (~60 FPS) then suddenly decided to take 33.02 ms (~30 FPS) causing the sudden drop in frame rate.
This can’t be a coincidence as those sleeps correspond to perfectly match the 60 FPS and 30 FPS mark?
It works fine on Linux and maintains a constant 60 FPS where glfwSwapInterval
always takes a similar amount of sleep time. On Windows, it is very hard to reproduce as it happens in such a sporadic and unpredictable way randomly locking to 30 FPS for a few seconds.
I have tried on friends computers, updated my drivers and completely rewritten my rendering code - but swapping the buffers always seems to bottleneck it at random.
Is this the driver limiting the FPS from 60 to 30 randomly for a few seconds (and why)? Is this an issue with Windows sleep functions/internals of GLFW? It seems vaguely similar to https://github.com/glfw/glfw/issues/603.
Here is what the profiling log, looks like (notice that the time it takes for swap doubles randomly, then returns to normal):
[2017-12-13 01:42:12.645] [engine] [info] FPS: 60
[2017-12-13 01:42:12.646] [engine] [info] Render: 1.0003e+006 ns
[2017-12-13 01:42:12.646] [engine] [info] Swap: 1.50109e+007 ns
[2017-12-13 01:42:14.647] [engine] [info] FPS: 60
[2017-12-13 01:42:14.648] [engine] [info] Render: 1.0006e+006 ns
[2017-12-13 01:42:14.648] [engine] [info] Swap: 1.50102e+007 ns
[2017-12-13 01:42:15.665] [engine] [info] FPS: 42
[2017-12-13 01:42:15.665] [engine] [info] Render: 1.0011e+006 ns
[2017-12-13 01:42:15.666] [engine] [info] Swap: 3.30234e+007 ns
[2017-12-13 01:42:16.665] [engine] [info] FPS: 53
[2017-12-13 01:42:16.666] [engine] [info] Render: 2.0011e+006 ns
[2017-12-13 01:42:16.666] [engine] [info] Swap: 1.50108e+007 ns
[2017-12-13 01:42:17.666] [engine] [info] FPS: 60
[2017-12-13 01:42:17.667] [engine] [info] Render: 1.0002e+006 ns
[2017-12-13 01:42:17.667] [engine] [info] Swap: 1.50106e+007 ns
[2017-12-13 01:42:18.667] [engine] [info] FPS: 60
[2017-12-13 01:42:18.668] [engine] [info] Render: 2.0009e+006 ns
[2017-12-13 01:42:18.668] [engine] [info] Swap: 1.40099e+007 ns
I’m extremely confused on how to fix this, so any help would be appreciated!