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