Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly pad and align data in OpenGL for std430 layout

Tags:

c++

opengl

glsl

I cannot get the following structure to work when using it in Shader Storage Object:

Definitions in shader:

struct Object
{
    vec4                color;
    mat3                transform;
    float               depth;
    float               pObjIndex;
    //float align1;
    //float align2;
};

layout (std430, binding = 0) buffer Objects
{
  Object objects[];
};

It's my understanding that the Object structure is aligned in GLSL by its largest member which is vec4, i.e. by 16-byte boundary. (mat3 is treated as 3-element array of vec3's and is aligned by 12-bytes boundary). The vec4, mat3 and 2 floats give 72 bytes. When I pad the structure to the nearest 16-byte multiple - 80 - with two floats, it's not working properly just as without padding.

Are both variants of this structure's layout - with padding with 2 floats and without any padding - incorrect?

like image 448
Al Berger Avatar asked May 13 '14 10:05

Al Berger


1 Answers

the mat3 is actually 3 vec4s as each vec3 is 3 floats aligned to 16 bytes (or at least it may not straddle a 16 byte boundary)

struct Object
{
    float[4]           color;
    float[4][3]         transform;
    float           depth;
    float           pObjIndex;
    //float align1;
    //float align2;
};
like image 97
ratchet freak Avatar answered Sep 28 '22 11:09

ratchet freak