Search code examples
iosshadermetal

Metal: unknown type name float4


I'm trying to include a header file in a metal shader. For a prototype like this,

float4 someFunction(float4 v);

I get this error message,

Unknown type name 'float4'; did you mean 'float'?

It seems it doesn't understand it's a header for a shader program... Although other errors suggest it does. For instance, if I don't specify the address space here,

static float someK = 2.0;

I get this error,

Global variables must have a constant address space qualifier

which can be fixed if I add

constant static float someK = 2.0;

If I use references, I also get these type of errors,

Reference type must include device, threadgroup, constant, or thread address space qualifier

So it does look as if the compiler knows it's a shader. Why it doesn't know about float4? :(


Solution

  • Make sure you have the first two lines in your shader like in this example:

    #include <metal_stdlib>
    
    using namespace metal;
    
    float4 someFunction(float4 v);
    
    kernel void compute(texture2d<float, access::write> output [[texture(0)]], 
                        uint2 gid [[thread_position_in_grid]])
    {
        float4 color = float4(0, 0.5, 0.5, 1);
        output.write(color, gid);
    }
    

    This works fine for me.