Code: Select all
class MyClass
{
public:
MyClass() = default;
private:
// Trying to use this in a shader, that's why the alignments is as such
XMFLOAT4 color;
XMFLOAT3 position;
float innerangle;
XMFLOAT3 direction;
float outerangle;
};
Code: Select all
struct MyStruct
{
XMFLOAT4 color;
XMFLOAT3 position;
float innerangle;
XMFLOAT3 direction;
float outerangle;
};
So to those that might know, does VS or C++ treat classes any differently than structs where allocating memory is concerned? Would it matter that MyClass is a child of two other Interface classes ( ie. No data members, only virtual methods ). I thought the C++ standard was that sizeof would only return size of the data members. If it is, then the class is being 32 byte aligned by default, because 48 is a multiple of 16, so there shouldn't be a need for any padding. The versions of xmfloat I'm using aren't the aligned versions.
I'll keep working at it, just wanted to see if anyone had any ideas as to what is causing me to have to copy all data from class to struct before updating constant buffer.
EDIT: Ok, so apparently it does matter about inheritance, even if the parent(s) are just interfaces. Using just the samples above, making MyClass inherit from just one parent causes the sizeof to return 8 more bytes than without totaling 56, so with the two parents I mentioned, if each parent uses an extra 8 bytes, then that would explain the 64 bytes sizeof is returning on my child class.
This kind of makes me want to start using classes for interfaces and just have the struct be for the data, that way I don't have to copy to the struct, then copy to the GPU.
Code: Select all
class IMyInterface
{
// All virtual methods setters, getters = 0; for pure virtual
};
class MyBase :public IMyInterface // haven't figure when to use other types of inheritance
{
struct MyStruct
{
// Common data for this base type
};
public:
MyBase() = default;
// Implement all virtual getter/setter methods
// For shaders stuff, return just the struct object
protected:
MyStruct m_myStruct;
};
class MyClass :public MyBase
{
public:
MyClass() = default;
// Implementation specific functions
};