C++ and GLSL structure alignment

Can you see any problem with my C++ and GLSL structures here? I am using std430 layout to store these in a shader storage buffer. sizeof(GPUMaterial) is 208 and offsetof(GPUMaterial, textureHandle) equals 80.

C++

struct GPUMaterial
{
	float diffuseColor[4];
	float metalnessRoughness[2];
	float emissiveColor[3]; uint32_t flags;
	float texturescroll[3]; float alphacutoff;
	float displacement[2];
	float specular[3], gloss;
	GLuint64 texturehandle[16];
};

GLSL:

struct Material
{
	vec4 diffuseColor;
	vec2 metalnessRoughness;
	vec3 emissiveColor;
	uint flags;
	vec3 texturescroll;
	float alphacutoff;
	vec2 displacement;
	vec4 speculargloss;
	uvec2 textureHandle[16];
};

The storage buffer is declared like this:

layout(std430, binding = STORAGE_BUFFER_MATERIALS) readonly buffer MaterialBlock { Material materials[]; };

The C++ version won’t have any padding; the GLSL version will have 2 words of padding before the emissiveColor field (vec3 is aligned to a 4-word boundary) and another 2 words of padding before the specularGloss field (vec4 is aligned to a 4-word boundary).

Note that there’s no difference between std140 and std430 in this case.

You can use glGetProgramResource to query the offsets of structure members.

1 Like

Yep, that was it. Thanks for the info!