金属着色语言 - 使用Struct来获取缓冲区值
问题描述:
如何使用struct
来获取缓冲区值?例如:金属着色语言 - 使用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但是我不确定如何将其转化为我的用途。
答
为鼠标位置的两个组件使用单独的缓冲区似乎对我来说很愚蠢和浪费。
创建一个包含两个缓冲区的单个缓冲区。
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.
}
然后用类似的签名写你的计算功能