2021-11-02 10:27:38 +00:00
|
|
|
/*************************************************************************/
|
2022-01-26 09:01:27 +00:00
|
|
|
/* gl_manager_osx_legacy.mm */
|
2021-11-02 10:27:38 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2021-11-02 10:27:38 +00:00
|
|
|
/* */
|
|
|
|
/* 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-01-26 09:01:27 +00:00
|
|
|
#include "gl_manager_osx_legacy.h"
|
2021-11-02 10:27:38 +00:00
|
|
|
|
|
|
|
#ifdef OSX_ENABLED
|
|
|
|
#ifdef GLES3_ENABLED
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-01-26 09:01:27 +00:00
|
|
|
Error GLManager_OSX::create_context(GLWindow &win) {
|
2021-11-02 10:27:38 +00: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 09:01:27 +00:00
|
|
|
win.context = [[NSOpenGLContext alloc] initWithFormat:pixel_format shareContext:shared_context];
|
2021-11-02 10:27:38 +00:00
|
|
|
ERR_FAIL_COND_V(win.context == nil, ERR_CANT_CREATE);
|
2022-01-26 09:01:27 +00:00
|
|
|
if (shared_context == nullptr) {
|
|
|
|
shared_context = win.context;
|
2021-11-02 10:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[win.context setView:win.window_view];
|
|
|
|
[win.context makeCurrentContext];
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error GLManager_OSX::window_create(DisplayServer::WindowID p_window_id, id p_view, int p_width, int p_height) {
|
2022-01-26 09:01:27 +00:00
|
|
|
GLWindow win;
|
2021-11-02 10:27:38 +00:00
|
|
|
win.width = p_width;
|
|
|
|
win.height = p_height;
|
|
|
|
win.window_view = p_view;
|
|
|
|
|
2022-01-26 09:01:27 +00:00
|
|
|
if (create_context(win) != OK) {
|
2021-11-02 10:27:38 +00:00
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
2022-01-26 09:01:27 +00:00
|
|
|
windows[p_window_id] = win;
|
|
|
|
window_make_current(p_window_id);
|
2021-11-02 10:27:38 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLManager_OSX::window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) {
|
2022-01-26 09:01:27 +00:00
|
|
|
if (!windows.has(p_window_id)) {
|
2021-11-02 10:27:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-26 09:01:27 +00:00
|
|
|
GLWindow &win = windows[p_window_id];
|
2021-11-02 10:27:38 +00: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];
|
|
|
|
}
|
|
|
|
|
|
|
|
int GLManager_OSX::window_get_width(DisplayServer::WindowID p_window_id) {
|
2022-01-26 09:01:27 +00:00
|
|
|
if (!windows.has(p_window_id)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLWindow &win = windows[p_window_id];
|
|
|
|
return win.width;
|
2021-11-02 10:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int GLManager_OSX::window_get_height(DisplayServer::WindowID p_window_id) {
|
2022-01-26 09:01:27 +00:00
|
|
|
if (!windows.has(p_window_id)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLWindow &win = windows[p_window_id];
|
|
|
|
return win.height;
|
2021-11-02 10:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLManager_OSX::window_destroy(DisplayServer::WindowID p_window_id) {
|
2022-01-26 09:01:27 +00:00
|
|
|
if (!windows.has(p_window_id)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-02 10:27:38 +00:00
|
|
|
|
2022-01-26 09:01:27 +00:00
|
|
|
if (current_window == p_window_id) {
|
|
|
|
current_window = DisplayServer::INVALID_WINDOW_ID;
|
2021-11-02 10:27:38 +00:00
|
|
|
}
|
2022-01-26 09:01:27 +00:00
|
|
|
|
|
|
|
windows.erase(p_window_id);
|
2021-11-02 10:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLManager_OSX::release_current() {
|
2022-01-26 09:01:27 +00:00
|
|
|
if (current_window == DisplayServer::INVALID_WINDOW_ID) {
|
2021-11-02 10:27:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[NSOpenGLContext clearCurrentContext];
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLManager_OSX::window_make_current(DisplayServer::WindowID p_window_id) {
|
2022-01-26 09:01:27 +00:00
|
|
|
if (current_window == p_window_id) {
|
2021-11-02 10:27:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-01-26 09:01:27 +00:00
|
|
|
if (!windows.has(p_window_id)) {
|
2021-11-02 10:27:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-26 09:01:27 +00:00
|
|
|
GLWindow &win = windows[p_window_id];
|
2021-11-02 10:27:38 +00:00
|
|
|
[win.context makeCurrentContext];
|
|
|
|
|
2022-01-26 09:01:27 +00:00
|
|
|
current_window = p_window_id;
|
2021-11-02 10:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLManager_OSX::make_current() {
|
2022-01-26 09:01:27 +00:00
|
|
|
if (current_window == DisplayServer::INVALID_WINDOW_ID) {
|
2021-11-02 10:27:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-01-26 09:01:27 +00:00
|
|
|
if (!windows.has(current_window)) {
|
2021-11-02 10:27:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-01-26 09:01:27 +00:00
|
|
|
|
|
|
|
GLWindow &win = windows[current_window];
|
|
|
|
[win.context makeCurrentContext];
|
2021-11-02 10:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLManager_OSX::swap_buffers() {
|
2022-01-26 09:01:27 +00:00
|
|
|
for (Map<DisplayServer::WindowID, GLWindow>::Element *E = windows.front(); E; E = E->next()) {
|
|
|
|
[E->get().context flushBuffer];
|
2021-11-02 10:27:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLManager_OSX::window_update(DisplayServer::WindowID p_window_id) {
|
2022-01-26 09:01:27 +00:00
|
|
|
if (!windows.has(p_window_id)) {
|
2021-11-02 10:27:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-26 09:01:27 +00:00
|
|
|
GLWindow &win = windows[p_window_id];
|
|
|
|
[win.context update];
|
|
|
|
}
|
2021-11-02 10:27:38 +00:00
|
|
|
|
2022-01-26 09:01:27 +00:00
|
|
|
void GLManager_OSX::window_set_per_pixel_transparency_enabled(DisplayServer::WindowID p_window_id, bool p_enabled) {
|
|
|
|
if (!windows.has(p_window_id)) {
|
2021-11-02 10:27:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-26 09:01:27 +00: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 10:27:38 +00:00
|
|
|
[win.context update];
|
|
|
|
}
|
|
|
|
|
|
|
|
Error GLManager_OSX::initialize() {
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLManager_OSX::set_use_vsync(bool p_use) {
|
|
|
|
use_vsync = p_use;
|
2022-01-26 09:01:27 +00:00
|
|
|
|
2021-11-02 10:27:38 +00:00
|
|
|
CGLContextObj ctx = CGLGetCurrentContext();
|
|
|
|
if (ctx) {
|
|
|
|
GLint swapInterval = p_use ? 1 : 0;
|
|
|
|
CGLSetParameter(ctx, kCGLCPSwapInterval, &swapInterval);
|
|
|
|
use_vsync = p_use;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLManager_OSX::is_using_vsync() const {
|
|
|
|
return use_vsync;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLManager_OSX::GLManager_OSX(ContextType p_context_type) {
|
|
|
|
context_type = p_context_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLManager_OSX::~GLManager_OSX() {
|
|
|
|
release_current();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // GLES3_ENABLED
|
|
|
|
#endif // OSX
|