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

<p>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?</p>

<p>Then, what's the best way to iterate over this array?</p>

<p>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 <code>if (i == myCurrentMaximumIterationNumber)</code>.</p>

CC BY-SA 3.0
2016-03-08T21:38:13.670Z
What are you trying to do with the array and loop? I'm asking because this somehow sounds like an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-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
CC BY-SA 3.0
2016-03-09T07:38:31.413Z
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
<opengl><rendering><shader><glsl><performance>
Loop performance in a shader
MaT
# Loop performance in a shader

(Asked by: username_0 on 2016-03-08)

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)`.

**Tags:** opengl, rendering, shader, glsl, performance

> **Comments:**
> **username_1**: What are you trying to do with the array and loop? I'm asking because this somehow sounds like an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-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.  
> **username_0**: 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.  

---

## 0 answers

