Search code examples
iosarraysmetal

Metal array indexing works using literals but crashes using a variable


I have an array in a compute kernel that stores a series of values (only around 10 for now). I assign the values to the array as below:

int count = uniforms;
int i = 0;
float pointsx[count];
float pointsy[count];
for (i = 0; i < count; i++){
  pointsx[i] = simplex3D(i*2,1,1);
  pointsy[i] = simplex3D(1,i*2,1);
}

I then go on to iterate over these points using a similar for loop. This crashes at runtime reporting a compiler error when I try and access the array at any index, however if I were to replace the i with an integer literal it works.

I feel I'm missing something very obvious, any help would be appreciated.


Solution

  • Variable-length arrays are not supported in Metal. Since there is a small upper bound on the number of entries in the array, you can declare an array of that size. Using a buffer is more flexible, but requires you to read from device memory.