diff --git a/ch14/chapter14.playground/Resources/Shaders.metal b/ch14/chapter14.playground/Resources/Shaders.metal index 946c689..cddf12f 100755 --- a/ch14/chapter14.playground/Resources/Shaders.metal +++ b/ch14/chapter14.playground/Resources/Shaders.metal @@ -33,8 +33,7 @@ float fbm(float2 uv) } kernel void compute(texture2d output [[texture(0)]], - constant float2 &mouse [[buffer(0)]], - constant float &timer [[buffer(1)]], + constant float &timer [[buffer(0)]], uint2 gid [[thread_position_in_grid]]) { int width = output.get_width(); diff --git a/ch14/chapter14.playground/Sources/MetalView.swift b/ch14/chapter14.playground/Sources/MetalView.swift index 2a6bdcb..c53b382 100755 --- a/ch14/chapter14.playground/Sources/MetalView.swift +++ b/ch14/chapter14.playground/Sources/MetalView.swift @@ -7,15 +7,6 @@ public class MetalView: MTKView, NSWindowDelegate { var cps: MTLComputePipelineState! = nil var timer: Float = 0 var timerBuffer: MTLBuffer! - var mouseBuffer: MTLBuffer! - var pos: NSPoint! - - override public func mouseDown(_ event: NSEvent) { - pos = convertToLayer(convert(event.locationInWindow, from: nil)) - let scale = layer!.contentsScale - pos.x *= scale - pos.y *= scale - } required public init(coder: NSCoder) { super.init(coder: coder) @@ -28,12 +19,11 @@ public class MetalView: MTKView, NSWindowDelegate { override public func draw(_ dirtyRect: NSRect) { 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) - commandEncoder.setBuffer(timerBuffer, offset: 0, at: 1) + commandEncoder.setBuffer(timerBuffer, offset: 0, at: 0) update() let threadGroupCount = MTLSizeMake(8, 8, 1) let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1) @@ -47,24 +37,21 @@ public class MetalView: MTKView, NSWindowDelegate { func update() { timer += 0.01 - var bufferPointer = timerBuffer.contents() - memcpy(bufferPointer, &timer, sizeof(Float.self)) - bufferPointer = mouseBuffer.contents() - memcpy(bufferPointer, &pos, sizeof(NSPoint.self)) + let bufferPointer = timerBuffer.contents() + memcpy(bufferPointer, &timer, MemoryLayout.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.size, options: []) } } diff --git a/ch14/chapter14.playground/contents.xcplayground b/ch14/chapter14.playground/contents.xcplayground index 06828af..9f9eecc 100644 --- a/ch14/chapter14.playground/contents.xcplayground +++ b/ch14/chapter14.playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/ch14/chapter14.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch14/chapter14.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate index bbb890a..dfb49fd 100755 Binary files a/ch14/chapter14.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate and b/ch14/chapter14.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch14/chapter14.playground/timeline.xctimeline b/ch14/chapter14.playground/timeline.xctimeline deleted file mode 100644 index bf468af..0000000 --- a/ch14/chapter14.playground/timeline.xctimeline +++ /dev/null @@ -1,6 +0,0 @@ - - - - -