diff --git a/ch10/chapter10.playground/Sources/MetalView.swift b/ch10/chapter10.playground/Sources/MetalView.swift index 393222e..879e690 100755 --- a/ch10/chapter10.playground/Sources/MetalView.swift +++ b/ch10/chapter10.playground/Sources/MetalView.swift @@ -14,13 +14,13 @@ public class MetalView: NSObject, MTKViewDelegate { func registerShaders() { device = MTLCreateSystemDefaultDevice()! - 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)") } @@ -30,8 +30,8 @@ public class MetalView: NSObject, MTKViewDelegate { public func draw(in view: MTKView) { if let drawable = view.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) let threadGroupCount = MTLSizeMake(8, 8, 1) diff --git a/ch10/chapter10.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch10/chapter10.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate index 4591e06..70c0b8b 100755 Binary files a/ch10/chapter10.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate and b/ch10/chapter10.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch11/chapter11.playground/Sources/MetalView.swift b/ch11/chapter11.playground/Sources/MetalView.swift index db61db1..24c4256 100755 --- a/ch11/chapter11.playground/Sources/MetalView.swift +++ b/ch11/chapter11.playground/Sources/MetalView.swift @@ -2,44 +2,44 @@ import MetalKit public class MetalView: NSObject, MTKViewDelegate { - - public var device: MTLDevice! - var queue: MTLCommandQueue! - var cps: MTLComputePipelineState! - - override public init() { - super.init() - registerShaders() + + public var device: MTLDevice! + var queue: MTLCommandQueue! + var cps: MTLComputePipelineState! + + override public init() { + super.init() + registerShaders() + } + + func registerShaders() { + device = MTLCreateSystemDefaultDevice()! + 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.makeLibrary(source: input, options: nil) + let kernel = library.makeFunction(name: "compute")! + cps = try device.makeComputePipelineState(function: kernel) + } catch let e { + Swift.print("\(e)") } - - func registerShaders() { - device = MTLCreateSystemDefaultDevice()! - queue = device.newCommandQueue() - let path = Bundle.main.pathForResource("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) - } catch let e { - Swift.print("\(e)") - } - } - - public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {} - - public func draw(in view: MTKView) { - if let drawable = view.currentDrawable { - let commandBuffer = queue.commandBuffer() - let commandEncoder = commandBuffer.computeCommandEncoder() - commandEncoder.setComputePipelineState(cps) - commandEncoder.setTexture(drawable.texture, at: 0) - let threadGroupCount = MTLSizeMake(8, 8, 1) - let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1) - commandEncoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount) - commandEncoder.endEncoding() - commandBuffer.present(drawable) - commandBuffer.commit() - } + } + + public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {} + + public func draw(in view: MTKView) { + if let drawable = view.currentDrawable { + let commandBuffer = queue.makeCommandBuffer() + let commandEncoder = commandBuffer.makeComputeCommandEncoder() + commandEncoder.setComputePipelineState(cps) + commandEncoder.setTexture(drawable.texture, at: 0) + let threadGroupCount = MTLSizeMake(8, 8, 1) + let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1) + commandEncoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount) + commandEncoder.endEncoding() + commandBuffer.present(drawable) + commandBuffer.commit() } + } } diff --git a/ch11/chapter11.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch11/chapter11.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate index 81d391f..d18e907 100755 Binary files a/ch11/chapter11.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate and b/ch11/chapter11.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ