PDA

View Full Version : Cannot align data passed to shader



nbdspcl
05-13-2014, 09:21 AM
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[];
};

One can use arrays of floats in the structure instead of mat3 and vec4, but how to align matrices and vectors in shaders?

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 as well as without any padding.

Quering the offset of align2 with glGetProgramResourceiv gives 76, what probably should mean that the whole structure is of 80 bytes size what is multiple of 16.

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