Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursion in GLSL prohibited?

I ran into this error when trying to write the following recursive call. I have seen a lot of demos of implementations of recursive Ray tracing in GLSL so I assumed that GLSL supported recursion.

Is this not the case?

OpenGL is returning 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; 
}
like image 544
138 Avatar asked Apr 25 '17 04:04

138


People also ask

Why recursion is not recommended?

In imperative programming languages, recursive functions should be avoided in most cases (please, no hate mail about how this isn't true 100% of the time). Recursive functions are less efficient than their iterative counterparts. Additionally, they are subject to the perils of stack overflows.

What are the limitations of recursive function?

Limitations of Recursive Approach:Each function call requires push of return memory address, parameters, returned result,etc. and every function return requires that many pops. 2. Each time you call a function you use up some of your memory allocation may be in stack or heap.

Does OpenCL allow recursion?

OpenCL does not support recursive control flow, which includes mutual recursion. Therefore, to ensure that your code works properly on every platform you may wish to target, you should refrain from using any form of recursion, and instead write your algorithms using an iterative approach.

Does Hlsl support recursion?

None of the available profiles support recursive functions. It seems you have to re-write the algorithm so it doesn't use recursions. If you need help with that, just say so.


1 Answers

I assumed that GLSL supported recursion

No. GLSL doesn't support or better said allow recursive function calls.

GLSL does not. The GLSL memory model does not allow for recursive function calls. This allows GLSL to execute on hardware that simply doesn't allow for recursion. It allows GLSL to function when there is no ability to write arbitrarily to memory, which is true of most shader hardware (though it is becoming less true with time).

So, no recursion in GLSL. Of any kind.

OpenGL Wiki – Core Language (GLSL)

and

Recursion is not allowed, not even statically. Static recursion is present if the static function-call graph of a program contains cycles. This includes all potential function calls through variables declared as subroutine uniform (described below). It is a compile-time or link-time error if a single compilation unit (shader) contains either static recursion or the potential for recursion through subroutine variables.

GLSL 4.5 Specification, Page 115

like image 147
vallentin Avatar answered Sep 18 '22 01:09

vallentin