From 48e666e238a8c00f44a5ad6180641738a6d70f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 29 Dec 2014 22:00:27 +0100 Subject: [PATCH] Platform: try to create core context in Sdl2Application on all platforms. Originally enabled just on OSX, apparently something similar is needed also in Mesa. If the version is not user-specified, core GL 3.2 is created on OSX, core 3.0 elsewhere. If that fails, the application falls back to creating compatibility 2.1 context. Hopefully I didn't break anything. The only difference on OSX is that the application doesn't fall back anymore if the version is user-specified. --- src/Magnum/Platform/Sdl2Application.cpp | 61 +++++++++++++------------ src/Magnum/Platform/Sdl2Application.h | 5 ++ 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/Magnum/Platform/Sdl2Application.cpp b/src/Magnum/Platform/Sdl2Application.cpp index 7006e3a7e..5e82e44b2 100644 --- a/src/Magnum/Platform/Sdl2Application.cpp +++ b/src/Magnum/Platform/Sdl2Application.cpp @@ -111,7 +111,7 @@ bool Sdl2Application::tryCreateContext(const Configuration& configuration) { /** @todo Remove when Emscripten has proper SDL2 support */ #ifndef CORRADE_TARGET_EMSCRIPTEN - /* Set context version, if requested */ + /* Set context version, if user-specified */ if(configuration.version() != Version::None) { Int major, minor; std::tie(major, minor) = version(configuration.version()); @@ -124,10 +124,25 @@ bool Sdl2Application::tryCreateContext(const Configuration& configuration) { #else SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); #endif - } - #ifdef MAGNUM_TARGET_GLES - else { + /* Request usable version otherwise */ + } else { + #ifndef MAGNUM_TARGET_GLES + /* First try to create core context. This is needed mainly on OS X and + Mesa, as support for recent OpenGL versions isn't implemented in + compatibility contexts (which are the default). At least GL 3.2 is + needed on OSX, at least GL 3.0 is needed on Mesa. Bite the bullet + and try 3.0 also elsewhere. */ + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + #ifdef CORRADE_TARGET_APPLE + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); + #else + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); + #endif + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + + #else + /* For ES the major context version is compile-time constant */ #ifdef MAGNUM_TARGET_GLES3 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); #elif defined(MAGNUM_TARGET_GLES2) @@ -137,21 +152,9 @@ bool Sdl2Application::tryCreateContext(const Configuration& configuration) { #endif SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); + #endif } - /* On OS X we need to create 3.2 context, as the default (2.1) contains - compatibility features which are not implemented for newer GL versions - in Apple's GL drivers, thus we would be forever stuck on 2.1 without the - new features. In practice SDL fails to create 2.1 context on recent OS X - versions. */ - #elif defined(CORRADE_TARGET_APPLE) - else { - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - } - #endif - /* Create window */ if(!(window = SDL_CreateWindow(configuration.title().data(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, @@ -162,9 +165,13 @@ bool Sdl2Application::tryCreateContext(const Configuration& configuration) { return false; } - /* Fall back to GL 2.1, if 3.2 context creation fails on OS X */ - #ifdef CORRADE_TARGET_APPLE - if(!(context = SDL_GL_CreateContext(window))){ + /* Create context */ + context = SDL_GL_CreateContext(window); + + #ifndef MAGNUM_TARGET_GLES + /* Fall back to (forward compatible) GL 2.1, if core context creation fails + and if version is not user-specified */ + if(configuration.version() == Version::None && !context) { Warning() << "Platform::Sdl2Application::tryCreateContext(): cannot create core context:" << SDL_GetError() << "(falling back to compatibility context)"; SDL_DestroyWindow(window); @@ -180,22 +187,16 @@ bool Sdl2Application::tryCreateContext(const Configuration& configuration) { Error() << "Platform::Sdl2Application::tryCreateContext(): cannot create window:" << SDL_GetError(); return false; } - - if(!(context = SDL_GL_CreateContext(window))){ - Error() << "Platform::Sdl2Application::tryCreateContext(): cannot create compatibility context:" << SDL_GetError(); - SDL_DestroyWindow(window); - window = nullptr; - return false; - } } - #else - if(!(context = SDL_GL_CreateContext(window))) { + #endif + + /* Cannot create context (or fallback compatibility context on desktop) */ + if(!context) { Error() << "Platform::Sdl2Application::tryCreateContext(): cannot create context:" << SDL_GetError(); SDL_DestroyWindow(window); window = nullptr; return false; } - #endif #else /* Emscripten-specific initialization */ diff --git a/src/Magnum/Platform/Sdl2Application.h b/src/Magnum/Platform/Sdl2Application.h index f57dba419..b49514006 100644 --- a/src/Magnum/Platform/Sdl2Application.h +++ b/src/Magnum/Platform/Sdl2Application.h @@ -247,6 +247,11 @@ class Sdl2Application { * Must be called if and only if the context wasn't created by the * constructor itself. The program exits if the context cannot be * created, see @ref tryCreateContext() for an alternative. + * + * On desktop GL, if version is not specified in @p configuration, the + * application first tries to create core context (OpenGL 3.2+ on OS X, + * OpenGL 3.0+ elsewhere) and if that fails, falls back to + * compatibility OpenGL 2.1 context. */ #ifdef DOXYGEN_GENERATING_OUTPUT void createContext(const Configuration& configuration = Configuration());