src/render/opengles2/SDL_render_gles2.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 14 Aug 2017 05:51:44 -0700
changeset 11282 180e8906dc3c
parent 10945 1300a3135d61
child 11285 16f51c1376ae
permissions -rw-r--r--
Implemented more flexible blending modes for accelerated renderers

This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA

blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);

This fixes bug 2828 - Subtractive Blending

blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);


This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode

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