SDL_GL_SwapWindow + event polls: Where is it best to put them?

Hello,

I have a game loop that updates & renders every ~16 ms. I am running the game on a 144Hz monitor. I am trying different configurations of SDL_GL_SetSwapInterval, event poll placement and SDL_GL_SwapWindow placement within the loop and I am having a hard time figuring out what is optimal. Here are some combinations and the results in Nvidia’s Performance Overlay:

SDL_GL_SetSwapInterval(1);
t1 = get_time();
while (game) {
    if (get_time - t1 >= 16.667) {
        t1 = get_time();
        while (SDL_PollEvent...) {
                //blah
            }
        }
        RenderLotsofStuff();
        SDL_GL_SwapWindow(gWindow);
    }
}

Overlay reports 60fps and Render Latency fluctuates around 17 ms. GPU: ~40%. Setting SDL_GL_SetSwapInterval to zero doesn’t affect those results.

Now I’m moving SDL_GL_SwapWindow so that it’s executed all the time, not every 16ms:

SDL_GL_SetSwapInterval(1);
t1 = get_time();
while (game) {
    if (get_time - t1 >= 16.667) {
        t1 = get_time();
        while (SDL_PollEvent...) {
                //blah
            }
        }
        RenderStuff();
    }
    SDL_GL_SwapWindow(gWindow);
}

Results now are 144 fps, and Render latency fluctuates between 1.5 - 17 ms. GPU: ~40%

Setting SDL_GL_SetSwapInterval to zero: Fps are ~600 and Render Latency around 2 ms. GPU: ~60%

I have 2 main questions:

  1. I have trouble understanding what SDL_GL_SwapWindow exactly does (I mean, aside from showing up stuff you drew). Does it enforce some delay? I am not sure what the “right” position for it is inside the code. Are the results I posted “inaccurate/confused” and they fail to measure accurately what happens under the hood?

  2. Is it a good practice to poll for events every 1/60th of a second, or is it fine just leaving it run all the time in the while() loop?

Thanks!

Just check for events every frame.

edit: SDL_GL_SwapWindow() swaps the back buffer (where the rendering happens) to the front buffer (what you see on the screen). It should be after you do all your rendering. It does not enforce a delay beyond whatever the operating system, GPU driver, and GPU itself incur. (be aware that profiling tools, like Nvidia’s Performance Overlay, will incur a small delay as well)

Also, you should be actually rendering every frame, not every 16.667 ms. Otherwise you’re calling SDL_GL_SwapWindow() for nothing.