Home > Net >  How to deal with the lack of `simd_packed_float3` in Swift
How to deal with the lack of `simd_packed_float3` in Swift

Time:11-05

There is no simd_packed_float3 type in Swift.

Why it's a problem?

Consider this Metal struct:

struct Test{
    packed_float3 x;
    float y;
};

First of all, you can't calculate a buffer pointer to address the memory of y, since you can't do this:

MemoryLayout<simd_packed_float3>.size

(Not sure if stride makes sense with packed types, but anyway with simd types it always gives the same length as size on my devices)

You can't use MemoryLayout<simd_float3>.size either, since it will return 16 and not 12 like in architectures available to me for testing.

Second, if you need to write a packed_float3 value of x to the buffer you will need to write the three consecutive floats, but not a single simd type. Again, simd_float3 is not usable since it will write 0 into the forth word corrupting the memory of the next property in the struct (y).

So I've done this:

struct Float_3{
   var x: Float
   var y: Float
   var z: Float
}

typealias simd_packed_float3 = Float_3

It seems to be a functioning solution, but I'm not sure it's not a nasty thing to do... What problems may I encounter with this approach, and how could I be sure that it won't break on some device that I don't have?

CodePudding user response:

You can define a packed struct in your bridging header:

struct __attribute__((packed)) PackedFloat3 {
    float x;
    float y;
    float z;
};
MemoryLayout<PackedFloat3>.size == 12
MemoryLayout<PackedFloat3>.stride == 12

By the way, simd_float3 is 16 bytes everywhere, simd types have stricter alignment requirements.

You can also typedef it to packed_float3 under #ifndef #ifdef __METAL_VERSION__ to have the same spelling in Swift and MSL.

The reason to do it in bridging header instead of Swift is that you can use the same structs with same spelling in both shaders and Swift.

CodePudding user response:

I'm answering this following the answers I received on the Swift forum.

Turns out that someone in the Metal team at Apple has already thought of this problem and created the MTLPacked types exactly for the types that would have irregular sizes:

  • Related