MetalKit - Drawable.texture 断言错误

MetalKit - Drawable.texture assertion error

我是 MetalKit 的新手,正在尝试将 this tutorialplayground 转换回 OSX app:

    import MetalKit

public class MetalView: MTKView {

    var queue: MTLCommandQueue! = nil
    var cps: MTLComputePipelineState! = nil

    required public init(coder: NSCoder) {
        super.init(coder: coder)
        device = MTLCreateSystemDefaultDevice()
        registerShaders()
    }


    override public func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
        if let drawable = currentDrawable {
            let command_buffer = queue.commandBuffer()
            let command_encoder = command_buffer.computeCommandEncoder()
            command_encoder.setComputePipelineState(cps)
            command_encoder.setTexture(drawable.texture, atIndex: 0)
            let threadGroupCount = MTLSizeMake(8, 8, 1)
            let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1)
            command_encoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount)
            command_encoder.endEncoding()
            command_buffer.presentDrawable(drawable)
            command_buffer.commit()
        }
    }

    func registerShaders() {
        queue = device!.newCommandQueue()
        do {
            let library = device!.newDefaultLibrary()!
            let kernel = library.newFunctionWithName("compute")!
            cps = try device!.newComputePipelineStateWithFunction(kernel)
        } catch let e {
            Swift.print("\(e)")
        }
    }
}

我在以下行遇到错误:

command_encoder.setTexture(drawable.texture, atIndex: 0)

failed assertion `frameBufferOnly texture not supported for compute.'

我该如何解决这个问题?

如果您想从计算函数写入可绘制对象的纹理,您需要告诉 MTKView 它应该将其层配置为仅帧缓冲区:

metalView.framebufferOnly = false

将此值设置为 false,您的可绘制对象将为您提供设置了 shaderWrite 使用标志的纹理,这是从着色器函数编写纹理时所必需的。

即使我将 frameBufferOnly 设置为 false 并在使用 drawable 之前打印布尔值以进行双重检查,我也有同样的问题。

但是当我关闭 GPU frame capture 时,不会再出现断言(至少现在是这样)。还是不知道为什么。

Menu > Scheme > Edit Scheme > Run > Options > GPU Frame Capture: 禁用