src/video/SDL_renderer_gl.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 16 Oct 2010 17:14:04 -0700
changeset 4905 9779093454d2
parent 4455 6cf71111cf68
child 4910 f205711f73d5
permissions -rw-r--r--
This fixes SDL_renderer_gl so that it builds with c89.
From: Fran├žois Revol <revol@free.fr>
Checked By: Scott McCreary <scottmc2@gmail.com>
slouken@1918
     1
/*
slouken@1918
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@1918
     4
slouken@1918
     5
    This library is free software; you can redistribute it and/or
slouken@1918
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1918
     7
    License as published by the Free Software Foundation; either
slouken@1918
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1918
     9
slouken@1918
    10
    This library is distributed in the hope that it will be useful,
slouken@1918
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1918
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1918
    13
    Lesser General Public License for more details.
slouken@1918
    14
slouken@1918
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1918
    16
    License along with this library; if not, write to the Free Software
slouken@1918
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1918
    18
slouken@1918
    19
    Sam Lantinga
slouken@1918
    20
    slouken@libsdl.org
slouken@1918
    21
*/
slouken@1918
    22
#include "SDL_config.h"
slouken@1918
    23
slouken@1952
    24
#if SDL_VIDEO_RENDER_OGL
slouken@1920
    25
slouken@1920
    26
#include "SDL_video.h"
slouken@1920
    27
#include "SDL_opengl.h"
slouken@1920
    28
#include "SDL_sysvideo.h"
slouken@1920
    29
#include "SDL_pixels_c.h"
slouken@1920
    30
#include "SDL_rect_c.h"
slouken@1920
    31
#include "SDL_yuv_sw_c.h"
slouken@1918
    32
slouken@2246
    33
#ifdef __MACOSX__
slouken@2246
    34
#include <OpenGL/OpenGL.h>
slouken@2246
    35
#endif
slouken@2246
    36
slouken@2778
    37
slouken@1918
    38
/* OpenGL renderer implementation */
slouken@1918
    39
slouken@2230
    40
/* Details on optimizing the texture path on Mac OS X:
slouken@2230
    41
   http://developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/chapter_10_section_2.html
slouken@2230
    42
*/
slouken@2230
    43
icculus@2835
    44
/* !!! FIXME: this should go in a higher level than the GL renderer. */
icculus@2835
    45
static __inline__ int
icculus@2835
    46
bytes_per_pixel(const Uint32 format)
icculus@2835
    47
{
slouken@2839
    48
    if (!SDL_ISPIXELFORMAT_FOURCC(format)) {
slouken@2839
    49
        return SDL_BYTESPERPIXEL(format);
slouken@2839
    50
    }
slouken@2839
    51
slouken@2839
    52
    /* FOURCC format */
icculus@2835
    53
    switch (format) {
slouken@2884
    54
    case SDL_PIXELFORMAT_YV12:
slouken@2884
    55
    case SDL_PIXELFORMAT_IYUV:
slouken@2884
    56
    case SDL_PIXELFORMAT_YUY2:
slouken@2884
    57
    case SDL_PIXELFORMAT_UYVY:
slouken@2884
    58
    case SDL_PIXELFORMAT_YVYU:
slouken@2884
    59
        return 2;
slouken@2884
    60
    default:
slouken@2884
    61
        return 1;               /* shouldn't ever hit this. */
icculus@2835
    62
    }
icculus@2835
    63
}
icculus@2835
    64
icculus@2835
    65
slouken@1985
    66
static const float inv255f = 1.0f / 255.0f;
slouken@1985
    67
slouken@1918
    68
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@1923
    69
static int GL_ActivateRenderer(SDL_Renderer * renderer);
slouken@1970
    70
static int GL_DisplayModeChanged(SDL_Renderer * renderer);
slouken@1918
    71
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@2222
    72
static int GL_QueryTexturePixels(SDL_Renderer * renderer,
slouken@2222
    73
                                 SDL_Texture * texture, void **pixels,
slouken@2222
    74
                                 int *pitch);
slouken@1918
    75
static int GL_SetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    76
                                SDL_Texture * texture,
slouken@1918
    77
                                const SDL_Color * colors, int firstcolor,
slouken@1918
    78
                                int ncolors);
slouken@1918
    79
static int GL_GetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    80
                                SDL_Texture * texture, SDL_Color * colors,
slouken@1918
    81
                                int firstcolor, int ncolors);
slouken@1985
    82
static int GL_SetTextureColorMod(SDL_Renderer * renderer,
slouken@1985
    83
                                 SDL_Texture * texture);
slouken@1985
    84
static int GL_SetTextureAlphaMod(SDL_Renderer * renderer,
slouken@1985
    85
                                 SDL_Texture * texture);
slouken@1985
    86
static int GL_SetTextureBlendMode(SDL_Renderer * renderer,
slouken@1985
    87
                                  SDL_Texture * texture);
slouken@1985
    88
static int GL_SetTextureScaleMode(SDL_Renderer * renderer,
slouken@1985
    89
                                  SDL_Texture * texture);
slouken@1918
    90
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    91
                            const SDL_Rect * rect, const void *pixels,
slouken@1918
    92
                            int pitch);
slouken@1918
    93
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    94
                          const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1985
    95
                          int *pitch);
slouken@1918
    96
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    97
static void GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    98
                            int numrects, const SDL_Rect * rects);
slouken@3596
    99
static int GL_RenderClear(SDL_Renderer * renderer);
slouken@3596
   100
static int GL_RenderDrawPoints(SDL_Renderer * renderer,
slouken@3596
   101
                               const SDL_Point * points, int count);
slouken@3596
   102
static int GL_RenderDrawLines(SDL_Renderer * renderer,
slouken@3596
   103
                              const SDL_Point * points, int count);
slouken@3596
   104
static int GL_RenderDrawRects(SDL_Renderer * renderer,
slouken@3596
   105
                              const SDL_Rect ** rects, int count);
slouken@3596
   106
static int GL_RenderFillRects(SDL_Renderer * renderer,
slouken@3596
   107
                              const SDL_Rect ** rects, int count);
slouken@1918
   108
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   109
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@3431
   110
static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
   111
                               Uint32 pixel_format, void * pixels, int pitch);
slouken@3431
   112
static int GL_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
   113
                                Uint32 pixel_format, const void * pixels, int pitch);
slouken@1918
   114
static void GL_RenderPresent(SDL_Renderer * renderer);
slouken@1918
   115
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
   116
static void GL_DestroyRenderer(SDL_Renderer * renderer);
slouken@1918
   117
slouken@1918
   118
slouken@1918
   119
SDL_RenderDriver GL_RenderDriver = {
slouken@1918
   120
    GL_CreateRenderer,
slouken@1918
   121
    {
slouken@1918
   122
     "opengl",
slouken@1974
   123
     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD |
slouken@1974
   124
      SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
slouken@1985
   125
     (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
slouken@1985
   126
      SDL_TEXTUREMODULATE_ALPHA),
slouken@2884
   127
     (SDL_BLENDMODE_NONE | SDL_BLENDMODE_MASK |
slouken@2884
   128
      SDL_BLENDMODE_BLEND | SDL_BLENDMODE_ADD | SDL_BLENDMODE_MOD),
slouken@1965
   129
     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST |
slouken@1965
   130
      SDL_TEXTURESCALEMODE_SLOW),
slouken@2813
   131
     15,
slouken@1918
   132
     {
slouken@1965
   133
      SDL_PIXELFORMAT_INDEX1LSB,
slouken@1965
   134
      SDL_PIXELFORMAT_INDEX1MSB,
slouken@1965
   135
      SDL_PIXELFORMAT_INDEX8,
slouken@1965
   136
      SDL_PIXELFORMAT_RGB332,
slouken@1965
   137
      SDL_PIXELFORMAT_RGB444,
slouken@1965
   138
      SDL_PIXELFORMAT_RGB555,
slouken@1965
   139
      SDL_PIXELFORMAT_ARGB4444,
slouken@1965
   140
      SDL_PIXELFORMAT_ARGB1555,
slouken@1965
   141
      SDL_PIXELFORMAT_RGB565,
slouken@1965
   142
      SDL_PIXELFORMAT_RGB24,
slouken@1965
   143
      SDL_PIXELFORMAT_BGR24,
slouken@1965
   144
      SDL_PIXELFORMAT_RGB888,
slouken@1965
   145
      SDL_PIXELFORMAT_BGR888,
slouken@1965
   146
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
   147
      SDL_PIXELFORMAT_ABGR8888,
slouken@2813
   148
      SDL_PIXELFORMAT_ARGB2101010},
slouken@1918
   149
     0,
slouken@1918
   150
     0}
slouken@1918
   151
};
slouken@1918
   152
slouken@1918
   153
typedef struct
slouken@1918
   154
{
slouken@1918
   155
    SDL_GLContext context;
slouken@2833
   156
    SDL_bool updateSize;
slouken@2233
   157
    SDL_bool GL_ARB_texture_rectangle_supported;
slouken@1974
   158
    SDL_bool GL_EXT_paletted_texture_supported;
slouken@2845
   159
    SDL_bool GL_APPLE_ycbcr_422_supported;
slouken@2845
   160
    SDL_bool GL_MESA_ycbcr_texture_supported;
icculus@2835
   161
    SDL_bool GL_ARB_fragment_program_supported;
slouken@1927
   162
    int blendMode;
slouken@1927
   163
    int scaleMode;
slouken@1927
   164
slouken@1927
   165
    /* OpenGL functions */
slouken@1927
   166
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
slouken@1927
   167
#include "SDL_glfuncs.h"
slouken@1927
   168
#undef SDL_PROC
slouken@1974
   169
slouken@1974
   170
    PFNGLCOLORTABLEEXTPROC glColorTableEXT;
slouken@2233
   171
    void (*glTextureRangeAPPLE) (GLenum target, GLsizei length,
slouken@2233
   172
                                 const GLvoid * pointer);
icculus@2835
   173
icculus@2835
   174
    PFNGLGETPROGRAMIVARBPROC glGetProgramivARB;
icculus@2835
   175
    PFNGLGETPROGRAMSTRINGARBPROC glGetProgramStringARB;
icculus@2835
   176
    PFNGLPROGRAMLOCALPARAMETER4FVARBPROC glProgramLocalParameter4fvARB;
icculus@2835
   177
    PFNGLDELETEPROGRAMSARBPROC glDeleteProgramsARB;
icculus@2835
   178
    PFNGLGENPROGRAMSARBPROC glGenProgramsARB;
icculus@2835
   179
    PFNGLBINDPROGRAMARBPROC glBindProgramARB;
icculus@2835
   180
    PFNGLPROGRAMSTRINGARBPROC glProgramStringARB;
icculus@2835
   181
icculus@2835
   182
    /* (optional) fragment programs */
slouken@3468
   183
    GLuint fragment_program_mask;
icculus@2835
   184
    GLuint fragment_program_UYVY;
slouken@1918
   185
} GL_RenderData;
slouken@1918
   186
slouken@1918
   187
typedef struct
slouken@1918
   188
{
slouken@1918
   189
    GLuint texture;
icculus@2835
   190
    GLuint shader;
slouken@1920
   191
    GLenum type;
slouken@1918
   192
    GLfloat texw;
slouken@1918
   193
    GLfloat texh;
slouken@1920
   194
    GLenum format;
slouken@1920
   195
    GLenum formattype;
slouken@1974
   196
    Uint8 *palette;
slouken@1918
   197
    void *pixels;
slouken@1918
   198
    int pitch;
slouken@1920
   199
    SDL_DirtyRectList dirty;
slouken@2884
   200
    int HACK_RYAN_FIXME;
slouken@1918
   201
} GL_TextureData;
slouken@1918
   202
slouken@1918
   203
slouken@1924
   204
static void
slouken@1924
   205
GL_SetError(const char *prefix, GLenum result)
slouken@1924
   206
{
slouken@1924
   207
    const char *error;
slouken@1924
   208
slouken@1924
   209
    switch (result) {
slouken@1924
   210
    case GL_NO_ERROR:
slouken@1924
   211
        error = "GL_NO_ERROR";
slouken@1924
   212
        break;
slouken@1924
   213
    case GL_INVALID_ENUM:
slouken@1924
   214
        error = "GL_INVALID_ENUM";
slouken@1924
   215
        break;
slouken@1924
   216
    case GL_INVALID_VALUE:
slouken@1924
   217
        error = "GL_INVALID_VALUE";
slouken@1924
   218
        break;
slouken@1924
   219
    case GL_INVALID_OPERATION:
slouken@1924
   220
        error = "GL_INVALID_OPERATION";
slouken@1924
   221
        break;
slouken@1924
   222
    case GL_STACK_OVERFLOW:
slouken@1924
   223
        error = "GL_STACK_OVERFLOW";
slouken@1924
   224
        break;
slouken@1924
   225
    case GL_STACK_UNDERFLOW:
slouken@1924
   226
        error = "GL_STACK_UNDERFLOW";
slouken@1924
   227
        break;
slouken@1924
   228
    case GL_OUT_OF_MEMORY:
slouken@1924
   229
        error = "GL_OUT_OF_MEMORY";
slouken@1924
   230
        break;
slouken@1924
   231
    case GL_TABLE_TOO_LARGE:
slouken@1924
   232
        error = "GL_TABLE_TOO_LARGE";
slouken@1924
   233
        break;
slouken@1924
   234
    default:
slouken@1924
   235
        error = "UNKNOWN";
slouken@1924
   236
        break;
slouken@1924
   237
    }
slouken@1924
   238
    SDL_SetError("%s: %s", prefix, error);
slouken@1924
   239
}
slouken@1924
   240
slouken@1927
   241
static int
slouken@1927
   242
GL_LoadFunctions(GL_RenderData * data)
slouken@1927
   243
{
slouken@1927
   244
#if defined(__QNXNTO__) && (_NTO_VERSION < 630)
slouken@1927
   245
#define __SDL_NOGETPROCADDR__
slouken@1927
   246
#endif
slouken@1927
   247
#ifdef __SDL_NOGETPROCADDR__
slouken@1927
   248
#define SDL_PROC(ret,func,params) data->func=func;
slouken@1927
   249
#else
slouken@1927
   250
#define SDL_PROC(ret,func,params) \
slouken@1927
   251
    do { \
slouken@1927
   252
        data->func = SDL_GL_GetProcAddress(#func); \
slouken@1927
   253
        if ( ! data->func ) { \
slouken@1927
   254
            SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
slouken@1927
   255
            return -1; \
slouken@1927
   256
        } \
slouken@1927
   257
    } while ( 0 );
slouken@1927
   258
#endif /* __SDL_NOGETPROCADDR__ */
slouken@1927
   259
slouken@1927
   260
#include "SDL_glfuncs.h"
slouken@1927
   261
#undef SDL_PROC
slouken@1927
   262
    return 0;
slouken@1927
   263
}
slouken@1927
   264
slouken@1918
   265
SDL_Renderer *
slouken@1918
   266
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1918
   267
{
slouken@1918
   268
    SDL_Renderer *renderer;
slouken@1918
   269
    GL_RenderData *data;
slouken@1952
   270
    GLint value;
slouken@1974
   271
    int doublebuffer;
slouken@1918
   272
slouken@1974
   273
    /* Render directly to the window, unless we're compositing */
slouken@1974
   274
#ifndef __MACOSX__
slouken@1974
   275
    if (flags & SDL_RENDERER_SINGLEBUFFER) {
slouken@1974
   276
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
slouken@1974
   277
    }
slouken@1974
   278
#endif
slouken@1918
   279
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@1928
   280
        if (SDL_RecreateWindow(window, window->flags | SDL_WINDOW_OPENGL) < 0) {
slouken@1924
   281
            return NULL;
slouken@1924
   282
        }
slouken@1918
   283
    }
slouken@1918
   284
slouken@1920
   285
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1918
   286
    if (!renderer) {
slouken@1918
   287
        SDL_OutOfMemory();
slouken@1918
   288
        return NULL;
slouken@1918
   289
    }
slouken@1918
   290
slouken@1920
   291
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   292
    if (!data) {
slouken@1918
   293
        GL_DestroyRenderer(renderer);
slouken@1918
   294
        SDL_OutOfMemory();
slouken@1918
   295
        return NULL;
slouken@1918
   296
    }
slouken@1918
   297
slouken@1923
   298
    renderer->ActivateRenderer = GL_ActivateRenderer;
slouken@1970
   299
    renderer->DisplayModeChanged = GL_DisplayModeChanged;
slouken@1918
   300
    renderer->CreateTexture = GL_CreateTexture;
slouken@2222
   301
    renderer->QueryTexturePixels = GL_QueryTexturePixels;
slouken@1918
   302
    renderer->SetTexturePalette = GL_SetTexturePalette;
slouken@1918
   303
    renderer->GetTexturePalette = GL_GetTexturePalette;
slouken@1985
   304
    renderer->SetTextureColorMod = GL_SetTextureColorMod;
slouken@1985
   305
    renderer->SetTextureAlphaMod = GL_SetTextureAlphaMod;
slouken@1985
   306
    renderer->SetTextureBlendMode = GL_SetTextureBlendMode;
slouken@1985
   307
    renderer->SetTextureScaleMode = GL_SetTextureScaleMode;
slouken@1918
   308
    renderer->UpdateTexture = GL_UpdateTexture;
slouken@1918
   309
    renderer->LockTexture = GL_LockTexture;
slouken@1918
   310
    renderer->UnlockTexture = GL_UnlockTexture;
slouken@1918
   311
    renderer->DirtyTexture = GL_DirtyTexture;
slouken@3596
   312
    renderer->RenderClear = GL_RenderClear;
slouken@3596
   313
    renderer->RenderDrawPoints = GL_RenderDrawPoints;
slouken@3596
   314
    renderer->RenderDrawLines = GL_RenderDrawLines;
slouken@3596
   315
    renderer->RenderDrawRects = GL_RenderDrawRects;
slouken@3596
   316
    renderer->RenderFillRects = GL_RenderFillRects;
slouken@1918
   317
    renderer->RenderCopy = GL_RenderCopy;
slouken@3431
   318
    renderer->RenderReadPixels = GL_RenderReadPixels;
slouken@3431
   319
    renderer->RenderWritePixels = GL_RenderWritePixels;
slouken@1918
   320
    renderer->RenderPresent = GL_RenderPresent;
slouken@1918
   321
    renderer->DestroyTexture = GL_DestroyTexture;
slouken@1918
   322
    renderer->DestroyRenderer = GL_DestroyRenderer;
slouken@1918
   323
    renderer->info = GL_RenderDriver.info;
slouken@3685
   324
    renderer->window = window;
slouken@1918
   325
    renderer->driverdata = data;
slouken@1918
   326
slouken@1918
   327
    renderer->info.flags =
slouken@1965
   328
        (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED);
slouken@1918
   329
slouken@1927
   330
    if (GL_LoadFunctions(data) < 0) {
slouken@1927
   331
        GL_DestroyRenderer(renderer);
slouken@1927
   332
        return NULL;
slouken@1927
   333
    }
slouken@1927
   334
slouken@3685
   335
    data->context = SDL_GL_CreateContext(window);
slouken@1918
   336
    if (!data->context) {
slouken@1918
   337
        GL_DestroyRenderer(renderer);
slouken@1918
   338
        return NULL;
slouken@1918
   339
    }
slouken@3685
   340
    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
slouken@1918
   341
        GL_DestroyRenderer(renderer);
slouken@1918
   342
        return NULL;
slouken@1918
   343
    }
slouken@2246
   344
#ifdef __MACOSX__
slouken@2246
   345
    /* Enable multi-threaded rendering */
slouken@2246
   346
    /* Disabled until Ryan finishes his VBO/PBO code...
bob@2295
   347
       CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
bob@2295
   348
     */
slouken@2246
   349
#endif
slouken@2246
   350
slouken@1965
   351
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1918
   352
        SDL_GL_SetSwapInterval(1);
slouken@1918
   353
    } else {
slouken@1918
   354
        SDL_GL_SetSwapInterval(0);
slouken@1918
   355
    }
slouken@1918
   356
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@1965
   357
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1918
   358
    }
slouken@1918
   359
slouken@1974
   360
    if (SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer) == 0) {
slouken@1974
   361
        if (!doublebuffer) {
slouken@1974
   362
            renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER;
slouken@1974
   363
        }
slouken@1974
   364
    }
slouken@1974
   365
slouken@1952
   366
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   367
    renderer->info.max_texture_width = value;
slouken@1952
   368
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   369
    renderer->info.max_texture_height = value;
slouken@1920
   370
slouken@1926
   371
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
slouken@1926
   372
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
slouken@1926
   373
        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
slouken@1926
   374
    }
slouken@1974
   375
    if (SDL_GL_ExtensionSupported("GL_EXT_paletted_texture")) {
slouken@1974
   376
        data->GL_EXT_paletted_texture_supported = SDL_TRUE;
slouken@1974
   377
        data->glColorTableEXT =
slouken@1974
   378
            (PFNGLCOLORTABLEEXTPROC) SDL_GL_GetProcAddress("glColorTableEXT");
slouken@1974
   379
    } else {
slouken@1974
   380
        /* Don't advertise support for 8-bit indexed texture format */
slouken@1974
   381
        Uint32 i, j;
slouken@1974
   382
        SDL_RendererInfo *info = &renderer->info;
slouken@1974
   383
        for (i = 0, j = 0; i < info->num_texture_formats; ++i) {
slouken@1974
   384
            if (info->texture_formats[i] != SDL_PIXELFORMAT_INDEX8) {
slouken@1974
   385
                info->texture_formats[j++] = info->texture_formats[i];
slouken@1974
   386
            }
slouken@1974
   387
        }
slouken@1974
   388
        --info->num_texture_formats;
slouken@1974
   389
    }
slouken@2845
   390
    if (SDL_GL_ExtensionSupported("GL_APPLE_ycbcr_422")) {
slouken@2845
   391
        data->GL_APPLE_ycbcr_422_supported = SDL_TRUE;
slouken@2845
   392
    }
slouken@2845
   393
    if (SDL_GL_ExtensionSupported("GL_MESA_ycbcr_texture")) {
slouken@2845
   394
        data->GL_MESA_ycbcr_texture_supported = SDL_TRUE;
slouken@2845
   395
    }
slouken@2233
   396
    if (SDL_GL_ExtensionSupported("GL_APPLE_texture_range")) {
slouken@2233
   397
        data->glTextureRangeAPPLE =
slouken@2233
   398
            (void (*)(GLenum, GLsizei, const GLvoid *))
slouken@2233
   399
            SDL_GL_GetProcAddress("glTextureRangeAPPLE");
slouken@2233
   400
    }
slouken@1920
   401
icculus@2835
   402
    /* we might use fragment programs for YUV data, etc. */
icculus@2835
   403
    if (SDL_GL_ExtensionSupported("GL_ARB_fragment_program")) {
icculus@2835
   404
        /* !!! FIXME: this doesn't check for errors. */
icculus@2835
   405
        /* !!! FIXME: this should really reuse the glfuncs.h stuff. */
icculus@2835
   406
        data->glGetProgramivARB = (PFNGLGETPROGRAMIVARBPROC)
icculus@2835
   407
            SDL_GL_GetProcAddress("glGetProgramivARB");
icculus@2835
   408
        data->glGetProgramStringARB = (PFNGLGETPROGRAMSTRINGARBPROC)
icculus@2835
   409
            SDL_GL_GetProcAddress("glGetProgramStringARB");
icculus@2835
   410
        data->glProgramLocalParameter4fvARB =
icculus@2835
   411
            (PFNGLPROGRAMLOCALPARAMETER4FVARBPROC)
icculus@2835
   412
            SDL_GL_GetProcAddress("glProgramLocalParameter4fvARB");
icculus@2835
   413
        data->glDeleteProgramsARB = (PFNGLDELETEPROGRAMSARBPROC)
icculus@2835
   414
            SDL_GL_GetProcAddress("glDeleteProgramsARB");
icculus@2835
   415
        data->glGenProgramsARB = (PFNGLGENPROGRAMSARBPROC)
icculus@2835
   416
            SDL_GL_GetProcAddress("glGenProgramsARB");
icculus@2835
   417
        data->glBindProgramARB = (PFNGLBINDPROGRAMARBPROC)
icculus@2835
   418
            SDL_GL_GetProcAddress("glBindProgramARB");
icculus@2835
   419
        data->glProgramStringARB = (PFNGLPROGRAMSTRINGARBPROC)
icculus@2835
   420
            SDL_GL_GetProcAddress("glProgramStringARB");
icculus@2835
   421
        data->GL_ARB_fragment_program_supported = SDL_TRUE;
icculus@2835
   422
    }
icculus@2835
   423
slouken@1918
   424
    /* Set up parameters for rendering */
slouken@1927
   425
    data->blendMode = -1;
slouken@1927
   426
    data->scaleMode = -1;
slouken@1927
   427
    data->glDisable(GL_DEPTH_TEST);
slouken@1927
   428
    data->glDisable(GL_CULL_FACE);
slouken@3262
   429
    /* This ended up causing video discrepancies between OpenGL and Direct3D */
slouken@3262
   430
    /*data->glEnable(GL_LINE_SMOOTH);*/
slouken@1926
   431
    if (data->GL_ARB_texture_rectangle_supported) {
slouken@1927
   432
        data->glEnable(GL_TEXTURE_RECTANGLE_ARB);
slouken@1926
   433
    } else {
slouken@1927
   434
        data->glEnable(GL_TEXTURE_2D);
slouken@1926
   435
    }
slouken@2833
   436
    data->updateSize = SDL_TRUE;
slouken@1918
   437
slouken@1918
   438
    return renderer;
slouken@1918
   439
}
slouken@1918
   440
slouken@1923
   441
static int
slouken@1923
   442
GL_ActivateRenderer(SDL_Renderer * renderer)
slouken@1923
   443
{
slouken@1923
   444
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3685
   445
    SDL_Window *window = renderer->window;
slouken@1923
   446
slouken@3685
   447
    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
slouken@1970
   448
        return -1;
slouken@1970
   449
    }
slouken@2833
   450
    if (data->updateSize) {
slouken@2836
   451
        data->glMatrixMode(GL_PROJECTION);
slouken@2836
   452
        data->glLoadIdentity();
slouken@2836
   453
        data->glMatrixMode(GL_MODELVIEW);
slouken@2836
   454
        data->glLoadIdentity();
slouken@2836
   455
        data->glViewport(0, 0, window->w, window->h);
slouken@3324
   456
        data->glOrtho(0.0, (GLdouble) window->w,
slouken@3324
   457
                      (GLdouble) window->h, 0.0, 0.0, 1.0);
slouken@2833
   458
        data->updateSize = SDL_FALSE;
slouken@2833
   459
    }
slouken@1970
   460
    return 0;
slouken@1970
   461
}
slouken@1970
   462
slouken@1970
   463
static int
slouken@1970
   464
GL_DisplayModeChanged(SDL_Renderer * renderer)
slouken@1970
   465
{
slouken@1970
   466
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1970
   467
slouken@2836
   468
    /* Rebind the context to the window area and update matrices */
slouken@2836
   469
    data->updateSize = SDL_TRUE;
slouken@2836
   470
    return GL_ActivateRenderer(renderer);
slouken@1923
   471
}
slouken@1923
   472
slouken@1922
   473
static __inline__ int
slouken@1922
   474
power_of_2(int input)
slouken@1922
   475
{
slouken@1922
   476
    int value = 1;
slouken@1922
   477
slouken@1922
   478
    while (value < input) {
slouken@1922
   479
        value <<= 1;
slouken@1922
   480
    }
slouken@1922
   481
    return value;
slouken@1922
   482
}
slouken@1922
   483
icculus@2835
   484
slouken@2858
   485
//#define DEBUG_PROGRAM_COMPILE 1
icculus@2835
   486
slouken@3468
   487
static void
slouken@3468
   488
set_shader_error(GL_RenderData * data, const char *prefix)
slouken@3468
   489
{
slouken@3468
   490
    GLint pos = 0;
slouken@3468
   491
    const GLubyte *errstr;
slouken@3468
   492
    data->glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
slouken@3468
   493
    errstr = data->glGetString(GL_PROGRAM_ERROR_STRING_ARB);
slouken@3479
   494
    SDL_SetError("%s: shader compile error at position %d: %s",
slouken@3468
   495
           prefix, (int) pos, (const char *) errstr);
slouken@3468
   496
}
slouken@3468
   497
icculus@2835
   498
static GLuint
slouken@2884
   499
compile_shader(GL_RenderData * data, GLenum shader_type, const char *_code)
icculus@2835
   500
{
icculus@2847
   501
    const int have_texture_rects = data->GL_ARB_texture_rectangle_supported;
icculus@2847
   502
    const char *replacement = have_texture_rects ? "RECT" : "2D";
slouken@2918
   503
    const size_t replacementlen = SDL_strlen(replacement);
icculus@2847
   504
    const char *token = "%TEXTURETARGET%";
slouken@2918
   505
    const size_t tokenlen = SDL_strlen(token);
icculus@2847
   506
    char *code = NULL;
icculus@2847
   507
    char *ptr = NULL;
icculus@2847
   508
    GLuint program = 0;
icculus@2847
   509
icculus@2847
   510
    /*
icculus@2847
   511
     * The TEX instruction needs a different target depending on what we use.
icculus@2847
   512
     *  To handle this, we use "%TEXTURETARGET%" and replace the string before
icculus@2847
   513
     *  compiling the shader.
icculus@2847
   514
     */
icculus@2847
   515
    code = SDL_strdup(_code);
icculus@2847
   516
    if (code == NULL)
icculus@2847
   517
        return 0;
icculus@2847
   518
slouken@2884
   519
    for (ptr = SDL_strstr(code, token); ptr; ptr = SDL_strstr(ptr + 1, token)) {
slouken@2918
   520
        SDL_memcpy(ptr, replacement, replacementlen);
slouken@2918
   521
        SDL_memmove(ptr + replacementlen, ptr + tokenlen,
slouken@2918
   522
                    SDL_strlen(ptr + tokenlen) + 1);
icculus@2847
   523
    }
icculus@2847
   524
icculus@2835
   525
#if DEBUG_PROGRAM_COMPILE
icculus@2847
   526
    printf("compiling shader:\n%s\n\n", code);
icculus@2835
   527
#endif
icculus@2835
   528
slouken@2884
   529
    data->glGetError();         /* flush any existing error state. */
icculus@2835
   530
    data->glGenProgramsARB(1, &program);
icculus@2835
   531
    data->glBindProgramARB(shader_type, program);
icculus@2835
   532
    data->glProgramStringARB(shader_type, GL_PROGRAM_FORMAT_ASCII_ARB,
slouken@3253
   533
                             (GLsizei)SDL_strlen(code), code);
icculus@2847
   534
icculus@2847
   535
    SDL_free(code);
icculus@2835
   536
slouken@2884
   537
    if (data->glGetError() == GL_INVALID_OPERATION) {
icculus@2835
   538
#if DEBUG_PROGRAM_COMPILE
icculus@2835
   539
        GLint pos = 0;
icculus@2835
   540
        const GLubyte *errstr;
icculus@2835
   541
        data->glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
icculus@2835
   542
        errstr = data->glGetString(GL_PROGRAM_ERROR_STRING_ARB);
icculus@2835
   543
        printf("program compile error at position %d: %s\n\n",
slouken@2884
   544
               (int) pos, (const char *) errstr);
icculus@2835
   545
#endif
icculus@2835
   546
        data->glBindProgramARB(shader_type, 0);
icculus@2835
   547
        data->glDeleteProgramsARB(1, &program);
icculus@2835
   548
        return 0;
icculus@2848
   549
    }
icculus@2835
   550
icculus@2835
   551
    return program;
icculus@2835
   552
}
icculus@2835
   553
icculus@2848
   554
icculus@2848
   555
/*
slouken@3468
   556
 * Fragment program that implements mask semantics
slouken@3468
   557
 */
slouken@3468
   558
static const char *fragment_program_mask_source_code = "!!ARBfp1.0\n"
slouken@3468
   559
"OUTPUT output = result.color;\n"
slouken@3468
   560
"TEMP value;\n"
slouken@3468
   561
"TEX value, fragment.texcoord[0], texture[0], %TEXTURETARGET%;\n"
slouken@3468
   562
"MUL value, fragment.color, value;\n"
slouken@3468
   563
"SGE value.a, value.a, 0.001;\n"
slouken@3468
   564
"MOV output, value;\n"
slouken@3468
   565
"END";
slouken@3468
   566
slouken@3468
   567
/*
icculus@2848
   568
 * Fragment program that renders from UYVY textures.
icculus@2848
   569
 * The UYVY to RGB equasion is:
icculus@2848
   570
 *   R = 1.164(Y-16) + 1.596(Cr-128)
icculus@2848
   571
 *   G = 1.164(Y-16) - 0.813(Cr-128) - 0.391(Cb-128)
icculus@2848
   572
 *   B = 1.164(Y-16) + 2.018(Cb-128)
icculus@2848
   573
 * Byte layout is Cb, Y1, Cr, Y2, stored in the R, G, B, A channels.
icculus@2848
   574
 * 4 bytes == 2 pixels: Y1/Cb/Cr, Y2/Cb/Cr
icculus@2848
   575
 *
icculus@2848
   576
 * !!! FIXME: this ignores blendmodes, etc.
icculus@2848
   577
 * !!! FIXME: this could be more efficient...use a dot product for green, etc.
icculus@2848
   578
 */
slouken@2884
   579
static const char *fragment_program_UYVY_source_code = "!!ARBfp1.0\n"
icculus@2848
   580
    /* outputs... */
icculus@2835
   581
    "OUTPUT outcolor = result.color;\n"
icculus@2848
   582
    /* scratch registers... */
slouken@2884
   583
    "TEMP uyvy;\n" "TEMP luminance;\n" "TEMP work;\n"
icculus@2848
   584
    /* Halve the coordinates to grab the correct 32 bits for the fragment. */
icculus@2835
   585
    "MUL work, fragment.texcoord, { 0.5, 1.0, 1.0, 1.0 };\n"
icculus@2848
   586
    /* Sample the YUV texture. Cb, Y1, Cr, Y2, are stored in x, y, z, w. */
icculus@2847
   587
    "TEX uyvy, work, texture[0], %TEXTURETARGET%;\n"
icculus@2848
   588
    /* Do subtractions (128/255, 16/255, 128/255, 16/255) */
slouken@2846
   589
    "SUB uyvy, uyvy, { 0.501960784313726, 0.06274509803922, 0.501960784313726, 0.06274509803922 };\n"
icculus@2848
   590
    /* Choose the luminance component by texcoord. */
icculus@2848
   591
    /* !!! FIXME: laziness wins out for now... just average Y1 and Y2. */
icculus@2835
   592
    "ADD luminance, uyvy.yyyy, uyvy.wwww;\n"
icculus@2835
   593
    "MUL luminance, luminance, { 0.5, 0.5, 0.5, 0.5 };\n"
icculus@2848
   594
    /* Multiply luminance by its magic value. */
icculus@2835
   595
    "MUL luminance, luminance, { 1.164, 1.164, 1.164, 1.164 };\n"
icculus@2848
   596
    /* uyvy.xyzw becomes Cr/Cr/Cb/Cb, with multiplications. */
icculus@2835
   597
    "MUL uyvy, uyvy.zzxx, { 1.596, -0.813, 2.018, -0.391 };\n"
icculus@2848
   598
    /* Add luminance to Cr and Cb, store to RGB channels. */
icculus@2835
   599
    "ADD work.rgb, luminance, uyvy;\n"
icculus@2848
   600
    /* Do final addition for Green channel.  (!!! FIXME: this should be a DPH?) */
icculus@2835
   601
    "ADD work.g, work.g, uyvy.w;\n"
icculus@2848
   602
    /* Make sure alpha channel is fully opaque.  (!!! FIXME: blend modes!) */
icculus@2835
   603
    "MOV work.a, { 1.0 };\n"
icculus@2848
   604
    /* Store out the final fragment color... */
icculus@2835
   605
    "MOV outcolor, work;\n"
icculus@2848
   606
    /* ...and we're done! */
icculus@2835
   607
    "END\n";
icculus@2835
   608
slouken@3433
   609
static __inline__ SDL_bool
slouken@3433
   610
convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
slouken@3433
   611
               GLint* internalFormat, GLenum* format, GLenum* type)
slouken@3433
   612
{
slouken@3433
   613
    switch (pixel_format) {
slouken@3433
   614
    case SDL_PIXELFORMAT_INDEX1LSB:
slouken@3433
   615
    case SDL_PIXELFORMAT_INDEX1MSB:
slouken@3433
   616
        *internalFormat = GL_RGB;
slouken@3433
   617
        *format = GL_COLOR_INDEX;
slouken@3433
   618
        *type = GL_BITMAP;
slouken@3433
   619
        break;
slouken@3433
   620
    case SDL_PIXELFORMAT_INDEX8:
slouken@3433
   621
        if (!renderdata->GL_EXT_paletted_texture_supported) {
slouken@3433
   622
            return SDL_FALSE;
slouken@3433
   623
        }
slouken@3433
   624
        *internalFormat = GL_COLOR_INDEX8_EXT;
slouken@3433
   625
        *format = GL_COLOR_INDEX;
slouken@3433
   626
        *type = GL_UNSIGNED_BYTE;
slouken@3433
   627
        break;
slouken@3433
   628
    case SDL_PIXELFORMAT_RGB332:
slouken@3433
   629
        *internalFormat = GL_R3_G3_B2;
slouken@3433
   630
        *format = GL_RGB;
slouken@3433
   631
        *type = GL_UNSIGNED_BYTE_3_3_2;
slouken@3433
   632
        break;
slouken@3433
   633
    case SDL_PIXELFORMAT_RGB444:
slouken@3433
   634
        *internalFormat = GL_RGB4;
slouken@3433
   635
        *format = GL_RGB;
slouken@3433
   636
        *type = GL_UNSIGNED_SHORT_4_4_4_4;
slouken@3433
   637
        break;
slouken@3433
   638
    case SDL_PIXELFORMAT_RGB555:
slouken@3433
   639
        *internalFormat = GL_RGB5;
slouken@3433
   640
        *format = GL_RGB;
slouken@3433
   641
        *type = GL_UNSIGNED_SHORT_5_5_5_1;
slouken@3433
   642
        break;
slouken@3433
   643
    case SDL_PIXELFORMAT_ARGB4444:
slouken@3433
   644
        *internalFormat = GL_RGBA4;
slouken@3433
   645
        *format = GL_BGRA;
slouken@3433
   646
        *type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
slouken@3433
   647
        break;
slouken@3433
   648
    case SDL_PIXELFORMAT_ARGB1555:
slouken@3433
   649
        *internalFormat = GL_RGB5_A1;
slouken@3433
   650
        *format = GL_BGRA;
slouken@3433
   651
        *type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
slouken@3433
   652
        break;
slouken@3433
   653
    case SDL_PIXELFORMAT_RGB565:
slouken@3433
   654
        *internalFormat = GL_RGB8;
slouken@3433
   655
        *format = GL_RGB;
slouken@3433
   656
        *type = GL_UNSIGNED_SHORT_5_6_5;
slouken@3433
   657
        break;
slouken@3433
   658
    case SDL_PIXELFORMAT_RGB24:
slouken@3433
   659
        *internalFormat = GL_RGB8;
slouken@3433
   660
        *format = GL_RGB;
slouken@3433
   661
        *type = GL_UNSIGNED_BYTE;
slouken@3433
   662
        break;
slouken@3433
   663
    case SDL_PIXELFORMAT_RGB888:
slouken@3433
   664
        *internalFormat = GL_RGB8;
slouken@3433
   665
        *format = GL_BGRA;
slouken@3433
   666
        *type = GL_UNSIGNED_BYTE;
slouken@3433
   667
        break;
slouken@3433
   668
    case SDL_PIXELFORMAT_BGR24:
slouken@3433
   669
        *internalFormat = GL_RGB8;
slouken@3433
   670
        *format = GL_BGR;
slouken@3433
   671
        *type = GL_UNSIGNED_BYTE;
slouken@3433
   672
        break;
slouken@3433
   673
    case SDL_PIXELFORMAT_BGR888:
slouken@3433
   674
        *internalFormat = GL_RGB8;
slouken@3433
   675
        *format = GL_RGBA;
slouken@3433
   676
        *type = GL_UNSIGNED_BYTE;
slouken@3433
   677
        break;
slouken@3433
   678
    case SDL_PIXELFORMAT_ARGB8888:
slouken@3433
   679
#ifdef __MACOSX__
slouken@3433
   680
        *internalFormat = GL_RGBA;
slouken@3433
   681
        *format = GL_BGRA;
slouken@3433
   682
        *type = GL_UNSIGNED_INT_8_8_8_8_REV;
slouken@3433
   683
#else
slouken@3433
   684
        *internalFormat = GL_RGBA8;
slouken@3433
   685
        *format = GL_BGRA;
slouken@3433
   686
        *type = GL_UNSIGNED_BYTE;
slouken@3433
   687
#endif
slouken@3433
   688
        break;
slouken@3433
   689
    case SDL_PIXELFORMAT_ABGR8888:
slouken@3433
   690
        *internalFormat = GL_RGBA8;
slouken@3433
   691
        *format = GL_RGBA;
slouken@3433
   692
        *type = GL_UNSIGNED_BYTE;
slouken@3433
   693
        break;
slouken@3433
   694
    case SDL_PIXELFORMAT_ARGB2101010:
slouken@3433
   695
        *internalFormat = GL_RGB10_A2;
slouken@3433
   696
        *format = GL_BGRA;
slouken@3433
   697
        *type = GL_UNSIGNED_INT_2_10_10_10_REV;
slouken@3433
   698
        break;
slouken@3433
   699
    case SDL_PIXELFORMAT_UYVY:
slouken@3433
   700
        if (renderdata->GL_APPLE_ycbcr_422_supported) {
slouken@3433
   701
            *internalFormat = GL_RGB;
slouken@3433
   702
            *format = GL_YCBCR_422_APPLE;
slouken@3433
   703
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@3433
   704
            *type = GL_UNSIGNED_SHORT_8_8_APPLE;
slouken@3433
   705
#else
slouken@3433
   706
            *type = GL_UNSIGNED_SHORT_8_8_REV_APPLE;
slouken@3433
   707
#endif
slouken@3433
   708
        } else if (renderdata->GL_MESA_ycbcr_texture_supported) {
slouken@3494
   709
            *internalFormat = GL_YCBCR_MESA;
slouken@3433
   710
            *format = GL_YCBCR_MESA;
slouken@3433
   711
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@3433
   712
            *type = GL_UNSIGNED_SHORT_8_8_MESA;
slouken@3433
   713
#else
slouken@3433
   714
            *type = GL_UNSIGNED_SHORT_8_8_REV_MESA;
slouken@3433
   715
#endif
slouken@3433
   716
        } else if (renderdata->GL_ARB_fragment_program_supported) {
slouken@3433
   717
            *internalFormat = GL_RGBA;
slouken@3433
   718
            *format = GL_RGBA;
slouken@3433
   719
            *type = GL_UNSIGNED_BYTE;
slouken@3433
   720
        } else {
slouken@3433
   721
            return SDL_FALSE;
slouken@3433
   722
        }
slouken@3433
   723
        break;
slouken@3433
   724
    case SDL_PIXELFORMAT_YUY2:
slouken@3433
   725
        if (renderdata->GL_APPLE_ycbcr_422_supported) {
slouken@3433
   726
            *internalFormat = GL_RGB;
slouken@3433
   727
            *format = GL_YCBCR_422_APPLE;
slouken@3433
   728
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@3433
   729
            *type = GL_UNSIGNED_SHORT_8_8_REV_APPLE;
slouken@3433
   730
#else
slouken@3433
   731
            *type = GL_UNSIGNED_SHORT_8_8_APPLE;
slouken@3433
   732
#endif
slouken@3433
   733
        } else if (renderdata->GL_MESA_ycbcr_texture_supported) {
slouken@3494
   734
            *internalFormat = GL_YCBCR_MESA;
slouken@3433
   735
            *format = GL_YCBCR_MESA;
slouken@3433
   736
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@3433
   737
            *type = GL_UNSIGNED_SHORT_8_8_REV_MESA;
slouken@3433
   738
#else
slouken@3433
   739
            *type = GL_UNSIGNED_SHORT_8_8_MESA;
slouken@3433
   740
#endif
slouken@3433
   741
        } else {
slouken@3433
   742
            return SDL_FALSE;
slouken@3433
   743
        }
slouken@3433
   744
        break;
slouken@3433
   745
    default:
slouken@3433
   746
        return SDL_FALSE;
slouken@3433
   747
    }
slouken@3433
   748
    return SDL_TRUE;
slouken@3433
   749
}
icculus@2835
   750
slouken@1918
   751
static int
slouken@1918
   752
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   753
{
slouken@1918
   754
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   755
    GL_TextureData *data;
slouken@1920
   756
    GLint internalFormat;
slouken@1920
   757
    GLenum format, type;
slouken@1922
   758
    int texture_w, texture_h;
icculus@2835
   759
    GLuint shader = 0;
slouken@1924
   760
    GLenum result;
slouken@1918
   761
slouken@3433
   762
    if (!convert_format(renderdata, texture->format, &internalFormat,
slouken@3433
   763
                        &format, &type)) {
slouken@1920
   764
        SDL_SetError("Unsupported texture format");
slouken@1920
   765
        return -1;
slouken@1920
   766
    }
slouken@3433
   767
    if (texture->format == SDL_PIXELFORMAT_UYVY &&
slouken@3433
   768
        !renderdata->GL_APPLE_ycbcr_422_supported &&
slouken@3433
   769
        !renderdata->GL_MESA_ycbcr_texture_supported &&
slouken@3433
   770
        renderdata->GL_ARB_fragment_program_supported) {
slouken@3433
   771
        if (renderdata->fragment_program_UYVY == 0) {
slouken@3433
   772
            renderdata->fragment_program_UYVY =
slouken@3433
   773
                compile_shader(renderdata, GL_FRAGMENT_PROGRAM_ARB,
slouken@3433
   774
                               fragment_program_UYVY_source_code);
slouken@3433
   775
            if (renderdata->fragment_program_UYVY == 0) {
slouken@3468
   776
                set_shader_error(renderdata, "UYVY");
slouken@3433
   777
                return -1;
slouken@3433
   778
            }
slouken@3433
   779
        }
slouken@3433
   780
        shader = renderdata->fragment_program_UYVY;
slouken@3433
   781
    }
slouken@1920
   782
slouken@1920
   783
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   784
    if (!data) {
slouken@1918
   785
        SDL_OutOfMemory();
slouken@1918
   786
        return -1;
slouken@1918
   787
    }
slouken@1918
   788
icculus@2835
   789
    data->shader = shader;
icculus@2835
   790
slouken@1974
   791
    if (texture->format == SDL_PIXELFORMAT_INDEX8) {
slouken@1974
   792
        data->palette = (Uint8 *) SDL_malloc(3 * 256 * sizeof(Uint8));
slouken@1974
   793
        if (!data->palette) {
slouken@1974
   794
            SDL_OutOfMemory();
slouken@1974
   795
            SDL_free(data);
slouken@1974
   796
            return -1;
slouken@1974
   797
        }
slouken@1974
   798
        SDL_memset(data->palette, 0xFF, 3 * 256 * sizeof(Uint8));
slouken@1974
   799
    }
slouken@1974
   800
slouken@2222
   801
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
icculus@2835
   802
        data->pitch = texture->w * bytes_per_pixel(texture->format);
slouken@2222
   803
        data->pixels = SDL_malloc(texture->h * data->pitch);
slouken@2222
   804
        if (!data->pixels) {
slouken@2222
   805
            SDL_OutOfMemory();
slouken@2222
   806
            SDL_free(data);
slouken@2222
   807
            return -1;
slouken@2222
   808
        }
slouken@2222
   809
    }
slouken@2222
   810
slouken@1918
   811
    texture->driverdata = data;
slouken@1918
   812
slouken@1927
   813
    renderdata->glGetError();
slouken@1927
   814
    renderdata->glGenTextures(1, &data->texture);
slouken@1926
   815
    if (renderdata->GL_ARB_texture_rectangle_supported) {
slouken@1926
   816
        data->type = GL_TEXTURE_RECTANGLE_ARB;
slouken@1926
   817
        texture_w = texture->w;
slouken@1926
   818
        texture_h = texture->h;
icculus@2835
   819
        data->texw = (GLfloat) texture_w;
icculus@2835
   820
        data->texh = (GLfloat) texture_h;
slouken@1926
   821
    } else {
slouken@1926
   822
        data->type = GL_TEXTURE_2D;
slouken@1926
   823
        texture_w = power_of_2(texture->w);
slouken@1926
   824
        texture_h = power_of_2(texture->h);
icculus@2835
   825
        data->texw = (GLfloat) (texture->w) / texture_w;
slouken@1926
   826
        data->texh = (GLfloat) texture->h / texture_h;
slouken@1926
   827
    }
icculus@2835
   828
slouken@2839
   829
    /* YUV formats use RGBA but are really two bytes per pixel */
slouken@2839
   830
    if (internalFormat == GL_RGBA && bytes_per_pixel(texture->format) < 4) {
slouken@3707
   831
        texture_w /= 2;
slouken@3707
   832
        if (data->type == GL_TEXTURE_2D) {
slouken@3707
   833
            data->texw *= 2.0f;
slouken@3707
   834
        }
slouken@2884
   835
        data->HACK_RYAN_FIXME = 2;
slouken@2843
   836
    } else {
slouken@2884
   837
        data->HACK_RYAN_FIXME = 1;
slouken@2839
   838
    }
slouken@2839
   839
slouken@1920
   840
    data->format = format;
slouken@1920
   841
    data->formattype = type;
slouken@2884
   842
    renderdata->glEnable(data->type);
slouken@1927
   843
    renderdata->glBindTexture(data->type, data->texture);
slouken@2230
   844
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@2230
   845
                                GL_NEAREST);
slouken@2230
   846
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@2230
   847
                                GL_NEAREST);
slouken@2230
   848
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@2230
   849
                                GL_CLAMP_TO_EDGE);
slouken@2230
   850
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@2230
   851
                                GL_CLAMP_TO_EDGE);
slouken@2840
   852
#ifdef __MACOSX__
slouken@2230
   853
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
slouken@2230
   854
#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
slouken@2230
   855
#endif
slouken@2230
   856
#ifndef STORAGE_CACHED_APPLE
slouken@2230
   857
#define STORAGE_CACHED_APPLE                0x85BE
slouken@2230
   858
#endif
slouken@2230
   859
#ifndef STORAGE_SHARED_APPLE
slouken@2230
   860
#define STORAGE_SHARED_APPLE                0x85BF
slouken@2230
   861
#endif
slouken@2230
   862
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@2230
   863
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   864
                                    GL_STORAGE_SHARED_APPLE);
slouken@2230
   865
    } else {
slouken@2230
   866
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   867
                                    GL_STORAGE_CACHED_APPLE);
slouken@2230
   868
    }
slouken@2809
   869
/* This causes a crash in testoverlay for some reason.  Apple bug? */
slouken@2809
   870
#if 0
bob@2295
   871
    if (texture->access == SDL_TEXTUREACCESS_STREAMING
bob@2295
   872
        && texture->format == SDL_PIXELFORMAT_ARGB8888) {
slouken@2237
   873
        /*
bob@2295
   874
           if (renderdata->glTextureRangeAPPLE) {
bob@2295
   875
           renderdata->glTextureRangeAPPLE(data->type,
bob@2295
   876
           texture->h * data->pitch,
bob@2295
   877
           data->pixels);
bob@2295
   878
           }
bob@2295
   879
         */
slouken@2230
   880
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
slouken@2230
   881
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   882
                                 texture_h, 0, format, type, data->pixels);
slouken@2230
   883
    } else
slouken@2230
   884
#endif
slouken@2809
   885
#endif
slouken@2230
   886
    {
slouken@2230
   887
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   888
                                 texture_h, 0, format, type, NULL);
slouken@2230
   889
    }
slouken@3041
   890
    renderdata->glDisable(data->type);
slouken@1927
   891
    result = renderdata->glGetError();
slouken@1924
   892
    if (result != GL_NO_ERROR) {
slouken@1924
   893
        GL_SetError("glTexImage2D()", result);
slouken@1924
   894
        return -1;
slouken@1924
   895
    }
slouken@1918
   896
    return 0;
slouken@1918
   897
}
slouken@1918
   898
slouken@1918
   899
static int
slouken@2222
   900
GL_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@2222
   901
                      void **pixels, int *pitch)
slouken@2222
   902
{
slouken@2222
   903
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@2222
   904
slouken@2222
   905
    *pixels = data->pixels;
slouken@2222
   906
    *pitch = data->pitch;
slouken@2222
   907
    return 0;
slouken@2222
   908
}
slouken@2222
   909
slouken@2222
   910
static int
slouken@1918
   911
GL_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   912
                     const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   913
{
slouken@1918
   914
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   915
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1974
   916
    Uint8 *palette;
slouken@1918
   917
slouken@1974
   918
    if (!data->palette) {
slouken@1974
   919
        SDL_SetError("Texture doesn't have a palette");
slouken@1974
   920
        return -1;
slouken@1974
   921
    }
slouken@1974
   922
    palette = data->palette + firstcolor * 3;
slouken@1974
   923
    while (ncolors--) {
slouken@1974
   924
        *palette++ = colors->r;
slouken@1974
   925
        *palette++ = colors->g;
slouken@1974
   926
        *palette++ = colors->b;
slouken@1974
   927
        ++colors;
slouken@1974
   928
    }
slouken@2884
   929
    renderdata->glEnable(data->type);
slouken@1974
   930
    renderdata->glBindTexture(data->type, data->texture);
slouken@1974
   931
    renderdata->glColorTableEXT(data->type, GL_RGB8, 256, GL_RGB,
slouken@1974
   932
                                GL_UNSIGNED_BYTE, data->palette);
slouken@1918
   933
    return 0;
slouken@1918
   934
}
slouken@1918
   935
slouken@1918
   936
static int
slouken@1918
   937
GL_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   938
                     SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   939
{
slouken@1918
   940
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1974
   941
    Uint8 *palette;
slouken@1918
   942
slouken@1974
   943
    if (!data->palette) {
slouken@1974
   944
        SDL_SetError("Texture doesn't have a palette");
slouken@1974
   945
        return -1;
slouken@1974
   946
    }
slouken@1974
   947
    palette = data->palette + firstcolor * 3;
slouken@1974
   948
    while (ncolors--) {
slouken@1974
   949
        colors->r = *palette++;
slouken@1974
   950
        colors->g = *palette++;
slouken@1974
   951
        colors->b = *palette++;
slouken@1974
   952
        colors->unused = SDL_ALPHA_OPAQUE;
slouken@1974
   953
        ++colors;
slouken@1974
   954
    }
slouken@1918
   955
    return 0;
slouken@1918
   956
}
slouken@1918
   957
slouken@1924
   958
static void
slouken@1927
   959
SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture,
slouken@1927
   960
                   int pitch)
slouken@1924
   961
{
slouken@1965
   962
    if (texture->format == SDL_PIXELFORMAT_INDEX1LSB) {
slouken@1927
   963
        renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 1);
slouken@1965
   964
    } else if (texture->format == SDL_PIXELFORMAT_INDEX1MSB) {
slouken@1927
   965
        renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 0);
slouken@1924
   966
    }
slouken@1927
   967
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@2808
   968
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@2884
   969
                              (pitch / bytes_per_pixel(texture->format)) /
slouken@3013
   970
                              ((GL_TextureData *) texture->driverdata)->
slouken@3013
   971
                              HACK_RYAN_FIXME);
slouken@1924
   972
}
slouken@1924
   973
slouken@1918
   974
static int
slouken@1985
   975
GL_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   976
{
slouken@1986
   977
    return 0;
slouken@1985
   978
}
slouken@1985
   979
slouken@1985
   980
static int
slouken@1985
   981
GL_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   982
{
slouken@1986
   983
    return 0;
slouken@1985
   984
}
slouken@1985
   985
slouken@1985
   986
static int
slouken@1985
   987
GL_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   988
{
slouken@1985
   989
    switch (texture->blendMode) {
slouken@2884
   990
    case SDL_BLENDMODE_NONE:
slouken@2884
   991
    case SDL_BLENDMODE_MASK:
slouken@2884
   992
    case SDL_BLENDMODE_BLEND:
slouken@2884
   993
    case SDL_BLENDMODE_ADD:
slouken@2884
   994
    case SDL_BLENDMODE_MOD:
slouken@1985
   995
        return 0;
slouken@1985
   996
    default:
slouken@1985
   997
        SDL_Unsupported();
slouken@2884
   998
        texture->blendMode = SDL_BLENDMODE_NONE;
slouken@1985
   999
        return -1;
slouken@1985
  1000
    }
slouken@1985
  1001
}
slouken@1985
  1002
slouken@1985
  1003
static int
slouken@1985
  1004
GL_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
  1005
{
slouken@1985
  1006
    switch (texture->scaleMode) {
slouken@1985
  1007
    case SDL_TEXTURESCALEMODE_NONE:
slouken@1985
  1008
    case SDL_TEXTURESCALEMODE_FAST:
slouken@1985
  1009
    case SDL_TEXTURESCALEMODE_SLOW:
slouken@1985
  1010
        return 0;
slouken@1985
  1011
    case SDL_TEXTURESCALEMODE_BEST:
slouken@1985
  1012
        SDL_Unsupported();
slouken@1985
  1013
        texture->scaleMode = SDL_TEXTURESCALEMODE_SLOW;
slouken@1985
  1014
        return -1;
slouken@1985
  1015
    default:
slouken@1985
  1016
        SDL_Unsupported();
slouken@1985
  1017
        texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
slouken@1985
  1018
        return -1;
slouken@1985
  1019
    }
slouken@1985
  1020
}
slouken@1985
  1021
slouken@1985
  1022
static int
slouken@1918
  1023
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
  1024
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
  1025
{
slouken@1927
  1026
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1027
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1924
  1028
    GLenum result;
slouken@1918
  1029
slouken@1927
  1030
    renderdata->glGetError();
slouken@1927
  1031
    SetupTextureUpdate(renderdata, texture, pitch);
slouken@2884
  1032
    renderdata->glEnable(data->type);
slouken@1927
  1033
    renderdata->glBindTexture(data->type, data->texture);
slouken@1927
  1034
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
slouken@1927
  1035
                                rect->h, data->format, data->formattype,
slouken@1927
  1036
                                pixels);
slouken@3041
  1037
    renderdata->glDisable(data->type);
slouken@1927
  1038
    result = renderdata->glGetError();
slouken@1924
  1039
    if (result != GL_NO_ERROR) {
slouken@1924
  1040
        GL_SetError("glTexSubImage2D()", result);
slouken@1924
  1041
        return -1;
slouken@1924
  1042
    }
slouken@1918
  1043
    return 0;
slouken@1918
  1044
}
slouken@1918
  1045
slouken@1918
  1046
static int
slouken@1918
  1047
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
  1048
               const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1918
  1049
               int *pitch)
slouken@1918
  1050
{
slouken@1918
  1051
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
  1052
slouken@1920
  1053
    if (markDirty) {
slouken@1920
  1054
        SDL_AddDirtyRect(&data->dirty, rect);
slouken@1920
  1055
    }
slouken@1918
  1056
slouken@1920
  1057
    *pixels =
slouken@1920
  1058
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
icculus@2835
  1059
                  rect->x * bytes_per_pixel(texture->format));
slouken@1920
  1060
    *pitch = data->pitch;
slouken@1918
  1061
    return 0;
slouken@1918
  1062
}
slouken@1918
  1063
slouken@1918
  1064
static void
slouken@1918
  1065
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
  1066
{
slouken@1918
  1067
}
slouken@1918
  1068
slouken@1918
  1069
static void
slouken@1918
  1070
GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
slouken@1918
  1071
                const SDL_Rect * rects)
slouken@1918
  1072
{
slouken@1918
  1073
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
  1074
    int i;
slouken@1918
  1075
slouken@1918
  1076
    for (i = 0; i < numrects; ++i) {
slouken@1920
  1077
        SDL_AddDirtyRect(&data->dirty, &rects[i]);
slouken@1918
  1078
    }
slouken@1918
  1079
}
slouken@1918
  1080
slouken@2936
  1081
static void
slouken@3458
  1082
GL_SetBlendMode(GL_RenderData * data, int blendMode, int isprimitive)
slouken@2936
  1083
{
slouken@2936
  1084
    if (blendMode != data->blendMode) {
slouken@2936
  1085
        switch (blendMode) {
slouken@2936
  1086
        case SDL_BLENDMODE_NONE:
slouken@2936
  1087
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
slouken@2936
  1088
            data->glDisable(GL_BLEND);
slouken@2936
  1089
            break;
slouken@2936
  1090
        case SDL_BLENDMODE_MASK:
slouken@3458
  1091
            if (isprimitive) {
slouken@3458
  1092
                /* The same as SDL_BLENDMODE_NONE */
slouken@3458
  1093
                blendMode = SDL_BLENDMODE_NONE;
slouken@3458
  1094
                data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
slouken@3458
  1095
                data->glDisable(GL_BLEND);
slouken@3458
  1096
            } else {
slouken@3458
  1097
                data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
slouken@3458
  1098
                data->glEnable(GL_BLEND);
slouken@3458
  1099
                data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@3458
  1100
            }
slouken@3457
  1101
            break;
slouken@2936
  1102
        case SDL_BLENDMODE_BLEND:
slouken@2936
  1103
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
  1104
            data->glEnable(GL_BLEND);
slouken@2936
  1105
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@2936
  1106
            break;
slouken@2936
  1107
        case SDL_BLENDMODE_ADD:
slouken@2936
  1108
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
  1109
            data->glEnable(GL_BLEND);
slouken@2936
  1110
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@2936
  1111
            break;
slouken@2936
  1112
        case SDL_BLENDMODE_MOD:
slouken@2936
  1113
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
  1114
            data->glEnable(GL_BLEND);
slouken@2936
  1115
            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@2936
  1116
            break;
slouken@2936
  1117
        }
slouken@2936
  1118
        data->blendMode = blendMode;
slouken@2936
  1119
    }
slouken@2936
  1120
}
slouken@2936
  1121
slouken@1918
  1122
static int
slouken@3596
  1123
GL_RenderClear(SDL_Renderer * renderer)
slouken@3596
  1124
{
slouken@3596
  1125
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3596
  1126
slouken@3596
  1127
    data->glClearColor((GLfloat) renderer->r * inv255f,
slouken@3596
  1128
                       (GLfloat) renderer->g * inv255f,
slouken@3596
  1129
                       (GLfloat) renderer->b * inv255f,
slouken@3596
  1130
                       (GLfloat) renderer->a * inv255f);
slouken@3596
  1131
slouken@3596
  1132
    data->glClear(GL_COLOR_BUFFER_BIT);
slouken@3596
  1133
slouken@3596
  1134
    return 0;
slouken@3596
  1135
}
slouken@3596
  1136
slouken@3596
  1137
static int
slouken@3596
  1138
GL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
  1139
                    int count)
slouken@2884
  1140
{
slouken@2884
  1141
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
  1142
    int i;
slouken@2884
  1143
slouken@3458
  1144
    GL_SetBlendMode(data, renderer->blendMode, 1);
slouken@2884
  1145
slouken@2884
  1146
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2884
  1147
                    (GLfloat) renderer->g * inv255f,
slouken@2884
  1148
                    (GLfloat) renderer->b * inv255f,
slouken@2884
  1149
                    (GLfloat) renderer->a * inv255f);
slouken@2884
  1150
slouken@2901
  1151
    data->glBegin(GL_POINTS);
slouken@3536
  1152
    for (i = 0; i < count; ++i) {
slouken@3536
  1153
        data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
  1154
    }
slouken@2901
  1155
    data->glEnd();
slouken@2901
  1156
slouken@2901
  1157
    return 0;
slouken@2901
  1158
}
slouken@2901
  1159
slouken@2901
  1160
static int
slouken@3596
  1161
GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
  1162
                   int count)
slouken@2901
  1163
{
slouken@2901
  1164
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
  1165
    int i;
slouken@2901
  1166
slouken@3458
  1167
    GL_SetBlendMode(data, renderer->blendMode, 1);
slouken@2901
  1168
slouken@2901
  1169
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2901
  1170
                    (GLfloat) renderer->g * inv255f,
slouken@2901
  1171
                    (GLfloat) renderer->b * inv255f,
slouken@2901
  1172
                    (GLfloat) renderer->a * inv255f);
slouken@2901
  1173
slouken@3536
  1174
    if (count > 2 && 
slouken@3536
  1175
        points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
slouken@3536
  1176
        data->glBegin(GL_LINE_LOOP);
slouken@3536
  1177
        /* GL_LINE_LOOP takes care of the final segment */
slouken@3536
  1178
        --count;
slouken@3536
  1179
        for (i = 0; i < count; ++i) {
slouken@3536
  1180
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
  1181
        }
slouken@3536
  1182
        data->glEnd();
slouken@3536
  1183
    } else {
slouken@4905
  1184
        int x1, y1, x2, y2;
slouken@4905
  1185
slouken@3536
  1186
        data->glBegin(GL_LINE_STRIP);
slouken@3536
  1187
        for (i = 0; i < count; ++i) {
slouken@3536
  1188
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
  1189
        }
slouken@3536
  1190
        data->glEnd();
slouken@3474
  1191
slouken@3536
  1192
        /* The line is half open, so we need one more point to complete it.
slouken@3536
  1193
         * http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node47.html
slouken@3536
  1194
         * If we have to, we can use vertical line and horizontal line textures
slouken@3536
  1195
         * for vertical and horizontal lines, and then create custom textures
slouken@3536
  1196
         * for diagonal lines and software render those.  It's terrible, but at
slouken@3536
  1197
         * least it would be pixel perfect.
slouken@3536
  1198
         */
slouken@3536
  1199
        data->glBegin(GL_POINTS);
slouken@3533
  1200
#if defined(__APPLE__) || defined(__WIN32__)
slouken@3536
  1201
        /* Mac OS X and Windows seem to always leave the second point open */
slouken@3536
  1202
        data->glVertex2f(0.5f + points[count-1].x, 0.5f + points[count-1].y);
slouken@3474
  1203
#else
slouken@3536
  1204
        /* Linux seems to leave the right-most or bottom-most point open */
slouken@4905
  1205
        x1 = points[0].x;
slouken@4905
  1206
        y1 = points[0].y;
slouken@4905
  1207
        x2 = points[count-1].x;
slouken@4905
  1208
        y2 = points[count-1].y;
slouken@3536
  1209
slouken@3536
  1210
        if (x1 > x2) {
slouken@3536
  1211
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@3536
  1212
        } else if (x2 > x1) {
slouken@3536
  1213
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@3536
  1214
        } else if (y1 > y2) {
slouken@3536
  1215
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@3536
  1216
        } else if (y2 > y1) {
slouken@3536
  1217
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@3536
  1218
        }
slouken@3536
  1219
#endif
slouken@3536
  1220
        data->glEnd();
slouken@3474
  1221
    }
slouken@3455
  1222
slouken@1918
  1223
    return 0;
slouken@1918
  1224
}
slouken@1918
  1225
slouken@1918
  1226
static int
slouken@3596
  1227
GL_RenderDrawRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count)
slouken@3596
  1228
{
slouken@3596
  1229
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3596
  1230
    int i, x, y;
slouken@3596
  1231
slouken@3596
  1232
    GL_SetBlendMode(data, renderer->blendMode, 1);
slouken@3596
  1233
slouken@3596
  1234
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@3596
  1235
                    (GLfloat) renderer->g * inv255f,
slouken@3596
  1236
                    (GLfloat) renderer->b * inv255f,
slouken@3596
  1237
                    (GLfloat) renderer->a * inv255f);
slouken@3596
  1238
slouken@3596
  1239
    data->glBegin(GL_LINE_LOOP);
slouken@3596
  1240
    for (i = 0; i < count; ++i) {
slouken@3596
  1241
        const SDL_Rect *rect = rects[i];
slouken@3596
  1242
slouken@3596
  1243
        x = rect->x;
slouken@3596
  1244
        y = rect->y;
slouken@3596
  1245
        data->glVertex2f(0.5f + x, 0.5f + y);
slouken@3596
  1246
slouken@3596
  1247
        x = rect->x+rect->w-1;
slouken@3596
  1248
        y = rect->y;
slouken@3596
  1249
        data->glVertex2f(0.5f + x, 0.5f + y);
slouken@3596
  1250
slouken@3596
  1251
        x = rect->x+rect->w-1;
slouken@3596
  1252
        y = rect->y+rect->h-1;
slouken@3596
  1253
        data->glVertex2f(0.5f + x, 0.5f + y);
slouken@3596
  1254
slouken@3596
  1255
        x = rect->x;
slouken@3596
  1256
        y = rect->y+rect->h-1;
slouken@3596
  1257
        data->glVertex2f(0.5f + x, 0.5f + y);
slouken@3596
  1258
    }
slouken@3596
  1259
    data->glEnd();
slouken@3596
  1260
slouken@3596
  1261
    return 0;
slouken@3596
  1262
}
slouken@3596
  1263
slouken@3596
  1264
static int
slouken@3596
  1265
GL_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count)
slouken@2925
  1266
{
slouken@2925
  1267
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
  1268
    int i;
slouken@2925
  1269
slouken@3458
  1270
    GL_SetBlendMode(data, renderer->blendMode, 1);
slouken@2936
  1271
slouken@2925
  1272
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2925
  1273
                    (GLfloat) renderer->g * inv255f,
slouken@2925
  1274
                    (GLfloat) renderer->b * inv255f,
slouken@2925
  1275
                    (GLfloat) renderer->a * inv255f);
slouken@2936
  1276
slouken@3536
  1277
    for (i = 0; i < count; ++i) {
slouken@3536
  1278
        const SDL_Rect *rect = rects[i];
slouken@3536
  1279
slouken@3536
  1280
        data->glRecti(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
slouken@3536
  1281
    }
slouken@2925
  1282
slouken@2925
  1283
    return 0;
slouken@2925
  1284
}
slouken@2925
  1285
slouken@2925
  1286
static int
slouken@1918
  1287
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
  1288
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1918
  1289
{
slouken@1918
  1290
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1291
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@3468
  1292
    GLuint shader = 0;
slouken@1918
  1293
    int minx, miny, maxx, maxy;
slouken@1918
  1294
    GLfloat minu, maxu, minv, maxv;
slouken@1918
  1295
slouken@2275
  1296
    if (texturedata->dirty.list) {
slouken@1920
  1297
        SDL_DirtyRect *dirty;
slouken@1920
  1298
        void *pixels;
icculus@2835
  1299
        int bpp = bytes_per_pixel(texture->format);
slouken@1920
  1300
        int pitch = texturedata->pitch;
slouken@1920
  1301
slouken@1927
  1302
        SetupTextureUpdate(data, texture, pitch);
slouken@2884
  1303
        data->glEnable(texturedata->type);
slouken@1927
  1304
        data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1920
  1305
        for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
slouken@1920
  1306
            SDL_Rect *rect = &dirty->rect;
slouken@1920
  1307
            pixels =
slouken@1920
  1308
                (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
slouken@1920
  1309
                          rect->x * bpp);
slouken@1927
  1310
            data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
slouken@2884
  1311
                                  rect->w / texturedata->HACK_RYAN_FIXME,
slouken@2884
  1312
                                  rect->h, texturedata->format,
slouken@1927
  1313
                                  texturedata->formattype, pixels);
slouken@1920
  1314
        }
slouken@1920
  1315
        SDL_ClearDirtyRects(&texturedata->dirty);
slouken@1920
  1316
    }
slouken@1920
  1317
slouken@1918
  1318
    minx = dstrect->x;
slouken@1918
  1319
    miny = dstrect->y;
slouken@1918
  1320
    maxx = dstrect->x + dstrect->w;
slouken@1918
  1321
    maxy = dstrect->y + dstrect->h;
slouken@1918
  1322
slouken@1918
  1323
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
  1324
    minu *= texturedata->texw;
slouken@1918
  1325
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
  1326
    maxu *= texturedata->texw;
slouken@1918
  1327
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
  1328
    minv *= texturedata->texh;
slouken@1918
  1329
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
  1330
    maxv *= texturedata->texh;
slouken@1918
  1331
slouken@2884
  1332
    data->glEnable(texturedata->type);
slouken@1927
  1333
    data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1918
  1334
slouken@1985
  1335
    if (texture->modMode) {
slouken@1985
  1336
        data->glColor4f((GLfloat) texture->r * inv255f,
slouken@1985
  1337
                        (GLfloat) texture->g * inv255f,
slouken@1985
  1338
                        (GLfloat) texture->b * inv255f,
slouken@1985
  1339
                        (GLfloat) texture->a * inv255f);
slouken@1985
  1340
    } else {
slouken@1985
  1341
        data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
slouken@1985
  1342
    }
slouken@1985
  1343
slouken@3458
  1344
    GL_SetBlendMode(data, texture->blendMode, 0);
slouken@1918
  1345
slouken@3468
  1346
    /* Set up the shader for the copy, we have a special one for MASK */
slouken@3468
  1347
    shader = texturedata->shader;
slouken@3468
  1348
    if (texture->blendMode == SDL_BLENDMODE_MASK && !shader) {
slouken@3468
  1349
        if (data->fragment_program_mask == 0) {
slouken@3468
  1350
            data->fragment_program_mask =
slouken@3468
  1351
                compile_shader(data, GL_FRAGMENT_PROGRAM_ARB,
slouken@3468
  1352
                               fragment_program_mask_source_code);
slouken@3468
  1353
            if (data->fragment_program_mask == 0) {
slouken@3468
  1354
                /* That's okay, we'll just miss some of the blend semantics */
slouken@3468
  1355
                data->fragment_program_mask = ~0;
slouken@3468
  1356
            }
slouken@3468
  1357
        }
slouken@3468
  1358
        if (data->fragment_program_mask != ~0) {
slouken@3468
  1359
            shader = data->fragment_program_mask;
slouken@3468
  1360
        }
slouken@3468
  1361
    }
slouken@3468
  1362
slouken@1985
  1363
    if (texture->scaleMode != data->scaleMode) {
slouken@1985
  1364
        switch (texture->scaleMode) {
slouken@1965
  1365
        case SDL_TEXTURESCALEMODE_NONE:
slouken@1965
  1366
        case SDL_TEXTURESCALEMODE_FAST:
slouken@1927
  1367
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
slouken@1927
  1368
                                  GL_NEAREST);
slouken@1927
  1369
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
slouken@1927
  1370
                                  GL_NEAREST);
slouken@1927
  1371
            break;
slouken@1965
  1372
        case SDL_TEXTURESCALEMODE_SLOW:
slouken@1965
  1373
        case SDL_TEXTURESCALEMODE_BEST:
slouken@1927
  1374
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
slouken@1927
  1375
                                  GL_LINEAR);
slouken@1927
  1376
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
slouken@1927
  1377
                                  GL_LINEAR);
slouken@1927
  1378
            break;
slouken@1927
  1379
        }
slouken@1985
  1380
        data->scaleMode = texture->scaleMode;
slouken@1918
  1381
    }
slouken@1918
  1382
slouken@3468
  1383
    if (shader) {
icculus@2835
  1384
        data->glEnable(GL_FRAGMENT_PROGRAM_ARB);
slouken@3468
  1385
        data->glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader);
icculus@2835
  1386
    }
icculus@2835
  1387
slouken@1927
  1388
    data->glBegin(GL_TRIANGLE_STRIP);
slouken@1927
  1389
    data->glTexCoord2f(minu, minv);
slouken@3472
  1390
    data->glVertex2f((GLfloat) minx, (GLfloat) miny);
slouken@1927
  1391
    data->glTexCoord2f(maxu, minv);
slouken@3472
  1392
    data->glVertex2f((GLfloat) maxx, (GLfloat) miny);
slouken@1927
  1393
    data->glTexCoord2f(minu, maxv);
slouken@3472
  1394
    data->glVertex2f((GLfloat) minx, (GLfloat) maxy);
slouken@1927
  1395
    data->glTexCoord2f(maxu, maxv);
slouken@3472
  1396
    data->glVertex2f((GLfloat) maxx, (GLfloat) maxy);
slouken@1927
  1397
    data->glEnd();
slouken@1918
  1398
slouken@3468
  1399
    if (shader) {
icculus@2835
  1400
        data->glDisable(GL_FRAGMENT_PROGRAM_ARB);
icculus@2835
  1401
    }
slouken@2884
  1402
slouken@2884
  1403
    data->glDisable(texturedata->type);
slouken@2884
  1404
slouken@1918
  1405
    return 0;
slouken@1918
  1406
}
slouken@1918
  1407
slouken@3431
  1408
static int
slouken@3431
  1409
GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
  1410
                    Uint32 pixel_format, void * pixels, int pitch)
slouken@3431
  1411
{
slouken@3433
  1412
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3685
  1413
    SDL_Window *window = renderer->window;
slouken@3433
  1414
    GLint internalFormat;
slouken@3433
  1415
    GLenum format, type;
slouken@3435
  1416
    Uint8 *src, *dst, *tmp;
slouken@3435
  1417
    int length, rows;
slouken@3433
  1418
slouken@3433
  1419
    if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
slouken@3435
  1420
        /* FIXME: Do a temp copy to a format that is supported */
slouken@3433
  1421
        SDL_SetError("Unsupported pixel format");
slouken@3433
  1422
        return -1;
slouken@3433
  1423
    }
slouken@3433
  1424
slouken@3433
  1425
    if (pixel_format == SDL_PIXELFORMAT_INDEX1LSB) {
slouken@3433
  1426
        data->glPixelStorei(GL_PACK_LSB_FIRST, 1);
slouken@3433
  1427
    } else if (pixel_format == SDL_PIXELFORMAT_INDEX1MSB) {
slouken@3433
  1428
        data->glPixelStorei(GL_PACK_LSB_FIRST, 0);
slouken@3433
  1429
    }
slouken@3446
  1430
    data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
slouken@3446
  1431
    data->glPixelStorei(GL_PACK_ROW_LENGTH,
slouken@3446
  1432
                        (pitch / bytes_per_pixel(pixel_format)));
slouken@3433
  1433
slouken@3447
  1434
    data->glReadPixels(rect->x, (window->h-rect->y)-rect->h, rect->w, rect->h,
slouken@3435
  1435
                       format, type, pixels);
slouken@3435
  1436
slouken@3435
  1437
    /* Flip the rows to be top-down */
slouken@3435
  1438
    length = rect->w * bytes_per_pixel(pixel_format);
slouken@3435
  1439
    src = (Uint8*)pixels + (rect->h-1)*pitch;
slouken@3435
  1440
    dst = (Uint8*)pixels;
slouken@3435
  1441
    tmp = SDL_stack_alloc(Uint8, length);
slouken@3435
  1442
    rows = rect->h / 2;
slouken@3435
  1443
    while (rows--) {
slouken@3435
  1444
        SDL_memcpy(tmp, dst, length);
slouken@3435
  1445
        SDL_memcpy(dst, src, length);
slouken@3435
  1446
        SDL_memcpy(src, tmp, length);
slouken@3447
  1447
        dst += pitch;
slouken@3447
  1448
        src -= pitch;
slouken@3435
  1449
    }
slouken@3435
  1450
    SDL_stack_free(tmp);
slouken@3440
  1451
slouken@3440
  1452
    return 0;
slouken@3431
  1453
}
slouken@3431
  1454
slouken@3431
  1455
static int
slouken@3431
  1456
GL_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
  1457
                     Uint32 pixel_format, const void * pixels, int pitch)
slouken@3431
  1458
{
slouken@3435
  1459
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3685
  1460
    SDL_Window *window = renderer->window;
slouken@3435
  1461
    GLint internalFormat;
slouken@3435
  1462
    GLenum format, type;
slouken@3607
  1463
    Uint8 *src, *dst, *tmp;
slouken@3607
  1464
    int length, rows;
slouken@3435
  1465
slouken@3435
  1466
    if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
slouken@3435
  1467
        /* FIXME: Do a temp copy to a format that is supported */
slouken@3435
  1468
        SDL_SetError("Unsupported pixel format");
slouken@3435
  1469
        return -1;
slouken@3435
  1470
    }
slouken@3435
  1471
slouken@3435
  1472
    if (pixel_format == SDL_PIXELFORMAT_INDEX1LSB) {
slouken@3435
  1473
        data->glPixelStorei(GL_UNPACK_LSB_FIRST, 1);
slouken@3435
  1474
    } else if (pixel_format == SDL_PIXELFORMAT_INDEX1MSB) {
slouken@3435
  1475
        data->glPixelStorei(GL_UNPACK_LSB_FIRST, 0);
slouken@3435
  1476
    }
slouken@3435
  1477
    data->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@3435
  1478
    data->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@3435
  1479
                        (pitch / bytes_per_pixel(pixel_format)));
slouken@3435
  1480
slouken@3607
  1481
    /* Flip the rows to be bottom-up */
slouken@3607
  1482
    length = rect->h * rect->w * pitch;
slouken@3607
  1483
    tmp = SDL_stack_alloc(Uint8, length);
slouken@3607
  1484
    src = (Uint8*)pixels + (rect->h-1)*pitch;
slouken@3607
  1485
    dst = (Uint8*)tmp;
slouken@3607
  1486
    rows = rect->h;
slouken@3607
  1487
    while (rows--) {
slouken@3607
  1488
        SDL_memcpy(dst, src, pitch);
slouken@3607
  1489
        dst += pitch;
slouken@3607
  1490
        src -= pitch;
slouken@3607
  1491
    }
slouken@3607
  1492
slouken@3607
  1493
    data->glRasterPos2i(rect->x, (window->h-rect->y));
slouken@3607
  1494
    data->glDrawPixels(rect->w, rect->h, format, type, tmp);
slouken@3607
  1495
    SDL_stack_free(tmp);
slouken@3440
  1496
slouken@3440
  1497
    return 0;
slouken@3431
  1498
}
slouken@3431
  1499
slouken@1918
  1500
static void
slouken@1918
  1501
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
  1502
{
slouken@1918
  1503
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
  1504
}
slouken@1918
  1505
slouken@1918
  1506
static void
slouken@1918
  1507
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
  1508
{
slouken@1927
  1509
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1510
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
  1511
slouken@1918
  1512
    if (!data) {
slouken@1918
  1513
        return;
slouken@1918
  1514
    }
slouken@1918
  1515
    if (data->texture) {
slouken@1927
  1516
        renderdata->glDeleteTextures(1, &data->texture);
slouken@1918
  1517
    }
slouken@1974
  1518
    if (data->palette) {
slouken@1974
  1519
        SDL_free(data->palette);
slouken@1974
  1520
    }
slouken@1920
  1521
    if (data->pixels) {
slouken@1920
  1522
        SDL_free(data->pixels);
slouken@1920
  1523
    }
slouken@1920
  1524
    SDL_FreeDirtyRects(&data->dirty);
slouken@1918
  1525
    SDL_free(data);
slouken@1918
  1526
    texture->driverdata = NULL;
slouken@1918
  1527
}
slouken@1918
  1528
slouken@1975
  1529
static void
slouken@1918
  1530
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
  1531
{
slouken@1918
  1532
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1533
slouken@1918
  1534
    if (data) {
slouken@1920
  1535
        if (data->context) {
icculus@2835
  1536
            if (data->GL_ARB_fragment_program_supported) {
icculus@2835
  1537
                data->glDisable(GL_FRAGMENT_PROGRAM_ARB);
icculus@2835
  1538
                data->glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0);
slouken@3468
  1539
                if (data->fragment_program_mask &&
slouken@3468
  1540
                    data->fragment_program_mask != ~0) {
slouken@3468
  1541
                    data->glDeleteProgramsARB(1,
slouken@3468
  1542
                                              &data->fragment_program_mask);
slouken@3468
  1543
                }
slouken@3468
  1544
                if (data->fragment_program_UYVY &&
slouken@3468
  1545
                    data->fragment_program_UYVY != ~0) {
slouken@2884
  1546
                    data->glDeleteProgramsARB(1,
slouken@2884
  1547
                                              &data->fragment_program_UYVY);
icculus@2835
  1548
                }
icculus@2835
  1549
            }
icculus@2835
  1550
bob@2328
  1551
            /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
slouken@1920
  1552
            SDL_GL_DeleteContext(data->context);
slouken@1918
  1553
        }
slouken@1918
  1554
        SDL_free(data);
slouken@1918
  1555
    }
slouken@1918
  1556
    SDL_free(renderer);
slouken@1918
  1557
}
slouken@1918
  1558
slouken@1952
  1559
#endif /* SDL_VIDEO_RENDER_OGL */
slouken@1918
  1560
slouken@1918
  1561
/* vi: set ts=4 sw=4 expandtab: */