glfwSwapBuffers slow every 4th cycle

Started by Aisaaax, February 04, 2021, 09:33:56

Previous topic - Next topic

Aisaaax

Hello!
I've recently made another project on OpenGL-ES for ARM with LWJGL. And I am using GLFW now (I was previously using EGL rendering with no issue)

But I noticed noticeable stuttering in my program. I investigated it and found that every 4th cycle of my main loop - glfwSwapBuffers(window) takes A LOT longer than in other cycles. We are talking of the difference between 0.0003 seconds and 0.28 seconds.

I have no idea what caused this. As you can see from the code (below), I am explicitly testing the time it takes swapBuffers to work. I can guarantee that the rest of my program (which is extremely small BTW) doesn't really take that much time at all, and nothing should cause any issues. I think.
I'm using LWJGUI, which is also a first time for me, but I tested its performance and it seems like it's doing completely OK.

Vsync is off, but I think double-buffering is on.

Here's my loop:
while (running)
        {
            context.updateGlfwWindow();
            Vector2i windowSize = context.getFramebufferSize();
            glClearColor(1, 1, 1, 1);
            glViewport(0, 0, windowSize.x, windowSize.y);
            glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

            renderer.render(frame, context);

            glfwPollEvents();

            double timeStart  = System.nanoTime() / 1000_000_000.0;

            glfwSwapBuffers(window);

            double timeEnd = System.nanoTime() / 1000_000_000.0;
            double delta = timeEnd - timeStart;
            System.err.printf("%f\n", delta);
            
            initializer.getSystemEventProcessor().processEvents(frame, context);

            initializer.getGuiEventProcessor().processEvents();
        }


Here's the sample output (timings). notice that the dip happens exactly every 4th time.
0.000372
0.000378
0.000370
0.274361
0.000344
0.000367
0.000395
0.275922
0.000393
0.000365
0.000370
0.274104
0.0003989
0.000477
0.000393
0.273459

Aisaaax

update:
I tried limiting the loop, by adding some basic FPS control there. Basically I check the time at the end of the loop, and if it's not yet the time to have another frame - I simply wait. My idea was that perhaps the videodriver gets a bit overwhelmed by all the calls and chugs periodically.

Unfortunately, this didn't solve the issue. But now the stutter happens every 8 cycles instead of 4.
So the slower the loop runs - the less frequent those stutters are. But they are still simply inexcusable - 0.3 seconds is as you understand - way too much. It's like 3FPS which is 10 times worse than what I'm aiming at.

Also I want to stress that rendering seems completely fine otherwise.

Aisaaax

Narrowed the problem down to LWJGUI/ImageView. For some reason, I can render lots of simple elements, like buttons, but if I render images it just hickups every X'th frame.
I have a suspicion that perhaps something like NanoVG or something else causes images (textures) to re-load periodically. But it is interesting that glSwapBuffers gets affected.