我刚刚开始在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")
// }
}
}
答案 0 :(得分:0)
我认为你需要:
device.newComputePipelineStateWithFunction(function) { (state:MTLComputePipelineState?, error:NSError?) in
}