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