English 中文(简体)
GLSL被禁止的再保险?
原标题:Recursion in GLSL prohibited?

我在试图写下以下休妻的呼吁时就犯了这一错误。 我看到了在高纬度地区开展反射雷追踪工作的许多废气,因此我假定,高纬度线部队支持再次入侵。

Is this not the case?

开放式自由工正在退回一个汇编的错误信息:

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; 
}
最佳回答

我假定,自由党支持再次入侵

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).

因此,在最不发达等国家没有再入侵。 其中任何一种。

- <

以及

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.

- <

问题回答

虽然自由工联并不直接支持再入侵,但如果再入侵的深度有限,则可以采用宏观方法加以实施。 这是一项可回收的<代码>fibonacci功能,最高可再深入5:

#define fib(name,name0) 
int name(int n){ 
    if (n <= 1) 
        return n; 
    return name0(n - 1) + name0(n - 2); 
}

//set this variable to "true" if the maximum depth of recursion is exceeded
bool overflow = false;
int fib0(int n)
{
    overflow=true;
    return -1;
}

//the maximum recursion depth is 5
fib(fib1,fib0)
fib(fib2,fib1)
fib(fib3,fib2)
fib(fib4,fib3)
fib(fib5,fib4)
fib(fibonacci,fib5)

宏观职能扩大到这些职能:

bool overflow = false;
int fib0(int n)
{
    overflow=true;
    return -1;
}

int fib1(int n){ if (n <= 1) return n; return fib0(n - 1) + fib0(n - 2); }
int fib2(int n){ if (n <= 1) return n; return fib1(n - 1) + fib1(n - 2); }
int fib3(int n){ if (n <= 1) return n; return fib2(n - 1) + fib2(n - 2); }
int fib4(int n){ if (n <= 1) return n; return fib3(n - 1) + fib3(n - 2); }
int fib5(int n){ if (n <= 1) return n; return fib4(n - 1) + fib4(n - 2); }
int fibonacci(int n){ if (n <= 1) return n; return fib5(n - 1) + fib5(n - 2); }

或者,可以像这样实施:

#define recursive(s1) struct s1{int s_;};
recursive(s0)

bool overflow = false;
int fib(int n, s0 s_){
    overflow=true;
    return -1;
}


#define fib_(name,name1) 
recursive(name1) 
int fib(int n,name1 s_){ 
    if (n <= 1) 
        return n; 
    return fib(n - 1,name(0)) + fib(n - 2,name(0)); 
} 

fib_(s0,s1)
fib_(s1,s2)
fib_(s2,s3)
fib_(s3,s4)
fib_(s3,s5)

int fibonacci(int n){
    return fib(n,s5(0));
}

你们也可以用宏观方法确定“再保险”数据结构,就像这一双 tree树一样:

#define tree(name,name0) 
struct name 
{ 
 int data; 
 name0 left; 
 name0 right; 
}; 
name Tree(int data, name0 left, name0 right){ 
    return name(data,left,right); 
}

//base case
struct tree0
{
 int data;
};
tree0 Tree(int data){
    return tree0(data);
}

tree(tree1,tree0)
tree(tree2,tree1)
tree(tree3,tree2)

int demo(){
    tree2 tree = Tree(1,Tree(1,Tree(1),Tree(2)),Tree(1,Tree(1),Tree(2)));
    return tree.left.left.data;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签