├── 3. Texured cube ├── MetalTryOut-Objc │ ├── bricks.jpeg │ ├── screenshot.png │ ├── Shaders │ │ ├── VertexStruct.swift │ │ ├── VertexBufferGenerator.h │ │ ├── Vertex.swift │ │ ├── METLTexture.h │ │ ├── UniformsBufferGenerator.h │ │ ├── BaseEffect.swift │ │ ├── VertexBufferGenerator.m │ │ ├── Shaders.metal │ │ ├── UniformsBufferGenerator.m │ │ └── METLTexture.swift │ ├── Triangle-Bridging-Header.h │ ├── Main │ │ ├── AppDelegate.swift │ │ ├── Storyboard.storyboard │ │ ├── MetalViewController.swift │ │ └── MetalView.swift │ ├── Images.xcassets │ │ ├── AppIcon.appiconset │ │ │ └── Contents.json │ │ └── LaunchImage.launchimage │ │ │ └── Contents.json │ ├── Info.plist │ └── Nodes │ │ ├── Matrix4.h │ │ ├── Square.swift │ │ ├── Matrix4.m │ │ └── Matrix4x4.swift ├── Textured Cube.xcodeproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ ├── xcuserdata │ │ │ └── haawa799.xcuserdatad │ │ │ │ ├── UserInterfaceState.xcuserstate │ │ │ │ ├── UserInterfaceState[Conflict].xcuserstate │ │ │ │ └── WorkspaceSettings.xcsettings │ │ └── xcshareddata │ │ │ └── Triangle.xccheckout │ └── xcuserdata │ │ └── haawa799.xcuserdatad │ │ └── xcschemes │ │ └── xcschememanagement.plist └── MetalTryOut-ObjcTests │ ├── Info.plist │ └── MetalTryOut_ObjcTests.m ├── 5. Imported Model ├── MetalTryOut-Objc │ ├── bricks.jpeg │ ├── smiley.png │ ├── screenshot.png │ ├── Nodes │ │ ├── Ram │ │ │ ├── char_ram_col.jpg │ │ │ ├── ram.mtl │ │ │ └── Ram.swift │ │ ├── Matrix4.h │ │ ├── TestScene.swift │ │ ├── Square.swift │ │ ├── Matrix4.m │ │ ├── Scene.swift │ │ ├── Matrix4x4.swift │ │ └── Cube.swift │ ├── Shaders │ │ ├── VertexStruct.swift │ │ ├── VertexBufferGenerator.h │ │ ├── METLTexture.h │ │ ├── UniformsBufferGenerator.h │ │ ├── Vertex.swift │ │ ├── VertexBufferGenerator.m │ │ ├── BaseEffect.swift │ │ ├── METLTexture.swift │ │ └── UniformsBufferGenerator.m │ ├── Triangle-Bridging-Header.h │ ├── Main │ │ ├── AppDelegate.swift │ │ ├── FrameBuffer.h │ │ ├── MySceneViewController.swift │ │ ├── MetalView.h │ │ ├── Storyboard.storyboard │ │ ├── MetalViewController.swift │ │ └── MetalView[Conflict].swift │ ├── Images.xcassets │ │ ├── AppIcon.appiconset │ │ │ └── Contents.json │ │ └── LaunchImage.launchimage │ │ │ └── Contents.json │ └── Info.plist ├── Imported Model.xcodeproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ ├── xcuserdata │ │ │ └── haawa799.xcuserdatad │ │ │ │ ├── UserInterfaceState.xcuserstate │ │ │ │ ├── UserInterfaceState[Conflict].xcuserstate │ │ │ │ ├── UserInterfaceState[Conflict 1].xcuserstate │ │ │ │ ├── UserInterfaceState[Conflict 2].xcuserstate │ │ │ │ └── WorkspaceSettings.xcsettings │ │ └── xcshareddata │ │ │ ├── Imported Model.xccheckout │ │ │ └── Triangle.xccheckout │ └── xcuserdata │ │ └── haawa799.xcuserdatad │ │ └── xcschemes │ │ └── xcschememanagement.plist └── MetalTryOut-ObjcTests │ ├── Info.plist │ └── MetalTryOut_ObjcTests.m ├── 4. Cube + Lighting ├── MetalTryOut-Objc │ ├── bricks.jpeg │ ├── screenshot.png │ ├── Shaders │ │ ├── VertexStruct.swift │ │ ├── VertexBufferGenerator.h │ │ ├── METLTexture.h │ │ ├── Vertex.swift │ │ ├── UniformsBufferGenerator.h │ │ ├── VertexBufferGenerator.m │ │ ├── BaseEffect.swift │ │ ├── METLTexture.swift │ │ └── UniformsBufferGenerator.m │ ├── Triangle-Bridging-Header.h │ ├── Main │ │ ├── AppDelegate.swift │ │ ├── Storyboard.storyboard │ │ ├── MetalViewController.swift │ │ └── MetalView.swift │ ├── Images.xcassets │ │ ├── AppIcon.appiconset │ │ │ └── Contents.json │ │ └── LaunchImage.launchimage │ │ │ └── Contents.json │ ├── Info.plist │ └── Nodes │ │ ├── Matrix4.h │ │ ├── Square.swift │ │ ├── Matrix4.m │ │ └── Matrix4x4.swift ├── Cube Lighting.xcodeproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ ├── xcuserdata │ │ │ └── haawa799.xcuserdatad │ │ │ │ ├── UserInterfaceState.xcuserstate │ │ │ │ ├── UserInterfaceState[Conflict].xcuserstate │ │ │ │ ├── UserInterfaceState[Conflict 1].xcuserstate │ │ │ │ └── WorkspaceSettings.xcsettings │ │ └── xcshareddata │ │ │ └── Triangle.xccheckout │ └── xcuserdata │ │ └── haawa799.xcuserdatad │ │ └── xcschemes │ │ └── xcschememanagement.plist └── MetalTryOut-ObjcTests │ ├── Info.plist │ └── MetalTryOut_ObjcTests.m ├── 1. Colored Triangle ├── MetalTryOut-Objc │ ├── screenshot.png │ ├── Triangle-Bridging-Header.h │ ├── Main │ │ ├── AppDelegate.swift │ │ ├── MetalViewController.swift │ │ ├── Storyboard.storyboard │ │ └── MetalView.swift │ ├── Images.xcassets │ │ ├── AppIcon.appiconset │ │ │ └── Contents.json │ │ └── LaunchImage.launchimage │ │ │ └── Contents.json │ ├── Shaders │ │ ├── VertexBufferGenerator.h │ │ ├── Vertex.swift │ │ ├── Shaders.metal │ │ ├── VertexBufferGenerator.m │ │ └── BaseEffect.swift │ ├── Nodes │ │ ├── Triangle.swift │ │ └── Model.swift │ └── Info.plist ├── Triangle.xcodeproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ ├── xcuserdata │ │ │ └── haawa799.xcuserdatad │ │ │ │ ├── UserInterfaceState.xcuserstate │ │ │ │ └── WorkspaceSettings.xcsettings │ │ └── xcshareddata │ │ │ └── Triangle.xccheckout │ └── xcuserdata │ │ └── haawa799.xcuserdatad │ │ ├── xcschemes │ │ └── xcschememanagement.plist │ │ └── xcdebugger │ │ └── Breakpoints_v2.xcbkptlist └── MetalTryOut-ObjcTests │ ├── Info.plist │ └── MetalTryOut_ObjcTests.m ├── 2. Cube + Transform ├── MetalTryOut-Objc │ ├── screenshot.png │ ├── Triangle-Bridging-Header.h │ ├── Shaders │ │ ├── VertexStruct.swift │ │ ├── VertexBufferGenerator.h │ │ ├── Vertex.swift │ │ ├── UniformsBufferGenerator.h │ │ ├── UniformsBufferGenerator.m │ │ ├── BaseEffect.swift │ │ ├── VertexBufferGenerator.m │ │ └── Shaders.metal │ ├── Main │ │ ├── AppDelegate.swift │ │ ├── Storyboard.storyboard │ │ ├── MetalViewController.swift │ │ └── MetalView.swift │ ├── Images.xcassets │ │ ├── AppIcon.appiconset │ │ │ └── Contents.json │ │ └── LaunchImage.launchimage │ │ │ └── Contents.json │ ├── Nodes │ │ ├── Triangle.swift │ │ ├── Square.swift │ │ ├── Matrix4.h │ │ ├── Matrix4.m │ │ ├── Cube.swift │ │ └── Matrix4x4.swift │ └── Info.plist ├── Cube+Transform.xcodeproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ ├── xcuserdata │ │ │ └── haawa799.xcuserdatad │ │ │ │ ├── UserInterfaceState.xcuserstate │ │ │ │ ├── UserInterfaceState[Conflict].xcuserstate │ │ │ │ └── WorkspaceSettings.xcsettings │ │ └── xcshareddata │ │ │ └── Triangle.xccheckout │ └── xcuserdata │ │ └── haawa799.xcuserdatad │ │ └── xcschemes │ │ └── xcschememanagement.plist └── MetalTryOut-ObjcTests │ ├── Info.plist │ └── MetalTryOut_ObjcTests.m └── README.md /3. Texured cube/MetalTryOut-Objc/bricks.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/3. Texured cube/MetalTryOut-Objc/bricks.jpeg -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/bricks.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/5. Imported Model/MetalTryOut-Objc/bricks.jpeg -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/smiley.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/5. Imported Model/MetalTryOut-Objc/smiley.png -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/3. Texured cube/MetalTryOut-Objc/screenshot.png -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/bricks.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/4. Cube + Lighting/MetalTryOut-Objc/bricks.jpeg -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-Objc/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/1. Colored Triangle/MetalTryOut-Objc/screenshot.png -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/2. Cube + Transform/MetalTryOut-Objc/screenshot.png -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/4. Cube + Lighting/MetalTryOut-Objc/screenshot.png -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/5. Imported Model/MetalTryOut-Objc/screenshot.png -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Nodes/Ram/char_ram_col.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/5. Imported Model/MetalTryOut-Objc/Nodes/Ram/char_ram_col.jpg -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-Objc/Triangle-Bridging-Header.h: -------------------------------------------------------------------------------- 1 | // 2 | // Use this file to import your target's public headers that you would like to expose to Swift. 3 | // 4 | 5 | #import "VertexBufferGenerator.h" -------------------------------------------------------------------------------- /1. Colored Triangle/Triangle.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /3. Texured cube/Textured Cube.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /4. Cube + Lighting/Cube Lighting.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /2. Cube + Transform/Cube+Transform.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /5. Imported Model/Imported Model.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /1. Colored Triangle/Triangle.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/1. Colored Triangle/Triangle.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Triangle-Bridging-Header.h: -------------------------------------------------------------------------------- 1 | // 2 | // Use this file to import your target's public headers that you would like to expose to Swift. 3 | // 4 | 5 | #import "VertexBufferGenerator.h" 6 | #import "UniformsBufferGenerator.h" 7 | 8 | #import "Matrix4.h" 9 | 10 | -------------------------------------------------------------------------------- /3. Texured cube/Textured Cube.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/3. Texured cube/Textured Cube.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /4. Cube + Lighting/Cube Lighting.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/4. Cube + Lighting/Cube Lighting.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /5. Imported Model/Imported Model.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/5. Imported Model/Imported Model.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /2. Cube + Transform/Cube+Transform.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/2. Cube + Transform/Cube+Transform.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Shaders/VertexStruct.swift: -------------------------------------------------------------------------------- 1 | // 2 | // VertexStruct.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/25/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class VertexStruct: NSObject { 12 | 13 | } 14 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Shaders/VertexStruct.swift: -------------------------------------------------------------------------------- 1 | // 2 | // VertexStruct.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/25/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class VertexStruct: NSObject { 12 | 13 | } 14 | -------------------------------------------------------------------------------- /3. Texured cube/Textured Cube.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState[Conflict].xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/3. Texured cube/Textured Cube.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState[Conflict].xcuserstate -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Shaders/VertexStruct.swift: -------------------------------------------------------------------------------- 1 | // 2 | // VertexStruct.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/25/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class VertexStruct: NSObject { 12 | 13 | } 14 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Shaders/VertexStruct.swift: -------------------------------------------------------------------------------- 1 | // 2 | // VertexStruct.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/25/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class VertexStruct: NSObject { 12 | 13 | } 14 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Triangle-Bridging-Header.h: -------------------------------------------------------------------------------- 1 | // 2 | // Use this file to import your target's public headers that you would like to expose to Swift. 3 | // 4 | 5 | #import "VertexBufferGenerator.h" 6 | #import "UniformsBufferGenerator.h" 7 | #import "METLTexture.h" 8 | 9 | #import "Matrix4.h" 10 | 11 | -------------------------------------------------------------------------------- /4. Cube + Lighting/Cube Lighting.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState[Conflict].xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/4. Cube + Lighting/Cube Lighting.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState[Conflict].xcuserstate -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Triangle-Bridging-Header.h: -------------------------------------------------------------------------------- 1 | // 2 | // Use this file to import your target's public headers that you would like to expose to Swift. 3 | // 4 | 5 | #import "VertexBufferGenerator.h" 6 | #import "UniformsBufferGenerator.h" 7 | #import "METLTexture.h" 8 | 9 | #import "Matrix4.h" 10 | 11 | -------------------------------------------------------------------------------- /5. Imported Model/Imported Model.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState[Conflict].xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/5. Imported Model/Imported Model.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState[Conflict].xcuserstate -------------------------------------------------------------------------------- /2. Cube + Transform/Cube+Transform.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState[Conflict].xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/2. Cube + Transform/Cube+Transform.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState[Conflict].xcuserstate -------------------------------------------------------------------------------- /4. Cube + Lighting/Cube Lighting.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState[Conflict 1].xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/4. Cube + Lighting/Cube Lighting.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState[Conflict 1].xcuserstate -------------------------------------------------------------------------------- /5. Imported Model/Imported Model.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState[Conflict 1].xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/5. Imported Model/Imported Model.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState[Conflict 1].xcuserstate -------------------------------------------------------------------------------- /5. Imported Model/Imported Model.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState[Conflict 2].xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haawa799/METAL_Playground/HEAD/5. Imported Model/Imported Model.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/UserInterfaceState[Conflict 2].xcuserstate -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Triangle-Bridging-Header.h: -------------------------------------------------------------------------------- 1 | // 2 | // Use this file to import your target's public headers that you would like to expose to Swift. 3 | // 4 | 5 | 6 | #import "VertexBufferGenerator.h" 7 | #import "UniformsBufferGenerator.h" 8 | #import "METLTexture.h" 9 | #import "FrameBuffer.h" 10 | 11 | #import "Matrix4.h" 12 | 13 | -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-Objc/Main/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/25/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | var window: UIWindow? 14 | } 15 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Main/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/25/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | var window: UIWindow? 14 | } 15 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Main/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/25/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | var window: UIWindow? 14 | } 15 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Main/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/25/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | var window: UIWindow? 14 | } 15 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Main/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/25/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | var window: UIWindow? 14 | } 15 | -------------------------------------------------------------------------------- /1. Colored Triangle/Triangle.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges 6 | 7 | SnapshotAutomaticallyBeforeSignificantChanges 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /3. Texured cube/Textured Cube.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges 6 | 7 | SnapshotAutomaticallyBeforeSignificantChanges 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /2. Cube + Transform/Cube+Transform.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges 6 | 7 | SnapshotAutomaticallyBeforeSignificantChanges 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /4. Cube + Lighting/Cube Lighting.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges 6 | 7 | SnapshotAutomaticallyBeforeSignificantChanges 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /5. Imported Model/Imported Model.xcodeproj/project.xcworkspace/xcuserdata/haawa799.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges 6 | 7 | SnapshotAutomaticallyBeforeSignificantChanges 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "40x40", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "60x60", 16 | "scale" : "2x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "40x40", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "60x60", 16 | "scale" : "2x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "40x40", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "60x60", 16 | "scale" : "2x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-Objc/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "40x40", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "60x60", 16 | "scale" : "2x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "40x40", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "60x60", 16 | "scale" : "2x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Shaders/VertexBufferGenerator.h: -------------------------------------------------------------------------------- 1 | // 2 | // VertexBufferGenerator.h 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | 13 | @interface VertexBufferGenerator : NSObject 14 | 15 | + (id )generateBufferVertices:(NSArray *)vertices 16 | vertexCount:(NSNumber *)vertexCount 17 | device:(id )device; 18 | 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Shaders/VertexBufferGenerator.h: -------------------------------------------------------------------------------- 1 | // 2 | // VertexBufferGenerator.h 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | 13 | @interface VertexBufferGenerator : NSObject 14 | 15 | + (id )generateBufferVertices:(NSArray *)vertices 16 | vertexCount:(NSNumber *)vertexCount 17 | device:(id )device; 18 | 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Shaders/VertexBufferGenerator.h: -------------------------------------------------------------------------------- 1 | // 2 | // VertexBufferGenerator.h 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | 13 | @interface VertexBufferGenerator : NSObject 14 | 15 | + (id )generateBufferVertices:(NSArray *)vertices 16 | vertexCount:(NSNumber *)vertexCount 17 | device:(id )device; 18 | 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-Objc/Shaders/VertexBufferGenerator.h: -------------------------------------------------------------------------------- 1 | // 2 | // VertexBufferGenerator.h 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | 13 | @interface VertexBufferGenerator : NSObject 14 | 15 | + (id )generateBufferVertices:(NSArray *)vertices 16 | vertexCount:(NSNumber *)vertexCount 17 | device:(id )device; 18 | 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Shaders/VertexBufferGenerator.h: -------------------------------------------------------------------------------- 1 | // 2 | // VertexBufferGenerator.h 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | 13 | @interface VertexBufferGenerator : NSObject 14 | 15 | + (id )generateBufferVertices:(NSArray *)vertices 16 | vertexCount:(NSNumber *)vertexCount 17 | device:(id )device; 18 | 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-Objc/Images.xcassets/LaunchImage.launchimage/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "orientation" : "portrait", 5 | "idiom" : "iphone", 6 | "extent" : "full-screen", 7 | "minimum-system-version" : "7.0", 8 | "scale" : "2x" 9 | }, 10 | { 11 | "orientation" : "portrait", 12 | "idiom" : "iphone", 13 | "subtype" : "retina4", 14 | "extent" : "full-screen", 15 | "minimum-system-version" : "7.0", 16 | "scale" : "2x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Images.xcassets/LaunchImage.launchimage/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "orientation" : "portrait", 5 | "idiom" : "iphone", 6 | "extent" : "full-screen", 7 | "minimum-system-version" : "7.0", 8 | "scale" : "2x" 9 | }, 10 | { 11 | "orientation" : "portrait", 12 | "idiom" : "iphone", 13 | "subtype" : "retina4", 14 | "extent" : "full-screen", 15 | "minimum-system-version" : "7.0", 16 | "scale" : "2x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Images.xcassets/LaunchImage.launchimage/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "orientation" : "portrait", 5 | "idiom" : "iphone", 6 | "extent" : "full-screen", 7 | "minimum-system-version" : "7.0", 8 | "scale" : "2x" 9 | }, 10 | { 11 | "orientation" : "portrait", 12 | "idiom" : "iphone", 13 | "subtype" : "retina4", 14 | "extent" : "full-screen", 15 | "minimum-system-version" : "7.0", 16 | "scale" : "2x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Images.xcassets/LaunchImage.launchimage/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "orientation" : "portrait", 5 | "idiom" : "iphone", 6 | "extent" : "full-screen", 7 | "minimum-system-version" : "7.0", 8 | "scale" : "2x" 9 | }, 10 | { 11 | "orientation" : "portrait", 12 | "idiom" : "iphone", 13 | "subtype" : "retina4", 14 | "extent" : "full-screen", 15 | "minimum-system-version" : "7.0", 16 | "scale" : "2x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Images.xcassets/LaunchImage.launchimage/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "orientation" : "portrait", 5 | "idiom" : "iphone", 6 | "extent" : "full-screen", 7 | "minimum-system-version" : "7.0", 8 | "scale" : "2x" 9 | }, 10 | { 11 | "orientation" : "portrait", 12 | "idiom" : "iphone", 13 | "subtype" : "retina4", 14 | "extent" : "full-screen", 15 | "minimum-system-version" : "7.0", 16 | "scale" : "2x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-Objc/Shaders/Vertex.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Vertex.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @objc class Vertex: NSObject 12 | { 13 | var x,y,z,w,r,g,b,a: Float 14 | 15 | init(x:Float, y:Float, z:Float, w:Float, r:Float, g:Float, b:Float, a:Float) 16 | { 17 | self.x = x 18 | self.y = y 19 | self.z = z 20 | self.w = w 21 | self.r = r 22 | self.g = g 23 | self.b = b 24 | self.a = a 25 | 26 | super.init() 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Shaders/Vertex.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Vertex.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @objc class Vertex: NSObject 12 | { 13 | var x,y,z,w,r,g,b,a: Float 14 | 15 | init(x:Float, y:Float, z:Float, w:Float, r:Float, g:Float, b:Float, a:Float) 16 | { 17 | self.x = x 18 | self.y = y 19 | self.z = z 20 | self.w = w 21 | self.r = r 22 | self.g = g 23 | self.b = b 24 | self.a = a 25 | 26 | super.init() 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Shaders/UniformsBufferGenerator.h: -------------------------------------------------------------------------------- 1 | // 2 | // UniformsBufferGenerator.h 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/26/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | #import "Matrix4.h" 12 | 13 | @class Matrix4x4; 14 | 15 | @interface UniformsBufferGenerator : NSObject 16 | 17 | + (id )generateUniformBufferProjectionMatrix:(Matrix4 *)projMatrix 18 | modelViewMatrix:(Matrix4 *)mvMatrix 19 | device:(id )device; 20 | 21 | @end 22 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Shaders/Vertex.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Vertex.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @objc class Vertex: NSObject 12 | { 13 | var x,y,z,r,g,b,a,u,v: Float 14 | 15 | init(x:Float, y:Float, z:Float, r:Float, g:Float, b:Float, a:Float, u:Float ,v:Float) 16 | { 17 | self.x = x 18 | self.y = y 19 | self.z = z 20 | self.r = r 21 | self.g = g 22 | self.b = b 23 | self.a = a 24 | self.u = u 25 | self.v = v 26 | 27 | super.init() 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /1. Colored Triangle/Triangle.xcodeproj/xcuserdata/haawa799.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | MetalTryOut-Objc.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 4304BFCA195455A800326016 16 | 17 | primary 18 | 19 | 20 | 4304BFE0195455A800326016 21 | 22 | primary 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /3. Texured cube/Textured Cube.xcodeproj/xcuserdata/haawa799.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | MetalTryOut-Objc.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 4304BFCA195455A800326016 16 | 17 | primary 18 | 19 | 20 | 4304BFE0195455A800326016 21 | 22 | primary 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /4. Cube + Lighting/Cube Lighting.xcodeproj/xcuserdata/haawa799.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | MetalTryOut-Objc.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 4304BFCA195455A800326016 16 | 17 | primary 18 | 19 | 20 | 4304BFE0195455A800326016 21 | 22 | primary 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /5. Imported Model/Imported Model.xcodeproj/xcuserdata/haawa799.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | MetalTryOut-Objc.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 4304BFCA195455A800326016 16 | 17 | primary 18 | 19 | 20 | 4304BFE0195455A800326016 21 | 22 | primary 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /2. Cube + Transform/Cube+Transform.xcodeproj/xcuserdata/haawa799.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | MetalTryOut-Objc.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 4304BFCA195455A800326016 16 | 17 | primary 18 | 19 | 20 | 4304BFE0195455A800326016 21 | 22 | primary 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-Objc/Nodes/Triangle.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Triangle.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | 12 | @objc class Triangle: Model 13 | { 14 | init(baseEffect: BaseEffect) 15 | { 16 | var A = Vertex(x: -1.0, y: -1.0, z: 0.0, w: 1.0, r: 1.0, g: 0.0, b: 0.0, a: 1.0) 17 | var B = Vertex(x: 1.0, y: -1.0, z: 0.0, w: 1.0, r: 0.0, g: 1.0, b: 0.0, a: 1.0) 18 | var C = Vertex(x: 0.0, y: 0.0, z: 0.0, w: 1.0, r: 0.0, g: 0.0, b: 1.0, a: 1.0) 19 | 20 | var verticesArray:Array = [A,B,C] 21 | 22 | super.init(name: "Triangle", baseEffect: baseEffect, vertices: verticesArray, vertexCount:3) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-ObjcTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.kharchyshyn.andrew.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-ObjcTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.kharchyshyn.andrew.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-ObjcTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.kharchyshyn.andrew.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-ObjcTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.kharchyshyn.andrew.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-ObjcTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.kharchyshyn.andrew.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-Objc/Shaders/Shaders.metal: -------------------------------------------------------------------------------- 1 | // 2 | // Shaders.metal 3 | // MetalTryOut-Objc 4 | // 5 | // Created by Andrew K. on 6/22/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace metal; 11 | 12 | 13 | struct Vertex{ 14 | float4 position; 15 | float4 color; 16 | }; 17 | 18 | struct VertexOut 19 | { 20 | float4 position [[position]]; 21 | float4 color; 22 | }; 23 | 24 | 25 | vertex VertexOut myVertexShader(const device Vertex* vertexArray [[buffer(0)]], 26 | unsigned int vid [[vertex_id]]) 27 | { 28 | VertexOut out; 29 | out.position = vertexArray[vid].position; 30 | out.color = vertexArray[vid].color; 31 | return out; 32 | } 33 | 34 | 35 | fragment float4 myFragmentShader(VertexOut interpolated [[stage_in]]) 36 | { 37 | return interpolated.color; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Shaders/METLTexture.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2014 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | 7 | Simple Utility class for creating a 2d texture 8 | 9 | */ 10 | 11 | #import 12 | #import 13 | 14 | @interface METLTexture : NSObject 15 | 16 | @property (nonatomic) id texture; 17 | @property (nonatomic) MTLTextureType target; 18 | @property (nonatomic) uint32_t width; 19 | @property (nonatomic) uint32_t height; 20 | @property (nonatomic) uint32_t depth; 21 | @property (nonatomic) uint32_t format; 22 | @property (nonatomic) BOOL hasAlpha; 23 | @property (nonatomic) NSString *path; 24 | 25 | - (id) initWithResourceName:(NSString *)name 26 | ext:(NSString *)ext; 27 | 28 | - (BOOL) finalize:(id)device; 29 | 30 | - (BOOL) finalize:(id)device 31 | flip:(BOOL)flip; 32 | 33 | @end 34 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Shaders/METLTexture.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2014 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | 7 | Simple Utility class for creating a 2d texture 8 | 9 | */ 10 | 11 | #import 12 | #import 13 | 14 | @interface METLTexture : NSObject 15 | 16 | @property (nonatomic) id texture; 17 | @property (nonatomic) MTLTextureType target; 18 | @property (nonatomic) uint32_t width; 19 | @property (nonatomic) uint32_t height; 20 | @property (nonatomic) uint32_t depth; 21 | @property (nonatomic) uint32_t format; 22 | @property (nonatomic) BOOL hasAlpha; 23 | @property (nonatomic) NSString *path; 24 | 25 | - (id) initWithResourceName:(NSString *)name 26 | ext:(NSString *)ext; 27 | 28 | - (BOOL) finalize:(id)device; 29 | 30 | - (BOOL) finalize:(id)device 31 | flip:(BOOL)flip; 32 | 33 | @end 34 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Shaders/METLTexture.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2014 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | 7 | Simple Utility class for creating a 2d texture 8 | 9 | */ 10 | 11 | #import 12 | #import 13 | 14 | @interface METLTexture : NSObject 15 | 16 | @property (nonatomic) id texture; 17 | @property (nonatomic) MTLTextureType target; 18 | @property (nonatomic) uint32_t width; 19 | @property (nonatomic) uint32_t height; 20 | @property (nonatomic) uint32_t depth; 21 | @property (nonatomic) uint32_t format; 22 | @property (nonatomic) BOOL hasAlpha; 23 | @property (nonatomic) NSString *path; 24 | 25 | - (id) initWithResourceName:(NSString *)name 26 | ext:(NSString *)ext; 27 | 28 | - (BOOL) finalize:(id)device; 29 | 30 | - (BOOL) finalize:(id)device 31 | flip:(BOOL)flip; 32 | 33 | @end 34 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Nodes/Triangle.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Triangle.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @objc class Triangle: Model 12 | { 13 | init(baseEffect: BaseEffect) 14 | { 15 | var A = Vertex(x: -1.0, y: -1.0, z: 0.0, w: 1.0, r: 1.0, g: 0.0, b: 0.0, a: 1.0) 16 | var B = Vertex(x: 1.0, y: -1.0, z: 0.0, w: 1.0, r: 0.0, g: 1.0, b: 0.0, a: 1.0) 17 | var C = Vertex(x: -1.0, y: 1.0, z: 0.0, w: 1.0, r: 0.0, g: 0.0, b: 1.0, a: 1.0) 18 | 19 | var verticesArray:Array = [A,B,C] 20 | 21 | super.init(name: "Triangle", baseEffect: baseEffect, vertices: verticesArray, vertexCount: 3) 22 | } 23 | 24 | override func updateWithDelta(delta: CFTimeInterval) 25 | { 26 | super.updateWithDelta(delta) 27 | var secsPerMove: Float = 2.0 28 | positionX = sinf( Float(time) * 2.0 * Float(M_PI) / secsPerMove) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Shaders/Vertex.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Vertex.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @objc class Vertex: NSObject 12 | { 13 | var x,y,z,u,v,nX,nY,nZ: Float 14 | 15 | 16 | init(x:Float, y:Float, z:Float, u:Float ,v:Float ,nX:Float ,nY:Float ,nZ:Float) 17 | { 18 | self.x = x 19 | self.y = y 20 | self.z = z 21 | 22 | self.u = u 23 | self.v = v 24 | 25 | self.nX = nX 26 | self.nY = nY 27 | self.nZ = nZ 28 | 29 | super.init() 30 | } 31 | /* 32 | init(x:Float, y:Float, z:Float, r:Float, g:Float, b:Float, a:Float, u:Float ,v:Float) 33 | { 34 | self.x = x 35 | self.y = y 36 | self.z = z 37 | self.r = r 38 | self.g = g 39 | self.b = b 40 | self.a = a 41 | self.u = u 42 | self.v = v 43 | 44 | super.init() 45 | } 46 | */ 47 | } 48 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-ObjcTests/MetalTryOut_ObjcTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // MetalTryOut_ObjcTests.m 3 | // MetalTryOut-ObjcTests 4 | // 5 | // Created by Andrew K. on 6/20/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface MetalTryOut_ObjcTests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation MetalTryOut_ObjcTests 16 | 17 | - (void)setUp { 18 | [super setUp]; 19 | // Put setup code here. This method is called before the invocation of each test method in the class. 20 | } 21 | 22 | - (void)tearDown { 23 | // Put teardown code here. This method is called after the invocation of each test method in the class. 24 | [super tearDown]; 25 | } 26 | 27 | - (void)testExample { 28 | // This is an example of a functional test case. 29 | XCTAssert(YES, @"Pass"); 30 | } 31 | 32 | - (void)testPerformanceExample { 33 | // This is an example of a performance test case. 34 | [self measureBlock:^{ 35 | // Put the code you want to measure the time of here. 36 | }]; 37 | } 38 | 39 | @end 40 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-ObjcTests/MetalTryOut_ObjcTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // MetalTryOut_ObjcTests.m 3 | // MetalTryOut-ObjcTests 4 | // 5 | // Created by Andrew K. on 6/20/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface MetalTryOut_ObjcTests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation MetalTryOut_ObjcTests 16 | 17 | - (void)setUp { 18 | [super setUp]; 19 | // Put setup code here. This method is called before the invocation of each test method in the class. 20 | } 21 | 22 | - (void)tearDown { 23 | // Put teardown code here. This method is called after the invocation of each test method in the class. 24 | [super tearDown]; 25 | } 26 | 27 | - (void)testExample { 28 | // This is an example of a functional test case. 29 | XCTAssert(YES, @"Pass"); 30 | } 31 | 32 | - (void)testPerformanceExample { 33 | // This is an example of a performance test case. 34 | [self measureBlock:^{ 35 | // Put the code you want to measure the time of here. 36 | }]; 37 | } 38 | 39 | @end 40 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-ObjcTests/MetalTryOut_ObjcTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // MetalTryOut_ObjcTests.m 3 | // MetalTryOut-ObjcTests 4 | // 5 | // Created by Andrew K. on 6/20/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface MetalTryOut_ObjcTests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation MetalTryOut_ObjcTests 16 | 17 | - (void)setUp { 18 | [super setUp]; 19 | // Put setup code here. This method is called before the invocation of each test method in the class. 20 | } 21 | 22 | - (void)tearDown { 23 | // Put teardown code here. This method is called after the invocation of each test method in the class. 24 | [super tearDown]; 25 | } 26 | 27 | - (void)testExample { 28 | // This is an example of a functional test case. 29 | XCTAssert(YES, @"Pass"); 30 | } 31 | 32 | - (void)testPerformanceExample { 33 | // This is an example of a performance test case. 34 | [self measureBlock:^{ 35 | // Put the code you want to measure the time of here. 36 | }]; 37 | } 38 | 39 | @end 40 | -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-ObjcTests/MetalTryOut_ObjcTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // MetalTryOut_ObjcTests.m 3 | // MetalTryOut-ObjcTests 4 | // 5 | // Created by Andrew K. on 6/20/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface MetalTryOut_ObjcTests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation MetalTryOut_ObjcTests 16 | 17 | - (void)setUp { 18 | [super setUp]; 19 | // Put setup code here. This method is called before the invocation of each test method in the class. 20 | } 21 | 22 | - (void)tearDown { 23 | // Put teardown code here. This method is called after the invocation of each test method in the class. 24 | [super tearDown]; 25 | } 26 | 27 | - (void)testExample { 28 | // This is an example of a functional test case. 29 | XCTAssert(YES, @"Pass"); 30 | } 31 | 32 | - (void)testPerformanceExample { 33 | // This is an example of a performance test case. 34 | [self measureBlock:^{ 35 | // Put the code you want to measure the time of here. 36 | }]; 37 | } 38 | 39 | @end 40 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-ObjcTests/MetalTryOut_ObjcTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // MetalTryOut_ObjcTests.m 3 | // MetalTryOut-ObjcTests 4 | // 5 | // Created by Andrew K. on 6/20/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface MetalTryOut_ObjcTests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation MetalTryOut_ObjcTests 16 | 17 | - (void)setUp { 18 | [super setUp]; 19 | // Put setup code here. This method is called before the invocation of each test method in the class. 20 | } 21 | 22 | - (void)tearDown { 23 | // Put teardown code here. This method is called after the invocation of each test method in the class. 24 | [super tearDown]; 25 | } 26 | 27 | - (void)testExample { 28 | // This is an example of a functional test case. 29 | XCTAssert(YES, @"Pass"); 30 | } 31 | 32 | - (void)testPerformanceExample { 33 | // This is an example of a performance test case. 34 | [self measureBlock:^{ 35 | // Put the code you want to measure the time of here. 36 | }]; 37 | } 38 | 39 | @end 40 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.kharchyshyn.andrew.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UIMainStoryboardFile 26 | Storyboard 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-Objc/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.kharchyshyn.andrew.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UIMainStoryboardFile 26 | Storyboard 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.kharchyshyn.andrew.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UIMainStoryboardFile 26 | Storyboard 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.kharchyshyn.andrew.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UIMainStoryboardFile 26 | Storyboard 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Nodes/Square.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Square.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/27/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @objc class Square: Model { 12 | 13 | init(baseEffect: BaseEffect) 14 | { 15 | var A = Vertex(x: -1.0, y: 1.0, z: 0.0, w: 1.0, r: 1.0, g: 0.0, b: 0.0, a: 1.0) 16 | var B = Vertex(x: -1.0, y: -1.0, z: 0.0, w: 1.0, r: 0.0, g: 1.0, b: 0.0, a: 1.0) 17 | var C = Vertex(x: 1.0, y: -1.0, z: 0.0, w: 1.0, r: 0.0, g: 0.0, b: 1.0, a: 1.0) 18 | var D = Vertex(x: 1.0, y: 1.0, z: 0.0, w: 1.0, r: 0.1, g: 0.6, b: 0.4, a: 1.0) 19 | 20 | var verticesArray:Array = [A,B,C ,A,C,D] 21 | 22 | super.init(name: "Square", baseEffect: baseEffect, vertices: verticesArray, vertexCount: verticesArray.count) 23 | } 24 | 25 | override func updateWithDelta(delta: CFTimeInterval) 26 | { 27 | super.updateWithDelta(delta) 28 | 29 | var secsPerMove: Float = 2.0 30 | positionX = sinf( Float(time) * 2.0 * Float(M_PI) / secsPerMove) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Nodes/Matrix4.h: -------------------------------------------------------------------------------- 1 | // 2 | // Matrix4.h 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/27/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | #import 12 | 13 | // Wraper around GLKMath's GLKMatrix4, becouse we can't use it directly from Swift code 14 | 15 | @interface Matrix4 : NSObject 16 | { 17 | @public 18 | GLKMatrix4 glkMatrix; 19 | } 20 | 21 | + (float)degreesToRad:(float)degrees; 22 | 23 | + (Matrix4 *)makePerspectiveViewAngle:(float)angleRad 24 | aspectRatio:(float)aspect 25 | nearZ:(float)nearZ 26 | farZ:(float)farZ; 27 | 28 | - (void)transpose; 29 | 30 | - (void)multiplyLeft:(Matrix4 *)matrix; 31 | 32 | - (void)rotateAroundX:(float)xAngleRad 33 | y:(float)yAngleRad 34 | z:(float)zAngleRad; 35 | 36 | - (void)translate:(float)x 37 | y:(float)y 38 | z:(float)z; 39 | 40 | - (void)scale:(float)x 41 | y:(float)y 42 | z:(float)z; 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Nodes/Matrix4.h: -------------------------------------------------------------------------------- 1 | // 2 | // Matrix4.h 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/27/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | #import 12 | 13 | // Wraper around GLKMath's GLKMatrix4, becouse we can't use it directly from Swift code 14 | 15 | @interface Matrix4 : NSObject 16 | { 17 | @public 18 | GLKMatrix4 glkMatrix; 19 | } 20 | 21 | + (float)degreesToRad:(float)degrees; 22 | 23 | + (Matrix4 *)makePerspectiveViewAngle:(float)angleRad 24 | aspectRatio:(float)aspect 25 | nearZ:(float)nearZ 26 | farZ:(float)farZ; 27 | 28 | - (void)transpose; 29 | 30 | - (void)multiplyLeft:(Matrix4 *)matrix; 31 | 32 | - (void)rotateAroundX:(float)xAngleRad 33 | y:(float)yAngleRad 34 | z:(float)zAngleRad; 35 | 36 | - (void)translate:(float)x 37 | y:(float)y 38 | z:(float)z; 39 | 40 | - (void)scale:(float)x 41 | y:(float)y 42 | z:(float)z; 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Nodes/Matrix4.h: -------------------------------------------------------------------------------- 1 | // 2 | // Matrix4.h 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/27/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | #import 12 | 13 | // Wraper around GLKMath's GLKMatrix4, becouse we can't use it directly from Swift code 14 | 15 | @interface Matrix4 : NSObject 16 | { 17 | @public 18 | GLKMatrix4 glkMatrix; 19 | } 20 | 21 | + (float)degreesToRad:(float)degrees; 22 | 23 | + (Matrix4 *)makePerspectiveViewAngle:(float)angleRad 24 | aspectRatio:(float)aspect 25 | nearZ:(float)nearZ 26 | farZ:(float)farZ; 27 | 28 | - (void)transpose; 29 | 30 | - (void)multiplyLeft:(Matrix4 *)matrix; 31 | 32 | - (void)rotateAroundX:(float)xAngleRad 33 | y:(float)yAngleRad 34 | z:(float)zAngleRad; 35 | 36 | - (void)translate:(float)x 37 | y:(float)y 38 | z:(float)z; 39 | 40 | - (void)scale:(float)x 41 | y:(float)y 42 | z:(float)z; 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Shaders/UniformsBufferGenerator.m: -------------------------------------------------------------------------------- 1 | // 2 | // UniformsBufferGenerator.m 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/26/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import "UniformsBufferGenerator.h" 10 | #import "Cube_Transform-Swift.h" 11 | 12 | @implementation UniformsBufferGenerator 13 | 14 | + (id )generateUniformBufferProjectionMatrix:(Matrix4 *)projMatrix 15 | modelViewMatrix:(Matrix4 *)mvMatrix 16 | device:(id )device 17 | { 18 | float buffer[32]; 19 | for (int k = 0; k < 2; k++) 20 | { 21 | for (int i = 0; i < 16; i++) 22 | { 23 | if(k == 0) 24 | { 25 | buffer[16*k + i] = mvMatrix->glkMatrix.m[i]; 26 | } 27 | else 28 | { 29 | buffer[16*k + i] = projMatrix->glkMatrix.m[i]; 30 | } 31 | } 32 | } 33 | 34 | id uniformBuffer = [device newBufferWithBytes:buffer length:sizeof(buffer) options:0]; 35 | return uniformBuffer; 36 | } 37 | 38 | @end 39 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Nodes/Matrix4.h: -------------------------------------------------------------------------------- 1 | // 2 | // Matrix4.h 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/27/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | #import 12 | 13 | // Wraper around GLKMath's GLKMatrix4, becouse we can't use it directly from Swift code 14 | 15 | @interface Matrix4 : NSObject 16 | { 17 | @public 18 | GLKMatrix4 glkMatrix; 19 | } 20 | 21 | + (float)degreesToRad:(float)degrees; 22 | 23 | + (Matrix4 *)makePerspectiveViewAngle:(float)angleRad 24 | aspectRatio:(float)aspect 25 | nearZ:(float)nearZ 26 | farZ:(float)farZ; 27 | 28 | - (void)transpose; 29 | 30 | - (void)multiplyLeft:(Matrix4 *)matrix; 31 | 32 | - (void)rotateAroundX:(float)xAngleRad 33 | y:(float)yAngleRad 34 | z:(float)zAngleRad; 35 | 36 | - (void)translate:(float)x 37 | y:(float)y 38 | z:(float)z; 39 | 40 | - (void)scale:(float)x 41 | y:(float)y 42 | z:(float)z; 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Nodes/Ram/ram.mtl: -------------------------------------------------------------------------------- 1 | # Blender MTL File: 'ram_glsl.blend' 2 | # Material Count: 4 3 | 4 | newmtl ram_skin 5 | Ns -0.000015 6 | Ka 0.000000 0.000000 0.000000 7 | Kd 0.074341 0.056455 0.056455 8 | Ks 0.000000 0.000000 0.000000 9 | Ni 1.000000 10 | d 1.000000 11 | illum 1 12 | map_Bump textures/char_ram_nor.png 13 | map_Kd textures/char_ram_col.png 14 | 15 | newmtl ram_skin_eyelids 16 | Ns 260.784314 17 | Ka 0.000000 0.000000 0.000000 18 | Kd 0.129611 0.129611 0.129611 19 | Ks 0.500000 0.500000 0.500000 20 | Ni 1.000000 21 | d 1.000000 22 | illum 2 23 | map_Bump textures/char_ram_nor.png 24 | map_Kd textures/char_ram_col.png 25 | 26 | newmtl ram_skin_eyes 27 | Ns 372.549020 28 | Ka 0.000000 0.000000 0.000000 29 | Kd 0.200000 0.200000 0.200000 30 | Ks 0.500000 0.500000 0.500000 31 | Ni 1.000000 32 | d 1.000000 33 | illum 2 34 | map_Bump textures/char_ram_nor.png 35 | map_Kd textures/char_ram_col.png 36 | 37 | newmtl ram_skin_horns 38 | Ns 45.098039 39 | Ka 0.000000 0.000000 0.000000 40 | Kd 0.259223 0.259223 0.259223 41 | Ks 0.200000 0.200000 0.200000 42 | Ni 1.000000 43 | d 1.000000 44 | illum 2 45 | map_Bump textures/char_ram_nor.png 46 | map_Kd textures/char_ram_col.png 47 | map_Ns textures/char_ram_nor.png 48 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Shaders/UniformsBufferGenerator.h: -------------------------------------------------------------------------------- 1 | // 2 | // UniformsBufferGenerator.h 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/26/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | #import "Matrix4.h" 12 | 13 | @class Matrix4x4; 14 | 15 | 16 | /// This class is responsible for providing a uniformBuffer which will be passed to vertex shader. It holds n buffers. In case n == 3 for frame0 it will give buffer0 for frame1 - buffer1 for frame2 - buffer2 for frame3 - buffer0 and so on. It's user responsibility to make sure that GPU is not using that buffer before use. For details refer to wwdc session 604 (18:00). 17 | @interface UniformsBufferGenerator : NSObject 18 | 19 | @property(nonatomic,readonly) int indexOfAvaliableBuffer; 20 | @property(nonatomic,readonly) int numberOfInflightBuffers; 21 | 22 | - (instancetype)initWithNumberOfInflightBuffers:(int)inflightBuffersCount 23 | withDevice:(id )device; 24 | 25 | - (id )bufferWithProjectionMatrix:(Matrix4 *)projMatrix 26 | modelViewMatrix:(Matrix4 *)mvMatrix; 27 | 28 | @end 29 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.kharchyshyn.andrew.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UIMainStoryboardFile 26 | Storyboard 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Main/FrameBuffer.h: -------------------------------------------------------------------------------- 1 | // 2 | // FrameBufferHelper.h 3 | // Imported Model 4 | // 5 | // Created by Andrew K. on 7/3/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | #import 12 | 13 | @class MetalView; 14 | 15 | @interface FrameBuffer : NSObject 16 | 17 | 18 | @property (nonatomic, readonly) id device; 19 | @property (nonatomic, readonly) id currentDrawable; 20 | @property (nonatomic, readonly) MTLRenderPassDescriptor *renderPassDescriptor; 21 | 22 | @property (nonatomic) MTLPixelFormat depthPixelFormat; 23 | @property (nonatomic) MTLPixelFormat stencilPixelFormat; 24 | @property (nonatomic) NSUInteger sampleCount; 25 | 26 | // Change this value when layerSize changes (orientation change or when contens scale set) 27 | @property (nonatomic) BOOL layerSizeDidUpdate; 28 | 29 | // New drawable size must be reset before call display 30 | @property (nonatomic) CGSize drawableSize; 31 | 32 | 33 | - (instancetype)initWithMetalView:(MetalView *)metalView; 34 | - (void)releaseTextures; 35 | - (void)displayWithDrawableSize:(CGSize)drawableSize; 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Shaders/UniformsBufferGenerator.h: -------------------------------------------------------------------------------- 1 | // 2 | // UniformsBufferGenerator.h 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/26/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | #import 12 | #import "Matrix4.h" 13 | 14 | @class BaseEffect; 15 | 16 | /// This class is responsible for providing a uniformBuffer which will be passed to vertex shader. It holds n buffers. In case n == 3 for frame0 it will give buffer0 for frame1 - buffer1 for frame2 - buffer2 for frame3 - buffer0 and so on. It's user responsibility to make sure that GPU is not using that buffer before use. For details refer to wwdc session 604 (18:00). 17 | @interface UniformsBufferGenerator : NSObject 18 | 19 | @property(nonatomic,readonly) int indexOfAvaliableBuffer; 20 | @property(nonatomic,readonly) int numberOfInflightBuffers; 21 | 22 | - (instancetype)initWithNumberOfInflightBuffers:(int)inflightBuffersCount 23 | withDevice:(id )device; 24 | 25 | - (id )bufferWithProjectionMatrix:(Matrix4 *)projMatrix 26 | modelViewMatrix:(Matrix4 *)mvMatrix 27 | withBaseEffect:(BaseEffect *)baseEffect; 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Nodes/Square.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Square.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/27/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @objc class Square: Model { 12 | 13 | init(baseEffect: BaseEffect) 14 | { 15 | var A = Vertex(x: -1.0, y: 1.0, z: 0.0, r: 1.0, g: 0.0, b: 0.0, a: 1.0, u: 0.0, v: 1.0) 16 | var B = Vertex(x: -1.0, y: -1.0, z: 0.0, r: 0.0, g: 1.0, b: 0.0, a: 1.0, u: 0.0, v: 0.0) 17 | var C = Vertex(x: 1.0, y: -1.0, z: 0.0, r: 0.0, g: 0.0, b: 1.0, a: 1.0, u: 1.0, v: 0.0) 18 | var D = Vertex(x: 1.0, y: 1.0, z: 0.0, r: 0.1, g: 0.6, b: 0.4, a: 1.0, u: 1.0, v: 1.0) 19 | 20 | var verticesArray:Array = [A,B,C ,A,C,D] 21 | 22 | var mTexture:METLTexture = METLTexture(resourceName: "bricks", ext: "jpeg") 23 | mTexture.finalize(baseEffect.device) 24 | 25 | super.init(name: "Square", baseEffect: baseEffect, vertices: verticesArray, vertexCount: verticesArray.count, texture: mTexture.texture) 26 | } 27 | 28 | override func updateWithDelta(delta: CFTimeInterval) 29 | { 30 | super.updateWithDelta(delta) 31 | 32 | var secsPerMove: Float = 2.0 33 | positionX = sinf( Float(time) * 2.0 * Float(M_PI) / secsPerMove) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Nodes/TestScene.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TestScene.swift 3 | // Imported Model 4 | // 5 | // Created by Andrew K. on 7/4/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class TestScene: Scene { 12 | 13 | 14 | var sheeps: Array 15 | let numberOfSheeps = 9 16 | 17 | init(baseEffect: BaseEffect) 18 | { 19 | sheeps = Array() 20 | 21 | super.init(name: "TestScene", baseEffect: baseEffect) 22 | 23 | for var i = 0; i = [V0, V1, V2, V2, V3, V0] 21 | 22 | var mTexture:METLTexture = METLTexture(resourceName: "bricks", ext: "jpeg") 23 | mTexture.finalize(baseEffect.device) 24 | 25 | super.init(name: "Square", baseEffect: baseEffect, vertices: verticesArray, vertexCount: verticesArray.count, textureName: "bricks.jpeg") 26 | } 27 | 28 | override func updateWithDelta(delta: CFTimeInterval) 29 | { 30 | super.updateWithDelta(delta) 31 | 32 | var secsPerMove: Float = 2.0 33 | positionX = sinf( Float(time) * 2.0 * Float(M_PI) / secsPerMove) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Nodes/Square.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Square.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/27/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @objc class Square: Model { 12 | 13 | init(baseEffect: BaseEffect) 14 | { 15 | var V0 = Vertex(x: 1.0, y: -1.0, z: 1.0, u: 1.0 , v: 0.0 , nX: 0.0 , nY: 0.0 , nZ: 0.0) 16 | var V1 = Vertex(x: 1.0, y: 1.0, z: 1.0, u: 1.0 , v: 1.0 , nX: 0.0 , nY: 0.0 , nZ: 0.0) 17 | var V2 = Vertex(x: -1.0, y: 1.0, z: 1.0, u: 0.0 , v: 1.0 , nX: 0.0 , nY: 0.0 , nZ: 0.0) 18 | var V3 = Vertex(x: -1.0, y: -1.0, z: 1.0, u: 0.0 , v: 0.0 , nX: 0.0 , nY: 0.0 , nZ: 0.0) 19 | 20 | var verticesArray:Array = [V0, V1, V2, V2, V3, V0] 21 | 22 | var mTexture:METLTexture = METLTexture(resourceName: "bricks", ext: "jpeg") 23 | mTexture.finalize(baseEffect.device) 24 | 25 | super.init(name: "Square", baseEffect: baseEffect, vertices: verticesArray, vertexCount: verticesArray.count, texture: mTexture.texture) 26 | } 27 | 28 | override func updateWithDelta(delta: CFTimeInterval) 29 | { 30 | super.updateWithDelta(delta) 31 | 32 | var secsPerMove: Float = 2.0 33 | positionX = sinf( Float(time) * 2.0 * Float(M_PI) / secsPerMove) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Shaders/UniformsBufferGenerator.h: -------------------------------------------------------------------------------- 1 | // 2 | // UniformsBufferGenerator.h 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/26/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | #import 12 | #import "Matrix4.h" 13 | 14 | @class BaseEffect; 15 | @class Node; 16 | 17 | /// This class is responsible for providing a uniformBuffer which will be passed to vertex shader. It holds n buffers. In case n == 3 for frame0 it will give buffer0 for frame1 - buffer1 for frame2 - buffer2 for frame3 - buffer0 and so on. It's user responsibility to make sure that GPU is not using that buffer before use. For details refer to wwdc session 604 (18:00). 18 | @interface UniformsBufferGenerator : NSObject 19 | 20 | @property(nonatomic,readonly) int indexOfAvaliableBuffer; 21 | @property(nonatomic,readonly) int numberOfInflightBuffers; 22 | 23 | - (instancetype)initWithNumberOfInflightBuffers:(int)inflightBuffersCount 24 | withDevice:(id )device; 25 | 26 | - (id )bufferWithProjectionMatrix:(Matrix4 *)projMatrix 27 | modelViewMatrix:(Matrix4 *)mvMatrix 28 | withBaseEffect:(BaseEffect *)baseEffect 29 | withModel:(Node *)node; 30 | 31 | @end 32 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Main/MySceneViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MySceneViewController.swift 3 | // Imported Model 4 | // 5 | // Created by Andrew K. on 7/4/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class MySceneViewController: MetalViewController { 12 | 13 | var scene: TestScene! 14 | var baseEffect: BaseEffect! 15 | 16 | override func setupMetal() 17 | { 18 | super.setupMetal() 19 | 20 | baseEffect = BaseEffect(device: device, vertexShaderName: "myVertexShader", fragmentShaderName: "myFragmentShader") 21 | baseEffect.lightDirection = [0.0,1.0,-1.0] 22 | 23 | var ratio: Float = Float(self.view.bounds.size.width) / Float(self.view.bounds.size.height) 24 | baseEffect.projectionMatrix = Matrix4.makePerspectiveViewAngle(Matrix4.degreesToRad(85.0), aspectRatio: ratio, nearZ: 1.0, farZ: 150.0) 25 | 26 | scene = TestScene(baseEffect: baseEffect) 27 | renderPipeline = baseEffect.compile() 28 | } 29 | 30 | override func tearDownMetal() 31 | { 32 | scene = nil 33 | baseEffect = nil 34 | } 35 | 36 | override func update(elapsed: CFTimeInterval) 37 | { 38 | var matrix: Matrix4 = Matrix4() 39 | scene.render(commandQ, metalView: metalView, parentMVMatrix: matrix) 40 | scene.updateWithDelta(elapsed) 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Shaders/Vertex.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Vertex.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @objc class Vertex: NSObject 12 | { 13 | var x,y,z,u,v,nX,nY,nZ: Float 14 | 15 | 16 | init(x:Float, y:Float, z:Float, u:Float ,v:Float ,nX:Float ,nY:Float ,nZ:Float) 17 | { 18 | self.x = x 19 | self.y = y 20 | self.z = z 21 | 22 | self.u = u 23 | self.v = v 24 | 25 | self.nX = nX 26 | self.nY = nY 27 | self.nZ = nZ 28 | 29 | super.init() 30 | } 31 | 32 | init(text: String) 33 | { 34 | var list = text.componentsSeparatedByString(" ") 35 | 36 | var counter = 0 37 | 38 | self.x = list[counter++].bridgeToObjectiveC().floatValue 39 | self.y = list[counter++].bridgeToObjectiveC().floatValue 40 | self.z = list[counter++].bridgeToObjectiveC().floatValue 41 | 42 | self.u = list[counter++].bridgeToObjectiveC().floatValue 43 | self.v = list[counter++].bridgeToObjectiveC().floatValue 44 | 45 | self.nX = list[counter++].bridgeToObjectiveC().floatValue 46 | self.nY = list[counter++].bridgeToObjectiveC().floatValue 47 | self.nZ = list[counter++].bridgeToObjectiveC().floatValue 48 | 49 | super.init() 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Shaders/BaseEffect.swift: -------------------------------------------------------------------------------- 1 | // 2 | // BaseEffect.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import Metal 11 | 12 | @objc class BaseEffect: NSObject 13 | { 14 | var device:MTLDevice 15 | var renderPipelineState:MTLRenderPipelineState? 16 | var pipeLineDescriptor:MTLRenderPipelineDescriptor 17 | var projectionMatrix:AnyObject = Matrix4() 18 | 19 | init(device:MTLDevice ,vertexShaderName: String, fragmentShaderName:String) 20 | { 21 | self.device = device 22 | 23 | // Setup MTLRenderPipline descriptor object with vertex and fragment shader 24 | pipeLineDescriptor = MTLRenderPipelineDescriptor(); 25 | var library = device.newDefaultLibrary(); 26 | pipeLineDescriptor.vertexFunction = library.newFunctionWithName(vertexShaderName); 27 | pipeLineDescriptor.fragmentFunction = library.newFunctionWithName(fragmentShaderName); 28 | pipeLineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormat.BGRA8Unorm; 29 | 30 | super.init() 31 | } 32 | 33 | func compile() -> MTLRenderPipelineState? 34 | { 35 | // Compile the MTLRenderPipline object into immutable and cheap for use MTLRenderPipelineState 36 | renderPipelineState = device.newRenderPipelineStateWithDescriptor(pipeLineDescriptor, error: nil); 37 | return renderPipelineState 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Shaders/BaseEffect.swift: -------------------------------------------------------------------------------- 1 | // 2 | // BaseEffect.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import Metal 11 | 12 | @objc class BaseEffect: NSObject 13 | { 14 | var device:MTLDevice 15 | var renderPipelineState:MTLRenderPipelineState? 16 | var pipeLineDescriptor:MTLRenderPipelineDescriptor 17 | var projectionMatrix:AnyObject = Matrix4() 18 | 19 | init(device:MTLDevice ,vertexShaderName: String, fragmentShaderName:String) 20 | { 21 | self.device = device 22 | 23 | // Setup MTLRenderPipline descriptor object with vertex and fragment shader 24 | pipeLineDescriptor = MTLRenderPipelineDescriptor(); 25 | var library = device.newDefaultLibrary(); 26 | pipeLineDescriptor.vertexFunction = library.newFunctionWithName(vertexShaderName); 27 | pipeLineDescriptor.fragmentFunction = library.newFunctionWithName(fragmentShaderName); 28 | pipeLineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormat.BGRA8Unorm; 29 | 30 | super.init() 31 | } 32 | 33 | func compile() -> MTLRenderPipelineState? 34 | { 35 | // Compile the MTLRenderPipline object into immutable and cheap for use MTLRenderPipelineState 36 | renderPipelineState = device.newRenderPipelineStateWithDescriptor(pipeLineDescriptor, error: nil); 37 | return renderPipelineState 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-Objc/Shaders/VertexBufferGenerator.m: -------------------------------------------------------------------------------- 1 | // 2 | // VertexBufferGenerator.m 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import "VertexBufferGenerator.h" 10 | #import "Triangle-Swift.h" 11 | 12 | static const int kNumberOfPositionComponents = 4; 13 | static const int kNumberOfColorComponents = 4; 14 | 15 | 16 | @implementation VertexBufferGenerator 17 | 18 | + (id )generateBufferVertices:(NSArray *)vertices 19 | vertexCount:(NSNumber *)vertexCount 20 | device:(id )device 21 | { 22 | float vertexData[(kNumberOfPositionComponents + kNumberOfColorComponents)*vertexCount.intValue]; 23 | int counter = 0; 24 | 25 | for (int vertexID = 0; vertexID < vertexCount.integerValue; vertexID++) 26 | { 27 | Vertex *vertex = vertices[vertexID]; 28 | vertexData[counter++] = vertex.x; 29 | vertexData[counter++] = vertex.y; 30 | vertexData[counter++] = vertex.z; 31 | vertexData[counter++] = vertex.w; 32 | 33 | vertexData[counter++] = vertex.r; 34 | vertexData[counter++] = vertex.g; 35 | vertexData[counter++] = vertex.b; 36 | vertexData[counter++] = vertex.a; 37 | } 38 | 39 | id vertexBuffer = [device newBufferWithBytes:vertexData length:sizeof(vertexData) options:0]; 40 | 41 | return vertexBuffer; 42 | } 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-Objc/Shaders/BaseEffect.swift: -------------------------------------------------------------------------------- 1 | // 2 | // BaseEffect.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import Metal 11 | 12 | @objc class BaseEffect: NSObject 13 | { 14 | var device:MTLDevice 15 | var renderPipelineState:MTLRenderPipelineState? 16 | var pipeLineDescriptor:MTLRenderPipelineDescriptor 17 | 18 | init(device:MTLDevice ,vertexShaderName: String, fragmentShaderName:String) 19 | { 20 | self.device = device 21 | 22 | // Setup MTLRenderPipline descriptor object with vertex and fragment shader 23 | pipeLineDescriptor = MTLRenderPipelineDescriptor(); 24 | var library = device.newDefaultLibrary(); 25 | pipeLineDescriptor.vertexFunction = library.newFunctionWithName(vertexShaderName); 26 | pipeLineDescriptor.fragmentFunction = library.newFunctionWithName(fragmentShaderName); 27 | pipeLineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormat.BGRA8Unorm; 28 | // BaseEffectSfiftFixer.setup(pipeLineDescriptor) 29 | 30 | super.init() 31 | } 32 | 33 | func compile() -> MTLRenderPipelineState? 34 | { 35 | // Compile the MTLRenderPipline object into immutable and cheap for use MTLRenderPipelineState 36 | renderPipelineState = device.newRenderPipelineStateWithDescriptor(pipeLineDescriptor, error: nil); 37 | return renderPipelineState 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Shaders/VertexBufferGenerator.m: -------------------------------------------------------------------------------- 1 | // 2 | // VertexBufferGenerator.m 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import "VertexBufferGenerator.h" 10 | #import "Cube_Transform-Swift.h" 11 | 12 | static const int kNumberOfPositionComponents = 4; 13 | static const int kNumberOfColorComponents = 4; 14 | 15 | 16 | @implementation VertexBufferGenerator 17 | 18 | + (id )generateBufferVertices:(NSArray *)vertices 19 | vertexCount:(NSNumber *)vertexCount 20 | device:(id )device 21 | { 22 | float vertexData[(kNumberOfPositionComponents + kNumberOfColorComponents)*vertexCount.intValue]; 23 | int counter = 0; 24 | 25 | for (int vertexID = 0; vertexID < vertexCount.integerValue; vertexID++) 26 | { 27 | Vertex *vertex = vertices[vertexID]; 28 | vertexData[counter++] = vertex.x; 29 | vertexData[counter++] = vertex.y; 30 | vertexData[counter++] = vertex.z; 31 | vertexData[counter++] = vertex.w; 32 | 33 | vertexData[counter++] = vertex.r; 34 | vertexData[counter++] = vertex.g; 35 | vertexData[counter++] = vertex.b; 36 | vertexData[counter++] = vertex.a; 37 | } 38 | 39 | id vertexBuffer = [device newBufferWithBytes:vertexData length:sizeof(vertexData) options:0]; 40 | 41 | return vertexBuffer; 42 | } 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Nodes/Ram/Ram.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | @objc class Ram: Node { 4 | 5 | init(baseEffect: BaseEffect) 6 | { 7 | 8 | var verticesArray:Array = [] 9 | let path = NSBundle.mainBundle().pathForResource("ram", ofType: "txt") 10 | var possibleContent = String.stringWithContentsOfFile(path, encoding: NSUTF8StringEncoding, error: nil) 11 | 12 | if let content = possibleContent { 13 | var array = content.componentsSeparatedByString("\n") 14 | array.removeLast() 15 | for line in array{ 16 | var vertex = Vertex(text: line) 17 | verticesArray.append(vertex) 18 | } 19 | array.removeAll(keepCapacity: false) 20 | } 21 | 22 | 23 | super.init(name: "Ram", baseEffect: baseEffect, vertices: verticesArray, vertexCount: verticesArray.count, textureName: "char_ram_col.jpg") 24 | 25 | verticesArray.removeAll(keepCapacity: false) 26 | 27 | self.ambientIntensity = 0.400000 28 | self.diffuseIntensity = 0.8 29 | self.specularIntensity = 0.200000 30 | self.shininess = 45.098039 31 | 32 | self.rotationX = Matrix4.degreesToRad(-90.0) 33 | 34 | } 35 | 36 | override func updateWithDelta(delta: CFTimeInterval) 37 | { 38 | super.updateWithDelta(delta) 39 | 40 | // rotationZ += Float(M_PI/10) * Float(delta) 41 | rotationZ += Float(M_PI/8) * Float(delta) 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Shaders/VertexBufferGenerator.m: -------------------------------------------------------------------------------- 1 | // 2 | // VertexBufferGenerator.m 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import "VertexBufferGenerator.h" 10 | #import "Cube_Lighting-Swift.h" 11 | 12 | static const int kNumberOfPositionComponents = 4; 13 | static const int kNumberOfNormalComponents = 3; 14 | static const int kNumberOfTextureComponents = 2; 15 | 16 | 17 | @implementation VertexBufferGenerator 18 | 19 | + (id )generateBufferVertices:(NSArray *)vertices 20 | vertexCount:(NSNumber *)vertexCount 21 | device:(id )device 22 | { 23 | float vertexData[(kNumberOfPositionComponents + kNumberOfTextureComponents + kNumberOfNormalComponents)*vertexCount.intValue]; 24 | int counter = 0; 25 | 26 | for (int vertexID = 0; vertexID < vertexCount.integerValue; vertexID++) 27 | { 28 | Vertex *vertex = vertices[vertexID]; 29 | 30 | vertexData[counter++] = vertex.x; 31 | vertexData[counter++] = vertex.y; 32 | vertexData[counter++] = vertex.z; 33 | 34 | vertexData[counter++] = vertex.nX; 35 | vertexData[counter++] = vertex.nY; 36 | vertexData[counter++] = vertex.nZ; 37 | 38 | vertexData[counter++] = vertex.u; 39 | vertexData[counter++] = vertex.v; 40 | 41 | 42 | } 43 | 44 | id vertexBuffer = [device newBufferWithBytes:vertexData length:sizeof(vertexData) options:0]; 45 | 46 | return vertexBuffer; 47 | } 48 | 49 | @end 50 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Shaders/VertexBufferGenerator.m: -------------------------------------------------------------------------------- 1 | // 2 | // VertexBufferGenerator.m 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import "VertexBufferGenerator.h" 10 | #import "Imported_Model-Swift.h" 11 | 12 | static const int kNumberOfPositionComponents = 4; 13 | static const int kNumberOfNormalComponents = 3; 14 | static const int kNumberOfTextureComponents = 2; 15 | 16 | 17 | @implementation VertexBufferGenerator 18 | 19 | + (id )generateBufferVertices:(NSArray *)vertices 20 | vertexCount:(NSNumber *)vertexCount 21 | device:(id )device 22 | { 23 | float vertexData[(kNumberOfPositionComponents + kNumberOfTextureComponents + kNumberOfNormalComponents)*vertexCount.intValue]; 24 | int counter = 0; 25 | 26 | for (int vertexID = 0; vertexID < vertexCount.integerValue; vertexID++) 27 | { 28 | Vertex *vertex = vertices[vertexID]; 29 | 30 | vertexData[counter++] = vertex.x; 31 | vertexData[counter++] = vertex.y; 32 | vertexData[counter++] = vertex.z; 33 | 34 | vertexData[counter++] = vertex.nX; 35 | vertexData[counter++] = vertex.nY; 36 | vertexData[counter++] = vertex.nZ; 37 | 38 | vertexData[counter++] = vertex.u; 39 | vertexData[counter++] = vertex.v; 40 | 41 | 42 | } 43 | 44 | id vertexBuffer = [device newBufferWithBytes:vertexData length:sizeof(vertexData) options:0]; 45 | 46 | return vertexBuffer; 47 | } 48 | 49 | @end 50 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Shaders/VertexBufferGenerator.m: -------------------------------------------------------------------------------- 1 | // 2 | // VertexBufferGenerator.m 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import "VertexBufferGenerator.h" 10 | #import "Textured_Cube-Swift.h" 11 | 12 | static const int kNumberOfPositionComponents = 4; 13 | static const int kNumberOfColorComponents = 4; 14 | static const int kNumberOfTextureComponents = 2; 15 | 16 | 17 | @implementation VertexBufferGenerator 18 | 19 | + (id )generateBufferVertices:(NSArray *)vertices 20 | vertexCount:(NSNumber *)vertexCount 21 | device:(id )device 22 | { 23 | float vertexData[(kNumberOfPositionComponents + kNumberOfColorComponents + kNumberOfTextureComponents)*vertexCount.intValue]; 24 | int counter = 0; 25 | 26 | for (int vertexID = 0; vertexID < vertexCount.integerValue; vertexID++) 27 | { 28 | Vertex *vertex = vertices[vertexID]; 29 | 30 | vertexData[counter++] = vertex.r; 31 | vertexData[counter++] = vertex.g; 32 | vertexData[counter++] = vertex.b; 33 | vertexData[counter++] = vertex.a; 34 | 35 | vertexData[counter++] = vertex.x; 36 | vertexData[counter++] = vertex.y; 37 | vertexData[counter++] = vertex.z; 38 | 39 | vertexData[counter++] = vertex.u; 40 | vertexData[counter++] = vertex.v; 41 | } 42 | 43 | id vertexBuffer = [device newBufferWithBytes:vertexData length:sizeof(vertexData) options:0]; 44 | 45 | return vertexBuffer; 46 | } 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /1. Colored Triangle/Triangle.xcodeproj/project.xcworkspace/xcshareddata/Triangle.xccheckout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDESourceControlProjectFavoriteDictionaryKey 6 | 7 | IDESourceControlProjectIdentifier 8 | C7C4F90F-5B2B-4FE5-90E4-382F80829CE8 9 | IDESourceControlProjectName 10 | Triangle 11 | IDESourceControlProjectOriginsDictionary 12 | 13 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 14 | https://github.com/haawa799/METAL_TriangleObj-C.git 15 | 16 | IDESourceControlProjectPath 17 | 1. Colored Triangle/Triangle.xcodeproj/project.xcworkspace 18 | IDESourceControlProjectRelativeInstallPathDictionary 19 | 20 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 21 | ../../.. 22 | 23 | IDESourceControlProjectURL 24 | https://github.com/haawa799/METAL_TriangleObj-C.git 25 | IDESourceControlProjectVersion 26 | 110 27 | IDESourceControlProjectWCCIdentifier 28 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 29 | IDESourceControlProjectWCConfigurations 30 | 31 | 32 | IDESourceControlRepositoryExtensionIdentifierKey 33 | public.vcs.git 34 | IDESourceControlWCCIdentifierKey 35 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 36 | IDESourceControlWCCName 37 | MetalTryOut-Objc 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /3. Texured cube/Textured Cube.xcodeproj/project.xcworkspace/xcshareddata/Triangle.xccheckout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDESourceControlProjectFavoriteDictionaryKey 6 | 7 | IDESourceControlProjectIdentifier 8 | C7C4F90F-5B2B-4FE5-90E4-382F80829CE8 9 | IDESourceControlProjectName 10 | Triangle 11 | IDESourceControlProjectOriginsDictionary 12 | 13 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 14 | https://github.com/haawa799/METAL_TriangleObj-C.git 15 | 16 | IDESourceControlProjectPath 17 | 1. Colored Triangle/Triangle.xcodeproj/project.xcworkspace 18 | IDESourceControlProjectRelativeInstallPathDictionary 19 | 20 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 21 | ../../.. 22 | 23 | IDESourceControlProjectURL 24 | https://github.com/haawa799/METAL_TriangleObj-C.git 25 | IDESourceControlProjectVersion 26 | 110 27 | IDESourceControlProjectWCCIdentifier 28 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 29 | IDESourceControlProjectWCConfigurations 30 | 31 | 32 | IDESourceControlRepositoryExtensionIdentifierKey 33 | public.vcs.git 34 | IDESourceControlWCCIdentifierKey 35 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 36 | IDESourceControlWCCName 37 | MetalTryOut-Objc 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /2. Cube + Transform/Cube+Transform.xcodeproj/project.xcworkspace/xcshareddata/Triangle.xccheckout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDESourceControlProjectFavoriteDictionaryKey 6 | 7 | IDESourceControlProjectIdentifier 8 | C7C4F90F-5B2B-4FE5-90E4-382F80829CE8 9 | IDESourceControlProjectName 10 | Triangle 11 | IDESourceControlProjectOriginsDictionary 12 | 13 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 14 | https://github.com/haawa799/METAL_TriangleObj-C.git 15 | 16 | IDESourceControlProjectPath 17 | 1. Colored Triangle/Triangle.xcodeproj/project.xcworkspace 18 | IDESourceControlProjectRelativeInstallPathDictionary 19 | 20 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 21 | ../../.. 22 | 23 | IDESourceControlProjectURL 24 | https://github.com/haawa799/METAL_TriangleObj-C.git 25 | IDESourceControlProjectVersion 26 | 110 27 | IDESourceControlProjectWCCIdentifier 28 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 29 | IDESourceControlProjectWCConfigurations 30 | 31 | 32 | IDESourceControlRepositoryExtensionIdentifierKey 33 | public.vcs.git 34 | IDESourceControlWCCIdentifierKey 35 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 36 | IDESourceControlWCCName 37 | MetalTryOut-Objc 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /4. Cube + Lighting/Cube Lighting.xcodeproj/project.xcworkspace/xcshareddata/Triangle.xccheckout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDESourceControlProjectFavoriteDictionaryKey 6 | 7 | IDESourceControlProjectIdentifier 8 | C7C4F90F-5B2B-4FE5-90E4-382F80829CE8 9 | IDESourceControlProjectName 10 | Triangle 11 | IDESourceControlProjectOriginsDictionary 12 | 13 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 14 | https://github.com/haawa799/METAL_TriangleObj-C.git 15 | 16 | IDESourceControlProjectPath 17 | 1. Colored Triangle/Triangle.xcodeproj/project.xcworkspace 18 | IDESourceControlProjectRelativeInstallPathDictionary 19 | 20 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 21 | ../../.. 22 | 23 | IDESourceControlProjectURL 24 | https://github.com/haawa799/METAL_TriangleObj-C.git 25 | IDESourceControlProjectVersion 26 | 110 27 | IDESourceControlProjectWCCIdentifier 28 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 29 | IDESourceControlProjectWCConfigurations 30 | 31 | 32 | IDESourceControlRepositoryExtensionIdentifierKey 33 | public.vcs.git 34 | IDESourceControlWCCIdentifierKey 35 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 36 | IDESourceControlWCCName 37 | MetalTryOut-Objc 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /5. Imported Model/Imported Model.xcodeproj/project.xcworkspace/xcshareddata/Imported Model.xccheckout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDESourceControlProjectFavoriteDictionaryKey 6 | 7 | IDESourceControlProjectIdentifier 8 | 0A91637D-7EE0-4BD3-B2D4-0D807F6D18FE 9 | IDESourceControlProjectName 10 | Imported Model 11 | IDESourceControlProjectOriginsDictionary 12 | 13 | 9DA9A127-8D62-4014-A6ED-4F9B8A3D0749 14 | https://github.com/haawa799/METAL_Playground 15 | 16 | IDESourceControlProjectPath 17 | 5. Imported Model/Imported Model.xcodeproj/project.xcworkspace 18 | IDESourceControlProjectRelativeInstallPathDictionary 19 | 20 | 9DA9A127-8D62-4014-A6ED-4F9B8A3D0749 21 | ../../.. 22 | 23 | IDESourceControlProjectURL 24 | https://github.com/haawa799/METAL_Playground 25 | IDESourceControlProjectVersion 26 | 110 27 | IDESourceControlProjectWCCIdentifier 28 | 9DA9A127-8D62-4014-A6ED-4F9B8A3D0749 29 | IDESourceControlProjectWCConfigurations 30 | 31 | 32 | IDESourceControlRepositoryExtensionIdentifierKey 33 | public.vcs.git 34 | IDESourceControlWCCIdentifierKey 35 | 9DA9A127-8D62-4014-A6ED-4F9B8A3D0749 36 | IDESourceControlWCCName 37 | MetalTryOut-Objc 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /5. Imported Model/Imported Model.xcodeproj/project.xcworkspace/xcshareddata/Triangle.xccheckout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDESourceControlProjectFavoriteDictionaryKey 6 | 7 | IDESourceControlProjectIdentifier 8 | C7C4F90F-5B2B-4FE5-90E4-382F80829CE8 9 | IDESourceControlProjectName 10 | Triangle 11 | IDESourceControlProjectOriginsDictionary 12 | 13 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 14 | https://github.com/haawa799/METAL_TriangleObj-C.git 15 | 16 | IDESourceControlProjectPath 17 | 1. Colored Triangle/Triangle.xcodeproj/project.xcworkspace 18 | IDESourceControlProjectRelativeInstallPathDictionary 19 | 20 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 21 | ../../.. 22 | 23 | IDESourceControlProjectURL 24 | https://github.com/haawa799/METAL_TriangleObj-C.git 25 | IDESourceControlProjectVersion 26 | 110 27 | IDESourceControlProjectWCCIdentifier 28 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 29 | IDESourceControlProjectWCConfigurations 30 | 31 | 32 | IDESourceControlRepositoryExtensionIdentifierKey 33 | public.vcs.git 34 | IDESourceControlWCCIdentifierKey 35 | 5136E0EA-4DA9-4973-A2EA-32C0100986A5 36 | IDESourceControlWCCName 37 | MetalTryOut-Objc 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Nodes/Matrix4.m: -------------------------------------------------------------------------------- 1 | // 2 | // Matrix4.m 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/27/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import "Matrix4.h" 10 | 11 | @implementation Matrix4 12 | 13 | + (float)degreesToRad:(float)degrees 14 | { 15 | return GLKMathDegreesToRadians(degrees); 16 | } 17 | 18 | + (Matrix4 *)makePerspectiveViewAngle:(float)angleRad 19 | aspectRatio:(float)aspect 20 | nearZ:(float)nearZ 21 | farZ:(float)farZ 22 | { 23 | Matrix4 *matrix = [[Matrix4 alloc] init]; 24 | matrix->glkMatrix = GLKMatrix4MakePerspective(angleRad, aspect, nearZ, farZ); 25 | return matrix; 26 | } 27 | 28 | -(instancetype)init 29 | { 30 | self = [super init]; 31 | if(self != nil) 32 | { 33 | glkMatrix = GLKMatrix4Identity; 34 | } 35 | return self; 36 | } 37 | 38 | -(void)transpose 39 | { 40 | glkMatrix = GLKMatrix4Transpose(glkMatrix); 41 | } 42 | 43 | 44 | - (void)multiplyLeft:(Matrix4 *)matrix 45 | { 46 | glkMatrix = GLKMatrix4Multiply(matrix->glkMatrix, glkMatrix); 47 | } 48 | 49 | - (void)rotateAroundX:(float)xAngleRad y:(float)yAngleRad z:(float)zAngleRad 50 | { 51 | glkMatrix = GLKMatrix4Rotate(glkMatrix, xAngleRad, 1, 0, 0); 52 | glkMatrix = GLKMatrix4Rotate(glkMatrix, yAngleRad, 0, 1, 0); 53 | glkMatrix = GLKMatrix4Rotate(glkMatrix, zAngleRad, 0, 0, 1); 54 | } 55 | 56 | 57 | 58 | -(void)translate:(float)x y:(float)y z:(float)z 59 | { 60 | glkMatrix = GLKMatrix4Translate(glkMatrix, x, y, z); 61 | } 62 | 63 | -(void)scale:(float)x y:(float)y z:(float)z 64 | { 65 | glkMatrix = GLKMatrix4Scale(glkMatrix, x, y, z); 66 | } 67 | 68 | 69 | @end 70 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Nodes/Matrix4.m: -------------------------------------------------------------------------------- 1 | // 2 | // Matrix4.m 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/27/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import "Matrix4.h" 10 | 11 | @implementation Matrix4 12 | 13 | + (float)degreesToRad:(float)degrees 14 | { 15 | return GLKMathDegreesToRadians(degrees); 16 | } 17 | 18 | + (Matrix4 *)makePerspectiveViewAngle:(float)angleRad 19 | aspectRatio:(float)aspect 20 | nearZ:(float)nearZ 21 | farZ:(float)farZ 22 | { 23 | Matrix4 *matrix = [[Matrix4 alloc] init]; 24 | matrix->glkMatrix = GLKMatrix4MakePerspective(angleRad, aspect, nearZ, farZ); 25 | return matrix; 26 | } 27 | 28 | -(instancetype)init 29 | { 30 | self = [super init]; 31 | if(self != nil) 32 | { 33 | glkMatrix = GLKMatrix4Identity; 34 | } 35 | return self; 36 | } 37 | 38 | -(void)transpose 39 | { 40 | glkMatrix = GLKMatrix4Transpose(glkMatrix); 41 | } 42 | 43 | 44 | - (void)multiplyLeft:(Matrix4 *)matrix 45 | { 46 | glkMatrix = GLKMatrix4Multiply(matrix->glkMatrix, glkMatrix); 47 | } 48 | 49 | - (void)rotateAroundX:(float)xAngleRad y:(float)yAngleRad z:(float)zAngleRad 50 | { 51 | glkMatrix = GLKMatrix4Rotate(glkMatrix, xAngleRad, 1, 0, 0); 52 | glkMatrix = GLKMatrix4Rotate(glkMatrix, yAngleRad, 0, 1, 0); 53 | glkMatrix = GLKMatrix4Rotate(glkMatrix, zAngleRad, 0, 0, 1); 54 | } 55 | 56 | 57 | 58 | -(void)translate:(float)x y:(float)y z:(float)z 59 | { 60 | glkMatrix = GLKMatrix4Translate(glkMatrix, x, y, z); 61 | } 62 | 63 | -(void)scale:(float)x y:(float)y z:(float)z 64 | { 65 | glkMatrix = GLKMatrix4Scale(glkMatrix, x, y, z); 66 | } 67 | 68 | 69 | @end 70 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Nodes/Matrix4.m: -------------------------------------------------------------------------------- 1 | // 2 | // Matrix4.m 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/27/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import "Matrix4.h" 10 | 11 | @implementation Matrix4 12 | 13 | + (float)degreesToRad:(float)degrees 14 | { 15 | return GLKMathDegreesToRadians(degrees); 16 | } 17 | 18 | + (Matrix4 *)makePerspectiveViewAngle:(float)angleRad 19 | aspectRatio:(float)aspect 20 | nearZ:(float)nearZ 21 | farZ:(float)farZ 22 | { 23 | Matrix4 *matrix = [[Matrix4 alloc] init]; 24 | matrix->glkMatrix = GLKMatrix4MakePerspective(angleRad, aspect, nearZ, farZ); 25 | return matrix; 26 | } 27 | 28 | -(instancetype)init 29 | { 30 | self = [super init]; 31 | if(self != nil) 32 | { 33 | glkMatrix = GLKMatrix4Identity; 34 | } 35 | return self; 36 | } 37 | 38 | -(void)transpose 39 | { 40 | glkMatrix = GLKMatrix4Transpose(glkMatrix); 41 | } 42 | 43 | 44 | - (void)multiplyLeft:(Matrix4 *)matrix 45 | { 46 | glkMatrix = GLKMatrix4Multiply(matrix->glkMatrix, glkMatrix); 47 | } 48 | 49 | - (void)rotateAroundX:(float)xAngleRad y:(float)yAngleRad z:(float)zAngleRad 50 | { 51 | glkMatrix = GLKMatrix4Rotate(glkMatrix, xAngleRad, 1, 0, 0); 52 | glkMatrix = GLKMatrix4Rotate(glkMatrix, yAngleRad, 0, 1, 0); 53 | glkMatrix = GLKMatrix4Rotate(glkMatrix, zAngleRad, 0, 0, 1); 54 | } 55 | 56 | 57 | 58 | -(void)translate:(float)x y:(float)y z:(float)z 59 | { 60 | glkMatrix = GLKMatrix4Translate(glkMatrix, x, y, z); 61 | } 62 | 63 | -(void)scale:(float)x y:(float)y z:(float)z 64 | { 65 | glkMatrix = GLKMatrix4Scale(glkMatrix, x, y, z); 66 | } 67 | 68 | 69 | @end 70 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Nodes/Matrix4.m: -------------------------------------------------------------------------------- 1 | // 2 | // Matrix4.m 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/27/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import "Matrix4.h" 10 | 11 | @implementation Matrix4 12 | 13 | + (float)degreesToRad:(float)degrees 14 | { 15 | return GLKMathDegreesToRadians(degrees); 16 | } 17 | 18 | + (Matrix4 *)makePerspectiveViewAngle:(float)angleRad 19 | aspectRatio:(float)aspect 20 | nearZ:(float)nearZ 21 | farZ:(float)farZ 22 | { 23 | Matrix4 *matrix = [[Matrix4 alloc] init]; 24 | matrix->glkMatrix = GLKMatrix4MakePerspective(angleRad, aspect, nearZ, farZ); 25 | return matrix; 26 | } 27 | 28 | -(instancetype)init 29 | { 30 | self = [super init]; 31 | if(self != nil) 32 | { 33 | glkMatrix = GLKMatrix4Identity; 34 | } 35 | return self; 36 | } 37 | 38 | -(void)transpose 39 | { 40 | glkMatrix = GLKMatrix4Transpose(glkMatrix); 41 | } 42 | 43 | 44 | - (void)multiplyLeft:(Matrix4 *)matrix 45 | { 46 | glkMatrix = GLKMatrix4Multiply(matrix->glkMatrix, glkMatrix); 47 | } 48 | 49 | - (void)rotateAroundX:(float)xAngleRad y:(float)yAngleRad z:(float)zAngleRad 50 | { 51 | glkMatrix = GLKMatrix4Rotate(glkMatrix, xAngleRad, 1, 0, 0); 52 | glkMatrix = GLKMatrix4Rotate(glkMatrix, yAngleRad, 0, 1, 0); 53 | glkMatrix = GLKMatrix4Rotate(glkMatrix, zAngleRad, 0, 0, 1); 54 | } 55 | 56 | 57 | 58 | -(void)translate:(float)x y:(float)y z:(float)z 59 | { 60 | glkMatrix = GLKMatrix4Translate(glkMatrix, x, y, z); 61 | } 62 | 63 | -(void)scale:(float)x y:(float)y z:(float)z 64 | { 65 | glkMatrix = GLKMatrix4Scale(glkMatrix, x, y, z); 66 | } 67 | 68 | 69 | @end 70 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Nodes/Cube.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Cube.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/27/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @objc class Cube: Model { 12 | 13 | init(baseEffect: BaseEffect) 14 | { 15 | var A = Vertex(x: -1.0, y: 1.0, z: 1.0, w: 1.0, r: 1.0, g: 0.0, b: 0.0, a: 1.0) 16 | var B = Vertex(x: -1.0, y: -1.0, z: 1.0, w: 1.0, r: 0.0, g: 1.0, b: 0.0, a: 1.0) 17 | var C = Vertex(x: 1.0, y: -1.0, z: 1.0, w: 1.0, r: 0.0, g: 0.0, b: 1.0, a: 1.0) 18 | var D = Vertex(x: 1.0, y: 1.0, z: 1.0, w: 1.0, r: 0.1, g: 0.6, b: 0.4, a: 1.0) 19 | 20 | var Q = Vertex(x: -1.0, y: 1.0, z: -1.0, w: 1.0, r: 1.0, g: 0.0, b: 0.0, a: 1.0) 21 | var R = Vertex(x: 1.0, y: 1.0, z: -1.0, w: 1.0, r: 0.0, g: 1.0, b: 0.0, a: 1.0) 22 | var S = Vertex(x: -1.0, y: -1.0, z: -1.0, w: 1.0, r: 0.0, g: 0.0, b: 1.0, a: 1.0) 23 | var T = Vertex(x: 1.0, y: -1.0, z: -1.0, w: 1.0, r: 0.1, g: 0.6, b: 0.4, a: 1.0) 24 | 25 | var verticesArray:Array = [ 26 | A,B,C ,A,C,D, //Front 27 | R,T,S ,Q,R,S, //Back 28 | 29 | Q,S,B ,Q,B,A, //Left 30 | D,C,T ,D,T,R, //Right 31 | 32 | Q,A,D ,Q,D,R, //Top 33 | B,S,T ,B,T,C //Bot 34 | ] 35 | 36 | super.init(name: "Cube", baseEffect: baseEffect, vertices: verticesArray, vertexCount: verticesArray.count) 37 | } 38 | 39 | override func updateWithDelta(delta: CFTimeInterval) 40 | { 41 | super.updateWithDelta(delta) 42 | 43 | rotationZ += Float(M_PI) * Float(delta) 44 | rotationY += Float(M_PI) * Float(delta) 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Shaders/BaseEffect.swift: -------------------------------------------------------------------------------- 1 | // 2 | // BaseEffect.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import Metal 11 | 12 | @objc class BaseEffect: NSObject 13 | { 14 | var device:MTLDevice 15 | var renderPipelineState:MTLRenderPipelineState? 16 | var pipeLineDescriptor:MTLRenderPipelineDescriptor 17 | var projectionMatrix:AnyObject = Matrix4() 18 | 19 | var lightColor = MTLClearColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) 20 | var lightDirection: [Float] = [0.0,0.0,0.0] 21 | var diffuseIntensity: Float = 1.0 22 | var ambientIntensity: Float = 1.0 23 | var specularIntensity: Float = 1.0 24 | var shininess: Float = 1.0 25 | 26 | init(device:MTLDevice ,vertexShaderName: String, fragmentShaderName:String) 27 | { 28 | self.device = device 29 | 30 | // Setup MTLRenderPipline descriptor object with vertex and fragment shader 31 | pipeLineDescriptor = MTLRenderPipelineDescriptor(); 32 | var library = device.newDefaultLibrary(); 33 | pipeLineDescriptor.vertexFunction = library.newFunctionWithName(vertexShaderName); 34 | pipeLineDescriptor.fragmentFunction = library.newFunctionWithName(fragmentShaderName); 35 | pipeLineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormat.BGRA8Unorm; 36 | 37 | super.init() 38 | } 39 | 40 | func compile() -> MTLRenderPipelineState? 41 | { 42 | // Compile the MTLRenderPipline object into immutable and cheap for use MTLRenderPipelineState 43 | renderPipelineState = device.newRenderPipelineStateWithDescriptor(pipeLineDescriptor, error: nil); 44 | return renderPipelineState 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-Objc/Main/MetalViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MetalViewController.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | 10 | import UIKit 11 | import QuartzCore 12 | import Metal 13 | 14 | class MetalViewController: UIViewController,MetalViewProtocol { 15 | 16 | var metalView: MetalView! 17 | 18 | let device: MTLDevice = MTLCreateSystemDefaultDevice() 19 | var commandQ: MTLCommandQueue! 20 | var renderPipeline: MTLRenderPipelineState! 21 | 22 | var triangle:Triangle! 23 | var baseEffect: BaseEffect! 24 | 25 | deinit{ 26 | tearDownMetal() 27 | } 28 | 29 | override func viewDidLoad() { 30 | super.viewDidLoad() 31 | 32 | metalView = self.view as? MetalView 33 | metalView.metalViewDelegate = self 34 | setupMetal() 35 | } 36 | 37 | override func viewDidAppear(animated: Bool){ 38 | super.viewDidAppear(animated) 39 | metalView.resume() 40 | } 41 | 42 | override func viewDidDisappear(animated: Bool){ 43 | super.viewDidDisappear(animated) 44 | metalView.pause() 45 | } 46 | 47 | func setupMetal(){ 48 | commandQ = device.newCommandQueue() 49 | baseEffect = BaseEffect(device: device, vertexShaderName: "myVertexShader", fragmentShaderName: "myFragmentShader") 50 | triangle = Triangle(baseEffect: baseEffect!) 51 | renderPipeline = baseEffect.compile() 52 | } 53 | 54 | func tearDownMetal(){ 55 | commandQ = nil 56 | renderPipeline = nil 57 | triangle = nil 58 | baseEffect = nil 59 | } 60 | 61 | func update(delta: CFTimeInterval){ 62 | //Draw 63 | var metalLayer:CAMetalLayer? = self.view.layer as? CAMetalLayer 64 | 65 | if let mLayer = metalLayer 66 | { 67 | var drawable = mLayer.newDrawable() 68 | triangle.render(commandQ, drawable: drawable) 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Main/Storyboard.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-Objc/Main/Storyboard.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Main/Storyboard.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Main/Storyboard.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Main/MetalView.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2014 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | 7 | View for Metal Sample Code. Manages screen drawable framebuffers and expects a delegate to repond to render commands to perform drawing. 8 | 9 | */ 10 | 11 | #import 12 | #import 13 | #import 14 | 15 | @protocol MetalViewDelegate; 16 | 17 | @interface MetalView : UIView 18 | @property (nonatomic, weak) IBOutlet id delegate; 19 | 20 | // view has a handle to the metal device when created 21 | @property (nonatomic, readonly) id device; 22 | 23 | // the current drawable created within the view's CAMetalLayer 24 | @property (nonatomic, readonly) id currentDrawable; 25 | 26 | // the current drawable view size 27 | @property (nonatomic, readonly) CGSize drawableSize; 28 | 29 | // The current framebuffer can be read by delegate during -[MetalViewDelegate render:] 30 | // This call may block until the framebuffer is available. 31 | @property (nonatomic, readonly) MTLRenderPassDescriptor *renderPassDescriptor; 32 | 33 | // set these pixel formats to have the main drawable framebuffer get created with depth and/or stencil attachments 34 | @property (nonatomic) MTLPixelFormat depthPixelFormat; 35 | @property (nonatomic) MTLPixelFormat stencilPixelFormat; 36 | @property (nonatomic) NSUInteger sampleCount; 37 | 38 | // view controller will be call off the main thread 39 | - (void)display; 40 | 41 | // release any color/depth/stencil resources. view controller will call when paused. 42 | - (void)releaseTextures; 43 | 44 | @end 45 | 46 | // rendering delegate (App must implement a rendering delegate that responds to these messages 47 | @protocol MetalViewDelegate 48 | 49 | @required 50 | // called if the view changes orientation or size, renderer can precompute its view and projection matricies here for example 51 | - (void)reshape:(MetalView *)view; 52 | 53 | // delegate should perform all rendering here 54 | - (void)render:(MetalView *)view; 55 | 56 | @end 57 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Main/Storyboard.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Shaders/Shaders.metal: -------------------------------------------------------------------------------- 1 | // 2 | // Shaders.metal 3 | // MetalTryOut-Objc 4 | // 5 | // Created by Andrew K. on 6/22/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace metal; 11 | 12 | struct Uniforms{ 13 | float4 modelViewMatrixColumns[4]; 14 | float4 projectionMatrixColumns[4]; 15 | }; 16 | 17 | struct Vertex{ 18 | float4 position; 19 | float4 color; 20 | }; 21 | 22 | struct VertexOut 23 | { 24 | float4 position [[position]]; 25 | float4 color; 26 | }; 27 | 28 | float4x4 mv_MatrixFromUniformBuffer(constant Uniforms& uniformMatrix); 29 | float4x4 proj_MatrixFromUniformBuffer(constant Uniforms& uniformMatrix); 30 | 31 | 32 | float4x4 mv_MatrixFromUniformBuffer(constant Uniforms& uniformMatrix) 33 | { 34 | float4x4 matrix; 35 | for (int i = 0; i < 4; i++) 36 | { 37 | for (int j = 0; j < 4; j++) 38 | { 39 | matrix[j][i] = uniformMatrix.modelViewMatrixColumns[j][i]; 40 | } 41 | } 42 | return matrix; 43 | } 44 | 45 | float4x4 proj_MatrixFromUniformBuffer(constant Uniforms& uniformMatrix) 46 | { 47 | float4x4 matrix; 48 | for (int i = 0; i < 4; i++) 49 | { 50 | for (int j = 0; j < 4; j++) 51 | { 52 | matrix[j][i] = uniformMatrix.projectionMatrixColumns[j][i]; 53 | } 54 | } 55 | return matrix; 56 | } 57 | 58 | vertex VertexOut myVertexShader(const device Vertex* vertexArray [[buffer(0)]], 59 | constant Uniforms& uniforms [[buffer(1)]], 60 | unsigned int vid [[vertex_id]]) 61 | { 62 | float4x4 mv_Matrix = mv_MatrixFromUniformBuffer(uniforms); 63 | float4x4 proj_Matrix = proj_MatrixFromUniformBuffer(uniforms); 64 | float4 position = vertexArray[vid].position; 65 | 66 | VertexOut out; 67 | out.position = proj_Matrix * mv_Matrix * position; 68 | out.color = vertexArray[vid].color; 69 | return out; 70 | } 71 | 72 | 73 | fragment float4 myFragmentShader(VertexOut interpolated [[stage_in]]) 74 | { 75 | return interpolated.color; 76 | } 77 | 78 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Shaders/BaseEffect.swift: -------------------------------------------------------------------------------- 1 | // 2 | // BaseEffect.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import Metal 11 | 12 | @objc class BaseEffect: NSObject 13 | { 14 | var device:MTLDevice 15 | var renderPipelineState:MTLRenderPipelineState? 16 | var pipeLineDescriptor:MTLRenderPipelineDescriptor 17 | var projectionMatrix:AnyObject = Matrix4() 18 | 19 | var lightColor = MTLClearColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) 20 | var lightDirection: [Float] = [0.0,0.0,0.0] 21 | 22 | init(device:MTLDevice ,vertexShaderName: String, fragmentShaderName:String) 23 | { 24 | self.device = device 25 | 26 | // Setup MTLRenderPipline descriptor object with vertex and fragment shader 27 | pipeLineDescriptor = MTLRenderPipelineDescriptor() 28 | var library = device.newDefaultLibrary() 29 | pipeLineDescriptor.vertexFunction = library.newFunctionWithName(vertexShaderName) 30 | pipeLineDescriptor.fragmentFunction = library.newFunctionWithName(fragmentShaderName) 31 | pipeLineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormat.BGRA8Unorm 32 | pipeLineDescriptor.depthWriteEnabled = true 33 | pipeLineDescriptor.sampleCount = 4 34 | pipeLineDescriptor.depthAttachmentPixelFormat = MTLPixelFormat.Depth32Float 35 | pipeLineDescriptor.visibilityResultEnabled = true 36 | 37 | // var colorDescriptor = MTLRenderPipelineAttachmentDescriptor() 38 | // colorDescriptor.pixelFormat = .FormatBGRA8Unorm 39 | // 40 | // var depthDescriptor = MTLRenderPipelineAttachmentDescriptor() 41 | // depthDescriptor.pixelFormat = .FormatBGRA8Unorm 42 | 43 | // pipeLineDescriptor.colorAttachments[0] = colorDescriptor 44 | 45 | super.init() 46 | } 47 | 48 | func compile() -> MTLRenderPipelineState? 49 | { 50 | // Compile the MTLRenderPipline object into immutable and cheap for use MTLRenderPipelineState 51 | renderPipelineState = device.newRenderPipelineStateWithDescriptor(pipeLineDescriptor, error: nil) 52 | return renderPipelineState 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Main/MetalViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MetalViewController.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | 10 | import UIKit 11 | import QuartzCore 12 | import Metal 13 | 14 | class MetalViewController: UIViewController,MetalViewProtocol { 15 | 16 | var metalView: MetalView! 17 | 18 | let device: MTLDevice = MTLCreateSystemDefaultDevice() 19 | var commandQ: MTLCommandQueue! 20 | var renderPipeline: MTLRenderPipelineState! 21 | 22 | var cube: Cube! 23 | var baseEffect: BaseEffect! 24 | 25 | deinit{ 26 | tearDownMetal() 27 | } 28 | 29 | override func viewDidLoad() { 30 | super.viewDidLoad() 31 | 32 | var font = UIApplication.sharedApplication() 33 | 34 | metalView = self.view as? MetalView 35 | metalView.metalViewDelegate = self 36 | setupMetal() 37 | } 38 | 39 | override func viewDidAppear(animated: Bool){ 40 | super.viewDidAppear(animated) 41 | metalView.resume() 42 | } 43 | 44 | override func viewDidDisappear(animated: Bool){ 45 | super.viewDidDisappear(animated) 46 | metalView.pause() 47 | } 48 | 49 | func setupMetal(){ 50 | commandQ = device.newCommandQueue() 51 | baseEffect = BaseEffect(device: device, vertexShaderName: "myVertexShader", fragmentShaderName: "myFragmentShader") 52 | var ratio: Float = Float(self.view.bounds.size.width) / Float(self.view.bounds.size.height) 53 | baseEffect.projectionMatrix = Matrix4.makePerspectiveViewAngle(Matrix4.degreesToRad(85.0), aspectRatio: ratio, nearZ: 1.0, farZ: 150.0) 54 | 55 | cube = Cube(baseEffect: baseEffect) 56 | renderPipeline = baseEffect.compile() 57 | } 58 | 59 | func tearDownMetal(){ 60 | commandQ = nil 61 | renderPipeline = nil 62 | cube = nil 63 | baseEffect = nil 64 | } 65 | 66 | func update(delta: CFTimeInterval){ 67 | //Draw 68 | var metalLayer:CAMetalLayer? = self.view.layer as? CAMetalLayer 69 | 70 | if let mLayer = metalLayer 71 | { 72 | var drawable = mLayer.newDrawable() 73 | var matrix: Matrix4 = Matrix4() 74 | matrix.translate(0, y: 0, z: -5) 75 | cube.render(commandQ, drawable: drawable, parentMVMatrix: matrix) 76 | } 77 | 78 | cube.updateWithDelta(delta) 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Main/MetalViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MetalViewController.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | 10 | import UIKit 11 | import QuartzCore 12 | import Metal 13 | 14 | class MetalViewController: UIViewController,MetalViewProtocol { 15 | 16 | var metalView: MetalView! 17 | 18 | let device: MTLDevice = MTLCreateSystemDefaultDevice() 19 | var commandQ: MTLCommandQueue! 20 | var renderPipeline: MTLRenderPipelineState! 21 | 22 | var cube: Cube! 23 | var baseEffect: BaseEffect! 24 | 25 | deinit{ 26 | tearDownMetal() 27 | } 28 | 29 | override func viewDidLoad() { 30 | super.viewDidLoad() 31 | 32 | var font = UIApplication.sharedApplication() 33 | 34 | metalView = self.view as? MetalView 35 | metalView.metalViewDelegate = self 36 | setupMetal() 37 | } 38 | 39 | override func viewDidAppear(animated: Bool){ 40 | super.viewDidAppear(animated) 41 | metalView.resume() 42 | } 43 | 44 | override func viewDidDisappear(animated: Bool){ 45 | super.viewDidDisappear(animated) 46 | metalView.pause() 47 | } 48 | 49 | func setupMetal(){ 50 | commandQ = device.newCommandQueue() 51 | baseEffect = BaseEffect(device: device, vertexShaderName: "myVertexShader", fragmentShaderName: "myFragmentShader") 52 | var ratio: Float = Float(self.view.bounds.size.width) / Float(self.view.bounds.size.height) 53 | baseEffect.projectionMatrix = Matrix4.makePerspectiveViewAngle(Matrix4.degreesToRad(85.0), aspectRatio: ratio, nearZ: 1.0, farZ: 150.0) 54 | 55 | cube = Cube(baseEffect: baseEffect) 56 | renderPipeline = baseEffect.compile() 57 | } 58 | 59 | func tearDownMetal(){ 60 | commandQ = nil 61 | renderPipeline = nil 62 | cube = nil 63 | baseEffect = nil 64 | } 65 | 66 | func update(delta: CFTimeInterval){ 67 | //Draw 68 | var metalLayer:CAMetalLayer? = self.view.layer as? CAMetalLayer 69 | 70 | if let mLayer = metalLayer 71 | { 72 | var drawable = mLayer.newDrawable() 73 | var matrix: Matrix4 = Matrix4() 74 | matrix.translate(0, y: 0, z: -5) 75 | cube.render(commandQ, drawable: drawable, parentMVMatrix: matrix) 76 | } 77 | 78 | cube.updateWithDelta(delta) 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Main/MetalViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MetalViewController.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | 10 | import UIKit 11 | import QuartzCore 12 | import Metal 13 | 14 | class MetalViewController: UIViewController { 15 | 16 | init(coder aDecoder: NSCoder!) 17 | { 18 | super.init(coder: aDecoder) 19 | } 20 | 21 | init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) 22 | { 23 | super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) 24 | } 25 | 26 | var metalView: MetalView! 27 | 28 | let device: MTLDevice = MTLCreateSystemDefaultDevice() 29 | var commandQ: MTLCommandQueue! 30 | var renderPipeline: MTLRenderPipelineState! 31 | 32 | var fpsLabel: UILabel! 33 | 34 | 35 | //Private 36 | var _displayLink: CADisplayLink! 37 | var _lastFrameTimestamp: CFTimeInterval! 38 | var _gameLoopPaused: Bool! 39 | 40 | 41 | deinit{ 42 | tearDownMetal() 43 | } 44 | 45 | override func viewDidLoad() { 46 | super.viewDidLoad() 47 | 48 | metalView = self.view as? MetalView 49 | setupMetal() 50 | } 51 | 52 | override func viewDidAppear(animated: Bool){ 53 | super.viewDidAppear(animated) 54 | } 55 | 56 | override func viewDidDisappear(animated: Bool){ 57 | super.viewDidDisappear(animated) 58 | } 59 | 60 | func setupMetal(){ 61 | 62 | commandQ = device.newCommandQueue() 63 | 64 | _displayLink = CADisplayLink(target: self, selector: Selector.convertFromStringLiteral("newFrame:")) 65 | _displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes) 66 | } 67 | 68 | func tearDownMetal(){ 69 | commandQ = nil 70 | renderPipeline = nil 71 | } 72 | 73 | 74 | func newFrame(displayLink: CADisplayLink){ 75 | 76 | if _lastFrameTimestamp == nil 77 | { 78 | _lastFrameTimestamp = displayLink.timestamp 79 | } 80 | 81 | var elapsed:CFTimeInterval = displayLink.timestamp - _lastFrameTimestamp 82 | metalView.fpsLabel!.text = "fps: \(Int(1.0/elapsed))" 83 | _lastFrameTimestamp = displayLink.timestamp 84 | metalView.display() 85 | 86 | update(elapsed) 87 | } 88 | 89 | func update(elapsed: CFTimeInterval) 90 | { 91 | 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Shaders/Shaders.metal: -------------------------------------------------------------------------------- 1 | // 2 | // Shaders.metal 3 | // MetalTryOut-Objc 4 | // 5 | // Created by Andrew K. on 6/22/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace metal; 11 | 12 | struct Uniforms{ 13 | float4 modelViewMatrixColumns[4]; 14 | float4 projectionMatrixColumns[4]; 15 | }; 16 | 17 | struct Vertex{ 18 | packed_float4 color; 19 | packed_float3 position; 20 | packed_float2 texCoord; 21 | }; 22 | 23 | struct VertexOut 24 | { 25 | float4 position [[position]]; 26 | float4 color; 27 | float2 texCoord [[user(texturecoord)]];; 28 | }; 29 | 30 | float4x4 mv_MatrixFromUniformBuffer(constant Uniforms& uniformMatrix); 31 | float4x4 proj_MatrixFromUniformBuffer(constant Uniforms& uniformMatrix); 32 | 33 | float4x4 mv_MatrixFromUniformBuffer(constant Uniforms& uniformMatrix) 34 | { 35 | float4x4 matrix; 36 | for (int i = 0; i < 4; i++) 37 | { 38 | for (int j = 0; j < 4; j++) 39 | { 40 | matrix[j][i] = uniformMatrix.modelViewMatrixColumns[j][i]; 41 | } 42 | } 43 | return matrix; 44 | } 45 | 46 | float4x4 proj_MatrixFromUniformBuffer(constant Uniforms& uniformMatrix) 47 | { 48 | float4x4 matrix; 49 | for (int i = 0; i < 4; i++) 50 | { 51 | for (int j = 0; j < 4; j++) 52 | { 53 | matrix[j][i] = uniformMatrix.projectionMatrixColumns[j][i]; 54 | } 55 | } 56 | return matrix; 57 | } 58 | 59 | vertex VertexOut myVertexShader(const device Vertex* vertexArray [[buffer(0)]], 60 | constant Uniforms& uniforms [[buffer(1)]], 61 | unsigned int vid [[vertex_id]]) 62 | { 63 | float4x4 mv_Matrix = mv_MatrixFromUniformBuffer(uniforms); 64 | float4x4 proj_Matrix = proj_MatrixFromUniformBuffer(uniforms); 65 | float4 position = {vertexArray[vid].position[0],vertexArray[vid].position[1],vertexArray[vid].position[2],1.0}; 66 | 67 | VertexOut out; 68 | out.position = proj_Matrix * mv_Matrix * position; 69 | out.color = vertexArray[vid].color; 70 | out.texCoord = vertexArray[vid].texCoord; 71 | return out; 72 | } 73 | 74 | 75 | fragment float4 myFragmentShader(VertexOut interpolated [[stage_in]], 76 | texture2d tex2D [[ texture(0) ]], 77 | sampler sampler2D [[ sampler(0) ]]) 78 | { 79 | return /*interpolated.color */ tex2D.sample(sampler2D, interpolated.texCoord); 80 | } 81 | 82 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Shaders/UniformsBufferGenerator.m: -------------------------------------------------------------------------------- 1 | // 2 | // UniformsBufferGenerator.m 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/26/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import "UniformsBufferGenerator.h" 10 | #import "Textured_Cube-Swift.h" 11 | 12 | static const long kMaxBufferBytesPerFrame = 1024*1024; 13 | static const int kFloatsPerMatrix4 = 16; 14 | 15 | @interface UniformsBufferGenerator() 16 | 17 | @property(nonatomic,readwrite) int indexOfAvaliableBuffer; 18 | @property(nonatomic,readwrite) int numberOfInflightBuffers; 19 | 20 | @property(nonatomic,strong) NSArray *buffers; 21 | 22 | @end 23 | 24 | @implementation UniformsBufferGenerator 25 | 26 | 27 | - (instancetype)initWithNumberOfInflightBuffers:(int)inflightBuffersCount 28 | withDevice:(id )device 29 | { 30 | self = [super init]; 31 | if(self != nil) 32 | { 33 | self.numberOfInflightBuffers = inflightBuffersCount; 34 | self.indexOfAvaliableBuffer = 0; 35 | 36 | NSMutableArray *mBuffers = [[NSMutableArray alloc] initWithCapacity:inflightBuffersCount]; 37 | for (int i = 0; i < inflightBuffersCount; i++) 38 | { 39 | id buffer = [device newBufferWithLength:kMaxBufferBytesPerFrame options:0]; 40 | buffer.label = [NSString stringWithFormat:@"Uniform buffer #%@",@(i)]; 41 | [mBuffers addObject:buffer]; 42 | } 43 | self.buffers = mBuffers; 44 | } 45 | return self; 46 | } 47 | 48 | - (id )bufferWithProjectionMatrix:(Matrix4 *)projMatrix 49 | modelViewMatrix:(Matrix4 *)mvMatrix 50 | { 51 | 52 | 53 | float uniformFloatsBuffer[kFloatsPerMatrix4 * 2]; 54 | for (int k = 0; k < 2; k++) 55 | { 56 | for (int i = 0; i < 16; i++) 57 | { 58 | if(k == 0) 59 | { 60 | uniformFloatsBuffer[16*k + i] = mvMatrix->glkMatrix.m[i]; 61 | } 62 | else 63 | { 64 | uniformFloatsBuffer[16*k + i] = projMatrix->glkMatrix.m[i]; 65 | } 66 | } 67 | } 68 | 69 | id uniformBuffer = self.buffers[self.indexOfAvaliableBuffer++]; 70 | if(self.indexOfAvaliableBuffer == self.numberOfInflightBuffers) 71 | { 72 | self.indexOfAvaliableBuffer = 0; 73 | } 74 | uint8_t *bufferPointer = (uint8_t *)[uniformBuffer contents]; 75 | 76 | // 1st box 77 | memcpy(bufferPointer, &uniformFloatsBuffer, sizeof(uniformFloatsBuffer)); 78 | bufferPointer += sizeof(uniformFloatsBuffer); 79 | 80 | return uniformBuffer; 81 | } 82 | 83 | @end 84 | -------------------------------------------------------------------------------- /1. Colored Triangle/Triangle.xcodeproj/xcuserdata/haawa799.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 8 | 14 | 15 | 16 | 18 | 30 | 31 | 32 | 34 | 46 | 47 | 48 | 50 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-Objc/Nodes/Model.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Model.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import Metal 11 | import QuartzCore 12 | 13 | @objc class Model: NSObject 14 | { 15 | var baseEffect: BaseEffect 16 | let name: String 17 | var vertexCount: Int 18 | 19 | var vertexBuffer: MTLBuffer? 20 | 21 | init(name: String, 22 | baseEffect: BaseEffect, 23 | vertices: Array, 24 | vertexCount: Int) 25 | { 26 | self.name = name 27 | self.baseEffect = baseEffect 28 | self.vertexCount = vertexCount 29 | 30 | super.init() 31 | 32 | self.vertexBuffer = generateVertexBuffer(vertices, vertexCount: vertexCount, device: baseEffect.device) 33 | } 34 | 35 | func render(commandQueue: MTLCommandQueue, drawable: CAMetalDrawable) 36 | { 37 | var commandBuffer = commandQueue.commandBuffer() 38 | 39 | 40 | // MTLRenderPassDescriptor object represents a collection of configurable states 41 | var renderPassDesc:MTLRenderPassDescriptor = MTLRenderPassDescriptor() 42 | renderPassDesc.colorAttachments[0].texture = drawable.texture 43 | renderPassDesc.colorAttachments[0].loadAction = MTLLoadAction.Clear 44 | // renderPassDesc.colorAttachments[0].clearValue = MTLClearColor(red: 0.0, green: 104.0/255, blue: 55.0/255, alpha: 1) 45 | 46 | // Create MTLRenderCommandEncoder object which translates all states into a command for GPU 47 | var commandEncoder:MTLRenderCommandEncoder = commandBuffer.renderCommandEncoderWithDescriptor(renderPassDesc) 48 | commandEncoder.setRenderPipelineState(baseEffect.renderPipelineState) 49 | commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, atIndex: 0) 50 | commandEncoder.drawPrimitives(MTLPrimitiveType.Triangle, vertexStart: 0, vertexCount: vertexCount) 51 | commandEncoder.endEncoding() 52 | 53 | // After command in command buffer is encoded for GPU we provide drawable that will be invoked when this command buffer has been scheduled for execution 54 | commandBuffer.presentDrawable(drawable) 55 | 56 | // Commit commandBuffer to his commandQueue in which he will be executed after commands before him in queue 57 | commandBuffer.commit() 58 | 59 | } 60 | 61 | func generateVertexBuffer(vertices: Array, vertexCount: Int, device: MTLDevice) -> MTLBuffer? 62 | { 63 | vertexBuffer = VertexBufferGenerator.generateBufferVertices(vertices, vertexCount: vertexCount, device: device) 64 | return vertexBuffer 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | UPDATE 2 | =================== 3 | After finishing this try-out project I made Flappy bird colne with Metal, go check it out https://github.com/haawa799/Metal-Flaps 4 | 5 | 6 | METAL_Playground 7 | =================== 8 | Task: Create complex 3D scene app using Apples new Metal framework, using Swift as much as possible. 9 | 10 | NOTE1: Since I'm trying to use Swift where it's posiible, this code is not super efficient or fast, there are some wrappers which are not recommanded in this kind of projects. 11 | 12 | Hope to improve it as new Xcode betas will come out. 13 | 14 | NOTE: 15 | 16 | 1) For now (Xcode 6 - beta2) requires -Ofast compiler flag to build. 17 | 18 | 2) iOS 8 and device with A7 required! Doesn't work on simulator. 19 | 20 | ![alt tag](http://cl.ly/image/2h2p3x2r0f2E/triangle.png) ![alt tag](http://cl.ly/image/400Y0C3G2c0o/transform.png) ![alt tag](http://cl.ly/image/2u2v1W3H3T3b/texture.png) ![alt tag](http://cl.ly/image/0B153R3g2M21/lighting.png) ![alt tag](http://cl.ly/image/0g2K022n220m/ram.png) 21 | 22 | 23 | Model importing script Obj_Convert.py 24 | =================== 25 | 26 | In part 5 i wrote simple python script which parse OBJ file (generated from Blender or any other 3D model tool) and produce Swift class and txt file. 27 | 28 | Here are steps on how I used it to get that Ram rendered. Here's a link to free 3D model http://www.3dvia.com/models/67EC495D6F415365/ramiro-the-ram, thanks to dipsy. 29 | 30 | 1. Put your OBJ and MTL file (generated from Blender) into one folder 31 | ![alt tag](http://cl.ly/image/012R0p303g2J/Screen%20Shot%202014-07-04%20at%2012.14.22%20AM.png) 32 | 33 | 2. Make sure that both OBJ and MTL files have same name, and this name will be your Swift class name, in my case it's "ram.obj" and "ram.mtl" 34 | 35 | 3. Put Obj_Convert.py file into same folder 36 | ![alt tag](http://cl.ly/image/0s2A0G1P2G2X/Screen%20Shot%202014-07-04%20at%2012.14.57%20AM.png) 37 | 38 | 4. From terminal cd to your folder 39 | ![alt tag](http://cl.ly/image/00312g2F2Q2y/Screen%20Shot%202014-07-04%20at%2012.16.01%20AM.png) 40 | 41 | 5. Run following command "python Obj_Convert.py name" name should be equal to .obj and .mtl file names in my case it's "ram" 42 | ![alt tag](http://cl.ly/image/010Y1q410g2E/Screen%20Shot%202014-07-04%20at%2012.16.41%20AM.png) 43 | 44 | 6. Check you folder, .swift and .txt files should be there, drag and drop them into your project 45 | ![alt tag](http://cl.ly/image/410j0F330G22/Screen%20Shot%202014-07-04%20at%2012.19.00%20AM.png) 46 | ![alt tag](http://cl.ly/image/2Z3a1B3w3v3c/Screen%20Shot%202014-07-04%20at%2012.20.55%20AM.png) 47 | 48 | 7. Double check .swift file, wethere texture name is correct 49 | ![alt tag](http://cl.ly/image/3N2u1l3k1f0w/Screen%20Shot%202014-07-04%20at%2012.57.21%20AM.png) 50 | 51 | 8. Use imported model as any other Model subclass inside your MetalViewController 52 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Nodes/Scene.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Scene.swift 3 | // Imported Model 4 | // 5 | // Created by Andrew K. on 7/4/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class Scene: Node { 12 | 13 | init(name: String, baseEffect: BaseEffect) 14 | { 15 | super.init(name: name, baseEffect: baseEffect, vertices: nil, vertexCount: 0, textureName: nil) 16 | } 17 | 18 | func render(commandQueue: MTLCommandQueue, metalView: MetalView, parentMVMatrix: AnyObject) 19 | { 20 | 21 | var parentModelViewMatrix: Matrix4 = parentMVMatrix as Matrix4 22 | var myModelViewMatrix: Matrix4 = modelMatrix() as Matrix4 23 | myModelViewMatrix.multiplyLeft(parentModelViewMatrix) 24 | var projectionMatrix: Matrix4 = baseEffect.projectionMatrix as Matrix4 25 | self.uniformsBuffer = getUniformsBuffer(myModelViewMatrix, projMatrix: projectionMatrix, baseEffect: baseEffect) 26 | 27 | 28 | //We are using 3 uniform buffers, we need to wait in case CPU wants to write in first uniform buffer, while GPU is still using it (case when GPU is 2 frames ahead CPU) 29 | dispatch_semaphore_wait(avaliableUniformBuffers, DISPATCH_TIME_FOREVER) 30 | 31 | 32 | var renderPathDescriptor = metalView.frameBuffer.renderPassDescriptor 33 | var commandBuffer = commandQueue.commandBuffer() 34 | commandBuffer.addCompletedHandler( 35 | { 36 | (buffer:MTLCommandBuffer!) -> Void in 37 | var q = dispatch_semaphore_signal(self.avaliableUniformBuffers) 38 | }) 39 | 40 | 41 | var shouldEndEncodingOnLastChild = vertexCount <= 0 42 | var commandEncoder: MTLRenderCommandEncoder? = nil 43 | 44 | for var i = 0; i < children.count; i++ 45 | { 46 | var child = children[i] 47 | var lastChild = i == children.count - 1 48 | commandEncoder = renderNode(child, parentMatrix: myModelViewMatrix, projectionMatrix: projectionMatrix, renderPassDescriptor: renderPathDescriptor, commandBuffer: commandBuffer, encoder: commandEncoder) 49 | } 50 | 51 | if vertexCount > 0 52 | { 53 | commandEncoder = renderNode(self, parentMatrix: parentModelViewMatrix, projectionMatrix: projectionMatrix, renderPassDescriptor: renderPathDescriptor, commandBuffer: commandBuffer, encoder: commandEncoder) 54 | } 55 | 56 | commandBuffer.presentDrawable(metalView.frameBuffer.currentDrawable) 57 | 58 | commandEncoder?.endEncoding() 59 | 60 | // Commit commandBuffer to his commandQueue in which he will be executed after commands before him in queue 61 | commandBuffer.commit(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Main/MetalViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MetalViewController.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | 10 | import UIKit 11 | import QuartzCore 12 | import Metal 13 | 14 | class MetalViewController: UIViewController,MetalViewProtocol { 15 | 16 | var metalView: MetalView! 17 | 18 | let device: MTLDevice = MTLCreateSystemDefaultDevice() 19 | var commandQ: MTLCommandQueue! 20 | var renderPipeline: MTLRenderPipelineState! 21 | 22 | var cube: Cube! 23 | var cube1: Cube! 24 | var baseEffect: BaseEffect! 25 | 26 | deinit{ 27 | tearDownMetal() 28 | } 29 | 30 | override func viewDidLoad() { 31 | super.viewDidLoad() 32 | 33 | var font = UIApplication.sharedApplication() 34 | 35 | metalView = self.view as? MetalView 36 | metalView.metalViewDelegate = self 37 | setupMetal() 38 | } 39 | 40 | override func viewDidAppear(animated: Bool){ 41 | super.viewDidAppear(animated) 42 | metalView.resume() 43 | } 44 | 45 | override func viewDidDisappear(animated: Bool){ 46 | super.viewDidDisappear(animated) 47 | metalView.pause() 48 | } 49 | 50 | func setupMetal(){ 51 | commandQ = device.newCommandQueue() 52 | baseEffect = BaseEffect(device: device, vertexShaderName: "myVertexShader", fragmentShaderName: "myFragmentShader") 53 | baseEffect.ambientIntensity = 0.4 54 | baseEffect.diffuseIntensity = 0.8 55 | baseEffect.lightDirection = [0.0,1.0,-1.0] 56 | baseEffect.specularIntensity = 2.0 57 | baseEffect.shininess = 8.0 58 | 59 | var ratio: Float = Float(self.view.bounds.size.width) / Float(self.view.bounds.size.height) 60 | baseEffect.projectionMatrix = Matrix4.makePerspectiveViewAngle(Matrix4.degreesToRad(85.0), aspectRatio: ratio, nearZ: 1.0, farZ: 150.0) 61 | 62 | cube = Cube(baseEffect: baseEffect) 63 | cube1 = Cube(baseEffect: baseEffect) 64 | renderPipeline = baseEffect.compile() 65 | } 66 | 67 | func tearDownMetal(){ 68 | commandQ = nil 69 | renderPipeline = nil 70 | cube = nil 71 | cube1 = nil 72 | baseEffect = nil 73 | } 74 | 75 | func update(delta: CFTimeInterval){ 76 | //Draw 77 | var metalLayer:CAMetalLayer? = self.view.layer as? CAMetalLayer 78 | 79 | if let mLayer = metalLayer 80 | { 81 | var drawable = mLayer.newDrawable() 82 | var matrix: Matrix4 = Matrix4() 83 | matrix.translate(0, y: 0, z: -5) 84 | matrix.rotateAroundX(Matrix4.degreesToRad(20.0), y: 0, z: 0) 85 | cube.render(commandQ, drawable: drawable, parentMVMatrix: matrix) 86 | matrix.translate(0, y: -2, z: -5) 87 | cube1.render(commandQ, drawable: drawable, parentMVMatrix: matrix) 88 | } 89 | 90 | cube.updateWithDelta(delta) 91 | cube1.updateWithDelta(delta*2) 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /1. Colored Triangle/MetalTryOut-Objc/Main/MetalView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MetalView.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import QuartzCore 11 | 12 | @objc protocol MetalViewProtocol 13 | { 14 | func update(delta: CFTimeInterval) 15 | } 16 | 17 | @objc class MetalView: UIView 18 | { 19 | //Public API 20 | var metalViewDelegate: MetalViewProtocol? 21 | 22 | func setFPSLabelHidden(hidden: Bool){ 23 | if let label = fpsLabel{ 24 | label.hidden = hidden 25 | } 26 | 27 | } 28 | 29 | func pause(){ 30 | if let link = displayLink{ 31 | link.paused = true 32 | } 33 | } 34 | 35 | func resume(){ 36 | if let link = displayLink{ 37 | link.paused = false 38 | } 39 | } 40 | 41 | init(frame: CGRect) { 42 | super.init(frame: frame) 43 | // Initialization code 44 | setup() 45 | } 46 | 47 | init(coder aDecoder: NSCoder!){ 48 | super.init(coder: aDecoder) 49 | setup() 50 | } 51 | 52 | //Private 53 | var displayLink: CADisplayLink? 54 | var lastFrameTimestamp: CFTimeInterval? 55 | var fpsLabel: UILabel? 56 | 57 | override class func layerClass() -> AnyClass{ 58 | return CAMetalLayer.self 59 | } 60 | 61 | override func layoutSubviews() 62 | { 63 | var rightConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1.0, constant: 0.0) 64 | var botConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1.0, constant: 0.0) 65 | var heightConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 25.0) 66 | var widthConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 85.0) 67 | 68 | self.addConstraints([rightConstraint,botConstraint,widthConstraint,heightConstraint]) 69 | super.layoutSubviews() 70 | } 71 | 72 | func setup(){ 73 | fpsLabel = UILabel(frame: CGRectZero) 74 | fpsLabel!.setTranslatesAutoresizingMaskIntoConstraints(false) 75 | self.addSubview(fpsLabel) 76 | 77 | displayLink = CADisplayLink(target: self, selector: Selector.convertFromStringLiteral("drawCall:")) 78 | displayLink?.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes) 79 | } 80 | 81 | func drawCall(displayLink: CADisplayLink){ 82 | if lastFrameTimestamp == nil{ 83 | lastFrameTimestamp = displayLink.timestamp 84 | } 85 | 86 | var elapsed:CFTimeInterval = displayLink.timestamp - lastFrameTimestamp! 87 | 88 | if fpsLabel?.hidden == false{ 89 | var fps = 1.0 / elapsed 90 | fpsLabel!.text = "fps: \(fps)" 91 | } 92 | lastFrameTimestamp = displayLink.timestamp 93 | 94 | metalViewDelegate?.update(elapsed) 95 | 96 | } 97 | 98 | 99 | } 100 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Nodes/Matrix4x4.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Matrix4x4.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/25/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | @objc class Matrix4x4 { 10 | 11 | let rows: Int = 4 12 | let columns: Int = 4 13 | var grid: [Float] 14 | 15 | init() { 16 | grid = Array(count: rows * columns, repeatedValue: 0.0) 17 | grid[0] = 1.0 18 | grid[5] = 1.0 19 | grid[10] = 1.0 20 | grid[15] = 1.0 21 | } 22 | 23 | func transpose() 24 | { 25 | var tmp: [Float] = Array(count: rows * columns, repeatedValue: 0.0) 26 | var i,j : Int 27 | for i = 0; i < 4; i++ 28 | { 29 | for j = 0; j < 4; j++ 30 | { 31 | tmp[4 * i + j] = grid[i + 4 * j] 32 | } 33 | } 34 | for var i = 0; i < 16; i++ 35 | { 36 | grid[i] = tmp[i] 37 | } 38 | } 39 | 40 | func multiplyMatrix(matrix: Matrix4x4) 41 | { 42 | var tmp: [Float] = Array(count: columns, repeatedValue: 0.0) 43 | for var j = 0; j < 4; j++ 44 | { 45 | tmp[0] = grid[j] 46 | tmp[1] = grid[4+j] 47 | tmp[2] = grid[8+j]; 48 | tmp[3] = grid[12+j]; 49 | for var i = 0; i < 4; i++ 50 | { 51 | var value = matrix.grid[4*i ]*tmp[0] 52 | value = value + matrix.grid[4*i+1]*tmp[1] 53 | value = value + matrix.grid[4*i+2]*tmp[2] 54 | value = value + matrix.grid[4*i+3]*tmp[3] 55 | 56 | grid[4*i+j] = value 57 | } 58 | } 59 | } 60 | 61 | func rotateAroundX(angleRad: Float) 62 | { 63 | var m: Matrix4x4 = Matrix4x4() 64 | 65 | m.grid[ 0] = 1.0 66 | m.grid[ 5] = cosf(angleRad) 67 | m.grid[10] = m.grid[5] 68 | m.grid[ 6] = sinf(angleRad) 69 | m.grid[ 9] = -m.grid[6] 70 | 71 | multiplyMatrix(m) 72 | } 73 | 74 | func rotateAroundY(angleRad: Float) 75 | { 76 | var m: Matrix4x4 = Matrix4x4() 77 | 78 | m.grid[ 0] = cosf(angleRad) 79 | m.grid[ 5] = 1.0 80 | m.grid[10] = m.grid[0] 81 | m.grid[ 2] = -sinf(angleRad) 82 | m.grid[ 8] = -m.grid[2] 83 | 84 | multiplyMatrix(m) 85 | } 86 | 87 | func rotateAroundZ(angleRad: Float) 88 | { 89 | var m: Matrix4x4 = Matrix4x4() 90 | 91 | m.grid[ 0] = cosf(angleRad) 92 | m.grid[ 5] = m.grid[0] 93 | m.grid[10] = 1.0 94 | m.grid[ 1] = sinf(angleRad) 95 | m.grid[ 4] = -m.grid[1] 96 | 97 | multiplyMatrix(m) 98 | } 99 | 100 | func translate(x:Float, y:Float, z:Float) 101 | { 102 | var m: Matrix4x4 = Matrix4x4() // create identity matrix 103 | m.grid[12] = x 104 | m.grid[13] = y 105 | m.grid[14] = z 106 | 107 | multiplyMatrix(m) 108 | } 109 | 110 | func scale(x:Float, y:Float, z:Float) 111 | { 112 | var m: Matrix4x4 = Matrix4x4() // create identity matrix 113 | m.grid[12] = x 114 | m.grid[13] = y 115 | m.grid[14] = z 116 | 117 | multiplyMatrix(m) 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Nodes/Matrix4x4.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Matrix4x4.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/25/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | @objc class Matrix4x4 { 10 | 11 | let rows: Int = 4 12 | let columns: Int = 4 13 | var grid: [Float] 14 | 15 | init() { 16 | grid = Array(count: rows * columns, repeatedValue: 0.0) 17 | grid[0] = 1.0 18 | grid[5] = 1.0 19 | grid[10] = 1.0 20 | grid[15] = 1.0 21 | } 22 | 23 | func transpose() 24 | { 25 | var tmp: [Float] = Array(count: rows * columns, repeatedValue: 0.0) 26 | var i,j : Int 27 | for i = 0; i < 4; i++ 28 | { 29 | for j = 0; j < 4; j++ 30 | { 31 | tmp[4 * i + j] = grid[i + 4 * j] 32 | } 33 | } 34 | for var i = 0; i < 16; i++ 35 | { 36 | grid[i] = tmp[i] 37 | } 38 | } 39 | 40 | func multiplyMatrix(matrix: Matrix4x4) 41 | { 42 | var tmp: [Float] = Array(count: columns, repeatedValue: 0.0) 43 | for var j = 0; j < 4; j++ 44 | { 45 | tmp[0] = grid[j] 46 | tmp[1] = grid[4+j] 47 | tmp[2] = grid[8+j]; 48 | tmp[3] = grid[12+j]; 49 | for var i = 0; i < 4; i++ 50 | { 51 | var value = matrix.grid[4*i ]*tmp[0] 52 | value = value + matrix.grid[4*i+1]*tmp[1] 53 | value = value + matrix.grid[4*i+2]*tmp[2] 54 | value = value + matrix.grid[4*i+3]*tmp[3] 55 | 56 | grid[4*i+j] = value 57 | } 58 | } 59 | } 60 | 61 | func rotateAroundX(angleRad: Float) 62 | { 63 | var m: Matrix4x4 = Matrix4x4() 64 | 65 | m.grid[ 0] = 1.0 66 | m.grid[ 5] = cosf(angleRad) 67 | m.grid[10] = m.grid[5] 68 | m.grid[ 6] = sinf(angleRad) 69 | m.grid[ 9] = -m.grid[6] 70 | 71 | multiplyMatrix(m) 72 | } 73 | 74 | func rotateAroundY(angleRad: Float) 75 | { 76 | var m: Matrix4x4 = Matrix4x4() 77 | 78 | m.grid[ 0] = cosf(angleRad) 79 | m.grid[ 5] = 1.0 80 | m.grid[10] = m.grid[0] 81 | m.grid[ 2] = -sinf(angleRad) 82 | m.grid[ 8] = -m.grid[2] 83 | 84 | multiplyMatrix(m) 85 | } 86 | 87 | func rotateAroundZ(angleRad: Float) 88 | { 89 | var m: Matrix4x4 = Matrix4x4() 90 | 91 | m.grid[ 0] = cosf(angleRad) 92 | m.grid[ 5] = m.grid[0] 93 | m.grid[10] = 1.0 94 | m.grid[ 1] = sinf(angleRad) 95 | m.grid[ 4] = -m.grid[1] 96 | 97 | multiplyMatrix(m) 98 | } 99 | 100 | func translate(x:Float, y:Float, z:Float) 101 | { 102 | var m: Matrix4x4 = Matrix4x4() // create identity matrix 103 | m.grid[12] = x 104 | m.grid[13] = y 105 | m.grid[14] = z 106 | 107 | multiplyMatrix(m) 108 | } 109 | 110 | func scale(x:Float, y:Float, z:Float) 111 | { 112 | var m: Matrix4x4 = Matrix4x4() // create identity matrix 113 | m.grid[12] = x 114 | m.grid[13] = y 115 | m.grid[14] = z 116 | 117 | multiplyMatrix(m) 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Nodes/Matrix4x4.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Matrix4x4.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/25/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | @objc class Matrix4x4 { 10 | 11 | let rows: Int = 4 12 | let columns: Int = 4 13 | var grid: [Float] 14 | 15 | init() { 16 | grid = Array(count: rows * columns, repeatedValue: 0.0) 17 | grid[0] = 1.0 18 | grid[5] = 1.0 19 | grid[10] = 1.0 20 | grid[15] = 1.0 21 | } 22 | 23 | func transpose() 24 | { 25 | var tmp: [Float] = Array(count: rows * columns, repeatedValue: 0.0) 26 | var i,j : Int 27 | for i = 0; i < 4; i++ 28 | { 29 | for j = 0; j < 4; j++ 30 | { 31 | tmp[4 * i + j] = grid[i + 4 * j] 32 | } 33 | } 34 | for var i = 0; i < 16; i++ 35 | { 36 | grid[i] = tmp[i] 37 | } 38 | } 39 | 40 | func multiplyMatrix(matrix: Matrix4x4) 41 | { 42 | var tmp: [Float] = Array(count: columns, repeatedValue: 0.0) 43 | for var j = 0; j < 4; j++ 44 | { 45 | tmp[0] = grid[j] 46 | tmp[1] = grid[4+j] 47 | tmp[2] = grid[8+j]; 48 | tmp[3] = grid[12+j]; 49 | for var i = 0; i < 4; i++ 50 | { 51 | var value = matrix.grid[4*i ]*tmp[0] 52 | value = value + matrix.grid[4*i+1]*tmp[1] 53 | value = value + matrix.grid[4*i+2]*tmp[2] 54 | value = value + matrix.grid[4*i+3]*tmp[3] 55 | 56 | grid[4*i+j] = value 57 | } 58 | } 59 | } 60 | 61 | func rotateAroundX(angleRad: Float) 62 | { 63 | var m: Matrix4x4 = Matrix4x4() 64 | 65 | m.grid[ 0] = 1.0 66 | m.grid[ 5] = cosf(angleRad) 67 | m.grid[10] = m.grid[5] 68 | m.grid[ 6] = sinf(angleRad) 69 | m.grid[ 9] = -m.grid[6] 70 | 71 | multiplyMatrix(m) 72 | } 73 | 74 | func rotateAroundY(angleRad: Float) 75 | { 76 | var m: Matrix4x4 = Matrix4x4() 77 | 78 | m.grid[ 0] = cosf(angleRad) 79 | m.grid[ 5] = 1.0 80 | m.grid[10] = m.grid[0] 81 | m.grid[ 2] = -sinf(angleRad) 82 | m.grid[ 8] = -m.grid[2] 83 | 84 | multiplyMatrix(m) 85 | } 86 | 87 | func rotateAroundZ(angleRad: Float) 88 | { 89 | var m: Matrix4x4 = Matrix4x4() 90 | 91 | m.grid[ 0] = cosf(angleRad) 92 | m.grid[ 5] = m.grid[0] 93 | m.grid[10] = 1.0 94 | m.grid[ 1] = sinf(angleRad) 95 | m.grid[ 4] = -m.grid[1] 96 | 97 | multiplyMatrix(m) 98 | } 99 | 100 | func translate(x:Float, y:Float, z:Float) 101 | { 102 | var m: Matrix4x4 = Matrix4x4() // create identity matrix 103 | m.grid[12] = x 104 | m.grid[13] = y 105 | m.grid[14] = z 106 | 107 | multiplyMatrix(m) 108 | } 109 | 110 | func scale(x:Float, y:Float, z:Float) 111 | { 112 | var m: Matrix4x4 = Matrix4x4() // create identity matrix 113 | m.grid[12] = x 114 | m.grid[13] = y 115 | m.grid[14] = z 116 | 117 | multiplyMatrix(m) 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Nodes/Matrix4x4.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Matrix4x4.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/25/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | @objc class Matrix4x4 { 10 | 11 | let rows: Int = 4 12 | let columns: Int = 4 13 | var grid: [Float] 14 | 15 | init() { 16 | grid = Array(count: rows * columns, repeatedValue: 0.0) 17 | grid[0] = 1.0 18 | grid[5] = 1.0 19 | grid[10] = 1.0 20 | grid[15] = 1.0 21 | } 22 | 23 | func transpose() 24 | { 25 | var tmp: [Float] = Array(count: rows * columns, repeatedValue: 0.0) 26 | var i,j : Int 27 | for i = 0; i < 4; i++ 28 | { 29 | for j = 0; j < 4; j++ 30 | { 31 | tmp[4 * i + j] = grid[i + 4 * j] 32 | } 33 | } 34 | for var i = 0; i < 16; i++ 35 | { 36 | grid[i] = tmp[i] 37 | } 38 | } 39 | 40 | func multiplyMatrix(matrix: Matrix4x4) 41 | { 42 | var tmp: [Float] = Array(count: columns, repeatedValue: 0.0) 43 | for var j = 0; j < 4; j++ 44 | { 45 | tmp[0] = grid[j] 46 | tmp[1] = grid[4+j] 47 | tmp[2] = grid[8+j]; 48 | tmp[3] = grid[12+j]; 49 | for var i = 0; i < 4; i++ 50 | { 51 | var value = matrix.grid[4*i ]*tmp[0] 52 | value = value + matrix.grid[4*i+1]*tmp[1] 53 | value = value + matrix.grid[4*i+2]*tmp[2] 54 | value = value + matrix.grid[4*i+3]*tmp[3] 55 | 56 | grid[4*i+j] = value 57 | } 58 | } 59 | } 60 | 61 | func rotateAroundX(angleRad: Float) 62 | { 63 | var m: Matrix4x4 = Matrix4x4() 64 | 65 | m.grid[ 0] = 1.0 66 | m.grid[ 5] = cosf(angleRad) 67 | m.grid[10] = m.grid[5] 68 | m.grid[ 6] = sinf(angleRad) 69 | m.grid[ 9] = -m.grid[6] 70 | 71 | multiplyMatrix(m) 72 | } 73 | 74 | func rotateAroundY(angleRad: Float) 75 | { 76 | var m: Matrix4x4 = Matrix4x4() 77 | 78 | m.grid[ 0] = cosf(angleRad) 79 | m.grid[ 5] = 1.0 80 | m.grid[10] = m.grid[0] 81 | m.grid[ 2] = -sinf(angleRad) 82 | m.grid[ 8] = -m.grid[2] 83 | 84 | multiplyMatrix(m) 85 | } 86 | 87 | func rotateAroundZ(angleRad: Float) 88 | { 89 | var m: Matrix4x4 = Matrix4x4() 90 | 91 | m.grid[ 0] = cosf(angleRad) 92 | m.grid[ 5] = m.grid[0] 93 | m.grid[10] = 1.0 94 | m.grid[ 1] = sinf(angleRad) 95 | m.grid[ 4] = -m.grid[1] 96 | 97 | multiplyMatrix(m) 98 | } 99 | 100 | func translate(x:Float, y:Float, z:Float) 101 | { 102 | var m: Matrix4x4 = Matrix4x4() // create identity matrix 103 | m.grid[12] = x 104 | m.grid[13] = y 105 | m.grid[14] = z 106 | 107 | multiplyMatrix(m) 108 | } 109 | 110 | func scale(x:Float, y:Float, z:Float) 111 | { 112 | var m: Matrix4x4 = Matrix4x4() // create identity matrix 113 | m.grid[12] = x 114 | m.grid[13] = y 115 | m.grid[14] = z 116 | 117 | multiplyMatrix(m) 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Main/MetalView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MetalView.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import QuartzCore 11 | 12 | @objc protocol MetalViewProtocol 13 | { 14 | func update(delta: CFTimeInterval) 15 | } 16 | 17 | @objc class MetalView: UIView 18 | { 19 | //Public API 20 | var metalViewDelegate: MetalViewProtocol? 21 | 22 | func setFPSLabelHidden(hidden: Bool){ 23 | if let label = fpsLabel{ 24 | label.hidden = hidden 25 | } 26 | 27 | } 28 | 29 | func pause(){ 30 | if let link = displayLink{ 31 | link.paused = true 32 | } 33 | } 34 | 35 | func resume(){ 36 | if let link = displayLink{ 37 | link.paused = false 38 | } 39 | } 40 | 41 | init(frame: CGRect) { 42 | super.init(frame: frame) 43 | // Initialization code 44 | setup() 45 | } 46 | 47 | init(coder aDecoder: NSCoder!){ 48 | super.init(coder: aDecoder) 49 | setup() 50 | } 51 | 52 | //Private 53 | var displayLink: CADisplayLink? 54 | var lastFrameTimestamp: CFTimeInterval? 55 | var fpsLabel: UILabel? 56 | 57 | override class func layerClass() -> AnyClass{ 58 | return CAMetalLayer.self 59 | } 60 | 61 | override func layoutSubviews() 62 | { 63 | var rightConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1.0, constant: 0.0) 64 | var botConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1.0, constant: 0.0) 65 | var heightConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 25.0) 66 | var widthConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 60.0) 67 | 68 | self.addConstraints([rightConstraint,botConstraint,widthConstraint,heightConstraint]) 69 | super.layoutSubviews() 70 | } 71 | 72 | func setup(){ 73 | fpsLabel = UILabel(frame: CGRectZero) 74 | fpsLabel!.setTranslatesAutoresizingMaskIntoConstraints(false) 75 | fpsLabel!.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.6) 76 | self.addSubview(fpsLabel) 77 | 78 | displayLink = CADisplayLink(target: self, selector: Selector.convertFromStringLiteral("drawCall:")) 79 | displayLink?.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes) 80 | } 81 | 82 | func drawCall(displayLink: CADisplayLink){ 83 | if lastFrameTimestamp == nil{ 84 | lastFrameTimestamp = displayLink.timestamp 85 | } 86 | 87 | var elapsed:CFTimeInterval = displayLink.timestamp - lastFrameTimestamp! 88 | 89 | if fpsLabel?.hidden == false{ 90 | var fps = 1.0 / elapsed 91 | fpsLabel!.text = "fps: \(Int(fps))" 92 | } 93 | lastFrameTimestamp = displayLink.timestamp 94 | 95 | metalViewDelegate?.update(elapsed) 96 | 97 | } 98 | 99 | 100 | } 101 | -------------------------------------------------------------------------------- /2. Cube + Transform/MetalTryOut-Objc/Main/MetalView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MetalView.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import QuartzCore 11 | 12 | @objc protocol MetalViewProtocol 13 | { 14 | func update(delta: CFTimeInterval) 15 | } 16 | 17 | @objc class MetalView: UIView 18 | { 19 | //Public API 20 | var metalViewDelegate: MetalViewProtocol? 21 | 22 | func setFPSLabelHidden(hidden: Bool){ 23 | if let label = fpsLabel{ 24 | label.hidden = hidden 25 | } 26 | 27 | } 28 | 29 | func pause(){ 30 | if let link = displayLink{ 31 | link.paused = true 32 | } 33 | } 34 | 35 | func resume(){ 36 | if let link = displayLink{ 37 | link.paused = false 38 | } 39 | } 40 | 41 | init(frame: CGRect) { 42 | super.init(frame: frame) 43 | // Initialization code 44 | setup() 45 | } 46 | 47 | init(coder aDecoder: NSCoder!){ 48 | super.init(coder: aDecoder) 49 | setup() 50 | } 51 | 52 | //Private 53 | var displayLink: CADisplayLink? 54 | var lastFrameTimestamp: CFTimeInterval? 55 | var fpsLabel: UILabel? 56 | 57 | override class func layerClass() -> AnyClass{ 58 | return CAMetalLayer.self 59 | } 60 | 61 | override func layoutSubviews() 62 | { 63 | var rightConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1.0, constant: 0.0) 64 | var botConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1.0, constant: 0.0) 65 | var heightConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 25.0) 66 | var widthConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 60.0) 67 | 68 | self.addConstraints([rightConstraint,botConstraint,widthConstraint,heightConstraint]) 69 | super.layoutSubviews() 70 | } 71 | 72 | func setup(){ 73 | fpsLabel = UILabel(frame: CGRectZero) 74 | fpsLabel!.setTranslatesAutoresizingMaskIntoConstraints(false) 75 | fpsLabel!.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.6) 76 | self.addSubview(fpsLabel) 77 | 78 | displayLink = CADisplayLink(target: self, selector: Selector.convertFromStringLiteral("drawCall:")) 79 | displayLink?.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes) 80 | } 81 | 82 | func drawCall(displayLink: CADisplayLink){ 83 | if lastFrameTimestamp == nil{ 84 | lastFrameTimestamp = displayLink.timestamp 85 | } 86 | 87 | var elapsed:CFTimeInterval = displayLink.timestamp - lastFrameTimestamp! 88 | 89 | if fpsLabel?.hidden == false{ 90 | var fps = 1.0 / elapsed 91 | fpsLabel!.text = "fps: \(Int(fps))" 92 | } 93 | lastFrameTimestamp = displayLink.timestamp 94 | 95 | metalViewDelegate?.update(elapsed) 96 | 97 | } 98 | 99 | 100 | } 101 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Main/MetalView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MetalView.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import QuartzCore 11 | 12 | @objc protocol MetalViewProtocol 13 | { 14 | func update(delta: CFTimeInterval) 15 | } 16 | 17 | @objc class MetalView: UIView 18 | { 19 | //Public API 20 | var metalViewDelegate: MetalViewProtocol? 21 | 22 | func setFPSLabelHidden(hidden: Bool){ 23 | if let label = fpsLabel{ 24 | label.hidden = hidden 25 | } 26 | 27 | } 28 | 29 | func pause(){ 30 | if let link = displayLink{ 31 | link.paused = true 32 | } 33 | } 34 | 35 | func resume(){ 36 | if let link = displayLink{ 37 | link.paused = false 38 | } 39 | } 40 | 41 | init(frame: CGRect) { 42 | super.init(frame: frame) 43 | // Initialization code 44 | setup() 45 | } 46 | 47 | init(coder aDecoder: NSCoder!){ 48 | super.init(coder: aDecoder) 49 | setup() 50 | } 51 | 52 | //Private 53 | var displayLink: CADisplayLink? 54 | var lastFrameTimestamp: CFTimeInterval? 55 | var fpsLabel: UILabel? 56 | 57 | override class func layerClass() -> AnyClass{ 58 | return CAMetalLayer.self 59 | } 60 | 61 | override func layoutSubviews() 62 | { 63 | var rightConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1.0, constant: 0.0) 64 | var botConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1.0, constant: 0.0) 65 | var heightConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 25.0) 66 | var widthConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 60.0) 67 | 68 | self.addConstraints([rightConstraint,botConstraint,widthConstraint,heightConstraint]) 69 | super.layoutSubviews() 70 | } 71 | 72 | func setup(){ 73 | fpsLabel = UILabel(frame: CGRectZero) 74 | fpsLabel!.setTranslatesAutoresizingMaskIntoConstraints(false) 75 | fpsLabel!.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.6) 76 | self.addSubview(fpsLabel) 77 | 78 | displayLink = CADisplayLink(target: self, selector: Selector.convertFromStringLiteral("drawCall:")) 79 | displayLink?.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes) 80 | } 81 | 82 | func drawCall(displayLink: CADisplayLink){ 83 | if lastFrameTimestamp == nil{ 84 | lastFrameTimestamp = displayLink.timestamp 85 | } 86 | 87 | var elapsed:CFTimeInterval = displayLink.timestamp - lastFrameTimestamp! 88 | 89 | if fpsLabel?.hidden == false{ 90 | var fps = 1.0 / elapsed 91 | fpsLabel!.text = "fps: \(Int(fps))" 92 | } 93 | lastFrameTimestamp = displayLink.timestamp 94 | 95 | metalViewDelegate?.update(elapsed) 96 | 97 | } 98 | 99 | 100 | } 101 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Main/MetalView[Conflict].swift: -------------------------------------------------------------------------------- 1 | // 2 | // MetalView.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/24/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import QuartzCore 11 | 12 | @objc protocol MetalViewProtocol 13 | { 14 | func update(metalView : MetalView ,delta: CFTimeInterval) 15 | func reshape(metalView : MetalView) 16 | } 17 | 18 | @objc class MetalView: UIView 19 | { 20 | //Public API 21 | var metalViewDelegate: MetalViewProtocol? 22 | 23 | func setFPSLabelHidden(hidden: Bool){ 24 | if let label = fpsLabel{ 25 | label.hidden = hidden 26 | } 27 | 28 | } 29 | 30 | func pause(){ 31 | if let link = displayLink{ 32 | link.paused = true 33 | } 34 | } 35 | 36 | func resume(){ 37 | if let link = displayLink{ 38 | link.paused = false 39 | } 40 | } 41 | 42 | init(frame: CGRect) { 43 | super.init(frame: frame) 44 | // Initialization code 45 | setup() 46 | } 47 | 48 | init(coder aDecoder: NSCoder!){ 49 | super.init(coder: aDecoder) 50 | setup() 51 | } 52 | 53 | //Private 54 | var displayLink: CADisplayLink? 55 | var lastFrameTimestamp: CFTimeInterval? 56 | var fpsLabel: UILabel? 57 | 58 | override class func layerClass() -> AnyClass{ 59 | return CAMetalLayer.self 60 | } 61 | 62 | override func layoutSubviews() 63 | { 64 | var rightConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1.0, constant: 0.0) 65 | var botConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1.0, constant: 0.0) 66 | var heightConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 25.0) 67 | var widthConstraint = NSLayoutConstraint(item: fpsLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 60.0) 68 | 69 | self.addConstraints([rightConstraint,botConstraint,widthConstraint,heightConstraint]) 70 | super.layoutSubviews() 71 | } 72 | 73 | func setup(){ 74 | fpsLabel = UILabel(frame: CGRectZero) 75 | fpsLabel!.setTranslatesAutoresizingMaskIntoConstraints(false) 76 | fpsLabel!.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.6) 77 | self.addSubview(fpsLabel) 78 | 79 | displayLink = CADisplayLink(target: self, selector: Selector.convertFromStringLiteral("drawCall:")) 80 | displayLink?.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes) 81 | } 82 | 83 | func drawCall(displayLink: CADisplayLink){ 84 | if lastFrameTimestamp == nil{ 85 | lastFrameTimestamp = displayLink.timestamp 86 | } 87 | 88 | var elapsed:CFTimeInterval = displayLink.timestamp - lastFrameTimestamp! 89 | 90 | if fpsLabel?.hidden == false{ 91 | var fps = 1.0 / elapsed 92 | fpsLabel!.text = "fps: \(Int(fps))" 93 | } 94 | lastFrameTimestamp = displayLink.timestamp 95 | 96 | metalViewDelegate?.update(self, delta: elapsed) 97 | 98 | } 99 | 100 | 101 | } 102 | -------------------------------------------------------------------------------- /3. Texured cube/MetalTryOut-Objc/Shaders/METLTexture.swift: -------------------------------------------------------------------------------- 1 | // 2 | // METLTexture.swift 3 | // Textured Cube 4 | // 5 | // Created by Andrew K. on 6/29/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import Metal 11 | 12 | class METLTexture: NSObject { 13 | 14 | var texture: MTLTexture? 15 | var target: MTLTextureType 16 | var width: UInt32 17 | var height: UInt32 18 | var depth: UInt32 19 | var format: MTLPixelFormat 20 | var hasAlpha: Bool 21 | var path: String 22 | 23 | init(name: String, extention: String) 24 | { 25 | var qPath = NSBundle.mainBundle().pathForResource(name, ofType: extention); 26 | 27 | path = qPath 28 | width = 0 29 | height = 0 30 | depth = 1 31 | format = MTLPixelFormat.FormatRGBA8Unorm 32 | target = MTLTextureType.Type2D 33 | texture = nil 34 | hasAlpha = false 35 | 36 | super.init() 37 | } 38 | 39 | func finalizeTexture(device:MTLDevice) -> Bool 40 | { 41 | return finalizeTexture(device, flip: true) 42 | } 43 | 44 | func finalizeTexture(device: MTLDevice, flip: Bool) -> Bool 45 | { 46 | var pImage: UIImage? = UIImage(contentsOfFile: path) 47 | if pImage == nil 48 | { 49 | return false 50 | } 51 | 52 | var pColorSpace:CGColorSpaceRef = CGColorSpaceCreateDeviceRGB() 53 | 54 | if pColorSpace == nil 55 | { 56 | pImage = nil 57 | return false 58 | } // if 59 | 60 | self.width = UInt32(CGImageGetWidth(pImage?.CGImage)) 61 | self.height = UInt32(CGImageGetHeight(pImage?.CGImage)) 62 | 63 | var width = self.width 64 | var height = self.height 65 | var rowBytes = width * 4 66 | 67 | var info:CGBitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.toRaw()) 68 | 69 | var pContext:CGContextRef = CGBitmapContextCreate(nil, UInt(width), UInt(height), 8, UInt(rowBytes), pColorSpace, info) 70 | 71 | 72 | 73 | CGColorSpaceRelease(pColorSpace) 74 | 75 | if pContext == nil 76 | { 77 | return false 78 | } 79 | 80 | var bounds = CGRectMake(0.0, 0.0, CGFloat(width), CGFloat(height)); 81 | 82 | CGContextClearRect(pContext, bounds) 83 | 84 | // Vertical Reflect 85 | if(flip) 86 | { 87 | CGContextTranslateCTM(pContext, CGFloat(width), CGFloat(height)); 88 | CGContextScaleCTM(pContext, -1.0, -1.0); 89 | } // if 90 | 91 | CGContextDrawImage(pContext, bounds, pImage!.CGImage); 92 | 93 | pImage = nil; 94 | 95 | var pTexDesc = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(MTLPixelFormat.FormatRGBA8Unorm, width: Int(width), height: Int(height), mipmapped: false) 96 | self.target = pTexDesc.textureType 97 | self.texture = device.newTextureWithDescriptor(pTexDesc) 98 | 99 | pTexDesc = nil; 100 | 101 | if self.texture == nil 102 | { 103 | CGContextRelease(pContext) 104 | return false 105 | } 106 | 107 | var pPixels: COpaquePointer = CGBitmapContextGetData(pContext); 108 | self.texture?.replaceRegion(MTLTextureRegionMake2D(0, 0, Int(width), Int(height)), mipmapLevel: 0, withBytes: pPixels, bytesPerRow: Int(rowBytes)) 109 | 110 | 111 | CGContextRelease(pContext) 112 | 113 | return true 114 | } 115 | 116 | } 117 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Shaders/METLTexture.swift: -------------------------------------------------------------------------------- 1 | // 2 | // METLTexture.swift 3 | // Textured Cube 4 | // 5 | // Created by Andrew K. on 6/29/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import Metal 11 | 12 | class METLTexture: NSObject { 13 | 14 | var texture: MTLTexture? 15 | var target: MTLTextureType 16 | var width: UInt32 17 | var height: UInt32 18 | var depth: UInt32 19 | var format: MTLPixelFormat 20 | var hasAlpha: Bool 21 | var path: String 22 | 23 | init(name: String, extention: String) 24 | { 25 | var qPath = NSBundle.mainBundle().pathForResource(name, ofType: extention); 26 | 27 | path = qPath 28 | width = 0 29 | height = 0 30 | depth = 1 31 | format = MTLPixelFormat.FormatRGBA8Unorm 32 | target = MTLTextureType.Type2D 33 | texture = nil 34 | hasAlpha = false 35 | 36 | super.init() 37 | } 38 | 39 | func finalizeTexture(device:MTLDevice) -> Bool 40 | { 41 | return finalizeTexture(device, flip: true) 42 | } 43 | 44 | func finalizeTexture(device: MTLDevice, flip: Bool) -> Bool 45 | { 46 | var pImage: UIImage? = UIImage(contentsOfFile: path) 47 | if pImage == nil 48 | { 49 | return false 50 | } 51 | 52 | var pColorSpace:CGColorSpaceRef = CGColorSpaceCreateDeviceRGB() 53 | 54 | if pColorSpace == nil 55 | { 56 | pImage = nil 57 | return false 58 | } // if 59 | 60 | self.width = UInt32(CGImageGetWidth(pImage?.CGImage)) 61 | self.height = UInt32(CGImageGetHeight(pImage?.CGImage)) 62 | 63 | var width = self.width 64 | var height = self.height 65 | var rowBytes = width * 4 66 | 67 | var info:CGBitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.toRaw()) 68 | 69 | var pContext:CGContextRef = CGBitmapContextCreate(nil, UInt(width), UInt(height), 8, UInt(rowBytes), pColorSpace, info) 70 | 71 | 72 | 73 | CGColorSpaceRelease(pColorSpace) 74 | 75 | if pContext == nil 76 | { 77 | return false 78 | } 79 | 80 | var bounds = CGRectMake(0.0, 0.0, CGFloat(width), CGFloat(height)); 81 | 82 | CGContextClearRect(pContext, bounds) 83 | 84 | // Vertical Reflect 85 | if(flip) 86 | { 87 | CGContextTranslateCTM(pContext, CGFloat(width), CGFloat(height)); 88 | CGContextScaleCTM(pContext, -1.0, -1.0); 89 | } // if 90 | 91 | CGContextDrawImage(pContext, bounds, pImage!.CGImage); 92 | 93 | pImage = nil; 94 | 95 | var pTexDesc = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(MTLPixelFormat.FormatRGBA8Unorm, width: Int(width), height: Int(height), mipmapped: false) 96 | self.target = pTexDesc.textureType 97 | self.texture = device.newTextureWithDescriptor(pTexDesc) 98 | 99 | pTexDesc = nil; 100 | 101 | if self.texture == nil 102 | { 103 | CGContextRelease(pContext) 104 | return false 105 | } 106 | 107 | var pPixels: COpaquePointer = CGBitmapContextGetData(pContext); 108 | self.texture?.replaceRegion(MTLTextureRegionMake2D(0, 0, Int(width), Int(height)), mipmapLevel: 0, withBytes: pPixels, bytesPerRow: Int(rowBytes)) 109 | 110 | 111 | CGContextRelease(pContext) 112 | 113 | return true 114 | } 115 | 116 | } 117 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Shaders/METLTexture.swift: -------------------------------------------------------------------------------- 1 | // 2 | // METLTexture.swift 3 | // Textured Cube 4 | // 5 | // Created by Andrew K. on 6/29/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import Metal 11 | 12 | class METLTexture: NSObject { 13 | 14 | var texture: MTLTexture? 15 | var target: MTLTextureType 16 | var width: UInt32 17 | var height: UInt32 18 | var depth: UInt32 19 | var format: MTLPixelFormat 20 | var hasAlpha: Bool 21 | var path: String 22 | 23 | init(name: String, extention: String) 24 | { 25 | var qPath = NSBundle.mainBundle().pathForResource(name, ofType: extention); 26 | 27 | path = qPath 28 | width = 0 29 | height = 0 30 | depth = 1 31 | format = MTLPixelFormat.FormatRGBA8Unorm 32 | target = MTLTextureType.Type2D 33 | texture = nil 34 | hasAlpha = false 35 | 36 | super.init() 37 | } 38 | 39 | func finalizeTexture(device:MTLDevice) -> Bool 40 | { 41 | return finalizeTexture(device, flip: true) 42 | } 43 | 44 | func finalizeTexture(device: MTLDevice, flip: Bool) -> Bool 45 | { 46 | var pImage: UIImage? = UIImage(contentsOfFile: path) 47 | if pImage == nil 48 | { 49 | return false 50 | } 51 | 52 | var pColorSpace:CGColorSpaceRef = CGColorSpaceCreateDeviceRGB() 53 | 54 | if pColorSpace == nil 55 | { 56 | pImage = nil 57 | return false 58 | } // if 59 | 60 | self.width = UInt32(CGImageGetWidth(pImage?.CGImage)) 61 | self.height = UInt32(CGImageGetHeight(pImage?.CGImage)) 62 | 63 | var width = self.width 64 | var height = self.height 65 | var rowBytes = width * 4 66 | 67 | var info:CGBitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.toRaw()) 68 | 69 | var pContext:CGContextRef = CGBitmapContextCreate(nil, UInt(width), UInt(height), 8, UInt(rowBytes), pColorSpace, info) 70 | 71 | 72 | 73 | CGColorSpaceRelease(pColorSpace) 74 | 75 | if pContext == nil 76 | { 77 | return false 78 | } 79 | 80 | var bounds = CGRectMake(0.0, 0.0, CGFloat(width), CGFloat(height)); 81 | 82 | CGContextClearRect(pContext, bounds) 83 | 84 | // Vertical Reflect 85 | if(flip) 86 | { 87 | CGContextTranslateCTM(pContext, CGFloat(width), CGFloat(height)); 88 | CGContextScaleCTM(pContext, -1.0, -1.0); 89 | } // if 90 | 91 | CGContextDrawImage(pContext, bounds, pImage!.CGImage); 92 | 93 | pImage = nil; 94 | 95 | var pTexDesc = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(MTLPixelFormat.FormatRGBA8Unorm, width: Int(width), height: Int(height), mipmapped: false) 96 | self.target = pTexDesc.textureType 97 | self.texture = device.newTextureWithDescriptor(pTexDesc) 98 | 99 | pTexDesc = nil; 100 | 101 | if self.texture == nil 102 | { 103 | CGContextRelease(pContext) 104 | return false 105 | } 106 | 107 | var pPixels: COpaquePointer = CGBitmapContextGetData(pContext); 108 | self.texture?.replaceRegion(MTLTextureRegionMake2D(0, 0, Int(width), Int(height)), mipmapLevel: 0, withBytes: pPixels, bytesPerRow: Int(rowBytes)) 109 | 110 | 111 | CGContextRelease(pContext) 112 | 113 | return true 114 | } 115 | 116 | } 117 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Shaders/UniformsBufferGenerator.m: -------------------------------------------------------------------------------- 1 | // 2 | // UniformsBufferGenerator.m 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/26/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import "UniformsBufferGenerator.h" 10 | #import "Imported_Model-Swift.h" 11 | 12 | 13 | static const long kMaxBufferBytesPerFrame = 1024*1024; 14 | static const int kFloatsPerMatrix4 = 16; 15 | 16 | static const int kLightColorComponents = 4; 17 | static const int kLightDirectionComponents = 3; 18 | static const int kLightIntensityComponents = 4; 19 | 20 | @interface UniformsBufferGenerator() 21 | 22 | @property(nonatomic,readwrite) int indexOfAvaliableBuffer; 23 | @property(nonatomic,readwrite) int numberOfInflightBuffers; 24 | 25 | @property(nonatomic,strong) NSArray *buffers; 26 | 27 | @end 28 | 29 | @implementation UniformsBufferGenerator 30 | 31 | 32 | - (instancetype)initWithNumberOfInflightBuffers:(int)inflightBuffersCount 33 | withDevice:(id )device 34 | { 35 | self = [super init]; 36 | if(self != nil) 37 | { 38 | self.numberOfInflightBuffers = inflightBuffersCount; 39 | self.indexOfAvaliableBuffer = 0; 40 | 41 | NSMutableArray *mBuffers = [[NSMutableArray alloc] initWithCapacity:inflightBuffersCount]; 42 | for (int i = 0; i < inflightBuffersCount; i++) 43 | { 44 | id buffer = [device newBufferWithLength:kMaxBufferBytesPerFrame options:0]; 45 | buffer.label = [NSString stringWithFormat:@"Uniform buffer #%@",@(i)]; 46 | [mBuffers addObject:buffer]; 47 | } 48 | self.buffers = mBuffers; 49 | } 50 | return self; 51 | } 52 | 53 | - (id )bufferWithProjectionMatrix:(Matrix4 *)projMatrix 54 | modelViewMatrix:(Matrix4 *)mvMatrix 55 | withBaseEffect:(BaseEffect *)baseEffect 56 | withModel:(Node *)node 57 | { 58 | 59 | float color[4] = {baseEffect.lightColor.red,baseEffect.lightColor.green,baseEffect.lightColor.blue,baseEffect.lightColor.alpha}; 60 | 61 | float uniformFloatsBuffer[kFloatsPerMatrix4 * 2 + kLightColorComponents + kLightDirectionComponents + kLightIntensityComponents]; 62 | 63 | int counter = 0; 64 | for (int k = 0; k < 2; k++) 65 | { 66 | for (int i = 0; i < kFloatsPerMatrix4; i++) 67 | { 68 | if(k == 0) 69 | { 70 | uniformFloatsBuffer[counter++] = mvMatrix->glkMatrix.m[i]; 71 | } 72 | else 73 | { 74 | uniformFloatsBuffer[counter++] = projMatrix->glkMatrix.m[i]; 75 | } 76 | } 77 | } 78 | for (int i = 0; i < kLightColorComponents; i++) 79 | { 80 | uniformFloatsBuffer[counter++] = color[i]; 81 | } 82 | for (int i = 0; i < kLightDirectionComponents; i++) 83 | { 84 | uniformFloatsBuffer[counter++] = [baseEffect.lightDirection[i] floatValue]; 85 | } 86 | uniformFloatsBuffer[counter++] = node.ambientIntensity; 87 | uniformFloatsBuffer[counter++] = node.diffuseIntensity; 88 | uniformFloatsBuffer[counter++] = node.specularIntensity; 89 | uniformFloatsBuffer[counter++] = node.shininess; 90 | 91 | id uniformBuffer = self.buffers[self.indexOfAvaliableBuffer++]; 92 | if(self.indexOfAvaliableBuffer == self.numberOfInflightBuffers) 93 | { 94 | self.indexOfAvaliableBuffer = 0; 95 | } 96 | uint8_t *bufferPointer = (uint8_t *)[uniformBuffer contents]; 97 | 98 | memcpy(bufferPointer, &uniformFloatsBuffer, sizeof(uniformFloatsBuffer)); 99 | 100 | return uniformBuffer; 101 | } 102 | 103 | @end 104 | -------------------------------------------------------------------------------- /4. Cube + Lighting/MetalTryOut-Objc/Shaders/UniformsBufferGenerator.m: -------------------------------------------------------------------------------- 1 | // 2 | // UniformsBufferGenerator.m 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/26/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | #import "UniformsBufferGenerator.h" 10 | #import "Cube_Lighting-Swift.h" 11 | 12 | 13 | static const long kMaxBufferBytesPerFrame = 1024*1024; 14 | static const int kFloatsPerMatrix4 = 16; 15 | 16 | static const int kLightColorComponents = 4; 17 | static const int kLightDirectionComponents = 3; 18 | static const int kLightIntensityComponents = 4; 19 | 20 | @interface UniformsBufferGenerator() 21 | 22 | @property(nonatomic,readwrite) int indexOfAvaliableBuffer; 23 | @property(nonatomic,readwrite) int numberOfInflightBuffers; 24 | 25 | @property(nonatomic,strong) NSArray *buffers; 26 | 27 | @end 28 | 29 | @implementation UniformsBufferGenerator 30 | 31 | 32 | - (instancetype)initWithNumberOfInflightBuffers:(int)inflightBuffersCount 33 | withDevice:(id )device 34 | { 35 | self = [super init]; 36 | if(self != nil) 37 | { 38 | self.numberOfInflightBuffers = inflightBuffersCount; 39 | self.indexOfAvaliableBuffer = 0; 40 | 41 | NSMutableArray *mBuffers = [[NSMutableArray alloc] initWithCapacity:inflightBuffersCount]; 42 | for (int i = 0; i < inflightBuffersCount; i++) 43 | { 44 | id buffer = [device newBufferWithLength:kMaxBufferBytesPerFrame options:0]; 45 | buffer.label = [NSString stringWithFormat:@"Uniform buffer #%@",@(i)]; 46 | [mBuffers addObject:buffer]; 47 | } 48 | self.buffers = mBuffers; 49 | } 50 | return self; 51 | } 52 | 53 | - (id )bufferWithProjectionMatrix:(Matrix4 *)projMatrix 54 | modelViewMatrix:(Matrix4 *)mvMatrix 55 | withBaseEffect:(BaseEffect *)baseEffect 56 | { 57 | 58 | float color[4] = {baseEffect.lightColor.red,baseEffect.lightColor.green,baseEffect.lightColor.blue,baseEffect.lightColor.alpha}; 59 | 60 | float uniformFloatsBuffer[kFloatsPerMatrix4 * 2 + kLightColorComponents + kLightDirectionComponents + kLightIntensityComponents]; 61 | 62 | int counter = 0; 63 | for (int k = 0; k < 2; k++) 64 | { 65 | for (int i = 0; i < kFloatsPerMatrix4; i++) 66 | { 67 | if(k == 0) 68 | { 69 | uniformFloatsBuffer[counter++] = mvMatrix->glkMatrix.m[i]; 70 | } 71 | else 72 | { 73 | uniformFloatsBuffer[counter++] = projMatrix->glkMatrix.m[i]; 74 | } 75 | } 76 | } 77 | for (int i = 0; i < kLightColorComponents; i++) 78 | { 79 | uniformFloatsBuffer[counter++] = color[i]; 80 | } 81 | for (int i = 0; i < kLightDirectionComponents; i++) 82 | { 83 | uniformFloatsBuffer[counter++] = [baseEffect.lightDirection[i] floatValue]; 84 | } 85 | uniformFloatsBuffer[counter++] = baseEffect.ambientIntensity; 86 | uniformFloatsBuffer[counter++] = baseEffect.diffuseIntensity; 87 | uniformFloatsBuffer[counter++] = baseEffect.specularIntensity; 88 | uniformFloatsBuffer[counter++] = baseEffect.shininess; 89 | 90 | id uniformBuffer = self.buffers[self.indexOfAvaliableBuffer++]; 91 | if(self.indexOfAvaliableBuffer == self.numberOfInflightBuffers) 92 | { 93 | self.indexOfAvaliableBuffer = 0; 94 | } 95 | uint8_t *bufferPointer = (uint8_t *)[uniformBuffer contents]; 96 | 97 | memcpy(bufferPointer, &uniformFloatsBuffer, sizeof(uniformFloatsBuffer)); 98 | bufferPointer += sizeof(uniformFloatsBuffer); 99 | 100 | return uniformBuffer; 101 | } 102 | 103 | @end 104 | -------------------------------------------------------------------------------- /5. Imported Model/MetalTryOut-Objc/Nodes/Cube.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Cube.swift 3 | // Triangle 4 | // 5 | // Created by Andrew K. on 6/27/14. 6 | // Copyright (c) 2014 Andrew Kharchyshyn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @objc class Cube: Node { 12 | 13 | 14 | init(baseEffect: BaseEffect) 15 | { 16 | //Front 17 | var V0 = Vertex(x: 1.0, y: -1.0, z: 1.0, u: 1.0 , v: 0.0 , nX: 0.0 , nY: 0.0 , nZ: 1.0) 18 | var V1 = Vertex(x: 1.0, y: 1.0, z: 1.0, u: 1.0 , v: 1.0 , nX: 0.0 , nY: 0.0 , nZ: 1.0) 19 | var V2 = Vertex(x: -1.0, y: 1.0, z: 1.0, u: 0.0 , v: 1.0 , nX: 0.0 , nY: 0.0 , nZ: 1.0) 20 | var V3 = Vertex(x: -1.0, y: -1.0, z: 1.0, u: 0.0 , v: 0.0 , nX: 0.0 , nY: 0.0 , nZ: 1.0) 21 | //Back 22 | var V4 = Vertex(x: -1.0, y: -1.0, z: -1.0, u: 1.0 , v: 0.0 , nX: 0.0 , nY: 0.0 , nZ: -1.0) 23 | var V5 = Vertex(x: -1.0, y: 1.0, z: -1.0, u: 1.0 , v: 1.0 , nX: 0.0 , nY: 0.0 , nZ: -1.0) 24 | var V6 = Vertex(x: 1.0, y: 1.0, z: -1.0, u: 0.0 , v: 1.0 , nX: 0.0 , nY: 0.0 , nZ: -1.0) 25 | var V7 = Vertex(x: 1.0, y: -1.0, z: -1.0, u: 0.0 , v: 0.0 , nX: 0.0 , nY: 0.0 , nZ: -1.0) 26 | //Left 27 | var V8 = Vertex(x: -1.0, y: -1.0, z: 1.0, u: 1.0 , v: 0.0 , nX: -1.0 , nY: 0.0 , nZ: 0.0) 28 | var V9 = Vertex(x: -1.0, y: 1.0, z: 1.0, u: 1.0 , v: 1.0 , nX: -1.0 , nY: 0.0 , nZ: 0.0) 29 | var V10 = Vertex(x: -1.0, y: 1.0, z: -1.0, u: 0.0 , v: 1.0 , nX: -1.0 , nY: 0.0 , nZ: 0.0) 30 | var V11 = Vertex(x: -1.0, y: -1.0, z: -1.0, u: 0.0 , v: 0.0 , nX: -1.0 , nY: 0.0 , nZ: 0.0) 31 | //Right 32 | var V12 = Vertex(x: 1.0, y: -1.0, z: -1.0, u: 1.0 , v: 0.0 , nX: 1.0 , nY: 0.0 , nZ: 0.0) 33 | var V13 = Vertex(x: 1.0, y: 1.0, z: -1.0, u: 1.0 , v: 1.0 , nX: 1.0 , nY: 0.0 , nZ: 0.0) 34 | var V14 = Vertex(x: 1.0, y: 1.0, z: 1.0, u: 0.0 , v: 1.0 , nX: 1.0 , nY: 0.0 , nZ: 0.0) 35 | var V15 = Vertex(x: 1.0, y: -1.0, z: 1.0, u: 0.0 , v: 0.0 , nX: 1.0 , nY: 0.0 , nZ: 0.0) 36 | //Top 37 | var V16 = Vertex(x: 1.0, y: 1.0, z: 1.0, u: 1.0 , v: 0.0 , nX: 0.0 , nY: 1.0 , nZ: 0.0) 38 | var V17 = Vertex(x: 1.0, y: 1.0, z: -1.0, u: 1.0 , v: 1.0 , nX: 0.0 , nY: 1.0 , nZ: 0.0) 39 | var V18 = Vertex(x: -1.0, y: 1.0, z: -1.0, u: 0.0 , v: 1.0 , nX: 0.0 , nY: 1.0 , nZ: 0.0) 40 | var V19 = Vertex(x: -1.0, y: 1.0, z: 1.0, u: 0.0 , v: 0.0 , nX: 0.0 , nY: 1.0 , nZ: 0.0) 41 | //Bottom 42 | var V20 = Vertex(x: -1.0, y: -1.0, z: 1.0, u: 0.0 , v: 1.0 , nX: 0.0 , nY: -1.0 , nZ: 0.0) 43 | var V21 = Vertex(x: -1.0, y: -1.0, z: -1.0, u: 0.0 , v: 0.0 , nX: 0.0 , nY: -1.0 , nZ: 0.0) 44 | var V22 = Vertex(x: 1.0, y: -1.0, z: -1.0, u: 1.0 , v: 0.0 , nX: 0.0 , nY: -1.0 , nZ: 0.0) 45 | var V23 = Vertex(x: 1.0, y: -1.0, z: 1.0, u: 1.0 , v: 1.0 , nX: 0.0 , nY: -1.0 , nZ: 0.0) 46 | 47 | var verticesArray:Array = [ 48 | // Front 49 | V0, V1, V2, 50 | V2, V3, V0, 51 | // Back 52 | V4, V5, V6, 53 | V6, V7, V4, 54 | // Left 55 | V8, V9, V10, 56 | V10, V11, V8, 57 | // Right 58 | V12, V13, V14, 59 | V14, V15, V12, 60 | // Top 61 | V16, V17, V18, 62 | V18, V19, V16, 63 | // Bottom 64 | V20, V21, V22, 65 | V22, V23, V20 66 | ] 67 | 68 | 69 | super.init(name: "Cube", baseEffect: baseEffect, vertices: verticesArray, vertexCount: verticesArray.count, textureName: "smiley.png") 70 | } 71 | 72 | override func updateWithDelta(delta: CFTimeInterval) 73 | { 74 | super.updateWithDelta(delta) 75 | 76 | // rotationZ += Float(M_PI/10) * Float(delta) 77 | rotationY += Float(M_PI/8) * Float(delta) 78 | } 79 | } 80 | --------------------------------------------------------------------------------