comparison src/video/wincommon/SDL_wingl.c @ 655:9c42ee1b7d77

Date: Thu, 24 Apr 2003 15:13:47 -0400 From: Shawn Kirst Subject: SDL-1.2.5 patch to add ARB_multisample support Attached is a patch I have written for SDL-1.2.5 that adds ARB_multisample support. I only have the X11 and Win32 video patched. The Win32 patch also adds support for WGL_ARB_pixel_format, as it was required for getting a multisample capable pixel format. No additional GL header files are required to compile on either platform (though you need an up-to-date glx.h for X11). Requesting a multisample pixel format is made possible using SDL_GL_SetAttribute with the two new SDL_GLattr's I've added (SDL_GL_SAMPLE_BUFFERS and SDL_GL_SAMPLES). I've been using SDL in my projects for quite a while now, so I am happy to contribute back to the project. Now you can have and control FSAA in your SDL/GL apps at the application level!
author Sam Lantinga <slouken@libsdl.org>
date Tue, 22 Jul 2003 15:10:06 +0000
parents a6fa62b1be09
children 864e2d2a9a55
comparison
equal deleted inserted replaced
654:e92bcf2573cb 655:9c42ee1b7d77
22 22
23 #ifdef SAVE_RCSID 23 #ifdef SAVE_RCSID
24 static char rcsid = 24 static char rcsid =
25 "@(#) $Id$"; 25 "@(#) $Id$";
26 #endif 26 #endif
27
28 #include <string.h>
27 29
28 /* WGL implementation of SDL OpenGL support */ 30 /* WGL implementation of SDL OpenGL support */
29 31
30 #ifdef HAVE_OPENGL 32 #ifdef HAVE_OPENGL
31 #include "SDL_opengl.h" 33 #include "SDL_opengl.h"
75 status = -1; 77 status = -1;
76 } 78 }
77 return(status); 79 return(status);
78 } 80 }
79 81
82 static int Init_WGL_ARB_extensions(_THIS)
83 {
84 HWND hwnd;
85 HDC hdc;
86 HGLRC hglrc;
87 int pformat;
88 const char * (WINAPI *wglGetExtensionsStringARB)(HDC) = 0;
89
90 hwnd = CreateWindow(NULL, "PFormat", WS_POPUP | WS_DISABLED,
91 0, 0, 10, 10,
92 NULL, NULL, SDL_Instance,NULL);
93 hdc = GetDC(hwnd);
94
95 pformat = ChoosePixelFormat(hdc, &GL_pfd);
96 SetPixelFormat(hdc, pformat, &GL_pfd);
97
98 hglrc = this->gl_data->wglCreateContext(hdc);
99 this->gl_data->wglMakeCurrent(hdc, hglrc);
100
101 wglGetExtensionsStringARB = (const char * (WINAPI *)(HDC))
102 this->gl_data->wglGetProcAddress("wglGetExtensionsStringARB");
103
104 if(wglGetExtensionsStringARB && strstr(wglGetExtensionsStringARB(hdc),"WGL_ARB_pixel_format")) {
105 this->gl_data->wglChoosePixelFormatARB =
106 (BOOL (WINAPI *)(HDC, const int *, const FLOAT *, UINT, int *, UINT *))
107 this->gl_data->wglGetProcAddress("wglChoosePixelFormatARB");
108 this->gl_data->wglGetPixelFormatAttribivARB =
109 (BOOL (WINAPI *)(HDC, int, int, UINT, const int *, int *))
110 this->gl_data->wglGetProcAddress("wglGetPixelFormatAttribivARB");
111
112 if( (this->gl_data->wglChoosePixelFormatARB != NULL) &&
113 (this->gl_data->wglGetPixelFormatAttribivARB != NULL) )
114 this->gl_data->wgl_arb_pixel_format = 1;
115 else
116 this->gl_data->wgl_arb_pixel_format = 0;
117 } else {
118 this->gl_data->wgl_arb_pixel_format = 0;
119 }
120
121 this->gl_data->wglMakeCurrent(NULL, NULL);
122 this->gl_data->wglDeleteContext(hglrc);
123 ReleaseDC(hwnd, hdc);
124 DestroyWindow(hwnd);
125 }
126
80 int WIN_GL_SetupWindow(_THIS) 127 int WIN_GL_SetupWindow(_THIS)
81 { 128 {
82 int retval; 129 int retval;
83 #ifdef HAVE_OPENGL 130 #ifdef HAVE_OPENGL
84 int i; 131 int i;
85 int pixel_format; 132 unsigned int matching;
133 int iAttribs[64];
134 int *iAttr;
135 float fAttribs[1] = { 0 };
86 136
87 /* load the gl driver from a default path */ 137 /* load the gl driver from a default path */
88 if ( ! this->gl_config.driver_loaded ) { 138 if ( ! this->gl_config.driver_loaded ) {
89 /* no driver has been loaded, use default (ourselves) */ 139 /* no driver has been loaded, use default (ourselves) */
90 if ( WIN_GL_LoadLibrary(this, NULL) < 0 ) { 140 if ( WIN_GL_LoadLibrary(this, NULL) < 0 ) {
125 (GL_pfd.cAccumRedBits + GL_pfd.cAccumGreenBits + 175 (GL_pfd.cAccumRedBits + GL_pfd.cAccumGreenBits +
126 GL_pfd.cAccumBlueBits + GL_pfd.cAccumAlphaBits); 176 GL_pfd.cAccumBlueBits + GL_pfd.cAccumAlphaBits);
127 GL_pfd.cDepthBits = this->gl_config.depth_size; 177 GL_pfd.cDepthBits = this->gl_config.depth_size;
128 GL_pfd.cStencilBits = this->gl_config.stencil_size; 178 GL_pfd.cStencilBits = this->gl_config.stencil_size;
129 179
180 /* initialize WGL_ARB_pixel_format */
181 Init_WGL_ARB_extensions(this);
182
183 /* setup WGL_ARB_pixel_format attribs */
184 iAttr = &iAttribs[0];
185
186 *iAttr++ = WGL_DRAW_TO_WINDOW_ARB;
187 *iAttr++ = GL_TRUE;
188 *iAttr++ = WGL_ACCELERATION_ARB;
189 *iAttr++ = WGL_FULL_ACCELERATION_ARB;
190 *iAttr++ = WGL_RED_BITS_ARB;
191 *iAttr++ = this->gl_config.red_size;
192 *iAttr++ = WGL_GREEN_BITS_ARB;
193 *iAttr++ = this->gl_config.green_size;
194 *iAttr++ = WGL_BLUE_BITS_ARB;
195 *iAttr++ = this->gl_config.blue_size;
196
197 if ( this->gl_config.alpha_size ) {
198 *iAttr++ = WGL_ALPHA_BITS_ARB;
199 *iAttr++ = this->gl_config.alpha_size;
200 }
201
202 if ( this->gl_config.double_buffer ) {
203 *iAttr ++ = WGL_DOUBLE_BUFFER_ARB;
204 *iAttr ++ = GL_TRUE;
205 }
206
207 *iAttr++ = WGL_DEPTH_BITS_ARB;
208 *iAttr++ = this->gl_config.depth_size;
209
210 if ( this->gl_config.stencil_size ) {
211 *iAttr++ = WGL_STENCIL_BITS_ARB;
212 *iAttr++ = this->gl_config.stencil_size;
213 }
214
215 if ( this->gl_config.accum_red_size ) {
216 *iAttr++ = WGL_ACCUM_RED_BITS_ARB;
217 *iAttr++ = this->gl_config.accum_red_size;
218 }
219
220 if ( this->gl_config.accum_green_size ) {
221 *iAttr++ = WGL_ACCUM_GREEN_BITS_ARB;
222 *iAttr++ = this->gl_config.accum_green_size;
223 }
224
225 if ( this->gl_config.accum_blue_size ) {
226 *iAttr++ = WGL_ACCUM_BLUE_BITS_ARB;
227 *iAttr++ = this->gl_config.accum_blue_size;
228 }
229
230 if ( this->gl_config.accum_alpha_size ) {
231 *iAttr++ = WGL_ACCUM_ALPHA_BITS_ARB;
232 *iAttr++ = this->gl_config.accum_alpha_size;
233 }
234
235 if ( this->gl_config.stereo ) {
236 *iAttr++ = WGL_STEREO_ARB;
237 *iAttr++ = this->gl_config.stereo;
238 }
239
240 if ( this->gl_config.sample_buffers ) {
241 *iAttr++ = WGL_SAMPLE_BUFFERS_ARB;
242 *iAttr++ = this->gl_config.sample_buffers;
243 }
244
245 if ( this->gl_config.samples ) {
246 *iAttr++ = WGL_SAMPLES_ARB;
247 *iAttr++ = this->gl_config.samples;
248 }
249
250 *iAttr = 0;
251
130 /* Choose and set the closest available pixel format */ 252 /* Choose and set the closest available pixel format */
131 pixel_format = ChoosePixelFormat(GL_hdc, &GL_pfd); 253 if ( !this->gl_data->wgl_arb_pixel_format ||
254 !this->gl_data->wglChoosePixelFormatARB(GL_hdc, iAttribs, fAttribs, 1, &pixel_format, &matching) ||
255 !matching ) {
256 pixel_format = ChoosePixelFormat(GL_hdc, &GL_pfd);
257 this->gl_data->wgl_arb_pixel_format = 0;
258 }
132 if ( !pixel_format ) { 259 if ( !pixel_format ) {
133 SDL_SetError("No matching GL pixel format available"); 260 SDL_SetError("No matching GL pixel format available");
134 return(-1); 261 return(-1);
135 } 262 }
136 if( !SetPixelFormat(GL_hdc, pixel_format, &GL_pfd) ) { 263 if ( !SetPixelFormat(GL_hdc, pixel_format, &GL_pfd) ) {
137 if ( i == 0 ) { 264 if ( i == 0 ) {
138 /* First time through, try resetting the window */ 265 /* First time through, try resetting the window */
139 if ( WIN_GL_ResetWindow(this) < 0 ) { 266 if ( WIN_GL_ResetWindow(this) < 0 ) {
140 return(-1); 267 return(-1);
141 } 268 }
148 break; 275 break;
149 } 276 }
150 DescribePixelFormat(GL_hdc, pixel_format, sizeof(GL_pfd), &GL_pfd); 277 DescribePixelFormat(GL_hdc, pixel_format, sizeof(GL_pfd), &GL_pfd);
151 278
152 GL_hrc = this->gl_data->wglCreateContext(GL_hdc); 279 GL_hrc = this->gl_data->wglCreateContext(GL_hdc);
153 if( GL_hrc == NULL ) { 280 if ( GL_hrc == NULL ) {
154 SDL_SetError("Unable to create GL context"); 281 SDL_SetError("Unable to create GL context");
155 return(-1); 282 return(-1);
156 } 283 }
157 gl_active = 1; 284 gl_active = 1;
158 #else 285 #else
202 329
203 /* Get attribute data from glX. */ 330 /* Get attribute data from glX. */
204 int WIN_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value) 331 int WIN_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
205 { 332 {
206 int retval; 333 int retval;
334
335 if ( this->gl_data->wgl_arb_pixel_format ) {
336 int wgl_attrib;
337
338 switch(attrib) {
339 case SDL_GL_RED_SIZE:
340 wgl_attrib = WGL_RED_BITS_ARB;
341 break;
342 case SDL_GL_GREEN_SIZE:
343 wgl_attrib = WGL_GREEN_BITS_ARB;
344 break;
345 case SDL_GL_BLUE_SIZE:
346 wgl_attrib = WGL_BLUE_BITS_ARB;
347 break;
348 case SDL_GL_ALPHA_SIZE:
349 wgl_attrib = WGL_ALPHA_BITS_ARB;
350 break;
351 case SDL_GL_DOUBLEBUFFER:
352 wgl_attrib = WGL_DOUBLE_BUFFER_ARB;
353 break;
354 case SDL_GL_BUFFER_SIZE:
355 wgl_attrib = WGL_COLOR_BITS_ARB;
356 break;
357 case SDL_GL_DEPTH_SIZE:
358 wgl_attrib = WGL_DEPTH_BITS_ARB;
359 break;
360 case SDL_GL_STENCIL_SIZE:
361 wgl_attrib = WGL_STENCIL_BITS_ARB;
362 break;
363 case SDL_GL_ACCUM_RED_SIZE:
364 wgl_attrib = WGL_ACCUM_RED_BITS_ARB;
365 break;
366 case SDL_GL_ACCUM_GREEN_SIZE:
367 wgl_attrib = WGL_ACCUM_GREEN_BITS_ARB;
368 break;
369 case SDL_GL_ACCUM_BLUE_SIZE:
370 wgl_attrib = WGL_ACCUM_BLUE_BITS_ARB;
371 break;
372 case SDL_GL_ACCUM_ALPHA_SIZE:
373 wgl_attrib = WGL_ACCUM_ALPHA_BITS_ARB;
374 break;
375 case SDL_GL_STEREO:
376 wgl_attrib = WGL_STEREO_ARB;
377 break;
378 case SDL_GL_SAMPLE_BUFFERS:
379 wgl_attrib = WGL_SAMPLE_BUFFERS_ARB;
380 break;
381 case SDL_GL_SAMPLES:
382 wgl_attrib = WGL_SAMPLES_ARB;
383 break;
384 default:
385 return(-1);
386 }
387 this->gl_data->wglGetPixelFormatAttribivARB(GL_hdc, pixel_format, 0, 1, &wgl_attrib, value);
388
389 return 0;
390 }
207 391
208 retval = 0; 392 retval = 0;
209 switch( attrib ) { 393 switch ( attrib ) {
210 case SDL_GL_RED_SIZE: 394 case SDL_GL_RED_SIZE:
211 *value = GL_pfd.cRedBits; 395 *value = GL_pfd.cRedBits;
212 break; 396 break;
213 case SDL_GL_GREEN_SIZE: 397 case SDL_GL_GREEN_SIZE:
214 *value = GL_pfd.cGreenBits; 398 *value = GL_pfd.cGreenBits;
273 457
274 this->gl_data->wglGetProcAddress = NULL; 458 this->gl_data->wglGetProcAddress = NULL;
275 this->gl_data->wglCreateContext = NULL; 459 this->gl_data->wglCreateContext = NULL;
276 this->gl_data->wglDeleteContext = NULL; 460 this->gl_data->wglDeleteContext = NULL;
277 this->gl_data->wglMakeCurrent = NULL; 461 this->gl_data->wglMakeCurrent = NULL;
462 this->gl_data->wglChoosePixelFormatARB = NULL;
463 this->gl_data->wglGetPixelFormatAttribivARB = NULL;
278 464
279 this->gl_config.dll_handle = NULL; 465 this->gl_config.dll_handle = NULL;
280 this->gl_config.driver_loaded = 0; 466 this->gl_config.driver_loaded = 0;
281 } 467 }
282 } 468 }
302 488
303 /* Unload the old driver and reset the pointers */ 489 /* Unload the old driver and reset the pointers */
304 WIN_GL_UnloadLibrary(this); 490 WIN_GL_UnloadLibrary(this);
305 491
306 /* Load new function pointers */ 492 /* Load new function pointers */
493 memset(this->gl_data, 0, sizeof(*this->gl_data));
307 this->gl_data->wglGetProcAddress = (void * (WINAPI *)(const char *)) 494 this->gl_data->wglGetProcAddress = (void * (WINAPI *)(const char *))
308 GetProcAddress(handle, "wglGetProcAddress"); 495 GetProcAddress(handle, "wglGetProcAddress");
309 this->gl_data->wglCreateContext = (HGLRC (WINAPI *)(HDC)) 496 this->gl_data->wglCreateContext = (HGLRC (WINAPI *)(HDC))
310 GetProcAddress(handle, "wglCreateContext"); 497 GetProcAddress(handle, "wglCreateContext");
311 this->gl_data->wglDeleteContext = (BOOL (WINAPI *)(HGLRC)) 498 this->gl_data->wglDeleteContext = (BOOL (WINAPI *)(HGLRC))