2021-11-02 11:27:38 +01:00
|
|
|
/**************************************************************************/
|
2022-07-20 08:28:22 +02:00
|
|
|
/* gl_manager_macos_legacy.mm */
|
2021-11-02 11:27:38 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
#include "gl_manager_macos_legacy.h"
|
2021-11-02 11:27:38 +01:00
|
|
|
|
2023-06-08 14:51:32 +02:00
|
|
|
#if defined(MACOS_ENABLED) && defined(GLES3_ENABLED)
|
2023-01-20 10:31:07 +01:00
|
|
|
|
2024-08-07 10:57:27 +02:00
|
|
|
#include <dlfcn.h>
|
2021-11-02 11:27:38 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2023-06-08 14:51:32 +02:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations" // OpenGL is deprecated in macOS 10.14
|
|
|
|
|
2021-11-12 13:49:49 +01:00
|
|
|
Error GLManagerLegacy_MacOS::create_context(GLWindow &win) {
|
2021-11-02 11:27:38 +01:00
|
|
|
NSOpenGLPixelFormatAttribute attributes[] = {
|
|
|
|
NSOpenGLPFADoubleBuffer,
|
|
|
|
NSOpenGLPFAClosestPolicy,
|
|
|
|
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
|
|
|
|
NSOpenGLPFAColorSize, 32,
|
|
|
|
NSOpenGLPFADepthSize, 24,
|
|
|
|
NSOpenGLPFAStencilSize, 8,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
NSOpenGLPixelFormat *pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
|
2023-09-28 11:40:18 +02:00
|
|
|
ERR_FAIL_NULL_V(pixel_format, ERR_CANT_CREATE);
|
2021-11-02 11:27:38 +01:00
|
|
|
|
2022-01-26 10:01:27 +01:00
|
|
|
win.context = [[NSOpenGLContext alloc] initWithFormat:pixel_format shareContext:shared_context];
|
2023-09-28 11:40:18 +02:00
|
|
|
ERR_FAIL_NULL_V(win.context, ERR_CANT_CREATE);
|
2022-01-26 10:01:27 +01:00
|
|
|
if (shared_context == nullptr) {
|
|
|
|
shared_context = win.context;
|
2021-11-02 11:27:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[win.context setView:win.window_view];
|
|
|
|
[win.context makeCurrentContext];
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:49:49 +01:00
|
|
|
Error GLManagerLegacy_MacOS::window_create(DisplayServer::WindowID p_window_id, id p_view, int p_width, int p_height) {
|
2022-01-26 10:01:27 +01:00
|
|
|
GLWindow win;
|
2021-11-02 11:27:38 +01:00
|
|
|
win.window_view = p_view;
|
|
|
|
|
2022-01-26 10:01:27 +01:00
|
|
|
if (create_context(win) != OK) {
|
2021-11-02 11:27:38 +01:00
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:01:27 +01:00
|
|
|
windows[p_window_id] = win;
|
|
|
|
window_make_current(p_window_id);
|
2021-11-02 11:27:38 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:49:49 +01:00
|
|
|
void GLManagerLegacy_MacOS::window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) {
|
2022-01-26 10:01:27 +01:00
|
|
|
if (!windows.has(p_window_id)) {
|
2021-11-02 11:27:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:01:27 +01:00
|
|
|
GLWindow &win = windows[p_window_id];
|
2021-11-02 11:27:38 +01:00
|
|
|
|
|
|
|
GLint dim[2];
|
|
|
|
dim[0] = p_width;
|
|
|
|
dim[1] = p_height;
|
|
|
|
CGLSetParameter((CGLContextObj)[win.context CGLContextObj], kCGLCPSurfaceBackingSize, &dim[0]);
|
|
|
|
CGLEnable((CGLContextObj)[win.context CGLContextObj], kCGLCESurfaceBackingSize);
|
|
|
|
if (OS::get_singleton()->is_hidpi_allowed()) {
|
|
|
|
[win.window_view setWantsBestResolutionOpenGLSurface:YES];
|
|
|
|
} else {
|
|
|
|
[win.window_view setWantsBestResolutionOpenGLSurface:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
[win.context update];
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:49:49 +01:00
|
|
|
void GLManagerLegacy_MacOS::window_destroy(DisplayServer::WindowID p_window_id) {
|
2022-01-26 10:01:27 +01:00
|
|
|
if (!windows.has(p_window_id)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-02 11:27:38 +01:00
|
|
|
|
2022-01-26 10:01:27 +01:00
|
|
|
if (current_window == p_window_id) {
|
|
|
|
current_window = DisplayServer::INVALID_WINDOW_ID;
|
2021-11-02 11:27:38 +01:00
|
|
|
}
|
2022-01-26 10:01:27 +01:00
|
|
|
|
|
|
|
windows.erase(p_window_id);
|
2021-11-02 11:27:38 +01:00
|
|
|
}
|
|
|
|
|
2021-11-12 13:49:49 +01:00
|
|
|
void GLManagerLegacy_MacOS::release_current() {
|
2022-01-26 10:01:27 +01:00
|
|
|
if (current_window == DisplayServer::INVALID_WINDOW_ID) {
|
2021-11-02 11:27:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[NSOpenGLContext clearCurrentContext];
|
2024-04-09 11:47:06 +02:00
|
|
|
current_window = DisplayServer::INVALID_WINDOW_ID;
|
2021-11-02 11:27:38 +01:00
|
|
|
}
|
|
|
|
|
2021-11-12 13:49:49 +01:00
|
|
|
void GLManagerLegacy_MacOS::window_make_current(DisplayServer::WindowID p_window_id) {
|
2022-01-26 10:01:27 +01:00
|
|
|
if (current_window == p_window_id) {
|
2021-11-02 11:27:38 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-01-26 10:01:27 +01:00
|
|
|
if (!windows.has(p_window_id)) {
|
2021-11-02 11:27:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:01:27 +01:00
|
|
|
GLWindow &win = windows[p_window_id];
|
2021-11-02 11:27:38 +01:00
|
|
|
[win.context makeCurrentContext];
|
|
|
|
|
2022-01-26 10:01:27 +01:00
|
|
|
current_window = p_window_id;
|
2021-11-02 11:27:38 +01:00
|
|
|
}
|
|
|
|
|
2021-11-12 13:49:49 +01:00
|
|
|
void GLManagerLegacy_MacOS::swap_buffers() {
|
2022-09-12 23:57:11 +02:00
|
|
|
GLWindow &win = windows[current_window];
|
|
|
|
[win.context flushBuffer];
|
2021-11-02 11:27:38 +01:00
|
|
|
}
|
|
|
|
|
2021-11-12 13:49:49 +01:00
|
|
|
void GLManagerLegacy_MacOS::window_set_per_pixel_transparency_enabled(DisplayServer::WindowID p_window_id, bool p_enabled) {
|
2022-01-26 10:01:27 +01:00
|
|
|
if (!windows.has(p_window_id)) {
|
2021-11-02 11:27:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:01:27 +01:00
|
|
|
GLWindow &win = windows[p_window_id];
|
|
|
|
if (p_enabled) {
|
|
|
|
GLint opacity = 0;
|
|
|
|
[win.context setValues:&opacity forParameter:NSOpenGLContextParameterSurfaceOpacity];
|
|
|
|
} else {
|
|
|
|
GLint opacity = 1;
|
|
|
|
[win.context setValues:&opacity forParameter:NSOpenGLContextParameterSurfaceOpacity];
|
|
|
|
}
|
2021-11-02 11:27:38 +01:00
|
|
|
[win.context update];
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:49:49 +01:00
|
|
|
Error GLManagerLegacy_MacOS::initialize() {
|
2024-08-07 10:57:27 +02:00
|
|
|
return framework_loaded ? OK : ERR_CANT_CREATE;
|
2021-11-02 11:27:38 +01:00
|
|
|
}
|
|
|
|
|
2021-11-12 13:49:49 +01:00
|
|
|
void GLManagerLegacy_MacOS::set_use_vsync(bool p_use) {
|
2021-11-02 11:27:38 +01:00
|
|
|
use_vsync = p_use;
|
2022-01-26 10:01:27 +01:00
|
|
|
|
2021-11-02 11:27:38 +01:00
|
|
|
CGLContextObj ctx = CGLGetCurrentContext();
|
|
|
|
if (ctx) {
|
|
|
|
GLint swapInterval = p_use ? 1 : 0;
|
2021-11-12 13:49:49 +01:00
|
|
|
if (CGLSetParameter(ctx, kCGLCPSwapInterval, &swapInterval) != kCGLNoError) {
|
|
|
|
WARN_PRINT("Could not set V-Sync mode.");
|
|
|
|
}
|
2021-11-02 11:27:38 +01:00
|
|
|
use_vsync = p_use;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:49:49 +01:00
|
|
|
bool GLManagerLegacy_MacOS::is_using_vsync() const {
|
2021-11-02 11:27:38 +01:00
|
|
|
return use_vsync;
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:49:49 +01:00
|
|
|
NSOpenGLContext *GLManagerLegacy_MacOS::get_context(DisplayServer::WindowID p_window_id) {
|
2022-10-23 03:29:15 +02:00
|
|
|
if (!windows.has(p_window_id)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLWindow &win = windows[p_window_id];
|
|
|
|
return win.context;
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:49:49 +01:00
|
|
|
GLManagerLegacy_MacOS::GLManagerLegacy_MacOS() {
|
2024-08-07 10:57:27 +02:00
|
|
|
NSBundle *framework = [NSBundle bundleWithPath:@"/System/Library/Frameworks/OpenGL.framework"];
|
|
|
|
if (framework) {
|
|
|
|
void *library_handle = dlopen([framework.executablePath UTF8String], RTLD_NOW);
|
|
|
|
if (library_handle) {
|
|
|
|
CGLEnable = (CGLEnablePtr)dlsym(library_handle, "CGLEnable");
|
|
|
|
CGLSetParameter = (CGLSetParameterPtr)dlsym(library_handle, "CGLSetParameter");
|
|
|
|
CGLGetCurrentContext = (CGLGetCurrentContextPtr)dlsym(library_handle, "CGLGetCurrentContext");
|
|
|
|
|
|
|
|
framework_loaded = CGLEnable && CGLSetParameter && CGLGetCurrentContext;
|
|
|
|
}
|
|
|
|
}
|
2021-11-02 11:27:38 +01:00
|
|
|
}
|
|
|
|
|
2021-11-12 13:49:49 +01:00
|
|
|
GLManagerLegacy_MacOS::~GLManagerLegacy_MacOS() {
|
2021-11-02 11:27:38 +01:00
|
|
|
release_current();
|
|
|
|
}
|
|
|
|
|
2023-01-20 10:31:07 +01:00
|
|
|
#pragma clang diagnostic pop
|
|
|
|
|
2023-06-08 14:51:32 +02:00
|
|
|
#endif // MACOS_ENABLED && GLES3_ENABLED
|