金属内核函数缺少缓冲区绑定随时间变化?

问题描述 投票:0回答:2

我开发了一个iOS金属相机应用程序,但有一个错误:

Compute Function(kernel_function): missing buffer binding at index 0 for timeDelta[0]

内核代码如下:

kernel void kernel_function(
         texture2d<float, access::sample> inTexture [[texture(0)]],
         texture2d<float, access::write> outTexture [[texture(1)]],
         const device float *timeDelta [[buffer(0)]],
         uint2 gid [[thread_position_in_grid]],
         uint2 tpg [[threads_per_grid]])
{

    float time = timeDelta[0];
    .......

似乎问题是timeDelta错过缓冲区绑定。如果我删除timeDelta[0]并设置

float time = 1.0

没有错误,应用程序可以顺利运行。但屏幕效果是固定的图片而不是动画。因此,timeDelta是让效果随时间变化成为视频。有没有人知道如何在内核函数上应用时间或在iOS Metal中绑定timeDelta缓冲区来解决错误?非常感谢。

ios swift metal
2个回答
1
投票

在您的应用代码中,您没有在setBuffer()上调用setBytes()MTLComputeCommandEncoder,索引为0。您的应用程序未向着色器提供所需的缓冲区。

顺便说一句,你应该使用constant地址空间为timeDelta,而不是device。此外,假设只有一个值,请不要使用数组语法,请使用引用语法。所以:

     constant float &timeDelta [[buffer(0)]],

并直接在代码中使用timeDelta。 (不需要[0]或宣布本地副本,time。)


0
投票

感谢Ken Thomases的回答,你帮了我很多忙。我已经通过添加代码解决了这个问题

computeEncoder.setBytes(&timing, length: MemoryLayout<Float>.size, index: 0)

并且timing是一个随机浮点数,每次都可以更改computeEncoder的字节,以便根据需要创建动画。希望我的问题可以帮助那些有同样问题的人。感谢大家。

© www.soinside.com 2019 - 2024. All rights reserved.