src/render/opengl/SDL_render_gl.c
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Mon, 22 Apr 2013 18:15:08 -0700
changeset 7091 ff07fad908f8
parent 7037 3fedf1f25b94
child 7141 e276777b4247
permissions -rw-r--r--
SDL_GL_DeleteContext would leave an invalid current_glctx.

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