Daily Archives: December 4, 2014

Journal Entry 63 – Profiling Reflections

This will be a quick one, because I’m knee deep in refactoring some of the reflection code. I use occlusion culling on reflective surfaces to ensure that I only render the reflection when necessary. Unfortunately, occlusion querying requires the GPU to finish rendering the surface that is being tested against. This is basically equivalent to calling glFlush, which can stall the pipeline. On my previous GPU this didn’t appear to be a huge deal, but on my new GPU this is a big problem! Check out this screenshot from GPU PerfStudio:

I've never seen my CPU so busy and my GPU so under utilized.  What's going on?
I’ve never seen my CPU so busy and my GPU so under utilized. What’s going on?

What is my CPU so busy with? Lets dig into the OpenGL API calls using the same profiler:

Wow, my CPU is sitting there polling for an occlusion query.  This can't be good.
Wow, my CPU is sitting there polling for an occlusion query. This can’t be good.

Ok, I think I’ve found the problem. Lets track down the offending C# code.

while (queryState[0] == 0)
{
   Gl.GetQueryObjectiv(queryObjects[y], GetQueryObjectParam.QueryResultAvailable, queryState);
}

Yaaaaa, that’s a problem. We can use the query results from the previous frame, incurring a single frame latency before the reflection will be rendered, but will anyone notice? It just takes a few lines to re-order the commands and draw the water based on the previous frame occlusion query. Here are the results!

By waiting until the next frame to perform the query, we can guarantee the query is ready and remove the bottle neck.  This is all at the expense of one frame delay before updating the reflection.
By waiting until the next frame to perform the query, we can guarantee the query is ready and remove the bottle neck. This is all at the expense of one frame delay before updating the reflection.

Looks like we’re back in business! Profiling is a very nice way to see what is impacting the performance of the application. Until next time,

Giawa