[kit] Shuffle things around so delegate doesn't have to be given in init()
Classes that use the Renderer might not be ready to assign themselves as delegate when the Renderer is initialized
This commit is contained in:
parent
43caa7d844
commit
6b663f92e8
1 changed files with 52 additions and 36 deletions
|
@ -25,25 +25,16 @@ struct Vertex {
|
|||
}
|
||||
|
||||
public class Renderer: NSObject, MTKViewDelegate {
|
||||
var delegate: RendererDelegate
|
||||
|
||||
private var device: MTLDevice
|
||||
private var commandQueue: MTLCommandQueue
|
||||
private var renderPipelineState: MTLRenderPipelineState
|
||||
|
||||
public init(delegate: RendererDelegate) throws {
|
||||
self.delegate = delegate
|
||||
|
||||
guard let device = MTLCreateSystemDefaultDevice() else {
|
||||
throw RendererError.MetalError("Unable to create Metal system device")
|
||||
public var delegate: RendererDelegate? = nil {
|
||||
didSet {
|
||||
guard let delegate = delegate else {
|
||||
return
|
||||
}
|
||||
let view = delegate.metalView
|
||||
|
||||
self.device = device
|
||||
let view = delegate.metalView
|
||||
view.device = device
|
||||
|
||||
try delegate.field.setupMetal(withDevice: device)
|
||||
|
||||
do {
|
||||
let library = try device.makeDefaultLibrary(bundle: Bundle.main)
|
||||
let vertexShader = library.makeFunction(name: "passthroughVertexShader")
|
||||
let fragmentShader = library.makeFunction(name: "sampleToColorShader")
|
||||
|
@ -66,18 +57,43 @@ public class Renderer: NSObject, MTKViewDelegate {
|
|||
}
|
||||
renderPipelineState = try device.makeRenderPipelineState(descriptor: pipelineStateDescriptor)
|
||||
|
||||
try delegate.field.setupMetal(withDevice: device)
|
||||
} catch let e {
|
||||
fatalError("\(e)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var device: MTLDevice
|
||||
private var commandQueue: MTLCommandQueue
|
||||
private var renderPipelineState: MTLRenderPipelineState? = nil
|
||||
|
||||
override public init() {
|
||||
guard let device = MTLCreateSystemDefaultDevice() else {
|
||||
fatalError("Unable to create Metal system device")
|
||||
}
|
||||
|
||||
self.device = device
|
||||
commandQueue = device.makeCommandQueue()
|
||||
|
||||
super.init()
|
||||
}
|
||||
|
||||
public convenience init(delegate: RendererDelegate) throws {
|
||||
self.init()
|
||||
self.delegate = delegate
|
||||
}
|
||||
|
||||
/// MARK: - MTKViewDelegate
|
||||
|
||||
public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
|
||||
delegate.renderSize = Size(size: size)
|
||||
delegate?.renderSize = Size(size: size)
|
||||
}
|
||||
|
||||
public func draw(in view: MTKView) {
|
||||
let field = delegate.field
|
||||
guard let field = delegate?.field else {
|
||||
return
|
||||
}
|
||||
|
||||
// Two triangles, plus texture coordinates.
|
||||
let points: [Vertex] = [
|
||||
|
@ -95,7 +111,7 @@ public class Renderer: NSObject, MTKViewDelegate {
|
|||
let buffer = commandQueue.makeCommandBuffer()
|
||||
buffer.label = "Render"
|
||||
|
||||
if let renderPass = view.currentRenderPassDescriptor {
|
||||
if let renderPass = view.currentRenderPassDescriptor, let renderPipelineState = renderPipelineState {
|
||||
let encoder = buffer.makeRenderCommandEncoder(descriptor: renderPass)
|
||||
encoder.label = "Render Pass"
|
||||
encoder.setViewport(MTLViewport(originX: 0.0, originY: 0.0, width: Double(view.drawableSize.width), height: Double(view.drawableSize.height), znear: -1.0, zfar: 1.0))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue