2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* gl_manager_macos_legacy.mm */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2021-11-02 11:27:38 +01:00
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
#include "gl_manager_macos_legacy.h"
|
2021-11-02 11:27:38 +01:00
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
#ifdef MACOS_ENABLED
|
2021-11-02 11:27:38 +01:00
|
|
|
#ifdef GLES3_ENABLED
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
Error GLManager_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];
|
|
|
|
ERR_FAIL_COND_V(pixel_format == nil, ERR_CANT_CREATE);
|
|
|
|
|
2022-01-26 10:01:27 +01:00
|
|
|
win.context = [[NSOpenGLContext alloc] initWithFormat:pixel_format shareContext:shared_context];
|
2021-11-02 11:27:38 +01:00
|
|
|
ERR_FAIL_COND_V(win.context == nil, 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;
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
Error GLManager_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.width = p_width;
|
|
|
|
win.height = p_height;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
void GLManager_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
|
|
|
|
|
|
|
win.width = p_width;
|
|
|
|
win.height = p_height;
|
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
int GLManager_MacOS::window_get_width(DisplayServer::WindowID p_window_id) {
|
2022-01-26 10:01:27 +01:00
|
|
|
if (!windows.has(p_window_id)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLWindow &win = windows[p_window_id];
|
|
|
|
return win.width;
|
2021-11-02 11:27:38 +01:00
|
|
|
}
|
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
int GLManager_MacOS::window_get_height(DisplayServer::WindowID p_window_id) {
|
2022-01-26 10:01:27 +01:00
|
|
|
if (!windows.has(p_window_id)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLWindow &win = windows[p_window_id];
|
|
|
|
return win.height;
|
2021-11-02 11:27:38 +01:00
|
|
|
}
|
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
void GLManager_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
|
|
|
}
|
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
void GLManager_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];
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
void GLManager_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
|
|
|
}
|
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
void GLManager_MacOS::make_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;
|
|
|
|
}
|
2022-01-26 10:01:27 +01:00
|
|
|
if (!windows.has(current_window)) {
|
2021-11-02 11:27:38 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-01-26 10:01:27 +01:00
|
|
|
|
|
|
|
GLWindow &win = windows[current_window];
|
|
|
|
[win.context makeCurrentContext];
|
2021-11-02 11:27:38 +01:00
|
|
|
}
|
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
void GLManager_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
|
|
|
}
|
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
void GLManager_MacOS::window_update(DisplayServer::WindowID p_window_id) {
|
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];
|
|
|
|
[win.context update];
|
|
|
|
}
|
2021-11-02 11:27:38 +01:00
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
void GLManager_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];
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
Error GLManager_MacOS::initialize() {
|
2021-11-02 11:27:38 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
void GLManager_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;
|
|
|
|
CGLSetParameter(ctx, kCGLCPSwapInterval, &swapInterval);
|
|
|
|
use_vsync = p_use;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
bool GLManager_MacOS::is_using_vsync() const {
|
2021-11-02 11:27:38 +01:00
|
|
|
return use_vsync;
|
|
|
|
}
|
|
|
|
|
2022-10-23 03:29:15 +02:00
|
|
|
NSOpenGLContext *GLManager_MacOS::get_context(DisplayServer::WindowID p_window_id) {
|
|
|
|
if (!windows.has(p_window_id)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLWindow &win = windows[p_window_id];
|
|
|
|
return win.context;
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
GLManager_MacOS::GLManager_MacOS(ContextType p_context_type) {
|
2021-11-02 11:27:38 +01:00
|
|
|
context_type = p_context_type;
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:28:22 +02:00
|
|
|
GLManager_MacOS::~GLManager_MacOS() {
|
2021-11-02 11:27:38 +01:00
|
|
|
release_current();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // GLES3_ENABLED
|
2022-07-20 08:28:22 +02:00
|
|
|
#endif // MACOS_ENABLED
|