From ade81ab598df652e507c14ff3065d72b7116de1b Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sun, 21 Jan 2018 18:46:41 -0800 Subject: Updated OpenGL context initialization on MacOS --- Graphics/GraphicsEngineOpenGL/CMakeLists.txt | 10 +++- .../GraphicsEngineOpenGL/include/GLContextMacOS.h | 6 +-- .../GraphicsEngineOpenGL/src/GLContextMacOS.cpp | 58 ++++++---------------- 3 files changed, 24 insertions(+), 50 deletions(-) (limited to 'Graphics/GraphicsEngineOpenGL') diff --git a/Graphics/GraphicsEngineOpenGL/CMakeLists.txt b/Graphics/GraphicsEngineOpenGL/CMakeLists.txt index ade8728a..5f028151 100644 --- a/Graphics/GraphicsEngineOpenGL/CMakeLists.txt +++ b/Graphics/GraphicsEngineOpenGL/CMakeLists.txt @@ -99,6 +99,10 @@ elseif(PLATFORM_LINUX) elseif(PLATFORM_MACOS) list(APPEND SOURCE src/GLContextMacOS.cpp) list(APPEND INCLUDE include/GLContextMacOS.h) + set_source_files_properties( + src/GLContextMacOS.cpp + COMPILE_FLAGS "-x objective-c++" + ) else() message(FATAL_ERROR "Unknown platform") endif() @@ -166,7 +170,11 @@ elseif(PLATFORM_LINUX) set(PRIVATE_DEPENDENCIES ${PRIVATE_DEPENDENCIES} glew-static) elseif(PLATFORM_MACOS) find_package(OpenGL REQUIRED) - set(PRIVATE_DEPENDENCIES ${PRIVATE_DEPENDENCIES} glew-static ${OPENGL_LIBRARY}) + find_library(APP_KIT AppKit) + if (NOT APP_KIT) + message(FATAL_ERROR "AppKit not found") + endif() + set(PRIVATE_DEPENDENCIES ${PRIVATE_DEPENDENCIES} glew-static ${OPENGL_LIBRARY} ${APP_KIT}) else() message(FATAL_ERROR "Unknown platform") endif() diff --git a/Graphics/GraphicsEngineOpenGL/include/GLContextMacOS.h b/Graphics/GraphicsEngineOpenGL/include/GLContextMacOS.h index 399251c9..3e5b77bb 100644 --- a/Graphics/GraphicsEngineOpenGL/include/GLContextMacOS.h +++ b/Graphics/GraphicsEngineOpenGL/include/GLContextMacOS.h @@ -25,7 +25,6 @@ namespace Diligent { - struct ContextInitInfo { SwapChainDesc SwapChainAttribs; @@ -35,10 +34,9 @@ namespace Diligent class GLContext { public: - typedef void* NativeGLContextType; + typedef void* NativeGLContextType; // NSOpenGLContext* GLContext(const ContextInitInfo &Info, struct DeviceCaps &DeviceCaps); - ~GLContext(); void SwapBuffers(); const SwapChainDesc& GetSwapChainDesc()const{ return m_SwapChainAttribs; } @@ -46,8 +44,6 @@ namespace Diligent NativeGLContextType GetCurrentNativeGLContext(); private: - void *m_pNativeWindow = nullptr; - NativeGLContextType m_Context; SwapChainDesc m_SwapChainAttribs; }; } diff --git a/Graphics/GraphicsEngineOpenGL/src/GLContextMacOS.cpp b/Graphics/GraphicsEngineOpenGL/src/GLContextMacOS.cpp index d0c54c37..81e0efdd 100644 --- a/Graphics/GraphicsEngineOpenGL/src/GLContextMacOS.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/GLContextMacOS.cpp @@ -23,6 +23,8 @@ #include "pch.h" +#import + #include "GLContextMacOS.h" #include "DeviceCaps.h" #include "GLTypeConversions.h" @@ -31,39 +33,24 @@ namespace Diligent { GLContext::GLContext( const ContextInitInfo &Info, DeviceCaps &DeviceCaps ) : - m_pNativeWindow(Info.pNativeWndHandle), - m_Context(0), m_SwapChainAttribs(Info.SwapChainAttribs) { - //auto CurrentCtx = glXGetCurrentContext(); - //if (CurrentCtx == 0) - //{ - // LOG_ERROR_AND_THROW("No current GL context found!"); - //} + if (GetCurrentNativeGLContext() == nullptr) + { + LOG_ERROR_AND_THROW("No current GL context found!"); + } // Initialize GLEW glewExperimental = true; // This is required on MacOS GLenum err = glewInit(); if( GLEW_OK != err ) LOG_ERROR_AND_THROW( "Failed to initialize GLEW" ); -#if 0 - if(Info.pNativeWndHandle != nullptr && Info.pDisplay != nullptr) - { - auto wnd = static_cast(reinterpret_cast(Info.pNativeWndHandle)); - auto display = reinterpret_cast(Info.pDisplay); - - XWindowAttributes XWndAttribs; - XGetWindowAttributes(display, wnd, &XWndAttribs); - m_SwapChainAttribs.Width = XWndAttribs.width; - m_SwapChainAttribs.Height = XWndAttribs.height; - - //glXSwapIntervalEXT(0); - } -#endif - - m_SwapChainAttribs.Width = 1024; - m_SwapChainAttribs.Height = 768; + //Set dummy width and height until resize is called by the app + if(m_SwapChainAttribs.Width == 0) + m_SwapChainAttribs.Width = 1024; + if(m_SwapChainAttribs.Height) + m_SwapChainAttribs.Height = 768; //Checking GL version const GLubyte *GLVersionString = glGetString( GL_VERSION ); @@ -106,31 +93,14 @@ namespace Diligent DeviceCaps.bMultithreadedResourceCreationSupported = False; } - GLContext::~GLContext() - { - } - void GLContext::SwapBuffers() { -#if 0 - if(m_pNativeWindow != nullptr && m_pDisplay != nullptr) - { - auto wnd = static_cast(reinterpret_cast(m_pNativeWindow)); - auto display = reinterpret_cast(m_pDisplay); - glXSwapBuffers(display, wnd); - } - else - { - LOG_ERROR("Swap buffer failed because window and/or display handle is not initialized"); - } -#endif + LOG_ERROR("Swap buffers operation must be performed by the app on MacOS"); } GLContext::NativeGLContextType GLContext::GetCurrentNativeGLContext() { -#if 0 - return glXGetCurrentContext(); -#endif - return nullptr; + NSOpenGLContext* CurrentCtx = [NSOpenGLContext currentContext]; + return CurrentCtx; } } -- cgit v1.2.3