Search code examples
graphicsglslvulkan

Array of SSBO buffer access fault


I have a storage buffer:

struct GameObjectDesc {
    uint64_t verticesAddress;
    uint64_t indicesAddress;
    PBR pbr;
    ivec2 textureEntry;
};
layout (set = 1, binding = 1) readonly buffer GameObjectDescBuffer {GameObjectDesc gameObjectDescs[];} gameObjectDescBuffer;

The struct was 80 bytes large.

I have checked the descriptor writer many times. After that I have used NSight to debug it, the data is written into the shader correctly.

NSight debug results

Everything goes well if i just access the first element by:

    GameObjectDesc gameObjectDesc = gameObjectDescBuffer.gameObjectDescs[0];

But, the program crashes if I use:

    GameObjectDesc gameObjectDesc = gameObjectDescBuffer.gameObjectDescs[1];

What confuses me is that the data is written into the shader correctly because NSight shows everything well, there are two elements in the array of the buffer, but the access always seems to violate the bound of the array once I was trying to access the second element.

I have tried to align the struct to 128 bytes but nothing works. I just want to access the other elements of the array successfully.


Solution

  • I have found the problem: I was confused by struct array in a single buffer and a buffer array. I was using one buffer with an array of struct in it but when I was creating descriptor layout and set bindings, set writes, I made the struct count as the descriptor count, which is a serious problem.

    For example: I have 4 struct and write into a single buffer. When binding and writing, the descriptor count must be 1.

    layout (set = 1, binding = 1,std430) readonly buffer GameObjectDescBuffer {GameObjectDesc gameObjectDescs[];} gameObjectDescBuffer;
    

    If I make descriptor count 4, that means:

    layout (set = 1, binding = 1,std430) readonly buffer GameObjectDescBuffer {GameObjectDesc gameObjectDescs;} gameObjectDescBuffer[];