diff --git a/ARKit/arkitdemo.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ARKit/arkitdemo.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate index ce58460..10da66e 100644 Binary files a/ARKit/arkitdemo.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate and b/ARKit/arkitdemo.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/README.md b/README.md index 5241289..b5b05f6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Metal Repository to accompany the following blog posts: + - [Introducing the Metal framework](http://metalkit.org/2016/01/04/introducing-the-metal-framework.html) - [Using MetalKit part 1](http://metalkit.org/2016/01/11/using-metalkit-part-1.html) - [Using MetalKit part 2](http://metalkit.org/2016/01/18/using-metalkit-part-2.html) diff --git a/ambient_occlusion/ao.playground/Sources/MetalView.swift b/ambient_occlusion/ao.playground/Sources/MetalView.swift index bf69602..d4bd02e 100644 --- a/ambient_occlusion/ao.playground/Sources/MetalView.swift +++ b/ambient_occlusion/ao.playground/Sources/MetalView.swift @@ -38,12 +38,12 @@ public class MetalView: NSObject, MTKViewDelegate { 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() + if let drawable = view.currentDrawable, + let commandBuffer = queue.makeCommandBuffer(), + let commandEncoder = commandBuffer.makeComputeCommandEncoder() { commandEncoder.setComputePipelineState(cps) - commandEncoder.setTexture(drawable.texture, at: 0) - commandEncoder.setBuffer(timerBuffer, offset: 0, at: 0) + commandEncoder.setTexture(drawable.texture, index: 0) + commandEncoder.setBuffer(timerBuffer, offset: 0, index: 0) update() let threadGroupCount = MTLSizeMake(8, 8, 1) let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1) diff --git a/ambient_occlusion/ao.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ambient_occlusion/ao.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate index 501a0aa..ecd30a0 100644 Binary files a/ambient_occlusion/ao.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate and b/ambient_occlusion/ao.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch01/chapter01.xcodeproj/project.pbxproj b/ch01/chapter01.xcodeproj/project.pbxproj index 2d19679..b1fbd4e 100644 --- a/ch01/chapter01.xcodeproj/project.pbxproj +++ b/ch01/chapter01.xcodeproj/project.pbxproj @@ -93,12 +93,12 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0810; + LastUpgradeCheck = 0900; ORGANIZATIONNAME = "Marius Horga"; TargetAttributes = { 29400C781C3AEE1300345568 = { CreatedOnToolsVersion = 7.2; - LastSwiftMigration = 0800; + LastSwiftMigration = 0900; }; }; }; @@ -163,14 +163,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -209,14 +215,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -247,7 +259,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter01; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -260,7 +273,8 @@ PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter01; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Release; }; diff --git a/ch01/chapter01.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ch01/chapter01.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..d52983f Binary files /dev/null and b/ch01/chapter01.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch01/chapter01.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist b/ch01/chapter01.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..7b928cf --- /dev/null +++ b/ch01/chapter01.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + chapter01.xcscheme + + orderHint + 0 + + + + diff --git a/ch01/chapter01/ViewController.swift b/ch01/chapter01/ViewController.swift index 994db6f..129eef5 100644 --- a/ch01/chapter01/ViewController.swift +++ b/ch01/chapter01/ViewController.swift @@ -23,7 +23,7 @@ class ViewController: NSViewController { } label.stringValue = "Your system has the following GPU(s):\n" for device in devices { - label.stringValue += "\(device.name!)\n" + label.stringValue += "\(device.name)\n" } } } diff --git a/ch02/chapter02.xcodeproj/project.pbxproj b/ch02/chapter02.xcodeproj/project.pbxproj index 98f74a4..10e92f9 100644 --- a/ch02/chapter02.xcodeproj/project.pbxproj +++ b/ch02/chapter02.xcodeproj/project.pbxproj @@ -93,12 +93,12 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0810; + LastUpgradeCheck = 0900; ORGANIZATIONNAME = "Marius Horga"; TargetAttributes = { 29E74ACC1C4159D60023A39A = { CreatedOnToolsVersion = 7.2; - LastSwiftMigration = 0800; + LastSwiftMigration = 0900; }; }; }; @@ -163,14 +163,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -209,14 +215,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -247,7 +259,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter02; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -260,7 +273,8 @@ PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter02; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Release; }; diff --git a/ch02/chapter02.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ch02/chapter02.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..49e5814 Binary files /dev/null and b/ch02/chapter02.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch02/chapter02.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist b/ch02/chapter02.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..7cc379e --- /dev/null +++ b/ch02/chapter02.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + chapter02.xcscheme + + orderHint + 0 + + + + diff --git a/ch02/chapter02/MetalView.swift b/ch02/chapter02/MetalView.swift index 48617b0..5451f30 100644 --- a/ch02/chapter02/MetalView.swift +++ b/ch02/chapter02/MetalView.swift @@ -20,11 +20,11 @@ class MetalView: MTKView { rpd.colorAttachments[0].texture = currentDrawable!.texture rpd.colorAttachments[0].clearColor = MTLClearColor(red: 0, green: 0.5, blue: 0.5, alpha: 1) rpd.colorAttachments[0].loadAction = .clear - let commandBuffer = device!.makeCommandQueue().makeCommandBuffer() - let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd) - commandEncoder.endEncoding() - commandBuffer.present(drawable) - commandBuffer.commit() + let commandBuffer = device!.makeCommandQueue()?.makeCommandBuffer() + let commandEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: rpd) + commandEncoder?.endEncoding() + commandBuffer?.present(drawable) + commandBuffer?.commit() } } } diff --git a/ch03/chapter03.xcodeproj/project.pbxproj b/ch03/chapter03.xcodeproj/project.pbxproj index dc830fa..80f7018 100644 --- a/ch03/chapter03.xcodeproj/project.pbxproj +++ b/ch03/chapter03.xcodeproj/project.pbxproj @@ -96,12 +96,12 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0810; + LastUpgradeCheck = 0900; ORGANIZATIONNAME = "Marius Horga"; TargetAttributes = { 29CB598B1C4461E90004DB55 = { CreatedOnToolsVersion = 7.2; - LastSwiftMigration = 0800; + LastSwiftMigration = 0900; }; }; }; @@ -167,14 +167,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -213,14 +219,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -251,7 +263,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter03; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -264,7 +277,8 @@ PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter03; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Release; }; diff --git a/ch03/chapter03.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ch03/chapter03.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..206d02a Binary files /dev/null and b/ch03/chapter03.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch03/chapter03.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist b/ch03/chapter03.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..026cf9f --- /dev/null +++ b/ch03/chapter03.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + chapter03.xcscheme + + orderHint + 0 + + + + diff --git a/ch03/chapter03/MetalView.swift b/ch03/chapter03/MetalView.swift index e7512d2..72b37f4 100644 --- a/ch03/chapter03/MetalView.swift +++ b/ch03/chapter03/MetalView.swift @@ -28,7 +28,7 @@ class MetalView: MTKView { 0.0, 1.0, 0.0, 1.0] let dataSize = vertexData!.count * MemoryLayout.size vertexBuffer = device!.makeBuffer(bytes: vertexData!, length: dataSize, options: []) - let library = device!.newDefaultLibrary()! + let library = device!.makeDefaultLibrary()! let vertex_func = library.makeFunction(name: "vertex_func") let frag_func = library.makeFunction(name: "fragment_func") let rpld = MTLRenderPipelineDescriptor() @@ -38,7 +38,7 @@ class MetalView: MTKView { do { try rps = device!.makeRenderPipelineState(descriptor: rpld) } catch let error { - self.print("\(error)") + self.printView("\(error)") } } @@ -46,13 +46,13 @@ class MetalView: MTKView { if let drawable = currentDrawable, let rpd = currentRenderPassDescriptor { rpd.colorAttachments[0].clearColor = MTLClearColorMake(0, 0.5, 0.5, 1.0) let commandBuffer = commandQueue!.makeCommandBuffer() - let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd) - commandEncoder.setRenderPipelineState(rps!) - commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0) - commandEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) - commandEncoder.endEncoding() - commandBuffer.present(drawable) - commandBuffer.commit() + let commandEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: rpd) + commandEncoder?.setRenderPipelineState(rps!) + commandEncoder?.setVertexBuffer(vertexBuffer, offset: 0, index: 0) + commandEncoder?.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) + commandEncoder?.endEncoding() + commandBuffer?.present(drawable) + commandBuffer?.commit() } } } diff --git a/ch04/chapter04.xcodeproj/project.pbxproj b/ch04/chapter04.xcodeproj/project.pbxproj index 758c724..6a06372 100644 --- a/ch04/chapter04.xcodeproj/project.pbxproj +++ b/ch04/chapter04.xcodeproj/project.pbxproj @@ -96,12 +96,12 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0810; + LastUpgradeCheck = 0900; ORGANIZATIONNAME = "Marius Horga"; TargetAttributes = { 29C87D3C1C56E93A005F4615 = { CreatedOnToolsVersion = 7.2; - LastSwiftMigration = 0800; + LastSwiftMigration = 0900; }; }; }; @@ -167,14 +167,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -213,14 +219,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -251,7 +263,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter04; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -264,7 +277,8 @@ PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter04; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Release; }; diff --git a/ch04/chapter04.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ch04/chapter04.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..54cb2ee Binary files /dev/null and b/ch04/chapter04.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch04/chapter04.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist b/ch04/chapter04.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..68acd03 --- /dev/null +++ b/ch04/chapter04.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + chapter04.xcscheme + + orderHint + 0 + + + + diff --git a/ch04/chapter04/MetalView.swift b/ch04/chapter04/MetalView.swift index 13712ee..34927a8 100644 --- a/ch04/chapter04/MetalView.swift +++ b/ch04/chapter04/MetalView.swift @@ -35,7 +35,7 @@ class MetalView: MTKView { } func registerShaders() { - let library = device!.newDefaultLibrary()! + let library = device!.makeDefaultLibrary()! let vertex_func = library.makeFunction(name: "vertex_func") let frag_func = library.makeFunction(name: "fragment_func") let rpld = MTLRenderPipelineDescriptor() @@ -49,13 +49,13 @@ class MetalView: MTKView { if let drawable = currentDrawable, let rpd = currentRenderPassDescriptor { rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0) let commandBuffer = commandQueue!.makeCommandBuffer() - let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd) - commandEncoder.setRenderPipelineState(rps!) - commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0) - commandEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) - commandEncoder.endEncoding() - commandBuffer.present(drawable) - commandBuffer.commit() + let commandEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: rpd) + commandEncoder?.setRenderPipelineState(rps!) + commandEncoder?.setVertexBuffer(vertexBuffer, offset: 0, index: 0) + commandEncoder?.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) + commandEncoder?.endEncoding() + commandBuffer?.present(drawable) + commandBuffer?.commit() } } } diff --git a/ch05/chapter05.xcodeproj/project.pbxproj b/ch05/chapter05.xcodeproj/project.pbxproj index f1362b3..164439c 100644 --- a/ch05/chapter05.xcodeproj/project.pbxproj +++ b/ch05/chapter05.xcodeproj/project.pbxproj @@ -96,12 +96,12 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0810; + LastUpgradeCheck = 0900; ORGANIZATIONNAME = "Marius Horga"; TargetAttributes = { 2980371E1C62437900FBBC6A = { CreatedOnToolsVersion = 7.2.1; - LastSwiftMigration = 0800; + LastSwiftMigration = 0900; }; }; }; @@ -167,14 +167,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -213,14 +219,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -253,7 +265,8 @@ PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter05; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -267,7 +280,8 @@ PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter05; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Release; }; diff --git a/ch05/chapter05.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ch05/chapter05.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..50c7ce8 Binary files /dev/null and b/ch05/chapter05.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch05/chapter05.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist b/ch05/chapter05.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..44045fa --- /dev/null +++ b/ch05/chapter05.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + chapter05.xcscheme + + orderHint + 0 + + + + diff --git a/ch05/chapter05/MetalView.swift b/ch05/chapter05/MetalView.swift index 73ad3bf..44ff391 100644 --- a/ch05/chapter05/MetalView.swift +++ b/ch05/chapter05/MetalView.swift @@ -87,7 +87,7 @@ class MetalView: MTKView { } func registerShaders() { - let library = device!.newDefaultLibrary()! + let library = device!.makeDefaultLibrary()! let vertex_func = library.makeFunction(name: "vertex_func") let frag_func = library.makeFunction(name: "fragment_func") let rpld = MTLRenderPipelineDescriptor() @@ -97,22 +97,22 @@ class MetalView: MTKView { do { try rps = device!.makeRenderPipelineState(descriptor: rpld) } catch let error { - self.print("\(error)") + self.printView("\(error)") } } override func draw(_ dirtyRect: NSRect) { if let rpd = currentRenderPassDescriptor, let drawable = currentDrawable { rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0) - let commandBuffer = device!.makeCommandQueue().makeCommandBuffer() - let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd) - commandEncoder.setRenderPipelineState(rps!) - commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0) - commandEncoder.setVertexBuffer(uniformBuffer, offset: 0, at: 1) - commandEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) - commandEncoder.endEncoding() - commandBuffer.present(drawable) - commandBuffer.commit() + let commandBuffer = device!.makeCommandQueue()?.makeCommandBuffer() + let commandEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: rpd) + commandEncoder?.setRenderPipelineState(rps!) + commandEncoder?.setVertexBuffer(vertexBuffer, offset: 0, index: 0) + commandEncoder?.setVertexBuffer(uniformBuffer, offset: 0, index: 1) + commandEncoder?.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) + commandEncoder?.endEncoding() + commandBuffer?.present(drawable) + commandBuffer?.commit() } } } diff --git a/ch06/chapter06.xcodeproj/project.pbxproj b/ch06/chapter06.xcodeproj/project.pbxproj index d317b25..b2cb9ce 100644 --- a/ch06/chapter06.xcodeproj/project.pbxproj +++ b/ch06/chapter06.xcodeproj/project.pbxproj @@ -93,13 +93,14 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0800; - LastUpgradeCheck = 0810; + LastUpgradeCheck = 0900; ORGANIZATIONNAME = Marius; TargetAttributes = { 2984512B1D47EF08008D81B9 = { CreatedOnToolsVersion = 8.0; DevelopmentTeam = C2X9Q6VFVJ; DevelopmentTeamName = "Marius Horga"; + LastSwiftMigration = 0900; ProvisioningStyle = Automatic; }; }; @@ -166,7 +167,9 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_DOCUMENTATION_COMMENTS = YES; @@ -174,7 +177,11 @@ CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -197,7 +204,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 10.0; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; @@ -216,7 +223,9 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_DOCUMENTATION_COMMENTS = YES; @@ -224,7 +233,11 @@ CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -241,7 +254,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 10.0; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; @@ -260,7 +273,8 @@ PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE = ""; PROVISIONING_PROFILE_SPECIFIER = ""; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -272,7 +286,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = org.metalkit.chapter06; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Release; }; diff --git a/ch06/chapter06.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ch06/chapter06.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..7e84e1f Binary files /dev/null and b/ch06/chapter06.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch06/chapter06.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist b/ch06/chapter06.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..73b6155 --- /dev/null +++ b/ch06/chapter06.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + chapter06.xcscheme + + orderHint + 0 + + + + diff --git a/ch06/chapter06/MetalView.swift b/ch06/chapter06/MetalView.swift index 7570bd4..6f44d71 100755 --- a/ch06/chapter06/MetalView.swift +++ b/ch06/chapter06/MetalView.swift @@ -29,9 +29,9 @@ class MetalView: UIView { descriptor.colorAttachments[0].clearColor = MTLClearColorMake(0, 0.5, 0.5, 1) descriptor.colorAttachments[0].texture = drawable.texture let commandBuffer = commandQueue.makeCommandBuffer() - let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: descriptor) - commandEncoder.endEncoding() - commandBuffer.present(drawable) - commandBuffer.commit() + let commandEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: descriptor) + commandEncoder?.endEncoding() + commandBuffer?.present(drawable) + commandBuffer?.commit() } } diff --git a/ch07/chapter07.xcodeproj/project.pbxproj b/ch07/chapter07.xcodeproj/project.pbxproj index 0d1d83c..4dc85b4 100644 --- a/ch07/chapter07.xcodeproj/project.pbxproj +++ b/ch07/chapter07.xcodeproj/project.pbxproj @@ -99,12 +99,12 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0810; + LastUpgradeCheck = 0900; ORGANIZATIONNAME = "Marius Horga"; TargetAttributes = { 29D878251C84D2B0007B5F17 = { CreatedOnToolsVersion = 7.2.1; - LastSwiftMigration = 0800; + LastSwiftMigration = 0900; }; }; }; @@ -171,14 +171,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -217,14 +223,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -255,7 +267,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter07; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -268,7 +281,8 @@ PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter07; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Release; }; diff --git a/ch07/chapter07.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ch07/chapter07.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..dbc5bd5 Binary files /dev/null and b/ch07/chapter07.xcodeproj/project.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch07/chapter07.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist b/ch07/chapter07.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..0efa542 --- /dev/null +++ b/ch07/chapter07.xcodeproj/xcuserdata/mhorga.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + chapter07.xcscheme + + orderHint + 0 + + + + diff --git a/ch07/chapter07/MetalView.swift b/ch07/chapter07/MetalView.swift index b20b49c..d8f0e84 100644 --- a/ch07/chapter07/MetalView.swift +++ b/ch07/chapter07/MetalView.swift @@ -35,7 +35,7 @@ class MetalView: MTKView { } func registerShaders() { - let library = device!.newDefaultLibrary()! + let library = device!.makeDefaultLibrary()! let vertex_func = library.makeFunction(name: "vertex_func") let frag_func = library.makeFunction(name: "fragment_func") let rpld = MTLRenderPipelineDescriptor() @@ -45,7 +45,7 @@ class MetalView: MTKView { do { try rps = device!.makeRenderPipelineState(descriptor: rpld) } catch let error { - self.print("\(error)") + self.printView("\(error)") } } @@ -53,15 +53,15 @@ class MetalView: MTKView { super.draw(dirtyRect) if let rpd = currentRenderPassDescriptor, let drawable = currentDrawable { rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0) - let commandBuffer = device!.makeCommandQueue().makeCommandBuffer() - let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd) - commandEncoder.setRenderPipelineState(rps!) - commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0) - commandEncoder.setVertexBuffer(uniformBuffer, offset: 0, at: 1) - commandEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) - commandEncoder.endEncoding() - commandBuffer.present(drawable) - commandBuffer.commit() + let commandBuffer = device!.makeCommandQueue()?.makeCommandBuffer() + let commandEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: rpd) + commandEncoder?.setRenderPipelineState(rps!) + commandEncoder?.setVertexBuffer(vertexBuffer, offset: 0, index: 0) + commandEncoder?.setVertexBuffer(uniformBuffer, offset: 0, index: 1) + commandEncoder?.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) + commandEncoder?.endEncoding() + commandBuffer?.present(drawable) + commandBuffer?.commit() } } } diff --git a/ch08/chapter08.playground/Sources/MetalView.swift b/ch08/chapter08.playground/Sources/MetalView.swift index a425315..0423b27 100644 --- a/ch08/chapter08.playground/Sources/MetalView.swift +++ b/ch08/chapter08.playground/Sources/MetalView.swift @@ -52,13 +52,14 @@ public class MetalView: NSObject, MTKViewDelegate { public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {} public func draw(in view: MTKView) { - if let rpd = view.currentRenderPassDescriptor, let drawable = view.currentDrawable { + if let rpd = view.currentRenderPassDescriptor, + let drawable = view.currentDrawable, + let commandBuffer = queue.makeCommandBuffer(), + let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd) { rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0) - let commandBuffer = queue.makeCommandBuffer() - let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd) commandEncoder.setRenderPipelineState(rps) - commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0) - commandEncoder.setVertexBuffer(uniformBuffer, offset: 0, at: 1) + commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0) + commandEncoder.setVertexBuffer(uniformBuffer, offset: 0, index: 1) commandEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) commandEncoder.endEncoding() commandBuffer.present(drawable) diff --git a/ch08/chapter08.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ch08/chapter08.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..8870a83 Binary files /dev/null and b/ch08/chapter08.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch09/chapter09.playground/Sources/MetalView.swift b/ch09/chapter09.playground/Sources/MetalView.swift index 795b1ae..b4b69ac 100755 --- a/ch09/chapter09.playground/Sources/MetalView.swift +++ b/ch09/chapter09.playground/Sources/MetalView.swift @@ -65,9 +65,9 @@ public class MetalView: NSObject, MTKViewDelegate { func update() { let scaled = scalingMatrix(scale: 0.5) - rotation += 1 / 100 * Float(M_PI) / 4 + rotation += 1 / 100 * Float.pi / 4 let rotatedY = rotationMatrix(angle: rotation, axis: float3(0, 1, 0)) - let rotatedX = rotationMatrix(angle: Float(M_PI) / 4, axis: float3(1, 0, 0)) + let rotatedX = rotationMatrix(angle: Float.pi / 4, axis: float3(1, 0, 0)) let modelMatrix = matrix_multiply(matrix_multiply(rotatedX, rotatedY), scaled) let cameraPosition = vector_float3(0, 0, -3) let viewMatrix = translationMatrix(position: cameraPosition) @@ -82,15 +82,16 @@ public class MetalView: NSObject, MTKViewDelegate { public func draw(in view: MTKView) { update() - if let rpd = view.currentRenderPassDescriptor, let drawable = view.currentDrawable { + if let rpd = view.currentRenderPassDescriptor, + let drawable = view.currentDrawable, + let commandBuffer = queue.makeCommandBuffer(), + let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd) { rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0) - let commandBuffer = queue.makeCommandBuffer() - let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd) commandEncoder.setRenderPipelineState(rps) commandEncoder.setFrontFacing(.counterClockwise) commandEncoder.setCullMode(.back) - commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0) - commandEncoder.setVertexBuffer(uniformBuffer, offset: 0, at: 1) + commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0) + commandEncoder.setVertexBuffer(uniformBuffer, offset: 0, index: 1) commandEncoder.drawIndexedPrimitives(type: .triangle, indexCount: indexBuffer.length / MemoryLayout.size, indexType: MTLIndexType.uint16, indexBuffer: indexBuffer, indexBufferOffset: 0) commandEncoder.endEncoding() commandBuffer.present(drawable) diff --git a/ch09/chapter09.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ch09/chapter09.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..eedabe6 Binary files /dev/null and b/ch09/chapter09.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch10/chapter10.playground/Sources/MetalView.swift b/ch10/chapter10.playground/Sources/MetalView.swift index 879e690..082a3f9 100755 --- a/ch10/chapter10.playground/Sources/MetalView.swift +++ b/ch10/chapter10.playground/Sources/MetalView.swift @@ -29,11 +29,11 @@ public class MetalView: NSObject, MTKViewDelegate { 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() + if let drawable = view.currentDrawable, + let commandBuffer = queue.makeCommandBuffer(), + let commandEncoder = commandBuffer.makeComputeCommandEncoder() { commandEncoder.setComputePipelineState(cps) - commandEncoder.setTexture(drawable.texture, at: 0) + commandEncoder.setTexture(drawable.texture, index: 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) diff --git a/ch10/chapter10.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ch10/chapter10.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..7c005c2 Binary files /dev/null and b/ch10/chapter10.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch11/chapter11.playground/Sources/MetalView.swift b/ch11/chapter11.playground/Sources/MetalView.swift index 24c4256..411a03e 100755 --- a/ch11/chapter11.playground/Sources/MetalView.swift +++ b/ch11/chapter11.playground/Sources/MetalView.swift @@ -29,11 +29,11 @@ public class MetalView: NSObject, MTKViewDelegate { 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() + if let drawable = view.currentDrawable, + let commandBuffer = queue.makeCommandBuffer(), + let commandEncoder = commandBuffer.makeComputeCommandEncoder() { commandEncoder.setComputePipelineState(cps) - commandEncoder.setTexture(drawable.texture, at: 0) + commandEncoder.setTexture(drawable.texture, index: 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) diff --git a/ch11/chapter11.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ch11/chapter11.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..8fbbd0f Binary files /dev/null and b/ch11/chapter11.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch12/chapter12.playground/Sources/MetalView.swift b/ch12/chapter12.playground/Sources/MetalView.swift index e84b3f7..dfdae7d 100755 --- a/ch12/chapter12.playground/Sources/MetalView.swift +++ b/ch12/chapter12.playground/Sources/MetalView.swift @@ -28,13 +28,13 @@ public class MetalView: MTKView, NSWindowDelegate { override public func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) - if let drawable = currentDrawable { - let commandBuffer = queue.makeCommandBuffer() - let commandEncoder = commandBuffer.makeComputeCommandEncoder() + if let drawable = currentDrawable, + 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.setTexture(drawable.texture, index: 0) + commandEncoder.setBuffer(mouseBuffer, offset: 0, index: 2) + commandEncoder.setBuffer(timerBuffer, offset: 0, index: 1) update() let threadGroupCount = MTLSizeMake(8, 8, 1) let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1) diff --git a/ch12/chapter12.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ch12/chapter12.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..c7ed9f1 Binary files /dev/null and b/ch12/chapter12.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch13/chapter13.playground/Sources/MetalView.swift b/ch13/chapter13.playground/Sources/MetalView.swift index e41a720..d1595ab 100755 --- a/ch13/chapter13.playground/Sources/MetalView.swift +++ b/ch13/chapter13.playground/Sources/MetalView.swift @@ -27,13 +27,13 @@ public class MetalView: MTKView, NSWindowDelegate { } override public func draw(_ dirtyRect: NSRect) { - if let drawable = currentDrawable { - let commandBuffer = queue.makeCommandBuffer() - let commandEncoder = commandBuffer.makeComputeCommandEncoder() + if let drawable = currentDrawable, + 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.setTexture(drawable.texture, index: 0) + commandEncoder.setBuffer(mouseBuffer, offset: 0, index: 2) + commandEncoder.setBuffer(timerBuffer, offset: 0, index: 1) update() let threadGroupCount = MTLSizeMake(8, 8, 1) let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1) diff --git a/ch13/chapter13.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ch13/chapter13.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..dd98198 Binary files /dev/null and b/ch13/chapter13.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch14/chapter14.playground/Sources/MetalView.swift b/ch14/chapter14.playground/Sources/MetalView.swift index c53b382..e66e923 100755 --- a/ch14/chapter14.playground/Sources/MetalView.swift +++ b/ch14/chapter14.playground/Sources/MetalView.swift @@ -18,12 +18,12 @@ public class MetalView: MTKView, NSWindowDelegate { } override public func draw(_ dirtyRect: NSRect) { - if let drawable = currentDrawable { - let commandBuffer = queue.makeCommandBuffer() - let commandEncoder = commandBuffer.makeComputeCommandEncoder() + if let drawable = currentDrawable, + let commandBuffer = queue.makeCommandBuffer(), + let commandEncoder = commandBuffer.makeComputeCommandEncoder() { commandEncoder.setComputePipelineState(cps) - commandEncoder.setTexture(drawable.texture, at: 0) - commandEncoder.setBuffer(timerBuffer, offset: 0, at: 0) + commandEncoder.setTexture(drawable.texture, index: 0) + commandEncoder.setBuffer(timerBuffer, offset: 0, index: 0) update() let threadGroupCount = MTLSizeMake(8, 8, 1) let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1) diff --git a/ch14/chapter14.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ch14/chapter14.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..bf0431f Binary files /dev/null and b/ch14/chapter14.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch15/chapter15.playground/Resources/Shaders.metal b/ch15/chapter15.playground/Resources/Shaders.metal index 83af2b1..f07d87c 100755 --- a/ch15/chapter15.playground/Resources/Shaders.metal +++ b/ch15/chapter15.playground/Resources/Shaders.metal @@ -11,15 +11,15 @@ kernel void compute(texture2d output [[texture(0)]], int width = input.get_width(); int height = input.get_height(); float2 uv = float2(gid) / float2(width, height); - uv = uv * 2.0 - 1.0; - uv = uv * 2; + uv = uv * 2.0 - 0.5; + uv = uv * 4; float radius = 1; float distance = length(uv) - radius; constexpr sampler textureSampler(coord::normalized, address::repeat, min_filter::linear, mag_filter::linear, - mip_filter::linear ); + mip_filter::linear); float3 norm = float3(uv, sqrt(1.0 - dot(uv, uv))); float pi = 3.14; float s = atan2( norm.z, norm.x ) / (2 * pi); diff --git a/ch15/chapter15.playground/Sources/MetalView.swift b/ch15/chapter15.playground/Sources/MetalView.swift index a451e4d..b2081e0 100755 --- a/ch15/chapter15.playground/Sources/MetalView.swift +++ b/ch15/chapter15.playground/Sources/MetalView.swift @@ -21,7 +21,7 @@ public class MetalView: NSObject, MTKViewDelegate { func setUpTexture() { let path = Bundle.main.path(forResource: "texture", ofType: "jpg") let textureLoader = MTKTextureLoader(device: device!) - texture = try! textureLoader.newTexture(withContentsOf: URL(fileURLWithPath: path!), options: nil) + texture = try! textureLoader.newTexture(URL: URL(fileURLWithPath: path!), options: nil) } func registerShaders() { @@ -46,13 +46,13 @@ public class MetalView: NSObject, MTKViewDelegate { 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() + if let drawable = view.currentDrawable, + let commandBuffer = queue.makeCommandBuffer(), + let commandEncoder = commandBuffer.makeComputeCommandEncoder() { commandEncoder.setComputePipelineState(cps) - commandEncoder.setTexture(drawable.texture, at: 0) - commandEncoder.setTexture(texture, at: 1) - commandEncoder.setBuffer(timerBuffer, offset: 0, at: 0) + commandEncoder.setTexture(drawable.texture, index: 0) + commandEncoder.setTexture(texture, index: 1) + commandEncoder.setBuffer(timerBuffer, offset: 0, index: 0) update() let threadGroupCount = MTLSizeMake(8, 8, 1) let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1) diff --git a/ch15/chapter15.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ch15/chapter15.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..e071f21 Binary files /dev/null and b/ch15/chapter15.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch17/chapter17.playground/Sources/Render.swift b/ch17/chapter17.playground/Sources/Render.swift index afea1d9..e5a6d48 100644 --- a/ch17/chapter17.playground/Sources/Render.swift +++ b/ch17/chapter17.playground/Sources/Render.swift @@ -4,11 +4,11 @@ import MetalKit public class Render : NSObject, MTKViewDelegate { weak var view: MTKView! - let commandQueue: MTLCommandQueue - let renderPipelineState: MTLRenderPipelineState - let device: MTLDevice - var vertexBuffer: MTLBuffer - var indexBuffer: MTLBuffer + let commandQueue: MTLCommandQueue! + let renderPipelineState: MTLRenderPipelineState! + let device: MTLDevice! + var vertexBuffer: MTLBuffer! + var indexBuffer: MTLBuffer! struct Vertex { var position: float4 @@ -19,9 +19,9 @@ public class Render : NSObject, MTKViewDelegate { view = mtkView view.clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1) view.colorPixelFormat = .bgra8Unorm - device = MTLCreateSystemDefaultDevice()! + device = MTLCreateSystemDefaultDevice() commandQueue = device.makeCommandQueue() - renderPipelineState = try! Render.buildRenderPipelineWithDevice(device: device, view: mtkView) + try! renderPipelineState = Render.buildRenderPipelineWithDevice(device: device, view: mtkView) var vertices = [Vertex]() vertices.append(Vertex(position: float4(-0.5, -0.5, 0, 1), color: float4(1, 0, 0, 1))) @@ -42,9 +42,9 @@ public class Render : NSObject, MTKViewDelegate { } class func buildRenderPipelineWithDevice(device: MTLDevice, view: MTKView) throws -> MTLRenderPipelineState { - let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")! - let input = try! String(contentsOfFile: path, encoding: String.Encoding.utf8) - let library = try! device.makeLibrary(source: input, options: nil) + guard let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { fatalError() } + let input = try String(contentsOfFile: path, encoding: String.Encoding.utf8) + let library = try device.makeLibrary(source: input, options: nil) let vertexFunction = library.makeFunction(name: "vertex_transform") let fragmentFunction = library.makeFunction(name: "fragment_lighting") let pipelineDescriptor = MTLRenderPipelineDescriptor() @@ -58,16 +58,17 @@ public class Render : NSObject, MTKViewDelegate { public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {} public func draw(in view: MTKView) { - let commandBuffer = commandQueue.makeCommandBuffer() - let renderPassDescriptor = view.currentRenderPassDescriptor! - let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor) - renderEncoder.setVertexBuffer(vertexBuffer, offset:0, at:0) - renderEncoder.setRenderPipelineState(renderPipelineState) - renderEncoder.setTriangleFillMode(.lines) - renderEncoder.drawIndexedPrimitives(type: .triangle, indexCount: 3, indexType: .uint16, indexBuffer: indexBuffer,indexBufferOffset: 0) - renderEncoder.endEncoding() - commandBuffer.present(view.currentDrawable!) - commandBuffer.commit() + if let commandBuffer = commandQueue.makeCommandBuffer(), + let renderPassDescriptor = view.currentRenderPassDescriptor, + let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor) { + renderEncoder.setVertexBuffer(vertexBuffer, offset:0, index:0) + renderEncoder.setRenderPipelineState(renderPipelineState) + renderEncoder.setTriangleFillMode(.lines) + renderEncoder.drawIndexedPrimitives(type: .triangle, indexCount: 3, indexType: .uint16, indexBuffer: indexBuffer,indexBufferOffset: 0) + renderEncoder.endEncoding() + commandBuffer.present(view.currentDrawable!) + commandBuffer.commit() + } } } diff --git a/ch17/chapter17.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ch17/chapter17.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..8b2faa3 Binary files /dev/null and b/ch17/chapter17.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch18/chapter18.playground/Sources/MetalView.swift b/ch18/chapter18.playground/Sources/MetalView.swift index b8c4802..8c8373b 100644 --- a/ch18/chapter18.playground/Sources/MetalView.swift +++ b/ch18/chapter18.playground/Sources/MetalView.swift @@ -3,9 +3,9 @@ import MetalKit public class MetalView: NSObject, MTKViewDelegate { weak var view: MTKView! - let commandQueue: MTLCommandQueue - let device: MTLDevice - let cps: MTLComputePipelineState + let commandQueue: MTLCommandQueue! + let device: MTLDevice! + let cps: MTLComputePipelineState! public init?(mtkView: MTKView, shader: String) { view = mtkView @@ -25,16 +25,17 @@ public class MetalView: NSObject, MTKViewDelegate { public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {} public func draw(in view: MTKView) { - let drawable = view.currentDrawable! - let commandBuffer = commandQueue.makeCommandBuffer() - let commandEncoder = commandBuffer.makeComputeCommandEncoder() - commandEncoder.setComputePipelineState(cps) - commandEncoder.setTexture(drawable.texture, at: 0) - let groups = MTLSize(width: Int(view.frame.width)/4, height: Int(view.frame.height)/4, depth: 1) - let threads = MTLSize(width: 8, height: 8,depth: 1) - commandEncoder.dispatchThreadgroups(groups,threadsPerThreadgroup: threads) - commandEncoder.endEncoding() - commandBuffer.present(drawable) - commandBuffer.commit() + if let drawable = view.currentDrawable, + let commandBuffer = commandQueue.makeCommandBuffer(), + let commandEncoder = commandBuffer.makeComputeCommandEncoder() { + commandEncoder.setComputePipelineState(cps) + commandEncoder.setTexture(drawable.texture, index: 0) + let groups = MTLSize(width: Int(view.frame.width)/4, height: Int(view.frame.height)/4, depth: 1) + let threads = MTLSize(width: 8, height: 8,depth: 1) + commandEncoder.dispatchThreadgroups(groups,threadsPerThreadgroup: threads) + commandEncoder.endEncoding() + commandBuffer.present(drawable) + commandBuffer.commit() + } } } diff --git a/ch18/chapter18.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/ch18/chapter18.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..372a154 Binary files /dev/null and b/ch18/chapter18.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/memory/memory_1.playground/Contents.swift b/memory/memory_1.playground/Contents.swift index 1ca6bd2..5096ea6 100644 --- a/memory/memory_1.playground/Contents.swift +++ b/memory/memory_1.playground/Contents.swift @@ -1,14 +1,14 @@ import MetalKit -let device = MTLCreateSystemDefaultDevice()! -let queue = device.makeCommandQueue() +guard let device = MTLCreateSystemDefaultDevice() else { fatalError() } +guard let queue = device.makeCommandQueue() else { fatalError() } let count = 1500 var myVector = [Float](repeating: 0, count: count) var length = count * MemoryLayout.size -//print(length) -var outBuffer = device.makeBuffer(bytes: myVector, length: length, options: []) -for (index, value) in myVector.enumerated() { myVector[index] = Float(index) } +print(length) +guard let outBuffer = device.makeBuffer(bytes: myVector, length: length, options: []) else { fatalError() } +for (index, _) in myVector.enumerated() { myVector[index] = Float(index) } var inBuffer = device.makeBuffer(bytes: myVector, length: length, options: []) let path = Bundle.main.path(forResource: "memory", ofType: "metal") @@ -17,11 +17,11 @@ let library = try device.makeLibrary(source: input, options: nil) let function = library.makeFunction(name: "compute")! let computePipelineState = try! device.makeComputePipelineState(function: function) -let commandBuffer = queue.makeCommandBuffer() -let encoder = commandBuffer.makeComputeCommandEncoder() +guard let commandBuffer = queue.makeCommandBuffer() else { fatalError() } +guard let encoder = commandBuffer.makeComputeCommandEncoder() else { fatalError() } encoder.setComputePipelineState(computePipelineState) -encoder.setBuffer(inBuffer, offset: 0, at: 0) -encoder.setBuffer(outBuffer, offset: 0, at: 1) +encoder.setBuffer(inBuffer, offset: 0, index: 0) +encoder.setBuffer(outBuffer, offset: 0, index: 1) let size = MTLSize(width: count, height: 1, depth: 1) encoder.dispatchThreadgroups(size, threadsPerThreadgroup: size) encoder.endEncoding() diff --git a/memory/memory_1.playground/contents.xcplayground b/memory/memory_1.playground/contents.xcplayground index 63b6dd8..a93d484 100644 --- a/memory/memory_1.playground/contents.xcplayground +++ b/memory/memory_1.playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/memory/memory_1.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/memory/memory_1.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate index 33c67a2..9981636 100644 Binary files a/memory/memory_1.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate and b/memory/memory_1.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/memory/memory_1.playground/timeline.xctimeline b/memory/memory_1.playground/timeline.xctimeline index 648e929..8a9c7b3 100644 --- a/memory/memory_1.playground/timeline.xctimeline +++ b/memory/memory_1.playground/timeline.xctimeline @@ -3,7 +3,7 @@ version = "3.0"> diff --git a/memory/memory_2.playground/Contents.swift b/memory/memory_2.playground/Contents.swift index 8c3ecbb..6c71b58 100644 --- a/memory/memory_2.playground/Contents.swift +++ b/memory/memory_2.playground/Contents.swift @@ -5,16 +5,17 @@ guard let device = MTLCreateSystemDefaultDevice() else { fatalError() } let count = 2000 let length = count * MemoryLayout< Float >.stride +var myBuffer: MTLBuffer! // 1. makeBuffer(length:) // -//let myBuffer = device.makeBuffer(length: length, options: []) +//myBuffer = device.makeBuffer(length: length, options: []) //print(myBuffer.contents()) // 2. makeBuffer(bytes:) // //var myVector = [Float](repeating: 0, count: count) -//let myBuffer = device.makeBuffer(bytes: myVector, length: length, options: []) +//myBuffer = device.makeBuffer(bytes: myVector, length: length, options: []) //withUnsafePointer(to: &myVector) { print($0) } //print(myBuffer.contents()) @@ -24,11 +25,11 @@ var memory: UnsafeMutableRawPointer? = nil let alignment = 0x1000 let allocationSize = (length + alignment - 1) & (~(alignment - 1)) posix_memalign(&memory, alignment, allocationSize) -let myBuffer = device.makeBuffer(bytesNoCopy: memory!, - length: allocationSize, - options: [], - deallocator: { (pointer: UnsafeMutableRawPointer, _: Int) in - free(pointer) - }) +myBuffer = device.makeBuffer(bytesNoCopy: memory!, + length: allocationSize, + options: [], + deallocator: { (pointer: UnsafeMutableRawPointer, _: Int) in + free(pointer) + }) print(memory!) -print(myBuffer.contents()) +print(myBuffer!.contents()) diff --git a/memory/memory_2.playground/contents.xcplayground b/memory/memory_2.playground/contents.xcplayground index 63b6dd8..a93d484 100644 --- a/memory/memory_2.playground/contents.xcplayground +++ b/memory/memory_2.playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/memory/memory_2.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/memory/memory_2.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate index 3835f78..6f8b010 100644 Binary files a/memory/memory_2.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate and b/memory/memory_2.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/metal2/metal2.playground/Contents.swift b/metal2/metal2.playground/Contents.swift index f270ef8..3eb207c 100644 --- a/metal2/metal2.playground/Contents.swift +++ b/metal2/metal2.playground/Contents.swift @@ -3,7 +3,7 @@ import Metal let devices = MTLCopyAllDevices() -devices[0].name! +devices[0].name devices[0].areRasterOrderGroupsSupported devices[0].isDepth24Stencil8PixelFormatSupported devices[0].isLowPower @@ -12,7 +12,7 @@ devices[0].maxThreadgroupMemoryLength devices[0].supportsFeatureSet(MTLFeatureSet.macOS_GPUFamily1_v3) devices[0].supportsFeatureSet(MTLFeatureSet.macOS_ReadWriteTextureTier2) -devices[1].name! +devices[1].name devices[1].areRasterOrderGroupsSupported devices[1].isDepth24Stencil8PixelFormatSupported devices[1].isLowPower diff --git a/metal2/metal2.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/metal2/metal2.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..b0da128 Binary files /dev/null and b/metal2/metal2.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/metal2/metal2.playground/timeline.xctimeline b/metal2/metal2.playground/timeline.xctimeline index 9389752..e21cc37 100644 --- a/metal2/metal2.playground/timeline.xctimeline +++ b/metal2/metal2.playground/timeline.xctimeline @@ -3,7 +3,7 @@ version = "3.0"> diff --git a/particles/particle.playground/Resources/Shaders.metal b/particles/particle.playground/Resources/Shaders.metal index dda48b9..0da8ea0 100644 --- a/particles/particle.playground/Resources/Shaders.metal +++ b/particles/particle.playground/Resources/Shaders.metal @@ -27,4 +27,4 @@ kernel void compute(texture2d output [[texture(0)]], float4 color = float4(1, 0.7, 0, 1); if (distance > 0) { color = float4(0.2, 0.5, 0.7, 1); } output.write(float4(color), gid); -} +} diff --git a/particles/particle.playground/Sources/MetalView.swift b/particles/particle.playground/Sources/MetalView.swift index dde5ba7..4c786f5 100644 --- a/particles/particle.playground/Sources/MetalView.swift +++ b/particles/particle.playground/Sources/MetalView.swift @@ -55,3 +55,4 @@ public class MetalView: NSObject, MTKViewDelegate { } } } + diff --git a/particles/particle.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/particles/particle.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..b1d57fa Binary files /dev/null and b/particles/particle.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/raymarching/raymarching.playground/Sources/MetalView.swift b/raymarching/raymarching.playground/Sources/MetalView.swift index 19203f9..4e93127 100755 --- a/raymarching/raymarching.playground/Sources/MetalView.swift +++ b/raymarching/raymarching.playground/Sources/MetalView.swift @@ -32,12 +32,12 @@ public class MetalView: NSObject, MTKViewDelegate { 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() + if let drawable = view.currentDrawable, + let commandBuffer = queue.makeCommandBuffer(), + let commandEncoder = commandBuffer.makeComputeCommandEncoder() { commandEncoder.setComputePipelineState(cps) - commandEncoder.setTexture(drawable.texture, at: 0) - commandEncoder.setBuffer(timeBuffer, offset: 0, at: 0) + commandEncoder.setTexture(drawable.texture, index: 0) + commandEncoder.setBuffer(timeBuffer, offset: 0, index: 0) time += 0.01 let bufferPointer = timeBuffer.contents() memcpy(bufferPointer, &time, MemoryLayout.size) diff --git a/raymarching/raymarching.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/raymarching/raymarching.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..464daf4 Binary files /dev/null and b/raymarching/raymarching.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/shadows/shadows.playground/Sources/MetalView.swift b/shadows/shadows.playground/Sources/MetalView.swift index a3a47b1..6ffd9d2 100755 --- a/shadows/shadows.playground/Sources/MetalView.swift +++ b/shadows/shadows.playground/Sources/MetalView.swift @@ -38,12 +38,12 @@ public class MetalView: NSObject, MTKViewDelegate { 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() + if let drawable = view.currentDrawable, + let commandBuffer = queue.makeCommandBuffer(), + let commandEncoder = commandBuffer.makeComputeCommandEncoder() { commandEncoder.setComputePipelineState(cps) - commandEncoder.setTexture(drawable.texture, at: 0) - commandEncoder.setBuffer(timerBuffer, offset: 0, at: 0) + commandEncoder.setTexture(drawable.texture, index: 0) + commandEncoder.setBuffer(timerBuffer, offset: 0, index: 0) update() let threadGroupCount = MTLSizeMake(8, 8, 1) let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1) diff --git a/shadows/shadows.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/shadows/shadows.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate index 8c75744..7e9e1c7 100644 Binary files a/shadows/shadows.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate and b/shadows/shadows.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/shadows/shadows2.playground/Sources/MetalView.swift b/shadows/shadows2.playground/Sources/MetalView.swift index bf69602..d4bd02e 100644 --- a/shadows/shadows2.playground/Sources/MetalView.swift +++ b/shadows/shadows2.playground/Sources/MetalView.swift @@ -38,12 +38,12 @@ public class MetalView: NSObject, MTKViewDelegate { 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() + if let drawable = view.currentDrawable, + let commandBuffer = queue.makeCommandBuffer(), + let commandEncoder = commandBuffer.makeComputeCommandEncoder() { commandEncoder.setComputePipelineState(cps) - commandEncoder.setTexture(drawable.texture, at: 0) - commandEncoder.setBuffer(timerBuffer, offset: 0, at: 0) + commandEncoder.setTexture(drawable.texture, index: 0) + commandEncoder.setBuffer(timerBuffer, offset: 0, index: 0) update() let threadGroupCount = MTLSizeMake(8, 8, 1) let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1) diff --git a/shadows/shadows2.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/shadows/shadows2.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate index 501a0aa..876a411 100644 Binary files a/shadows/shadows2.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate and b/shadows/shadows2.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ