0

To simply put it, I have the following problem:

I am trying to implement a stack in GLSL, but it keeps giving me the following errors:

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

My code is the following:

//---------------------------
// 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;
}

I really am clueless on how this error occurred. And why are some warnings and some are errors?

I hope you can help me or provide me with a solution or direction on how to create a stack in GLSL.

EDIT: This is not the full code as I am not including the Ray part, but this should be the only relevant code.

7
  • The code you pasted so far has some serious isseus. You don't even have a stack to work with.
    – derhass
    Sep 27, 2014 at 16:43
  • Of course there's a global stack variable, I'll add the rest of the code too then. (I omitted it because it would otherwise get unneccecary complex)
    – user896326
    Sep 27, 2014 at 17:19
  • @derhass btw! Screen is a struct with only a width and height, but I think that's obvious right?
    – user896326
    Sep 27, 2014 at 17:21
  • 2
    Your code still does not make sense. Your push and pop functions work on a copy of the stack which is never passed out again, effectively doing nothing. Which might even trigger the error, as you will try to access the element -1 in that first pop call. The expression stack.entries.length is also not valid, it should be lenght().
    – derhass
    Sep 27, 2014 at 18:33
  • 1
    You mean length() ;) Sep 27, 2014 at 19:05

1 Answer 1

0

I figured out thanks to derhass and Andon that I should have putted the inout keyword at the function signature. Otherwise parameters are passed by value instead of by reference.:P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.