This question asks wether one can rely on the compiler to not mess with a struct
s values order and padding.
According to the answer to that question,
OpenGL defines, very clearly, what the byte layout of a
std140
interface block is.C++11 defines a concept called "standard layout types".
The only things C++ tells you about standard layout types with regard to layout is that empty base classes are ignored (so long as it remains standard layout) and that the first NSDM will be at the very beginning of the class. That is, there will never be padding at the front.
The other thing the standard says is that NSDMs of the same access class will be allocated in order, with later ones having larger offsets than earlier ones.
But that's it, as far as the C++ standard is concerned. [class.mem]/13 states that implementations can add padding between members for various reasons.
That possible but not always present padding can really mess things up, and the worst part - it depends on the compiler.
To avoid bugs and nightmares, isn't it better to use a compiler-agnostic approach?
For example:
class BufferData
{
private:
GLfloat data[12];
public:
GLfloat* getCameraPosition()
{
return (GLfloat*) &data[0];
}
GLfloat* getLightPosition()
{
return (GLfloat*) &data[4];
}
GLfloat* getLightDiffuse()
{
return (GLfloat*) &data[8];
}
GLfloat* getData()
{
return data;
}
};
As opposed to the naive:
struct BufferData
{
GLfloat camera_position[4];
GLfloat light_position[4];
GLfloat light_diffuse[4];
};
Or is the naive approach good enough?
(Let's suppose that the class/struct has more than just that, and might change)