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