2013-01-15 23:37:07 -05:00
|
|
|
/*
|
2015-01-02 17:43:11 -05:00
|
|
|
* Copyright 2011-2015 Branimir Karadzic. All rights reserved.
|
2013-01-15 23:37:07 -05:00
|
|
|
* License: http://www.opensource.org/licenses/BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "bgfx_p.h"
|
|
|
|
|
2014-08-22 14:06:05 -04:00
|
|
|
#if BX_PLATFORM_OSX && (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)
|
2013-01-15 23:37:07 -05:00
|
|
|
# include "renderer_gl.h"
|
2015-04-02 16:42:56 -04:00
|
|
|
# include <AvailabilityMacros.h>
|
2013-07-13 19:52:28 -04:00
|
|
|
# include <Cocoa/Cocoa.h>
|
|
|
|
# include <bx/os.h>
|
2013-01-15 23:37:07 -05:00
|
|
|
|
2015-03-22 01:11:59 -04:00
|
|
|
namespace bgfx { namespace gl
|
2013-01-15 23:37:07 -05:00
|
|
|
{
|
|
|
|
|
2013-12-07 13:19:54 -05:00
|
|
|
# define GL_IMPORT(_optional, _proto, _func, _import) _proto _func
|
2013-07-14 02:11:02 -04:00
|
|
|
# include "glimports.h"
|
2014-10-12 01:24:07 -04:00
|
|
|
|
|
|
|
struct SwapChainGL
|
|
|
|
{
|
|
|
|
SwapChainGL(void* _nwh)
|
|
|
|
{
|
|
|
|
BX_UNUSED(_nwh);
|
|
|
|
}
|
|
|
|
|
|
|
|
~SwapChainGL()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void makeCurrent()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void swapBuffers()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2015-03-25 01:19:21 -04:00
|
|
|
|
2015-04-02 16:42:56 -04:00
|
|
|
class AutoreleasePoolHolder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoreleasePoolHolder() : m_pool([[NSAutoreleasePool alloc] init])
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoreleasePoolHolder()
|
|
|
|
{
|
|
|
|
[m_pool release];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AutoreleasePoolHolder(AutoreleasePoolHolder const&);
|
|
|
|
|
|
|
|
NSAutoreleasePool* const m_pool;
|
|
|
|
};
|
|
|
|
|
2013-07-14 02:11:02 -04:00
|
|
|
static void* s_opengl = NULL;
|
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
void GlContext::create(uint32_t _width, uint32_t _height)
|
|
|
|
{
|
2013-12-07 13:19:54 -05:00
|
|
|
BX_UNUSED(_width, _height);
|
|
|
|
|
2013-07-14 02:11:02 -04:00
|
|
|
s_opengl = bx::dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL");
|
|
|
|
BX_CHECK(NULL != s_opengl, "OpenGL dynamic library is not found!");
|
|
|
|
|
2015-04-02 16:42:56 -04:00
|
|
|
const AutoreleasePoolHolder pool;
|
2015-04-20 19:22:40 -04:00
|
|
|
NSWindow* nsWindow = (NSWindow*)g_platformData.nwh;
|
|
|
|
m_context = g_platformData.context;
|
2013-07-14 02:11:02 -04:00
|
|
|
|
2015-04-20 19:22:40 -04:00
|
|
|
if (NULL == g_platformData.context)
|
2015-03-25 01:19:21 -04:00
|
|
|
{
|
2015-04-02 16:42:56 -04:00
|
|
|
#if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
|
2015-03-25 01:19:21 -04:00
|
|
|
NSOpenGLPixelFormatAttribute profile =
|
2013-07-14 13:13:10 -04:00
|
|
|
#if BGFX_CONFIG_RENDERER_OPENGL >= 31
|
2015-03-25 01:19:21 -04:00
|
|
|
NSOpenGLProfileVersion3_2Core
|
2013-07-14 13:13:10 -04:00
|
|
|
#else
|
2015-03-25 01:19:21 -04:00
|
|
|
NSOpenGLProfileVersionLegacy
|
2013-07-14 13:13:10 -04:00
|
|
|
#endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
|
2015-03-25 01:19:21 -04:00
|
|
|
;
|
2015-04-02 16:42:56 -04:00
|
|
|
#endif // defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
|
2015-03-25 01:19:21 -04:00
|
|
|
|
|
|
|
NSOpenGLPixelFormatAttribute pixelFormatAttributes[] = {
|
2015-04-02 16:42:56 -04:00
|
|
|
#if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
|
2015-03-25 01:19:21 -04:00
|
|
|
NSOpenGLPFAOpenGLProfile, profile,
|
2015-04-02 16:42:56 -04:00
|
|
|
#endif // defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
|
2015-03-25 01:19:21 -04:00
|
|
|
NSOpenGLPFAColorSize, 24,
|
|
|
|
NSOpenGLPFAAlphaSize, 8,
|
|
|
|
NSOpenGLPFADepthSize, 24,
|
|
|
|
NSOpenGLPFAStencilSize, 8,
|
|
|
|
NSOpenGLPFADoubleBuffer, true,
|
|
|
|
NSOpenGLPFAAccelerated, true,
|
|
|
|
NSOpenGLPFANoRecovery, true,
|
|
|
|
0, 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes];
|
|
|
|
BGFX_FATAL(NULL != pixelFormat, Fatal::UnableToInitialize, "Failed to initialize pixel format.");
|
|
|
|
|
|
|
|
NSRect glViewRect = [[nsWindow contentView] bounds];
|
|
|
|
NSOpenGLView* glView = [[NSOpenGLView alloc] initWithFrame:glViewRect pixelFormat:pixelFormat];
|
|
|
|
|
|
|
|
[pixelFormat release];
|
2015-04-07 00:31:26 -04:00
|
|
|
// [glView setWantsBestResolutionOpenGLSurface:YES];
|
2015-03-25 01:19:21 -04:00
|
|
|
[nsWindow setContentView:glView];
|
|
|
|
|
|
|
|
NSOpenGLContext* glContext = [glView openGLContext];
|
|
|
|
BGFX_FATAL(NULL != glContext, Fatal::UnableToInitialize, "Failed to initialize GL context.");
|
|
|
|
|
|
|
|
[glContext makeCurrentContext];
|
|
|
|
GLint interval = 0;
|
|
|
|
[glContext setValues:&interval forParameter:NSOpenGLCPSwapInterval];
|
|
|
|
|
|
|
|
m_view = glView;
|
|
|
|
m_context = glContext;
|
|
|
|
}
|
2014-03-29 22:58:24 -04:00
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
import();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlContext::destroy()
|
|
|
|
{
|
2015-04-20 19:22:40 -04:00
|
|
|
if (NULL == g_platformData.context)
|
2015-03-25 22:43:12 -04:00
|
|
|
{
|
|
|
|
NSOpenGLView* glView = (NSOpenGLView*)m_view;
|
|
|
|
[glView release];
|
|
|
|
}
|
2013-07-14 02:11:02 -04:00
|
|
|
|
2015-03-25 22:43:12 -04:00
|
|
|
m_view = 0;
|
|
|
|
m_context = 0;
|
2013-07-14 02:11:02 -04:00
|
|
|
bx::dlclose(s_opengl);
|
2013-01-15 23:37:07 -05:00
|
|
|
}
|
|
|
|
|
2015-04-07 00:31:26 -04:00
|
|
|
void GlContext::resize(uint32_t _width, uint32_t _height, uint32_t _flags)
|
2013-01-15 23:37:07 -05:00
|
|
|
{
|
2014-10-01 23:25:56 -04:00
|
|
|
BX_UNUSED(_width, _height);
|
2014-03-17 22:14:20 -04:00
|
|
|
|
2015-04-07 00:31:26 -04:00
|
|
|
bool vsync = !!(_flags&BGFX_RESET_VSYNC);
|
|
|
|
GLint interval = vsync ? 1 : 0;
|
2014-03-17 22:14:20 -04:00
|
|
|
NSOpenGLContext* glContext = (NSOpenGLContext*)m_context;
|
|
|
|
[glContext setValues:&interval forParameter:NSOpenGLCPSwapInterval];
|
2014-10-01 23:25:56 -04:00
|
|
|
[glContext update];
|
2013-01-15 23:37:07 -05:00
|
|
|
}
|
|
|
|
|
2014-09-23 23:35:39 -04:00
|
|
|
bool GlContext::isSwapChainSupported()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-12 01:24:07 -04:00
|
|
|
SwapChainGL* GlContext::createSwapChain(void* _nwh)
|
2013-01-15 23:37:07 -05:00
|
|
|
{
|
2014-10-12 01:24:07 -04:00
|
|
|
return BX_NEW(g_allocator, SwapChainGL)(_nwh);
|
2014-09-07 20:46:52 -04:00
|
|
|
}
|
|
|
|
|
2014-11-30 12:06:47 -05:00
|
|
|
void GlContext::destroySwapChain(SwapChainGL* _swapChain)
|
2014-09-07 20:46:52 -04:00
|
|
|
{
|
2014-10-12 01:24:07 -04:00
|
|
|
BX_DELETE(g_allocator, _swapChain);
|
2014-09-07 20:46:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlContext::swap(SwapChainGL* _swapChain)
|
|
|
|
{
|
2014-10-12 01:24:07 -04:00
|
|
|
if (NULL == _swapChain)
|
|
|
|
{
|
|
|
|
NSOpenGLContext* glContext = (NSOpenGLContext*)m_context;
|
|
|
|
[glContext makeCurrentContext];
|
|
|
|
[glContext flushBuffer];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_swapChain->makeCurrent();
|
|
|
|
_swapChain->swapBuffers();
|
|
|
|
}
|
2013-01-15 23:37:07 -05:00
|
|
|
}
|
|
|
|
|
2014-10-12 01:24:07 -04:00
|
|
|
void GlContext::makeCurrent(SwapChainGL* _swapChain)
|
2014-09-07 20:46:52 -04:00
|
|
|
{
|
2014-10-12 01:24:07 -04:00
|
|
|
if (NULL == _swapChain)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_swapChain->makeCurrent();
|
|
|
|
}
|
2014-09-07 20:46:52 -04:00
|
|
|
}
|
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
void GlContext::import()
|
|
|
|
{
|
2014-02-19 23:08:22 -05:00
|
|
|
BX_TRACE("Import:");
|
|
|
|
# define GL_EXTENSION(_optional, _proto, _func, _import) \
|
|
|
|
{ \
|
|
|
|
if (_func == NULL) \
|
|
|
|
{ \
|
|
|
|
_func = (_proto)bx::dlsym(s_opengl, #_import); \
|
|
|
|
BX_TRACE("%p " #_func " (" #_import ")", _func); \
|
|
|
|
} \
|
|
|
|
BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGL context. NSGLGetProcAddress(\"%s\")", #_import); \
|
|
|
|
}
|
2013-01-15 23:37:07 -05:00
|
|
|
# include "glimports.h"
|
|
|
|
}
|
|
|
|
|
2015-03-22 01:11:59 -04:00
|
|
|
} /* namespace gl */ } // namespace bgfx
|
2013-01-15 23:37:07 -05:00
|
|
|
|
2013-07-13 19:52:28 -04:00
|
|
|
#endif // BX_PLATFORM_OSX && (BGFX_CONFIG_RENDERER_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3|BGFX_CONFIG_RENDERER_OPENGL)
|