Skip to content

Commit

Permalink
GLContext: Runtime selection of EGL/GLX on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Oct 20, 2018
1 parent 025e909 commit 0559311
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 87 deletions.
48 changes: 13 additions & 35 deletions CMakeLists.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ project(dolphin-emu)
# unique name here. # unique name here.
set(DISTRIBUTOR "None" CACHE STRING "Name of the distributor.") set(DISTRIBUTOR "None" CACHE STRING "Name of the distributor.")


option(USE_EGL "Enables EGL OpenGL Interface" OFF) if(UNIX AND NOT APPLE AND NOT ANDROID)
option(TRY_X11 "Enables X11 Support" ON) option(ENABLE_X11 "Enables X11 Support" ON)
endif()
if(NOT WIN32 AND NOT APPLE)
option(ENABLE_EGL "Enables EGL OpenGL Interface" ON)
endif()

option(USE_SHARED_ENET "Use shared libenet if found rather than Dolphin's soon-to-compatibly-diverge version" OFF) option(USE_SHARED_ENET "Use shared libenet if found rather than Dolphin's soon-to-compatibly-diverge version" OFF)
option(USE_UPNP "Enables UPnP port mapping support" ON) option(USE_UPNP "Enables UPnP port mapping support" ON)
option(ENABLE_QT "Enable Qt (Default)" ON) option(ENABLE_QT "Enable Qt (Default)" ON)
Expand Down Expand Up @@ -365,9 +370,7 @@ if(ANDROID)
# but not as a shared library. We want an executable. # but not as a shared library. We want an executable.
set(ANDROID 0) set(ANDROID 0)
endif() endif()
set(USE_X11 0)
set(USE_UPNP 0) set(USE_UPNP 0)
set(USE_EGL 1)
set(ENABLE_QT 0) set(ENABLE_QT 0)
set(USE_DISCORD_PRESENCE 0) set(USE_DISCORD_PRESENCE 0)


Expand All @@ -379,13 +382,7 @@ if(ANDROID)
endif() endif()


if(ENABLE_HEADLESS) if(ENABLE_HEADLESS)
if(APPLE) message(STATUS "Enabling Headless! Disabling GUI.")
message(STATUS "Enabling Headless! Disabling GUI.")
else()
message(STATUS "Enabling Headless! Disabling GUI, force enabling EGL!")
set(USE_EGL 1)
endif()
set(USE_X11 0)
set(ENABLE_QT 0) set(ENABLE_QT 0)
set(USE_DISCORD_PRESENCE 0) set(USE_DISCORD_PRESENCE 0)
add_definitions(-DUSE_HEADLESS) add_definitions(-DUSE_HEADLESS)
Expand Down Expand Up @@ -416,30 +413,11 @@ if (OPENGL_GL)
include_directories(${OPENGL_INCLUDE_DIR}) include_directories(${OPENGL_INCLUDE_DIR})
endif() endif()


set(USE_X11 0) if(ENABLE_X11)

find_package(X11 REQUIRED)
if(UNIX AND NOT APPLE AND NOT ANDROID AND NOT ENABLE_HEADLESS) add_definitions(-DHAVE_X11=1)
find_package(X11) message(STATUS "X11 support enabled")
if(TRY_X11 AND X11_FOUND)
set(USE_X11 1)
add_definitions(-DHAVE_X11=1)
include_directories(${X11_INCLUDE_DIR})
message(STATUS "X11 support enabled")
else()
set(USE_X11 0)
SET(X11_FOUND "")
message(STATUS "X11 support disabled")
add_definitions(-DHAVE_X11=0)
endif()

if (NOT USE_X11)
message(FATAL_ERROR "\n"
"No suitable display platform found\n"
"Requires x11 to run")
endif()
endif()


if(USE_X11)
check_lib(XRANDR xrandr Xrandr) check_lib(XRANDR xrandr Xrandr)
if(XRANDR_FOUND) if(XRANDR_FOUND)
add_definitions(-DHAVE_XRANDR=1) add_definitions(-DHAVE_XRANDR=1)
Expand Down Expand Up @@ -473,7 +451,7 @@ if(OPROFILING)
endif() endif()
endif() endif()


if(USE_EGL) if(ENABLE_EGL)
message(STATUS "EGL OpenGL interface enabled") message(STATUS "EGL OpenGL interface enabled")
add_definitions(-DUSE_EGL=1) add_definitions(-DUSE_EGL=1)
endif() endif()
Expand Down
23 changes: 12 additions & 11 deletions Source/Core/Common/CMakeLists.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -113,12 +113,10 @@ target_sources(common PRIVATE
GL/GLContext.cpp GL/GLContext.cpp
) )


if(USE_EGL) if(ENABLE_EGL)
target_sources(common PRIVATE GL/GLInterface/EGL.cpp) target_sources(common PRIVATE GL/GLInterface/EGL.cpp)
if(ANDROID) if(ANDROID)
target_sources(common PRIVATE GL/GLInterface/EGLAndroid.cpp) target_sources(common PRIVATE GL/GLInterface/EGLAndroid.cpp)
elseif(USE_X11)
target_sources(common PRIVATE GL/GLInterface/EGLX11.cpp)
endif() endif()
target_link_libraries(common PUBLIC EGL) target_link_libraries(common PUBLIC EGL)
endif() endif()
Expand All @@ -130,15 +128,18 @@ if(WIN32)
) )
elseif(APPLE) elseif(APPLE)
target_sources(common PRIVATE GL/GLInterface/AGL.mm) target_sources(common PRIVATE GL/GLInterface/AGL.mm)
elseif(USE_X11) elseif(ENABLE_X11)
if (NOT USE_EGL) target_sources(common PRIVATE
target_sources(common PRIVATE GL/GLInterface/GLX.cpp) GL/GLX11Window.cpp
# GLX has a hard dependency on libGL. GL/GLInterface/GLX.cpp)
# Make sure to link to it if using GLX.
target_link_libraries(common PUBLIC ${OPENGL_LIBRARIES}) # GLX has a hard dependency on libGL.
# Make sure to link to it if using GLX.
target_link_libraries(common PUBLIC ${OPENGL_LIBRARIES})

if (ENABLE_EGL)
target_sources(common PRIVATE GL/GLInterface/EGLX11.cpp)
endif() endif()
target_sources(common PRIVATE GL/GLX11Window.cpp)
target_link_libraries(common PUBLIC ${XRANDR_LIBRARIES})
endif() endif()


if(CMAKE_SYSTEM_NAME STREQUAL "Linux") if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
Expand Down
36 changes: 19 additions & 17 deletions Source/Core/Common/GL/GLContext.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@
#include "Common/GL/GLInterface/AGL.h" #include "Common/GL/GLInterface/AGL.h"
#elif defined(_WIN32) #elif defined(_WIN32)
#include "Common/GL/GLInterface/WGL.h" #include "Common/GL/GLInterface/WGL.h"
#elif defined(ANDROID)
#include "Common/GL/GLInterface/EGLAndroid.h"
#elif HAVE_X11 #elif HAVE_X11
#if defined(USE_EGL) && USE_EGL
#include "Common/GL/GLInterface/EGLX11.h" #include "Common/GL/GLInterface/EGLX11.h"
#else
#include "Common/GL/GLInterface/GLX.h" #include "Common/GL/GLInterface/GLX.h"
#endif #elif HAVE_EGL
#elif defined(USE_EGL) && USE_EGL && defined(USE_HEADLESS)
#include "Common/GL/GLInterface/EGL.h" #include "Common/GL/GLInterface/EGL.h"
#elif ANDROID
#include "Common/GL/GLInterface/EGLAndroid.h"
#error Platform doesnt have a GLInterface
#endif #endif


GLContext::~GLContext() = default; GLContext::~GLContext() = default;
Expand Down Expand Up @@ -71,26 +67,32 @@ void* GLContext::GetFuncAddress(const std::string& name)
return nullptr; return nullptr;
} }


std::unique_ptr<GLContext> GLContext::Create(const WindowSystemInfo& wsi, bool stereo, bool core) std::unique_ptr<GLContext> GLContext::Create(const WindowSystemInfo& wsi, bool stereo, bool core,
bool prefer_egl, bool prefer_gles)
{ {
std::unique_ptr<GLContext> context; std::unique_ptr<GLContext> context;
#if defined(__APPLE__) #if defined(__APPLE__)
context = std::make_unique<GLContextAGL>(); context = std::make_unique<GLContextAGL>();
#elif defined(_WIN32) #elif defined(_WIN32)
context = std::make_unique<GLContextWGL>(); context = std::make_unique<GLContextWGL>();
#elif defined(USE_EGL) && defined(USE_HEADLESS) #elif defined(ANDROID)
context = std::make_unique<GLContextEGL>();
#elif defined(HAVE_X11) && HAVE_X11
#if defined(USE_EGL) && USE_EGL
context = std::make_unique<GLContextEGLX11>();
#else
context = std::make_unique<GLContextGLX>();
#endif
#elif ANDROID
context = std::make_unique<GLContextEGLAndroid>(); context = std::make_unique<GLContextEGLAndroid>();
#elif HAVE_X11
// GLES is not supported via GLX?
if (prefer_egl || prefer_gles)
context = std::make_unique<GLContextEGLX11>();
else
context = std::make_unique<GLContextGLX>();
#elif HAVE_EGL
context = std::make_unique<GLContextEGL>();
#else #else
return nullptr; return nullptr;
#endif #endif

// Option to prefer GLES on desktop platforms, useful for testing.
if (prefer_gles)
context->m_opengl_mode = Mode::OpenGLES;

if (!context->Initialize(wsi.display_connection, wsi.render_surface, stereo, core)) if (!context->Initialize(wsi.display_connection, wsi.render_surface, stereo, core))
return nullptr; return nullptr;


Expand Down
3 changes: 2 additions & 1 deletion Source/Core/Common/GL/GLContext.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class GLContext
// Creates an instance of GLContext specific to the platform we are running on. // Creates an instance of GLContext specific to the platform we are running on.
// If successful, the context is made current on the calling thread. // If successful, the context is made current on the calling thread.
static std::unique_ptr<GLContext> Create(const WindowSystemInfo& wsi, bool stereo = false, static std::unique_ptr<GLContext> Create(const WindowSystemInfo& wsi, bool stereo = false,
bool core = true); bool core = true, bool prefer_egl = false,
bool prefer_gles = false);


protected: protected:
virtual bool Initialize(void* display_handle, void* window_handle, bool stereo, bool core); virtual bool Initialize(void* display_handle, void* window_handle, bool stereo, bool core);
Expand Down
26 changes: 5 additions & 21 deletions Source/Core/Common/GL/GLInterface/EGL.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@


#include "Common/GL/GLInterface/EGL.h" #include "Common/GL/GLInterface/EGL.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "Core/Config/GraphicsSettings.h"


#ifndef EGL_KHR_create_context #ifndef EGL_KHR_create_context
#define EGL_KHR_create_context 1 #define EGL_KHR_create_context 1
Expand Down Expand Up @@ -56,8 +55,6 @@ void* GLContextEGL::GetFuncAddress(const std::string& name)


void GLContextEGL::DetectMode(bool has_handle) void GLContextEGL::DetectMode(bool has_handle)
{ {
bool preferGLES = Config::Get(Config::GFX_PREFER_GLES);

EGLint num_configs; EGLint num_configs;
bool supportsGL = false, supportsGLES3 = false; bool supportsGL = false, supportsGLES3 = false;
std::array<int, 3> renderable_types{{EGL_OPENGL_BIT, EGL_OPENGL_ES3_BIT_KHR}}; std::array<int, 3> renderable_types{{EGL_OPENGL_BIT, EGL_OPENGL_ES3_BIT_KHR}};
Expand Down Expand Up @@ -111,30 +108,17 @@ void GLContextEGL::DetectMode(bool has_handle)
delete[] config; delete[] config;
} }


if (preferGLES) if (supportsGL)
{
if (supportsGLES3)
m_opengl_mode = Mode::OpenGLES;
else if (supportsGL)
m_opengl_mode = Mode::OpenGL;
}
else
{
if (supportsGL)
m_opengl_mode = Mode::OpenGL;
else if (supportsGLES3)
m_opengl_mode = Mode::OpenGLES;
}

if (m_opengl_mode == Mode::OpenGL)
{ {
INFO_LOG(VIDEO, "Using OpenGL"); INFO_LOG(VIDEO, "Using OpenGL");
m_opengl_mode = Mode::OpenGL;
} }
else if (m_opengl_mode == Mode::OpenGLES) else if (supportsGLES3)
{ {
INFO_LOG(VIDEO, "Using OpenGL|ES"); INFO_LOG(VIDEO, "Using OpenGL|ES");
m_opengl_mode = Mode::OpenGLES;
} }
else if (m_opengl_mode == Mode::Detect) else
{ {
// Errored before we found a mode // Errored before we found a mode
ERROR_LOG(VIDEO, "Error: Failed to detect OpenGL flavour, falling back to OpenGL"); ERROR_LOG(VIDEO, "Error: Failed to detect OpenGL flavour, falling back to OpenGL");
Expand Down
4 changes: 3 additions & 1 deletion Source/Core/UICommon/CMakeLists.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ if ((DEFINED CMAKE_ANDROID_ARCH_ABI AND CMAKE_ANDROID_ARCH_ABI MATCHES "x86|x86_
target_link_libraries(uicommon PRIVATE bdisasm) target_link_libraries(uicommon PRIVATE bdisasm)
endif() endif()


if(USE_X11) if(ENABLE_X11)
target_include_directories(uicommon PRIVATE ${X11_INCLUDE_DIR})
target_sources(uicommon PRIVATE X11Utils.cpp) target_sources(uicommon PRIVATE X11Utils.cpp)
target_link_libraries(uicommon PUBLIC ${XRANDR_LIBRARIES})
endif() endif()


if(LIBUSB_FOUND) if(LIBUSB_FOUND)
Expand Down
5 changes: 4 additions & 1 deletion Source/Core/VideoBackends/OGL/main.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Make AA apply instantly during gameplay if possible
#include "Common/GL/GLUtil.h" #include "Common/GL/GLUtil.h"
#include "Common/MsgHandler.h" #include "Common/MsgHandler.h"


#include "Core/Config/GraphicsSettings.h"

#include "VideoBackends/OGL/BoundingBox.h" #include "VideoBackends/OGL/BoundingBox.h"
#include "VideoBackends/OGL/PerfQuery.h" #include "VideoBackends/OGL/PerfQuery.h"
#include "VideoBackends/OGL/ProgramShaderCache.h" #include "VideoBackends/OGL/ProgramShaderCache.h"
Expand Down Expand Up @@ -162,7 +164,8 @@ bool VideoBackend::Initialize(const WindowSystemInfo& wsi)
InitializeShared(); InitializeShared();


std::unique_ptr<GLContext> main_gl_context = std::unique_ptr<GLContext> main_gl_context =
GLContext::Create(wsi, g_ActiveConfig.stereo_mode == StereoMode::QuadBuffer); GLContext::Create(wsi, g_ActiveConfig.stereo_mode == StereoMode::QuadBuffer, true, false,
Config::Get(Config::GFX_PREFER_GLES));
if (!main_gl_context) if (!main_gl_context)
return false; return false;


Expand Down

0 comments on commit 0559311

Please sign in to comment.