src/render/opengles2/SDL_render_gles2.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 18 Jan 2012 22:45:49 -0500
changeset 6232 37e8d0736366
parent 6190 519380462400
child 6237 240f1bced46b
permissions -rwxr-xr-x
Implementation of render targets, by Mason Wheeler and Gabriel Jacobo

Thanks guys!
slouken@5201
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@5201
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@5201
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@5201
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@5201
    20
*/
slouken@5201
    21
#include "SDL_config.h"
slouken@5201
    22
slouken@5226
    23
#if SDL_VIDEO_RENDER_OGL_ES2 && !SDL_RENDER_DISABLED
slouken@5201
    24
slouken@5484
    25
#include "SDL_hints.h"
slouken@5204
    26
#include "SDL_opengles2.h"
slouken@5201
    27
#include "../SDL_sysrender.h"
slouken@5201
    28
#include "SDL_shaders_gles2.h"
slouken@5201
    29
slouken@6190
    30
/* Used to re-create the window with OpenGL ES capability */
slouken@6188
    31
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
slouken@6188
    32
slouken@5201
    33
/*************************************************************************************************
slouken@5201
    34
 * Bootstrap data                                                                                *
slouken@5201
    35
 *************************************************************************************************/
slouken@5201
    36
slouken@5201
    37
static SDL_Renderer *GLES2_CreateRenderer(SDL_Window *window, Uint32 flags);
slouken@5201
    38
slouken@5201
    39
SDL_RenderDriver GLES2_RenderDriver = {
slouken@5201
    40
    GLES2_CreateRenderer,
slouken@5201
    41
    {
slouken@5201
    42
        "opengles2",
slouken@5201
    43
        (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
slouken@6113
    44
        4,
slouken@6113
    45
        {SDL_PIXELFORMAT_ABGR8888,
slouken@6113
    46
        SDL_PIXELFORMAT_ARGB8888,
slouken@6113
    47
        SDL_PIXELFORMAT_RGB888,
slouken@6113
    48
        SDL_PIXELFORMAT_BGR888},
slouken@5201
    49
        0,
slouken@5201
    50
        0
slouken@5201
    51
    }
slouken@5201
    52
};
slouken@5201
    53
slouken@5201
    54
/*************************************************************************************************
slouken@5201
    55
 * Context structures                                                                            *
slouken@5201
    56
 *************************************************************************************************/
slouken@5201
    57
slouken@6232
    58
typedef struct GLES2_FBOList GLES2_FBOList;
slouken@6232
    59
slouken@6232
    60
struct GLES2_FBOList
slouken@6232
    61
{
slouken@6232
    62
   Uint32 w, h;
slouken@6232
    63
   GLuint FBO;
slouken@6232
    64
   GLES2_FBOList *next;
slouken@6232
    65
};
slouken@6232
    66
slouken@5201
    67
typedef struct GLES2_TextureData
slouken@5201
    68
{
slouken@5201
    69
    GLenum texture;
slouken@5201
    70
    GLenum texture_type;
slouken@5201
    71
    GLenum pixel_format;
slouken@5201
    72
    GLenum pixel_type;
slouken@5201
    73
    void *pixel_data;
slouken@5201
    74
    size_t pitch;
slouken@6232
    75
    GLES2_FBOList *fbo;
slouken@5201
    76
} GLES2_TextureData;
slouken@5201
    77
slouken@5201
    78
typedef struct GLES2_ShaderCacheEntry
slouken@5201
    79
{
slouken@5201
    80
    GLuint id;
slouken@5201
    81
    GLES2_ShaderType type;
slouken@5201
    82
    const GLES2_ShaderInstance *instance;
slouken@5201
    83
    int references;
slouken@5201
    84
    struct GLES2_ShaderCacheEntry *prev;
slouken@5201
    85
    struct GLES2_ShaderCacheEntry *next;
slouken@5201
    86
} GLES2_ShaderCacheEntry;
slouken@5201
    87
slouken@5201
    88
typedef struct GLES2_ShaderCache
slouken@5201
    89
{
slouken@5201
    90
    int count;
slouken@5201
    91
    GLES2_ShaderCacheEntry *head;
slouken@5201
    92
} GLES2_ShaderCache;
slouken@5201
    93
slouken@5201
    94
typedef struct GLES2_ProgramCacheEntry
slouken@5201
    95
{
slouken@5201
    96
    GLuint id;
slouken@5201
    97
    SDL_BlendMode blend_mode;
slouken@5201
    98
    GLES2_ShaderCacheEntry *vertex_shader;
slouken@5201
    99
    GLES2_ShaderCacheEntry *fragment_shader;
slouken@5201
   100
    GLuint uniform_locations[16];
slouken@5201
   101
    struct GLES2_ProgramCacheEntry *prev;
slouken@5201
   102
    struct GLES2_ProgramCacheEntry *next;
slouken@5201
   103
} GLES2_ProgramCacheEntry;
slouken@5201
   104
slouken@5201
   105
typedef struct GLES2_ProgramCache
slouken@5201
   106
{
slouken@5201
   107
    int count;
slouken@5201
   108
    GLES2_ProgramCacheEntry *head;
slouken@5201
   109
    GLES2_ProgramCacheEntry *tail;
slouken@5201
   110
} GLES2_ProgramCache;
slouken@5201
   111
slouken@5201
   112
typedef enum
slouken@5201
   113
{
slouken@5201
   114
    GLES2_ATTRIBUTE_POSITION = 0,
slouken@5201
   115
    GLES2_ATTRIBUTE_TEXCOORD = 1
slouken@5201
   116
} GLES2_Attribute;
slouken@5201
   117
slouken@5201
   118
typedef enum
slouken@5201
   119
{
slouken@5201
   120
    GLES2_UNIFORM_PROJECTION,
slouken@5201
   121
    GLES2_UNIFORM_TEXTURE,
slouken@5201
   122
    GLES2_UNIFORM_MODULATION,
slouken@5201
   123
    GLES2_UNIFORM_COLOR,
slouken@5201
   124
    GLES2_UNIFORM_COLORTABLE
slouken@5201
   125
} GLES2_Uniform;
slouken@5201
   126
slouken@5201
   127
typedef enum
slouken@5201
   128
{
slouken@5201
   129
    GLES2_IMAGESOURCE_SOLID,
slouken@6113
   130
    GLES2_IMAGESOURCE_TEXTURE_ABGR,
slouken@6113
   131
    GLES2_IMAGESOURCE_TEXTURE_ARGB,
slouken@6113
   132
    GLES2_IMAGESOURCE_TEXTURE_RGB,
slouken@6113
   133
    GLES2_IMAGESOURCE_TEXTURE_BGR
slouken@5201
   134
} GLES2_ImageSource;
slouken@5201
   135
slouken@5201
   136
typedef struct GLES2_DriverContext
slouken@5201
   137
{
slouken@5201
   138
    SDL_GLContext *context;
slouken@5355
   139
    struct {
slouken@5355
   140
        int blendMode;
slouken@5355
   141
        SDL_bool tex_coords;
slouken@5355
   142
    } current;
slouken@5355
   143
slouken@6188
   144
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
slouken@6188
   145
#include "SDL_gles2funcs.h"
slouken@6188
   146
#undef SDL_PROC
slouken@6232
   147
    GLES2_FBOList *framebuffers;
slouken@6232
   148
    SDL_Texture *renderTarget;
slouken@6232
   149
    SDL_Rect viewport_copy;
slouken@6188
   150
slouken@5201
   151
    int shader_format_count;
slouken@5201
   152
    GLenum *shader_formats;
slouken@5201
   153
    GLES2_ShaderCache shader_cache;
slouken@5201
   154
    GLES2_ProgramCache program_cache;
slouken@5201
   155
    GLES2_ProgramCacheEntry *current_program;
slouken@5201
   156
} GLES2_DriverContext;
slouken@5201
   157
slouken@5201
   158
#define GLES2_MAX_CACHED_PROGRAMS 8
slouken@5201
   159
slouken@5201
   160
/*************************************************************************************************
slouken@5201
   161
 * Renderer state APIs                                                                           *
slouken@5201
   162
 *************************************************************************************************/
slouken@5201
   163
slouken@5224
   164
static int GLES2_ActivateRenderer(SDL_Renderer *renderer);
slouken@5201
   165
static void GLES2_WindowEvent(SDL_Renderer * renderer,
slouken@5201
   166
                              const SDL_WindowEvent *event);
slouken@5297
   167
static int GLES2_UpdateViewport(SDL_Renderer * renderer);
slouken@5201
   168
static void GLES2_DestroyRenderer(SDL_Renderer *renderer);
slouken@5201
   169
slouken@6232
   170
static int GLES2_SetTargetTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@6232
   171
slouken@5201
   172
static SDL_GLContext SDL_CurrentContext = NULL;
slouken@5201
   173
slouken@6188
   174
static int GLES2_LoadFunctions(GLES2_DriverContext * data)
slouken@6188
   175
{
slouken@6190
   176
#if SDL_VIDEO_DRIVER_UIKIT
slouken@6190
   177
#define __SDL_NOGETPROCADDR__
slouken@6190
   178
#elif SDL_VIDEO_DRIVER_ANDROID
slouken@6190
   179
#define __SDL_NOGETPROCADDR__
slouken@6190
   180
#elif SDL_VIDEO_DRIVER_PANDORA
slouken@6190
   181
#define __SDL_NOGETPROCADDR__
slouken@6190
   182
#endif
slouken@6190
   183
slouken@6190
   184
#if defined __SDL_NOGETPROCADDR__
slouken@6188
   185
#define SDL_PROC(ret,func,params) data->func=func;
slouken@6188
   186
#else
slouken@6188
   187
#define SDL_PROC(ret,func,params) \
slouken@6188
   188
    do { \
slouken@6188
   189
        data->func = SDL_GL_GetProcAddress(#func); \
slouken@6188
   190
        if ( ! data->func ) { \
slouken@6188
   191
            SDL_SetError("Couldn't load GLES2 function %s: %s\n", #func, SDL_GetError()); \
slouken@6188
   192
            return -1; \
slouken@6188
   193
        } \
slouken@6232
   194
    } while ( 0 );
slouken@6188
   195
#endif /* _SDL_NOGETPROCADDR_ */
slouken@6188
   196
slouken@6188
   197
#include "SDL_gles2funcs.h"
slouken@6188
   198
#undef SDL_PROC
slouken@6188
   199
    return 0;
slouken@6188
   200
}
slouken@6188
   201
slouken@6232
   202
GLES2_FBOList *
slouken@6232
   203
GLES2_GetFBO(GLES2_DriverContext *data, Uint32 w, Uint32 h)
slouken@6232
   204
{
slouken@6232
   205
   GLES2_FBOList *result = data->framebuffers;
slouken@6232
   206
   while ((result) && ((result->w != w) || (result->h != h)) )
slouken@6232
   207
   {
slouken@6232
   208
       result = result->next;
slouken@6232
   209
   }
slouken@6232
   210
   if (result == NULL)
slouken@6232
   211
   {
slouken@6232
   212
       result = SDL_malloc(sizeof(GLES2_FBOList));
slouken@6232
   213
       result->w = w;
slouken@6232
   214
       result->h = h;
slouken@6232
   215
       glGenFramebuffers(1, &result->FBO);
slouken@6232
   216
       result->next = data->framebuffers;
slouken@6232
   217
       data->framebuffers = result;
slouken@6232
   218
   }
slouken@6232
   219
   return result;
slouken@6232
   220
}
slouken@6232
   221
slouken@5201
   222
static int
slouken@5201
   223
GLES2_ActivateRenderer(SDL_Renderer * renderer)
slouken@5201
   224
{
slouken@5201
   225
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   226
slouken@5201
   227
    if (SDL_CurrentContext != rdata->context) {
slouken@5201
   228
        /* Null out the current program to ensure we set it again */
slouken@5201
   229
        rdata->current_program = NULL;
slouken@5201
   230
slouken@5297
   231
        if (SDL_GL_MakeCurrent(renderer->window, rdata->context) < 0) {
slouken@5201
   232
            return -1;
slouken@5201
   233
        }
slouken@5201
   234
        SDL_CurrentContext = rdata->context;
slouken@5201
   235
slouken@5297
   236
        GLES2_UpdateViewport(renderer);
slouken@5201
   237
    }
slouken@5201
   238
    return 0;
slouken@5201
   239
}
slouken@5201
   240
slouken@5201
   241
static void
slouken@5201
   242
GLES2_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@5201
   243
{
slouken@6188
   244
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@6232
   245
slouken@5276
   246
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@5201
   247
        /* Rebind the context to the window area */
slouken@5201
   248
        SDL_CurrentContext = NULL;
slouken@5201
   249
    }
slouken@6060
   250
slouken@6060
   251
    if (event->event == SDL_WINDOWEVENT_MINIMIZED) {
slouken@6060
   252
        /* According to Apple documentation, we need to finish drawing NOW! */
slouken@6188
   253
        rdata->glFinish();
slouken@6060
   254
    }
slouken@5201
   255
}
slouken@5201
   256
slouken@5297
   257
static int
slouken@5297
   258
GLES2_UpdateViewport(SDL_Renderer * renderer)
slouken@5224
   259
{
slouken@5297
   260
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5224
   261
slouken@5297
   262
    if (SDL_CurrentContext != rdata->context) {
slouken@5297
   263
        /* We'll update the viewport after we rebind the context */
slouken@5297
   264
        return 0;
slouken@5297
   265
    }
slouken@5224
   266
slouken@6188
   267
    rdata->glViewport(renderer->viewport.x, renderer->viewport.y,
slouken@5297
   268
               renderer->viewport.w, renderer->viewport.h);
slouken@5297
   269
    return 0;
slouken@5224
   270
}
slouken@5224
   271
slouken@5224
   272
static void
slouken@5201
   273
GLES2_DestroyRenderer(SDL_Renderer *renderer)
slouken@5201
   274
{
slouken@5201
   275
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   276
slouken@5209
   277
    /* Deallocate everything */
slouken@5209
   278
    if (rdata) {
slouken@5209
   279
        GLES2_ActivateRenderer(renderer);
slouken@5201
   280
slouken@6178
   281
        {
slouken@6178
   282
            GLES2_ShaderCacheEntry *entry;
slouken@6178
   283
            GLES2_ShaderCacheEntry *next;
slouken@6178
   284
            entry = rdata->shader_cache.head;
slouken@6178
   285
            while (entry)
slouken@6178
   286
            {
slouken@6188
   287
                rdata->glDeleteShader(entry->id);
slouken@6052
   288
                next = entry->next;
slouken@6052
   289
                SDL_free(entry);
slouken@6052
   290
                entry = next;
slouken@6178
   291
            }
slouken@6178
   292
        }
slouken@6178
   293
        {
slouken@6178
   294
            GLES2_ProgramCacheEntry *entry;
slouken@6178
   295
            GLES2_ProgramCacheEntry *next;
slouken@6052
   296
            entry = rdata->program_cache.head;
slouken@6052
   297
            while (entry) {
slouken@6188
   298
                rdata->glDeleteProgram(entry->id);
slouken@6052
   299
                next = entry->next;
slouken@6052
   300
                SDL_free(entry);
slouken@6052
   301
                entry = next;
slouken@6052
   302
            }
slouken@6178
   303
        }
slouken@5209
   304
        if (rdata->context) {
slouken@6232
   305
            while (rdata->framebuffers) {
slouken@6232
   306
                GLES2_FBOList *nextnode = rdata->framebuffers->next;
slouken@6232
   307
                rdata->glDeleteFramebuffers(1, &rdata->framebuffers->FBO);
slouken@6232
   308
                SDL_free(rdata->framebuffers);
slouken@6232
   309
                rdata->framebuffers = nextnode;
slouken@6232
   310
            }
slouken@5209
   311
            SDL_GL_DeleteContext(rdata->context);
slouken@5209
   312
        }
slouken@5209
   313
        if (rdata->shader_formats) {
slouken@5209
   314
            SDL_free(rdata->shader_formats);
slouken@5209
   315
        }
slouken@5209
   316
        SDL_free(rdata);
slouken@5201
   317
    }
slouken@5201
   318
    SDL_free(renderer);
slouken@5201
   319
}
slouken@5201
   320
slouken@5201
   321
/*************************************************************************************************
slouken@5201
   322
 * Texture APIs                                                                                  *
slouken@5201
   323
 *************************************************************************************************/
slouken@5201
   324
slouken@5201
   325
static int GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture);
slouken@5201
   326
static void GLES2_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture);
slouken@5201
   327
static int GLES2_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
slouken@5201
   328
                             void **pixels, int *pitch);
slouken@5201
   329
static void GLES2_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture);
slouken@5201
   330
static int GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
slouken@5201
   331
                               const void *pixels, int pitch);
slouken@5201
   332
slouken@5484
   333
static GLenum
slouken@5484
   334
GetScaleQuality(void)
slouken@5484
   335
{
slouken@5484
   336
    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
slouken@5484
   337
slouken@5484
   338
    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
slouken@5484
   339
        return GL_NEAREST;
slouken@5484
   340
    } else {
slouken@5484
   341
        return GL_LINEAR;
slouken@5484
   342
    }
slouken@5484
   343
}
slouken@5484
   344
slouken@5201
   345
static int
slouken@5201
   346
GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
slouken@5201
   347
{
slouken@6188
   348
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   349
    GLES2_TextureData *tdata;
slouken@5201
   350
    GLenum format;
slouken@5201
   351
    GLenum type;
slouken@5503
   352
    GLenum scaleMode;
slouken@5201
   353
slouken@5201
   354
    GLES2_ActivateRenderer(renderer);
slouken@5201
   355
slouken@5201
   356
    /* Determine the corresponding GLES texture format params */
slouken@5201
   357
    switch (texture->format)
slouken@5201
   358
    {
slouken@5201
   359
    case SDL_PIXELFORMAT_ABGR8888:
slouken@6113
   360
    case SDL_PIXELFORMAT_ARGB8888:
slouken@6113
   361
    case SDL_PIXELFORMAT_BGR888:
slouken@6113
   362
    case SDL_PIXELFORMAT_RGB888:
slouken@5201
   363
        format = GL_RGBA;
slouken@5201
   364
        type = GL_UNSIGNED_BYTE;
slouken@5201
   365
        break;
slouken@5201
   366
    default:
slouken@5201
   367
        SDL_SetError("Texture format not supported");
slouken@5201
   368
        return -1;
slouken@5201
   369
    }
slouken@5201
   370
slouken@5201
   371
    /* Allocate a texture struct */
slouken@5201
   372
    tdata = (GLES2_TextureData *)SDL_calloc(1, sizeof(GLES2_TextureData));
slouken@5201
   373
    if (!tdata)
slouken@5201
   374
    {
slouken@5201
   375
        SDL_OutOfMemory();
slouken@5201
   376
        return -1;
slouken@5201
   377
    }
slouken@5201
   378
    tdata->texture = 0;
slouken@5201
   379
    tdata->texture_type = GL_TEXTURE_2D;
slouken@5201
   380
    tdata->pixel_format = format;
slouken@5201
   381
    tdata->pixel_type = type;
slouken@5503
   382
    scaleMode = GetScaleQuality();
slouken@5201
   383
slouken@5201
   384
    /* Allocate a blob for image data */
slouken@5201
   385
    if (texture->access == SDL_TEXTUREACCESS_STREAMING)
slouken@5201
   386
    {
slouken@5201
   387
        tdata->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
slouken@5402
   388
        tdata->pixel_data = SDL_calloc(1, tdata->pitch * texture->h);
slouken@5201
   389
        if (!tdata->pixel_data)
slouken@5201
   390
        {
slouken@5201
   391
            SDL_OutOfMemory();
slouken@5201
   392
            SDL_free(tdata);
slouken@5201
   393
            return -1;
slouken@5201
   394
        }
slouken@5201
   395
    }
slouken@5201
   396
slouken@5201
   397
    /* Allocate the texture */
slouken@6188
   398
    rdata->glGetError();
slouken@6188
   399
    rdata->glGenTextures(1, &tdata->texture);
slouken@6188
   400
    rdata->glActiveTexture(GL_TEXTURE0);
slouken@6188
   401
    rdata->glBindTexture(tdata->texture_type, tdata->texture);
slouken@6188
   402
    rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_MIN_FILTER, scaleMode);
slouken@6188
   403
    rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_MAG_FILTER, scaleMode);
slouken@6188
   404
    rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
slouken@6188
   405
    rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
slouken@6188
   406
    rdata->glTexImage2D(tdata->texture_type, 0, format, texture->w, texture->h, 0, format, type, NULL);
slouken@6188
   407
    if (rdata->glGetError() != GL_NO_ERROR)
slouken@5201
   408
    {
slouken@5201
   409
        SDL_SetError("Texture creation failed");
slouken@6188
   410
        rdata->glDeleteTextures(1, &tdata->texture);
slouken@5201
   411
        SDL_free(tdata);
slouken@5201
   412
        return -1;
slouken@5201
   413
    }
slouken@5201
   414
    texture->driverdata = tdata;
slouken@6232
   415
slouken@6232
   416
    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
slouken@6232
   417
       tdata->fbo = GLES2_GetFBO(renderer->driverdata, texture->w, texture->h);
slouken@6232
   418
    } else {
slouken@6232
   419
       tdata->fbo = NULL;
slouken@6232
   420
    }
slouken@6232
   421
slouken@5201
   422
    return 0;
slouken@5201
   423
}
slouken@5201
   424
slouken@5201
   425
static void
slouken@5201
   426
GLES2_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture)
slouken@5201
   427
{
slouken@6188
   428
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   429
    GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
slouken@5201
   430
slouken@5201
   431
    GLES2_ActivateRenderer(renderer);
slouken@5201
   432
slouken@5201
   433
    /* Destroy the texture */
slouken@5201
   434
    if (tdata)
slouken@5201
   435
    {
slouken@6188
   436
        rdata->glDeleteTextures(1, &tdata->texture);
slouken@5201
   437
        SDL_free(tdata->pixel_data);
slouken@5201
   438
        SDL_free(tdata);
slouken@5201
   439
        texture->driverdata = NULL;
slouken@5201
   440
    }
slouken@5201
   441
}
slouken@5201
   442
slouken@5201
   443
static int
slouken@5201
   444
GLES2_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
slouken@5201
   445
                  void **pixels, int *pitch)
slouken@5201
   446
{
slouken@5201
   447
    GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
slouken@5201
   448
slouken@5201
   449
    /* Retrieve the buffer/pitch for the specified region */
slouken@5201
   450
    *pixels = (Uint8 *)tdata->pixel_data +
slouken@5201
   451
              (tdata->pitch * rect->y) +
slouken@5201
   452
              (rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5201
   453
    *pitch = tdata->pitch;
slouken@5201
   454
slouken@5201
   455
    return 0;
slouken@5201
   456
}
slouken@5201
   457
slouken@5201
   458
static void
slouken@5201
   459
GLES2_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture)
slouken@5201
   460
{
slouken@5201
   461
    GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
slouken@5227
   462
    SDL_Rect rect;
slouken@5201
   463
slouken@5227
   464
    /* We do whole texture updates, at least for now */
slouken@5227
   465
    rect.x = 0;
slouken@5227
   466
    rect.y = 0;
slouken@5227
   467
    rect.w = texture->w;
slouken@5227
   468
    rect.h = texture->h;
slouken@5227
   469
    GLES2_UpdateTexture(renderer, texture, &rect, tdata->pixel_data, tdata->pitch);
slouken@5201
   470
}
slouken@5201
   471
slouken@5201
   472
static int
slouken@5201
   473
GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
slouken@5201
   474
                    const void *pixels, int pitch)
slouken@5201
   475
{
slouken@6188
   476
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   477
    GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
slouken@5201
   478
    Uint8 *blob = NULL;
slouken@5201
   479
    Uint8 *src;
slouken@5201
   480
    int srcPitch;
slouken@5201
   481
    int y;
slouken@5201
   482
slouken@5201
   483
    GLES2_ActivateRenderer(renderer);
slouken@6232
   484
slouken@5201
   485
    /* Bail out if we're supposed to update an empty rectangle */
slouken@5201
   486
    if (rect->w <= 0 || rect->h <= 0)
slouken@5201
   487
        return 0;
slouken@5201
   488
slouken@5201
   489
    /* Reformat the texture data into a tightly packed array */
slouken@5201
   490
    srcPitch = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@5201
   491
    src = (Uint8 *)pixels;
slouken@5201
   492
    if (pitch != srcPitch)
slouken@5201
   493
    {
slouken@5201
   494
        blob = (Uint8 *)SDL_malloc(srcPitch * rect->h);
slouken@5201
   495
        if (!blob)
slouken@5201
   496
        {
slouken@5201
   497
            SDL_OutOfMemory();
slouken@5201
   498
            return -1;
slouken@5201
   499
        }
slouken@5201
   500
        src = blob;
slouken@5201
   501
        for (y = 0; y < rect->h; ++y)
slouken@5201
   502
        {
slouken@5201
   503
            SDL_memcpy(src, pixels, srcPitch);
slouken@5201
   504
            src += srcPitch;
slouken@5201
   505
            pixels = (Uint8 *)pixels + pitch;
slouken@5201
   506
        }
slouken@5201
   507
        src = blob;
slouken@5201
   508
    }
slouken@5201
   509
slouken@5201
   510
    /* Create a texture subimage with the supplied data */
slouken@6188
   511
    rdata->glGetError();
slouken@6188
   512
    rdata->glActiveTexture(GL_TEXTURE0);
slouken@6188
   513
    rdata->glBindTexture(tdata->texture_type, tdata->texture);
slouken@6188
   514
    rdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@6188
   515
    rdata->glTexSubImage2D(tdata->texture_type,
slouken@5201
   516
                    0,
slouken@5201
   517
                    rect->x,
slouken@5201
   518
                    rect->y,
slouken@5201
   519
                    rect->w,
slouken@5201
   520
                    rect->h,
slouken@5201
   521
                    tdata->pixel_format,
slouken@5201
   522
                    tdata->pixel_type,
slouken@5201
   523
                    src);
slouken@5227
   524
    if (blob) {
slouken@5227
   525
        SDL_free(blob);
slouken@5227
   526
    }
slouken@5214
   527
slouken@6188
   528
    if (rdata->glGetError() != GL_NO_ERROR)
slouken@5201
   529
    {
slouken@5201
   530
        SDL_SetError("Failed to update texture");
slouken@5201
   531
        return -1;
slouken@5201
   532
    }
slouken@5201
   533
    return 0;
slouken@5201
   534
}
slouken@5201
   535
slouken@5201
   536
/*************************************************************************************************
slouken@5201
   537
 * Shader management functions                                                                   *
slouken@5201
   538
 *************************************************************************************************/
slouken@5201
   539
slouken@5201
   540
static GLES2_ShaderCacheEntry *GLES2_CacheShader(SDL_Renderer *renderer, GLES2_ShaderType type,
slouken@5201
   541
                                                 SDL_BlendMode blendMode);
slouken@5201
   542
static void GLES2_EvictShader(SDL_Renderer *renderer, GLES2_ShaderCacheEntry *entry);
slouken@5201
   543
static GLES2_ProgramCacheEntry *GLES2_CacheProgram(SDL_Renderer *renderer,
slouken@5201
   544
                                                   GLES2_ShaderCacheEntry *vertex,
slouken@5201
   545
                                                   GLES2_ShaderCacheEntry *fragment,
slouken@5201
   546
                                                   SDL_BlendMode blendMode);
slouken@5201
   547
static int GLES2_SelectProgram(SDL_Renderer *renderer, GLES2_ImageSource source,
slouken@5201
   548
                               SDL_BlendMode blendMode);
slouken@5201
   549
static int GLES2_SetOrthographicProjection(SDL_Renderer *renderer);
slouken@5201
   550
slouken@5201
   551
static GLES2_ProgramCacheEntry *
slouken@5201
   552
GLES2_CacheProgram(SDL_Renderer *renderer, GLES2_ShaderCacheEntry *vertex,
slouken@5201
   553
                   GLES2_ShaderCacheEntry *fragment, SDL_BlendMode blendMode)
slouken@5201
   554
{
slouken@5201
   555
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   556
    GLES2_ProgramCacheEntry *entry;
slouken@5201
   557
    GLES2_ShaderCacheEntry *shaderEntry;
slouken@5201
   558
    GLint linkSuccessful;
slouken@5201
   559
slouken@5201
   560
    /* Check if we've already cached this program */
slouken@5201
   561
    entry = rdata->program_cache.head;
slouken@5201
   562
    while (entry)
slouken@5201
   563
    {
slouken@5201
   564
        if (entry->vertex_shader == vertex && entry->fragment_shader == fragment)
slouken@5201
   565
            break;
slouken@5201
   566
        entry = entry->next;
slouken@5201
   567
    }
slouken@5201
   568
    if (entry)
slouken@5201
   569
    {
slouken@6061
   570
        if (rdata->program_cache.head != entry)
slouken@5201
   571
        {
slouken@5201
   572
            if (entry->next)
slouken@5201
   573
                entry->next->prev = entry->prev;
slouken@5201
   574
            if (entry->prev)
slouken@5201
   575
                entry->prev->next = entry->next;
slouken@5201
   576
            entry->prev = NULL;
slouken@5201
   577
            entry->next = rdata->program_cache.head;
slouken@5201
   578
            rdata->program_cache.head->prev = entry;
slouken@5201
   579
            rdata->program_cache.head = entry;
slouken@5201
   580
        }
slouken@5201
   581
        return entry;
slouken@5201
   582
    }
slouken@5201
   583
slouken@5201
   584
    /* Create a program cache entry */
slouken@5201
   585
    entry = (GLES2_ProgramCacheEntry *)SDL_calloc(1, sizeof(GLES2_ProgramCacheEntry));
slouken@5201
   586
    if (!entry)
slouken@5201
   587
    {
slouken@5201
   588
        SDL_OutOfMemory();
slouken@5201
   589
        return NULL;
slouken@5201
   590
    }
slouken@5201
   591
    entry->vertex_shader = vertex;
slouken@5201
   592
    entry->fragment_shader = fragment;
slouken@5201
   593
    entry->blend_mode = blendMode;
slouken@6232
   594
slouken@5201
   595
    /* Create the program and link it */
slouken@6188
   596
    rdata->glGetError();
slouken@6188
   597
    entry->id = rdata->glCreateProgram();
slouken@6188
   598
    rdata->glAttachShader(entry->id, vertex->id);
slouken@6188
   599
    rdata->glAttachShader(entry->id, fragment->id);
slouken@6188
   600
    rdata->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_POSITION, "a_position");
slouken@6188
   601
    rdata->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_TEXCOORD, "a_texCoord");
slouken@6188
   602
    rdata->glLinkProgram(entry->id);
slouken@6188
   603
    rdata->glGetProgramiv(entry->id, GL_LINK_STATUS, &linkSuccessful);
slouken@6188
   604
    if (rdata->glGetError() != GL_NO_ERROR || !linkSuccessful)
slouken@5201
   605
    {
slouken@5201
   606
        SDL_SetError("Failed to link shader program");
slouken@6188
   607
        rdata->glDeleteProgram(entry->id);
slouken@5201
   608
        SDL_free(entry);
slouken@5201
   609
        return NULL;
slouken@5201
   610
    }
slouken@6232
   611
slouken@5201
   612
    /* Predetermine locations of uniform variables */
slouken@5201
   613
    entry->uniform_locations[GLES2_UNIFORM_PROJECTION] =
slouken@6188
   614
        rdata->glGetUniformLocation(entry->id, "u_projection");
slouken@5201
   615
    entry->uniform_locations[GLES2_UNIFORM_TEXTURE] =
slouken@6188
   616
        rdata->glGetUniformLocation(entry->id, "u_texture");
slouken@5201
   617
    entry->uniform_locations[GLES2_UNIFORM_MODULATION] =
slouken@6188
   618
        rdata->glGetUniformLocation(entry->id, "u_modulation");
slouken@5201
   619
    entry->uniform_locations[GLES2_UNIFORM_COLOR] =
slouken@6188
   620
        rdata->glGetUniformLocation(entry->id, "u_color");
slouken@5201
   621
    entry->uniform_locations[GLES2_UNIFORM_COLORTABLE] =
slouken@6188
   622
        rdata->glGetUniformLocation(entry->id, "u_colorTable");
slouken@5201
   623
slouken@5201
   624
    /* Cache the linked program */
slouken@5201
   625
    if (rdata->program_cache.head)
slouken@5201
   626
    {
slouken@5201
   627
        entry->next = rdata->program_cache.head;
slouken@5201
   628
        rdata->program_cache.head->prev = entry;
slouken@5201
   629
    }
slouken@5201
   630
    else
slouken@5201
   631
    {
slouken@5201
   632
        rdata->program_cache.tail = entry;
slouken@5201
   633
    }
slouken@5201
   634
    rdata->program_cache.head = entry;
slouken@5201
   635
    ++rdata->program_cache.count;
slouken@5201
   636
slouken@5201
   637
    /* Increment the refcount of the shaders we're using */
slouken@5201
   638
    ++vertex->references;
slouken@5201
   639
    ++fragment->references;
slouken@5201
   640
slouken@5201
   641
    /* Evict the last entry from the cache if we exceed the limit */
slouken@5201
   642
    if (rdata->program_cache.count > GLES2_MAX_CACHED_PROGRAMS)
slouken@5201
   643
    {
slouken@5201
   644
        shaderEntry = rdata->program_cache.tail->vertex_shader;
slouken@5201
   645
        if (--shaderEntry->references <= 0)
slouken@5201
   646
            GLES2_EvictShader(renderer, shaderEntry);
slouken@5201
   647
        shaderEntry = rdata->program_cache.tail->fragment_shader;
slouken@5201
   648
        if (--shaderEntry->references <= 0)
slouken@5201
   649
            GLES2_EvictShader(renderer, shaderEntry);
slouken@6188
   650
        rdata->glDeleteProgram(rdata->program_cache.tail->id);
slouken@5201
   651
        rdata->program_cache.tail = rdata->program_cache.tail->prev;
slouken@5201
   652
        SDL_free(rdata->program_cache.tail->next);
slouken@5201
   653
        rdata->program_cache.tail->next = NULL;
slouken@5201
   654
        --rdata->program_cache.count;
slouken@5201
   655
    }
slouken@5201
   656
    return entry;
slouken@5201
   657
}
slouken@5201
   658
slouken@5201
   659
static GLES2_ShaderCacheEntry *
slouken@5201
   660
GLES2_CacheShader(SDL_Renderer *renderer, GLES2_ShaderType type, SDL_BlendMode blendMode)
slouken@5201
   661
{
slouken@5201
   662
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   663
    const GLES2_Shader *shader;
slouken@5201
   664
    const GLES2_ShaderInstance *instance = NULL;
slouken@5201
   665
    GLES2_ShaderCacheEntry *entry = NULL;
slouken@5201
   666
    GLint compileSuccessful = GL_FALSE;
slouken@5201
   667
    int i, j;
slouken@5201
   668
slouken@5201
   669
    /* Find the corresponding shader */
slouken@5201
   670
    shader = GLES2_GetShader(type, blendMode);
slouken@5201
   671
    if (!shader)
slouken@5201
   672
    {
slouken@5201
   673
        SDL_SetError("No shader matching the requested characteristics was found");
slouken@5201
   674
        return NULL;
slouken@5201
   675
    }
slouken@6232
   676
slouken@5201
   677
    /* Find a matching shader instance that's supported on this hardware */
slouken@5206
   678
    for (i = 0; i < shader->instance_count && !instance; ++i)
slouken@5201
   679
    {
slouken@5206
   680
        for (j = 0; j < rdata->shader_format_count && !instance; ++j)
slouken@5201
   681
        {
slouken@5201
   682
            if (!shader->instances)
slouken@5201
   683
                continue;
slouken@5206
   684
            if (!shader->instances[i])
slouken@5206
   685
                continue;
slouken@5201
   686
            if (shader->instances[i]->format != rdata->shader_formats[j])
slouken@5201
   687
                continue;
slouken@5201
   688
            instance = shader->instances[i];
slouken@5201
   689
        }
slouken@5201
   690
    }
slouken@5201
   691
    if (!instance)
slouken@5201
   692
    {
slouken@5201
   693
        SDL_SetError("The specified shader cannot be loaded on the current platform");
slouken@5201
   694
        return NULL;
slouken@5201
   695
    }
slouken@5201
   696
slouken@5201
   697
    /* Check if we've already cached this shader */
slouken@5201
   698
    entry = rdata->shader_cache.head;
slouken@5201
   699
    while (entry)
slouken@5201
   700
    {
slouken@5201
   701
        if (entry->instance == instance)
slouken@5201
   702
            break;
slouken@5201
   703
        entry = entry->next;
slouken@5201
   704
    }
slouken@5201
   705
    if (entry)
slouken@5201
   706
        return entry;
slouken@5201
   707
slouken@5201
   708
    /* Create a shader cache entry */
slouken@5201
   709
    entry = (GLES2_ShaderCacheEntry *)SDL_calloc(1, sizeof(GLES2_ShaderCacheEntry));
slouken@5201
   710
    if (!entry)
slouken@5201
   711
    {
slouken@5201
   712
        SDL_OutOfMemory();
slouken@5201
   713
        return NULL;
slouken@5201
   714
    }
slouken@5201
   715
    entry->type = type;
slouken@5201
   716
    entry->instance = instance;
slouken@5201
   717
slouken@5201
   718
    /* Compile or load the selected shader instance */
slouken@6188
   719
    rdata->glGetError();
slouken@6188
   720
    entry->id = rdata->glCreateShader(instance->type);
slouken@5201
   721
    if (instance->format == (GLenum)-1)
slouken@5201
   722
    {
slouken@6188
   723
        rdata->glShaderSource(entry->id, 1, (const char **)&instance->data, NULL);
slouken@6188
   724
        rdata->glCompileShader(entry->id);
slouken@6188
   725
        rdata->glGetShaderiv(entry->id, GL_COMPILE_STATUS, &compileSuccessful);
slouken@5201
   726
    }
slouken@5201
   727
    else
slouken@5201
   728
    {
slouken@6188
   729
        rdata->glShaderBinary(1, &entry->id, instance->format, instance->data, instance->length);
slouken@5201
   730
        compileSuccessful = GL_TRUE;
slouken@5201
   731
    }
slouken@6188
   732
    if (rdata->glGetError() != GL_NO_ERROR || !compileSuccessful)
slouken@5201
   733
    {
slouken@5212
   734
        char *info = NULL;
slouken@6113
   735
        int length = 0;
slouken@5212
   736
slouken@6188
   737
        rdata->glGetShaderiv(entry->id, GL_INFO_LOG_LENGTH, &length);
slouken@5212
   738
        if (length > 0) {
slouken@5212
   739
            info = SDL_stack_alloc(char, length);
slouken@5212
   740
            if (info) {
slouken@6188
   741
                rdata->glGetShaderInfoLog(entry->id, length, &length, info);
slouken@5212
   742
            }
slouken@5212
   743
        }
slouken@5212
   744
        if (info) {
slouken@5212
   745
            SDL_SetError("Failed to load the shader: %s", info);
slouken@5212
   746
            SDL_stack_free(info);
slouken@5212
   747
        } else {
slouken@5212
   748
            SDL_SetError("Failed to load the shader");
slouken@5212
   749
        }
slouken@6188
   750
        rdata->glDeleteShader(entry->id);
slouken@5201
   751
        SDL_free(entry);
slouken@5201
   752
        return NULL;
slouken@5201
   753
    }
slouken@5201
   754
slouken@5201
   755
    /* Link the shader entry in at the front of the cache */
slouken@5201
   756
    if (rdata->shader_cache.head)
slouken@5201
   757
    {
slouken@5201
   758
        entry->next = rdata->shader_cache.head;
slouken@5201
   759
        rdata->shader_cache.head->prev = entry;
slouken@5201
   760
    }
slouken@5201
   761
    rdata->shader_cache.head = entry;
slouken@5201
   762
    ++rdata->shader_cache.count;
slouken@5201
   763
    return entry;
slouken@5201
   764
}
slouken@5201
   765
slouken@5201
   766
static void
slouken@5201
   767
GLES2_EvictShader(SDL_Renderer *renderer, GLES2_ShaderCacheEntry *entry)
slouken@5201
   768
{
slouken@5201
   769
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   770
slouken@5201
   771
    /* Unlink the shader from the cache */
slouken@5201
   772
    if (entry->next)
slouken@5201
   773
        entry->next->prev = entry->prev;
slouken@5201
   774
    if (entry->prev)
slouken@5201
   775
        entry->prev->next = entry->next;
slouken@5201
   776
    if (rdata->shader_cache.head == entry)
slouken@5201
   777
        rdata->shader_cache.head = entry->next;
slouken@5201
   778
    --rdata->shader_cache.count;
slouken@5201
   779
slouken@5201
   780
    /* Deallocate the shader */
slouken@6188
   781
    rdata->glDeleteShader(entry->id);
slouken@5201
   782
    SDL_free(entry);
slouken@5201
   783
}
slouken@5201
   784
slouken@5201
   785
static int
slouken@5201
   786
GLES2_SelectProgram(SDL_Renderer *renderer, GLES2_ImageSource source, SDL_BlendMode blendMode)
slouken@5201
   787
{
slouken@5201
   788
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   789
    GLES2_ShaderCacheEntry *vertex = NULL;
slouken@5201
   790
    GLES2_ShaderCacheEntry *fragment = NULL;
slouken@5201
   791
    GLES2_ShaderType vtype, ftype;
slouken@5201
   792
    GLES2_ProgramCacheEntry *program;
slouken@5201
   793
slouken@5201
   794
    /* Select an appropriate shader pair for the specified modes */
slouken@5201
   795
    vtype = GLES2_SHADER_VERTEX_DEFAULT;
slouken@5201
   796
    switch (source)
slouken@5201
   797
    {
slouken@5201
   798
    case GLES2_IMAGESOURCE_SOLID:
slouken@5201
   799
        ftype = GLES2_SHADER_FRAGMENT_SOLID_SRC;
slouken@5201
   800
        break;
slouken@6113
   801
    case GLES2_IMAGESOURCE_TEXTURE_ABGR:
slouken@6113
   802
        ftype = GLES2_SHADER_FRAGMENT_TEXTURE_ABGR_SRC;
slouken@6113
   803
            break;
slouken@6113
   804
    case GLES2_IMAGESOURCE_TEXTURE_ARGB:
slouken@6113
   805
        ftype = GLES2_SHADER_FRAGMENT_TEXTURE_ARGB_SRC;
slouken@5201
   806
        break;
slouken@6113
   807
    case GLES2_IMAGESOURCE_TEXTURE_RGB:
slouken@6113
   808
        ftype = GLES2_SHADER_FRAGMENT_TEXTURE_RGB_SRC;
slouken@6113
   809
        break;
slouken@6113
   810
    case GLES2_IMAGESOURCE_TEXTURE_BGR:
slouken@6113
   811
        ftype = GLES2_SHADER_FRAGMENT_TEXTURE_BGR_SRC;
slouken@6113
   812
        break;
slouken@6113
   813
    default:
slouken@6113
   814
        goto fault;
slouken@5201
   815
    }
slouken@5201
   816
slouken@5201
   817
    /* Load the requested shaders */
slouken@5201
   818
    vertex = GLES2_CacheShader(renderer, vtype, blendMode);
slouken@5201
   819
    if (!vertex)
slouken@5201
   820
        goto fault;
slouken@5201
   821
    fragment = GLES2_CacheShader(renderer, ftype, blendMode);
slouken@5201
   822
    if (!fragment)
slouken@5201
   823
        goto fault;
slouken@5201
   824
slouken@5201
   825
    /* Check if we need to change programs at all */
slouken@5201
   826
    if (rdata->current_program &&
slouken@5201
   827
        rdata->current_program->vertex_shader == vertex &&
slouken@5201
   828
        rdata->current_program->fragment_shader == fragment)
slouken@5201
   829
        return 0;
slouken@5201
   830
slouken@5201
   831
    /* Generate a matching program */
slouken@5201
   832
    program = GLES2_CacheProgram(renderer, vertex, fragment, blendMode);
slouken@5201
   833
    if (!program)
slouken@5201
   834
        goto fault;
slouken@5201
   835
slouken@5201
   836
    /* Select that program in OpenGL */
slouken@6188
   837
    rdata->glGetError();
slouken@6188
   838
    rdata->glUseProgram(program->id);
slouken@6188
   839
    if (rdata->glGetError() != GL_NO_ERROR)
slouken@5201
   840
    {
slouken@5201
   841
        SDL_SetError("Failed to select program");
slouken@5201
   842
        goto fault;
slouken@5201
   843
    }
slouken@5201
   844
slouken@5201
   845
    /* Set the current program */
slouken@5201
   846
    rdata->current_program = program;
slouken@5201
   847
slouken@5201
   848
    /* Activate an orthographic projection */
slouken@5201
   849
    if (GLES2_SetOrthographicProjection(renderer) < 0)
slouken@5201
   850
        goto fault;
slouken@5201
   851
slouken@5201
   852
    /* Clean up and return */
slouken@5201
   853
    return 0;
slouken@5201
   854
fault:
slouken@5201
   855
    if (vertex && vertex->references <= 0)
slouken@5201
   856
        GLES2_EvictShader(renderer, vertex);
slouken@5201
   857
    if (fragment && fragment->references <= 0)
slouken@5201
   858
        GLES2_EvictShader(renderer, fragment);
slouken@5201
   859
    rdata->current_program = NULL;
slouken@5201
   860
    return -1;
slouken@5201
   861
}
slouken@5201
   862
slouken@5201
   863
static int
slouken@5201
   864
GLES2_SetOrthographicProjection(SDL_Renderer *renderer)
slouken@5201
   865
{
slouken@5201
   866
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   867
    GLfloat projection[4][4];
slouken@5201
   868
    GLuint locProjection;
slouken@5201
   869
slouken@5201
   870
    /* Prepare an orthographic projection */
slouken@5297
   871
    projection[0][0] = 2.0f / renderer->viewport.w;
slouken@5201
   872
    projection[0][1] = 0.0f;
slouken@5201
   873
    projection[0][2] = 0.0f;
slouken@5201
   874
    projection[0][3] = 0.0f;
slouken@5201
   875
    projection[1][0] = 0.0f;
slouken@5297
   876
    projection[1][1] = -2.0f / renderer->viewport.h;
slouken@5201
   877
    projection[1][2] = 0.0f;
slouken@5201
   878
    projection[1][3] = 0.0f;
slouken@5201
   879
    projection[2][0] = 0.0f;
slouken@5201
   880
    projection[2][1] = 0.0f;
slouken@5201
   881
    projection[2][2] = 1.0f;
slouken@5201
   882
    projection[2][3] = 0.0f;
slouken@5201
   883
    projection[3][0] = -1.0f;
slouken@5201
   884
    projection[3][1] = 1.0f;
slouken@5201
   885
    projection[3][2] = 0.0f;
slouken@5201
   886
    projection[3][3] = 1.0f;
slouken@5201
   887
slouken@5201
   888
    /* Set the projection matrix */
slouken@5201
   889
    locProjection = rdata->current_program->uniform_locations[GLES2_UNIFORM_PROJECTION];
slouken@6188
   890
    rdata->glGetError();
slouken@6188
   891
    rdata->glUniformMatrix4fv(locProjection, 1, GL_FALSE, (GLfloat *)projection);
slouken@6188
   892
    if (rdata->glGetError() != GL_NO_ERROR)
slouken@5201
   893
    {
slouken@5201
   894
        SDL_SetError("Failed to set orthographic projection");
slouken@5201
   895
        return -1;
slouken@5201
   896
    }
slouken@5201
   897
    return 0;
slouken@5201
   898
}
slouken@5201
   899
slouken@5201
   900
/*************************************************************************************************
slouken@5201
   901
 * Rendering functions                                                                           *
slouken@5201
   902
 *************************************************************************************************/
slouken@5201
   903
slouken@5224
   904
static const float inv255f = 1.0f / 255.0f;
slouken@5224
   905
slouken@5201
   906
static int GLES2_RenderClear(SDL_Renderer *renderer);
slouken@5201
   907
static int GLES2_RenderDrawPoints(SDL_Renderer *renderer, const SDL_Point *points, int count);
slouken@5201
   908
static int GLES2_RenderDrawLines(SDL_Renderer *renderer, const SDL_Point *points, int count);
slouken@5297
   909
static int GLES2_RenderFillRects(SDL_Renderer *renderer, const SDL_Rect *rects, int count);
slouken@5201
   910
static int GLES2_RenderCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect,
slouken@5201
   911
                            const SDL_Rect *dstrect);
slouken@6042
   912
static int GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@6042
   913
                    Uint32 pixel_format, void * pixels, int pitch);
slouken@5201
   914
static void GLES2_RenderPresent(SDL_Renderer *renderer);
slouken@5201
   915
slouken@5333
   916
slouken@5201
   917
static int
slouken@5333
   918
GLES2_RenderClear(SDL_Renderer * renderer)
slouken@5201
   919
{
slouken@6188
   920
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@6188
   921
slouken@5201
   922
    GLES2_ActivateRenderer(renderer);
slouken@6232
   923
slouken@6188
   924
    rdata->glClearColor((GLfloat) renderer->r * inv255f,
slouken@5333
   925
                 (GLfloat) renderer->g * inv255f,
slouken@5333
   926
                 (GLfloat) renderer->b * inv255f,
slouken@5333
   927
                 (GLfloat) renderer->a * inv255f);
slouken@5333
   928
slouken@6188
   929
    rdata->glClear(GL_COLOR_BUFFER_BIT);
slouken@5333
   930
slouken@5201
   931
    return 0;
slouken@5201
   932
}
slouken@5201
   933
slouken@5201
   934
static void
slouken@5355
   935
GLES2_SetBlendMode(GLES2_DriverContext *rdata, int blendMode)
slouken@5201
   936
{
slouken@5355
   937
    if (blendMode != rdata->current.blendMode) {
slouken@5355
   938
        switch (blendMode) {
slouken@5355
   939
        default:
slouken@5355
   940
        case SDL_BLENDMODE_NONE:
slouken@6188
   941
            rdata->glDisable(GL_BLEND);
slouken@5355
   942
            break;
slouken@5355
   943
        case SDL_BLENDMODE_BLEND:
slouken@6188
   944
            rdata->glEnable(GL_BLEND);
slouken@6188
   945
            rdata->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@5355
   946
            break;
slouken@5355
   947
        case SDL_BLENDMODE_ADD:
slouken@6188
   948
            rdata->glEnable(GL_BLEND);
slouken@6188
   949
            rdata->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@5355
   950
            break;
slouken@5355
   951
        case SDL_BLENDMODE_MOD:
slouken@6188
   952
            rdata->glEnable(GL_BLEND);
slouken@6188
   953
            rdata->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@5355
   954
            break;
slouken@5355
   955
        }
slouken@5355
   956
        rdata->current.blendMode = blendMode;
slouken@5201
   957
    }
slouken@5201
   958
}
slouken@5201
   959
slouken@5355
   960
static void
slouken@5355
   961
GLES2_SetTexCoords(GLES2_DriverContext * rdata, SDL_bool enabled)
slouken@5355
   962
{
slouken@5355
   963
    if (enabled != rdata->current.tex_coords) {
slouken@5355
   964
        if (enabled) {
slouken@6188
   965
            rdata->glEnableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
slouken@5355
   966
        } else {
slouken@6188
   967
            rdata->glDisableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
slouken@5355
   968
        }
slouken@5355
   969
        rdata->current.tex_coords = enabled;
slouken@5355
   970
    }
slouken@5355
   971
}
slouken@5355
   972
slouken@5355
   973
static int
slouken@5355
   974
GLES2_SetDrawingState(SDL_Renderer * renderer)
slouken@5355
   975
{
slouken@5355
   976
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5355
   977
    int blendMode = renderer->blendMode;
slouken@5355
   978
    GLuint locColor;
slouken@5355
   979
slouken@6188
   980
    rdata->glGetError();
slouken@5355
   981
slouken@5355
   982
    GLES2_ActivateRenderer(renderer);
slouken@5355
   983
slouken@5355
   984
    GLES2_SetBlendMode(rdata, blendMode);
slouken@5355
   985
slouken@5355
   986
    GLES2_SetTexCoords(rdata, SDL_FALSE);
slouken@5355
   987
slouken@5355
   988
    /* Activate an appropriate shader and set the projection matrix */
slouken@5355
   989
    if (GLES2_SelectProgram(renderer, GLES2_IMAGESOURCE_SOLID, blendMode) < 0)
slouken@5355
   990
        return -1;
slouken@5355
   991
slouken@5355
   992
    /* Select the color to draw with */
slouken@5355
   993
    locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
slouken@6188
   994
    rdata->glUniform4f(locColor,
slouken@5355
   995
                renderer->r * inv255f,
slouken@5355
   996
                renderer->g * inv255f,
slouken@5355
   997
                renderer->b * inv255f,
slouken@5355
   998
                renderer->a * inv255f);
slouken@5355
   999
    return 0;
slouken@5355
  1000
}
slouken@5355
  1001
slouken@5201
  1002
static int
slouken@5201
  1003
GLES2_RenderDrawPoints(SDL_Renderer *renderer, const SDL_Point *points, int count)
slouken@5201
  1004
{
slouken@6188
  1005
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
  1006
    GLfloat *vertices;
slouken@5201
  1007
    int idx;
slouken@5201
  1008
slouken@5355
  1009
    if (GLES2_SetDrawingState(renderer) < 0) {
slouken@5201
  1010
        return -1;
slouken@5355
  1011
    }
slouken@5201
  1012
slouken@5201
  1013
    /* Emit the specified vertices as points */
slouken@5201
  1014
    vertices = SDL_stack_alloc(GLfloat, count * 2);
slouken@5201
  1015
    for (idx = 0; idx < count; ++idx)
slouken@5201
  1016
    {
slouken@5201
  1017
        GLfloat x = (GLfloat)points[idx].x + 0.5f;
slouken@5201
  1018
        GLfloat y = (GLfloat)points[idx].y + 0.5f;
slouken@5201
  1019
slouken@5201
  1020
        vertices[idx * 2] = x;
slouken@5201
  1021
        vertices[(idx * 2) + 1] = y;
slouken@5201
  1022
    }
slouken@6188
  1023
    rdata->glGetError();
slouken@6188
  1024
    rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
slouken@6188
  1025
    rdata->glDrawArrays(GL_POINTS, 0, count);
slouken@5201
  1026
    SDL_stack_free(vertices);
slouken@6188
  1027
    if (rdata->glGetError() != GL_NO_ERROR)
slouken@5201
  1028
    {
slouken@5201
  1029
        SDL_SetError("Failed to render lines");
slouken@5201
  1030
        return -1;
slouken@5201
  1031
    }
slouken@5201
  1032
    return 0;
slouken@5201
  1033
}
slouken@5201
  1034
slouken@5201
  1035
static int
slouken@5201
  1036
GLES2_RenderDrawLines(SDL_Renderer *renderer, const SDL_Point *points, int count)
slouken@5201
  1037
{
slouken@6188
  1038
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
  1039
    GLfloat *vertices;
slouken@5201
  1040
    int idx;
slouken@5201
  1041
slouken@5355
  1042
    if (GLES2_SetDrawingState(renderer) < 0) {
slouken@5201
  1043
        return -1;
slouken@5355
  1044
    }
slouken@5201
  1045
slouken@5201
  1046
    /* Emit a line strip including the specified vertices */
slouken@5201
  1047
    vertices = SDL_stack_alloc(GLfloat, count * 2);
slouken@5201
  1048
    for (idx = 0; idx < count; ++idx)
slouken@5201
  1049
    {
slouken@5201
  1050
        GLfloat x = (GLfloat)points[idx].x + 0.5f;
slouken@5201
  1051
        GLfloat y = (GLfloat)points[idx].y + 0.5f;
slouken@5201
  1052
slouken@5201
  1053
        vertices[idx * 2] = x;
slouken@5201
  1054
        vertices[(idx * 2) + 1] = y;
slouken@5201
  1055
    }
slouken@6188
  1056
    rdata->glGetError();
slouken@6188
  1057
    rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
slouken@6188
  1058
    rdata->glDrawArrays(GL_LINE_STRIP, 0, count);
slouken@6076
  1059
slouken@6076
  1060
    /* We need to close the endpoint of the line */
slouken@6076
  1061
    if (count == 2 ||
slouken@6076
  1062
        points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
slouken@6188
  1063
        rdata->glDrawArrays(GL_POINTS, count-1, 1);
slouken@6076
  1064
    }
slouken@5201
  1065
    SDL_stack_free(vertices);
slouken@6188
  1066
    if (rdata->glGetError() != GL_NO_ERROR)
slouken@5201
  1067
    {
slouken@5201
  1068
        SDL_SetError("Failed to render lines");
slouken@5201
  1069
        return -1;
slouken@5201
  1070
    }
slouken@5201
  1071
    return 0;
slouken@5201
  1072
}
slouken@5201
  1073
slouken@5201
  1074
static int
slouken@5297
  1075
GLES2_RenderFillRects(SDL_Renderer *renderer, const SDL_Rect *rects, int count)
slouken@5201
  1076
{
slouken@6188
  1077
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
  1078
    GLfloat vertices[8];
slouken@5201
  1079
    int idx;
slouken@5201
  1080
slouken@5355
  1081
    if (GLES2_SetDrawingState(renderer) < 0) {
slouken@5201
  1082
        return -1;
slouken@5355
  1083
    }
slouken@5201
  1084
slouken@5201
  1085
    /* Emit a line loop for each rectangle */
slouken@6188
  1086
    rdata->glGetError();
slouken@5297
  1087
    for (idx = 0; idx < count; ++idx) {
slouken@5297
  1088
        const SDL_Rect *rect = &rects[idx];
slouken@5297
  1089
slouken@5297
  1090
        GLfloat xMin = (GLfloat)rect->x;
slouken@5297
  1091
        GLfloat xMax = (GLfloat)(rect->x + rect->w);
slouken@5297
  1092
        GLfloat yMin = (GLfloat)rect->y;
slouken@5297
  1093
        GLfloat yMax = (GLfloat)(rect->y + rect->h);
slouken@5201
  1094
slouken@5201
  1095
        vertices[0] = xMin;
slouken@5201
  1096
        vertices[1] = yMin;
slouken@5201
  1097
        vertices[2] = xMax;
slouken@5201
  1098
        vertices[3] = yMin;
slouken@5201
  1099
        vertices[4] = xMin;
slouken@5201
  1100
        vertices[5] = yMax;
slouken@5201
  1101
        vertices[6] = xMax;
slouken@5201
  1102
        vertices[7] = yMax;
slouken@6188
  1103
        rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
slouken@6188
  1104
        rdata->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
slouken@5201
  1105
    }
slouken@6188
  1106
    if (rdata->glGetError() != GL_NO_ERROR)
slouken@5201
  1107
    {
slouken@5201
  1108
        SDL_SetError("Failed to render lines");
slouken@5201
  1109
        return -1;
slouken@5201
  1110
    }
slouken@5201
  1111
    return 0;
slouken@5201
  1112
}
slouken@5201
  1113
slouken@5201
  1114
static int
slouken@5201
  1115
GLES2_RenderCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect,
slouken@5201
  1116
                 const SDL_Rect *dstrect)
slouken@5201
  1117
{
slouken@5201
  1118
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
  1119
    GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
slouken@5201
  1120
    GLES2_ImageSource sourceType;
slouken@5201
  1121
    SDL_BlendMode blendMode;
slouken@5201
  1122
    GLfloat vertices[8];
slouken@5201
  1123
    GLfloat texCoords[8];
slouken@5201
  1124
    GLuint locTexture;
slouken@5201
  1125
    GLuint locModulation;
slouken@5201
  1126
slouken@5201
  1127
    GLES2_ActivateRenderer(renderer);
slouken@5201
  1128
slouken@5201
  1129
    /* Activate an appropriate shader and set the projection matrix */
slouken@5201
  1130
    blendMode = texture->blendMode;
slouken@6232
  1131
    if (rdata->renderTarget!=NULL) {
slouken@6232
  1132
        /* Check if we need to do color mapping between the source and render target textures */
slouken@6232
  1133
        if (rdata->renderTarget->format != texture->format) {
slouken@6232
  1134
            switch (texture->format)
slouken@6232
  1135
            {
slouken@6232
  1136
            case SDL_PIXELFORMAT_ABGR8888:
slouken@6232
  1137
                switch (rdata->renderTarget->format)
slouken@6232
  1138
                {
slouken@6232
  1139
                    case SDL_PIXELFORMAT_ARGB8888:
slouken@6232
  1140
                    case SDL_PIXELFORMAT_RGB888:
slouken@6232
  1141
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
slouken@6232
  1142
                        break;
slouken@6232
  1143
                    case SDL_PIXELFORMAT_BGR888:
slouken@6232
  1144
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
slouken@6232
  1145
                        break;
slouken@6232
  1146
                }
slouken@6232
  1147
                break;
slouken@6232
  1148
            case SDL_PIXELFORMAT_ARGB8888:
slouken@6232
  1149
                switch (rdata->renderTarget->format)
slouken@6232
  1150
                {
slouken@6232
  1151
                    case SDL_PIXELFORMAT_ABGR8888:
slouken@6232
  1152
                    case SDL_PIXELFORMAT_BGR888:
slouken@6232
  1153
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
slouken@6232
  1154
                        break;
slouken@6232
  1155
                    case SDL_PIXELFORMAT_RGB888:
slouken@6232
  1156
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
slouken@6232
  1157
                        break;
slouken@6232
  1158
                }
slouken@6232
  1159
                break;
slouken@6232
  1160
            case SDL_PIXELFORMAT_BGR888:
slouken@6232
  1161
                switch (rdata->renderTarget->format)
slouken@6232
  1162
                {
slouken@6232
  1163
                    case SDL_PIXELFORMAT_ABGR8888:
slouken@6232
  1164
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
slouken@6232
  1165
                        break;
slouken@6232
  1166
                    case SDL_PIXELFORMAT_ARGB8888:
slouken@6232
  1167
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_RGB;
slouken@6232
  1168
                        break;
slouken@6232
  1169
                    case SDL_PIXELFORMAT_RGB888:
slouken@6232
  1170
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
slouken@6232
  1171
                        break;
slouken@6232
  1172
                }
slouken@6232
  1173
                break;
slouken@6232
  1174
            case SDL_PIXELFORMAT_RGB888:
slouken@6232
  1175
                switch (rdata->renderTarget->format)
slouken@6232
  1176
                {
slouken@6232
  1177
                    case SDL_PIXELFORMAT_ABGR8888:
slouken@6232
  1178
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
slouken@6232
  1179
                        break;
slouken@6232
  1180
                    case SDL_PIXELFORMAT_ARGB8888:
slouken@6232
  1181
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
slouken@6232
  1182
                        break;
slouken@6232
  1183
                    case SDL_PIXELFORMAT_BGR888:
slouken@6232
  1184
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
slouken@6232
  1185
                        break;
slouken@6232
  1186
                }
slouken@6232
  1187
                break;
slouken@6232
  1188
            }
slouken@6232
  1189
        }
slouken@6232
  1190
        else sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;   // Texture formats match, use the non color mapping shader (even if the formats are not ABGR)
slouken@6232
  1191
    }
slouken@6232
  1192
    else {
slouken@6232
  1193
        switch (texture->format)
slouken@6232
  1194
        {
slouken@6232
  1195
            case SDL_PIXELFORMAT_ABGR8888:
slouken@6232
  1196
                sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
slouken@6232
  1197
                break;
slouken@6232
  1198
            case SDL_PIXELFORMAT_ARGB8888:
slouken@6232
  1199
                sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
slouken@6232
  1200
                break;
slouken@6232
  1201
            case SDL_PIXELFORMAT_BGR888:
slouken@6232
  1202
                sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
slouken@6232
  1203
                break;
slouken@6232
  1204
            case SDL_PIXELFORMAT_RGB888:
slouken@6232
  1205
                sourceType = GLES2_IMAGESOURCE_TEXTURE_RGB;
slouken@6232
  1206
                break;
slouken@6232
  1207
        }
slouken@6113
  1208
    }
slouken@5201
  1209
    if (GLES2_SelectProgram(renderer, sourceType, blendMode) < 0)
slouken@5201
  1210
        return -1;
slouken@5201
  1211
slouken@5201
  1212
    /* Select the target texture */
slouken@5201
  1213
    locTexture = rdata->current_program->uniform_locations[GLES2_UNIFORM_TEXTURE];
slouken@6188
  1214
    rdata->glGetError();
slouken@6188
  1215
    rdata->glActiveTexture(GL_TEXTURE0);
slouken@6188
  1216
    rdata->glBindTexture(tdata->texture_type, tdata->texture);
slouken@6188
  1217
    rdata->glUniform1i(locTexture, 0);
slouken@5201
  1218
slouken@5201
  1219
    /* Configure color modulation */
slouken@5201
  1220
    locModulation = rdata->current_program->uniform_locations[GLES2_UNIFORM_MODULATION];
slouken@6188
  1221
    rdata->glUniform4f(locModulation,
slouken@5224
  1222
                texture->r * inv255f,
slouken@5224
  1223
                texture->g * inv255f,
slouken@5224
  1224
                texture->b * inv255f,
slouken@5355
  1225
                texture->a * inv255f);
slouken@5355
  1226
slouken@5355
  1227
    /* Configure texture blending */
slouken@5355
  1228
    GLES2_SetBlendMode(rdata, blendMode);
slouken@5355
  1229
slouken@5355
  1230
    GLES2_SetTexCoords(rdata, SDL_TRUE);
slouken@5201
  1231
slouken@5201
  1232
    /* Emit the textured quad */
slouken@6232
  1233
    if (rdata->renderTarget!=NULL) {
slouken@6232
  1234
        // Flip the texture vertically to compensate for the inversion it'll be subjected to later when it's rendered to the screen
slouken@6232
  1235
        vertices[0] = (GLfloat)dstrect->x;
slouken@6232
  1236
        vertices[1] = (GLfloat)renderer->viewport.h-dstrect->y;
slouken@6232
  1237
        vertices[2] = (GLfloat)(dstrect->x + dstrect->w);
slouken@6232
  1238
        vertices[3] = (GLfloat)renderer->viewport.h-dstrect->y;
slouken@6232
  1239
        vertices[4] = (GLfloat)dstrect->x;
slouken@6232
  1240
        vertices[5] = (GLfloat)renderer->viewport.h-(dstrect->y + dstrect->h);
slouken@6232
  1241
        vertices[6] = (GLfloat)(dstrect->x + dstrect->w);
slouken@6232
  1242
        vertices[7] = (GLfloat)renderer->viewport.h-(dstrect->y + dstrect->h);
slouken@6232
  1243
    }
slouken@6232
  1244
    else {
slouken@6232
  1245
        vertices[0] = (GLfloat)dstrect->x;
slouken@6232
  1246
        vertices[1] = (GLfloat)dstrect->y;
slouken@6232
  1247
        vertices[2] = (GLfloat)(dstrect->x + dstrect->w);
slouken@6232
  1248
        vertices[3] = (GLfloat)dstrect->y;
slouken@6232
  1249
        vertices[4] = (GLfloat)dstrect->x;
slouken@6232
  1250
        vertices[5] = (GLfloat)(dstrect->y + dstrect->h);
slouken@6232
  1251
        vertices[6] = (GLfloat)(dstrect->x + dstrect->w);
slouken@6232
  1252
        vertices[7] = (GLfloat)(dstrect->y + dstrect->h);
slouken@6232
  1253
    }
slouken@6188
  1254
    rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
slouken@6232
  1255
slouken@5201
  1256
    texCoords[0] = srcrect->x / (GLfloat)texture->w;
slouken@5201
  1257
    texCoords[1] = srcrect->y / (GLfloat)texture->h;
slouken@5201
  1258
    texCoords[2] = (srcrect->x + srcrect->w) / (GLfloat)texture->w;
slouken@5201
  1259
    texCoords[3] = srcrect->y / (GLfloat)texture->h;
slouken@5201
  1260
    texCoords[4] = srcrect->x / (GLfloat)texture->w;
slouken@5201
  1261
    texCoords[5] = (srcrect->y + srcrect->h) / (GLfloat)texture->h;
slouken@5201
  1262
    texCoords[6] = (srcrect->x + srcrect->w) / (GLfloat)texture->w;
slouken@5201
  1263
    texCoords[7] = (srcrect->y + srcrect->h) / (GLfloat)texture->h;
slouken@6188
  1264
    rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, texCoords);
slouken@6188
  1265
    rdata->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
slouken@6188
  1266
    if (rdata->glGetError() != GL_NO_ERROR)
slouken@5201
  1267
    {
slouken@5201
  1268
        SDL_SetError("Failed to render texture");
slouken@5201
  1269
        return -1;
slouken@5201
  1270
    }
slouken@5201
  1271
    return 0;
slouken@5201
  1272
}
slouken@5201
  1273
slouken@6042
  1274
static int
slouken@6042
  1275
GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@6042
  1276
                    Uint32 pixel_format, void * pixels, int pitch)
slouken@6042
  1277
{
slouken@6188
  1278
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@6042
  1279
    SDL_Window *window = renderer->window;
slouken@6042
  1280
    Uint32 temp_format = SDL_PIXELFORMAT_ABGR8888;
slouken@6042
  1281
    void *temp_pixels;
slouken@6042
  1282
    int temp_pitch;
slouken@6042
  1283
    Uint8 *src, *dst, *tmp;
slouken@6042
  1284
    int w, h, length, rows;
slouken@6042
  1285
    int status;
slouken@6042
  1286
slouken@6042
  1287
    GLES2_ActivateRenderer(renderer);
slouken@6042
  1288
slouken@6042
  1289
    temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
slouken@6042
  1290
    temp_pixels = SDL_malloc(rect->h * temp_pitch);
slouken@6042
  1291
    if (!temp_pixels) {
slouken@6042
  1292
        SDL_OutOfMemory();
slouken@6042
  1293
        return -1;
slouken@6042
  1294
    }
slouken@6042
  1295
slouken@6042
  1296
    SDL_GetWindowSize(window, &w, &h);
slouken@6042
  1297
slouken@6188
  1298
    rdata->glPixelStorei(GL_PACK_ALIGNMENT, 1);
slouken@6042
  1299
slouken@6188
  1300
    rdata->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
slouken@6042
  1301
                       GL_RGBA, GL_UNSIGNED_BYTE, temp_pixels);
slouken@6042
  1302
slouken@6042
  1303
    /* Flip the rows to be top-down */
slouken@6042
  1304
    length = rect->w * SDL_BYTESPERPIXEL(temp_format);
slouken@6042
  1305
    src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
slouken@6042
  1306
    dst = (Uint8*)temp_pixels;
slouken@6042
  1307
    tmp = SDL_stack_alloc(Uint8, length);
slouken@6042
  1308
    rows = rect->h / 2;
slouken@6042
  1309
    while (rows--) {
slouken@6042
  1310
        SDL_memcpy(tmp, dst, length);
slouken@6042
  1311
        SDL_memcpy(dst, src, length);
slouken@6042
  1312
        SDL_memcpy(src, tmp, length);
slouken@6042
  1313
        dst += temp_pitch;
slouken@6042
  1314
        src -= temp_pitch;
slouken@6042
  1315
    }
slouken@6042
  1316
    SDL_stack_free(tmp);
slouken@6042
  1317
slouken@6042
  1318
    status = SDL_ConvertPixels(rect->w, rect->h,
slouken@6042
  1319
                               temp_format, temp_pixels, temp_pitch,
slouken@6042
  1320
                               pixel_format, pixels, pitch);
slouken@6042
  1321
    SDL_free(temp_pixels);
slouken@6042
  1322
slouken@6042
  1323
    return status;
slouken@6042
  1324
}
slouken@6042
  1325
slouken@5201
  1326
static void
slouken@5201
  1327
GLES2_RenderPresent(SDL_Renderer *renderer)
slouken@5201
  1328
{
slouken@5201
  1329
    GLES2_ActivateRenderer(renderer);
slouken@5201
  1330
slouken@5201
  1331
    /* Tell the video driver to swap buffers */
slouken@5201
  1332
    SDL_GL_SwapWindow(renderer->window);
slouken@5201
  1333
}
slouken@5201
  1334
slouken@5201
  1335
/*************************************************************************************************
slouken@5201
  1336
 * Renderer instantiation                                                                        *
slouken@5201
  1337
 *************************************************************************************************/
slouken@5201
  1338
slouken@5201
  1339
#define GL_NVIDIA_PLATFORM_BINARY_NV 0x890B
slouken@5201
  1340
slouken@5355
  1341
static void
slouken@5355
  1342
GLES2_ResetState(SDL_Renderer *renderer)
slouken@5355
  1343
{
slouken@5355
  1344
    GLES2_DriverContext *rdata = (GLES2_DriverContext *) renderer->driverdata;
slouken@5355
  1345
slouken@5355
  1346
    if (SDL_CurrentContext == rdata->context) {
slouken@5355
  1347
        GLES2_UpdateViewport(renderer);
slouken@5355
  1348
    } else {
slouken@5355
  1349
        GLES2_ActivateRenderer(renderer);
slouken@5355
  1350
    }
slouken@5355
  1351
slouken@5355
  1352
    rdata->current.blendMode = -1;
slouken@5355
  1353
    rdata->current.tex_coords = SDL_FALSE;
slouken@5355
  1354
slouken@6188
  1355
    rdata->glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
slouken@6188
  1356
    rdata->glDisableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
slouken@5355
  1357
}
slouken@5355
  1358
slouken@6232
  1359
static int
slouken@6232
  1360
GLES2_SetTargetTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@6232
  1361
{
slouken@6232
  1362
    GLES2_DriverContext *data = (GLES2_DriverContext *) renderer->driverdata;
slouken@6232
  1363
    GLES2_TextureData *texturedata = NULL;
slouken@6232
  1364
    GLenum status;
slouken@6232
  1365
    SDL_BlendMode blendMode;
slouken@6232
  1366
slouken@6232
  1367
    if (!renderer) return -1;
slouken@6232
  1368
slouken@6232
  1369
    blendMode = texture->blendMode;
slouken@6232
  1370
    if (texture == NULL) {
slouken@6232
  1371
        if (data->renderTarget!=NULL) {
slouken@6232
  1372
            data->glBindFramebuffer(GL_FRAMEBUFFER, 0);
slouken@6232
  1373
            renderer->viewport = data->viewport_copy;
slouken@6232
  1374
            data->renderTarget = NULL;
slouken@6232
  1375
            data->glViewport(renderer->viewport.x, renderer->viewport.y, renderer->viewport.w, renderer->viewport.h);
slouken@6232
  1376
            if(data->current_program) GLES2_SetOrthographicProjection(renderer);
slouken@6232
  1377
        }
slouken@6232
  1378
        return 0;
slouken@6232
  1379
    }
slouken@6232
  1380
    if (renderer != texture->renderer) return -1;
slouken@6232
  1381
    if (data->renderTarget==NULL) {
slouken@6232
  1382
        // Keep a copy of the default viewport to restore when texture==NULL
slouken@6232
  1383
        data->viewport_copy = renderer->viewport;
slouken@6232
  1384
    }
slouken@6232
  1385
slouken@6232
  1386
    texturedata = (GLES2_TextureData *) texture->driverdata;
slouken@6232
  1387
    if (!texturedata) {
slouken@6232
  1388
        if (texture->native && texture->native->driverdata) {
slouken@6232
  1389
            texture = texture->native;
slouken@6232
  1390
            texturedata = texture->driverdata;
slouken@6232
  1391
        }
slouken@6232
  1392
        else return -1;
slouken@6232
  1393
    }
slouken@6232
  1394
    data->glBindFramebuffer(GL_FRAMEBUFFER, texturedata->fbo->FBO);
slouken@6232
  1395
    /* TODO: check if texture pixel format allows this operation */
slouken@6232
  1396
    data->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texturedata->texture_type, texturedata->texture, 0);
slouken@6232
  1397
    /* Check FBO status */
slouken@6232
  1398
    status = data->glCheckFramebufferStatus(GL_FRAMEBUFFER);
slouken@6232
  1399
    if (status != GL_FRAMEBUFFER_COMPLETE) {
slouken@6232
  1400
        return -1;
slouken@6232
  1401
    }
slouken@6232
  1402
    
slouken@6232
  1403
    renderer->viewport.x = 0;
slouken@6232
  1404
    renderer->viewport.y = 0;
slouken@6232
  1405
    renderer->viewport.w = texture->w;
slouken@6232
  1406
    renderer->viewport.h = texture->h;
slouken@6232
  1407
    data->renderTarget = texture;
slouken@6232
  1408
    data->glViewport(0, 0, texture->w, texture->h);
slouken@6232
  1409
    if(data->current_program) GLES2_SetOrthographicProjection(renderer);
slouken@6232
  1410
    return 0;
slouken@6232
  1411
}
slouken@6232
  1412
slouken@5201
  1413
static SDL_Renderer *
slouken@5201
  1414
GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
slouken@5201
  1415
{
slouken@5201
  1416
    SDL_Renderer *renderer;
slouken@5201
  1417
    GLES2_DriverContext *rdata;
slouken@5201
  1418
    GLint nFormats;
slouken@5201
  1419
#ifndef ZUNE_HD
slouken@5201
  1420
    GLboolean hasCompiler;
slouken@5201
  1421
#endif
slouken@6188
  1422
    Uint32 windowFlags;
slouken@6232
  1423
slouken@6188
  1424
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
slouken@6188
  1425
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
slouken@6232
  1426
slouken@6188
  1427
    windowFlags = SDL_GetWindowFlags(window);
slouken@6188
  1428
    if (!(windowFlags & SDL_WINDOW_OPENGL)) {
slouken@6188
  1429
        if (SDL_RecreateWindow(window, windowFlags | SDL_WINDOW_OPENGL) < 0) {
slouken@6188
  1430
            /* Uh oh, better try to put it back... */
slouken@6188
  1431
            SDL_RecreateWindow(window, windowFlags);
slouken@6188
  1432
            return NULL;
slouken@6188
  1433
        }
slouken@6188
  1434
    }
slouken@5201
  1435
slouken@5201
  1436
    /* Create the renderer struct */
slouken@5201
  1437
    renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(SDL_Renderer));
slouken@5209
  1438
    if (!renderer) {
slouken@5209
  1439
        SDL_OutOfMemory();
slouken@5209
  1440
        return NULL;
slouken@5209
  1441
    }
slouken@5209
  1442
slouken@5201
  1443
    rdata = (GLES2_DriverContext *)SDL_calloc(1, sizeof(GLES2_DriverContext));
slouken@5209
  1444
    if (!rdata) {
slouken@5209
  1445
        GLES2_DestroyRenderer(renderer);
slouken@5201
  1446
        SDL_OutOfMemory();
slouken@5201
  1447
        return NULL;
slouken@5201
  1448
    }
slouken@5201
  1449
    renderer->info = GLES2_RenderDriver.info;
slouken@5297
  1450
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@5201
  1451
    renderer->driverdata = rdata;
slouken@6171
  1452
    renderer->window = window;
slouken@5201
  1453
slouken@5209
  1454
    /* Create an OpenGL ES 2.0 context */
slouken@5201
  1455
    rdata->context = SDL_GL_CreateContext(window);
slouken@5201
  1456
    if (!rdata->context)
slouken@5201
  1457
    {
slouken@5209
  1458
        GLES2_DestroyRenderer(renderer);
slouken@5201
  1459
        return NULL;
slouken@5201
  1460
    }
slouken@5202
  1461
    if (SDL_GL_MakeCurrent(window, rdata->context) < 0) {
slouken@5209
  1462
        GLES2_DestroyRenderer(renderer);
slouken@5202
  1463
        return NULL;
slouken@5202
  1464
    }
slouken@5202
  1465
slouken@6188
  1466
    if (GLES2_LoadFunctions(rdata) < 0) {
slouken@6188
  1467
        GLES2_DestroyRenderer(renderer);
slouken@6188
  1468
        return NULL;
slouken@6188
  1469
    }
slouken@6188
  1470
slouken@5202
  1471
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@5202
  1472
        SDL_GL_SetSwapInterval(1);
slouken@5202
  1473
    } else {
slouken@5202
  1474
        SDL_GL_SetSwapInterval(0);
slouken@5202
  1475
    }
slouken@5202
  1476
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@5202
  1477
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@5202
  1478
    }
slouken@5201
  1479
slouken@5201
  1480
    /* Determine supported shader formats */
slouken@5201
  1481
    /* HACK: glGetInteger is broken on the Zune HD's compositor, so we just hardcode this */
slouken@6188
  1482
    rdata->glGetError();
slouken@5201
  1483
#ifdef ZUNE_HD
slouken@5201
  1484
    nFormats = 1;
slouken@5201
  1485
#else /* !ZUNE_HD */
slouken@6188
  1486
    rdata->glGetIntegerv(GL_NUM_SHADER_BINARY_FORMATS, &nFormats);
slouken@6188
  1487
    rdata->glGetBooleanv(GL_SHADER_COMPILER, &hasCompiler);
slouken@5201
  1488
    if (hasCompiler)
slouken@5201
  1489
        ++nFormats;
slouken@5201
  1490
#endif /* ZUNE_HD */
slouken@5201
  1491
    rdata->shader_formats = (GLenum *)SDL_calloc(nFormats, sizeof(GLenum));
slouken@5201
  1492
    if (!rdata->shader_formats)
slouken@5201
  1493
    {
slouken@5209
  1494
        GLES2_DestroyRenderer(renderer);
slouken@5201
  1495
        SDL_OutOfMemory();
slouken@5201
  1496
        return NULL;
slouken@5201
  1497
    }
slouken@5201
  1498
    rdata->shader_format_count = nFormats;
slouken@5201
  1499
#ifdef ZUNE_HD
slouken@5201
  1500
    rdata->shader_formats[0] = GL_NVIDIA_PLATFORM_BINARY_NV;
slouken@5201
  1501
#else /* !ZUNE_HD */
slouken@6188
  1502
    rdata->glGetIntegerv(GL_SHADER_BINARY_FORMATS, (GLint *)rdata->shader_formats);
slouken@6188
  1503
    if (rdata->glGetError() != GL_NO_ERROR)
slouken@5201
  1504
    {
slouken@5209
  1505
        GLES2_DestroyRenderer(renderer);
slouken@5201
  1506
        SDL_SetError("Failed to query supported shader formats");
slouken@5201
  1507
        return NULL;
slouken@5201
  1508
    }
slouken@5201
  1509
    if (hasCompiler)
slouken@5201
  1510
        rdata->shader_formats[nFormats - 1] = (GLenum)-1;
slouken@5201
  1511
#endif /* ZUNE_HD */
slouken@5201
  1512
slouken@6232
  1513
    rdata->framebuffers = NULL;
slouken@6232
  1514
    rdata->renderTarget = NULL;
slouken@6232
  1515
slouken@5201
  1516
    /* Populate the function pointers for the module */
slouken@5201
  1517
    renderer->WindowEvent         = &GLES2_WindowEvent;
slouken@5201
  1518
    renderer->CreateTexture       = &GLES2_CreateTexture;
slouken@5201
  1519
    renderer->UpdateTexture       = &GLES2_UpdateTexture;
slouken@5201
  1520
    renderer->LockTexture         = &GLES2_LockTexture;
slouken@5201
  1521
    renderer->UnlockTexture       = &GLES2_UnlockTexture;
slouken@5297
  1522
    renderer->UpdateViewport      = &GLES2_UpdateViewport;
slouken@5201
  1523
    renderer->RenderClear         = &GLES2_RenderClear;
slouken@5201
  1524
    renderer->RenderDrawPoints    = &GLES2_RenderDrawPoints;
slouken@5201
  1525
    renderer->RenderDrawLines     = &GLES2_RenderDrawLines;
slouken@5201
  1526
    renderer->RenderFillRects     = &GLES2_RenderFillRects;
slouken@5201
  1527
    renderer->RenderCopy          = &GLES2_RenderCopy;
slouken@6042
  1528
    renderer->RenderReadPixels    = &GLES2_RenderReadPixels;
slouken@5201
  1529
    renderer->RenderPresent       = &GLES2_RenderPresent;
slouken@5201
  1530
    renderer->DestroyTexture      = &GLES2_DestroyTexture;
slouken@5201
  1531
    renderer->DestroyRenderer     = &GLES2_DestroyRenderer;
slouken@6232
  1532
    renderer->SetTargetTexture    = &GLES2_SetTargetTexture;
slouken@5355
  1533
slouken@5355
  1534
    GLES2_ResetState(renderer);
slouken@5355
  1535
slouken@5201
  1536
    return renderer;
slouken@5201
  1537
}
slouken@5201
  1538
slouken@5226
  1539
#endif /* SDL_VIDEO_RENDER_OGL_ES2 && !SDL_RENDER_DISABLED */
slouken@5201
  1540
slouken@5201
  1541
/* vi: set ts=4 sw=4 expandtab: */