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