cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

spectral
Adept II

struct alignment question

Hi,

Can I do this ? Or should I align the AABB struct too ?

 

typedef struct __attribute__ ((packed)) _AABB { float MinX, MinY, MinZ, MaxX, MaxY, MaxZ; } AABB; typedef struct __attribute__ ((packed)) _BoundingVolume { AABB BBox; int SkipNodeIndex; int PrimitiveId; ushort Count; ushort pad1; int pad2; int pad3; int pad4; } BoundingVolume; BoundingVolume * list; list[15].Count = 10;

0 Likes
3 Replies
spectral
Adept II

Also, the alignment is only necessary for struct that are transfered between the OpenCL kernel and the host ?

By example, in my code I use the following struct and pass its pointer to functions.... is it a problem ?

typedef struct MITData { float4 InverseDirection; int IsXNegative; int IsYNegative; int IsZNegative; } MITData;

0 Likes

typedef struct __attribute__ ((packed)) _AABB
{
    float MinX, MinY, MinZ, MaxX, MaxY, MaxZ;
} AABB;




This struct is aligned as alignment of each element is satisfied. Alignment
requirement of a struct/Union is least multiple of alignments of all its
variables which in this case is 4. Hence no padding is required.

typedef struct __attribute__ ((packed)) _BoundingVolume
{
    AABB BBox;                        //Required alignment - 4 (x)
    int SkipNodeIndex;                //Required alignment - 4 (x + 24)
    int PrimitiveId;                //Required alignment - 4 (x + 28)
    ushort Count;                        //Required alignment - 2 (x + 32)
   
    ushort pad1;                        //Required alignment - 2 (x + 34)

    int pad2;                        //Required alignment - 4 (x + 36)
    int pad3;                        //Required alignment - 4 (x + 40)
    int pad4;                        //Required alignment - 4 (x + 44)

} BoundingVolume

  As the next element will come at x + 48 (AABB), its alignment is
satisfied. Hence this is correct.

typedef struct MITData
{
    float4 InverseDirection;        //x

    int IsXNegative;                //x + 16
    int IsYNegative;                //x + 20               
    int IsZNegative;                //x + 24
} MITData;

Alignment is only required when you transfer from host to device. The above struct does not respect the alignment of float4 variable as its not circular aligned. Suppose the struct starts with memory location x then the next object of the struct will be at x + 28 (i.e float4 variable) but it requires
alignment of 16 (or its multiple). Hence to satisfy its alignment you will
have to pad the struct at the end on host side -

typedef struct MITData
{
    float4 InverseDirection;        //x

    int IsXNegative;                //x + 16
    int IsYNegative;                //x + 20               
    int IsZNegative;                //x + 24
    int pad;                        //x + 28
} MITData;

0 Likes

Hi,

Thanks for your answer...

So, everything is correct 🙂

0 Likes