src/render/opengles2/SDL_render_gles2.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 09 Feb 2014 01:49:01 -0800
changeset 8192 560c66a04082
parent 8149 681eb46b8ac4
child 8256 bcfb214c2950
permissions -rw-r--r--
Fixed bug 2354 - [ES 2.0] SDL_RenderClear clears render target with wrong color

ny00

SDL_RenderClear clears a render target with the wrong color, if the opengles2 renderer driver is used and the target texture's format is SDL_PIXELFORMAT_ARGB8888.

The bug is *not* reproduced if SDL_PIXELFORMAT_ABGR8888 is used as the texture format (the first from the renderer's list).
It is further not reproduced using any of the following renderer drivers: opengl, opengles (apparently powered by Gallium3D), software.
Finally, the correct color can be drawn using SDL_RenderFillRect (instead of SDL_RenderClear).

A few details about the current setup:
- OS: Ubuntu 12.04 for x86_64
- GPU: GeForce GTX 460
- GPU driver version: 331.20-0ubuntu1~xedgers~precise1 (from the xorg-edgers PPA)


---

Seth Williams

Sam,

It appears that the clear just needs to take the render target format into consideration.

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