-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
GLX.cpp
320 lines (276 loc) · 9.32 KB
/
GLX.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright 2012 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "Common/GL/GLInterface/GLX.h"
#include <array>
#include <sstream>
#include "Common/Logging/Log.h"
#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
typedef GLXContext (*PFNGLXCREATECONTEXTATTRIBSPROC)(Display*, GLXFBConfig, GLXContext, Bool,
const int*);
#ifndef GLX_EXT_swap_control
typedef void (*PFNGLXSWAPINTERVALEXTPROC)(Display*, GLXDrawable, int);
#endif
typedef int (*PFNGLXSWAPINTERVALMESAPROC)(unsigned int);
static PFNGLXCREATECONTEXTATTRIBSPROC glXCreateContextAttribs = nullptr;
static PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXTPtr = nullptr;
static PFNGLXSWAPINTERVALMESAPROC glXSwapIntervalMESAPtr = nullptr;
static PFNGLXCREATEGLXPBUFFERSGIXPROC glXCreateGLXPbufferSGIX = nullptr;
static PFNGLXDESTROYGLXPBUFFERSGIXPROC glXDestroyGLXPbufferSGIX = nullptr;
static bool s_glxError;
static int ctxErrorHandler(Display* dpy, XErrorEvent* ev)
{
s_glxError = true;
return 0;
}
GLContextGLX::~GLContextGLX()
{
DestroyWindowSurface();
if (m_context)
{
if (glXGetCurrentContext() == m_context)
glXMakeCurrent(m_display, None, nullptr);
glXDestroyContext(m_display, m_context);
}
}
bool GLContextGLX::IsHeadless() const
{
return !m_render_window;
}
void GLContextGLX::SwapInterval(int Interval)
{
if (!m_drawable)
return;
// Try EXT_swap_control, then MESA_swap_control.
if (glXSwapIntervalEXTPtr)
{
glXSwapIntervalEXTPtr(m_display, m_drawable, Interval);
}
else if (glXSwapIntervalMESAPtr)
{
glXSwapIntervalMESAPtr(static_cast<unsigned int>(Interval));
}
else
{
ERROR_LOG_FMT(VIDEO,
"No support for SwapInterval (framerate clamped to monitor refresh rate).");
}
}
void* GLContextGLX::GetFuncAddress(const std::string& name)
{
return reinterpret_cast<void*>(glXGetProcAddress(reinterpret_cast<const GLubyte*>(name.c_str())));
}
void GLContextGLX::Swap()
{
glXSwapBuffers(m_display, m_drawable);
}
// Create rendering window.
// Call browser: Core.cpp:EmuThread() > main.cpp:Video_Initialize()
bool GLContextGLX::Initialize(const WindowSystemInfo& wsi, bool stereo, bool core)
{
m_display = static_cast<Display*>(wsi.display_connection);
int screen = DefaultScreen(m_display);
// checking glx version
int glxMajorVersion, glxMinorVersion;
glXQueryVersion(m_display, &glxMajorVersion, &glxMinorVersion);
if (glxMajorVersion < 1 || (glxMajorVersion == 1 && glxMinorVersion < 4))
{
ERROR_LOG_FMT(VIDEO, "glX-Version {}.{} detected, but need at least 1.4", glxMajorVersion,
glxMinorVersion);
return false;
}
// loading core context creation function
glXCreateContextAttribs =
(PFNGLXCREATECONTEXTATTRIBSPROC)GetFuncAddress("glXCreateContextAttribsARB");
if (!glXCreateContextAttribs)
{
ERROR_LOG_FMT(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,
GLX_STEREO,
stereo ? True : False,
None};
int fbcount = 0;
GLXFBConfig* fbc = glXChooseFBConfig(m_display, screen, visual_attribs, &fbcount);
if (!fbc || !fbcount)
{
ERROR_LOG_FMT(VIDEO, "Failed to retrieve a framebuffer config");
return false;
}
m_fbconfig = *fbc;
XFree(fbc);
s_glxError = false;
XErrorHandler oldHandler = XSetErrorHandler(&ctxErrorHandler);
// Create a GLX context.
if (core)
{
for (const auto& version : s_desktop_opengl_versions)
{
std::array<int, 9> context_attribs = {
{GLX_CONTEXT_MAJOR_VERSION_ARB, version.first, GLX_CONTEXT_MINOR_VERSION_ARB,
version.second, GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, None}};
s_glxError = false;
m_context =
glXCreateContextAttribs(m_display, m_fbconfig, nullptr, True, &context_attribs[0]);
XSync(m_display, False);
if (!m_context || s_glxError)
continue;
// Got a context.
INFO_LOG_FMT(VIDEO, "Created a GLX context with version {}.{}", version.first,
version.second);
m_attribs.insert(m_attribs.end(), context_attribs.begin(), context_attribs.end());
break;
}
}
// Failed to create any core contexts, try for anything.
if (!m_context || s_glxError)
{
std::array<int, 5> context_attribs_legacy = {
{GLX_CONTEXT_MAJOR_VERSION_ARB, 1, GLX_CONTEXT_MINOR_VERSION_ARB, 0, None}};
s_glxError = false;
m_context =
glXCreateContextAttribs(m_display, m_fbconfig, nullptr, True, &context_attribs_legacy[0]);
XSync(m_display, False);
m_attribs.clear();
m_attribs.insert(m_attribs.end(), context_attribs_legacy.begin(), context_attribs_legacy.end());
}
if (!m_context || s_glxError)
{
ERROR_LOG_FMT(VIDEO, "Unable to create GL context.");
XSetErrorHandler(oldHandler);
return false;
}
glXSwapIntervalEXTPtr = nullptr;
glXSwapIntervalMESAPtr = nullptr;
glXCreateGLXPbufferSGIX = nullptr;
glXDestroyGLXPbufferSGIX = nullptr;
m_supports_pbuffer = false;
std::string tmp;
std::istringstream buffer(glXQueryExtensionsString(m_display, screen));
while (buffer >> tmp)
{
if (tmp == "GLX_SGIX_pbuffer")
{
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"));
}
}
if (!CreateWindowSurface(reinterpret_cast<Window>(wsi.render_surface)))
{
ERROR_LOG_FMT(VIDEO, "Error: CreateWindowSurface failed\n");
XSetErrorHandler(oldHandler);
return false;
}
XSetErrorHandler(oldHandler);
m_opengl_mode = Mode::OpenGL;
return MakeCurrent();
}
std::unique_ptr<GLContext> GLContextGLX::CreateSharedContext()
{
s_glxError = false;
XErrorHandler oldHandler = XSetErrorHandler(&ctxErrorHandler);
GLXContext new_glx_context =
glXCreateContextAttribs(m_display, m_fbconfig, m_context, True, &m_attribs[0]);
XSync(m_display, False);
if (!new_glx_context || s_glxError)
{
ERROR_LOG_FMT(VIDEO, "Unable to create GL context.");
XSetErrorHandler(oldHandler);
return nullptr;
}
std::unique_ptr<GLContextGLX> new_context = std::make_unique<GLContextGLX>();
new_context->m_context = new_glx_context;
new_context->m_opengl_mode = m_opengl_mode;
new_context->m_supports_pbuffer = m_supports_pbuffer;
new_context->m_display = m_display;
new_context->m_fbconfig = m_fbconfig;
new_context->m_is_shared = true;
if (m_supports_pbuffer && !new_context->CreateWindowSurface(None))
{
ERROR_LOG_FMT(VIDEO, "Error: CreateWindowSurface failed");
XSetErrorHandler(oldHandler);
return nullptr;
}
XSetErrorHandler(oldHandler);
return new_context;
}
bool GLContextGLX::CreateWindowSurface(Window window_handle)
{
if (window_handle)
{
// Get an appropriate visual
XVisualInfo* vi = glXGetVisualFromFBConfig(m_display, m_fbconfig);
m_render_window = GLX11Window::Create(m_display, window_handle, vi);
if (!m_render_window)
return false;
m_backbuffer_width = m_render_window->GetWidth();
m_backbuffer_height = m_render_window->GetHeight();
m_drawable = static_cast<GLXDrawable>(m_render_window->GetWindow());
XFree(vi);
}
else if (m_supports_pbuffer)
{
m_pbuffer = glXCreateGLXPbufferSGIX(m_display, m_fbconfig, 1, 1, nullptr);
if (!m_pbuffer)
return false;
m_drawable = static_cast<GLXDrawable>(m_pbuffer);
}
return true;
}
void GLContextGLX::DestroyWindowSurface()
{
m_render_window.reset();
if (m_supports_pbuffer && m_pbuffer)
{
glXDestroyGLXPbufferSGIX(m_display, m_pbuffer);
m_pbuffer = 0;
}
}
bool GLContextGLX::MakeCurrent()
{
return glXMakeCurrent(m_display, m_drawable, m_context);
}
bool GLContextGLX::ClearCurrent()
{
return glXMakeCurrent(m_display, None, nullptr);
}
void GLContextGLX::Update()
{
m_render_window->UpdateDimensions();
m_backbuffer_width = m_render_window->GetWidth();
m_backbuffer_height = m_render_window->GetHeight();
}