GLSL layout std140 padding dilemma
Asked Answered
M

2

5

I have the following uniform buffer:

layout(std140) uniform Light
{
    vec4  AmbientLight;
    vec4  LightIntensity;
    vec3  LightPosition;
    float LightAttenuation;
};  

I have some issues when buffering the data and the padding I need to add. I have read the http://ptgmedia.pearsoncmg.com/images/9780321552624/downloads/0321552628_AppL.pdf which says I have to add an extra 4 bytes at the end of the vec3 for padding - so I will upload a total of 13 bytes for 'Light'. When I do that however, 'LightAttenuation' gets the value I padded on 'LightPosition', rather than one byte ahead, so I get the correct values in the shader when I do NOT pad. Why is this?

Mapel answered 1/4, 2013 at 19:58 Comment(1)
"I have read the [PDF] which says" Who cares what it says? The OpenGL specification is the primary source; everything else is secondary. And the description of the std140 layout is not as unreadable as people might say.Ephod
R
12

See section 7.6.2.2 of the OpenGL spec for the details, but basically, std140 layout says that each variable will be laid out immediately after the previous variable with enough padding added for the alignment required for the variable's type. vec3 and vec4 both require 16-byte alignment and are 12 and 16 bytes respectively. float requires 4 byte alignment and has 4 byte size. So with std140 layout, LightPosition will get 16 byte alignment so will always end at an address that is 12 mod 16. Since this is 4-byte aligned, no extra padding will be inserted before LightAttenuation.

Ragucci answered 1/4, 2013 at 21:9 Comment(0)
A
0

Usually yes, openGL will treat a vec3 as an vec4. But AFAIK in this case it appends the float LightAttenuation to the vec3 LightPosition - forming an overall vec4 (its some kind of optimization, done by the glsl compiler). The whole structure will be of size 3x vec4.

Try out using a vec3 or vec4 for LightAttenuation.

Auroraauroral answered 1/4, 2013 at 20:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.