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