Group Group Group Group Group Group Group Group Group

Passing an struct with an array to Metal

Hi again!
Is it possible to declare a header file with a struct containing, among others, an array of float3 and pass it to Metal? If yes, how do you set the length when making the buffer? Adding each property individually?
Thanks in advance

You can only pass a fixed length array in a struct. The GPU can’t do dynamic length arrays.

An example of passing an array would be Instances in Chapter 13.

You place a struct describing an Instance in Common.h. You then create a buffer with an array of Instances. You pass this buffer to the vertex shader and use instance_id to extract the correct Instance from the array.

1 Like

Ok, thank you very much!

What I’m really trying is to pass an structure with an fixed length array and not an array of structures:

typedef struct{
    float amplitude;
    float frequency;
    int p [256];
    float Gx [256];
    float Gy [256];
    float Gz [256];

var noise = PerlinNoise();
noise.p = Array(repeating: Int32(0), count: 256) //Cannot assign value of type 'Array<Int32>' to type '(Int32, Int32, ...)'

Looks like expects a tuple ?!?!

Yes. It expects a tuple. Swift doesn’t support fixed length arrays yet :frowning:

That person is using an Objective-C wrapper :scream:

1 Like