migrate to Swift3 and Xcode 8.1

This commit is contained in:
chenyungui 2016-10-31 18:50:11 +08:00
parent 4a466f831e
commit 218973aaa1
10 changed files with 22 additions and 22 deletions

View File

@ -10,7 +10,7 @@ public class MetalView: MTKView, NSWindowDelegate {
var mouseBuffer: MTLBuffer! var mouseBuffer: MTLBuffer!
var pos: NSPoint! var pos: NSPoint!
override public func mouseDown(_ event: NSEvent) { override public func mouseDown(with event: NSEvent) {
pos = convertToLayer(convert(event.locationInWindow, from: nil)) pos = convertToLayer(convert(event.locationInWindow, from: nil))
let scale = layer!.contentsScale let scale = layer!.contentsScale
pos.x *= scale pos.x *= scale
@ -28,8 +28,8 @@ public class MetalView: MTKView, NSWindowDelegate {
override public func draw(_ dirtyRect: NSRect) { override public func draw(_ dirtyRect: NSRect) {
if let drawable = currentDrawable { if let drawable = 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.setBuffer(mouseBuffer, offset: 0, at: 2) commandEncoder.setBuffer(mouseBuffer, offset: 0, at: 2)
@ -48,23 +48,23 @@ public class MetalView: MTKView, NSWindowDelegate {
func update() { func update() {
timer += 0.01 timer += 0.01
var bufferPointer = timerBuffer.contents() var bufferPointer = timerBuffer.contents()
memcpy(bufferPointer, &timer, sizeof(Float.self)) memcpy(bufferPointer, &timer, MemoryLayout<Float>.size)
bufferPointer = mouseBuffer.contents() bufferPointer = mouseBuffer.contents()
memcpy(bufferPointer, &pos, sizeof(NSPoint.self)) memcpy(bufferPointer, &pos, MemoryLayout<NSPoint>.size)
} }
func registerShaders() { func registerShaders() {
queue = device!.newCommandQueue() queue = device!.makeCommandQueue()
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: [])
mouseBuffer = device!.newBuffer(withLength: sizeof(NSPoint.self), options: []) mouseBuffer = device!.makeBuffer(length: MemoryLayout<NSPoint>.size, options: [])
} }
} }

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)