From f7b92586eabb5e774d9a0cce8903c9a1954ec55a Mon Sep 17 00:00:00 2001 From: Peter Huang Date: Wed, 26 Sep 2018 22:48:36 +0800 Subject: [PATCH 1/3] Update for Swift 4.2 --- MetalScope.xcodeproj/project.pbxproj | 4 ++-- Sources/DeviceOrientationProvider.swift | 2 +- Sources/InterfaceOrientationUpdater.swift | 14 +++++++------- Sources/RenderLoop.swift | 2 +- Sources/StereoViewController.swift | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/MetalScope.xcodeproj/project.pbxproj b/MetalScope.xcodeproj/project.pbxproj index 7d830cd..11fe72b 100644 --- a/MetalScope.xcodeproj/project.pbxproj +++ b/MetalScope.xcodeproj/project.pbxproj @@ -471,7 +471,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.2; }; name = Debug; }; @@ -492,7 +492,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "com.eje-c.MetalScope"; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.2; }; name = Release; }; diff --git a/Sources/DeviceOrientationProvider.swift b/Sources/DeviceOrientationProvider.swift index 0effb16..63aba48 100644 --- a/Sources/DeviceOrientationProvider.swift +++ b/Sources/DeviceOrientationProvider.swift @@ -27,7 +27,7 @@ extension DeviceOrientationProvider { let queue = DispatchQueue(label: "com.eje-c.MetalScope.DeviceOrientationProvider.waitingQueue") let timer = DispatchSource.makeTimerSource(queue: queue) - timer.scheduleRepeating(deadline: .now(), interval: .milliseconds(10)) + timer.schedule(deadline: .now(), repeating: .milliseconds(10)) timer.setEventHandler { guard let _ = self.deviceOrientation(atTime: time) else { return diff --git a/Sources/InterfaceOrientationUpdater.swift b/Sources/InterfaceOrientationUpdater.swift index f7d3873..b76d438 100644 --- a/Sources/InterfaceOrientationUpdater.swift +++ b/Sources/InterfaceOrientationUpdater.swift @@ -53,7 +53,7 @@ internal final class InterfaceOrientationUpdater { UIDevice.current.beginGeneratingDeviceOrientationNotifications() - let observer = NotificationCenter.default.addObserver(forName: .UIDeviceOrientationDidChange, object: nil, queue: .main) { [weak self] _ in + let observer = NotificationCenter.default.addObserver(forName: UIDevice.orientationDidChangeNotification, object: nil, queue: .main) { [weak self] _ in guard UIDevice.current.orientation.isValidInterfaceOrientation, self?.isTransitioning == false else { return } @@ -76,21 +76,21 @@ internal final class InterfaceOrientationUpdater { } } -private extension UIViewAnimationCurve { +private extension UIView.AnimationCurve { var caMediaTimingFunction: CAMediaTimingFunction { let name: String switch self { case .easeIn: - name = kCAMediaTimingFunctionEaseIn + name = CAMediaTimingFunctionName.easeIn.rawValue case .easeOut: - name = kCAMediaTimingFunctionEaseOut + name = CAMediaTimingFunctionName.easeOut.rawValue case .easeInOut: - name = kCAMediaTimingFunctionEaseInEaseOut + name = CAMediaTimingFunctionName.easeInEaseOut.rawValue case .linear: - name = kCAMediaTimingFunctionLinear + name = CAMediaTimingFunctionName.linear.rawValue } - return CAMediaTimingFunction(name: name) + return CAMediaTimingFunction(name: CAMediaTimingFunctionName(rawValue: name)) } } diff --git a/Sources/RenderLoop.swift b/Sources/RenderLoop.swift index f95f829..5da6eec 100644 --- a/Sources/RenderLoop.swift +++ b/Sources/RenderLoop.swift @@ -19,7 +19,7 @@ public final class RenderLoop { private lazy var displayLink: CADisplayLink = { let link = CADisplayLink(target: self, selector: #selector(handleDisplayLink(_:))) - link.add(to: .main, forMode: .commonModes) + link.add(to: .main, forMode: RunLoop.Mode.common) link.isPaused = true return link }() diff --git a/Sources/StereoViewController.swift b/Sources/StereoViewController.swift index 71cb12a..9b2783f 100644 --- a/Sources/StereoViewController.swift +++ b/Sources/StereoViewController.swift @@ -344,7 +344,7 @@ open class StereoViewController: UIViewController, SceneLoadable { precondition(introductionViewUpdateTimer == nil) let timer = DispatchSource.makeTimerSource(queue: .main) - timer.scheduleRepeating(deadline: .now() + delay, interval: interval) + timer.schedule(deadline: .now() + delay, repeating: interval) timer.setEventHandler { [weak self] in guard self?.isViewLoaded == true, let _ = self?.introductionView else { return From 1a6f470ba4393bfa973b73acb0d668a70b74492b Mon Sep 17 00:00:00 2001 From: Peter Huang Date: Thu, 27 Sep 2018 11:01:58 +0800 Subject: [PATCH 2/3] Update project settings and deployment target --- MetalScope.xcodeproj/project.pbxproj | 14 +++++++++++--- .../xcshareddata/xcschemes/MetalScope.xcscheme | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/MetalScope.xcodeproj/project.pbxproj b/MetalScope.xcodeproj/project.pbxproj index 11fe72b..45d0d19 100644 --- a/MetalScope.xcodeproj/project.pbxproj +++ b/MetalScope.xcodeproj/project.pbxproj @@ -258,7 +258,7 @@ BFB8916F1E2E013400FA9129 /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0900; + LastUpgradeCheck = 1000; ORGANIZATIONNAME = "eje Inc."; TargetAttributes = { BFB891771E2E013400FA9129 = { @@ -355,6 +355,7 @@ CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_DOCUMENTATION_COMMENTS = YES; CLANG_WARN_EMPTY_BODY = YES; @@ -362,6 +363,7 @@ CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; @@ -389,12 +391,13 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 9.0; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 4.2; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -414,6 +417,7 @@ CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_DOCUMENTATION_COMMENTS = YES; CLANG_WARN_EMPTY_BODY = YES; @@ -421,6 +425,7 @@ CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; @@ -442,10 +447,11 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 9.0; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 4.2; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; VERSIONING_SYSTEM = "apple-generic"; @@ -466,6 +472,7 @@ DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = Sources/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.eje-c.MetalScope"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -488,6 +495,7 @@ DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = Sources/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.eje-c.MetalScope"; PRODUCT_NAME = "$(TARGET_NAME)"; diff --git a/MetalScope.xcodeproj/xcshareddata/xcschemes/MetalScope.xcscheme b/MetalScope.xcodeproj/xcshareddata/xcschemes/MetalScope.xcscheme index acbc580..c6f619d 100644 --- a/MetalScope.xcodeproj/xcshareddata/xcschemes/MetalScope.xcscheme +++ b/MetalScope.xcodeproj/xcshareddata/xcschemes/MetalScope.xcscheme @@ -1,6 +1,6 @@ Date: Thu, 27 Sep 2018 11:20:03 +0800 Subject: [PATCH 3/3] Fix errors --- Sources/PlayerItemRenderer.swift | 7 ++++--- Sources/StereoRenderer.swift | 14 ++++++++++---- Sources/StereoScene.swift | 2 +- Sources/StereoView.swift | 5 ++++- Sources/VideoScene.swift | 29 +++++++++++++++++------------ 5 files changed, 36 insertions(+), 21 deletions(-) diff --git a/Sources/PlayerItemRenderer.swift b/Sources/PlayerItemRenderer.swift index f422bd8..ddcdb1e 100644 --- a/Sources/PlayerItemRenderer.swift +++ b/Sources/PlayerItemRenderer.swift @@ -120,9 +120,10 @@ public final class PlayerItemRenderer { let sourceSize = MTLSizeMake(sourceTexture.width, sourceTexture.height, sourceTexture.depth) let destinationOrigin = MTLOriginMake(0, 0, 0) - let blitCommandEncoder = commandBuffer.makeBlitCommandEncoder() - blitCommandEncoder.copy(from: sourceTexture, sourceSlice: 0, sourceLevel: 0, sourceOrigin: sourceOrigin, sourceSize: sourceSize, to: texture, destinationSlice: 0, destinationLevel: 0, destinationOrigin: destinationOrigin) - blitCommandEncoder.endEncoding() + if let blitCommandEncoder = commandBuffer.makeBlitCommandEncoder() { + blitCommandEncoder.copy(from: sourceTexture, sourceSlice: 0, sourceLevel: 0, sourceOrigin: sourceOrigin, sourceSize: sourceSize, to: texture, destinationSlice: 0, destinationLevel: 0, destinationOrigin: destinationOrigin) + blitCommandEncoder.endEncoding() + } } public func hasNewPixelBuffer(atHostTime time: TimeInterval) -> Bool { diff --git a/Sources/StereoRenderer.swift b/Sources/StereoRenderer.swift index 3fd4208..2ef5723 100644 --- a/Sources/StereoRenderer.swift +++ b/Sources/StereoRenderer.swift @@ -51,9 +51,13 @@ internal final class StereoRenderer { ) eyeTextureDescriptor.usage = .renderTarget + guard let texture = device.makeTexture(descriptor: eyeTextureDescriptor) else { + fatalError("Failed to makeTexture") + } + eyeRenderingConfigurations = [ - .left: EyeRenderingConfiguration(texture: device.makeTexture(descriptor: eyeTextureDescriptor)), - .right: EyeRenderingConfiguration(texture: device.makeTexture(descriptor: eyeTextureDescriptor)) + .left: EyeRenderingConfiguration(texture: texture), + .right: EyeRenderingConfiguration(texture: texture) ] } @@ -75,7 +79,10 @@ internal final class StereoRenderer { for (eye, configuration) in eyeRenderingConfigurations { semaphore.wait() - let commandBuffer = commandQueue.makeCommandBuffer() + guard + let commandBuffer = commandQueue.makeCommandBuffer(), + let blitCommandEncoder = commandBuffer.makeBlitCommandEncoder() + else { return } rendererDelegateProxy.currentRenderingEye = eye @@ -99,7 +106,6 @@ internal final class StereoRenderer { destinationOrigin = MTLOrigin(x: outputTexture.width / 2, y: 0, z: 0) } - let blitCommandEncoder = commandBuffer.makeBlitCommandEncoder() blitCommandEncoder.copy( from: texture, sourceSlice: 0, diff --git a/Sources/StereoScene.swift b/Sources/StereoScene.swift index 105ea48..bd11742 100644 --- a/Sources/StereoScene.swift +++ b/Sources/StereoScene.swift @@ -57,7 +57,7 @@ internal final class StereoScene: SCNScene { let mesh = SCNGeometry( sources: [ - SCNGeometrySource(vertices: vertices, count: vertices.count), + SCNGeometrySource(vertices: vertices), SCNGeometrySource(texcoord: texcoord), SCNGeometrySource(colors: colors) ], diff --git a/Sources/StereoView.swift b/Sources/StereoView.swift index 1819d76..6ae5829 100644 --- a/Sources/StereoView.swift +++ b/Sources/StereoView.swift @@ -144,7 +144,10 @@ public final class StereoView: UIView, SceneLoadable { height: Int(textureSize.height), mipmapped: true ) - let texture = device.makeTexture(descriptor: textureDescriptor) + + guard let texture = device.makeTexture(descriptor: textureDescriptor) else { + fatalError("Failed to makeTexture") + } self.init(stereoTexture: texture) diff --git a/Sources/VideoScene.swift b/Sources/VideoScene.swift index 00fc5b9..7327cbd 100644 --- a/Sources/VideoScene.swift +++ b/Sources/VideoScene.swift @@ -75,7 +75,10 @@ public final class MonoSphericalVideoScene: MonoSphericalMediaScene, VideoScene public init(renderer: PlayerRenderer) { self.renderer = renderer - commandQueue = renderer.device.makeCommandQueue() + guard let commandQueue = renderer.device.makeCommandQueue() else { + fatalError("Failed to makeCommandQueue()") + } + self.commandQueue = commandQueue super.init() renderLoop.resume() } @@ -110,12 +113,12 @@ public final class MonoSphericalVideoScene: MonoSphericalMediaScene, VideoScene updateTextureIfNeeded() - guard let texture = playerTexture else { - return - } + guard + let texture = playerTexture, + let commandBuffer = (commandQueue ?? self.commandQueue).makeCommandBuffer() + else { return } do { - let commandBuffer = (commandQueue ?? self.commandQueue).makeCommandBuffer() try renderer.render(atHostTime: time, to: texture, commandBuffer: commandBuffer) commandBuffer.commit() } catch let error as CVError { @@ -163,7 +166,10 @@ public final class StereoSphericalVideoScene: StereoSphericalMediaScene, VideoSc public init(renderer: PlayerRenderer) { self.renderer = renderer - commandQueue = renderer.device.makeCommandQueue() + guard let commandQueue = renderer.device.makeCommandQueue() else { + fatalError("Failed to makeCommandQueue()") + } + self.commandQueue = commandQueue super.init() renderLoop.resume() } @@ -202,17 +208,16 @@ public final class StereoSphericalVideoScene: StereoSphericalMediaScene, VideoSc updateTexturesIfNeeded() - guard let playerTexture = playerTexture else { - return - } - - let commandBuffer = (commandQueue ?? self.commandQueue).makeCommandBuffer() + guard + let playerTexture = playerTexture, + let commandBuffer = (commandQueue ?? self.commandQueue).makeCommandBuffer(), + let blitCommandEncoder = commandBuffer.makeBlitCommandEncoder() + else { return } do { try renderer.render(atHostTime: time, to: playerTexture, commandBuffer: commandBuffer) func copyPlayerTexture(region: MTLRegion, to sphereTexture: MTLTexture) { - let blitCommandEncoder = commandBuffer.makeBlitCommandEncoder() blitCommandEncoder.copy( from: playerTexture, sourceSlice: 0,