updated to Swift 3

This commit is contained in:
Marius Horga 2016-11-30 00:25:19 +02:00
parent dfc0532882
commit 189b8b986c
2 changed files with 10 additions and 10 deletions

View File

@ -13,42 +13,42 @@ public class MetalView: NSObject, MTKViewDelegate {
override public init() { override public init() {
super.init() super.init()
device = MTLCreateSystemDefaultDevice()! device = MTLCreateSystemDefaultDevice()!
queue = device!.newCommandQueue() queue = device!.makeCommandQueue()
setUpTexture() setUpTexture()
registerShaders() registerShaders()
} }
func setUpTexture() { func setUpTexture() {
let path = Bundle.main.pathForResource("texture", ofType: "jpg") let path = Bundle.main.path(forResource: "texture", ofType: "jpg")
let textureLoader = MTKTextureLoader(device: device!) let textureLoader = MTKTextureLoader(device: device!)
texture = try! textureLoader.newTexture(withContentsOf: URL(fileURLWithPath: path!), options: nil) texture = try! textureLoader.newTexture(withContentsOf: URL(fileURLWithPath: path!), options: nil)
} }
func registerShaders() { func registerShaders() {
let path = Bundle.main.pathForResource("Shaders", ofType: "metal") let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")
do { do {
let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8) let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8)
let library = try device!.newLibrary(withSource: input, options: nil) let library = try device!.makeLibrary(source: input, options: nil)
let kernel = library.newFunction(withName: "compute")! let kernel = library.makeFunction(name: "compute")!
cps = try device!.newComputePipelineState(with: kernel) cps = try device!.makeComputePipelineState(function: kernel)
} catch let e { } catch let e {
Swift.print("\(e)") Swift.print("\(e)")
} }
timerBuffer = device!.newBuffer(withLength: sizeof(Float.self), options: []) timerBuffer = device!.makeBuffer(length: MemoryLayout<Float>.size, options: [])
} }
func update() { func update() {
timer += 0.01 timer += 0.01
let bufferPointer = timerBuffer.contents() let bufferPointer = timerBuffer.contents()
memcpy(bufferPointer, &timer, sizeof(Float.self)) memcpy(bufferPointer, &timer, MemoryLayout<Float>.size)
} }
public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {} public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {}
public func draw(in view: MTKView) { public func draw(in view: MTKView) {
if let drawable = view.currentDrawable { if let drawable = view.currentDrawable {
let commandBuffer = queue.commandBuffer() let commandBuffer = queue.makeCommandBuffer()
let commandEncoder = commandBuffer.computeCommandEncoder() let commandEncoder = commandBuffer.makeComputeCommandEncoder()
commandEncoder.setComputePipelineState(cps) commandEncoder.setComputePipelineState(cps)
commandEncoder.setTexture(drawable.texture, at: 0) commandEncoder.setTexture(drawable.texture, at: 0)
commandEncoder.setTexture(texture, at: 1) commandEncoder.setTexture(texture, at: 1)