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