src/render/opengles2/SDL_render_gles2.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Feb 2011 13:59:59 -0800
changeset 5297 1800dc39b74c
parent 5276 8e421890cdb8
child 5332 ae8b6fb1feaf
permissions -rw-r--r--
Changed the concept of a render clip rect to a render viewport.
The render viewport is automatically re-centered when the window changes size, so applications that don't care will not have to handle recalculating their rendering coordinates.

Fixed API for drawing and filling multiple rectangles - the parameter should be an array of rects, not an array of pointers to rects.

Fixed API for updating window rects for consistency with other APIs - the order is pointer to array followed by count in array.
slouken@5201
     1
/*
slouken@5201
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
slouken@5201
     4
    Copyright (C) 2010 itsnotabigtruck.
slouken@5201
     5
slouken@5260
     6
    This library is free software; you can redistribute it and/or
slouken@5260
     7
    modify it under the terms of the GNU Lesser General Public
slouken@5260
     8
    License as published by the Free Software Foundation; either
slouken@5260
     9
    version 2.1 of the License, or (at your option) any later version.
slouken@5201
    10
slouken@5260
    11
    This library is distributed in the hope that it will be useful,
slouken@5260
    12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@5260
    13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@5260
    14
    Lesser General Public License for more details.
slouken@5201
    15
slouken@5260
    16
    You should have received a copy of the GNU Lesser General Public
slouken@5260
    17
    License along with this library; if not, write to the Free Software
slouken@5260
    18
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@5260
    19
slouken@5260
    20
    Sam Lantinga
slouken@5260
    21
    slouken@libsdl.org
slouken@5201
    22
*/
slouken@5201
    23
#include "SDL_config.h"
slouken@5201
    24
slouken@5226
    25
#if SDL_VIDEO_RENDER_OGL_ES2 && !SDL_RENDER_DISABLED
slouken@5201
    26
slouken@5204
    27
#include "SDL_opengles2.h"
slouken@5201
    28
#include "../SDL_sysrender.h"
slouken@5201
    29
#include "SDL_shaders_gles2.h"
slouken@5201
    30
slouken@5201
    31
/*************************************************************************************************
slouken@5201
    32
 * Bootstrap data                                                                                *
slouken@5201
    33
 *************************************************************************************************/
slouken@5201
    34
slouken@5201
    35
static SDL_Renderer *GLES2_CreateRenderer(SDL_Window *window, Uint32 flags);
slouken@5201
    36
slouken@5201
    37
SDL_RenderDriver GLES2_RenderDriver = {
slouken@5201
    38
    GLES2_CreateRenderer,
slouken@5201
    39
    {
slouken@5201
    40
        "opengles2",
slouken@5201
    41
        (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
slouken@5202
    42
        1,
slouken@5202
    43
        {SDL_PIXELFORMAT_ABGR8888},
slouken@5201
    44
        0,
slouken@5201
    45
        0
slouken@5201
    46
    }
slouken@5201
    47
};
slouken@5201
    48
slouken@5201
    49
/*************************************************************************************************
slouken@5201
    50
 * Context structures                                                                            *
slouken@5201
    51
 *************************************************************************************************/
slouken@5201
    52
slouken@5201
    53
typedef struct GLES2_TextureData
slouken@5201
    54
{
slouken@5201
    55
    GLenum texture;
slouken@5201
    56
    GLenum texture_type;
slouken@5201
    57
    GLenum pixel_format;
slouken@5201
    58
    GLenum pixel_type;
slouken@5201
    59
    void *pixel_data;
slouken@5201
    60
    size_t pitch;
slouken@5201
    61
} GLES2_TextureData;
slouken@5201
    62
slouken@5201
    63
typedef struct GLES2_ShaderCacheEntry
slouken@5201
    64
{
slouken@5201
    65
    GLuint id;
slouken@5201
    66
    GLES2_ShaderType type;
slouken@5201
    67
    const GLES2_ShaderInstance *instance;
slouken@5201
    68
    int references;
slouken@5201
    69
    struct GLES2_ShaderCacheEntry *prev;
slouken@5201
    70
    struct GLES2_ShaderCacheEntry *next;
slouken@5201
    71
} GLES2_ShaderCacheEntry;
slouken@5201
    72
slouken@5201
    73
typedef struct GLES2_ShaderCache
slouken@5201
    74
{
slouken@5201
    75
    int count;
slouken@5201
    76
    GLES2_ShaderCacheEntry *head;
slouken@5201
    77
} GLES2_ShaderCache;
slouken@5201
    78
slouken@5201
    79
typedef struct GLES2_ProgramCacheEntry
slouken@5201
    80
{
slouken@5201
    81
    GLuint id;
slouken@5201
    82
    SDL_BlendMode blend_mode;
slouken@5201
    83
    GLES2_ShaderCacheEntry *vertex_shader;
slouken@5201
    84
    GLES2_ShaderCacheEntry *fragment_shader;
slouken@5201
    85
    GLuint uniform_locations[16];
slouken@5201
    86
    struct GLES2_ProgramCacheEntry *prev;
slouken@5201
    87
    struct GLES2_ProgramCacheEntry *next;
slouken@5201
    88
} GLES2_ProgramCacheEntry;
slouken@5201
    89
slouken@5201
    90
typedef struct GLES2_ProgramCache
slouken@5201
    91
{
slouken@5201
    92
    int count;
slouken@5201
    93
    GLES2_ProgramCacheEntry *head;
slouken@5201
    94
    GLES2_ProgramCacheEntry *tail;
slouken@5201
    95
} GLES2_ProgramCache;
slouken@5201
    96
slouken@5201
    97
typedef enum
slouken@5201
    98
{
slouken@5201
    99
    GLES2_ATTRIBUTE_POSITION = 0,
slouken@5201
   100
    GLES2_ATTRIBUTE_TEXCOORD = 1
slouken@5201
   101
} GLES2_Attribute;
slouken@5201
   102
slouken@5201
   103
typedef enum
slouken@5201
   104
{
slouken@5201
   105
    GLES2_UNIFORM_PROJECTION,
slouken@5201
   106
    GLES2_UNIFORM_TEXTURE,
slouken@5201
   107
    GLES2_UNIFORM_MODULATION,
slouken@5201
   108
    GLES2_UNIFORM_COLOR,
slouken@5201
   109
    GLES2_UNIFORM_COLORTABLE
slouken@5201
   110
} GLES2_Uniform;
slouken@5201
   111
slouken@5201
   112
typedef enum
slouken@5201
   113
{
slouken@5201
   114
    GLES2_IMAGESOURCE_SOLID,
slouken@5201
   115
    GLES2_IMAGESOURCE_TEXTURE
slouken@5201
   116
} GLES2_ImageSource;
slouken@5201
   117
slouken@5201
   118
typedef struct GLES2_DriverContext
slouken@5201
   119
{
slouken@5201
   120
    SDL_GLContext *context;
slouken@5201
   121
    int shader_format_count;
slouken@5201
   122
    GLenum *shader_formats;
slouken@5201
   123
    GLES2_ShaderCache shader_cache;
slouken@5201
   124
    GLES2_ProgramCache program_cache;
slouken@5201
   125
    GLES2_ProgramCacheEntry *current_program;
slouken@5201
   126
} GLES2_DriverContext;
slouken@5201
   127
slouken@5201
   128
#define GLES2_MAX_CACHED_PROGRAMS 8
slouken@5201
   129
slouken@5201
   130
/*************************************************************************************************
slouken@5201
   131
 * Renderer state APIs                                                                           *
slouken@5201
   132
 *************************************************************************************************/
slouken@5201
   133
slouken@5224
   134
static int GLES2_ActivateRenderer(SDL_Renderer *renderer);
slouken@5201
   135
static void GLES2_WindowEvent(SDL_Renderer * renderer,
slouken@5201
   136
                              const SDL_WindowEvent *event);
slouken@5297
   137
static int GLES2_UpdateViewport(SDL_Renderer * renderer);
slouken@5201
   138
static void GLES2_DestroyRenderer(SDL_Renderer *renderer);
slouken@5201
   139
slouken@5201
   140
static SDL_GLContext SDL_CurrentContext = NULL;
slouken@5201
   141
slouken@5201
   142
static int
slouken@5201
   143
GLES2_ActivateRenderer(SDL_Renderer * renderer)
slouken@5201
   144
{
slouken@5201
   145
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   146
slouken@5201
   147
    if (SDL_CurrentContext != rdata->context) {
slouken@5201
   148
        /* Null out the current program to ensure we set it again */
slouken@5201
   149
        rdata->current_program = NULL;
slouken@5201
   150
slouken@5297
   151
        if (SDL_GL_MakeCurrent(renderer->window, rdata->context) < 0) {
slouken@5201
   152
            return -1;
slouken@5201
   153
        }
slouken@5201
   154
        SDL_CurrentContext = rdata->context;
slouken@5201
   155
slouken@5297
   156
        GLES2_UpdateViewport(renderer);
slouken@5201
   157
    }
slouken@5201
   158
    return 0;
slouken@5201
   159
}
slouken@5201
   160
slouken@5201
   161
static void
slouken@5201
   162
GLES2_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@5201
   163
{
slouken@5201
   164
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   165
slouken@5276
   166
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@5201
   167
        /* Rebind the context to the window area */
slouken@5201
   168
        SDL_CurrentContext = NULL;
slouken@5201
   169
    }
slouken@5201
   170
}
slouken@5201
   171
slouken@5297
   172
static int
slouken@5297
   173
GLES2_UpdateViewport(SDL_Renderer * renderer)
slouken@5224
   174
{
slouken@5297
   175
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5224
   176
slouken@5297
   177
    if (SDL_CurrentContext != rdata->context) {
slouken@5297
   178
        /* We'll update the viewport after we rebind the context */
slouken@5297
   179
        return 0;
slouken@5297
   180
    }
slouken@5224
   181
slouken@5297
   182
    glViewport(renderer->viewport.x, renderer->viewport.y,
slouken@5297
   183
               renderer->viewport.w, renderer->viewport.h);
slouken@5297
   184
    return 0;
slouken@5224
   185
}
slouken@5224
   186
slouken@5224
   187
static void
slouken@5201
   188
GLES2_DestroyRenderer(SDL_Renderer *renderer)
slouken@5201
   189
{
slouken@5201
   190
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   191
    GLES2_ProgramCacheEntry *entry;
slouken@5201
   192
    GLES2_ProgramCacheEntry *next;
slouken@5201
   193
slouken@5209
   194
    /* Deallocate everything */
slouken@5209
   195
    if (rdata) {
slouken@5209
   196
        GLES2_ActivateRenderer(renderer);
slouken@5201
   197
slouken@5209
   198
        entry = rdata->program_cache.head;
slouken@5209
   199
        while (entry) {
slouken@5209
   200
            glDeleteShader(entry->vertex_shader->id);
slouken@5209
   201
            glDeleteShader(entry->fragment_shader->id);
slouken@5209
   202
            SDL_free(entry->vertex_shader);
slouken@5209
   203
            SDL_free(entry->fragment_shader);
slouken@5209
   204
            glDeleteProgram(entry->id);
slouken@5209
   205
            next = entry->next;
slouken@5209
   206
            SDL_free(entry);
slouken@5209
   207
            entry = next;
slouken@5209
   208
        }
slouken@5209
   209
        if (rdata->context) {
slouken@5209
   210
            SDL_GL_DeleteContext(rdata->context);
slouken@5209
   211
        }
slouken@5209
   212
        if (rdata->shader_formats) {
slouken@5209
   213
            SDL_free(rdata->shader_formats);
slouken@5209
   214
        }
slouken@5209
   215
        SDL_free(rdata);
slouken@5201
   216
    }
slouken@5201
   217
    SDL_free(renderer);
slouken@5201
   218
}
slouken@5201
   219
slouken@5201
   220
/*************************************************************************************************
slouken@5201
   221
 * Texture APIs                                                                                  *
slouken@5201
   222
 *************************************************************************************************/
slouken@5201
   223
slouken@5201
   224
static int GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture);
slouken@5201
   225
static void GLES2_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture);
slouken@5201
   226
static int GLES2_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
slouken@5201
   227
                             void **pixels, int *pitch);
slouken@5201
   228
static void GLES2_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture);
slouken@5201
   229
static int GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
slouken@5201
   230
                               const void *pixels, int pitch);
slouken@5201
   231
slouken@5201
   232
static int
slouken@5201
   233
GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
slouken@5201
   234
{
slouken@5201
   235
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   236
    GLES2_TextureData *tdata;
slouken@5201
   237
    GLenum format;
slouken@5201
   238
    GLenum type;
slouken@5201
   239
slouken@5201
   240
    GLES2_ActivateRenderer(renderer);
slouken@5201
   241
slouken@5201
   242
    /* Determine the corresponding GLES texture format params */
slouken@5201
   243
    switch (texture->format)
slouken@5201
   244
    {
slouken@5201
   245
    case SDL_PIXELFORMAT_ABGR8888:
slouken@5201
   246
        format = GL_RGBA;
slouken@5201
   247
        type = GL_UNSIGNED_BYTE;
slouken@5201
   248
        break;
slouken@5201
   249
    default:
slouken@5201
   250
        SDL_SetError("Texture format not supported");
slouken@5201
   251
        return -1;
slouken@5201
   252
    }
slouken@5201
   253
slouken@5201
   254
    /* Allocate a texture struct */
slouken@5201
   255
    tdata = (GLES2_TextureData *)SDL_calloc(1, sizeof(GLES2_TextureData));
slouken@5201
   256
    if (!tdata)
slouken@5201
   257
    {
slouken@5201
   258
        SDL_OutOfMemory();
slouken@5201
   259
        return -1;
slouken@5201
   260
    }
slouken@5201
   261
    tdata->texture = 0;
slouken@5201
   262
    tdata->texture_type = GL_TEXTURE_2D;
slouken@5201
   263
    tdata->pixel_format = format;
slouken@5201
   264
    tdata->pixel_type = type;
slouken@5201
   265
slouken@5201
   266
    /* Allocate a blob for image data */
slouken@5201
   267
    if (texture->access == SDL_TEXTUREACCESS_STREAMING)
slouken@5201
   268
    {
slouken@5201
   269
        tdata->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
slouken@5201
   270
        tdata->pixel_data = SDL_malloc(tdata->pitch * texture->h);
slouken@5201
   271
        if (!tdata->pixel_data)
slouken@5201
   272
        {
slouken@5201
   273
            SDL_OutOfMemory();
slouken@5201
   274
            SDL_free(tdata);
slouken@5201
   275
            return -1;
slouken@5201
   276
        }
slouken@5201
   277
    }
slouken@5201
   278
slouken@5201
   279
    /* Allocate the texture */
slouken@5201
   280
    glGetError();
slouken@5201
   281
    glGenTextures(1, &tdata->texture);
slouken@5201
   282
    glActiveTexture(GL_TEXTURE0);
slouken@5201
   283
    glBindTexture(tdata->texture_type, tdata->texture);
slouken@5201
   284
    glTexParameteri(tdata->texture_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
slouken@5201
   285
    glTexParameteri(tdata->texture_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
slouken@5201
   286
    glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
slouken@5201
   287
    glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
slouken@5201
   288
    glTexImage2D(tdata->texture_type, 0, format, texture->w, texture->h, 0, format, type, NULL);
slouken@5201
   289
    if (glGetError() != GL_NO_ERROR)
slouken@5201
   290
    {
slouken@5201
   291
        SDL_SetError("Texture creation failed");
slouken@5201
   292
        glDeleteTextures(1, &tdata->texture);
slouken@5201
   293
        SDL_free(tdata);
slouken@5201
   294
        return -1;
slouken@5201
   295
    }
slouken@5201
   296
    texture->driverdata = tdata;
slouken@5201
   297
    return 0;
slouken@5201
   298
}
slouken@5201
   299
slouken@5201
   300
static void
slouken@5201
   301
GLES2_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture)
slouken@5201
   302
{
slouken@5201
   303
    GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
slouken@5201
   304
slouken@5201
   305
    GLES2_ActivateRenderer(renderer);
slouken@5201
   306
slouken@5201
   307
    /* Destroy the texture */
slouken@5201
   308
    if (tdata)
slouken@5201
   309
    {
slouken@5201
   310
        glDeleteTextures(1, &tdata->texture);
slouken@5201
   311
        SDL_free(tdata->pixel_data);
slouken@5201
   312
        SDL_free(tdata);
slouken@5201
   313
        texture->driverdata = NULL;
slouken@5201
   314
    }
slouken@5201
   315
}
slouken@5201
   316
slouken@5201
   317
static int
slouken@5201
   318
GLES2_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
slouken@5201
   319
                  void **pixels, int *pitch)
slouken@5201
   320
{
slouken@5201
   321
    GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
slouken@5201
   322
slouken@5201
   323
    /* Retrieve the buffer/pitch for the specified region */
slouken@5201
   324
    *pixels = (Uint8 *)tdata->pixel_data +
slouken@5201
   325
              (tdata->pitch * rect->y) +
slouken@5201
   326
              (rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5201
   327
    *pitch = tdata->pitch;
slouken@5201
   328
slouken@5201
   329
    return 0;
slouken@5201
   330
}
slouken@5201
   331
slouken@5201
   332
static void
slouken@5201
   333
GLES2_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture)
slouken@5201
   334
{
slouken@5201
   335
    GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
slouken@5227
   336
    SDL_Rect rect;
slouken@5201
   337
slouken@5227
   338
    /* We do whole texture updates, at least for now */
slouken@5227
   339
    rect.x = 0;
slouken@5227
   340
    rect.y = 0;
slouken@5227
   341
    rect.w = texture->w;
slouken@5227
   342
    rect.h = texture->h;
slouken@5227
   343
    GLES2_UpdateTexture(renderer, texture, &rect, tdata->pixel_data, tdata->pitch);
slouken@5201
   344
}
slouken@5201
   345
slouken@5201
   346
static int
slouken@5201
   347
GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
slouken@5201
   348
                    const void *pixels, int pitch)
slouken@5201
   349
{
slouken@5201
   350
    GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
slouken@5201
   351
    Uint8 *blob = NULL;
slouken@5201
   352
    Uint8 *src;
slouken@5201
   353
    int srcPitch;
slouken@5201
   354
    int y;
slouken@5201
   355
slouken@5201
   356
    GLES2_ActivateRenderer(renderer);
slouken@5201
   357
slouken@5201
   358
    /* Bail out if we're supposed to update an empty rectangle */
slouken@5201
   359
    if (rect->w <= 0 || rect->h <= 0)
slouken@5201
   360
        return 0;
slouken@5201
   361
slouken@5201
   362
    /* Reformat the texture data into a tightly packed array */
slouken@5201
   363
    srcPitch = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@5201
   364
    src = (Uint8 *)pixels;
slouken@5201
   365
    if (pitch != srcPitch)
slouken@5201
   366
    {
slouken@5201
   367
        blob = (Uint8 *)SDL_malloc(srcPitch * rect->h);
slouken@5201
   368
        if (!blob)
slouken@5201
   369
        {
slouken@5201
   370
            SDL_OutOfMemory();
slouken@5201
   371
            return -1;
slouken@5201
   372
        }
slouken@5201
   373
        src = blob;
slouken@5201
   374
        for (y = 0; y < rect->h; ++y)
slouken@5201
   375
        {
slouken@5201
   376
            SDL_memcpy(src, pixels, srcPitch);
slouken@5201
   377
            src += srcPitch;
slouken@5201
   378
            pixels = (Uint8 *)pixels + pitch;
slouken@5201
   379
        }
slouken@5201
   380
        src = blob;
slouken@5201
   381
    }
slouken@5201
   382
slouken@5201
   383
    /* Create a texture subimage with the supplied data */
slouken@5201
   384
    glGetError();
slouken@5201
   385
    glActiveTexture(GL_TEXTURE0);
slouken@5201
   386
    glBindTexture(tdata->texture_type, tdata->texture);
slouken@5201
   387
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@5201
   388
    glTexSubImage2D(tdata->texture_type,
slouken@5201
   389
                    0,
slouken@5201
   390
                    rect->x,
slouken@5201
   391
                    rect->y,
slouken@5201
   392
                    rect->w,
slouken@5201
   393
                    rect->h,
slouken@5201
   394
                    tdata->pixel_format,
slouken@5201
   395
                    tdata->pixel_type,
slouken@5201
   396
                    src);
slouken@5227
   397
    if (blob) {
slouken@5227
   398
        SDL_free(blob);
slouken@5227
   399
    }
slouken@5214
   400
slouken@5201
   401
    if (glGetError() != GL_NO_ERROR)
slouken@5201
   402
    {
slouken@5201
   403
        SDL_SetError("Failed to update texture");
slouken@5201
   404
        return -1;
slouken@5201
   405
    }
slouken@5201
   406
    return 0;
slouken@5201
   407
}
slouken@5201
   408
slouken@5201
   409
/*************************************************************************************************
slouken@5201
   410
 * Shader management functions                                                                   *
slouken@5201
   411
 *************************************************************************************************/
slouken@5201
   412
slouken@5201
   413
static GLES2_ShaderCacheEntry *GLES2_CacheShader(SDL_Renderer *renderer, GLES2_ShaderType type,
slouken@5201
   414
                                                 SDL_BlendMode blendMode);
slouken@5201
   415
static void GLES2_EvictShader(SDL_Renderer *renderer, GLES2_ShaderCacheEntry *entry);
slouken@5201
   416
static GLES2_ProgramCacheEntry *GLES2_CacheProgram(SDL_Renderer *renderer,
slouken@5201
   417
                                                   GLES2_ShaderCacheEntry *vertex,
slouken@5201
   418
                                                   GLES2_ShaderCacheEntry *fragment,
slouken@5201
   419
                                                   SDL_BlendMode blendMode);
slouken@5201
   420
static int GLES2_SelectProgram(SDL_Renderer *renderer, GLES2_ImageSource source,
slouken@5201
   421
                               SDL_BlendMode blendMode);
slouken@5201
   422
static int GLES2_SetOrthographicProjection(SDL_Renderer *renderer);
slouken@5201
   423
slouken@5201
   424
static GLES2_ProgramCacheEntry *
slouken@5201
   425
GLES2_CacheProgram(SDL_Renderer *renderer, GLES2_ShaderCacheEntry *vertex,
slouken@5201
   426
                   GLES2_ShaderCacheEntry *fragment, SDL_BlendMode blendMode)
slouken@5201
   427
{
slouken@5201
   428
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   429
    GLES2_ProgramCacheEntry *entry;
slouken@5201
   430
    GLES2_ShaderCacheEntry *shaderEntry;
slouken@5201
   431
    GLint linkSuccessful;
slouken@5201
   432
slouken@5201
   433
    /* Check if we've already cached this program */
slouken@5201
   434
    entry = rdata->program_cache.head;
slouken@5201
   435
    while (entry)
slouken@5201
   436
    {
slouken@5201
   437
        if (entry->vertex_shader == vertex && entry->fragment_shader == fragment)
slouken@5201
   438
            break;
slouken@5201
   439
        entry = entry->next;
slouken@5201
   440
    }
slouken@5201
   441
    if (entry)
slouken@5201
   442
    {
slouken@5201
   443
        if (rdata->program_cache.count > 1)
slouken@5201
   444
        {
slouken@5201
   445
            if (entry->next)
slouken@5201
   446
                entry->next->prev = entry->prev;
slouken@5201
   447
            if (entry->prev)
slouken@5201
   448
                entry->prev->next = entry->next;
slouken@5201
   449
            entry->prev = NULL;
slouken@5201
   450
            entry->next = rdata->program_cache.head;
slouken@5201
   451
            rdata->program_cache.head->prev = entry;
slouken@5201
   452
            rdata->program_cache.head = entry;
slouken@5201
   453
        }
slouken@5201
   454
        return entry;
slouken@5201
   455
    }
slouken@5201
   456
slouken@5201
   457
    /* Create a program cache entry */
slouken@5201
   458
    entry = (GLES2_ProgramCacheEntry *)SDL_calloc(1, sizeof(GLES2_ProgramCacheEntry));
slouken@5201
   459
    if (!entry)
slouken@5201
   460
    {
slouken@5201
   461
        SDL_OutOfMemory();
slouken@5201
   462
        return NULL;
slouken@5201
   463
    }
slouken@5201
   464
    entry->vertex_shader = vertex;
slouken@5201
   465
    entry->fragment_shader = fragment;
slouken@5201
   466
    entry->blend_mode = blendMode;
slouken@5201
   467
    
slouken@5201
   468
    /* Create the program and link it */
slouken@5201
   469
    glGetError();
slouken@5201
   470
    entry->id = glCreateProgram();
slouken@5201
   471
    glAttachShader(entry->id, vertex->id);
slouken@5201
   472
    glAttachShader(entry->id, fragment->id);
slouken@5201
   473
    glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_POSITION, "a_position");
slouken@5201
   474
    glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_TEXCOORD, "a_texCoord");
slouken@5201
   475
    glLinkProgram(entry->id);
slouken@5201
   476
    glGetProgramiv(entry->id, GL_LINK_STATUS, &linkSuccessful);
slouken@5201
   477
    if (glGetError() != GL_NO_ERROR || !linkSuccessful)
slouken@5201
   478
    {
slouken@5201
   479
        SDL_SetError("Failed to link shader program");
slouken@5201
   480
        glDeleteProgram(entry->id);
slouken@5201
   481
        SDL_free(entry);
slouken@5201
   482
        return NULL;
slouken@5201
   483
    }
slouken@5201
   484
    
slouken@5201
   485
    /* Predetermine locations of uniform variables */
slouken@5201
   486
    entry->uniform_locations[GLES2_UNIFORM_PROJECTION] =
slouken@5201
   487
        glGetUniformLocation(entry->id, "u_projection");
slouken@5201
   488
    entry->uniform_locations[GLES2_UNIFORM_TEXTURE] =
slouken@5201
   489
        glGetUniformLocation(entry->id, "u_texture");
slouken@5201
   490
    entry->uniform_locations[GLES2_UNIFORM_MODULATION] =
slouken@5201
   491
        glGetUniformLocation(entry->id, "u_modulation");
slouken@5201
   492
    entry->uniform_locations[GLES2_UNIFORM_COLOR] =
slouken@5201
   493
        glGetUniformLocation(entry->id, "u_color");
slouken@5201
   494
    entry->uniform_locations[GLES2_UNIFORM_COLORTABLE] =
slouken@5201
   495
        glGetUniformLocation(entry->id, "u_colorTable");
slouken@5201
   496
slouken@5201
   497
    /* Cache the linked program */
slouken@5201
   498
    if (rdata->program_cache.head)
slouken@5201
   499
    {
slouken@5201
   500
        entry->next = rdata->program_cache.head;
slouken@5201
   501
        rdata->program_cache.head->prev = entry;
slouken@5201
   502
    }
slouken@5201
   503
    else
slouken@5201
   504
    {
slouken@5201
   505
        rdata->program_cache.tail = entry;
slouken@5201
   506
    }
slouken@5201
   507
    rdata->program_cache.head = entry;
slouken@5201
   508
    ++rdata->program_cache.count;
slouken@5201
   509
slouken@5201
   510
    /* Increment the refcount of the shaders we're using */
slouken@5201
   511
    ++vertex->references;
slouken@5201
   512
    ++fragment->references;
slouken@5201
   513
slouken@5201
   514
    /* Evict the last entry from the cache if we exceed the limit */
slouken@5201
   515
    if (rdata->program_cache.count > GLES2_MAX_CACHED_PROGRAMS)
slouken@5201
   516
    {
slouken@5201
   517
        shaderEntry = rdata->program_cache.tail->vertex_shader;
slouken@5201
   518
        if (--shaderEntry->references <= 0)
slouken@5201
   519
            GLES2_EvictShader(renderer, shaderEntry);
slouken@5201
   520
        shaderEntry = rdata->program_cache.tail->fragment_shader;
slouken@5201
   521
        if (--shaderEntry->references <= 0)
slouken@5201
   522
            GLES2_EvictShader(renderer, shaderEntry);
slouken@5201
   523
        glDeleteProgram(rdata->program_cache.tail->id);
slouken@5201
   524
        rdata->program_cache.tail = rdata->program_cache.tail->prev;
slouken@5201
   525
        SDL_free(rdata->program_cache.tail->next);
slouken@5201
   526
        rdata->program_cache.tail->next = NULL;
slouken@5201
   527
        --rdata->program_cache.count;
slouken@5201
   528
    }
slouken@5201
   529
    return entry;
slouken@5201
   530
}
slouken@5201
   531
slouken@5201
   532
static GLES2_ShaderCacheEntry *
slouken@5201
   533
GLES2_CacheShader(SDL_Renderer *renderer, GLES2_ShaderType type, SDL_BlendMode blendMode)
slouken@5201
   534
{
slouken@5201
   535
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   536
    const GLES2_Shader *shader;
slouken@5201
   537
    const GLES2_ShaderInstance *instance = NULL;
slouken@5201
   538
    GLES2_ShaderCacheEntry *entry = NULL;
slouken@5201
   539
    GLint compileSuccessful = GL_FALSE;
slouken@5201
   540
    int i, j;
slouken@5201
   541
slouken@5201
   542
    /* Find the corresponding shader */
slouken@5201
   543
    shader = GLES2_GetShader(type, blendMode);
slouken@5201
   544
    if (!shader)
slouken@5201
   545
    {
slouken@5201
   546
        SDL_SetError("No shader matching the requested characteristics was found");
slouken@5201
   547
        return NULL;
slouken@5201
   548
    }
slouken@5201
   549
    
slouken@5201
   550
    /* Find a matching shader instance that's supported on this hardware */
slouken@5206
   551
    for (i = 0; i < shader->instance_count && !instance; ++i)
slouken@5201
   552
    {
slouken@5206
   553
        for (j = 0; j < rdata->shader_format_count && !instance; ++j)
slouken@5201
   554
        {
slouken@5201
   555
            if (!shader->instances)
slouken@5201
   556
                continue;
slouken@5206
   557
            if (!shader->instances[i])
slouken@5206
   558
                continue;
slouken@5201
   559
            if (shader->instances[i]->format != rdata->shader_formats[j])
slouken@5201
   560
                continue;
slouken@5201
   561
            instance = shader->instances[i];
slouken@5201
   562
        }
slouken@5201
   563
    }
slouken@5201
   564
    if (!instance)
slouken@5201
   565
    {
slouken@5201
   566
        SDL_SetError("The specified shader cannot be loaded on the current platform");
slouken@5201
   567
        return NULL;
slouken@5201
   568
    }
slouken@5201
   569
slouken@5201
   570
    /* Check if we've already cached this shader */
slouken@5201
   571
    entry = rdata->shader_cache.head;
slouken@5201
   572
    while (entry)
slouken@5201
   573
    {
slouken@5201
   574
        if (entry->instance == instance)
slouken@5201
   575
            break;
slouken@5201
   576
        entry = entry->next;
slouken@5201
   577
    }
slouken@5201
   578
    if (entry)
slouken@5201
   579
        return entry;
slouken@5201
   580
slouken@5201
   581
    /* Create a shader cache entry */
slouken@5201
   582
    entry = (GLES2_ShaderCacheEntry *)SDL_calloc(1, sizeof(GLES2_ShaderCacheEntry));
slouken@5201
   583
    if (!entry)
slouken@5201
   584
    {
slouken@5201
   585
        SDL_OutOfMemory();
slouken@5201
   586
        return NULL;
slouken@5201
   587
    }
slouken@5201
   588
    entry->type = type;
slouken@5201
   589
    entry->instance = instance;
slouken@5201
   590
slouken@5201
   591
    /* Compile or load the selected shader instance */
slouken@5201
   592
    glGetError();
slouken@5201
   593
    entry->id = glCreateShader(instance->type);
slouken@5201
   594
    if (instance->format == (GLenum)-1)
slouken@5201
   595
    {
slouken@5212
   596
        glShaderSource(entry->id, 1, (const char **)&instance->data, NULL);
slouken@5201
   597
        glCompileShader(entry->id);
slouken@5201
   598
        glGetShaderiv(entry->id, GL_COMPILE_STATUS, &compileSuccessful);
slouken@5201
   599
    }
slouken@5201
   600
    else
slouken@5201
   601
    {
slouken@5201
   602
        glShaderBinary(1, &entry->id, instance->format, instance->data, instance->length);
slouken@5201
   603
        compileSuccessful = GL_TRUE;
slouken@5201
   604
    }
slouken@5201
   605
    if (glGetError() != GL_NO_ERROR || !compileSuccessful)
slouken@5201
   606
    {
slouken@5212
   607
        char *info = NULL;
slouken@5212
   608
        int length;
slouken@5212
   609
slouken@5212
   610
        glGetShaderiv(entry->id, GL_INFO_LOG_LENGTH, &length);
slouken@5212
   611
        if (length > 0) {
slouken@5212
   612
            info = SDL_stack_alloc(char, length);
slouken@5212
   613
            if (info) {
slouken@5212
   614
                glGetShaderInfoLog(entry->id, length, &length, info);
slouken@5212
   615
            }
slouken@5212
   616
        }
slouken@5212
   617
        if (info) {
slouken@5212
   618
            SDL_SetError("Failed to load the shader: %s", info);
slouken@5212
   619
            SDL_stack_free(info);
slouken@5212
   620
        } else {
slouken@5212
   621
            SDL_SetError("Failed to load the shader");
slouken@5212
   622
        }
slouken@5201
   623
        glDeleteShader(entry->id);
slouken@5201
   624
        SDL_free(entry);
slouken@5201
   625
        return NULL;
slouken@5201
   626
    }
slouken@5201
   627
slouken@5201
   628
    /* Link the shader entry in at the front of the cache */
slouken@5201
   629
    if (rdata->shader_cache.head)
slouken@5201
   630
    {
slouken@5201
   631
        entry->next = rdata->shader_cache.head;
slouken@5201
   632
        rdata->shader_cache.head->prev = entry;
slouken@5201
   633
    }
slouken@5201
   634
    rdata->shader_cache.head = entry;
slouken@5201
   635
    ++rdata->shader_cache.count;
slouken@5201
   636
    return entry;
slouken@5201
   637
}
slouken@5201
   638
slouken@5201
   639
static void
slouken@5201
   640
GLES2_EvictShader(SDL_Renderer *renderer, GLES2_ShaderCacheEntry *entry)
slouken@5201
   641
{
slouken@5201
   642
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   643
slouken@5201
   644
    /* Unlink the shader from the cache */
slouken@5201
   645
    if (entry->next)
slouken@5201
   646
        entry->next->prev = entry->prev;
slouken@5201
   647
    if (entry->prev)
slouken@5201
   648
        entry->prev->next = entry->next;
slouken@5201
   649
    if (rdata->shader_cache.head == entry)
slouken@5201
   650
        rdata->shader_cache.head = entry->next;
slouken@5201
   651
    --rdata->shader_cache.count;
slouken@5201
   652
slouken@5201
   653
    /* Deallocate the shader */
slouken@5201
   654
    glDeleteShader(entry->id);
slouken@5201
   655
    SDL_free(entry);
slouken@5201
   656
}
slouken@5201
   657
slouken@5201
   658
static int
slouken@5201
   659
GLES2_SelectProgram(SDL_Renderer *renderer, GLES2_ImageSource source, SDL_BlendMode blendMode)
slouken@5201
   660
{
slouken@5201
   661
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   662
    GLES2_ShaderCacheEntry *vertex = NULL;
slouken@5201
   663
    GLES2_ShaderCacheEntry *fragment = NULL;
slouken@5201
   664
    GLES2_ShaderType vtype, ftype;
slouken@5201
   665
    GLES2_ProgramCacheEntry *program;
slouken@5201
   666
slouken@5201
   667
    /* Select an appropriate shader pair for the specified modes */
slouken@5201
   668
    vtype = GLES2_SHADER_VERTEX_DEFAULT;
slouken@5201
   669
    switch (source)
slouken@5201
   670
    {
slouken@5201
   671
    case GLES2_IMAGESOURCE_SOLID:
slouken@5201
   672
        ftype = GLES2_SHADER_FRAGMENT_SOLID_SRC;
slouken@5201
   673
        break;
slouken@5201
   674
    case GLES2_IMAGESOURCE_TEXTURE:
slouken@5201
   675
        ftype = GLES2_SHADER_FRAGMENT_TEXTURE_SRC;
slouken@5201
   676
        break;
slouken@5201
   677
    }
slouken@5201
   678
slouken@5201
   679
    /* Load the requested shaders */
slouken@5201
   680
    vertex = GLES2_CacheShader(renderer, vtype, blendMode);
slouken@5201
   681
    if (!vertex)
slouken@5201
   682
        goto fault;
slouken@5201
   683
    fragment = GLES2_CacheShader(renderer, ftype, blendMode);
slouken@5201
   684
    if (!fragment)
slouken@5201
   685
        goto fault;
slouken@5201
   686
slouken@5201
   687
    /* Check if we need to change programs at all */
slouken@5201
   688
    if (rdata->current_program &&
slouken@5201
   689
        rdata->current_program->vertex_shader == vertex &&
slouken@5201
   690
        rdata->current_program->fragment_shader == fragment)
slouken@5201
   691
        return 0;
slouken@5201
   692
slouken@5201
   693
    /* Generate a matching program */
slouken@5201
   694
    program = GLES2_CacheProgram(renderer, vertex, fragment, blendMode);
slouken@5201
   695
    if (!program)
slouken@5201
   696
        goto fault;
slouken@5201
   697
slouken@5201
   698
    /* Select that program in OpenGL */
slouken@5201
   699
    glGetError();
slouken@5201
   700
    glUseProgram(program->id);
slouken@5201
   701
    if (glGetError() != GL_NO_ERROR)
slouken@5201
   702
    {
slouken@5201
   703
        SDL_SetError("Failed to select program");
slouken@5201
   704
        goto fault;
slouken@5201
   705
    }
slouken@5201
   706
slouken@5201
   707
    /* Set the current program */
slouken@5201
   708
    rdata->current_program = program;
slouken@5201
   709
slouken@5201
   710
    /* Activate an orthographic projection */
slouken@5201
   711
    if (GLES2_SetOrthographicProjection(renderer) < 0)
slouken@5201
   712
        goto fault;
slouken@5201
   713
slouken@5201
   714
    /* Clean up and return */
slouken@5201
   715
    return 0;
slouken@5201
   716
fault:
slouken@5201
   717
    if (vertex && vertex->references <= 0)
slouken@5201
   718
        GLES2_EvictShader(renderer, vertex);
slouken@5201
   719
    if (fragment && fragment->references <= 0)
slouken@5201
   720
        GLES2_EvictShader(renderer, fragment);
slouken@5201
   721
    rdata->current_program = NULL;
slouken@5201
   722
    return -1;
slouken@5201
   723
}
slouken@5201
   724
slouken@5201
   725
static int
slouken@5201
   726
GLES2_SetOrthographicProjection(SDL_Renderer *renderer)
slouken@5201
   727
{
slouken@5201
   728
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   729
    GLfloat projection[4][4];
slouken@5201
   730
    GLuint locProjection;
slouken@5201
   731
slouken@5201
   732
    /* Prepare an orthographic projection */
slouken@5297
   733
    projection[0][0] = 2.0f / renderer->viewport.w;
slouken@5201
   734
    projection[0][1] = 0.0f;
slouken@5201
   735
    projection[0][2] = 0.0f;
slouken@5201
   736
    projection[0][3] = 0.0f;
slouken@5201
   737
    projection[1][0] = 0.0f;
slouken@5297
   738
    projection[1][1] = -2.0f / renderer->viewport.h;
slouken@5201
   739
    projection[1][2] = 0.0f;
slouken@5201
   740
    projection[1][3] = 0.0f;
slouken@5201
   741
    projection[2][0] = 0.0f;
slouken@5201
   742
    projection[2][1] = 0.0f;
slouken@5201
   743
    projection[2][2] = 1.0f;
slouken@5201
   744
    projection[2][3] = 0.0f;
slouken@5201
   745
    projection[3][0] = -1.0f;
slouken@5201
   746
    projection[3][1] = 1.0f;
slouken@5201
   747
    projection[3][2] = 0.0f;
slouken@5201
   748
    projection[3][3] = 1.0f;
slouken@5201
   749
slouken@5201
   750
    /* Set the projection matrix */
slouken@5201
   751
    locProjection = rdata->current_program->uniform_locations[GLES2_UNIFORM_PROJECTION];
slouken@5201
   752
    glGetError();
slouken@5201
   753
    glUniformMatrix4fv(locProjection, 1, GL_FALSE, (GLfloat *)projection);
slouken@5201
   754
    if (glGetError() != GL_NO_ERROR)
slouken@5201
   755
    {
slouken@5201
   756
        SDL_SetError("Failed to set orthographic projection");
slouken@5201
   757
        return -1;
slouken@5201
   758
    }
slouken@5201
   759
    return 0;
slouken@5201
   760
}
slouken@5201
   761
slouken@5201
   762
/*************************************************************************************************
slouken@5201
   763
 * Rendering functions                                                                           *
slouken@5201
   764
 *************************************************************************************************/
slouken@5201
   765
slouken@5224
   766
static const float inv255f = 1.0f / 255.0f;
slouken@5224
   767
slouken@5201
   768
static int GLES2_RenderClear(SDL_Renderer *renderer);
slouken@5201
   769
static int GLES2_RenderDrawPoints(SDL_Renderer *renderer, const SDL_Point *points, int count);
slouken@5201
   770
static int GLES2_RenderDrawLines(SDL_Renderer *renderer, const SDL_Point *points, int count);
slouken@5297
   771
static int GLES2_RenderFillRects(SDL_Renderer *renderer, const SDL_Rect *rects, int count);
slouken@5201
   772
static int GLES2_RenderCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect,
slouken@5201
   773
                            const SDL_Rect *dstrect);
slouken@5201
   774
static void GLES2_RenderPresent(SDL_Renderer *renderer);
slouken@5201
   775
slouken@5201
   776
static int
slouken@5201
   777
GLES2_RenderClear(SDL_Renderer *renderer)
slouken@5201
   778
{
slouken@5224
   779
    float r = (float)renderer->r * inv255f;
slouken@5224
   780
    float g = (float)renderer->g * inv255f;
slouken@5224
   781
    float b = (float)renderer->b * inv255f;
slouken@5224
   782
    float a = (float)renderer->a * inv255f;
slouken@5201
   783
slouken@5201
   784
    GLES2_ActivateRenderer(renderer);
slouken@5201
   785
slouken@5201
   786
    /* Clear the backbuffer with the selected color */
slouken@5201
   787
    glClearColor(r, g, b, a);
slouken@5201
   788
    glClear(GL_COLOR_BUFFER_BIT);
slouken@5201
   789
    return 0;
slouken@5201
   790
}
slouken@5201
   791
slouken@5201
   792
static void
slouken@5201
   793
GLES2_SetBlendMode(int blendMode)
slouken@5201
   794
{
slouken@5201
   795
    switch (blendMode)
slouken@5201
   796
    {
slouken@5201
   797
    case SDL_BLENDMODE_NONE:
slouken@5201
   798
    default:
slouken@5201
   799
        glDisable(GL_BLEND);
slouken@5201
   800
        break;
slouken@5201
   801
    case SDL_BLENDMODE_BLEND:
slouken@5201
   802
        glEnable(GL_BLEND);
slouken@5201
   803
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@5201
   804
        break;
slouken@5201
   805
    case SDL_BLENDMODE_ADD:
slouken@5201
   806
        glEnable(GL_BLEND);
slouken@5201
   807
        glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@5201
   808
        break;
slouken@5201
   809
    case SDL_BLENDMODE_MOD:
slouken@5201
   810
        glEnable(GL_BLEND);
slouken@5201
   811
        glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@5201
   812
        break;
slouken@5201
   813
    }
slouken@5201
   814
}
slouken@5201
   815
slouken@5201
   816
static int
slouken@5201
   817
GLES2_RenderDrawPoints(SDL_Renderer *renderer, const SDL_Point *points, int count)
slouken@5201
   818
{
slouken@5201
   819
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   820
    GLfloat *vertices;
slouken@5201
   821
    SDL_BlendMode blendMode;
slouken@5201
   822
    int alpha;
slouken@5201
   823
    GLuint locColor;
slouken@5201
   824
    int idx;
slouken@5201
   825
slouken@5201
   826
    GLES2_ActivateRenderer(renderer);
slouken@5201
   827
slouken@5201
   828
    blendMode = renderer->blendMode;
slouken@5201
   829
    alpha = renderer->a;
slouken@5201
   830
slouken@5201
   831
    /* Activate an appropriate shader and set the projection matrix */
slouken@5201
   832
    if (GLES2_SelectProgram(renderer, GLES2_IMAGESOURCE_SOLID, blendMode) < 0)
slouken@5201
   833
        return -1;
slouken@5201
   834
slouken@5201
   835
    /* Select the color to draw with */
slouken@5201
   836
    locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
slouken@5201
   837
    glGetError();
slouken@5201
   838
    glUniform4f(locColor,
slouken@5224
   839
                renderer->r * inv255f,
slouken@5224
   840
                renderer->g * inv255f,
slouken@5224
   841
                renderer->b * inv255f,
slouken@5224
   842
                alpha * inv255f);
slouken@5201
   843
slouken@5201
   844
    /* Configure the correct blend mode */
slouken@5201
   845
    GLES2_SetBlendMode(blendMode);
slouken@5201
   846
slouken@5201
   847
    /* Emit the specified vertices as points */
slouken@5201
   848
    vertices = SDL_stack_alloc(GLfloat, count * 2);
slouken@5201
   849
    for (idx = 0; idx < count; ++idx)
slouken@5201
   850
    {
slouken@5201
   851
        GLfloat x = (GLfloat)points[idx].x + 0.5f;
slouken@5201
   852
        GLfloat y = (GLfloat)points[idx].y + 0.5f;
slouken@5201
   853
slouken@5201
   854
        vertices[idx * 2] = x;
slouken@5201
   855
        vertices[(idx * 2) + 1] = y;
slouken@5201
   856
    }
slouken@5201
   857
    glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
slouken@5201
   858
    glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
slouken@5201
   859
    glDrawArrays(GL_POINTS, 0, count);
slouken@5201
   860
    glDisableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
slouken@5201
   861
    SDL_stack_free(vertices);
slouken@5201
   862
    if (glGetError() != GL_NO_ERROR)
slouken@5201
   863
    {
slouken@5201
   864
        SDL_SetError("Failed to render lines");
slouken@5201
   865
        return -1;
slouken@5201
   866
    }
slouken@5201
   867
    return 0;
slouken@5201
   868
}
slouken@5201
   869
slouken@5201
   870
static int
slouken@5201
   871
GLES2_RenderDrawLines(SDL_Renderer *renderer, const SDL_Point *points, int count)
slouken@5201
   872
{
slouken@5201
   873
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   874
    GLfloat *vertices;
slouken@5201
   875
    SDL_BlendMode blendMode;
slouken@5201
   876
    int alpha;
slouken@5201
   877
    GLuint locColor;
slouken@5201
   878
    int idx;
slouken@5201
   879
slouken@5201
   880
    GLES2_ActivateRenderer(renderer);
slouken@5201
   881
slouken@5201
   882
    blendMode = renderer->blendMode;
slouken@5201
   883
    alpha = renderer->a;
slouken@5201
   884
slouken@5201
   885
    /* Activate an appropriate shader and set the projection matrix */
slouken@5201
   886
    if (GLES2_SelectProgram(renderer, GLES2_IMAGESOURCE_SOLID, blendMode) < 0)
slouken@5201
   887
        return -1;
slouken@5201
   888
slouken@5201
   889
    /* Select the color to draw with */
slouken@5201
   890
    locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
slouken@5201
   891
    glGetError();
slouken@5201
   892
    glUniform4f(locColor,
slouken@5224
   893
                renderer->r * inv255f,
slouken@5224
   894
                renderer->g * inv255f,
slouken@5224
   895
                renderer->b * inv255f,
slouken@5224
   896
                alpha * inv255f);
slouken@5201
   897
slouken@5201
   898
    /* Configure the correct blend mode */
slouken@5201
   899
    GLES2_SetBlendMode(blendMode);
slouken@5201
   900
slouken@5201
   901
    /* Emit a line strip including the specified vertices */
slouken@5201
   902
    vertices = SDL_stack_alloc(GLfloat, count * 2);
slouken@5201
   903
    for (idx = 0; idx < count; ++idx)
slouken@5201
   904
    {
slouken@5201
   905
        GLfloat x = (GLfloat)points[idx].x + 0.5f;
slouken@5201
   906
        GLfloat y = (GLfloat)points[idx].y + 0.5f;
slouken@5201
   907
slouken@5201
   908
        vertices[idx * 2] = x;
slouken@5201
   909
        vertices[(idx * 2) + 1] = y;
slouken@5201
   910
    }
slouken@5201
   911
    glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
slouken@5201
   912
    glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
slouken@5201
   913
    glDrawArrays(GL_LINE_STRIP, 0, count);
slouken@5201
   914
    glDisableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
slouken@5201
   915
    SDL_stack_free(vertices);
slouken@5201
   916
    if (glGetError() != GL_NO_ERROR)
slouken@5201
   917
    {
slouken@5201
   918
        SDL_SetError("Failed to render lines");
slouken@5201
   919
        return -1;
slouken@5201
   920
    }
slouken@5201
   921
    return 0;
slouken@5201
   922
}
slouken@5201
   923
slouken@5201
   924
static int
slouken@5297
   925
GLES2_RenderFillRects(SDL_Renderer *renderer, const SDL_Rect *rects, int count)
slouken@5201
   926
{
slouken@5201
   927
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   928
    GLfloat vertices[8];
slouken@5201
   929
    SDL_BlendMode blendMode;
slouken@5201
   930
    int alpha;
slouken@5201
   931
    GLuint locColor;
slouken@5201
   932
    int idx;
slouken@5201
   933
slouken@5201
   934
    GLES2_ActivateRenderer(renderer);
slouken@5201
   935
slouken@5201
   936
    blendMode = renderer->blendMode;
slouken@5201
   937
    alpha = renderer->a;
slouken@5201
   938
slouken@5201
   939
    /* Activate an appropriate shader and set the projection matrix */
slouken@5201
   940
    if (GLES2_SelectProgram(renderer, GLES2_IMAGESOURCE_SOLID, blendMode) < 0)
slouken@5201
   941
        return -1;
slouken@5201
   942
slouken@5201
   943
    /* Select the color to draw with */
slouken@5201
   944
    locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
slouken@5201
   945
    glGetError();
slouken@5201
   946
    glUniform4f(locColor,
slouken@5224
   947
                renderer->r * inv255f,
slouken@5224
   948
                renderer->g * inv255f,
slouken@5224
   949
                renderer->b * inv255f,
slouken@5224
   950
                alpha * inv255f);
slouken@5201
   951
slouken@5201
   952
    /* Configure the correct blend mode */
slouken@5201
   953
    GLES2_SetBlendMode(blendMode);
slouken@5201
   954
slouken@5201
   955
    /* Emit a line loop for each rectangle */
slouken@5201
   956
    glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
slouken@5297
   957
    for (idx = 0; idx < count; ++idx) {
slouken@5297
   958
        const SDL_Rect *rect = &rects[idx];
slouken@5297
   959
slouken@5297
   960
        GLfloat xMin = (GLfloat)rect->x;
slouken@5297
   961
        GLfloat xMax = (GLfloat)(rect->x + rect->w);
slouken@5297
   962
        GLfloat yMin = (GLfloat)rect->y;
slouken@5297
   963
        GLfloat yMax = (GLfloat)(rect->y + rect->h);
slouken@5201
   964
slouken@5201
   965
        vertices[0] = xMin;
slouken@5201
   966
        vertices[1] = yMin;
slouken@5201
   967
        vertices[2] = xMax;
slouken@5201
   968
        vertices[3] = yMin;
slouken@5201
   969
        vertices[4] = xMin;
slouken@5201
   970
        vertices[5] = yMax;
slouken@5201
   971
        vertices[6] = xMax;
slouken@5201
   972
        vertices[7] = yMax;
slouken@5201
   973
        glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
slouken@5201
   974
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
slouken@5201
   975
    }
slouken@5201
   976
    glDisableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
slouken@5201
   977
    if (glGetError() != GL_NO_ERROR)
slouken@5201
   978
    {
slouken@5201
   979
        SDL_SetError("Failed to render lines");
slouken@5201
   980
        return -1;
slouken@5201
   981
    }
slouken@5201
   982
    return 0;
slouken@5201
   983
}
slouken@5201
   984
slouken@5201
   985
static int
slouken@5201
   986
GLES2_RenderCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect,
slouken@5201
   987
                 const SDL_Rect *dstrect)
slouken@5201
   988
{
slouken@5201
   989
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
slouken@5201
   990
    GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
slouken@5201
   991
    GLES2_ImageSource sourceType;
slouken@5201
   992
    SDL_BlendMode blendMode;
slouken@5201
   993
    int alpha;
slouken@5201
   994
    GLfloat vertices[8];
slouken@5201
   995
    GLfloat texCoords[8];
slouken@5201
   996
    GLuint locTexture;
slouken@5201
   997
    GLuint locModulation;
slouken@5201
   998
    GLuint locColorTable;
slouken@5201
   999
slouken@5201
  1000
    GLES2_ActivateRenderer(renderer);
slouken@5201
  1001
slouken@5201
  1002
    /* Activate an appropriate shader and set the projection matrix */
slouken@5201
  1003
    blendMode = texture->blendMode;
slouken@5201
  1004
    alpha = texture->a;
slouken@5201
  1005
    sourceType = GLES2_IMAGESOURCE_TEXTURE;
slouken@5201
  1006
    if (GLES2_SelectProgram(renderer, sourceType, blendMode) < 0)
slouken@5201
  1007
        return -1;
slouken@5201
  1008
slouken@5201
  1009
    /* Select the target texture */
slouken@5201
  1010
    locTexture = rdata->current_program->uniform_locations[GLES2_UNIFORM_TEXTURE];
slouken@5201
  1011
    glGetError();
slouken@5201
  1012
    glActiveTexture(GL_TEXTURE0);
slouken@5201
  1013
    glBindTexture(tdata->texture_type, tdata->texture);
slouken@5201
  1014
    glUniform1i(locTexture, 0);
slouken@5201
  1015
slouken@5201
  1016
    /* Configure texture blending */
slouken@5201
  1017
    GLES2_SetBlendMode(blendMode);
slouken@5201
  1018
slouken@5201
  1019
    /* Configure color modulation */
slouken@5201
  1020
    locModulation = rdata->current_program->uniform_locations[GLES2_UNIFORM_MODULATION];
slouken@5201
  1021
    glUniform4f(locModulation,
slouken@5224
  1022
                texture->r * inv255f,
slouken@5224
  1023
                texture->g * inv255f,
slouken@5224
  1024
                texture->b * inv255f,
slouken@5224
  1025
                alpha * inv255f);
slouken@5201
  1026
slouken@5201
  1027
    /* Emit the textured quad */
slouken@5201
  1028
    glEnableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
slouken@5201
  1029
    glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
slouken@5201
  1030
    vertices[0] = (GLfloat)dstrect->x;
slouken@5201
  1031
    vertices[1] = (GLfloat)dstrect->y;
slouken@5201
  1032
    vertices[2] = (GLfloat)(dstrect->x + dstrect->w);
slouken@5201
  1033
    vertices[3] = (GLfloat)dstrect->y;
slouken@5201
  1034
    vertices[4] = (GLfloat)dstrect->x;
slouken@5201
  1035
    vertices[5] = (GLfloat)(dstrect->y + dstrect->h);
slouken@5201
  1036
    vertices[6] = (GLfloat)(dstrect->x + dstrect->w);
slouken@5201
  1037
    vertices[7] = (GLfloat)(dstrect->y + dstrect->h);
slouken@5201
  1038
    glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
slouken@5201
  1039
    texCoords[0] = srcrect->x / (GLfloat)texture->w;
slouken@5201
  1040
    texCoords[1] = srcrect->y / (GLfloat)texture->h;
slouken@5201
  1041
    texCoords[2] = (srcrect->x + srcrect->w) / (GLfloat)texture->w;
slouken@5201
  1042
    texCoords[3] = srcrect->y / (GLfloat)texture->h;
slouken@5201
  1043
    texCoords[4] = srcrect->x / (GLfloat)texture->w;
slouken@5201
  1044
    texCoords[5] = (srcrect->y + srcrect->h) / (GLfloat)texture->h;
slouken@5201
  1045
    texCoords[6] = (srcrect->x + srcrect->w) / (GLfloat)texture->w;
slouken@5201
  1046
    texCoords[7] = (srcrect->y + srcrect->h) / (GLfloat)texture->h;
slouken@5201
  1047
    glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, texCoords);
slouken@5201
  1048
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
slouken@5201
  1049
    glDisableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
slouken@5201
  1050
    glDisableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
slouken@5201
  1051
    if (glGetError() != GL_NO_ERROR)
slouken@5201
  1052
    {
slouken@5201
  1053
        SDL_SetError("Failed to render texture");
slouken@5201
  1054
        return -1;
slouken@5201
  1055
    }
slouken@5201
  1056
    return 0;
slouken@5201
  1057
}
slouken@5201
  1058
slouken@5201
  1059
static void
slouken@5201
  1060
GLES2_RenderPresent(SDL_Renderer *renderer)
slouken@5201
  1061
{
slouken@5201
  1062
    GLES2_ActivateRenderer(renderer);
slouken@5201
  1063
slouken@5201
  1064
    /* Tell the video driver to swap buffers */
slouken@5201
  1065
    SDL_GL_SwapWindow(renderer->window);
slouken@5201
  1066
}
slouken@5201
  1067
slouken@5201
  1068
/*************************************************************************************************
slouken@5201
  1069
 * Renderer instantiation                                                                        *
slouken@5201
  1070
 *************************************************************************************************/
slouken@5201
  1071
slouken@5201
  1072
#define GL_NVIDIA_PLATFORM_BINARY_NV 0x890B
slouken@5201
  1073
slouken@5201
  1074
static SDL_Renderer *
slouken@5201
  1075
GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
slouken@5201
  1076
{
slouken@5201
  1077
    SDL_Renderer *renderer;
slouken@5201
  1078
    GLES2_DriverContext *rdata;
slouken@5201
  1079
    GLint nFormats;
slouken@5201
  1080
#ifndef ZUNE_HD
slouken@5201
  1081
    GLboolean hasCompiler;
slouken@5201
  1082
#endif
slouken@5201
  1083
slouken@5201
  1084
    /* Create the renderer struct */
slouken@5201
  1085
    renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(SDL_Renderer));
slouken@5209
  1086
    if (!renderer) {
slouken@5209
  1087
        SDL_OutOfMemory();
slouken@5209
  1088
        return NULL;
slouken@5209
  1089
    }
slouken@5209
  1090
slouken@5201
  1091
    rdata = (GLES2_DriverContext *)SDL_calloc(1, sizeof(GLES2_DriverContext));
slouken@5209
  1092
    if (!rdata) {
slouken@5209
  1093
        GLES2_DestroyRenderer(renderer);
slouken@5201
  1094
        SDL_OutOfMemory();
slouken@5201
  1095
        return NULL;
slouken@5201
  1096
    }
slouken@5201
  1097
    renderer->info = GLES2_RenderDriver.info;
slouken@5297
  1098
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@5201
  1099
    renderer->driverdata = rdata;
slouken@5201
  1100
slouken@5209
  1101
    /* Create an OpenGL ES 2.0 context */
slouken@5209
  1102
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
slouken@5209
  1103
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
slouken@5209
  1104
slouken@5201
  1105
    rdata->context = SDL_GL_CreateContext(window);
slouken@5201
  1106
    if (!rdata->context)
slouken@5201
  1107
    {
slouken@5209
  1108
        GLES2_DestroyRenderer(renderer);
slouken@5201
  1109
        return NULL;
slouken@5201
  1110
    }
slouken@5202
  1111
    if (SDL_GL_MakeCurrent(window, rdata->context) < 0) {
slouken@5209
  1112
        GLES2_DestroyRenderer(renderer);
slouken@5202
  1113
        return NULL;
slouken@5202
  1114
    }
slouken@5202
  1115
slouken@5202
  1116
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@5202
  1117
        SDL_GL_SetSwapInterval(1);
slouken@5202
  1118
    } else {
slouken@5202
  1119
        SDL_GL_SetSwapInterval(0);
slouken@5202
  1120
    }
slouken@5202
  1121
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@5202
  1122
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@5202
  1123
    }
slouken@5201
  1124
slouken@5201
  1125
    /* Determine supported shader formats */
slouken@5201
  1126
    /* HACK: glGetInteger is broken on the Zune HD's compositor, so we just hardcode this */
slouken@5201
  1127
    glGetError();
slouken@5201
  1128
#ifdef ZUNE_HD
slouken@5201
  1129
    nFormats = 1;
slouken@5201
  1130
#else /* !ZUNE_HD */
slouken@5201
  1131
    glGetIntegerv(GL_NUM_SHADER_BINARY_FORMATS, &nFormats);
slouken@5201
  1132
    glGetBooleanv(GL_SHADER_COMPILER, &hasCompiler);
slouken@5201
  1133
    if (hasCompiler)
slouken@5201
  1134
        ++nFormats;
slouken@5201
  1135
#endif /* ZUNE_HD */
slouken@5201
  1136
    rdata->shader_formats = (GLenum *)SDL_calloc(nFormats, sizeof(GLenum));
slouken@5201
  1137
    if (!rdata->shader_formats)
slouken@5201
  1138
    {
slouken@5209
  1139
        GLES2_DestroyRenderer(renderer);
slouken@5201
  1140
        SDL_OutOfMemory();
slouken@5201
  1141
        return NULL;
slouken@5201
  1142
    }
slouken@5201
  1143
    rdata->shader_format_count = nFormats;
slouken@5201
  1144
#ifdef ZUNE_HD
slouken@5201
  1145
    rdata->shader_formats[0] = GL_NVIDIA_PLATFORM_BINARY_NV;
slouken@5201
  1146
#else /* !ZUNE_HD */
slouken@5201
  1147
    glGetIntegerv(GL_SHADER_BINARY_FORMATS, (GLint *)rdata->shader_formats);
slouken@5201
  1148
    if (glGetError() != GL_NO_ERROR)
slouken@5201
  1149
    {
slouken@5209
  1150
        GLES2_DestroyRenderer(renderer);
slouken@5201
  1151
        SDL_SetError("Failed to query supported shader formats");
slouken@5201
  1152
        return NULL;
slouken@5201
  1153
    }
slouken@5201
  1154
    if (hasCompiler)
slouken@5201
  1155
        rdata->shader_formats[nFormats - 1] = (GLenum)-1;
slouken@5201
  1156
#endif /* ZUNE_HD */
slouken@5201
  1157
slouken@5201
  1158
    /* Populate the function pointers for the module */
slouken@5201
  1159
    renderer->WindowEvent         = &GLES2_WindowEvent;
slouken@5201
  1160
    renderer->CreateTexture       = &GLES2_CreateTexture;
slouken@5201
  1161
    renderer->UpdateTexture       = &GLES2_UpdateTexture;
slouken@5201
  1162
    renderer->LockTexture         = &GLES2_LockTexture;
slouken@5201
  1163
    renderer->UnlockTexture       = &GLES2_UnlockTexture;
slouken@5297
  1164
    renderer->UpdateViewport      = &GLES2_UpdateViewport;
slouken@5201
  1165
    renderer->RenderClear         = &GLES2_RenderClear;
slouken@5201
  1166
    renderer->RenderDrawPoints    = &GLES2_RenderDrawPoints;
slouken@5201
  1167
    renderer->RenderDrawLines     = &GLES2_RenderDrawLines;
slouken@5201
  1168
    renderer->RenderFillRects     = &GLES2_RenderFillRects;
slouken@5201
  1169
    renderer->RenderCopy          = &GLES2_RenderCopy;
slouken@5201
  1170
    renderer->RenderPresent       = &GLES2_RenderPresent;
slouken@5201
  1171
    renderer->DestroyTexture      = &GLES2_DestroyTexture;
slouken@5201
  1172
    renderer->DestroyRenderer     = &GLES2_DestroyRenderer;
slouken@5201
  1173
    return renderer;
slouken@5201
  1174
}
slouken@5201
  1175
slouken@5226
  1176
#endif /* SDL_VIDEO_RENDER_OGL_ES2 && !SDL_RENDER_DISABLED */
slouken@5201
  1177
slouken@5201
  1178
/* vi: set ts=4 sw=4 expandtab: */