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