Search code examples
swiftmetalmtkview

MTKView : Type of expression is ambiguous without more context on commandBuffer.present


I am getting compile time error while setting drawable on command buffer object.

Following is my function which is being called from draw method of MTKView subclass.

fileprivate func executeMetalShader() {
        guard let pipelineState = self.pipelineState,
            let threadgroupsPerGrid = self.threadgroupsPerGrid,
            let threadsPerThreadgroup = self.threadsPerThreadgroup
        else { return }

        guard let drawable: CAMetalDrawable = self.currentDrawable else { fatalError("Failed to create drawable") }
        let commandBuffer = commandQueue?.makeCommandBuffer()
        let commandEncoder = commandBuffer?.makeComputeCommandEncoder()
        commandEncoder?.setComputePipelineState(pipelineState)

        commandEncoder?.setTexture(yTexture, index: 0)
        commandEncoder?.setTexture(uTexture, index: 1)
        commandEncoder?.setTexture(vTexture, index: 2)
        commandEncoder?.setTexture(outTexture, index: 3)

        commandEncoder?.dispatchThreadgroups(threadgroupsPerGrid,
                                             threadsPerThreadgroup: threadsPerThreadgroup)
        commandEncoder?.endEncoding()
        commandBuffer.present(drawable) //. Error: Type of expression is ambiguous without more context
        commandBuffer?.commit()
        print("shader execution finish")
    }

Is there anything wrong I am doing or some API changes is there in Swift 5?


Solution

  • I think you're missing a ?. Fix:

    commandBuffer?.present(drawable)