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
|
/*
Copyright (C) 2003-2012 Andrey Nazarov
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
//
// glx.c -- support for GLX extensions
//
#include "video.h"
#include <SDL_syswm.h>
#include <GL/glx.h>
#include <GL/glxext.h>
#define QGLX_EXT_swap_control (1<<0)
#define QGLX_EXT_swap_control_tear (1<<1)
#define QGLX_SGI_video_sync (1<<2)
// for debugging
#define SHOW_SYNC() \
Com_DDDDPrintf("%s: %u\n", __func__, glx_sync_count)
static const char *(*qglXQueryExtensionsString)(Display *, int);
static GLXDrawable (*qglXGetCurrentDrawable)(void);
static int (*qglXSwapIntervalEXT)(Display *, GLXDrawable, int);
static int (*qglXGetVideoSyncSGI)(unsigned int *);
static int (*qglXWaitVideoSyncSGI)(int, int, unsigned int *);
static Display *glx_dpy;
static unsigned glx_extensions;
static unsigned glx_sync_count;
static cvar_t *gl_swapinterval;
static cvar_t *gl_video_sync;
static unsigned GLX_ParseExtensionString(const char *s)
{
static const char *const extnames[] = {
"GLX_EXT_swap_control",
"GLX_EXT_swap_control_tear",
"GLX_SGI_video_sync",
NULL
};
return Com_ParseExtensionString(s, extnames);
}
static void gl_swapinterval_changed(cvar_t *self)
{
int drawable;
if (!qglXSwapIntervalEXT)
return;
if (!qglXGetCurrentDrawable)
return;
drawable = qglXGetCurrentDrawable();
if (!drawable)
return;
if (self->integer < 0 && !(glx_extensions & QGLX_EXT_swap_control_tear)) {
Com_Printf("Negative swap interval is not supported on this system.\n");
Cvar_Reset(self);
}
qglXSwapIntervalEXT(glx_dpy, drawable, self->integer);
}
void VID_GLX_SurfaceChanged(void)
{
SDL_SysWMinfo info;
const char *extensions;
SDL_VERSION(&info.version);
if (!SDL_GetWMInfo(&info))
return;
if (info.subsystem != SDL_SYSWM_X11)
return;
glx_dpy = info.info.x11.display;
if (!glx_dpy)
return;
gl_swapinterval = Cvar_Get("gl_swapinterval", "1", CVAR_ARCHIVE);
gl_video_sync = Cvar_Get("gl_video_sync", "1", CVAR_REFRESH);
qglXQueryExtensionsString = SDL_GL_GetProcAddress("glXQueryExtensionsString");
qglXGetCurrentDrawable = SDL_GL_GetProcAddress("glXGetCurrentDrawable");
if (qglXQueryExtensionsString)
extensions = qglXQueryExtensionsString(glx_dpy, DefaultScreen(glx_dpy));
else
extensions = NULL;
glx_extensions = GLX_ParseExtensionString(extensions);
if (glx_extensions & QGLX_EXT_swap_control) {
if (glx_extensions & QGLX_EXT_swap_control_tear)
Com_Printf("...enabling GLX_EXT_swap_control(_tear)\n");
else
Com_Printf("...enabling GLX_EXT_swap_control\n");
qglXSwapIntervalEXT = SDL_GL_GetProcAddress("glXSwapIntervalEXT");
gl_swapinterval->changed = gl_swapinterval_changed;
gl_swapinterval_changed(gl_swapinterval);
} else {
Com_Printf("GLX_EXT_swap_control not found\n");
Cvar_Set("gl_swapinterval", "0");
}
if (glx_extensions & QGLX_SGI_video_sync) {
if (gl_video_sync->integer) {
Com_Printf("...enabling GLX_SGI_video_sync\n");
qglXGetVideoSyncSGI = SDL_GL_GetProcAddress("glXGetVideoSyncSGI");
qglXWaitVideoSyncSGI = SDL_GL_GetProcAddress("glXWaitVideoSyncSGI");
if (qglXGetVideoSyncSGI) {
qglXGetVideoSyncSGI(&glx_sync_count);
SHOW_SYNC();
sdl.flags |= QVF_VIDEOSYNC;
}
} else {
Com_Printf("...ignoring GLX_SGI_video_sync\n");
}
} else if (gl_video_sync->integer) {
Com_Printf("GLX_SGI_video_sync not found\n");
Cvar_Set("gl_video_sync", "0");
}
}
void VID_Shutdown(void)
{
glx_dpy = NULL;
glx_extensions = 0;
glx_sync_count = 0;
if (gl_swapinterval)
gl_swapinterval->changed = NULL;
qglXQueryExtensionsString = NULL;
qglXGetCurrentDrawable = NULL;
qglXSwapIntervalEXT = NULL;
qglXGetVideoSyncSGI = NULL;
qglXWaitVideoSyncSGI = NULL;
VID_SDL_Shutdown();
}
void VID_VideoWait(void)
{
if (!qglXGetVideoSyncSGI)
return;
if (!qglXWaitVideoSyncSGI)
return;
// work around glXWaitVideoSyncSGI blocking indefinitely if vsync is enabled
if (gl_swapinterval->integer)
qglXGetVideoSyncSGI(&glx_sync_count);
else
qglXWaitVideoSyncSGI(1, 0, &glx_sync_count);
SHOW_SYNC();
}
qboolean VID_VideoSync(void)
{
unsigned count;
if (!qglXGetVideoSyncSGI)
return qtrue;
if (qglXGetVideoSyncSGI(&count))
return qtrue;
if (count != glx_sync_count) {
glx_sync_count = count;
SHOW_SYNC();
return qtrue;
}
return qfalse;
}
void VID_BeginFrame(void)
{
}
void VID_EndFrame(void)
{
SDL_GL_SwapBuffers();
if (qglXGetVideoSyncSGI) {
qglXGetVideoSyncSGI(&glx_sync_count);
SHOW_SYNC();
}
}
|