601387c1625991613ae96b094b9453ed708afcbf
[xonotic/darkplaces.git] / vid_3dfxsvga.c
1 /*
2         vid_3dfxsvga.c
3
4         OpenGL device driver for 3Dfx chipsets running Linux
5
6         Copyright (C) 1996-1997  Id Software, Inc.
7         Copyright (C) 1999,2000  Nelson Rush.
8         Copyright (C) 1999,2000  contributors of the QuakeForge project
9         Please see the file "AUTHORS" for a list of contributors
10
11         This program is free software; you can redistribute it and/or
12         modify it under the terms of the GNU General Public License
13         as published by the Free Software Foundation; either version 2
14         of the License, or (at your option) any later version.
15
16         This program is distributed in the hope that it will be useful,
17         but WITHOUT ANY WARRANTY; without even the implied warranty of
18         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19
20         See the GNU General Public License for more details.
21
22         You should have received a copy of the GNU General Public License
23         along with this program; if not, write to:
24
25                 Free Software Foundation, Inc.
26                 59 Temple Place - Suite 330
27                 Boston, MA  02111-1307, USA
28
29         $Id$
30 */
31
32 #include "quakedef.h"
33 #include "sys.h"
34 #include "console.h"
35 #include "sbar.h"
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <signal.h>
40 #include <string.h>
41
42 #include <dlfcn.h>
43
44 #include <GL/gl.h>
45 #include <GL/fxmesa.h>
46 #include <glide/sst1vid.h>
47
48
49 cvar_t          vid_mode = {"vid_mode","0",false};
50
51 viddef_t        vid;    // global video state
52
53 static void     *dlhand = NULL;
54
55 static fxMesaContext fc = NULL;
56 static int      scr_width, scr_height;
57
58 int     VID_options_items = 0;
59
60 /*-----------------------------------------------------------------------*/
61
62 float           gldepthmin, gldepthmax;
63
64 const char *gl_vendor;
65 const char *gl_renderer;
66 const char *gl_version;
67 const char *gl_extensions;
68
69 /*-----------------------------------------------------------------------*/
70 void D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
71 {
72 }
73
74 void D_EndDirectRect (int x, int y, int width, int height)
75 {
76 }
77
78 void VID_Shutdown(void)
79 {
80         if (!fc)
81                 return;
82
83         fxMesaDestroyContext(fc);
84 }
85
86 void signal_handler(int sig)
87 {
88         printf("Received signal %d, exiting...\n", sig);
89         Host_Shutdown();
90         abort();
91         //Sys_Quit();
92         exit(0);
93 }
94
95 void InitSig(void)
96 {
97         signal(SIGHUP, signal_handler);
98         signal(SIGINT, signal_handler);
99         signal(SIGQUIT, signal_handler);
100         signal(SIGILL, signal_handler);
101         signal(SIGTRAP, signal_handler);
102 //      signal(SIGIOT, signal_handler);
103         signal(SIGBUS, signal_handler);
104 //      signal(SIGFPE, signal_handler);
105         signal(SIGSEGV, signal_handler);
106         signal(SIGTERM, signal_handler);
107 }
108
109 /*
110         VID_CheckMultitexture
111
112         Check for ARB, SGIS, or EXT multitexture support
113 */
114 void VID_CheckMultitexture()
115 {
116         Con_Printf ("Checking for multitexture... ");
117         if (COM_CheckParm ("-nomtex"))
118         {
119                 Con_Printf ("disabled\n");
120                 return;
121         }
122         dlhand = dlopen (NULL, RTLD_LAZY);
123         if (dlhand == NULL)
124         {
125                 Con_Printf ("unable to check\n");
126                 return;
127         }
128         if (strstr(gl_extensions, "GL_ARB_multitexture "))
129         {
130                 Con_Printf ("GL_ARB_multitexture\n");
131                 qglMTexCoord2f = (void *)dlsym(dlhand, "glMultiTexCoord2fARB");
132                 qglSelectTexture = (void *)dlsym(dlhand, "glActiveTextureARB");
133                 gl_mtex_enum = GL_TEXTURE0_ARB;
134                 gl_mtexable = true;
135         }
136         else if (strstr(gl_extensions, "GL_SGIS_multitexture "))
137         {
138                 Con_Printf ("GL_SGIS_multitexture\n");
139                 qglMTexCoord2f = (void *)dlsym(dlhand, "glMTexCoord2fSGIS");
140                 qglSelectTexture = (void *)dlsym(dlhand, "glSelectTextureSGIS");
141                 gl_mtex_enum = TEXTURE0_SGIS;
142                 gl_mtexable = true;
143         }
144         else
145                 Con_Printf ("none found\n");
146         dlclose(dlhand);
147         dlhand = NULL;
148 }
149
150 void VID_CheckCVA(void)
151 {
152         qglLockArraysEXT = NULL;
153         qglUnlockArraysEXT = NULL;
154         gl_supportslockarrays = false;
155         if (COM_CheckParm("-nocva"))
156         {
157                 Con_Printf("...compiled vertex arrays disabled\n");
158                 return;
159         }
160         dlhand = dlopen (NULL, RTLD_LAZY);
161         if (dlhand == NULL)
162         {
163                 Con_Printf("Unable to open symbol list for main program.\n");
164                 return;
165         }
166         if (strstr(gl_extensions, "GL_EXT_compiled_vertex_array"))
167         {
168                 Con_Printf("...using compiled vertex arrays\n");
169                 qglLockArraysEXT = (void *) dlsym(dlhand, "glLockArraysEXT");
170                 qglUnlockArraysEXT = (void *) dlsym(dlhand, "glUnlockArraysEXT");
171                 gl_supportslockarrays = true;
172         }
173         dlclose(dlhand);
174         dlhand = NULL;
175 }
176
177
178 typedef void (GLAPIENTRY *gl3DfxSetDitherModeEXT_FUNC) (GrDitherMode_t mode);
179
180 void VID_SetupDithering()
181 {
182         Con_Printf ("Dithering: ");
183
184         dlhand = dlopen (NULL, RTLD_LAZY);
185
186         if (dlhand == NULL) {
187                 Con_SafePrintf ("unable to set.\n");
188                 return;
189         }
190
191         if (strstr(gl_extensions, "3DFX_set_dither_mode")) {
192                 gl3DfxSetDitherModeEXT_FUNC dither_select = NULL;
193
194                 dither_select = (void *) dlsym(dlhand, "gl3DfxSetDitherModeEXT");
195
196                 if (COM_CheckParm ("-dither_2x2")) {
197                         dither_select(GR_DITHER_2x2);
198                         Con_Printf ("2x2.\n");
199                 } else if (COM_CheckParm ("-dither_4x4")) {
200                         dither_select(GR_DITHER_4x4);
201                         Con_Printf ("4x4.\n");
202                 } else {
203                         glDisable(GL_DITHER);
204                         Con_Printf ("disabled.\n");
205                 }
206         }
207         dlclose(dlhand);
208         dlhand = NULL;
209 }
210
211 /*
212 =================
213 GL_BeginRendering
214
215 =================
216 */
217 void GL_BeginRendering (int *x, int *y, int *width, int *height)
218 {
219         *x = *y = 0;
220         *width = scr_width;
221         *height = scr_height;
222
223 //    if (!wglMakeCurrent( maindc, baseRC ))
224 //              Sys_Error ("wglMakeCurrent failed");
225
226 //      glViewport (*x, *y, *width, *height);
227 }
228
229
230 void GL_EndRendering (void)
231 {
232         if (!r_render.value)
233                 return;
234         glFlush();
235         fxMesaSwapBuffers();
236 }
237
238 static int resolutions[][3]={
239         { 320,  200,    GR_RESOLUTION_320x200 },
240         { 320,  240,    GR_RESOLUTION_320x240 },
241         { 400,  256,    GR_RESOLUTION_400x256 },
242         { 400,  300,    GR_RESOLUTION_400x300 },
243         { 512,  256,    GR_RESOLUTION_512x256 },
244         { 512,  384,    GR_RESOLUTION_512x384 },
245         { 640,  200,    GR_RESOLUTION_640x200 },
246         { 640,  350,    GR_RESOLUTION_640x350 },
247         { 640,  400,    GR_RESOLUTION_640x400 },
248         { 640,  480,    GR_RESOLUTION_640x480 },
249         { 800,  600,    GR_RESOLUTION_800x600 },
250         { 856,  480,    GR_RESOLUTION_856x480 },
251         { 960,  720,    GR_RESOLUTION_960x720 },
252 #ifdef GR_RESOLUTION_1024x768
253         { 1024, 768,    GR_RESOLUTION_1024x768 },
254 #endif
255 #ifdef GR_RESOLUTION_1152x864
256         { 1152, 864,    GR_RESOLUTION_1152x864 },
257 #endif
258 #ifdef GR_RESOLUTION_1280x960
259         { 1280, 960,    GR_RESOLUTION_1280x960 },
260 #endif
261 #ifdef GR_RESOLUTION_1280x1024
262         { 1280, 1024,   GR_RESOLUTION_1280x1024 },
263 #endif
264 #ifdef GR_RESOLUTION_1600x1024
265         { 1600, 1024,   GR_RESOLUTION_1600x1024 },
266 #endif
267 #ifdef GR_RESOLUTION_1600x1200
268         { 1600, 1200,   GR_RESOLUTION_1600x1200 },
269 #endif
270 #ifdef GR_RESOLUTION_1792x1344
271         { 1792, 1344,   GR_RESOLUTION_1792x1344 },
272 #endif
273 #ifdef GR_RESOLUTION_1856x1392
274         { 1856, 1392,   GR_RESOLUTION_1856x1392 },
275 #endif
276 #ifdef GR_RESOLUTION_1920x1440
277         { 1920, 1440,   GR_RESOLUTION_1920x1440 },
278 #endif
279 #ifdef GR_RESOLUTION_2048x1536
280         { 2048, 1536,   GR_RESOLUTION_2048x1536 },
281 #endif
282 #ifdef GR_RESOLUTION_2048x2048
283         { 2048, 2048,   GR_RESOLUTION_2048x2048 }
284 #endif
285 };
286
287 #define NUM_RESOLUTIONS         (sizeof(resolutions)/(sizeof(int)*3))
288
289
290 static int
291 findres(int *width, int *height)
292 {
293         int i;
294
295         for(i=0; i < NUM_RESOLUTIONS; i++) {
296                 if((*width <= resolutions[i][0]) &&
297                    (*height <= resolutions[i][1])) {
298                         *width = resolutions[i][0];
299                         *height = resolutions[i][1];
300                         return resolutions[i][2];
301                 }
302         }
303
304         *width = 640;
305         *height = 480;
306         return GR_RESOLUTION_640x480;
307 }
308
309 void VID_Init()
310 {
311         int i;
312         GLint attribs[32];
313         int width = 640, height = 480;
314
315 // set vid parameters
316         attribs[0] = FXMESA_DOUBLEBUFFER;
317         attribs[1] = FXMESA_ALPHA_SIZE;
318         attribs[2] = 1;
319         attribs[3] = FXMESA_DEPTH_SIZE;
320         attribs[4] = 1;
321         attribs[5] = FXMESA_NONE;
322
323         if ((i = COM_CheckParm("-width")) != 0)
324                 width = atoi(com_argv[i+1]);
325         if ((i = COM_CheckParm("-height")) != 0)
326                 height = atoi(com_argv[i+1]);
327
328         if ((i = COM_CheckParm("-conwidth")) != 0)
329                 vid.conwidth = atoi(com_argv[i+1]);
330         else
331                 vid.conwidth = 640;
332
333         vid.conwidth &= 0xfff8; // make it a multiple of eight
334
335         if (vid.conwidth < 320)
336                 vid.conwidth = 320;
337
338         // pick a conheight that matches with correct aspect
339         vid.conheight = vid.conwidth*3 / 4;
340
341         if ((i = COM_CheckParm("-conheight")) != 0)
342                 vid.conheight = atoi(com_argv[i+1]);
343         if (vid.conheight < 200)
344                 vid.conheight = 200;
345
346         fc = fxMesaCreateContext(0, findres(&width, &height), GR_REFRESH_75Hz,
347                 attribs);
348         if (!fc)
349                 Sys_Error("Unable to create 3DFX context.\n");
350
351         scr_width = width;
352         scr_height = height;
353
354         fxMesaMakeCurrent(fc);
355
356         if (vid.conheight > height)
357                 vid.conheight = height;
358         if (vid.conwidth > width)
359                 vid.conwidth = width;
360         vid.width = vid.conwidth;
361         vid.height = vid.conheight;
362
363         vid.aspect = ((float)vid.height / (float)vid.width) * (320.0 / 240.0);
364
365         InitSig(); // trap evil signals
366
367         GL_Init();
368
369         VID_SetupDithering(); // 3DFX specific
370
371         Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
372
373         vid.recalc_refdef = 1;                          // force a surface cache flush
374 }
375
376 void VID_ExtraOptionDraw(unsigned int options_draw_cursor)
377 {
378 /* Port specific Options menu entrys */
379 }
380
381 void VID_ExtraOptionCmd(int option_cursor)
382 {
383 /*
384         switch(option_cursor)
385         {
386         case 12:  // Always start with 12
387         break;
388         }
389 */
390 }
391 void VID_InitCvars ()
392 {
393 }
394
395 void VID_SetCaption (char *text)
396 {
397 }
398
399 void VID_HandlePause (qboolean pause)
400 {
401 }