summaryrefslogtreecommitdiff
path: root/source/win_glimp.c
blob: 9486f562d7dd287ee7ae51877ed5a4117a3ed4d8 (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
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_allow_software;
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.
*/
static void GLimp_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 GLimp_VerifyDriver( void ) {
	char buffer[MAX_STRING_CHARS];

	Q_strncpyz( buffer, qglGetString( GL_RENDERER ), sizeof( buffer ) );
	Q_strlwr( buffer );
	if( strcmp( buffer, "gdi generic" ) == 0 )
		if( !( win.flags & QVF_ACCELERATED ) )
			return qfalse;
	return qtrue;
}

static qboolean GLimp_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;
	DWORD dwLastError = 0;

	// 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 ) ) {
		dwLastError = GetLastError();
		Com_DPrintf( "failed\n" );
		goto fail;
	}
	Com_DPrintf( "ok\n" );

	Com_DPrintf( "...setting pixel format: " );
	if( glw.minidriver ) {
		if ( !( pixelformat = qwglChoosePixelFormat( win.dc, &pfd ) ) ) {
			dwLastError = GetLastError();
			Com_DPrintf( "failed\n" );
			goto fail;
		}

		if( qwglSetPixelFormat( win.dc, pixelformat, &pfd ) == FALSE ) {
			dwLastError = GetLastError();
			Com_DPrintf( "failed\n" );
			goto fail;
		}

		qwglDescribePixelFormat( win.dc, pixelformat, sizeof( pfd ), &pfd );
	} else {
		if( ( pixelformat = ChoosePixelFormat( win.dc, &pfd ) ) == 0 ) {
			dwLastError = GetLastError();
			Com_DPrintf( "failed\n" );
			goto fail;
		}

		if( SetPixelFormat( win.dc, pixelformat, &pfd ) == FALSE ) {
			dwLastError = GetLastError();
			Com_DPrintf( "failed\n" );
			goto fail;
		}

		DescribePixelFormat( win.dc, pixelformat, sizeof( pfd ), &pfd );

		if( ( pfd.dwFlags & PFD_GENERIC_ACCELERATED ) ||
                gl_allow_software->integer )
        {
			win.flags |= QVF_ACCELERATED;
		}
	}
	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 ) {
		dwLastError = GetLastError();
		Com_DPrintf( "failed\n" );
		goto fail;
	}
	Com_DPrintf( "ok\n" );

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

	if( !GLimp_VerifyDriver() ) {
		Com_Printf( "No hardware acceleration detected\n" );
		goto fail;
	}

	// print out PFD specifics 
	Com_Printf( "GL_VENDOR: %s\n", qglGetString( GL_VENDOR ) );
	Com_Printf( "GL_RENDERER: %s\n", qglGetString( GL_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;

fail:
    if( dwLastError ) {
    	Com_DPrintf( "GetLastError() = 0x%lx\n", dwLastError );
    }

	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.
*/
static qboolean GLimp_Init( void ) {
    const char *extensions;

	Cvar_Subsystem( CVAR_SYSTEM_VIDEO );

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

	Cvar_Subsystem( CVAR_SYSTEM_GENERIC );

    // create the window
	Win_Init();

    // initialize OpenGL context
    if( !GLimp_InitGL() ) {
        if( !glw.minidriver ) {
            goto fail;
        }
        Com_Printf( "...attempting to load opengl32\n" );
        Cvar_Set( "gl_driver","opengl32" );
        if( !GLimp_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" );
    }

    Win_SetMode();
    Win_ModeChanged();

	return qtrue;

fail:
    Win_Shutdown();
    return qfalse;
}


static void GLimp_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.
*/
static void GLimp_EndFrame( void ) {
    if( !qwglSwapBuffers( win.dc ) )
        Com_Error( ERR_FATAL, "GLimp_EndFrame: wglSwapBuffers failed" );
}

/*
@@@@@@@@@@@@
GLimp_FillAPI
@@@@@@@@@@@@
*/
void Video_FillGLAPI( videoAPI_t *api ) {
	api->Init = GLimp_Init;
	api->Shutdown = GLimp_Shutdown;
	api->UpdateGamma = Win_UpdateGamma;
	api->GetProcAddr = WGL_GetProcAddress;
	api->BeginFrame = GLimp_BeginFrame;
	api->EndFrame = GLimp_EndFrame;
}