3
$\begingroup$

I know I can use QueryPerformanceCounter for measure time for whole single frame. But can I when it comes to OpenGL draw or compute call?

I always assumed that, as on the diagram, CPU waits for GPU to finish so timer would measure approximate time or am I wrong?

enter image description here

As seen in code:

glBindVertexArray(vertexArrayObject);
timer->startCounter();      
glDrawElements(GL_TRIANGLE_STRIP, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
estimatedRenderTime = timer->getCounter();

or

timer->startCounter();
glDispatchCompute(width, height, 1);
estimatedRenderTime = timer->getCounter();
$\endgroup$

1 Answer 1

6
$\begingroup$

That diagram includes a CPU block titled "wait for GPU". I do not see the part of your code that includes an equivalent command.

Furthermore, even if it did include glFinish/glClientWaitSync/etc, there is no guarantee that the CPU time in any way relates to how much time the GPU spent processing it. glFinish might block on a mutex, thus ending the thread's timeslice. Which means that the thread will start up sometime after that mutex is released. How long after? Who knows, but your QueryPerformanceCounter call will include that time.

If you want to measure GPU time, you should use tools that actually measure GPU time. For example, timer queries. You bracket some commands with glBeginQuery(GL_TIME_ELAPSED, queryObject) and glEndQuery(GL_TIME_ELAPSED). Then you can read the result after some time has elapsed. Like maybe next frame.

And unlike glFinish, this will not absolutely murder your performance ;)

$\endgroup$
3
  • $\begingroup$ Yeah I expected such answer. Wait for GPU - I mean that draw call as any other function returns after its finishes work, in this case let's say - ordered work. Yeah there is some confusion here. $\endgroup$
    – mdkdy
    Commented Jan 3, 2017 at 15:13
  • 1
    $\begingroup$ @narthex: "returns after its finishes work" That all depends on how you define "its work". The "work" of a draw call is to drop a token into the GPU's command stream. And sometimes not even that much. In all likelihood, by the time that function returns, nothing has even started to render, let alone finished. $\endgroup$ Commented Jan 3, 2017 at 15:23
  • $\begingroup$ OpenGL Rendering Commands are assumed to be asynchronous. I feel like noob now. $\endgroup$
    – mdkdy
    Commented Jan 3, 2017 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.