GLSL - 数组索引越界的

问题描述:

就干脆把它,我有以下问题:GLSL - 数组索引越界的

我想实现在GLSL堆栈,但它不断给我下面的错误:

warning C1068: array index out of bounds. 
error C1068: array index out of bounds. 

我的代码如下:

//--------------------------- 
// Utility 
//--------------------------- 
uint PackColor(vec3 color) 
{ 
    uint value = 0 << 24;     // Alpha 
    value = value + uint(color.r) << 16; // Red 
    value = value + uint(color.g) << 8;  // Green 
    value = value + uint(color.b);   // Blue 
    return value; 
} 


// 
// Stack.glsl ~ Contains a Stack and StackEntry structure for holding multiple rays to be   evaluated. 
// Date: 11-09-2014 
// Authors: Christian Veenman, Tom van Dijkhuizen 
// Type: Library 
// 

#define MAX_STACK_SIZE 64 

struct StackEntry 
{ 
    uint depth; 
    float fraction; 
    Ray ray; 
}; 

struct Stack 
{ 
    int current; 
    StackEntry entries[MAX_STACK_SIZE]; 
}; 

// Pushes an element on the stack. 
void Push(Stack stack, StackEntry entry) 
{ 
    stack.entries[stack.current] = entry; 
    stack.current = stack.current + 1; 
} 

// Pops an element from the stack. 
StackEntry Pop(Stack stack) 
{ 
    stack.current = stack.current - 1; 
    return stack.entries[stack.current]; 
} 

// Checks if the stack is empty 
bool isEmpty(Stack stack) 
{ 
    if(stack.current == 0) 
     return true; 
    else 
     return false; 
} 

Screen screen; 
Stack stack; 
void main() 
{ 
    // Init stack 
    stack.current = 0; 
    for(int i = 0; i < stack.entries.length; i++) 
     stack.entries[i] = StackEntry(0, 0, Ray(vec3(0,0,0),vec3(0,0,0))); 

    // Init screen 
    screen.width = 1280; 
    screen.height = 1024; 

    // Screen coordinates 
    uint x = gl_GlobalInvocationID.x; 
    uint y = gl_GlobalInvocationID.y; 

    Push(stack, StackEntry(0, 1.0f, Ray(vec3(1,0,0),vec3(1,0,0)))); 
    StackEntry entry = Pop(stack); 
    entry.ray.direction *= 255; 

    uint RGBA = PackColor(entry.ray.direction); 
    pixels[(screen.height - y - 1)*screen.width + x] = RGBA; 
} 

我真的是如何发生这个错误一无所知。为什么有些警告和一些错误?

我希望你能帮助我,或者向我提供一个关于如何在GLSL中创建堆栈的解决方案或方向。

编辑:这不是完整的代码,因为我不包括雷部分,但这应该是唯一相关的代码。

+0

您粘贴的代码到目前为止有一些严重的isseus。你甚至没有一个堆栈可以使用。 – derhass 2014-09-27 16:43:18

+0

当然,还有一个全局堆栈变量,那么我也会添加其余的代码。 (我省略了它,因为否则它会变得无关紧要) – 2014-09-27 17:19:20

+0

@derhass btw!屏幕是只有一个宽度和高度的结构,但我认为这是明显的权利? – 2014-09-27 17:21:26

我想通过derhass和Andon得知我应该在函数签名中加入inout关键字。否则参数按值传递,而不是通过引用传递。:P