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