From a38397ade47904c22dd6e889fdfd4f692160306d Mon Sep 17 00:00:00 2001 From: Ian Clawson Date: Thu, 12 Jan 2023 23:38:39 -0700 Subject: [PATCH] [feat] always use best available GL API --- DeltaCore.xcodeproj/project.pbxproj | 6 +++- .../Video/OpenGLESProcessor.swift | 2 +- .../Emulator Core/Video/VideoManager.swift | 2 +- .../EAGLContext+BestAvailable.swift | 35 +++++++++++++++++++ DeltaCore/UI/Game/GameView.swift | 6 ++-- 5 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 DeltaCore/Extensions/EAGLContext+BestAvailable.swift diff --git a/DeltaCore.xcodeproj/project.pbxproj b/DeltaCore.xcodeproj/project.pbxproj index 0f5490f..0c24d8d 100644 --- a/DeltaCore.xcodeproj/project.pbxproj +++ b/DeltaCore.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 37AA165F297133080023E041 /* EAGLContext+BestAvailable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AA165E297133080023E041 /* EAGLContext+BestAvailable.swift */; }; BF0B260B23E91745007BE38B /* Bundle+Resources.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF0B260A23E91745007BE38B /* Bundle+Resources.swift */; }; BF0BC4FA225C138A000151C6 /* BitmapProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF0BC4F9225C138A000151C6 /* BitmapProcessor.swift */; }; BF0BC4FD225C15E8000151C6 /* OpenGLESProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF0BC4FC225C15E8000151C6 /* OpenGLESProcessor.swift */; }; @@ -100,6 +101,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 37AA165E297133080023E041 /* EAGLContext+BestAvailable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "EAGLContext+BestAvailable.swift"; sourceTree = ""; }; BF0B260A23E91745007BE38B /* Bundle+Resources.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Bundle+Resources.swift"; sourceTree = ""; }; BF0BC4F9225C138A000151C6 /* BitmapProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BitmapProcessor.swift; sourceTree = ""; }; BF0BC4FC225C15E8000151C6 /* OpenGLESProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenGLESProcessor.swift; sourceTree = ""; }; @@ -380,6 +382,7 @@ BF0B260A23E91745007BE38B /* Bundle+Resources.swift */, D5537CF92887558B00A66016 /* UIScene+KeyboardFocus.swift */, D5777511289882F5005E8B05 /* UIWindowScene+StageManager.swift */, + 37AA165E297133080023E041 /* EAGLContext+BestAvailable.swift */, ); path = Extensions; sourceTree = ""; @@ -543,7 +546,7 @@ }; BFCBD91320CC6F6F0028E309 /* ZIPFoundationTests.xctest */ = { isa = PBXReferenceProxy; - fileType = file; + fileType = wrapper.cfbundle; path = ZIPFoundationTests.xctest; remoteRef = BFCBD91220CC6F6F0028E309 /* PBXContainerItemProxy */; sourceTree = BUILT_PRODUCTS_DIR; @@ -588,6 +591,7 @@ BFE438121AF60CDA0095A5F1 /* ControllerView.swift in Sources */, BFAAB95B1C2780C40001B7C4 /* ControllerDebugView.swift in Sources */, BFA5FCF620D3804400A3C5AB /* UIResponder+FirstResponder.swift in Sources */, + 37AA165F297133080023E041 /* EAGLContext+BestAvailable.swift in Sources */, BF1FB1891C5EEC14007E2494 /* SaveState.swift in Sources */, BF5E0D9125AE67AD00A3E4A8 /* RenderThread.swift in Sources */, BF4689821AAD703500A2586D /* GameProtocol.swift in Sources */, diff --git a/DeltaCore/Emulator Core/Video/OpenGLESProcessor.swift b/DeltaCore/Emulator Core/Video/OpenGLESProcessor.swift index 3eb1f64..4c90781 100644 --- a/DeltaCore/Emulator Core/Video/OpenGLESProcessor.swift +++ b/DeltaCore/Emulator Core/Video/OpenGLESProcessor.swift @@ -35,7 +35,7 @@ class OpenGLESProcessor: VideoProcessor init(videoFormat: VideoFormat, context: EAGLContext) { self.videoFormat = videoFormat - self.context = EAGLContext(api: .openGLES2, sharegroup: context.sharegroup)! + self.context = EAGLContext.createWithBestAvailableAPI(context.sharegroup) } deinit diff --git a/DeltaCore/Emulator Core/Video/VideoManager.swift b/DeltaCore/Emulator Core/Video/VideoManager.swift index b81639d..94cf942 100644 --- a/DeltaCore/Emulator Core/Video/VideoManager.swift +++ b/DeltaCore/Emulator Core/Video/VideoManager.swift @@ -65,7 +65,7 @@ public class VideoManager: NSObject, VideoRendering public init(videoFormat: VideoFormat) { self.videoFormat = videoFormat - self.context = EAGLContext(api: .openGLES2)! + self.context = EAGLContext.createWithBestAvailableAPI() self.ciContext = CIContext(eaglContext: self.context, options: [.workingColorSpace: NSNull()]) switch videoFormat.format diff --git a/DeltaCore/Extensions/EAGLContext+BestAvailable.swift b/DeltaCore/Extensions/EAGLContext+BestAvailable.swift new file mode 100644 index 0000000..89377a0 --- /dev/null +++ b/DeltaCore/Extensions/EAGLContext+BestAvailable.swift @@ -0,0 +1,35 @@ +// +// EAGLContext+BestAvailable.swift +// DeltaCore +// +// Created by Ian Clawson on 1/12/23. +// Copyright © 2023 Riley Testut. All rights reserved. +// + +import GLKit + +extension EAGLContext { + static func createWithBestAvailableAPI(_ sharegroup: EAGLSharegroup? = nil) -> EAGLContext { + if let sharegroup = sharegroup { + var context = EAGLContext(api: .openGLES3, sharegroup: sharegroup) + if context == nil { + context = EAGLContext(api: .openGLES2, sharegroup: sharegroup) + if context == nil { + context = EAGLContext(api: .openGLES1, sharegroup: sharegroup) + } + } + + return context! + } + + var context = EAGLContext(api: .openGLES3) + if context == nil { + context = EAGLContext(api: .openGLES2) + if context == nil { + context = EAGLContext(api: .openGLES1) + } + } + + return context! + } +} diff --git a/DeltaCore/UI/Game/GameView.swift b/DeltaCore/UI/Game/GameView.swift index 367d007..cf0e6a2 100644 --- a/DeltaCore/UI/Game/GameView.swift +++ b/DeltaCore/UI/Game/GameView.swift @@ -93,7 +93,7 @@ public class GameView: UIView // to self.glkView may crash if we've already rendered to a game view. EAGLContext.setCurrent(nil) - self.glkView.context = EAGLContext(api: .openGLES2, sharegroup: newValue.sharegroup)! + self.glkView.context = EAGLContext.createWithBestAvailableAPI(newValue.sharegroup) self.context = self.makeContext() DispatchQueue.main.async { @@ -112,7 +112,7 @@ public class GameView: UIView public override init(frame: CGRect) { - let eaglContext = EAGLContext(api: .openGLES2)! + let eaglContext = EAGLContext.createWithBestAvailableAPI() self.glkView = GLKView(frame: CGRect.zero, context: eaglContext) super.init(frame: frame) @@ -122,7 +122,7 @@ public class GameView: UIView public required init?(coder aDecoder: NSCoder) { - let eaglContext = EAGLContext(api: .openGLES2)! + let eaglContext = EAGLContext.createWithBestAvailableAPI() self.glkView = GLKView(frame: CGRect.zero, context: eaglContext) super.init(coder: aDecoder)