0

Padding and formatting between C++ code and GLSL code is working but doesn't make sense. I would be happy if someone can explain.

I am sending this struct with uniform buffer to my shader.

struct PerFrameData
{
    glm::mat4 CameraViewProjection{};
    glm::vec3 CameraWorldPosition{};
    float TotalTime;
} frameData{};

I have tested and alignments of elements are 0,64,76. Total size is 80B and struct alignment is 4B.

According to std140:

  • scalar types are aligned on 4B
  • vec2 is aligned on 8B
  • vec3 and vec4 are aligned on 16B
  • mat3 and mat4 are aligned on 16B per row/column
  • array elements are aligned on ceil 16B offset from array element size

When I see my struct above. It would make sense to put 4B of padding before "float TotalTime", but then it doesn't work. (It works when it doesn't have padding and that is what bugs me).

And this is how it looks in GLSL shader:

layout(std140, binding = 1) uniform PerFrameBuffer
{
    mat4 CameraViewProjection;
    vec3 CameraWorldPosition;
    float TotalTime;
};

Am I missing something or?

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 1
    "aligned to 16B" means that the element's start offset is aligned to 16 bytes, but it does not mean that the element consumes 16 bytes of memory. The alignment of a `float` in a structure is 4 bytes – Rabbid76 Dec 28 '22 at 14:05
  • 2
    @Bruno: This scenario is one of the reasons why you shouldn't use `vec3`. – Nicol Bolas Dec 28 '22 at 14:35

0 Answers0