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