【发布时间】:2023-02-08 18:19:20
【问题描述】:
我在我的项目中使用 Metal,并且我将一些内核封装为函数,与 MetalPerformanceShaders 建议的方式相同。
所以我的每个 Metal 内核都有带有以下方法的 Objective-C 类:
- (void)encodeToCommandBuffer:(id<MTLCommandBuffer>)cmdBuffer
inputTexture:(id<MTLTexture>)inputTexture
outputTexture:(id<MTLTexture>)outputTexture
inputSize:(TextureSize)inputSize
outputSize:(TextureSize)outputSize
{
id<MTLComputeCommandEncoder> enc = [cmdBuffer computeCommandEncoder];
[enc setComputePipelineState:_state];
//set arguments to the state
[enc dispatchThreadgroups:_threadgroupsPerGrid threadsPerThreadgroup:_threadsPerThreadgroup];
[enc endEncoding];
}
问题是我的代码因断言而崩溃:
failed assertion A command encoder is already encoding to this command buffer
问题是随机的,发生在不同的功能上。错误描述是不言自明的,但我很好奇 - 我的encodeToCommandBuffer 方法发生崩溃。在管道中,我还使用来自 MetalPerformanceShaders 的图像处理函数,这些函数也通过 encodeToCommandBuffer 方法调用,并且不会崩溃。
所以很明显,我对encodeToCommandBuffer方法应该如何编写的理解是错误的。我需要如何修改代码?我需要以某种方式检查 cmdBuffer 状态吗?它已准备好生产新的编码器。如果不是呢?我是否需要某种 while 循环来等待缓冲区准备好?
【问题讨论】:
-
现实情况是,您需要以一种不会将带有未完成编码器的命令缓冲区传递给
encodeToCommandBuffer之类的函数的方式组织代码
标签: objective-c gpu assert metal metal-performance-shaders