]> git.xonotic.org Git - xonotic/darkplaces.git/blob - vid_shared.c
Remove a lot of OpenGL extension checks.
[xonotic/darkplaces.git] / vid_shared.c
1
2 #include "quakedef.h"
3 #ifdef CONFIG_CD
4 #include "cdaudio.h"
5 #endif
6 #include "image.h"
7
8 #ifdef WIN32
9 //#include <XInput.h>
10 #define XINPUT_GAMEPAD_DPAD_UP          0x0001
11 #define XINPUT_GAMEPAD_DPAD_DOWN        0x0002
12 #define XINPUT_GAMEPAD_DPAD_LEFT        0x0004
13 #define XINPUT_GAMEPAD_DPAD_RIGHT       0x0008
14 #define XINPUT_GAMEPAD_START            0x0010
15 #define XINPUT_GAMEPAD_BACK             0x0020
16 #define XINPUT_GAMEPAD_LEFT_THUMB       0x0040
17 #define XINPUT_GAMEPAD_RIGHT_THUMB      0x0080
18 #define XINPUT_GAMEPAD_LEFT_SHOULDER    0x0100
19 #define XINPUT_GAMEPAD_RIGHT_SHOULDER   0x0200
20 #define XINPUT_GAMEPAD_A                0x1000
21 #define XINPUT_GAMEPAD_B                0x2000
22 #define XINPUT_GAMEPAD_X                0x4000
23 #define XINPUT_GAMEPAD_Y                0x8000
24 #define XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE  7849
25 #define XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE 8689
26 #define XINPUT_GAMEPAD_TRIGGER_THRESHOLD    30
27 #define XUSER_INDEX_ANY                 0x000000FF
28
29 typedef struct xinput_gamepad_s
30 {
31         WORD wButtons;
32         BYTE bLeftTrigger;
33         BYTE bRightTrigger;
34         SHORT sThumbLX;
35         SHORT sThumbLY;
36         SHORT sThumbRX;
37         SHORT sThumbRY;
38 }
39 xinput_gamepad_t;
40
41 typedef struct xinput_state_s
42 {
43         DWORD dwPacketNumber;
44         xinput_gamepad_t Gamepad;
45 }
46 xinput_state_t;
47
48 typedef struct xinput_keystroke_s
49 {
50     WORD    VirtualKey;
51     WCHAR   Unicode;
52     WORD    Flags;
53     BYTE    UserIndex;
54     BYTE    HidCode;
55 }
56 xinput_keystroke_t;
57
58 DWORD (WINAPI *qXInputGetState)(DWORD index, xinput_state_t *state);
59 DWORD (WINAPI *qXInputGetKeystroke)(DWORD index, DWORD reserved, xinput_keystroke_t *keystroke);
60
61 qboolean vid_xinputinitialized = false;
62 int vid_xinputindex = -1;
63 #endif
64
65 // global video state
66 viddef_t vid;
67
68 // AK FIXME -> input_dest
69 qboolean in_client_mouse = true;
70
71 // AK where should it be placed ?
72 float in_mouse_x, in_mouse_y;
73 float in_windowmouse_x, in_windowmouse_y;
74
75 // LordHavoc: if window is hidden, don't update screen
76 qboolean vid_hidden = true;
77 // LordHavoc: if window is not the active window, don't hog as much CPU time,
78 // let go of the mouse, turn off sound, and restore system gamma ramps...
79 qboolean vid_activewindow = true;
80
81 vid_joystate_t vid_joystate;
82
83 #ifdef WIN32
84 cvar_t joy_xinputavailable = {CVAR_READONLY, "joy_xinputavailable", "0", "indicates which devices are being reported by the Windows XInput API (first controller = 1, second = 2, third = 4, fourth = 8, added together)"};
85 #endif
86 cvar_t joy_active = {CVAR_READONLY, "joy_active", "0", "indicates that a joystick is active (detected and enabled)"};
87 cvar_t joy_detected = {CVAR_READONLY, "joy_detected", "0", "number of joysticks detected by engine"};
88 cvar_t joy_enable = {CVAR_SAVE, "joy_enable", "0", "enables joystick support"};
89 cvar_t joy_index = {0, "joy_index", "0", "selects which joystick to use if you have multiple (0 uses the first controller, 1 uses the second, ...)"};
90 cvar_t joy_axisforward = {0, "joy_axisforward", "1", "which joystick axis to query for forward/backward movement"};
91 cvar_t joy_axisside = {0, "joy_axisside", "0", "which joystick axis to query for right/left movement"};
92 cvar_t joy_axisup = {0, "joy_axisup", "-1", "which joystick axis to query for up/down movement"};
93 cvar_t joy_axispitch = {0, "joy_axispitch", "3", "which joystick axis to query for looking up/down"};
94 cvar_t joy_axisyaw = {0, "joy_axisyaw", "2", "which joystick axis to query for looking right/left"};
95 cvar_t joy_axisroll = {0, "joy_axisroll", "-1", "which joystick axis to query for tilting head right/left"};
96 cvar_t joy_deadzoneforward = {0, "joy_deadzoneforward", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
97 cvar_t joy_deadzoneside = {0, "joy_deadzoneside", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
98 cvar_t joy_deadzoneup = {0, "joy_deadzoneup", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
99 cvar_t joy_deadzonepitch = {0, "joy_deadzonepitch", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
100 cvar_t joy_deadzoneyaw = {0, "joy_deadzoneyaw", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
101 cvar_t joy_deadzoneroll = {0, "joy_deadzoneroll", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
102 cvar_t joy_sensitivityforward = {0, "joy_sensitivityforward", "-1", "movement multiplier"};
103 cvar_t joy_sensitivityside = {0, "joy_sensitivityside", "1", "movement multiplier"};
104 cvar_t joy_sensitivityup = {0, "joy_sensitivityup", "1", "movement multiplier"};
105 cvar_t joy_sensitivitypitch = {0, "joy_sensitivitypitch", "1", "movement multiplier"};
106 cvar_t joy_sensitivityyaw = {0, "joy_sensitivityyaw", "-1", "movement multiplier"};
107 cvar_t joy_sensitivityroll = {0, "joy_sensitivityroll", "1", "movement multiplier"};
108 cvar_t joy_axiskeyevents = {CVAR_SAVE, "joy_axiskeyevents", "0", "generate uparrow/leftarrow etc. keyevents for joystick axes, use if your joystick driver is not generating them"};
109 cvar_t joy_axiskeyevents_deadzone = {CVAR_SAVE, "joy_axiskeyevents_deadzone", "0.5", "deadzone value for axes"};
110 cvar_t joy_x360_axisforward = {0, "joy_x360_axisforward", "1", "which joystick axis to query for forward/backward movement"};
111 cvar_t joy_x360_axisside = {0, "joy_x360_axisside", "0", "which joystick axis to query for right/left movement"};
112 cvar_t joy_x360_axisup = {0, "joy_x360_axisup", "-1", "which joystick axis to query for up/down movement"};
113 cvar_t joy_x360_axispitch = {0, "joy_x360_axispitch", "3", "which joystick axis to query for looking up/down"};
114 cvar_t joy_x360_axisyaw = {0, "joy_x360_axisyaw", "2", "which joystick axis to query for looking right/left"};
115 cvar_t joy_x360_axisroll = {0, "joy_x360_axisroll", "-1", "which joystick axis to query for tilting head right/left"};
116 cvar_t joy_x360_deadzoneforward = {0, "joy_x360_deadzoneforward", "0.266", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
117 cvar_t joy_x360_deadzoneside = {0, "joy_x360_deadzoneside", "0.266", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
118 cvar_t joy_x360_deadzoneup = {0, "joy_x360_deadzoneup", "0.266", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
119 cvar_t joy_x360_deadzonepitch = {0, "joy_x360_deadzonepitch", "0.266", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
120 cvar_t joy_x360_deadzoneyaw = {0, "joy_x360_deadzoneyaw", "0.266", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
121 cvar_t joy_x360_deadzoneroll = {0, "joy_x360_deadzoneroll", "0.266", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
122 cvar_t joy_x360_sensitivityforward = {0, "joy_x360_sensitivityforward", "1", "movement multiplier"};
123 cvar_t joy_x360_sensitivityside = {0, "joy_x360_sensitivityside", "1", "movement multiplier"};
124 cvar_t joy_x360_sensitivityup = {0, "joy_x360_sensitivityup", "1", "movement multiplier"};
125 cvar_t joy_x360_sensitivitypitch = {0, "joy_x360_sensitivitypitch", "-1", "movement multiplier"};
126 cvar_t joy_x360_sensitivityyaw = {0, "joy_x360_sensitivityyaw", "-1", "movement multiplier"};
127 cvar_t joy_x360_sensitivityroll = {0, "joy_x360_sensitivityroll", "1", "movement multiplier"};
128
129 // VorteX: more info cvars, mostly set in VID_CheckExtensions
130 cvar_t gl_info_vendor = {CVAR_READONLY, "gl_info_vendor", "", "indicates brand of graphics chip"};
131 cvar_t gl_info_renderer = {CVAR_READONLY, "gl_info_renderer", "", "indicates graphics chip model and other information"};
132 cvar_t gl_info_version = {CVAR_READONLY, "gl_info_version", "", "indicates version of current renderer. begins with 1.0.0, 1.1.0, 1.2.0, 1.3.1 etc."};
133 cvar_t gl_info_extensions = {CVAR_READONLY, "gl_info_extensions", "", "indicates extension list found by engine, space separated."};
134 cvar_t gl_info_platform = {CVAR_READONLY, "gl_info_platform", "", "indicates GL platform: WGL, GLX, or AGL."};
135 cvar_t gl_info_driver = {CVAR_READONLY, "gl_info_driver", "", "name of driver library (opengl32.dll, libGL.so.1, or whatever)."};
136
137 cvar_t vid_fullscreen = {CVAR_SAVE, "vid_fullscreen", "1", "use fullscreen (1) or windowed (0)"};
138 cvar_t vid_width = {CVAR_SAVE, "vid_width", "640", "resolution"};
139 cvar_t vid_height = {CVAR_SAVE, "vid_height", "480", "resolution"};
140 cvar_t vid_bitsperpixel = {CVAR_READONLY, "vid_bitsperpixel", "32", "how many bits per pixel to render at (this is not currently configurable)"};
141 cvar_t vid_samples = {CVAR_SAVE, "vid_samples", "1", "how many anti-aliasing samples per pixel to request from the graphics driver (4 is recommended, 1 is faster)"};
142 cvar_t vid_refreshrate = {CVAR_SAVE, "vid_refreshrate", "60", "refresh rate to use, in hz (higher values flicker less, if supported by your monitor)"};
143 cvar_t vid_userefreshrate = {CVAR_SAVE, "vid_userefreshrate", "0", "set this to 1 to make vid_refreshrate used, or to 0 to let the engine choose a sane default"};
144 cvar_t vid_stereobuffer = {CVAR_SAVE, "vid_stereobuffer", "0", "enables 'quad-buffered' stereo rendering for stereo shutterglasses, HMD (head mounted display) devices, or polarized stereo LCDs, if supported by your drivers"};
145 // the density cvars are completely optional, set and use when something needs to have a density-independent size.
146 // TODO: set them when changing resolution, setting them from the commandline will be independent from the resolution - use only if you have a native fixed resolution.
147 // values for the Samsung Galaxy SIII, Snapdragon version: 2.000000 density, 304.799988 xdpi, 303.850464 ydpi
148 cvar_t vid_touchscreen_density = {0, "vid_touchscreen_density", "2.0", "Standard quantized screen density multiplier (see Android documentation for DisplayMetrics), similar values are given on iPhoneOS"};
149 cvar_t vid_touchscreen_xdpi = {0, "vid_touchscreen_xdpi", "300", "Horizontal DPI of the screen (only valid on Android currently)"};
150 cvar_t vid_touchscreen_ydpi = {0, "vid_touchscreen_ydpi", "300", "Vertical DPI of the screen (only valid on Android currently)"};
151
152 cvar_t vid_vsync = {CVAR_SAVE, "vid_vsync", "0", "sync to vertical blank, prevents 'tearing' (seeing part of one frame and part of another on the screen at the same time), automatically disabled when doing timedemo benchmarks"};
153 cvar_t vid_mouse = {CVAR_SAVE, "vid_mouse", "1", "whether to use the mouse in windowed mode (fullscreen always does)"};
154 cvar_t vid_grabkeyboard = {CVAR_SAVE, "vid_grabkeyboard", "0", "whether to grab the keyboard when mouse is active (prevents use of volume control keys, music player keys, etc on some keyboards)"};
155 cvar_t vid_minwidth = {0, "vid_minwidth", "0", "minimum vid_width that is acceptable (to be set in default.cfg in mods)"};
156 cvar_t vid_minheight = {0, "vid_minheight", "0", "minimum vid_height that is acceptable (to be set in default.cfg in mods)"};
157 cvar_t gl_finish = {0, "gl_finish", "0", "make the cpu wait for the graphics processor at the end of each rendered frame (can help with strange input or video lag problems on some machines)"};
158 cvar_t vid_sRGB = {CVAR_SAVE, "vid_sRGB", "0", "if hardware is capable, modify rendering to be gamma corrected for the sRGB color standard (computer monitors, TVs), recommended"};
159 cvar_t vid_sRGB_fallback = {CVAR_SAVE, "vid_sRGB_fallback", "0", "do an approximate sRGB fallback if not properly supported by hardware (2: also use the fallback if framebuffer is 8bit, 3: always use the fallback even if sRGB is supported)"};
160
161 cvar_t vid_touchscreen = {0, "vid_touchscreen", "0", "Use touchscreen-style input (no mouse grab, track mouse motion only while button is down, screen areas for mimicing joystick axes and buttons"};
162 cvar_t vid_touchscreen_showkeyboard = {0, "vid_touchscreen_showkeyboard", "0", "shows the platform's screen keyboard for text entry, can be set by csqc or menu qc if it wants to receive text input, does nothing if the platform has no screen keyboard"};
163 cvar_t vid_touchscreen_supportshowkeyboard = {CVAR_READONLY, "vid_touchscreen_supportshowkeyboard", "0", "indicates if the platform supports a virtual keyboard"};
164 cvar_t vid_stick_mouse = {CVAR_SAVE, "vid_stick_mouse", "0", "have the mouse stuck in the center of the screen" };
165 cvar_t vid_resizable = {CVAR_SAVE, "vid_resizable", "0", "0: window not resizable, 1: resizable, 2: window can be resized but the framebuffer isn't adjusted" };
166 cvar_t vid_desktopfullscreen = {CVAR_SAVE, "vid_desktopfullscreen", "1", "force desktop resolution for fullscreen; also use some OS dependent tricks for better fullscreen integration"};
167
168 cvar_t v_gamma = {CVAR_SAVE, "v_gamma", "1", "inverse gamma correction value, a brightness effect that does not affect white or black, and tends to make the image grey and dull"};
169 cvar_t v_contrast = {CVAR_SAVE, "v_contrast", "1", "brightness of white (values above 1 give a brighter image with increased color saturation, unlike v_gamma)"};
170 cvar_t v_brightness = {CVAR_SAVE, "v_brightness", "0", "brightness of black, useful for monitors that are too dark"};
171 cvar_t v_contrastboost = {CVAR_SAVE, "v_contrastboost", "1", "by how much to multiply the contrast in dark areas (1 is no change)"};
172 cvar_t v_color_enable = {CVAR_SAVE, "v_color_enable", "0", "enables black-grey-white color correction curve controls"};
173 cvar_t v_color_black_r = {CVAR_SAVE, "v_color_black_r", "0", "desired color of black"};
174 cvar_t v_color_black_g = {CVAR_SAVE, "v_color_black_g", "0", "desired color of black"};
175 cvar_t v_color_black_b = {CVAR_SAVE, "v_color_black_b", "0", "desired color of black"};
176 cvar_t v_color_grey_r = {CVAR_SAVE, "v_color_grey_r", "0.5", "desired color of grey"};
177 cvar_t v_color_grey_g = {CVAR_SAVE, "v_color_grey_g", "0.5", "desired color of grey"};
178 cvar_t v_color_grey_b = {CVAR_SAVE, "v_color_grey_b", "0.5", "desired color of grey"};
179 cvar_t v_color_white_r = {CVAR_SAVE, "v_color_white_r", "1", "desired color of white"};
180 cvar_t v_color_white_g = {CVAR_SAVE, "v_color_white_g", "1", "desired color of white"};
181 cvar_t v_color_white_b = {CVAR_SAVE, "v_color_white_b", "1", "desired color of white"};
182 cvar_t v_glslgamma_2d = {CVAR_SAVE, "v_glslgamma_2d", "1", "applies GLSL gamma to 2d pictures (HUD, fonts)"};
183 cvar_t v_psycho = {0, "v_psycho", "0", "easter egg - R.I.P. zinx http://obits.al.com/obituaries/birmingham/obituary.aspx?n=christopher-robert-lais&pid=186080667"};
184
185 // brand of graphics chip
186 const char *gl_vendor;
187 // graphics chip model and other information
188 const char *gl_renderer;
189 // begins with 1.0.0, 1.1.0, 1.2.0, 1.2.1, 1.3.0, 1.3.1, or 1.4.0
190 const char *gl_version;
191 // extensions list, space separated
192 const char *gl_extensions;
193 // WGL, GLX, or AGL
194 const char *gl_platform;
195 // another extensions list, containing platform-specific extensions that are
196 // not in the main list
197 const char *gl_platformextensions;
198 // name of driver library (opengl32.dll, libGL.so.1, or whatever)
199 char gl_driver[256];
200
201 #ifndef USE_GLES2
202 // GL_ARB_multitexture
203 void (GLAPIENTRY *qglActiveTexture) (GLenum);
204
205 // general GL functions
206
207 void (GLAPIENTRY *qglClearColor)(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
208
209 void (GLAPIENTRY *qglClear)(GLbitfield mask);
210
211 void (GLAPIENTRY *qglBlendFunc)(GLenum sfactor, GLenum dfactor);
212 void (GLAPIENTRY *qglCullFace)(GLenum mode);
213
214 void (GLAPIENTRY *qglDrawBuffer)(GLenum mode);
215 void (GLAPIENTRY *qglReadBuffer)(GLenum mode);
216 void (GLAPIENTRY *qglEnable)(GLenum cap);
217 void (GLAPIENTRY *qglDisable)(GLenum cap);
218 GLboolean (GLAPIENTRY *qglIsEnabled)(GLenum cap);
219
220 void (GLAPIENTRY *qglGetBooleanv)(GLenum pname, GLboolean *params);
221 void (GLAPIENTRY *qglGetDoublev)(GLenum pname, GLdouble *params);
222 void (GLAPIENTRY *qglGetFloatv)(GLenum pname, GLfloat *params);
223 void (GLAPIENTRY *qglGetIntegerv)(GLenum pname, GLint *params);
224
225 GLenum (GLAPIENTRY *qglGetError)(void);
226 const GLubyte* (GLAPIENTRY *qglGetString)(GLenum name);
227 void (GLAPIENTRY *qglFinish)(void);
228 void (GLAPIENTRY *qglFlush)(void);
229
230 void (GLAPIENTRY *qglClearDepth)(GLclampd depth);
231 void (GLAPIENTRY *qglDepthFunc)(GLenum func);
232 void (GLAPIENTRY *qglDepthMask)(GLboolean flag);
233 void (GLAPIENTRY *qglDepthRange)(GLclampd near_val, GLclampd far_val);
234 void (GLAPIENTRY *qglDepthRangef)(GLclampf near_val, GLclampf far_val);
235 void (GLAPIENTRY *qglColorMask)(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha);
236
237 void (GLAPIENTRY *qglDrawElements)(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
238 void (GLAPIENTRY *qglDrawArrays)(GLenum mode, GLint first, GLsizei count);
239
240 void (GLAPIENTRY *qglViewport)(GLint x, GLint y, GLsizei width, GLsizei height);
241
242 void (GLAPIENTRY *qglGetPointerv)(GLenum pname, GLvoid** params);
243 void (GLAPIENTRY *qglReadPixels)(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels);
244
245 void (GLAPIENTRY *qglStencilFunc)(GLenum func, GLint ref, GLuint mask);
246 void (GLAPIENTRY *qglStencilMask)(GLuint mask);
247 void (GLAPIENTRY *qglStencilOp)(GLenum fail, GLenum zfail, GLenum zpass);
248 void (GLAPIENTRY *qglClearStencil)(GLint s);
249
250 void (GLAPIENTRY *qglTexParameterf)(GLenum target, GLenum pname, GLfloat param);
251 void (GLAPIENTRY *qglTexParameterfv)(GLenum target, GLenum pname, GLfloat *params);
252 void (GLAPIENTRY *qglTexParameteri)(GLenum target, GLenum pname, GLint param);
253 void (GLAPIENTRY *qglGetTexParameterfv)(GLenum target, GLenum pname, GLfloat *params);
254 void (GLAPIENTRY *qglGetTexParameteriv)(GLenum target, GLenum pname, GLint *params);
255 void (GLAPIENTRY *qglGetTexLevelParameterfv)(GLenum target, GLint level, GLenum pname, GLfloat *params);
256 void (GLAPIENTRY *qglGetTexLevelParameteriv)(GLenum target, GLint level, GLenum pname, GLint *params);
257 void (GLAPIENTRY *qglGetTexImage)(GLenum target, GLint level, GLenum format, GLenum type, GLvoid *pixels);
258 void (GLAPIENTRY *qglHint)(GLenum target, GLenum mode);
259
260 void (GLAPIENTRY *qglGenTextures)(GLsizei n, GLuint *textures);
261 void (GLAPIENTRY *qglDeleteTextures)(GLsizei n, const GLuint *textures);
262 void (GLAPIENTRY *qglBindTexture)(GLenum target, GLuint texture);
263
264 void (GLAPIENTRY *qglPixelStorei)(GLenum pname, GLint param);
265
266 void (GLAPIENTRY *qglTexImage2D)(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels);
267 void (GLAPIENTRY *qglTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels);
268 void (GLAPIENTRY *qglCopyTexImage2D)(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border);
269 void (GLAPIENTRY *qglCopyTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height);
270
271 void (GLAPIENTRY *qglTexImage3D)(GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels);
272 void (GLAPIENTRY *qglTexSubImage3D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels);
273 void (GLAPIENTRY *qglCopyTexSubImage3D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height);
274
275 void (GLAPIENTRY *qglScissor)(GLint x, GLint y, GLsizei width, GLsizei height);
276
277 void (GLAPIENTRY *qglPolygonOffset)(GLfloat factor, GLfloat units);
278 void (GLAPIENTRY *qglPolygonMode)(GLenum face, GLenum mode);
279
280 void (GLAPIENTRY *qglPointSize)(GLfloat size);
281
282 void (GLAPIENTRY *qglBlendEquation)(GLenum);
283
284 void (GLAPIENTRY *qglStencilOpSeparate)(GLenum, GLenum, GLenum, GLenum);
285 void (GLAPIENTRY *qglStencilFuncSeparate)(GLenum, GLenum, GLint, GLuint);
286 void (GLAPIENTRY *qglActiveStencilFace)(GLenum);
287
288 void (GLAPIENTRY *qglDeleteShader)(GLuint obj);
289 void (GLAPIENTRY *qglDeleteProgram)(GLuint obj);
290 void (GLAPIENTRY *qglDetachShader)(GLuint containerObj, GLuint attachedObj);
291 GLuint (GLAPIENTRY *qglCreateShader)(GLenum shaderType);
292 void (GLAPIENTRY *qglShaderSource)(GLuint shaderObj, GLsizei count, const GLchar **string, const GLint *length);
293 void (GLAPIENTRY *qglCompileShader)(GLuint shaderObj);
294 GLuint (GLAPIENTRY *qglCreateProgram)(void);
295 void (GLAPIENTRY *qglAttachShader)(GLuint containerObj, GLuint obj);
296 void (GLAPIENTRY *qglLinkProgram)(GLuint programObj);
297 void (GLAPIENTRY *qglUseProgram)(GLuint programObj);
298 void (GLAPIENTRY *qglValidateProgram)(GLuint programObj);
299 void (GLAPIENTRY *qglUniform1f)(GLint location, GLfloat v0);
300 void (GLAPIENTRY *qglUniform2f)(GLint location, GLfloat v0, GLfloat v1);
301 void (GLAPIENTRY *qglUniform3f)(GLint location, GLfloat v0, GLfloat v1, GLfloat v2);
302 void (GLAPIENTRY *qglUniform4f)(GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);
303 void (GLAPIENTRY *qglUniform1i)(GLint location, GLint v0);
304 void (GLAPIENTRY *qglUniform2i)(GLint location, GLint v0, GLint v1);
305 void (GLAPIENTRY *qglUniform3i)(GLint location, GLint v0, GLint v1, GLint v2);
306 void (GLAPIENTRY *qglUniform4i)(GLint location, GLint v0, GLint v1, GLint v2, GLint v3);
307 void (GLAPIENTRY *qglUniform1fv)(GLint location, GLsizei count, const GLfloat *value);
308 void (GLAPIENTRY *qglUniform2fv)(GLint location, GLsizei count, const GLfloat *value);
309 void (GLAPIENTRY *qglUniform3fv)(GLint location, GLsizei count, const GLfloat *value);
310 void (GLAPIENTRY *qglUniform4fv)(GLint location, GLsizei count, const GLfloat *value);
311 void (GLAPIENTRY *qglUniform1iv)(GLint location, GLsizei count, const GLint *value);
312 void (GLAPIENTRY *qglUniform2iv)(GLint location, GLsizei count, const GLint *value);
313 void (GLAPIENTRY *qglUniform3iv)(GLint location, GLsizei count, const GLint *value);
314 void (GLAPIENTRY *qglUniform4iv)(GLint location, GLsizei count, const GLint *value);
315 void (GLAPIENTRY *qglUniformMatrix2fv)(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
316 void (GLAPIENTRY *qglUniformMatrix3fv)(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
317 void (GLAPIENTRY *qglUniformMatrix4fv)(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
318 void (GLAPIENTRY *qglGetShaderiv)(GLuint obj, GLenum pname, GLint *params);
319 void (GLAPIENTRY *qglGetProgramiv)(GLuint obj, GLenum pname, GLint *params);
320 void (GLAPIENTRY *qglGetShaderInfoLog)(GLuint obj, GLsizei maxLength, GLsizei *length, GLchar *infoLog);
321 void (GLAPIENTRY *qglGetProgramInfoLog)(GLuint obj, GLsizei maxLength, GLsizei *length, GLchar *infoLog);
322 void (GLAPIENTRY *qglGetAttachedShaders)(GLuint containerObj, GLsizei maxCount, GLsizei *count, GLuint *obj);
323 GLint (GLAPIENTRY *qglGetUniformLocation)(GLuint programObj, const GLchar *name);
324 void (GLAPIENTRY *qglGetActiveUniform)(GLuint programObj, GLuint index, GLsizei maxLength, GLsizei *length, GLint *size, GLenum *type, GLchar *name);
325 void (GLAPIENTRY *qglGetUniformfv)(GLuint programObj, GLint location, GLfloat *params);
326 void (GLAPIENTRY *qglGetUniformiv)(GLuint programObj, GLint location, GLint *params);
327 void (GLAPIENTRY *qglGetShaderSource)(GLuint obj, GLsizei maxLength, GLsizei *length, GLchar *source);
328
329 void (GLAPIENTRY *qglVertexAttrib1f)(GLuint index, GLfloat v0);
330 void (GLAPIENTRY *qglVertexAttrib1s)(GLuint index, GLshort v0);
331 void (GLAPIENTRY *qglVertexAttrib1d)(GLuint index, GLdouble v0);
332 void (GLAPIENTRY *qglVertexAttrib2f)(GLuint index, GLfloat v0, GLfloat v1);
333 void (GLAPIENTRY *qglVertexAttrib2s)(GLuint index, GLshort v0, GLshort v1);
334 void (GLAPIENTRY *qglVertexAttrib2d)(GLuint index, GLdouble v0, GLdouble v1);
335 void (GLAPIENTRY *qglVertexAttrib3f)(GLuint index, GLfloat v0, GLfloat v1, GLfloat v2);
336 void (GLAPIENTRY *qglVertexAttrib3s)(GLuint index, GLshort v0, GLshort v1, GLshort v2);
337 void (GLAPIENTRY *qglVertexAttrib3d)(GLuint index, GLdouble v0, GLdouble v1, GLdouble v2);
338 void (GLAPIENTRY *qglVertexAttrib4f)(GLuint index, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);
339 void (GLAPIENTRY *qglVertexAttrib4s)(GLuint index, GLshort v0, GLshort v1, GLshort v2, GLshort v3);
340 void (GLAPIENTRY *qglVertexAttrib4d)(GLuint index, GLdouble v0, GLdouble v1, GLdouble v2, GLdouble v3);
341 void (GLAPIENTRY *qglVertexAttrib4Nub)(GLuint index, GLubyte x, GLubyte y, GLubyte z, GLubyte w);
342 void (GLAPIENTRY *qglVertexAttrib1fv)(GLuint index, const GLfloat *v);
343 void (GLAPIENTRY *qglVertexAttrib1sv)(GLuint index, const GLshort *v);
344 void (GLAPIENTRY *qglVertexAttrib1dv)(GLuint index, const GLdouble *v);
345 void (GLAPIENTRY *qglVertexAttrib2fv)(GLuint index, const GLfloat *v);
346 void (GLAPIENTRY *qglVertexAttrib2sv)(GLuint index, const GLshort *v);
347 void (GLAPIENTRY *qglVertexAttrib2dv)(GLuint index, const GLdouble *v);
348 void (GLAPIENTRY *qglVertexAttrib3fv)(GLuint index, const GLfloat *v);
349 void (GLAPIENTRY *qglVertexAttrib3sv)(GLuint index, const GLshort *v);
350 void (GLAPIENTRY *qglVertexAttrib3dv)(GLuint index, const GLdouble *v);
351 void (GLAPIENTRY *qglVertexAttrib4fv)(GLuint index, const GLfloat *v);
352 void (GLAPIENTRY *qglVertexAttrib4sv)(GLuint index, const GLshort *v);
353 void (GLAPIENTRY *qglVertexAttrib4dv)(GLuint index, const GLdouble *v);
354 void (GLAPIENTRY *qglVertexAttrib4iv)(GLuint index, const GLint *v);
355 void (GLAPIENTRY *qglVertexAttrib4bv)(GLuint index, const GLbyte *v);
356 void (GLAPIENTRY *qglVertexAttrib4ubv)(GLuint index, const GLubyte *v);
357 void (GLAPIENTRY *qglVertexAttrib4usv)(GLuint index, const GLushort *v);
358 void (GLAPIENTRY *qglVertexAttrib4uiv)(GLuint index, const GLuint *v);
359 void (GLAPIENTRY *qglVertexAttrib4Nbv)(GLuint index, const GLbyte *v);
360 void (GLAPIENTRY *qglVertexAttrib4Nsv)(GLuint index, const GLshort *v);
361 void (GLAPIENTRY *qglVertexAttrib4Niv)(GLuint index, const GLint *v);
362 void (GLAPIENTRY *qglVertexAttrib4Nubv)(GLuint index, const GLubyte *v);
363 void (GLAPIENTRY *qglVertexAttrib4Nusv)(GLuint index, const GLushort *v);
364 void (GLAPIENTRY *qglVertexAttrib4Nuiv)(GLuint index, const GLuint *v);
365 void (GLAPIENTRY *qglVertexAttribPointer)(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer);
366 void (GLAPIENTRY *qglEnableVertexAttribArray)(GLuint index);
367 void (GLAPIENTRY *qglDisableVertexAttribArray)(GLuint index);
368 void (GLAPIENTRY *qglBindAttribLocation)(GLuint programObj, GLuint index, const GLchar *name);
369 void (GLAPIENTRY *qglBindFragDataLocation)(GLuint programObj, GLuint index, const GLchar *name);
370 void (GLAPIENTRY *qglGetActiveAttrib)(GLuint programObj, GLuint index, GLsizei maxLength, GLsizei *length, GLint *size, GLenum *type, GLchar *name);
371 GLint (GLAPIENTRY *qglGetAttribLocation)(GLuint programObj, const GLchar *name);
372 void (GLAPIENTRY *qglGetVertexAttribdv)(GLuint index, GLenum pname, GLdouble *params);
373 void (GLAPIENTRY *qglGetVertexAttribfv)(GLuint index, GLenum pname, GLfloat *params);
374 void (GLAPIENTRY *qglGetVertexAttribiv)(GLuint index, GLenum pname, GLint *params);
375 void (GLAPIENTRY *qglGetVertexAttribPointerv)(GLuint index, GLenum pname, GLvoid **pointer);
376
377 //GL_ARB_vertex_buffer_object
378 void (GLAPIENTRY *qglBindBuffer) (GLenum target, GLuint buffer);
379 void (GLAPIENTRY *qglDeleteBuffers) (GLsizei n, const GLuint *buffers);
380 void (GLAPIENTRY *qglGenBuffers) (GLsizei n, GLuint *buffers);
381 GLboolean (GLAPIENTRY *qglIsBuffer) (GLuint buffer);
382 GLvoid* (GLAPIENTRY *qglMapBuffer) (GLenum target, GLenum access);
383 GLboolean (GLAPIENTRY *qglUnmapBuffer) (GLenum target);
384 void (GLAPIENTRY *qglBufferData) (GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage);
385 void (GLAPIENTRY *qglBufferSubData) (GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid *data);
386
387 //GL_ARB_framebuffer_object
388 GLboolean (GLAPIENTRY *qglIsRenderbuffer)(GLuint renderbuffer);
389 GLvoid (GLAPIENTRY *qglBindRenderbuffer)(GLenum target, GLuint renderbuffer);
390 GLvoid (GLAPIENTRY *qglDeleteRenderbuffers)(GLsizei n, const GLuint *renderbuffers);
391 GLvoid (GLAPIENTRY *qglGenRenderbuffers)(GLsizei n, GLuint *renderbuffers);
392 GLvoid (GLAPIENTRY *qglRenderbufferStorage)(GLenum target, GLenum internalformat, GLsizei width, GLsizei height);
393 GLvoid (GLAPIENTRY *qglRenderbufferStorageMultisample)(GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height);
394 GLvoid (GLAPIENTRY *qglGetRenderbufferParameteriv)(GLenum target, GLenum pname, GLint *params);
395 GLboolean (GLAPIENTRY *qglIsFramebuffer)(GLuint framebuffer);
396 GLvoid (GLAPIENTRY *qglBindFramebuffer)(GLenum target, GLuint framebuffer);
397 GLvoid (GLAPIENTRY *qglDeleteFramebuffers)(GLsizei n, const GLuint *framebuffers);
398 GLvoid (GLAPIENTRY *qglGenFramebuffers)(GLsizei n, GLuint *framebuffers);
399 GLenum (GLAPIENTRY *qglCheckFramebufferStatus)(GLenum target);
400 GLvoid (GLAPIENTRY *qglFramebufferTexture1D)(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level);
401 GLvoid (GLAPIENTRY *qglFramebufferTexture2D)(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level);
402 GLvoid (GLAPIENTRY *qglFramebufferTexture3D)(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLint layer);
403 GLvoid (GLAPIENTRY *qglFramebufferTextureLayer)(GLenum target, GLenum attachment, GLuint texture, GLint level, GLint layer);
404 GLvoid (GLAPIENTRY *qglFramebufferRenderbuffer)(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer);
405 GLvoid (GLAPIENTRY *qglGetFramebufferAttachmentParameteriv)(GLenum target, GLenum attachment, GLenum pname, GLint *params);
406 GLvoid (GLAPIENTRY *qglBlitFramebuffer)(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, GLenum filter);
407 GLvoid (GLAPIENTRY *qglGenerateMipmap)(GLenum target);
408
409 void (GLAPIENTRY *qglDrawBuffers)(GLsizei n, const GLenum *bufs);
410
411 void (GLAPIENTRY *qglCompressedTexImage3D)(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const void *data);
412 void (GLAPIENTRY *qglCompressedTexImage2D)(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border,  GLsizei imageSize, const void *data);
413 //void (GLAPIENTRY *qglCompressedTexImage1D)(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLint border, GLsizei imageSize, const void *data);
414 void (GLAPIENTRY *qglCompressedTexSubImage3D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const void *data);
415 void (GLAPIENTRY *qglCompressedTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *data);
416 //void (GLAPIENTRY *qglCompressedTexSubImage1D)(GLenum target, GLint level, GLint xoffset, GLsizei width, GLenum format, GLsizei imageSize, const void *data);
417 void (GLAPIENTRY *qglGetCompressedTexImage)(GLenum target, GLint lod, void *img);
418
419 void (GLAPIENTRY *qglGenQueries)(GLsizei n, GLuint *ids);
420 void (GLAPIENTRY *qglDeleteQueries)(GLsizei n, const GLuint *ids);
421 GLboolean (GLAPIENTRY *qglIsQuery)(GLuint qid);
422 void (GLAPIENTRY *qglBeginQuery)(GLenum target, GLuint qid);
423 void (GLAPIENTRY *qglEndQuery)(GLenum target);
424 void (GLAPIENTRY *qglGetQueryiv)(GLenum target, GLenum pname, GLint *params);
425 void (GLAPIENTRY *qglGetQueryObjectiv)(GLuint qid, GLenum pname, GLint *params);
426 void (GLAPIENTRY *qglGetQueryObjectuiv)(GLuint qid, GLenum pname, GLuint *params);
427
428 void (GLAPIENTRY *qglSampleCoverage)(GLclampf value, GLboolean invert);
429
430 void (GLAPIENTRY *qglGetUniformIndices)(GLuint program, GLsizei uniformCount, const GLchar** uniformNames, GLuint* uniformIndices);
431 void (GLAPIENTRY *qglGetActiveUniformsiv)(GLuint program, GLsizei uniformCount, const GLuint* uniformIndices, GLenum pname, GLint* params);
432 void (GLAPIENTRY *qglGetActiveUniformName)(GLuint program, GLuint uniformIndex, GLsizei bufSize, GLsizei* length, GLchar* uniformName);
433 GLuint (GLAPIENTRY *qglGetUniformBlockIndex)(GLuint program, const GLchar* uniformBlockName);
434 void (GLAPIENTRY *qglGetActiveUniformBlockiv)(GLuint program, GLuint uniformBlockIndex, GLenum pname,  GLint* params);
435 void (GLAPIENTRY *qglGetActiveUniformBlockName)(GLuint program, GLuint uniformBlockIndex, GLsizei bufSize, GLsizei* length, GLchar* uniformBlockName);
436 void (GLAPIENTRY *qglBindBufferRange)(GLenum target, GLuint index, GLuint buffer, GLintptr offset, GLsizeiptr size);
437 void (GLAPIENTRY *qglBindBufferBase)(GLenum target, GLuint index, GLuint buffer);
438 void (GLAPIENTRY *qglGetIntegeri_v)(GLenum target, GLuint index, GLint* data);
439 void (GLAPIENTRY *qglUniformBlockBinding)(GLuint program, GLuint uniformBlockIndex, GLuint uniformBlockBinding);
440
441 void (GLAPIENTRY *qglBlendFuncSeparate)(GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha);
442
443 GLuint (GLAPIENTRY *qglGetDebugMessageLogARB)(GLuint count, GLsizei bufSize, GLenum* sources, GLenum* types, GLuint* ids, GLenum* severities, GLsizei* lengths, GLchar* messageLog);
444 void (GLAPIENTRY *qglDebugMessageCallbackARB)(GLDEBUGPROCARB callback, const GLvoid* userParam);
445 void (GLAPIENTRY *qglDebugMessageControlARB)(GLenum source, GLenum type, GLenum severity, GLsizei count, const GLuint* ids, GLboolean enabled);
446 void (GLAPIENTRY *qglDebugMessageInsertARB)(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* buf);
447 #endif
448
449 #if _MSC_VER >= 1400
450 #define sscanf sscanf_s
451 #endif
452
453 qboolean GL_CheckExtension(const char *minglver_or_ext, const dllfunction_t *funcs, const char *disableparm, int silent)
454 {
455         int failed = false;
456         const dllfunction_t *func;
457         struct { int major, minor; } min_version, curr_version;
458         char extstr[MAX_INPUTLINE];
459         int ext;
460
461         if(sscanf(minglver_or_ext, "%d.%d", &min_version.major, &min_version.minor) == 2)
462                 ext = 0; // opengl version
463         else if(minglver_or_ext[0] != toupper(minglver_or_ext[0]))
464                 ext = -1; // pseudo name
465         else
466                 ext = 1; // extension name
467
468         if (ext)
469                 Con_DPrintf("checking for %s...  ", minglver_or_ext);
470         else
471                 Con_DPrintf("checking for OpenGL %s core features...  ", minglver_or_ext);
472
473         for (func = funcs;func && func->name;func++)
474                 *func->funcvariable = NULL;
475
476         if (disableparm && (COM_CheckParm(disableparm) || COM_CheckParm("-safe")))
477         {
478                 Con_DPrint("disabled by commandline\n");
479                 return false;
480         }
481
482         if (ext == 1) // opengl extension
483         {
484                 if (!strstr(gl_extensions ? gl_extensions : "", minglver_or_ext) && !strstr(gl_platformextensions ? gl_platformextensions : "", minglver_or_ext))
485                 {
486                         Con_DPrint("not detected\n");
487                         return false;
488                 }
489         }
490
491         if(ext == 0) // opengl version
492         {
493                 if (sscanf(gl_version, "%d.%d", &curr_version.major, &curr_version.minor) < 2)
494                         curr_version.major = curr_version.minor = 1;
495
496                 if (curr_version.major < min_version.major || (curr_version.major == min_version.major && curr_version.minor < min_version.minor))
497                 {
498                         Con_DPrintf("not detected (OpenGL %d.%d loaded)\n", curr_version.major, curr_version.minor);
499                         return false;
500                 }
501         }
502
503         for (func = funcs;func && func->name != NULL;func++)
504         {
505                 // Con_DPrintf("\n    %s...  ", func->name);
506
507                 // functions are cleared before all the extensions are evaluated
508                 if (!(*func->funcvariable = (void *) GL_GetProcAddress(func->name)))
509                 {
510                         if (ext && !silent)
511                                 Con_DPrintf("%s is missing function \"%s\" - broken driver!\n", minglver_or_ext, func->name);
512                         if (!ext)
513                                 Con_Printf("OpenGL %s core features are missing function \"%s\" - broken driver!\n", minglver_or_ext, func->name);
514                         failed = true;
515                 }
516         }
517         // delay the return so it prints all missing functions
518         if (failed)
519                 return false;
520         // VorteX: add to found extension list
521         dpsnprintf(extstr, sizeof(extstr), "%s %s ", gl_info_extensions.string, minglver_or_ext);
522         Cvar_SetQuick(&gl_info_extensions, extstr);
523
524         Con_DPrint("enabled\n");
525         return true;
526 }
527
528 #ifndef USE_GLES2
529 // functions we require from the driver - some of these are OpenGL 2.0, some are a bit newer
530 static dllfunction_t openglfuncs[] =
531 {
532         {"glClearColor", (void **) &qglClearColor},
533         {"glClear", (void **) &qglClear},
534         {"glBlendFunc", (void **) &qglBlendFunc},
535         {"glCullFace", (void **) &qglCullFace},
536         {"glDrawBuffer", (void **) &qglDrawBuffer},
537         {"glReadBuffer", (void **) &qglReadBuffer},
538         {"glEnable", (void **) &qglEnable},
539         {"glDisable", (void **) &qglDisable},
540         {"glIsEnabled", (void **) &qglIsEnabled},
541         {"glGetBooleanv", (void **) &qglGetBooleanv},
542         {"glGetDoublev", (void **) &qglGetDoublev},
543         {"glGetFloatv", (void **) &qglGetFloatv},
544         {"glGetIntegerv", (void **) &qglGetIntegerv},
545         {"glGetError", (void **) &qglGetError},
546         {"glGetString", (void **) &qglGetString},
547         {"glFinish", (void **) &qglFinish},
548         {"glFlush", (void **) &qglFlush},
549         {"glClearDepth", (void **) &qglClearDepth},
550         {"glDepthFunc", (void **) &qglDepthFunc},
551         {"glDepthMask", (void **) &qglDepthMask},
552         {"glDepthRange", (void **) &qglDepthRange},
553         {"glDrawElements", (void **) &qglDrawElements},
554         {"glDrawArrays", (void **) &qglDrawArrays},
555         {"glColorMask", (void **) &qglColorMask},
556         {"glPointSize", (void**) &qglPointSize},
557         {"glViewport", (void **) &qglViewport},
558         {"glReadPixels", (void **) &qglReadPixels},
559         {"glStencilFunc", (void **) &qglStencilFunc},
560         {"glStencilMask", (void **) &qglStencilMask},
561         {"glStencilOp", (void **) &qglStencilOp},
562         {"glClearStencil", (void **) &qglClearStencil},
563         {"glTexParameterf", (void **) &qglTexParameterf},
564         {"glTexParameterfv", (void **) &qglTexParameterfv},
565         {"glTexParameteri", (void **) &qglTexParameteri},
566         {"glGetTexImage", (void **) &qglGetTexImage},
567         {"glGetTexParameterfv", (void **) &qglGetTexParameterfv},
568         {"glGetTexParameteriv", (void **) &qglGetTexParameteriv},
569         {"glGetTexLevelParameterfv", (void **) &qglGetTexLevelParameterfv},
570         {"glGetTexLevelParameteriv", (void **) &qglGetTexLevelParameteriv},
571         {"glHint", (void **) &qglHint},
572         {"glPixelStorei", (void **) &qglPixelStorei},
573         {"glGenTextures", (void **) &qglGenTextures},
574         {"glDeleteTextures", (void **) &qglDeleteTextures},
575         {"glBindTexture", (void **) &qglBindTexture},
576         {"glTexImage2D", (void **) &qglTexImage2D},
577         {"glTexSubImage2D", (void **) &qglTexSubImage2D},
578         {"glCopyTexImage2D", (void **) &qglCopyTexImage2D},
579         {"glCopyTexSubImage2D", (void **) &qglCopyTexSubImage2D},
580         {"glScissor", (void **) &qglScissor},
581         {"glPolygonOffset", (void **) &qglPolygonOffset},
582         {"glPolygonMode", (void **) &qglPolygonMode},
583         {"glActiveTexture", (void **) &qglActiveTexture},
584         {"glTexImage3D", (void **) &qglTexImage3D},
585         {"glTexSubImage3D", (void **) &qglTexSubImage3D},
586         {"glCopyTexSubImage3D", (void **) &qglCopyTexSubImage3D},
587         {"glDeleteShader", (void **) &qglDeleteShader},
588         {"glDeleteProgram", (void **) &qglDeleteProgram},
589         {"glDetachShader", (void **) &qglDetachShader},
590         {"glCreateShader", (void **) &qglCreateShader},
591         {"glShaderSource", (void **) &qglShaderSource},
592         {"glCompileShader", (void **) &qglCompileShader},
593         {"glCreateProgram", (void **) &qglCreateProgram},
594         {"glAttachShader", (void **) &qglAttachShader},
595         {"glLinkProgram", (void **) &qglLinkProgram},
596         {"glUseProgram", (void **) &qglUseProgram},
597         {"glValidateProgram", (void **) &qglValidateProgram},
598         {"glUniform1f", (void **) &qglUniform1f},
599         {"glUniform2f", (void **) &qglUniform2f},
600         {"glUniform3f", (void **) &qglUniform3f},
601         {"glUniform4f", (void **) &qglUniform4f},
602         {"glUniform1i", (void **) &qglUniform1i},
603         {"glUniform2i", (void **) &qglUniform2i},
604         {"glUniform3i", (void **) &qglUniform3i},
605         {"glUniform4i", (void **) &qglUniform4i},
606         {"glUniform1fv", (void **) &qglUniform1fv},
607         {"glUniform2fv", (void **) &qglUniform2fv},
608         {"glUniform3fv", (void **) &qglUniform3fv},
609         {"glUniform4fv", (void **) &qglUniform4fv},
610         {"glUniform1iv", (void **) &qglUniform1iv},
611         {"glUniform2iv", (void **) &qglUniform2iv},
612         {"glUniform3iv", (void **) &qglUniform3iv},
613         {"glUniform4iv", (void **) &qglUniform4iv},
614         {"glUniformMatrix2fv", (void **) &qglUniformMatrix2fv},
615         {"glUniformMatrix3fv", (void **) &qglUniformMatrix3fv},
616         {"glUniformMatrix4fv", (void **) &qglUniformMatrix4fv},
617         {"glGetShaderiv", (void **) &qglGetShaderiv},
618         {"glGetProgramiv", (void **) &qglGetProgramiv},
619         {"glGetShaderInfoLog", (void **) &qglGetShaderInfoLog},
620         {"glGetProgramInfoLog", (void **) &qglGetProgramInfoLog},
621         {"glGetAttachedShaders", (void **) &qglGetAttachedShaders},
622         {"glGetUniformLocation", (void **) &qglGetUniformLocation},
623         {"glGetActiveUniform", (void **) &qglGetActiveUniform},
624         {"glGetUniformfv", (void **) &qglGetUniformfv},
625         {"glGetUniformiv", (void **) &qglGetUniformiv},
626         {"glGetShaderSource", (void **) &qglGetShaderSource},
627         {"glVertexAttrib1f", (void **) &qglVertexAttrib1f},
628         {"glVertexAttrib1s", (void **) &qglVertexAttrib1s},
629         {"glVertexAttrib1d", (void **) &qglVertexAttrib1d},
630         {"glVertexAttrib2f", (void **) &qglVertexAttrib2f},
631         {"glVertexAttrib2s", (void **) &qglVertexAttrib2s},
632         {"glVertexAttrib2d", (void **) &qglVertexAttrib2d},
633         {"glVertexAttrib3f", (void **) &qglVertexAttrib3f},
634         {"glVertexAttrib3s", (void **) &qglVertexAttrib3s},
635         {"glVertexAttrib3d", (void **) &qglVertexAttrib3d},
636         {"glVertexAttrib4f", (void **) &qglVertexAttrib4f},
637         {"glVertexAttrib4s", (void **) &qglVertexAttrib4s},
638         {"glVertexAttrib4d", (void **) &qglVertexAttrib4d},
639         {"glVertexAttrib4Nub", (void **) &qglVertexAttrib4Nub},
640         {"glVertexAttrib1fv", (void **) &qglVertexAttrib1fv},
641         {"glVertexAttrib1sv", (void **) &qglVertexAttrib1sv},
642         {"glVertexAttrib1dv", (void **) &qglVertexAttrib1dv},
643         {"glVertexAttrib2fv", (void **) &qglVertexAttrib1fv},
644         {"glVertexAttrib2sv", (void **) &qglVertexAttrib1sv},
645         {"glVertexAttrib2dv", (void **) &qglVertexAttrib1dv},
646         {"glVertexAttrib3fv", (void **) &qglVertexAttrib1fv},
647         {"glVertexAttrib3sv", (void **) &qglVertexAttrib1sv},
648         {"glVertexAttrib3dv", (void **) &qglVertexAttrib1dv},
649         {"glVertexAttrib4fv", (void **) &qglVertexAttrib1fv},
650         {"glVertexAttrib4sv", (void **) &qglVertexAttrib1sv},
651         {"glVertexAttrib4dv", (void **) &qglVertexAttrib1dv},
652 //      {"glVertexAttrib4iv", (void **) &qglVertexAttrib1iv},
653 //      {"glVertexAttrib4bv", (void **) &qglVertexAttrib1bv},
654 //      {"glVertexAttrib4ubv", (void **) &qglVertexAttrib1ubv},
655 //      {"glVertexAttrib4usv", (void **) &qglVertexAttrib1usv},
656 //      {"glVertexAttrib4uiv", (void **) &qglVertexAttrib1uiv},
657 //      {"glVertexAttrib4Nbv", (void **) &qglVertexAttrib1Nbv},
658 //      {"glVertexAttrib4Nsv", (void **) &qglVertexAttrib1Nsv},
659 //      {"glVertexAttrib4Niv", (void **) &qglVertexAttrib1Niv},
660 //      {"glVertexAttrib4Nubv", (void **) &qglVertexAttrib1Nubv},
661 //      {"glVertexAttrib4Nusv", (void **) &qglVertexAttrib1Nusv},
662 //      {"glVertexAttrib4Nuiv", (void **) &qglVertexAttrib1Nuiv},
663         {"glVertexAttribPointer", (void **) &qglVertexAttribPointer},
664         {"glEnableVertexAttribArray", (void **) &qglEnableVertexAttribArray},
665         {"glDisableVertexAttribArray", (void **) &qglDisableVertexAttribArray},
666         {"glBindAttribLocation", (void **) &qglBindAttribLocation},
667         {"glGetActiveAttrib", (void **) &qglGetActiveAttrib},
668         {"glGetAttribLocation", (void **) &qglGetAttribLocation},
669         {"glGetVertexAttribdv", (void **) &qglGetVertexAttribdv},
670         {"glGetVertexAttribfv", (void **) &qglGetVertexAttribfv},
671         {"glGetVertexAttribiv", (void **) &qglGetVertexAttribiv},
672         {"glGetVertexAttribPointerv", (void **) &qglGetVertexAttribPointerv},
673         {"glBindBuffer", (void **) &qglBindBuffer},
674         {"glDeleteBuffers", (void **) &qglDeleteBuffers},
675         {"glGenBuffers", (void **) &qglGenBuffers},
676         {"glIsBuffer", (void **) &qglIsBuffer},
677         {"glMapBuffer", (void **) &qglMapBuffer},
678         {"glUnmapBuffer", (void **) &qglUnmapBuffer},
679         {"glBufferData", (void **) &qglBufferData},
680         {"glBufferSubData", (void **) &qglBufferSubData},
681         {"glIsRenderbuffer", (void **) &qglIsRenderbuffer},
682         {"glBindRenderbuffer", (void **) &qglBindRenderbuffer},
683         {"glDeleteRenderbuffers", (void **) &qglDeleteRenderbuffers},
684         {"glGenRenderbuffers", (void **) &qglGenRenderbuffers},
685         {"glRenderbufferStorage", (void **) &qglRenderbufferStorage},
686         {"glRenderbufferStorageMultisample", (void **) &qglRenderbufferStorageMultisample},
687         {"glGetRenderbufferParameteriv", (void **) &qglGetRenderbufferParameteriv},
688         {"glIsFramebuffer", (void **) &qglIsFramebuffer},
689         {"glBindFramebuffer", (void **) &qglBindFramebuffer},
690         {"glDeleteFramebuffers", (void **) &qglDeleteFramebuffers},
691         {"glGenFramebuffers", (void **) &qglGenFramebuffers},
692         {"glCheckFramebufferStatus", (void **) &qglCheckFramebufferStatus},
693         {"glFramebufferTexture1D", (void **) &qglFramebufferTexture1D},
694         {"glFramebufferTexture2D", (void **) &qglFramebufferTexture2D},
695         {"glFramebufferTexture3D", (void **) &qglFramebufferTexture3D},
696         {"glFramebufferTextureLayer", (void **) &qglFramebufferTextureLayer},
697         {"glFramebufferRenderbuffer", (void **) &qglFramebufferRenderbuffer},
698         {"glGetFramebufferAttachmentParameteriv", (void **) &qglGetFramebufferAttachmentParameteriv},
699         {"glBlitFramebuffer", (void **) &qglBlitFramebuffer},
700         {"glGenerateMipmap", (void **) &qglGenerateMipmap},
701         {"glGetUniformIndices", (void **) &qglGetUniformIndices},
702         {"glGetActiveUniformsiv", (void **) &qglGetActiveUniformsiv},
703         {"glGetActiveUniformName", (void **) &qglGetActiveUniformName},
704         {"glGetUniformBlockIndex", (void **) &qglGetUniformBlockIndex},
705         {"glGetActiveUniformBlockiv", (void **) &qglGetActiveUniformBlockiv},
706         {"glGetActiveUniformBlockName", (void **) &qglGetActiveUniformBlockName},
707         {"glBindBufferRange", (void **) &qglBindBufferRange},
708         {"glBindBufferBase", (void **) &qglBindBufferBase},
709         {"glGetIntegeri_v", (void **) &qglGetIntegeri_v},
710         {"glUniformBlockBinding", (void **) &qglUniformBlockBinding},
711         {"glBlendEquation", (void **) &qglBlendEquation},
712         {"glCompressedTexImage3D", (void **) &qglCompressedTexImage3D},
713         {"glCompressedTexImage2D", (void **) &qglCompressedTexImage2D},
714         {"glCompressedTexSubImage3D", (void **) &qglCompressedTexSubImage3D},
715         {"glCompressedTexSubImage2D", (void **) &qglCompressedTexSubImage2D},
716         {"glGetCompressedTexImage", (void **) &qglGetCompressedTexImage},
717         {"glGenQueries", (void **) &qglGenQueries},
718         {"glDeleteQueries", (void **) &qglDeleteQueries},
719         {"glIsQuery", (void **) &qglIsQuery},
720         {"glBeginQuery", (void **) &qglBeginQuery},
721         {"glEndQuery", (void **) &qglEndQuery},
722         {"glGetQueryiv", (void **) &qglGetQueryiv},
723         {"glGetQueryObjectiv", (void **) &qglGetQueryObjectiv},
724         {"glGetQueryObjectuiv", (void **) &qglGetQueryObjectuiv},
725         {"glDrawBuffers", (void **) &qglDrawBuffers},
726         {"glSampleCoverage", (void **) &qglSampleCoverage},
727         {"glBlendFuncSeparate", (void **) &qglBlendFuncSeparate},
728         {"glBindFragDataLocation", (void **) &qglBindFragDataLocation},
729         {NULL, NULL}
730 };
731
732 static dllfunction_t debugoutputfuncs[] =
733 {
734         {"glDebugMessageControlARB", (void **)&qglDebugMessageControlARB},
735         {"glDebugMessageInsertARB", (void **)&qglDebugMessageInsertARB},
736         {"glDebugMessageCallbackARB", (void **)&qglDebugMessageCallbackARB},
737         {"glGetDebugMessageLogARB", (void **)&qglGetDebugMessageLogARB},
738         {"glGetPointerv", (void **)&qglGetPointerv},
739         {NULL, NULL}
740 };
741
742 #endif
743
744 void VID_ClearExtensions(void)
745 {
746         // VorteX: reset extensions info cvar, it got filled by GL_CheckExtension
747         Cvar_SetQuick(&gl_info_extensions, "");
748
749         // clear the extension flags
750         memset(&vid.support, 0, sizeof(vid.support));
751 }
752
753 #ifndef USE_GLES2
754 void VID_CheckExtensions(void)
755 {
756         char *s;
757         if (!GL_CheckExtension("glbase", openglfuncs, NULL, false))
758                 Sys_Error("OpenGL driver/hardware lacks required features");
759
760         CHECKGLERROR
761
762         Con_DPrint("Checking OpenGL extensions...\n");
763
764         // detect what GLSL version is available, to enable features like r_glsl_skeletal and higher quality reliefmapping
765         vid.support.glshaderversion = 100;
766         s = (char *) qglGetString(GL_SHADING_LANGUAGE_VERSION);
767         if (s)
768                 vid.support.glshaderversion = (int)(atof(s) * 100.0f + 0.5f);
769         if (vid.support.glshaderversion < 100)
770                 vid.support.glshaderversion = 100;
771         Con_DPrintf("Detected GLSL #version %i\n", vid.support.glshaderversion);
772         vid.support.gl20shaders130 = true;
773
774         // GL drivers generally prefer GL_BGRA
775         vid.forcetextype = GL_BGRA;
776
777         vid.support.amd_texture_texture4 = GL_CheckExtension("GL_AMD_texture_texture4", NULL, "-notexture4", false);
778         vid.support.arb_draw_buffers = true;
779         vid.support.arb_occlusion_query = true;
780         vid.support.arb_query_buffer_object = true;
781         vid.support.arb_texture_compression = true;
782         vid.support.arb_texture_gather = GL_CheckExtension("GL_ARB_texture_gather", NULL, "-notexturegather", false);
783         vid.support.ext_blend_minmax = true;
784         vid.support.ext_blend_subtract = true;
785         vid.support.ext_blend_func_separate = true;
786         vid.support.ext_packed_depth_stencil = true;
787         vid.support.ext_texture_compression_s3tc = GL_CheckExtension("GL_EXT_texture_compression_s3tc", NULL, "-nos3tc", false);
788         vid.support.ext_texture_filter_anisotropic = GL_CheckExtension("GL_EXT_texture_filter_anisotropic", NULL, "-noanisotropy", false);
789         vid.support.ext_texture_srgb = true;
790         vid.support.arb_texture_float = true;
791         vid.support.arb_half_float_pixel = true;
792         vid.support.arb_half_float_vertex = true;
793         vid.support.arb_multisample = true;
794         vid.support.arb_debug_output = GL_CheckExtension("GL_ARB_debug_output", debugoutputfuncs, "-nogldebugoutput", false);
795         vid.allowalphatocoverage = false;
796
797 // COMMANDLINEOPTION: GL: -noanisotropy disables GL_EXT_texture_filter_anisotropic (allows higher quality texturing)
798 // COMMANDLINEOPTION: GL: -noblendminmax disables GL_EXT_blend_minmax
799 // COMMANDLINEOPTION: GL: -noblendsubtract disables GL_EXT_blend_subtract
800 // COMMANDLINEOPTION: GL: -nocombine disables GL_ARB_texture_env_combine or GL_EXT_texture_env_combine (required for bumpmapping and faster map rendering)
801 // COMMANDLINEOPTION: GL: -nocubemap disables GL_ARB_texture_cube_map (required for bumpmapping)
802 // COMMANDLINEOPTION: GL: -nodepthtexture disables use of GL_ARB_depth_texture (required for shadowmapping)
803 // COMMANDLINEOPTION: GL: -nodrawbuffers disables use of GL_ARB_draw_buffers (required for r_shadow_deferredprepass)
804 // COMMANDLINEOPTION: GL: -noedgeclamp disables GL_EXT_texture_edge_clamp or GL_SGIS_texture_edge_clamp (recommended, some cards do not support the other texture clamp method)
805 // COMMANDLINEOPTION: GL: -nofbo disables GL_EXT_framebuffer_object (which accelerates rendering), only used if GL_ARB_fragment_shader is also available
806 // COMMANDLINEOPTION: GL: -nomtex disables GL_ARB_multitexture (required for faster map rendering)
807 // COMMANDLINEOPTION: GL: -noocclusionquery disables GL_ARB_occlusion_query (which allows coronas to fade according to visibility, and potentially used for rendering optimizations)
808 // COMMANDLINEOPTION: GL: -noquerybuffer disables GL_ARB_query_buffer_object (which allows corona fading without synchronous rendering)
809 // COMMANDLINEOPTION: GL: -nos3tc disables GL_EXT_texture_compression_s3tc (which allows use of .dds texture caching)
810 // COMMANDLINEOPTION: GL: -noshadow disables use of GL_ARB_shadow (required for hardware shadowmap filtering)
811 // COMMANDLINEOPTION: GL: -notexture3d disables GL_EXT_texture3D (required for spherical lights, otherwise they render as a column)
812 // COMMANDLINEOPTION: GL: -notexture4 disables GL_AMD_texture_texture4 (which provides fetch4 sampling)
813 // COMMANDLINEOPTION: GL: -notexturecompression disables GL_ARB_texture_compression (which saves video memory if it is supported, but can also degrade image quality, see gl_texturecompression cvar documentation for more information)
814 // COMMANDLINEOPTION: GL: -notexturegather disables GL_ARB_texture_gather (which provides fetch4 sampling)
815 // COMMANDLINEOPTION: GL: -notexturenonpoweroftwo disables GL_ARB_texture_non_power_of_two (which saves video memory if it is supported, but crashes on some buggy drivers)
816 // COMMANDLINEOPTION: GL: -novbo disables GL_ARB_vertex_buffer_object (which accelerates rendering)
817 // COMMANDLINEOPTION: GL: -nosrgb disables GL_EXT_texture_sRGB (which is used for higher quality non-linear texture gamma)
818 // COMMANDLINEOPTION: GL: -nomultisample disables GL_ARB_multisample
819
820         if (vid.support.arb_draw_buffers)
821                 qglGetIntegerv(GL_MAX_DRAW_BUFFERS, (GLint*)&vid.maxdrawbuffers);
822
823         qglGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint*)&vid.maxtexturesize_2d);
824         qglGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, (GLint*)&vid.max_anisotropy);
825         qglGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, (GLint*)&vid.maxtexturesize_cubemap);
826         qglGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, (GLint*)&vid.maxtexturesize_3d);
827
828         vid.renderpath = RENDERPATH_GL32;
829         vid.sRGBcapable2D = false;
830         vid.sRGBcapable3D = true;
831         vid.allowalphatocoverage = true; // but see below, it may get turned to false again if GL_SAMPLES is <= 1
832
833         // enable multisample antialiasing if possible
834         if(vid.support.arb_multisample)
835         {
836                 int samples = 0;
837                 qglGetIntegerv(GL_SAMPLES, &samples);
838                 vid.samples = samples;
839                 if (samples > 1)
840                         qglEnable(GL_MULTISAMPLE);
841                 else
842                         vid.allowalphatocoverage = false;
843         }
844         else
845         {
846                 vid.allowalphatocoverage = false;
847                 vid.samples = 1;
848         }
849
850         // VorteX: set other info (maybe place them in VID_InitMode?)
851         Cvar_SetQuick(&gl_info_vendor, gl_vendor);
852         Cvar_SetQuick(&gl_info_renderer, gl_renderer);
853         Cvar_SetQuick(&gl_info_version, gl_version);
854         Cvar_SetQuick(&gl_info_platform, gl_platform ? gl_platform : "");
855         Cvar_SetQuick(&gl_info_driver, gl_driver);
856 }
857 #endif
858
859 float VID_JoyState_GetAxis(const vid_joystate_t *joystate, int axis, float fsensitivity, float deadzone)
860 {
861         float value;
862         value = (axis >= 0 && axis < MAXJOYAXIS) ? joystate->axis[axis] : 0.0f;
863         value = value > deadzone ? (value - deadzone) : (value < -deadzone ? (value + deadzone) : 0.0f);
864         value *= deadzone > 0 ? (1.0f / (1.0f - deadzone)) : 1.0f;
865         value = bound(-1, value, 1);
866         return value * fsensitivity;
867 }
868
869 qboolean VID_JoyBlockEmulatedKeys(int keycode)
870 {
871         int j;
872         vid_joystate_t joystate;
873
874         if (!joy_axiskeyevents.integer)
875                 return false;
876         if (vid_joystate.is360)
877                 return false;
878         if (keycode != K_UPARROW && keycode != K_DOWNARROW && keycode != K_RIGHTARROW && keycode != K_LEFTARROW)
879                 return false;
880
881         // block system-generated key events for arrow keys if we're emulating the arrow keys ourselves
882         VID_BuildJoyState(&joystate);
883         for (j = 32;j < 36;j++)
884                 if (vid_joystate.button[j] || joystate.button[j])
885                         return true;
886
887         return false;
888 }
889
890 void VID_Shared_BuildJoyState_Begin(vid_joystate_t *joystate)
891 {
892 #ifdef WIN32
893         xinput_state_t xinputstate;
894 #endif
895         memset(joystate, 0, sizeof(*joystate));
896 #ifdef WIN32
897         if (vid_xinputindex >= 0 && qXInputGetState && qXInputGetState(vid_xinputindex, &xinputstate) == S_OK)
898         {
899                 joystate->is360 = true;
900                 joystate->button[ 0] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP) != 0;
901                 joystate->button[ 1] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN) != 0;
902                 joystate->button[ 2] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) != 0;
903                 joystate->button[ 3] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) != 0;
904                 joystate->button[ 4] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_START) != 0;
905                 joystate->button[ 5] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) != 0;
906                 joystate->button[ 6] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB) != 0;
907                 joystate->button[ 7] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) != 0;
908                 joystate->button[ 8] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) != 0;
909                 joystate->button[ 9] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) != 0;
910                 joystate->button[10] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_A) != 0;
911                 joystate->button[11] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_B) != 0;
912                 joystate->button[12] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_X) != 0;
913                 joystate->button[13] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_Y) != 0;
914                 joystate->button[14] = xinputstate.Gamepad.bLeftTrigger >= XINPUT_GAMEPAD_TRIGGER_THRESHOLD;
915                 joystate->button[15] = xinputstate.Gamepad.bRightTrigger >= XINPUT_GAMEPAD_TRIGGER_THRESHOLD;
916                 joystate->button[16] = xinputstate.Gamepad.sThumbLY < -16384;
917                 joystate->button[17] = xinputstate.Gamepad.sThumbLY >  16384;
918                 joystate->button[18] = xinputstate.Gamepad.sThumbLX < -16384;
919                 joystate->button[19] = xinputstate.Gamepad.sThumbLX >  16384;
920                 joystate->button[20] = xinputstate.Gamepad.sThumbRY < -16384;
921                 joystate->button[21] = xinputstate.Gamepad.sThumbRY >  16384;
922                 joystate->button[22] = xinputstate.Gamepad.sThumbRX < -16384;
923                 joystate->button[23] = xinputstate.Gamepad.sThumbRX >  16384;
924                 joystate->axis[ 4] = xinputstate.Gamepad.bLeftTrigger * (1.0f / 255.0f);
925                 joystate->axis[ 5] = xinputstate.Gamepad.bRightTrigger * (1.0f / 255.0f);
926                 joystate->axis[ 0] = xinputstate.Gamepad.sThumbLX * (1.0f / 32767.0f);
927                 joystate->axis[ 1] = xinputstate.Gamepad.sThumbLY * (1.0f / 32767.0f);
928                 joystate->axis[ 2] = xinputstate.Gamepad.sThumbRX * (1.0f / 32767.0f);
929                 joystate->axis[ 3] = xinputstate.Gamepad.sThumbRY * (1.0f / 32767.0f);
930         }
931 #endif
932 }
933
934 void VID_Shared_BuildJoyState_Finish(vid_joystate_t *joystate)
935 {
936         float f, r;
937         if (joystate->is360)
938                 return;
939         // emulate key events for thumbstick
940         f = VID_JoyState_GetAxis(joystate, joy_axisforward.integer, 1, joy_axiskeyevents_deadzone.value) * joy_sensitivityforward.value;
941         r = VID_JoyState_GetAxis(joystate, joy_axisside.integer   , 1, joy_axiskeyevents_deadzone.value) * joy_sensitivityside.value;
942 #if MAXJOYBUTTON != 36
943 #error this code must be updated if MAXJOYBUTTON changes!
944 #endif
945         joystate->button[32] = f > 0.0f;
946         joystate->button[33] = f < 0.0f;
947         joystate->button[34] = r > 0.0f;
948         joystate->button[35] = r < 0.0f;
949 }
950
951 static void VID_KeyEventForButton(qboolean oldbutton, qboolean newbutton, int key, double *timer)
952 {
953         if (oldbutton)
954         {
955                 if (newbutton)
956                 {
957                         if (realtime >= *timer)
958                         {
959                                 Key_Event(key, 0, true);
960                                 *timer = realtime + 0.1;
961                         }
962                 }
963                 else
964                 {
965                         Key_Event(key, 0, false);
966                         *timer = 0;
967                 }
968         }
969         else
970         {
971                 if (newbutton)
972                 {
973                         Key_Event(key, 0, true);
974                         *timer = realtime + 0.5;
975                 }
976         }
977 }
978
979 #if MAXJOYBUTTON != 36
980 #error this code must be updated if MAXJOYBUTTON changes!
981 #endif
982 static int joybuttonkey[MAXJOYBUTTON][2] =
983 {
984         {K_JOY1, K_ENTER}, {K_JOY2, K_ESCAPE}, {K_JOY3, 0}, {K_JOY4, 0}, {K_JOY5, 0}, {K_JOY6, 0}, {K_JOY7, 0}, {K_JOY8, 0}, {K_JOY9, 0}, {K_JOY10, 0}, {K_JOY11, 0}, {K_JOY12, 0}, {K_JOY13, 0}, {K_JOY14, 0}, {K_JOY15, 0}, {K_JOY16, 0},
985         {K_AUX1, 0}, {K_AUX2, 0}, {K_AUX3, 0}, {K_AUX4, 0}, {K_AUX5, 0}, {K_AUX6, 0}, {K_AUX7, 0}, {K_AUX8, 0}, {K_AUX9, 0}, {K_AUX10, 0}, {K_AUX11, 0}, {K_AUX12, 0}, {K_AUX13, 0}, {K_AUX14, 0}, {K_AUX15, 0}, {K_AUX16, 0},
986         {K_JOY_UP, K_UPARROW}, {K_JOY_DOWN, K_DOWNARROW}, {K_JOY_RIGHT, K_RIGHTARROW}, {K_JOY_LEFT, K_LEFTARROW},
987 };
988
989 static int joybuttonkey360[][2] =
990 {
991         {K_X360_DPAD_UP, K_UPARROW},
992         {K_X360_DPAD_DOWN, K_DOWNARROW},
993         {K_X360_DPAD_LEFT, K_LEFTARROW},
994         {K_X360_DPAD_RIGHT, K_RIGHTARROW},
995         {K_X360_START, K_ESCAPE},
996         {K_X360_BACK, K_ESCAPE},
997         {K_X360_LEFT_THUMB, 0},
998         {K_X360_RIGHT_THUMB, 0},
999         {K_X360_LEFT_SHOULDER, 0},
1000         {K_X360_RIGHT_SHOULDER, 0},
1001         {K_X360_A, K_ENTER},
1002         {K_X360_B, K_ESCAPE},
1003         {K_X360_X, 0},
1004         {K_X360_Y, 0},
1005         {K_X360_LEFT_TRIGGER, 0},
1006         {K_X360_RIGHT_TRIGGER, 0},
1007         {K_X360_LEFT_THUMB_DOWN, K_DOWNARROW},
1008         {K_X360_LEFT_THUMB_UP, K_UPARROW},
1009         {K_X360_LEFT_THUMB_LEFT, K_LEFTARROW},
1010         {K_X360_LEFT_THUMB_RIGHT, K_RIGHTARROW},
1011         {K_X360_RIGHT_THUMB_DOWN, 0},
1012         {K_X360_RIGHT_THUMB_UP, 0},
1013         {K_X360_RIGHT_THUMB_LEFT, 0},
1014         {K_X360_RIGHT_THUMB_RIGHT, 0},
1015 };
1016
1017 double vid_joybuttontimer[MAXJOYBUTTON];
1018 void VID_ApplyJoyState(vid_joystate_t *joystate)
1019 {
1020         int j;
1021         int c = joy_axiskeyevents.integer != 0;
1022         if (joystate->is360)
1023         {
1024 #if 0
1025                 // keystrokes (chatpad)
1026                 // DOES NOT WORK - no driver support in xinput1_3.dll :(
1027                 xinput_keystroke_t keystroke;
1028                 while (qXInputGetKeystroke && qXInputGetKeystroke(XUSER_INDEX_ANY, 0, &keystroke) == S_OK)
1029                         Con_Printf("XInput KeyStroke: VirtualKey %i, Unicode %i, Flags %x, UserIndex %i, HidCode %i\n", keystroke.VirtualKey, keystroke.Unicode, keystroke.Flags, keystroke.UserIndex, keystroke.HidCode);
1030 #endif
1031
1032                 // emit key events for buttons
1033                 for (j = 0;j < (int)(sizeof(joybuttonkey360)/sizeof(joybuttonkey360[0]));j++)
1034                         VID_KeyEventForButton(vid_joystate.button[j] != 0, joystate->button[j] != 0, joybuttonkey360[j][c], &vid_joybuttontimer[j]);
1035
1036                 // axes
1037                 cl.cmd.forwardmove += VID_JoyState_GetAxis(joystate, joy_x360_axisforward.integer, joy_x360_sensitivityforward.value, joy_x360_deadzoneforward.value) * cl_forwardspeed.value;
1038                 cl.cmd.sidemove    += VID_JoyState_GetAxis(joystate, joy_x360_axisside.integer, joy_x360_sensitivityside.value, joy_x360_deadzoneside.value) * cl_sidespeed.value;
1039                 cl.cmd.upmove      += VID_JoyState_GetAxis(joystate, joy_x360_axisup.integer, joy_x360_sensitivityup.value, joy_x360_deadzoneup.value) * cl_upspeed.value;
1040                 cl.viewangles[0]   += VID_JoyState_GetAxis(joystate, joy_x360_axispitch.integer, joy_x360_sensitivitypitch.value, joy_x360_deadzonepitch.value) * cl.realframetime * cl_pitchspeed.value;
1041                 cl.viewangles[1]   += VID_JoyState_GetAxis(joystate, joy_x360_axisyaw.integer, joy_x360_sensitivityyaw.value, joy_x360_deadzoneyaw.value) * cl.realframetime * cl_yawspeed.value;
1042                 //cl.viewangles[2]   += VID_JoyState_GetAxis(joystate, joy_x360_axisroll.integer, joy_x360_sensitivityroll.value, joy_x360_deadzoneroll.value) * cl.realframetime * cl_rollspeed.value;
1043         }
1044         else
1045         {
1046                 // emit key events for buttons
1047                 for (j = 0;j < MAXJOYBUTTON;j++)
1048                         VID_KeyEventForButton(vid_joystate.button[j] != 0, joystate->button[j] != 0, joybuttonkey[j][c], &vid_joybuttontimer[j]);
1049
1050                 // axes
1051                 cl.cmd.forwardmove += VID_JoyState_GetAxis(joystate, joy_axisforward.integer, joy_sensitivityforward.value, joy_deadzoneforward.value) * cl_forwardspeed.value;
1052                 cl.cmd.sidemove    += VID_JoyState_GetAxis(joystate, joy_axisside.integer, joy_sensitivityside.value, joy_deadzoneside.value) * cl_sidespeed.value;
1053                 cl.cmd.upmove      += VID_JoyState_GetAxis(joystate, joy_axisup.integer, joy_sensitivityup.value, joy_deadzoneup.value) * cl_upspeed.value;
1054                 cl.viewangles[0]   += VID_JoyState_GetAxis(joystate, joy_axispitch.integer, joy_sensitivitypitch.value, joy_deadzonepitch.value) * cl.realframetime * cl_pitchspeed.value;
1055                 cl.viewangles[1]   += VID_JoyState_GetAxis(joystate, joy_axisyaw.integer, joy_sensitivityyaw.value, joy_deadzoneyaw.value) * cl.realframetime * cl_yawspeed.value;
1056                 //cl.viewangles[2]   += VID_JoyState_GetAxis(joystate, joy_axisroll.integer, joy_sensitivityroll.value, joy_deadzoneroll.value) * cl.realframetime * cl_rollspeed.value;
1057         }
1058
1059         vid_joystate = *joystate;
1060 }
1061
1062 int VID_Shared_SetJoystick(int index)
1063 {
1064 #ifdef WIN32
1065         int i;
1066         int xinputcount = 0;
1067         int xinputindex = -1;
1068         int xinputavailable = 0;
1069         xinput_state_t state;
1070         // detect available XInput controllers
1071         for (i = 0;i < 4;i++)
1072         {
1073                 if (qXInputGetState && qXInputGetState(i, &state) == S_OK)
1074                 {
1075                         xinputavailable |= 1<<i;
1076                         if (index == xinputcount)
1077                                 xinputindex = i;
1078                         xinputcount++;
1079                 }
1080         }
1081         if (joy_xinputavailable.integer != xinputavailable)
1082                 Cvar_SetValueQuick(&joy_xinputavailable, xinputavailable);
1083         if (vid_xinputindex != xinputindex)
1084         {
1085                 vid_xinputindex = xinputindex;
1086                 if (xinputindex >= 0)
1087                         Con_Printf("Joystick %i opened (XInput Device %i)\n", index, xinputindex);
1088         }
1089         return xinputcount;
1090 #else
1091         return 0;
1092 #endif
1093 }
1094
1095
1096 static void Force_CenterView_f (void)
1097 {
1098         cl.viewangles[PITCH] = 0;
1099 }
1100
1101 static int gamma_forcenextframe = false;
1102 static float cachegamma, cachebrightness, cachecontrast, cacheblack[3], cachegrey[3], cachewhite[3], cachecontrastboost;
1103 static int cachecolorenable;
1104
1105 void VID_ApplyGammaToColor(const float *rgb, float *out)
1106 {
1107         int i;
1108         if (cachecolorenable)
1109         {
1110                 for (i = 0; i < 3; i++)
1111                         out[i] = pow(cachecontrastboost * rgb[i] / ((cachecontrastboost - 1) * rgb[i] + 1), 1.0 / invpow(0.5, 1 - cachegrey[i])) * cachewhite[i] + cacheblack[i];
1112         }
1113         else
1114         {
1115                 for (i = 0; i < 3; i++)
1116                         out[i] = pow(cachecontrastboost * rgb[i] / ((cachecontrastboost - 1) * rgb[i] + 1), 1.0 / cachegamma) * cachecontrast + cachebrightness;
1117         }
1118 }
1119
1120 unsigned int vid_gammatables_serial = 0; // so other subsystems can poll if gamma parameters have changed
1121 qboolean vid_gammatables_trivial = true;
1122 void VID_BuildGammaTables(unsigned short *ramps, int rampsize)
1123 {
1124         if (cachecolorenable)
1125         {
1126                 BuildGammaTable16(1.0f, invpow(0.5, 1 - cachegrey[0]), cachewhite[0], cacheblack[0], cachecontrastboost, ramps, rampsize);
1127                 BuildGammaTable16(1.0f, invpow(0.5, 1 - cachegrey[1]), cachewhite[1], cacheblack[1], cachecontrastboost, ramps + rampsize, rampsize);
1128                 BuildGammaTable16(1.0f, invpow(0.5, 1 - cachegrey[2]), cachewhite[2], cacheblack[2], cachecontrastboost, ramps + rampsize*2, rampsize);
1129         }
1130         else
1131         {
1132                 BuildGammaTable16(1.0f, cachegamma, cachecontrast, cachebrightness, cachecontrastboost, ramps, rampsize);
1133                 BuildGammaTable16(1.0f, cachegamma, cachecontrast, cachebrightness, cachecontrastboost, ramps + rampsize, rampsize);
1134                 BuildGammaTable16(1.0f, cachegamma, cachecontrast, cachebrightness, cachecontrastboost, ramps + rampsize*2, rampsize);
1135         }
1136
1137         if(vid.sRGB2D || vid.sRGB3D)
1138         {
1139                 int i;
1140                 for(i = 0; i < 3*rampsize; ++i)
1141                         ramps[i] = (int)floor(bound(0.0f, Image_sRGBFloatFromLinearFloat(ramps[i] / 65535.0f), 1.0f) * 65535.0f + 0.5f);
1142         }
1143
1144         // LordHavoc: this code came from Ben Winslow and Zinx Verituse, I have
1145         // immensely butchered it to work with variable framerates and fit in with
1146         // the rest of darkplaces.
1147         //
1148         // R.I.P. zinx http://obits.al.com/obituaries/birmingham/obituary.aspx?n=christopher-robert-lais&pid=186080667
1149         if (v_psycho.integer)
1150         {
1151                 int x, y;
1152                 float t;
1153                 static float n[3], nd[3], nt[3];
1154                 static int init = true;
1155                 unsigned short *ramp;
1156                 gamma_forcenextframe = true;
1157                 if (init)
1158                 {
1159                         init = false;
1160                         for (x = 0;x < 3;x++)
1161                         {
1162                                 n[x] = lhrandom(0, 1);
1163                                 nd[x] = (rand()&1)?-0.25:0.25;
1164                                 nt[x] = lhrandom(1, 8.2);
1165                         }
1166                 }
1167
1168                 for (x = 0;x < 3;x++)
1169                 {
1170                         nt[x] -= cl.realframetime;
1171                         if (nt[x] < 0)
1172                         {
1173                                 nd[x] = -nd[x];
1174                                 nt[x] += lhrandom(1, 8.2);
1175                         }
1176                         n[x] += nd[x] * cl.realframetime;
1177                         n[x] -= floor(n[x]);
1178                 }
1179
1180                 for (x = 0, ramp = ramps;x < 3;x++)
1181                         for (y = 0, t = n[x] - 0.75f;y < rampsize;y++, t += 0.75f * (2.0f / rampsize))
1182                                 *ramp++ = (unsigned short)(cos(t*(M_PI*2.0)) * 32767.0f + 32767.0f);
1183         }
1184 }
1185
1186 void VID_UpdateGamma(void)
1187 {
1188         cvar_t *c;
1189         float f;
1190         qboolean gamma_changed = false;
1191
1192 #define BOUNDCVAR(cvar, m1, m2) c = &(cvar);f = bound(m1, c->value, m2);if (c->value != f) Cvar_SetValueQuick(c, f);
1193         BOUNDCVAR(v_gamma, 0.1, 5);
1194         BOUNDCVAR(v_contrast, 0.2, 5);
1195         BOUNDCVAR(v_brightness, -v_contrast.value * 0.8, 0.8);
1196         //BOUNDCVAR(v_contrastboost, 0.0625, 16);
1197         BOUNDCVAR(v_color_black_r, 0, 0.8);
1198         BOUNDCVAR(v_color_black_g, 0, 0.8);
1199         BOUNDCVAR(v_color_black_b, 0, 0.8);
1200         BOUNDCVAR(v_color_grey_r, 0, 0.95);
1201         BOUNDCVAR(v_color_grey_g, 0, 0.95);
1202         BOUNDCVAR(v_color_grey_b, 0, 0.95);
1203         BOUNDCVAR(v_color_white_r, 1, 5);
1204         BOUNDCVAR(v_color_white_g, 1, 5);
1205         BOUNDCVAR(v_color_white_b, 1, 5);
1206 #undef BOUNDCVAR
1207
1208         // set vid_gammatables_trivial to true if the current settings would generate the identity gamma table
1209         vid_gammatables_trivial = false;
1210         if(v_psycho.integer == 0)
1211         if(v_contrastboost.value == 1)
1212         if(!vid.sRGB2D)
1213         if(!vid.sRGB3D)
1214         {
1215                 if(v_color_enable.integer)
1216                 {
1217                         if(v_color_black_r.value == 0)
1218                         if(v_color_black_g.value == 0)
1219                         if(v_color_black_b.value == 0)
1220                         if(fabs(v_color_grey_r.value - 0.5) < 1e-6)
1221                         if(fabs(v_color_grey_g.value - 0.5) < 1e-6)
1222                         if(fabs(v_color_grey_b.value - 0.5) < 1e-6)
1223                         if(v_color_white_r.value == 1)
1224                         if(v_color_white_g.value == 1)
1225                         if(v_color_white_b.value == 1)
1226                                 vid_gammatables_trivial = true;
1227                 }
1228                 else
1229                 {
1230                         if(v_gamma.value == 1)
1231                         if(v_contrast.value == 1)
1232                         if(v_brightness.value == 0)
1233                                 vid_gammatables_trivial = true;
1234                 }
1235         }
1236
1237         // if any gamma settings were changed, bump vid_gammatables_serial so we regenerate the gamma ramp texture
1238 #define GAMMACHECK(cache, value) if (cache != (value)) gamma_changed = true;cache = (value)
1239         if(v_psycho.integer)
1240                 gamma_changed = true;
1241         GAMMACHECK(cachegamma      , v_gamma.value);
1242         GAMMACHECK(cachecontrast   , v_contrast.value);
1243         GAMMACHECK(cachebrightness , v_brightness.value);
1244         GAMMACHECK(cachecontrastboost, v_contrastboost.value);
1245         GAMMACHECK(cachecolorenable, v_color_enable.integer);
1246         GAMMACHECK(cacheblack[0]   , v_color_black_r.value);
1247         GAMMACHECK(cacheblack[1]   , v_color_black_g.value);
1248         GAMMACHECK(cacheblack[2]   , v_color_black_b.value);
1249         GAMMACHECK(cachegrey[0]    , v_color_grey_r.value);
1250         GAMMACHECK(cachegrey[1]    , v_color_grey_g.value);
1251         GAMMACHECK(cachegrey[2]    , v_color_grey_b.value);
1252         GAMMACHECK(cachewhite[0]   , v_color_white_r.value);
1253         GAMMACHECK(cachewhite[1]   , v_color_white_g.value);
1254         GAMMACHECK(cachewhite[2]   , v_color_white_b.value);
1255
1256         if(gamma_changed)
1257                 ++vid_gammatables_serial;
1258 #undef GAMMACHECK
1259 }
1260
1261 #ifdef WIN32
1262 static dllfunction_t xinputdllfuncs[] =
1263 {
1264         {"XInputGetState", (void **) &qXInputGetState},
1265         {"XInputGetKeystroke", (void **) &qXInputGetKeystroke},
1266         {NULL, NULL}
1267 };
1268 static const char* xinputdllnames [] =
1269 {
1270         "xinput1_3.dll",
1271         "xinput1_2.dll",
1272         "xinput1_1.dll",
1273         NULL
1274 };
1275 static dllhandle_t xinputdll_dll = NULL;
1276 #endif
1277
1278 void VID_Shared_Init(void)
1279 {
1280         Cvar_RegisterVariable(&gl_info_vendor);
1281         Cvar_RegisterVariable(&gl_info_renderer);
1282         Cvar_RegisterVariable(&gl_info_version);
1283         Cvar_RegisterVariable(&gl_info_extensions);
1284         Cvar_RegisterVariable(&gl_info_platform);
1285         Cvar_RegisterVariable(&gl_info_driver);
1286         Cvar_RegisterVariable(&v_gamma);
1287         Cvar_RegisterVariable(&v_brightness);
1288         Cvar_RegisterVariable(&v_contrastboost);
1289         Cvar_RegisterVariable(&v_contrast);
1290
1291         Cvar_RegisterVariable(&v_color_enable);
1292         Cvar_RegisterVariable(&v_color_black_r);
1293         Cvar_RegisterVariable(&v_color_black_g);
1294         Cvar_RegisterVariable(&v_color_black_b);
1295         Cvar_RegisterVariable(&v_color_grey_r);
1296         Cvar_RegisterVariable(&v_color_grey_g);
1297         Cvar_RegisterVariable(&v_color_grey_b);
1298         Cvar_RegisterVariable(&v_color_white_r);
1299         Cvar_RegisterVariable(&v_color_white_g);
1300         Cvar_RegisterVariable(&v_color_white_b);
1301
1302         Cvar_RegisterVariable(&v_glslgamma_2d);
1303
1304         Cvar_RegisterVariable(&v_psycho);
1305
1306         Cvar_RegisterVariable(&vid_fullscreen);
1307         Cvar_RegisterVariable(&vid_width);
1308         Cvar_RegisterVariable(&vid_height);
1309         Cvar_RegisterVariable(&vid_bitsperpixel);
1310         Cvar_RegisterVariable(&vid_samples);
1311         Cvar_RegisterVariable(&vid_refreshrate);
1312         Cvar_RegisterVariable(&vid_userefreshrate);
1313         Cvar_RegisterVariable(&vid_stereobuffer);
1314         Cvar_RegisterVariable(&vid_touchscreen_density);
1315         Cvar_RegisterVariable(&vid_touchscreen_xdpi);
1316         Cvar_RegisterVariable(&vid_touchscreen_ydpi);
1317         Cvar_RegisterVariable(&vid_vsync);
1318         Cvar_RegisterVariable(&vid_mouse);
1319         Cvar_RegisterVariable(&vid_grabkeyboard);
1320         Cvar_RegisterVariable(&vid_touchscreen);
1321         Cvar_RegisterVariable(&vid_touchscreen_showkeyboard);
1322         Cvar_RegisterVariable(&vid_touchscreen_supportshowkeyboard);
1323         Cvar_RegisterVariable(&vid_stick_mouse);
1324         Cvar_RegisterVariable(&vid_resizable);
1325         Cvar_RegisterVariable(&vid_desktopfullscreen);
1326         Cvar_RegisterVariable(&vid_minwidth);
1327         Cvar_RegisterVariable(&vid_minheight);
1328         Cvar_RegisterVariable(&gl_finish);
1329         Cvar_RegisterVariable(&vid_sRGB);
1330         Cvar_RegisterVariable(&vid_sRGB_fallback);
1331
1332         Cvar_RegisterVariable(&joy_active);
1333 #ifdef WIN32
1334         Cvar_RegisterVariable(&joy_xinputavailable);
1335 #endif
1336         Cvar_RegisterVariable(&joy_detected);
1337         Cvar_RegisterVariable(&joy_enable);
1338         Cvar_RegisterVariable(&joy_index);
1339         Cvar_RegisterVariable(&joy_axisforward);
1340         Cvar_RegisterVariable(&joy_axisside);
1341         Cvar_RegisterVariable(&joy_axisup);
1342         Cvar_RegisterVariable(&joy_axispitch);
1343         Cvar_RegisterVariable(&joy_axisyaw);
1344         //Cvar_RegisterVariable(&joy_axisroll);
1345         Cvar_RegisterVariable(&joy_deadzoneforward);
1346         Cvar_RegisterVariable(&joy_deadzoneside);
1347         Cvar_RegisterVariable(&joy_deadzoneup);
1348         Cvar_RegisterVariable(&joy_deadzonepitch);
1349         Cvar_RegisterVariable(&joy_deadzoneyaw);
1350         //Cvar_RegisterVariable(&joy_deadzoneroll);
1351         Cvar_RegisterVariable(&joy_sensitivityforward);
1352         Cvar_RegisterVariable(&joy_sensitivityside);
1353         Cvar_RegisterVariable(&joy_sensitivityup);
1354         Cvar_RegisterVariable(&joy_sensitivitypitch);
1355         Cvar_RegisterVariable(&joy_sensitivityyaw);
1356         //Cvar_RegisterVariable(&joy_sensitivityroll);
1357         Cvar_RegisterVariable(&joy_axiskeyevents);
1358         Cvar_RegisterVariable(&joy_axiskeyevents_deadzone);
1359         Cvar_RegisterVariable(&joy_x360_axisforward);
1360         Cvar_RegisterVariable(&joy_x360_axisside);
1361         Cvar_RegisterVariable(&joy_x360_axisup);
1362         Cvar_RegisterVariable(&joy_x360_axispitch);
1363         Cvar_RegisterVariable(&joy_x360_axisyaw);
1364         //Cvar_RegisterVariable(&joy_x360_axisroll);
1365         Cvar_RegisterVariable(&joy_x360_deadzoneforward);
1366         Cvar_RegisterVariable(&joy_x360_deadzoneside);
1367         Cvar_RegisterVariable(&joy_x360_deadzoneup);
1368         Cvar_RegisterVariable(&joy_x360_deadzonepitch);
1369         Cvar_RegisterVariable(&joy_x360_deadzoneyaw);
1370         //Cvar_RegisterVariable(&joy_x360_deadzoneroll);
1371         Cvar_RegisterVariable(&joy_x360_sensitivityforward);
1372         Cvar_RegisterVariable(&joy_x360_sensitivityside);
1373         Cvar_RegisterVariable(&joy_x360_sensitivityup);
1374         Cvar_RegisterVariable(&joy_x360_sensitivitypitch);
1375         Cvar_RegisterVariable(&joy_x360_sensitivityyaw);
1376         //Cvar_RegisterVariable(&joy_x360_sensitivityroll);
1377
1378 #ifdef WIN32
1379         Sys_LoadLibrary(xinputdllnames, &xinputdll_dll, xinputdllfuncs);
1380 #endif
1381
1382         Cmd_AddCommand("force_centerview", Force_CenterView_f, "recenters view (stops looking up/down)");
1383         Cmd_AddCommand("vid_restart", VID_Restart_f, "restarts video system (closes and reopens the window, restarts renderer)");
1384 }
1385
1386 static int VID_Mode(int fullscreen, int width, int height, int bpp, float refreshrate, int stereobuffer, int samples)
1387 {
1388         viddef_mode_t mode;
1389         char vabuf[1024];
1390
1391         memset(&mode, 0, sizeof(mode));
1392         mode.fullscreen = fullscreen != 0;
1393         mode.width = width;
1394         mode.height = height;
1395         mode.bitsperpixel = bpp;
1396         mode.refreshrate = vid_userefreshrate.integer ? max(1, refreshrate) : 0;
1397         mode.userefreshrate = vid_userefreshrate.integer != 0;
1398         mode.stereobuffer = stereobuffer != 0;
1399         mode.samples = samples;
1400         cl_ignoremousemoves = 2;
1401         VID_ClearExtensions();
1402
1403         vid.samples = vid.mode.samples;
1404         if (VID_InitMode(&mode))
1405         {
1406                 // accept the (possibly modified) mode
1407                 vid.mode = mode;
1408                 vid.fullscreen     = vid.mode.fullscreen;
1409                 vid.width          = vid.mode.width;
1410                 vid.height         = vid.mode.height;
1411                 vid.bitsperpixel   = vid.mode.bitsperpixel;
1412                 vid.refreshrate    = vid.mode.refreshrate;
1413                 vid.userefreshrate = vid.mode.userefreshrate;
1414                 vid.stereobuffer   = vid.mode.stereobuffer;
1415                 vid.stencil        = vid.mode.bitsperpixel > 16;
1416                 vid.sRGB2D         = vid_sRGB.integer >= 1 && vid.sRGBcapable2D;
1417                 vid.sRGB3D         = vid_sRGB.integer >= 1 && vid.sRGBcapable3D;
1418
1419                 switch(vid.renderpath)
1420                 {
1421                 case RENDERPATH_GL32:
1422 #ifdef GL_STEREO
1423                         {
1424                                 GLboolean stereo;
1425                                 qglGetBooleanv(GL_STEREO, &stereo);
1426                                 vid.stereobuffer = stereo != 0;
1427                         }
1428 #endif
1429                         break;
1430                 case RENDERPATH_GLES2:
1431                 default:
1432                         vid.stereobuffer = false;
1433                         break;
1434                 }
1435
1436                 if(
1437                         (vid_sRGB_fallback.integer >= 3) // force fallback
1438                         ||
1439                         (vid_sRGB_fallback.integer >= 2 && // fallback if framebuffer is 8bit
1440                                 r_viewfbo.integer < 2)
1441                 )
1442                         vid.sRGB2D = vid.sRGB3D = false;
1443
1444                 if(vid.samples != vid.mode.samples)
1445                         Con_Printf("NOTE: requested %dx AA, got %dx AA\n", vid.mode.samples, vid.samples);
1446
1447                 Con_Printf("Video Mode: %s %dx%dx%dx%.2fhz%s%s\n", mode.fullscreen ? "fullscreen" : "window", mode.width, mode.height, mode.bitsperpixel, mode.refreshrate, mode.stereobuffer ? " stereo" : "", mode.samples > 1 ? va(vabuf, sizeof(vabuf), " (%ix AA)", mode.samples) : "");
1448
1449                 Cvar_SetValueQuick(&vid_fullscreen, vid.mode.fullscreen);
1450                 Cvar_SetValueQuick(&vid_width, vid.mode.width);
1451                 Cvar_SetValueQuick(&vid_height, vid.mode.height);
1452                 Cvar_SetValueQuick(&vid_bitsperpixel, vid.mode.bitsperpixel);
1453                 Cvar_SetValueQuick(&vid_samples, vid.mode.samples);
1454                 if(vid_userefreshrate.integer)
1455                         Cvar_SetValueQuick(&vid_refreshrate, vid.mode.refreshrate);
1456                 Cvar_SetValueQuick(&vid_stereobuffer, vid.stereobuffer ? 1 : 0);
1457
1458                 if (vid_touchscreen.integer)
1459                 {
1460                         in_windowmouse_x = vid_width.value / 2.f;
1461                         in_windowmouse_y = vid_height.value / 2.f;
1462                 }
1463
1464                 return true;
1465         }
1466         else
1467                 return false;
1468 }
1469
1470 static void VID_OpenSystems(void)
1471 {
1472         Key_ReleaseAll();
1473         R_Modules_Start();
1474         S_Startup();
1475 }
1476
1477 static void VID_CloseSystems(void)
1478 {
1479         S_Shutdown();
1480         R_Modules_Shutdown();
1481         Key_ReleaseAll();
1482 }
1483
1484 qboolean vid_commandlinecheck = true;
1485 extern qboolean vid_opened;
1486
1487 void VID_Restart_f(void)
1488 {
1489         char vabuf[1024];
1490         char vabuf2[1024];
1491         // don't crash if video hasn't started yet
1492         if (vid_commandlinecheck)
1493                 return;
1494
1495         if (!vid_opened)
1496         {
1497                 SCR_BeginLoadingPlaque(false);
1498                 return;
1499         }
1500
1501         Con_Printf("VID_Restart: changing from %s %dx%dx%dbpp%s%s, to %s %dx%dx%dbpp%s%s.\n",
1502                 vid.mode.fullscreen ? "fullscreen" : "window", vid.mode.width, vid.mode.height, vid.mode.bitsperpixel, vid.mode.fullscreen && vid.mode.userefreshrate ? va(vabuf, sizeof(vabuf), "x%.2fhz", vid.mode.refreshrate) : "", vid.mode.samples > 1 ? va(vabuf2, sizeof(vabuf2), " (%ix AA)", vid.mode.samples) : "",
1503                 vid_fullscreen.integer ? "fullscreen" : "window", vid_width.integer, vid_height.integer, vid_bitsperpixel.integer, vid_fullscreen.integer && vid_userefreshrate.integer ? va(vabuf, sizeof(vabuf), "x%.2fhz", vid_refreshrate.value) : "", vid_samples.integer > 1 ? va(vabuf2, sizeof(vabuf2), " (%ix AA)", vid_samples.integer) : "");
1504         VID_CloseSystems();
1505         VID_Shutdown();
1506         if (!VID_Mode(vid_fullscreen.integer, vid_width.integer, vid_height.integer, vid_bitsperpixel.integer, vid_refreshrate.value, vid_stereobuffer.integer, vid_samples.integer))
1507         {
1508                 Con_Print("Video mode change failed\n");
1509                 if (!VID_Mode(vid.mode.fullscreen, vid.mode.width, vid.mode.height, vid.mode.bitsperpixel, vid.mode.refreshrate, vid.mode.stereobuffer, vid.mode.samples))
1510                         Sys_Error("Unable to restore to last working video mode");
1511         }
1512         VID_OpenSystems();
1513 }
1514
1515 const char *vidfallbacks[][2] =
1516 {
1517         {"vid_stereobuffer", "0"},
1518         {"vid_samples", "1"},
1519         {"vid_userefreshrate", "0"},
1520         {"vid_width", "640"},
1521         {"vid_height", "480"},
1522         {"vid_bitsperpixel", "32"},
1523         {NULL, NULL}
1524 };
1525
1526 // this is only called once by Host_StartVideo and again on each FS_GameDir_f
1527 void VID_Start(void)
1528 {
1529         int i, width, height, success;
1530         if (vid_commandlinecheck)
1531         {
1532                 // interpret command-line parameters
1533                 vid_commandlinecheck = false;
1534 // COMMANDLINEOPTION: Video: -window performs +vid_fullscreen 0
1535                 if (COM_CheckParm("-window") || COM_CheckParm("-safe"))
1536                         Cvar_SetValueQuick(&vid_fullscreen, false);
1537 // COMMANDLINEOPTION: Video: -fullscreen performs +vid_fullscreen 1
1538                 if (COM_CheckParm("-fullscreen"))
1539                         Cvar_SetValueQuick(&vid_fullscreen, true);
1540                 width = 0;
1541                 height = 0;
1542 // COMMANDLINEOPTION: Video: -width <pixels> performs +vid_width <pixels> and also +vid_height <pixels*3/4> if only -width is specified (example: -width 1024 sets 1024x768 mode)
1543                 if ((i = COM_CheckParm("-width")) != 0)
1544                         width = atoi(com_argv[i+1]);
1545 // COMMANDLINEOPTION: Video: -height <pixels> performs +vid_height <pixels> and also +vid_width <pixels*4/3> if only -height is specified (example: -height 768 sets 1024x768 mode)
1546                 if ((i = COM_CheckParm("-height")) != 0)
1547                         height = atoi(com_argv[i+1]);
1548                 if (width == 0)
1549                         width = height * 4 / 3;
1550                 if (height == 0)
1551                         height = width * 3 / 4;
1552                 if (width)
1553                         Cvar_SetValueQuick(&vid_width, width);
1554                 if (height)
1555                         Cvar_SetValueQuick(&vid_height, height);
1556 // COMMANDLINEOPTION: Video: -density <multiplier> performs +vid_touchscreen_density <multiplier> (example -density 1 or -density 1.5)
1557                 if ((i = COM_CheckParm("-density")) != 0)
1558                         Cvar_SetQuick(&vid_touchscreen_density, com_argv[i+1]);
1559 // COMMANDLINEOPTION: Video: -xdpi <dpi> performs +vid_touchscreen_xdpi <dpi> (example -xdpi 160 or -xdpi 320)
1560                 if ((i = COM_CheckParm("-touchscreen_xdpi")) != 0)
1561                         Cvar_SetQuick(&vid_touchscreen_xdpi, com_argv[i+1]);
1562 // COMMANDLINEOPTION: Video: -ydpi <dpi> performs +vid_touchscreen_ydpi <dpi> (example -ydpi 160 or -ydpi 320)
1563                 if ((i = COM_CheckParm("-touchscreen_ydpi")) != 0)
1564                         Cvar_SetQuick(&vid_touchscreen_ydpi, com_argv[i+1]);
1565         }
1566
1567         success = VID_Mode(vid_fullscreen.integer, vid_width.integer, vid_height.integer, vid_bitsperpixel.integer, vid_refreshrate.value, vid_stereobuffer.integer, vid_samples.integer);
1568         if (!success)
1569         {
1570                 Con_Print("Desired video mode fail, trying fallbacks...\n");
1571                 for (i = 0;!success && vidfallbacks[i][0] != NULL;i++)
1572                 {
1573                         Cvar_Set(vidfallbacks[i][0], vidfallbacks[i][1]);
1574                         success = VID_Mode(vid_fullscreen.integer, vid_width.integer, vid_height.integer, vid_bitsperpixel.integer, vid_refreshrate.value, vid_stereobuffer.integer, vid_samples.integer);
1575                 }
1576                 if (!success)
1577                         Sys_Error("Video modes failed");
1578         }
1579         VID_OpenSystems();
1580 }
1581
1582 void VID_Stop(void)
1583 {
1584         VID_CloseSystems();
1585         VID_Shutdown();
1586 }
1587
1588 static int VID_SortModes_Compare(const void *a_, const void *b_)
1589 {
1590         vid_mode_t *a = (vid_mode_t *) a_;
1591         vid_mode_t *b = (vid_mode_t *) b_;
1592         if(a->width > b->width)
1593                 return +1;
1594         if(a->width < b->width)
1595                 return -1;
1596         if(a->height > b->height)
1597                 return +1;
1598         if(a->height < b->height)
1599                 return -1;
1600         if(a->refreshrate > b->refreshrate)
1601                 return +1;
1602         if(a->refreshrate < b->refreshrate)
1603                 return -1;
1604         if(a->bpp > b->bpp)
1605                 return +1;
1606         if(a->bpp < b->bpp)
1607                 return -1;
1608         if(a->pixelheight_num * b->pixelheight_denom > a->pixelheight_denom * b->pixelheight_num)
1609                 return +1;
1610         if(a->pixelheight_num * b->pixelheight_denom < a->pixelheight_denom * b->pixelheight_num)
1611                 return -1;
1612         return 0;
1613 }
1614 size_t VID_SortModes(vid_mode_t *modes, size_t count, qboolean usebpp, qboolean userefreshrate, qboolean useaspect)
1615 {
1616         size_t i;
1617         if(count == 0)
1618                 return 0;
1619         // 1. sort them
1620         qsort(modes, count, sizeof(*modes), VID_SortModes_Compare);
1621         // 2. remove duplicates
1622         for(i = 0; i < count; ++i)
1623         {
1624                 if(modes[i].width && modes[i].height)
1625                 {
1626                         if(i == 0)
1627                                 continue;
1628                         if(modes[i].width != modes[i-1].width)
1629                                 continue;
1630                         if(modes[i].height != modes[i-1].height)
1631                                 continue;
1632                         if(userefreshrate)
1633                                 if(modes[i].refreshrate != modes[i-1].refreshrate)
1634                                         continue;
1635                         if(usebpp)
1636                                 if(modes[i].bpp != modes[i-1].bpp)
1637                                         continue;
1638                         if(useaspect)
1639                                 if(modes[i].pixelheight_num * modes[i-1].pixelheight_denom != modes[i].pixelheight_denom * modes[i-1].pixelheight_num)
1640                                         continue;
1641                 }
1642                 // a dupe, or a bogus mode!
1643                 if(i < count-1)
1644                         memmove(&modes[i], &modes[i+1], sizeof(*modes) * (count-1 - i));
1645                 --i; // check this index again, as mode i+1 is now here
1646                 --count;
1647         }
1648         return count;
1649 }