c++ - 禁止 GLSL 中的递归?

标签 c++ opengl glsl raytracing

我在尝试编写以下递归调用时遇到了这个错误。我看过很多 GLSL 递归光线追踪实现的演示,所以我假设 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; 
}

最佳答案

I assumed that GLSL supported recursion

没有。 GLSL 不支持或者更好的说法是允许递归函数调用。

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)

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

关于c++ - 禁止 GLSL 中的递归?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43601521/

相关文章:

iphone - 在 C++ 中编码/解码 NSCoder 数据

c++ - 无法使用 LDAP SSL 连接服务器?

opengl - glReadPixels 不将数据写入数组

GLSL,传递函数

c++ - 为 x64 平台编译时出现 c2593 错误(运算符标识符不明确)

C++:检查 vector<Class> 是否是 vector<Class> 的子集

OpenGL 图像加载/存储 24 位 RGB 图像

opengl - 如何剪切任意形状的纹理?

opengl - 使用 gl_FragColor 与 out vec4 颜色?

java - libGDX - TextButton 字体的自定义着色器