简体   繁体   中英

iOS Metal Swift newComputePipelineStateWithFunction not working error

I've just started playing around with Metal in Swift to try and get some parallel computing going on the GPU, and I'm having some trouble specifically with the newComputePipelineStateWithFunction. I've looked at multiple sites like Apple's Documentation for Data-Parallel Compute Processing but I'm getting errors there too.

Here's the error I'm currently getting:

Incorrect argument label in call (have '_:error:', expected '_:completionHandler:')

I tried replacing the "error" with a completion handler but was having difficulty there too. Thanks in advance. Here's my code:

import UIKit
import Metal

class ViewController: UIViewController {

    var device: MTLDevice! = nil
    var defaultLibrary: MTLLibrary! = nil
    var thefunc: MTLFunction! = nil
    var pipelineState: MTLComputePipelineState!
    var commandQueue: MTLCommandQueue! = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        setupMetal()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    func setupMetal()
    {
        // Our default MTLDevice
        device = MTLCreateSystemDefaultDevice()
        defaultLibrary = device.newDefaultLibrary()
        commandQueue = device.newCommandQueue()

        // Define the kernel function
        let kernelFunction: MTLFunction = defaultLibrary.newFunctionWithName("particleRendererShader")!

        // Define the pipeline state
        let pipelineState: MTLComputePipelineState = device.newComputePipelineStateWithFunction(kernelFunction, error: nil)

        // Define the command buffer
        let commandBuffer: MTLCommandBuffer = commandQueue.commandBuffer()

        // Define the command encoder
        let commandEncoder: MTLComputeCommandEncoder = commandBuffer.computeCommandEncoder()
        commandEncoder.setComputePipelineState(pipelineState)



        // thefunc = library.newFunctionWithName("filter_main");
        // filterState = device.newComputePipelineStateWithFunction(thefunc, error: nil);
        // let kernelFunction = library.newFunctionWithName("filter_main")
        // pipelineState = device.newComputePipelineStateWithFunction(kernelFunction!, error: nil)



        // do {
        //     try pipelineState = device.newComputePipelineStateWithDescriptor(pipelineStateDescriptor)
        // } catch _ {
        //     print("Failed to create pipeline state, error")
        // }

    }

}

I think you need:

device.newComputePipelineStateWithFunction(function) { (state:MTLComputePipelineState?, error:NSError?) in

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM