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