【发布时间】: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 {}中,就像您注释掉的代码所做的那样。 -
我认为这行得通,谢谢!!!!