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

Conversation

misl6
Copy link
Contributor

@misl6 misl6 commented Apr 25, 2021

Description

This PR adds support for MetalANGLEprovided "window" for iOS / iPadOS.

It keeps the "standard way" to instantiate a view on iOS by using the deprecated OpenGLES Apple own implementation, but adds a way to support OpenGL views also after the (not still known) removal date.

I don't expect a merge as-is, but the code We ( https://github.com/kivy/kivy-ios ) have used for our specific implementation is quite stable and tested, so it was worth to share it and start a discussion about the future. (See the related PR on kivy-ios: kivy/kivy-ios#448)

Existing Issue(s)

@misl6 misl6 changed the title Add support for MetalANGLE on UIKit (OpenGL ES to Metal API translati… Add support for MetalANGLE on UIKit (OpenGL ES to Metal API translation layer) Apr 25, 2021
@misl6
Copy link
Contributor Author

misl6 commented Apr 25, 2021

Our toolchain manages by itself the choice, but seems logical for users unfamiliar with kivy-ios to add a mini-guide about how to start using the MetalANGLE views without relying on kivy-ios own code:

  • Compile or download the prebuilt binaries of MetalANGLE from https://github.com/kakashidinho/metalangle, the prebuilt binaries differ by platform, so be sure to download the correct one.

  • Place the MetalANGLE.framework framework (you downloaded or built) into Xcode/SDL/third-party/frameworks.

  • By using the include/SDL_config_iphoneos.h file you have to disable:
    SDL_VIDEO_OPENGL_ES2, SDL_VIDEO_OPENGL_ES, SDL_VIDEO_RENDER_OGL_ES and SDL_VIDEO_RENDER_OGL_ES2 support.

  • Instead, you should add a new define, in the same file, to enable SDL_VIDEO_METALANGLE.

  • Build the SDL iOS Library as you did before, but be aware that you'll need to embed MetalANGLE.framework into your app, cause unlike OpenGLES.framework is not available on iOS.

@sezero
Copy link
Contributor

sezero commented Apr 25, 2021

This adds SDL_VIDEO_METALANGLE to SDL_config.h.in but doesn't touch
SDL_config.h.cmake, nor does it add any configurations to autotools
or cmake.

@@ -402,7 +402,7 @@ struct SDL_VideoDevice
struct SDL_EGL_VideoData *egl_data;
#endif

#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2 || SDL_VIDEO_METALANGLE
Copy link
Contributor

Choose a reason for hiding this comment

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

I think a better approach would be to do a similar thing to the EGL, CGL, GLX, etc. opengl context backend defines (you can see some examples by looking at how SDL_VIDEO_OPENGL_EGL is used) – I don't think code here needs to care whether metal-ANGLE is used to create the OpenGL ES context, since it's just an implementation of the OpenGL ES API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, and it makes sense.
I'm going for a SDL_VIDEO_OPENGL_METALANGLE if it looks nice.

@@ -46,6 +46,14 @@
#include "SDL_opengles2.h"
#endif /* SDL_VIDEO_OPENGL_ES2 && !SDL_VIDEO_OPENGL */

#if SDL_VIDEO_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 "../SDL_sysvideo.h"

extern int UIKit_GL_MakeCurrent(_THIS, SDL_Window * window,
Copy link
Contributor

Choose a reason for hiding this comment

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

These files have a lot of duplicate code. I wonder if it might be cleaner to merge it all into SDL_uikitopengles etc. with a couple #ifdefs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Uhm, there's a lot of duplicated code (especially for the header files), but, instead src/video/uikit/SDL_uikitmetalangleview.m is going to be definitely cleaner into a separate file.

I will try to merge the two implementations when changing SDL_VIDEO_METALANGLE to SDL_VIDEO_OPENGL_METALANGLE unless it makes sense to have both on the same file.

@@ -370,6 +375,13 @@ - (void)layoutSubviews
info->info.uikit.colorbuffer = glview.drawableRenderbuffer;
info->info.uikit.resolveFramebuffer = glview.msaaResolveFramebuffer;
} else {
#elif SDL_VIDEO_METALANGLE
Copy link
Contributor

Choose a reason for hiding this comment

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

If the metal-angle view code is able to be merged into SDL_uikitopenglview using a couple defines in key places, this codepath shouldn't be needed anymore.

@slime73
Copy link
Contributor

slime73 commented Apr 25, 2021

I don't remember how MoltenVK is integrated into SDL's build process but it's probably worth looking at that to see if the two can be made consistent with each other. It'd be weird to have two context backends not using the same integration process (I'm not sure whether it would make more sense to do what MoltenVK is doing, or to change what MoltenVK is doing to match this change.)

@slime73
Copy link
Contributor

slime73 commented Apr 25, 2021

From what I understand MetalANGLE is a fork of ANGLE. Is it going to be upstreamed any time in the foreseeable future? If so, is there anything this change can do to make the transition easier from SDL's side when it happens? SDL has ANGLE integration on a few other platforms.

@Cacodemon345
Copy link
Contributor

Cacodemon345 commented Apr 25, 2021

Upstream ANGLE has the Metal renderer in progress according to the readme and only OpenGL ES 2.0 is supported right now. OpenGL ES 3.0 is in progress currently.

@misl6
Copy link
Contributor Author

misl6 commented Apr 25, 2021

Yes, MetalANGLE is a fork of ANGLE, and part of the code is already being migrated to ANGLE, but as the author of MetalANGLE specified, there's some differences between the two codebases: (https://github.com/kakashidinho/metalangle#differences-between-metalangle-and-googles-angle)

The most important one, at this time, is:

MetalANGLE includes iOS supports and high level API such as MGLKit that mimics Apple's deprecated EAGL & GLKit API. These features are unlikely to be merged into ANGLE since ANGLE project doesn't have any plan to support iOS in near future.

So yes, there's a small chance, but seems it's not happening in the near future (unfortunately).

@misl6
Copy link
Contributor Author

misl6 commented Apr 30, 2021

@slime73, some updates:

  • I reduced the code footprint by using some #if/else preprocessor directives. Some minor bunches of code are still redundant in order to keep the code easily readable.

  • I took a quick look at how MoltenVK is implemented and it relies on a vendored version of vulkan headers. I'm not a fan at all of vendoring things but feel free to start a discussion about it.

  • Needs some additional cleanup & testing

@icculus
Copy link
Collaborator

icculus commented Nov 23, 2022

We're going to watch upstream ANGLE (not MetalANGLE) to decide when iOS support is ready and deal with it then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants