GLSL layout std140 add-ons dilemma

I have the following uniform buffer:

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

I have some problems with data buffering and the addition I need to add. I read http://ptgmedia.pearsoncmg.com/images/9780321552624/downloads/0321552628_AppL.pdf which says that I have to add an extra 4 bytes to the end of vec3 to fill in - so I will upload only 13 bytes for "Light" . However, when I do this, "LightAttenuation" gets the value that I put on the "LightPosition", and not one byte forward, so I get the correct values ​​in the shader when I am NOT a deck. Why is this?

+4
source share
2 answers

For details, see section 7.6.2.2 of the OpenGL specification, but basically the std140 layout says that each variable will be laid out immediately after the previous variable with the addition of sufficient padding for alignment needed for the variable type. vec3 and vec4 both require 16-byte alignment and are equal to 12 and 16 bytes, respectively. float requires 4 byte alignment and has a size of 4 bytes. Thus, with the layout std140 LightPosition will get 16-byte alignment, so it will always end at an address equal to 12 mod 16. Since this is 4-byte aligned, the add-on will not be inserted before LightAttenuation .

+9
source

Usually yes, openGL will handle vec3 as vec4. But AFAIK in this case adds float LightAttenuation to vec3 LightPosition - forming a common vec4 (its some optimization performed by the glsl compiler). The whole structure will have a size of 3x vec4.

Try using vec3 or vec4 for LightAttenuation.

0
source

All Articles