禁止GLSL中的递归?

时间:2017-04-25 04:20:18

标签: c++ opengl glsl raytracing

尝试编写以下递归调用时遇到此错误。我在GLSL中看到过很多关于递归Ray跟踪实现的演示,所以我假设GLSL支持递归。

情况不是这样吗?

OpenGL正在返回编译时错误消息:

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

这是我的功能定义:

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; 
}

1 个答案:

答案 0 :(得分:10)

  

我认为GLSL支持递归

没有。 GLSL不支持或更好地说允许递归函数调用。

  

GLSL没有。 GLSL内存模型不允许递归函数调用。这允许GLSL在不允许递归的硬件上执行。当没有能力随意写入内存时,它允许GLSL运行,这对于大多数着色器硬件来说都是如此(尽管它随着时间变得越来越不真实。)

     

因此,GLSL中没有递归。任何一种。

     

- OpenGL Wiki – Core Language (GLSL)

  

不允许递归,甚至不是静态递归。如果是静态函数调用图,则存在静态递归   程序包含循环。这包括通过声明为变量的所有潜在函数调用   子程序均匀(如下所述)。如果是单个编译单元,则为编译时或链接时错误   (着色器)包含静态递归或通过子例程变量递归的可能性。

     

- GLSL 4.5 Specification, Page 115