Simd_half3 in a Header file

Hi there!

I’m using simd_half3 in both sides: swift SIMD3<Float16> and Metal half3 but when I want to define a struct in a header file I can’t:

#import <simd/simd.h>

typedef struct {
   simd_half3 pos; // ERROR
} Particle;

Any idea?

Thanks in advance

There is no half in Swift, unfortunately.

I’ve never done it, but you could try something like this: swift - Populating MTLBuffer with 16-bit Floats - Stack Overflow

From last swift version has half :
isn’t it ?
My issue is define a half type in the header file

Oh my! I didn’t know this or forgot it - thank you!

Oliver on Twitter has the answer:

I quickly tested it by adding a half test to the Light structure and putting a color in there, and it worked.

In Common.h:

#import <simd/simd.h>
#ifndef __METAL_VERSION__
#import <Foundation/Foundation.h>
#define half float16_t
typedef struct {
  vector_float3 position;
... etc
  float coneAttenuation;
  half test;
} Light;

In Swift:

light.test = Float16(0.5)

In shader:

return float4(lights[0].test, 0, 0, 1);

(Not available on macOS though)

1 Like

Wow!! super!!! Works fine to me.

Thank you very much @caroline

But… why does not exist a float16x3_t ?!?!? and float16x4_t yes

Maybe it will appear this WWDC :grin: