EN VI

C++ - How does OpenGL std430 layout align elements in an array?

2024-03-15 23:00:12
C++ - How does OpenGL std430 layout align elements in an array?

I have a PointLight struct in C++

struct PointLight
{
  glm::vec4 position; // 16 bytes
  glm::vec4 color;    // 16 bytes
  float intensity;    // 4 bytes rounded to 16 bytes?
  float range;        // 4 bytes rounded to 16 bytes?
};

usage in ssbo:

layout(std430, binding = 3) buffer lightSSBO
{
  PointLight pointLight[];
};

Will the float elements be rounded up to 16 bytes due to the vec4s? Does std430 always round elements in an array to the size of the largest element?

Will I need to add padding to my struct to correctly access the variables?

struct PointLight
{
  glm::vec4 position; // 16 bytes
  glm::vec4 color;    // 16 bytes
  float intensity;    // 4 bytes
  float range;        // 4 bytes
  float padding[2];   // padding to make struct multiple of 16 bytes, because
                      // largest element vec4 is 16 bytes
  // total 48 bytes
};

Solution:

Yes. (see https://registry.khronos.org/OpenGL/specs/gl/glspec46.core.pdf#page=166&zoom=100,168,308) from rule 9 and 10 for std430: If the member is an array of S structures, the base alignment of the structure is N, where N is the largest base alignment value of any of its members.
In the case of std140, this would be rounded up to the basic alignment of a vec4. I case of std430 is is not rounded up. However, since the largest element in your case is a vec4, this makes no difference in your case.

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login