Skip to content

Instantly share code, notes, and snippets.

@aktau
Created December 27, 2013 17:44
Show Gist options
  • Select an option

  • Save aktau/8150230 to your computer and use it in GitHub Desktop.

Select an option

Save aktau/8150230 to your computer and use it in GitHub Desktop.
OpenGL timer query problem
glBeginQuery(GL_TIME_ELAPSED, gQueries[gQueryBackBuffer][TIMER_RENDER]);
GL_ERROR("begin RENDER timer");
{
world.timer = ms;
nocull.timer = ms;
/* start a batch and render */
gfxBatch(&world);
gfxRender(&sheet, &nocull, &waveShader);
gfxRender(&axis, &world, &colorShader);
gfxRender(&crystal, &world, &shader);
gfxRender(&cube, &world, &colorShader);
}
{
gui.timer = ms;
gfxBatch(&gui);
gfxRender(&quad, &gui, &guiShader);
}
glEndQuery(GL_TIME_ELAPSED);
GL_ERROR("end RENDER timer");
glQueryCounter(gSwapQuery, GL_TIMESTAMP);
GL_ERROR("set timer");
GLint64 timer1 = 5;
glGetInteger64v(GL_TIMESTAMP, &timer1);
uint32_t beforeSwap = SDL_GetTicks();
SDL_GL_SwapWindow(window);
uint32_t elapsedSwap = SDL_GetTicks() - beforeSwap;
GLuint done;
glGetQueryObjectiv(gQueries[gQueryFrontBuffer][TIMER_RENDER], GL_QUERY_RESULT_AVAILABLE, &done);
if (!done) trace("render timer not done\n");
done = false;
while (!done) {
glGetQueryObjectiv(gSwapQuery, GL_QUERY_RESULT_AVAILABLE, &done);
GL_ERROR("check");
}
GLuint64 timer2;
glGetQueryObjectui64v(gSwapQuery, GL_QUERY_RESULT, &timer2);
GL_ERROR("fetch result");
/* collect the timers of the frontbuffer before swapping the query set */
GLuint64 timerRend, timerSwap;
glGetQueryObjectui64v(gQueries[gQueryFrontBuffer][TIMER_RENDER], GL_QUERY_RESULT, &timerRend);
// glGetQueryObjectui64v(gQueries[gQueryFrontBuffer][TIMER_SWAP], GL_QUERY_RESULT, &timerSwap);
printf("render objects: %f ms, CPU stall: %f (%u) (%"PRIu64" - %"PRId64" = %f)\n", timerRend / 1000000.0, timerSwap / 1000000.0, elapsedSwap, timer2, timer1, (timer2 - timer1) / 1000000.0);
/* swap the query set */
swapQueryBuffers();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment