src/render/opengles2/SDL_render_gles2.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 25 Jun 2014 00:58:40 -0700
changeset 8923 b627c37a25e8
parent 8913 d3c44f989e4d
child 8959 9dbfb553c555
permissions -rw-r--r--
Fixed bug 2595 - Padded, non-contiguous YUV does not display correctly using OpenGL ES 2.0 renderer

Sylvain

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