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