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

<p>Furthermore, even if it did include <code>glFinish</code>/<code>glClientWaitSync</code>/etc, there is no guarantee that the CPU time in any way relates to how much time the GPU spent processing it. <code>glFinish</code> 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 <code>QueryPerformanceCounter</code> call will include that time.</p>

<p>If you want to measure GPU time, you should use tools that actually <em>measure GPU time</em>. For example, <a href="https://www.khronos.org/opengl/wiki/Query_Object#Timer_queries">timer queries</a>. You bracket some commands with <code>glBeginQuery(GL_TIME_ELAPSED, queryObject)</code> and <code>glEndQuery(GL_TIME_ELAPSED)</code>. Then you can <a href="https://www.khronos.org/opengl/wiki/Query_Object#Query_retrieval">read the result after some time has elapsed</a>. Like maybe next frame.</p>

<p>And unlike <code>glFinish</code>, this will not absolutely murder your performance ;)</p>

CC BY-SA 3.0
2017-01-03T15:13:51.317Z
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.
mdkdy
CC BY-SA 3.0
2017-01-03T15:23:51.260Z
@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](https://www.khronos.org/opengl/wiki/Synchronization).
Nicol Bolas
CC BY-SA 3.0
2017-01-03T15:42:09.103Z
_OpenGL Rendering Commands are assumed to be asynchronous._ I feel like noob now.
mdkdy
CC BY-SA 3.0
2017-01-03T15:04:56.997Z
2017-01-03T15:04:56.997Z
Nicol Bolas
<p>I know I can use <code>QueryPerformanceCounter</code> for measure time for whole single frame. But can I when it comes to OpenGL draw or compute call?</p>

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

<p><a href="https://i.sstatic.net/2nb3d.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/2nb3d.png" alt="enter image description here"></a></p>

<p>As seen in code:</p>

<pre><code>glBindVertexArray(vertexArrayObject);
timer-&gt;startCounter();      
glDrawElements(GL_TRIANGLE_STRIP, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
estimatedRenderTime = timer-&gt;getCounter();
</code></pre>

<p>or</p>

<pre><code>timer-&gt;startCounter();
glDispatchCompute(width, height, 1);
estimatedRenderTime = timer-&gt;getCounter();
</code></pre>

<opengl><gpu><performance>
Can I use QueryPerformanceCounter to measure single draw or compute call performance?
mdkdy
# Can I use QueryPerformanceCounter to measure single draw or compute call performance?

(Asked by: username_0 on 2017-01-03)

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](https://i.sstatic.net/2nb3d.png)](https://i.sstatic.net/2nb3d.png)

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();

```

**Tags:** opengl, gpu, performance


---

## 1 answers

### Answer by username_1 on 2017-01-03. Score: 6 | Accepted answer

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](https://www.khronos.org/opengl/wiki/Query_Object#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](https://www.khronos.org/opengl/wiki/Query_Object#Query_retrieval). Like maybe next frame.

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

> **Comments:**
> **username_0**: 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.  
> **username_1**: @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](https://www.khronos.org/opengl/wiki/Synchronization).  
> **username_0**: _OpenGL Rendering Commands are assumed to be asynchronous._ I feel like noob now.  

---

