In order to try and track down persistent stuttering in a game I'm working on I created an absolutely minimal game loop for a baseline. However, even doing nothing but swap buffers every frame I am getting frequent dropping of frames. Often the interval between iterations of the loop is in the range of 0.035 to 0.08 seconds (with these spikes occurring at least once a second). Has anyone had similar experiences, particularly on OS X? I'm leaning towards some sort of hardware failure or OS bug at this point, but any advice is welcome.
Code below:
public class SlowUpdates {
long window = 0l;
public void run() {
try {
init();
loop();
glfwDestroyWindow(window);
} finally {
glfwTerminate();
}
}
public void init() {
if (!glfwInit()) {
throw new IllegalStateException("Unable to initialize GLFW");
}
// Configure our window
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); // using a 4.1 profile exhibits the same problems
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
window = glfwCreateWindow(800,600, "Hello World!", 0L, 0L);
// Get the resolution of the primary monitor
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
// Make the OpenGL context current
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Setting VSync off makes no difference
glfwShowWindow(window);
}
public void loop() {
GL.createCapabilities();
glViewport(0,0,800,600);
double lastUpdated = glfwGetTime();
// Run until the user indicates we should stop
while (!glfwWindowShouldClose(window)) {
double curTime = glfwGetTime();
double deltaSeconds = curTime - lastUpdated;
lastUpdated = curTime;
// If we have a gap of more than 2 frames, print out the delta
if (deltaSeconds > (0.016666667 * 2.1)) {
System.out.println("Long update time: " + deltaSeconds);
}
glfwSwapBuffers(window);
glfwPollEvents();
}
}
public static void main(String[] args) {
new SlowUpdates().run();
}
}
Further running information:
Machine: 2.3GHz Late 2013 Macbook Pro (with discrete graphics card)
Resolution: Default
VM Args: -XstartOnFirstThread
Java version : 8
LWJGL version : 3.0.0
Example output from running on my machine for 10 seconds or so:
Long update time: 0.03519966600000002
Long update time: 0.048400801999999965
Long update time: 0.05260543899999992
Long update time: 0.05686535199999998
Long update time: 0.039696041999999654
Long update time: 0.05309769600000003
Long update time: 0.05737294300000029
Long update time: 0.03731823899999975
Long update time: 0.0663081339999998
Long update time: 0.04076894600000003
Long update time: 0.03728830400000005
Long update time: 0.038040972999999756
Long update time: 0.05522503099999998
Long update time: 0.04303661199999986
Long update time: 0.043963965000000105
Long update time: 0.03907344100000021
Long update time: 0.03926434200000006
Long update time: 0.03820931900000035
Long update time: 0.05243613700000038
Long update time: 0.03801172999999958
Long update time: 0.03797626900000006
Long update time: 0.03930413699999935
Long update time: 0.05232179700000028
Long update time: 0.04220759300000054
Long update time: 0.03862296800000031
Long update time: 0.039433597000000375
Long update time: 0.04283244800000041
Long update time: 0.036080586000000636
Long update time: 0.03919839099999933
Long update time: 0.04091377800000018
Long update time: 0.05657044099999986
Long update time: 0.041036066000000204
Long update time: 0.04767241800000033
Long update time: 0.0377868320000001
Long update time: 0.04219191099999975
Long update time: 0.04396938799999983
Long update time: 0.04859977400000037
Long update time: 0.05908144699999962
Long update time: 0.039497417000000645
Long update time: 0.046268094000000204
Long update time: 0.03927137400000014
Long update time: 0.043469968000000136
Long update time: 0.04399093800000031
Long update time: 0.041085579999999844
Long update time: 0.04568488099999968
Long update time: 0.03579920800000025
Long update time: 0.05230304200000013
Long update time: 0.04685661700000132
Long update time: 0.04761215599999957
Long update time: 0.03753488800000149
Long update time: 0.038046283999999986
Long update time: 0.03772946099999963
Long update time: 0.04108765500000011
Long update time: 0.038663435000000135
Long update time: 0.03759004499999996
Long update time: 0.052365797000000214
Long update time: 0.0374093490000007
Long update time: 0.04953469599999849
Long update time: 0.0395372260000002
Long update time: 0.03847309400000043
Long update time: 0.03951624100000117
Long update time: 0.04416338000000053
Long update time: 0.038125530000000296
Long update time: 0.03944585000000167
Long update time: 0.03862907599999943
Long update time: 0.03533686100000111
Long update time: 0.03519534199999974
Long update time: 0.03853694799999907
Long update time: 0.04572978800000094
Long update time: 0.06691946499999979
Long update time: 0.04981436099999925
Long update time: 0.03572906099999962
Long update time: 0.038748087999998404
Long update time: 0.03947808300000055
Long update time: 0.047825573999999094
Long update time: 0.03803626299999863
Long update time: 0.03783003899999926
Long update time: 0.03801885099999858
Long update time: 0.04459856499999937
Long update time: 0.03689624899999977
Long update time: 0.05462112100000027
Long update time: 0.037220873000000765
Long update time: 0.035962877000001114
Long update time: 0.04610506699999917
Long update time: 0.035799774000000895
Long update time: 0.049529357999999135
Long update time: 0.03719905700000048
Long update time: 0.04233656099999905
Long update time: 0.04300985000000068
Long update time: 0.04495792899999884
Long update time: 0.05274710000000127
Long update time: 0.05670581400000074
Long update time: 0.04431241099999994
Long update time: 0.04083954999999939
Long update time: 0.03708499800000098
Long update time: 0.03880964099999851
Long update time: 0.03889574400000129
Long update time: 0.05666360800000092
Long update time: 0.04578334400000017
Long update time: 0.038040420999999824
Long update time: 0.039791002000001185
Long update time: 0.038779772999999906
Long update time: 0.03913228300000071
Long update time: 0.03952030800000017
Long update time: 0.06215457499999921
Long update time: 0.0441046140000001
Long update time: 0.038172996000000126
Long update time: 0.03680606600000047
Long update time: 0.038834962999999334
Long update time: 0.0384102189999993
Long update time: 0.038783572999999905
Edit: spent an hour getting the test program running on the same machine under Windows and the poor performance does not occur. This would appear to be something mac specific.
Edit: wrote an equivalent program in C using glfw directly (version 3.0.2) and the problem does not occur. For sanity checking I built a separate project that runs the code above, but using the JGLFW bindings instead of the LWJGL bindings, and performance is good there as well. Unless I can figure something else out, it looks like there is some sort of odd behavior in the LWJGL layer on a mac, but since all LWJGL's glfwSwapBuffers does is call straight through to GLFW, I'm not sure what could cause that.
Edit: and now the JGLFW version has started behaving poorly as well. Latest working theory is that my computer is haunted.