Prefer the OpenGL ES 2.0 context when it's available, make it possible to create an OpenGL 2.0 context on iPhoneOS
authorSam Lantinga <slouken@libsdl.org>
Sun, 06 Feb 2011 10:22:25 -0800
changeset 5209115fff0641ee
parent 5208 78db79f5a4e2
child 5210 1be088cec098
Prefer the OpenGL ES 2.0 context when it's available, make it possible to create an OpenGL 2.0 context on iPhoneOS
src/render/SDL_render.c
src/render/SDL_sysrender.h
src/render/opengles/SDL_render_gles.c
src/render/opengles2/SDL_render_gles2.c
src/video/SDL_video.c
src/video/uikit/SDL_uikitopengles.m
src/video/uikit/SDL_uikitopenglview.h
src/video/uikit/SDL_uikitopenglview.m
     1.1 --- a/src/render/SDL_render.c	Sun Feb 06 09:02:10 2011 -0800
     1.2 +++ b/src/render/SDL_render.c	Sun Feb 06 10:22:25 2011 -0800
     1.3 @@ -50,12 +50,12 @@
     1.4  #if SDL_VIDEO_RENDER_OGL
     1.5      &GL_RenderDriver,
     1.6  #endif
     1.7 +#if SDL_VIDEO_RENDER_OGL_ES2
     1.8 +    &GLES2_RenderDriver,
     1.9 +#endif
    1.10  #if SDL_VIDEO_RENDER_OGL_ES
    1.11      &GLES_RenderDriver,
    1.12  #endif
    1.13 -#if SDL_VIDEO_RENDER_OGL_ES2
    1.14 -    &GLES2_RenderDriver,
    1.15 -#endif
    1.16  #if SDL_VIDEO_RENDER_DIRECTFB
    1.17      &DirectFB_RenderDriver,
    1.18  #endif
     2.1 --- a/src/render/SDL_sysrender.h	Sun Feb 06 09:02:10 2011 -0800
     2.2 +++ b/src/render/SDL_sysrender.h	Sun Feb 06 10:22:25 2011 -0800
     2.3 @@ -124,12 +124,12 @@
     2.4  #if SDL_VIDEO_RENDER_OGL
     2.5  extern SDL_RenderDriver GL_RenderDriver;
     2.6  #endif
     2.7 +#if SDL_VIDEO_RENDER_OGL_ES2
     2.8 +extern SDL_RenderDriver GLES2_RenderDriver;
     2.9 +#endif
    2.10  #if SDL_VIDEO_RENDER_OGL_ES
    2.11  extern SDL_RenderDriver GLES_RenderDriver;
    2.12  #endif
    2.13 -#if SDL_VIDEO_RENDER_OGL_ES2
    2.14 -extern SDL_RenderDriver GLES2_RenderDriver;
    2.15 -#endif
    2.16  #if SDL_VIDEO_RENDER_DIRECTFB
    2.17  extern SDL_RenderDriver DirectFB_RenderDriver;
    2.18  #endif
     3.1 --- a/src/render/opengles/SDL_render_gles.c	Sun Feb 06 09:02:10 2011 -0800
     3.2 +++ b/src/render/opengles/SDL_render_gles.c	Sun Feb 06 10:22:25 2011 -0800
     3.3 @@ -184,6 +184,9 @@
     3.4  
     3.5      renderer->info.flags = SDL_RENDERER_ACCELERATED;
     3.6  
     3.7 +    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
     3.8 +    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
     3.9 +
    3.10      data->context = SDL_GL_CreateContext(window);
    3.11      if (!data->context) {
    3.12          GLES_DestroyRenderer(renderer);
     4.1 --- a/src/render/opengles2/SDL_render_gles2.c	Sun Feb 06 09:02:10 2011 -0800
     4.2 +++ b/src/render/opengles2/SDL_render_gles2.c	Sun Feb 06 10:22:25 2011 -0800
     4.3 @@ -185,24 +185,29 @@
     4.4      GLES2_ProgramCacheEntry *entry;
     4.5      GLES2_ProgramCacheEntry *next;
     4.6  
     4.7 -    GLES2_ActivateRenderer(renderer);
     4.8 +    /* Deallocate everything */
     4.9 +    if (rdata) {
    4.10 +        GLES2_ActivateRenderer(renderer);
    4.11  
    4.12 -    /* Deallocate everything */
    4.13 -    entry = rdata->program_cache.head;
    4.14 -    while (entry)
    4.15 -    {
    4.16 -        glDeleteShader(entry->vertex_shader->id);
    4.17 -        glDeleteShader(entry->fragment_shader->id);
    4.18 -        SDL_free(entry->vertex_shader);
    4.19 -        SDL_free(entry->fragment_shader);
    4.20 -        glDeleteProgram(entry->id);
    4.21 -        next = entry->next;
    4.22 -        SDL_free(entry);
    4.23 -        entry = next;
    4.24 +        entry = rdata->program_cache.head;
    4.25 +        while (entry) {
    4.26 +            glDeleteShader(entry->vertex_shader->id);
    4.27 +            glDeleteShader(entry->fragment_shader->id);
    4.28 +            SDL_free(entry->vertex_shader);
    4.29 +            SDL_free(entry->fragment_shader);
    4.30 +            glDeleteProgram(entry->id);
    4.31 +            next = entry->next;
    4.32 +            SDL_free(entry);
    4.33 +            entry = next;
    4.34 +        }
    4.35 +        if (rdata->context) {
    4.36 +            SDL_GL_DeleteContext(rdata->context);
    4.37 +        }
    4.38 +        if (rdata->shader_formats) {
    4.39 +            SDL_free(rdata->shader_formats);
    4.40 +        }
    4.41 +        SDL_free(rdata);
    4.42      }
    4.43 -    SDL_GL_DeleteContext(rdata->context);
    4.44 -    SDL_free(rdata->shader_formats);
    4.45 -    SDL_free(renderer->driverdata);
    4.46      SDL_free(renderer);
    4.47  }
    4.48  
    4.49 @@ -1081,12 +1086,15 @@
    4.50  
    4.51      /* Create the renderer struct */
    4.52      renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(SDL_Renderer));
    4.53 +    if (!renderer) {
    4.54 +        SDL_OutOfMemory();
    4.55 +        return NULL;
    4.56 +    }
    4.57 +
    4.58      rdata = (GLES2_DriverContext *)SDL_calloc(1, sizeof(GLES2_DriverContext));
    4.59 -    if (!renderer)
    4.60 -    {
    4.61 +    if (!rdata) {
    4.62 +        GLES2_DestroyRenderer(renderer);
    4.63          SDL_OutOfMemory();
    4.64 -        SDL_free(renderer);
    4.65 -        SDL_free(rdata);
    4.66          return NULL;
    4.67      }
    4.68      renderer->info = GLES2_RenderDriver.info;
    4.69 @@ -1095,17 +1103,18 @@
    4.70  
    4.71      renderer->info.flags = SDL_RENDERER_ACCELERATED;
    4.72  
    4.73 -    /* Create the GL context */
    4.74 +    /* Create an OpenGL ES 2.0 context */
    4.75 +    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
    4.76 +    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
    4.77 +
    4.78      rdata->context = SDL_GL_CreateContext(window);
    4.79      if (!rdata->context)
    4.80      {
    4.81 -        SDL_free(renderer);
    4.82 -        SDL_free(rdata);
    4.83 +        GLES2_DestroyRenderer(renderer);
    4.84          return NULL;
    4.85      }
    4.86      if (SDL_GL_MakeCurrent(window, rdata->context) < 0) {
    4.87 -        SDL_free(renderer);
    4.88 -        SDL_free(rdata);
    4.89 +        GLES2_DestroyRenderer(renderer);
    4.90          return NULL;
    4.91      }
    4.92  
    4.93 @@ -1132,9 +1141,8 @@
    4.94      rdata->shader_formats = (GLenum *)SDL_calloc(nFormats, sizeof(GLenum));
    4.95      if (!rdata->shader_formats)
    4.96      {
    4.97 +        GLES2_DestroyRenderer(renderer);
    4.98          SDL_OutOfMemory();
    4.99 -        SDL_free(renderer);
   4.100 -        SDL_free(rdata);
   4.101          return NULL;
   4.102      }
   4.103      rdata->shader_format_count = nFormats;
   4.104 @@ -1144,10 +1152,8 @@
   4.105      glGetIntegerv(GL_SHADER_BINARY_FORMATS, (GLint *)rdata->shader_formats);
   4.106      if (glGetError() != GL_NO_ERROR)
   4.107      {
   4.108 +        GLES2_DestroyRenderer(renderer);
   4.109          SDL_SetError("Failed to query supported shader formats");
   4.110 -        SDL_free(renderer);
   4.111 -        SDL_free(rdata->shader_formats);
   4.112 -        SDL_free(rdata);
   4.113          return NULL;
   4.114      }
   4.115      if (hasCompiler)
     5.1 --- a/src/video/SDL_video.c	Sun Feb 06 09:02:10 2011 -0800
     5.2 +++ b/src/video/SDL_video.c	Sun Feb 06 10:22:25 2011 -0800
     5.3 @@ -30,13 +30,17 @@
     5.4  #include "SDL_pixels_c.h"
     5.5  #include "../events/SDL_events_c.h"
     5.6  
     5.7 +#if SDL_VIDEO_OPENGL
     5.8 +#include "SDL_opengl.h"
     5.9 +#endif /* SDL_VIDEO_OPENGL */
    5.10 +
    5.11  #if SDL_VIDEO_OPENGL_ES
    5.12  #include "SDL_opengles.h"
    5.13  #endif /* SDL_VIDEO_OPENGL_ES */
    5.14  
    5.15 -#if SDL_VIDEO_OPENGL
    5.16 -#include "SDL_opengl.h"
    5.17 -#endif /* SDL_VIDEO_OPENGL */
    5.18 +#if SDL_VIDEO_OPENGL_ES2
    5.19 +#include "SDL_opengles2.h"
    5.20 +#endif /* SDL_VIDEO_OPENGL_ES2 */
    5.21  
    5.22  #include "SDL_syswm.h"
    5.23  
    5.24 @@ -481,8 +485,16 @@
    5.25      _this->gl_config.multisamplesamples = 0;
    5.26      _this->gl_config.retained_backing = 1;
    5.27      _this->gl_config.accelerated = -1;  /* accelerated or not, both are fine */
    5.28 +#if SDL_VIDEO_OPENGL
    5.29      _this->gl_config.major_version = 2;
    5.30      _this->gl_config.minor_version = 1;
    5.31 +#elif SDL_VIDEO_OPENGL_ES2
    5.32 +    _this->gl_config.major_version = 2;
    5.33 +    _this->gl_config.minor_version = 0;
    5.34 +#elif SDL_VIDEO_OPENGL_ES
    5.35 +    _this->gl_config.major_version = 1;
    5.36 +    _this->gl_config.minor_version = 1;
    5.37 +#endif
    5.38  
    5.39      /* Initialize the video subsystem */
    5.40      if (_this->VideoInit(_this) < 0) {
    5.41 @@ -1897,7 +1909,7 @@
    5.42  SDL_bool
    5.43  SDL_GL_ExtensionSupported(const char *extension)
    5.44  {
    5.45 -#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
    5.46 +#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
    5.47      const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
    5.48      const char *extensions;
    5.49      const char *start;
    5.50 @@ -1951,7 +1963,7 @@
    5.51  int
    5.52  SDL_GL_SetAttribute(SDL_GLattr attr, int value)
    5.53  {
    5.54 -#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
    5.55 +#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
    5.56      int retval;
    5.57  
    5.58      if (!_this) {
    5.59 @@ -2032,7 +2044,7 @@
    5.60  int
    5.61  SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
    5.62  {
    5.63 -#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
    5.64 +#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
    5.65      void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
    5.66      GLenum(APIENTRY * glGetErrorFunc) (void);
    5.67      GLenum attrib = 0;
    5.68 @@ -2068,7 +2080,7 @@
    5.69          attrib = GL_ALPHA_BITS;
    5.70          break;
    5.71      case SDL_GL_DOUBLEBUFFER:
    5.72 -#ifndef SDL_VIDEO_OPENGL_ES
    5.73 +#if SDL_VIDEO_OPENGL
    5.74          attrib = GL_DOUBLEBUFFER;
    5.75          break;
    5.76  #else
    5.77 @@ -2084,7 +2096,7 @@
    5.78      case SDL_GL_STENCIL_SIZE:
    5.79          attrib = GL_STENCIL_BITS;
    5.80          break;
    5.81 -#ifndef SDL_VIDEO_OPENGL_ES
    5.82 +#if SDL_VIDEO_OPENGL
    5.83      case SDL_GL_ACCUM_RED_SIZE:
    5.84          attrib = GL_ACCUM_RED_BITS;
    5.85          break;
    5.86 @@ -2111,14 +2123,14 @@
    5.87          return 0;
    5.88  #endif
    5.89      case SDL_GL_MULTISAMPLEBUFFERS:
    5.90 -#ifndef SDL_VIDEO_OPENGL_ES
    5.91 +#if SDL_VIDEO_OPENGL
    5.92          attrib = GL_SAMPLE_BUFFERS_ARB;
    5.93  #else
    5.94          attrib = GL_SAMPLE_BUFFERS;
    5.95  #endif
    5.96          break;
    5.97      case SDL_GL_MULTISAMPLESAMPLES:
    5.98 -#ifndef SDL_VIDEO_OPENGL_ES
    5.99 +#if SDL_VIDEO_OPENGL
   5.100          attrib = GL_SAMPLES_ARB;
   5.101  #else
   5.102          attrib = GL_SAMPLES;
     6.1 --- a/src/video/uikit/SDL_uikitopengles.m	Sun Feb 06 09:02:10 2011 -0800
     6.2 +++ b/src/video/uikit/SDL_uikitopengles.m	Sun Feb 06 10:22:25 2011 -0800
     6.3 @@ -113,7 +113,8 @@
     6.4                                      gBits: _this->gl_config.green_size \
     6.5                                      bBits: _this->gl_config.blue_size \
     6.6                                      aBits: _this->gl_config.alpha_size \
     6.7 -                                    depthBits: _this->gl_config.depth_size];
     6.8 +                                    depthBits: _this->gl_config.depth_size \
     6.9 +                                    majorVersion: _this->gl_config.major_version];
    6.10      
    6.11      data->view = view;
    6.12      
     7.1 --- a/src/video/uikit/SDL_uikitopenglview.h	Sun Feb 06 09:02:10 2011 -0800
     7.2 +++ b/src/video/uikit/SDL_uikitopenglview.h	Sun Feb 06 10:22:25 2011 -0800
     7.3 @@ -26,26 +26,26 @@
     7.4  #import <OpenGLES/ES1/glext.h>
     7.5  #import "SDL_uikitview.h"
     7.6  /*
     7.7 -	This class wraps the CAEAGLLayer from CoreAnimation into a convenient UIView subclass.
     7.8 -	The view content is basically an EAGL surface you render your OpenGL scene into.
     7.9 -	Note that setting the view non-opaque will only work if the EAGL surface has an alpha channel.
    7.10 +    This class wraps the CAEAGLLayer from CoreAnimation into a convenient UIView subclass.
    7.11 +    The view content is basically an EAGL surface you render your OpenGL scene into.
    7.12 +    Note that setting the view non-opaque will only work if the EAGL surface has an alpha channel.
    7.13   */
    7.14  /* *INDENT-OFF* */
    7.15  @interface SDL_uikitopenglview : SDL_uikitview {
    7.16 -	
    7.17 +    
    7.18  @private
    7.19 -	/* The pixel dimensions of the backbuffer */
    7.20 -	GLint backingWidth;
    7.21 -	GLint backingHeight;
    7.22 -	
    7.23 -	EAGLContext *context;
    7.24 -	
    7.25 -	/* OpenGL names for the renderbuffer and framebuffers used to render to this view */
    7.26 -	GLuint viewRenderbuffer, viewFramebuffer;
    7.27 -	
    7.28 -	/* OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist) */
    7.29 -	GLuint depthRenderbuffer;
    7.30 -	
    7.31 +    /* The pixel dimensions of the backbuffer */
    7.32 +    GLint backingWidth;
    7.33 +    GLint backingHeight;
    7.34 +    
    7.35 +    EAGLContext *context;
    7.36 +    
    7.37 +    /* OpenGL names for the renderbuffer and framebuffers used to render to this view */
    7.38 +    GLuint viewRenderbuffer, viewFramebuffer;
    7.39 +    
    7.40 +    /* OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist) */
    7.41 +    GLuint depthRenderbuffer;
    7.42 +    
    7.43  }
    7.44  
    7.45  @property (nonatomic, retain, readonly) EAGLContext *context;
    7.46 @@ -54,12 +54,13 @@
    7.47  - (void)setCurrentContext;
    7.48  
    7.49  - (id)initWithFrame:(CGRect)frame
    7.50 -	retainBacking:(BOOL)retained \
    7.51 -	rBits:(int)rBits \
    7.52 -	gBits:(int)gBits \
    7.53 -	bBits:(int)bBits \
    7.54 -	aBits:(int)aBits \
    7.55 -	depthBits:(int)depthBits;
    7.56 +    retainBacking:(BOOL)retained \
    7.57 +    rBits:(int)rBits \
    7.58 +    gBits:(int)gBits \
    7.59 +    bBits:(int)bBits \
    7.60 +    aBits:(int)aBits \
    7.61 +    depthBits:(int)depthBits \
    7.62 +    majorVersion:(int)majorVersion;
    7.63  
    7.64  @end
    7.65  /* *INDENT-ON* */
     8.1 --- a/src/video/uikit/SDL_uikitopenglview.m	Sun Feb 06 09:02:10 2011 -0800
     8.2 +++ b/src/video/uikit/SDL_uikitopenglview.m	Sun Feb 06 10:22:25 2011 -0800
     8.3 @@ -47,6 +47,7 @@
     8.4        bBits:(int)bBits \
     8.5        aBits:(int)aBits \
     8.6        depthBits:(int)depthBits \
     8.7 +      majorVersion:(int)majorVersion \
     8.8  {
     8.9      NSString *colorFormat=nil;
    8.10      GLuint depthBufferFormat;
    8.11 @@ -86,8 +87,11 @@
    8.12          eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
    8.13                                          [NSNumber numberWithBool: retained], kEAGLDrawablePropertyRetainedBacking, colorFormat, kEAGLDrawablePropertyColorFormat, nil];
    8.14          
    8.15 -        context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES1];
    8.16 -        
    8.17 +        if (majorVersion > 1) {
    8.18 +            context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
    8.19 +        } else {
    8.20 +            context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES1];
    8.21 +        }
    8.22          if (!context || ![EAGLContext setCurrentContext:context]) {
    8.23              [self release];
    8.24              return nil;