https://www.khronos.org/opengl/wiki/Query_Object#Timer_queries
To query the time needed (in nanoseconds) for OpenGL to execute commands, we use GL_TIME_ELAPSED:
In Main.cpp
void Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* start query */
glBeginQuery(GL_TIME_ELAPSED, query);
glUseProgram(program);
glBindVertexArray(vertexarray);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glUseProgram(0);
/* stop query */
glEndQuery(GL_TIME_ELAPSED);
/* wait until OpenGL has the result */
GLint result = GL_FALSE;
while (result == GL_FALSE)
glGetQueryObjectiv(query, GL_QUERY_RESULT_AVAILABLE, &result);
/* get the result */
glGetQueryObjectiv(query, GL_QUERY_RESULT, &result);
cout << "time to draw 1 triangle (in nanoseconds): " << result << endl;
CheckForGLError();
}
To query the current GPU time, we use GL_TIMESTAMP, here we dont need to begin / end the query, we just ask for it at a certain location in the code using glQueryCounter(...). The result here will be the GPU time, a GLint64 integer, the starting point of this time in implementation-dependent:
void Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* issue query */
glQueryCounter(query, GL_TIMESTAMP);
/* wait until OpenGL has the result */
GLint result_available = GL_FALSE;
while (result_available == GL_FALSE)
glGetQueryObjectiv(query, GL_QUERY_RESULT_AVAILABLE, &result_available);
/* get the result */
GLint64 result = 0;
glGetQueryObjecti64v(query, GL_QUERY_RESULT, &result);
cout << "current GPU time (in nanoseconds): " << result << endl;
glUseProgram(program);
glBindVertexArray(vertexarray);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glUseProgram(0);
CheckForGLError();
}