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
|
// Copyright 2012 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <array>
#include <sstream>
#include "Common/GL/GLInterface/GLX.h"
#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*);
typedef void (*PFNGLXSWAPINTERVALEXTPROC)(Display*, GLXDrawable, int);
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;
}
void cInterfaceGLX::SwapInterval(int Interval)
{
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));
else
ERROR_LOG(VIDEO, "No support for SwapInterval (framerate clamped to monitor refresh rate).");
}
void* cInterfaceGLX::GetFuncAddress(const std::string& name)
{
return (void*)glXGetProcAddress((const GLubyte*)name.c_str());
}
void cInterfaceGLX::Swap()
{
glXSwapBuffers(dpy, win);
}
// Create rendering window.
// Call browser: Core.cpp:EmuThread() > main.cpp:Video_Initialize()
bool cInterfaceGLX::Create(void* window_handle, bool stereo, bool core)
{
m_has_handle = !!window_handle;
m_host_window = (Window)window_handle;
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,
GLX_STEREO,
stereo ? True : False,
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.
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}};
ctx = nullptr;
if (core)
{
ctx = glXCreateContextAttribs(dpy, fbconfig, 0, True, &context_attribs[0]);
XSync(dpy, False);
m_attribs.insert(m_attribs.end(), context_attribs.begin(), context_attribs.end());
}
if (core && (!ctx || s_glxError))
{
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}};
s_glxError = false;
ctx = glXCreateContextAttribs(dpy, fbconfig, 0, True, &context_attribs_33[0]);
XSync(dpy, False);
m_attribs.clear();
m_attribs.insert(m_attribs.end(), context_attribs_33.begin(), context_attribs_33.end());
}
if (!ctx || 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;
ctx = glXCreateContextAttribs(dpy, fbconfig, 0, True, &context_attribs_legacy[0]);
XSync(dpy, False);
m_attribs.clear();
m_attribs.insert(m_attribs.end(), context_attribs_legacy.begin(), context_attribs_legacy.end());
}
if (!ctx || s_glxError)
{
ERROR_LOG(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(dpy, 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())
{
ERROR_LOG(VIDEO, "Error: CreateWindowSurface failed\n");
XSetErrorHandler(oldHandler);
return false;
}
XSetErrorHandler(oldHandler);
return true;
}
bool cInterfaceGLX::Create(cInterfaceBase* main_context)
{
cInterfaceGLX* glx_context = static_cast<cInterfaceGLX*>(main_context);
m_has_handle = false;
m_supports_pbuffer = glx_context->m_supports_pbuffer;
dpy = glx_context->dpy;
fbconfig = glx_context->fbconfig;
s_glxError = false;
XErrorHandler oldHandler = XSetErrorHandler(&ctxErrorHandler);
ctx = glXCreateContextAttribs(dpy, fbconfig, glx_context->ctx, True, &glx_context->m_attribs[0]);
XSync(dpy, False);
if (!ctx || s_glxError)
{
ERROR_LOG(VIDEO, "Unable to create GL context.");
XSetErrorHandler(oldHandler);
return false;
}
if (m_supports_pbuffer && !CreateWindowSurface())
{
ERROR_LOG(VIDEO, "Error: CreateWindowSurface failed\n");
XSetErrorHandler(oldHandler);
return false;
}
XSetErrorHandler(oldHandler);
return true;
}
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);
}
else if (m_supports_pbuffer)
{
win = m_pbuffer = glXCreateGLXPbufferSGIX(dpy, fbconfig, 1, 1, nullptr);
if (!m_pbuffer)
return false;
}
return true;
}
void cInterfaceGLX::DestroyWindowSurface()
{
if (m_has_handle)
{
XWindow.DestroyXWindow();
}
else if (m_supports_pbuffer && m_pbuffer)
{
glXDestroyGLXPbufferSGIX(dpy, m_pbuffer);
m_pbuffer = 0;
}
}
bool cInterfaceGLX::MakeCurrent()
{
return glXMakeCurrent(dpy, win, ctx);
}
bool cInterfaceGLX::ClearCurrent()
{
return glXMakeCurrent(dpy, None, nullptr);
}
// Close backend
void cInterfaceGLX::Shutdown()
{
DestroyWindowSurface();
if (ctx)
{
glXDestroyContext(dpy, ctx);
// 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;
}
}
}
|