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