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