diff --git a/ch01/chapter01.xcodeproj/project.pbxproj b/ch01/chapter01.xcodeproj/project.pbxproj index 2d5df56..6cbc653 100644 --- a/ch01/chapter01.xcodeproj/project.pbxproj +++ b/ch01/chapter01.xcodeproj/project.pbxproj @@ -9,7 +9,6 @@ /* Begin PBXBuildFile section */ 29400C7D1C3AEE1300345568 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29400C7C1C3AEE1300345568 /* AppDelegate.swift */; }; 29400C7F1C3AEE1300345568 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29400C7E1C3AEE1300345568 /* ViewController.swift */; }; - 29400C811C3AEE1300345568 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 29400C801C3AEE1300345568 /* Assets.xcassets */; }; 29400C841C3AEE1300345568 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 29400C821C3AEE1300345568 /* Main.storyboard */; }; /* End PBXBuildFile section */ @@ -17,7 +16,6 @@ 29400C791C3AEE1300345568 /* chapter01.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = chapter01.app; sourceTree = BUILT_PRODUCTS_DIR; }; 29400C7C1C3AEE1300345568 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 29400C7E1C3AEE1300345568 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; - 29400C801C3AEE1300345568 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 29400C831C3AEE1300345568 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 29400C851C3AEE1300345568 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; /* End PBXFileReference section */ @@ -53,7 +51,6 @@ isa = PBXGroup; children = ( 29400C7E1C3AEE1300345568 /* ViewController.swift */, - 29400C801C3AEE1300345568 /* Assets.xcassets */, 29400C821C3AEE1300345568 /* Main.storyboard */, 29400C851C3AEE1300345568 /* Info.plist */, 29400C7C1C3AEE1300345568 /* AppDelegate.swift */, @@ -88,11 +85,12 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0720; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = "Marius Horga"; TargetAttributes = { 29400C781C3AEE1300345568 = { CreatedOnToolsVersion = 7.2; + LastSwiftMigration = 0800; }; }; }; @@ -119,7 +117,6 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - 29400C811C3AEE1300345568 /* Assets.xcassets in Resources */, 29400C841C3AEE1300345568 /* Main.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -186,7 +183,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.11; + MACOSX_DEPLOYMENT_TARGET = 10.12; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = macosx; @@ -224,7 +221,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.11; + MACOSX_DEPLOYMENT_TARGET = 10.12; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = macosx; }; @@ -233,24 +230,25 @@ 29400C891C3AEE1300345568 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { - ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; COMBINE_HIDPI_IMAGES = YES; INFOPLIST_FILE = chapter01/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter01; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; 29400C8A1C3AEE1300345568 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { - ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; COMBINE_HIDPI_IMAGES = YES; INFOPLIST_FILE = chapter01/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter01; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -273,6 +271,7 @@ 29400C8A1C3AEE1300345568 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/ch01/chapter01.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch01/chapter01.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate index 18e3b2b..ba92cab 100644 Binary files a/ch01/chapter01.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate and b/ch01/chapter01.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch01/chapter01.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter01.xcscheme b/ch01/chapter01.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter01.xcscheme index b3bc40d..2adec09 100644 --- a/ch01/chapter01.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter01.xcscheme +++ b/ch01/chapter01.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter01.xcscheme @@ -1,6 +1,6 @@ - + + - + @@ -673,8 +673,11 @@ - - + + + + + @@ -682,6 +685,11 @@ + + + + + diff --git a/ch01/chapter01/ViewController.swift b/ch01/chapter01/ViewController.swift index 65fd44c..994db6f 100644 --- a/ch01/chapter01/ViewController.swift +++ b/ch01/chapter01/ViewController.swift @@ -6,7 +6,7 @@ // Copyright © 2016 Marius Horga. All rights reserved. // -import Cocoa // contains Metal +import Cocoa class ViewController: NSViewController { @@ -15,10 +15,15 @@ class ViewController: NSViewController { override func viewDidLoad() { super.viewDidLoad() - if let device = MTLCreateSystemDefaultDevice() { - label.stringValue = "Your GPU name is:\n\(device.name!)" - } else { - label.stringValue = "Your GPU does not support Metal!" + /* The MTLCopyAllDevices() function is only available in macOS. + For iOS/tvOS devices use MTLCreateSystemDefaultDevice() instead. */ + let devices = MTLCopyAllDevices() + guard let _ = devices.first else { + fatalError("Your GPU does not support Metal!") + } + label.stringValue = "Your system has the following GPU(s):\n" + for device in devices { + label.stringValue += "\(device.name!)\n" } } } diff --git a/ch02/chapter02.xcodeproj/project.pbxproj b/ch02/chapter02.xcodeproj/project.pbxproj index c91e5c5..c8b196f 100644 --- a/ch02/chapter02.xcodeproj/project.pbxproj +++ b/ch02/chapter02.xcodeproj/project.pbxproj @@ -8,7 +8,6 @@ /* Begin PBXBuildFile section */ 29E74AD11C4159D60023A39A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29E74AD01C4159D60023A39A /* AppDelegate.swift */; }; - 29E74AD51C4159D60023A39A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 29E74AD41C4159D60023A39A /* Assets.xcassets */; }; 29E74AD81C4159D60023A39A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 29E74AD61C4159D60023A39A /* Main.storyboard */; }; 29E74AE01C415A020023A39A /* MetalView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29E74ADF1C415A020023A39A /* MetalView.swift */; }; /* End PBXBuildFile section */ @@ -16,7 +15,6 @@ /* Begin PBXFileReference section */ 29E74ACD1C4159D60023A39A /* chapter02.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = chapter02.app; sourceTree = BUILT_PRODUCTS_DIR; }; 29E74AD01C4159D60023A39A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; - 29E74AD41C4159D60023A39A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 29E74AD71C4159D60023A39A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 29E74AD91C4159D60023A39A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 29E74ADF1C415A020023A39A /* MetalView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MetalView.swift; sourceTree = ""; }; @@ -54,7 +52,6 @@ children = ( 29E74ADF1C415A020023A39A /* MetalView.swift */, 29E74AD61C4159D60023A39A /* Main.storyboard */, - 29E74AD41C4159D60023A39A /* Assets.xcassets */, 29E74AD91C4159D60023A39A /* Info.plist */, 29E74AD01C4159D60023A39A /* AppDelegate.swift */, ); @@ -88,11 +85,12 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0720; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = "Marius Horga"; TargetAttributes = { 29E74ACC1C4159D60023A39A = { CreatedOnToolsVersion = 7.2; + LastSwiftMigration = 0800; }; }; }; @@ -119,7 +117,6 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - 29E74AD51C4159D60023A39A /* Assets.xcassets in Resources */, 29E74AD81C4159D60023A39A /* Main.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -186,7 +183,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.11; + MACOSX_DEPLOYMENT_TARGET = 10.12; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = macosx; @@ -224,7 +221,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.11; + MACOSX_DEPLOYMENT_TARGET = 10.12; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = macosx; }; @@ -233,24 +230,25 @@ 29E74ADD1C4159D60023A39A /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { - ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; COMBINE_HIDPI_IMAGES = YES; INFOPLIST_FILE = chapter02/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter02; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; 29E74ADE1C4159D60023A39A /* Release */ = { isa = XCBuildConfiguration; buildSettings = { - ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; COMBINE_HIDPI_IMAGES = YES; INFOPLIST_FILE = chapter02/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter02; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -273,6 +271,7 @@ 29E74ADE1C4159D60023A39A /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/ch02/chapter02.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch02/chapter02.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate index e571953..6326d60 100644 Binary files a/ch02/chapter02.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate and b/ch02/chapter02.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch02/chapter02.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter02.xcscheme b/ch02/chapter02.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter02.xcscheme index c6b8bd5..d27fa5f 100644 --- a/ch02/chapter02.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter02.xcscheme +++ b/ch02/chapter02.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter02.xcscheme @@ -1,6 +1,6 @@ - + + - + @@ -668,7 +668,7 @@ - + diff --git a/ch04/chapter04/MetalView.swift b/ch04/chapter04/MetalView.swift index 847b46e..744f295 100644 --- a/ch04/chapter04/MetalView.swift +++ b/ch04/chapter04/MetalView.swift @@ -10,59 +10,56 @@ import MetalKit class MetalView: MTKView { - var vertex_buffer: MTLBuffer! - var rps: MTLRenderPipelineState! = nil - - override func drawRect(dirtyRect: NSRect) { - super.drawRect(dirtyRect) - render() - } - - func render() { - device = MTLCreateSystemDefaultDevice() - createBuffer() - registerShaders() - sendToGPU() - } + var commandQueue: MTLCommandQueue? + var rps: MTLRenderPipelineState? + var vertexBuffer: MTLBuffer? struct Vertex { var position: vector_float4 var color: vector_float4 - }; + } + + required init(coder: NSCoder) { + super.init(coder: coder) + createBuffer() + registerShaders() + } func createBuffer() { - let vertex_data = [Vertex(position: [-1.0, -1.0, 0.0, 1.0], color: [1, 0, 0, 1]), - Vertex(position: [ 1.0, -1.0, 0.0, 1.0], color: [0, 1, 0, 1]), - Vertex(position: [ 0.0, 1.0, 0.0, 1.0], color: [0, 0, 1, 1])] - vertex_buffer = device!.newBufferWithBytes(vertex_data, length: sizeof(Vertex) * 3, options:[]) + device = MTLCreateSystemDefaultDevice()! + commandQueue = device!.newCommandQueue() + let vertexData = [Vertex(position: [-1.0, -1.0, 0.0, 1.0], color: [1, 0, 0, 1]), + Vertex(position: [ 1.0, -1.0, 0.0, 1.0], color: [0, 1, 0, 1]), + Vertex(position: [ 0.0, 1.0, 0.0, 1.0], color: [0, 0, 1, 1])] + vertexBuffer = device!.newBuffer(withBytes: vertexData, length: sizeof(Vertex.self) * 3, options:[]) } func registerShaders() { let library = device!.newDefaultLibrary()! - let vertex_func = library.newFunctionWithName("vertex_func") - let frag_func = library.newFunctionWithName("fragment_func") + let vertex_func = library.newFunction(withName: "vertex_func") + let frag_func = library.newFunction(withName: "fragment_func") let rpld = MTLRenderPipelineDescriptor() rpld.vertexFunction = vertex_func rpld.fragmentFunction = frag_func - rpld.colorAttachments[0].pixelFormat = .BGRA8Unorm + rpld.colorAttachments[0].pixelFormat = .bgra8Unorm do { - try rps = device!.newRenderPipelineStateWithDescriptor(rpld) + try rps = device!.newRenderPipelineState(with: rpld) } catch let error { self.print("\(error)") } } - func sendToGPU() { - if let rpd = currentRenderPassDescriptor, drawable = currentDrawable { + override func draw(_ dirtyRect: NSRect) { + if let drawable = currentDrawable, let rpd = currentRenderPassDescriptor { rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0) - let command_buffer = device!.newCommandQueue().commandBuffer() - let command_encoder = command_buffer.renderCommandEncoderWithDescriptor(rpd) - command_encoder.setRenderPipelineState(rps) - command_encoder.setVertexBuffer(vertex_buffer, offset: 0, atIndex: 0) - command_encoder.drawPrimitives(.Triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) - command_encoder.endEncoding() - command_buffer.presentDrawable(drawable) - command_buffer.commit() + let commandBuffer = commandQueue!.commandBuffer() + let commandEncoder = commandBuffer.renderCommandEncoder(with: rpd) + commandEncoder.setRenderPipelineState(rps!) + commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0) + commandEncoder.drawPrimitives(.triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) + commandEncoder.endEncoding() + commandBuffer.present(drawable) + commandBuffer.commit() } } } diff --git a/ch04/chapter04/Shaders.metal b/ch04/chapter04/Shaders.metal index 87188e5..958e762 100644 --- a/ch04/chapter04/Shaders.metal +++ b/ch04/chapter04/Shaders.metal @@ -14,7 +14,8 @@ struct Vertex { float4 color; }; -vertex Vertex vertex_func(constant Vertex *vertices [[buffer(0)]], uint vid [[vertex_id]]) { +vertex Vertex vertex_func(constant Vertex *vertices [[buffer(0)]], + uint vid [[vertex_id]]) { return vertices[vid]; } diff --git a/ch04/chapter04/ViewController.swift b/ch04/chapter04/ViewController.swift deleted file mode 100644 index d8740a2..0000000 --- a/ch04/chapter04/ViewController.swift +++ /dev/null @@ -1,27 +0,0 @@ -// -// ViewController.swift -// chapter04 -// -// Created by Marius on 1/26/16. -// Copyright © 2016 Marius Horga. All rights reserved. -// - -import Cocoa - -class ViewController: NSViewController { - - override func viewDidLoad() { - super.viewDidLoad() - - // Do any additional setup after loading the view. - } - - override var representedObject: AnyObject? { - didSet { - // Update the view, if already loaded. - } - } - - -} - diff --git a/ch05/chapter05.xcodeproj/project.pbxproj b/ch05/chapter05.xcodeproj/project.pbxproj index 4ffaaea..c2db75a 100644 --- a/ch05/chapter05.xcodeproj/project.pbxproj +++ b/ch05/chapter05.xcodeproj/project.pbxproj @@ -8,7 +8,6 @@ /* Begin PBXBuildFile section */ 298037231C62437900FBBC6A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 298037221C62437900FBBC6A /* AppDelegate.swift */; }; - 298037271C62437900FBBC6A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 298037261C62437900FBBC6A /* Assets.xcassets */; }; 2980372A1C62437900FBBC6A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 298037281C62437900FBBC6A /* Main.storyboard */; }; 298037321C6243CB00FBBC6A /* MetalView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 298037311C6243CB00FBBC6A /* MetalView.swift */; }; 298037341C62440400FBBC6A /* Shaders.metal in Sources */ = {isa = PBXBuildFile; fileRef = 298037331C62440400FBBC6A /* Shaders.metal */; }; @@ -17,7 +16,6 @@ /* Begin PBXFileReference section */ 2980371F1C62437900FBBC6A /* chapter05.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = chapter05.app; sourceTree = BUILT_PRODUCTS_DIR; }; 298037221C62437900FBBC6A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; - 298037261C62437900FBBC6A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 298037291C62437900FBBC6A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 2980372B1C62437900FBBC6A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 298037311C6243CB00FBBC6A /* MetalView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MetalView.swift; sourceTree = ""; }; @@ -56,7 +54,6 @@ children = ( 298037311C6243CB00FBBC6A /* MetalView.swift */, 298037331C62440400FBBC6A /* Shaders.metal */, - 298037261C62437900FBBC6A /* Assets.xcassets */, 298037281C62437900FBBC6A /* Main.storyboard */, 2980372B1C62437900FBBC6A /* Info.plist */, 298037221C62437900FBBC6A /* AppDelegate.swift */, @@ -91,11 +88,12 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0720; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = "Marius Horga"; TargetAttributes = { 2980371E1C62437900FBBC6A = { CreatedOnToolsVersion = 7.2.1; + LastSwiftMigration = 0800; }; }; }; @@ -122,7 +120,6 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - 298037271C62437900FBBC6A /* Assets.xcassets in Resources */, 2980372A1C62437900FBBC6A /* Main.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -190,7 +187,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.11; + MACOSX_DEPLOYMENT_TARGET = 10.12; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = macosx; @@ -228,7 +225,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.11; + MACOSX_DEPLOYMENT_TARGET = 10.12; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = macosx; }; @@ -237,7 +234,6 @@ 2980372F1C62437900FBBC6A /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { - ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; COMBINE_HIDPI_IMAGES = YES; INFOPLIST_FILE = chapter05/Info.plist; @@ -245,19 +241,21 @@ PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter05; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; }; name = Debug; }; 298037301C62437900FBBC6A /* Release */ = { isa = XCBuildConfiguration; buildSettings = { - ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; COMBINE_HIDPI_IMAGES = YES; INFOPLIST_FILE = chapter05/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter05; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/ch05/chapter05.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch05/chapter05.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate index d5df0be..963e81b 100644 Binary files a/ch05/chapter05.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate and b/ch05/chapter05.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch05/chapter05.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter05.xcscheme b/ch05/chapter05.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter05.xcscheme index 1ad0d75..2f4f9c3 100644 --- a/ch05/chapter05.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter05.xcscheme +++ b/ch05/chapter05.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter05.xcscheme @@ -1,6 +1,6 @@ Matrix { + func translationMatrix(_ matrix: Matrix, _ position: float3) -> Matrix { + var matrix = matrix matrix.m[12] = position.x matrix.m[13] = position.y matrix.m[14] = position.z return matrix } - - func scalingMatrix(var matrix: Matrix, _ scale: Float) -> Matrix { + func scalingMatrix(_ matrix: Matrix, _ scale: Float) -> Matrix { + var matrix = matrix matrix.m[0] = scale matrix.m[5] = scale matrix.m[10] = scale matrix.m[15] = 1.0 return matrix } - - func rotationMatrix(var matrix: Matrix, _ rot: float3) -> Matrix { + func rotationMatrix(_ matrix: Matrix, _ rot: float3) -> Matrix { + var matrix = matrix matrix.m[0] = cos(rot.y) * cos(rot.z) matrix.m[4] = cos(rot.z) * sin(rot.x) * sin(rot.y) - cos(rot.x) * sin(rot.z) matrix.m[8] = cos(rot.x) * cos(rot.z) * sin(rot.y) + sin(rot.x) * sin(rot.z) @@ -70,8 +64,8 @@ class MetalView: MTKView { matrix.m[15] = 1.0 return matrix } - - func modelMatrix(var matrix: Matrix) -> Matrix { + func modelMatrix(_ matrix: Matrix) -> Matrix { + var matrix = matrix matrix = rotationMatrix(matrix, float3(0.0, 0.0, 0.1)) matrix = scalingMatrix(matrix, 0.25) matrix = translationMatrix(matrix, float3(0.0, 0.5, 0.0)) @@ -80,44 +74,45 @@ class MetalView: MTKView { } func createBuffers() { - let vertex_data = [ - Vertex(position: [-1.0, -1.0, 0.0, 1.0], color: [1, 0, 0, 1]), - Vertex(position: [ 1.0, -1.0, 0.0, 1.0], color: [0, 1, 0, 1]), - Vertex(position: [ 0.0, 1.0, 0.0, 1.0], color: [0, 0, 1, 1]) + device = MTLCreateSystemDefaultDevice()! + commandQueue = device!.newCommandQueue() + let vertex_data = [Vertex(position: [-1.0, -1.0, 0.0, 1.0], color: [1, 0, 0, 1]), + Vertex(position: [ 1.0, -1.0, 0.0, 1.0], color: [0, 1, 0, 1]), + Vertex(position: [ 0.0, 1.0, 0.0, 1.0], color: [0, 0, 1, 1]) ] - vertex_buffer = device!.newBufferWithBytes(vertex_data, length: sizeof(Vertex) * 3, options:[]) - uniform_buffer = device!.newBufferWithLength(sizeof(Float) * 16, options: []) - let bufferPointer = uniform_buffer.contents() - memcpy(bufferPointer, Matrix().modelMatrix(Matrix()).m, sizeof(Float) * 16) + vertexBuffer = device!.newBuffer(withBytes: vertex_data, length: sizeof(Vertex.self) * 3, options:[]) + uniformBuffer = device!.newBuffer(withLength: sizeof(Float.self) * 16, options: []) + let bufferPointer = uniformBuffer.contents() + memcpy(bufferPointer, Matrix().modelMatrix(Matrix()).m, sizeof(Float.self) * 16) } func registerShaders() { let library = device!.newDefaultLibrary()! - let vertex_func = library.newFunctionWithName("vertex_func") - let frag_func = library.newFunctionWithName("fragment_func") + let vertex_func = library.newFunction(withName: "vertex_func") + let frag_func = library.newFunction(withName: "fragment_func") let rpld = MTLRenderPipelineDescriptor() rpld.vertexFunction = vertex_func rpld.fragmentFunction = frag_func - rpld.colorAttachments[0].pixelFormat = .BGRA8Unorm + rpld.colorAttachments[0].pixelFormat = .bgra8Unorm do { - try rps = device!.newRenderPipelineStateWithDescriptor(rpld) + try rps = device!.newRenderPipelineState(with: rpld) } catch let error { self.print("\(error)") } } - func sendToGPU() { + override func draw(_ dirtyRect: NSRect) { if let rpd = currentRenderPassDescriptor, drawable = currentDrawable { rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0) - let command_buffer = device!.newCommandQueue().commandBuffer() - let command_encoder = command_buffer.renderCommandEncoderWithDescriptor(rpd) - command_encoder.setRenderPipelineState(rps) - command_encoder.setVertexBuffer(vertex_buffer, offset: 0, atIndex: 0) - command_encoder.setVertexBuffer(uniform_buffer, offset: 0, atIndex: 1) - command_encoder.drawPrimitives(.Triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) - command_encoder.endEncoding() - command_buffer.presentDrawable(drawable) - command_buffer.commit() + let commandBuffer = device!.newCommandQueue().commandBuffer() + let commandEncoder = commandBuffer.renderCommandEncoder(with: rpd) + commandEncoder.setRenderPipelineState(rps!) + commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0) + commandEncoder.setVertexBuffer(uniformBuffer, offset: 0, at: 1) + commandEncoder.drawPrimitives(.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 d5b5d90..04ca30b 100644 --- a/ch06/chapter06.xcodeproj/project.pbxproj +++ b/ch06/chapter06.xcodeproj/project.pbxproj @@ -7,23 +7,21 @@ objects = { /* Begin PBXBuildFile section */ - 292765421C6BDE3400094CA9 /* MetalView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 292765411C6BDE3400094CA9 /* MetalView.swift */; }; - 29DB1AF61C6BDDD500BCC31B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29DB1AF51C6BDDD500BCC31B /* AppDelegate.swift */; }; - 29DB1AFA1C6BDDD500BCC31B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 29DB1AF91C6BDDD500BCC31B /* Assets.xcassets */; }; - 29DB1AFD1C6BDDD500BCC31B /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 29DB1AFB1C6BDDD500BCC31B /* Main.storyboard */; }; + 298451301D47EF08008D81B9 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2984512F1D47EF08008D81B9 /* AppDelegate.swift */; }; + 298451351D47EF08008D81B9 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 298451331D47EF08008D81B9 /* Main.storyboard */; }; + 298451421D47F0BF008D81B9 /* MetalView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 298451411D47F0BF008D81B9 /* MetalView.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ - 292765411C6BDE3400094CA9 /* MetalView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MetalView.swift; sourceTree = ""; }; - 29DB1AF21C6BDDD500BCC31B /* chapter06.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = chapter06.app; sourceTree = BUILT_PRODUCTS_DIR; }; - 29DB1AF51C6BDDD500BCC31B /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; - 29DB1AF91C6BDDD500BCC31B /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; - 29DB1AFC1C6BDDD500BCC31B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; - 29DB1AFE1C6BDDD500BCC31B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 2984512C1D47EF08008D81B9 /* chapter06.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = chapter06.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 2984512F1D47EF08008D81B9 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + 298451341D47EF08008D81B9 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + 2984513B1D47EF08008D81B9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 298451411D47F0BF008D81B9 /* MetalView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MetalView.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ - 29DB1AEF1C6BDDD500BCC31B /* Frameworks */ = { + 298451291D47EF08008D81B9 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( @@ -33,30 +31,29 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ - 29DB1AE91C6BDDD400BCC31B = { + 298451231D47EF07008D81B9 = { isa = PBXGroup; children = ( - 29DB1AF41C6BDDD500BCC31B /* chapter06 */, - 29DB1AF31C6BDDD500BCC31B /* Products */, + 2984512E1D47EF08008D81B9 /* chapter06 */, + 2984512D1D47EF08008D81B9 /* Products */, ); sourceTree = ""; }; - 29DB1AF31C6BDDD500BCC31B /* Products */ = { + 2984512D1D47EF08008D81B9 /* Products */ = { isa = PBXGroup; children = ( - 29DB1AF21C6BDDD500BCC31B /* chapter06.app */, + 2984512C1D47EF08008D81B9 /* chapter06.app */, ); name = Products; sourceTree = ""; }; - 29DB1AF41C6BDDD500BCC31B /* chapter06 */ = { + 2984512E1D47EF08008D81B9 /* chapter06 */ = { isa = PBXGroup; children = ( - 292765411C6BDE3400094CA9 /* MetalView.swift */, - 29DB1AF91C6BDDD500BCC31B /* Assets.xcassets */, - 29DB1AFB1C6BDDD500BCC31B /* Main.storyboard */, - 29DB1AFE1C6BDDD500BCC31B /* Info.plist */, - 29DB1AF51C6BDDD500BCC31B /* AppDelegate.swift */, + 298451411D47F0BF008D81B9 /* MetalView.swift */, + 298451331D47EF08008D81B9 /* Main.storyboard */, + 2984513B1D47EF08008D81B9 /* Info.plist */, + 2984512F1D47EF08008D81B9 /* AppDelegate.swift */, ); path = chapter06; sourceTree = ""; @@ -64,13 +61,13 @@ /* End PBXGroup section */ /* Begin PBXNativeTarget section */ - 29DB1AF11C6BDDD500BCC31B /* chapter06 */ = { + 2984512B1D47EF08008D81B9 /* chapter06 */ = { isa = PBXNativeTarget; - buildConfigurationList = 29DB1B011C6BDDD500BCC31B /* Build configuration list for PBXNativeTarget "chapter06" */; + buildConfigurationList = 2984513E1D47EF08008D81B9 /* Build configuration list for PBXNativeTarget "chapter06" */; buildPhases = ( - 29DB1AEE1C6BDDD500BCC31B /* Sources */, - 29DB1AEF1C6BDDD500BCC31B /* Frameworks */, - 29DB1AF01C6BDDD500BCC31B /* Resources */, + 298451281D47EF08008D81B9 /* Sources */, + 298451291D47EF08008D81B9 /* Frameworks */, + 2984512A1D47EF08008D81B9 /* Resources */, ); buildRules = ( ); @@ -78,25 +75,28 @@ ); name = chapter06; productName = chapter06; - productReference = 29DB1AF21C6BDDD500BCC31B /* chapter06.app */; + productReference = 2984512C1D47EF08008D81B9 /* chapter06.app */; productType = "com.apple.product-type.application"; }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ - 29DB1AEA1C6BDDD400BCC31B /* Project object */ = { + 298451241D47EF07008D81B9 /* Project object */ = { isa = PBXProject; attributes = { - LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0720; - ORGANIZATIONNAME = "Marius Horga"; + LastSwiftUpdateCheck = 0800; + LastUpgradeCheck = 0800; + ORGANIZATIONNAME = Marius; TargetAttributes = { - 29DB1AF11C6BDDD500BCC31B = { - CreatedOnToolsVersion = 7.2.1; + 2984512B1D47EF08008D81B9 = { + CreatedOnToolsVersion = 8.0; + DevelopmentTeam = C2X9Q6VFVJ; + DevelopmentTeamName = "Marius Horga"; + ProvisioningStyle = Automatic; }; }; }; - buildConfigurationList = 29DB1AED1C6BDDD400BCC31B /* Build configuration list for PBXProject "chapter06" */; + buildConfigurationList = 298451271D47EF07008D81B9 /* Build configuration list for PBXProject "chapter06" */; compatibilityVersion = "Xcode 3.2"; developmentRegion = English; hasScannedForEncodings = 0; @@ -104,45 +104,44 @@ en, Base, ); - mainGroup = 29DB1AE91C6BDDD400BCC31B; - productRefGroup = 29DB1AF31C6BDDD500BCC31B /* Products */; + mainGroup = 298451231D47EF07008D81B9; + productRefGroup = 2984512D1D47EF08008D81B9 /* Products */; projectDirPath = ""; projectRoot = ""; targets = ( - 29DB1AF11C6BDDD500BCC31B /* chapter06 */, + 2984512B1D47EF08008D81B9 /* chapter06 */, ); }; /* End PBXProject section */ /* Begin PBXResourcesBuildPhase section */ - 29DB1AF01C6BDDD500BCC31B /* Resources */ = { + 2984512A1D47EF08008D81B9 /* Resources */ = { isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - 29DB1AFA1C6BDDD500BCC31B /* Assets.xcassets in Resources */, - 29DB1AFD1C6BDDD500BCC31B /* Main.storyboard in Resources */, + 298451351D47EF08008D81B9 /* Main.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXResourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ - 29DB1AEE1C6BDDD500BCC31B /* Sources */ = { + 298451281D47EF08008D81B9 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 292765421C6BDE3400094CA9 /* MetalView.swift in Sources */, - 29DB1AF61C6BDDD500BCC31B /* AppDelegate.swift in Sources */, + 298451421D47F0BF008D81B9 /* MetalView.swift in Sources */, + 298451301D47EF08008D81B9 /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXSourcesBuildPhase section */ /* Begin PBXVariantGroup section */ - 29DB1AFB1C6BDDD500BCC31B /* Main.storyboard */ = { + 298451331D47EF08008D81B9 /* Main.storyboard */ = { isa = PBXVariantGroup; children = ( - 29DB1AFC1C6BDDD500BCC31B /* Base */, + 298451341D47EF08008D81B9 /* Base */, ); name = Main.storyboard; sourceTree = ""; @@ -150,10 +149,11 @@ /* End PBXVariantGroup section */ /* Begin XCBuildConfiguration section */ - 29DB1AFF1C6BDDD500BCC31B /* Debug */ = { + 2984513C1D47EF08008D81B9 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; @@ -161,13 +161,14 @@ CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - CODE_SIGN_IDENTITY = "-"; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = NO; DEBUG_INFORMATION_FORMAT = dwarf; ENABLE_STRICT_OBJC_MSGSEND = YES; @@ -186,18 +187,21 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.11; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; - SDKROOT = macosx; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; }; - 29DB1B001C6BDDD500BCC31B /* Release */ = { + 2984513D1D47EF08008D81B9 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; @@ -205,13 +209,14 @@ CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - CODE_SIGN_IDENTITY = "-"; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = NO; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; ENABLE_NS_ASSERTIONS = NO; @@ -224,58 +229,63 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.11; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; MTL_ENABLE_DEBUG_INFO = NO; - SDKROOT = macosx; + SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + TARGETED_DEVICE_FAMILY = "1,2"; + VALIDATE_PRODUCT = YES; }; name = Release; }; - 29DB1B021C6BDDD500BCC31B /* Debug */ = { + 2984513F1D47EF08008D81B9 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { - ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; - COMBINE_HIDPI_IMAGES = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; INFOPLIST_FILE = chapter06/Info.plist; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter06; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = org.metalkit.chapter06; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE = ""; + PROVISIONING_PROFILE_SPECIFIER = ""; + SWIFT_VERSION = 3.0; }; name = Debug; }; - 29DB1B031C6BDDD500BCC31B /* Release */ = { + 298451401D47EF08008D81B9 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { - ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; - COMBINE_HIDPI_IMAGES = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; INFOPLIST_FILE = chapter06/Info.plist; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter06; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = org.metalkit.chapter06; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ - 29DB1AED1C6BDDD400BCC31B /* Build configuration list for PBXProject "chapter06" */ = { + 298451271D47EF07008D81B9 /* Build configuration list for PBXProject "chapter06" */ = { isa = XCConfigurationList; buildConfigurations = ( - 29DB1AFF1C6BDDD500BCC31B /* Debug */, - 29DB1B001C6BDDD500BCC31B /* Release */, + 2984513C1D47EF08008D81B9 /* Debug */, + 2984513D1D47EF08008D81B9 /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; - 29DB1B011C6BDDD500BCC31B /* Build configuration list for PBXNativeTarget "chapter06" */ = { + 2984513E1D47EF08008D81B9 /* Build configuration list for PBXNativeTarget "chapter06" */ = { isa = XCConfigurationList; buildConfigurations = ( - 29DB1B021C6BDDD500BCC31B /* Debug */, - 29DB1B031C6BDDD500BCC31B /* Release */, + 2984513F1D47EF08008D81B9 /* Debug */, + 298451401D47EF08008D81B9 /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; - rootObject = 29DB1AEA1C6BDDD400BCC31B /* Project object */; + rootObject = 298451241D47EF07008D81B9 /* Project object */; } diff --git a/ch06/chapter06.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch06/chapter06.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate index b174838..8f4dc11 100644 Binary files a/ch06/chapter06.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate and b/ch06/chapter06.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch06/chapter06.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter06.xcscheme b/ch06/chapter06.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter06.xcscheme index d92b1b7..af7d21a 100644 --- a/ch06/chapter06.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter06.xcscheme +++ b/ch06/chapter06.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter06.xcscheme @@ -1,6 +1,6 @@ @@ -32,7 +32,7 @@ @@ -55,7 +55,7 @@ runnableDebuggingMode = "0"> @@ -74,7 +74,7 @@ runnableDebuggingMode = "0"> diff --git a/ch06/chapter06.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist b/ch06/chapter06.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist index 8e90fc7..9b1a329 100644 --- a/ch06/chapter06.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/ch06/chapter06.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist @@ -12,7 +12,7 @@ SuppressBuildableAutocreation - 29DB1AF11C6BDDD500BCC31B + 2984512B1D47EF08008D81B9 primary diff --git a/ch06/chapter06/AppDelegate.swift b/ch06/chapter06/AppDelegate.swift index 247cfb6..b492ba3 100644 --- a/ch06/chapter06/AppDelegate.swift +++ b/ch06/chapter06/AppDelegate.swift @@ -2,13 +2,14 @@ // AppDelegate.swift // chapter06 // -// Created by Marius on 2/10/16. -// Copyright © 2016 Marius Horga. All rights reserved. +// Created by Marius on 7/26/16. +// Copyright © 2016 Marius. All rights reserved. // -import Cocoa +import UIKit -@NSApplicationMain -class AppDelegate: NSObject, NSApplicationDelegate { +@UIApplicationMain +class AppDelegate: UIResponder, UIApplicationDelegate { + var window: UIWindow? } diff --git a/ch06/chapter06/Assets.xcassets/AppIcon.appiconset/Contents.json b/ch06/chapter06/Assets.xcassets/AppIcon.appiconset/Contents.json deleted file mode 100644 index 2db2b1c..0000000 --- a/ch06/chapter06/Assets.xcassets/AppIcon.appiconset/Contents.json +++ /dev/null @@ -1,58 +0,0 @@ -{ - "images" : [ - { - "idiom" : "mac", - "size" : "16x16", - "scale" : "1x" - }, - { - "idiom" : "mac", - "size" : "16x16", - "scale" : "2x" - }, - { - "idiom" : "mac", - "size" : "32x32", - "scale" : "1x" - }, - { - "idiom" : "mac", - "size" : "32x32", - "scale" : "2x" - }, - { - "idiom" : "mac", - "size" : "128x128", - "scale" : "1x" - }, - { - "idiom" : "mac", - "size" : "128x128", - "scale" : "2x" - }, - { - "idiom" : "mac", - "size" : "256x256", - "scale" : "1x" - }, - { - "idiom" : "mac", - "size" : "256x256", - "scale" : "2x" - }, - { - "idiom" : "mac", - "size" : "512x512", - "scale" : "1x" - }, - { - "idiom" : "mac", - "size" : "512x512", - "scale" : "2x" - } - ], - "info" : { - "version" : 1, - "author" : "xcode" - } -} \ No newline at end of file diff --git a/ch06/chapter06/Base.lproj/Main.storyboard b/ch06/chapter06/Base.lproj/Main.storyboard index 46163bb..5327e07 100644 --- a/ch06/chapter06/Base.lproj/Main.storyboard +++ b/ch06/chapter06/Base.lproj/Main.storyboard @@ -1,682 +1,27 @@ - - + + - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Default - - - - - - - Left to Right - - - - - - - Right to Left - - - - - - - - - - - Default - - - - - - - Left to Right - - - - - - - Right to Left - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - + + + + + + + + + - + - diff --git a/ch06/chapter06/Info.plist b/ch06/chapter06/Info.plist index 69d1345..e4ece02 100644 --- a/ch06/chapter06/Info.plist +++ b/ch06/chapter06/Info.plist @@ -6,8 +6,6 @@ en CFBundleExecutable $(EXECUTABLE_NAME) - CFBundleIconFile - CFBundleIdentifier $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion @@ -18,17 +16,28 @@ APPL CFBundleShortVersionString 1.0 - CFBundleSignature - ???? CFBundleVersion 1 - LSMinimumSystemVersion - $(MACOSX_DEPLOYMENT_TARGET) - NSHumanReadableCopyright - Copyright © 2016 Marius Horga. All rights reserved. - NSMainStoryboardFile + LSRequiresIPhoneOS + + UILaunchStoryboardName Main - NSPrincipalClass - NSApplication + UIMainStoryboardFile + Main + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + + UISupportedInterfaceOrientations~ipad + + UIInterfaceOrientationPortrait + UIInterfaceOrientationPortraitUpsideDown + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + diff --git a/ch06/chapter06/MetalView.swift b/ch06/chapter06/MetalView.swift old mode 100644 new mode 100755 index 33682be..58fccb5 --- a/ch06/chapter06/MetalView.swift +++ b/ch06/chapter06/MetalView.swift @@ -6,39 +6,36 @@ // Copyright © 2016 Marius Horga. All rights reserved. // -import Cocoa +import UIKit -class MetalView: NSView { +class MetalView: UIView { - override func drawRect(dirtyRect: NSRect) { - super.drawRect(dirtyRect) - render() + var commandQueue: MTLCommandQueue! + + var metalLayer: CAMetalLayer { + return self.layer as! CAMetalLayer } override class func layerClass() -> AnyClass { return CAMetalLayer.self } - var metalLayer: CAMetalLayer { - return layer as! CAMetalLayer + override func didMoveToWindow() { + super.didMoveToWindow() + let device = MTLCreateSystemDefaultDevice()! + commandQueue = device.newCommandQueue() + redraw() } - func render() { - let device = MTLCreateSystemDefaultDevice()! - metalLayer.device = device - metalLayer.pixelFormat = .BGRA8Unorm - let drawable = metalLayer.nextDrawable() - let texture = drawable!.texture - let rpd = MTLRenderPassDescriptor() - rpd.colorAttachments[0].texture = texture - rpd.colorAttachments[0].loadAction = .Clear - rpd.colorAttachments[0].storeAction = .Store - rpd.colorAttachments[0].clearColor = MTLClearColor(red: 1, green: 0, blue: 0, alpha: 1) - let commandQueue = device.newCommandQueue() + private func redraw() { + let drawable = metalLayer.nextDrawable()! + let descriptor = MTLRenderPassDescriptor() + descriptor.colorAttachments[0].clearColor = MTLClearColorMake(0, 1, 1, 1) + descriptor.colorAttachments[0].texture = drawable.texture let commandBuffer = commandQueue.commandBuffer() - let commandEncoder = commandBuffer.renderCommandEncoderWithDescriptor(rpd) + let commandEncoder = commandBuffer.renderCommandEncoder(with: descriptor) commandEncoder.endEncoding() - commandBuffer.presentDrawable(drawable!) + commandBuffer.present(drawable) commandBuffer.commit() } } diff --git a/ch07/chapter07.xcodeproj/project.pbxproj b/ch07/chapter07.xcodeproj/project.pbxproj index bc672e5..2bb7acb 100644 --- a/ch07/chapter07.xcodeproj/project.pbxproj +++ b/ch07/chapter07.xcodeproj/project.pbxproj @@ -9,7 +9,6 @@ /* Begin PBXBuildFile section */ 29B186261C84FF5800E9C0AA /* MathUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B186251C84FF5800E9C0AA /* MathUtils.swift */; }; 29D8782A1C84D2B0007B5F17 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29D878291C84D2B0007B5F17 /* AppDelegate.swift */; }; - 29D8782E1C84D2B0007B5F17 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 29D8782D1C84D2B0007B5F17 /* Assets.xcassets */; }; 29D878311C84D2B0007B5F17 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 29D8782F1C84D2B0007B5F17 /* Main.storyboard */; }; 29D878391C84D309007B5F17 /* MetalView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29D878381C84D309007B5F17 /* MetalView.swift */; }; 29D8783B1C84D335007B5F17 /* Shaders.metal in Sources */ = {isa = PBXBuildFile; fileRef = 29D8783A1C84D335007B5F17 /* Shaders.metal */; }; @@ -19,7 +18,6 @@ 29B186251C84FF5800E9C0AA /* MathUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MathUtils.swift; sourceTree = ""; }; 29D878261C84D2B0007B5F17 /* chapter07.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = chapter07.app; sourceTree = BUILT_PRODUCTS_DIR; }; 29D878291C84D2B0007B5F17 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; - 29D8782D1C84D2B0007B5F17 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 29D878301C84D2B0007B5F17 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 29D878321C84D2B0007B5F17 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 29D878381C84D309007B5F17 /* MetalView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MetalView.swift; sourceTree = ""; }; @@ -59,7 +57,6 @@ 29D878381C84D309007B5F17 /* MetalView.swift */, 29B186251C84FF5800E9C0AA /* MathUtils.swift */, 29D8783A1C84D335007B5F17 /* Shaders.metal */, - 29D8782D1C84D2B0007B5F17 /* Assets.xcassets */, 29D8782F1C84D2B0007B5F17 /* Main.storyboard */, 29D878321C84D2B0007B5F17 /* Info.plist */, 29D878291C84D2B0007B5F17 /* AppDelegate.swift */, @@ -94,11 +91,12 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0720; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = "Marius Horga"; TargetAttributes = { 29D878251C84D2B0007B5F17 = { CreatedOnToolsVersion = 7.2.1; + LastSwiftMigration = 0800; }; }; }; @@ -125,7 +123,6 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - 29D8782E1C84D2B0007B5F17 /* Assets.xcassets in Resources */, 29D878311C84D2B0007B5F17 /* Main.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -194,7 +191,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.11; + MACOSX_DEPLOYMENT_TARGET = 10.12; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = macosx; @@ -232,7 +229,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.11; + MACOSX_DEPLOYMENT_TARGET = 10.12; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = macosx; }; @@ -241,24 +238,25 @@ 29D878361C84D2B0007B5F17 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { - ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; COMBINE_HIDPI_IMAGES = YES; INFOPLIST_FILE = chapter07/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter07; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; 29D878371C84D2B0007B5F17 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { - ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; COMBINE_HIDPI_IMAGES = YES; INFOPLIST_FILE = chapter07/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter07; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/ch07/chapter07.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch07/chapter07.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate index 4118b49..43480b9 100644 Binary files a/ch07/chapter07.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate and b/ch07/chapter07.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch07/chapter07.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter07.xcscheme b/ch07/chapter07.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter07.xcscheme index 7ba25a7..c45abd3 100644 --- a/ch07/chapter07.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter07.xcscheme +++ b/ch07/chapter07.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter07.xcscheme @@ -1,6 +1,6 @@ Matrix { + func translationMatrix(_ matrix: Matrix, _ position: float3) -> Matrix { + var matrix = matrix matrix.m[12] = position.x matrix.m[13] = position.y matrix.m[14] = position.z return matrix } - func scalingMatrix(var matrix: Matrix, _ scale: Float) -> Matrix { + func scalingMatrix(_ matrix: Matrix, _ scale: Float) -> Matrix { + var matrix = matrix matrix.m[0] = scale matrix.m[5] = scale matrix.m[10] = scale @@ -39,7 +41,8 @@ struct Matrix { return matrix } - func rotationMatrix(var matrix: Matrix, _ rot: float3) -> Matrix { + func rotationMatrix(_ matrix: Matrix, _ rot: float3) -> Matrix { + var matrix = matrix matrix.m[0] = cos(rot.y) * cos(rot.z) matrix.m[4] = cos(rot.z) * sin(rot.x) * sin(rot.y) - cos(rot.x) * sin(rot.z) matrix.m[8] = cos(rot.x) * cos(rot.z) * sin(rot.y) + sin(rot.x) * sin(rot.z) @@ -53,7 +56,8 @@ struct Matrix { return matrix } - func modelMatrix(var matrix: Matrix) -> Matrix { + func modelMatrix(_ matrix: Matrix) -> Matrix { + var matrix = matrix matrix = rotationMatrix(matrix, float3(0.0, 0.0, 0.1)) matrix = scalingMatrix(matrix, 0.25) matrix = translationMatrix(matrix, float3(0.0, 0.5, 0.0)) diff --git a/ch07/chapter07/MetalView.swift b/ch07/chapter07/MetalView.swift index a042c98..975c572 100644 --- a/ch07/chapter07/MetalView.swift +++ b/ch07/chapter07/MetalView.swift @@ -10,57 +10,58 @@ import MetalKit class MetalView: MTKView { - var vertex_buffer: MTLBuffer! - var uniform_buffer: MTLBuffer! - var rps: MTLRenderPipelineState! = nil + var commandQueue: MTLCommandQueue? + var rps: MTLRenderPipelineState? + var vertexBuffer: MTLBuffer! + var uniformBuffer: MTLBuffer! required init(coder: NSCoder) { super.init(coder: coder) - device = MTLCreateSystemDefaultDevice() createBuffers() registerShaders() } - override func drawRect(dirtyRect: NSRect) { - super.drawRect(dirtyRect) - if let rpd = currentRenderPassDescriptor, drawable = currentDrawable { - rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0) - let command_buffer = device!.newCommandQueue().commandBuffer() - let command_encoder = command_buffer.renderCommandEncoderWithDescriptor(rpd) - command_encoder.setRenderPipelineState(rps) - command_encoder.setVertexBuffer(vertex_buffer, offset: 0, atIndex: 0) - command_encoder.setVertexBuffer(uniform_buffer, offset: 0, atIndex: 1) - command_encoder.drawPrimitives(.Triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) - command_encoder.endEncoding() - command_buffer.presentDrawable(drawable) - command_buffer.commit() - } - } - func createBuffers() { - let vertex_data = [ - Vertex(position: [-1.0, -1.0, 0.0, 1.0], color: [1, 0, 0, 1]), - Vertex(position: [ 1.0, -1.0, 0.0, 1.0], color: [0, 1, 0, 1]), - Vertex(position: [ 0.0, 1.0, 0.0, 1.0], color: [0, 0, 1, 1]) + device = MTLCreateSystemDefaultDevice()! + commandQueue = device!.newCommandQueue() + let vertex_data = [Vertex(position: [-1.0, -1.0, 0.0, 1.0], color: [1, 0, 0, 1]), + Vertex(position: [ 1.0, -1.0, 0.0, 1.0], color: [0, 1, 0, 1]), + Vertex(position: [ 0.0, 1.0, 0.0, 1.0], color: [0, 0, 1, 1]) ] - vertex_buffer = device!.newBufferWithBytes(vertex_data, length: sizeof(Vertex) * 3, options:[]) - uniform_buffer = device!.newBufferWithLength(sizeof(Float) * 16, options: []) - let bufferPointer = uniform_buffer.contents() - memcpy(bufferPointer, Matrix().modelMatrix(Matrix()).m, sizeof(Float) * 16) + vertexBuffer = device!.newBuffer(withBytes: vertex_data, length: sizeof(Vertex.self) * 3, options:[]) + uniformBuffer = device!.newBuffer(withLength: sizeof(Float.self) * 16, options: []) + let bufferPointer = uniformBuffer.contents() + memcpy(bufferPointer, Matrix().modelMatrix(Matrix()).m, sizeof(Float.self) * 16) } func registerShaders() { let library = device!.newDefaultLibrary()! - let vertex_func = library.newFunctionWithName("vertex_func") - let frag_func = library.newFunctionWithName("fragment_func") + let vertex_func = library.newFunction(withName: "vertex_func") + let frag_func = library.newFunction(withName: "fragment_func") let rpld = MTLRenderPipelineDescriptor() rpld.vertexFunction = vertex_func rpld.fragmentFunction = frag_func - rpld.colorAttachments[0].pixelFormat = .BGRA8Unorm + rpld.colorAttachments[0].pixelFormat = .bgra8Unorm do { - try rps = device!.newRenderPipelineStateWithDescriptor(rpld) + try rps = device!.newRenderPipelineState(with: rpld) } catch let error { self.print("\(error)") } } + + override func draw(_ dirtyRect: NSRect) { + super.draw(dirtyRect) + if let rpd = currentRenderPassDescriptor, drawable = currentDrawable { + rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0) + let commandBuffer = device!.newCommandQueue().commandBuffer() + let commandEncoder = commandBuffer.renderCommandEncoder(with: rpd) + commandEncoder.setRenderPipelineState(rps!) + commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0) + commandEncoder.setVertexBuffer(uniformBuffer, offset: 0, at: 1) + commandEncoder.drawPrimitives(.triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) + commandEncoder.endEncoding() + commandBuffer.present(drawable) + commandBuffer.commit() + } + } } diff --git a/ch08/chapter08.playground/Contents.swift b/ch08/chapter08.playground/Contents.swift index 96ca6ff..694d377 100644 --- a/ch08/chapter08.playground/Contents.swift +++ b/ch08/chapter08.playground/Contents.swift @@ -1,8 +1,9 @@ -import Cocoa -import XCPlayground +import MetalKit +import PlaygroundSupport -let device = MTLCreateSystemDefaultDevice()! -let frame = NSRect(x: 0, y: 0, width: 300, height: 300) -let view = MetalView(frame: frame, device: device) -XCPlaygroundPage.currentPage.liveView = view +let frame = NSRect(x: 0, y: 0, width: 400, height: 400) +let delegate = MetalView() +let view = MTKView(frame: frame, device: delegate.device) +view.delegate = delegate +PlaygroundPage.current.liveView = view diff --git a/ch08/chapter08.playground/Resources/Shaders.metal b/ch08/chapter08.playground/Resources/Shaders.metal index a9bad7e..2234835 100644 --- a/ch08/chapter08.playground/Resources/Shaders.metal +++ b/ch08/chapter08.playground/Resources/Shaders.metal @@ -11,7 +11,9 @@ struct Uniforms { float4x4 modelMatrix; }; -vertex Vertex vertex_func(constant Vertex *vertices [[buffer(0)]], constant Uniforms &uniforms [[buffer(1)]], uint vid [[vertex_id]]) { +vertex Vertex vertex_func(constant Vertex *vertices [[buffer(0)]], + constant Uniforms &uniforms [[buffer(1)]], + uint vid [[vertex_id]]) { float4x4 matrix = uniforms.modelMatrix; Vertex in = vertices[vid]; Vertex out; diff --git a/ch08/chapter08.playground/Sources/MathUtils.swift b/ch08/chapter08.playground/Sources/MathUtils.swift index cc13d82..3461891 100644 --- a/ch08/chapter08.playground/Sources/MathUtils.swift +++ b/ch08/chapter08.playground/Sources/MathUtils.swift @@ -21,14 +21,16 @@ struct Matrix { ] } - func translationMatrix(var matrix: Matrix, _ position: float3) -> Matrix { + func translationMatrix(_ matrix: Matrix, _ position: float3) -> Matrix { + var matrix = matrix matrix.m[12] = position.x matrix.m[13] = position.y matrix.m[14] = position.z return matrix } - func scalingMatrix(var matrix: Matrix, _ scale: Float) -> Matrix { + func scalingMatrix(_ matrix: Matrix, _ scale: Float) -> Matrix { + var matrix = matrix matrix.m[0] = scale matrix.m[5] = scale matrix.m[10] = scale @@ -36,7 +38,8 @@ struct Matrix { return matrix } - func rotationMatrix(var matrix: Matrix, _ rot: float3) -> Matrix { + func rotationMatrix(_ matrix: Matrix, _ rot: float3) -> Matrix { + var matrix = matrix matrix.m[0] = cos(rot.y) * cos(rot.z) matrix.m[4] = cos(rot.z) * sin(rot.x) * sin(rot.y) - cos(rot.x) * sin(rot.z) matrix.m[8] = cos(rot.x) * cos(rot.z) * sin(rot.y) + sin(rot.x) * sin(rot.z) @@ -50,7 +53,8 @@ struct Matrix { return matrix } - func modelMatrix(var matrix: Matrix) -> Matrix { + func modelMatrix(matrix: Matrix) -> Matrix { + var matrix = matrix matrix = rotationMatrix(matrix, float3(0.0, 0.0, 0.1)) matrix = scalingMatrix(matrix, 0.25) matrix = translationMatrix(matrix, float3(0.0, 0.5, 0.0)) diff --git a/ch08/chapter08.playground/Sources/MetalView.swift b/ch08/chapter08.playground/Sources/MetalView.swift index 9305067..61a864c 100644 --- a/ch08/chapter08.playground/Sources/MetalView.swift +++ b/ch08/chapter08.playground/Sources/MetalView.swift @@ -1,69 +1,68 @@ import MetalKit -public class MetalView: MTKView { +public class MetalView: NSObject, MTKViewDelegate { - var vertex_buffer: MTLBuffer! - var uniform_buffer: MTLBuffer! - var rps: MTLRenderPipelineState! = nil + public var device: MTLDevice! + var queue: MTLCommandQueue! + var vertexBuffer: MTLBuffer! + var uniformBuffer: MTLBuffer! + var rps: MTLRenderPipelineState! - required public init(coder: NSCoder) { - super.init(coder: coder) - } - - override public init(frame frameRect: CGRect, device: MTLDevice?) { - super.init(frame: frameRect, device: device) + override public init() { + super.init() createBuffers() registerShaders() } - override public func drawRect(dirtyRect: NSRect) { - super.drawRect(dirtyRect) - if let rpd = currentRenderPassDescriptor, drawable = currentDrawable { - rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0) - let command_buffer = device!.newCommandQueue().commandBuffer() - let command_encoder = command_buffer.renderCommandEncoderWithDescriptor(rpd) - command_encoder.setRenderPipelineState(rps) - command_encoder.setVertexBuffer(vertex_buffer, offset: 0, atIndex: 0) - command_encoder.setVertexBuffer(uniform_buffer, offset: 0, atIndex: 1) - command_encoder.drawPrimitives(.Triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) - command_encoder.endEncoding() - command_buffer.presentDrawable(drawable) - command_buffer.commit() - } - } - func createBuffers() { - let vertex_data = [ - Vertex(pos: [-1.0, -1.0, 0.0, 1.0], col: [1, 0, 0, 1]), - Vertex(pos: [ 1.0, -1.0, 0.0, 1.0], col: [0, 1, 0, 1]), - Vertex(pos: [ 0.0, 1.0, 0.0, 1.0], col: [0, 0, 1, 1]) + device = MTLCreateSystemDefaultDevice() + queue = device.newCommandQueue() + let vertexData = [Vertex(pos: [-1.0, -1.0, 0.0, 1.0], col: [1, 0, 0, 1]), + Vertex(pos: [ 1.0, -1.0, 0.0, 1.0], col: [0, 1, 0, 1]), + Vertex(pos: [ 0.0, 1.0, 0.0, 1.0], col: [0, 0, 1, 1]) ] - vertex_buffer = device!.newBufferWithBytes(vertex_data, length: sizeof(Vertex) * 3, options:[]) - uniform_buffer = device!.newBufferWithLength(sizeof(Float) * 16, options: []) - let bufferPointer = uniform_buffer.contents() - memcpy(bufferPointer, Matrix().modelMatrix(Matrix()).m, sizeof(Float) * 16) + vertexBuffer = device!.newBuffer(withBytes: vertexData, length: sizeof(Vertex.self) * 3, options:[]) + uniformBuffer = device!.newBuffer(withLength: sizeof(Float.self) * 16, options: []) + let bufferPointer = uniformBuffer.contents() + memcpy(bufferPointer, Matrix().modelMatrix(matrix: Matrix()).m, sizeof(Float.self) * 16) } func registerShaders() { - let path = NSBundle.mainBundle().pathForResource("Shaders", ofType: "metal") + let path = Bundle.main.pathForResource("Shaders", ofType: "metal") let input: String? let library: MTLLibrary let vert_func: MTLFunction let frag_func: MTLFunction do { - input = try String(contentsOfFile: path!, encoding: NSUTF8StringEncoding) - library = try device!.newLibraryWithSource(input!, options: nil) - //print(library.functionNames) - vert_func = library.newFunctionWithName("vertex_func")! - frag_func = library.newFunctionWithName("fragment_func")! + input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8) + library = try device!.newLibrary(withSource: input!, options: nil) + vert_func = library.newFunction(withName: "vertex_func")! + frag_func = library.newFunction(withName: "fragment_func")! let rpld = MTLRenderPipelineDescriptor() rpld.vertexFunction = vert_func rpld.fragmentFunction = frag_func - rpld.colorAttachments[0].pixelFormat = .BGRA8Unorm - rps = try device!.newRenderPipelineStateWithDescriptor(rpld) + rpld.colorAttachments[0].pixelFormat = .bgra8Unorm + rps = try device!.newRenderPipelineState(with: rpld) } catch let e { Swift.print("\(e)") } } + + public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {} + + public func draw(in view: MTKView) { + if let rpd = view.currentRenderPassDescriptor, let drawable = view.currentDrawable { + rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0) + let commandBuffer = queue.commandBuffer() + let commandEncoder = commandBuffer.renderCommandEncoder(with: rpd) + commandEncoder.setRenderPipelineState(rps) + commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0) + commandEncoder.setVertexBuffer(uniformBuffer, offset: 0, at: 1) + commandEncoder.drawPrimitives(.triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) + commandEncoder.endEncoding() + commandBuffer.present(drawable) + commandBuffer.commit() + } + } } diff --git a/ch08/chapter08.playground/contents.xcplayground b/ch08/chapter08.playground/contents.xcplayground index 06828af..9f9eecc 100644 --- a/ch08/chapter08.playground/contents.xcplayground +++ b/ch08/chapter08.playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/ch08/chapter08.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch08/chapter08.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate index 4b12b70..9826afd 100644 Binary files a/ch08/chapter08.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate and b/ch08/chapter08.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch09/chapter09.playground/Contents.swift b/ch09/chapter09.playground/Contents.swift index 96ca6ff..694d377 100644 --- a/ch09/chapter09.playground/Contents.swift +++ b/ch09/chapter09.playground/Contents.swift @@ -1,8 +1,9 @@ -import Cocoa -import XCPlayground +import MetalKit +import PlaygroundSupport -let device = MTLCreateSystemDefaultDevice()! -let frame = NSRect(x: 0, y: 0, width: 300, height: 300) -let view = MetalView(frame: frame, device: device) -XCPlaygroundPage.currentPage.liveView = view +let frame = NSRect(x: 0, y: 0, width: 400, height: 400) +let delegate = MetalView() +let view = MTKView(frame: frame, device: delegate.device) +view.delegate = delegate +PlaygroundPage.current.liveView = view diff --git a/ch09/chapter09.playground/Sources/MathUtils.swift b/ch09/chapter09.playground/Sources/MathUtils.swift index 81ef738..cd61a82 100755 --- a/ch09/chapter09.playground/Sources/MathUtils.swift +++ b/ch09/chapter09.playground/Sources/MathUtils.swift @@ -30,7 +30,7 @@ func scalingMatrix(scale: Float) -> matrix_float4x4 { return matrix_float4x4(columns:(X, Y, Z, W)) } -func rotationMatrix(angle: Float, _ axis: vector_float3) -> matrix_float4x4 { +func rotationMatrix(angle: Float, axis: vector_float3) -> matrix_float4x4 { var X = vector_float4(0, 0, 0, 0) X.x = axis.x * axis.x + (1 - axis.x * axis.x) * cos(angle) X.y = axis.x * axis.y * (1 - cos(angle)) - axis.z * sin(angle) diff --git a/ch09/chapter09.playground/Sources/MetalView.swift b/ch09/chapter09.playground/Sources/MetalView.swift index 27e9042..769c51d 100755 --- a/ch09/chapter09.playground/Sources/MetalView.swift +++ b/ch09/chapter09.playground/Sources/MetalView.swift @@ -1,28 +1,26 @@ import MetalKit -public class MetalView: MTKView { +public class MetalView: NSObject, MTKViewDelegate { - var vertex_buffer: MTLBuffer! - var uniform_buffer: MTLBuffer! - var index_buffer: MTLBuffer! - var dss: MTLDepthStencilState! + public var device: MTLDevice! + var queue: MTLCommandQueue! + var vertexBuffer: MTLBuffer! + var uniformBuffer: MTLBuffer! + var indexBuffer: MTLBuffer! var rps: MTLRenderPipelineState! - var commandQueue: MTLCommandQueue! var rotation: Float = 0 - required public init(coder: NSCoder) { - super.init(coder: coder) - } - - override public init(frame frameRect: CGRect, device: MTLDevice?) { - super.init(frame: frameRect, device: device) + override public init() { + super.init() + device = MTLCreateSystemDefaultDevice() + queue = device.newCommandQueue() createBuffers() createPipeline() } func createBuffers() { - let vertex_data = [ + let vertexData = [ Vertex(pos: [-1.0, -1.0, 1.0, 1.0], col: [1, 1, 1, 1]), Vertex(pos: [ 1.0, -1.0, 1.0, 1.0], col: [1, 0, 0, 1]), Vertex(pos: [ 1.0, 1.0, 1.0, 1.0], col: [1, 1, 0, 1]), @@ -30,71 +28,75 @@ public class MetalView: MTKView { Vertex(pos: [-1.0, -1.0, -1.0, 1.0], col: [0, 0, 1, 1]), Vertex(pos: [ 1.0, -1.0, -1.0, 1.0], col: [1, 0, 1, 1]), Vertex(pos: [ 1.0, 1.0, -1.0, 1.0], col: [0, 0, 0, 1]), - Vertex(pos: [-1.0, 1.0, -1.0, 1.0], col: [0, 1, 1, 1]) - ] - let index_data: [UInt16] = [ - 0, 1, 2, 2, 3, 0, // front - 1, 5, 6, 6, 2, 1, // right - 3, 2, 6, 6, 7, 3, // top - 4, 5, 1, 1, 0, 4, // bottom - 4, 0, 3, 3, 7, 4, // left - 7, 6, 5, 5, 4, 7, // back - ] - vertex_buffer = device!.newBufferWithBytes(vertex_data, length: sizeof(Vertex) * vertex_data.count, options: []) - index_buffer = device!.newBufferWithBytes(index_data, length: sizeof(UInt16) * index_data.count , options: []) - uniform_buffer = device!.newBufferWithLength(sizeof(matrix_float4x4), options: []) + Vertex(pos: [-1.0, 1.0, -1.0, 1.0], col: [0, 1, 1, 1])] + + let indexData: [UInt16] = [0, 1, 2, 2, 3, 0, // front + 1, 5, 6, 6, 2, 1, // right + 3, 2, 6, 6, 7, 3, // top + 4, 5, 1, 1, 0, 4, // bottom + 4, 0, 3, 3, 7, 4, // left + 7, 6, 5, 5, 4, 7] // back + + vertexBuffer = device!.newBuffer(withBytes: vertexData, length: sizeof(Vertex.self) * vertexData.count, options: []) + indexBuffer = device!.newBuffer(withBytes: indexData, length: sizeof(UInt16.self) * indexData.count , options: []) + uniformBuffer = device!.newBuffer(withLength: sizeof(matrix_float4x4.self), options: []) } func createPipeline() { + let path = Bundle.main.pathForResource("Shaders", ofType: "metal") + let input: String? + let library: MTLLibrary + let vert_func: MTLFunction + let frag_func: MTLFunction do { - let path = NSBundle.mainBundle().pathForResource("Shaders", ofType: "metal") - let input = try String(contentsOfFile: path!, encoding: NSUTF8StringEncoding) - let library = try device!.newLibraryWithSource(input, options: nil) - let vert_func = library.newFunctionWithName("vertex_func")! - let frag_func = library.newFunctionWithName("fragment_func")! + input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8) + library = try device!.newLibrary(withSource: input!, options: nil) + vert_func = library.newFunction(withName: "vertex_func")! + frag_func = library.newFunction(withName: "fragment_func")! let rpld = MTLRenderPipelineDescriptor() rpld.vertexFunction = vert_func rpld.fragmentFunction = frag_func - rpld.colorAttachments[0].pixelFormat = .BGRA8Unorm - rps = try device!.newRenderPipelineStateWithDescriptor(rpld) + rpld.colorAttachments[0].pixelFormat = .bgra8Unorm + rps = try device!.newRenderPipelineState(with: rpld) } catch let e { Swift.print("\(e)") } - commandQueue = device?.newCommandQueue() } func update() { - let scaled = scalingMatrix(0.5) + let scaled = scalingMatrix(scale: 0.5) rotation += 1 / 100 * Float(M_PI) / 4 - let rotatedY = rotationMatrix(rotation, float3(0, 1, 0)) - let rotatedX = rotationMatrix(Float(M_PI) / 4, float3(1, 0, 0)) + let rotatedY = rotationMatrix(angle: rotation, axis: float3(0, 1, 0)) + let rotatedX = rotationMatrix(angle: Float(M_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(cameraPosition) - let aspect = Float(drawableSize.width / drawableSize.height) - let projMatrix = projectionMatrix(0, far: 10, aspect: aspect, fovy: 1) + let viewMatrix = translationMatrix(position: cameraPosition) +// let aspect = Float(view.drawableSize.width / view.drawableSize.height) + let projMatrix = projectionMatrix(near: 0, far: 10, aspect: 1, fovy: 1) +// let projMatrix = projectionMatrix(0, far: 10, aspect: aspect, fovy: 1) let modelViewProjectionMatrix = matrix_multiply(projMatrix, matrix_multiply(viewMatrix, modelMatrix)) - let bufferPointer = uniform_buffer.contents() + let bufferPointer = uniformBuffer.contents() var uniforms = Uniforms(modelViewProjectionMatrix: modelViewProjectionMatrix) - memcpy(bufferPointer, &uniforms, sizeof(Uniforms)) + memcpy(bufferPointer, &uniforms, sizeof(Uniforms.self)) } - override public func drawRect(dirtyRect: NSRect) { - super.drawRect(dirtyRect) + public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {} + + public func draw(in view: MTKView) { update() - if let rpd = currentRenderPassDescriptor, drawable = currentDrawable { - rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.3, 0.5, 0.5, 1.0) - let command_buffer = commandQueue.commandBuffer() - let command_encoder = command_buffer.renderCommandEncoderWithDescriptor(rpd) - command_encoder.setRenderPipelineState(rps) - command_encoder.setFrontFacingWinding(.CounterClockwise) - command_encoder.setCullMode(.Back) - command_encoder.setVertexBuffer(vertex_buffer, offset: 0, atIndex: 0) - command_encoder.setVertexBuffer(uniform_buffer, offset: 0, atIndex: 1) - command_encoder.drawIndexedPrimitives(.Triangle, indexCount: index_buffer.length / sizeof(UInt16), indexType: MTLIndexType.UInt16, indexBuffer: index_buffer, indexBufferOffset: 0) - command_encoder.endEncoding() - command_buffer.presentDrawable(drawable) - command_buffer.commit() + if let rpd = view.currentRenderPassDescriptor, let drawable = view.currentDrawable { + rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0) + let commandBuffer = queue.commandBuffer() + let commandEncoder = commandBuffer.renderCommandEncoder(with: 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.drawIndexedPrimitives(.triangle, indexCount: indexBuffer.length / sizeof(UInt16.self), indexType: MTLIndexType.uInt16, indexBuffer: indexBuffer, indexBufferOffset: 0) + commandEncoder.endEncoding() + commandBuffer.present(drawable) + commandBuffer.commit() } } } diff --git a/ch09/chapter09.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch09/chapter09.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate index 6294b51..1d5e4b9 100755 Binary files a/ch09/chapter09.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate and b/ch09/chapter09.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch09/chapter09.playground/timeline.xctimeline b/ch09/chapter09.playground/timeline.xctimeline deleted file mode 100644 index bf468af..0000000 --- a/ch09/chapter09.playground/timeline.xctimeline +++ /dev/null @@ -1,6 +0,0 @@ - - - - - diff --git a/ch10/chapter10.playground/Contents.swift b/ch10/chapter10.playground/Contents.swift old mode 100755 new mode 100644 index 96ca6ff..694d377 --- a/ch10/chapter10.playground/Contents.swift +++ b/ch10/chapter10.playground/Contents.swift @@ -1,8 +1,9 @@ -import Cocoa -import XCPlayground +import MetalKit +import PlaygroundSupport -let device = MTLCreateSystemDefaultDevice()! -let frame = NSRect(x: 0, y: 0, width: 300, height: 300) -let view = MetalView(frame: frame, device: device) -XCPlaygroundPage.currentPage.liveView = view +let frame = NSRect(x: 0, y: 0, width: 400, height: 400) +let delegate = MetalView() +let view = MTKView(frame: frame, device: delegate.device) +view.delegate = delegate +PlaygroundPage.current.liveView = view diff --git a/ch10/chapter10.playground/Sources/MetalView.swift b/ch10/chapter10.playground/Sources/MetalView.swift index 6cbc65c..393222e 100755 --- a/ch10/chapter10.playground/Sources/MetalView.swift +++ b/ch10/chapter10.playground/Sources/MetalView.swift @@ -1,46 +1,45 @@ import MetalKit -public class MetalView: MTKView { +public class MetalView: NSObject, MTKViewDelegate { - var queue: MTLCommandQueue! = nil - var cps: MTLComputePipelineState! = nil + public var device: MTLDevice! + var queue: MTLCommandQueue! + var cps: MTLComputePipelineState! - required public init(coder: NSCoder) { - super.init(coder: coder) - } - - override public init(frame frameRect: CGRect, device: MTLDevice?) { - super.init(frame: frameRect, device: device) + override public init() { + super.init() registerShaders() } - override public func drawRect(dirtyRect: NSRect) { - super.drawRect(dirtyRect) - if let drawable = currentDrawable { - let command_buffer = queue.commandBuffer() - let command_encoder = command_buffer.computeCommandEncoder() - command_encoder.setComputePipelineState(cps) - command_encoder.setTexture(drawable.texture, atIndex: 0) - let threadGroupCount = MTLSizeMake(8, 8, 1) - let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1) - command_encoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount) - command_encoder.endEncoding() - command_buffer.presentDrawable(drawable) - command_buffer.commit() - } - } - func registerShaders() { - queue = device!.newCommandQueue() - let path = NSBundle.mainBundle().pathForResource("Shaders", ofType: "metal") + device = MTLCreateSystemDefaultDevice()! + queue = device.newCommandQueue() + let path = Bundle.main.pathForResource("Shaders", ofType: "metal") do { - let input = try String(contentsOfFile: path!, encoding: NSUTF8StringEncoding) - let library = try device!.newLibraryWithSource(input, options: nil) - let kernel = library.newFunctionWithName("compute")! - cps = try device!.newComputePipelineStateWithFunction(kernel) + 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() + } + } } diff --git a/ch10/chapter10.playground/contents.xcplayground b/ch10/chapter10.playground/contents.xcplayground old mode 100755 new mode 100644 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 b0371a6..4591e06 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/ch10/chapter10.playground/timeline.xctimeline b/ch10/chapter10.playground/timeline.xctimeline deleted file mode 100755 index bf468af..0000000 --- a/ch10/chapter10.playground/timeline.xctimeline +++ /dev/null @@ -1,6 +0,0 @@ - - - - - diff --git a/ch11/chapter11.playground/Contents.swift b/ch11/chapter11.playground/Contents.swift index 00f436f..694d377 100644 --- a/ch11/chapter11.playground/Contents.swift +++ b/ch11/chapter11.playground/Contents.swift @@ -1,8 +1,9 @@ -import Cocoa -import XCPlayground +import MetalKit +import PlaygroundSupport -let device = MTLCreateSystemDefaultDevice()! -let frame = NSRect(x:0, y:0, width:300, height:300) -let view = MetalView(frame: frame, device: device) -XCPlaygroundPage.currentPage.liveView = view +let frame = NSRect(x: 0, y: 0, width: 400, height: 400) +let delegate = MetalView() +let view = MTKView(frame: frame, device: delegate.device) +view.delegate = delegate +PlaygroundPage.current.liveView = view diff --git a/ch11/chapter11.playground/Sources/MetalView.swift b/ch11/chapter11.playground/Sources/MetalView.swift index 425569a..db61db1 100755 --- a/ch11/chapter11.playground/Sources/MetalView.swift +++ b/ch11/chapter11.playground/Sources/MetalView.swift @@ -1,56 +1,45 @@ import MetalKit -public class MetalView: MTKView { +public class MetalView: NSObject, MTKViewDelegate { - var queue: MTLCommandQueue! = nil - var cps: MTLComputePipelineState! = nil + public var device: MTLDevice! + var queue: MTLCommandQueue! + var cps: MTLComputePipelineState! - var shader = - "#include \n" + - "using namespace metal;" + - "kernel void compute(texture2d output [[texture(0)]]," + - " uint2 gid [[thread_position_in_grid]])" + - "{" + - " output.write(float4(1, 1, 0, 1), gid);" + - "}" - - required public init(coder: NSCoder) { - super.init(coder: coder) - } - - override public init(frame frameRect: CGRect, device: MTLDevice?) { - super.init(frame: frameRect, device: device) + override public init() { + super.init() registerShaders() } - override public func drawRect(dirtyRect: NSRect) { - super.drawRect(dirtyRect) - if let drawable = currentDrawable { - let command_buffer = queue.commandBuffer() - let command_encoder = command_buffer.computeCommandEncoder() - command_encoder.setComputePipelineState(cps) - command_encoder.setTexture(drawable.texture, atIndex: 0) - let threadGroupCount = MTLSizeMake(8, 8, 1) - let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1) - command_encoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount) - command_encoder.endEncoding() - command_buffer.presentDrawable(drawable) - command_buffer.commit() - } - } - func registerShaders() { - queue = device!.newCommandQueue() - let path = NSBundle.mainBundle().pathForResource("Shaders", ofType: "metal") + device = MTLCreateSystemDefaultDevice()! + queue = device.newCommandQueue() + let path = Bundle.main.pathForResource("Shaders", ofType: "metal") do { - let input = try String(contentsOfFile: path!, encoding: NSUTF8StringEncoding) - let library = try device!.newLibraryWithSource(input, options: nil) -// let library = try device!.newLibraryWithSource(shader, options: nil) - let kernel = library.newFunctionWithName("compute")! - cps = try device!.newComputePipelineStateWithFunction(kernel) + 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() + } + } } 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 f4c9511..81d391f 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 diff --git a/ch11/chapter11.playground/timeline.xctimeline b/ch11/chapter11.playground/timeline.xctimeline deleted file mode 100644 index bf468af..0000000 --- a/ch11/chapter11.playground/timeline.xctimeline +++ /dev/null @@ -1,6 +0,0 @@ - - - - - diff --git a/ch12/chapter12.playground/Contents.swift b/ch12/chapter12.playground/Contents.swift index 9faf781..939e603 100644 --- a/ch12/chapter12.playground/Contents.swift +++ b/ch12/chapter12.playground/Contents.swift @@ -1,8 +1,8 @@ -import Cocoa -import XCPlayground +import MetalKit +import PlaygroundSupport let device = MTLCreateSystemDefaultDevice()! -let frame = NSRect(x:0, y:0, width:300, height:300) -let metalView = MetalView(frame: frame, device: device) -XCPlaygroundPage.currentPage.liveView = metalView +let frame = NSRect(x: 0, y: 0, width: 400, height: 400) +let view = MetalView(frame: frame, device: device) +PlaygroundPage.current.liveView = view diff --git a/ch12/chapter12.playground/Sources/MetalView.swift b/ch12/chapter12.playground/Sources/MetalView.swift index 37c8100..9cc48fd 100755 --- a/ch12/chapter12.playground/Sources/MetalView.swift +++ b/ch12/chapter12.playground/Sources/MetalView.swift @@ -10,8 +10,8 @@ public class MetalView: MTKView, NSWindowDelegate { var mouseBuffer: MTLBuffer! var pos: NSPoint! - override public func mouseDown(event: NSEvent) { - pos = convertPointToLayer(convertPoint(event.locationInWindow, fromView: nil)) + override public func mouseDown(_ event: NSEvent) { + pos = convertToLayer(convert(event.locationInWindow, from: nil)) let scale = layer!.contentsScale pos.x *= scale pos.y *= scale @@ -26,21 +26,21 @@ public class MetalView: MTKView, NSWindowDelegate { registerShaders() } - override public func drawRect(dirtyRect: NSRect) { - super.drawRect(dirtyRect) + override public func draw(_ dirtyRect: NSRect) { + super.draw(dirtyRect) if let drawable = currentDrawable { let commandBuffer = queue.commandBuffer() let commandEncoder = commandBuffer.computeCommandEncoder() commandEncoder.setComputePipelineState(cps) - commandEncoder.setTexture(drawable.texture, atIndex: 0) - commandEncoder.setBuffer(mouseBuffer, offset: 0, atIndex: 2) - commandEncoder.setBuffer(timerBuffer, offset: 0, atIndex: 1) + commandEncoder.setTexture(drawable.texture, at: 0) + commandEncoder.setBuffer(mouseBuffer, offset: 0, at: 2) + commandEncoder.setBuffer(timerBuffer, offset: 0, at: 1) update() 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.presentDrawable(drawable) + commandBuffer.present(drawable) commandBuffer.commit() } @@ -49,23 +49,23 @@ public class MetalView: MTKView, NSWindowDelegate { func update() { timer += 0.01 var bufferPointer = timerBuffer.contents() - memcpy(bufferPointer, &timer, sizeof(Float)) + memcpy(bufferPointer, &timer, sizeof(Float.self)) bufferPointer = mouseBuffer.contents() - memcpy(bufferPointer, &pos, sizeof(NSPoint)) + memcpy(bufferPointer, &pos, sizeof(NSPoint.self)) } func registerShaders() { queue = device!.newCommandQueue() - let path = NSBundle.mainBundle().pathForResource("Shaders", ofType: "metal") + let path = Bundle.main.pathForResource("Shaders", ofType: "metal") do { - let input = try String(contentsOfFile: path!, encoding: NSUTF8StringEncoding) - let library = try device!.newLibraryWithSource(input, options: nil) - let kernel = library.newFunctionWithName("compute")! - cps = try device!.newComputePipelineStateWithFunction(kernel) + 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)") } - timerBuffer = device!.newBufferWithLength(sizeof(Float), options: []) - mouseBuffer = device!.newBufferWithLength(sizeof(NSPoint), options: []) + timerBuffer = device!.newBuffer(withLength: sizeof(Float.self), options: []) + mouseBuffer = device!.newBuffer(withLength: sizeof(NSPoint.self), options: []) } } diff --git a/ch12/chapter12.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch12/chapter12.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate index 746466e..844b8df 100755 Binary files a/ch12/chapter12.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate and b/ch12/chapter12.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch13/chapter13.playground/Contents.swift b/ch13/chapter13.playground/Contents.swift index 366d424..feabd6e 100644 --- a/ch13/chapter13.playground/Contents.swift +++ b/ch13/chapter13.playground/Contents.swift @@ -1,8 +1,8 @@ -import Cocoa -import XCPlayground +import MetalKit +import PlaygroundSupport let device = MTLCreateSystemDefaultDevice()! let frame = NSRect(x:0, y:0, width:400, height:400) let view = MetalView(frame: frame, device: device) -XCPlaygroundPage.currentPage.liveView = view +PlaygroundPage.current.liveView = view diff --git a/ch13/chapter13.playground/Sources/MetalView.swift b/ch13/chapter13.playground/Sources/MetalView.swift index 7120f0b..2a6bdcb 100755 --- a/ch13/chapter13.playground/Sources/MetalView.swift +++ b/ch13/chapter13.playground/Sources/MetalView.swift @@ -10,8 +10,8 @@ public class MetalView: MTKView, NSWindowDelegate { var mouseBuffer: MTLBuffer! var pos: NSPoint! - override public func mouseDown(event: NSEvent) { - pos = convertPointToLayer(convertPoint(event.locationInWindow, fromView: nil)) + override public func mouseDown(_ event: NSEvent) { + pos = convertToLayer(convert(event.locationInWindow, from: nil)) let scale = layer!.contentsScale pos.x *= scale pos.y *= scale @@ -26,20 +26,20 @@ public class MetalView: MTKView, NSWindowDelegate { registerShaders() } - override public func drawRect(dirtyRect: NSRect) { + override public func draw(_ dirtyRect: NSRect) { if let drawable = currentDrawable { let commandBuffer = queue.commandBuffer() let commandEncoder = commandBuffer.computeCommandEncoder() commandEncoder.setComputePipelineState(cps) - commandEncoder.setTexture(drawable.texture, atIndex: 0) - commandEncoder.setBuffer(mouseBuffer, offset: 0, atIndex: 2) - commandEncoder.setBuffer(timerBuffer, offset: 0, atIndex: 1) + commandEncoder.setTexture(drawable.texture, at: 0) + commandEncoder.setBuffer(mouseBuffer, offset: 0, at: 2) + commandEncoder.setBuffer(timerBuffer, offset: 0, at: 1) update() 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.presentDrawable(drawable) + commandBuffer.present(drawable) commandBuffer.commit() } @@ -48,23 +48,23 @@ public class MetalView: MTKView, NSWindowDelegate { func update() { timer += 0.01 var bufferPointer = timerBuffer.contents() - memcpy(bufferPointer, &timer, sizeof(Float)) + memcpy(bufferPointer, &timer, sizeof(Float.self)) bufferPointer = mouseBuffer.contents() - memcpy(bufferPointer, &pos, sizeof(NSPoint)) + memcpy(bufferPointer, &pos, sizeof(NSPoint.self)) } func registerShaders() { queue = device!.newCommandQueue() - let path = NSBundle.mainBundle().pathForResource("Shaders", ofType: "metal") + let path = Bundle.main.pathForResource("Shaders", ofType: "metal") do { - let input = try String(contentsOfFile: path!, encoding: NSUTF8StringEncoding) - let library = try device!.newLibraryWithSource(input, options: nil) - let kernel = library.newFunctionWithName("compute")! - cps = try device!.newComputePipelineStateWithFunction(kernel) + 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)") } - timerBuffer = device!.newBufferWithLength(sizeof(Float), options: []) - mouseBuffer = device!.newBufferWithLength(sizeof(NSPoint), options: []) + timerBuffer = device!.newBuffer(withLength: sizeof(Float.self), options: []) + mouseBuffer = device!.newBuffer(withLength: sizeof(NSPoint.self), options: []) } } diff --git a/ch13/chapter13.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch13/chapter13.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate index ac9ca7b..4c407cd 100755 Binary files a/ch13/chapter13.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate and b/ch13/chapter13.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch14/chapter14.playground/Contents.swift b/ch14/chapter14.playground/Contents.swift index 366d424..abfc392 100644 --- a/ch14/chapter14.playground/Contents.swift +++ b/ch14/chapter14.playground/Contents.swift @@ -1,8 +1,8 @@ import Cocoa -import XCPlayground +import PlaygroundSupport let device = MTLCreateSystemDefaultDevice()! let frame = NSRect(x:0, y:0, width:400, height:400) let view = MetalView(frame: frame, device: device) -XCPlaygroundPage.currentPage.liveView = view +PlaygroundPage.current.liveView = view diff --git a/ch14/chapter14.playground/Sources/MetalView.swift b/ch14/chapter14.playground/Sources/MetalView.swift index 7120f0b..2a6bdcb 100755 --- a/ch14/chapter14.playground/Sources/MetalView.swift +++ b/ch14/chapter14.playground/Sources/MetalView.swift @@ -10,8 +10,8 @@ public class MetalView: MTKView, NSWindowDelegate { var mouseBuffer: MTLBuffer! var pos: NSPoint! - override public func mouseDown(event: NSEvent) { - pos = convertPointToLayer(convertPoint(event.locationInWindow, fromView: nil)) + override public func mouseDown(_ event: NSEvent) { + pos = convertToLayer(convert(event.locationInWindow, from: nil)) let scale = layer!.contentsScale pos.x *= scale pos.y *= scale @@ -26,20 +26,20 @@ public class MetalView: MTKView, NSWindowDelegate { registerShaders() } - override public func drawRect(dirtyRect: NSRect) { + override public func draw(_ dirtyRect: NSRect) { if let drawable = currentDrawable { let commandBuffer = queue.commandBuffer() let commandEncoder = commandBuffer.computeCommandEncoder() commandEncoder.setComputePipelineState(cps) - commandEncoder.setTexture(drawable.texture, atIndex: 0) - commandEncoder.setBuffer(mouseBuffer, offset: 0, atIndex: 2) - commandEncoder.setBuffer(timerBuffer, offset: 0, atIndex: 1) + commandEncoder.setTexture(drawable.texture, at: 0) + commandEncoder.setBuffer(mouseBuffer, offset: 0, at: 2) + commandEncoder.setBuffer(timerBuffer, offset: 0, at: 1) update() 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.presentDrawable(drawable) + commandBuffer.present(drawable) commandBuffer.commit() } @@ -48,23 +48,23 @@ public class MetalView: MTKView, NSWindowDelegate { func update() { timer += 0.01 var bufferPointer = timerBuffer.contents() - memcpy(bufferPointer, &timer, sizeof(Float)) + memcpy(bufferPointer, &timer, sizeof(Float.self)) bufferPointer = mouseBuffer.contents() - memcpy(bufferPointer, &pos, sizeof(NSPoint)) + memcpy(bufferPointer, &pos, sizeof(NSPoint.self)) } func registerShaders() { queue = device!.newCommandQueue() - let path = NSBundle.mainBundle().pathForResource("Shaders", ofType: "metal") + let path = Bundle.main.pathForResource("Shaders", ofType: "metal") do { - let input = try String(contentsOfFile: path!, encoding: NSUTF8StringEncoding) - let library = try device!.newLibraryWithSource(input, options: nil) - let kernel = library.newFunctionWithName("compute")! - cps = try device!.newComputePipelineStateWithFunction(kernel) + 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)") } - timerBuffer = device!.newBufferWithLength(sizeof(Float), options: []) - mouseBuffer = device!.newBufferWithLength(sizeof(NSPoint), options: []) + timerBuffer = device!.newBuffer(withLength: sizeof(Float.self), options: []) + mouseBuffer = device!.newBuffer(withLength: sizeof(NSPoint.self), options: []) } } 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 a985682..bbb890a 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/ch15/chapter15.playground/Contents.swift b/ch15/chapter15.playground/Contents.swift old mode 100755 new mode 100644 index 366d424..694d377 --- a/ch15/chapter15.playground/Contents.swift +++ b/ch15/chapter15.playground/Contents.swift @@ -1,8 +1,9 @@ -import Cocoa -import XCPlayground +import MetalKit +import PlaygroundSupport -let device = MTLCreateSystemDefaultDevice()! -let frame = NSRect(x:0, y:0, width:400, height:400) -let view = MetalView(frame: frame, device: device) -XCPlaygroundPage.currentPage.liveView = view +let frame = NSRect(x: 0, y: 0, width: 400, height: 400) +let delegate = MetalView() +let view = MTKView(frame: frame, device: delegate.device) +view.delegate = delegate +PlaygroundPage.current.liveView = view diff --git a/ch15/chapter15.playground/Sources/MetalView.swift b/ch15/chapter15.playground/Sources/MetalView.swift index fd4ce12..39c41e9 100755 --- a/ch15/chapter15.playground/Sources/MetalView.swift +++ b/ch15/chapter15.playground/Sources/MetalView.swift @@ -1,66 +1,65 @@ import MetalKit -public class MetalView: MTKView, NSWindowDelegate { +public class MetalView: NSObject, MTKViewDelegate { + public var device: MTLDevice! var queue: MTLCommandQueue! = nil var cps: MTLComputePipelineState! = nil var timer: Float = 0 var timerBuffer: MTLBuffer! var texture: MTLTexture! - required public init(coder: NSCoder) { - super.init(coder: coder) - } - - override public init(frame frameRect: CGRect, device: MTLDevice?) { - super.init(frame: frameRect, device: device) - registerShaders() + override public init() { + super.init() + device = MTLCreateSystemDefaultDevice()! + queue = device!.newCommandQueue() setUpTexture() + registerShaders() } func setUpTexture() { - let path = NSBundle.mainBundle().pathForResource("texture", ofType: "jpg") + let path = Bundle.main.pathForResource("texture", ofType: "jpg") let textureLoader = MTKTextureLoader(device: device!) - texture = try! textureLoader.newTextureWithContentsOfURL(NSURL(fileURLWithPath: path!), options: nil) + texture = try! textureLoader.newTexture(withContentsOf: URL(fileURLWithPath: path!), options: nil) } - override public func drawRect(dirtyRect: NSRect) { - if let drawable = currentDrawable { + func registerShaders() { + 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)") + } + timerBuffer = device!.newBuffer(withLength: sizeof(Float.self), options: []) + } + + func update() { + timer += 0.01 + let bufferPointer = timerBuffer.contents() + memcpy(bufferPointer, &timer, sizeof(Float.self)) + } + + 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, atIndex: 0) - commandEncoder.setTexture(texture, atIndex: 1) - commandEncoder.setBuffer(timerBuffer, offset: 0, atIndex: 0) + commandEncoder.setTexture(drawable.texture, at: 0) + commandEncoder.setTexture(texture, 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) commandEncoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount) commandEncoder.endEncoding() - commandBuffer.presentDrawable(drawable) + commandBuffer.present(drawable) commandBuffer.commit() } - - } - - func update() { - timer += 0.01 - let bufferPointer = timerBuffer.contents() - memcpy(bufferPointer, &timer, sizeof(Float)) - } - - func registerShaders() { - queue = device!.newCommandQueue() - let path = NSBundle.mainBundle().pathForResource("Shaders", ofType: "metal") - do { - let input = try String(contentsOfFile: path!, encoding: NSUTF8StringEncoding) - let library = try device!.newLibraryWithSource(input, options: nil) - let kernel = library.newFunctionWithName("compute")! - cps = try device!.newComputePipelineStateWithFunction(kernel) - } catch let e { - Swift.print("\(e)") - } - timerBuffer = device!.newBufferWithLength(sizeof(Float), options: []) } } diff --git a/ch15/chapter15.playground/contents.xcplayground b/ch15/chapter15.playground/contents.xcplayground old mode 100755 new mode 100644 diff --git a/ch15/chapter15.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch15/chapter15.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate index 14e3d06..c4d17f0 100755 Binary files a/ch15/chapter15.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate and b/ch15/chapter15.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch15/chapter15.playground/timeline.xctimeline b/ch15/chapter15.playground/timeline.xctimeline old mode 100755 new mode 100644