commit ea49c7202cb5d843a278e263d1522ffad9296c97 Author: Marius Horga Date: Wed May 25 13:46:57 2016 +0300 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/README.md b/README.md new file mode 100644 index 0000000..c6d03ea --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# Metal + +Repository to accompany the following blog posts: +- [Introducing the Metal framework](http://mhorga.org/2016/01/04/introducing-the-metal-framework.html) +- [Using MetalKit part 1](http://mhorga.org/2016/01/11/using-metalkit-part-1.html) +- [Using MetalKit part 2](http://mhorga.org/2016/01/18/using-metalkit-part-2.html) +- [Using MetalKit part 3](http://mhorga.org/2016/01/25/using-metalkit-part-3.html) +- [Using MetalKit part 4](http://mhorga.org/2016/02/01/using-metalkit-part-4.html) +- [Using MetalKit part 5](http://mhorga.org/2016/02/08/using-metalkit-part-5.html) +- [Using MetalKit part 6](http://mhorga.org/2016/02/15/using-metalkit-part-6.html) +- [Using MetalKit part 7](http://mhorga.org/2016/02/29/using-metalkit-part-7.html) +- [Using MetalKit part 8](http://mhorga.org/2016/03/07/using-metalkit-part-8.html) +- [Using MetalKit part 9](http://mhorga.org/2016/04/18/using-metalkit-part-9.html) +- [Using MetalKit part 10](http://mhorga.org/2016/05/02/using-metalkit-part-10.html) +- [Using MetalKit part 11](http://mhorga.org/2016/05/10/using-metalkit-part-11.html) +- [Using MetalKit part 12](http://mhorga.org/2016/05/18/using-metalkit-part-12.html) \ No newline at end of file diff --git a/ch01/chapter01.xcodeproj/project.pbxproj b/ch01/chapter01.xcodeproj/project.pbxproj new file mode 100644 index 0000000..2d5df56 --- /dev/null +++ b/ch01/chapter01.xcodeproj/project.pbxproj @@ -0,0 +1,280 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* 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 */ + +/* Begin PBXFileReference section */ + 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 */ + +/* Begin PBXFrameworksBuildPhase section */ + 29400C761C3AEE1300345568 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 29400C701C3AEE1300345568 = { + isa = PBXGroup; + children = ( + 29400C7B1C3AEE1300345568 /* chapter01 */, + 29400C7A1C3AEE1300345568 /* Products */, + ); + sourceTree = ""; + }; + 29400C7A1C3AEE1300345568 /* Products */ = { + isa = PBXGroup; + children = ( + 29400C791C3AEE1300345568 /* chapter01.app */, + ); + name = Products; + sourceTree = ""; + }; + 29400C7B1C3AEE1300345568 /* chapter01 */ = { + isa = PBXGroup; + children = ( + 29400C7E1C3AEE1300345568 /* ViewController.swift */, + 29400C801C3AEE1300345568 /* Assets.xcassets */, + 29400C821C3AEE1300345568 /* Main.storyboard */, + 29400C851C3AEE1300345568 /* Info.plist */, + 29400C7C1C3AEE1300345568 /* AppDelegate.swift */, + ); + path = chapter01; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 29400C781C3AEE1300345568 /* chapter01 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 29400C881C3AEE1300345568 /* Build configuration list for PBXNativeTarget "chapter01" */; + buildPhases = ( + 29400C751C3AEE1300345568 /* Sources */, + 29400C761C3AEE1300345568 /* Frameworks */, + 29400C771C3AEE1300345568 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = chapter01; + productName = chapter01; + productReference = 29400C791C3AEE1300345568 /* chapter01.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 29400C711C3AEE1300345568 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0720; + LastUpgradeCheck = 0720; + ORGANIZATIONNAME = "Marius Horga"; + TargetAttributes = { + 29400C781C3AEE1300345568 = { + CreatedOnToolsVersion = 7.2; + }; + }; + }; + buildConfigurationList = 29400C741C3AEE1300345568 /* Build configuration list for PBXProject "chapter01" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 29400C701C3AEE1300345568; + productRefGroup = 29400C7A1C3AEE1300345568 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 29400C781C3AEE1300345568 /* chapter01 */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 29400C771C3AEE1300345568 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 29400C811C3AEE1300345568 /* Assets.xcassets in Resources */, + 29400C841C3AEE1300345568 /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 29400C751C3AEE1300345568 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 29400C7F1C3AEE1300345568 /* ViewController.swift in Sources */, + 29400C7D1C3AEE1300345568 /* AppDelegate.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + 29400C821C3AEE1300345568 /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 29400C831C3AEE1300345568 /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + 29400C861C3AEE1300345568 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 29400C871C3AEE1300345568 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + 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)"; + }; + 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)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 29400C741C3AEE1300345568 /* Build configuration list for PBXProject "chapter01" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 29400C861C3AEE1300345568 /* Debug */, + 29400C871C3AEE1300345568 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 29400C881C3AEE1300345568 /* Build configuration list for PBXNativeTarget "chapter01" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 29400C891C3AEE1300345568 /* Debug */, + 29400C8A1C3AEE1300345568 /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; +/* End XCConfigurationList section */ + }; + rootObject = 29400C711C3AEE1300345568 /* Project object */; +} diff --git a/ch01/chapter01.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/ch01/chapter01.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..a9cba61 --- /dev/null +++ b/ch01/chapter01.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/ch01/chapter01.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch01/chapter01.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..18e3b2b Binary files /dev/null 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 new file mode 100644 index 0000000..b3bc40d --- /dev/null +++ b/ch01/chapter01.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter01.xcscheme @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ch01/chapter01.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist b/ch01/chapter01.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..f5ae71c --- /dev/null +++ b/ch01/chapter01.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,22 @@ + + + + + SchemeUserState + + chapter01.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + 29400C781C3AEE1300345568 + + primary + + + + + diff --git a/ch01/chapter01/AppDelegate.swift b/ch01/chapter01/AppDelegate.swift new file mode 100644 index 0000000..99b058d --- /dev/null +++ b/ch01/chapter01/AppDelegate.swift @@ -0,0 +1,26 @@ +// +// AppDelegate.swift +// chapter01 +// +// Created by Marius on 1/4/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +import Cocoa + +@NSApplicationMain +class AppDelegate: NSObject, NSApplicationDelegate { + + + + func applicationDidFinishLaunching(aNotification: NSNotification) { + // Insert code here to initialize your application + } + + func applicationWillTerminate(aNotification: NSNotification) { + // Insert code here to tear down your application + } + + +} + diff --git a/ch01/chapter01/Assets.xcassets/AppIcon.appiconset/Contents.json b/ch01/chapter01/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..2db2b1c --- /dev/null +++ b/ch01/chapter01/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,58 @@ +{ + "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/ch01/chapter01/Base.lproj/Main.storyboard b/ch01/chapter01/Base.lproj/Main.storyboard new file mode 100644 index 0000000..51664cd --- /dev/null +++ b/ch01/chapter01/Base.lproj/Main.storyboard @@ -0,0 +1,695 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ch01/chapter01/Info.plist b/ch01/chapter01/Info.plist new file mode 100644 index 0000000..69d1345 --- /dev/null +++ b/ch01/chapter01/Info.plist @@ -0,0 +1,34 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIconFile + + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + LSMinimumSystemVersion + $(MACOSX_DEPLOYMENT_TARGET) + NSHumanReadableCopyright + Copyright © 2016 Marius Horga. All rights reserved. + NSMainStoryboardFile + Main + NSPrincipalClass + NSApplication + + diff --git a/ch01/chapter01/ViewController.swift b/ch01/chapter01/ViewController.swift new file mode 100644 index 0000000..65fd44c --- /dev/null +++ b/ch01/chapter01/ViewController.swift @@ -0,0 +1,24 @@ +// +// ViewController.swift +// chapter01 +// +// Created by Marius on 1/4/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +import Cocoa // contains Metal + +class ViewController: NSViewController { + + @IBOutlet weak var label: NSTextField! + + 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!" + } + } +} diff --git a/ch02/chapter02.xcodeproj/project.pbxproj b/ch02/chapter02.xcodeproj/project.pbxproj new file mode 100644 index 0000000..c91e5c5 --- /dev/null +++ b/ch02/chapter02.xcodeproj/project.pbxproj @@ -0,0 +1,280 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* 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 */ + +/* 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 = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 29E74ACA1C4159D60023A39A /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 29E74AC41C4159D60023A39A = { + isa = PBXGroup; + children = ( + 29E74ACF1C4159D60023A39A /* chapter02 */, + 29E74ACE1C4159D60023A39A /* Products */, + ); + sourceTree = ""; + }; + 29E74ACE1C4159D60023A39A /* Products */ = { + isa = PBXGroup; + children = ( + 29E74ACD1C4159D60023A39A /* chapter02.app */, + ); + name = Products; + sourceTree = ""; + }; + 29E74ACF1C4159D60023A39A /* chapter02 */ = { + isa = PBXGroup; + children = ( + 29E74ADF1C415A020023A39A /* MetalView.swift */, + 29E74AD61C4159D60023A39A /* Main.storyboard */, + 29E74AD41C4159D60023A39A /* Assets.xcassets */, + 29E74AD91C4159D60023A39A /* Info.plist */, + 29E74AD01C4159D60023A39A /* AppDelegate.swift */, + ); + path = chapter02; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 29E74ACC1C4159D60023A39A /* chapter02 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 29E74ADC1C4159D60023A39A /* Build configuration list for PBXNativeTarget "chapter02" */; + buildPhases = ( + 29E74AC91C4159D60023A39A /* Sources */, + 29E74ACA1C4159D60023A39A /* Frameworks */, + 29E74ACB1C4159D60023A39A /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = chapter02; + productName = chapter02; + productReference = 29E74ACD1C4159D60023A39A /* chapter02.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 29E74AC51C4159D60023A39A /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0720; + LastUpgradeCheck = 0720; + ORGANIZATIONNAME = "Marius Horga"; + TargetAttributes = { + 29E74ACC1C4159D60023A39A = { + CreatedOnToolsVersion = 7.2; + }; + }; + }; + buildConfigurationList = 29E74AC81C4159D60023A39A /* Build configuration list for PBXProject "chapter02" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 29E74AC41C4159D60023A39A; + productRefGroup = 29E74ACE1C4159D60023A39A /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 29E74ACC1C4159D60023A39A /* chapter02 */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 29E74ACB1C4159D60023A39A /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 29E74AD51C4159D60023A39A /* Assets.xcassets in Resources */, + 29E74AD81C4159D60023A39A /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 29E74AC91C4159D60023A39A /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 29E74AE01C415A020023A39A /* MetalView.swift in Sources */, + 29E74AD11C4159D60023A39A /* AppDelegate.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + 29E74AD61C4159D60023A39A /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 29E74AD71C4159D60023A39A /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + 29E74ADA1C4159D60023A39A /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 29E74ADB1C4159D60023A39A /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + 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)"; + }; + 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)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 29E74AC81C4159D60023A39A /* Build configuration list for PBXProject "chapter02" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 29E74ADA1C4159D60023A39A /* Debug */, + 29E74ADB1C4159D60023A39A /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 29E74ADC1C4159D60023A39A /* Build configuration list for PBXNativeTarget "chapter02" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 29E74ADD1C4159D60023A39A /* Debug */, + 29E74ADE1C4159D60023A39A /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; +/* End XCConfigurationList section */ + }; + rootObject = 29E74AC51C4159D60023A39A /* Project object */; +} diff --git a/ch02/chapter02.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/ch02/chapter02.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..9a3f0c0 --- /dev/null +++ b/ch02/chapter02.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/ch02/chapter02.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch02/chapter02.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..e571953 Binary files /dev/null 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 new file mode 100644 index 0000000..c6b8bd5 --- /dev/null +++ b/ch02/chapter02.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter02.xcscheme @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ch02/chapter02.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist b/ch02/chapter02.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..107843d --- /dev/null +++ b/ch02/chapter02.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,22 @@ + + + + + SchemeUserState + + chapter02.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + 29E74ACC1C4159D60023A39A + + primary + + + + + diff --git a/ch02/chapter02/AppDelegate.swift b/ch02/chapter02/AppDelegate.swift new file mode 100644 index 0000000..79d0a15 --- /dev/null +++ b/ch02/chapter02/AppDelegate.swift @@ -0,0 +1,13 @@ +// +// AppDelegate.swift +// chapter02 +// +// Created by Marius on 1/9/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +import Cocoa + +@NSApplicationMain +class AppDelegate: NSObject, NSApplicationDelegate { +} diff --git a/ch02/chapter02/Assets.xcassets/AppIcon.appiconset/Contents.json b/ch02/chapter02/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..2db2b1c --- /dev/null +++ b/ch02/chapter02/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,58 @@ +{ + "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/ch02/chapter02/Base.lproj/Main.storyboard b/ch02/chapter02/Base.lproj/Main.storyboard new file mode 100644 index 0000000..325a85d --- /dev/null +++ b/ch02/chapter02/Base.lproj/Main.storyboard @@ -0,0 +1,682 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ch02/chapter02/Info.plist b/ch02/chapter02/Info.plist new file mode 100644 index 0000000..69d1345 --- /dev/null +++ b/ch02/chapter02/Info.plist @@ -0,0 +1,34 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIconFile + + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + LSMinimumSystemVersion + $(MACOSX_DEPLOYMENT_TARGET) + NSHumanReadableCopyright + Copyright © 2016 Marius Horga. All rights reserved. + NSMainStoryboardFile + Main + NSPrincipalClass + NSApplication + + diff --git a/ch02/chapter02/MetalView.swift b/ch02/chapter02/MetalView.swift new file mode 100644 index 0000000..631d998 --- /dev/null +++ b/ch02/chapter02/MetalView.swift @@ -0,0 +1,34 @@ +// +// MetalView.swift +// chapter02 +// +// Created by Marius on 1/9/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +import MetalKit + +class MetalView: MTKView { + + override func drawRect(dirtyRect: NSRect) { + super.drawRect(dirtyRect) + + render() + } + + func render() { + let device = MTLCreateSystemDefaultDevice()! + self.device = device + let rpd = MTLRenderPassDescriptor() + let bleen = MTLClearColor(red: 0, green: 0.5, blue: 0.5, alpha: 1) + rpd.colorAttachments[0].texture = currentDrawable!.texture + rpd.colorAttachments[0].clearColor = bleen + rpd.colorAttachments[0].loadAction = .Clear + let commandQueue = device.newCommandQueue() + let commandBuffer = commandQueue.commandBuffer() + let encoder = commandBuffer.renderCommandEncoderWithDescriptor(rpd) + encoder.endEncoding() + commandBuffer.presentDrawable(currentDrawable!) + commandBuffer.commit() + } +} diff --git a/ch03/chapter03.xcodeproj/project.pbxproj b/ch03/chapter03.xcodeproj/project.pbxproj new file mode 100644 index 0000000..ed33bc4 --- /dev/null +++ b/ch03/chapter03.xcodeproj/project.pbxproj @@ -0,0 +1,285 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + 294BD6211C4D1FD7009CF449 /* MetalView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 294BD6201C4D1FD7009CF449 /* MetalView.swift */; }; + 29CB59901C4461EA0004DB55 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29CB598F1C4461EA0004DB55 /* AppDelegate.swift */; }; + 29CB59941C4461EA0004DB55 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 29CB59931C4461EA0004DB55 /* Assets.xcassets */; }; + 29CB59971C4461EA0004DB55 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 29CB59951C4461EA0004DB55 /* Main.storyboard */; }; + 29CB599F1C44665B0004DB55 /* Shaders.metal in Sources */ = {isa = PBXBuildFile; fileRef = 29CB599E1C44665B0004DB55 /* Shaders.metal */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 294BD6201C4D1FD7009CF449 /* MetalView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MetalView.swift; sourceTree = ""; }; + 29CB598C1C4461EA0004DB55 /* chapter03.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = chapter03.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 29CB598F1C4461EA0004DB55 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + 29CB59931C4461EA0004DB55 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + 29CB59961C4461EA0004DB55 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + 29CB59981C4461EA0004DB55 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 29CB599E1C44665B0004DB55 /* Shaders.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = Shaders.metal; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 29CB59891C4461E90004DB55 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 29CB59831C4461E90004DB55 = { + isa = PBXGroup; + children = ( + 29CB598E1C4461EA0004DB55 /* chapter03 */, + 29CB598D1C4461EA0004DB55 /* Products */, + ); + sourceTree = ""; + }; + 29CB598D1C4461EA0004DB55 /* Products */ = { + isa = PBXGroup; + children = ( + 29CB598C1C4461EA0004DB55 /* chapter03.app */, + ); + name = Products; + sourceTree = ""; + }; + 29CB598E1C4461EA0004DB55 /* chapter03 */ = { + isa = PBXGroup; + children = ( + 294BD6201C4D1FD7009CF449 /* MetalView.swift */, + 29CB599E1C44665B0004DB55 /* Shaders.metal */, + 29CB59951C4461EA0004DB55 /* Main.storyboard */, + 29CB59931C4461EA0004DB55 /* Assets.xcassets */, + 29CB59981C4461EA0004DB55 /* Info.plist */, + 29CB598F1C4461EA0004DB55 /* AppDelegate.swift */, + ); + path = chapter03; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 29CB598B1C4461E90004DB55 /* chapter03 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 29CB599B1C4461EA0004DB55 /* Build configuration list for PBXNativeTarget "chapter03" */; + buildPhases = ( + 29CB59881C4461E90004DB55 /* Sources */, + 29CB59891C4461E90004DB55 /* Frameworks */, + 29CB598A1C4461E90004DB55 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = chapter03; + productName = chapter03; + productReference = 29CB598C1C4461EA0004DB55 /* chapter03.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 29CB59841C4461E90004DB55 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0720; + LastUpgradeCheck = 0720; + ORGANIZATIONNAME = "Marius Horga"; + TargetAttributes = { + 29CB598B1C4461E90004DB55 = { + CreatedOnToolsVersion = 7.2; + }; + }; + }; + buildConfigurationList = 29CB59871C4461E90004DB55 /* Build configuration list for PBXProject "chapter03" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 29CB59831C4461E90004DB55; + productRefGroup = 29CB598D1C4461EA0004DB55 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 29CB598B1C4461E90004DB55 /* chapter03 */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 29CB598A1C4461E90004DB55 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 29CB59941C4461EA0004DB55 /* Assets.xcassets in Resources */, + 29CB59971C4461EA0004DB55 /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 29CB59881C4461E90004DB55 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 294BD6211C4D1FD7009CF449 /* MetalView.swift in Sources */, + 29CB599F1C44665B0004DB55 /* Shaders.metal in Sources */, + 29CB59901C4461EA0004DB55 /* AppDelegate.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + 29CB59951C4461EA0004DB55 /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 29CB59961C4461EA0004DB55 /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + 29CB59991C4461EA0004DB55 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 29CB599A1C4461EA0004DB55 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + 29CB599C1C4461EA0004DB55 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + COMBINE_HIDPI_IMAGES = YES; + INFOPLIST_FILE = chapter03/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter03; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 29CB599D1C4461EA0004DB55 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + COMBINE_HIDPI_IMAGES = YES; + INFOPLIST_FILE = chapter03/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter03; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 29CB59871C4461E90004DB55 /* Build configuration list for PBXProject "chapter03" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 29CB59991C4461EA0004DB55 /* Debug */, + 29CB599A1C4461EA0004DB55 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 29CB599B1C4461EA0004DB55 /* Build configuration list for PBXNativeTarget "chapter03" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 29CB599C1C4461EA0004DB55 /* Debug */, + 29CB599D1C4461EA0004DB55 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 29CB59841C4461E90004DB55 /* Project object */; +} diff --git a/ch03/chapter03.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/ch03/chapter03.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..7cddba2 --- /dev/null +++ b/ch03/chapter03.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/ch03/chapter03.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch03/chapter03.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..b5d75a4 Binary files /dev/null and b/ch03/chapter03.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch03/chapter03.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter03.xcscheme b/ch03/chapter03.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter03.xcscheme new file mode 100644 index 0000000..6ec6aa7 --- /dev/null +++ b/ch03/chapter03.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter03.xcscheme @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ch03/chapter03.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist b/ch03/chapter03.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..d621961 --- /dev/null +++ b/ch03/chapter03.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,22 @@ + + + + + SchemeUserState + + chapter03.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + 29CB598B1C4461E90004DB55 + + primary + + + + + diff --git a/ch03/chapter03/AppDelegate.swift b/ch03/chapter03/AppDelegate.swift new file mode 100644 index 0000000..cc40090 --- /dev/null +++ b/ch03/chapter03/AppDelegate.swift @@ -0,0 +1,13 @@ +// +// AppDelegate.swift +// chapter03 +// +// Created by Marius on 1/12/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +import Cocoa + +@NSApplicationMain +class AppDelegate: NSObject, NSApplicationDelegate { +} diff --git a/ch03/chapter03/Assets.xcassets/AppIcon.appiconset/Contents.json b/ch03/chapter03/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..2db2b1c --- /dev/null +++ b/ch03/chapter03/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,58 @@ +{ + "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/ch03/chapter03/Base.lproj/Main.storyboard b/ch03/chapter03/Base.lproj/Main.storyboard new file mode 100644 index 0000000..f33a444 --- /dev/null +++ b/ch03/chapter03/Base.lproj/Main.storyboard @@ -0,0 +1,682 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ch03/chapter03/Info.plist b/ch03/chapter03/Info.plist new file mode 100644 index 0000000..69d1345 --- /dev/null +++ b/ch03/chapter03/Info.plist @@ -0,0 +1,34 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIconFile + + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + LSMinimumSystemVersion + $(MACOSX_DEPLOYMENT_TARGET) + NSHumanReadableCopyright + Copyright © 2016 Marius Horga. All rights reserved. + NSMainStoryboardFile + Main + NSPrincipalClass + NSApplication + + diff --git a/ch03/chapter03/MetalView.swift b/ch03/chapter03/MetalView.swift new file mode 100644 index 0000000..2dc500b --- /dev/null +++ b/ch03/chapter03/MetalView.swift @@ -0,0 +1,50 @@ +// +// MetalView.swift +// chapter03 +// +// Created by Marius on 1/12/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +import MetalKit + +class MetalView: MTKView { + + override func drawRect(dirtyRect: NSRect) { + super.drawRect(dirtyRect) + render() + } + + func render() { + device = MTLCreateSystemDefaultDevice() + let vertex_data:[Float] = [-1.0, -1.0, 0.0, 1.0, + 1.0, -1.0, 0.0, 1.0, + 0.0, 1.0, 0.0, 1.0] + let data_size = vertex_data.count * sizeof(Float) + let vertex_buffer = device!.newBufferWithBytes(vertex_data, length: data_size, options: []) + let library = device!.newDefaultLibrary()! + let vertex_func = library.newFunctionWithName("vertex_func") + let frag_func = library.newFunctionWithName("fragment_func") + let rpld = MTLRenderPipelineDescriptor() + rpld.vertexFunction = vertex_func + rpld.fragmentFunction = frag_func + rpld.colorAttachments[0].pixelFormat = .BGRA8Unorm + var rps: MTLRenderPipelineState! = nil + do { + try rps = device!.newRenderPipelineStateWithDescriptor(rpld) + } catch let error { + self.print("\(error)") + } + if let rpd = currentRenderPassDescriptor, drawable = currentDrawable { + rpd.colorAttachments[0].clearColor = MTLClearColorMake(0, 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() + } + } +} diff --git a/ch03/chapter03/Shaders.metal b/ch03/chapter03/Shaders.metal new file mode 100644 index 0000000..dd09adb --- /dev/null +++ b/ch03/chapter03/Shaders.metal @@ -0,0 +1,22 @@ +// +// Shaders.metal +// chapter03 +// +// Created by Marius on 1/12/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +#include +using namespace metal; + +struct Vertex { + float4 position [[position]]; +}; + +vertex Vertex vertex_func(constant Vertex *vertices [[buffer(0)]], uint vid [[vertex_id]]) { + return vertices[vid]; +} + +fragment float4 fragment_func(Vertex vert [[stage_in]]) { + return float4(0.7, 1, 1, 1); +} diff --git a/ch04/chapter04.xcodeproj/project.pbxproj b/ch04/chapter04.xcodeproj/project.pbxproj new file mode 100644 index 0000000..760ab6c --- /dev/null +++ b/ch04/chapter04.xcodeproj/project.pbxproj @@ -0,0 +1,289 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + 29C87D411C56E93A005F4615 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C87D401C56E93A005F4615 /* AppDelegate.swift */; }; + 29C87D431C56E93A005F4615 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C87D421C56E93A005F4615 /* ViewController.swift */; }; + 29C87D451C56E93A005F4615 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 29C87D441C56E93A005F4615 /* Assets.xcassets */; }; + 29C87D481C56E93B005F4615 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 29C87D461C56E93B005F4615 /* Main.storyboard */; }; + 29C87D501C56E9C2005F4615 /* Shaders.metal in Sources */ = {isa = PBXBuildFile; fileRef = 29C87D4F1C56E9C2005F4615 /* Shaders.metal */; }; + 29C87D521C56E9DE005F4615 /* MetalView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C87D511C56E9DE005F4615 /* MetalView.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 29C87D3D1C56E93A005F4615 /* chapter04.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = chapter04.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 29C87D401C56E93A005F4615 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + 29C87D421C56E93A005F4615 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; + 29C87D441C56E93A005F4615 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + 29C87D471C56E93B005F4615 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + 29C87D491C56E93B005F4615 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 29C87D4F1C56E9C2005F4615 /* Shaders.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = Shaders.metal; sourceTree = ""; }; + 29C87D511C56E9DE005F4615 /* MetalView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MetalView.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 29C87D3A1C56E93A005F4615 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 29C87D341C56E93A005F4615 = { + isa = PBXGroup; + children = ( + 29C87D3F1C56E93A005F4615 /* chapter04 */, + 29C87D3E1C56E93A005F4615 /* Products */, + ); + sourceTree = ""; + }; + 29C87D3E1C56E93A005F4615 /* Products */ = { + isa = PBXGroup; + children = ( + 29C87D3D1C56E93A005F4615 /* chapter04.app */, + ); + name = Products; + sourceTree = ""; + }; + 29C87D3F1C56E93A005F4615 /* chapter04 */ = { + isa = PBXGroup; + children = ( + 29C87D511C56E9DE005F4615 /* MetalView.swift */, + 29C87D4F1C56E9C2005F4615 /* Shaders.metal */, + 29C87D441C56E93A005F4615 /* Assets.xcassets */, + 29C87D421C56E93A005F4615 /* ViewController.swift */, + 29C87D461C56E93B005F4615 /* Main.storyboard */, + 29C87D491C56E93B005F4615 /* Info.plist */, + 29C87D401C56E93A005F4615 /* AppDelegate.swift */, + ); + path = chapter04; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 29C87D3C1C56E93A005F4615 /* chapter04 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 29C87D4C1C56E93B005F4615 /* Build configuration list for PBXNativeTarget "chapter04" */; + buildPhases = ( + 29C87D391C56E93A005F4615 /* Sources */, + 29C87D3A1C56E93A005F4615 /* Frameworks */, + 29C87D3B1C56E93A005F4615 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = chapter04; + productName = chapter04; + productReference = 29C87D3D1C56E93A005F4615 /* chapter04.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 29C87D351C56E93A005F4615 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0720; + LastUpgradeCheck = 0720; + ORGANIZATIONNAME = "Marius Horga"; + TargetAttributes = { + 29C87D3C1C56E93A005F4615 = { + CreatedOnToolsVersion = 7.2; + }; + }; + }; + buildConfigurationList = 29C87D381C56E93A005F4615 /* Build configuration list for PBXProject "chapter04" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 29C87D341C56E93A005F4615; + productRefGroup = 29C87D3E1C56E93A005F4615 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 29C87D3C1C56E93A005F4615 /* chapter04 */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 29C87D3B1C56E93A005F4615 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 29C87D451C56E93A005F4615 /* Assets.xcassets in Resources */, + 29C87D481C56E93B005F4615 /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 29C87D391C56E93A005F4615 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 29C87D521C56E9DE005F4615 /* MetalView.swift in Sources */, + 29C87D501C56E9C2005F4615 /* Shaders.metal in Sources */, + 29C87D431C56E93A005F4615 /* ViewController.swift in Sources */, + 29C87D411C56E93A005F4615 /* AppDelegate.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + 29C87D461C56E93B005F4615 /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 29C87D471C56E93B005F4615 /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + 29C87D4A1C56E93B005F4615 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 29C87D4B1C56E93B005F4615 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + 29C87D4D1C56E93B005F4615 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + COMBINE_HIDPI_IMAGES = YES; + INFOPLIST_FILE = chapter04/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter04; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 29C87D4E1C56E93B005F4615 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + COMBINE_HIDPI_IMAGES = YES; + INFOPLIST_FILE = chapter04/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter04; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 29C87D381C56E93A005F4615 /* Build configuration list for PBXProject "chapter04" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 29C87D4A1C56E93B005F4615 /* Debug */, + 29C87D4B1C56E93B005F4615 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 29C87D4C1C56E93B005F4615 /* Build configuration list for PBXNativeTarget "chapter04" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 29C87D4D1C56E93B005F4615 /* Debug */, + 29C87D4E1C56E93B005F4615 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 29C87D351C56E93A005F4615 /* Project object */; +} diff --git a/ch04/chapter04.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/ch04/chapter04.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..97a56d9 --- /dev/null +++ b/ch04/chapter04.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/ch04/chapter04.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch04/chapter04.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..0ea0b23 Binary files /dev/null and b/ch04/chapter04.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch04/chapter04.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter04.xcscheme b/ch04/chapter04.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter04.xcscheme new file mode 100644 index 0000000..a541f24 --- /dev/null +++ b/ch04/chapter04.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter04.xcscheme @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ch04/chapter04.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist b/ch04/chapter04.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..65aa13b --- /dev/null +++ b/ch04/chapter04.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,22 @@ + + + + + SchemeUserState + + chapter04.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + 29C87D3C1C56E93A005F4615 + + primary + + + + + diff --git a/ch04/chapter04/AppDelegate.swift b/ch04/chapter04/AppDelegate.swift new file mode 100644 index 0000000..d94af3b --- /dev/null +++ b/ch04/chapter04/AppDelegate.swift @@ -0,0 +1,26 @@ +// +// AppDelegate.swift +// chapter04 +// +// Created by Marius on 1/26/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +import Cocoa + +@NSApplicationMain +class AppDelegate: NSObject, NSApplicationDelegate { + + + + func applicationDidFinishLaunching(aNotification: NSNotification) { + // Insert code here to initialize your application + } + + func applicationWillTerminate(aNotification: NSNotification) { + // Insert code here to tear down your application + } + + +} + diff --git a/ch04/chapter04/Assets.xcassets/AppIcon.appiconset/Contents.json b/ch04/chapter04/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..2db2b1c --- /dev/null +++ b/ch04/chapter04/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,58 @@ +{ + "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/ch04/chapter04/Base.lproj/Main.storyboard b/ch04/chapter04/Base.lproj/Main.storyboard new file mode 100644 index 0000000..4b8d6f1 --- /dev/null +++ b/ch04/chapter04/Base.lproj/Main.storyboard @@ -0,0 +1,682 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ch04/chapter04/Info.plist b/ch04/chapter04/Info.plist new file mode 100644 index 0000000..69d1345 --- /dev/null +++ b/ch04/chapter04/Info.plist @@ -0,0 +1,34 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIconFile + + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + LSMinimumSystemVersion + $(MACOSX_DEPLOYMENT_TARGET) + NSHumanReadableCopyright + Copyright © 2016 Marius Horga. All rights reserved. + NSMainStoryboardFile + Main + NSPrincipalClass + NSApplication + + diff --git a/ch04/chapter04/MetalView.swift b/ch04/chapter04/MetalView.swift new file mode 100644 index 0000000..847b46e --- /dev/null +++ b/ch04/chapter04/MetalView.swift @@ -0,0 +1,68 @@ +// +// MetalView.swift +// chapter04 +// +// Created by Marius on 1/26/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +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() + } + + struct Vertex { + var position: vector_float4 + var color: vector_float4 + }; + + 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:[]) + } + + func registerShaders() { + let library = device!.newDefaultLibrary()! + let vertex_func = library.newFunctionWithName("vertex_func") + let frag_func = library.newFunctionWithName("fragment_func") + let rpld = MTLRenderPipelineDescriptor() + rpld.vertexFunction = vertex_func + rpld.fragmentFunction = frag_func + rpld.colorAttachments[0].pixelFormat = .BGRA8Unorm + do { + try rps = device!.newRenderPipelineStateWithDescriptor(rpld) + } catch let error { + self.print("\(error)") + } + } + + func sendToGPU() { + 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.drawPrimitives(.Triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) + command_encoder.endEncoding() + command_buffer.presentDrawable(drawable) + command_buffer.commit() + } + } +} diff --git a/ch04/chapter04/Shaders.metal b/ch04/chapter04/Shaders.metal new file mode 100644 index 0000000..87188e5 --- /dev/null +++ b/ch04/chapter04/Shaders.metal @@ -0,0 +1,23 @@ +// +// Shaders.metal +// chapter04 +// +// Created by Marius on 1/26/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +#include +using namespace metal; + +struct Vertex { + float4 position [[position]]; + float4 color; +}; + +vertex Vertex vertex_func(constant Vertex *vertices [[buffer(0)]], uint vid [[vertex_id]]) { + return vertices[vid]; +} + +fragment float4 fragment_func(Vertex vert [[stage_in]]) { + return vert.color; +} diff --git a/ch04/chapter04/ViewController.swift b/ch04/chapter04/ViewController.swift new file mode 100644 index 0000000..d8740a2 --- /dev/null +++ b/ch04/chapter04/ViewController.swift @@ -0,0 +1,27 @@ +// +// 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 new file mode 100644 index 0000000..4ffaaea --- /dev/null +++ b/ch05/chapter05.xcodeproj/project.pbxproj @@ -0,0 +1,288 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* 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 */; }; +/* End PBXBuildFile section */ + +/* 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 = ""; }; + 298037331C62440400FBBC6A /* Shaders.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = Shaders.metal; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 2980371C1C62437900FBBC6A /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 298037161C62437900FBBC6A = { + isa = PBXGroup; + children = ( + 298037211C62437900FBBC6A /* chapter05 */, + 298037201C62437900FBBC6A /* Products */, + ); + sourceTree = ""; + }; + 298037201C62437900FBBC6A /* Products */ = { + isa = PBXGroup; + children = ( + 2980371F1C62437900FBBC6A /* chapter05.app */, + ); + name = Products; + sourceTree = ""; + }; + 298037211C62437900FBBC6A /* chapter05 */ = { + isa = PBXGroup; + children = ( + 298037311C6243CB00FBBC6A /* MetalView.swift */, + 298037331C62440400FBBC6A /* Shaders.metal */, + 298037261C62437900FBBC6A /* Assets.xcassets */, + 298037281C62437900FBBC6A /* Main.storyboard */, + 2980372B1C62437900FBBC6A /* Info.plist */, + 298037221C62437900FBBC6A /* AppDelegate.swift */, + ); + path = chapter05; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 2980371E1C62437900FBBC6A /* chapter05 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 2980372E1C62437900FBBC6A /* Build configuration list for PBXNativeTarget "chapter05" */; + buildPhases = ( + 2980371B1C62437900FBBC6A /* Sources */, + 2980371C1C62437900FBBC6A /* Frameworks */, + 2980371D1C62437900FBBC6A /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = chapter05; + productName = chapter05; + productReference = 2980371F1C62437900FBBC6A /* chapter05.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 298037171C62437900FBBC6A /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0720; + LastUpgradeCheck = 0720; + ORGANIZATIONNAME = "Marius Horga"; + TargetAttributes = { + 2980371E1C62437900FBBC6A = { + CreatedOnToolsVersion = 7.2.1; + }; + }; + }; + buildConfigurationList = 2980371A1C62437900FBBC6A /* Build configuration list for PBXProject "chapter05" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 298037161C62437900FBBC6A; + productRefGroup = 298037201C62437900FBBC6A /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 2980371E1C62437900FBBC6A /* chapter05 */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 2980371D1C62437900FBBC6A /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 298037271C62437900FBBC6A /* Assets.xcassets in Resources */, + 2980372A1C62437900FBBC6A /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 2980371B1C62437900FBBC6A /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 298037341C62440400FBBC6A /* Shaders.metal in Sources */, + 298037321C6243CB00FBBC6A /* MetalView.swift in Sources */, + 298037231C62437900FBBC6A /* AppDelegate.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + 298037281C62437900FBBC6A /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 298037291C62437900FBBC6A /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + 2980372C1C62437900FBBC6A /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 2980372D1C62437900FBBC6A /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + 2980372F1C62437900FBBC6A /* Debug */ = { + 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 = "-Onone"; + }; + 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)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 2980371A1C62437900FBBC6A /* Build configuration list for PBXProject "chapter05" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 2980372C1C62437900FBBC6A /* Debug */, + 2980372D1C62437900FBBC6A /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 2980372E1C62437900FBBC6A /* Build configuration list for PBXNativeTarget "chapter05" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 2980372F1C62437900FBBC6A /* Debug */, + 298037301C62437900FBBC6A /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 298037171C62437900FBBC6A /* Project object */; +} diff --git a/ch05/chapter05.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/ch05/chapter05.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..61afa29 --- /dev/null +++ b/ch05/chapter05.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/ch05/chapter05.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch05/chapter05.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..d5df0be Binary files /dev/null and b/ch05/chapter05.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch05/chapter05.xcodeproj/xcuserdata/marius.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/ch05/chapter05.xcodeproj/xcuserdata/marius.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..fe2b454 --- /dev/null +++ b/ch05/chapter05.xcodeproj/xcuserdata/marius.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,5 @@ + + + diff --git a/ch05/chapter05.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter05.xcscheme b/ch05/chapter05.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter05.xcscheme new file mode 100644 index 0000000..1ad0d75 --- /dev/null +++ b/ch05/chapter05.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter05.xcscheme @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ch05/chapter05.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist b/ch05/chapter05.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..9b6e229 --- /dev/null +++ b/ch05/chapter05.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,22 @@ + + + + + SchemeUserState + + chapter05.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + 2980371E1C62437900FBBC6A + + primary + + + + + diff --git a/ch05/chapter05/AppDelegate.swift b/ch05/chapter05/AppDelegate.swift new file mode 100644 index 0000000..3d4d50a --- /dev/null +++ b/ch05/chapter05/AppDelegate.swift @@ -0,0 +1,13 @@ +// +// AppDelegate.swift +// chapter05 +// +// Created by Marius on 2/3/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +import Cocoa + +@NSApplicationMain +class AppDelegate: NSObject, NSApplicationDelegate { +} diff --git a/ch05/chapter05/Assets.xcassets/AppIcon.appiconset/Contents.json b/ch05/chapter05/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..2db2b1c --- /dev/null +++ b/ch05/chapter05/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,58 @@ +{ + "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/ch05/chapter05/Base.lproj/Main.storyboard b/ch05/chapter05/Base.lproj/Main.storyboard new file mode 100644 index 0000000..1ed57e4 --- /dev/null +++ b/ch05/chapter05/Base.lproj/Main.storyboard @@ -0,0 +1,682 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ch05/chapter05/Info.plist b/ch05/chapter05/Info.plist new file mode 100644 index 0000000..69d1345 --- /dev/null +++ b/ch05/chapter05/Info.plist @@ -0,0 +1,34 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIconFile + + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + LSMinimumSystemVersion + $(MACOSX_DEPLOYMENT_TARGET) + NSHumanReadableCopyright + Copyright © 2016 Marius Horga. All rights reserved. + NSMainStoryboardFile + Main + NSPrincipalClass + NSApplication + + diff --git a/ch05/chapter05/MetalView.swift b/ch05/chapter05/MetalView.swift new file mode 100644 index 0000000..cae788e --- /dev/null +++ b/ch05/chapter05/MetalView.swift @@ -0,0 +1,123 @@ +// +// MetalView.swift +// chapter05 +// +// Created by Marius on 2/3/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +import MetalKit + +class MetalView: MTKView { + + var vertex_buffer: MTLBuffer! + var uniform_buffer: MTLBuffer! + var rps: MTLRenderPipelineState! = nil + + override func drawRect(dirtyRect: NSRect) { + super.drawRect(dirtyRect) + render() + } + + func render() { + device = MTLCreateSystemDefaultDevice() + createBuffers() + registerShaders() + sendToGPU() + } + + struct Vertex { + var position: vector_float4 + var color: vector_float4 + } + + struct Matrix { + var m: [Float] + + init() { + m = [1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1 + ] + } + + func translationMatrix(var matrix: Matrix, _ position: float3) -> 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 { + 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 { + 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) + matrix.m[1] = cos(rot.y) * sin(rot.z) + matrix.m[5] = cos(rot.x) * cos(rot.z) + sin(rot.x) * sin(rot.y) * sin(rot.z) + matrix.m[9] = -cos(rot.z) * sin(rot.x) + cos(rot.x) * sin(rot.y) * sin(rot.z) + matrix.m[2] = -sin(rot.y) + matrix.m[6] = cos(rot.y) * sin(rot.x) + matrix.m[10] = cos(rot.x) * cos(rot.y) + matrix.m[15] = 1.0 + return matrix + } + + func modelMatrix(var matrix: 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)) + return matrix + } + } + + 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]) + ] + 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) + } + + func registerShaders() { + let library = device!.newDefaultLibrary()! + let vertex_func = library.newFunctionWithName("vertex_func") + let frag_func = library.newFunctionWithName("fragment_func") + let rpld = MTLRenderPipelineDescriptor() + rpld.vertexFunction = vertex_func + rpld.fragmentFunction = frag_func + rpld.colorAttachments[0].pixelFormat = .BGRA8Unorm + do { + try rps = device!.newRenderPipelineStateWithDescriptor(rpld) + } catch let error { + self.print("\(error)") + } + } + + func sendToGPU() { + 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() + } + } +} diff --git a/ch05/chapter05/Shaders.metal b/ch05/chapter05/Shaders.metal new file mode 100644 index 0000000..a223380 --- /dev/null +++ b/ch05/chapter05/Shaders.metal @@ -0,0 +1,34 @@ +// +// Shaders.metal +// chapter05 +// +// Created by Marius on 2/3/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +#include +using namespace metal; + +struct Vertex { + float4 position [[position]]; + float4 color; +}; + +struct Uniforms { + float4x4 modelMatrix; +}; + +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; + out.position = matrix * float4(in.position); + out.color = in.color; + return out; +} + +fragment float4 fragment_func(Vertex vert [[stage_in]]) { + return vert.color; +} diff --git a/ch06/chapter06.xcodeproj/project.pbxproj b/ch06/chapter06.xcodeproj/project.pbxproj new file mode 100644 index 0000000..d5b5d90 --- /dev/null +++ b/ch06/chapter06.xcodeproj/project.pbxproj @@ -0,0 +1,281 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + 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 */; }; +/* 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 = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 29DB1AEF1C6BDDD500BCC31B /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 29DB1AE91C6BDDD400BCC31B = { + isa = PBXGroup; + children = ( + 29DB1AF41C6BDDD500BCC31B /* chapter06 */, + 29DB1AF31C6BDDD500BCC31B /* Products */, + ); + sourceTree = ""; + }; + 29DB1AF31C6BDDD500BCC31B /* Products */ = { + isa = PBXGroup; + children = ( + 29DB1AF21C6BDDD500BCC31B /* chapter06.app */, + ); + name = Products; + sourceTree = ""; + }; + 29DB1AF41C6BDDD500BCC31B /* chapter06 */ = { + isa = PBXGroup; + children = ( + 292765411C6BDE3400094CA9 /* MetalView.swift */, + 29DB1AF91C6BDDD500BCC31B /* Assets.xcassets */, + 29DB1AFB1C6BDDD500BCC31B /* Main.storyboard */, + 29DB1AFE1C6BDDD500BCC31B /* Info.plist */, + 29DB1AF51C6BDDD500BCC31B /* AppDelegate.swift */, + ); + path = chapter06; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 29DB1AF11C6BDDD500BCC31B /* chapter06 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 29DB1B011C6BDDD500BCC31B /* Build configuration list for PBXNativeTarget "chapter06" */; + buildPhases = ( + 29DB1AEE1C6BDDD500BCC31B /* Sources */, + 29DB1AEF1C6BDDD500BCC31B /* Frameworks */, + 29DB1AF01C6BDDD500BCC31B /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = chapter06; + productName = chapter06; + productReference = 29DB1AF21C6BDDD500BCC31B /* chapter06.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 29DB1AEA1C6BDDD400BCC31B /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0720; + LastUpgradeCheck = 0720; + ORGANIZATIONNAME = "Marius Horga"; + TargetAttributes = { + 29DB1AF11C6BDDD500BCC31B = { + CreatedOnToolsVersion = 7.2.1; + }; + }; + }; + buildConfigurationList = 29DB1AED1C6BDDD400BCC31B /* Build configuration list for PBXProject "chapter06" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 29DB1AE91C6BDDD400BCC31B; + productRefGroup = 29DB1AF31C6BDDD500BCC31B /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 29DB1AF11C6BDDD500BCC31B /* chapter06 */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 29DB1AF01C6BDDD500BCC31B /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 29DB1AFA1C6BDDD500BCC31B /* Assets.xcassets in Resources */, + 29DB1AFD1C6BDDD500BCC31B /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 29DB1AEE1C6BDDD500BCC31B /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 292765421C6BDE3400094CA9 /* MetalView.swift in Sources */, + 29DB1AF61C6BDDD500BCC31B /* AppDelegate.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + 29DB1AFB1C6BDDD500BCC31B /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 29DB1AFC1C6BDDD500BCC31B /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + 29DB1AFF1C6BDDD500BCC31B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 29DB1B001C6BDDD500BCC31B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + 29DB1B021C6BDDD500BCC31B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + COMBINE_HIDPI_IMAGES = YES; + INFOPLIST_FILE = chapter06/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter06; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 29DB1B031C6BDDD500BCC31B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + COMBINE_HIDPI_IMAGES = YES; + INFOPLIST_FILE = chapter06/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter06; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 29DB1AED1C6BDDD400BCC31B /* Build configuration list for PBXProject "chapter06" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 29DB1AFF1C6BDDD500BCC31B /* Debug */, + 29DB1B001C6BDDD500BCC31B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 29DB1B011C6BDDD500BCC31B /* Build configuration list for PBXNativeTarget "chapter06" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 29DB1B021C6BDDD500BCC31B /* Debug */, + 29DB1B031C6BDDD500BCC31B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 29DB1AEA1C6BDDD400BCC31B /* Project object */; +} diff --git a/ch06/chapter06.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/ch06/chapter06.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..8002602 --- /dev/null +++ b/ch06/chapter06.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/ch06/chapter06.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch06/chapter06.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..b174838 Binary files /dev/null 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 new file mode 100644 index 0000000..d92b1b7 --- /dev/null +++ b/ch06/chapter06.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter06.xcscheme @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ch06/chapter06.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist b/ch06/chapter06.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..8e90fc7 --- /dev/null +++ b/ch06/chapter06.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,22 @@ + + + + + SchemeUserState + + chapter06.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + 29DB1AF11C6BDDD500BCC31B + + primary + + + + + diff --git a/ch06/chapter06/AppDelegate.swift b/ch06/chapter06/AppDelegate.swift new file mode 100644 index 0000000..247cfb6 --- /dev/null +++ b/ch06/chapter06/AppDelegate.swift @@ -0,0 +1,14 @@ +// +// AppDelegate.swift +// chapter06 +// +// Created by Marius on 2/10/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +import Cocoa + +@NSApplicationMain +class AppDelegate: NSObject, NSApplicationDelegate { + +} diff --git a/ch06/chapter06/Assets.xcassets/AppIcon.appiconset/Contents.json b/ch06/chapter06/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..2db2b1c --- /dev/null +++ b/ch06/chapter06/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,58 @@ +{ + "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 new file mode 100644 index 0000000..46163bb --- /dev/null +++ b/ch06/chapter06/Base.lproj/Main.storyboard @@ -0,0 +1,682 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 new file mode 100644 index 0000000..69d1345 --- /dev/null +++ b/ch06/chapter06/Info.plist @@ -0,0 +1,34 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIconFile + + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + LSMinimumSystemVersion + $(MACOSX_DEPLOYMENT_TARGET) + NSHumanReadableCopyright + Copyright © 2016 Marius Horga. All rights reserved. + NSMainStoryboardFile + Main + NSPrincipalClass + NSApplication + + diff --git a/ch06/chapter06/MetalView.swift b/ch06/chapter06/MetalView.swift new file mode 100644 index 0000000..33682be --- /dev/null +++ b/ch06/chapter06/MetalView.swift @@ -0,0 +1,44 @@ +// +// MetalView.swift +// chapter06 +// +// Created by Marius on 2/10/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +import Cocoa + +class MetalView: NSView { + + override func drawRect(dirtyRect: NSRect) { + super.drawRect(dirtyRect) + render() + } + + override class func layerClass() -> AnyClass { + return CAMetalLayer.self + } + + var metalLayer: CAMetalLayer { + return layer as! CAMetalLayer + } + + 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() + let commandBuffer = commandQueue.commandBuffer() + let commandEncoder = commandBuffer.renderCommandEncoderWithDescriptor(rpd) + commandEncoder.endEncoding() + commandBuffer.presentDrawable(drawable!) + commandBuffer.commit() + } +} diff --git a/ch07/chapter07.xcodeproj/project.pbxproj b/ch07/chapter07.xcodeproj/project.pbxproj new file mode 100644 index 0000000..bc672e5 --- /dev/null +++ b/ch07/chapter07.xcodeproj/project.pbxproj @@ -0,0 +1,289 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* 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 */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 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 = ""; }; + 29D8783A1C84D335007B5F17 /* Shaders.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = Shaders.metal; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 29D878231C84D2B0007B5F17 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 29D8781D1C84D2B0007B5F17 = { + isa = PBXGroup; + children = ( + 29D878281C84D2B0007B5F17 /* chapter07 */, + 29D878271C84D2B0007B5F17 /* Products */, + ); + sourceTree = ""; + }; + 29D878271C84D2B0007B5F17 /* Products */ = { + isa = PBXGroup; + children = ( + 29D878261C84D2B0007B5F17 /* chapter07.app */, + ); + name = Products; + sourceTree = ""; + }; + 29D878281C84D2B0007B5F17 /* chapter07 */ = { + isa = PBXGroup; + children = ( + 29D878381C84D309007B5F17 /* MetalView.swift */, + 29B186251C84FF5800E9C0AA /* MathUtils.swift */, + 29D8783A1C84D335007B5F17 /* Shaders.metal */, + 29D8782D1C84D2B0007B5F17 /* Assets.xcassets */, + 29D8782F1C84D2B0007B5F17 /* Main.storyboard */, + 29D878321C84D2B0007B5F17 /* Info.plist */, + 29D878291C84D2B0007B5F17 /* AppDelegate.swift */, + ); + path = chapter07; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 29D878251C84D2B0007B5F17 /* chapter07 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 29D878351C84D2B0007B5F17 /* Build configuration list for PBXNativeTarget "chapter07" */; + buildPhases = ( + 29D878221C84D2B0007B5F17 /* Sources */, + 29D878231C84D2B0007B5F17 /* Frameworks */, + 29D878241C84D2B0007B5F17 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = chapter07; + productName = chapter07; + productReference = 29D878261C84D2B0007B5F17 /* chapter07.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 29D8781E1C84D2B0007B5F17 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0720; + LastUpgradeCheck = 0720; + ORGANIZATIONNAME = "Marius Horga"; + TargetAttributes = { + 29D878251C84D2B0007B5F17 = { + CreatedOnToolsVersion = 7.2.1; + }; + }; + }; + buildConfigurationList = 29D878211C84D2B0007B5F17 /* Build configuration list for PBXProject "chapter07" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 29D8781D1C84D2B0007B5F17; + productRefGroup = 29D878271C84D2B0007B5F17 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 29D878251C84D2B0007B5F17 /* chapter07 */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 29D878241C84D2B0007B5F17 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 29D8782E1C84D2B0007B5F17 /* Assets.xcassets in Resources */, + 29D878311C84D2B0007B5F17 /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 29D878221C84D2B0007B5F17 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 29D8783B1C84D335007B5F17 /* Shaders.metal in Sources */, + 29B186261C84FF5800E9C0AA /* MathUtils.swift in Sources */, + 29D878391C84D309007B5F17 /* MetalView.swift in Sources */, + 29D8782A1C84D2B0007B5F17 /* AppDelegate.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + 29D8782F1C84D2B0007B5F17 /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 29D878301C84D2B0007B5F17 /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + 29D878331C84D2B0007B5F17 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 29D878341C84D2B0007B5F17 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + 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)"; + }; + 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)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 29D878211C84D2B0007B5F17 /* Build configuration list for PBXProject "chapter07" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 29D878331C84D2B0007B5F17 /* Debug */, + 29D878341C84D2B0007B5F17 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 29D878351C84D2B0007B5F17 /* Build configuration list for PBXNativeTarget "chapter07" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 29D878361C84D2B0007B5F17 /* Debug */, + 29D878371C84D2B0007B5F17 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 29D8781E1C84D2B0007B5F17 /* Project object */; +} diff --git a/ch07/chapter07.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/ch07/chapter07.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..92da390 --- /dev/null +++ b/ch07/chapter07.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/ch07/chapter07.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch07/chapter07.xcodeproj/project.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..4118b49 Binary files /dev/null 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 new file mode 100644 index 0000000..7ba25a7 --- /dev/null +++ b/ch07/chapter07.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/chapter07.xcscheme @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ch07/chapter07.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist b/ch07/chapter07.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..f85199c --- /dev/null +++ b/ch07/chapter07.xcodeproj/xcuserdata/marius.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,22 @@ + + + + + SchemeUserState + + chapter07.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + 29D878251C84D2B0007B5F17 + + primary + + + + + diff --git a/ch07/chapter07/AppDelegate.swift b/ch07/chapter07/AppDelegate.swift new file mode 100644 index 0000000..b124b06 --- /dev/null +++ b/ch07/chapter07/AppDelegate.swift @@ -0,0 +1,13 @@ +// +// AppDelegate.swift +// chapter07 +// +// Created by Marius on 2/29/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +import Cocoa + +@NSApplicationMain +class AppDelegate: NSObject, NSApplicationDelegate { +} diff --git a/ch07/chapter07/Assets.xcassets/AppIcon.appiconset/Contents.json b/ch07/chapter07/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..2db2b1c --- /dev/null +++ b/ch07/chapter07/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,58 @@ +{ + "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/ch07/chapter07/Base.lproj/Main.storyboard b/ch07/chapter07/Base.lproj/Main.storyboard new file mode 100644 index 0000000..3d10714 --- /dev/null +++ b/ch07/chapter07/Base.lproj/Main.storyboard @@ -0,0 +1,682 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ch07/chapter07/Info.plist b/ch07/chapter07/Info.plist new file mode 100644 index 0000000..69d1345 --- /dev/null +++ b/ch07/chapter07/Info.plist @@ -0,0 +1,34 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIconFile + + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + LSMinimumSystemVersion + $(MACOSX_DEPLOYMENT_TARGET) + NSHumanReadableCopyright + Copyright © 2016 Marius Horga. All rights reserved. + NSMainStoryboardFile + Main + NSPrincipalClass + NSApplication + + diff --git a/ch07/chapter07/MathUtils.swift b/ch07/chapter07/MathUtils.swift new file mode 100644 index 0000000..58b4ed4 --- /dev/null +++ b/ch07/chapter07/MathUtils.swift @@ -0,0 +1,62 @@ +// +// MathUtils.swift +// chapter07 +// +// Created by Marius on 3/1/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +import simd + +struct Vertex { + var position: vector_float4 + var color: vector_float4 +} + +struct Matrix { + var m: [Float] + + init() { + m = [1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1 + ] + } + + func translationMatrix(var matrix: Matrix, _ position: float3) -> 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 { + 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 { + 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) + matrix.m[1] = cos(rot.y) * sin(rot.z) + matrix.m[5] = cos(rot.x) * cos(rot.z) + sin(rot.x) * sin(rot.y) * sin(rot.z) + matrix.m[9] = -cos(rot.z) * sin(rot.x) + cos(rot.x) * sin(rot.y) * sin(rot.z) + matrix.m[2] = -sin(rot.y) + matrix.m[6] = cos(rot.y) * sin(rot.x) + matrix.m[10] = cos(rot.x) * cos(rot.y) + matrix.m[15] = 1.0 + return matrix + } + + func modelMatrix(var matrix: 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)) + return matrix + } +} diff --git a/ch07/chapter07/MetalView.swift b/ch07/chapter07/MetalView.swift new file mode 100644 index 0000000..a042c98 --- /dev/null +++ b/ch07/chapter07/MetalView.swift @@ -0,0 +1,66 @@ +// +// MetalView.swift +// chapter07 +// +// Created by Marius on 2/29/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +import MetalKit + +class MetalView: MTKView { + + var vertex_buffer: MTLBuffer! + var uniform_buffer: MTLBuffer! + var rps: MTLRenderPipelineState! = nil + + 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]) + ] + 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) + } + + func registerShaders() { + let library = device!.newDefaultLibrary()! + let vertex_func = library.newFunctionWithName("vertex_func") + let frag_func = library.newFunctionWithName("fragment_func") + let rpld = MTLRenderPipelineDescriptor() + rpld.vertexFunction = vertex_func + rpld.fragmentFunction = frag_func + rpld.colorAttachments[0].pixelFormat = .BGRA8Unorm + do { + try rps = device!.newRenderPipelineStateWithDescriptor(rpld) + } catch let error { + self.print("\(error)") + } + } +} diff --git a/ch07/chapter07/Shaders.metal b/ch07/chapter07/Shaders.metal new file mode 100644 index 0000000..2223ad6 --- /dev/null +++ b/ch07/chapter07/Shaders.metal @@ -0,0 +1,34 @@ +// +// Shaders.metal +// chapter07 +// +// Created by Marius on 2/29/16. +// Copyright © 2016 Marius Horga. All rights reserved. +// + +#include +using namespace metal; + +struct Vertex { + float4 position [[position]]; + float4 color; +}; + +struct Uniforms { + float4x4 modelMatrix; +}; + +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; + out.position = matrix * float4(in.position); + out.color = in.color; + return out; +} + +fragment float4 fragment_func(Vertex vert [[stage_in]]) { + return vert.color; +} diff --git a/ch08/chapter08.playground/Contents.swift b/ch08/chapter08.playground/Contents.swift new file mode 100644 index 0000000..96ca6ff --- /dev/null +++ b/ch08/chapter08.playground/Contents.swift @@ -0,0 +1,8 @@ + +import Cocoa +import XCPlayground + +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 diff --git a/ch08/chapter08.playground/Resources/Shaders.metal b/ch08/chapter08.playground/Resources/Shaders.metal new file mode 100644 index 0000000..a9bad7e --- /dev/null +++ b/ch08/chapter08.playground/Resources/Shaders.metal @@ -0,0 +1,25 @@ + +#include +using namespace metal; + +struct Vertex { + float4 position [[position]]; + float4 color; +}; + +struct Uniforms { + float4x4 modelMatrix; +}; + +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; + out.position = matrix * float4(in.position); + out.color = in.color; + return out; +} + +fragment float4 fragment_func(Vertex vert [[stage_in]]) { + return vert.color; +} diff --git a/ch08/chapter08.playground/Sources/MathUtils.swift b/ch08/chapter08.playground/Sources/MathUtils.swift new file mode 100644 index 0000000..cc13d82 --- /dev/null +++ b/ch08/chapter08.playground/Sources/MathUtils.swift @@ -0,0 +1,59 @@ + +import simd + +struct Vertex { + var position: vector_float4 + var color: vector_float4 + init(pos: vector_float4, col: vector_float4) { + position = pos + color = col + } +} + +struct Matrix { + var m: [Float] + + init() { + m = [1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1 + ] + } + + func translationMatrix(var matrix: Matrix, _ position: float3) -> 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 { + 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 { + 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) + matrix.m[1] = cos(rot.y) * sin(rot.z) + matrix.m[5] = cos(rot.x) * cos(rot.z) + sin(rot.x) * sin(rot.y) * sin(rot.z) + matrix.m[9] = -cos(rot.z) * sin(rot.x) + cos(rot.x) * sin(rot.y) * sin(rot.z) + matrix.m[2] = -sin(rot.y) + matrix.m[6] = cos(rot.y) * sin(rot.x) + matrix.m[10] = cos(rot.x) * cos(rot.y) + matrix.m[15] = 1.0 + return matrix + } + + func modelMatrix(var matrix: 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)) + return matrix + } +} diff --git a/ch08/chapter08.playground/Sources/MetalView.swift b/ch08/chapter08.playground/Sources/MetalView.swift new file mode 100644 index 0000000..9305067 --- /dev/null +++ b/ch08/chapter08.playground/Sources/MetalView.swift @@ -0,0 +1,69 @@ + +import MetalKit + +public class MetalView: MTKView { + + var vertex_buffer: MTLBuffer! + var uniform_buffer: MTLBuffer! + var rps: MTLRenderPipelineState! = nil + + required public init(coder: NSCoder) { + super.init(coder: coder) + } + + override public init(frame frameRect: CGRect, device: MTLDevice?) { + super.init(frame: frameRect, device: device) + 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]) + ] + 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) + } + + func registerShaders() { + let path = NSBundle.mainBundle().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")! + let rpld = MTLRenderPipelineDescriptor() + rpld.vertexFunction = vert_func + rpld.fragmentFunction = frag_func + rpld.colorAttachments[0].pixelFormat = .BGRA8Unorm + rps = try device!.newRenderPipelineStateWithDescriptor(rpld) + } catch let e { + Swift.print("\(e)") + } + } +} diff --git a/ch08/chapter08.playground/contents.xcplayground b/ch08/chapter08.playground/contents.xcplayground new file mode 100644 index 0000000..06828af --- /dev/null +++ b/ch08/chapter08.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/ch08/chapter08.playground/playground.xcworkspace/contents.xcworkspacedata b/ch08/chapter08.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/ch08/chapter08.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/ch08/chapter08.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch08/chapter08.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..4b12b70 Binary files /dev/null and b/ch08/chapter08.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch08/chapter08.playground/timeline.xctimeline b/ch08/chapter08.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/ch08/chapter08.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/ch09/chapter09.playground/Contents.swift b/ch09/chapter09.playground/Contents.swift new file mode 100644 index 0000000..96ca6ff --- /dev/null +++ b/ch09/chapter09.playground/Contents.swift @@ -0,0 +1,8 @@ + +import Cocoa +import XCPlayground + +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 diff --git a/ch09/chapter09.playground/Resources/Shaders.metal b/ch09/chapter09.playground/Resources/Shaders.metal new file mode 100755 index 0000000..e52b42e --- /dev/null +++ b/ch09/chapter09.playground/Resources/Shaders.metal @@ -0,0 +1,25 @@ + +#include +using namespace metal; + +struct Vertex { + float4 position [[position]]; + float4 color; +}; + +struct Uniforms { + float4x4 modelViewProjectionMatrix; +}; + +vertex Vertex vertex_func(constant Vertex *vertices [[buffer(0)]], constant Uniforms &uniforms [[buffer(1)]], uint vid [[vertex_id]]) { + float4x4 matrix = uniforms.modelViewProjectionMatrix; + Vertex in = vertices[vid]; + Vertex out; + out.position = matrix * float4(in.position); + out.color = in.color; + return out; +} + +fragment float4 fragment_func(Vertex vert [[stage_in]]) { + return vert.color; +} diff --git a/ch09/chapter09.playground/Sources/MathUtils.swift b/ch09/chapter09.playground/Sources/MathUtils.swift new file mode 100755 index 0000000..81ef738 --- /dev/null +++ b/ch09/chapter09.playground/Sources/MathUtils.swift @@ -0,0 +1,63 @@ + +import simd + +struct Vertex { + var position: vector_float4 + var color: vector_float4 + init(pos: vector_float4, col: vector_float4) { + position = pos + color = col + } +} + +struct Uniforms { + var modelViewProjectionMatrix: matrix_float4x4 +} + +func translationMatrix(position: float3) -> matrix_float4x4 { + let X = vector_float4(1, 0, 0, 0) + let Y = vector_float4(0, 1, 0, 0) + let Z = vector_float4(0, 0, 1, 0) + let W = vector_float4(position.x, position.y, position.z, 1) + return matrix_float4x4(columns:(X, Y, Z, W)) +} + +func scalingMatrix(scale: Float) -> matrix_float4x4 { + let X = vector_float4(scale, 0, 0, 0) + let Y = vector_float4(0, scale, 0, 0) + let Z = vector_float4(0, 0, scale, 0) + let W = vector_float4(0, 0, 0, 1) + return matrix_float4x4(columns:(X, Y, Z, W)) +} + +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) + X.z = axis.x * axis.z * (1 - cos(angle)) + axis.y * sin(angle) + X.w = 0.0 + var Y = vector_float4(0, 0, 0, 0) + Y.x = axis.x * axis.y * (1 - cos(angle)) + axis.z * sin(angle) + Y.y = axis.y * axis.y + (1 - axis.y * axis.y) * cos(angle) + Y.z = axis.y * axis.z * (1 - cos(angle)) - axis.x * sin(angle) + Y.w = 0.0 + var Z = vector_float4(0, 0, 0, 0) + Z.x = axis.x * axis.z * (1 - cos(angle)) - axis.y * sin(angle) + Z.y = axis.y * axis.z * (1 - cos(angle)) + axis.x * sin(angle) + Z.z = axis.z * axis.z + (1 - axis.z * axis.z) * cos(angle) + Z.w = 0.0 + let W = vector_float4(0, 0, 0, 1) + return matrix_float4x4(columns:(X, Y, Z, W)) +} + +func projectionMatrix(near: Float, far: Float, aspect: Float, fovy: Float) -> matrix_float4x4 { + let scaleY = 1 / tan(fovy * 0.5) + let scaleX = scaleY / aspect + let scaleZ = -(far + near) / (far - near) + let scaleW = -2 * far * near / (far - near) + let X = vector_float4(scaleX, 0, 0, 0) + let Y = vector_float4(0, scaleY, 0, 0) + let Z = vector_float4(0, 0, scaleZ, -1) + let W = vector_float4(0, 0, scaleW, 0) + return matrix_float4x4(columns:(X, Y, Z, W)) +} diff --git a/ch09/chapter09.playground/Sources/MetalView.swift b/ch09/chapter09.playground/Sources/MetalView.swift new file mode 100755 index 0000000..27e9042 --- /dev/null +++ b/ch09/chapter09.playground/Sources/MetalView.swift @@ -0,0 +1,100 @@ + +import MetalKit + +public class MetalView: MTKView { + + var vertex_buffer: MTLBuffer! + var uniform_buffer: MTLBuffer! + var index_buffer: MTLBuffer! + var dss: MTLDepthStencilState! + 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) + createBuffers() + createPipeline() + } + + func createBuffers() { + let vertex_data = [ + 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]), + Vertex(pos: [-1.0, 1.0, 1.0, 1.0], col: [0, 1, 0, 1]), + 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: []) + } + + func createPipeline() { + 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")! + let rpld = MTLRenderPipelineDescriptor() + rpld.vertexFunction = vert_func + rpld.fragmentFunction = frag_func + rpld.colorAttachments[0].pixelFormat = .BGRA8Unorm + rps = try device!.newRenderPipelineStateWithDescriptor(rpld) + } catch let e { + Swift.print("\(e)") + } + commandQueue = device?.newCommandQueue() + } + + func update() { + let scaled = scalingMatrix(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 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 modelViewProjectionMatrix = matrix_multiply(projMatrix, matrix_multiply(viewMatrix, modelMatrix)) + let bufferPointer = uniform_buffer.contents() + var uniforms = Uniforms(modelViewProjectionMatrix: modelViewProjectionMatrix) + memcpy(bufferPointer, &uniforms, sizeof(Uniforms)) + } + + override public func drawRect(dirtyRect: NSRect) { + super.drawRect(dirtyRect) + 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() + } + } +} diff --git a/ch09/chapter09.playground/contents.xcplayground b/ch09/chapter09.playground/contents.xcplayground new file mode 100644 index 0000000..06828af --- /dev/null +++ b/ch09/chapter09.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/ch09/chapter09.playground/playground.xcworkspace/contents.xcworkspacedata b/ch09/chapter09.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100755 index 0000000..919434a --- /dev/null +++ b/ch09/chapter09.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/ch09/chapter09.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch09/chapter09.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100755 index 0000000..6294b51 Binary files /dev/null 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 new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/ch09/chapter09.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/ch10/chapter10.playground/Contents.swift b/ch10/chapter10.playground/Contents.swift new file mode 100755 index 0000000..96ca6ff --- /dev/null +++ b/ch10/chapter10.playground/Contents.swift @@ -0,0 +1,8 @@ + +import Cocoa +import XCPlayground + +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 diff --git a/ch10/chapter10.playground/Resources/Shaders.metal b/ch10/chapter10.playground/Resources/Shaders.metal new file mode 100755 index 0000000..66302ca --- /dev/null +++ b/ch10/chapter10.playground/Resources/Shaders.metal @@ -0,0 +1,23 @@ + +#include +using namespace metal; + +float dist(float2 point, float2 center, float radius) +{ + return length(point - center) - radius; +} + +kernel void compute(texture2d output [[texture(0)]], + uint2 gid [[thread_position_in_grid]]) +{ + int width = output.get_width(); + int height = output.get_height(); + float red = float(gid.x) / float(width); + float green = float(gid.y) / float(height); + float2 uv = float2(gid) / float2(width, height); + uv = uv * 2.0 - 1.0; + float distToCircle = dist(uv, float2(0), 0.5); + float distToCircle2 = dist(uv, float2(-0.1, 0.1), 0.5); + bool inside = distToCircle2 < 0; + output.write(inside ? float4(0) : float4(1, 0.7, 0, 1) * (1 - distToCircle), gid); +} \ No newline at end of file diff --git a/ch10/chapter10.playground/Sources/MetalView.swift b/ch10/chapter10.playground/Sources/MetalView.swift new file mode 100755 index 0000000..6cbc65c --- /dev/null +++ b/ch10/chapter10.playground/Sources/MetalView.swift @@ -0,0 +1,46 @@ + +import MetalKit + +public class MetalView: MTKView { + + var queue: MTLCommandQueue! = nil + var cps: MTLComputePipelineState! = nil + + 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 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") + 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)") + } + } +} diff --git a/ch10/chapter10.playground/contents.xcplayground b/ch10/chapter10.playground/contents.xcplayground new file mode 100755 index 0000000..06828af --- /dev/null +++ b/ch10/chapter10.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/ch10/chapter10.playground/playground.xcworkspace/contents.xcworkspacedata b/ch10/chapter10.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100755 index 0000000..919434a --- /dev/null +++ b/ch10/chapter10.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/ch10/chapter10.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch10/chapter10.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100755 index 0000000..b0371a6 Binary files /dev/null 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 new file mode 100755 index 0000000..bf468af --- /dev/null +++ b/ch10/chapter10.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/ch11/chapter11.playground/Contents.swift b/ch11/chapter11.playground/Contents.swift new file mode 100644 index 0000000..00f436f --- /dev/null +++ b/ch11/chapter11.playground/Contents.swift @@ -0,0 +1,8 @@ + +import Cocoa +import XCPlayground + +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 diff --git a/ch11/chapter11.playground/Resources/Shaders.metal b/ch11/chapter11.playground/Resources/Shaders.metal new file mode 100755 index 0000000..bfafc95 --- /dev/null +++ b/ch11/chapter11.playground/Resources/Shaders.metal @@ -0,0 +1,58 @@ + +#include + +using namespace metal; + +float distToCircle(float2 point, float2 center, float radius) +{ + return length(point - center) - radius; +} + +float smootherstep(float e1, float e2, float x) +{ + x = clamp((x - e1) / (e2 - e1), 0.0, 1.0); + return x * x * x * (x * (x * 6 - 15) + 10); +} + +kernel void compute(texture2d output [[texture(0)]], + uint2 gid [[thread_position_in_grid]]) +{ + int width = output.get_width(); + int height = output.get_height(); + float2 uv = float2(gid) / float2(width, height); + +// //uncomment this code block for the 1st part of the tutorial and comment out the others +// uv = uv * 2.0 - 1.0; +// float distance = distToCircle(uv, float2(0), 0.5); +// float xMax = width/height; +// float4 sun = float4(1, 0.7, 0, 1) * (1 - distance); +// float4 planet = float4(0); +// float radius = 0.5; +// float m = smootherstep(radius - 0.005, radius + 0.005, length(uv - float2(xMax-1, 0))); +// float4 pixel = mix(planet, sun, m); +// output.write(pixel, gid); + +// // uncomment this code block for the 2nd part of the tutorial and comment out the others +// float3 color = float3(0.7); +// if(fmod(uv.x, 0.1) < 0.005 || fmod(uv.y, 0.1) < 0.005) color = float3(0,0,1); +// float2 uv_ext = uv * 2.0 - 1.0; +// if(abs(uv_ext.x) < 0.02 || abs(uv_ext.y) < 0.02) color = float3(1, 0, 0); +// if(abs(uv_ext.x - uv_ext.y) < 0.02 || abs(uv_ext.x + uv_ext.y) < 0.02) color = float3(0, 1, 0); +// output.write(float4(color, 1), gid); + + // uncomment this code block for the 3rd part of the tutorial and comment out the others + float2 cc = 1.1*float2( 0.5*cos(0.1) - 0.25*cos(0.2), 0.5*sin(0.1) - 0.25*sin(0.2) ); + float4 dmin = float4(1000.0); + float2 z = (-1.0 + 2.0*uv)*float2(1.7,1.0); + for( int i=0; i<64; i++ ) { + z = cc + float2( z.x*z.x - z.y*z.y, 2.0*z.x*z.y ); + dmin=min(dmin, float4(abs(0.0+z.y + 0.5*sin(z.x)), abs(1.0+z.x + 0.5*sin(z.y)), dot(z,z), length( fract(z)-0.5) ) ); + } + float3 color = float3( dmin.w ); + color = mix( color, float3(1.00,0.80,0.60), min(1.0,pow(dmin.x*0.25,0.20)) ); + color = mix( color, float3(0.72,0.70,0.60), min(1.0,pow(dmin.y*0.50,0.50)) ); + color = mix( color, float3(1.00,1.00,1.00), 1.0-min(1.0,pow(dmin.z*1.00,0.15) )); + color = 1.25*color*color; + color *= 0.5 + 0.5*pow(16.0*uv.x*(1.0-uv.x)*uv.y*(1.0-uv.y),0.15); + output.write(float4(color, 1), gid); +} diff --git a/ch11/chapter11.playground/Sources/MetalView.swift b/ch11/chapter11.playground/Sources/MetalView.swift new file mode 100755 index 0000000..425569a --- /dev/null +++ b/ch11/chapter11.playground/Sources/MetalView.swift @@ -0,0 +1,56 @@ + +import MetalKit + +public class MetalView: MTKView { + + var queue: MTLCommandQueue! = nil + var cps: MTLComputePipelineState! = nil + + 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) + 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") + 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) + } catch let e { + Swift.print("\(e)") + } + } +} diff --git a/ch11/chapter11.playground/contents.xcplayground b/ch11/chapter11.playground/contents.xcplayground new file mode 100644 index 0000000..06828af --- /dev/null +++ b/ch11/chapter11.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/ch11/chapter11.playground/playground.xcworkspace/contents.xcworkspacedata b/ch11/chapter11.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100755 index 0000000..919434a --- /dev/null +++ b/ch11/chapter11.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/ch11/chapter11.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch11/chapter11.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100755 index 0000000..f4c9511 Binary files /dev/null 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 new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/ch11/chapter11.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/ch12/chapter12.playground/Contents.swift b/ch12/chapter12.playground/Contents.swift new file mode 100644 index 0000000..9faf781 --- /dev/null +++ b/ch12/chapter12.playground/Contents.swift @@ -0,0 +1,8 @@ + +import Cocoa +import XCPlayground + +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 diff --git a/ch12/chapter12.playground/Resources/Shaders.metal b/ch12/chapter12.playground/Resources/Shaders.metal new file mode 100755 index 0000000..556aea3 --- /dev/null +++ b/ch12/chapter12.playground/Resources/Shaders.metal @@ -0,0 +1,28 @@ + +#include + +using namespace metal; + +kernel void compute(texture2d output [[texture(0)]], + constant float &timer [[buffer(1)]], + constant float2 &mouse [[buffer(2)]], + uint2 gid [[thread_position_in_grid]]) +{ + int width = output.get_width(); + int height = output.get_height(); + float2 uv = float2(gid) / float2(width, height); + float2 cc = 1.1*float2( 0.5*cos(0.1*timer) - 0.25*cos(0.2*timer), 0.5*sin(0.1*timer) - 0.25*sin(0.2*timer) ); + float4 dmin = float4(1000.0); + float2 z = (-1.0 + 2.0*uv)*float2(1.7,1.0); + for( int i=0; i<64; i++ ) { + z = cc + float2( z.x*z.x - z.y*z.y, 2.0*z.x*z.y ); + dmin=min(dmin, float4(abs(0.0+z.y + 0.5*sin(z.x)), abs(1.0+z.x + 0.5*sin(z.y)), dot(z,z), length( fract(z)-0.5) ) ); + } + float3 color = float3(mouse.x - mouse.y); + color = mix( color, float3(1.00,0.80,0.60), min(1.0,pow(dmin.x*0.25,0.20)) ); + color = mix( color, float3(0.72,0.70,0.60), min(1.0,pow(dmin.y*0.50,0.50)) ); + color = mix( color, float3(1.00,1.00,1.00), 1.0-min(1.0,pow(dmin.z*1.00,0.15) )); + color = 1.25*color*color; + color *= 0.5 + 0.5*pow(16.0*uv.x*(1.0-uv.x)*uv.y*(1.0-uv.y),0.15); + output.write(float4(color, 1), gid); +} diff --git a/ch12/chapter12.playground/Sources/MetalView.swift b/ch12/chapter12.playground/Sources/MetalView.swift new file mode 100755 index 0000000..37c8100 --- /dev/null +++ b/ch12/chapter12.playground/Sources/MetalView.swift @@ -0,0 +1,71 @@ + +import MetalKit + +public class MetalView: MTKView, NSWindowDelegate { + + var queue: MTLCommandQueue! = nil + var cps: MTLComputePipelineState! = nil + var timer: Float = 0 + var timerBuffer: MTLBuffer! + var mouseBuffer: MTLBuffer! + var pos: NSPoint! + + override public func mouseDown(event: NSEvent) { + pos = convertPointToLayer(convertPoint(event.locationInWindow, fromView: nil)) + let scale = layer!.contentsScale + pos.x *= scale + pos.y *= scale + } + + 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 func drawRect(dirtyRect: NSRect) { + super.drawRect(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) + 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.commit() + } + + } + + func update() { + timer += 0.01 + var bufferPointer = timerBuffer.contents() + memcpy(bufferPointer, &timer, sizeof(Float)) + bufferPointer = mouseBuffer.contents() + memcpy(bufferPointer, &pos, sizeof(NSPoint)) + } + + 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: []) + mouseBuffer = device!.newBufferWithLength(sizeof(NSPoint), options: []) + } +} diff --git a/ch12/chapter12.playground/contents.xcplayground b/ch12/chapter12.playground/contents.xcplayground new file mode 100644 index 0000000..06828af --- /dev/null +++ b/ch12/chapter12.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/ch12/chapter12.playground/playground.xcworkspace/contents.xcworkspacedata b/ch12/chapter12.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100755 index 0000000..919434a --- /dev/null +++ b/ch12/chapter12.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/ch12/chapter12.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/ch12/chapter12.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100755 index 0000000..746466e Binary files /dev/null and b/ch12/chapter12.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ch12/chapter12.playground/timeline.xctimeline b/ch12/chapter12.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/ch12/chapter12.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + +