Metal - 缓冲区作为全局变量

Metal - buffer as global variable

我有一个缓冲区可以将鼠标位置传递给

kernel void compute(texture2d<float, access::write> output [[texture(0)]],
                    constant float2 &mouse [[buffer(1)]],//<-- mouse buffer
                    uint2 gid [[thread_position_in_grid]])
{
... 
}

如何将它设为全局常量,以便我可以在内核之外的任何函数中访问它?例如:

float abc(float p){

float a = p * globalmouseposition.x;

return a;

}

Metal 不支持可变全局变量。您应该将必要的值作为参数传递给所有使用它们的函数。还有其他方法(例如将所有函数包装在一个结构或 class 中并使用成员变量来模拟全局变量),但我建议只将您需要的值从一个函数传递到另一个函数。