2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2012 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2014-02-10 18:54:46 +00:00
|
|
|
// Refer to the license.txt file included.
|
2012-12-17 21:01:52 +00:00
|
|
|
|
2016-01-23 10:21:53 +00:00
|
|
|
#include <array>
|
|
|
|
#include <sstream>
|
2014-03-12 19:33:41 +00:00
|
|
|
|
2015-09-18 16:40:00 +00:00
|
|
|
#include "Common/GL/GLInterface/GLX.h"
|
2015-09-18 17:53:32 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2012-12-17 21:01:52 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
|
|
|
|
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
|
2014-12-20 09:57:34 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
typedef GLXContext (*PFNGLXCREATECONTEXTATTRIBSPROC)(Display*, GLXFBConfig, GLXContext, Bool,
|
|
|
|
const int*);
|
2018-03-26 12:09:22 +00:00
|
|
|
typedef void (*PFNGLXSWAPINTERVALEXTPROC)(Display*, GLXDrawable, int);
|
|
|
|
typedef int (*PFNGLXSWAPINTERVALMESAPROC)(unsigned int);
|
2014-12-20 09:57:34 +00:00
|
|
|
|
|
|
|
static PFNGLXCREATECONTEXTATTRIBSPROC glXCreateContextAttribs = nullptr;
|
2018-03-26 12:09:22 +00:00
|
|
|
static PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXTPtr = nullptr;
|
|
|
|
static PFNGLXSWAPINTERVALMESAPROC glXSwapIntervalMESAPtr = nullptr;
|
2013-12-30 13:22:50 +00:00
|
|
|
|
2016-01-23 10:21:53 +00:00
|
|
|
static PFNGLXCREATEGLXPBUFFERSGIXPROC glXCreateGLXPbufferSGIX = nullptr;
|
|
|
|
static PFNGLXDESTROYGLXPBUFFERSGIXPROC glXDestroyGLXPbufferSGIX = nullptr;
|
|
|
|
|
2014-12-20 09:57:34 +00:00
|
|
|
static bool s_glxError;
|
2016-06-24 08:43:46 +00:00
|
|
|
static int ctxErrorHandler(Display* dpy, XErrorEvent* ev)
|
2014-12-20 09:57:34 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
s_glxError = true;
|
|
|
|
return 0;
|
2014-12-20 09:57:34 +00:00
|
|
|
}
|
|
|
|
|
2013-01-24 16:31:08 +00:00
|
|
|
void cInterfaceGLX::SwapInterval(int Interval)
|
|
|
|
{
|
2018-03-26 12:09:22 +00:00
|
|
|
if (!m_has_handle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Try EXT_swap_control, then MESA_swap_control.
|
|
|
|
if (glXSwapIntervalEXTPtr)
|
|
|
|
glXSwapIntervalEXTPtr(dpy, win, Interval);
|
|
|
|
else if (glXSwapIntervalMESAPtr)
|
|
|
|
glXSwapIntervalMESAPtr(static_cast<unsigned int>(Interval));
|
2016-06-24 08:43:46 +00:00
|
|
|
else
|
|
|
|
ERROR_LOG(VIDEO, "No support for SwapInterval (framerate clamped to monitor refresh rate).");
|
2013-01-24 16:31:08 +00:00
|
|
|
}
|
2018-03-26 12:09:22 +00:00
|
|
|
|
2014-03-17 23:17:12 +00:00
|
|
|
void* cInterfaceGLX::GetFuncAddress(const std::string& name)
|
2013-12-30 13:22:50 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return (void*)glXGetProcAddress((const GLubyte*)name.c_str());
|
2013-12-30 13:22:50 +00:00
|
|
|
}
|
2013-01-24 16:31:08 +00:00
|
|
|
|
2012-12-26 06:34:09 +00:00
|
|
|
void cInterfaceGLX::Swap()
|
2012-12-17 21:01:52 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
glXSwapBuffers(dpy, win);
|
2012-12-17 21:01:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create rendering window.
|
2014-02-17 04:51:41 +00:00
|
|
|
// Call browser: Core.cpp:EmuThread() > main.cpp:Video_Initialize()
|
2017-06-26 10:32:09 +00:00
|
|
|
bool cInterfaceGLX::Create(void* window_handle, bool stereo, bool core)
|
2012-12-17 21:01:52 +00:00
|
|
|
{
|
2016-01-23 10:21:53 +00:00
|
|
|
m_has_handle = !!window_handle;
|
|
|
|
m_host_window = (Window)window_handle;
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
dpy = XOpenDisplay(nullptr);
|
|
|
|
int screen = DefaultScreen(dpy);
|
|
|
|
|
|
|
|
// checking glx version
|
|
|
|
int glxMajorVersion, glxMinorVersion;
|
|
|
|
glXQueryVersion(dpy, &glxMajorVersion, &glxMinorVersion);
|
|
|
|
if (glxMajorVersion < 1 || (glxMajorVersion == 1 && glxMinorVersion < 4))
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "glX-Version %d.%d detected, but need at least 1.4", glxMajorVersion,
|
|
|
|
glxMinorVersion);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// loading core context creation function
|
|
|
|
glXCreateContextAttribs =
|
|
|
|
(PFNGLXCREATECONTEXTATTRIBSPROC)GetFuncAddress("glXCreateContextAttribsARB");
|
|
|
|
if (!glXCreateContextAttribs)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO,
|
|
|
|
"glXCreateContextAttribsARB not found, do you support GLX_ARB_create_context?");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// choosing framebuffer
|
|
|
|
int visual_attribs[] = {GLX_X_RENDERABLE,
|
|
|
|
True,
|
|
|
|
GLX_DRAWABLE_TYPE,
|
|
|
|
GLX_WINDOW_BIT,
|
|
|
|
GLX_X_VISUAL_TYPE,
|
|
|
|
GLX_TRUE_COLOR,
|
|
|
|
GLX_RED_SIZE,
|
|
|
|
8,
|
|
|
|
GLX_GREEN_SIZE,
|
|
|
|
8,
|
|
|
|
GLX_BLUE_SIZE,
|
|
|
|
8,
|
|
|
|
GLX_DEPTH_SIZE,
|
|
|
|
0,
|
|
|
|
GLX_STENCIL_SIZE,
|
|
|
|
0,
|
|
|
|
GLX_DOUBLEBUFFER,
|
|
|
|
True,
|
2017-06-26 10:32:09 +00:00
|
|
|
GLX_STEREO,
|
|
|
|
stereo ? True : False,
|
2016-06-24 08:43:46 +00:00
|
|
|
None};
|
|
|
|
int fbcount = 0;
|
|
|
|
GLXFBConfig* fbc = glXChooseFBConfig(dpy, screen, visual_attribs, &fbcount);
|
|
|
|
if (!fbc || !fbcount)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "Failed to retrieve a framebuffer config");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fbconfig = *fbc;
|
|
|
|
XFree(fbc);
|
|
|
|
|
|
|
|
s_glxError = false;
|
|
|
|
XErrorHandler oldHandler = XSetErrorHandler(&ctxErrorHandler);
|
|
|
|
|
|
|
|
// Create a GLX context.
|
|
|
|
// We try to get a 4.0 core profile, else we try 3.3, else try it with anything we get.
|
2016-01-23 10:21:53 +00:00
|
|
|
std::array<int, 9> context_attribs = {
|
|
|
|
{GLX_CONTEXT_MAJOR_VERSION_ARB, 4, GLX_CONTEXT_MINOR_VERSION_ARB, 0,
|
|
|
|
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB, GLX_CONTEXT_FLAGS_ARB,
|
|
|
|
GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, None}};
|
2016-06-24 08:43:46 +00:00
|
|
|
ctx = nullptr;
|
|
|
|
if (core)
|
|
|
|
{
|
2016-01-23 10:21:53 +00:00
|
|
|
ctx = glXCreateContextAttribs(dpy, fbconfig, 0, True, &context_attribs[0]);
|
2016-06-24 08:43:46 +00:00
|
|
|
XSync(dpy, False);
|
2016-01-23 10:21:53 +00:00
|
|
|
m_attribs.insert(m_attribs.end(), context_attribs.begin(), context_attribs.end());
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
if (core && (!ctx || s_glxError))
|
|
|
|
{
|
2016-01-23 10:21:53 +00:00
|
|
|
std::array<int, 9> context_attribs_33 = {
|
|
|
|
{GLX_CONTEXT_MAJOR_VERSION_ARB, 3, GLX_CONTEXT_MINOR_VERSION_ARB, 3,
|
|
|
|
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB, GLX_CONTEXT_FLAGS_ARB,
|
|
|
|
GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, None}};
|
2016-06-24 08:43:46 +00:00
|
|
|
s_glxError = false;
|
2016-01-23 10:21:53 +00:00
|
|
|
ctx = glXCreateContextAttribs(dpy, fbconfig, 0, True, &context_attribs_33[0]);
|
2016-06-24 08:43:46 +00:00
|
|
|
XSync(dpy, False);
|
2016-01-23 10:21:53 +00:00
|
|
|
m_attribs.clear();
|
|
|
|
m_attribs.insert(m_attribs.end(), context_attribs_33.begin(), context_attribs_33.end());
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
if (!ctx || s_glxError)
|
|
|
|
{
|
2016-01-23 10:21:53 +00:00
|
|
|
std::array<int, 5> context_attribs_legacy = {
|
|
|
|
{GLX_CONTEXT_MAJOR_VERSION_ARB, 1, GLX_CONTEXT_MINOR_VERSION_ARB, 0, None}};
|
2016-06-24 08:43:46 +00:00
|
|
|
s_glxError = false;
|
2016-01-23 10:21:53 +00:00
|
|
|
ctx = glXCreateContextAttribs(dpy, fbconfig, 0, True, &context_attribs_legacy[0]);
|
2016-06-24 08:43:46 +00:00
|
|
|
XSync(dpy, False);
|
2016-01-23 10:21:53 +00:00
|
|
|
m_attribs.clear();
|
|
|
|
m_attribs.insert(m_attribs.end(), context_attribs_legacy.begin(), context_attribs_legacy.end());
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
if (!ctx || s_glxError)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "Unable to create GL context.");
|
2017-07-31 12:53:28 +00:00
|
|
|
XSetErrorHandler(oldHandler);
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-26 12:09:22 +00:00
|
|
|
glXSwapIntervalEXTPtr = nullptr;
|
|
|
|
glXSwapIntervalMESAPtr = nullptr;
|
|
|
|
glXCreateGLXPbufferSGIX = nullptr;
|
|
|
|
glXDestroyGLXPbufferSGIX = nullptr;
|
|
|
|
m_supports_pbuffer = false;
|
|
|
|
|
2016-01-23 10:21:53 +00:00
|
|
|
std::string tmp;
|
|
|
|
std::istringstream buffer(glXQueryExtensionsString(dpy, screen));
|
|
|
|
while (buffer >> tmp)
|
|
|
|
{
|
|
|
|
if (tmp == "GLX_SGIX_pbuffer")
|
2018-03-26 12:09:22 +00:00
|
|
|
{
|
|
|
|
glXCreateGLXPbufferSGIX = reinterpret_cast<PFNGLXCREATEGLXPBUFFERSGIXPROC>(
|
|
|
|
GetFuncAddress("glXCreateGLXPbufferSGIX"));
|
|
|
|
glXDestroyGLXPbufferSGIX = reinterpret_cast<PFNGLXDESTROYGLXPBUFFERSGIXPROC>(
|
|
|
|
GetFuncAddress("glXDestroyGLXPbufferSGIX"));
|
|
|
|
m_supports_pbuffer = glXCreateGLXPbufferSGIX && glXDestroyGLXPbufferSGIX;
|
|
|
|
}
|
|
|
|
else if (tmp == "GLX_EXT_swap_control")
|
|
|
|
{
|
|
|
|
glXSwapIntervalEXTPtr =
|
|
|
|
reinterpret_cast<PFNGLXSWAPINTERVALEXTPROC>(GetFuncAddress("glXSwapIntervalEXT"));
|
|
|
|
}
|
|
|
|
else if (tmp == "GLX_MESA_swap_control")
|
|
|
|
{
|
|
|
|
glXSwapIntervalMESAPtr =
|
|
|
|
reinterpret_cast<PFNGLXSWAPINTERVALMESAPROC>(GetFuncAddress("glXSwapIntervalMESA"));
|
|
|
|
}
|
2016-01-23 10:21:53 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-23 10:21:53 +00:00
|
|
|
if (!CreateWindowSurface())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-01-23 10:21:53 +00:00
|
|
|
ERROR_LOG(VIDEO, "Error: CreateWindowSurface failed\n");
|
2017-07-31 12:53:28 +00:00
|
|
|
XSetErrorHandler(oldHandler);
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-07-31 12:53:28 +00:00
|
|
|
|
|
|
|
XSetErrorHandler(oldHandler);
|
2016-01-23 10:21:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cInterfaceGLX::Create(cInterfaceBase* main_context)
|
|
|
|
{
|
|
|
|
cInterfaceGLX* glx_context = static_cast<cInterfaceGLX*>(main_context);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-23 10:21:53 +00:00
|
|
|
m_has_handle = false;
|
2017-07-31 12:53:28 +00:00
|
|
|
m_supports_pbuffer = glx_context->m_supports_pbuffer;
|
2016-01-23 10:21:53 +00:00
|
|
|
dpy = glx_context->dpy;
|
|
|
|
fbconfig = glx_context->fbconfig;
|
|
|
|
s_glxError = false;
|
|
|
|
XErrorHandler oldHandler = XSetErrorHandler(&ctxErrorHandler);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-23 10:21:53 +00:00
|
|
|
ctx = glXCreateContextAttribs(dpy, fbconfig, glx_context->ctx, True, &glx_context->m_attribs[0]);
|
|
|
|
XSync(dpy, False);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-23 10:21:53 +00:00
|
|
|
if (!ctx || s_glxError)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "Unable to create GL context.");
|
2017-07-31 12:53:28 +00:00
|
|
|
XSetErrorHandler(oldHandler);
|
2016-01-23 10:21:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-31 12:53:28 +00:00
|
|
|
if (m_supports_pbuffer && !CreateWindowSurface())
|
2016-01-23 10:21:53 +00:00
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "Error: CreateWindowSurface failed\n");
|
2017-07-31 12:53:28 +00:00
|
|
|
XSetErrorHandler(oldHandler);
|
2016-01-23 10:21:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-07-31 12:53:28 +00:00
|
|
|
|
|
|
|
XSetErrorHandler(oldHandler);
|
2016-06-24 08:43:46 +00:00
|
|
|
return true;
|
2012-12-17 21:01:52 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 10:21:53 +00:00
|
|
|
std::unique_ptr<cInterfaceBase> cInterfaceGLX::CreateSharedContext()
|
|
|
|
{
|
|
|
|
std::unique_ptr<cInterfaceBase> context = std::make_unique<cInterfaceGLX>();
|
|
|
|
if (!context->Create(this))
|
|
|
|
return nullptr;
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cInterfaceGLX::CreateWindowSurface()
|
|
|
|
{
|
|
|
|
if (m_has_handle)
|
|
|
|
{
|
|
|
|
// Get an appropriate visual
|
|
|
|
XVisualInfo* vi = glXGetVisualFromFBConfig(dpy, fbconfig);
|
|
|
|
|
|
|
|
XWindow.Initialize(dpy);
|
|
|
|
|
|
|
|
XWindowAttributes attribs;
|
|
|
|
if (!XGetWindowAttributes(dpy, m_host_window, &attribs))
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "Window attribute retrieval failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_backbuffer_width = attribs.width;
|
|
|
|
s_backbuffer_height = attribs.height;
|
|
|
|
|
|
|
|
win = XWindow.CreateXWindow(m_host_window, vi);
|
|
|
|
XFree(vi);
|
|
|
|
}
|
2017-07-31 12:53:28 +00:00
|
|
|
else if (m_supports_pbuffer)
|
2016-01-23 10:21:53 +00:00
|
|
|
{
|
|
|
|
win = m_pbuffer = glXCreateGLXPbufferSGIX(dpy, fbconfig, 1, 1, nullptr);
|
|
|
|
if (!m_pbuffer)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cInterfaceGLX::DestroyWindowSurface()
|
|
|
|
{
|
2017-07-31 12:53:28 +00:00
|
|
|
if (m_has_handle)
|
2016-01-23 10:21:53 +00:00
|
|
|
{
|
|
|
|
XWindow.DestroyXWindow();
|
|
|
|
}
|
2017-07-31 12:53:28 +00:00
|
|
|
else if (m_supports_pbuffer && m_pbuffer)
|
2016-01-23 10:21:53 +00:00
|
|
|
{
|
|
|
|
glXDestroyGLXPbufferSGIX(dpy, m_pbuffer);
|
|
|
|
m_pbuffer = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-17 21:01:52 +00:00
|
|
|
bool cInterfaceGLX::MakeCurrent()
|
|
|
|
{
|
2018-03-26 12:09:22 +00:00
|
|
|
return glXMakeCurrent(dpy, win, ctx);
|
2012-12-17 21:01:52 +00:00
|
|
|
}
|
2013-04-11 01:32:07 +00:00
|
|
|
|
|
|
|
bool cInterfaceGLX::ClearCurrent()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return glXMakeCurrent(dpy, None, nullptr);
|
2013-04-11 01:32:07 +00:00
|
|
|
}
|
|
|
|
|
2012-12-17 21:01:52 +00:00
|
|
|
// Close backend
|
|
|
|
void cInterfaceGLX::Shutdown()
|
|
|
|
{
|
2016-01-23 10:21:53 +00:00
|
|
|
DestroyWindowSurface();
|
2016-06-24 08:43:46 +00:00
|
|
|
if (ctx)
|
|
|
|
{
|
|
|
|
glXDestroyContext(dpy, ctx);
|
2016-01-23 10:21:53 +00:00
|
|
|
|
|
|
|
// Don't close the display connection if we are a shared context.
|
|
|
|
// Saves doing reference counting on this object, and the main context will always
|
|
|
|
// be shut down last anyway.
|
|
|
|
if (m_has_handle)
|
|
|
|
{
|
|
|
|
XCloseDisplay(dpy);
|
|
|
|
ctx = nullptr;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2012-12-17 21:01:52 +00:00
|
|
|
}
|