0

It is possible to use array in GLSL like this:

float[4](.5, .5, .5, .5)

Is it possible to use arrays in metal shader, if yes how can I declare it? Thanks!

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
Artem
  • 364
  • 2
  • 18
  • Wouldn't you use `float4` instead? – trojanfoe Jul 01 '20 at 06:48
  • 1
    no, I need more then 7 items, it was just example – Artem Jul 01 '20 at 07:19
  • This would probably work: `float mytype[7] = { 0, 1, 2, 3, 4, 5, 6 };`. – trojanfoe Jul 01 '20 at 07:27
  • doesn't work, "ERROR: expected expression" – Artem Jul 01 '20 at 07:47
  • Did you accidentally copy-paste the period `.` after the declaration? @trojanfoe's code works for me (though you can also omit the `=` to use C++'s uniform initialization syntax; the result is the same). – warrenm Jul 01 '20 at 17:43
  • Well, Im doing something wrong, but it doesn't work for me https://www.screencast.com/t/E60gjQKVLBf – Artem Jul 01 '20 at 20:10
  • Hm, when I use without = (float a[7]{ 1., 1., 1., 1., 1., 1., 1. };) it doesn't produce error, but when Im trying to take value like a[0] it is always 0. for any indexes. When I do a[0] = 1., then yes it is 1., but after initialization it looks empty – Artem Jul 01 '20 at 20:16
  • [This answer](https://stackoverflow.com/a/10470628/155187) may have a workaround for you. – warrenm Jul 02 '20 at 18:29

1 Answers1

0

It works almost like in c++:

float array[3] = float[3](0.5,0.5,0.5);
float array[3] = float[3](0.5);// also works, but you can't initialize the entire array with that number, it's going to be (0.5,0.0,0.0)

see: https://www.khronos.org/opengl/wiki/Data_Type_(GLSL)#Arrays