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