src/render/opengles2/SDL_render_gles2.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 31 Mar 2013 12:48:50 -0400
changeset 7037 3fedf1f25b94
parent 6955 a3ebd92ff0b2
child 7075 f4a3c5a68119
permissions -rw-r--r--
Make SDL_SetError and friends unconditionally return -1.

This lets us change things like this...

if (Failed) {
SDL_SetError("We failed");
return -1;
}

...into this...

if (Failed) {
return SDL_SetError("We failed");
}


Fixes Bugzilla #1778.
slouken@5201
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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,
gabomdq@6320
   115
    GLES2_ATTRIBUTE_TEXCOORD = 1,
gabomdq@6320
   116
    GLES2_ATTRIBUTE_ANGLE = 2,
gabomdq@6320
   117
    GLES2_ATTRIBUTE_CENTER = 3,
slouken@5201
   118
} GLES2_Attribute;
slouken@5201
   119
slouken@5201
   120
typedef enum
slouken@5201
   121
{
slouken@5201
   122
    GLES2_UNIFORM_PROJECTION,
slouken@5201
   123
    GLES2_UNIFORM_TEXTURE,
slouken@5201
   124
    GLES2_UNIFORM_MODULATION,
slouken@5201
   125
    GLES2_UNIFORM_COLOR,
slouken@5201
   126
    GLES2_UNIFORM_COLORTABLE
slouken@5201
   127
} GLES2_Uniform;
slouken@5201
   128
slouken@5201
   129
typedef enum
slouken@5201
   130
{
slouken@5201
   131
    GLES2_IMAGESOURCE_SOLID,
slouken@6113
   132
    GLES2_IMAGESOURCE_TEXTURE_ABGR,
slouken@6113
   133
    GLES2_IMAGESOURCE_TEXTURE_ARGB,
slouken@6113
   134
    GLES2_IMAGESOURCE_TEXTURE_RGB,
slouken@6113
   135
    GLES2_IMAGESOURCE_TEXTURE_BGR
slouken@5201
   136
} GLES2_ImageSource;
slouken@5201
   137
slouken@5201
   138
typedef struct GLES2_DriverContext
slouken@5201
   139
{
slouken@5201
   140
    SDL_GLContext *context;
slouken@5355
   141
    struct {
slouken@5355
   142
        int blendMode;
slouken@5355
   143
        SDL_bool tex_coords;
slouken@5355
   144
    } current;
slouken@5355
   145
slouken@6188
   146
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
slouken@6188
   147
#include "SDL_gles2funcs.h"
slouken@6188
   148
#undef SDL_PROC
slouken@6232
   149
    GLES2_FBOList *framebuffers;
slouken@6271
   150
    GLuint window_framebuffer;
slouken@6188
   151
slouken@5201
   152
    int shader_format_count;
slouken@5201
   153
    GLenum *shader_formats;
slouken@5201
   154
    GLES2_ShaderCache shader_cache;
slouken@5201
   155
    GLES2_ProgramCache program_cache;
slouken@5201
   156
    GLES2_ProgramCacheEntry *current_program;
slouken@5201
   157
} GLES2_DriverContext;
slouken@5201
   158
slouken@5201
   159
#define GLES2_MAX_CACHED_PROGRAMS 8
slouken@5201
   160
slouken@5201
   161
/*************************************************************************************************
slouken@5201
   162
 * Renderer state APIs                                                                           *
slouken@5201
   163
 *************************************************************************************************/
slouken@5201
   164
slouken@5224
   165
static int GLES2_ActivateRenderer(SDL_Renderer *renderer);
slouken@5201
   166
static void GLES2_WindowEvent(SDL_Renderer * renderer,
slouken@5201
   167
                              const SDL_WindowEvent *event);
slouken@5297
   168
static int GLES2_UpdateViewport(SDL_Renderer * renderer);
slouken@5201
   169
static void GLES2_DestroyRenderer(SDL_Renderer *renderer);
slouken@6246
   170
static int GLES2_SetOrthographicProjection(SDL_Renderer *renderer);
slouken@5201
   171
slouken@6232
   172
slouken@5201
   173
static SDL_GLContext SDL_CurrentContext = NULL;
slouken@5201
   174
slouken@6188
   175
static int GLES2_LoadFunctions(GLES2_DriverContext * data)
slouken@6188
   176
{
slouken@6190
   177
#if SDL_VIDEO_DRIVER_UIKIT
slouken@6190
   178
#define __SDL_NOGETPROCADDR__
slouken@6190
   179
#elif SDL_VIDEO_DRIVER_ANDROID
slouken@6190
   180
#define __SDL_NOGETPROCADDR__
slouken@6190
   181
#elif SDL_VIDEO_DRIVER_PANDORA
slouken@6190
   182
#define __SDL_NOGETPROCADDR__
slouken@6190
   183
#endif
slouken@6190
   184
slouken@6190
   185
#if defined __SDL_NOGETPROCADDR__
slouken@6188
   186
#define SDL_PROC(ret,func,params) data->func=func;
slouken@6188
   187
#else
slouken@6188
   188
#define SDL_PROC(ret,func,params) \
slouken@6188
   189
    do { \
slouken@6188
   190
        data->func = SDL_GL_GetProcAddress(#func); \
slouken@6188
   191
        if ( ! data->func ) { \
icculus@7037
   192
            return SDL_SetError("Couldn't load GLES2 function %s: %s\n", #func, SDL_GetError()); \
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@6269
   215
       data->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@6260
   246
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED ||
slouken@6260
   247
        event->event == SDL_WINDOWEVENT_SHOWN ||
slouken@6260
   248
        event->event == SDL_WINDOWEVENT_HIDDEN) {
slouken@5201
   249
        /* Rebind the context to the window area */
slouken@5201
   250
        SDL_CurrentContext = NULL;
slouken@5201
   251
    }
slouken@6060
   252
slouken@6060
   253
    if (event->event == SDL_WINDOWEVENT_MINIMIZED) {
slouken@6060
   254
        /* According to Apple documentation, we need to finish drawing NOW! */
slouken@6188
   255
        rdata->glFinish();
slouken@6060
   256
    }
slouken@5201
   257
}
slouken@5201
   258
slouken@5297
   259
static int
slouken@5297
   260
GLES2_UpdateViewport(SDL_Renderer * renderer)
slouken@5224
   261
{
slouken@5297
   262
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5224
   263
slouken@5297
   264
    if (SDL_CurrentContext != rdata->context) {
slouken@5297
   265
        /* We'll update the viewport after we rebind the context */
slouken@5297
   266
        return 0;
slouken@5297
   267
    }
slouken@5224
   268
slouken@6188
   269
    rdata->glViewport(renderer->viewport.x, renderer->viewport.y,
slouken@5297
   270
               renderer->viewport.w, renderer->viewport.h);
slouken@6268
   271
slouken@6268
   272
    if (rdata->current_program) {
slouken@6268
   273
        GLES2_SetOrthographicProjection(renderer);
slouken@6268
   274
    }
slouken@5297
   275
    return 0;
slouken@5224
   276
}
slouken@5224
   277
slouken@5224
   278
static void
slouken@5201
   279
GLES2_DestroyRenderer(SDL_Renderer *renderer)
slouken@5201
   280
{
slouken@5201
   281
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   282
slouken@5209
   283
    /* Deallocate everything */
slouken@5209
   284
    if (rdata) {
slouken@5209
   285
        GLES2_ActivateRenderer(renderer);
slouken@5201
   286
slouken@6178
   287
        {
slouken@6178
   288
            GLES2_ShaderCacheEntry *entry;
slouken@6178
   289
            GLES2_ShaderCacheEntry *next;
slouken@6178
   290
            entry = rdata->shader_cache.head;
slouken@6178
   291
            while (entry)
slouken@6178
   292
            {
slouken@6188
   293
                rdata->glDeleteShader(entry->id);
slouken@6052
   294
                next = entry->next;
slouken@6052
   295
                SDL_free(entry);
slouken@6052
   296
                entry = next;
slouken@6178
   297
            }
slouken@6178
   298
        }
slouken@6178
   299
        {
slouken@6178
   300
            GLES2_ProgramCacheEntry *entry;
slouken@6178
   301
            GLES2_ProgramCacheEntry *next;
slouken@6052
   302
            entry = rdata->program_cache.head;
slouken@6052
   303
            while (entry) {
slouken@6188
   304
                rdata->glDeleteProgram(entry->id);
slouken@6052
   305
                next = entry->next;
slouken@6052
   306
                SDL_free(entry);
slouken@6052
   307
                entry = next;
slouken@6052
   308
            }
slouken@6178
   309
        }
slouken@5209
   310
        if (rdata->context) {
slouken@6232
   311
            while (rdata->framebuffers) {
slouken@6232
   312
                GLES2_FBOList *nextnode = rdata->framebuffers->next;
slouken@6232
   313
                rdata->glDeleteFramebuffers(1, &rdata->framebuffers->FBO);
slouken@6232
   314
                SDL_free(rdata->framebuffers);
slouken@6232
   315
                rdata->framebuffers = nextnode;
slouken@6232
   316
            }
slouken@5209
   317
            SDL_GL_DeleteContext(rdata->context);
slouken@5209
   318
        }
slouken@5209
   319
        if (rdata->shader_formats) {
slouken@5209
   320
            SDL_free(rdata->shader_formats);
slouken@5209
   321
        }
slouken@5209
   322
        SDL_free(rdata);
slouken@5201
   323
    }
slouken@5201
   324
    SDL_free(renderer);
slouken@5201
   325
}
slouken@5201
   326
slouken@5201
   327
/*************************************************************************************************
slouken@5201
   328
 * Texture APIs                                                                                  *
slouken@5201
   329
 *************************************************************************************************/
slouken@5201
   330
slouken@5201
   331
static int GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture);
slouken@5201
   332
static void GLES2_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture);
slouken@5201
   333
static int GLES2_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
slouken@5201
   334
                             void **pixels, int *pitch);
slouken@5201
   335
static void GLES2_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture);
slouken@5201
   336
static int GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
slouken@5201
   337
                               const void *pixels, int pitch);
slouken@6247
   338
static int GLES2_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@5201
   339
slouken@5484
   340
static GLenum
slouken@5484
   341
GetScaleQuality(void)
slouken@5484
   342
{
slouken@5484
   343
    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
slouken@5484
   344
slouken@5484
   345
    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
slouken@5484
   346
        return GL_NEAREST;
slouken@5484
   347
    } else {
slouken@5484
   348
        return GL_LINEAR;
slouken@5484
   349
    }
slouken@5484
   350
}
slouken@5484
   351
slouken@5201
   352
static int
slouken@5201
   353
GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
slouken@5201
   354
{
slouken@6188
   355
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   356
    GLES2_TextureData *tdata;
slouken@5201
   357
    GLenum format;
slouken@5201
   358
    GLenum type;
slouken@5503
   359
    GLenum scaleMode;
slouken@5201
   360
slouken@5201
   361
    GLES2_ActivateRenderer(renderer);
slouken@5201
   362
slouken@5201
   363
    /* Determine the corresponding GLES texture format params */
slouken@5201
   364
    switch (texture->format)
slouken@5201
   365
    {
slouken@5201
   366
    case SDL_PIXELFORMAT_ABGR8888:
slouken@6113
   367
    case SDL_PIXELFORMAT_ARGB8888:
slouken@6113
   368
    case SDL_PIXELFORMAT_BGR888:
slouken@6113
   369
    case SDL_PIXELFORMAT_RGB888:
slouken@5201
   370
        format = GL_RGBA;
slouken@5201
   371
        type = GL_UNSIGNED_BYTE;
slouken@5201
   372
        break;
slouken@5201
   373
    default:
icculus@7037
   374
        return SDL_SetError("Texture format not supported");
slouken@5201
   375
    }
slouken@5201
   376
slouken@5201
   377
    /* Allocate a texture struct */
slouken@5201
   378
    tdata = (GLES2_TextureData *)SDL_calloc(1, sizeof(GLES2_TextureData));
icculus@7037
   379
    if (!tdata) {
icculus@7037
   380
        return SDL_OutOfMemory();
slouken@5201
   381
    }
slouken@5201
   382
    tdata->texture = 0;
slouken@5201
   383
    tdata->texture_type = GL_TEXTURE_2D;
slouken@5201
   384
    tdata->pixel_format = format;
slouken@5201
   385
    tdata->pixel_type = type;
slouken@5503
   386
    scaleMode = GetScaleQuality();
slouken@5201
   387
slouken@5201
   388
    /* Allocate a blob for image data */
icculus@7037
   389
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5201
   390
        tdata->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
slouken@5402
   391
        tdata->pixel_data = SDL_calloc(1, tdata->pitch * texture->h);
icculus@7037
   392
        if (!tdata->pixel_data) {
slouken@5201
   393
            SDL_free(tdata);
icculus@7037
   394
            return SDL_OutOfMemory();
slouken@5201
   395
        }
slouken@5201
   396
    }
slouken@5201
   397
slouken@5201
   398
    /* Allocate the texture */
slouken@6188
   399
    rdata->glGetError();
slouken@6188
   400
    rdata->glGenTextures(1, &tdata->texture);
slouken@6188
   401
    rdata->glActiveTexture(GL_TEXTURE0);
slouken@6188
   402
    rdata->glBindTexture(tdata->texture_type, tdata->texture);
slouken@6188
   403
    rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_MIN_FILTER, scaleMode);
slouken@6188
   404
    rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_MAG_FILTER, scaleMode);
slouken@6188
   405
    rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
slouken@6188
   406
    rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
slouken@6188
   407
    rdata->glTexImage2D(tdata->texture_type, 0, format, texture->w, texture->h, 0, format, type, NULL);
slouken@6188
   408
    if (rdata->glGetError() != GL_NO_ERROR)
slouken@5201
   409
    {
slouken@6188
   410
        rdata->glDeleteTextures(1, &tdata->texture);
slouken@5201
   411
        SDL_free(tdata);
icculus@7037
   412
        return SDL_SetError("Texture creation failed");
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;
icculus@7037
   492
    if (pitch != srcPitch) {
slouken@5201
   493
        blob = (Uint8 *)SDL_malloc(srcPitch * rect->h);
icculus@7037
   494
        if (!blob) {
icculus@7037
   495
            return SDL_OutOfMemory();
slouken@5201
   496
        }
slouken@5201
   497
        src = blob;
slouken@5201
   498
        for (y = 0; y < rect->h; ++y)
slouken@5201
   499
        {
slouken@5201
   500
            SDL_memcpy(src, pixels, srcPitch);
slouken@5201
   501
            src += srcPitch;
slouken@5201
   502
            pixels = (Uint8 *)pixels + pitch;
slouken@5201
   503
        }
slouken@5201
   504
        src = blob;
slouken@5201
   505
    }
slouken@5201
   506
slouken@5201
   507
    /* Create a texture subimage with the supplied data */
slouken@6188
   508
    rdata->glGetError();
slouken@6188
   509
    rdata->glActiveTexture(GL_TEXTURE0);
slouken@6188
   510
    rdata->glBindTexture(tdata->texture_type, tdata->texture);
slouken@6188
   511
    rdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@6188
   512
    rdata->glTexSubImage2D(tdata->texture_type,
slouken@5201
   513
                    0,
slouken@5201
   514
                    rect->x,
slouken@5201
   515
                    rect->y,
slouken@5201
   516
                    rect->w,
slouken@5201
   517
                    rect->h,
slouken@5201
   518
                    tdata->pixel_format,
slouken@5201
   519
                    tdata->pixel_type,
slouken@5201
   520
                    src);
slouken@5227
   521
    if (blob) {
slouken@5227
   522
        SDL_free(blob);
slouken@5227
   523
    }
slouken@5214
   524
icculus@7037
   525
    if (rdata->glGetError() != GL_NO_ERROR) {
icculus@7037
   526
        return SDL_SetError("Failed to update texture");
slouken@5201
   527
    }
slouken@5201
   528
    return 0;
slouken@5201
   529
}
slouken@5201
   530
slouken@6246
   531
static int
slouken@6247
   532
GLES2_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@6246
   533
{
slouken@6246
   534
    GLES2_DriverContext *data = (GLES2_DriverContext *) renderer->driverdata;
slouken@6246
   535
    GLES2_TextureData *texturedata = NULL;
slouken@6246
   536
    GLenum status;
slouken@6246
   537
slouken@6246
   538
    if (texture == NULL) {
slouken@6271
   539
        data->glBindFramebuffer(GL_FRAMEBUFFER, data->window_framebuffer);
slouken@6246
   540
    } else {
slouken@6246
   541
        texturedata = (GLES2_TextureData *) texture->driverdata;
slouken@6246
   542
        data->glBindFramebuffer(GL_FRAMEBUFFER, texturedata->fbo->FBO);
slouken@6246
   543
        /* TODO: check if texture pixel format allows this operation */
slouken@6246
   544
        data->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texturedata->texture_type, texturedata->texture, 0);
slouken@6246
   545
        /* Check FBO status */
slouken@6246
   546
        status = data->glCheckFramebufferStatus(GL_FRAMEBUFFER);
slouken@6246
   547
        if (status != GL_FRAMEBUFFER_COMPLETE) {
icculus@7037
   548
            return SDL_SetError("glFramebufferTexture2D() failed");
slouken@6246
   549
        }
slouken@6246
   550
    }
slouken@6246
   551
    return 0;
slouken@6246
   552
}
slouken@6246
   553
slouken@5201
   554
/*************************************************************************************************
slouken@5201
   555
 * Shader management functions                                                                   *
slouken@5201
   556
 *************************************************************************************************/
slouken@5201
   557
slouken@5201
   558
static GLES2_ShaderCacheEntry *GLES2_CacheShader(SDL_Renderer *renderer, GLES2_ShaderType type,
slouken@5201
   559
                                                 SDL_BlendMode blendMode);
slouken@5201
   560
static void GLES2_EvictShader(SDL_Renderer *renderer, GLES2_ShaderCacheEntry *entry);
slouken@5201
   561
static GLES2_ProgramCacheEntry *GLES2_CacheProgram(SDL_Renderer *renderer,
slouken@5201
   562
                                                   GLES2_ShaderCacheEntry *vertex,
slouken@5201
   563
                                                   GLES2_ShaderCacheEntry *fragment,
slouken@5201
   564
                                                   SDL_BlendMode blendMode);
slouken@5201
   565
static int GLES2_SelectProgram(SDL_Renderer *renderer, GLES2_ImageSource source,
slouken@5201
   566
                               SDL_BlendMode blendMode);
slouken@5201
   567
slouken@5201
   568
static GLES2_ProgramCacheEntry *
slouken@5201
   569
GLES2_CacheProgram(SDL_Renderer *renderer, GLES2_ShaderCacheEntry *vertex,
slouken@5201
   570
                   GLES2_ShaderCacheEntry *fragment, SDL_BlendMode blendMode)
slouken@5201
   571
{
slouken@5201
   572
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   573
    GLES2_ProgramCacheEntry *entry;
slouken@5201
   574
    GLES2_ShaderCacheEntry *shaderEntry;
slouken@5201
   575
    GLint linkSuccessful;
slouken@5201
   576
slouken@5201
   577
    /* Check if we've already cached this program */
slouken@5201
   578
    entry = rdata->program_cache.head;
slouken@5201
   579
    while (entry)
slouken@5201
   580
    {
slouken@5201
   581
        if (entry->vertex_shader == vertex && entry->fragment_shader == fragment)
slouken@5201
   582
            break;
slouken@5201
   583
        entry = entry->next;
slouken@5201
   584
    }
slouken@5201
   585
    if (entry)
slouken@5201
   586
    {
slouken@6061
   587
        if (rdata->program_cache.head != entry)
slouken@5201
   588
        {
slouken@5201
   589
            if (entry->next)
slouken@5201
   590
                entry->next->prev = entry->prev;
slouken@5201
   591
            if (entry->prev)
slouken@5201
   592
                entry->prev->next = entry->next;
slouken@5201
   593
            entry->prev = NULL;
slouken@5201
   594
            entry->next = rdata->program_cache.head;
slouken@5201
   595
            rdata->program_cache.head->prev = entry;
slouken@5201
   596
            rdata->program_cache.head = entry;
slouken@5201
   597
        }
slouken@5201
   598
        return entry;
slouken@5201
   599
    }
slouken@5201
   600
slouken@5201
   601
    /* Create a program cache entry */
slouken@5201
   602
    entry = (GLES2_ProgramCacheEntry *)SDL_calloc(1, sizeof(GLES2_ProgramCacheEntry));
slouken@5201
   603
    if (!entry)
slouken@5201
   604
    {
slouken@5201
   605
        SDL_OutOfMemory();
slouken@5201
   606
        return NULL;
slouken@5201
   607
    }
slouken@5201
   608
    entry->vertex_shader = vertex;
slouken@5201
   609
    entry->fragment_shader = fragment;
slouken@5201
   610
    entry->blend_mode = blendMode;
slouken@6232
   611
slouken@5201
   612
    /* Create the program and link it */
slouken@6188
   613
    rdata->glGetError();
slouken@6188
   614
    entry->id = rdata->glCreateProgram();
slouken@6188
   615
    rdata->glAttachShader(entry->id, vertex->id);
slouken@6188
   616
    rdata->glAttachShader(entry->id, fragment->id);
slouken@6188
   617
    rdata->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_POSITION, "a_position");
slouken@6188
   618
    rdata->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_TEXCOORD, "a_texCoord");
gabomdq@6320
   619
    rdata->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_ANGLE, "a_angle");
gabomdq@6320
   620
    rdata->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_CENTER, "a_center");
slouken@6188
   621
    rdata->glLinkProgram(entry->id);
slouken@6188
   622
    rdata->glGetProgramiv(entry->id, GL_LINK_STATUS, &linkSuccessful);
slouken@6188
   623
    if (rdata->glGetError() != GL_NO_ERROR || !linkSuccessful)
slouken@5201
   624
    {
slouken@6188
   625
        rdata->glDeleteProgram(entry->id);
slouken@5201
   626
        SDL_free(entry);
icculus@7037
   627
        SDL_SetError("Failed to link shader program");
slouken@5201
   628
        return NULL;
slouken@5201
   629
    }
slouken@6232
   630
slouken@5201
   631
    /* Predetermine locations of uniform variables */
slouken@5201
   632
    entry->uniform_locations[GLES2_UNIFORM_PROJECTION] =
slouken@6188
   633
        rdata->glGetUniformLocation(entry->id, "u_projection");
slouken@5201
   634
    entry->uniform_locations[GLES2_UNIFORM_TEXTURE] =
slouken@6188
   635
        rdata->glGetUniformLocation(entry->id, "u_texture");
slouken@5201
   636
    entry->uniform_locations[GLES2_UNIFORM_MODULATION] =
slouken@6188
   637
        rdata->glGetUniformLocation(entry->id, "u_modulation");
slouken@5201
   638
    entry->uniform_locations[GLES2_UNIFORM_COLOR] =
slouken@6188
   639
        rdata->glGetUniformLocation(entry->id, "u_color");
slouken@5201
   640
    entry->uniform_locations[GLES2_UNIFORM_COLORTABLE] =
slouken@6188
   641
        rdata->glGetUniformLocation(entry->id, "u_colorTable");
slouken@5201
   642
slouken@5201
   643
    /* Cache the linked program */
slouken@5201
   644
    if (rdata->program_cache.head)
slouken@5201
   645
    {
slouken@5201
   646
        entry->next = rdata->program_cache.head;
slouken@5201
   647
        rdata->program_cache.head->prev = entry;
slouken@5201
   648
    }
slouken@5201
   649
    else
slouken@5201
   650
    {
slouken@5201
   651
        rdata->program_cache.tail = entry;
slouken@5201
   652
    }
slouken@5201
   653
    rdata->program_cache.head = entry;
slouken@5201
   654
    ++rdata->program_cache.count;
slouken@5201
   655
slouken@5201
   656
    /* Increment the refcount of the shaders we're using */
slouken@5201
   657
    ++vertex->references;
slouken@5201
   658
    ++fragment->references;
slouken@5201
   659
slouken@5201
   660
    /* Evict the last entry from the cache if we exceed the limit */
slouken@5201
   661
    if (rdata->program_cache.count > GLES2_MAX_CACHED_PROGRAMS)
slouken@5201
   662
    {
slouken@5201
   663
        shaderEntry = rdata->program_cache.tail->vertex_shader;
slouken@5201
   664
        if (--shaderEntry->references <= 0)
slouken@5201
   665
            GLES2_EvictShader(renderer, shaderEntry);
slouken@5201
   666
        shaderEntry = rdata->program_cache.tail->fragment_shader;
slouken@5201
   667
        if (--shaderEntry->references <= 0)
slouken@5201
   668
            GLES2_EvictShader(renderer, shaderEntry);
slouken@6188
   669
        rdata->glDeleteProgram(rdata->program_cache.tail->id);
slouken@5201
   670
        rdata->program_cache.tail = rdata->program_cache.tail->prev;
slouken@5201
   671
        SDL_free(rdata->program_cache.tail->next);
slouken@5201
   672
        rdata->program_cache.tail->next = NULL;
slouken@5201
   673
        --rdata->program_cache.count;
slouken@5201
   674
    }
slouken@5201
   675
    return entry;
slouken@5201
   676
}
slouken@5201
   677
slouken@5201
   678
static GLES2_ShaderCacheEntry *
slouken@5201
   679
GLES2_CacheShader(SDL_Renderer *renderer, GLES2_ShaderType type, SDL_BlendMode blendMode)
slouken@5201
   680
{
slouken@5201
   681
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   682
    const GLES2_Shader *shader;
slouken@5201
   683
    const GLES2_ShaderInstance *instance = NULL;
slouken@5201
   684
    GLES2_ShaderCacheEntry *entry = NULL;
slouken@5201
   685
    GLint compileSuccessful = GL_FALSE;
slouken@5201
   686
    int i, j;
slouken@5201
   687
slouken@5201
   688
    /* Find the corresponding shader */
slouken@5201
   689
    shader = GLES2_GetShader(type, blendMode);
slouken@5201
   690
    if (!shader)
slouken@5201
   691
    {
slouken@5201
   692
        SDL_SetError("No shader matching the requested characteristics was found");
slouken@5201
   693
        return NULL;
slouken@5201
   694
    }
slouken@6232
   695
slouken@5201
   696
    /* Find a matching shader instance that's supported on this hardware */
slouken@5206
   697
    for (i = 0; i < shader->instance_count && !instance; ++i)
slouken@5201
   698
    {
slouken@5206
   699
        for (j = 0; j < rdata->shader_format_count && !instance; ++j)
slouken@5201
   700
        {
slouken@5201
   701
            if (!shader->instances)
slouken@5201
   702
                continue;
slouken@5206
   703
            if (!shader->instances[i])
slouken@5206
   704
                continue;
slouken@5201
   705
            if (shader->instances[i]->format != rdata->shader_formats[j])
slouken@5201
   706
                continue;
slouken@5201
   707
            instance = shader->instances[i];
slouken@5201
   708
        }
slouken@5201
   709
    }
slouken@5201
   710
    if (!instance)
slouken@5201
   711
    {
slouken@5201
   712
        SDL_SetError("The specified shader cannot be loaded on the current platform");
slouken@5201
   713
        return NULL;
slouken@5201
   714
    }
slouken@5201
   715
slouken@5201
   716
    /* Check if we've already cached this shader */
slouken@5201
   717
    entry = rdata->shader_cache.head;
slouken@5201
   718
    while (entry)
slouken@5201
   719
    {
slouken@5201
   720
        if (entry->instance == instance)
slouken@5201
   721
            break;
slouken@5201
   722
        entry = entry->next;
slouken@5201
   723
    }
slouken@5201
   724
    if (entry)
slouken@5201
   725
        return entry;
slouken@5201
   726
slouken@5201
   727
    /* Create a shader cache entry */
slouken@5201
   728
    entry = (GLES2_ShaderCacheEntry *)SDL_calloc(1, sizeof(GLES2_ShaderCacheEntry));
slouken@5201
   729
    if (!entry)
slouken@5201
   730
    {
slouken@5201
   731
        SDL_OutOfMemory();
slouken@5201
   732
        return NULL;
slouken@5201
   733
    }
slouken@5201
   734
    entry->type = type;
slouken@5201
   735
    entry->instance = instance;
slouken@5201
   736
slouken@5201
   737
    /* Compile or load the selected shader instance */
slouken@6188
   738
    rdata->glGetError();
slouken@6188
   739
    entry->id = rdata->glCreateShader(instance->type);
slouken@5201
   740
    if (instance->format == (GLenum)-1)
slouken@5201
   741
    {
slouken@6188
   742
        rdata->glShaderSource(entry->id, 1, (const char **)&instance->data, NULL);
slouken@6188
   743
        rdata->glCompileShader(entry->id);
slouken@6188
   744
        rdata->glGetShaderiv(entry->id, GL_COMPILE_STATUS, &compileSuccessful);
slouken@5201
   745
    }
slouken@5201
   746
    else
slouken@5201
   747
    {
slouken@6188
   748
        rdata->glShaderBinary(1, &entry->id, instance->format, instance->data, instance->length);
slouken@5201
   749
        compileSuccessful = GL_TRUE;
slouken@5201
   750
    }
slouken@6188
   751
    if (rdata->glGetError() != GL_NO_ERROR || !compileSuccessful)
slouken@5201
   752
    {
slouken@5212
   753
        char *info = NULL;
slouken@6113
   754
        int length = 0;
slouken@5212
   755
slouken@6188
   756
        rdata->glGetShaderiv(entry->id, GL_INFO_LOG_LENGTH, &length);
slouken@5212
   757
        if (length > 0) {
slouken@5212
   758
            info = SDL_stack_alloc(char, length);
slouken@5212
   759
            if (info) {
slouken@6188
   760
                rdata->glGetShaderInfoLog(entry->id, length, &length, info);
slouken@5212
   761
            }
slouken@5212
   762
        }
slouken@5212
   763
        if (info) {
slouken@5212
   764
            SDL_SetError("Failed to load the shader: %s", info);
slouken@5212
   765
            SDL_stack_free(info);
slouken@5212
   766
        } else {
slouken@5212
   767
            SDL_SetError("Failed to load the shader");
slouken@5212
   768
        }
slouken@6188
   769
        rdata->glDeleteShader(entry->id);
slouken@5201
   770
        SDL_free(entry);
slouken@5201
   771
        return NULL;
slouken@5201
   772
    }
slouken@5201
   773
slouken@5201
   774
    /* Link the shader entry in at the front of the cache */
slouken@5201
   775
    if (rdata->shader_cache.head)
slouken@5201
   776
    {
slouken@5201
   777
        entry->next = rdata->shader_cache.head;
slouken@5201
   778
        rdata->shader_cache.head->prev = entry;
slouken@5201
   779
    }
slouken@5201
   780
    rdata->shader_cache.head = entry;
slouken@5201
   781
    ++rdata->shader_cache.count;
slouken@5201
   782
    return entry;
slouken@5201
   783
}
slouken@5201
   784
slouken@5201
   785
static void
slouken@5201
   786
GLES2_EvictShader(SDL_Renderer *renderer, GLES2_ShaderCacheEntry *entry)
slouken@5201
   787
{
slouken@5201
   788
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   789
slouken@5201
   790
    /* Unlink the shader from the cache */
slouken@5201
   791
    if (entry->next)
slouken@5201
   792
        entry->next->prev = entry->prev;
slouken@5201
   793
    if (entry->prev)
slouken@5201
   794
        entry->prev->next = entry->next;
slouken@5201
   795
    if (rdata->shader_cache.head == entry)
slouken@5201
   796
        rdata->shader_cache.head = entry->next;
slouken@5201
   797
    --rdata->shader_cache.count;
slouken@5201
   798
slouken@5201
   799
    /* Deallocate the shader */
slouken@6188
   800
    rdata->glDeleteShader(entry->id);
slouken@5201
   801
    SDL_free(entry);
slouken@5201
   802
}
slouken@5201
   803
slouken@5201
   804
static int
slouken@5201
   805
GLES2_SelectProgram(SDL_Renderer *renderer, GLES2_ImageSource source, SDL_BlendMode blendMode)
slouken@5201
   806
{
slouken@5201
   807
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   808
    GLES2_ShaderCacheEntry *vertex = NULL;
slouken@5201
   809
    GLES2_ShaderCacheEntry *fragment = NULL;
slouken@5201
   810
    GLES2_ShaderType vtype, ftype;
slouken@5201
   811
    GLES2_ProgramCacheEntry *program;
slouken@5201
   812
slouken@5201
   813
    /* Select an appropriate shader pair for the specified modes */
slouken@5201
   814
    vtype = GLES2_SHADER_VERTEX_DEFAULT;
slouken@5201
   815
    switch (source)
slouken@5201
   816
    {
slouken@5201
   817
    case GLES2_IMAGESOURCE_SOLID:
slouken@5201
   818
        ftype = GLES2_SHADER_FRAGMENT_SOLID_SRC;
slouken@5201
   819
        break;
slouken@6113
   820
    case GLES2_IMAGESOURCE_TEXTURE_ABGR:
slouken@6113
   821
        ftype = GLES2_SHADER_FRAGMENT_TEXTURE_ABGR_SRC;
slouken@6341
   822
        break;
slouken@6113
   823
    case GLES2_IMAGESOURCE_TEXTURE_ARGB:
slouken@6113
   824
        ftype = GLES2_SHADER_FRAGMENT_TEXTURE_ARGB_SRC;
slouken@5201
   825
        break;
slouken@6113
   826
    case GLES2_IMAGESOURCE_TEXTURE_RGB:
slouken@6113
   827
        ftype = GLES2_SHADER_FRAGMENT_TEXTURE_RGB_SRC;
slouken@6113
   828
        break;
slouken@6113
   829
    case GLES2_IMAGESOURCE_TEXTURE_BGR:
slouken@6113
   830
        ftype = GLES2_SHADER_FRAGMENT_TEXTURE_BGR_SRC;
slouken@6113
   831
        break;
slouken@6113
   832
    default:
slouken@6113
   833
        goto fault;
slouken@5201
   834
    }
slouken@5201
   835
slouken@5201
   836
    /* Load the requested shaders */
slouken@5201
   837
    vertex = GLES2_CacheShader(renderer, vtype, blendMode);
slouken@5201
   838
    if (!vertex)
slouken@5201
   839
        goto fault;
slouken@5201
   840
    fragment = GLES2_CacheShader(renderer, ftype, blendMode);
slouken@5201
   841
    if (!fragment)
slouken@5201
   842
        goto fault;
slouken@5201
   843
slouken@5201
   844
    /* Check if we need to change programs at all */
slouken@5201
   845
    if (rdata->current_program &&
slouken@5201
   846
        rdata->current_program->vertex_shader == vertex &&
slouken@5201
   847
        rdata->current_program->fragment_shader == fragment)
slouken@5201
   848
        return 0;
slouken@5201
   849
slouken@5201
   850
    /* Generate a matching program */
slouken@5201
   851
    program = GLES2_CacheProgram(renderer, vertex, fragment, blendMode);
slouken@5201
   852
    if (!program)
slouken@5201
   853
        goto fault;
slouken@5201
   854
slouken@5201
   855
    /* Select that program in OpenGL */
slouken@6188
   856
    rdata->glGetError();
slouken@6188
   857
    rdata->glUseProgram(program->id);
slouken@6188
   858
    if (rdata->glGetError() != GL_NO_ERROR)
slouken@5201
   859
    {
slouken@5201
   860
        SDL_SetError("Failed to select program");
slouken@5201
   861
        goto fault;
slouken@5201
   862
    }
slouken@5201
   863
slouken@5201
   864
    /* Set the current program */
slouken@5201
   865
    rdata->current_program = program;
slouken@5201
   866
slouken@5201
   867
    /* Activate an orthographic projection */
slouken@5201
   868
    if (GLES2_SetOrthographicProjection(renderer) < 0)
slouken@5201
   869
        goto fault;
slouken@5201
   870
slouken@5201
   871
    /* Clean up and return */
slouken@5201
   872
    return 0;
slouken@5201
   873
fault:
slouken@5201
   874
    if (vertex && vertex->references <= 0)
slouken@5201
   875
        GLES2_EvictShader(renderer, vertex);
slouken@5201
   876
    if (fragment && fragment->references <= 0)
slouken@5201
   877
        GLES2_EvictShader(renderer, fragment);
slouken@5201
   878
    rdata->current_program = NULL;
slouken@5201
   879
    return -1;
slouken@5201
   880
}
slouken@5201
   881
slouken@5201
   882
static int
slouken@5201
   883
GLES2_SetOrthographicProjection(SDL_Renderer *renderer)
slouken@5201
   884
{
slouken@5201
   885
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   886
    GLfloat projection[4][4];
slouken@5201
   887
    GLuint locProjection;
slouken@5201
   888
slouken@5201
   889
    /* Prepare an orthographic projection */
slouken@5297
   890
    projection[0][0] = 2.0f / renderer->viewport.w;
slouken@5201
   891
    projection[0][1] = 0.0f;
slouken@5201
   892
    projection[0][2] = 0.0f;
slouken@5201
   893
    projection[0][3] = 0.0f;
slouken@5201
   894
    projection[1][0] = 0.0f;
slouken@6341
   895
    if (renderer->target) {
slouken@6341
   896
        projection[1][1] = 2.0f / renderer->viewport.h;
slouken@6341
   897
    } else {
slouken@6341
   898
        projection[1][1] = -2.0f / renderer->viewport.h;
slouken@6341
   899
    }
slouken@5201
   900
    projection[1][2] = 0.0f;
slouken@5201
   901
    projection[1][3] = 0.0f;
slouken@5201
   902
    projection[2][0] = 0.0f;
slouken@5201
   903
    projection[2][1] = 0.0f;
slouken@6341
   904
    projection[2][2] = 0.0f;
slouken@5201
   905
    projection[2][3] = 0.0f;
slouken@5201
   906
    projection[3][0] = -1.0f;
slouken@6341
   907
    if (renderer->target) {
slouken@6341
   908
        projection[3][1] = -1.0f;
slouken@6341
   909
    } else {
slouken@6341
   910
        projection[3][1] = 1.0f;
slouken@6341
   911
    }
slouken@5201
   912
    projection[3][2] = 0.0f;
slouken@5201
   913
    projection[3][3] = 1.0f;
slouken@5201
   914
slouken@5201
   915
    /* Set the projection matrix */
slouken@5201
   916
    locProjection = rdata->current_program->uniform_locations[GLES2_UNIFORM_PROJECTION];
slouken@6188
   917
    rdata->glGetError();
slouken@6188
   918
    rdata->glUniformMatrix4fv(locProjection, 1, GL_FALSE, (GLfloat *)projection);
icculus@7037
   919
    if (rdata->glGetError() != GL_NO_ERROR) {
icculus@7037
   920
        return SDL_SetError("Failed to set orthographic projection");
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@6528
   932
static int GLES2_RenderDrawPoints(SDL_Renderer *renderer, const SDL_FPoint *points, int count);
slouken@6528
   933
static int GLES2_RenderDrawLines(SDL_Renderer *renderer, const SDL_FPoint *points, int count);
slouken@6528
   934
static int GLES2_RenderFillRects(SDL_Renderer *renderer, const SDL_FRect *rects, int count);
slouken@5201
   935
static int GLES2_RenderCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect,
slouken@6528
   936
                            const SDL_FRect *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);
gabomdq@6320
   939
static int GLES2_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
   940
                         const SDL_Rect * srcrect, const SDL_FRect * dstrect,
slouken@6528
   941
                         const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip);
slouken@5201
   942
static void GLES2_RenderPresent(SDL_Renderer *renderer);
slouken@5201
   943
slouken@5333
   944
slouken@5201
   945
static int
slouken@5333
   946
GLES2_RenderClear(SDL_Renderer * renderer)
slouken@5201
   947
{
slouken@6188
   948
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@6188
   949
slouken@5201
   950
    GLES2_ActivateRenderer(renderer);
slouken@6232
   951
slouken@6188
   952
    rdata->glClearColor((GLfloat) renderer->r * inv255f,
slouken@5333
   953
                 (GLfloat) renderer->g * inv255f,
slouken@5333
   954
                 (GLfloat) renderer->b * inv255f,
slouken@5333
   955
                 (GLfloat) renderer->a * inv255f);
slouken@5333
   956
slouken@6188
   957
    rdata->glClear(GL_COLOR_BUFFER_BIT);
slouken@5333
   958
slouken@5201
   959
    return 0;
slouken@5201
   960
}
slouken@5201
   961
slouken@5201
   962
static void
slouken@5355
   963
GLES2_SetBlendMode(GLES2_DriverContext *rdata, int blendMode)
slouken@5201
   964
{
slouken@5355
   965
    if (blendMode != rdata->current.blendMode) {
slouken@5355
   966
        switch (blendMode) {
slouken@5355
   967
        default:
slouken@5355
   968
        case SDL_BLENDMODE_NONE:
slouken@6188
   969
            rdata->glDisable(GL_BLEND);
slouken@5355
   970
            break;
slouken@5355
   971
        case SDL_BLENDMODE_BLEND:
slouken@6188
   972
            rdata->glEnable(GL_BLEND);
slouken@6188
   973
            rdata->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@5355
   974
            break;
slouken@5355
   975
        case SDL_BLENDMODE_ADD:
slouken@6188
   976
            rdata->glEnable(GL_BLEND);
slouken@6188
   977
            rdata->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@5355
   978
            break;
slouken@5355
   979
        case SDL_BLENDMODE_MOD:
slouken@6188
   980
            rdata->glEnable(GL_BLEND);
slouken@6188
   981
            rdata->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@5355
   982
            break;
slouken@5355
   983
        }
slouken@5355
   984
        rdata->current.blendMode = blendMode;
slouken@5201
   985
    }
slouken@5201
   986
}
slouken@5201
   987
slouken@5355
   988
static void
slouken@5355
   989
GLES2_SetTexCoords(GLES2_DriverContext * rdata, SDL_bool enabled)
slouken@5355
   990
{
slouken@5355
   991
    if (enabled != rdata->current.tex_coords) {
slouken@5355
   992
        if (enabled) {
slouken@6188
   993
            rdata->glEnableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
slouken@5355
   994
        } else {
slouken@6188
   995
            rdata->glDisableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
slouken@5355
   996
        }
slouken@5355
   997
        rdata->current.tex_coords = enabled;
slouken@5355
   998
    }
slouken@5355
   999
}
slouken@5355
  1000
slouken@5355
  1001
static int
slouken@5355
  1002
GLES2_SetDrawingState(SDL_Renderer * renderer)
slouken@5355
  1003
{
slouken@5355
  1004
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5355
  1005
    int blendMode = renderer->blendMode;
slouken@5355
  1006
    GLuint locColor;
slouken@5355
  1007
slouken@6188
  1008
    rdata->glGetError();
slouken@5355
  1009
slouken@5355
  1010
    GLES2_ActivateRenderer(renderer);
slouken@5355
  1011
slouken@5355
  1012
    GLES2_SetBlendMode(rdata, blendMode);
slouken@5355
  1013
slouken@5355
  1014
    GLES2_SetTexCoords(rdata, SDL_FALSE);
slouken@5355
  1015
slouken@5355
  1016
    /* Activate an appropriate shader and set the projection matrix */
slouken@5355
  1017
    if (GLES2_SelectProgram(renderer, GLES2_IMAGESOURCE_SOLID, blendMode) < 0)
slouken@5355
  1018
        return -1;
slouken@5355
  1019
slouken@5355
  1020
    /* Select the color to draw with */
slouken@5355
  1021
    locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
slouken@6341
  1022
    if (renderer->target &&
slouken@6341
  1023
        (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 ||
slouken@6341
  1024
         renderer->target->format == SDL_PIXELFORMAT_RGB888)) {
slouken@6341
  1025
        rdata->glUniform4f(locColor,
slouken@6341
  1026
                    renderer->b * inv255f,
slouken@6341
  1027
                    renderer->g * inv255f,
slouken@6341
  1028
                    renderer->r * inv255f,
slouken@6341
  1029
                    renderer->a * inv255f);
slouken@6341
  1030
    } else {
slouken@6341
  1031
        rdata->glUniform4f(locColor,
slouken@6341
  1032
                    renderer->r * inv255f,
slouken@6341
  1033
                    renderer->g * inv255f,
slouken@6341
  1034
                    renderer->b * inv255f,
slouken@6341
  1035
                    renderer->a * inv255f);
slouken@6341
  1036
    }
slouken@5355
  1037
    return 0;
slouken@5355
  1038
}
slouken@5355
  1039
slouken@5201
  1040
static int
slouken@6528
  1041
GLES2_RenderDrawPoints(SDL_Renderer *renderer, const SDL_FPoint *points, int count)
slouken@5201
  1042
{
slouken@6188
  1043
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
  1044
    GLfloat *vertices;
slouken@5201
  1045
    int idx;
slouken@5201
  1046
slouken@5355
  1047
    if (GLES2_SetDrawingState(renderer) < 0) {
slouken@5201
  1048
        return -1;
slouken@5355
  1049
    }
slouken@5201
  1050
slouken@5201
  1051
    /* Emit the specified vertices as points */
slouken@5201
  1052
    vertices = SDL_stack_alloc(GLfloat, count * 2);
icculus@7037
  1053
    for (idx = 0; idx < count; ++idx) {
slouken@6528
  1054
        GLfloat x = points[idx].x + 0.5f;
slouken@6528
  1055
        GLfloat y = points[idx].y + 0.5f;
slouken@5201
  1056
slouken@5201
  1057
        vertices[idx * 2] = x;
slouken@5201
  1058
        vertices[(idx * 2) + 1] = y;
slouken@5201
  1059
    }
slouken@6188
  1060
    rdata->glGetError();
slouken@6188
  1061
    rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
slouken@6188
  1062
    rdata->glDrawArrays(GL_POINTS, 0, count);
slouken@5201
  1063
    SDL_stack_free(vertices);
icculus@7037
  1064
    if (rdata->glGetError() != GL_NO_ERROR) {
icculus@7037
  1065
        return SDL_SetError("Failed to render lines");
slouken@5201
  1066
    }
slouken@5201
  1067
    return 0;
slouken@5201
  1068
}
slouken@5201
  1069
slouken@5201
  1070
static int
slouken@6528
  1071
GLES2_RenderDrawLines(SDL_Renderer *renderer, const SDL_FPoint *points, int count)
slouken@5201
  1072
{
slouken@6188
  1073
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
  1074
    GLfloat *vertices;
slouken@5201
  1075
    int idx;
slouken@5201
  1076
slouken@5355
  1077
    if (GLES2_SetDrawingState(renderer) < 0) {
slouken@5201
  1078
        return -1;
slouken@5355
  1079
    }
slouken@5201
  1080
slouken@5201
  1081
    /* Emit a line strip including the specified vertices */
slouken@5201
  1082
    vertices = SDL_stack_alloc(GLfloat, count * 2);
icculus@7037
  1083
    for (idx = 0; idx < count; ++idx) {
slouken@6528
  1084
        GLfloat x = points[idx].x + 0.5f;
slouken@6528
  1085
        GLfloat y = points[idx].y + 0.5f;
slouken@5201
  1086
slouken@5201
  1087
        vertices[idx * 2] = x;
slouken@5201
  1088
        vertices[(idx * 2) + 1] = y;
slouken@5201
  1089
    }
slouken@6188
  1090
    rdata->glGetError();
slouken@6188
  1091
    rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
slouken@6188
  1092
    rdata->glDrawArrays(GL_LINE_STRIP, 0, count);
slouken@6076
  1093
slouken@6076
  1094
    /* We need to close the endpoint of the line */
slouken@6076
  1095
    if (count == 2 ||
slouken@6076
  1096
        points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
slouken@6188
  1097
        rdata->glDrawArrays(GL_POINTS, count-1, 1);
slouken@6076
  1098
    }
slouken@5201
  1099
    SDL_stack_free(vertices);
icculus@7037
  1100
    if (rdata->glGetError() != GL_NO_ERROR) {
icculus@7037
  1101
        return SDL_SetError("Failed to render lines");
slouken@5201
  1102
    }
slouken@5201
  1103
    return 0;
slouken@5201
  1104
}
slouken@5201
  1105
slouken@5201
  1106
static int
slouken@6528
  1107
GLES2_RenderFillRects(SDL_Renderer *renderer, const SDL_FRect *rects, int count)
slouken@5201
  1108
{
slouken@6188
  1109
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
  1110
    GLfloat vertices[8];
slouken@5201
  1111
    int idx;
slouken@5201
  1112
slouken@5355
  1113
    if (GLES2_SetDrawingState(renderer) < 0) {
slouken@5201
  1114
        return -1;
slouken@5355
  1115
    }
slouken@5201
  1116
slouken@5201
  1117
    /* Emit a line loop for each rectangle */
slouken@6188
  1118
    rdata->glGetError();
slouken@5297
  1119
    for (idx = 0; idx < count; ++idx) {
slouken@6528
  1120
        const SDL_FRect *rect = &rects[idx];
slouken@5297
  1121
slouken@6528
  1122
        GLfloat xMin = rect->x;
slouken@6528
  1123
        GLfloat xMax = (rect->x + rect->w);
slouken@6528
  1124
        GLfloat yMin = rect->y;
slouken@6528
  1125
        GLfloat yMax = (rect->y + rect->h);
slouken@5201
  1126
slouken@5201
  1127
        vertices[0] = xMin;
slouken@5201
  1128
        vertices[1] = yMin;
slouken@5201
  1129
        vertices[2] = xMax;
slouken@5201
  1130
        vertices[3] = yMin;
slouken@5201
  1131
        vertices[4] = xMin;
slouken@5201
  1132
        vertices[5] = yMax;
slouken@5201
  1133
        vertices[6] = xMax;
slouken@5201
  1134
        vertices[7] = yMax;
slouken@6188
  1135
        rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
slouken@6188
  1136
        rdata->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
slouken@5201
  1137
    }
icculus@7037
  1138
    if (rdata->glGetError() != GL_NO_ERROR) {
icculus@7037
  1139
        return SDL_SetError("Failed to render lines");
slouken@5201
  1140
    }
slouken@5201
  1141
    return 0;
slouken@5201
  1142
}
slouken@5201
  1143
slouken@5201
  1144
static int
slouken@5201
  1145
GLES2_RenderCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect,
slouken@6528
  1146
                 const SDL_FRect *dstrect)
slouken@5201
  1147
{
slouken@5201
  1148
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
  1149
    GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
slouken@5201
  1150
    GLES2_ImageSource sourceType;
slouken@5201
  1151
    SDL_BlendMode blendMode;
slouken@5201
  1152
    GLfloat vertices[8];
slouken@5201
  1153
    GLfloat texCoords[8];
slouken@5201
  1154
    GLuint locTexture;
slouken@5201
  1155
    GLuint locModulation;
slouken@5201
  1156
slouken@5201
  1157
    GLES2_ActivateRenderer(renderer);
slouken@5201
  1158
slouken@5201
  1159
    /* Activate an appropriate shader and set the projection matrix */
slouken@5201
  1160
    blendMode = texture->blendMode;
slouken@6246
  1161
    if (renderer->target) {
slouken@6232
  1162
        /* Check if we need to do color mapping between the source and render target textures */
slouken@6246
  1163
        if (renderer->target->format != texture->format) {
slouken@6232
  1164
            switch (texture->format)
slouken@6232
  1165
            {
slouken@6232
  1166
            case SDL_PIXELFORMAT_ABGR8888:
slouken@6246
  1167
                switch (renderer->target->format)
slouken@6232
  1168
                {
slouken@6232
  1169
                    case SDL_PIXELFORMAT_ARGB8888:
slouken@6232
  1170
                    case SDL_PIXELFORMAT_RGB888:
slouken@6232
  1171
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
slouken@6232
  1172
                        break;
slouken@6232
  1173
                    case SDL_PIXELFORMAT_BGR888:
slouken@6232
  1174
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
slouken@6232
  1175
                        break;
slouken@6232
  1176
                }
slouken@6232
  1177
                break;
slouken@6232
  1178
            case SDL_PIXELFORMAT_ARGB8888:
slouken@6246
  1179
                switch (renderer->target->format)
slouken@6232
  1180
                {
slouken@6232
  1181
                    case SDL_PIXELFORMAT_ABGR8888:
slouken@6232
  1182
                    case SDL_PIXELFORMAT_BGR888:
slouken@6232
  1183
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
slouken@6232
  1184
                        break;
slouken@6232
  1185
                    case SDL_PIXELFORMAT_RGB888:
slouken@6232
  1186
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
slouken@6232
  1187
                        break;
slouken@6232
  1188
                }
slouken@6232
  1189
                break;
slouken@6232
  1190
            case SDL_PIXELFORMAT_BGR888:
slouken@6246
  1191
                switch (renderer->target->format)
slouken@6232
  1192
                {
slouken@6232
  1193
                    case SDL_PIXELFORMAT_ABGR8888:
slouken@6232
  1194
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
slouken@6232
  1195
                        break;
slouken@6232
  1196
                    case SDL_PIXELFORMAT_ARGB8888:
slouken@6232
  1197
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_RGB;
slouken@6232
  1198
                        break;
slouken@6232
  1199
                    case SDL_PIXELFORMAT_RGB888:
slouken@6232
  1200
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
slouken@6232
  1201
                        break;
slouken@6232
  1202
                }
slouken@6232
  1203
                break;
slouken@6232
  1204
            case SDL_PIXELFORMAT_RGB888:
slouken@6246
  1205
                switch (renderer->target->format)
slouken@6232
  1206
                {
slouken@6232
  1207
                    case SDL_PIXELFORMAT_ABGR8888:
slouken@6232
  1208
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
slouken@6232
  1209
                        break;
slouken@6232
  1210
                    case SDL_PIXELFORMAT_ARGB8888:
slouken@6232
  1211
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
slouken@6232
  1212
                        break;
slouken@6232
  1213
                    case SDL_PIXELFORMAT_BGR888:
slouken@6232
  1214
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
slouken@6232
  1215
                        break;
slouken@6232
  1216
                }
slouken@6232
  1217
                break;
slouken@6232
  1218
            }
slouken@6232
  1219
        }
slouken@6232
  1220
        else sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;   // Texture formats match, use the non color mapping shader (even if the formats are not ABGR)
slouken@6232
  1221
    }
slouken@6232
  1222
    else {
slouken@6232
  1223
        switch (texture->format)
slouken@6232
  1224
        {
slouken@6232
  1225
            case SDL_PIXELFORMAT_ABGR8888:
slouken@6232
  1226
                sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
slouken@6232
  1227
                break;
slouken@6232
  1228
            case SDL_PIXELFORMAT_ARGB8888:
slouken@6232
  1229
                sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
slouken@6232
  1230
                break;
slouken@6232
  1231
            case SDL_PIXELFORMAT_BGR888:
slouken@6232
  1232
                sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
slouken@6232
  1233
                break;
slouken@6232
  1234
            case SDL_PIXELFORMAT_RGB888:
slouken@6232
  1235
                sourceType = GLES2_IMAGESOURCE_TEXTURE_RGB;
slouken@6232
  1236
                break;
slouken@6928
  1237
            default:
slouken@6928
  1238
                return -1;
slouken@6232
  1239
        }
slouken@6113
  1240
    }
slouken@5201
  1241
    if (GLES2_SelectProgram(renderer, sourceType, blendMode) < 0)
slouken@5201
  1242
        return -1;
slouken@5201
  1243
slouken@5201
  1244
    /* Select the target texture */
slouken@5201
  1245
    locTexture = rdata->current_program->uniform_locations[GLES2_UNIFORM_TEXTURE];
slouken@6188
  1246
    rdata->glGetError();
slouken@6341
  1247
    rdata->glActiveTexture(GL_TEXTURE0);
gabomdq@6320
  1248
    rdata->glBindTexture(tdata->texture_type, tdata->texture);
gabomdq@6320
  1249
    rdata->glUniform1i(locTexture, 0);
gabomdq@6320
  1250
gabomdq@6320
  1251
    /* Configure color modulation */
gabomdq@6320
  1252
    locModulation = rdata->current_program->uniform_locations[GLES2_UNIFORM_MODULATION];
slouken@6341
  1253
    if (renderer->target &&
slouken@6341
  1254
        (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 ||
slouken@6341
  1255
         renderer->target->format == SDL_PIXELFORMAT_RGB888)) {
slouken@6341
  1256
        rdata->glUniform4f(locModulation,
slouken@6341
  1257
                    texture->b * inv255f,
slouken@6341
  1258
                    texture->g * inv255f,
slouken@6341
  1259
                    texture->r * inv255f,
slouken@6341
  1260
                    texture->a * inv255f);
slouken@6341
  1261
    } else {
slouken@6341
  1262
        rdata->glUniform4f(locModulation,
slouken@6341
  1263
                    texture->r * inv255f,
slouken@6341
  1264
                    texture->g * inv255f,
slouken@6341
  1265
                    texture->b * inv255f,
slouken@6341
  1266
                    texture->a * inv255f);
slouken@6341
  1267
    }
gabomdq@6320
  1268
gabomdq@6320
  1269
    /* Configure texture blending */
gabomdq@6320
  1270
    GLES2_SetBlendMode(rdata, blendMode);
gabomdq@6320
  1271
gabomdq@6320
  1272
    GLES2_SetTexCoords(rdata, SDL_TRUE);
gabomdq@6320
  1273
gabomdq@6320
  1274
    /* Emit the textured quad */
slouken@6528
  1275
    vertices[0] = dstrect->x;
slouken@6528
  1276
    vertices[1] = dstrect->y;
slouken@6528
  1277
    vertices[2] = (dstrect->x + dstrect->w);
slouken@6528
  1278
    vertices[3] = dstrect->y;
slouken@6528
  1279
    vertices[4] = dstrect->x;
slouken@6528
  1280
    vertices[5] = (dstrect->y + dstrect->h);
slouken@6528
  1281
    vertices[6] = (dstrect->x + dstrect->w);
slouken@6528
  1282
    vertices[7] = (dstrect->y + dstrect->h);
gabomdq@6320
  1283
    rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
gabomdq@6320
  1284
    texCoords[0] = srcrect->x / (GLfloat)texture->w;
gabomdq@6320
  1285
    texCoords[1] = srcrect->y / (GLfloat)texture->h;
gabomdq@6320
  1286
    texCoords[2] = (srcrect->x + srcrect->w) / (GLfloat)texture->w;
gabomdq@6320
  1287
    texCoords[3] = srcrect->y / (GLfloat)texture->h;
gabomdq@6320
  1288
    texCoords[4] = srcrect->x / (GLfloat)texture->w;
gabomdq@6320
  1289
    texCoords[5] = (srcrect->y + srcrect->h) / (GLfloat)texture->h;
gabomdq@6320
  1290
    texCoords[6] = (srcrect->x + srcrect->w) / (GLfloat)texture->w;
gabomdq@6320
  1291
    texCoords[7] = (srcrect->y + srcrect->h) / (GLfloat)texture->h;
gabomdq@6320
  1292
    rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, texCoords);
gabomdq@6320
  1293
    rdata->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
icculus@7037
  1294
    if (rdata->glGetError() != GL_NO_ERROR) {
icculus@7037
  1295
        return SDL_SetError("Failed to render texture");
gabomdq@6320
  1296
    }
gabomdq@6320
  1297
    return 0;
gabomdq@6320
  1298
}
gabomdq@6320
  1299
gabomdq@6320
  1300
static int
gabomdq@6320
  1301
GLES2_RenderCopyEx(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect,
slouken@6528
  1302
                 const SDL_FRect *dstrect, const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
gabomdq@6320
  1303
{
gabomdq@6320
  1304
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
gabomdq@6320
  1305
    GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
gabomdq@6320
  1306
    GLES2_ImageSource sourceType;
gabomdq@6320
  1307
    SDL_BlendMode blendMode;
gabomdq@6320
  1308
    GLfloat vertices[8];
gabomdq@6320
  1309
    GLfloat texCoords[8];
gabomdq@6320
  1310
    GLuint locTexture;
gabomdq@6320
  1311
    GLuint locModulation;
gabomdq@6320
  1312
    GLfloat translate[8];
gabomdq@6320
  1313
    GLfloat fAngle[4];
gabomdq@6320
  1314
    GLfloat tmp;
gabomdq@6320
  1315
gabomdq@6320
  1316
    GLES2_ActivateRenderer(renderer);
gabomdq@6320
  1317
    
gabomdq@6320
  1318
    rdata->glEnableVertexAttribArray(GLES2_ATTRIBUTE_CENTER);
gabomdq@6320
  1319
    rdata->glEnableVertexAttribArray(GLES2_ATTRIBUTE_ANGLE);
slouken@6825
  1320
    fAngle[0] = fAngle[1] = fAngle[2] = fAngle[3] = (GLfloat)(360.0f - angle);
gabomdq@6320
  1321
    /* Calculate the center of rotation */
slouken@6528
  1322
    translate[0] = translate[2] = translate[4] = translate[6] = (center->x + dstrect->x);
slouken@6528
  1323
    translate[1] = translate[3] = translate[5] = translate[7] = (center->y + dstrect->y);
gabomdq@6320
  1324
gabomdq@6320
  1325
    /* Activate an appropriate shader and set the projection matrix */
gabomdq@6320
  1326
    blendMode = texture->blendMode;
gabomdq@6320
  1327
    if (renderer->target) {
gabomdq@6320
  1328
        /* Check if we need to do color mapping between the source and render target textures */
gabomdq@6320
  1329
        if (renderer->target->format != texture->format) {
gabomdq@6320
  1330
            switch (texture->format)
gabomdq@6320
  1331
            {
gabomdq@6320
  1332
            case SDL_PIXELFORMAT_ABGR8888:
gabomdq@6320
  1333
                switch (renderer->target->format)
gabomdq@6320
  1334
                {
gabomdq@6320
  1335
                    case SDL_PIXELFORMAT_ARGB8888:
gabomdq@6320
  1336
                    case SDL_PIXELFORMAT_RGB888:
gabomdq@6320
  1337
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
gabomdq@6320
  1338
                        break;
gabomdq@6320
  1339
                    case SDL_PIXELFORMAT_BGR888:
gabomdq@6320
  1340
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
gabomdq@6320
  1341
                        break;
gabomdq@6320
  1342
                }
gabomdq@6320
  1343
                break;
gabomdq@6320
  1344
            case SDL_PIXELFORMAT_ARGB8888:
gabomdq@6320
  1345
                switch (renderer->target->format)
gabomdq@6320
  1346
                {
gabomdq@6320
  1347
                    case SDL_PIXELFORMAT_ABGR8888:
gabomdq@6320
  1348
                    case SDL_PIXELFORMAT_BGR888:
gabomdq@6320
  1349
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
gabomdq@6320
  1350
                        break;
gabomdq@6320
  1351
                    case SDL_PIXELFORMAT_RGB888:
gabomdq@6320
  1352
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
gabomdq@6320
  1353
                        break;
gabomdq@6320
  1354
                }
gabomdq@6320
  1355
                break;
gabomdq@6320
  1356
            case SDL_PIXELFORMAT_BGR888:
gabomdq@6320
  1357
                switch (renderer->target->format)
gabomdq@6320
  1358
                {
gabomdq@6320
  1359
                    case SDL_PIXELFORMAT_ABGR8888:
gabomdq@6320
  1360
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
gabomdq@6320
  1361
                        break;
gabomdq@6320
  1362
                    case SDL_PIXELFORMAT_ARGB8888:
gabomdq@6320
  1363
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_RGB;
gabomdq@6320
  1364
                        break;
gabomdq@6320
  1365
                    case SDL_PIXELFORMAT_RGB888:
gabomdq@6320
  1366
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
gabomdq@6320
  1367
                        break;
gabomdq@6320
  1368
                }
gabomdq@6320
  1369
                break;
gabomdq@6320
  1370
            case SDL_PIXELFORMAT_RGB888:
gabomdq@6320
  1371
                switch (renderer->target->format)
gabomdq@6320
  1372
                {
gabomdq@6320
  1373
                    case SDL_PIXELFORMAT_ABGR8888:
gabomdq@6320
  1374
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
gabomdq@6320
  1375
                        break;
gabomdq@6320
  1376
                    case SDL_PIXELFORMAT_ARGB8888:
gabomdq@6320
  1377
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
gabomdq@6320
  1378
                        break;
gabomdq@6320
  1379
                    case SDL_PIXELFORMAT_BGR888:
gabomdq@6320
  1380
                        sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
gabomdq@6320
  1381
                        break;
gabomdq@6320
  1382
                }
gabomdq@6320
  1383
                break;
gabomdq@6320
  1384
            }
gabomdq@6320
  1385
        }
gabomdq@6320
  1386
        else sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;   // Texture formats match, use the non color mapping shader (even if the formats are not ABGR)
gabomdq@6320
  1387
    }
gabomdq@6320
  1388
    else {
gabomdq@6320
  1389
        switch (texture->format)
gabomdq@6320
  1390
        {
gabomdq@6320
  1391
            case SDL_PIXELFORMAT_ABGR8888:
gabomdq@6320
  1392
                sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
gabomdq@6320
  1393
                break;
gabomdq@6320
  1394
            case SDL_PIXELFORMAT_ARGB8888:
gabomdq@6320
  1395
                sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
gabomdq@6320
  1396
                break;
gabomdq@6320
  1397
            case SDL_PIXELFORMAT_BGR888:
gabomdq@6320
  1398
                sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
gabomdq@6320
  1399
                break;
gabomdq@6320
  1400
            case SDL_PIXELFORMAT_RGB888:
gabomdq@6320
  1401
                sourceType = GLES2_IMAGESOURCE_TEXTURE_RGB;
gabomdq@6320
  1402
                break;
slouken@6928
  1403
            default:
slouken@6928
  1404
                return -1;
gabomdq@6320
  1405
        }
gabomdq@6320
  1406
    }
gabomdq@6320
  1407
    if (GLES2_SelectProgram(renderer, sourceType, blendMode) < 0)
gabomdq@6320
  1408
        return -1;
gabomdq@6320
  1409
gabomdq@6320
  1410
    /* Select the target texture */
gabomdq@6320
  1411
    locTexture = rdata->current_program->uniform_locations[GLES2_UNIFORM_TEXTURE];
gabomdq@6320
  1412
    rdata->glGetError();
slouken@6188
  1413
    rdata->glActiveTexture(GL_TEXTURE0);
slouken@6188
  1414
    rdata->glBindTexture(tdata->texture_type, tdata->texture);
slouken@6188
  1415
    rdata->glUniform1i(locTexture, 0);
slouken@5201
  1416
slouken@5201
  1417
    /* Configure color modulation */
slouken@5201
  1418
    locModulation = rdata->current_program->uniform_locations[GLES2_UNIFORM_MODULATION];
slouken@6341
  1419
    if (renderer->target &&
slouken@6341
  1420
        (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 ||
slouken@6341
  1421
         renderer->target->format == SDL_PIXELFORMAT_RGB888)) {
slouken@6341
  1422
        rdata->glUniform4f(locModulation,
slouken@6341
  1423
                    texture->b * inv255f,
slouken@6341
  1424
                    texture->g * inv255f,
slouken@6341
  1425
                    texture->r * inv255f,
slouken@6341
  1426
                    texture->a * inv255f);
slouken@6341
  1427
    } else {
slouken@6341
  1428
        rdata->glUniform4f(locModulation,
slouken@6341
  1429
                    texture->r * inv255f,
slouken@6341
  1430
                    texture->g * inv255f,
slouken@6341
  1431
                    texture->b * inv255f,
slouken@6341
  1432
                    texture->a * inv255f);
slouken@6341
  1433
    }
slouken@5355
  1434
slouken@5355
  1435
    /* Configure texture blending */
slouken@5355
  1436
    GLES2_SetBlendMode(rdata, blendMode);
slouken@5355
  1437
slouken@5355
  1438
    GLES2_SetTexCoords(rdata, SDL_TRUE);
slouken@5201
  1439
slouken@5201
  1440
    /* Emit the textured quad */
slouken@6528
  1441
    vertices[0] = dstrect->x;
slouken@6528
  1442
    vertices[1] = dstrect->y;
slouken@6528
  1443
    vertices[2] = (dstrect->x + dstrect->w);
slouken@6528
  1444
    vertices[3] = dstrect->y;
slouken@6528
  1445
    vertices[4] = dstrect->x;
slouken@6528
  1446
    vertices[5] = (dstrect->y + dstrect->h);
slouken@6528
  1447
    vertices[6] = (dstrect->x + dstrect->w);
slouken@6528
  1448
    vertices[7] = (dstrect->y + dstrect->h);
gabomdq@6320
  1449
    if (flip & SDL_FLIP_HORIZONTAL) {
gabomdq@6320
  1450
        tmp = vertices[0];
gabomdq@6320
  1451
        vertices[0] = vertices[4] = vertices[2];
gabomdq@6320
  1452
        vertices[2] = vertices[6] = tmp;
gabomdq@6320
  1453
    }
gabomdq@6320
  1454
    if (flip & SDL_FLIP_VERTICAL) {
gabomdq@6320
  1455
        tmp = vertices[1];
gabomdq@6320
  1456
        vertices[1] = vertices[3] = vertices[5];
gabomdq@6320
  1457
        vertices[5] = vertices[7] = tmp;
gabomdq@6320
  1458
    }
gabomdq@6320
  1459
    
gabomdq@6320
  1460
    rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_ANGLE, 1, GL_FLOAT, GL_FALSE, 0, &fAngle);
gabomdq@6320
  1461
    rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_CENTER, 2, GL_FLOAT, GL_FALSE, 0, translate);
slouken@6188
  1462
    rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
slouken@6232
  1463
slouken@5201
  1464
    texCoords[0] = srcrect->x / (GLfloat)texture->w;
slouken@5201
  1465
    texCoords[1] = srcrect->y / (GLfloat)texture->h;
slouken@5201
  1466
    texCoords[2] = (srcrect->x + srcrect->w) / (GLfloat)texture->w;
slouken@5201
  1467
    texCoords[3] = srcrect->y / (GLfloat)texture->h;
slouken@5201
  1468
    texCoords[4] = srcrect->x / (GLfloat)texture->w;
slouken@5201
  1469
    texCoords[5] = (srcrect->y + srcrect->h) / (GLfloat)texture->h;
slouken@5201
  1470
    texCoords[6] = (srcrect->x + srcrect->w) / (GLfloat)texture->w;
slouken@5201
  1471
    texCoords[7] = (srcrect->y + srcrect->h) / (GLfloat)texture->h;
slouken@6188
  1472
    rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, texCoords);
slouken@6188
  1473
    rdata->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
gabomdq@6320
  1474
    rdata->glDisableVertexAttribArray(GLES2_ATTRIBUTE_CENTER);
gabomdq@6320
  1475
    rdata->glDisableVertexAttribArray(GLES2_ATTRIBUTE_ANGLE);
icculus@7037
  1476
    if (rdata->glGetError() != GL_NO_ERROR) {
icculus@7037
  1477
        return SDL_SetError("Failed to render texture");
slouken@5201
  1478
    }
slouken@5201
  1479
    return 0;
slouken@5201
  1480
}
slouken@5201
  1481
slouken@6042
  1482
static int
slouken@6042
  1483
GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@6042
  1484
                    Uint32 pixel_format, void * pixels, int pitch)
slouken@6042
  1485
{
slouken@6188
  1486
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@6042
  1487
    SDL_Window *window = renderer->window;
slouken@6042
  1488
    Uint32 temp_format = SDL_PIXELFORMAT_ABGR8888;
slouken@6042
  1489
    void *temp_pixels;
slouken@6042
  1490
    int temp_pitch;
slouken@6042
  1491
    Uint8 *src, *dst, *tmp;
slouken@6042
  1492
    int w, h, length, rows;
slouken@6042
  1493
    int status;
slouken@6042
  1494
slouken@6042
  1495
    GLES2_ActivateRenderer(renderer);
slouken@6042
  1496
slouken@6042
  1497
    temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
slouken@6042
  1498
    temp_pixels = SDL_malloc(rect->h * temp_pitch);
slouken@6042
  1499
    if (!temp_pixels) {
icculus@7037
  1500
        return SDL_OutOfMemory();
slouken@6042
  1501
    }
slouken@6042
  1502
slouken@6042
  1503
    SDL_GetWindowSize(window, &w, &h);
slouken@6042
  1504
slouken@6188
  1505
    rdata->glPixelStorei(GL_PACK_ALIGNMENT, 1);
slouken@6042
  1506
slouken@6188
  1507
    rdata->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
slouken@6042
  1508
                       GL_RGBA, GL_UNSIGNED_BYTE, temp_pixels);
slouken@6042
  1509
slouken@6042
  1510
    /* Flip the rows to be top-down */
slouken@6042
  1511
    length = rect->w * SDL_BYTESPERPIXEL(temp_format);
slouken@6042
  1512
    src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
slouken@6042
  1513
    dst = (Uint8*)temp_pixels;
slouken@6042
  1514
    tmp = SDL_stack_alloc(Uint8, length);
slouken@6042
  1515
    rows = rect->h / 2;
slouken@6042
  1516
    while (rows--) {
slouken@6042
  1517
        SDL_memcpy(tmp, dst, length);
slouken@6042
  1518
        SDL_memcpy(dst, src, length);
slouken@6042
  1519
        SDL_memcpy(src, tmp, length);
slouken@6042
  1520
        dst += temp_pitch;
slouken@6042
  1521
        src -= temp_pitch;
slouken@6042
  1522
    }
slouken@6042
  1523
    SDL_stack_free(tmp);
slouken@6042
  1524
slouken@6042
  1525
    status = SDL_ConvertPixels(rect->w, rect->h,
slouken@6042
  1526
                               temp_format, temp_pixels, temp_pitch,
slouken@6042
  1527
                               pixel_format, pixels, pitch);
slouken@6042
  1528
    SDL_free(temp_pixels);
slouken@6042
  1529
slouken@6042
  1530
    return status;
slouken@6042
  1531
}
slouken@6042
  1532
slouken@5201
  1533
static void
slouken@5201
  1534
GLES2_RenderPresent(SDL_Renderer *renderer)
slouken@5201
  1535
{
slouken@5201
  1536
    GLES2_ActivateRenderer(renderer);
slouken@5201
  1537
slouken@5201
  1538
    /* Tell the video driver to swap buffers */
slouken@5201
  1539
    SDL_GL_SwapWindow(renderer->window);
slouken@5201
  1540
}
slouken@5201
  1541
gabomdq@6414
  1542
gabomdq@6414
  1543
/*************************************************************************************************
gabomdq@6414
  1544
 * Bind/unbinding of textures
gabomdq@6414
  1545
 *************************************************************************************************/
gabomdq@6414
  1546
static int GLES2_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh);
gabomdq@6414
  1547
static int GLES2_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture);
gabomdq@6414
  1548
gabomdq@6414
  1549
static int GLES2_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh) {
gabomdq@6414
  1550
    GLES2_DriverContext *data = (GLES2_DriverContext *)renderer->driverdata;
gabomdq@6414
  1551
    GLES2_TextureData *texturedata = (GLES2_TextureData *)texture->driverdata;
gabomdq@6414
  1552
    GLES2_ActivateRenderer(renderer);
gabomdq@6414
  1553
gabomdq@6414
  1554
    data->glBindTexture(texturedata->texture_type, texturedata->texture);
gabomdq@6414
  1555
gabomdq@6414
  1556
    if(texw) *texw = 1.0;
gabomdq@6414
  1557
    if(texh) *texh = 1.0;
gabomdq@6414
  1558
gabomdq@6414
  1559
    return 0;
gabomdq@6414
  1560
}
gabomdq@6414
  1561
gabomdq@6414
  1562
static int GLES2_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture) {
gabomdq@6414
  1563
    GLES2_DriverContext *data = (GLES2_DriverContext *)renderer->driverdata;
gabomdq@6414
  1564
    GLES2_TextureData *texturedata = (GLES2_TextureData *)texture->driverdata;
gabomdq@6414
  1565
    GLES2_ActivateRenderer(renderer);
gabomdq@6414
  1566
gabomdq@6414
  1567
    data->glActiveTexture(GL_TEXTURE0);
gabomdq@6414
  1568
    data->glDisable(texturedata->texture_type);
gabomdq@6414
  1569
gabomdq@6414
  1570
    return 0;
gabomdq@6414
  1571
}
gabomdq@6414
  1572
gabomdq@6414
  1573
slouken@5201
  1574
/*************************************************************************************************
slouken@5201
  1575
 * Renderer instantiation                                                                        *
slouken@5201
  1576
 *************************************************************************************************/
slouken@5201
  1577
slouken@5201
  1578
#define GL_NVIDIA_PLATFORM_BINARY_NV 0x890B
slouken@5201
  1579
slouken@5355
  1580
static void
slouken@5355
  1581
GLES2_ResetState(SDL_Renderer *renderer)
slouken@5355
  1582
{
slouken@5355
  1583
    GLES2_DriverContext *rdata = (GLES2_DriverContext *) renderer->driverdata;
slouken@5355
  1584
slouken@5355
  1585
    if (SDL_CurrentContext == rdata->context) {
slouken@5355
  1586
        GLES2_UpdateViewport(renderer);
slouken@5355
  1587
    } else {
slouken@5355
  1588
        GLES2_ActivateRenderer(renderer);
slouken@5355
  1589
    }
slouken@5355
  1590
slouken@5355
  1591
    rdata->current.blendMode = -1;
slouken@5355
  1592
    rdata->current.tex_coords = SDL_FALSE;
slouken@5355
  1593
slouken@6188
  1594
    rdata->glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
slouken@6188
  1595
    rdata->glDisableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
slouken@5355
  1596
}
slouken@5355
  1597
slouken@5201
  1598
static SDL_Renderer *
slouken@5201
  1599
GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
slouken@5201
  1600
{
slouken@5201
  1601
    SDL_Renderer *renderer;
slouken@5201
  1602
    GLES2_DriverContext *rdata;
slouken@5201
  1603
    GLint nFormats;
slouken@5201
  1604
#ifndef ZUNE_HD
slouken@5201
  1605
    GLboolean hasCompiler;
slouken@5201
  1606
#endif
slouken@6188
  1607
    Uint32 windowFlags;
slouken@6271
  1608
    GLint window_framebuffer;
slouken@6232
  1609
slouken@6371
  1610
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_EGL, 1);
slouken@6188
  1611
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
slouken@6188
  1612
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
slouken@6232
  1613
slouken@6188
  1614
    windowFlags = SDL_GetWindowFlags(window);
slouken@6188
  1615
    if (!(windowFlags & SDL_WINDOW_OPENGL)) {
slouken@6188
  1616
        if (SDL_RecreateWindow(window, windowFlags | SDL_WINDOW_OPENGL) < 0) {
slouken@6188
  1617
            /* Uh oh, better try to put it back... */
slouken@6188
  1618
            SDL_RecreateWindow(window, windowFlags);
slouken@6188
  1619
            return NULL;
slouken@6188
  1620
        }
slouken@6188
  1621
    }
slouken@5201
  1622
slouken@5201
  1623
    /* Create the renderer struct */
slouken@5201
  1624
    renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(SDL_Renderer));
slouken@5209
  1625
    if (!renderer) {
slouken@5209
  1626
        SDL_OutOfMemory();
slouken@5209
  1627
        return NULL;
slouken@5209
  1628
    }
slouken@5209
  1629
slouken@5201
  1630
    rdata = (GLES2_DriverContext *)SDL_calloc(1, sizeof(GLES2_DriverContext));
slouken@5209
  1631
    if (!rdata) {
slouken@5209
  1632
        GLES2_DestroyRenderer(renderer);
slouken@5201
  1633
        SDL_OutOfMemory();
slouken@5201
  1634
        return NULL;
slouken@5201
  1635
    }
slouken@5201
  1636
    renderer->info = GLES2_RenderDriver.info;
slouken@6237
  1637
    renderer->info.flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE;
slouken@5201
  1638
    renderer->driverdata = rdata;
slouken@6171
  1639
    renderer->window = window;
slouken@5201
  1640
slouken@5209
  1641
    /* Create an OpenGL ES 2.0 context */
slouken@5201
  1642
    rdata->context = SDL_GL_CreateContext(window);
slouken@5201
  1643
    if (!rdata->context)
slouken@5201
  1644
    {
slouken@5209
  1645
        GLES2_DestroyRenderer(renderer);
slouken@5201
  1646
        return NULL;
slouken@5201
  1647
    }
slouken@5202
  1648
    if (SDL_GL_MakeCurrent(window, rdata->context) < 0) {
slouken@5209
  1649
        GLES2_DestroyRenderer(renderer);
slouken@5202
  1650
        return NULL;
slouken@5202
  1651
    }
slouken@5202
  1652
slouken@6188
  1653
    if (GLES2_LoadFunctions(rdata) < 0) {
slouken@6188
  1654
        GLES2_DestroyRenderer(renderer);
slouken@6188
  1655
        return NULL;
slouken@6188
  1656
    }
slouken@6188
  1657
slouken@5202
  1658
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@5202
  1659
        SDL_GL_SetSwapInterval(1);
slouken@5202
  1660
    } else {
slouken@5202
  1661
        SDL_GL_SetSwapInterval(0);
slouken@5202
  1662
    }
slouken@5202
  1663
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@5202
  1664
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@5202
  1665
    }
slouken@5201
  1666
slouken@5201
  1667
    /* Determine supported shader formats */
slouken@5201
  1668
    /* HACK: glGetInteger is broken on the Zune HD's compositor, so we just hardcode this */
slouken@6188
  1669
    rdata->glGetError();
slouken@5201
  1670
#ifdef ZUNE_HD
slouken@5201
  1671
    nFormats = 1;
slouken@5201
  1672
#else /* !ZUNE_HD */
slouken@6188
  1673
    rdata->glGetIntegerv(GL_NUM_SHADER_BINARY_FORMATS, &nFormats);
slouken@6188
  1674
    rdata->glGetBooleanv(GL_SHADER_COMPILER, &hasCompiler);
slouken@5201
  1675
    if (hasCompiler)
slouken@5201
  1676
        ++nFormats;
slouken@5201
  1677
#endif /* ZUNE_HD */
slouken@5201
  1678
    rdata->shader_formats = (GLenum *)SDL_calloc(nFormats, sizeof(GLenum));
slouken@5201
  1679
    if (!rdata->shader_formats)
slouken@5201
  1680
    {
slouken@5209
  1681
        GLES2_DestroyRenderer(renderer);
slouken@5201
  1682
        SDL_OutOfMemory();
slouken@5201
  1683
        return NULL;
slouken@5201
  1684
    }
slouken@5201
  1685
    rdata->shader_format_count = nFormats;
slouken@5201
  1686
#ifdef ZUNE_HD
slouken@5201
  1687
    rdata->shader_formats[0] = GL_NVIDIA_PLATFORM_BINARY_NV;
slouken@5201
  1688
#else /* !ZUNE_HD */
slouken@6188
  1689
    rdata->glGetIntegerv(GL_SHADER_BINARY_FORMATS, (GLint *)rdata->shader_formats);
slouken@6188
  1690
    if (rdata->glGetError() != GL_NO_ERROR)
slouken@5201
  1691
    {
slouken@5209
  1692
        GLES2_DestroyRenderer(renderer);
slouken@5201
  1693
        SDL_SetError("Failed to query supported shader formats");
slouken@5201
  1694
        return NULL;
slouken@5201
  1695
    }
slouken@5201
  1696
    if (hasCompiler)
slouken@5201
  1697
        rdata->shader_formats[nFormats - 1] = (GLenum)-1;
slouken@5201
  1698
#endif /* ZUNE_HD */
slouken@5201
  1699
slouken@6232
  1700
    rdata->framebuffers = NULL;
slouken@6271
  1701
    rdata->glGetIntegerv(GL_FRAMEBUFFER_BINDING, &window_framebuffer);
slouken@6271
  1702
    rdata->window_framebuffer = (GLuint)window_framebuffer;
slouken@6232
  1703
slouken@5201
  1704
    /* Populate the function pointers for the module */
slouken@5201
  1705
    renderer->WindowEvent         = &GLES2_WindowEvent;
slouken@5201
  1706
    renderer->CreateTexture       = &GLES2_CreateTexture;
slouken@5201
  1707
    renderer->UpdateTexture       = &GLES2_UpdateTexture;
slouken@5201
  1708
    renderer->LockTexture         = &GLES2_LockTexture;
slouken@5201
  1709
    renderer->UnlockTexture       = &GLES2_UnlockTexture;
slouken@6247
  1710
    renderer->SetRenderTarget     = &GLES2_SetRenderTarget;
slouken@5297
  1711
    renderer->UpdateViewport      = &GLES2_UpdateViewport;
slouken@5201
  1712
    renderer->RenderClear         = &GLES2_RenderClear;
slouken@5201
  1713
    renderer->RenderDrawPoints    = &GLES2_RenderDrawPoints;
slouken@5201
  1714
    renderer->RenderDrawLines     = &GLES2_RenderDrawLines;
slouken@5201
  1715
    renderer->RenderFillRects     = &GLES2_RenderFillRects;
slouken@5201
  1716
    renderer->RenderCopy          = &GLES2_RenderCopy;
slouken@6042
  1717
    renderer->RenderReadPixels    = &GLES2_RenderReadPixels;
gabomdq@6320
  1718
    renderer->RenderCopyEx        = &GLES2_RenderCopyEx;
slouken@5201
  1719
    renderer->RenderPresent       = &GLES2_RenderPresent;
slouken@5201
  1720
    renderer->DestroyTexture      = &GLES2_DestroyTexture;
slouken@5201
  1721
    renderer->DestroyRenderer     = &GLES2_DestroyRenderer;
gabomdq@6414
  1722
    renderer->GL_BindTexture      = &GLES2_BindTexture;
gabomdq@6414
  1723
    renderer->GL_UnbindTexture    = &GLES2_UnbindTexture;
slouken@5355
  1724
slouken@5355
  1725
    GLES2_ResetState(renderer);
slouken@5355
  1726
slouken@5201
  1727
    return renderer;
slouken@5201
  1728
}
slouken@5201
  1729
slouken@5226
  1730
#endif /* SDL_VIDEO_RENDER_OGL_ES2 && !SDL_RENDER_DISABLED */
slouken@5201
  1731
slouken@5201
  1732
/* vi: set ts=4 sw=4 expandtab: */