使用金属计算内核对输出缓冲区进行排序?

时间:2019-09-16 22:38:37

标签: ios sorting gpu metal

我有一个计算内核,用于计算2D点输入缓冲区中提供的每个点的亮度值:

struct DrawingPoint {
    half x;
    half y;
};

kernel void sortPoints(device DrawingPoint *artPoints [[buffer(0)]],
                             device DrawingPoint *artPointsOut [[buffer(1)]],
                             device atomic_int &counter [[buffer(2)]],
                             texture2d<half> colorTexture [[ texture(0) ]],
                             uint id [[thread_position_in_grid]]) {

    constexpr sampler default_sampler;

    DrawingPoint artPoint = artPoints[id];

    float2 colorTextureCoord = float2(artPoint.x, artPoint.y);
    half4 color = colorTexture.sample(default_sampler, colorTextureCoord);
    half gray = (color.r * 0.21h) +  (color.g * 0.72h) + (color.b * 0.07h);

    int counterValue = atomic_fetch_add_explicit(&counter, 1, memory_order_relaxed);

    artPointsOut[counterValue] = artPoints[id];
}

上面的代码只是以相同的顺序将输入点输出到输出缓冲区。

我想做的是按亮度顺序输出点(从最低到最高,反之亦然)。

金属有可能吗?有排序功能或库吗?我可以找到许多用于OpenGL的排序库,但是找不到用于Metal的排序库。

0 个答案:

没有答案