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

时间:2017-04-22 09:58:14

标签: 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 &mouseX [[buffer(1)]],///<--mouseX from swift
                    constant mouseInput.y &mouseY [[buffer(2)]],///<--mouseY from swift
                    uint2 gid [[thread_position_in_grid]]) {
...
}

然后我可以在mouseInput.x的任何地方访问Metal。最接近的是this thread但是我不确定如何将其转换为我的使用。

1 个答案:

答案 0 :(得分:2)

对鼠标位置的两个组成部分使用单独的缓冲区对我来说似乎很愚蠢和浪费。

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

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 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.
}
相关问题