【问题标题】:iOS Metal Swift newComputePipelineStateWithFunction not working erroriOS Metal Swift newComputePipelineStateWithFunction 不工作错误
【发布时间】:2016-10-02 23:24:58
【问题描述】:

我刚刚开始在 Swift 中使用 Metal 以尝试在 GPU 上进行一些并行计算,但我在使用 newComputePipelineStateWithFunction 时遇到了一些问题。我查看了多个站点,例如 Apple's Documentation for Data-Parallel Compute Processing,但我也遇到了错误。

这是我目前遇到的错误:

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

我尝试用完成处理程序替换“错误”,但在那里也遇到了困难。提前致谢。这是我的代码:

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")
        // }

    }

}

【问题讨论】:

  • Objective-C newComputePipelineStateWithFunction 接受 error 参数,Swift 版本不接受。例如; try pipelineState = device.newComputePipelineStateWithFunction(kernelFunction) 应该可以工作。
  • 感谢您的评论,但我收到错误“未处理从此处抛出的错误”。
  • 是的,所以不要接受 NSError 作为输入参数,在调用方法后应该检查它,一种更快捷的做事方式是抛出异常。您需要将其包装在 do {} catch {} 中,就像您注释掉的代码所做的那样。
  • 我认为这行得通,谢谢!!!!

标签: ios swift gpu metal


【解决方案1】:

我认为你需要:

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

    }

【讨论】:

  • 这是计算管道创建方法的异步变体之一。要使用它,您需要在编码任何计算工作之前等待调用完成处理程序(这里写为尾随闭包)。
猜你喜欢
  • 2015-12-31
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 2020-05-18
  • 2015-11-18
  • 1970-01-01
  • 2018-12-06
相关资源
最近更新 更多