src/render/opengles2/SDL_render_gles2.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 08 Jan 2012 13:31:22 -0500
changeset 6190 519380462400
parent 6188 e82023802002
child 6232 37e8d0736366
permissions -rwxr-xr-x
X11 OpenGL ES minor corrections

Scott Percival 2012-01-08 04:21:22 PST

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