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