r/opengl•
robololi

How to prevent hitting TDR timeout with long frame times?

I've got a very intensive frag shader that can take minutes to finish.

With a single draw call, it hits the TDR and crashes the app. If I split it into lots of smaller draw calls, opengl takes a ton of them up to a point, then freezes, and hits TDR.

If I do a glflush() after each small draw call, everything will finish, but its slow. If i do glflush() after, say, 16 small draw calls, it is significantly faster, but it can crash if it's too high, and depends on lots of other factors. Is there a better way to get the most out of the GPU without crashing, and is there anything I can do about windows becoming non-responsive while it's rendering?

Posted Jul 4, 2015, 5:13 PM UTC

713
Madsy9•

Figuring out exactly how much GPU work you could do (for an arbitrary OpenGL program) without hitting the TDR time limit is basically the same as solving the famous Halting Problem.

Now, I'm not 100% sure about all the possible solutions, but here are some suggestions:

  • You can change the default TDR timeout value in the Windows registry. This link has a list of all the relevant TDR registry values.
  • You could use WGL_ARB_create_context_robustness and GL_ARB_robustness to contain the consequences of a timeout. I think if you use those, then a timeout will only affect your application? But I'm not sure. If so, a timeout would be detectable by having your OpenGL context lost, and a more detailed status can be queried with glGetGraphicsResetStatusARB(). This would give you a way to detect when the work load is too large, and split up the work appropriately until it's manageable and context resets stop occurring.
  • Perhaps a better alternative than the previous one, time a small chunk of the overall workload, then compare it against the TDR timeout value in the registry. You then know roughly how much you can scale up the work without hitting the TDR.
  • If the time spent even for a single fragment take a lot of time, consider splitting up your complex shader into multiple passes. That's a viable option if you are still fillrate bound after splitting up the rendering into multiple batches of quads.

As Swapbuffers/wglSwapBuffes() do an implicit flush, the behavior you see makes sense. Alternatively you could try a glFinish() instead for more stability, but at greater performance cost. With an explicit glFinish() between N batches, you would block until all the batches were completed as opposed to just flushing the command queue. For even more stability, you could Sleep() for a few miliseconds after a flush or finish to relinquish your time slice to other processes to allow them to use the GPU.

Let us know which method works for you :)

6
robololiOP•

Thanks for the reply. I have tried to sleep before/after the flush and it wasn't working, but turns out I just wasn't dividing the work up into small enough chunks and had too much latency between draw calls.

I'll look into ARB_robustness, but that solution seems a bit ridiculous. Repeatedly flood the GPU with work while it fails over and over and dynamically cut back until it stops complaining? Surely there must be something more elegant... I can't imagine 3DS Max doing that. What happens when a single pixel has > TDR latency, split the shader into multiple passes?

2
Madsy9•

As I said, you could do the opposite. Start with a small work batch, time the GPU time then scale up the work load as far as the quried TDR timeout allows.

If 3D Studio Max literally never triggers the TDR, then they have either split up effects into multiple super-simple shaders, or have disabled the TDR completely.

The only solution if you hit the TDR for a single fragment is to split the offending fragment shader up into multiple simpler passes.

3
robololiOP•

Well I feel dumb, scaling up instead of down should have been obvious. That will work. Thanks.

2
Madsy9•

Remember to use OpenGL timer query objects when querying spent GPU time. Querying time with timeGetTime(), GetTickCount(), time() or QueryPerformanceCounter() will not work, as most OpenGL functions are asynchronous and do not block.

5
Aransentin•

What are you doing in your shader that makes it take so long to finish? Just curious.

3
robololiOP•

It's a toy program that's doing path tracing to bake lighting.

5
fnordstar•

Have you considered using Computer Shaders or CUDA / OpenCL? Should be more flexible and not give you any timeout issues.

3
robololiOP•

I'm relatively new to opengl and getting comfortable with it is my goal at the moment. I don't know anything about compute shaders. Is there a learning resource you could recommend?

2
GYN-k4H-Q3z-75B•

The OpenGL Superbible has a small chapter on it (Compute Shaders). They're like a separate shader pipeline and follow the OpenGL model (unlike OpenCL which is very different).

2
nou_spiro•

Nah OpenCL can cause timeout too. There is no difference in this regard. But OpenCL is indeed more suited to this kind of tasks.

1
genpfault•

Are you running this shader on the main framebuffer on the main thread? Or to a FBO on a secondary thread with a separate context?

1