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