src/render/opengl/SDL_render_gl.c
author Gabriel Jacobo <gabomdq@gmail.com>
Fri, 01 Jun 2012 19:51:08 -0300
changeset 6320 6077a1310907
parent 6260 fd494c5f305b
child 6414 df50b0d6c1c3
permissions -rwxr-xr-x
RenderCopyEx,rotation and flipping for all hardware/software backends (#1308)
slouken@1918
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@1918
     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@1918
     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@1918
    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@1918
    20
*/
slouken@1918
    21
#include "SDL_config.h"
slouken@1918
    22
slouken@5226
    23
#if SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED
slouken@1920
    24
slouken@5233
    25
#include "SDL_hints.h"
slouken@5233
    26
#include "SDL_log.h"
slouken@1920
    27
#include "SDL_opengl.h"
slouken@5154
    28
#include "../SDL_sysrender.h"
slouken@5228
    29
#include "SDL_shaders_gl.h"
slouken@1918
    30
slouken@2246
    31
#ifdef __MACOSX__
slouken@2246
    32
#include <OpenGL/OpenGL.h>
slouken@2246
    33
#endif
slouken@2246
    34
slouken@2778
    35
slouken@1918
    36
/* OpenGL renderer implementation */
slouken@1918
    37
slouken@2230
    38
/* Details on optimizing the texture path on Mac OS X:
slouken@5203
    39
   http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/opengl_texturedata.html
slouken@2230
    40
*/
slouken@2230
    41
slouken@5154
    42
/* Used to re-create the window with OpenGL capability */
slouken@5154
    43
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
icculus@2835
    44
slouken@1985
    45
static const float inv255f = 1.0f / 255.0f;
slouken@1985
    46
slouken@1918
    47
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@5147
    48
static void GL_WindowEvent(SDL_Renderer * renderer,
slouken@5147
    49
                           const SDL_WindowEvent *event);
slouken@1918
    50
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    51
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    52
                            const SDL_Rect * rect, const void *pixels,
slouken@1918
    53
                            int pitch);
slouken@1918
    54
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
    55
                          const SDL_Rect * rect, void **pixels, int *pitch);
slouken@1918
    56
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@6247
    57
static int GL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@5297
    58
static int GL_UpdateViewport(SDL_Renderer * renderer);
slouken@3596
    59
static int GL_RenderClear(SDL_Renderer * renderer);
slouken@3596
    60
static int GL_RenderDrawPoints(SDL_Renderer * renderer,
slouken@3596
    61
                               const SDL_Point * points, int count);
slouken@3596
    62
static int GL_RenderDrawLines(SDL_Renderer * renderer,
slouken@3596
    63
                              const SDL_Point * points, int count);
slouken@3596
    64
static int GL_RenderFillRects(SDL_Renderer * renderer,
slouken@5297
    65
                              const SDL_Rect * rects, int count);
slouken@1918
    66
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    67
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
gabomdq@6320
    68
static int GL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
gabomdq@6320
    69
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect,
gabomdq@6320
    70
                         const double angle, const SDL_Point *center, const SDL_RendererFlip flip);
slouken@3431
    71
static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
    72
                               Uint32 pixel_format, void * pixels, int pitch);
slouken@1918
    73
static void GL_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    74
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    75
static void GL_DestroyRenderer(SDL_Renderer * renderer);
slouken@1918
    76
slouken@1918
    77
slouken@1918
    78
SDL_RenderDriver GL_RenderDriver = {
slouken@1918
    79
    GL_CreateRenderer,
slouken@1918
    80
    {
slouken@1918
    81
     "opengl",
slouken@6237
    82
     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
slouken@5156
    83
     1,
slouken@5156
    84
     {SDL_PIXELFORMAT_ARGB8888},
slouken@1918
    85
     0,
slouken@1918
    86
     0}
slouken@1918
    87
};
slouken@1918
    88
slouken@6232
    89
typedef struct GL_FBOList GL_FBOList;
slouken@6232
    90
slouken@6232
    91
struct GL_FBOList
slouken@6232
    92
{
slouken@6232
    93
    Uint32 w, h;
slouken@6232
    94
    GLuint FBO;
slouken@6232
    95
    GL_FBOList *next;
slouken@6232
    96
};
slouken@6232
    97
slouken@1918
    98
typedef struct
slouken@1918
    99
{
slouken@1918
   100
    SDL_GLContext context;
slouken@2233
   101
    SDL_bool GL_ARB_texture_rectangle_supported;
slouken@5355
   102
    struct {
slouken@5355
   103
        GL_Shader shader;
slouken@5355
   104
        Uint32 color;
slouken@5355
   105
        int blendMode;
slouken@5355
   106
    } current;
slouken@6232
   107
    
slouken@6232
   108
    SDL_bool GL_EXT_framebuffer_object_supported;
slouken@6232
   109
    GL_FBOList *framebuffers;
slouken@1927
   110
slouken@1927
   111
    /* OpenGL functions */
slouken@1927
   112
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
slouken@5204
   113
#include "SDL_glfuncs.h"
slouken@1927
   114
#undef SDL_PROC
slouken@1974
   115
slouken@5228
   116
    /* Multitexture support */
slouken@5228
   117
    SDL_bool GL_ARB_multitexture_supported;
slouken@5228
   118
    PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;
slouken@5351
   119
    GLint num_texture_units;
slouken@6232
   120
    
slouken@6232
   121
    PFNGLGENFRAMEBUFFERSEXTPROC glGenFramebuffersEXT;
slouken@6232
   122
    PFNGLDELETEFRAMEBUFFERSEXTPROC glDeleteFramebuffersEXT;
slouken@6232
   123
    PFNGLFRAMEBUFFERTEXTURE2DEXTPROC glFramebufferTexture2DEXT;
slouken@6232
   124
    PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT;
slouken@6232
   125
    PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC glCheckFramebufferStatusEXT;
slouken@5228
   126
slouken@5228
   127
    /* Shader support */
slouken@5228
   128
    GL_ShaderContext *shaders;
slouken@5228
   129
slouken@1918
   130
} GL_RenderData;
slouken@1918
   131
slouken@1918
   132
typedef struct
slouken@1918
   133
{
slouken@1918
   134
    GLuint texture;
slouken@1920
   135
    GLenum type;
slouken@1918
   136
    GLfloat texw;
slouken@1918
   137
    GLfloat texh;
slouken@1920
   138
    GLenum format;
slouken@1920
   139
    GLenum formattype;
slouken@1918
   140
    void *pixels;
slouken@1918
   141
    int pitch;
slouken@5227
   142
    SDL_Rect locked_rect;
slouken@5264
   143
slouken@5264
   144
    /* YV12 texture support */
slouken@5264
   145
    SDL_bool yuv;
slouken@5264
   146
    GLuint utexture;
slouken@5264
   147
    GLuint vtexture;
slouken@6232
   148
    
slouken@6232
   149
    GL_FBOList *fbo;
slouken@1918
   150
} GL_TextureData;
slouken@1918
   151
slouken@1918
   152
slouken@1924
   153
static void
slouken@1924
   154
GL_SetError(const char *prefix, GLenum result)
slouken@1924
   155
{
slouken@1924
   156
    const char *error;
slouken@1924
   157
slouken@1924
   158
    switch (result) {
slouken@1924
   159
    case GL_NO_ERROR:
slouken@1924
   160
        error = "GL_NO_ERROR";
slouken@1924
   161
        break;
slouken@1924
   162
    case GL_INVALID_ENUM:
slouken@1924
   163
        error = "GL_INVALID_ENUM";
slouken@1924
   164
        break;
slouken@1924
   165
    case GL_INVALID_VALUE:
slouken@1924
   166
        error = "GL_INVALID_VALUE";
slouken@1924
   167
        break;
slouken@1924
   168
    case GL_INVALID_OPERATION:
slouken@1924
   169
        error = "GL_INVALID_OPERATION";
slouken@1924
   170
        break;
slouken@1924
   171
    case GL_STACK_OVERFLOW:
slouken@1924
   172
        error = "GL_STACK_OVERFLOW";
slouken@1924
   173
        break;
slouken@1924
   174
    case GL_STACK_UNDERFLOW:
slouken@1924
   175
        error = "GL_STACK_UNDERFLOW";
slouken@1924
   176
        break;
slouken@1924
   177
    case GL_OUT_OF_MEMORY:
slouken@1924
   178
        error = "GL_OUT_OF_MEMORY";
slouken@1924
   179
        break;
slouken@1924
   180
    case GL_TABLE_TOO_LARGE:
slouken@1924
   181
        error = "GL_TABLE_TOO_LARGE";
slouken@1924
   182
        break;
slouken@1924
   183
    default:
slouken@1924
   184
        error = "UNKNOWN";
slouken@1924
   185
        break;
slouken@1924
   186
    }
slouken@1924
   187
    SDL_SetError("%s: %s", prefix, error);
slouken@1924
   188
}
slouken@1924
   189
slouken@1927
   190
static int
slouken@1927
   191
GL_LoadFunctions(GL_RenderData * data)
slouken@1927
   192
{
slouken@1927
   193
#ifdef __SDL_NOGETPROCADDR__
slouken@1927
   194
#define SDL_PROC(ret,func,params) data->func=func;
slouken@1927
   195
#else
slouken@1927
   196
#define SDL_PROC(ret,func,params) \
slouken@1927
   197
    do { \
slouken@1927
   198
        data->func = SDL_GL_GetProcAddress(#func); \
slouken@1927
   199
        if ( ! data->func ) { \
slouken@1927
   200
            SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
slouken@1927
   201
            return -1; \
slouken@1927
   202
        } \
slouken@1927
   203
    } while ( 0 );
slouken@1927
   204
#endif /* __SDL_NOGETPROCADDR__ */
slouken@1927
   205
slouken@5204
   206
#include "SDL_glfuncs.h"
slouken@1927
   207
#undef SDL_PROC
slouken@1927
   208
    return 0;
slouken@1927
   209
}
slouken@1927
   210
slouken@5297
   211
static SDL_GLContext SDL_CurrentContext = NULL;
slouken@5297
   212
slouken@5297
   213
static int
slouken@5297
   214
GL_ActivateRenderer(SDL_Renderer * renderer)
slouken@5297
   215
{
slouken@5297
   216
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5297
   217
slouken@5297
   218
    if (SDL_CurrentContext != data->context) {
slouken@5297
   219
        if (SDL_GL_MakeCurrent(renderer->window, data->context) < 0) {
slouken@5297
   220
            return -1;
slouken@5297
   221
        }
slouken@5297
   222
        SDL_CurrentContext = data->context;
slouken@5297
   223
slouken@5297
   224
        GL_UpdateViewport(renderer);
slouken@5297
   225
    }
slouken@5297
   226
    return 0;
slouken@5297
   227
}
slouken@5297
   228
slouken@5355
   229
/* This is called if we need to invalidate all of the SDL OpenGL state */
slouken@5355
   230
static void
slouken@5355
   231
GL_ResetState(SDL_Renderer *renderer)
slouken@5355
   232
{
slouken@5355
   233
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5355
   234
slouken@5355
   235
    if (SDL_CurrentContext == data->context) {
slouken@5355
   236
        GL_UpdateViewport(renderer);
slouken@5355
   237
    } else {
slouken@5355
   238
        GL_ActivateRenderer(renderer);
slouken@5355
   239
    }
slouken@5355
   240
slouken@5355
   241
    data->current.shader = SHADER_NONE;
slouken@5355
   242
    data->current.color = 0;
slouken@5355
   243
    data->current.blendMode = -1;
slouken@5355
   244
slouken@5355
   245
    data->glDisable(GL_DEPTH_TEST);
slouken@5355
   246
    data->glDisable(GL_CULL_FACE);
slouken@5355
   247
    /* This ended up causing video discrepancies between OpenGL and Direct3D */
slouken@5355
   248
    /*data->glEnable(GL_LINE_SMOOTH);*/
slouken@5355
   249
slouken@5355
   250
    data->glMatrixMode(GL_MODELVIEW);
slouken@5355
   251
    data->glLoadIdentity();
slouken@5355
   252
}
slouken@5355
   253
slouken@6232
   254
slouken@6232
   255
GL_FBOList *
slouken@6232
   256
GL_GetFBO(GL_RenderData *data, Uint32 w, Uint32 h)
slouken@6232
   257
{
slouken@6232
   258
    GL_FBOList *result = data->framebuffers;
slouken@6232
   259
slouken@6232
   260
    while (result && ((result->w != w) || (result->h != h))) {
slouken@6232
   261
        result = result->next;
slouken@6232
   262
    }
slouken@6232
   263
slouken@6232
   264
    if (!result) {
slouken@6232
   265
        result = SDL_malloc(sizeof(GL_FBOList));
slouken@6232
   266
        if (result) {
slouken@6232
   267
            result->w = w;
slouken@6232
   268
            result->h = h;
slouken@6232
   269
            data->glGenFramebuffersEXT(1, &result->FBO);
slouken@6232
   270
            result->next = data->framebuffers;
slouken@6232
   271
            data->framebuffers = result;
slouken@6232
   272
        }
slouken@6232
   273
    }
slouken@6232
   274
    return result;
slouken@6232
   275
}
slouken@6232
   276
slouken@1918
   277
SDL_Renderer *
slouken@1918
   278
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1918
   279
{
slouken@1918
   280
    SDL_Renderer *renderer;
slouken@1918
   281
    GL_RenderData *data;
slouken@5233
   282
    const char *hint;
slouken@1952
   283
    GLint value;
slouken@5154
   284
    Uint32 window_flags;
slouken@1918
   285
slouken@5154
   286
    window_flags = SDL_GetWindowFlags(window);
slouken@5154
   287
    if (!(window_flags & SDL_WINDOW_OPENGL)) {
slouken@5154
   288
        if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
slouken@6182
   289
            /* Uh oh, better try to put it back... */
slouken@6182
   290
            SDL_RecreateWindow(window, window_flags);
slouken@1924
   291
            return NULL;
slouken@1924
   292
        }
slouken@1918
   293
    }
slouken@1918
   294
slouken@1920
   295
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1918
   296
    if (!renderer) {
slouken@1918
   297
        SDL_OutOfMemory();
slouken@1918
   298
        return NULL;
slouken@1918
   299
    }
slouken@1918
   300
slouken@1920
   301
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   302
    if (!data) {
slouken@1918
   303
        GL_DestroyRenderer(renderer);
slouken@1918
   304
        SDL_OutOfMemory();
slouken@1918
   305
        return NULL;
slouken@1918
   306
    }
slouken@1918
   307
slouken@5147
   308
    renderer->WindowEvent = GL_WindowEvent;
slouken@1918
   309
    renderer->CreateTexture = GL_CreateTexture;
slouken@1918
   310
    renderer->UpdateTexture = GL_UpdateTexture;
slouken@1918
   311
    renderer->LockTexture = GL_LockTexture;
slouken@1918
   312
    renderer->UnlockTexture = GL_UnlockTexture;
slouken@6247
   313
    renderer->SetRenderTarget = GL_SetRenderTarget;
slouken@5297
   314
    renderer->UpdateViewport = GL_UpdateViewport;
slouken@3596
   315
    renderer->RenderClear = GL_RenderClear;
slouken@3596
   316
    renderer->RenderDrawPoints = GL_RenderDrawPoints;
slouken@3596
   317
    renderer->RenderDrawLines = GL_RenderDrawLines;
slouken@3596
   318
    renderer->RenderFillRects = GL_RenderFillRects;
slouken@1918
   319
    renderer->RenderCopy = GL_RenderCopy;
gabomdq@6320
   320
    renderer->RenderCopyEx = GL_RenderCopyEx;
slouken@3431
   321
    renderer->RenderReadPixels = GL_RenderReadPixels;
slouken@1918
   322
    renderer->RenderPresent = GL_RenderPresent;
slouken@1918
   323
    renderer->DestroyTexture = GL_DestroyTexture;
slouken@1918
   324
    renderer->DestroyRenderer = GL_DestroyRenderer;
slouken@1918
   325
    renderer->info = GL_RenderDriver.info;
slouken@6246
   326
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@1918
   327
    renderer->driverdata = data;
slouken@6171
   328
    renderer->window = window;
slouken@1918
   329
slouken@3685
   330
    data->context = SDL_GL_CreateContext(window);
slouken@1918
   331
    if (!data->context) {
slouken@1918
   332
        GL_DestroyRenderer(renderer);
slouken@1918
   333
        return NULL;
slouken@1918
   334
    }
slouken@3685
   335
    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
slouken@1918
   336
        GL_DestroyRenderer(renderer);
slouken@1918
   337
        return NULL;
slouken@1918
   338
    }
slouken@5204
   339
slouken@5204
   340
    if (GL_LoadFunctions(data) < 0) {
slouken@5204
   341
        GL_DestroyRenderer(renderer);
slouken@5204
   342
        return NULL;
slouken@5204
   343
    }
slouken@5204
   344
slouken@2246
   345
#ifdef __MACOSX__
slouken@2246
   346
    /* Enable multi-threaded rendering */
slouken@2246
   347
    /* Disabled until Ryan finishes his VBO/PBO code...
bob@2295
   348
       CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
bob@2295
   349
     */
slouken@2246
   350
#endif
slouken@2246
   351
slouken@1965
   352
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1918
   353
        SDL_GL_SetSwapInterval(1);
slouken@1918
   354
    } else {
slouken@1918
   355
        SDL_GL_SetSwapInterval(0);
slouken@1918
   356
    }
slouken@1918
   357
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@1965
   358
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1918
   359
    }
slouken@1918
   360
slouken@1952
   361
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   362
    renderer->info.max_texture_width = value;
slouken@1952
   363
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   364
    renderer->info.max_texture_height = value;
slouken@1920
   365
slouken@1926
   366
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
slouken@1926
   367
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
slouken@1926
   368
        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
slouken@1926
   369
    }
slouken@1920
   370
slouken@5228
   371
    /* Check for multitexture support */
slouken@5228
   372
    if (SDL_GL_ExtensionSupported("GL_ARB_multitexture")) {
slouken@5228
   373
        data->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glActiveTextureARB");
slouken@5228
   374
        if (data->glActiveTextureARB) {
slouken@5228
   375
            data->GL_ARB_multitexture_supported = SDL_TRUE;
slouken@5228
   376
            data->glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &data->num_texture_units);
slouken@5228
   377
        }
slouken@5228
   378
    }
slouken@5228
   379
slouken@5228
   380
    /* Check for shader support */
slouken@5233
   381
    hint = SDL_GetHint(SDL_HINT_RENDER_OPENGL_SHADERS);
slouken@5233
   382
    if (!hint || *hint != '0') {
slouken@5233
   383
        data->shaders = GL_CreateShaderContext();
slouken@5233
   384
    }
slouken@5233
   385
    SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, "OpenGL shaders: %s",
slouken@5233
   386
                data->shaders ? "ENABLED" : "DISABLED");
slouken@5228
   387
slouken@5228
   388
    /* We support YV12 textures using 3 textures and a shader */
slouken@5228
   389
    if (data->shaders && data->num_texture_units >= 3) {
slouken@5228
   390
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_YV12;
slouken@5264
   391
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_IYUV;
slouken@5228
   392
    }
slouken@6232
   393
    
slouken@6232
   394
    if (SDL_GL_ExtensionSupported("GL_EXT_framebuffer_object")) {
slouken@6232
   395
        data->GL_EXT_framebuffer_object_supported = SDL_TRUE;
slouken@6232
   396
        data->glGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC)
slouken@6232
   397
            SDL_GL_GetProcAddress("glGenFramebuffersEXT");
slouken@6232
   398
        data->glDeleteFramebuffersEXT = (PFNGLDELETEFRAMEBUFFERSEXTPROC)
slouken@6232
   399
            SDL_GL_GetProcAddress("glDeleteFramebuffersEXT");
slouken@6232
   400
        data->glFramebufferTexture2DEXT = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)
slouken@6232
   401
            SDL_GL_GetProcAddress("glFramebufferTexture2DEXT");
slouken@6232
   402
        data->glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)
slouken@6232
   403
            SDL_GL_GetProcAddress("glBindFramebufferEXT");
slouken@6246
   404
        data->glCheckFramebufferStatusEXT = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)
slouken@6232
   405
            SDL_GL_GetProcAddress("glCheckFramebufferStatusEXT");
slouken@6246
   406
        renderer->info.flags |= SDL_RENDERER_TARGETTEXTURE;
slouken@6232
   407
    }
slouken@6232
   408
    data->framebuffers = NULL;
slouken@5228
   409
slouken@1918
   410
    /* Set up parameters for rendering */
slouken@5355
   411
    GL_ResetState(renderer);
slouken@1918
   412
slouken@1918
   413
    return renderer;
slouken@1918
   414
}
slouken@1918
   415
slouken@5147
   416
static void
slouken@5147
   417
GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1970
   418
{
slouken@6260
   419
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED ||
slouken@6260
   420
        event->event == SDL_WINDOWEVENT_SHOWN ||
slouken@6260
   421
        event->event == SDL_WINDOWEVENT_HIDDEN) {
slouken@5147
   422
        /* Rebind the context to the window area and update matrices */
slouken@5147
   423
        SDL_CurrentContext = NULL;
slouken@5147
   424
    }
slouken@1923
   425
}
slouken@1923
   426
slouken@1922
   427
static __inline__ int
slouken@1922
   428
power_of_2(int input)
slouken@1922
   429
{
slouken@1922
   430
    int value = 1;
slouken@1922
   431
slouken@1922
   432
    while (value < input) {
slouken@1922
   433
        value <<= 1;
slouken@1922
   434
    }
slouken@1922
   435
    return value;
slouken@1922
   436
}
slouken@1922
   437
slouken@3433
   438
static __inline__ SDL_bool
slouken@3433
   439
convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
slouken@3433
   440
               GLint* internalFormat, GLenum* format, GLenum* type)
slouken@3433
   441
{
slouken@3433
   442
    switch (pixel_format) {
slouken@3433
   443
    case SDL_PIXELFORMAT_ARGB8888:
slouken@5156
   444
        *internalFormat = GL_RGBA8;
slouken@3433
   445
        *format = GL_BGRA;
slouken@3433
   446
        *type = GL_UNSIGNED_INT_8_8_8_8_REV;
slouken@3433
   447
        break;
slouken@5264
   448
    case SDL_PIXELFORMAT_YV12:
slouken@5264
   449
    case SDL_PIXELFORMAT_IYUV:
slouken@5264
   450
        *internalFormat = GL_LUMINANCE;
slouken@5264
   451
        *format = GL_LUMINANCE;
slouken@5264
   452
        *type = GL_UNSIGNED_BYTE;
slouken@5264
   453
        break;
slouken@3433
   454
    default:
slouken@3433
   455
        return SDL_FALSE;
slouken@3433
   456
    }
slouken@3433
   457
    return SDL_TRUE;
slouken@3433
   458
}
icculus@2835
   459
slouken@5484
   460
static GLenum
slouken@5484
   461
GetScaleQuality(void)
slouken@5484
   462
{
slouken@5484
   463
    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
slouken@5484
   464
slouken@5484
   465
    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
slouken@5484
   466
        return GL_NEAREST;
slouken@5484
   467
    } else {
slouken@5484
   468
        return GL_LINEAR;
slouken@5484
   469
    }
slouken@5484
   470
}
slouken@5484
   471
slouken@1918
   472
static int
slouken@1918
   473
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   474
{
slouken@1918
   475
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   476
    GL_TextureData *data;
slouken@1920
   477
    GLint internalFormat;
slouken@1920
   478
    GLenum format, type;
slouken@1922
   479
    int texture_w, texture_h;
slouken@5503
   480
    GLenum scaleMode;
slouken@1924
   481
    GLenum result;
slouken@1918
   482
slouken@5147
   483
    GL_ActivateRenderer(renderer);
slouken@5147
   484
slouken@3433
   485
    if (!convert_format(renderdata, texture->format, &internalFormat,
slouken@3433
   486
                        &format, &type)) {
slouken@4990
   487
        SDL_SetError("Texture format %s not supported by OpenGL",
slouken@4990
   488
                     SDL_GetPixelFormatName(texture->format));
slouken@1920
   489
        return -1;
slouken@1920
   490
    }
slouken@1920
   491
slouken@1920
   492
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   493
    if (!data) {
slouken@1918
   494
        SDL_OutOfMemory();
slouken@1918
   495
        return -1;
slouken@1918
   496
    }
slouken@1918
   497
slouken@2222
   498
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5264
   499
        size_t size;
slouken@5156
   500
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
slouken@5264
   501
        size = texture->h * data->pitch;
slouken@5264
   502
        if (texture->format == SDL_PIXELFORMAT_YV12 ||
slouken@5264
   503
            texture->format == SDL_PIXELFORMAT_IYUV) {
slouken@5264
   504
            /* Need to add size for the U and V planes */
slouken@5264
   505
            size += (2 * (texture->h * data->pitch) / 4);
slouken@5264
   506
        }
slouken@5402
   507
        data->pixels = SDL_calloc(1, size);
slouken@2222
   508
        if (!data->pixels) {
slouken@2222
   509
            SDL_OutOfMemory();
slouken@2222
   510
            SDL_free(data);
slouken@2222
   511
            return -1;
slouken@2222
   512
        }
slouken@2222
   513
    }
slouken@2222
   514
slouken@1918
   515
    texture->driverdata = data;
slouken@6232
   516
    
slouken@6232
   517
    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
slouken@6232
   518
        data->fbo = GL_GetFBO(renderdata, texture->w, texture->h);
slouken@6232
   519
    } else {
slouken@6232
   520
        data->fbo = NULL;
slouken@6232
   521
    }
slouken@1918
   522
slouken@1927
   523
    renderdata->glGetError();
slouken@1927
   524
    renderdata->glGenTextures(1, &data->texture);
slouken@6232
   525
    if ((renderdata->GL_ARB_texture_rectangle_supported)
slouken@6232
   526
        /*&& texture->access != SDL_TEXTUREACCESS_TARGET*/){
slouken@1926
   527
        data->type = GL_TEXTURE_RECTANGLE_ARB;
slouken@1926
   528
        texture_w = texture->w;
slouken@1926
   529
        texture_h = texture->h;
icculus@2835
   530
        data->texw = (GLfloat) texture_w;
icculus@2835
   531
        data->texh = (GLfloat) texture_h;
slouken@1926
   532
    } else {
slouken@1926
   533
        data->type = GL_TEXTURE_2D;
slouken@1926
   534
        texture_w = power_of_2(texture->w);
slouken@1926
   535
        texture_h = power_of_2(texture->h);
icculus@2835
   536
        data->texw = (GLfloat) (texture->w) / texture_w;
slouken@1926
   537
        data->texh = (GLfloat) texture->h / texture_h;
slouken@1926
   538
    }
icculus@2835
   539
slouken@1920
   540
    data->format = format;
slouken@1920
   541
    data->formattype = type;
slouken@5503
   542
    scaleMode = GetScaleQuality();
slouken@2884
   543
    renderdata->glEnable(data->type);
slouken@1927
   544
    renderdata->glBindTexture(data->type, data->texture);
slouken@5503
   545
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER, scaleMode);
slouken@5503
   546
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER, scaleMode);
slouken@6207
   547
    /* According to the spec, CLAMP_TO_EDGE is the default for TEXTURE_RECTANGLE
slouken@6207
   548
       and setting it causes an INVALID_ENUM error in the latest NVidia drivers.
slouken@6207
   549
    */
slouken@6207
   550
    if (data->type != GL_TEXTURE_RECTANGLE_ARB) {
slouken@6207
   551
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@6207
   552
                                    GL_CLAMP_TO_EDGE);
slouken@6207
   553
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@6207
   554
                                    GL_CLAMP_TO_EDGE);
slouken@6207
   555
    }
slouken@2840
   556
#ifdef __MACOSX__
slouken@2230
   557
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
slouken@2230
   558
#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
slouken@2230
   559
#endif
slouken@2230
   560
#ifndef STORAGE_CACHED_APPLE
slouken@2230
   561
#define STORAGE_CACHED_APPLE                0x85BE
slouken@2230
   562
#endif
slouken@2230
   563
#ifndef STORAGE_SHARED_APPLE
slouken@2230
   564
#define STORAGE_SHARED_APPLE                0x85BF
slouken@2230
   565
#endif
slouken@2230
   566
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@2230
   567
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   568
                                    GL_STORAGE_SHARED_APPLE);
slouken@2230
   569
    } else {
slouken@2230
   570
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   571
                                    GL_STORAGE_CACHED_APPLE);
slouken@2230
   572
    }
bob@2295
   573
    if (texture->access == SDL_TEXTUREACCESS_STREAMING
slouken@5397
   574
        && texture->format == SDL_PIXELFORMAT_ARGB8888
slouken@5397
   575
        && (texture->w % 8) == 0) {
slouken@2230
   576
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
slouken@5397
   577
        renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@5397
   578
        renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@5397
   579
                          (data->pitch / SDL_BYTESPERPIXEL(texture->format)));
slouken@2230
   580
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   581
                                 texture_h, 0, format, type, data->pixels);
slouken@5397
   582
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
slouken@5156
   583
    }
slouken@5156
   584
    else
slouken@2809
   585
#endif
slouken@2230
   586
    {
slouken@2230
   587
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   588
                                 texture_h, 0, format, type, NULL);
slouken@2230
   589
    }
slouken@3041
   590
    renderdata->glDisable(data->type);
slouken@1927
   591
    result = renderdata->glGetError();
slouken@1924
   592
    if (result != GL_NO_ERROR) {
slouken@1924
   593
        GL_SetError("glTexImage2D()", result);
slouken@1924
   594
        return -1;
slouken@1924
   595
    }
slouken@5264
   596
slouken@5264
   597
    if (texture->format == SDL_PIXELFORMAT_YV12 ||
slouken@5264
   598
        texture->format == SDL_PIXELFORMAT_IYUV) {
slouken@5264
   599
        data->yuv = SDL_TRUE;
slouken@5264
   600
slouken@5264
   601
        renderdata->glGenTextures(1, &data->utexture);
slouken@5264
   602
        renderdata->glGenTextures(1, &data->vtexture);
slouken@5264
   603
        renderdata->glEnable(data->type);
slouken@5264
   604
slouken@5264
   605
        renderdata->glBindTexture(data->type, data->utexture);
slouken@5503
   606
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@5503
   607
                                    scaleMode);
slouken@5503
   608
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@5503
   609
                                    scaleMode);
slouken@5264
   610
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@5264
   611
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   612
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@5264
   613
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   614
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w/2,
slouken@5264
   615
                                 texture_h/2, 0, format, type, NULL);
slouken@5264
   616
slouken@5264
   617
        renderdata->glBindTexture(data->type, data->vtexture);
slouken@5503
   618
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@5503
   619
                                    scaleMode);
slouken@5503
   620
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@5503
   621
                                    scaleMode);
slouken@5264
   622
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@5264
   623
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   624
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@5264
   625
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   626
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w/2,
slouken@5264
   627
                                 texture_h/2, 0, format, type, NULL);
slouken@5264
   628
slouken@5264
   629
        renderdata->glDisable(data->type);
slouken@5264
   630
    }
slouken@1918
   631
    return 0;
slouken@1918
   632
}
slouken@1918
   633
slouken@1918
   634
static int
slouken@1918
   635
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   636
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
   637
{
slouken@1927
   638
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   639
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1924
   640
    GLenum result;
slouken@1918
   641
slouken@5147
   642
    GL_ActivateRenderer(renderer);
slouken@5147
   643
slouken@1927
   644
    renderdata->glGetError();
slouken@5397
   645
    renderdata->glEnable(data->type);
slouken@5397
   646
    renderdata->glBindTexture(data->type, data->texture);
slouken@5227
   647
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@5227
   648
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@5227
   649
                              (pitch / SDL_BYTESPERPIXEL(texture->format)));
slouken@1927
   650
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
slouken@1927
   651
                                rect->h, data->format, data->formattype,
slouken@1927
   652
                                pixels);
slouken@5264
   653
    if (data->yuv) {
slouken@5265
   654
        renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, (pitch / 2));
slouken@5265
   655
slouken@5264
   656
        /* Skip to the correct offset into the next texture */
slouken@6135
   657
        pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
slouken@5264
   658
        if (texture->format == SDL_PIXELFORMAT_YV12) {
slouken@5264
   659
            renderdata->glBindTexture(data->type, data->vtexture);
slouken@5264
   660
        } else {
slouken@5264
   661
            renderdata->glBindTexture(data->type, data->utexture);
slouken@5264
   662
        }
slouken@5264
   663
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@5264
   664
                                    rect->w/2, rect->h/2,
slouken@5264
   665
                                    data->format, data->formattype, pixels);
slouken@5264
   666
slouken@5264
   667
        /* Skip to the correct offset into the next texture */
slouken@6135
   668
        pixels = (const void*)((const Uint8*)pixels + (rect->h * pitch)/4);
slouken@5264
   669
        if (texture->format == SDL_PIXELFORMAT_YV12) {
slouken@5264
   670
            renderdata->glBindTexture(data->type, data->utexture);
slouken@5264
   671
        } else {
slouken@5264
   672
            renderdata->glBindTexture(data->type, data->vtexture);
slouken@5264
   673
        }
slouken@5264
   674
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@5264
   675
                                    rect->w/2, rect->h/2,
slouken@5264
   676
                                    data->format, data->formattype, pixels);
slouken@5264
   677
    }
slouken@3041
   678
    renderdata->glDisable(data->type);
slouken@1927
   679
    result = renderdata->glGetError();
slouken@1924
   680
    if (result != GL_NO_ERROR) {
slouken@1924
   681
        GL_SetError("glTexSubImage2D()", result);
slouken@1924
   682
        return -1;
slouken@1924
   683
    }
slouken@1918
   684
    return 0;
slouken@1918
   685
}
slouken@1918
   686
slouken@1918
   687
static int
slouken@1918
   688
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   689
               const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1918
   690
{
slouken@1918
   691
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   692
slouken@5227
   693
    data->locked_rect = *rect;
slouken@5227
   694
    *pixels = 
slouken@1920
   695
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@5156
   696
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@1920
   697
    *pitch = data->pitch;
slouken@1918
   698
    return 0;
slouken@1918
   699
}
slouken@1918
   700
slouken@1918
   701
static void
slouken@1918
   702
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   703
{
slouken@5156
   704
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@5227
   705
    const SDL_Rect *rect;
slouken@5227
   706
    void *pixels;
slouken@1918
   707
slouken@5227
   708
    rect = &data->locked_rect;
slouken@5227
   709
    pixels = 
slouken@5227
   710
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@5227
   711
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5227
   712
    GL_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
slouken@1918
   713
}
slouken@1918
   714
slouken@5297
   715
static int
slouken@6247
   716
GL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@6246
   717
{
slouken@6246
   718
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;    
slouken@6246
   719
    GL_TextureData *texturedata;
slouken@6246
   720
    GLenum status;
slouken@6246
   721
slouken@6246
   722
    GL_ActivateRenderer(renderer);
slouken@6246
   723
    
slouken@6246
   724
    if (texture == NULL) {
slouken@6246
   725
        data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
slouken@6246
   726
        return 0;
slouken@6246
   727
    }
slouken@6246
   728
slouken@6246
   729
    texturedata = (GL_TextureData *) texture->driverdata;
slouken@6246
   730
    data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, texturedata->fbo->FBO);
slouken@6246
   731
    /* TODO: check if texture pixel format allows this operation */
slouken@6246
   732
    data->glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, texturedata->type, texturedata->texture, 0);
slouken@6246
   733
    /* Check FBO status */
slouken@6246
   734
    status = data->glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
slouken@6246
   735
    if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
slouken@6246
   736
        SDL_SetError("glFramebufferTexture2DEXT() failed");
slouken@6246
   737
        return -1;
slouken@6246
   738
    }
slouken@6246
   739
    return 0;
slouken@6246
   740
}
slouken@6246
   741
slouken@6246
   742
static int
slouken@5297
   743
GL_UpdateViewport(SDL_Renderer * renderer)
slouken@5224
   744
{
slouken@5224
   745
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5224
   746
slouken@5297
   747
    if (SDL_CurrentContext != data->context) {
slouken@5297
   748
        /* We'll update the viewport after we rebind the context */
slouken@5297
   749
        return 0;
slouken@5297
   750
    }
slouken@5224
   751
slouken@5297
   752
    data->glViewport(renderer->viewport.x, renderer->viewport.y,
slouken@5297
   753
                     renderer->viewport.w, renderer->viewport.h);
slouken@5224
   754
slouken@5297
   755
    data->glMatrixMode(GL_PROJECTION);
slouken@5297
   756
    data->glLoadIdentity();
slouken@6246
   757
    if (renderer->target) {
slouken@6246
   758
        data->glOrtho((GLdouble) 0,
slouken@6246
   759
                      (GLdouble) renderer->viewport.w,
slouken@6246
   760
                      (GLdouble) 0,
slouken@6246
   761
                      (GLdouble) renderer->viewport.h,
slouken@6246
   762
                       0.0, 1.0);
slouken@6246
   763
    } else {
slouken@6246
   764
        data->glOrtho((GLdouble) 0,
slouken@6246
   765
                      (GLdouble) renderer->viewport.w,
slouken@6246
   766
                      (GLdouble) renderer->viewport.h,
slouken@6246
   767
                      (GLdouble) 0,
slouken@6246
   768
                       0.0, 1.0);
slouken@6246
   769
    }
slouken@5297
   770
    return 0;
slouken@5224
   771
}
slouken@5224
   772
slouken@5224
   773
static void
slouken@5355
   774
GL_SetShader(GL_RenderData * data, GL_Shader shader)
slouken@5355
   775
{
slouken@5355
   776
    if (data->shaders && shader != data->current.shader) {
slouken@5355
   777
        GL_SelectShader(data->shaders, shader);
slouken@5355
   778
        data->current.shader = shader;
slouken@5355
   779
    }
slouken@5355
   780
}
slouken@5355
   781
slouken@5355
   782
static void
slouken@5355
   783
GL_SetColor(GL_RenderData * data, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@5355
   784
{
slouken@5355
   785
    Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
slouken@5355
   786
slouken@5355
   787
    if (color != data->current.color) {
slouken@5355
   788
        data->glColor4f((GLfloat) r * inv255f,
slouken@5355
   789
                        (GLfloat) g * inv255f,
slouken@5355
   790
                        (GLfloat) b * inv255f,
slouken@5355
   791
                        (GLfloat) a * inv255f);
slouken@5355
   792
        data->current.color = color;
slouken@5355
   793
    }
slouken@5355
   794
}
slouken@5355
   795
slouken@5355
   796
static void
slouken@5140
   797
GL_SetBlendMode(GL_RenderData * data, int blendMode)
slouken@2936
   798
{
slouken@5355
   799
    if (blendMode != data->current.blendMode) {
slouken@2936
   800
        switch (blendMode) {
slouken@2936
   801
        case SDL_BLENDMODE_NONE:
slouken@2936
   802
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
slouken@2936
   803
            data->glDisable(GL_BLEND);
slouken@2936
   804
            break;
slouken@2936
   805
        case SDL_BLENDMODE_BLEND:
slouken@2936
   806
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
   807
            data->glEnable(GL_BLEND);
slouken@2936
   808
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@2936
   809
            break;
slouken@2936
   810
        case SDL_BLENDMODE_ADD:
slouken@2936
   811
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
   812
            data->glEnable(GL_BLEND);
slouken@2936
   813
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@2936
   814
            break;
slouken@5184
   815
        case SDL_BLENDMODE_MOD:
slouken@5184
   816
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@5184
   817
            data->glEnable(GL_BLEND);
slouken@5184
   818
            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@5184
   819
            break;
slouken@2936
   820
        }
slouken@5355
   821
        data->current.blendMode = blendMode;
slouken@2936
   822
    }
slouken@2936
   823
}
slouken@2936
   824
slouken@5355
   825
static void
slouken@5355
   826
GL_SetDrawingState(SDL_Renderer * renderer)
slouken@5355
   827
{
slouken@5355
   828
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5355
   829
slouken@5355
   830
    GL_ActivateRenderer(renderer);
slouken@5355
   831
slouken@5381
   832
    GL_SetColor(data, renderer->r,
slouken@5381
   833
                      renderer->g,
slouken@5381
   834
                      renderer->b,
slouken@5381
   835
                      renderer->a);
slouken@5355
   836
slouken@5355
   837
    GL_SetBlendMode(data, renderer->blendMode);
slouken@5355
   838
slouken@5355
   839
    GL_SetShader(data, SHADER_SOLID);
slouken@5355
   840
}
slouken@5355
   841
slouken@1918
   842
static int
slouken@3596
   843
GL_RenderClear(SDL_Renderer * renderer)
slouken@3596
   844
{
slouken@3596
   845
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3596
   846
slouken@5147
   847
    GL_ActivateRenderer(renderer);
slouken@5147
   848
slouken@3596
   849
    data->glClearColor((GLfloat) renderer->r * inv255f,
slouken@3596
   850
                       (GLfloat) renderer->g * inv255f,
slouken@3596
   851
                       (GLfloat) renderer->b * inv255f,
slouken@3596
   852
                       (GLfloat) renderer->a * inv255f);
slouken@3596
   853
slouken@3596
   854
    data->glClear(GL_COLOR_BUFFER_BIT);
slouken@3596
   855
slouken@3596
   856
    return 0;
slouken@3596
   857
}
slouken@3596
   858
slouken@3596
   859
static int
slouken@3596
   860
GL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   861
                    int count)
slouken@2884
   862
{
slouken@2884
   863
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   864
    int i;
slouken@2884
   865
slouken@5355
   866
    GL_SetDrawingState(renderer);
slouken@2884
   867
slouken@2901
   868
    data->glBegin(GL_POINTS);
slouken@3536
   869
    for (i = 0; i < count; ++i) {
slouken@3536
   870
        data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   871
    }
slouken@2901
   872
    data->glEnd();
slouken@2901
   873
slouken@2901
   874
    return 0;
slouken@2901
   875
}
slouken@2901
   876
slouken@2901
   877
static int
slouken@3596
   878
GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   879
                   int count)
slouken@2901
   880
{
slouken@2901
   881
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   882
    int i;
slouken@2901
   883
slouken@5355
   884
    GL_SetDrawingState(renderer);
slouken@2901
   885
slouken@3536
   886
    if (count > 2 && 
slouken@3536
   887
        points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
slouken@3536
   888
        data->glBegin(GL_LINE_LOOP);
slouken@3536
   889
        /* GL_LINE_LOOP takes care of the final segment */
slouken@3536
   890
        --count;
slouken@3536
   891
        for (i = 0; i < count; ++i) {
slouken@3536
   892
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   893
        }
slouken@3536
   894
        data->glEnd();
slouken@3536
   895
    } else {
slouken@5086
   896
#if defined(__APPLE__) || defined(__WIN32__)
aschiffler@4910
   897
#else
slouken@4905
   898
        int x1, y1, x2, y2;
aschiffler@4910
   899
#endif
slouken@4905
   900
slouken@3536
   901
        data->glBegin(GL_LINE_STRIP);
slouken@3536
   902
        for (i = 0; i < count; ++i) {
slouken@3536
   903
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   904
        }
slouken@3536
   905
        data->glEnd();
slouken@3474
   906
slouken@3536
   907
        /* The line is half open, so we need one more point to complete it.
slouken@3536
   908
         * http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node47.html
slouken@3536
   909
         * If we have to, we can use vertical line and horizontal line textures
slouken@3536
   910
         * for vertical and horizontal lines, and then create custom textures
slouken@3536
   911
         * for diagonal lines and software render those.  It's terrible, but at
slouken@3536
   912
         * least it would be pixel perfect.
slouken@3536
   913
         */
slouken@3536
   914
        data->glBegin(GL_POINTS);
slouken@5086
   915
#if defined(__APPLE__) || defined(__WIN32__)
slouken@3536
   916
        /* Mac OS X and Windows seem to always leave the second point open */
slouken@3536
   917
        data->glVertex2f(0.5f + points[count-1].x, 0.5f + points[count-1].y);
slouken@3474
   918
#else
slouken@3536
   919
        /* Linux seems to leave the right-most or bottom-most point open */
slouken@4905
   920
        x1 = points[0].x;
slouken@4905
   921
        y1 = points[0].y;
slouken@4905
   922
        x2 = points[count-1].x;
slouken@4905
   923
        y2 = points[count-1].y;
slouken@3536
   924
slouken@3536
   925
        if (x1 > x2) {
slouken@3536
   926
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@3536
   927
        } else if (x2 > x1) {
slouken@3536
   928
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@3536
   929
        } else if (y1 > y2) {
slouken@3536
   930
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@3536
   931
        } else if (y2 > y1) {
slouken@3536
   932
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@3536
   933
        }
slouken@3536
   934
#endif
slouken@3536
   935
        data->glEnd();
slouken@3474
   936
    }
slouken@3455
   937
slouken@1918
   938
    return 0;
slouken@1918
   939
}
slouken@1918
   940
slouken@1918
   941
static int
slouken@5297
   942
GL_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect * rects, int count)
slouken@2925
   943
{
slouken@2925
   944
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   945
    int i;
slouken@2925
   946
slouken@5355
   947
    GL_SetDrawingState(renderer);
slouken@2936
   948
slouken@3536
   949
    for (i = 0; i < count; ++i) {
slouken@5297
   950
        const SDL_Rect *rect = &rects[i];
slouken@3536
   951
slouken@3536
   952
        data->glRecti(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
slouken@3536
   953
    }
slouken@2925
   954
slouken@2925
   955
    return 0;
slouken@2925
   956
}
slouken@2925
   957
slouken@2925
   958
static int
slouken@1918
   959
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   960
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1918
   961
{
slouken@1918
   962
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   963
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@1918
   964
    int minx, miny, maxx, maxy;
slouken@1918
   965
    GLfloat minu, maxu, minv, maxv;
slouken@1918
   966
slouken@5147
   967
    GL_ActivateRenderer(renderer);
slouken@5147
   968
slouken@5355
   969
    data->glEnable(texturedata->type);
slouken@5355
   970
    if (texturedata->yuv) {
slouken@5355
   971
        data->glActiveTextureARB(GL_TEXTURE2_ARB);
slouken@5355
   972
        data->glBindTexture(texturedata->type, texturedata->vtexture);
slouken@5355
   973
slouken@5355
   974
        data->glActiveTextureARB(GL_TEXTURE1_ARB);
slouken@5355
   975
        data->glBindTexture(texturedata->type, texturedata->utexture);
slouken@5355
   976
slouken@5355
   977
        data->glActiveTextureARB(GL_TEXTURE0_ARB);
slouken@5355
   978
    }
slouken@5355
   979
    data->glBindTexture(texturedata->type, texturedata->texture);
slouken@5355
   980
slouken@5355
   981
    if (texture->modMode) {
slouken@5355
   982
        GL_SetColor(data, texture->r, texture->g, texture->b, texture->a);
slouken@5355
   983
    } else {
slouken@5355
   984
        GL_SetColor(data, 255, 255, 255, 255);
slouken@5355
   985
    }
slouken@5355
   986
slouken@5355
   987
    GL_SetBlendMode(data, texture->blendMode);
slouken@5355
   988
slouken@5355
   989
    if (texturedata->yuv) {
slouken@5355
   990
        GL_SetShader(data, SHADER_YV12);
slouken@5355
   991
    } else {
slouken@5355
   992
        GL_SetShader(data, SHADER_RGB);
slouken@5355
   993
    }
slouken@5355
   994
slouken@1918
   995
    minx = dstrect->x;
slouken@1918
   996
    miny = dstrect->y;
slouken@1918
   997
    maxx = dstrect->x + dstrect->w;
slouken@1918
   998
    maxy = dstrect->y + dstrect->h;
slouken@1918
   999
slouken@1918
  1000
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
  1001
    minu *= texturedata->texw;
slouken@1918
  1002
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
  1003
    maxu *= texturedata->texw;
slouken@1918
  1004
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
  1005
    minv *= texturedata->texh;
slouken@1918
  1006
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
  1007
    maxv *= texturedata->texh;
slouken@1918
  1008
slouken@1927
  1009
    data->glBegin(GL_TRIANGLE_STRIP);
slouken@1927
  1010
    data->glTexCoord2f(minu, minv);
slouken@3472
  1011
    data->glVertex2f((GLfloat) minx, (GLfloat) miny);
slouken@1927
  1012
    data->glTexCoord2f(maxu, minv);
slouken@3472
  1013
    data->glVertex2f((GLfloat) maxx, (GLfloat) miny);
slouken@1927
  1014
    data->glTexCoord2f(minu, maxv);
slouken@3472
  1015
    data->glVertex2f((GLfloat) minx, (GLfloat) maxy);
slouken@1927
  1016
    data->glTexCoord2f(maxu, maxv);
slouken@3472
  1017
    data->glVertex2f((GLfloat) maxx, (GLfloat) maxy);
slouken@1927
  1018
    data->glEnd();
slouken@1918
  1019
slouken@2884
  1020
    data->glDisable(texturedata->type);
slouken@2884
  1021
slouken@1918
  1022
    return 0;
slouken@1918
  1023
}
slouken@1918
  1024
slouken@3431
  1025
static int
gabomdq@6320
  1026
GL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
gabomdq@6320
  1027
              const SDL_Rect * srcrect, const SDL_Rect * dstrect,
gabomdq@6320
  1028
              const double angle, const SDL_Point *center, const SDL_RendererFlip flip)
gabomdq@6320
  1029
{
gabomdq@6320
  1030
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
gabomdq@6320
  1031
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
gabomdq@6320
  1032
    GLfloat minx, miny, maxx, maxy;
gabomdq@6320
  1033
    GLfloat centerx, centery;
gabomdq@6320
  1034
    GLfloat minu, maxu, minv, maxv;
gabomdq@6320
  1035
    GL_ActivateRenderer(renderer);
gabomdq@6320
  1036
gabomdq@6320
  1037
    data->glEnable(texturedata->type);
gabomdq@6320
  1038
    if (texturedata->yuv) {
gabomdq@6320
  1039
        data->glActiveTextureARB(GL_TEXTURE2_ARB);
gabomdq@6320
  1040
        data->glBindTexture(texturedata->type, texturedata->vtexture);
gabomdq@6320
  1041
gabomdq@6320
  1042
        data->glActiveTextureARB(GL_TEXTURE1_ARB);
gabomdq@6320
  1043
        data->glBindTexture(texturedata->type, texturedata->utexture);
gabomdq@6320
  1044
gabomdq@6320
  1045
        data->glActiveTextureARB(GL_TEXTURE0_ARB);
gabomdq@6320
  1046
    }
gabomdq@6320
  1047
    data->glBindTexture(texturedata->type, texturedata->texture);
gabomdq@6320
  1048
gabomdq@6320
  1049
    if (texture->modMode) {
gabomdq@6320
  1050
        GL_SetColor(data, texture->r, texture->g, texture->b, texture->a);
gabomdq@6320
  1051
    } else {
gabomdq@6320
  1052
        GL_SetColor(data, 255, 255, 255, 255);
gabomdq@6320
  1053
    }
gabomdq@6320
  1054
gabomdq@6320
  1055
    GL_SetBlendMode(data, texture->blendMode);
gabomdq@6320
  1056
gabomdq@6320
  1057
    if (texturedata->yuv) {
gabomdq@6320
  1058
        GL_SetShader(data, SHADER_YV12);
gabomdq@6320
  1059
    } else {
gabomdq@6320
  1060
        GL_SetShader(data, SHADER_RGB);
gabomdq@6320
  1061
    }
gabomdq@6320
  1062
gabomdq@6320
  1063
    centerx = (GLfloat)center->x;
gabomdq@6320
  1064
    centery = (GLfloat)center->y;
gabomdq@6320
  1065
gabomdq@6320
  1066
    if (flip & SDL_FLIP_HORIZONTAL) {
gabomdq@6320
  1067
        minx = (GLfloat) dstrect->w - centerx;
gabomdq@6320
  1068
        maxx = -centerx;
gabomdq@6320
  1069
    }
gabomdq@6320
  1070
    else {
gabomdq@6320
  1071
        minx = -centerx;
gabomdq@6320
  1072
        maxx = (GLfloat) dstrect->w - centerx;
gabomdq@6320
  1073
    }
gabomdq@6320
  1074
gabomdq@6320
  1075
    if (flip & SDL_FLIP_VERTICAL) {
gabomdq@6320
  1076
        miny = (GLfloat) dstrect->h - centery;
gabomdq@6320
  1077
        maxy = -centery;
gabomdq@6320
  1078
    }
gabomdq@6320
  1079
    else {
gabomdq@6320
  1080
        miny = -centery;
gabomdq@6320
  1081
        maxy = (GLfloat) dstrect->h - centery;
gabomdq@6320
  1082
    }
gabomdq@6320
  1083
gabomdq@6320
  1084
    minu = (GLfloat) srcrect->x / texture->w;
gabomdq@6320
  1085
    minu *= texturedata->texw;
gabomdq@6320
  1086
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
gabomdq@6320
  1087
    maxu *= texturedata->texw;
gabomdq@6320
  1088
    minv = (GLfloat) srcrect->y / texture->h;
gabomdq@6320
  1089
    minv *= texturedata->texh;
gabomdq@6320
  1090
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
gabomdq@6320
  1091
    maxv *= texturedata->texh;
gabomdq@6320
  1092
gabomdq@6320
  1093
    // Translate to flip, rotate, translate to position
gabomdq@6320
  1094
    data->glPushMatrix();
gabomdq@6320
  1095
    data->glTranslatef((GLfloat)dstrect->x + centerx, (GLfloat)dstrect->y + centery, (GLfloat)0.0);    
gabomdq@6320
  1096
    data->glRotated(angle, (GLdouble)0.0, (GLdouble)0.0, (GLdouble)1.0);
gabomdq@6320
  1097
    
gabomdq@6320
  1098
    data->glBegin(GL_TRIANGLE_STRIP);
gabomdq@6320
  1099
    data->glTexCoord2f(minu, minv);
gabomdq@6320
  1100
    data->glVertex2f(minx, miny);
gabomdq@6320
  1101
    data->glTexCoord2f(maxu, minv);
gabomdq@6320
  1102
    data->glVertex2f(maxx, miny);
gabomdq@6320
  1103
    data->glTexCoord2f(minu, maxv);
gabomdq@6320
  1104
    data->glVertex2f(minx, maxy);
gabomdq@6320
  1105
    data->glTexCoord2f(maxu, maxv);
gabomdq@6320
  1106
    data->glVertex2f(maxx, maxy);
gabomdq@6320
  1107
    data->glEnd();
gabomdq@6320
  1108
    data->glPopMatrix();
gabomdq@6320
  1109
    
gabomdq@6320
  1110
    data->glDisable(texturedata->type);
gabomdq@6320
  1111
gabomdq@6320
  1112
    return 0;
gabomdq@6320
  1113
}
gabomdq@6320
  1114
gabomdq@6320
  1115
static int
slouken@3431
  1116
GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
  1117
                    Uint32 pixel_format, void * pixels, int pitch)
slouken@3431
  1118
{
slouken@3433
  1119
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3685
  1120
    SDL_Window *window = renderer->window;
slouken@5465
  1121
    Uint32 temp_format = SDL_PIXELFORMAT_ARGB8888;
slouken@5465
  1122
    void *temp_pixels;
slouken@5465
  1123
    int temp_pitch;
slouken@3433
  1124
    GLint internalFormat;
slouken@3433
  1125
    GLenum format, type;
slouken@3435
  1126
    Uint8 *src, *dst, *tmp;
slouken@5154
  1127
    int w, h, length, rows;
slouken@5465
  1128
    int status;
slouken@3433
  1129
slouken@5147
  1130
    GL_ActivateRenderer(renderer);
slouken@5147
  1131
slouken@5465
  1132
    temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
slouken@5465
  1133
    temp_pixels = SDL_malloc(rect->h * temp_pitch);
slouken@5465
  1134
    if (!temp_pixels) {
slouken@5465
  1135
        SDL_OutOfMemory();
slouken@3433
  1136
        return -1;
slouken@3433
  1137
    }
slouken@3433
  1138
slouken@5465
  1139
    convert_format(data, temp_format, &internalFormat, &format, &type);
slouken@5465
  1140
slouken@5154
  1141
    SDL_GetWindowSize(window, &w, &h);
slouken@5154
  1142
slouken@3446
  1143
    data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
slouken@3446
  1144
    data->glPixelStorei(GL_PACK_ROW_LENGTH,
slouken@5465
  1145
                        (temp_pitch / SDL_BYTESPERPIXEL(temp_format)));
slouken@3433
  1146
slouken@5154
  1147
    data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
slouken@5465
  1148
                       format, type, temp_pixels);
slouken@3435
  1149
slouken@3435
  1150
    /* Flip the rows to be top-down */
slouken@5465
  1151
    length = rect->w * SDL_BYTESPERPIXEL(temp_format);
slouken@5465
  1152
    src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
slouken@5465
  1153
    dst = (Uint8*)temp_pixels;
slouken@3435
  1154
    tmp = SDL_stack_alloc(Uint8, length);
slouken@3435
  1155
    rows = rect->h / 2;
slouken@3435
  1156
    while (rows--) {
slouken@3435
  1157
        SDL_memcpy(tmp, dst, length);
slouken@3435
  1158
        SDL_memcpy(dst, src, length);
slouken@3435
  1159
        SDL_memcpy(src, tmp, length);
slouken@5465
  1160
        dst += temp_pitch;
slouken@5465
  1161
        src -= temp_pitch;
slouken@3435
  1162
    }
slouken@3435
  1163
    SDL_stack_free(tmp);
slouken@3440
  1164
slouken@5465
  1165
    status = SDL_ConvertPixels(rect->w, rect->h,
slouken@5465
  1166
                               temp_format, temp_pixels, temp_pitch,
slouken@5465
  1167
                               pixel_format, pixels, pitch);
slouken@5465
  1168
    SDL_free(temp_pixels);
slouken@5465
  1169
slouken@5465
  1170
    return status;
slouken@3431
  1171
}
slouken@3431
  1172
slouken@1918
  1173
static void
slouken@1918
  1174
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
  1175
{
slouken@5147
  1176
    GL_ActivateRenderer(renderer);
slouken@5147
  1177
slouken@1918
  1178
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
  1179
}
slouken@1918
  1180
slouken@1918
  1181
static void
slouken@1918
  1182
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
  1183
{
slouken@1927
  1184
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1185
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
  1186
slouken@5147
  1187
    GL_ActivateRenderer(renderer);
slouken@5147
  1188
slouken@1918
  1189
    if (!data) {
slouken@1918
  1190
        return;
slouken@1918
  1191
    }
slouken@1918
  1192
    if (data->texture) {
slouken@1927
  1193
        renderdata->glDeleteTextures(1, &data->texture);
slouken@1918
  1194
    }
slouken@5264
  1195
    if (data->yuv) {
slouken@5264
  1196
        renderdata->glDeleteTextures(1, &data->utexture);
slouken@5264
  1197
        renderdata->glDeleteTextures(1, &data->vtexture);
slouken@5264
  1198
    }
slouken@1920
  1199
    if (data->pixels) {
slouken@1920
  1200
        SDL_free(data->pixels);
slouken@1920
  1201
    }
slouken@1918
  1202
    SDL_free(data);
slouken@1918
  1203
    texture->driverdata = NULL;
slouken@1918
  1204
}
slouken@1918
  1205
slouken@1975
  1206
static void
slouken@1918
  1207
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
  1208
{
slouken@1918
  1209
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1210
slouken@1918
  1211
    if (data) {
icculus@5566
  1212
        if (data->shaders) {
icculus@5566
  1213
            GL_DestroyShaderContext(data->shaders);
icculus@5566
  1214
        }
slouken@1920
  1215
        if (data->context) {
slouken@6232
  1216
            while (data->framebuffers) {
slouken@6232
  1217
                GL_FBOList *nextnode = data->framebuffers->next;
slouken@6232
  1218
                /* delete the framebuffer object */
slouken@6232
  1219
                data->glDeleteFramebuffersEXT(1, &data->framebuffers->FBO);
slouken@6232
  1220
                SDL_free(data->framebuffers);
slouken@6232
  1221
                data->framebuffers = nextnode;
slouken@6232
  1222
            }            
bob@2328
  1223
            /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
slouken@1920
  1224
            SDL_GL_DeleteContext(data->context);
slouken@1918
  1225
        }
slouken@1918
  1226
        SDL_free(data);
slouken@1918
  1227
    }
slouken@1918
  1228
    SDL_free(renderer);
slouken@1918
  1229
}
slouken@1918
  1230
slouken@5226
  1231
#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
slouken@1918
  1232
slouken@1918
  1233
/* vi: set ts=4 sw=4 expandtab: */