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