├── .gitignore ├── LICENSE ├── MetalOfflineRecording.xcodeproj ├── project.pbxproj └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist └── MetalOfflineRecording ├── AppDelegate.swift ├── Assets.xcassets ├── AppIcon.appiconset │ └── Contents.json ├── ColorMap.textureset │ ├── Contents.json │ └── Universal.mipmapset │ │ ├── ColorMap.png │ │ └── Contents.json └── Contents.json ├── Base.lproj └── Main.storyboard ├── Info.plist ├── MetalOfflineRecording.entitlements ├── Renderer.swift ├── ShaderTypes.h ├── Shaders.metal ├── VideoRecorder.swift └── ViewController.swift /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | .DS_Store 25 | 26 | ## Obj-C/Swift specific 27 | *.hmap 28 | *.ipa 29 | *.dSYM.zip 30 | *.dSYM 31 | 32 | ## Playgrounds 33 | timeline.xctimeline 34 | playground.xcworkspace 35 | 36 | # Swift Package Manager 37 | # 38 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 39 | # Packages/ 40 | # Package.pins 41 | # Package.resolved 42 | .build/ 43 | 44 | # CocoaPods 45 | # 46 | # We recommend against adding the Pods directory to your .gitignore. However 47 | # you should judge for yourself, the pros and cons are mentioned at: 48 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 49 | # 50 | # Pods/ 51 | 52 | # Carthage 53 | # 54 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 55 | # Carthage/Checkouts 56 | 57 | Carthage/Build 58 | 59 | # fastlane 60 | # 61 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 62 | # screenshots whenever they are needed. 63 | # For more information about the recommended setup visit: 64 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 65 | 66 | fastlane/report.xml 67 | fastlane/Preview.html 68 | fastlane/screenshots/**/*.png 69 | fastlane/test_output 70 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Warren Moore 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MetalOfflineRecording.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 8334EBD52172F75400398038 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8334EBD42172F75400398038 /* AppDelegate.swift */; }; 11 | 8334EBD72172F75400398038 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8334EBD62172F75400398038 /* ViewController.swift */; }; 12 | 8334EBD92172F75400398038 /* Renderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8334EBD82172F75400398038 /* Renderer.swift */; }; 13 | 8334EBDB2172F75400398038 /* Shaders.metal in Sources */ = {isa = PBXBuildFile; fileRef = 8334EBDA2172F75400398038 /* Shaders.metal */; }; 14 | 8334EBDE2172F75600398038 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8334EBDD2172F75600398038 /* Assets.xcassets */; }; 15 | 8334EBE12172F75600398038 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8334EBDF2172F75600398038 /* Main.storyboard */; }; 16 | 8334EBEA2172FC4500398038 /* VideoRecorder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8334EBE92172FC4500398038 /* VideoRecorder.swift */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXFileReference section */ 20 | 8334EBD12172F75400398038 /* MetalOfflineRecording.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MetalOfflineRecording.app; sourceTree = BUILT_PRODUCTS_DIR; }; 21 | 8334EBD42172F75400398038 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 22 | 8334EBD62172F75400398038 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 23 | 8334EBD82172F75400398038 /* Renderer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Renderer.swift; sourceTree = ""; }; 24 | 8334EBDA2172F75400398038 /* Shaders.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = Shaders.metal; sourceTree = ""; }; 25 | 8334EBDC2172F75400398038 /* ShaderTypes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ShaderTypes.h; sourceTree = ""; }; 26 | 8334EBDD2172F75600398038 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 27 | 8334EBE02172F75600398038 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 28 | 8334EBE22172F75600398038 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 29 | 8334EBE32172F75600398038 /* MetalOfflineRecording.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = MetalOfflineRecording.entitlements; sourceTree = ""; }; 30 | 8334EBE92172FC4500398038 /* VideoRecorder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideoRecorder.swift; sourceTree = ""; }; 31 | /* End PBXFileReference section */ 32 | 33 | /* Begin PBXFrameworksBuildPhase section */ 34 | 8334EBCE2172F75400398038 /* Frameworks */ = { 35 | isa = PBXFrameworksBuildPhase; 36 | buildActionMask = 2147483647; 37 | files = ( 38 | ); 39 | runOnlyForDeploymentPostprocessing = 0; 40 | }; 41 | /* End PBXFrameworksBuildPhase section */ 42 | 43 | /* Begin PBXGroup section */ 44 | 8334EBC82172F75400398038 = { 45 | isa = PBXGroup; 46 | children = ( 47 | 8334EBD32172F75400398038 /* MetalOfflineRecording */, 48 | 8334EBD22172F75400398038 /* Products */, 49 | ); 50 | sourceTree = ""; 51 | }; 52 | 8334EBD22172F75400398038 /* Products */ = { 53 | isa = PBXGroup; 54 | children = ( 55 | 8334EBD12172F75400398038 /* MetalOfflineRecording.app */, 56 | ); 57 | name = Products; 58 | sourceTree = ""; 59 | }; 60 | 8334EBD32172F75400398038 /* MetalOfflineRecording */ = { 61 | isa = PBXGroup; 62 | children = ( 63 | 8334EBD42172F75400398038 /* AppDelegate.swift */, 64 | 8334EBD62172F75400398038 /* ViewController.swift */, 65 | 8334EBD82172F75400398038 /* Renderer.swift */, 66 | 8334EBE92172FC4500398038 /* VideoRecorder.swift */, 67 | 8334EBDA2172F75400398038 /* Shaders.metal */, 68 | 8334EBDC2172F75400398038 /* ShaderTypes.h */, 69 | 8334EBDD2172F75600398038 /* Assets.xcassets */, 70 | 8334EBDF2172F75600398038 /* Main.storyboard */, 71 | 8334EBE22172F75600398038 /* Info.plist */, 72 | 8334EBE32172F75600398038 /* MetalOfflineRecording.entitlements */, 73 | ); 74 | path = MetalOfflineRecording; 75 | sourceTree = ""; 76 | }; 77 | /* End PBXGroup section */ 78 | 79 | /* Begin PBXNativeTarget section */ 80 | 8334EBD02172F75400398038 /* MetalOfflineRecording */ = { 81 | isa = PBXNativeTarget; 82 | buildConfigurationList = 8334EBE62172F75600398038 /* Build configuration list for PBXNativeTarget "MetalOfflineRecording" */; 83 | buildPhases = ( 84 | 8334EBCD2172F75400398038 /* Sources */, 85 | 8334EBCE2172F75400398038 /* Frameworks */, 86 | 8334EBCF2172F75400398038 /* Resources */, 87 | ); 88 | buildRules = ( 89 | ); 90 | dependencies = ( 91 | ); 92 | name = MetalOfflineRecording; 93 | productName = MetalOfflineRecording; 94 | productReference = 8334EBD12172F75400398038 /* MetalOfflineRecording.app */; 95 | productType = "com.apple.product-type.application"; 96 | }; 97 | /* End PBXNativeTarget section */ 98 | 99 | /* Begin PBXProject section */ 100 | 8334EBC92172F75400398038 /* Project object */ = { 101 | isa = PBXProject; 102 | attributes = { 103 | LastSwiftUpdateCheck = 1000; 104 | LastUpgradeCheck = 1000; 105 | ORGANIZATIONNAME = "Warren Moore"; 106 | TargetAttributes = { 107 | 8334EBD02172F75400398038 = { 108 | CreatedOnToolsVersion = 10.0; 109 | }; 110 | }; 111 | }; 112 | buildConfigurationList = 8334EBCC2172F75400398038 /* Build configuration list for PBXProject "MetalOfflineRecording" */; 113 | compatibilityVersion = "Xcode 9.3"; 114 | developmentRegion = en; 115 | hasScannedForEncodings = 0; 116 | knownRegions = ( 117 | en, 118 | Base, 119 | ); 120 | mainGroup = 8334EBC82172F75400398038; 121 | productRefGroup = 8334EBD22172F75400398038 /* Products */; 122 | projectDirPath = ""; 123 | projectRoot = ""; 124 | targets = ( 125 | 8334EBD02172F75400398038 /* MetalOfflineRecording */, 126 | ); 127 | }; 128 | /* End PBXProject section */ 129 | 130 | /* Begin PBXResourcesBuildPhase section */ 131 | 8334EBCF2172F75400398038 /* Resources */ = { 132 | isa = PBXResourcesBuildPhase; 133 | buildActionMask = 2147483647; 134 | files = ( 135 | 8334EBDE2172F75600398038 /* Assets.xcassets in Resources */, 136 | 8334EBE12172F75600398038 /* Main.storyboard in Resources */, 137 | ); 138 | runOnlyForDeploymentPostprocessing = 0; 139 | }; 140 | /* End PBXResourcesBuildPhase section */ 141 | 142 | /* Begin PBXSourcesBuildPhase section */ 143 | 8334EBCD2172F75400398038 /* Sources */ = { 144 | isa = PBXSourcesBuildPhase; 145 | buildActionMask = 2147483647; 146 | files = ( 147 | 8334EBDB2172F75400398038 /* Shaders.metal in Sources */, 148 | 8334EBD72172F75400398038 /* ViewController.swift in Sources */, 149 | 8334EBEA2172FC4500398038 /* VideoRecorder.swift in Sources */, 150 | 8334EBD92172F75400398038 /* Renderer.swift in Sources */, 151 | 8334EBD52172F75400398038 /* AppDelegate.swift in Sources */, 152 | ); 153 | runOnlyForDeploymentPostprocessing = 0; 154 | }; 155 | /* End PBXSourcesBuildPhase section */ 156 | 157 | /* Begin PBXVariantGroup section */ 158 | 8334EBDF2172F75600398038 /* Main.storyboard */ = { 159 | isa = PBXVariantGroup; 160 | children = ( 161 | 8334EBE02172F75600398038 /* Base */, 162 | ); 163 | name = Main.storyboard; 164 | sourceTree = ""; 165 | }; 166 | /* End PBXVariantGroup section */ 167 | 168 | /* Begin XCBuildConfiguration section */ 169 | 8334EBE42172F75600398038 /* Debug */ = { 170 | isa = XCBuildConfiguration; 171 | buildSettings = { 172 | ALWAYS_SEARCH_USER_PATHS = NO; 173 | CLANG_ANALYZER_NONNULL = YES; 174 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 175 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 176 | CLANG_CXX_LIBRARY = "libc++"; 177 | CLANG_ENABLE_MODULES = YES; 178 | CLANG_ENABLE_OBJC_ARC = YES; 179 | CLANG_ENABLE_OBJC_WEAK = YES; 180 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 181 | CLANG_WARN_BOOL_CONVERSION = YES; 182 | CLANG_WARN_COMMA = YES; 183 | CLANG_WARN_CONSTANT_CONVERSION = YES; 184 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 185 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 186 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 187 | CLANG_WARN_EMPTY_BODY = YES; 188 | CLANG_WARN_ENUM_CONVERSION = YES; 189 | CLANG_WARN_INFINITE_RECURSION = YES; 190 | CLANG_WARN_INT_CONVERSION = YES; 191 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 192 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 193 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 194 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 195 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 196 | CLANG_WARN_STRICT_PROTOTYPES = YES; 197 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 198 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 199 | CLANG_WARN_UNREACHABLE_CODE = YES; 200 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 201 | CODE_SIGN_IDENTITY = "Mac Developer"; 202 | COPY_PHASE_STRIP = NO; 203 | DEBUG_INFORMATION_FORMAT = dwarf; 204 | ENABLE_STRICT_OBJC_MSGSEND = YES; 205 | ENABLE_TESTABILITY = YES; 206 | GCC_C_LANGUAGE_STANDARD = gnu11; 207 | GCC_DYNAMIC_NO_PIC = NO; 208 | GCC_NO_COMMON_BLOCKS = YES; 209 | GCC_OPTIMIZATION_LEVEL = 0; 210 | GCC_PREPROCESSOR_DEFINITIONS = ( 211 | "DEBUG=1", 212 | "$(inherited)", 213 | ); 214 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 215 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 216 | GCC_WARN_UNDECLARED_SELECTOR = YES; 217 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 218 | GCC_WARN_UNUSED_FUNCTION = YES; 219 | GCC_WARN_UNUSED_VARIABLE = YES; 220 | MACOSX_DEPLOYMENT_TARGET = 10.14; 221 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 222 | MTL_FAST_MATH = YES; 223 | ONLY_ACTIVE_ARCH = YES; 224 | SDKROOT = macosx; 225 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 226 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 227 | }; 228 | name = Debug; 229 | }; 230 | 8334EBE52172F75600398038 /* Release */ = { 231 | isa = XCBuildConfiguration; 232 | buildSettings = { 233 | ALWAYS_SEARCH_USER_PATHS = NO; 234 | CLANG_ANALYZER_NONNULL = YES; 235 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 236 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 237 | CLANG_CXX_LIBRARY = "libc++"; 238 | CLANG_ENABLE_MODULES = YES; 239 | CLANG_ENABLE_OBJC_ARC = YES; 240 | CLANG_ENABLE_OBJC_WEAK = YES; 241 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 242 | CLANG_WARN_BOOL_CONVERSION = YES; 243 | CLANG_WARN_COMMA = YES; 244 | CLANG_WARN_CONSTANT_CONVERSION = YES; 245 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 246 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 247 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 248 | CLANG_WARN_EMPTY_BODY = YES; 249 | CLANG_WARN_ENUM_CONVERSION = YES; 250 | CLANG_WARN_INFINITE_RECURSION = YES; 251 | CLANG_WARN_INT_CONVERSION = YES; 252 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 253 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 254 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 255 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 256 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 257 | CLANG_WARN_STRICT_PROTOTYPES = YES; 258 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 259 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 260 | CLANG_WARN_UNREACHABLE_CODE = YES; 261 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 262 | CODE_SIGN_IDENTITY = "Mac Developer"; 263 | COPY_PHASE_STRIP = NO; 264 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 265 | ENABLE_NS_ASSERTIONS = NO; 266 | ENABLE_STRICT_OBJC_MSGSEND = YES; 267 | GCC_C_LANGUAGE_STANDARD = gnu11; 268 | GCC_NO_COMMON_BLOCKS = YES; 269 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 270 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 271 | GCC_WARN_UNDECLARED_SELECTOR = YES; 272 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 273 | GCC_WARN_UNUSED_FUNCTION = YES; 274 | GCC_WARN_UNUSED_VARIABLE = YES; 275 | MACOSX_DEPLOYMENT_TARGET = 10.14; 276 | MTL_ENABLE_DEBUG_INFO = NO; 277 | MTL_FAST_MATH = YES; 278 | SDKROOT = macosx; 279 | SWIFT_COMPILATION_MODE = wholemodule; 280 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 281 | }; 282 | name = Release; 283 | }; 284 | 8334EBE72172F75600398038 /* Debug */ = { 285 | isa = XCBuildConfiguration; 286 | buildSettings = { 287 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 288 | CODE_SIGN_ENTITLEMENTS = MetalOfflineRecording/MetalOfflineRecording.entitlements; 289 | CODE_SIGN_STYLE = Automatic; 290 | COMBINE_HIDPI_IMAGES = YES; 291 | DEVELOPMENT_TEAM = ""; 292 | INFOPLIST_FILE = MetalOfflineRecording/Info.plist; 293 | LD_RUNPATH_SEARCH_PATHS = ( 294 | "$(inherited)", 295 | "@executable_path/../Frameworks", 296 | ); 297 | PRODUCT_BUNDLE_IDENTIFIER = com.metalbyexample.MetalOfflineRecording; 298 | PRODUCT_NAME = "$(TARGET_NAME)"; 299 | SWIFT_OBJC_BRIDGING_HEADER = MetalOfflineRecording/ShaderTypes.h; 300 | SWIFT_VERSION = 4.2; 301 | }; 302 | name = Debug; 303 | }; 304 | 8334EBE82172F75600398038 /* Release */ = { 305 | isa = XCBuildConfiguration; 306 | buildSettings = { 307 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 308 | CODE_SIGN_ENTITLEMENTS = MetalOfflineRecording/MetalOfflineRecording.entitlements; 309 | CODE_SIGN_STYLE = Automatic; 310 | COMBINE_HIDPI_IMAGES = YES; 311 | DEVELOPMENT_TEAM = ""; 312 | INFOPLIST_FILE = MetalOfflineRecording/Info.plist; 313 | LD_RUNPATH_SEARCH_PATHS = ( 314 | "$(inherited)", 315 | "@executable_path/../Frameworks", 316 | ); 317 | PRODUCT_BUNDLE_IDENTIFIER = com.metalbyexample.MetalOfflineRecording; 318 | PRODUCT_NAME = "$(TARGET_NAME)"; 319 | SWIFT_OBJC_BRIDGING_HEADER = MetalOfflineRecording/ShaderTypes.h; 320 | SWIFT_VERSION = 4.2; 321 | }; 322 | name = Release; 323 | }; 324 | /* End XCBuildConfiguration section */ 325 | 326 | /* Begin XCConfigurationList section */ 327 | 8334EBCC2172F75400398038 /* Build configuration list for PBXProject "MetalOfflineRecording" */ = { 328 | isa = XCConfigurationList; 329 | buildConfigurations = ( 330 | 8334EBE42172F75600398038 /* Debug */, 331 | 8334EBE52172F75600398038 /* Release */, 332 | ); 333 | defaultConfigurationIsVisible = 0; 334 | defaultConfigurationName = Release; 335 | }; 336 | 8334EBE62172F75600398038 /* Build configuration list for PBXNativeTarget "MetalOfflineRecording" */ = { 337 | isa = XCConfigurationList; 338 | buildConfigurations = ( 339 | 8334EBE72172F75600398038 /* Debug */, 340 | 8334EBE82172F75600398038 /* Release */, 341 | ); 342 | defaultConfigurationIsVisible = 0; 343 | defaultConfigurationName = Release; 344 | }; 345 | /* End XCConfigurationList section */ 346 | }; 347 | rootObject = 8334EBC92172F75400398038 /* Project object */; 348 | } 349 | -------------------------------------------------------------------------------- /MetalOfflineRecording.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /MetalOfflineRecording.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MetalOfflineRecording/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | import Cocoa 2 | 3 | @NSApplicationMain 4 | class AppDelegate: NSObject, NSApplicationDelegate { 5 | 6 | @IBOutlet weak var window: NSWindow! 7 | 8 | func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { 9 | return true 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /MetalOfflineRecording/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "mac", 5 | "size" : "16x16", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "mac", 10 | "size" : "16x16", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "mac", 15 | "size" : "32x32", 16 | "scale" : "1x" 17 | }, 18 | { 19 | "idiom" : "mac", 20 | "size" : "32x32", 21 | "scale" : "2x" 22 | }, 23 | { 24 | "idiom" : "mac", 25 | "size" : "128x128", 26 | "scale" : "1x" 27 | }, 28 | { 29 | "idiom" : "mac", 30 | "size" : "128x128", 31 | "scale" : "2x" 32 | }, 33 | { 34 | "idiom" : "mac", 35 | "size" : "256x256", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "mac", 40 | "size" : "256x256", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "mac", 45 | "size" : "512x512", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "mac", 50 | "size" : "512x512", 51 | "scale" : "2x" 52 | } 53 | ], 54 | "info" : { 55 | "version" : 1, 56 | "author" : "xcode" 57 | } 58 | } -------------------------------------------------------------------------------- /MetalOfflineRecording/Assets.xcassets/ColorMap.textureset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | }, 6 | "properties" : { 7 | "origin" : "bottom-left", 8 | "interpretation" : "non-premultiplied-colors" 9 | }, 10 | "textures" : [ 11 | { 12 | "idiom" : "universal", 13 | "filename" : "Universal.mipmapset" 14 | } 15 | ] 16 | } 17 | 18 | -------------------------------------------------------------------------------- /MetalOfflineRecording/Assets.xcassets/ColorMap.textureset/Universal.mipmapset/ColorMap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/warrenm/MetalOfflineRecording/b4ebaddc37950fd5d835ed60530e7f1905e6d293/MetalOfflineRecording/Assets.xcassets/ColorMap.textureset/Universal.mipmapset/ColorMap.png -------------------------------------------------------------------------------- /MetalOfflineRecording/Assets.xcassets/ColorMap.textureset/Universal.mipmapset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | }, 6 | "levels" : [ 7 | { 8 | "filename" : "ColorMap.png", 9 | "mipmap-level" : "base" 10 | } 11 | ] 12 | } 13 | -------------------------------------------------------------------------------- /MetalOfflineRecording/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /MetalOfflineRecording/Base.lproj/Main.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 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | Default 530 | 531 | 532 | 533 | 534 | 535 | 536 | Left to Right 537 | 538 | 539 | 540 | 541 | 542 | 543 | Right to Left 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | Default 555 | 556 | 557 | 558 | 559 | 560 | 561 | Left to Right 562 | 563 | 564 | 565 | 566 | 567 | 568 | Right to Left 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | -------------------------------------------------------------------------------- /MetalOfflineRecording/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIconFile 10 | 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | $(PRODUCT_NAME) 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleVersion 22 | 1 23 | LSMinimumSystemVersion 24 | $(MACOSX_DEPLOYMENT_TARGET) 25 | NSHumanReadableCopyright 26 | Copyright © 2018 Warren Moore. All rights reserved. 27 | NSMainStoryboardFile 28 | Main 29 | NSPrincipalClass 30 | NSApplication 31 | 32 | 33 | -------------------------------------------------------------------------------- /MetalOfflineRecording/MetalOfflineRecording.entitlements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | com.apple.security.app-sandbox 6 | 7 | com.apple.security.files.user-selected.read-only 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /MetalOfflineRecording/Renderer.swift: -------------------------------------------------------------------------------- 1 | 2 | import Metal 3 | import MetalKit 4 | import AVFoundation 5 | import simd 6 | 7 | let alignedUniformsSize = (MemoryLayout.size & ~0xFF) + 0x100 8 | 9 | let maxBuffersInFlight = 3 10 | 11 | class Renderer: NSObject { 12 | 13 | public let device: MTLDevice 14 | let commandQueue: MTLCommandQueue 15 | var dynamicUniformBuffer: MTLBuffer 16 | var pipelineState: MTLRenderPipelineState 17 | var depthState: MTLDepthStencilState 18 | var colorMap: MTLTexture 19 | 20 | let inFlightSemaphore = DispatchSemaphore(value: maxBuffersInFlight) 21 | 22 | var uniformBufferOffset = 0 23 | var uniformBufferIndex = 0 24 | var uniforms: UnsafeMutablePointer 25 | 26 | var projectionMatrix: matrix_float4x4 = matrix_float4x4() 27 | 28 | var rotation: Float = 0 29 | 30 | var mesh: MTKMesh 31 | 32 | init?(device: MTLDevice) { 33 | self.device = device 34 | self.commandQueue = self.device.makeCommandQueue()! 35 | 36 | let uniformBufferSize = alignedUniformsSize * maxBuffersInFlight 37 | 38 | self.dynamicUniformBuffer = self.device.makeBuffer(length:uniformBufferSize, 39 | options:[MTLResourceOptions.storageModeShared])! 40 | 41 | self.dynamicUniformBuffer.label = "UniformBuffer" 42 | 43 | uniforms = UnsafeMutableRawPointer(dynamicUniformBuffer.contents()).bindMemory(to:Uniforms.self, capacity:1) 44 | 45 | let mtlVertexDescriptor = Renderer.buildMetalVertexDescriptor() 46 | 47 | do { 48 | pipelineState = try Renderer.buildRenderPipelineWithDevice(device: device, 49 | mtlVertexDescriptor: mtlVertexDescriptor) 50 | } catch { 51 | print("Unable to compile render pipeline state. Error info: \(error)") 52 | return nil 53 | } 54 | 55 | let depthStateDesciptor = MTLDepthStencilDescriptor() 56 | depthStateDesciptor.depthCompareFunction = MTLCompareFunction.less 57 | depthStateDesciptor.isDepthWriteEnabled = true 58 | self.depthState = device.makeDepthStencilState(descriptor:depthStateDesciptor)! 59 | 60 | do { 61 | mesh = try Renderer.buildMesh(device: device, mtlVertexDescriptor: mtlVertexDescriptor) 62 | } catch { 63 | print("Unable to build MetalKit Mesh. Error info: \(error)") 64 | return nil 65 | } 66 | 67 | do { 68 | colorMap = try Renderer.loadTexture(device: device, textureName: "ColorMap") 69 | } catch { 70 | print("Unable to load texture. Error info: \(error)") 71 | return nil 72 | } 73 | 74 | super.init() 75 | 76 | } 77 | 78 | class func buildMetalVertexDescriptor() -> MTLVertexDescriptor { 79 | let mtlVertexDescriptor = MTLVertexDescriptor() 80 | 81 | mtlVertexDescriptor.attributes[VertexAttribute.position.rawValue].format = MTLVertexFormat.float3 82 | mtlVertexDescriptor.attributes[VertexAttribute.position.rawValue].offset = 0 83 | mtlVertexDescriptor.attributes[VertexAttribute.position.rawValue].bufferIndex = BufferIndex.meshPositions.rawValue 84 | 85 | mtlVertexDescriptor.attributes[VertexAttribute.texcoord.rawValue].format = MTLVertexFormat.float2 86 | mtlVertexDescriptor.attributes[VertexAttribute.texcoord.rawValue].offset = 0 87 | mtlVertexDescriptor.attributes[VertexAttribute.texcoord.rawValue].bufferIndex = BufferIndex.meshGenerics.rawValue 88 | 89 | mtlVertexDescriptor.layouts[BufferIndex.meshPositions.rawValue].stride = 12 90 | mtlVertexDescriptor.layouts[BufferIndex.meshPositions.rawValue].stepRate = 1 91 | mtlVertexDescriptor.layouts[BufferIndex.meshPositions.rawValue].stepFunction = MTLVertexStepFunction.perVertex 92 | 93 | mtlVertexDescriptor.layouts[BufferIndex.meshGenerics.rawValue].stride = 8 94 | mtlVertexDescriptor.layouts[BufferIndex.meshGenerics.rawValue].stepRate = 1 95 | mtlVertexDescriptor.layouts[BufferIndex.meshGenerics.rawValue].stepFunction = MTLVertexStepFunction.perVertex 96 | 97 | return mtlVertexDescriptor 98 | } 99 | 100 | class func buildRenderPipelineWithDevice(device: MTLDevice, 101 | mtlVertexDescriptor: MTLVertexDescriptor) throws -> MTLRenderPipelineState { 102 | let library = device.makeDefaultLibrary() 103 | 104 | let vertexFunction = library?.makeFunction(name: "vertexShader") 105 | let fragmentFunction = library?.makeFunction(name: "fragmentShader") 106 | 107 | let pipelineDescriptor = MTLRenderPipelineDescriptor() 108 | pipelineDescriptor.label = "RenderPipeline" 109 | pipelineDescriptor.sampleCount = 1 110 | pipelineDescriptor.vertexFunction = vertexFunction 111 | pipelineDescriptor.fragmentFunction = fragmentFunction 112 | pipelineDescriptor.vertexDescriptor = mtlVertexDescriptor 113 | 114 | pipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm 115 | pipelineDescriptor.depthAttachmentPixelFormat = .depth32Float 116 | 117 | return try device.makeRenderPipelineState(descriptor: pipelineDescriptor) 118 | } 119 | 120 | class func buildMesh(device: MTLDevice, 121 | mtlVertexDescriptor: MTLVertexDescriptor) throws -> MTKMesh { 122 | let metalAllocator = MTKMeshBufferAllocator(device: device) 123 | 124 | let mdlMesh = MDLMesh.newBox(withDimensions: float3(4, 4, 4), 125 | segments: uint3(2, 2, 2), 126 | geometryType: MDLGeometryType.triangles, 127 | inwardNormals:false, 128 | allocator: metalAllocator) 129 | 130 | let mdlVertexDescriptor = MTKModelIOVertexDescriptorFromMetal(mtlVertexDescriptor) 131 | 132 | guard let attributes = mdlVertexDescriptor.attributes as? [MDLVertexAttribute] else { 133 | fatalError("Invalid vertex descriptor") 134 | } 135 | attributes[VertexAttribute.position.rawValue].name = MDLVertexAttributePosition 136 | attributes[VertexAttribute.texcoord.rawValue].name = MDLVertexAttributeTextureCoordinate 137 | 138 | mdlMesh.vertexDescriptor = mdlVertexDescriptor 139 | 140 | return try MTKMesh(mesh:mdlMesh, device:device) 141 | } 142 | 143 | class func loadTexture(device: MTLDevice, 144 | textureName: String) throws -> MTLTexture { 145 | let textureLoader = MTKTextureLoader(device: device) 146 | 147 | let textureLoaderOptions = [ 148 | MTKTextureLoader.Option.textureUsage: NSNumber(value: MTLTextureUsage.shaderRead.rawValue), 149 | MTKTextureLoader.Option.textureStorageMode: NSNumber(value: MTLStorageMode.`private`.rawValue) 150 | ] 151 | 152 | return try textureLoader.newTexture(name: textureName, 153 | scaleFactor: 1.0, 154 | bundle: nil, 155 | options: textureLoaderOptions) 156 | 157 | } 158 | 159 | private func updateDynamicBufferState() { 160 | uniformBufferIndex = (uniformBufferIndex + 1) % maxBuffersInFlight 161 | 162 | uniformBufferOffset = alignedUniformsSize * uniformBufferIndex 163 | 164 | uniforms = UnsafeMutableRawPointer(dynamicUniformBuffer.contents() + uniformBufferOffset).bindMemory(to:Uniforms.self, capacity:1) 165 | } 166 | 167 | private func updateGameState(forTime time: Float) { 168 | uniforms[0].projectionMatrix = projectionMatrix 169 | 170 | let rotationAxis = float3(1, 1, 0) 171 | let modelMatrix = matrix4x4_rotation(radians: rotation, axis: rotationAxis) 172 | let viewMatrix = matrix4x4_translation(0.0, 0.0, -8.0) 173 | uniforms[0].modelViewMatrix = simd_mul(viewMatrix, modelMatrix) 174 | rotation = time 175 | } 176 | 177 | func renderMovie(size: CGSize, duration: TimeInterval, url: URL, completion: @escaping () -> Void) { 178 | 179 | projectionMatrix = matrix_perspective_right_hand(fovyRadians: .pi / 3, 180 | aspectRatio: Float(size.width/size.height), 181 | nearZ: 0.1, 182 | farZ: 100.0) 183 | 184 | let recorder = VideoRecorder(outputURL: url, size: size)! 185 | 186 | let textureDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .bgra8Unorm, 187 | width: Int(size.width), 188 | height: Int(size.height), 189 | mipmapped: false) 190 | textureDescriptor.usage = [ .renderTarget ] 191 | textureDescriptor.storageMode = .managed 192 | let renderBuffer = device.makeTexture(descriptor: textureDescriptor)! 193 | textureDescriptor.storageMode = .private 194 | textureDescriptor.pixelFormat = .depth32Float 195 | let depthBuffer = device.makeTexture(descriptor: textureDescriptor)! 196 | 197 | let framerate = 60.0 198 | let frameDelta = 1 / framerate 199 | 200 | recorder.startRecording() 201 | 202 | for t in stride(from: 0, through: duration, by: frameDelta) { 203 | self.draw(in: renderBuffer, depthTexture: depthBuffer, time: t) { (texture) in 204 | recorder.writeFrame(forTexture: texture, time: t) 205 | } 206 | } 207 | 208 | recorder.endRecording { 209 | completion() 210 | } 211 | } 212 | 213 | func draw(in texture: MTLTexture, depthTexture: MTLTexture, time: TimeInterval, completion: @escaping (MTLTexture) -> Void) { 214 | _ = inFlightSemaphore.wait(timeout: DispatchTime.distantFuture) 215 | 216 | if let commandBuffer = commandQueue.makeCommandBuffer() { 217 | let semaphore = inFlightSemaphore 218 | commandBuffer.addCompletedHandler { (_) in 219 | semaphore.signal() 220 | } 221 | 222 | commandBuffer.addCompletedHandler { (_) in 223 | completion(texture) 224 | } 225 | 226 | self.updateDynamicBufferState() 227 | 228 | self.updateGameState(forTime: Float(time)) 229 | 230 | let renderPassDescriptor = MTLRenderPassDescriptor() 231 | renderPassDescriptor.colorAttachments[0].texture = texture 232 | renderPassDescriptor.colorAttachments[0].loadAction = .clear 233 | renderPassDescriptor.colorAttachments[0].storeAction = .store 234 | renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 1) 235 | renderPassDescriptor.depthAttachment.texture = depthTexture 236 | renderPassDescriptor.depthAttachment.loadAction = .clear 237 | renderPassDescriptor.depthAttachment.storeAction = .dontCare 238 | renderPassDescriptor.depthAttachment.clearDepth = 1.0 239 | 240 | if let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor) { 241 | renderEncoder.label = "Primary Render Encoder" 242 | 243 | renderEncoder.pushDebugGroup("Draw Box") 244 | 245 | renderEncoder.setCullMode(.back) 246 | renderEncoder.setFrontFacing(.counterClockwise) 247 | renderEncoder.setRenderPipelineState(pipelineState) 248 | renderEncoder.setDepthStencilState(depthState) 249 | 250 | renderEncoder.setVertexBuffer(dynamicUniformBuffer, offset:uniformBufferOffset, index: BufferIndex.uniforms.rawValue) 251 | renderEncoder.setFragmentBuffer(dynamicUniformBuffer, offset:uniformBufferOffset, index: BufferIndex.uniforms.rawValue) 252 | 253 | for (index, element) in mesh.vertexDescriptor.layouts.enumerated() { 254 | guard let layout = element as? MDLVertexBufferLayout else { 255 | return 256 | } 257 | 258 | if layout.stride != 0 { 259 | let buffer = mesh.vertexBuffers[index] 260 | renderEncoder.setVertexBuffer(buffer.buffer, offset:buffer.offset, index: index) 261 | } 262 | } 263 | 264 | renderEncoder.setFragmentTexture(colorMap, index: TextureIndex.color.rawValue) 265 | 266 | for submesh in mesh.submeshes { 267 | renderEncoder.drawIndexedPrimitives(type: submesh.primitiveType, 268 | indexCount: submesh.indexCount, 269 | indexType: submesh.indexType, 270 | indexBuffer: submesh.indexBuffer.buffer, 271 | indexBufferOffset: submesh.indexBuffer.offset) 272 | 273 | } 274 | 275 | renderEncoder.popDebugGroup() 276 | 277 | renderEncoder.endEncoding() 278 | } 279 | 280 | let copybackEncoder = commandBuffer.makeBlitCommandEncoder()! 281 | copybackEncoder.synchronize(resource: texture) 282 | copybackEncoder.endEncoding() 283 | 284 | commandBuffer.commit() 285 | } 286 | } 287 | 288 | } 289 | 290 | func matrix4x4_rotation(radians: Float, axis: float3) -> matrix_float4x4 { 291 | let unitAxis = normalize(axis) 292 | let ct = cosf(radians) 293 | let st = sinf(radians) 294 | let ci = 1 - ct 295 | let x = unitAxis.x, y = unitAxis.y, z = unitAxis.z 296 | return matrix_float4x4.init(columns:(vector_float4( ct + x * x * ci, y * x * ci + z * st, z * x * ci - y * st, 0), 297 | vector_float4(x * y * ci - z * st, ct + y * y * ci, z * y * ci + x * st, 0), 298 | vector_float4(x * z * ci + y * st, y * z * ci - x * st, ct + z * z * ci, 0), 299 | vector_float4( 0, 0, 0, 1))) 300 | } 301 | 302 | func matrix4x4_translation(_ translationX: Float, _ translationY: Float, _ translationZ: Float) -> matrix_float4x4 { 303 | return matrix_float4x4.init(columns:(vector_float4(1, 0, 0, 0), 304 | vector_float4(0, 1, 0, 0), 305 | vector_float4(0, 0, 1, 0), 306 | vector_float4(translationX, translationY, translationZ, 1))) 307 | } 308 | 309 | func matrix_perspective_right_hand(fovyRadians fovy: Float, aspectRatio: Float, nearZ: Float, farZ: Float) -> matrix_float4x4 { 310 | let ys = 1 / tanf(fovy * 0.5) 311 | let xs = ys / aspectRatio 312 | let zs = farZ / (nearZ - farZ) 313 | return matrix_float4x4.init(columns:(vector_float4(xs, 0, 0, 0), 314 | vector_float4( 0, ys, 0, 0), 315 | vector_float4( 0, 0, zs, -1), 316 | vector_float4( 0, 0, zs * nearZ, 0))) 317 | } 318 | 319 | func radians_from_degrees(_ degrees: Float) -> Float { 320 | return (degrees / 180) * .pi 321 | } 322 | -------------------------------------------------------------------------------- /MetalOfflineRecording/ShaderTypes.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef ShaderTypes_h 3 | #define ShaderTypes_h 4 | 5 | #ifdef __METAL_VERSION__ 6 | #define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type 7 | #define NSInteger metal::int32_t 8 | #else 9 | #import 10 | #endif 11 | 12 | #include 13 | 14 | typedef NS_ENUM(NSInteger, BufferIndex) 15 | { 16 | BufferIndexMeshPositions = 0, 17 | BufferIndexMeshGenerics = 1, 18 | BufferIndexUniforms = 2 19 | }; 20 | 21 | typedef NS_ENUM(NSInteger, VertexAttribute) 22 | { 23 | VertexAttributePosition = 0, 24 | VertexAttributeTexcoord = 1, 25 | }; 26 | 27 | typedef NS_ENUM(NSInteger, TextureIndex) 28 | { 29 | TextureIndexColor = 0, 30 | }; 31 | 32 | typedef struct 33 | { 34 | matrix_float4x4 projectionMatrix; 35 | matrix_float4x4 modelViewMatrix; 36 | } Uniforms; 37 | 38 | #endif 39 | 40 | -------------------------------------------------------------------------------- /MetalOfflineRecording/Shaders.metal: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | #import "ShaderTypes.h" 6 | 7 | using namespace metal; 8 | 9 | typedef struct 10 | { 11 | float3 position [[attribute(VertexAttributePosition)]]; 12 | float2 texCoord [[attribute(VertexAttributeTexcoord)]]; 13 | } Vertex; 14 | 15 | typedef struct 16 | { 17 | float4 position [[position]]; 18 | float2 texCoord; 19 | } ColorInOut; 20 | 21 | vertex ColorInOut vertexShader(Vertex in [[stage_in]], 22 | constant Uniforms & uniforms [[ buffer(BufferIndexUniforms) ]]) 23 | { 24 | ColorInOut out; 25 | 26 | float4 position = float4(in.position, 1.0); 27 | out.position = uniforms.projectionMatrix * uniforms.modelViewMatrix * position; 28 | out.texCoord = in.texCoord; 29 | 30 | return out; 31 | } 32 | 33 | fragment float4 fragmentShader(ColorInOut in [[stage_in]], 34 | constant Uniforms & uniforms [[ buffer(BufferIndexUniforms) ]], 35 | texture2d colorMap [[ texture(TextureIndexColor) ]]) 36 | { 37 | constexpr sampler colorSampler(mip_filter::linear, 38 | mag_filter::linear, 39 | min_filter::linear); 40 | 41 | half4 colorSample = colorMap.sample(colorSampler, in.texCoord.xy); 42 | 43 | return float4(colorSample); 44 | } 45 | -------------------------------------------------------------------------------- /MetalOfflineRecording/VideoRecorder.swift: -------------------------------------------------------------------------------- 1 | 2 | import Metal 3 | import AVFoundation 4 | 5 | class VideoRecorder { 6 | var isRecording = false 7 | 8 | private var assetWriter: AVAssetWriter 9 | private var assetWriterVideoInput: AVAssetWriterInput 10 | private var assetWriterPixelBufferInput: AVAssetWriterInputPixelBufferAdaptor 11 | 12 | init?(outputURL url: URL, size: CGSize) { 13 | do { 14 | assetWriter = try AVAssetWriter(outputURL: url, fileType: .m4v) 15 | } catch { 16 | return nil 17 | } 18 | 19 | let outputSettings: [String: Any] = [ AVVideoCodecKey : AVVideoCodecType.h264, 20 | AVVideoWidthKey : size.width, 21 | AVVideoHeightKey : size.height ] 22 | 23 | assetWriterVideoInput = AVAssetWriterInput(mediaType: .video, outputSettings: outputSettings) 24 | assetWriterVideoInput.expectsMediaDataInRealTime = false 25 | 26 | let sourcePixelBufferAttributes: [String: Any] = [ 27 | kCVPixelBufferPixelFormatTypeKey as String : kCVPixelFormatType_32BGRA, 28 | kCVPixelBufferWidthKey as String : size.width, 29 | kCVPixelBufferHeightKey as String : size.height ] 30 | 31 | assetWriterPixelBufferInput = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: assetWriterVideoInput, 32 | sourcePixelBufferAttributes: sourcePixelBufferAttributes) 33 | 34 | assetWriter.add(assetWriterVideoInput) 35 | } 36 | 37 | func startRecording() { 38 | let result = assetWriter.startWriting() 39 | assert(result) 40 | assetWriter.startSession(atSourceTime: CMTime.zero) 41 | 42 | isRecording = true 43 | } 44 | 45 | func endRecording(_ completionHandler: @escaping () -> ()) { 46 | isRecording = false 47 | 48 | assetWriterVideoInput.markAsFinished() 49 | assetWriter.finishWriting(completionHandler: completionHandler) 50 | } 51 | 52 | func writeFrame(forTexture texture: MTLTexture, time: TimeInterval) { 53 | if !isRecording { 54 | return 55 | } 56 | 57 | while !assetWriterVideoInput.isReadyForMoreMediaData {} 58 | 59 | guard let pixelBufferPool = assetWriterPixelBufferInput.pixelBufferPool else { 60 | print("Pixel buffer asset writer input did not have a pixel buffer pool available; cannot retrieve frame") 61 | return 62 | } 63 | 64 | var maybePixelBuffer: CVPixelBuffer? = nil 65 | let status = CVPixelBufferPoolCreatePixelBuffer(nil, pixelBufferPool, &maybePixelBuffer) 66 | if status != kCVReturnSuccess { 67 | print("Could not get pixel buffer from asset writer input; dropping frame...") 68 | return 69 | } 70 | 71 | guard let pixelBuffer = maybePixelBuffer else { return } 72 | 73 | CVPixelBufferLockBaseAddress(pixelBuffer, []) 74 | let pixelBufferBytes = CVPixelBufferGetBaseAddress(pixelBuffer)! 75 | 76 | // Use the bytes per row value from the pixel buffer since its stride may be rounded up to be 16-byte aligned 77 | let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer) 78 | let region = MTLRegionMake2D(0, 0, texture.width, texture.height) 79 | 80 | texture.getBytes(pixelBufferBytes, bytesPerRow: bytesPerRow, from: region, mipmapLevel: 0) 81 | 82 | let presentationTime = CMTimeMakeWithSeconds(time, preferredTimescale: 240) 83 | assetWriterPixelBufferInput.append(pixelBuffer, withPresentationTime: presentationTime) 84 | 85 | CVPixelBufferUnlockBaseAddress(pixelBuffer, []) 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /MetalOfflineRecording/ViewController.swift: -------------------------------------------------------------------------------- 1 | 2 | import Cocoa 3 | 4 | class ViewController: NSViewController { 5 | 6 | var renderer: Renderer! 7 | 8 | override func viewDidLoad() { 9 | super.viewDidLoad() 10 | 11 | guard let defaultDevice = MTLCreateSystemDefaultDevice() else { 12 | print("Metal is not supported on this device") 13 | return 14 | } 15 | 16 | guard let newRenderer = Renderer(device: defaultDevice) else { 17 | print("Renderer cannot be initialized") 18 | return 19 | } 20 | 21 | renderer = newRenderer 22 | 23 | let dataURLString = NSString(string: "~/").expandingTildeInPath 24 | let movieURL = URL(fileURLWithPath: "movie.m4v", relativeTo: URL(fileURLWithPath: dataURLString)) 25 | try? FileManager.default.removeItem(at: movieURL) 26 | 27 | renderer.renderMovie(size: CGSize(width: 800, height: 600), duration: 6.3, url: movieURL) { 28 | DispatchQueue.main.async { 29 | NSWorkspace.shared.activateFileViewerSelecting([movieURL.absoluteURL]) 30 | self.view.window?.close() 31 | } 32 | } 33 | } 34 | } 35 | --------------------------------------------------------------------------------