Unity5 shaders and loop unrolling

Hi

So Im doing volume rendering in Unity5 and I have a simple ray casting shader, Im able to put the number of loops to ~400 but it takes 10-20 minutes to compile as the shader unrolls the loop. It also times out when trying to increase the max loop count even when specifying [unroll(500)] before the for loop.

Having searched around I havent found a way to prevent this and wondered if there is a way to prevent the shader unrolling.

Theres not much to show in the shader, Just something like below a small for loop in the fragment shader that gets a value from a 3D texture which is then used to colour the pixel

for(int i = 0 ; i < 50; i++)
{
    float index = tex3D(_Volume,tempPos).a
    
    //do something with this to get the pixel colour at the current location
    
 }

Any help or insight would be greatly appreciated thanks.

I experienced random compiler issues with a for loop directly in the frag.
Try to put it in a separate function and call it in the fragment shader, this way your code compiles in less than 2 seconds with 10000+ samples on my machine.

Edit: I just noticed [loop] before a for loop in a shader made by a colleague, he told me it enables dynamic branching instead of unrolling the loop, might worth a try

Hi helarts
Thanks for the reply!, Im affraid that doesnt seem to work,

Looking over the code snippet I stupidly left out the important part, It forces the unroll because apperently loops cant have gradient operations,

So in my for loop, Im trying to use i , each loop which it cannot handle and forces me to unroll

 for(int i = 0 ; i < 50; i++)
 {
     worldpos = start + (i * rayDirection);

     float index = tex3D(_Volume,tempPos).a
     
     //do something with this to get the pixel colour at the current location
     
  }