Recursion prohibited in GLSL?

Hello I'm pretty new to GLSL and ran into this error while trying to write the following recursive call. I have seen many demonstrations of the implementations of recursive ray tracing in GLSL, so I assumed GLSL supports recursion.

This is not true?

OpenGL returns a compile-time error message:

Error: Function trace(vec3, vec3, vec3, int) has static recursion

      

This is my function definition

vec3 trace(vec3 origin, vec3 direction, vec3 illum, int order) 
{       
   float dist;  
   int s_index = getSphereIntersect(origin, direction, dist);   
   //if light hit
   float light_dist = 200;
   for(int k = 0; k < L_COUNT;k++)      
       if(s_intersects(l_center[k], l_radius[k], 
             origin, direction, 
             light_dist)) 
             if(light_dist < dist )             
                 return l_color[k]; //light is pure color  

   if (s_index != -1)
   {
       illum = s_color[s_index];
       for(int j = 0; j < L_COUNT; j++)
       {
           float ambient = 0.68;
           float diffuse = 0.5;
           vec3 poi = view + (direction * dist); 
           vec3 li_disp = normalize( poi - l_center[j]); 
           vec3 poi_norm = s_normal(s_center[s_index], s_radius[s_index], poi); 
            float shade=  dot(li_disp, normalize(poi_norm)); 
            if(shade < 0) shade = 0;
            illum = illum*l_color[j]*ambient + diffuse * shade; 
            //test shadow ray onto objects, if shadow then 0    
            if(order > 0)
                  illum = trace(poi+.0001*poi_norm, poi_norm, illum, order-1); 
        }   
    }   
    else
        illum = vec3(0,0,0);
    return illum; 
}

      

+3


source to share


1 answer


I assumed GLSL supports recursion

Not. GLSL does not support or say it allows recursive function calls.

GLSL does not. The GLSL memory model does not allow recursive function calls. This allows GLSL to run on hardware that simply doesn't allow recursion. This allows GLSL to function when there is no way to randomly write to memory, which is true for most shader hardware (although it becomes less true over time.

So there is no recursion in GLSL. Any kind.

- OpenGL Wiki - Basic Language (GLSL)



and

Recursion is not allowed, not even statically. Static recursion is present if the program contains loops in the static graph of function calls. This includes all potential function calls via variables declared as subroutines (described below). It is a compile time or connection time error if one compilation unit (shader) contains either static recursion or the potential to recurse through subroutine variables.

- GLSL 4.5 Specification, Page 115

+5


source







All Articles