comparison src/video/x11/SDL_x11opengl.c @ 3100:7dc982143c06

Date: Sun, 22 Mar 2009 12:52:29 +0000 From: Luke Benstead Subject: OpenGL 3.0 Context Creation I've attached a patch which implements OpenGL 3.x context creation on the latest SVN. I've added two options to SDL_GL_SetAttribute, these are SDL_GL_CONTEXT_MAJOR_VERSION and SDL_GL_CONTEXT_MINOR_VERSION. These default to 2 and 1 respectively. If the major version is less than 3 then the current context creation method is used, otherwise the appropriate new context creation function is called (depending on the platform). Sample code: if (SDL_Init(SDL_INIT_VIDEO) != 0) { printf("Unable to initialize SDL: %s\n", SDL_GetError()); return 1; } SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); //Without these 2 lines, SDL will create a GL 2.x context SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_Surface* screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL | SDL_FULLSCREEN ); I've implemented context creation on both Win32 and X and run basic tests on both. This patch doesn't provide access to all the options allowed by the new context creation (e.g. shared contexts, forward compatible contexts) but they can be added pretty easily.
author Sam Lantinga <slouken@libsdl.org>
date Tue, 24 Mar 2009 10:43:53 +0000
parents 089a77aebb7d
children cdeee9f9b14b
comparison
equal deleted inserted replaced
3099:82e60908fab1 3100:7dc982143c06
49 #define GLX_EXT_visual_rating 49 #define GLX_EXT_visual_rating
50 #define GLX_VISUAL_CAVEAT_EXT 0x20 50 #define GLX_VISUAL_CAVEAT_EXT 0x20
51 #define GLX_NONE_EXT 0x8000 51 #define GLX_NONE_EXT 0x8000
52 #define GLX_SLOW_VISUAL_EXT 0x8001 52 #define GLX_SLOW_VISUAL_EXT 0x8001
53 #define GLX_NON_CONFORMANT_VISUAL_EXT 0x800D 53 #define GLX_NON_CONFORMANT_VISUAL_EXT 0x800D
54 #endif
55
56 #ifndef GLX_ARB_create_context
57 #define GLX_ARB_create_context
58 #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
59 #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
60 #define GLX_CONTEXT_FLAGS_ARB 0x2094
61 #define GLX_CONTEXT_DEBUG_BIT_ARB 0x0001
62 #define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
54 #endif 63 #endif
55 64
56 #define OPENGL_REQUIRS_DLOPEN 65 #define OPENGL_REQUIRS_DLOPEN
57 #if defined(OPENGL_REQUIRS_DLOPEN) && defined(SDL_LOADSO_DLOPEN) 66 #if defined(OPENGL_REQUIRS_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
58 #include <dlfcn.h> 67 #include <dlfcn.h>
64 #define GL_LoadFunction SDL_LoadFunction 73 #define GL_LoadFunction SDL_LoadFunction
65 #define GL_UnloadObject SDL_UnloadObject 74 #define GL_UnloadObject SDL_UnloadObject
66 #endif 75 #endif
67 76
68 static void X11_GL_InitExtensions(_THIS); 77 static void X11_GL_InitExtensions(_THIS);
78
79 /* Typedef for the GL 3.0 context creation function */
80 typedef GLXContext ( * PFNGLXCREATECONTEXTATTRIBSARBPROC) (Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
69 81
70 int 82 int
71 X11_GL_LoadLibrary(_THIS, const char *path) 83 X11_GL_LoadLibrary(_THIS, const char *path)
72 { 84 {
73 void *handle; 85 void *handle;
268 { 280 {
269 XVisualInfo *vinfo; 281 XVisualInfo *vinfo;
270 282
271 /* 64 seems nice. */ 283 /* 64 seems nice. */
272 int attribs[64]; 284 int attribs[64];
273 int i; 285 int i = 0;
274 286
275 /* Setup our GLX attributes according to the gl_config. */ 287 /* Setup our GLX attributes according to the gl_config. */
276 i = 0;
277 attribs[i++] = GLX_RGBA; 288 attribs[i++] = GLX_RGBA;
278 attribs[i++] = GLX_RED_SIZE; 289 attribs[i++] = GLX_RED_SIZE;
279 attribs[i++] = _this->gl_config.red_size; 290 attribs[i++] = _this->gl_config.red_size;
280 attribs[i++] = GLX_GREEN_SIZE; 291 attribs[i++] = GLX_GREEN_SIZE;
281 attribs[i++] = _this->gl_config.green_size; 292 attribs[i++] = _this->gl_config.green_size;
382 XGetWindowAttributes(display, data->window, &xattr); 393 XGetWindowAttributes(display, data->window, &xattr);
383 v.screen = screen; 394 v.screen = screen;
384 v.visualid = XVisualIDFromVisual(xattr.visual); 395 v.visualid = XVisualIDFromVisual(xattr.visual);
385 vinfo = XGetVisualInfo(display, VisualScreenMask | VisualIDMask, &v, &n); 396 vinfo = XGetVisualInfo(display, VisualScreenMask | VisualIDMask, &v, &n);
386 if (vinfo) { 397 if (vinfo) {
387 context = 398 if (_this->gl_config.major_version < 3) {
388 _this->gl_data->glXCreateContext(display, vinfo, NULL, True); 399 context =
400 _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
401 } else {
402 /* If we want a GL 3.0 context or later we need to get a temporary
403 context to grab the new context creation function */
404 GLXContext temp_context = _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
405 if (!temp_context) {
406 SDL_SetError("Could not create GL context");
407 return NULL;
408 } else {
409 int attribs[] = {
410 GLX_CONTEXT_MAJOR_VERSION_ARB, _this->gl_config.major_version,
411 GLX_CONTEXT_MINOR_VERSION_ARB, _this->gl_config.minor_version,
412 0
413 };
414
415 /* Get a pointer to the context creation function for GL 3.0 */
416 PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribs = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((GLubyte*)"glXCreateContextAttribsARB");
417 if (!glXCreateContextAttribs) {
418 SDL_SetError("GL 3.x is not supported");
419 context = temp_context;
420 } else {
421 /* Create a GL 3.0 context */
422 GLXFBConfig *framebuffer_config = NULL;
423 int fbcount = 0;
424 framebuffer_config = glXChooseFBConfig(display, DefaultScreen(display), NULL, &fbcount);
425 if (!framebuffer_config) {
426 SDL_SetError("No good framebuffers found. GL 3.0 disabled");
427 context = temp_context;
428 } else {
429 context = glXCreateContextAttribs(display, framebuffer_config[0], NULL, True, attribs);
430 glXDestroyContext(display, temp_context);
431 }
432 }
433 }
434 }
389 XFree(vinfo); 435 XFree(vinfo);
390 } 436 }
391 XSync(display, False); 437 XSync(display, False);
392 438
393 if (!context) { 439 if (!context) {