Skip to content

Commit

Permalink
updated to Swift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
mhorga committed Nov 29, 2016
1 parent 2fa2654 commit 7991e3b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
Binary file not shown.
24 changes: 12 additions & 12 deletions ch12/chapter12.playground/Sources/MetalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class MetalView: MTKView, NSWindowDelegate {
var mouseBuffer: MTLBuffer!
var pos: NSPoint!

override public func mouseDown(_ event: NSEvent) {
override public func mouseDown(with event: NSEvent) {
pos = convertToLayer(convert(event.locationInWindow, from: nil))
let scale = layer!.contentsScale
pos.x *= scale
Expand All @@ -29,8 +29,8 @@ public class MetalView: MTKView, NSWindowDelegate {
override public func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
if let drawable = currentDrawable {
let commandBuffer = queue.commandBuffer()
let commandEncoder = commandBuffer.computeCommandEncoder()
let commandBuffer = queue.makeCommandBuffer()
let commandEncoder = commandBuffer.makeComputeCommandEncoder()
commandEncoder.setComputePipelineState(cps)
commandEncoder.setTexture(drawable.texture, at: 0)
commandEncoder.setBuffer(mouseBuffer, offset: 0, at: 2)
Expand All @@ -49,23 +49,23 @@ public class MetalView: MTKView, NSWindowDelegate {
func update() {
timer += 0.01
var bufferPointer = timerBuffer.contents()
memcpy(bufferPointer, &timer, sizeof(Float.self))
memcpy(bufferPointer, &timer, MemoryLayout<Float>.size)
bufferPointer = mouseBuffer.contents()
memcpy(bufferPointer, &pos, sizeof(NSPoint.self))
memcpy(bufferPointer, &pos, MemoryLayout<NSPoint>.size)
}

func registerShaders() {
queue = device!.newCommandQueue()
let path = Bundle.main.pathForResource("Shaders", ofType: "metal")
queue = device!.makeCommandQueue()
let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")
do {
let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8)
let library = try device!.newLibrary(withSource: input, options: nil)
let kernel = library.newFunction(withName: "compute")!
cps = try device!.newComputePipelineState(with: kernel)
let library = try device!.makeLibrary(source: input, options: nil)
let kernel = library.makeFunction(name: "compute")!
cps = try device!.makeComputePipelineState(function: kernel)
} catch let e {
Swift.print("\(e)")
}
timerBuffer = device!.newBuffer(withLength: sizeof(Float.self), options: [])
mouseBuffer = device!.newBuffer(withLength: sizeof(NSPoint.self), options: [])
timerBuffer = device!.makeBuffer(length: MemoryLayout<Float>.size, options: [])
mouseBuffer = device!.makeBuffer(length: MemoryLayout<NSPoint>.size, options: [])
}
}
Binary file not shown.

0 comments on commit 7991e3b

Please sign in to comment.