如何使用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但是我不确定如何将其转换为我的使用。
答案 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 ¶ms [[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.
}