Impact of variable-length loops on GPU shaders
- by Will
Its popular to render procedural content inside the GPU e.g. in the demoscene (drawing a single quad to fill the screen and letting the GPU compute the pixels).
Ray marching is popular:
This means the GPU is executing some unknown number of loop iterations per pixel (although you can have an upper bound like maxIterations).
How does having a variable-length loop affect shader performance?
Imagine the simple ray-marching psuedocode:
t = 0.f;
while(t < maxDist) {
p = rayStart + rayDir * t;
d = DistanceFunc(p);
t += d;
if(d < epsilon) {
... emit p
return;
}
}
How are the various mainstream GPU families (Nvidia, ATI, PowerVR, Mali, Intel, etc) affected? Vertex shaders, but particularly fragment shaders?
How can it be optimised?