0

I have the following array of structs in glsl SSBO:

struct node {

  vec3 min, max;

  int hitNext;   
  int missNext;    
  int firstTri, numTris;
};
layout(std430, binding = 3) readonly buffer Nodes {
  node[] nodes;
};

I am passing them a bytebuffer where I have stored the data in the following format:

4 bytes min.x, 4 bytes min.y, 4 bytes min.z, 4 bytes 1.0f,
4 bytes max.x, 4 bytes max.y, 4 bytes max.z, 4 bytes 1.0f,
4 bytes hitNext, 4 bytes missNext, 4 bytes firstTrue, 4 bytes numTris

But glsl is not reading the data properly. If I change the vec3 in the struct definition to vec4 it works perfectly. But why is that the case? According to the OpenGL specs won't this be the alignment?:

struct node {               //Base alignment    Aligned Offset
  vec3 min;                 //  16                  0

  vec3 max;                 //  16                  16

  int hitNext;              //  4                   32

  int missNext;             //  4                   36

  int firstTri, numTris;    //  4                   40
                            //  4                   44
};
layout(std430, binding = 3) readonly buffer Nodes {
  node[] nodes;             //  16                  48
};

But this alignment matches up with the data I'm passing in. Why does this not work but vec4 works?

Rwitaban Goswami
  • 427
  • 3
  • 13

1 Answers1

3
struct node {               //Base alignment    Aligned Offset   
  vec3 min;                 //  16                  0
  vec3 max;                 //  16                  16
  int hitNext;              //  4                   32

Nope, that's not what the spec says. A vec3 may just need an alignment of 16 bytes, but it does not consume 16 bytes. Since the base alignment of the element following it is just 4, we end up with an aligned offset of 28 for hitNext.

This is just another instance of this question.

derhass
  • 43,833
  • 2
  • 57
  • 78