src/render/opengles2/SDL_render_gles2.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 18 Dec 2014 00:19:52 -0500
changeset 9278 8900afb78a19
parent 9057 8a1d9a60f9be
child 9299 1d3bee66d518
permissions -rw-r--r--
Initial merge of Emscripten port!

With this commit, you can compile SDL2 with Emscripten
( http://emscripten.org/ ), and make your SDL-based C/C++ program
into a web app.

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