Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: vec4 vs float[4] alignment in open gl 330 core

  1. #1
    Newbie Newbie
    Join Date
    Jun 2018

    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.
    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

    Code :
    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. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Quote Originally Posted by kevinyu View Post
    Is there something that i miss here?
    2.11.4 says (emphasis mine):

    4. 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 std140 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 std430 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts