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