Skip to content

Commit

Permalink
[macOS] Add support for ANGLE over native OpenGL (as a fallback for A…
Browse files Browse the repository at this point in the history
…NGLE over Metal).
  • Loading branch information
bruvzg committed Dec 4, 2023
1 parent d76c1d0 commit f159004
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
48 changes: 33 additions & 15 deletions platform/macos/display_server_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#import <IOKit/IOKitLib.h>
#import <IOKit/hid/IOHIDKeys.h>
#import <IOKit/hid/IOHIDLib.h>
#import <Metal/Metal.h>

const NSMenu *DisplayServerMacOS::_get_menu_root(const String &p_menu_root) const {
const NSMenu *menu = nullptr;
Expand Down Expand Up @@ -3405,7 +3406,7 @@ - (void)popupAction:(id)sender {
wd.wb_offset.x = MAX(wd.wb_offset.x, 12);
wd.wb_offset.y = MAX(wd.wb_offset.y, 12);
if (wd.window_button_view) {
[wd.window_button_view setOffset:NSMakePoint(wd.wb_offset.x, wd.wb_offset.y)];
[(GodotButtonView *)wd.window_button_view setOffset:NSMakePoint(wd.wb_offset.x, wd.wb_offset.y)];
}
}

Expand Down Expand Up @@ -4513,34 +4514,51 @@ - (void)popupAction:(id)sender {
[main_menu setSubmenu:apple_menu forItem:menu_item];
[main_menu setAutoenablesItems:NO];

//!!!!!!!!!!!!!!!!!!!!!!!!!!
//TODO - do Vulkan and OpenGL support checks, driver selection and fallback
rendering_driver = p_rendering_driver;

#if defined(GLES3_ENABLED)
if (rendering_driver == "opengl3_angle") {
gl_manager_angle = memnew(GLManagerANGLE_MacOS);
if (gl_manager_angle->initialize() != OK || gl_manager_angle->open_display(nullptr) != OK) {
memdelete(gl_manager_angle);
gl_manager_angle = nullptr;
bool fallback = GLOBAL_GET("rendering/gl_compatibility/fallback_to_native");
if (fallback) {
WARN_PRINT("Your video card drivers seem not to support the required Metal version, switching to native OpenGL.");
rendering_driver = "opengl3";
} else {
r_error = ERR_UNAVAILABLE;
ERR_FAIL_MSG("Could not initialize ANGLE OpenGL.");
if (@available(macOS 10.15, *)) {
// Try ANGLE over Metal if running on macOS 10.15+ and on hardware with Metal 2 support.
id device = MTLCreateSystemDefaultDevice();
if (device && [device supportsFamily:MTLGPUFamilyMac2]) {
gl_manager_angle = memnew(GLManagerANGLE_MacOS(true));
if (gl_manager_angle->initialize() != OK || gl_manager_angle->open_display(nullptr) != OK) {
memdelete(gl_manager_angle);
gl_manager_angle = nullptr;

WARN_PRINT("Your video card drivers seem not to support the required Metal version for ANGLE, switching to ANGLE over OpenGL.");
}
}
}
if (!gl_manager_angle) {
// Try ANGLE over native OpenGL.
gl_manager_angle = memnew(GLManagerANGLE_MacOS(false));
if (gl_manager_angle->initialize() != OK || gl_manager_angle->open_display(nullptr) != OK) {
memdelete(gl_manager_angle);
gl_manager_angle = nullptr;

bool fallback = GLOBAL_GET("rendering/gl_compatibility/fallback_to_native");
if (fallback) {
WARN_PRINT("Your video card drivers seem not to support the required OpenGL version for ANGLE, switching to native OpenGL.");
rendering_driver = "opengl3";
} else {
r_error = ERR_UNAVAILABLE;
ERR_FAIL_MSG("Could not initialize OpenGL (ANGLE).");
}
}
}
}

if (rendering_driver == "opengl3") {
// Try natvie OpenGL.
gl_manager_legacy = memnew(GLManagerLegacy_MacOS);
if (gl_manager_legacy->initialize() != OK) {
memdelete(gl_manager_legacy);
gl_manager_legacy = nullptr;

r_error = ERR_UNAVAILABLE;
ERR_FAIL_MSG("Could not initialize native OpenGL.");
ERR_FAIL_MSG("Could not initialize OpenGL (Native).");
}
}
#endif
Expand Down
4 changes: 3 additions & 1 deletion platform/macos/gl_manager_macos_angle.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

class GLManagerANGLE_MacOS : public EGLManager {
private:
bool use_metal = true;

virtual const char *_get_platform_extension_name() const override;
virtual EGLenum _get_platform_extension_enum() const override;
virtual EGLenum _get_platform_api_enum() const override;
Expand All @@ -54,7 +56,7 @@ class GLManagerANGLE_MacOS : public EGLManager {
public:
void window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) {}

GLManagerANGLE_MacOS() {}
GLManagerANGLE_MacOS(bool p_metal);
~GLManagerANGLE_MacOS() {}
};

Expand Down
10 changes: 9 additions & 1 deletion platform/macos/gl_manager_macos_angle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
Vector<EGLAttrib> GLManagerANGLE_MacOS::_get_platform_display_attributes() const {
Vector<EGLAttrib> ret;
ret.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE);
ret.push_back(EGL_PLATFORM_ANGLE_TYPE_METAL_ANGLE);
if (use_metal) {
ret.push_back(EGL_PLATFORM_ANGLE_TYPE_METAL_ANGLE);
} else {
ret.push_back(EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE);
}
ret.push_back(EGL_NONE);

return ret;
Expand All @@ -67,4 +71,8 @@
return ret;
}

GLManagerANGLE_MacOS::GLManagerANGLE_MacOS(bool p_metal) {
use_metal = p_metal;
}

#endif // MACOS_ENABLED && GLES3_ENABLED

0 comments on commit f159004

Please sign in to comment.