Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLSL layout std140 padding dilemma

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?

like image 813
KaiserJohaan Avatar asked Apr 01 '13 19:04

KaiserJohaan


1 Answers

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.

like image 128
Chris Dodd Avatar answered Oct 11 '22 23:10

Chris Dodd