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