Swift 金属着色语言-使用Struct获取缓冲区值

Swift 金属着色语言-使用Struct获取缓冲区值,swift,macos,kernel,metal,Swift,Macos,Kernel,Metal,如何使用struct获取缓冲区值?例如: struct mouseInput { float x; float y; }; kernel void compute(texture2d<float, access::write> output [[texture(0)]], constant float &time [[buffer(0)]], constant mouseInput.x &

如何使用
struct
获取缓冲区值?例如:

struct mouseInput
{
float x;
float y;
};

kernel void compute(texture2d<float, access::write> output [[texture(0)]],
                    constant float &time [[buffer(0)]],
                    constant mouseInput.x &mouseX [[buffer(1)]],///<--mouseX from swift
                    constant mouseInput.y &mouseY [[buffer(2)]],///<--mouseY from swift
                    uint2 gid [[thread_position_in_grid]]) {
...
}
struct mouseInput
{
浮动x;
浮动y;
};
内核无效计算(texture2d输出[[texture(0)]],
恒定浮动和时间[[缓冲区(0)],

常量mouseInput.x&mouseX[[buffer(1)],//对鼠标位置的两个组件使用单独的缓冲区对我来说既愚蠢又浪费

创建一个包含两者的缓冲区。然后使用以下签名编写计算函数:

struct mouseInput
{
float x;
float y;
};

kernel void compute(texture2d<float, access::write> output [[texture(0)]],
                    constant float &time [[buffer(0)]],
                    constant mouseInput &mouse [[buffer(1)]],
                    uint2 gid [[thread_position_in_grid]]) {
...
}
struct mouseInput
{
浮动x;
浮动y;
};
内核无效计算(texture2d输出[[texture(0)]],
恒定浮动和时间[[缓冲区(0)],
恒定鼠标输入和鼠标[[缓冲区(1)]],
uint2 gid[[螺纹位置在网格中]]{
...
}
事实上,根据应用程序的其他部分,将时间与鼠标位置结合起来可能是有意义的:

struct params
{
    float time;
    float2 mouse;
};

kernel void compute(texture2d<float, access::write> output [[texture(0)]],
                    constant params &params [[buffer(0)]],
                    uint2 gid [[thread_position_in_grid]]) {
...
// use params.time to get the time value.
// Use params.mouse.x and params.mouse.y to get the mouse position.
}
struct参数
{
浮动时间;
2只小鼠;
};
内核无效计算(texture2d输出[[texture(0)]],
常量参数和参数[[缓冲区(0)]],
uint2 gid[[螺纹位置在网格中]]{
...
//使用params.time获取时间值。
//使用params.mouse.x和params.mouse.y获取鼠标位置。
}