So, I've been playing around with optimizing some older code (in c++ / DirectX 9), and came across a bunch of struct statements that I had for vertex types, and each one of them shared some common attributes (an x/y/z for the vertex, and a method to get the size of the struct...) - so, doing the logical (in my mind...) thing, I decided to simplify some things by creating a base class with common attributes and deriving the other vertex types from that base class... which in turn would eliminate a lot of (almost) duplicate functions for setting up vertex buffers, etc... so essentially, I went from the following example (simplified for illustration):
struct SingleVertNoColor {
float x, y, z;
size_t getSize() { return sizeof(SingleVertNoColor); }
}
struct SingleVertWithColor {
float x, y, z;
int32 color;
size_t getSize() { return sizeof(SingleVertWithColor); }
}
... to the following code:
class IVertexType {
public:
float x, y, z;
virtual size_t getSize()=0;
}
class SingleVertNoColor : public IVertexType {
public:
virtual size_t getSize() { return sizeof(SingleVertNoColor); }
}
class SingleVertWithColor : public IVertexType {
public:
int32 color;
virtual size_t getSize() { return sizeof(SingleVertWithColor ); }
}
Now... in the struct versions, the sizeof(SingleVertNoColor) is 12 and sizeof(SingleVertWithColor) is 16 (which is expected...).
In the class versions, of course, there is the overhead of the _vftable, so the sizeof(SingleVertNoColor) becomes 16 and sizeof(SingleVertWithColor) jumps to 20.
Now, this isn't a problem, however, when I go to lock and copy my vertices into my LPDIRECT3DVERTEXBUFFER9, the extra size is no problem, but it always seems to want to put the _vtable pointer at the beginning of the memory space for the derived classes...
Is there any way to tell the compiler to push all member variables to the front of the classes' memory allocation, or, since I'm using customized D3DVERTEXELEMENT9 items for declaring my vertexes (i.e. non-FVF), is there any way to add in the extra _vftable pointer (and have it's usage be set to ignore / nothing)? Or - even with the second thought, would I even be guaranteed that my class items would even be in the same order?
Any thoughts? Much appreciated in advance.