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
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:
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 :)
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?
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.
Well I feel dumb, scaling up instead of down should have been obvious. That will work. Thanks.
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.