# Loop performance in a shader

I am wondering what's the best way to integrate a dynamic loop function in a shader?

First, it seems that dynamic arrays are not possible. So, is it better to create a maximum size array and only fill a part of it or define arrays with predefined sizes?

Then, what's the best way to iterate over this array?

Is it better to use an unrolled loop or a dynamic loop for something between 4 to 128 iterations? I've also seen that it's possible to unroll it to a maximum predefined number of iterations then stop it with a condition such as if (i == myCurrentMaximumIterationNumber).

• What are you trying to do with the array and loop? I'm asking because this somehow sounds like an XY Problem to me. Since the best way to use conditions and loops on the GPU is to refrain from using them, maybe there are even better ways instead of using arrays and loops in your case.
– Nero
Mar 8 '16 at 21:38
• I am implementing a screenspace subsurface scattering effect which currently work. But I have some doubts about the way I use the kernel according to performances. I've choose to do a maximum array size and fill only a part and use a dynamic loop with a dynamic number of iteration which is related to the currently used array content. I think that there are things to do or know when programming shaders according to performances for example. And in my opinion loops is a common performance topic which might follow some rules and maybe "good practices" but I didn't found any good answer about it.
– MaT
Mar 9 '16 at 7:38