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