Move pipelineState into pixelPipeline
Move configuration to its own method
This commit is contained in:
parent
80d4e6f043
commit
3808463e55
1 changed files with 44 additions and 30 deletions
|
@ -34,41 +34,21 @@ public class Renderer: NSObject, MTKViewDelegate {
|
||||||
let view = delegate.metalView
|
let view = delegate.metalView
|
||||||
view.device = device
|
view.device = device
|
||||||
|
|
||||||
do {
|
configure(pixelPipelineWithView: view)
|
||||||
let bundle = Bundle(for: type(of: self))
|
|
||||||
let library = try device.makeDefaultLibrary(bundle: bundle)
|
|
||||||
let vertexShader = library.makeFunction(name: "passthroughVertexShader")
|
|
||||||
let fragmentShader = library.makeFunction(name: "sampleToColorShader")
|
|
||||||
|
|
||||||
let pipelineStateDescriptor = MTLRenderPipelineDescriptor()
|
try! delegate.field.setupMetal(withDevice: device)
|
||||||
pipelineStateDescriptor.label = "Render Pipeline"
|
|
||||||
pipelineStateDescriptor.vertexFunction = vertexShader
|
|
||||||
pipelineStateDescriptor.fragmentFunction = fragmentShader
|
|
||||||
if let renderAttachment = pipelineStateDescriptor.colorAttachments[0] {
|
|
||||||
renderAttachment.pixelFormat = view.colorPixelFormat
|
|
||||||
// Pulled all this from SO. I don't know what it means, but it makes the alpha channel work.
|
|
||||||
// TODO: Learn what this means???
|
|
||||||
// https://stackoverflow.com/q/43727335/1174185
|
|
||||||
renderAttachment.isBlendingEnabled = true
|
|
||||||
renderAttachment.alphaBlendOperation = .add
|
|
||||||
renderAttachment.rgbBlendOperation = .add
|
|
||||||
renderAttachment.sourceRGBBlendFactor = .sourceAlpha
|
|
||||||
renderAttachment.sourceAlphaBlendFactor = .sourceAlpha
|
|
||||||
renderAttachment.destinationRGBBlendFactor = .oneMinusSourceAlpha
|
|
||||||
renderAttachment.destinationAlphaBlendFactor = .oneMinusSourceAlpha
|
|
||||||
}
|
|
||||||
renderPipelineState = try device.makeRenderPipelineState(descriptor: pipelineStateDescriptor)
|
|
||||||
|
|
||||||
try delegate.field.setupMetal(withDevice: device)
|
|
||||||
} catch let e {
|
|
||||||
fatalError("\(e)")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var device: MTLDevice
|
private var device: MTLDevice
|
||||||
|
|
||||||
|
private lazy var library: MTLLibrary? = {
|
||||||
|
let bundle = Bundle(for: type(of: self))
|
||||||
|
return try? device.makeDefaultLibrary(bundle: bundle)
|
||||||
|
}()
|
||||||
|
|
||||||
private var commandQueue: MTLCommandQueue
|
private var commandQueue: MTLCommandQueue
|
||||||
private var renderPipelineState: MTLRenderPipelineState? = nil
|
private var pixelPipeline: MTLRenderPipelineState?
|
||||||
|
|
||||||
override public init() {
|
override public init() {
|
||||||
guard let device = MTLCreateSystemDefaultDevice() else {
|
guard let device = MTLCreateSystemDefaultDevice() else {
|
||||||
|
@ -89,6 +69,40 @@ public class Renderer: NSObject, MTKViewDelegate {
|
||||||
self.delegate = delegate
|
self.delegate = delegate
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func configure(pixelPipelineWithView view: MTKView) {
|
||||||
|
guard let library = library else {
|
||||||
|
fatalError("Couldn't get Metal library")
|
||||||
|
}
|
||||||
|
|
||||||
|
let vertexShader = library.makeFunction(name: "passthroughVertexShader")
|
||||||
|
let fragmentShader = library.makeFunction(name: "sampleToColorShader")
|
||||||
|
|
||||||
|
let pipelineDesc = MTLRenderPipelineDescriptor()
|
||||||
|
pipelineDesc.label = "Pixel Pipeline"
|
||||||
|
pipelineDesc.vertexFunction = vertexShader
|
||||||
|
pipelineDesc.fragmentFunction = fragmentShader
|
||||||
|
if let renderAttachment = pipelineDesc.colorAttachments[0] {
|
||||||
|
renderAttachment.pixelFormat = view.colorPixelFormat
|
||||||
|
// Pulled all this from SO. I don't know what it means, but it makes the alpha channel work.
|
||||||
|
// TODO: Learn what this means???
|
||||||
|
// https://stackoverflow.com/q/43727335/1174185
|
||||||
|
renderAttachment.isBlendingEnabled = true
|
||||||
|
renderAttachment.alphaBlendOperation = .add
|
||||||
|
renderAttachment.rgbBlendOperation = .add
|
||||||
|
renderAttachment.sourceRGBBlendFactor = .sourceAlpha
|
||||||
|
renderAttachment.sourceAlphaBlendFactor = .sourceAlpha
|
||||||
|
renderAttachment.destinationRGBBlendFactor = .oneMinusSourceAlpha
|
||||||
|
renderAttachment.destinationAlphaBlendFactor = .oneMinusSourceAlpha
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
pixelPipeline = try device.makeRenderPipelineState(descriptor: pipelineDesc)
|
||||||
|
} catch let e {
|
||||||
|
print("Couldn't set up pixel pipeline! \(e)")
|
||||||
|
pixelPipeline = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// MARK: - MTKViewDelegate
|
/// MARK: - MTKViewDelegate
|
||||||
|
|
||||||
public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
|
public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
|
||||||
|
@ -118,7 +132,7 @@ public class Renderer: NSObject, MTKViewDelegate {
|
||||||
var didEncode = false
|
var didEncode = false
|
||||||
|
|
||||||
if let renderPass = view.currentRenderPassDescriptor,
|
if let renderPass = view.currentRenderPassDescriptor,
|
||||||
let renderPipelineState = renderPipelineState,
|
let renderPipelineState = pixelPipeline,
|
||||||
let encoder = buffer.makeRenderCommandEncoder(descriptor: renderPass) {
|
let encoder = buffer.makeRenderCommandEncoder(descriptor: renderPass) {
|
||||||
encoder.label = "Render Pass"
|
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))
|
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