Search code examples
iosgraphicsmetalmetalkit

MSL - How to specify uniform array parameters in Metal shader?


I'm trying to pass an uniform array into a Metal shader, e.g.:

fragment vec4 fragment_func(constant float4& colors[3] [[buffer(0)]], ...) {...}

I'm getting the error:

"NSLocalizedDescription" : "Compilation failed: \n\nprogram_source:2:1917: error: 'colors' declared as array of references of type 'const constant float4 &'\nprogram_source:2:1923: 
error: 'buffer' attribute cannot be applied to types\nprogram_source:2:1961:

I understand that the 'buffer' attribute can only be applied to pointers and references. In that case, what's the correct way to pass in uniform arrays in MSL?

Edit: MSL specs state that "Arrays of buffer types" are supported for buffer attributes. I must be doing something syntactically wrong?


Solution

  • You may also use:

    fragment vec4 fragment_func(constant float4 colors [[buffer(0)]][3], ...) {...}
    

    This is an unfortunate side-effect of how the attribute syntax works in C++. The advantage of doing it this way is that it retains the type on colors more directly.