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