struct alignment

What are the rules for the alignment of structs within structs in Metal? I cannot find it documented anywhere in the shading language specification. And do structs have any minimum size & alignment constraints? (I need to generate memory layouts before the shaders are compiled, so using the reflection API is not an option).


e.g. I have the following


struct S0

{

float4x4 A;

float4x4 B;

float3x3 C;

float4 D;

};

struct M0

{

float A;

};

struct N0

{

float A;

};

struct Uniforms

{

N0 nor0;

M0 mut0;

S0 su;

};


What would the expected alignments be? Should I assume they follow standard C++ behaviour?

e.g.
offsetof(Uniforms, N0) == 0

offsetof(Uniforms, M0) == 4

offsetof(Uniforms, S0) == 16


Thx - j

I think Metal follows standard C/C++ alignment algorithms, but one can get surprised when it comes to sizeof certain types. For example both sizeof float3 and its alignment is 16. You can find table in Metal Language Specification.

Thanks. I've found all the docs for the general types. It's the issue of structs within structs (and depending on the alignment contraints of their members) that doesn't appear to be documented *anywhere*. I guess I'll stick to guesswork and seeing how the C++ compiler packs things.

I think it just like standard C. See for example: http://www.catb.org/esr/structure-packing/

Just like in C. Google f.e. "the lost art of c structure packing". Posted a link here, but moderation takes ages here.

In those cases, just break the link, like this:


h ttp://www.removeTheSpaceToMakeWork.com

struct alignment
 
 
Q