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