GLSL - 数组索引超出范围

时间:2014-09-27 16:23:19

标签: opengl stack 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中创建堆栈的解决方案或方向。

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

1 个答案:

答案 0 :(得分:0)

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