import MetalKit
enum Colors { static let wenderlichGreen = MTLClearColor(red: 0.0, green: 0.4, blue: 0.21, alpha: 1.0) }
class Renderer: NSObject { let device: MTLDevice let commandQueue: MTLCommandQueue var vertices: [Float] = [ -1, 1, 0, -1, -1, 0, 1, -1, 0, 1, 1, 0, ] let indices: [UInt16] = [ 0, 1, 2, 2, 3, 0 ] var pipelineState: MTLRenderPipelineState? var vertexBuffer: MTLBuffer? var indexBuffer: MTLBuffer?
struct Constants { var animateBy: Float = 0 } var constants = Constants() var time: Float = 0
init(device: MTLDevice) { self.device = device commandQueue = device.makeCommandQueue()! super.init() buildModel() buildPipelineState() }
private func buildModel() { vertexBuffer = device.makeBuffer(bytes: vertices, length: vertices.count * MemoryLayout<Float>.size, options: []) indexBuffer = device.makeBuffer(bytes: indices, length: indices.count * MemoryLayout<UInt16>.size, options: []) }
private func buildPipelineState() { let library = device.makeDefaultLibrary() let vertexFunction = library?.makeFunction(name: "vertex_shader") let fragmentFunction = library?.makeFunction(name: "fragment_shader")
let pipelineDescriptor = MTLRenderPipelineDescriptor() pipelineDescriptor.vertexFunction = vertexFunction pipelineDescriptor.fragmentFunction = fragmentFunction pipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
do { pipelineState = try device.makeRenderPipelineState(descriptor: pipelineDescriptor) } catch let error as NSError { print("error: \(error.localizedDescription)") } } }
extension Renderer: MTKViewDelegate { func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) { } func draw(in view: MTKView) { guard let drawable = view.currentDrawable, let pipelineState = pipelineState, let indexBuffer = indexBuffer, let descriptor = view.currentRenderPassDescriptor else { return }
time += 1 / Float(view.preferredFramesPerSecond)
let animateBy = abs(sin(time) / 2 + 0.5) constants.animateBy = animateBy
let commandBuffer = commandQueue.makeCommandBuffer() let commandEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: view.currentRenderPassDescriptor!)
commandEncoder?.setRenderPipelineState(pipelineState) commandEncoder?.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
commandEncoder?.setVertexBytes(&constants, length: MemoryLayout<Constants>.stride, index: 1)
commandEncoder?.drawIndexedPrimitives(type: .triangle, indexCount: indices.count, indexType: .uint16, indexBuffer: indexBuffer, indexBufferOffset: 0)
commandEncoder?.endEncoding() commandBuffer?.present(view.currentDrawable!) commandBuffer?.commit() } }
|