I can’t seem to find any information on how to calculate padding within a struct or a class within C, C++.
In HLSL there are data structures called “Constant Variables” and they are very much like C structs with #pragma pack (4) enabled. Here is a link for more information with regards to Constant Variables in HLSL.
The problem I am having is with trying to create a struct format descriptor. After parsing the HLSL code, a constant descriptor will contain information about the data types contained within a constant variable struct. It will describe the data type of each member variable, its offset and the total size of the struct. The trouble I am having is in determining the final size of a struct due to padding.
If there is an algorithm for this then I should be able to code it up and calculate the actual padded size of any “Constant Variable” in HLSL. The problem is I don’t know what it is and nor do I know where to find it?
The compiler cannot shuffle data around in a C-style struct. C guarantees that the members are allocated in exactly the order they are declared. Any padding must come between members or at the end of the struct.
A C++ class with access specifiers does not have to be compatible with C, so some reordering is allowed.