src/render/opengles2/SDL_render_gles2.c
changeset 6232 37e8d0736366
parent 6190 519380462400
child 6237 240f1bced46b
equal deleted inserted replaced
6231:5eecf59b698f 6232:37e8d0736366
    53 
    53 
    54 /*************************************************************************************************
    54 /*************************************************************************************************
    55  * Context structures                                                                            *
    55  * Context structures                                                                            *
    56  *************************************************************************************************/
    56  *************************************************************************************************/
    57 
    57 
       
    58 typedef struct GLES2_FBOList GLES2_FBOList;
       
    59 
       
    60 struct GLES2_FBOList
       
    61 {
       
    62    Uint32 w, h;
       
    63    GLuint FBO;
       
    64    GLES2_FBOList *next;
       
    65 };
       
    66 
    58 typedef struct GLES2_TextureData
    67 typedef struct GLES2_TextureData
    59 {
    68 {
    60     GLenum texture;
    69     GLenum texture;
    61     GLenum texture_type;
    70     GLenum texture_type;
    62     GLenum pixel_format;
    71     GLenum pixel_format;
    63     GLenum pixel_type;
    72     GLenum pixel_type;
    64     void *pixel_data;
    73     void *pixel_data;
    65     size_t pitch;
    74     size_t pitch;
       
    75     GLES2_FBOList *fbo;
    66 } GLES2_TextureData;
    76 } GLES2_TextureData;
    67 
    77 
    68 typedef struct GLES2_ShaderCacheEntry
    78 typedef struct GLES2_ShaderCacheEntry
    69 {
    79 {
    70     GLuint id;
    80     GLuint id;
   132     } current;
   142     } current;
   133 
   143 
   134 #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
   144 #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
   135 #include "SDL_gles2funcs.h"
   145 #include "SDL_gles2funcs.h"
   136 #undef SDL_PROC
   146 #undef SDL_PROC
       
   147     GLES2_FBOList *framebuffers;
       
   148     SDL_Texture *renderTarget;
       
   149     SDL_Rect viewport_copy;
   137 
   150 
   138     int shader_format_count;
   151     int shader_format_count;
   139     GLenum *shader_formats;
   152     GLenum *shader_formats;
   140     GLES2_ShaderCache shader_cache;
   153     GLES2_ShaderCache shader_cache;
   141     GLES2_ProgramCache program_cache;
   154     GLES2_ProgramCache program_cache;
   151 static int GLES2_ActivateRenderer(SDL_Renderer *renderer);
   164 static int GLES2_ActivateRenderer(SDL_Renderer *renderer);
   152 static void GLES2_WindowEvent(SDL_Renderer * renderer,
   165 static void GLES2_WindowEvent(SDL_Renderer * renderer,
   153                               const SDL_WindowEvent *event);
   166                               const SDL_WindowEvent *event);
   154 static int GLES2_UpdateViewport(SDL_Renderer * renderer);
   167 static int GLES2_UpdateViewport(SDL_Renderer * renderer);
   155 static void GLES2_DestroyRenderer(SDL_Renderer *renderer);
   168 static void GLES2_DestroyRenderer(SDL_Renderer *renderer);
       
   169 
       
   170 static int GLES2_SetTargetTexture(SDL_Renderer * renderer, SDL_Texture * texture);
   156 
   171 
   157 static SDL_GLContext SDL_CurrentContext = NULL;
   172 static SDL_GLContext SDL_CurrentContext = NULL;
   158 
   173 
   159 static int GLES2_LoadFunctions(GLES2_DriverContext * data)
   174 static int GLES2_LoadFunctions(GLES2_DriverContext * data)
   160 {
   175 {
   174         data->func = SDL_GL_GetProcAddress(#func); \
   189         data->func = SDL_GL_GetProcAddress(#func); \
   175         if ( ! data->func ) { \
   190         if ( ! data->func ) { \
   176             SDL_SetError("Couldn't load GLES2 function %s: %s\n", #func, SDL_GetError()); \
   191             SDL_SetError("Couldn't load GLES2 function %s: %s\n", #func, SDL_GetError()); \
   177             return -1; \
   192             return -1; \
   178         } \
   193         } \
   179     } while ( 0 );  
   194     } while ( 0 );
   180 #endif /* _SDL_NOGETPROCADDR_ */
   195 #endif /* _SDL_NOGETPROCADDR_ */
   181 
   196 
   182 #include "SDL_gles2funcs.h"
   197 #include "SDL_gles2funcs.h"
   183 #undef SDL_PROC
   198 #undef SDL_PROC
   184     return 0;
   199     return 0;
   185 }
   200 }
   186 
   201 
       
   202 GLES2_FBOList *
       
   203 GLES2_GetFBO(GLES2_DriverContext *data, Uint32 w, Uint32 h)
       
   204 {
       
   205    GLES2_FBOList *result = data->framebuffers;
       
   206    while ((result) && ((result->w != w) || (result->h != h)) )
       
   207    {
       
   208        result = result->next;
       
   209    }
       
   210    if (result == NULL)
       
   211    {
       
   212        result = SDL_malloc(sizeof(GLES2_FBOList));
       
   213        result->w = w;
       
   214        result->h = h;
       
   215        glGenFramebuffers(1, &result->FBO);
       
   216        result->next = data->framebuffers;
       
   217        data->framebuffers = result;
       
   218    }
       
   219    return result;
       
   220 }
       
   221 
   187 static int
   222 static int
   188 GLES2_ActivateRenderer(SDL_Renderer * renderer)
   223 GLES2_ActivateRenderer(SDL_Renderer * renderer)
   189 {
   224 {
   190     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
   225     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
   191 
   226 
   205 
   240 
   206 static void
   241 static void
   207 GLES2_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
   242 GLES2_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
   208 {
   243 {
   209     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
   244     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
   210     
   245 
   211     if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
   246     if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
   212         /* Rebind the context to the window area */
   247         /* Rebind the context to the window area */
   213         SDL_CurrentContext = NULL;
   248         SDL_CurrentContext = NULL;
   214     }
   249     }
   215 
   250 
   265                 SDL_free(entry);
   300                 SDL_free(entry);
   266                 entry = next;
   301                 entry = next;
   267             }
   302             }
   268         }
   303         }
   269         if (rdata->context) {
   304         if (rdata->context) {
       
   305             while (rdata->framebuffers) {
       
   306                 GLES2_FBOList *nextnode = rdata->framebuffers->next;
       
   307                 rdata->glDeleteFramebuffers(1, &rdata->framebuffers->FBO);
       
   308                 SDL_free(rdata->framebuffers);
       
   309                 rdata->framebuffers = nextnode;
       
   310             }
   270             SDL_GL_DeleteContext(rdata->context);
   311             SDL_GL_DeleteContext(rdata->context);
   271         }
   312         }
   272         if (rdata->shader_formats) {
   313         if (rdata->shader_formats) {
   273             SDL_free(rdata->shader_formats);
   314             SDL_free(rdata->shader_formats);
   274         }
   315         }
   369         rdata->glDeleteTextures(1, &tdata->texture);
   410         rdata->glDeleteTextures(1, &tdata->texture);
   370         SDL_free(tdata);
   411         SDL_free(tdata);
   371         return -1;
   412         return -1;
   372     }
   413     }
   373     texture->driverdata = tdata;
   414     texture->driverdata = tdata;
       
   415 
       
   416     if (texture->access == SDL_TEXTUREACCESS_TARGET) {
       
   417        tdata->fbo = GLES2_GetFBO(renderer->driverdata, texture->w, texture->h);
       
   418     } else {
       
   419        tdata->fbo = NULL;
       
   420     }
       
   421 
   374     return 0;
   422     return 0;
   375 }
   423 }
   376 
   424 
   377 static void
   425 static void
   378 GLES2_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture)
   426 GLES2_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture)
   431     Uint8 *src;
   479     Uint8 *src;
   432     int srcPitch;
   480     int srcPitch;
   433     int y;
   481     int y;
   434 
   482 
   435     GLES2_ActivateRenderer(renderer);
   483     GLES2_ActivateRenderer(renderer);
   436     
   484 
   437     /* Bail out if we're supposed to update an empty rectangle */
   485     /* Bail out if we're supposed to update an empty rectangle */
   438     if (rect->w <= 0 || rect->h <= 0)
   486     if (rect->w <= 0 || rect->h <= 0)
   439         return 0;
   487         return 0;
   440 
   488 
   441     /* Reformat the texture data into a tightly packed array */
   489     /* Reformat the texture data into a tightly packed array */
   541         return NULL;
   589         return NULL;
   542     }
   590     }
   543     entry->vertex_shader = vertex;
   591     entry->vertex_shader = vertex;
   544     entry->fragment_shader = fragment;
   592     entry->fragment_shader = fragment;
   545     entry->blend_mode = blendMode;
   593     entry->blend_mode = blendMode;
   546     
   594 
   547     /* Create the program and link it */
   595     /* Create the program and link it */
   548     rdata->glGetError();
   596     rdata->glGetError();
   549     entry->id = rdata->glCreateProgram();
   597     entry->id = rdata->glCreateProgram();
   550     rdata->glAttachShader(entry->id, vertex->id);
   598     rdata->glAttachShader(entry->id, vertex->id);
   551     rdata->glAttachShader(entry->id, fragment->id);
   599     rdata->glAttachShader(entry->id, fragment->id);
   558         SDL_SetError("Failed to link shader program");
   606         SDL_SetError("Failed to link shader program");
   559         rdata->glDeleteProgram(entry->id);
   607         rdata->glDeleteProgram(entry->id);
   560         SDL_free(entry);
   608         SDL_free(entry);
   561         return NULL;
   609         return NULL;
   562     }
   610     }
   563     
   611 
   564     /* Predetermine locations of uniform variables */
   612     /* Predetermine locations of uniform variables */
   565     entry->uniform_locations[GLES2_UNIFORM_PROJECTION] =
   613     entry->uniform_locations[GLES2_UNIFORM_PROJECTION] =
   566         rdata->glGetUniformLocation(entry->id, "u_projection");
   614         rdata->glGetUniformLocation(entry->id, "u_projection");
   567     entry->uniform_locations[GLES2_UNIFORM_TEXTURE] =
   615     entry->uniform_locations[GLES2_UNIFORM_TEXTURE] =
   568         rdata->glGetUniformLocation(entry->id, "u_texture");
   616         rdata->glGetUniformLocation(entry->id, "u_texture");
   623     if (!shader)
   671     if (!shader)
   624     {
   672     {
   625         SDL_SetError("No shader matching the requested characteristics was found");
   673         SDL_SetError("No shader matching the requested characteristics was found");
   626         return NULL;
   674         return NULL;
   627     }
   675     }
   628     
   676 
   629     /* Find a matching shader instance that's supported on this hardware */
   677     /* Find a matching shader instance that's supported on this hardware */
   630     for (i = 0; i < shader->instance_count && !instance; ++i)
   678     for (i = 0; i < shader->instance_count && !instance; ++i)
   631     {
   679     {
   632         for (j = 0; j < rdata->shader_format_count && !instance; ++j)
   680         for (j = 0; j < rdata->shader_format_count && !instance; ++j)
   633         {
   681         {
   870 GLES2_RenderClear(SDL_Renderer * renderer)
   918 GLES2_RenderClear(SDL_Renderer * renderer)
   871 {
   919 {
   872     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
   920     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
   873 
   921 
   874     GLES2_ActivateRenderer(renderer);
   922     GLES2_ActivateRenderer(renderer);
   875     
   923 
   876     rdata->glClearColor((GLfloat) renderer->r * inv255f,
   924     rdata->glClearColor((GLfloat) renderer->r * inv255f,
   877                  (GLfloat) renderer->g * inv255f,
   925                  (GLfloat) renderer->g * inv255f,
   878                  (GLfloat) renderer->b * inv255f,
   926                  (GLfloat) renderer->b * inv255f,
   879                  (GLfloat) renderer->a * inv255f);
   927                  (GLfloat) renderer->a * inv255f);
   880 
   928 
  1078 
  1126 
  1079     GLES2_ActivateRenderer(renderer);
  1127     GLES2_ActivateRenderer(renderer);
  1080 
  1128 
  1081     /* Activate an appropriate shader and set the projection matrix */
  1129     /* Activate an appropriate shader and set the projection matrix */
  1082     blendMode = texture->blendMode;
  1130     blendMode = texture->blendMode;
  1083     switch (texture->format)
  1131     if (rdata->renderTarget!=NULL) {
  1084     {
  1132         /* Check if we need to do color mapping between the source and render target textures */
  1085         case SDL_PIXELFORMAT_ABGR8888:
  1133         if (rdata->renderTarget->format != texture->format) {
  1086             sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
  1134             switch (texture->format)
  1087             break;
  1135             {
  1088         case SDL_PIXELFORMAT_ARGB8888:
  1136             case SDL_PIXELFORMAT_ABGR8888:
  1089             sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
  1137                 switch (rdata->renderTarget->format)
  1090             break;
  1138                 {
  1091         case SDL_PIXELFORMAT_BGR888:
  1139                     case SDL_PIXELFORMAT_ARGB8888:
  1092             sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
  1140                     case SDL_PIXELFORMAT_RGB888:
  1093             break;
  1141                         sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
  1094         case SDL_PIXELFORMAT_RGB888:
  1142                         break;
  1095             sourceType = GLES2_IMAGESOURCE_TEXTURE_RGB;
  1143                     case SDL_PIXELFORMAT_BGR888:
  1096             break;
  1144                         sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
       
  1145                         break;
       
  1146                 }
       
  1147                 break;
       
  1148             case SDL_PIXELFORMAT_ARGB8888:
       
  1149                 switch (rdata->renderTarget->format)
       
  1150                 {
       
  1151                     case SDL_PIXELFORMAT_ABGR8888:
       
  1152                     case SDL_PIXELFORMAT_BGR888:
       
  1153                         sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
       
  1154                         break;
       
  1155                     case SDL_PIXELFORMAT_RGB888:
       
  1156                         sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
       
  1157                         break;
       
  1158                 }
       
  1159                 break;
       
  1160             case SDL_PIXELFORMAT_BGR888:
       
  1161                 switch (rdata->renderTarget->format)
       
  1162                 {
       
  1163                     case SDL_PIXELFORMAT_ABGR8888:
       
  1164                         sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
       
  1165                         break;
       
  1166                     case SDL_PIXELFORMAT_ARGB8888:
       
  1167                         sourceType = GLES2_IMAGESOURCE_TEXTURE_RGB;
       
  1168                         break;
       
  1169                     case SDL_PIXELFORMAT_RGB888:
       
  1170                         sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
       
  1171                         break;
       
  1172                 }
       
  1173                 break;
       
  1174             case SDL_PIXELFORMAT_RGB888:
       
  1175                 switch (rdata->renderTarget->format)
       
  1176                 {
       
  1177                     case SDL_PIXELFORMAT_ABGR8888:
       
  1178                         sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
       
  1179                         break;
       
  1180                     case SDL_PIXELFORMAT_ARGB8888:
       
  1181                         sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
       
  1182                         break;
       
  1183                     case SDL_PIXELFORMAT_BGR888:
       
  1184                         sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
       
  1185                         break;
       
  1186                 }
       
  1187                 break;
       
  1188             }
       
  1189         }
       
  1190         else sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;   // Texture formats match, use the non color mapping shader (even if the formats are not ABGR)
       
  1191     }
       
  1192     else {
       
  1193         switch (texture->format)
       
  1194         {
       
  1195             case SDL_PIXELFORMAT_ABGR8888:
       
  1196                 sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
       
  1197                 break;
       
  1198             case SDL_PIXELFORMAT_ARGB8888:
       
  1199                 sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
       
  1200                 break;
       
  1201             case SDL_PIXELFORMAT_BGR888:
       
  1202                 sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
       
  1203                 break;
       
  1204             case SDL_PIXELFORMAT_RGB888:
       
  1205                 sourceType = GLES2_IMAGESOURCE_TEXTURE_RGB;
       
  1206                 break;
       
  1207         }
  1097     }
  1208     }
  1098     if (GLES2_SelectProgram(renderer, sourceType, blendMode) < 0)
  1209     if (GLES2_SelectProgram(renderer, sourceType, blendMode) < 0)
  1099         return -1;
  1210         return -1;
  1100 
  1211 
  1101     /* Select the target texture */
  1212     /* Select the target texture */
  1117     GLES2_SetBlendMode(rdata, blendMode);
  1228     GLES2_SetBlendMode(rdata, blendMode);
  1118 
  1229 
  1119     GLES2_SetTexCoords(rdata, SDL_TRUE);
  1230     GLES2_SetTexCoords(rdata, SDL_TRUE);
  1120 
  1231 
  1121     /* Emit the textured quad */
  1232     /* Emit the textured quad */
  1122     vertices[0] = (GLfloat)dstrect->x;
  1233     if (rdata->renderTarget!=NULL) {
  1123     vertices[1] = (GLfloat)dstrect->y;
  1234         // Flip the texture vertically to compensate for the inversion it'll be subjected to later when it's rendered to the screen
  1124     vertices[2] = (GLfloat)(dstrect->x + dstrect->w);
  1235         vertices[0] = (GLfloat)dstrect->x;
  1125     vertices[3] = (GLfloat)dstrect->y;
  1236         vertices[1] = (GLfloat)renderer->viewport.h-dstrect->y;
  1126     vertices[4] = (GLfloat)dstrect->x;
  1237         vertices[2] = (GLfloat)(dstrect->x + dstrect->w);
  1127     vertices[5] = (GLfloat)(dstrect->y + dstrect->h);
  1238         vertices[3] = (GLfloat)renderer->viewport.h-dstrect->y;
  1128     vertices[6] = (GLfloat)(dstrect->x + dstrect->w);
  1239         vertices[4] = (GLfloat)dstrect->x;
  1129     vertices[7] = (GLfloat)(dstrect->y + dstrect->h);
  1240         vertices[5] = (GLfloat)renderer->viewport.h-(dstrect->y + dstrect->h);
       
  1241         vertices[6] = (GLfloat)(dstrect->x + dstrect->w);
       
  1242         vertices[7] = (GLfloat)renderer->viewport.h-(dstrect->y + dstrect->h);
       
  1243     }
       
  1244     else {
       
  1245         vertices[0] = (GLfloat)dstrect->x;
       
  1246         vertices[1] = (GLfloat)dstrect->y;
       
  1247         vertices[2] = (GLfloat)(dstrect->x + dstrect->w);
       
  1248         vertices[3] = (GLfloat)dstrect->y;
       
  1249         vertices[4] = (GLfloat)dstrect->x;
       
  1250         vertices[5] = (GLfloat)(dstrect->y + dstrect->h);
       
  1251         vertices[6] = (GLfloat)(dstrect->x + dstrect->w);
       
  1252         vertices[7] = (GLfloat)(dstrect->y + dstrect->h);
       
  1253     }
  1130     rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  1254     rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
       
  1255 
  1131     texCoords[0] = srcrect->x / (GLfloat)texture->w;
  1256     texCoords[0] = srcrect->x / (GLfloat)texture->w;
  1132     texCoords[1] = srcrect->y / (GLfloat)texture->h;
  1257     texCoords[1] = srcrect->y / (GLfloat)texture->h;
  1133     texCoords[2] = (srcrect->x + srcrect->w) / (GLfloat)texture->w;
  1258     texCoords[2] = (srcrect->x + srcrect->w) / (GLfloat)texture->w;
  1134     texCoords[3] = srcrect->y / (GLfloat)texture->h;
  1259     texCoords[3] = srcrect->y / (GLfloat)texture->h;
  1135     texCoords[4] = srcrect->x / (GLfloat)texture->w;
  1260     texCoords[4] = srcrect->x / (GLfloat)texture->w;
  1229 
  1354 
  1230     rdata->glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
  1355     rdata->glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
  1231     rdata->glDisableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
  1356     rdata->glDisableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
  1232 }
  1357 }
  1233 
  1358 
       
  1359 static int
       
  1360 GLES2_SetTargetTexture(SDL_Renderer * renderer, SDL_Texture * texture)
       
  1361 {
       
  1362     GLES2_DriverContext *data = (GLES2_DriverContext *) renderer->driverdata;
       
  1363     GLES2_TextureData *texturedata = NULL;
       
  1364     GLenum status;
       
  1365     SDL_BlendMode blendMode;
       
  1366 
       
  1367     if (!renderer) return -1;
       
  1368 
       
  1369     blendMode = texture->blendMode;
       
  1370     if (texture == NULL) {
       
  1371         if (data->renderTarget!=NULL) {
       
  1372             data->glBindFramebuffer(GL_FRAMEBUFFER, 0);
       
  1373             renderer->viewport = data->viewport_copy;
       
  1374             data->renderTarget = NULL;
       
  1375             data->glViewport(renderer->viewport.x, renderer->viewport.y, renderer->viewport.w, renderer->viewport.h);
       
  1376             if(data->current_program) GLES2_SetOrthographicProjection(renderer);
       
  1377         }
       
  1378         return 0;
       
  1379     }
       
  1380     if (renderer != texture->renderer) return -1;
       
  1381     if (data->renderTarget==NULL) {
       
  1382         // Keep a copy of the default viewport to restore when texture==NULL
       
  1383         data->viewport_copy = renderer->viewport;
       
  1384     }
       
  1385 
       
  1386     texturedata = (GLES2_TextureData *) texture->driverdata;
       
  1387     if (!texturedata) {
       
  1388         if (texture->native && texture->native->driverdata) {
       
  1389             texture = texture->native;
       
  1390             texturedata = texture->driverdata;
       
  1391         }
       
  1392         else return -1;
       
  1393     }
       
  1394     data->glBindFramebuffer(GL_FRAMEBUFFER, texturedata->fbo->FBO);
       
  1395     /* TODO: check if texture pixel format allows this operation */
       
  1396     data->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texturedata->texture_type, texturedata->texture, 0);
       
  1397     /* Check FBO status */
       
  1398     status = data->glCheckFramebufferStatus(GL_FRAMEBUFFER);
       
  1399     if (status != GL_FRAMEBUFFER_COMPLETE) {
       
  1400         return -1;
       
  1401     }
       
  1402     
       
  1403     renderer->viewport.x = 0;
       
  1404     renderer->viewport.y = 0;
       
  1405     renderer->viewport.w = texture->w;
       
  1406     renderer->viewport.h = texture->h;
       
  1407     data->renderTarget = texture;
       
  1408     data->glViewport(0, 0, texture->w, texture->h);
       
  1409     if(data->current_program) GLES2_SetOrthographicProjection(renderer);
       
  1410     return 0;
       
  1411 }
       
  1412 
  1234 static SDL_Renderer *
  1413 static SDL_Renderer *
  1235 GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
  1414 GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
  1236 {
  1415 {
  1237     SDL_Renderer *renderer;
  1416     SDL_Renderer *renderer;
  1238     GLES2_DriverContext *rdata;
  1417     GLES2_DriverContext *rdata;
  1239     GLint nFormats;
  1418     GLint nFormats;
  1240 #ifndef ZUNE_HD
  1419 #ifndef ZUNE_HD
  1241     GLboolean hasCompiler;
  1420     GLboolean hasCompiler;
  1242 #endif
  1421 #endif
  1243     Uint32 windowFlags;
  1422     Uint32 windowFlags;
  1244     
  1423 
  1245     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  1424     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  1246     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
  1425     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
  1247     
  1426 
  1248     windowFlags = SDL_GetWindowFlags(window);
  1427     windowFlags = SDL_GetWindowFlags(window);
  1249     if (!(windowFlags & SDL_WINDOW_OPENGL)) {
  1428     if (!(windowFlags & SDL_WINDOW_OPENGL)) {
  1250         if (SDL_RecreateWindow(window, windowFlags | SDL_WINDOW_OPENGL) < 0) {
  1429         if (SDL_RecreateWindow(window, windowFlags | SDL_WINDOW_OPENGL) < 0) {
  1251             /* Uh oh, better try to put it back... */
  1430             /* Uh oh, better try to put it back... */
  1252             SDL_RecreateWindow(window, windowFlags);
  1431             SDL_RecreateWindow(window, windowFlags);
  1328         return NULL;
  1507         return NULL;
  1329     }
  1508     }
  1330     if (hasCompiler)
  1509     if (hasCompiler)
  1331         rdata->shader_formats[nFormats - 1] = (GLenum)-1;
  1510         rdata->shader_formats[nFormats - 1] = (GLenum)-1;
  1332 #endif /* ZUNE_HD */
  1511 #endif /* ZUNE_HD */
       
  1512 
       
  1513     rdata->framebuffers = NULL;
       
  1514     rdata->renderTarget = NULL;
  1333 
  1515 
  1334     /* Populate the function pointers for the module */
  1516     /* Populate the function pointers for the module */
  1335     renderer->WindowEvent         = &GLES2_WindowEvent;
  1517     renderer->WindowEvent         = &GLES2_WindowEvent;
  1336     renderer->CreateTexture       = &GLES2_CreateTexture;
  1518     renderer->CreateTexture       = &GLES2_CreateTexture;
  1337     renderer->UpdateTexture       = &GLES2_UpdateTexture;
  1519     renderer->UpdateTexture       = &GLES2_UpdateTexture;
  1345     renderer->RenderCopy          = &GLES2_RenderCopy;
  1527     renderer->RenderCopy          = &GLES2_RenderCopy;
  1346     renderer->RenderReadPixels    = &GLES2_RenderReadPixels;
  1528     renderer->RenderReadPixels    = &GLES2_RenderReadPixels;
  1347     renderer->RenderPresent       = &GLES2_RenderPresent;
  1529     renderer->RenderPresent       = &GLES2_RenderPresent;
  1348     renderer->DestroyTexture      = &GLES2_DestroyTexture;
  1530     renderer->DestroyTexture      = &GLES2_DestroyTexture;
  1349     renderer->DestroyRenderer     = &GLES2_DestroyRenderer;
  1531     renderer->DestroyRenderer     = &GLES2_DestroyRenderer;
       
  1532     renderer->SetTargetTexture    = &GLES2_SetTargetTexture;
  1350 
  1533 
  1351     GLES2_ResetState(renderer);
  1534     GLES2_ResetState(renderer);
  1352 
  1535 
  1353     return renderer;
  1536     return renderer;
  1354 }
  1537 }