summaryrefslogtreecommitdiff
path: root/source/win_glimp.c
blob: cb83876411bdfcbabd374c5d125836384fac152b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

/*
GLW_IMP.C

This file contains ALL Win32 specific stuff having to do with the
OpenGL refresh.  When a port is being made the following functions
must be implemented by the port:

GLimp_EndFrame
GLimp_Init
GLimp_Shutdown
GLimp_SwitchFullscreen
*/

#include "win_local.h"
#include "win_glimp.h"
#include "win_wgl.h"

glwstate_t glw;

static cvar_t	*gl_driver;
static cvar_t	*gl_drawbuffer;
static cvar_t   *gl_swapinterval;

/*
GLimp_Shutdown

This routine does all OS specific shutdown procedures for the OpenGL
subsystem.  Under OpenGL this means NULLing out the current DC and
HGLRC, deleting the rendering context, and releasing the DC acquired
for the window.  The state structure is also nulled out.
*/
void VID_Shutdown( void ) {
	if( qwglMakeCurrent ) {
		qwglMakeCurrent( NULL, NULL );
	}

	if( glw.hGLRC && qwglDeleteContext ) {
		qwglDeleteContext( glw.hGLRC );
		glw.hGLRC = NULL;
	}

	WGL_Shutdown();
    Win_Shutdown();

    if( gl_swapinterval ) {
        gl_swapinterval->changed = NULL;
    }

    memset( &glw, 0, sizeof( glw ) );
}

static qboolean InitGL( void ) {
    PIXELFORMATDESCRIPTOR pfd = {
		sizeof( PIXELFORMATDESCRIPTOR ),	// size of this pfd
		1,								// version number
		PFD_DRAW_TO_WINDOW |			// support window
		PFD_SUPPORT_OPENGL |			// support OpenGL
		PFD_DOUBLEBUFFER,				// double buffered
		PFD_TYPE_RGBA,					// RGBA type
		24,								// 24-bit color depth
		0, 0, 0, 0, 0, 0,				// color bits ignored
		0,								// no alpha buffer
		0,								// shift bit ignored
		0,								// no accumulation buffer
		0, 0, 0, 0, 					// accum bits ignored
		32,								// 32-bit z-buffer	
		0,								// no stencil buffer
		0,								// no auxiliary buffer
		PFD_MAIN_PLANE,					// main layer
		0,								// reserved
		0, 0, 0							// layer masks ignored
    };
    int pixelformat;
    const char *renderer;

	// figure out if we're running on a minidriver or not
	if( !strstr( gl_driver->string, "opengl32" ) ) {
		Com_Printf( "...running a minidriver: %s\n", gl_driver->string );
		glw.minidriver = qtrue;
	}

	// load OpenGL library
	Com_DPrintf( "...initializing WGL: " );
	if( !WGL_Init( gl_driver->string ) ) {
		goto fail1;
	}
	Com_DPrintf( "ok\n" );

	Com_DPrintf( "...setting pixel format: " );
	if( glw.minidriver ) {
		if ( ( pixelformat = qwglChoosePixelFormat( win.dc, &pfd ) ) == 0 ) {
		    goto fail1;
		}
		if( qwglSetPixelFormat( win.dc, pixelformat, &pfd ) == FALSE ) {
		    goto fail1;
		}
		qwglDescribePixelFormat( win.dc, pixelformat, sizeof( pfd ), &pfd );
	} else {
		if( ( pixelformat = ChoosePixelFormat( win.dc, &pfd ) ) == 0 ) {
		    goto fail1;
		}
		if( SetPixelFormat( win.dc, pixelformat, &pfd ) == FALSE ) {
		    goto fail1;
		}
		DescribePixelFormat( win.dc, pixelformat, sizeof( pfd ), &pfd );
	}
	Com_DPrintf( "ok\n" );

	// startup the OpenGL subsystem by creating a context and making it current
	Com_DPrintf( "...creating OpenGL context: " );
	if( ( glw.hGLRC = qwglCreateContext( win.dc ) ) == NULL ) {
		goto fail1;
	}
	Com_DPrintf( "ok\n" );

	Com_DPrintf( "...making context current: " );
    if( !qwglMakeCurrent( win.dc, glw.hGLRC ) ) {
		goto fail1;
	}
	Com_DPrintf( "ok\n" );

    renderer = qglGetString( GL_RENDERER );

    if( pfd.dwFlags & PFD_GENERIC_ACCELERATED ) {
        win.flags |= QVF_ACCELERATED;
    } else if( !renderer || !renderer[0] || !Q_stricmp( renderer, "gdi generic" ) ) {
		Com_Printf( "No hardware OpenGL acceleration detected.\n" );
		goto fail2;
    }

	// print out PFD specifics
	Com_Printf( "GL_VENDOR: %s\n", qglGetString( GL_VENDOR ) );
	Com_Printf( "GL_RENDERER: %s\n", renderer );
	Com_Printf( "GL_PFD: color(%d-bits: %d,%d,%d,%d) Z(%d-bit) stencil(%d-bit)\n",
		pfd.cColorBits, pfd.cRedBits, pfd.cGreenBits, pfd.cBlueBits,
		pfd.cAlphaBits, pfd.cDepthBits, pfd.cStencilBits );

	return qtrue;

fail1:
	Com_DPrintf( "failed with error %#lx\n", GetLastError() );
fail2:
	if( glw.hGLRC && qwglDeleteContext ) {
		qwglDeleteContext( glw.hGLRC );
		glw.hGLRC = NULL;
	}

	WGL_Shutdown();

	return qfalse;
}

static void gl_swapinterval_changed( cvar_t *self ) {
	if( qwglSwapIntervalEXT ) {
		qwglSwapIntervalEXT( self->integer );
	}
}

/*
GLimp_Init

This routine is responsible for initializing the OS specific portions
of OpenGL.  Under Win32 this means dealing with the pixelformats and
doing the wgl interface stuff.
*/
qboolean VID_Init( void ) {
    const char *extensions;

	gl_driver = Cvar_Get( "gl_driver", "opengl32", CVAR_ARCHIVE|CVAR_REFRESH );
	gl_drawbuffer = Cvar_Get( "gl_drawbuffer", "GL_BACK", 0 );
	gl_swapinterval = Cvar_Get( "gl_swapinterval", "1", CVAR_ARCHIVE );

    // create the window
	Win_Init();

    // initialize OpenGL context
    if( !InitGL() ) {
        if( !glw.minidriver ) {
            goto fail;
        }
        Com_Printf( "...attempting to load opengl32\n" );
        Cvar_Set( "gl_driver","opengl32" );
        if( !InitGL() ) {
            goto fail;
        }
    }

    // initialize WGL extensions
	extensions = qglGetString( GL_EXTENSIONS );
	if( extensions && strstr( extensions, "WGL_EXT_swap_control" ) ) {
		Com_Printf( "...enabling WGL_EXT_swap_control\n" );
		qwglSwapIntervalEXT = ( PFNWGLSWAPINTERWALEXTPROC )qwglGetProcAddress( "wglSwapIntervalEXT" );        
        gl_swapinterval->changed = gl_swapinterval_changed;
        gl_swapinterval_changed( gl_swapinterval );
	} else {
		Com_Printf( "WGL_EXT_swap_control not found\n" );
    }

    VID_SetMode();

	return qtrue;

fail:
    Win_Shutdown();
    return qfalse;
}


void VID_BeginFrame( void ) {
}

/*
GLimp_EndFrame

Responsible for doing a swapbuffers and possibly for other stuff
as yet to be determined.  Probably better not to make this a GLimp
function and instead do a call to GLimp_SwapBuffers.
*/
void VID_EndFrame( void ) {
    if( !qwglSwapBuffers( win.dc ) ) {
        int error = GetLastError();

        if( !IsIconic( win.wnd ) ) {
            Com_Error( ERR_FATAL, "wglSwapBuffers failed with error %#x", error );
        }
    }
}

void *VID_GetProcAddr( const char *symbol ) {
    return ( void * )GetProcAddress( glw.hinstOpenGL, symbol );
}