Date: Sun, 22 Mar 2009 12:52:29 +0000
authorSam Lantinga <slouken@libsdl.org>
Tue, 24 Mar 2009 10:43:53 +0000
changeset 31007dc982143c06
parent 3099 82e60908fab1
child 3101 7b1da3f37f97
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.
include/SDL_video.h
src/video/SDL_sysvideo.h
src/video/SDL_video.c
src/video/win32/SDL_win32opengl.c
src/video/x11/SDL_x11opengl.c
     1.1 --- a/include/SDL_video.h	Tue Mar 24 10:33:12 2009 +0000
     1.2 +++ b/include/SDL_video.h	Tue Mar 24 10:43:53 2009 +0000
     1.3 @@ -270,7 +270,9 @@
     1.4      SDL_GL_MULTISAMPLEBUFFERS,
     1.5      SDL_GL_MULTISAMPLESAMPLES,
     1.6      SDL_GL_ACCELERATED_VISUAL,
     1.7 -    SDL_GL_RETAINED_BACKING
     1.8 +    SDL_GL_RETAINED_BACKING,
     1.9 +	SDL_GL_CONTEXT_MAJOR_VERSION,
    1.10 +	SDL_GL_CONTEXT_MINOR_VERSION
    1.11  } SDL_GLattr;
    1.12  
    1.13  
     2.1 --- a/src/video/SDL_sysvideo.h	Tue Mar 24 10:33:12 2009 +0000
     2.2 +++ b/src/video/SDL_sysvideo.h	Tue Mar 24 10:43:53 2009 +0000
     2.3 @@ -305,6 +305,8 @@
     2.4          int multisamplebuffers;
     2.5          int multisamplesamples;
     2.6          int accelerated;
     2.7 +        int major_version;
     2.8 +        int minor_version;
     2.9          int retained_backing;
    2.10          int driver_loaded;
    2.11          char driver_path[256];
     3.1 --- a/src/video/SDL_video.c	Tue Mar 24 10:33:12 2009 +0000
     3.2 +++ b/src/video/SDL_video.c	Tue Mar 24 10:43:53 2009 +0000
     3.3 @@ -257,6 +257,8 @@
     3.4      _this->gl_config.multisamplesamples = 0;
     3.5      _this->gl_config.retained_backing = 1;
     3.6      _this->gl_config.accelerated = -1;  /* not known, don't set */
     3.7 +    _this->gl_config.major_version = 2;
     3.8 +    _this->gl_config.minor_version = 1;
     3.9  
    3.10      /* Initialize the video subsystem */
    3.11      if (_this->VideoInit(_this) < 0) {
    3.12 @@ -2694,6 +2696,12 @@
    3.13      case SDL_GL_RETAINED_BACKING:
    3.14          _this->gl_config.retained_backing = value;
    3.15          break;
    3.16 +    case SDL_GL_CONTEXT_MAJOR_VERSION:
    3.17 +        _this->gl_config.major_version = value;
    3.18 +        break;
    3.19 +    case SDL_GL_CONTEXT_MINOR_VERSION:
    3.20 +        _this->gl_config.minor_version = value;
    3.21 +        break;
    3.22      default:
    3.23          SDL_SetError("Unknown OpenGL attribute");
    3.24          retval = -1;
     4.1 --- a/src/video/win32/SDL_win32opengl.c	Tue Mar 24 10:33:12 2009 +0000
     4.2 +++ b/src/video/win32/SDL_win32opengl.c	Tue Mar 24 10:43:53 2009 +0000
     4.3 @@ -30,6 +30,17 @@
     4.4  
     4.5  #define DEFAULT_OPENGL "OPENGL32.DLL"
     4.6  
     4.7 +#ifndef WGL_ARB_create_context
     4.8 +#define WGL_ARB_create_context
     4.9 +#define WGL_CONTEXT_MAJOR_VERSION_ARB   0x2091
    4.10 +#define WGL_CONTEXT_MINOR_VERSION_ARB   0x2092
    4.11 +#define WGL_CONTEXT_LAYER_PLANE_ARB     0x2093
    4.12 +#define WGL_CONTEXT_FLAGS_ARB           0x2093
    4.13 +#define WGL_CONTEXT_DEBUG_BIT_ARB       0x0001
    4.14 +#define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB  0x0002
    4.15 +#endif
    4.16 +
    4.17 +typedef HGLRC (APIENTRYP PFNWGLCREATECONTEXTATTRIBSARBPROC) (HDC hDC, HGLRC hShareContext, const int * attribList);
    4.18  
    4.19  int
    4.20  WIN_GL_LoadLibrary(_THIS, const char *path)
    4.21 @@ -485,7 +496,39 @@
    4.22      HDC hdc = ((SDL_WindowData *) window->driverdata)->hdc;
    4.23      HGLRC context;
    4.24  
    4.25 -    context = _this->gl_data->wglCreateContext(hdc);
    4.26 +    if (_this->gl_config.major_version < 3) {
    4.27 +        context = _this->gl_data->wglCreateContext(hdc);
    4.28 +    } else {
    4.29 +        PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
    4.30 +        HGLRC temp_context = _this->gl_data->wglCreateContext(hdc);
    4.31 +        if (!temp_context) {
    4.32 +            SDL_SetError("Could not create GL context");        
    4.33 +            return NULL;
    4.34 +        }
    4.35 +        
    4.36 +        /* Make the context current */
    4.37 +        if (WIN_GL_MakeCurrent(_this, window, temp_context) < 0) {
    4.38 +            WIN_GL_DeleteContext(_this, temp_context);
    4.39 +            return NULL;
    4.40 +        }
    4.41 +                
    4.42 +        wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress("wglCreateContextAttribsARB");
    4.43 +        if (!wglCreateContextAttribsARB) {
    4.44 +            SDL_SetError("GL 3.x is not supported");
    4.45 +            context = temp_context;
    4.46 +        } else {
    4.47 +            int attribs[] = {
    4.48 +                WGL_CONTEXT_MAJOR_VERSION_ARB, _this->gl_config.major_version,
    4.49 +                WGL_CONTEXT_MINOR_VERSION_ARB, _this->gl_config.minor_version,
    4.50 +                0 
    4.51 +            };
    4.52 +            /* Create the GL 3.x context */
    4.53 +            context = wglCreateContextAttribsARB(hdc, 0, attribs);
    4.54 +            /* Delete the GL 2.x context */
    4.55 +            wglDeleteContext(temp_context);
    4.56 +        }
    4.57 +    }
    4.58 +    
    4.59      if (!context) {
    4.60          SDL_SetError("Could not create GL context");
    4.61          return NULL;
     5.1 --- a/src/video/x11/SDL_x11opengl.c	Tue Mar 24 10:33:12 2009 +0000
     5.2 +++ b/src/video/x11/SDL_x11opengl.c	Tue Mar 24 10:43:53 2009 +0000
     5.3 @@ -53,6 +53,15 @@
     5.4  #define GLX_NON_CONFORMANT_VISUAL_EXT      0x800D
     5.5  #endif
     5.6  
     5.7 +#ifndef GLX_ARB_create_context
     5.8 +#define GLX_ARB_create_context
     5.9 +#define GLX_CONTEXT_MAJOR_VERSION_ARB      0x2091
    5.10 +#define GLX_CONTEXT_MINOR_VERSION_ARB      0x2092
    5.11 +#define GLX_CONTEXT_FLAGS_ARB              0x2094
    5.12 +#define GLX_CONTEXT_DEBUG_BIT_ARB          0x0001
    5.13 +#define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
    5.14 +#endif
    5.15 +
    5.16  #define OPENGL_REQUIRS_DLOPEN
    5.17  #if defined(OPENGL_REQUIRS_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
    5.18  #include <dlfcn.h>
    5.19 @@ -67,6 +76,9 @@
    5.20  
    5.21  static void X11_GL_InitExtensions(_THIS);
    5.22  
    5.23 +/* Typedef for the GL 3.0 context creation function */
    5.24 +typedef GLXContext ( * PFNGLXCREATECONTEXTATTRIBSARBPROC) (Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
    5.25 +
    5.26  int
    5.27  X11_GL_LoadLibrary(_THIS, const char *path)
    5.28  {
    5.29 @@ -270,10 +282,9 @@
    5.30  
    5.31      /* 64 seems nice. */
    5.32      int attribs[64];
    5.33 -    int i;
    5.34 -
    5.35 +    int i = 0;
    5.36 +    
    5.37      /* Setup our GLX attributes according to the gl_config. */
    5.38 -    i = 0;
    5.39      attribs[i++] = GLX_RGBA;
    5.40      attribs[i++] = GLX_RED_SIZE;
    5.41      attribs[i++] = _this->gl_config.red_size;
    5.42 @@ -384,8 +395,43 @@
    5.43      v.visualid = XVisualIDFromVisual(xattr.visual);
    5.44      vinfo = XGetVisualInfo(display, VisualScreenMask | VisualIDMask, &v, &n);
    5.45      if (vinfo) {
    5.46 -        context =
    5.47 -            _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
    5.48 +        if (_this->gl_config.major_version < 3) {        
    5.49 +            context =
    5.50 +                _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
    5.51 +        } else {
    5.52 +            /* If we want a GL 3.0 context or later we need to get a temporary
    5.53 +               context to grab the new context creation function */
    5.54 +            GLXContext temp_context = _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
    5.55 +            if (!temp_context) {
    5.56 +                SDL_SetError("Could not create GL context");        
    5.57 +                return NULL;
    5.58 +            } else {
    5.59 +                int attribs[] = {
    5.60 +                    GLX_CONTEXT_MAJOR_VERSION_ARB, _this->gl_config.major_version,
    5.61 +                    GLX_CONTEXT_MINOR_VERSION_ARB, _this->gl_config.minor_version,
    5.62 +                    0 
    5.63 +                };
    5.64 +                                
    5.65 +                /* Get a pointer to the context creation function for GL 3.0 */
    5.66 +                PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribs = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((GLubyte*)"glXCreateContextAttribsARB");
    5.67 +                if (!glXCreateContextAttribs) {
    5.68 +                    SDL_SetError("GL 3.x is not supported");
    5.69 +                    context = temp_context;
    5.70 +                } else {
    5.71 +                    /* Create a GL 3.0 context */
    5.72 +                    GLXFBConfig *framebuffer_config = NULL;
    5.73 +                    int fbcount = 0;                
    5.74 +                    framebuffer_config = glXChooseFBConfig(display, DefaultScreen(display), NULL, &fbcount);
    5.75 +                    if (!framebuffer_config) {
    5.76 +                        SDL_SetError("No good framebuffers found. GL 3.0 disabled");
    5.77 +                        context = temp_context;
    5.78 +                    } else {                                    
    5.79 +                        context = glXCreateContextAttribs(display, framebuffer_config[0], NULL, True, attribs);
    5.80 +                        glXDestroyContext(display, temp_context);
    5.81 +                    }
    5.82 +                }
    5.83 +            }
    5.84 +        }
    5.85          XFree(vinfo);
    5.86      }
    5.87      XSync(display, False);