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