【问题标题】:Metal assertion `A command encoder is already encoding to this command buffer`金属断言`命令编码器已经编码到这个命令缓冲区`
【发布时间】: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


【解决方案1】:

好的,整理好了。我的管道正在并行处理多个实例,我在代码中犯了一个错误 - 管道试图通过同一个命令缓冲区处理所有实例,这不是故意的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多