Can I make a GLSL struct have std140 layout?

1.2k Views Asked by At

I just tried to do this

C++

struct PointLight
{
    glm::vec4 position;
    glm::vec4 colour;
};

std::vector <PointLight> lights_array;

GLSL 320 ES:

layout (std140) struct PointLight
{
    vec4 position;
    vec4 colour;
};

layout (std140) buffer Lights 
{
    int        count;
    PointLight data [];
}
b_lights;

The compile error surprised me:

error C7600: no value specified for layout qualifier 'std140'

I can't find a straight answer but I get the impression that I can't specify std140 for struct definitions. Is this so? Or how can I spell it?

If not, then how am I able to guarantee that I can send lights_array to glBufferData so that it has the correct layout in the shader's b_lights.data array?

In other words, why is std140 required for the buffer but not for the struct?

1

There are 1 best solutions below

2
On

Interface blocks have layouts, not structs. The layout applies to how the block lays out its elements, recursively, through their entire contents.

So you don't need to apply an interface block layout to a random struct.