【问题标题】:Metal Shading Language - use Struct to get buffer valueMetal Shading Language - 使用 Struct 获取缓冲区值
【发布时间】:2017-04-22 09:58:14
【问题描述】:

如何使用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,但我不知道如何将其转化为我的使用。

【问题讨论】:

    标签: swift macos kernel metal


    【解决方案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 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.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-29
      • 2017-12-11
      相关资源
      最近更新 更多