src/render/opengles2/SDL_render_gles2.c
changeset 6188 e82023802002
parent 6178 1fc633857667
child 6190 519380462400
equal deleted inserted replaced
6187:6b1932e5a36b 6188:e82023802002
    25 #include "SDL_hints.h"
    25 #include "SDL_hints.h"
    26 #include "SDL_opengles2.h"
    26 #include "SDL_opengles2.h"
    27 #include "../SDL_sysrender.h"
    27 #include "../SDL_sysrender.h"
    28 #include "SDL_shaders_gles2.h"
    28 #include "SDL_shaders_gles2.h"
    29 
    29 
       
    30 /* Used to re-create the window with OpenGL capability */
       
    31 extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
       
    32 
    30 /*************************************************************************************************
    33 /*************************************************************************************************
    31  * Bootstrap data                                                                                *
    34  * Bootstrap data                                                                                *
    32  *************************************************************************************************/
    35  *************************************************************************************************/
    33 
    36 
    34 static SDL_Renderer *GLES2_CreateRenderer(SDL_Window *window, Uint32 flags);
    37 static SDL_Renderer *GLES2_CreateRenderer(SDL_Window *window, Uint32 flags);
   126     struct {
   129     struct {
   127         int blendMode;
   130         int blendMode;
   128         SDL_bool tex_coords;
   131         SDL_bool tex_coords;
   129     } current;
   132     } current;
   130 
   133 
       
   134 #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
       
   135 #include "SDL_gles2funcs.h"
       
   136 #undef SDL_PROC
       
   137 
   131     int shader_format_count;
   138     int shader_format_count;
   132     GLenum *shader_formats;
   139     GLenum *shader_formats;
   133     GLES2_ShaderCache shader_cache;
   140     GLES2_ShaderCache shader_cache;
   134     GLES2_ProgramCache program_cache;
   141     GLES2_ProgramCache program_cache;
   135     GLES2_ProgramCacheEntry *current_program;
   142     GLES2_ProgramCacheEntry *current_program;
   147 static int GLES2_UpdateViewport(SDL_Renderer * renderer);
   154 static int GLES2_UpdateViewport(SDL_Renderer * renderer);
   148 static void GLES2_DestroyRenderer(SDL_Renderer *renderer);
   155 static void GLES2_DestroyRenderer(SDL_Renderer *renderer);
   149 
   156 
   150 static SDL_GLContext SDL_CurrentContext = NULL;
   157 static SDL_GLContext SDL_CurrentContext = NULL;
   151 
   158 
       
   159 static int GLES2_LoadFunctions(GLES2_DriverContext * data)
       
   160 {
       
   161 #ifdef __SDL_NOGETPROCADDR__
       
   162 #define SDL_PROC(ret,func,params) data->func=func;
       
   163 #else
       
   164 #define SDL_PROC(ret,func,params) \
       
   165     do { \
       
   166         data->func = SDL_GL_GetProcAddress(#func); \
       
   167         if ( ! data->func ) { \
       
   168             SDL_SetError("Couldn't load GLES2 function %s: %s\n", #func, SDL_GetError()); \
       
   169             return -1; \
       
   170         } \
       
   171     } while ( 0 );  
       
   172 #endif /* _SDL_NOGETPROCADDR_ */
       
   173 
       
   174 #include "SDL_gles2funcs.h"
       
   175 #undef SDL_PROC
       
   176     return 0;
       
   177 }
       
   178 
   152 static int
   179 static int
   153 GLES2_ActivateRenderer(SDL_Renderer * renderer)
   180 GLES2_ActivateRenderer(SDL_Renderer * renderer)
   154 {
   181 {
   155     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
   182     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
   156 
   183 
   169 }
   196 }
   170 
   197 
   171 static void
   198 static void
   172 GLES2_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
   199 GLES2_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
   173 {
   200 {
       
   201     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
       
   202     
   174     if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
   203     if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
   175         /* Rebind the context to the window area */
   204         /* Rebind the context to the window area */
   176         SDL_CurrentContext = NULL;
   205         SDL_CurrentContext = NULL;
   177     }
   206     }
   178 
   207 
   179     if (event->event == SDL_WINDOWEVENT_MINIMIZED) {
   208     if (event->event == SDL_WINDOWEVENT_MINIMIZED) {
   180         /* According to Apple documentation, we need to finish drawing NOW! */
   209         /* According to Apple documentation, we need to finish drawing NOW! */
   181         glFinish();
   210         rdata->glFinish();
   182     }
   211     }
   183 }
   212 }
   184 
   213 
   185 static int
   214 static int
   186 GLES2_UpdateViewport(SDL_Renderer * renderer)
   215 GLES2_UpdateViewport(SDL_Renderer * renderer)
   190     if (SDL_CurrentContext != rdata->context) {
   219     if (SDL_CurrentContext != rdata->context) {
   191         /* We'll update the viewport after we rebind the context */
   220         /* We'll update the viewport after we rebind the context */
   192         return 0;
   221         return 0;
   193     }
   222     }
   194 
   223 
   195     glViewport(renderer->viewport.x, renderer->viewport.y,
   224     rdata->glViewport(renderer->viewport.x, renderer->viewport.y,
   196                renderer->viewport.w, renderer->viewport.h);
   225                renderer->viewport.w, renderer->viewport.h);
   197     return 0;
   226     return 0;
   198 }
   227 }
   199 
   228 
   200 static void
   229 static void
   210             GLES2_ShaderCacheEntry *entry;
   239             GLES2_ShaderCacheEntry *entry;
   211             GLES2_ShaderCacheEntry *next;
   240             GLES2_ShaderCacheEntry *next;
   212             entry = rdata->shader_cache.head;
   241             entry = rdata->shader_cache.head;
   213             while (entry)
   242             while (entry)
   214             {
   243             {
   215                 glDeleteShader(entry->id);
   244                 rdata->glDeleteShader(entry->id);
   216                 next = entry->next;
   245                 next = entry->next;
   217                 SDL_free(entry);
   246                 SDL_free(entry);
   218                 entry = next;
   247                 entry = next;
   219             }
   248             }
   220         }
   249         }
   221         {
   250         {
   222             GLES2_ProgramCacheEntry *entry;
   251             GLES2_ProgramCacheEntry *entry;
   223             GLES2_ProgramCacheEntry *next;
   252             GLES2_ProgramCacheEntry *next;
   224             entry = rdata->program_cache.head;
   253             entry = rdata->program_cache.head;
   225             while (entry) {
   254             while (entry) {
   226                 glDeleteProgram(entry->id);
   255                 rdata->glDeleteProgram(entry->id);
   227                 next = entry->next;
   256                 next = entry->next;
   228                 SDL_free(entry);
   257                 SDL_free(entry);
   229                 entry = next;
   258                 entry = next;
   230             }
   259             }
   231         }
   260         }
   265 }
   294 }
   266 
   295 
   267 static int
   296 static int
   268 GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
   297 GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
   269 {
   298 {
       
   299     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
   270     GLES2_TextureData *tdata;
   300     GLES2_TextureData *tdata;
   271     GLenum format;
   301     GLenum format;
   272     GLenum type;
   302     GLenum type;
   273     GLenum scaleMode;
   303     GLenum scaleMode;
   274 
   304 
   314             return -1;
   344             return -1;
   315         }
   345         }
   316     }
   346     }
   317 
   347 
   318     /* Allocate the texture */
   348     /* Allocate the texture */
   319     glGetError();
   349     rdata->glGetError();
   320     glGenTextures(1, &tdata->texture);
   350     rdata->glGenTextures(1, &tdata->texture);
   321     glActiveTexture(GL_TEXTURE0);
   351     rdata->glActiveTexture(GL_TEXTURE0);
   322     glBindTexture(tdata->texture_type, tdata->texture);
   352     rdata->glBindTexture(tdata->texture_type, tdata->texture);
   323     glTexParameteri(tdata->texture_type, GL_TEXTURE_MIN_FILTER, scaleMode);
   353     rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_MIN_FILTER, scaleMode);
   324     glTexParameteri(tdata->texture_type, GL_TEXTURE_MAG_FILTER, scaleMode);
   354     rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_MAG_FILTER, scaleMode);
   325     glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   355     rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   326     glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   356     rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   327     glTexImage2D(tdata->texture_type, 0, format, texture->w, texture->h, 0, format, type, NULL);
   357     rdata->glTexImage2D(tdata->texture_type, 0, format, texture->w, texture->h, 0, format, type, NULL);
   328     if (glGetError() != GL_NO_ERROR)
   358     if (rdata->glGetError() != GL_NO_ERROR)
   329     {
   359     {
   330         SDL_SetError("Texture creation failed");
   360         SDL_SetError("Texture creation failed");
   331         glDeleteTextures(1, &tdata->texture);
   361         rdata->glDeleteTextures(1, &tdata->texture);
   332         SDL_free(tdata);
   362         SDL_free(tdata);
   333         return -1;
   363         return -1;
   334     }
   364     }
   335     texture->driverdata = tdata;
   365     texture->driverdata = tdata;
   336     return 0;
   366     return 0;
   337 }
   367 }
   338 
   368 
   339 static void
   369 static void
   340 GLES2_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture)
   370 GLES2_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture)
   341 {
   371 {
       
   372     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
   342     GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
   373     GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
   343 
   374 
   344     GLES2_ActivateRenderer(renderer);
   375     GLES2_ActivateRenderer(renderer);
   345 
   376 
   346     /* Destroy the texture */
   377     /* Destroy the texture */
   347     if (tdata)
   378     if (tdata)
   348     {
   379     {
   349         glDeleteTextures(1, &tdata->texture);
   380         rdata->glDeleteTextures(1, &tdata->texture);
   350         SDL_free(tdata->pixel_data);
   381         SDL_free(tdata->pixel_data);
   351         SDL_free(tdata);
   382         SDL_free(tdata);
   352         texture->driverdata = NULL;
   383         texture->driverdata = NULL;
   353     }
   384     }
   354 }
   385 }
   384 
   415 
   385 static int
   416 static int
   386 GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
   417 GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
   387                     const void *pixels, int pitch)
   418                     const void *pixels, int pitch)
   388 {
   419 {
       
   420     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
   389     GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
   421     GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
   390     Uint8 *blob = NULL;
   422     Uint8 *blob = NULL;
   391     Uint8 *src;
   423     Uint8 *src;
   392     int srcPitch;
   424     int srcPitch;
   393     int y;
   425     int y;
   394 
   426 
   395     GLES2_ActivateRenderer(renderer);
   427     GLES2_ActivateRenderer(renderer);
   396 
   428     
   397     /* Bail out if we're supposed to update an empty rectangle */
   429     /* Bail out if we're supposed to update an empty rectangle */
   398     if (rect->w <= 0 || rect->h <= 0)
   430     if (rect->w <= 0 || rect->h <= 0)
   399         return 0;
   431         return 0;
   400 
   432 
   401     /* Reformat the texture data into a tightly packed array */
   433     /* Reformat the texture data into a tightly packed array */
   418         }
   450         }
   419         src = blob;
   451         src = blob;
   420     }
   452     }
   421 
   453 
   422     /* Create a texture subimage with the supplied data */
   454     /* Create a texture subimage with the supplied data */
   423     glGetError();
   455     rdata->glGetError();
   424     glActiveTexture(GL_TEXTURE0);
   456     rdata->glActiveTexture(GL_TEXTURE0);
   425     glBindTexture(tdata->texture_type, tdata->texture);
   457     rdata->glBindTexture(tdata->texture_type, tdata->texture);
   426     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   458     rdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   427     glTexSubImage2D(tdata->texture_type,
   459     rdata->glTexSubImage2D(tdata->texture_type,
   428                     0,
   460                     0,
   429                     rect->x,
   461                     rect->x,
   430                     rect->y,
   462                     rect->y,
   431                     rect->w,
   463                     rect->w,
   432                     rect->h,
   464                     rect->h,
   435                     src);
   467                     src);
   436     if (blob) {
   468     if (blob) {
   437         SDL_free(blob);
   469         SDL_free(blob);
   438     }
   470     }
   439 
   471 
   440     if (glGetError() != GL_NO_ERROR)
   472     if (rdata->glGetError() != GL_NO_ERROR)
   441     {
   473     {
   442         SDL_SetError("Failed to update texture");
   474         SDL_SetError("Failed to update texture");
   443         return -1;
   475         return -1;
   444     }
   476     }
   445     return 0;
   477     return 0;
   503     entry->vertex_shader = vertex;
   535     entry->vertex_shader = vertex;
   504     entry->fragment_shader = fragment;
   536     entry->fragment_shader = fragment;
   505     entry->blend_mode = blendMode;
   537     entry->blend_mode = blendMode;
   506     
   538     
   507     /* Create the program and link it */
   539     /* Create the program and link it */
   508     glGetError();
   540     rdata->glGetError();
   509     entry->id = glCreateProgram();
   541     entry->id = rdata->glCreateProgram();
   510     glAttachShader(entry->id, vertex->id);
   542     rdata->glAttachShader(entry->id, vertex->id);
   511     glAttachShader(entry->id, fragment->id);
   543     rdata->glAttachShader(entry->id, fragment->id);
   512     glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_POSITION, "a_position");
   544     rdata->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_POSITION, "a_position");
   513     glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_TEXCOORD, "a_texCoord");
   545     rdata->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_TEXCOORD, "a_texCoord");
   514     glLinkProgram(entry->id);
   546     rdata->glLinkProgram(entry->id);
   515     glGetProgramiv(entry->id, GL_LINK_STATUS, &linkSuccessful);
   547     rdata->glGetProgramiv(entry->id, GL_LINK_STATUS, &linkSuccessful);
   516     if (glGetError() != GL_NO_ERROR || !linkSuccessful)
   548     if (rdata->glGetError() != GL_NO_ERROR || !linkSuccessful)
   517     {
   549     {
   518         SDL_SetError("Failed to link shader program");
   550         SDL_SetError("Failed to link shader program");
   519         glDeleteProgram(entry->id);
   551         rdata->glDeleteProgram(entry->id);
   520         SDL_free(entry);
   552         SDL_free(entry);
   521         return NULL;
   553         return NULL;
   522     }
   554     }
   523     
   555     
   524     /* Predetermine locations of uniform variables */
   556     /* Predetermine locations of uniform variables */
   525     entry->uniform_locations[GLES2_UNIFORM_PROJECTION] =
   557     entry->uniform_locations[GLES2_UNIFORM_PROJECTION] =
   526         glGetUniformLocation(entry->id, "u_projection");
   558         rdata->glGetUniformLocation(entry->id, "u_projection");
   527     entry->uniform_locations[GLES2_UNIFORM_TEXTURE] =
   559     entry->uniform_locations[GLES2_UNIFORM_TEXTURE] =
   528         glGetUniformLocation(entry->id, "u_texture");
   560         rdata->glGetUniformLocation(entry->id, "u_texture");
   529     entry->uniform_locations[GLES2_UNIFORM_MODULATION] =
   561     entry->uniform_locations[GLES2_UNIFORM_MODULATION] =
   530         glGetUniformLocation(entry->id, "u_modulation");
   562         rdata->glGetUniformLocation(entry->id, "u_modulation");
   531     entry->uniform_locations[GLES2_UNIFORM_COLOR] =
   563     entry->uniform_locations[GLES2_UNIFORM_COLOR] =
   532         glGetUniformLocation(entry->id, "u_color");
   564         rdata->glGetUniformLocation(entry->id, "u_color");
   533     entry->uniform_locations[GLES2_UNIFORM_COLORTABLE] =
   565     entry->uniform_locations[GLES2_UNIFORM_COLORTABLE] =
   534         glGetUniformLocation(entry->id, "u_colorTable");
   566         rdata->glGetUniformLocation(entry->id, "u_colorTable");
   535 
   567 
   536     /* Cache the linked program */
   568     /* Cache the linked program */
   537     if (rdata->program_cache.head)
   569     if (rdata->program_cache.head)
   538     {
   570     {
   539         entry->next = rdata->program_cache.head;
   571         entry->next = rdata->program_cache.head;
   557         if (--shaderEntry->references <= 0)
   589         if (--shaderEntry->references <= 0)
   558             GLES2_EvictShader(renderer, shaderEntry);
   590             GLES2_EvictShader(renderer, shaderEntry);
   559         shaderEntry = rdata->program_cache.tail->fragment_shader;
   591         shaderEntry = rdata->program_cache.tail->fragment_shader;
   560         if (--shaderEntry->references <= 0)
   592         if (--shaderEntry->references <= 0)
   561             GLES2_EvictShader(renderer, shaderEntry);
   593             GLES2_EvictShader(renderer, shaderEntry);
   562         glDeleteProgram(rdata->program_cache.tail->id);
   594         rdata->glDeleteProgram(rdata->program_cache.tail->id);
   563         rdata->program_cache.tail = rdata->program_cache.tail->prev;
   595         rdata->program_cache.tail = rdata->program_cache.tail->prev;
   564         SDL_free(rdata->program_cache.tail->next);
   596         SDL_free(rdata->program_cache.tail->next);
   565         rdata->program_cache.tail->next = NULL;
   597         rdata->program_cache.tail->next = NULL;
   566         --rdata->program_cache.count;
   598         --rdata->program_cache.count;
   567     }
   599     }
   626     }
   658     }
   627     entry->type = type;
   659     entry->type = type;
   628     entry->instance = instance;
   660     entry->instance = instance;
   629 
   661 
   630     /* Compile or load the selected shader instance */
   662     /* Compile or load the selected shader instance */
   631     glGetError();
   663     rdata->glGetError();
   632     entry->id = glCreateShader(instance->type);
   664     entry->id = rdata->glCreateShader(instance->type);
   633     if (instance->format == (GLenum)-1)
   665     if (instance->format == (GLenum)-1)
   634     {
   666     {
   635         glShaderSource(entry->id, 1, (const char **)&instance->data, NULL);
   667         rdata->glShaderSource(entry->id, 1, (const char **)&instance->data, NULL);
   636         glCompileShader(entry->id);
   668         rdata->glCompileShader(entry->id);
   637         glGetShaderiv(entry->id, GL_COMPILE_STATUS, &compileSuccessful);
   669         rdata->glGetShaderiv(entry->id, GL_COMPILE_STATUS, &compileSuccessful);
   638     }
   670     }
   639     else
   671     else
   640     {
   672     {
   641         glShaderBinary(1, &entry->id, instance->format, instance->data, instance->length);
   673         rdata->glShaderBinary(1, &entry->id, instance->format, instance->data, instance->length);
   642         compileSuccessful = GL_TRUE;
   674         compileSuccessful = GL_TRUE;
   643     }
   675     }
   644     if (glGetError() != GL_NO_ERROR || !compileSuccessful)
   676     if (rdata->glGetError() != GL_NO_ERROR || !compileSuccessful)
   645     {
   677     {
   646         char *info = NULL;
   678         char *info = NULL;
   647         int length = 0;
   679         int length = 0;
   648 
   680 
   649         glGetShaderiv(entry->id, GL_INFO_LOG_LENGTH, &length);
   681         rdata->glGetShaderiv(entry->id, GL_INFO_LOG_LENGTH, &length);
   650         if (length > 0) {
   682         if (length > 0) {
   651             info = SDL_stack_alloc(char, length);
   683             info = SDL_stack_alloc(char, length);
   652             if (info) {
   684             if (info) {
   653                 glGetShaderInfoLog(entry->id, length, &length, info);
   685                 rdata->glGetShaderInfoLog(entry->id, length, &length, info);
   654             }
   686             }
   655         }
   687         }
   656         if (info) {
   688         if (info) {
   657             SDL_SetError("Failed to load the shader: %s", info);
   689             SDL_SetError("Failed to load the shader: %s", info);
   658             SDL_stack_free(info);
   690             SDL_stack_free(info);
   659         } else {
   691         } else {
   660             SDL_SetError("Failed to load the shader");
   692             SDL_SetError("Failed to load the shader");
   661         }
   693         }
   662         glDeleteShader(entry->id);
   694         rdata->glDeleteShader(entry->id);
   663         SDL_free(entry);
   695         SDL_free(entry);
   664         return NULL;
   696         return NULL;
   665     }
   697     }
   666 
   698 
   667     /* Link the shader entry in at the front of the cache */
   699     /* Link the shader entry in at the front of the cache */
   688     if (rdata->shader_cache.head == entry)
   720     if (rdata->shader_cache.head == entry)
   689         rdata->shader_cache.head = entry->next;
   721         rdata->shader_cache.head = entry->next;
   690     --rdata->shader_cache.count;
   722     --rdata->shader_cache.count;
   691 
   723 
   692     /* Deallocate the shader */
   724     /* Deallocate the shader */
   693     glDeleteShader(entry->id);
   725     rdata->glDeleteShader(entry->id);
   694     SDL_free(entry);
   726     SDL_free(entry);
   695 }
   727 }
   696 
   728 
   697 static int
   729 static int
   698 GLES2_SelectProgram(SDL_Renderer *renderer, GLES2_ImageSource source, SDL_BlendMode blendMode)
   730 GLES2_SelectProgram(SDL_Renderer *renderer, GLES2_ImageSource source, SDL_BlendMode blendMode)
   744     program = GLES2_CacheProgram(renderer, vertex, fragment, blendMode);
   776     program = GLES2_CacheProgram(renderer, vertex, fragment, blendMode);
   745     if (!program)
   777     if (!program)
   746         goto fault;
   778         goto fault;
   747 
   779 
   748     /* Select that program in OpenGL */
   780     /* Select that program in OpenGL */
   749     glGetError();
   781     rdata->glGetError();
   750     glUseProgram(program->id);
   782     rdata->glUseProgram(program->id);
   751     if (glGetError() != GL_NO_ERROR)
   783     if (rdata->glGetError() != GL_NO_ERROR)
   752     {
   784     {
   753         SDL_SetError("Failed to select program");
   785         SDL_SetError("Failed to select program");
   754         goto fault;
   786         goto fault;
   755     }
   787     }
   756 
   788 
   797     projection[3][2] = 0.0f;
   829     projection[3][2] = 0.0f;
   798     projection[3][3] = 1.0f;
   830     projection[3][3] = 1.0f;
   799 
   831 
   800     /* Set the projection matrix */
   832     /* Set the projection matrix */
   801     locProjection = rdata->current_program->uniform_locations[GLES2_UNIFORM_PROJECTION];
   833     locProjection = rdata->current_program->uniform_locations[GLES2_UNIFORM_PROJECTION];
   802     glGetError();
   834     rdata->glGetError();
   803     glUniformMatrix4fv(locProjection, 1, GL_FALSE, (GLfloat *)projection);
   835     rdata->glUniformMatrix4fv(locProjection, 1, GL_FALSE, (GLfloat *)projection);
   804     if (glGetError() != GL_NO_ERROR)
   836     if (rdata->glGetError() != GL_NO_ERROR)
   805     {
   837     {
   806         SDL_SetError("Failed to set orthographic projection");
   838         SDL_SetError("Failed to set orthographic projection");
   807         return -1;
   839         return -1;
   808     }
   840     }
   809     return 0;
   841     return 0;
   827 
   859 
   828 
   860 
   829 static int
   861 static int
   830 GLES2_RenderClear(SDL_Renderer * renderer)
   862 GLES2_RenderClear(SDL_Renderer * renderer)
   831 {
   863 {
       
   864     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
       
   865 
   832     GLES2_ActivateRenderer(renderer);
   866     GLES2_ActivateRenderer(renderer);
   833 
   867     
   834     glClearColor((GLfloat) renderer->r * inv255f,
   868     rdata->glClearColor((GLfloat) renderer->r * inv255f,
   835                  (GLfloat) renderer->g * inv255f,
   869                  (GLfloat) renderer->g * inv255f,
   836                  (GLfloat) renderer->b * inv255f,
   870                  (GLfloat) renderer->b * inv255f,
   837                  (GLfloat) renderer->a * inv255f);
   871                  (GLfloat) renderer->a * inv255f);
   838 
   872 
   839     glClear(GL_COLOR_BUFFER_BIT);
   873     rdata->glClear(GL_COLOR_BUFFER_BIT);
   840 
   874 
   841     return 0;
   875     return 0;
   842 }
   876 }
   843 
   877 
   844 static void
   878 static void
   846 {
   880 {
   847     if (blendMode != rdata->current.blendMode) {
   881     if (blendMode != rdata->current.blendMode) {
   848         switch (blendMode) {
   882         switch (blendMode) {
   849         default:
   883         default:
   850         case SDL_BLENDMODE_NONE:
   884         case SDL_BLENDMODE_NONE:
   851             glDisable(GL_BLEND);
   885             rdata->glDisable(GL_BLEND);
   852             break;
   886             break;
   853         case SDL_BLENDMODE_BLEND:
   887         case SDL_BLENDMODE_BLEND:
   854             glEnable(GL_BLEND);
   888             rdata->glEnable(GL_BLEND);
   855             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   889             rdata->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   856             break;
   890             break;
   857         case SDL_BLENDMODE_ADD:
   891         case SDL_BLENDMODE_ADD:
   858             glEnable(GL_BLEND);
   892             rdata->glEnable(GL_BLEND);
   859             glBlendFunc(GL_SRC_ALPHA, GL_ONE);
   893             rdata->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
   860             break;
   894             break;
   861         case SDL_BLENDMODE_MOD:
   895         case SDL_BLENDMODE_MOD:
   862             glEnable(GL_BLEND);
   896             rdata->glEnable(GL_BLEND);
   863             glBlendFunc(GL_ZERO, GL_SRC_COLOR);
   897             rdata->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
   864             break;
   898             break;
   865         }
   899         }
   866         rdata->current.blendMode = blendMode;
   900         rdata->current.blendMode = blendMode;
   867     }
   901     }
   868 }
   902 }
   870 static void
   904 static void
   871 GLES2_SetTexCoords(GLES2_DriverContext * rdata, SDL_bool enabled)
   905 GLES2_SetTexCoords(GLES2_DriverContext * rdata, SDL_bool enabled)
   872 {
   906 {
   873     if (enabled != rdata->current.tex_coords) {
   907     if (enabled != rdata->current.tex_coords) {
   874         if (enabled) {
   908         if (enabled) {
   875             glEnableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
   909             rdata->glEnableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
   876         } else {
   910         } else {
   877             glDisableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
   911             rdata->glDisableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
   878         }
   912         }
   879         rdata->current.tex_coords = enabled;
   913         rdata->current.tex_coords = enabled;
   880     }
   914     }
   881 }
   915 }
   882 
   916 
   885 {
   919 {
   886     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
   920     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
   887     int blendMode = renderer->blendMode;
   921     int blendMode = renderer->blendMode;
   888     GLuint locColor;
   922     GLuint locColor;
   889 
   923 
   890     glGetError();
   924     rdata->glGetError();
   891 
   925 
   892     GLES2_ActivateRenderer(renderer);
   926     GLES2_ActivateRenderer(renderer);
   893 
   927 
   894     GLES2_SetBlendMode(rdata, blendMode);
   928     GLES2_SetBlendMode(rdata, blendMode);
   895 
   929 
   899     if (GLES2_SelectProgram(renderer, GLES2_IMAGESOURCE_SOLID, blendMode) < 0)
   933     if (GLES2_SelectProgram(renderer, GLES2_IMAGESOURCE_SOLID, blendMode) < 0)
   900         return -1;
   934         return -1;
   901 
   935 
   902     /* Select the color to draw with */
   936     /* Select the color to draw with */
   903     locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
   937     locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
   904     glUniform4f(locColor,
   938     rdata->glUniform4f(locColor,
   905                 renderer->r * inv255f,
   939                 renderer->r * inv255f,
   906                 renderer->g * inv255f,
   940                 renderer->g * inv255f,
   907                 renderer->b * inv255f,
   941                 renderer->b * inv255f,
   908                 renderer->a * inv255f);
   942                 renderer->a * inv255f);
   909     return 0;
   943     return 0;
   910 }
   944 }
   911 
   945 
   912 static int
   946 static int
   913 GLES2_RenderDrawPoints(SDL_Renderer *renderer, const SDL_Point *points, int count)
   947 GLES2_RenderDrawPoints(SDL_Renderer *renderer, const SDL_Point *points, int count)
   914 {
   948 {
       
   949     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
   915     GLfloat *vertices;
   950     GLfloat *vertices;
   916     int idx;
   951     int idx;
   917 
   952 
   918     if (GLES2_SetDrawingState(renderer) < 0) {
   953     if (GLES2_SetDrawingState(renderer) < 0) {
   919         return -1;
   954         return -1;
   927         GLfloat y = (GLfloat)points[idx].y + 0.5f;
   962         GLfloat y = (GLfloat)points[idx].y + 0.5f;
   928 
   963 
   929         vertices[idx * 2] = x;
   964         vertices[idx * 2] = x;
   930         vertices[(idx * 2) + 1] = y;
   965         vertices[(idx * 2) + 1] = y;
   931     }
   966     }
   932     glGetError();
   967     rdata->glGetError();
   933     glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
   968     rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
   934     glDrawArrays(GL_POINTS, 0, count);
   969     rdata->glDrawArrays(GL_POINTS, 0, count);
   935     SDL_stack_free(vertices);
   970     SDL_stack_free(vertices);
   936     if (glGetError() != GL_NO_ERROR)
   971     if (rdata->glGetError() != GL_NO_ERROR)
   937     {
   972     {
   938         SDL_SetError("Failed to render lines");
   973         SDL_SetError("Failed to render lines");
   939         return -1;
   974         return -1;
   940     }
   975     }
   941     return 0;
   976     return 0;
   942 }
   977 }
   943 
   978 
   944 static int
   979 static int
   945 GLES2_RenderDrawLines(SDL_Renderer *renderer, const SDL_Point *points, int count)
   980 GLES2_RenderDrawLines(SDL_Renderer *renderer, const SDL_Point *points, int count)
   946 {
   981 {
       
   982     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
   947     GLfloat *vertices;
   983     GLfloat *vertices;
   948     int idx;
   984     int idx;
   949 
   985 
   950     if (GLES2_SetDrawingState(renderer) < 0) {
   986     if (GLES2_SetDrawingState(renderer) < 0) {
   951         return -1;
   987         return -1;
   959         GLfloat y = (GLfloat)points[idx].y + 0.5f;
   995         GLfloat y = (GLfloat)points[idx].y + 0.5f;
   960 
   996 
   961         vertices[idx * 2] = x;
   997         vertices[idx * 2] = x;
   962         vertices[(idx * 2) + 1] = y;
   998         vertices[(idx * 2) + 1] = y;
   963     }
   999     }
   964     glGetError();
  1000     rdata->glGetError();
   965     glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  1001     rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
   966     glDrawArrays(GL_LINE_STRIP, 0, count);
  1002     rdata->glDrawArrays(GL_LINE_STRIP, 0, count);
   967 
  1003 
   968     /* We need to close the endpoint of the line */
  1004     /* We need to close the endpoint of the line */
   969     if (count == 2 ||
  1005     if (count == 2 ||
   970         points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
  1006         points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
   971         glDrawArrays(GL_POINTS, count-1, 1);
  1007         rdata->glDrawArrays(GL_POINTS, count-1, 1);
   972     }
  1008     }
   973     SDL_stack_free(vertices);
  1009     SDL_stack_free(vertices);
   974     if (glGetError() != GL_NO_ERROR)
  1010     if (rdata->glGetError() != GL_NO_ERROR)
   975     {
  1011     {
   976         SDL_SetError("Failed to render lines");
  1012         SDL_SetError("Failed to render lines");
   977         return -1;
  1013         return -1;
   978     }
  1014     }
   979     return 0;
  1015     return 0;
   980 }
  1016 }
   981 
  1017 
   982 static int
  1018 static int
   983 GLES2_RenderFillRects(SDL_Renderer *renderer, const SDL_Rect *rects, int count)
  1019 GLES2_RenderFillRects(SDL_Renderer *renderer, const SDL_Rect *rects, int count)
   984 {
  1020 {
       
  1021     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
   985     GLfloat vertices[8];
  1022     GLfloat vertices[8];
   986     int idx;
  1023     int idx;
   987 
  1024 
   988     if (GLES2_SetDrawingState(renderer) < 0) {
  1025     if (GLES2_SetDrawingState(renderer) < 0) {
   989         return -1;
  1026         return -1;
   990     }
  1027     }
   991 
  1028 
   992     /* Emit a line loop for each rectangle */
  1029     /* Emit a line loop for each rectangle */
   993     glGetError();
  1030     rdata->glGetError();
   994     for (idx = 0; idx < count; ++idx) {
  1031     for (idx = 0; idx < count; ++idx) {
   995         const SDL_Rect *rect = &rects[idx];
  1032         const SDL_Rect *rect = &rects[idx];
   996 
  1033 
   997         GLfloat xMin = (GLfloat)rect->x;
  1034         GLfloat xMin = (GLfloat)rect->x;
   998         GLfloat xMax = (GLfloat)(rect->x + rect->w);
  1035         GLfloat xMax = (GLfloat)(rect->x + rect->w);
  1005         vertices[3] = yMin;
  1042         vertices[3] = yMin;
  1006         vertices[4] = xMin;
  1043         vertices[4] = xMin;
  1007         vertices[5] = yMax;
  1044         vertices[5] = yMax;
  1008         vertices[6] = xMax;
  1045         vertices[6] = xMax;
  1009         vertices[7] = yMax;
  1046         vertices[7] = yMax;
  1010         glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  1047         rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  1011         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  1048         rdata->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  1012     }
  1049     }
  1013     if (glGetError() != GL_NO_ERROR)
  1050     if (rdata->glGetError() != GL_NO_ERROR)
  1014     {
  1051     {
  1015         SDL_SetError("Failed to render lines");
  1052         SDL_SetError("Failed to render lines");
  1016         return -1;
  1053         return -1;
  1017     }
  1054     }
  1018     return 0;
  1055     return 0;
  1053     if (GLES2_SelectProgram(renderer, sourceType, blendMode) < 0)
  1090     if (GLES2_SelectProgram(renderer, sourceType, blendMode) < 0)
  1054         return -1;
  1091         return -1;
  1055 
  1092 
  1056     /* Select the target texture */
  1093     /* Select the target texture */
  1057     locTexture = rdata->current_program->uniform_locations[GLES2_UNIFORM_TEXTURE];
  1094     locTexture = rdata->current_program->uniform_locations[GLES2_UNIFORM_TEXTURE];
  1058     glGetError();
  1095     rdata->glGetError();
  1059     glActiveTexture(GL_TEXTURE0);
  1096     rdata->glActiveTexture(GL_TEXTURE0);
  1060     glBindTexture(tdata->texture_type, tdata->texture);
  1097     rdata->glBindTexture(tdata->texture_type, tdata->texture);
  1061     glUniform1i(locTexture, 0);
  1098     rdata->glUniform1i(locTexture, 0);
  1062 
  1099 
  1063     /* Configure color modulation */
  1100     /* Configure color modulation */
  1064     locModulation = rdata->current_program->uniform_locations[GLES2_UNIFORM_MODULATION];
  1101     locModulation = rdata->current_program->uniform_locations[GLES2_UNIFORM_MODULATION];
  1065     glUniform4f(locModulation,
  1102     rdata->glUniform4f(locModulation,
  1066                 texture->r * inv255f,
  1103                 texture->r * inv255f,
  1067                 texture->g * inv255f,
  1104                 texture->g * inv255f,
  1068                 texture->b * inv255f,
  1105                 texture->b * inv255f,
  1069                 texture->a * inv255f);
  1106                 texture->a * inv255f);
  1070 
  1107 
  1080     vertices[3] = (GLfloat)dstrect->y;
  1117     vertices[3] = (GLfloat)dstrect->y;
  1081     vertices[4] = (GLfloat)dstrect->x;
  1118     vertices[4] = (GLfloat)dstrect->x;
  1082     vertices[5] = (GLfloat)(dstrect->y + dstrect->h);
  1119     vertices[5] = (GLfloat)(dstrect->y + dstrect->h);
  1083     vertices[6] = (GLfloat)(dstrect->x + dstrect->w);
  1120     vertices[6] = (GLfloat)(dstrect->x + dstrect->w);
  1084     vertices[7] = (GLfloat)(dstrect->y + dstrect->h);
  1121     vertices[7] = (GLfloat)(dstrect->y + dstrect->h);
  1085     glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  1122     rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  1086     texCoords[0] = srcrect->x / (GLfloat)texture->w;
  1123     texCoords[0] = srcrect->x / (GLfloat)texture->w;
  1087     texCoords[1] = srcrect->y / (GLfloat)texture->h;
  1124     texCoords[1] = srcrect->y / (GLfloat)texture->h;
  1088     texCoords[2] = (srcrect->x + srcrect->w) / (GLfloat)texture->w;
  1125     texCoords[2] = (srcrect->x + srcrect->w) / (GLfloat)texture->w;
  1089     texCoords[3] = srcrect->y / (GLfloat)texture->h;
  1126     texCoords[3] = srcrect->y / (GLfloat)texture->h;
  1090     texCoords[4] = srcrect->x / (GLfloat)texture->w;
  1127     texCoords[4] = srcrect->x / (GLfloat)texture->w;
  1091     texCoords[5] = (srcrect->y + srcrect->h) / (GLfloat)texture->h;
  1128     texCoords[5] = (srcrect->y + srcrect->h) / (GLfloat)texture->h;
  1092     texCoords[6] = (srcrect->x + srcrect->w) / (GLfloat)texture->w;
  1129     texCoords[6] = (srcrect->x + srcrect->w) / (GLfloat)texture->w;
  1093     texCoords[7] = (srcrect->y + srcrect->h) / (GLfloat)texture->h;
  1130     texCoords[7] = (srcrect->y + srcrect->h) / (GLfloat)texture->h;
  1094     glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, texCoords);
  1131     rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, texCoords);
  1095     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  1132     rdata->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  1096     if (glGetError() != GL_NO_ERROR)
  1133     if (rdata->glGetError() != GL_NO_ERROR)
  1097     {
  1134     {
  1098         SDL_SetError("Failed to render texture");
  1135         SDL_SetError("Failed to render texture");
  1099         return -1;
  1136         return -1;
  1100     }
  1137     }
  1101     return 0;
  1138     return 0;
  1103 
  1140 
  1104 static int
  1141 static int
  1105 GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  1142 GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  1106                     Uint32 pixel_format, void * pixels, int pitch)
  1143                     Uint32 pixel_format, void * pixels, int pitch)
  1107 {
  1144 {
       
  1145     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
  1108     SDL_Window *window = renderer->window;
  1146     SDL_Window *window = renderer->window;
  1109     Uint32 temp_format = SDL_PIXELFORMAT_ABGR8888;
  1147     Uint32 temp_format = SDL_PIXELFORMAT_ABGR8888;
  1110     void *temp_pixels;
  1148     void *temp_pixels;
  1111     int temp_pitch;
  1149     int temp_pitch;
  1112     Uint8 *src, *dst, *tmp;
  1150     Uint8 *src, *dst, *tmp;
  1122         return -1;
  1160         return -1;
  1123     }
  1161     }
  1124 
  1162 
  1125     SDL_GetWindowSize(window, &w, &h);
  1163     SDL_GetWindowSize(window, &w, &h);
  1126 
  1164 
  1127     glPixelStorei(GL_PACK_ALIGNMENT, 1);
  1165     rdata->glPixelStorei(GL_PACK_ALIGNMENT, 1);
  1128 
  1166 
  1129     glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
  1167     rdata->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
  1130                        GL_RGBA, GL_UNSIGNED_BYTE, temp_pixels);
  1168                        GL_RGBA, GL_UNSIGNED_BYTE, temp_pixels);
  1131 
  1169 
  1132     /* Flip the rows to be top-down */
  1170     /* Flip the rows to be top-down */
  1133     length = rect->w * SDL_BYTESPERPIXEL(temp_format);
  1171     length = rect->w * SDL_BYTESPERPIXEL(temp_format);
  1134     src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
  1172     src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
  1179     }
  1217     }
  1180 
  1218 
  1181     rdata->current.blendMode = -1;
  1219     rdata->current.blendMode = -1;
  1182     rdata->current.tex_coords = SDL_FALSE;
  1220     rdata->current.tex_coords = SDL_FALSE;
  1183 
  1221 
  1184     glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
  1222     rdata->glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
  1185     glDisableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
  1223     rdata->glDisableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
  1186 }
  1224 }
  1187 
  1225 
  1188 static SDL_Renderer *
  1226 static SDL_Renderer *
  1189 GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
  1227 GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
  1190 {
  1228 {
  1192     GLES2_DriverContext *rdata;
  1230     GLES2_DriverContext *rdata;
  1193     GLint nFormats;
  1231     GLint nFormats;
  1194 #ifndef ZUNE_HD
  1232 #ifndef ZUNE_HD
  1195     GLboolean hasCompiler;
  1233     GLboolean hasCompiler;
  1196 #endif
  1234 #endif
       
  1235     Uint32 windowFlags;
       
  1236     
       
  1237     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
       
  1238     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
       
  1239     
       
  1240     windowFlags = SDL_GetWindowFlags(window);
       
  1241     if (!(windowFlags & SDL_WINDOW_OPENGL)) {
       
  1242         if (SDL_RecreateWindow(window, windowFlags | SDL_WINDOW_OPENGL) < 0) {
       
  1243             /* Uh oh, better try to put it back... */
       
  1244             SDL_RecreateWindow(window, windowFlags);
       
  1245             return NULL;
       
  1246         }
       
  1247     }
  1197 
  1248 
  1198     /* Create the renderer struct */
  1249     /* Create the renderer struct */
  1199     renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(SDL_Renderer));
  1250     renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(SDL_Renderer));
  1200     if (!renderer) {
  1251     if (!renderer) {
  1201         SDL_OutOfMemory();
  1252         SDL_OutOfMemory();
  1212     renderer->info.flags = SDL_RENDERER_ACCELERATED;
  1263     renderer->info.flags = SDL_RENDERER_ACCELERATED;
  1213     renderer->driverdata = rdata;
  1264     renderer->driverdata = rdata;
  1214     renderer->window = window;
  1265     renderer->window = window;
  1215 
  1266 
  1216     /* Create an OpenGL ES 2.0 context */
  1267     /* Create an OpenGL ES 2.0 context */
  1217     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
       
  1218     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
       
  1219 
       
  1220     rdata->context = SDL_GL_CreateContext(window);
  1268     rdata->context = SDL_GL_CreateContext(window);
  1221     if (!rdata->context)
  1269     if (!rdata->context)
  1222     {
  1270     {
  1223         GLES2_DestroyRenderer(renderer);
  1271         GLES2_DestroyRenderer(renderer);
  1224         return NULL;
  1272         return NULL;
  1225     }
  1273     }
  1226     if (SDL_GL_MakeCurrent(window, rdata->context) < 0) {
  1274     if (SDL_GL_MakeCurrent(window, rdata->context) < 0) {
       
  1275         GLES2_DestroyRenderer(renderer);
       
  1276         return NULL;
       
  1277     }
       
  1278 
       
  1279     if (GLES2_LoadFunctions(rdata) < 0) {
  1227         GLES2_DestroyRenderer(renderer);
  1280         GLES2_DestroyRenderer(renderer);
  1228         return NULL;
  1281         return NULL;
  1229     }
  1282     }
  1230 
  1283 
  1231     if (flags & SDL_RENDERER_PRESENTVSYNC) {
  1284     if (flags & SDL_RENDERER_PRESENTVSYNC) {
  1237         renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
  1290         renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
  1238     }
  1291     }
  1239 
  1292 
  1240     /* Determine supported shader formats */
  1293     /* Determine supported shader formats */
  1241     /* HACK: glGetInteger is broken on the Zune HD's compositor, so we just hardcode this */
  1294     /* HACK: glGetInteger is broken on the Zune HD's compositor, so we just hardcode this */
  1242     glGetError();
  1295     rdata->glGetError();
  1243 #ifdef ZUNE_HD
  1296 #ifdef ZUNE_HD
  1244     nFormats = 1;
  1297     nFormats = 1;
  1245 #else /* !ZUNE_HD */
  1298 #else /* !ZUNE_HD */
  1246     glGetIntegerv(GL_NUM_SHADER_BINARY_FORMATS, &nFormats);
  1299     rdata->glGetIntegerv(GL_NUM_SHADER_BINARY_FORMATS, &nFormats);
  1247     glGetBooleanv(GL_SHADER_COMPILER, &hasCompiler);
  1300     rdata->glGetBooleanv(GL_SHADER_COMPILER, &hasCompiler);
  1248     if (hasCompiler)
  1301     if (hasCompiler)
  1249         ++nFormats;
  1302         ++nFormats;
  1250 #endif /* ZUNE_HD */
  1303 #endif /* ZUNE_HD */
  1251     rdata->shader_formats = (GLenum *)SDL_calloc(nFormats, sizeof(GLenum));
  1304     rdata->shader_formats = (GLenum *)SDL_calloc(nFormats, sizeof(GLenum));
  1252     if (!rdata->shader_formats)
  1305     if (!rdata->shader_formats)
  1257     }
  1310     }
  1258     rdata->shader_format_count = nFormats;
  1311     rdata->shader_format_count = nFormats;
  1259 #ifdef ZUNE_HD
  1312 #ifdef ZUNE_HD
  1260     rdata->shader_formats[0] = GL_NVIDIA_PLATFORM_BINARY_NV;
  1313     rdata->shader_formats[0] = GL_NVIDIA_PLATFORM_BINARY_NV;
  1261 #else /* !ZUNE_HD */
  1314 #else /* !ZUNE_HD */
  1262     glGetIntegerv(GL_SHADER_BINARY_FORMATS, (GLint *)rdata->shader_formats);
  1315     rdata->glGetIntegerv(GL_SHADER_BINARY_FORMATS, (GLint *)rdata->shader_formats);
  1263     if (glGetError() != GL_NO_ERROR)
  1316     if (rdata->glGetError() != GL_NO_ERROR)
  1264     {
  1317     {
  1265         GLES2_DestroyRenderer(renderer);
  1318         GLES2_DestroyRenderer(renderer);
  1266         SDL_SetError("Failed to query supported shader formats");
  1319         SDL_SetError("Failed to query supported shader formats");
  1267         return NULL;
  1320         return NULL;
  1268     }
  1321     }