Vec4 vs float[4] alignment in open gl 330 core

Hi i am having an issue with uniform buffer object in opengl 330 core.

So I have a struct at my c++ code.

struct DirectionalLightUBO {
	Mat4 shadowMatrix[4];
	Vec3f direction;
	float pad1;
	Vec3f color;
	float pad2;
	float cascadeDepths[4];
};

struct LightDataUBO {
        DirectionalLightUBO directionalLights[4];
	int directionalLightCount;
} lightDataUBO;

And then I have a uniform buffer at my glsl

struct DirectionalLight {
	mat4 shadowMatrix[4];
	vec3 direction;
	float pad1;
	vec3 color;
	float pad2;
	float cascadeDepths[4];
};

layout (std140) uniform LightData {
	DirectionalLight directionalLights[4];
	int directionalLightCount;
};

I sucessfully transfer the value of my ubo up to pad2. But the value of cascadeDepths is not the same as what I set in my c++ code. The weird thing is if I change the cascadeDepths[4] to vec4 in the glsl then the data from my c++ code map nicely to glsl where cascadeDepths[0] -> vec4.x, cascadeDepths[1] -> vec4.y and so on. Is there something that i miss here?

§2.11.4 says (emphasis mine):

  1. If the member is an array of scalars or vectors, the base alignment and array stride are set to match the base alignment of a single array element, according to rules (1), (2), and (3), and rounded up to the base alignment of a vec4.

IOW, each array element occupies the space of a vec4. The [var]std140[/var] layout was designed with consideration for architectures where the basic unit of memory for uniforms is a vec4. This doesn’t prevent smaller types being packed together to form vec4s, but array indexing can’t yield anything smaller than a vec4.

The [var]std430[/var] layout doesn’t have this restriction, but it doesn’t exist in 3.3 and can’t be used for uniform blocks even where it exists.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.