Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MetalANGLE on UIKit (OpenGL ES to Metal API translation layer) #4333

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Xcode/SDL/SDL.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10037,6 +10037,7 @@
DYLIB_CURRENT_VERSION = 15.1.0;
DYLIB_INSTALL_NAME_BASE = "@rpath";
ENABLE_STRICT_OBJC_MSGSEND = YES;
FRAMEWORK_SEARCH_PATHS = "./third-party/frameworks";
GCC_ALTIVEC_EXTENSIONS = YES;
GCC_AUTO_VECTORIZATION = YES;
GCC_ENABLE_SSE3_EXTENSIONS = YES;
Expand Down Expand Up @@ -10121,6 +10122,7 @@
DYLIB_INSTALL_NAME_BASE = "@rpath";
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
FRAMEWORK_SEARCH_PATHS = "./third-party/frameworks";
GCC_ALTIVEC_EXTENSIONS = YES;
GCC_AUTO_VECTORIZATION = YES;
GCC_ENABLE_SSE3_EXTENSIONS = YES;
Expand Down
1 change: 1 addition & 0 deletions Xcode/SDL/third-party/frameworks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.framework
7 changes: 7 additions & 0 deletions Xcode/SDL/third-party/frameworks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Why this folder is here?

SDL Xcode project is using this folder for managing third-party components (like external Frameworks) that are needed during the build phase.

### Usage:

If you're building **SDL** with **MetalANGLE** support, just copy + paste your `MetalANGLE.framework` here.
1 change: 1 addition & 0 deletions include/SDL_config_iphoneos.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
#define SDL_VIDEO_OPENGL_ES 1
#define SDL_VIDEO_RENDER_OGL_ES 1
#define SDL_VIDEO_RENDER_OGL_ES2 1
#define SDL_VIDEO_OPENGL_METALANGLE 0
#endif

/* Metal supported on 64-bit devices running iOS 8.0 and tvOS 9.0 and newer
Expand Down
9 changes: 9 additions & 0 deletions src/video/SDL_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,18 @@

/* GL and GLES2 headers conflict on Linux 32 bits */
#if SDL_VIDEO_OPENGL_ES2 && !SDL_VIDEO_OPENGL
#if SDL_VIDEO_OPENGL_METALANGLE
#include <MetalANGLE/GLES2/gl2.h>
Copy link
Contributor

@slime73 slime73 Apr 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these headers needed here if the code is changed to keep SDL_VIDEO_OPENGL_ES2 defined instead of making it mutually exclusive with the metal-angle backend?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be merged inside the SDL_opengles2.h file, with a #if SDL_VIDEO_OPENGL_METALANGLE #else ...

#include <MetalANGLE/GLES2/gl2ext.h>
#ifndef APIENTRY
#define APIENTRY GL_APIENTRY
#endif
#else
#include "SDL_opengles2.h"
#endif /* SDL_VIDEO_OPENGL_METALANGLE */
#endif /* SDL_VIDEO_OPENGL_ES2 && !SDL_VIDEO_OPENGL */


#if !SDL_VIDEO_OPENGL
#ifndef GL_CONTEXT_RELEASE_BEHAVIOR_KHR
#define GL_CONTEXT_RELEASE_BEHAVIOR_KHR 0x82FB
Expand Down
32 changes: 30 additions & 2 deletions src/video/uikit/SDL_uikitopengles.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
#include "SDL_loadso.h"
#include <dlfcn.h>

#if SDL_VIDEO_OPENGL_METALANGLE
@interface SDLEAGLContext : MGLContext
#else
@interface SDLEAGLContext : EAGLContext
#endif

/* The OpenGL ES context owns a view / drawable. */
@property (nonatomic, strong) SDL_uikitopenglview *sdlView;
Expand Down Expand Up @@ -69,8 +73,13 @@ - (void)dealloc
{
@autoreleasepool {
SDLEAGLContext *eaglcontext = (__bridge SDLEAGLContext *) context;

if (![EAGLContext setCurrentContext:eaglcontext]) {

#if SDL_VIDEO_OPENGL_METALANGLE
if (![MGLContext setCurrentContext:eaglcontext])
#else
if (![EAGLContext setCurrentContext:eaglcontext])
#endif
{
return SDL_SetError("Could not make EAGL context current");
}

Expand Down Expand Up @@ -140,15 +149,23 @@ int UIKit_GL_SwapWindow(_THIS, SDL_Window * window)
SDL_uikitopenglview *view;
SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
CGRect frame = UIKit_ComputeViewFrame(window, data.uiwindow.screen);
#if SDL_VIDEO_OPENGL_METALANGLE
MGLSharegroup *sharegroup = nil;
#else
EAGLSharegroup *sharegroup = nil;
#endif
CGFloat scale = 1.0;
int samples = 0;
int major = _this->gl_config.major_version;
int minor = _this->gl_config.minor_version;

/* The EAGLRenderingAPI enum values currently map 1:1 to major GLES
* versions. */
#if SDL_VIDEO_OPENGL_METALANGLE
MGLRenderingAPI api = major;
#else
EAGLRenderingAPI api = major;
#endif

/* iOS currently doesn't support GLES >3.0. iOS 6 also only supports up
* to GLES 2.0. */
Expand All @@ -162,7 +179,11 @@ int UIKit_GL_SwapWindow(_THIS, SDL_Window * window)
}

if (_this->gl_config.share_with_current_context) {
#if SDL_VIDEO_OPENGL_METALANGLE
MGLContext *context = (__bridge MGLContext *) SDL_GL_GetCurrentContext();
#else
EAGLContext *context = (__bridge EAGLContext *) SDL_GL_GetCurrentContext();
#endif
sharegroup = context.sharegroup;
}

Expand Down Expand Up @@ -237,10 +258,17 @@ int UIKit_GL_SwapWindow(_THIS, SDL_Window * window)
finished running its own code for the frame. If this isn't done, the
app may crash or have other nasty symptoms when Dictation is used.
*/
#if SDL_VIDEO_OPENGL_METALANGLE
MGLContext *context = (__bridge MGLContext *) SDL_GL_GetCurrentContext();
if (context != NULL && [MGLContext currentContext] != context) {
[MGLContext setCurrentContext:context];
}
#else
EAGLContext *context = (__bridge EAGLContext *) SDL_GL_GetCurrentContext();
if (context != NULL && [EAGLContext currentContext] != context) {
[EAGLContext setCurrentContext:context];
}
#endif
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/video/uikit/SDL_uikitopenglview.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2

#import <UIKit/UIKit.h>
#if SDL_VIDEO_OPENGL_METALANGLE
#import <MetalANGLE/MGLKit.h>
#else
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES3/gl.h>
#endif

#import "SDL_uikitview.h"
#include "SDL_uikitvideo.h"
Expand All @@ -41,9 +45,17 @@
stencilBits:(int)stencilBits
sRGB:(BOOL)sRGB
multisamples:(int)multisamples
#if SDL_VIDEO_OPENGL_METALANGLE
context:(MGLContext *)glcontext;
#else
context:(EAGLContext *)glcontext;
#endif

#if SDL_VIDEO_OPENGL_METALANGLE
@property (nonatomic, readonly, weak) MGLContext *context;
#else
@property (nonatomic, readonly, weak) EAGLContext *context;
#endif

/* The width and height of the drawable in pixels (as opposed to points.) */
@property (nonatomic, readonly) int backingWidth;
Expand Down
Loading