src/video/SDL_renderer_gl.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 08 Dec 2008 00:24:15 +0000
changeset 2858 a38fcb093081
parent 2848 8a3aa505ecba
child 2884 9dde605c7540
permissions -rw-r--r--
A little cleanup for SDL snapshot release
slouken@1918
     1
/*
slouken@1918
     2
    SDL - Simple DirectMedia Layer
slouken@2858
     3
    Copyright (C) 1997-2009 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@2839
    54
        case SDL_PIXELFORMAT_YV12:
slouken@2839
    55
        case SDL_PIXELFORMAT_IYUV:
slouken@2839
    56
        case SDL_PIXELFORMAT_YUY2:
icculus@2835
    57
        case SDL_PIXELFORMAT_UYVY:
slouken@2839
    58
        case SDL_PIXELFORMAT_YVYU:
icculus@2835
    59
            return 2;
icculus@2835
    60
        default:
slouken@2839
    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@1985
    99
static int GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
slouken@1985
   100
                         Uint8 a, const SDL_Rect * rect);
slouken@1918
   101
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   102
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@1918
   103
static void GL_RenderPresent(SDL_Renderer * renderer);
slouken@1918
   104
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
   105
static void GL_DestroyRenderer(SDL_Renderer * renderer);
slouken@1918
   106
slouken@1918
   107
slouken@1918
   108
SDL_RenderDriver GL_RenderDriver = {
slouken@1918
   109
    GL_CreateRenderer,
slouken@1918
   110
    {
slouken@1918
   111
     "opengl",
slouken@1974
   112
     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD |
slouken@1974
   113
      SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
slouken@1985
   114
     (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
slouken@1985
   115
      SDL_TEXTUREMODULATE_ALPHA),
slouken@1965
   116
     (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
slouken@1965
   117
      SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD |
slouken@1965
   118
      SDL_TEXTUREBLENDMODE_MOD),
slouken@1965
   119
     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST |
slouken@1965
   120
      SDL_TEXTURESCALEMODE_SLOW),
slouken@2813
   121
     15,
slouken@1918
   122
     {
slouken@1965
   123
      SDL_PIXELFORMAT_INDEX1LSB,
slouken@1965
   124
      SDL_PIXELFORMAT_INDEX1MSB,
slouken@1965
   125
      SDL_PIXELFORMAT_INDEX8,
slouken@1965
   126
      SDL_PIXELFORMAT_RGB332,
slouken@1965
   127
      SDL_PIXELFORMAT_RGB444,
slouken@1965
   128
      SDL_PIXELFORMAT_RGB555,
slouken@1965
   129
      SDL_PIXELFORMAT_ARGB4444,
slouken@1965
   130
      SDL_PIXELFORMAT_ARGB1555,
slouken@1965
   131
      SDL_PIXELFORMAT_RGB565,
slouken@1965
   132
      SDL_PIXELFORMAT_RGB24,
slouken@1965
   133
      SDL_PIXELFORMAT_BGR24,
slouken@1965
   134
      SDL_PIXELFORMAT_RGB888,
slouken@1965
   135
      SDL_PIXELFORMAT_BGR888,
slouken@1965
   136
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
   137
      SDL_PIXELFORMAT_ABGR8888,
slouken@2813
   138
      SDL_PIXELFORMAT_ARGB2101010},
slouken@1918
   139
     0,
slouken@1918
   140
     0}
slouken@1918
   141
};
slouken@1918
   142
slouken@1918
   143
typedef struct
slouken@1918
   144
{
slouken@1918
   145
    SDL_GLContext context;
slouken@2833
   146
    SDL_bool updateSize;
slouken@2233
   147
    SDL_bool GL_ARB_texture_rectangle_supported;
slouken@1974
   148
    SDL_bool GL_EXT_paletted_texture_supported;
slouken@2845
   149
    SDL_bool GL_APPLE_ycbcr_422_supported;
slouken@2845
   150
    SDL_bool GL_MESA_ycbcr_texture_supported;
icculus@2835
   151
    SDL_bool GL_ARB_fragment_program_supported;
slouken@1927
   152
    int blendMode;
slouken@1927
   153
    int scaleMode;
slouken@1927
   154
slouken@1927
   155
    /* OpenGL functions */
slouken@1927
   156
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
slouken@1927
   157
#include "SDL_glfuncs.h"
slouken@1927
   158
#undef SDL_PROC
slouken@1974
   159
slouken@1974
   160
    PFNGLCOLORTABLEEXTPROC glColorTableEXT;
slouken@2233
   161
    void (*glTextureRangeAPPLE) (GLenum target, GLsizei length,
slouken@2233
   162
                                 const GLvoid * pointer);
icculus@2835
   163
icculus@2835
   164
    PFNGLGETPROGRAMIVARBPROC glGetProgramivARB;
icculus@2835
   165
    PFNGLGETPROGRAMSTRINGARBPROC glGetProgramStringARB;
icculus@2835
   166
    PFNGLPROGRAMLOCALPARAMETER4FVARBPROC glProgramLocalParameter4fvARB;
icculus@2835
   167
    PFNGLDELETEPROGRAMSARBPROC glDeleteProgramsARB;
icculus@2835
   168
    PFNGLGENPROGRAMSARBPROC glGenProgramsARB;
icculus@2835
   169
    PFNGLBINDPROGRAMARBPROC glBindProgramARB;
icculus@2835
   170
    PFNGLPROGRAMSTRINGARBPROC glProgramStringARB;
icculus@2835
   171
icculus@2835
   172
    /* (optional) fragment programs */
icculus@2835
   173
    GLuint fragment_program_UYVY;
slouken@1918
   174
} GL_RenderData;
slouken@1918
   175
slouken@1918
   176
typedef struct
slouken@1918
   177
{
slouken@1918
   178
    GLuint texture;
icculus@2835
   179
    GLuint shader;
slouken@1920
   180
    GLenum type;
slouken@1918
   181
    GLfloat texw;
slouken@1918
   182
    GLfloat texh;
slouken@1920
   183
    GLenum format;
slouken@1920
   184
    GLenum formattype;
slouken@1974
   185
    Uint8 *palette;
slouken@1918
   186
    void *pixels;
slouken@1918
   187
    int pitch;
slouken@1920
   188
    SDL_DirtyRectList dirty;
slouken@2843
   189
int HACK_RYAN_FIXME;
slouken@1918
   190
} GL_TextureData;
slouken@1918
   191
slouken@1918
   192
slouken@1924
   193
static void
slouken@1924
   194
GL_SetError(const char *prefix, GLenum result)
slouken@1924
   195
{
slouken@1924
   196
    const char *error;
slouken@1924
   197
slouken@1924
   198
    switch (result) {
slouken@1924
   199
    case GL_NO_ERROR:
slouken@1924
   200
        error = "GL_NO_ERROR";
slouken@1924
   201
        break;
slouken@1924
   202
    case GL_INVALID_ENUM:
slouken@1924
   203
        error = "GL_INVALID_ENUM";
slouken@1924
   204
        break;
slouken@1924
   205
    case GL_INVALID_VALUE:
slouken@1924
   206
        error = "GL_INVALID_VALUE";
slouken@1924
   207
        break;
slouken@1924
   208
    case GL_INVALID_OPERATION:
slouken@1924
   209
        error = "GL_INVALID_OPERATION";
slouken@1924
   210
        break;
slouken@1924
   211
    case GL_STACK_OVERFLOW:
slouken@1924
   212
        error = "GL_STACK_OVERFLOW";
slouken@1924
   213
        break;
slouken@1924
   214
    case GL_STACK_UNDERFLOW:
slouken@1924
   215
        error = "GL_STACK_UNDERFLOW";
slouken@1924
   216
        break;
slouken@1924
   217
    case GL_OUT_OF_MEMORY:
slouken@1924
   218
        error = "GL_OUT_OF_MEMORY";
slouken@1924
   219
        break;
slouken@1924
   220
    case GL_TABLE_TOO_LARGE:
slouken@1924
   221
        error = "GL_TABLE_TOO_LARGE";
slouken@1924
   222
        break;
slouken@1924
   223
    default:
slouken@1924
   224
        error = "UNKNOWN";
slouken@1924
   225
        break;
slouken@1924
   226
    }
slouken@1924
   227
    SDL_SetError("%s: %s", prefix, error);
slouken@1924
   228
}
slouken@1924
   229
slouken@1927
   230
static int
slouken@1927
   231
GL_LoadFunctions(GL_RenderData * data)
slouken@1927
   232
{
slouken@1927
   233
#if defined(__QNXNTO__) && (_NTO_VERSION < 630)
slouken@1927
   234
#define __SDL_NOGETPROCADDR__
slouken@1927
   235
#elif defined(__MINT__)
slouken@1927
   236
#define __SDL_NOGETPROCADDR__
slouken@1927
   237
#endif
slouken@1927
   238
#ifdef __SDL_NOGETPROCADDR__
slouken@1927
   239
#define SDL_PROC(ret,func,params) data->func=func;
slouken@1927
   240
#else
slouken@1927
   241
#define SDL_PROC(ret,func,params) \
slouken@1927
   242
    do { \
slouken@1927
   243
        data->func = SDL_GL_GetProcAddress(#func); \
slouken@1927
   244
        if ( ! data->func ) { \
slouken@1927
   245
            SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
slouken@1927
   246
            return -1; \
slouken@1927
   247
        } \
slouken@1927
   248
    } while ( 0 );
slouken@1927
   249
#endif /* __SDL_NOGETPROCADDR__ */
slouken@1927
   250
slouken@1927
   251
#include "SDL_glfuncs.h"
slouken@1927
   252
#undef SDL_PROC
slouken@1927
   253
    return 0;
slouken@1927
   254
}
slouken@1927
   255
slouken@1918
   256
void
slouken@1918
   257
GL_AddRenderDriver(_THIS)
slouken@1918
   258
{
slouken@1920
   259
    if (_this->GL_CreateContext) {
slouken@1918
   260
        SDL_AddRenderDriver(0, &GL_RenderDriver);
slouken@1918
   261
    }
slouken@1918
   262
}
slouken@1918
   263
slouken@1918
   264
SDL_Renderer *
slouken@1918
   265
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1918
   266
{
slouken@1918
   267
    SDL_Renderer *renderer;
slouken@1918
   268
    GL_RenderData *data;
slouken@1952
   269
    GLint value;
slouken@1974
   270
    int doublebuffer;
slouken@1918
   271
slouken@1974
   272
    /* Render directly to the window, unless we're compositing */
slouken@1974
   273
#ifndef __MACOSX__
slouken@1974
   274
    if (flags & SDL_RENDERER_SINGLEBUFFER) {
slouken@1974
   275
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
slouken@1974
   276
    }
slouken@1974
   277
#endif
slouken@1918
   278
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@1928
   279
        if (SDL_RecreateWindow(window, window->flags | SDL_WINDOW_OPENGL) < 0) {
slouken@1924
   280
            return NULL;
slouken@1924
   281
        }
slouken@1918
   282
    }
slouken@1918
   283
slouken@1920
   284
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1918
   285
    if (!renderer) {
slouken@1918
   286
        SDL_OutOfMemory();
slouken@1918
   287
        return NULL;
slouken@1918
   288
    }
slouken@1918
   289
slouken@1920
   290
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   291
    if (!data) {
slouken@1918
   292
        GL_DestroyRenderer(renderer);
slouken@1918
   293
        SDL_OutOfMemory();
slouken@1918
   294
        return NULL;
slouken@1918
   295
    }
slouken@1918
   296
slouken@1923
   297
    renderer->ActivateRenderer = GL_ActivateRenderer;
slouken@1970
   298
    renderer->DisplayModeChanged = GL_DisplayModeChanged;
slouken@1918
   299
    renderer->CreateTexture = GL_CreateTexture;
slouken@2222
   300
    renderer->QueryTexturePixels = GL_QueryTexturePixels;
slouken@1918
   301
    renderer->SetTexturePalette = GL_SetTexturePalette;
slouken@1918
   302
    renderer->GetTexturePalette = GL_GetTexturePalette;
slouken@1985
   303
    renderer->SetTextureColorMod = GL_SetTextureColorMod;
slouken@1985
   304
    renderer->SetTextureAlphaMod = GL_SetTextureAlphaMod;
slouken@1985
   305
    renderer->SetTextureBlendMode = GL_SetTextureBlendMode;
slouken@1985
   306
    renderer->SetTextureScaleMode = GL_SetTextureScaleMode;
slouken@1918
   307
    renderer->UpdateTexture = GL_UpdateTexture;
slouken@1918
   308
    renderer->LockTexture = GL_LockTexture;
slouken@1918
   309
    renderer->UnlockTexture = GL_UnlockTexture;
slouken@1918
   310
    renderer->DirtyTexture = GL_DirtyTexture;
slouken@1918
   311
    renderer->RenderFill = GL_RenderFill;
slouken@1918
   312
    renderer->RenderCopy = GL_RenderCopy;
slouken@1918
   313
    renderer->RenderPresent = GL_RenderPresent;
slouken@1918
   314
    renderer->DestroyTexture = GL_DestroyTexture;
slouken@1918
   315
    renderer->DestroyRenderer = GL_DestroyRenderer;
slouken@1918
   316
    renderer->info = GL_RenderDriver.info;
slouken@1918
   317
    renderer->window = window->id;
slouken@1918
   318
    renderer->driverdata = data;
slouken@1918
   319
slouken@1918
   320
    renderer->info.flags =
slouken@1965
   321
        (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED);
slouken@1918
   322
slouken@1927
   323
    if (GL_LoadFunctions(data) < 0) {
slouken@1927
   324
        GL_DestroyRenderer(renderer);
slouken@1927
   325
        return NULL;
slouken@1927
   326
    }
slouken@1927
   327
slouken@1918
   328
    data->context = SDL_GL_CreateContext(window->id);
slouken@1918
   329
    if (!data->context) {
slouken@1918
   330
        GL_DestroyRenderer(renderer);
slouken@1918
   331
        return NULL;
slouken@1918
   332
    }
slouken@1918
   333
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
slouken@1918
   334
        GL_DestroyRenderer(renderer);
slouken@1918
   335
        return NULL;
slouken@1918
   336
    }
slouken@2246
   337
#ifdef __MACOSX__
slouken@2246
   338
    /* Enable multi-threaded rendering */
slouken@2246
   339
    /* Disabled until Ryan finishes his VBO/PBO code...
bob@2295
   340
       CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
bob@2295
   341
     */
slouken@2246
   342
#endif
slouken@2246
   343
slouken@1965
   344
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1918
   345
        SDL_GL_SetSwapInterval(1);
slouken@1918
   346
    } else {
slouken@1918
   347
        SDL_GL_SetSwapInterval(0);
slouken@1918
   348
    }
slouken@1918
   349
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@1965
   350
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1918
   351
    }
slouken@1918
   352
slouken@1974
   353
    if (SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer) == 0) {
slouken@1974
   354
        if (!doublebuffer) {
slouken@1974
   355
            renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER;
slouken@1974
   356
        }
slouken@1974
   357
    }
slouken@1974
   358
slouken@1952
   359
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   360
    renderer->info.max_texture_width = value;
slouken@1952
   361
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   362
    renderer->info.max_texture_height = value;
slouken@1920
   363
slouken@1926
   364
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
slouken@1926
   365
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
slouken@1926
   366
        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
slouken@1926
   367
    }
slouken@1974
   368
    if (SDL_GL_ExtensionSupported("GL_EXT_paletted_texture")) {
slouken@1974
   369
        data->GL_EXT_paletted_texture_supported = SDL_TRUE;
slouken@1974
   370
        data->glColorTableEXT =
slouken@1974
   371
            (PFNGLCOLORTABLEEXTPROC) SDL_GL_GetProcAddress("glColorTableEXT");
slouken@1974
   372
    } else {
slouken@1974
   373
        /* Don't advertise support for 8-bit indexed texture format */
slouken@1974
   374
        Uint32 i, j;
slouken@1974
   375
        SDL_RendererInfo *info = &renderer->info;
slouken@1974
   376
        for (i = 0, j = 0; i < info->num_texture_formats; ++i) {
slouken@1974
   377
            if (info->texture_formats[i] != SDL_PIXELFORMAT_INDEX8) {
slouken@1974
   378
                info->texture_formats[j++] = info->texture_formats[i];
slouken@1974
   379
            }
slouken@1974
   380
        }
slouken@1974
   381
        --info->num_texture_formats;
slouken@1974
   382
    }
slouken@2845
   383
    if (SDL_GL_ExtensionSupported("GL_APPLE_ycbcr_422")) {
slouken@2845
   384
        data->GL_APPLE_ycbcr_422_supported = SDL_TRUE;
slouken@2845
   385
    }
slouken@2845
   386
    if (SDL_GL_ExtensionSupported("GL_MESA_ycbcr_texture")) {
slouken@2845
   387
        data->GL_MESA_ycbcr_texture_supported = SDL_TRUE;
slouken@2845
   388
    }
slouken@2233
   389
    if (SDL_GL_ExtensionSupported("GL_APPLE_texture_range")) {
slouken@2233
   390
        data->glTextureRangeAPPLE =
slouken@2233
   391
            (void (*)(GLenum, GLsizei, const GLvoid *))
slouken@2233
   392
            SDL_GL_GetProcAddress("glTextureRangeAPPLE");
slouken@2233
   393
    }
slouken@1920
   394
icculus@2835
   395
    /* we might use fragment programs for YUV data, etc. */
icculus@2835
   396
    if (SDL_GL_ExtensionSupported("GL_ARB_fragment_program")) {
icculus@2835
   397
        /* !!! FIXME: this doesn't check for errors. */
icculus@2835
   398
        /* !!! FIXME: this should really reuse the glfuncs.h stuff. */
icculus@2835
   399
        data->glGetProgramivARB = (PFNGLGETPROGRAMIVARBPROC)
icculus@2835
   400
            SDL_GL_GetProcAddress("glGetProgramivARB");
icculus@2835
   401
        data->glGetProgramStringARB = (PFNGLGETPROGRAMSTRINGARBPROC)
icculus@2835
   402
            SDL_GL_GetProcAddress("glGetProgramStringARB");
icculus@2835
   403
        data->glProgramLocalParameter4fvARB =
icculus@2835
   404
            (PFNGLPROGRAMLOCALPARAMETER4FVARBPROC)
icculus@2835
   405
            SDL_GL_GetProcAddress("glProgramLocalParameter4fvARB");
icculus@2835
   406
        data->glDeleteProgramsARB = (PFNGLDELETEPROGRAMSARBPROC)
icculus@2835
   407
            SDL_GL_GetProcAddress("glDeleteProgramsARB");
icculus@2835
   408
        data->glGenProgramsARB = (PFNGLGENPROGRAMSARBPROC)
icculus@2835
   409
            SDL_GL_GetProcAddress("glGenProgramsARB");
icculus@2835
   410
        data->glBindProgramARB = (PFNGLBINDPROGRAMARBPROC)
icculus@2835
   411
            SDL_GL_GetProcAddress("glBindProgramARB");
icculus@2835
   412
        data->glProgramStringARB = (PFNGLPROGRAMSTRINGARBPROC)
icculus@2835
   413
            SDL_GL_GetProcAddress("glProgramStringARB");
icculus@2835
   414
        data->GL_ARB_fragment_program_supported = SDL_TRUE;
icculus@2835
   415
    }
icculus@2835
   416
slouken@1918
   417
    /* Set up parameters for rendering */
slouken@1927
   418
    data->blendMode = -1;
slouken@1927
   419
    data->scaleMode = -1;
slouken@1927
   420
    data->glDisable(GL_DEPTH_TEST);
slouken@1927
   421
    data->glDisable(GL_CULL_FACE);
slouken@1926
   422
    if (data->GL_ARB_texture_rectangle_supported) {
slouken@1927
   423
        data->glEnable(GL_TEXTURE_RECTANGLE_ARB);
slouken@1926
   424
    } else {
slouken@1927
   425
        data->glEnable(GL_TEXTURE_2D);
slouken@1926
   426
    }
slouken@2833
   427
    data->updateSize = SDL_TRUE;
slouken@1918
   428
slouken@1918
   429
    return renderer;
slouken@1918
   430
}
slouken@1918
   431
slouken@1923
   432
static int
slouken@1923
   433
GL_ActivateRenderer(SDL_Renderer * renderer)
slouken@1923
   434
{
slouken@1923
   435
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1923
   436
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1923
   437
slouken@1970
   438
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
slouken@1970
   439
        return -1;
slouken@1970
   440
    }
slouken@2833
   441
    if (data->updateSize) {
slouken@2836
   442
        data->glMatrixMode(GL_PROJECTION);
slouken@2836
   443
        data->glLoadIdentity();
slouken@2836
   444
        data->glMatrixMode(GL_MODELVIEW);
slouken@2836
   445
        data->glLoadIdentity();
slouken@2836
   446
        data->glViewport(0, 0, window->w, window->h);
slouken@2836
   447
        data->glOrtho(0.0, (GLdouble) window->w, (GLdouble) window->h, 0.0,
slouken@2836
   448
                      0.0, 1.0);
slouken@2833
   449
        data->updateSize = SDL_FALSE;
slouken@2833
   450
    }
slouken@1970
   451
    return 0;
slouken@1970
   452
}
slouken@1970
   453
slouken@1970
   454
static int
slouken@1970
   455
GL_DisplayModeChanged(SDL_Renderer * renderer)
slouken@1970
   456
{
slouken@1970
   457
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1970
   458
slouken@2836
   459
    /* Rebind the context to the window area and update matrices */
slouken@2836
   460
    data->updateSize = SDL_TRUE;
slouken@2836
   461
    return GL_ActivateRenderer(renderer);
slouken@1923
   462
}
slouken@1923
   463
slouken@1922
   464
static __inline__ int
slouken@1922
   465
power_of_2(int input)
slouken@1922
   466
{
slouken@1922
   467
    int value = 1;
slouken@1922
   468
slouken@1922
   469
    while (value < input) {
slouken@1922
   470
        value <<= 1;
slouken@1922
   471
    }
slouken@1922
   472
    return value;
slouken@1922
   473
}
slouken@1922
   474
icculus@2835
   475
slouken@2858
   476
//#define DEBUG_PROGRAM_COMPILE 1
icculus@2835
   477
icculus@2835
   478
static GLuint
icculus@2847
   479
compile_shader(GL_RenderData *data, GLenum shader_type, const char *_code)
icculus@2835
   480
{
icculus@2847
   481
    const int have_texture_rects = data->GL_ARB_texture_rectangle_supported;
icculus@2847
   482
    const char *replacement = have_texture_rects ? "RECT" : "2D";
icculus@2847
   483
    const size_t replacementlen = strlen(replacement);
icculus@2847
   484
    const char *token = "%TEXTURETARGET%";
icculus@2847
   485
    const size_t tokenlen = strlen(token);
icculus@2847
   486
    char *code = NULL;
icculus@2847
   487
    char *ptr = NULL;
icculus@2847
   488
    GLuint program = 0;
icculus@2847
   489
icculus@2847
   490
    /*
icculus@2847
   491
     * The TEX instruction needs a different target depending on what we use.
icculus@2847
   492
     *  To handle this, we use "%TEXTURETARGET%" and replace the string before
icculus@2847
   493
     *  compiling the shader.
icculus@2847
   494
     */
icculus@2847
   495
    code = SDL_strdup(_code);
icculus@2847
   496
    if (code == NULL)
icculus@2847
   497
        return 0;
icculus@2847
   498
icculus@2847
   499
    for (ptr = SDL_strstr(code, token); ptr; ptr = SDL_strstr(ptr+1, token)) {
icculus@2847
   500
        memcpy(ptr, replacement, replacementlen);
icculus@2847
   501
        memmove(ptr+replacementlen, ptr+tokenlen, strlen(ptr+tokenlen)+1);
icculus@2847
   502
    }
icculus@2847
   503
icculus@2835
   504
#if DEBUG_PROGRAM_COMPILE
icculus@2847
   505
    printf("compiling shader:\n%s\n\n", code);
icculus@2835
   506
#endif
icculus@2835
   507
icculus@2835
   508
    data->glGetError();  /* flush any existing error state. */
icculus@2835
   509
    data->glGenProgramsARB(1, &program);
icculus@2835
   510
    data->glBindProgramARB(shader_type, program);
icculus@2835
   511
    data->glProgramStringARB(shader_type, GL_PROGRAM_FORMAT_ASCII_ARB,
icculus@2847
   512
                             SDL_strlen(code), code);
icculus@2847
   513
icculus@2847
   514
    SDL_free(code);
icculus@2835
   515
icculus@2835
   516
    if (data->glGetError() == GL_INVALID_OPERATION)
slouken@2845
   517
    {
icculus@2835
   518
#if DEBUG_PROGRAM_COMPILE
icculus@2835
   519
        GLint pos = 0;
icculus@2835
   520
        const GLubyte *errstr;
icculus@2835
   521
        data->glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
icculus@2835
   522
        errstr = data->glGetString(GL_PROGRAM_ERROR_STRING_ARB);
icculus@2835
   523
        printf("program compile error at position %d: %s\n\n",
icculus@2835
   524
                  (int) pos, (const char *) errstr);
icculus@2835
   525
#endif
icculus@2835
   526
        data->glBindProgramARB(shader_type, 0);
icculus@2835
   527
        data->glDeleteProgramsARB(1, &program);
icculus@2835
   528
        return 0;
icculus@2848
   529
    }
icculus@2835
   530
icculus@2835
   531
    return program;
icculus@2835
   532
}
icculus@2835
   533
icculus@2848
   534
icculus@2848
   535
/*
icculus@2848
   536
 * Fragment program that renders from UYVY textures.
icculus@2848
   537
 * The UYVY to RGB equasion is:
icculus@2848
   538
 *   R = 1.164(Y-16) + 1.596(Cr-128)
icculus@2848
   539
 *   G = 1.164(Y-16) - 0.813(Cr-128) - 0.391(Cb-128)
icculus@2848
   540
 *   B = 1.164(Y-16) + 2.018(Cb-128)
icculus@2848
   541
 * Byte layout is Cb, Y1, Cr, Y2, stored in the R, G, B, A channels.
icculus@2848
   542
 * 4 bytes == 2 pixels: Y1/Cb/Cr, Y2/Cb/Cr
icculus@2848
   543
 *
icculus@2848
   544
 * !!! FIXME: this ignores blendmodes, etc.
icculus@2848
   545
 * !!! FIXME: this could be more efficient...use a dot product for green, etc.
icculus@2848
   546
 */
icculus@2835
   547
static const char *fragment_program_UYVY_source_code =
icculus@2835
   548
    "!!ARBfp1.0\n"
icculus@2835
   549
icculus@2848
   550
    /* outputs... */
icculus@2835
   551
    "OUTPUT outcolor = result.color;\n"
icculus@2835
   552
icculus@2848
   553
    /* scratch registers... */
icculus@2835
   554
    "TEMP uyvy;\n"
icculus@2835
   555
    "TEMP luminance;\n"
icculus@2835
   556
    "TEMP work;\n"
icculus@2835
   557
icculus@2848
   558
    /* Halve the coordinates to grab the correct 32 bits for the fragment. */
icculus@2835
   559
    "MUL work, fragment.texcoord, { 0.5, 1.0, 1.0, 1.0 };\n"
icculus@2835
   560
icculus@2848
   561
    /* Sample the YUV texture. Cb, Y1, Cr, Y2, are stored in x, y, z, w. */
icculus@2847
   562
    "TEX uyvy, work, texture[0], %TEXTURETARGET%;\n"
icculus@2835
   563
icculus@2848
   564
    /* Do subtractions (128/255, 16/255, 128/255, 16/255) */
slouken@2846
   565
    "SUB uyvy, uyvy, { 0.501960784313726, 0.06274509803922, 0.501960784313726, 0.06274509803922 };\n"
icculus@2835
   566
icculus@2848
   567
    /* Choose the luminance component by texcoord. */
icculus@2848
   568
    /* !!! FIXME: laziness wins out for now... just average Y1 and Y2. */
icculus@2835
   569
    "ADD luminance, uyvy.yyyy, uyvy.wwww;\n"
icculus@2835
   570
    "MUL luminance, luminance, { 0.5, 0.5, 0.5, 0.5 };\n"
icculus@2835
   571
icculus@2848
   572
    /* Multiply luminance by its magic value. */
icculus@2835
   573
    "MUL luminance, luminance, { 1.164, 1.164, 1.164, 1.164 };\n"
icculus@2835
   574
icculus@2848
   575
    /* uyvy.xyzw becomes Cr/Cr/Cb/Cb, with multiplications. */
icculus@2835
   576
    "MUL uyvy, uyvy.zzxx, { 1.596, -0.813, 2.018, -0.391 };\n"
icculus@2835
   577
icculus@2848
   578
    /* Add luminance to Cr and Cb, store to RGB channels. */
icculus@2835
   579
    "ADD work.rgb, luminance, uyvy;\n"
icculus@2835
   580
icculus@2848
   581
    /* Do final addition for Green channel.  (!!! FIXME: this should be a DPH?) */
icculus@2835
   582
    "ADD work.g, work.g, uyvy.w;\n"
icculus@2835
   583
icculus@2848
   584
    /* Make sure alpha channel is fully opaque.  (!!! FIXME: blend modes!) */
icculus@2835
   585
    "MOV work.a, { 1.0 };\n"
icculus@2835
   586
icculus@2848
   587
    /* Store out the final fragment color... */
icculus@2835
   588
    "MOV outcolor, work;\n"
icculus@2835
   589
icculus@2848
   590
    /* ...and we're done! */
icculus@2835
   591
    "END\n";
icculus@2835
   592
icculus@2835
   593
slouken@1918
   594
static int
slouken@1918
   595
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   596
{
slouken@1918
   597
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   598
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1918
   599
    GL_TextureData *data;
slouken@1920
   600
    GLint internalFormat;
slouken@1920
   601
    GLenum format, type;
slouken@1922
   602
    int texture_w, texture_h;
icculus@2835
   603
    GLuint shader = 0;
slouken@1924
   604
    GLenum result;
slouken@1918
   605
slouken@1920
   606
    switch (texture->format) {
slouken@1965
   607
    case SDL_PIXELFORMAT_INDEX1LSB:
slouken@1965
   608
    case SDL_PIXELFORMAT_INDEX1MSB:
slouken@1920
   609
        internalFormat = GL_RGB;
slouken@1920
   610
        format = GL_COLOR_INDEX;
slouken@1920
   611
        type = GL_BITMAP;
slouken@1920
   612
        break;
slouken@1965
   613
    case SDL_PIXELFORMAT_INDEX8:
slouken@1974
   614
        if (!renderdata->GL_EXT_paletted_texture_supported) {
slouken@1974
   615
            SDL_SetError("Unsupported texture format");
slouken@1974
   616
            return -1;
slouken@1974
   617
        }
slouken@1974
   618
        internalFormat = GL_COLOR_INDEX8_EXT;
slouken@1920
   619
        format = GL_COLOR_INDEX;
slouken@1920
   620
        type = GL_UNSIGNED_BYTE;
slouken@1920
   621
        break;
slouken@1965
   622
    case SDL_PIXELFORMAT_RGB332:
slouken@1920
   623
        internalFormat = GL_R3_G3_B2;
slouken@1920
   624
        format = GL_RGB;
slouken@1920
   625
        type = GL_UNSIGNED_BYTE_3_3_2;
slouken@1920
   626
        break;
slouken@1965
   627
    case SDL_PIXELFORMAT_RGB444:
slouken@1920
   628
        internalFormat = GL_RGB4;
slouken@1920
   629
        format = GL_RGB;
slouken@1920
   630
        type = GL_UNSIGNED_SHORT_4_4_4_4;
slouken@1920
   631
        break;
slouken@1965
   632
    case SDL_PIXELFORMAT_RGB555:
slouken@1920
   633
        internalFormat = GL_RGB5;
slouken@1920
   634
        format = GL_RGB;
slouken@1920
   635
        type = GL_UNSIGNED_SHORT_5_5_5_1;
slouken@1920
   636
        break;
slouken@1965
   637
    case SDL_PIXELFORMAT_ARGB4444:
slouken@1920
   638
        internalFormat = GL_RGBA4;
slouken@1920
   639
        format = GL_BGRA;
slouken@1920
   640
        type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
slouken@1920
   641
        break;
slouken@1965
   642
    case SDL_PIXELFORMAT_ARGB1555:
slouken@1920
   643
        internalFormat = GL_RGB5_A1;
slouken@1920
   644
        format = GL_BGRA;
slouken@1920
   645
        type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
slouken@1920
   646
        break;
slouken@1965
   647
    case SDL_PIXELFORMAT_RGB565:
slouken@1920
   648
        internalFormat = GL_RGB8;
slouken@1920
   649
        format = GL_RGB;
slouken@1920
   650
        type = GL_UNSIGNED_SHORT_5_6_5;
slouken@1920
   651
        break;
slouken@1965
   652
    case SDL_PIXELFORMAT_RGB24:
slouken@1920
   653
        internalFormat = GL_RGB8;
slouken@1920
   654
        format = GL_RGB;
slouken@1920
   655
        type = GL_UNSIGNED_BYTE;
slouken@1920
   656
        break;
slouken@1965
   657
    case SDL_PIXELFORMAT_RGB888:
slouken@1920
   658
        internalFormat = GL_RGB8;
slouken@1924
   659
        format = GL_BGRA;
slouken@1924
   660
        type = GL_UNSIGNED_BYTE;
slouken@1920
   661
        break;
slouken@1965
   662
    case SDL_PIXELFORMAT_BGR24:
slouken@1920
   663
        internalFormat = GL_RGB8;
slouken@1920
   664
        format = GL_BGR;
slouken@1920
   665
        type = GL_UNSIGNED_BYTE;
slouken@1920
   666
        break;
slouken@1965
   667
    case SDL_PIXELFORMAT_BGR888:
slouken@1920
   668
        internalFormat = GL_RGB8;
slouken@1924
   669
        format = GL_RGBA;
slouken@1924
   670
        type = GL_UNSIGNED_BYTE;
slouken@1920
   671
        break;
slouken@1965
   672
    case SDL_PIXELFORMAT_ARGB8888:
slouken@2230
   673
#ifdef __MACOSX__
slouken@2230
   674
        internalFormat = GL_RGBA;
slouken@2230
   675
        format = GL_BGRA;
slouken@2230
   676
        type = GL_UNSIGNED_INT_8_8_8_8_REV;
slouken@2230
   677
#else
slouken@1920
   678
        internalFormat = GL_RGBA8;
slouken@1920
   679
        format = GL_BGRA;
slouken@1924
   680
        type = GL_UNSIGNED_BYTE;
slouken@2230
   681
#endif
slouken@1920
   682
        break;
slouken@1965
   683
    case SDL_PIXELFORMAT_ABGR8888:
slouken@1920
   684
        internalFormat = GL_RGBA8;
slouken@1920
   685
        format = GL_RGBA;
slouken@1924
   686
        type = GL_UNSIGNED_BYTE;
slouken@1920
   687
        break;
slouken@1965
   688
    case SDL_PIXELFORMAT_ARGB2101010:
slouken@1920
   689
        internalFormat = GL_RGB10_A2;
slouken@1920
   690
        format = GL_BGRA;
slouken@1920
   691
        type = GL_UNSIGNED_INT_2_10_10_10_REV;
slouken@1920
   692
        break;
slouken@2845
   693
    case SDL_PIXELFORMAT_UYVY:
slouken@2845
   694
        if (renderdata->GL_APPLE_ycbcr_422_supported) {
slouken@2845
   695
            internalFormat = GL_RGB;
slouken@2845
   696
            format = GL_YCBCR_422_APPLE;
slouken@2845
   697
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@2845
   698
            type = GL_UNSIGNED_SHORT_8_8_APPLE;
slouken@2844
   699
#else
slouken@2845
   700
            type = GL_UNSIGNED_SHORT_8_8_REV_APPLE;
slouken@2845
   701
#endif
slouken@2845
   702
        } else if (renderdata->GL_MESA_ycbcr_texture_supported) {
slouken@2845
   703
            internalFormat = GL_RGB;
slouken@2845
   704
            format = GL_YCBCR_MESA;
slouken@2845
   705
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@2845
   706
            type = GL_UNSIGNED_SHORT_8_8_MESA;
slouken@2845
   707
#else
slouken@2845
   708
            type = GL_UNSIGNED_SHORT_8_8_REV_MESA;
slouken@2845
   709
#endif
slouken@2845
   710
        } else if (renderdata->GL_ARB_fragment_program_supported) {
icculus@2835
   711
            if (renderdata->fragment_program_UYVY == 0) {
icculus@2835
   712
                renderdata->fragment_program_UYVY =
icculus@2835
   713
                    compile_shader(renderdata, GL_FRAGMENT_PROGRAM_ARB,
icculus@2835
   714
                                   fragment_program_UYVY_source_code);
icculus@2835
   715
                if (renderdata->fragment_program_UYVY == 0) {
icculus@2835
   716
                    SDL_SetError("Fragment program compile error");
icculus@2835
   717
                    return -1;
icculus@2835
   718
                }
icculus@2835
   719
            }
icculus@2835
   720
            shader = renderdata->fragment_program_UYVY;
icculus@2835
   721
            internalFormat = GL_RGBA;
icculus@2835
   722
            format = GL_RGBA;
icculus@2835
   723
            type = GL_UNSIGNED_BYTE;
icculus@2835
   724
        } else {
icculus@2835
   725
            SDL_SetError("Unsupported texture format");
icculus@2835
   726
            return -1;
icculus@2835
   727
        }
icculus@2835
   728
        break;
slouken@2845
   729
    case SDL_PIXELFORMAT_YUY2:
slouken@2845
   730
        if (renderdata->GL_APPLE_ycbcr_422_supported) {
slouken@2845
   731
            internalFormat = GL_RGB;
slouken@2845
   732
            format = GL_YCBCR_422_APPLE;
slouken@2845
   733
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@2845
   734
            type = GL_UNSIGNED_SHORT_8_8_REV_APPLE;
slouken@2845
   735
#else
slouken@2845
   736
            type = GL_UNSIGNED_SHORT_8_8_APPLE;
slouken@2844
   737
#endif
slouken@2845
   738
        } else if (renderdata->GL_MESA_ycbcr_texture_supported) {
slouken@2845
   739
            internalFormat = GL_RGB;
slouken@2845
   740
            format = GL_YCBCR_MESA;
slouken@2845
   741
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@2845
   742
            type = GL_UNSIGNED_SHORT_8_8_REV_MESA;
slouken@2845
   743
#else
slouken@2845
   744
            type = GL_UNSIGNED_SHORT_8_8_MESA;
slouken@2845
   745
#endif
slouken@2845
   746
        } else {
slouken@2845
   747
            SDL_SetError("Unsupported texture format");
slouken@2845
   748
            return -1;
slouken@2845
   749
        }
slouken@2845
   750
        break;
slouken@1920
   751
    default:
slouken@1920
   752
        SDL_SetError("Unsupported texture format");
slouken@1920
   753
        return -1;
slouken@1920
   754
    }
slouken@1920
   755
slouken@1920
   756
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   757
    if (!data) {
slouken@1918
   758
        SDL_OutOfMemory();
slouken@1918
   759
        return -1;
slouken@1918
   760
    }
slouken@1918
   761
icculus@2835
   762
    data->shader = shader;
icculus@2835
   763
slouken@1974
   764
    if (texture->format == SDL_PIXELFORMAT_INDEX8) {
slouken@1974
   765
        data->palette = (Uint8 *) SDL_malloc(3 * 256 * sizeof(Uint8));
slouken@1974
   766
        if (!data->palette) {
slouken@1974
   767
            SDL_OutOfMemory();
slouken@1974
   768
            SDL_free(data);
slouken@1974
   769
            return -1;
slouken@1974
   770
        }
slouken@1974
   771
        SDL_memset(data->palette, 0xFF, 3 * 256 * sizeof(Uint8));
slouken@1974
   772
    }
slouken@1974
   773
slouken@2222
   774
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
icculus@2835
   775
        data->pitch = texture->w * bytes_per_pixel(texture->format);
slouken@2222
   776
        data->pixels = SDL_malloc(texture->h * data->pitch);
slouken@2222
   777
        if (!data->pixels) {
slouken@2222
   778
            SDL_OutOfMemory();
slouken@2222
   779
            SDL_free(data);
slouken@2222
   780
            return -1;
slouken@2222
   781
        }
slouken@2222
   782
    }
slouken@2222
   783
slouken@1918
   784
    texture->driverdata = data;
slouken@1918
   785
slouken@1927
   786
    renderdata->glGetError();
slouken@1927
   787
    renderdata->glGenTextures(1, &data->texture);
slouken@1926
   788
    if (renderdata->GL_ARB_texture_rectangle_supported) {
slouken@1926
   789
        data->type = GL_TEXTURE_RECTANGLE_ARB;
slouken@1926
   790
        texture_w = texture->w;
slouken@1926
   791
        texture_h = texture->h;
icculus@2835
   792
        data->texw = (GLfloat) texture_w;
icculus@2835
   793
        data->texh = (GLfloat) texture_h;
slouken@1926
   794
    } else {
slouken@1926
   795
        data->type = GL_TEXTURE_2D;
slouken@1926
   796
        texture_w = power_of_2(texture->w);
slouken@1926
   797
        texture_h = power_of_2(texture->h);
icculus@2835
   798
        data->texw = (GLfloat) (texture->w) / texture_w;
slouken@1926
   799
        data->texh = (GLfloat) texture->h / texture_h;
slouken@1926
   800
    }
icculus@2835
   801
slouken@2839
   802
    /* YUV formats use RGBA but are really two bytes per pixel */
slouken@2839
   803
    if (internalFormat == GL_RGBA && bytes_per_pixel(texture->format) < 4) {
slouken@2843
   804
data->HACK_RYAN_FIXME = 2;
slouken@2843
   805
    } else {
slouken@2843
   806
data->HACK_RYAN_FIXME = 1;
slouken@2839
   807
    }
slouken@2843
   808
    texture_w /= data->HACK_RYAN_FIXME;
slouken@2839
   809
slouken@1920
   810
    data->format = format;
slouken@1920
   811
    data->formattype = type;
slouken@1927
   812
    renderdata->glBindTexture(data->type, data->texture);
slouken@2230
   813
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@2230
   814
                                GL_NEAREST);
slouken@2230
   815
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@2230
   816
                                GL_NEAREST);
slouken@2230
   817
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@2230
   818
                                GL_CLAMP_TO_EDGE);
slouken@2230
   819
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@2230
   820
                                GL_CLAMP_TO_EDGE);
slouken@2840
   821
#ifdef __MACOSX__
slouken@2230
   822
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
slouken@2230
   823
#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
slouken@2230
   824
#endif
slouken@2230
   825
#ifndef STORAGE_CACHED_APPLE
slouken@2230
   826
#define STORAGE_CACHED_APPLE                0x85BE
slouken@2230
   827
#endif
slouken@2230
   828
#ifndef STORAGE_SHARED_APPLE
slouken@2230
   829
#define STORAGE_SHARED_APPLE                0x85BF
slouken@2230
   830
#endif
slouken@2230
   831
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@2230
   832
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   833
                                    GL_STORAGE_SHARED_APPLE);
slouken@2230
   834
    } else {
slouken@2230
   835
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   836
                                    GL_STORAGE_CACHED_APPLE);
slouken@2230
   837
    }
slouken@2809
   838
/* This causes a crash in testoverlay for some reason.  Apple bug? */
slouken@2809
   839
#if 0
bob@2295
   840
    if (texture->access == SDL_TEXTUREACCESS_STREAMING
bob@2295
   841
        && texture->format == SDL_PIXELFORMAT_ARGB8888) {
slouken@2237
   842
        /*
bob@2295
   843
           if (renderdata->glTextureRangeAPPLE) {
bob@2295
   844
           renderdata->glTextureRangeAPPLE(data->type,
bob@2295
   845
           texture->h * data->pitch,
bob@2295
   846
           data->pixels);
bob@2295
   847
           }
bob@2295
   848
         */
slouken@2230
   849
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
slouken@2230
   850
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   851
                                 texture_h, 0, format, type, data->pixels);
slouken@2230
   852
    } else
slouken@2230
   853
#endif
slouken@2809
   854
#endif
slouken@2230
   855
    {
slouken@2230
   856
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   857
                                 texture_h, 0, format, type, NULL);
slouken@2230
   858
    }
slouken@1927
   859
    result = renderdata->glGetError();
slouken@1924
   860
    if (result != GL_NO_ERROR) {
slouken@1924
   861
        GL_SetError("glTexImage2D()", result);
slouken@1924
   862
        return -1;
slouken@1924
   863
    }
slouken@1918
   864
    return 0;
slouken@1918
   865
}
slouken@1918
   866
slouken@1918
   867
static int
slouken@2222
   868
GL_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@2222
   869
                      void **pixels, int *pitch)
slouken@2222
   870
{
slouken@2222
   871
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@2222
   872
slouken@2222
   873
    *pixels = data->pixels;
slouken@2222
   874
    *pitch = data->pitch;
slouken@2222
   875
    return 0;
slouken@2222
   876
}
slouken@2222
   877
slouken@2222
   878
static int
slouken@1918
   879
GL_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   880
                     const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   881
{
slouken@1918
   882
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   883
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1974
   884
    Uint8 *palette;
slouken@1918
   885
slouken@1974
   886
    if (!data->palette) {
slouken@1974
   887
        SDL_SetError("Texture doesn't have a palette");
slouken@1974
   888
        return -1;
slouken@1974
   889
    }
slouken@1974
   890
    palette = data->palette + firstcolor * 3;
slouken@1974
   891
    while (ncolors--) {
slouken@1974
   892
        *palette++ = colors->r;
slouken@1974
   893
        *palette++ = colors->g;
slouken@1974
   894
        *palette++ = colors->b;
slouken@1974
   895
        ++colors;
slouken@1974
   896
    }
slouken@1974
   897
    renderdata->glBindTexture(data->type, data->texture);
slouken@1974
   898
    renderdata->glColorTableEXT(data->type, GL_RGB8, 256, GL_RGB,
slouken@1974
   899
                                GL_UNSIGNED_BYTE, data->palette);
slouken@1918
   900
    return 0;
slouken@1918
   901
}
slouken@1918
   902
slouken@1918
   903
static int
slouken@1918
   904
GL_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   905
                     SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   906
{
slouken@1974
   907
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   908
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1974
   909
    Uint8 *palette;
slouken@1918
   910
slouken@1974
   911
    if (!data->palette) {
slouken@1974
   912
        SDL_SetError("Texture doesn't have a palette");
slouken@1974
   913
        return -1;
slouken@1974
   914
    }
slouken@1974
   915
    palette = data->palette + firstcolor * 3;
slouken@1974
   916
    while (ncolors--) {
slouken@1974
   917
        colors->r = *palette++;
slouken@1974
   918
        colors->g = *palette++;
slouken@1974
   919
        colors->b = *palette++;
slouken@1974
   920
        colors->unused = SDL_ALPHA_OPAQUE;
slouken@1974
   921
        ++colors;
slouken@1974
   922
    }
slouken@1918
   923
    return 0;
slouken@1918
   924
}
slouken@1918
   925
slouken@1924
   926
static void
slouken@1927
   927
SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture,
slouken@1927
   928
                   int pitch)
slouken@1924
   929
{
slouken@1965
   930
    if (texture->format == SDL_PIXELFORMAT_INDEX1LSB) {
slouken@1927
   931
        renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 1);
slouken@1965
   932
    } else if (texture->format == SDL_PIXELFORMAT_INDEX1MSB) {
slouken@1927
   933
        renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 0);
slouken@1924
   934
    }
slouken@1927
   935
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@2808
   936
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@2844
   937
                              (pitch / bytes_per_pixel(texture->format)) / ((GL_TextureData *)texture->driverdata)->HACK_RYAN_FIXME);
slouken@1924
   938
}
slouken@1924
   939
slouken@1918
   940
static int
slouken@1985
   941
GL_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   942
{
slouken@1986
   943
    return 0;
slouken@1985
   944
}
slouken@1985
   945
slouken@1985
   946
static int
slouken@1985
   947
GL_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   948
{
slouken@1986
   949
    return 0;
slouken@1985
   950
}
slouken@1985
   951
slouken@1985
   952
static int
slouken@1985
   953
GL_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   954
{
slouken@1985
   955
    switch (texture->blendMode) {
slouken@1985
   956
    case SDL_TEXTUREBLENDMODE_NONE:
slouken@1985
   957
    case SDL_TEXTUREBLENDMODE_MASK:
slouken@1985
   958
    case SDL_TEXTUREBLENDMODE_BLEND:
slouken@1985
   959
    case SDL_TEXTUREBLENDMODE_ADD:
slouken@1985
   960
    case SDL_TEXTUREBLENDMODE_MOD:
slouken@1985
   961
        return 0;
slouken@1985
   962
    default:
slouken@1985
   963
        SDL_Unsupported();
slouken@1985
   964
        texture->blendMode = SDL_TEXTUREBLENDMODE_NONE;
slouken@1985
   965
        return -1;
slouken@1985
   966
    }
slouken@1985
   967
}
slouken@1985
   968
slouken@1985
   969
static int
slouken@1985
   970
GL_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   971
{
slouken@1985
   972
    switch (texture->scaleMode) {
slouken@1985
   973
    case SDL_TEXTURESCALEMODE_NONE:
slouken@1985
   974
    case SDL_TEXTURESCALEMODE_FAST:
slouken@1985
   975
    case SDL_TEXTURESCALEMODE_SLOW:
slouken@1985
   976
        return 0;
slouken@1985
   977
    case SDL_TEXTURESCALEMODE_BEST:
slouken@1985
   978
        SDL_Unsupported();
slouken@1985
   979
        texture->scaleMode = SDL_TEXTURESCALEMODE_SLOW;
slouken@1985
   980
        return -1;
slouken@1985
   981
    default:
slouken@1985
   982
        SDL_Unsupported();
slouken@1985
   983
        texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
slouken@1985
   984
        return -1;
slouken@1985
   985
    }
slouken@1985
   986
}
slouken@1985
   987
slouken@1985
   988
static int
slouken@1918
   989
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   990
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
   991
{
slouken@1927
   992
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   993
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1924
   994
    GLenum result;
slouken@1918
   995
slouken@1927
   996
    renderdata->glGetError();
slouken@1927
   997
    SetupTextureUpdate(renderdata, texture, pitch);
slouken@1927
   998
    renderdata->glBindTexture(data->type, data->texture);
slouken@1927
   999
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
slouken@1927
  1000
                                rect->h, data->format, data->formattype,
slouken@1927
  1001
                                pixels);
slouken@1927
  1002
    result = renderdata->glGetError();
slouken@1924
  1003
    if (result != GL_NO_ERROR) {
slouken@1924
  1004
        GL_SetError("glTexSubImage2D()", result);
slouken@1924
  1005
        return -1;
slouken@1924
  1006
    }
slouken@1918
  1007
    return 0;
slouken@1918
  1008
}
slouken@1918
  1009
slouken@1918
  1010
static int
slouken@1918
  1011
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
  1012
               const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1918
  1013
               int *pitch)
slouken@1918
  1014
{
slouken@1918
  1015
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
  1016
slouken@1920
  1017
    if (markDirty) {
slouken@1920
  1018
        SDL_AddDirtyRect(&data->dirty, rect);
slouken@1920
  1019
    }
slouken@1918
  1020
slouken@1920
  1021
    *pixels =
slouken@1920
  1022
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
icculus@2835
  1023
                  rect->x * bytes_per_pixel(texture->format));
slouken@1920
  1024
    *pitch = data->pitch;
slouken@1918
  1025
    return 0;
slouken@1918
  1026
}
slouken@1918
  1027
slouken@1918
  1028
static void
slouken@1918
  1029
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
  1030
{
slouken@1918
  1031
}
slouken@1918
  1032
slouken@1918
  1033
static void
slouken@1918
  1034
GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
slouken@1918
  1035
                const SDL_Rect * rects)
slouken@1918
  1036
{
slouken@1918
  1037
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
  1038
    int i;
slouken@1918
  1039
slouken@1918
  1040
    for (i = 0; i < numrects; ++i) {
slouken@1920
  1041
        SDL_AddDirtyRect(&data->dirty, &rects[i]);
slouken@1918
  1042
    }
slouken@1918
  1043
}
slouken@1918
  1044
slouken@1918
  1045
static int
slouken@1985
  1046
GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a,
slouken@1985
  1047
              const SDL_Rect * rect)
slouken@1918
  1048
{
slouken@1918
  1049
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1050
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1918
  1051
slouken@1985
  1052
    data->glClearColor((GLclampf) r * inv255f, (GLclampf) g * inv255f,
slouken@1985
  1053
                       (GLclampf) b * inv255f, (GLclampf) a * inv255f);
slouken@1927
  1054
    data->glViewport(rect->x, window->h - rect->y, rect->w, rect->h);
slouken@1927
  1055
    data->glClear(GL_COLOR_BUFFER_BIT);
slouken@1927
  1056
    data->glViewport(0, 0, window->w, window->h);
slouken@1918
  1057
    return 0;
slouken@1918
  1058
}
slouken@1918
  1059
slouken@1918
  1060
static int
slouken@1918
  1061
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
  1062
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1918
  1063
{
slouken@1918
  1064
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1065
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@1918
  1066
    int minx, miny, maxx, maxy;
slouken@1918
  1067
    GLfloat minu, maxu, minv, maxv;
slouken@1918
  1068
slouken@2275
  1069
    if (texturedata->dirty.list) {
slouken@1920
  1070
        SDL_DirtyRect *dirty;
slouken@1920
  1071
        void *pixels;
icculus@2835
  1072
        int bpp = bytes_per_pixel(texture->format);
slouken@1920
  1073
        int pitch = texturedata->pitch;
slouken@1920
  1074
slouken@1927
  1075
        SetupTextureUpdate(data, texture, pitch);
slouken@1927
  1076
        data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1920
  1077
        for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
slouken@1920
  1078
            SDL_Rect *rect = &dirty->rect;
slouken@1920
  1079
            pixels =
slouken@1920
  1080
                (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
slouken@1920
  1081
                          rect->x * bpp);
slouken@1927
  1082
            data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
slouken@2843
  1083
                                  rect->w / texturedata->HACK_RYAN_FIXME, rect->h, texturedata->format,
slouken@1927
  1084
                                  texturedata->formattype, pixels);
slouken@1920
  1085
        }
slouken@1920
  1086
        SDL_ClearDirtyRects(&texturedata->dirty);
slouken@1920
  1087
    }
slouken@1920
  1088
slouken@1918
  1089
    minx = dstrect->x;
slouken@1918
  1090
    miny = dstrect->y;
slouken@1918
  1091
    maxx = dstrect->x + dstrect->w;
slouken@1918
  1092
    maxy = dstrect->y + dstrect->h;
slouken@1918
  1093
slouken@1918
  1094
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
  1095
    minu *= texturedata->texw;
slouken@1918
  1096
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
  1097
    maxu *= texturedata->texw;
slouken@1918
  1098
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
  1099
    minv *= texturedata->texh;
slouken@1918
  1100
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
  1101
    maxv *= texturedata->texh;
slouken@1918
  1102
slouken@1927
  1103
    data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1918
  1104
slouken@1985
  1105
    if (texture->modMode) {
slouken@1985
  1106
        data->glColor4f((GLfloat) texture->r * inv255f,
slouken@1985
  1107
                        (GLfloat) texture->g * inv255f,
slouken@1985
  1108
                        (GLfloat) texture->b * inv255f,
slouken@1985
  1109
                        (GLfloat) texture->a * inv255f);
slouken@1985
  1110
    } else {
slouken@1985
  1111
        data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
slouken@1985
  1112
    }
slouken@1985
  1113
slouken@1985
  1114
    if (texture->blendMode != data->blendMode) {
slouken@1985
  1115
        switch (texture->blendMode) {
slouken@1965
  1116
        case SDL_TEXTUREBLENDMODE_NONE:
slouken@1927
  1117
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
slouken@1927
  1118
            data->glDisable(GL_BLEND);
slouken@1927
  1119
            break;
slouken@1965
  1120
        case SDL_TEXTUREBLENDMODE_MASK:
slouken@1965
  1121
        case SDL_TEXTUREBLENDMODE_BLEND:
slouken@1927
  1122
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1927
  1123
            data->glEnable(GL_BLEND);
slouken@1927
  1124
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@1927
  1125
            break;
slouken@1965
  1126
        case SDL_TEXTUREBLENDMODE_ADD:
slouken@1927
  1127
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1927
  1128
            data->glEnable(GL_BLEND);
slouken@1927
  1129
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@1927
  1130
            break;
slouken@1965
  1131
        case SDL_TEXTUREBLENDMODE_MOD:
slouken@1927
  1132
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1927
  1133
            data->glEnable(GL_BLEND);
slouken@1927
  1134
            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@1927
  1135
            break;
slouken@1927
  1136
        }
slouken@1985
  1137
        data->blendMode = texture->blendMode;
slouken@1918
  1138
    }
slouken@1918
  1139
slouken@1985
  1140
    if (texture->scaleMode != data->scaleMode) {
slouken@1985
  1141
        switch (texture->scaleMode) {
slouken@1965
  1142
        case SDL_TEXTURESCALEMODE_NONE:
slouken@1965
  1143
        case SDL_TEXTURESCALEMODE_FAST:
slouken@1927
  1144
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
slouken@1927
  1145
                                  GL_NEAREST);
slouken@1927
  1146
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
slouken@1927
  1147
                                  GL_NEAREST);
slouken@1927
  1148
            break;
slouken@1965
  1149
        case SDL_TEXTURESCALEMODE_SLOW:
slouken@1965
  1150
        case SDL_TEXTURESCALEMODE_BEST:
slouken@1927
  1151
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
slouken@1927
  1152
                                  GL_LINEAR);
slouken@1927
  1153
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
slouken@1927
  1154
                                  GL_LINEAR);
slouken@1927
  1155
            break;
slouken@1927
  1156
        }
slouken@1985
  1157
        data->scaleMode = texture->scaleMode;
slouken@1918
  1158
    }
slouken@1918
  1159
icculus@2835
  1160
    if (texturedata->shader != 0) {
icculus@2835
  1161
        data->glEnable(GL_FRAGMENT_PROGRAM_ARB);
icculus@2835
  1162
        data->glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, texturedata->shader);
icculus@2835
  1163
    }
icculus@2835
  1164
slouken@1927
  1165
    data->glBegin(GL_TRIANGLE_STRIP);
slouken@1927
  1166
    data->glTexCoord2f(minu, minv);
slouken@1927
  1167
    data->glVertex2i(minx, miny);
slouken@1927
  1168
    data->glTexCoord2f(maxu, minv);
slouken@1927
  1169
    data->glVertex2i(maxx, miny);
slouken@1927
  1170
    data->glTexCoord2f(minu, maxv);
slouken@1927
  1171
    data->glVertex2i(minx, maxy);
slouken@1927
  1172
    data->glTexCoord2f(maxu, maxv);
slouken@1927
  1173
    data->glVertex2i(maxx, maxy);
slouken@1927
  1174
    data->glEnd();
slouken@1918
  1175
icculus@2835
  1176
    if (texturedata->shader != 0) {
icculus@2835
  1177
        data->glDisable(GL_FRAGMENT_PROGRAM_ARB);
icculus@2835
  1178
    }
slouken@1918
  1179
    return 0;
slouken@1918
  1180
}
slouken@1918
  1181
slouken@1918
  1182
static void
slouken@1918
  1183
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
  1184
{
slouken@1918
  1185
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
  1186
}
slouken@1918
  1187
slouken@1918
  1188
static void
slouken@1918
  1189
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
  1190
{
slouken@1927
  1191
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1192
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
  1193
slouken@1918
  1194
    if (!data) {
slouken@1918
  1195
        return;
slouken@1918
  1196
    }
slouken@1918
  1197
    if (data->texture) {
slouken@1927
  1198
        renderdata->glDeleteTextures(1, &data->texture);
slouken@1918
  1199
    }
slouken@1974
  1200
    if (data->palette) {
slouken@1974
  1201
        SDL_free(data->palette);
slouken@1974
  1202
    }
slouken@1920
  1203
    if (data->pixels) {
slouken@1920
  1204
        SDL_free(data->pixels);
slouken@1920
  1205
    }
slouken@1920
  1206
    SDL_FreeDirtyRects(&data->dirty);
slouken@1918
  1207
    SDL_free(data);
slouken@1918
  1208
    texture->driverdata = NULL;
slouken@1918
  1209
}
slouken@1918
  1210
slouken@1975
  1211
static void
slouken@1918
  1212
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
  1213
{
slouken@1918
  1214
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1215
slouken@1918
  1216
    if (data) {
slouken@1920
  1217
        if (data->context) {
icculus@2835
  1218
            if (data->GL_ARB_fragment_program_supported) {
icculus@2835
  1219
                data->glDisable(GL_FRAGMENT_PROGRAM_ARB);
icculus@2835
  1220
                data->glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0);
icculus@2835
  1221
                if (data->fragment_program_UYVY != 0) {
icculus@2835
  1222
                    data->glDeleteProgramsARB(1, &data->fragment_program_UYVY);
icculus@2835
  1223
                }
icculus@2835
  1224
            }
icculus@2835
  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
slouken@1952
  1234
#endif /* SDL_VIDEO_RENDER_OGL */
slouken@1918
  1235
slouken@1918
  1236
/* vi: set ts=4 sw=4 expandtab: */