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