【问题标题】:GLSL - Array index out of boundsGLSL - 数组索引超出范围
【发布时间】:2014-09-27 16:23:19
【问题描述】:

简单来说,我有以下问题:

我正在尝试在 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 部分,但这应该是唯一相关的代码。

【问题讨论】:

  • 到目前为止您粘贴的代码有一些严重的问题。你甚至没有堆栈可以使用。
  • 当然有全局栈变量,剩下的代码我也会添加。 (我省略了它,因为否则它会变得不必要的复杂)
  • @derhass 顺便说一句! screen 是一个只有宽度和高度的结构,但我认为这很明显吧?
  • 您的代码仍然没有意义。您的 push 和 pop 函数在堆栈的 copy 上工作,该堆栈永远不会再次传递出去,实际上什么都不做。这甚至可能触发错误,因为您将尝试在第一次 pop 调用中访问元素 -1。表达式stack.entries.length 也无效,应该是lenght()
  • 你的意思是length() ;)

标签: opengl stack glsl


【解决方案1】:

感谢 derhass 和 Andon,我发现我应该将 inout 关键字放在函数签名处。否则参数是按值而不是按引用传递的。:P

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多