Unrestricted while loop?

Started by
5 comments, last by ankhd 8 years, 5 months ago

I suspect the answer to this is going to be 'no', but I'll ask anyway. Is it possible to have a while loop in HLSL which continues indefinitely? I'm making an iterative path tracer which, for each initial ray, bounces it around the scene until it either hits a light or until its colour becomes close enough to black that I can just ignore it. At the moment, I am having to impose a maximum number of bounces because the shader refuses to compile unless I do. Has anyone done a similar project where they can share a solution?

Thanks a lot!

Advertisement
Which shader model / shader target are you compiling for?

you can use 2 interleaved while loops with [loop] tag

[loop]while(expr)
[loop]while(expr)

{
// your code
}

EDIT one "[loop]while(expr)" is enough for SM4+

It's allowed in Shader Model 4+.

But beware if the loop never ends, or it takes too long (e.g. more than one second) TDR will kick in, affecting your process, or eventually restarting the system via a BSOD (if you make a couple TDRs more in a row).

TDR can be disabled via regedit but is absolutely not recommended (only if you plan to use your own computer for intensive simulations).

On Shader Model 3 it is not allowed.

I accidentally had a shader loop than never ended and as Matias pointed out TDR kicked in. This was with an Nvidia card which recovered itself.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Most GPU's are non-preemptive, meaning that once your shader is running... it will run to completion no matter what, without letting other stuff like, say, rendering your desktop, run in parallel. In other words your screen will freeze and if you have a driver watchdog it will reset the driver after some number of seconds.

To do what you want you should just get each invocation of the shader to do N passes on each pixel, and accumulate the results, where N is a reasonable number (large enough that you're not invoking a shader run for every pass, but not too large that your shader takes too long to run). If you are using a dedicated second graphics card not connected to a monitor then N can be as large as you want.

That way you can see the results of the iterative passes, N passes at a time. You'll know if N is too large if your desktop feels choppy to use.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Could you you the same technique used in GPU particle system. the particle could live for ever or normally you give it a life time value.

This topic is closed to new replies.

Advertisement