【发布时间】:2019-09-25 17:54:43
【问题描述】:
我的 Metal Swift (iOS/MacOS) 项目中有一个渲染器类,它是一个 MTKViewDelegate。我使用 MTLCreateSystemDefaultDevice() 提取 MTLDevice,但是在初始化之后,它变成了 nil?我想知道我是否在这里错过了 Swift 或 Metal 的一个怪癖。代码大致是这样的,
class Renderer: NSObject, MTKViewDelegate {
var device: MTLDevice!
init(metalView: MTKView) {
guard let device = MTLCreateSystemDefaultDevice() else
{
fatalError("GPU not available")
}
metalView.device = device
if device != nil {
print (“device not nil”)
}
}
func draw(in view: MTKView) {
if device == nil {
print (“device is nil here”)
}
}
}
在我的 ViewController 我做
guard let metalView = view as? MTKView else {
fatalError("Metal View not setup")
}
renderer = Renderer(metalView: metalView)
我看到的是:
device not nil
device is nil here
device is nil here
device is nil here
device is nil here
每次绘制调用时为 60hz
编辑:编辑代码以明确设备实际上被分配给全局范围 (metalView) 中的变量。
【问题讨论】: