Alignment of records in oxygene Cocoa


(Friedrich Westermann) #1

I have a struct in Xcode like:

typedef struct
{
    // Positions in pixel space
    // (e.g. a value of 100 indicates 100 pixels from the center)
    vector_float2 position;

    // Floating-point RGBA colors
    vector_float4 color;
} AAPLVertex;

sizeof(AAPLVertex) in Xcode is 32

in oxygene I’m doing it:

  AAPLVertex =  record
    // Positions in pixel space
    // (e.g. a value of 100 indicates 100 pixels from the center)
    position : vector_float2;
    dummy : vector_float2; // For padding
    // Floating-point RGBA colors
    color : vector_float4;
  end;

I need to add. dummyfields
how can i declare a record without the need for the dummy member?


(SH) #2

I have the same thing going on for my 2D Engine and to be honest, its nothing so problematic about, isnt it?

I have also:

RGB =  record
  R,G,B, PADD: byte; //its only 1 field more
end;

(marc hoffman) #3

IIRC theres an attribute to control packing, [Packed], but @ck will know more, on Monday.


(Carlo Kok) #4

How did you define vector_float2 and friends? afaik we don’t currently import those (nor does the compiler currently properly support vector types).


(Friedrich Westermann) #5

Hello Carlo

my defines :

type
  vector_float2 = Array[0..1] of Single;  // sizeof(vector_float2) = 8 in XCode
  vector_float4 = Array[0..3] of Single; //sizeof(vector_float2) = 16 in XCode

first type

typedef struct
{
    // Positions in pixel space (i.e. a value of 100 indicates 100 pixels from the origin/center)
    vector_float2 position;

    // 2D texture coordinate
    vector_float2 textureCoordinate;
} AAPLVertex;   // sizeof(AAPLVertex) = 16 in XCode

second type

typedef struct
{
    // Positions in pixel space (i.e. a value of 100 indicates 100 pixels from the origin/center)
    vector_float2 position;

    // Floating point RGBA colors
    vector_float4 color;
} AAPLVertex;   // sizeof(AAPLVertex) = 32 in XCode

so for me it looks like the alignment = 16 in XCode
How can I get these in Oxygene without the need for padding myself?


(Carlo Kok) #6

The problem here is that a vector of 2 * single has a different alignment (8) than array of Single (4). Arrays are aligned on their biggest member, vectors on the total.

I’ve logged an issue to import these vector types and add basic support for them in the compiler (though for now I suggest the manual padding)


(Friedrich Westermann) #7

Maybe I’m stupid, but would a Align Attribute not better in these case.
so we can define simple

  [ALIGN, 16]
  AAPLVertex =  record
      // Positions in pixel space
      // (e.g. a value of 100 indicates 100 pixels from the center)
    position : vector_float2;
      // Floating-point RGBA colors
    color : Color;//vector_float4;
  end; 

and no need for using the imported types?


(Carlo Kok) #8

It would; kind of, but llvm has no such concept so it’d be pretty hard to implement. That said we should properly support these types anyway, might as well add them.


(Michael Thuma) #9

In Objective-C/Cocoa it’s possible to use a the #pragma pack(push,X) which is passed to the compiler.

I did not investigate that matter in the very detail, but as far as I have seen very likely ObjectiveC/Cocoa doesn’t align a struct.

I think pack(push, 16) provided the result Mr. Friedrich mentioned in the post. I tried with 3 floats and a double value.

There had been a keyword packed once, but I think packing a record by default is not the worst idea at all. Adding a dummy is lot more specific and reliable.

Has vector_float2 been imported from a C/C++ Library or is it a different type?


(Friedrich Westermann) #10

I‘m in the work of translate the apple metal examples to oxygene.
These types are from simd.h
They are also used in the shaders, so the alignment is important. But for now i can doing the alignment with padding Fields.


(Michael Thuma) #11

Ok. Good look!

Sounds like std:vector. From my feeling a way to align data types will be required. C++11. The moment you specify a collection you can pass a certain kind of alignment attribute or something like that. So I really tend to support your case.

The moment it can be done, it will be done. No matter if it makes sense or not :grinning:. Importing dynlibs …

MMX afik requires 16bit alignment or graphic pipelines in general are more likely to do so. But I’m no expert in this field.