金属:未知类型名称 float4

Metal: unknown type name float4

我正在尝试在金属着色器中包含一个 header 文件。 对于这样的原型,

float4 someFunction(float4 v);

我收到此错误消息,

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

它似乎不理解它是着色器程序的 header...尽管其他错误表明它理解。例如,如果我在这里不指定地址 space,

static float someK = 2.0;

我收到这个错误,

Global variables must have a constant address space qualifier

如果我添加

可以修复
constant static float someK = 2.0;

如果我使用引用,我也会遇到这些类型的错误,

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

所以看起来编译器确实知道它是一个着色器。为什么它不知道 float4? :(

确保您的着色器中有前两行,如本例所示:

#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);
}

这对我来说很好。

尝试使用

vector_float4

相反。