]> icculus.org git repositories - divverent/darkplaces.git/blob - vid_3dfxsvga.c
host.c, sys_linux.c: Clear up linefeeds in Sys_Printf() -- we don't need to
[divverent/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 #define WARP_WIDTH              320
49 #define WARP_HEIGHT             200
50
51
52 //unsigned short        d_8to16table[256];
53 unsigned                d_8to24table[256];
54 unsigned char   d_15to8table[65536];
55
56 cvar_t          vid_mode = {"vid_mode","0",false};
57
58 viddef_t        vid;    // global video state
59
60 static void     *dlhand = NULL;
61
62 static fxMesaContext fc = NULL;
63 static int      scr_width, scr_height;
64
65 int     VID_options_items = 0;
66
67 /*-----------------------------------------------------------------------*/
68
69 //int   texture_mode = GL_NEAREST;
70 //int   texture_mode = GL_NEAREST_MIPMAP_NEAREST;
71 //int   texture_mode = GL_NEAREST_MIPMAP_LINEAR;
72 int     texture_mode = GL_LINEAR;
73 //int   texture_mode = GL_LINEAR_MIPMAP_NEAREST;
74 //int   texture_mode = GL_LINEAR_MIPMAP_LINEAR;
75
76 int     texture_extension_number = 1;
77
78 float           gldepthmin, gldepthmax;
79
80 const char *gl_vendor;
81 const char *gl_renderer;
82 const char *gl_version;
83 const char *gl_extensions;
84
85 void (*qglColorTableEXT) (int, int, int, int, int, const void*);
86 void (*qgl3DfxSetPaletteEXT) (GLuint *);
87 void (*qglMTexCoord2f) (GLenum, GLfloat, GLfloat);
88 void (*qglSelectTexture) (GLenum);
89
90 int gl_mtex_enum = 0;
91
92 // LordHavoc: in GLX these are never set, simply provided to make the rest of the code work
93 qboolean is8bit = false;
94 qboolean isPermedia = false;
95 qboolean isATI = false;
96 qboolean isG200 = false;
97 qboolean isRagePro = false;
98 qboolean gl_mtexable = false;
99 qboolean gl_arrays = false;
100
101 /*-----------------------------------------------------------------------*/
102 void D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
103 {
104 }
105
106 void D_EndDirectRect (int x, int y, int width, int height)
107 {
108 }
109
110 void VID_Shutdown(void)
111 {
112         if (!fc)
113                 return;
114
115         fxMesaDestroyContext(fc);
116 }
117
118 void signal_handler(int sig)
119 {
120         printf("Received signal %d, exiting...\n", sig);
121         Host_Shutdown();
122         abort();
123         //Sys_Quit();
124         exit(0);
125 }
126
127 void InitSig(void)
128 {
129         signal(SIGHUP, signal_handler);
130         signal(SIGINT, signal_handler);
131         signal(SIGQUIT, signal_handler);
132         signal(SIGILL, signal_handler);
133         signal(SIGTRAP, signal_handler);
134 //      signal(SIGIOT, signal_handler);
135         signal(SIGBUS, signal_handler);
136 //      signal(SIGFPE, signal_handler);
137         signal(SIGSEGV, signal_handler);
138         signal(SIGTERM, signal_handler);
139 }
140
141 /*
142         CheckMultiTextureExtensions
143
144         Check for ARB, SGIS, or EXT multitexture support
145 */
146 void
147 CheckMultiTextureExtensions ( void )
148 {
149         Con_Printf ("Checking for multitexture... ");
150         if (COM_CheckParm ("-nomtex"))
151         {
152                 Con_Printf ("disabled\n");
153                 return;
154         }
155         dlhand = dlopen (NULL, RTLD_LAZY);
156         if (dlhand == NULL)
157         {
158                 Con_Printf ("unable to check\n");
159                 return;
160         }
161         if (strstr(gl_extensions, "GL_ARB_multitexture "))
162         {
163                 Con_Printf ("GL_ARB_multitexture\n");
164                 qglMTexCoord2f = (void *)dlsym(dlhand, "glMultiTexCoord2fARB");
165                 qglSelectTexture = (void *)dlsym(dlhand, "glActiveTextureARB");
166                 gl_mtex_enum = GL_TEXTURE0_ARB;
167                 gl_mtexable = true;
168         } else if (strstr(gl_extensions, "GL_SGIS_multitexture "))
169         {
170                 Con_Printf ("GL_SGIS_multitexture\n");
171                 qglMTexCoord2f = (void *)dlsym(dlhand, "glMTexCoord2fSGIS");
172                 qglSelectTexture = (void *)dlsym(dlhand, "glSelectTextureSGIS");
173                 gl_mtex_enum = TEXTURE0_SGIS;
174                 gl_mtexable = true;
175         } else {
176                 Con_Printf ("none found\n");
177         }
178         dlclose(dlhand);
179         dlhand = NULL;          
180 }
181
182
183 typedef void (GLAPIENTRY *gl3DfxSetDitherModeEXT_FUNC) (GrDitherMode_t mode);
184
185 /*
186 ===============
187 GL_Init
188 ===============
189 */
190 void GL_Init (void)
191 {
192         gl_vendor = glGetString (GL_VENDOR);
193         Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
194         gl_renderer = glGetString (GL_RENDERER);
195         Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
196
197         gl_version = glGetString (GL_VERSION);
198         Con_Printf ("GL_VERSION: %s\n", gl_version);
199         gl_extensions = glGetString (GL_EXTENSIONS);
200         Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
201
202         CheckMultiTextureExtensions ();
203
204         glClearColor (1,0,0,0);
205         glCullFace(GL_FRONT);
206         glEnable(GL_TEXTURE_2D);
207
208         glEnable(GL_ALPHA_TEST);
209         glAlphaFunc(GL_GREATER, 0.666);
210
211         glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
212         glShadeModel (GL_FLAT);
213
214         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
215         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
216         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
217         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
218
219         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
220
221         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
222
223         Con_Printf ("Dithering: ");
224
225         dlhand = dlopen (NULL, RTLD_LAZY);
226
227         if (dlhand == NULL) {
228                 Con_SafePrintf ("unable to set.\n");
229                 return;
230         }
231
232         if (strstr(gl_extensions, "3DFX_set_dither_mode")) {
233                 gl3DfxSetDitherModeEXT_FUNC dither_select = NULL;
234
235                 dither_select = (void *) dlsym(dlhand, "gl3DfxSetDitherModeEXT");
236
237                 if (COM_CheckParm ("-dither_2x2")) {
238                         dither_select(GR_DITHER_2x2);
239                         Con_Printf ("2x2.\n");
240                 } else if (COM_CheckParm ("-dither_4x4")) {
241                         dither_select(GR_DITHER_4x4);
242                         Con_Printf ("4x4.\n");
243                 } else {
244                         glDisable(GL_DITHER);
245                         Con_Printf ("disabled.\n");
246                 }
247         }
248         dlclose(dlhand);
249         dlhand = NULL;
250 }
251
252 /*
253 =================
254 GL_BeginRendering
255
256 =================
257 */
258 void GL_BeginRendering (int *x, int *y, int *width, int *height)
259 {
260         *x = *y = 0;
261         *width = scr_width;
262         *height = scr_height;
263
264 //    if (!wglMakeCurrent( maindc, baseRC ))
265 //              Sys_Error ("wglMakeCurrent failed");
266
267 //      glViewport (*x, *y, *width, *height);
268 }
269
270
271 void GL_EndRendering (void)
272 {
273         glFlush();
274         fxMesaSwapBuffers();
275 }
276
277 static int resolutions[][3]={
278         { 320,  200,    GR_RESOLUTION_320x200 },
279         { 320,  240,    GR_RESOLUTION_320x240 },
280         { 400,  256,    GR_RESOLUTION_400x256 },
281         { 400,  300,    GR_RESOLUTION_400x300 },
282         { 512,  256,    GR_RESOLUTION_512x256 },
283         { 512,  384,    GR_RESOLUTION_512x384 },
284         { 640,  200,    GR_RESOLUTION_640x200 },
285         { 640,  350,    GR_RESOLUTION_640x350 },
286         { 640,  400,    GR_RESOLUTION_640x400 },
287         { 640,  480,    GR_RESOLUTION_640x480 },
288         { 800,  600,    GR_RESOLUTION_800x600 },
289         { 856,  480,    GR_RESOLUTION_856x480 },
290         { 960,  720,    GR_RESOLUTION_960x720 },
291 #ifdef GR_RESOLUTION_1024x768
292         { 1024, 768,    GR_RESOLUTION_1024x768 },
293 #endif
294 #ifdef GR_RESOLUTION_1152x864
295         { 1152, 864,    GR_RESOLUTION_1152x864 },
296 #endif
297 #ifdef GR_RESOLUTION_1280x960
298         { 1280, 960,    GR_RESOLUTION_1280x960 },
299 #endif
300 #ifdef GR_RESOLUTION_1280x1024
301         { 1280, 1024,   GR_RESOLUTION_1280x1024 },
302 #endif
303 #ifdef GR_RESOLUTION_1600x1024
304         { 1600, 1024,   GR_RESOLUTION_1600x1024 },
305 #endif
306 #ifdef GR_RESOLUTION_1600x1200
307         { 1600, 1200,   GR_RESOLUTION_1600x1200 },
308 #endif
309 #ifdef GR_RESOLUTION_1792x1344
310         { 1792, 1344,   GR_RESOLUTION_1792x1344 },
311 #endif
312 #ifdef GR_RESOLUTION_1856x1392
313         { 1856, 1392,   GR_RESOLUTION_1856x1392 },
314 #endif
315 #ifdef GR_RESOLUTION_1920x1440
316         { 1920, 1440,   GR_RESOLUTION_1920x1440 },
317 #endif
318 #ifdef GR_RESOLUTION_2048x1536
319         { 2048, 1536,   GR_RESOLUTION_2048x1536 },
320 #endif
321 #ifdef GR_RESOLUTION_2048x2048
322         { 2048, 2048,   GR_RESOLUTION_2048x2048 }
323 #endif
324 };
325
326 #define NUM_RESOLUTIONS         (sizeof(resolutions)/(sizeof(int)*3))
327
328
329 static int
330 findres(int *width, int *height)
331 {
332         int i;
333
334         for(i=0; i < NUM_RESOLUTIONS; i++) {
335                 if((*width <= resolutions[i][0]) &&
336                    (*height <= resolutions[i][1])) {
337                         *width = resolutions[i][0];
338                         *height = resolutions[i][1];
339                         return resolutions[i][2];
340                 }
341         }
342
343         *width = 640;
344         *height = 480;
345         return GR_RESOLUTION_640x480;
346 }
347
348 qboolean VID_Is8bit(void)
349 {
350         return is8bit;
351 }
352
353 typedef void (GLAPIENTRY *glColorTableEXT_FUNC) (GLenum, GLenum, GLsizei, 
354                 GLenum, GLenum, const GLvoid *);
355 typedef void (GLAPIENTRY *gl3DfxSetPaletteEXT_FUNC) (GLuint *pal);
356
357 void VID_Init8bitPalette()
358 {
359         // Check for 8bit Extensions and initialize them.
360         int i;
361
362         dlhand = dlopen (NULL, RTLD_LAZY);
363
364         Con_SafePrintf ("8-bit GL extensions: ");
365
366         if (dlhand == NULL) {
367                 Con_SafePrintf ("unable to check.\n");
368                 return;
369         }
370
371         if (COM_CheckParm("-no8bit")) {
372                 Con_SafePrintf("disabled.\n");
373                 return;
374         }
375
376         if (strstr(gl_extensions, "3DFX_set_global_palette") && (qgl3DfxSetPaletteEXT = dlsym(dlhand, "gl3DfxSetPaletteEXT")) != NULL)
377         {
378                 GLubyte table[256][4];
379                 char *oldpal;
380
381                 Con_SafePrintf("3DFX_set_global_palette.\n");
382                 glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
383                 oldpal = (char *) d_8to24table; //d_8to24table3dfx;
384                 for (i=0;i<256;i++)
385                 {
386                         table[i][2] = *oldpal++;
387                         table[i][1] = *oldpal++;
388                         table[i][0] = *oldpal++;
389                         table[i][3] = 255;
390                         oldpal++;
391                 }
392                 qgl3DfxSetPaletteEXT((GLuint *)table);
393                 is8bit = true;
394         } else if (strstr(gl_extensions, "GL_EXT_shared_texture_palette")) {
395                 char thePalette[256*3];
396                 char *oldPalette, *newPalette;
397                 glColorTableEXT_FUNC load_texture = NULL;
398
399                 Con_SafePrintf("GL_EXT_shared.\n");
400                 load_texture = (void *) dlsym(dlhand, "glColorTableEXT");
401
402                 glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
403                 oldPalette = (char *) d_8to24table; //d_8to24table3dfx;
404                 newPalette = thePalette;
405                 for (i=0;i<256;i++) {
406                         *newPalette++ = *oldPalette++;
407                         *newPalette++ = *oldPalette++;
408                         *newPalette++ = *oldPalette++;
409                         oldPalette++;
410                 }
411                 load_texture(GL_SHARED_TEXTURE_PALETTE_EXT, GL_RGB, 256, GL_RGB, GL_UNSIGNED_BYTE, (void *) thePalette);
412                 is8bit = true;
413         } else {
414                 Con_SafePrintf ("not found.\n");
415         }
416
417         dlclose(dlhand);
418         dlhand = NULL;
419 }
420
421 extern void Check_Gamma (unsigned char *pal);
422 void VID_Setup15to8Palette ();
423
424 void VID_Init(unsigned char *palette)
425 {
426         int i;
427         GLint attribs[32];
428         char    gldir[MAX_OSPATH];
429         int width = 640, height = 480;
430
431 // set vid parameters
432         attribs[0] = FXMESA_DOUBLEBUFFER;
433         attribs[1] = FXMESA_ALPHA_SIZE;
434         attribs[2] = 1;
435         attribs[3] = FXMESA_DEPTH_SIZE;
436         attribs[4] = 1;
437         attribs[5] = FXMESA_NONE;
438
439         if ((i = COM_CheckParm("-width")) != 0)
440                 width = atoi(com_argv[i+1]);
441         if ((i = COM_CheckParm("-height")) != 0)
442                 height = atoi(com_argv[i+1]);
443
444         if ((i = COM_CheckParm("-conwidth")) != 0)
445                 vid.conwidth = atoi(com_argv[i+1]);
446         else
447                 vid.conwidth = 640;
448
449         vid.conwidth &= 0xfff8; // make it a multiple of eight
450
451         if (vid.conwidth < 320)
452                 vid.conwidth = 320;
453
454         // pick a conheight that matches with correct aspect
455         vid.conheight = vid.conwidth*3 / 4;
456
457         if ((i = COM_CheckParm("-conheight")) != 0)
458                 vid.conheight = atoi(com_argv[i+1]);
459         if (vid.conheight < 200)
460                 vid.conheight = 200;
461
462         fc = fxMesaCreateContext(0, findres(&width, &height), GR_REFRESH_75Hz,
463                 attribs);
464         if (!fc)
465                 Sys_Error("Unable to create 3DFX context.\n");
466
467         scr_width = width;
468         scr_height = height;
469
470         fxMesaMakeCurrent(fc);
471
472         if (vid.conheight > height)
473                 vid.conheight = height;
474         if (vid.conwidth > width)
475                 vid.conwidth = width;
476         vid.width = vid.conwidth;
477         vid.height = vid.conheight;
478
479         vid.aspect = ((float)vid.height / (float)vid.width) * (320.0 / 240.0);
480
481         InitSig(); // trap evil signals
482
483         GL_Init();
484
485         snprintf(gldir, sizeof(gldir), "%s/glquake", com_gamedir);
486         Sys_mkdir (gldir);
487
488         VID_SetPalette(palette);
489
490         Check_Gamma(palette);
491
492         // Check for 3DFX Extensions and initialize them.
493         VID_Init8bitPalette();
494
495         if (is8bit) // LordHavoc: avoid calculating 15to8 table if it won't be used
496                 VID_Setup15to8Palette ();
497
498         Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
499
500         vid.recalc_refdef = 1;                          // force a surface cache flush
501 }
502
503 void VID_ExtraOptionDraw(unsigned int options_draw_cursor)
504 {
505 /* Port specific Options menu entrys */
506 }
507
508 void VID_ExtraOptionCmd(int option_cursor)
509 {
510 /*
511         switch(option_cursor)
512         {
513         case 12:  // Always start with 12
514         break;
515         }
516 */
517 }
518 void VID_InitCvars ()
519 {
520 }
521
522 void VID_SetCaption (char *text)
523 {
524 }
525
526 void VID_HandlePause (qboolean pause)
527 {
528 }