Metal makeComputeCommandEncoder断言失败

8

我正在尝试设置并执行计算内核,并将其输出提交给MTKView以进行绘制。但是我遇到了以下崩溃:

 -[MTLDebugCommandBuffer computeCommandEncoder]:889: failed assertion `encoding in progress'

下面的代码有什么问题?将计算着色器的输出馈送到渲染管道是否不支持使用同一命令缓冲区?
func computeKernel(_ texture:MTLTexture, commandBuffer:MTLCommandBuffer) {
    let computeEncoder = commandBuffer.makeComputeCommandEncoder()
    computeEncoder?.setComputePipelineState(computePipelineState!)

    computeEncoder?.setTexture(texture, index: 0)
    computeEncoder?.setTexture(texture, index: 1)
    computeEncoder?.dispatchThreadgroups(threadgroupCount, threadsPerThreadgroup: threadgroupSize)
    computeEncoder?.endEncoding()

    /*
    commandBuffer.commit()
    commandBuffer.waitUntilCompleted()
     */
}


   override func draw(_ rect: CGRect) {


    guard let drawable = currentDrawable,
        let currentRenderPassDescriptor = currentRenderPassDescriptor
     else {
            return
    }



    // Set up command buffer and encoder
    guard let commandQueue = commandQueue else {
        print("Failed to create Metal command queue")
        return
    }

    guard let commandBuffer = commandQueue.makeCommandBuffer() else {
        print("Failed to create Metal command buffer")
        return
    }

    guard let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: currentRenderPassDescriptor) else {
        print("Failed to create Metal command encoder")
        return
    }


    commandEncoder.label = "Preview display"

    let texture = ... //Grab a Metal texture
    computeKernel(texture, commandBuffer: commandBuffer)
    commandEncoder.setRenderPipelineState(defaultRenderPipelineState!)


    commandEncoder.setFragmentTexture(texture, index: 0)
    commandEncoder.setVertexBytes(vertices, length: vertices.count * MemoryLayout<AAPLVertex>.stride, index: 0)
    commandEncoder.drawPrimitives(type: .triangleStrip, vertexStart: 0, vertexCount: 4)
    commandEncoder.endEncoding()

    commandBuffer.present(drawable) // Draw to the screen
    commandBuffer.commit()
}
1个回答

14

你可以将计算和渲染工作编码到同一个命令缓冲区中,但当一个现有的命令编码器在编码时,你无法启动另一个命令编码器。在你的情况下,你创建了渲染命令编码器,然后调用一个创建计算命令编码器的函数,而不结束渲染命令编码器。相反,你应该先调用计算函数,然后创建并使用渲染命令编码器。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接