forked from sascha/godot
[macOS] Add support for OpenGLES3 video driver.
parent
87727f70ed
commit
1ad14eb14b
@ -1,161 +0,0 @@
|
||||
/*************************************************************************/
|
||||
/* context_gl_osx.mm */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* 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. */
|
||||
/*************************************************************************/
|
||||
|
||||
#include "context_gl_osx.h"
|
||||
|
||||
#if defined(GLES3_ENABLED) || defined(GLES_ENABLED)
|
||||
|
||||
void ContextGL_OSX::release_current() {
|
||||
[NSOpenGLContext clearCurrentContext];
|
||||
}
|
||||
|
||||
void ContextGL_OSX::make_current() {
|
||||
[context makeCurrentContext];
|
||||
}
|
||||
|
||||
void ContextGL_OSX::update() {
|
||||
[context update];
|
||||
}
|
||||
|
||||
void ContextGL_OSX::set_opacity(GLint p_opacity) {
|
||||
[context setValues:&p_opacity forParameter:NSOpenGLCPSurfaceOpacity];
|
||||
}
|
||||
|
||||
int ContextGL_OSX::get_window_width() {
|
||||
return OS::get_singleton()->get_video_mode().width;
|
||||
}
|
||||
|
||||
int ContextGL_OSX::get_window_height() {
|
||||
return OS::get_singleton()->get_video_mode().height;
|
||||
}
|
||||
|
||||
void ContextGL_OSX::swap_buffers() {
|
||||
[context flushBuffer];
|
||||
}
|
||||
|
||||
void ContextGL_OSX::set_use_vsync(bool p_use) {
|
||||
CGLContextObj ctx = CGLGetCurrentContext();
|
||||
if (ctx) {
|
||||
GLint swapInterval = p_use ? 1 : 0;
|
||||
CGLSetParameter(ctx, kCGLCPSwapInterval, &swapInterval);
|
||||
use_vsync = p_use;
|
||||
}
|
||||
}
|
||||
|
||||
bool ContextGL_OSX::is_using_vsync() const {
|
||||
return use_vsync;
|
||||
}
|
||||
|
||||
Error ContextGL_OSX::initialize() {
|
||||
framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl"));
|
||||
ERR_FAIL_COND_V(!framework, ERR_CANT_CREATE);
|
||||
|
||||
unsigned int attributeCount = 0;
|
||||
|
||||
// OS X needs non-zero color size, so set reasonable values
|
||||
int colorBits = 32;
|
||||
|
||||
// Fail if a robustness strategy was requested
|
||||
|
||||
#define ADD_ATTR(x) \
|
||||
{ attributes[attributeCount++] = x; }
|
||||
#define ADD_ATTR2(x, y) \
|
||||
{ \
|
||||
ADD_ATTR(x); \
|
||||
ADD_ATTR(y); \
|
||||
}
|
||||
|
||||
// Arbitrary array size here
|
||||
NSOpenGLPixelFormatAttribute attributes[40];
|
||||
|
||||
ADD_ATTR(NSOpenGLPFADoubleBuffer);
|
||||
ADD_ATTR(NSOpenGLPFAClosestPolicy);
|
||||
|
||||
if (!gles3_context) {
|
||||
ADD_ATTR2(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersionLegacy);
|
||||
} else {
|
||||
//we now need OpenGL 3 or better, maybe even change this to 3_3Core ?
|
||||
ADD_ATTR2(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
|
||||
}
|
||||
|
||||
ADD_ATTR2(NSOpenGLPFAColorSize, colorBits);
|
||||
|
||||
/*
|
||||
if (fbconfig->alphaBits > 0)
|
||||
ADD_ATTR2(NSOpenGLPFAAlphaSize, fbconfig->alphaBits);
|
||||
*/
|
||||
|
||||
ADD_ATTR2(NSOpenGLPFADepthSize, 24);
|
||||
|
||||
ADD_ATTR2(NSOpenGLPFAStencilSize, 8);
|
||||
|
||||
/*
|
||||
if (fbconfig->stereo)
|
||||
ADD_ATTR(NSOpenGLPFAStereo);
|
||||
*/
|
||||
|
||||
/*
|
||||
if (fbconfig->samples > 0) {
|
||||
ADD_ATTR2(NSOpenGLPFASampleBuffers, 1);
|
||||
ADD_ATTR2(NSOpenGLPFASamples, fbconfig->samples);
|
||||
}
|
||||
*/
|
||||
|
||||
// NOTE: All NSOpenGLPixelFormats on the relevant cards support sRGB
|
||||
// framebuffer, so there's no need (and no way) to request it
|
||||
|
||||
ADD_ATTR(0);
|
||||
|
||||
#undef ADD_ATTR
|
||||
#undef ADD_ATTR2
|
||||
|
||||
pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
|
||||
ERR_FAIL_COND_V(pixelFormat == nil, ERR_CANT_CREATE);
|
||||
|
||||
context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
|
||||
|
||||
ERR_FAIL_COND_V(context == nil, ERR_CANT_CREATE);
|
||||
|
||||
[context setView:window_view];
|
||||
|
||||
[context makeCurrentContext];
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
ContextGL_OSX::ContextGL_OSX(id p_view, bool p_gles3_context) {
|
||||
gles3_context = p_gles3_context;
|
||||
window_view = p_view;
|
||||
use_vsync = false;
|
||||
}
|
||||
|
||||
ContextGL_OSX::~ContextGL_OSX() {}
|
||||
|
||||
#endif
|
||||
@ -0,0 +1,233 @@
|
||||
/*************************************************************************/
|
||||
/* gl_manager_osx.mm */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* 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. */
|
||||
/*************************************************************************/
|
||||
|
||||
#include "gl_manager_osx.h"
|
||||
|
||||
#ifdef OSX_ENABLED
|
||||
#ifdef GLES3_ENABLED
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
Error GLManager_OSX::_create_context(GLWindow &win) {
|
||||
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);
|
||||
|
||||
win.context = [[NSOpenGLContext alloc] initWithFormat:pixel_format shareContext:_shared_context];
|
||||
ERR_FAIL_COND_V(win.context == nil, ERR_CANT_CREATE);
|
||||
if (_shared_context == nullptr) {
|
||||
_shared_context = win.context;
|
||||
}
|
||||
|
||||
[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) {
|
||||
if (p_window_id >= (int)_windows.size()) {
|
||||
_windows.resize(p_window_id + 1);
|
||||
}
|
||||
|
||||
GLWindow &win = _windows[p_window_id];
|
||||
win.in_use = true;
|
||||
win.window_id = p_window_id;
|
||||
win.width = p_width;
|
||||
win.height = p_height;
|
||||
win.window_view = p_view;
|
||||
|
||||
if (_create_context(win) != OK) {
|
||||
_windows.remove(_windows.size() - 1);
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
window_make_current(_windows.size() - 1);
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
void GLManager_OSX::_internal_set_current_window(GLWindow *p_win) {
|
||||
_current_window = p_win;
|
||||
}
|
||||
|
||||
void GLManager_OSX::window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) {
|
||||
if (p_window_id == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
GLWindow &win = _windows[p_window_id];
|
||||
if (!win.in_use) {
|
||||
return;
|
||||
}
|
||||
|
||||
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) {
|
||||
return get_window(p_window_id).width;
|
||||
}
|
||||
|
||||
int GLManager_OSX::window_get_height(DisplayServer::WindowID p_window_id) {
|
||||
return get_window(p_window_id).height;
|
||||
}
|
||||
|
||||
void GLManager_OSX::window_destroy(DisplayServer::WindowID p_window_id) {
|
||||
GLWindow &win = get_window(p_window_id);
|
||||
win.in_use = false;
|
||||
|
||||
if (_current_window == &win) {
|
||||
_current_window = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void GLManager_OSX::release_current() {
|
||||
if (!_current_window) {
|
||||
return;
|
||||
}
|
||||
|
||||
[NSOpenGLContext clearCurrentContext];
|
||||
}
|
||||
|
||||
void GLManager_OSX::window_make_current(DisplayServer::WindowID p_window_id) {
|
||||
if (p_window_id == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
GLWindow &win = _windows[p_window_id];
|
||||
if (!win.in_use) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (&win == _current_window) {
|
||||
return;
|
||||
}
|
||||
|
||||
[win.context makeCurrentContext];
|
||||
|
||||
_internal_set_current_window(&win);
|
||||
}
|
||||
|
||||
void GLManager_OSX::make_current() {
|
||||
if (!_current_window) {
|
||||
return;
|
||||
}
|
||||
if (!_current_window->in_use) {
|
||||
WARN_PRINT("current window not in use!");
|
||||
return;
|
||||
}
|
||||
[_current_window->context makeCurrentContext];
|
||||
}
|
||||
|
||||
void GLManager_OSX::swap_buffers() {
|
||||
// NO NEED TO CALL SWAP BUFFERS for each window...
|
||||
// see https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glXSwapBuffers.xml
|
||||
|
||||
if (!_current_window) {
|
||||
return;
|
||||
}
|
||||
if (!_current_window->in_use) {
|
||||
WARN_PRINT("current window not in use!");
|
||||
return;
|
||||
}
|
||||
[_current_window->context flushBuffer];
|
||||
}
|
||||
|
||||
void GLManager_OSX::window_update(DisplayServer::WindowID p_window_id) {
|
||||
if (p_window_id == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
GLWindow &win = _windows[p_window_id];
|
||||
if (!win.in_use) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (&win == _current_window) {
|
||||
return;
|
||||
}
|
||||
|
||||
[win.context update];
|
||||
}
|
||||
|
||||
Error GLManager_OSX::initialize() {
|
||||
return OK;
|
||||
}
|
||||
|
||||
void GLManager_OSX::set_use_vsync(bool p_use) {
|
||||
use_vsync = p_use;
|
||||
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;
|
||||
use_vsync = false;
|
||||
_current_window = nullptr;
|
||||
}
|
||||
|
||||
GLManager_OSX::~GLManager_OSX() {
|
||||
release_current();
|
||||
}
|
||||
|
||||
#endif // GLES3_ENABLED
|
||||
#endif // OSX
|
||||
Loading…
Reference in New Issue