I suppose this is a kind-of cross between a pure C++ question and an OpenGL question. I have a uniform buffer and am allocating space in it of sizeof(ShaderData) bytes. I'm using std140 layout on the GPU side in the shader.
According to std140 rules, I need to add padding in various places in my structure to make sure things like vectors are aligned correctly. The structure below is an example (for my light):
struct ShaderData {
float Light_Intensity;
float _pad1[3]; // align following vec3 on 4N boundary
Math::Vec3f Light_Position;
float _pad2; // align following vec4 on 4N boundary
Math::Colour4f Light_Ambient;
Math::Colour4f Light_Diffuse;
Math::Colour4f Light_Specular;
float Light_AttenuationMin;
float Light_AttenuationMax;
} MyShaderData;
Is this the way people generally do things in C++, or are there special keywords or pragmas for aligning individual elements of a structure CPU side that are a little tidier?
No, in this way you just waste space. You have to find the optimized layout according to std140 rules.
float
needs 4 bytes and it's 4 bytes alignedvec3
needs 12 bytes and it's 16 bytes alignedvec4
needs 16 bytes and it's 16 bytes alignedThis means that you can find a better layout for your struct:
float Light_Intensity; X
float _pad1[3]; XXX
Math::Vec3f Light_Position; XXX
float _pad2; X
As you can see you are wasting 4 bytes and what's worse is the fact that you can just do something like:
Math::Vec3f Light_Position XXX
float Light_Intensity; X
To have it aligned and without the need to waste a byte. This works because vec3
will be aligned to 16 bytes boundaries while the float
will be still aligned to 4 bytes boundaries.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With