src/video/SDL_renderer_gl.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 12 Aug 2007 07:02:28 +0000
changeset 2230 9b7d29d2432b
parent 2222 926294b2bb4e
child 2233 fb01ee9716bc
permissions -rw-r--r--
Optimized OpenGL renderer for Mac OS X.
The SDL 1.2 API version of testsprite went from 120 FPS to 320 FPS. :)
slouken@1918
     1
/*
slouken@1918
     2
    SDL - Simple DirectMedia Layer
slouken@1918
     3
    Copyright (C) 1997-2006 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@1918
    33
/* OpenGL renderer implementation */
slouken@1918
    34
slouken@2230
    35
/* Details on optimizing the texture path on Mac OS X:
slouken@2230
    36
   http://developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/chapter_10_section_2.html
slouken@2230
    37
*/
slouken@2230
    38
slouken@1985
    39
static const float inv255f = 1.0f / 255.0f;
slouken@1985
    40
slouken@1918
    41
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@1923
    42
static int GL_ActivateRenderer(SDL_Renderer * renderer);
slouken@1970
    43
static int GL_DisplayModeChanged(SDL_Renderer * renderer);
slouken@1918
    44
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@2222
    45
static int GL_QueryTexturePixels(SDL_Renderer * renderer,
slouken@2222
    46
                                 SDL_Texture * texture, void **pixels,
slouken@2222
    47
                                 int *pitch);
slouken@1918
    48
static int GL_SetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    49
                                SDL_Texture * texture,
slouken@1918
    50
                                const SDL_Color * colors, int firstcolor,
slouken@1918
    51
                                int ncolors);
slouken@1918
    52
static int GL_GetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    53
                                SDL_Texture * texture, SDL_Color * colors,
slouken@1918
    54
                                int firstcolor, int ncolors);
slouken@1985
    55
static int GL_SetTextureColorMod(SDL_Renderer * renderer,
slouken@1985
    56
                                 SDL_Texture * texture);
slouken@1985
    57
static int GL_SetTextureAlphaMod(SDL_Renderer * renderer,
slouken@1985
    58
                                 SDL_Texture * texture);
slouken@1985
    59
static int GL_SetTextureBlendMode(SDL_Renderer * renderer,
slouken@1985
    60
                                  SDL_Texture * texture);
slouken@1985
    61
static int GL_SetTextureScaleMode(SDL_Renderer * renderer,
slouken@1985
    62
                                  SDL_Texture * texture);
slouken@1918
    63
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    64
                            const SDL_Rect * rect, const void *pixels,
slouken@1918
    65
                            int pitch);
slouken@1918
    66
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    67
                          const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1985
    68
                          int *pitch);
slouken@1918
    69
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    70
static void GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    71
                            int numrects, const SDL_Rect * rects);
slouken@1985
    72
static int GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
slouken@1985
    73
                         Uint8 a, const SDL_Rect * rect);
slouken@1918
    74
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    75
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@1918
    76
static void GL_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    77
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    78
static void GL_DestroyRenderer(SDL_Renderer * renderer);
slouken@1918
    79
slouken@1918
    80
slouken@1918
    81
SDL_RenderDriver GL_RenderDriver = {
slouken@1918
    82
    GL_CreateRenderer,
slouken@1918
    83
    {
slouken@1918
    84
     "opengl",
slouken@1974
    85
     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD |
slouken@1974
    86
      SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
slouken@1985
    87
     (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
slouken@1985
    88
      SDL_TEXTUREMODULATE_ALPHA),
slouken@1965
    89
     (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
slouken@1965
    90
      SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD |
slouken@1965
    91
      SDL_TEXTUREBLENDMODE_MOD),
slouken@1965
    92
     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST |
slouken@1965
    93
      SDL_TEXTURESCALEMODE_SLOW),
slouken@1924
    94
     16,
slouken@1918
    95
     {
slouken@1965
    96
      SDL_PIXELFORMAT_INDEX1LSB,
slouken@1965
    97
      SDL_PIXELFORMAT_INDEX1MSB,
slouken@1965
    98
      SDL_PIXELFORMAT_INDEX8,
slouken@1965
    99
      SDL_PIXELFORMAT_RGB332,
slouken@1965
   100
      SDL_PIXELFORMAT_RGB444,
slouken@1965
   101
      SDL_PIXELFORMAT_RGB555,
slouken@1965
   102
      SDL_PIXELFORMAT_ARGB4444,
slouken@1965
   103
      SDL_PIXELFORMAT_ARGB1555,
slouken@1965
   104
      SDL_PIXELFORMAT_RGB565,
slouken@1965
   105
      SDL_PIXELFORMAT_RGB24,
slouken@1965
   106
      SDL_PIXELFORMAT_BGR24,
slouken@1965
   107
      SDL_PIXELFORMAT_RGB888,
slouken@1965
   108
      SDL_PIXELFORMAT_BGR888,
slouken@1965
   109
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
   110
      SDL_PIXELFORMAT_ABGR8888,
slouken@1965
   111
      SDL_PIXELFORMAT_ARGB2101010},
slouken@1918
   112
     0,
slouken@1918
   113
     0}
slouken@1918
   114
};
slouken@1918
   115
slouken@1918
   116
typedef struct
slouken@1918
   117
{
slouken@1918
   118
    SDL_GLContext context;
slouken@1970
   119
    SDL_bool updateSize;
slouken@1974
   120
    SDL_bool GL_EXT_paletted_texture_supported;
slouken@1926
   121
    SDL_bool GL_ARB_texture_rectangle_supported;
slouken@1927
   122
    int blendMode;
slouken@1927
   123
    int scaleMode;
slouken@1927
   124
slouken@1927
   125
    /* OpenGL functions */
slouken@1927
   126
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
slouken@1927
   127
#include "SDL_glfuncs.h"
slouken@1927
   128
#undef SDL_PROC
slouken@1974
   129
slouken@1974
   130
    PFNGLCOLORTABLEEXTPROC glColorTableEXT;
slouken@1918
   131
} GL_RenderData;
slouken@1918
   132
slouken@1918
   133
typedef struct
slouken@1918
   134
{
slouken@1918
   135
    GLuint texture;
slouken@1920
   136
    GLenum type;
slouken@1918
   137
    GLfloat texw;
slouken@1918
   138
    GLfloat texh;
slouken@1920
   139
    GLenum format;
slouken@1920
   140
    GLenum formattype;
slouken@1974
   141
    Uint8 *palette;
slouken@1918
   142
    void *pixels;
slouken@1918
   143
    int pitch;
slouken@1920
   144
    SDL_DirtyRectList dirty;
slouken@1918
   145
} GL_TextureData;
slouken@1918
   146
slouken@1918
   147
slouken@1924
   148
static void
slouken@1924
   149
GL_SetError(const char *prefix, GLenum result)
slouken@1924
   150
{
slouken@1924
   151
    const char *error;
slouken@1924
   152
slouken@1924
   153
    switch (result) {
slouken@1924
   154
    case GL_NO_ERROR:
slouken@1924
   155
        error = "GL_NO_ERROR";
slouken@1924
   156
        break;
slouken@1924
   157
    case GL_INVALID_ENUM:
slouken@1924
   158
        error = "GL_INVALID_ENUM";
slouken@1924
   159
        break;
slouken@1924
   160
    case GL_INVALID_VALUE:
slouken@1924
   161
        error = "GL_INVALID_VALUE";
slouken@1924
   162
        break;
slouken@1924
   163
    case GL_INVALID_OPERATION:
slouken@1924
   164
        error = "GL_INVALID_OPERATION";
slouken@1924
   165
        break;
slouken@1924
   166
    case GL_STACK_OVERFLOW:
slouken@1924
   167
        error = "GL_STACK_OVERFLOW";
slouken@1924
   168
        break;
slouken@1924
   169
    case GL_STACK_UNDERFLOW:
slouken@1924
   170
        error = "GL_STACK_UNDERFLOW";
slouken@1924
   171
        break;
slouken@1924
   172
    case GL_OUT_OF_MEMORY:
slouken@1924
   173
        error = "GL_OUT_OF_MEMORY";
slouken@1924
   174
        break;
slouken@1924
   175
    case GL_TABLE_TOO_LARGE:
slouken@1924
   176
        error = "GL_TABLE_TOO_LARGE";
slouken@1924
   177
        break;
slouken@1924
   178
    default:
slouken@1924
   179
        error = "UNKNOWN";
slouken@1924
   180
        break;
slouken@1924
   181
    }
slouken@1924
   182
    SDL_SetError("%s: %s", prefix, error);
slouken@1924
   183
}
slouken@1924
   184
slouken@1927
   185
static int
slouken@1927
   186
GL_LoadFunctions(GL_RenderData * data)
slouken@1927
   187
{
slouken@1927
   188
#if defined(__QNXNTO__) && (_NTO_VERSION < 630)
slouken@1927
   189
#define __SDL_NOGETPROCADDR__
slouken@1927
   190
#elif defined(__MINT__)
slouken@1927
   191
#define __SDL_NOGETPROCADDR__
slouken@1927
   192
#endif
slouken@1927
   193
#ifdef __SDL_NOGETPROCADDR__
slouken@1927
   194
#define SDL_PROC(ret,func,params) data->func=func;
slouken@1927
   195
#else
slouken@1927
   196
#define SDL_PROC(ret,func,params) \
slouken@1927
   197
    do { \
slouken@1927
   198
        data->func = SDL_GL_GetProcAddress(#func); \
slouken@1927
   199
        if ( ! data->func ) { \
slouken@1927
   200
            SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
slouken@1927
   201
            return -1; \
slouken@1927
   202
        } \
slouken@1927
   203
    } while ( 0 );
slouken@1927
   204
#endif /* __SDL_NOGETPROCADDR__ */
slouken@1927
   205
slouken@1927
   206
#include "SDL_glfuncs.h"
slouken@1927
   207
#undef SDL_PROC
slouken@1927
   208
    return 0;
slouken@1927
   209
}
slouken@1927
   210
slouken@1918
   211
void
slouken@1918
   212
GL_AddRenderDriver(_THIS)
slouken@1918
   213
{
slouken@1920
   214
    if (_this->GL_CreateContext) {
slouken@1918
   215
        SDL_AddRenderDriver(0, &GL_RenderDriver);
slouken@1918
   216
    }
slouken@1918
   217
}
slouken@1918
   218
slouken@1918
   219
SDL_Renderer *
slouken@1918
   220
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1918
   221
{
slouken@1918
   222
    SDL_Renderer *renderer;
slouken@1918
   223
    GL_RenderData *data;
slouken@1952
   224
    GLint value;
slouken@1974
   225
    int doublebuffer;
slouken@1918
   226
slouken@1974
   227
    /* Render directly to the window, unless we're compositing */
slouken@1974
   228
#ifndef __MACOSX__
slouken@1974
   229
    if (flags & SDL_RENDERER_SINGLEBUFFER) {
slouken@1974
   230
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
slouken@1974
   231
    }
slouken@1974
   232
#endif
slouken@1918
   233
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@1928
   234
        if (SDL_RecreateWindow(window, window->flags | SDL_WINDOW_OPENGL) < 0) {
slouken@1924
   235
            return NULL;
slouken@1924
   236
        }
slouken@1918
   237
    }
slouken@1918
   238
slouken@1920
   239
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1918
   240
    if (!renderer) {
slouken@1918
   241
        SDL_OutOfMemory();
slouken@1918
   242
        return NULL;
slouken@1918
   243
    }
slouken@1918
   244
slouken@1920
   245
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   246
    if (!data) {
slouken@1918
   247
        GL_DestroyRenderer(renderer);
slouken@1918
   248
        SDL_OutOfMemory();
slouken@1918
   249
        return NULL;
slouken@1918
   250
    }
slouken@1918
   251
slouken@1923
   252
    renderer->ActivateRenderer = GL_ActivateRenderer;
slouken@1970
   253
    renderer->DisplayModeChanged = GL_DisplayModeChanged;
slouken@1918
   254
    renderer->CreateTexture = GL_CreateTexture;
slouken@2222
   255
    renderer->QueryTexturePixels = GL_QueryTexturePixels;
slouken@1918
   256
    renderer->SetTexturePalette = GL_SetTexturePalette;
slouken@1918
   257
    renderer->GetTexturePalette = GL_GetTexturePalette;
slouken@1985
   258
    renderer->SetTextureColorMod = GL_SetTextureColorMod;
slouken@1985
   259
    renderer->SetTextureAlphaMod = GL_SetTextureAlphaMod;
slouken@1985
   260
    renderer->SetTextureBlendMode = GL_SetTextureBlendMode;
slouken@1985
   261
    renderer->SetTextureScaleMode = GL_SetTextureScaleMode;
slouken@1918
   262
    renderer->UpdateTexture = GL_UpdateTexture;
slouken@1918
   263
    renderer->LockTexture = GL_LockTexture;
slouken@1918
   264
    renderer->UnlockTexture = GL_UnlockTexture;
slouken@1918
   265
    renderer->DirtyTexture = GL_DirtyTexture;
slouken@1918
   266
    renderer->RenderFill = GL_RenderFill;
slouken@1918
   267
    renderer->RenderCopy = GL_RenderCopy;
slouken@1918
   268
    renderer->RenderPresent = GL_RenderPresent;
slouken@1918
   269
    renderer->DestroyTexture = GL_DestroyTexture;
slouken@1918
   270
    renderer->DestroyRenderer = GL_DestroyRenderer;
slouken@1918
   271
    renderer->info = GL_RenderDriver.info;
slouken@1918
   272
    renderer->window = window->id;
slouken@1918
   273
    renderer->driverdata = data;
slouken@1918
   274
slouken@1918
   275
    renderer->info.flags =
slouken@1965
   276
        (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED);
slouken@1918
   277
slouken@1927
   278
    if (GL_LoadFunctions(data) < 0) {
slouken@1927
   279
        GL_DestroyRenderer(renderer);
slouken@1927
   280
        return NULL;
slouken@1927
   281
    }
slouken@1927
   282
slouken@1918
   283
    data->context = SDL_GL_CreateContext(window->id);
slouken@1918
   284
    if (!data->context) {
slouken@1918
   285
        GL_DestroyRenderer(renderer);
slouken@1918
   286
        return NULL;
slouken@1918
   287
    }
slouken@1918
   288
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
slouken@1918
   289
        GL_DestroyRenderer(renderer);
slouken@1918
   290
        return NULL;
slouken@1918
   291
    }
slouken@1918
   292
slouken@1965
   293
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1918
   294
        SDL_GL_SetSwapInterval(1);
slouken@1918
   295
    } else {
slouken@1918
   296
        SDL_GL_SetSwapInterval(0);
slouken@1918
   297
    }
slouken@1918
   298
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@1965
   299
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1918
   300
    }
slouken@1918
   301
slouken@1974
   302
    if (SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer) == 0) {
slouken@1974
   303
        if (!doublebuffer) {
slouken@1974
   304
            renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER;
slouken@1974
   305
        }
slouken@1974
   306
    }
slouken@1974
   307
slouken@1952
   308
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   309
    renderer->info.max_texture_width = value;
slouken@1952
   310
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   311
    renderer->info.max_texture_height = value;
slouken@1920
   312
slouken@1926
   313
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
slouken@1926
   314
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
slouken@1926
   315
        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
slouken@1926
   316
    }
slouken@1974
   317
    if (SDL_GL_ExtensionSupported("GL_EXT_paletted_texture")) {
slouken@1974
   318
        data->GL_EXT_paletted_texture_supported = SDL_TRUE;
slouken@1974
   319
        data->glColorTableEXT =
slouken@1974
   320
            (PFNGLCOLORTABLEEXTPROC) SDL_GL_GetProcAddress("glColorTableEXT");
slouken@1974
   321
    } else {
slouken@1974
   322
        /* Don't advertise support for 8-bit indexed texture format */
slouken@1974
   323
        Uint32 i, j;
slouken@1974
   324
        SDL_RendererInfo *info = &renderer->info;
slouken@1974
   325
        for (i = 0, j = 0; i < info->num_texture_formats; ++i) {
slouken@1974
   326
            if (info->texture_formats[i] != SDL_PIXELFORMAT_INDEX8) {
slouken@1974
   327
                info->texture_formats[j++] = info->texture_formats[i];
slouken@1974
   328
            }
slouken@1974
   329
        }
slouken@1974
   330
        --info->num_texture_formats;
slouken@1974
   331
    }
slouken@1920
   332
slouken@1918
   333
    /* Set up parameters for rendering */
slouken@1927
   334
    data->blendMode = -1;
slouken@1927
   335
    data->scaleMode = -1;
slouken@1927
   336
    data->glDisable(GL_DEPTH_TEST);
slouken@1927
   337
    data->glDisable(GL_CULL_FACE);
slouken@1926
   338
    if (data->GL_ARB_texture_rectangle_supported) {
slouken@1927
   339
        data->glEnable(GL_TEXTURE_RECTANGLE_ARB);
slouken@1926
   340
    } else {
slouken@1927
   341
        data->glEnable(GL_TEXTURE_2D);
slouken@1926
   342
    }
slouken@1970
   343
    data->updateSize = SDL_TRUE;
slouken@1918
   344
slouken@1918
   345
    return renderer;
slouken@1918
   346
}
slouken@1918
   347
slouken@1923
   348
static int
slouken@1923
   349
GL_ActivateRenderer(SDL_Renderer * renderer)
slouken@1923
   350
{
slouken@1923
   351
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1923
   352
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1923
   353
slouken@1970
   354
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
slouken@1970
   355
        return -1;
slouken@1970
   356
    }
slouken@1970
   357
    if (data->updateSize) {
slouken@1970
   358
        data->glMatrixMode(GL_PROJECTION);
slouken@1970
   359
        data->glLoadIdentity();
slouken@1970
   360
        data->glMatrixMode(GL_MODELVIEW);
slouken@1970
   361
        data->glLoadIdentity();
slouken@1970
   362
        data->glViewport(0, 0, window->w, window->h);
slouken@1970
   363
        data->glOrtho(0.0, (GLdouble) window->w, (GLdouble) window->h, 0.0,
slouken@1970
   364
                      0.0, 1.0);
slouken@1970
   365
        data->updateSize = SDL_FALSE;
slouken@1970
   366
    }
slouken@1970
   367
    return 0;
slouken@1970
   368
}
slouken@1970
   369
slouken@1970
   370
static int
slouken@1970
   371
GL_DisplayModeChanged(SDL_Renderer * renderer)
slouken@1970
   372
{
slouken@1970
   373
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1970
   374
slouken@1970
   375
    data->updateSize = SDL_TRUE;
slouken@1970
   376
    return 0;
slouken@1923
   377
}
slouken@1923
   378
slouken@1922
   379
static __inline__ int
slouken@1922
   380
power_of_2(int input)
slouken@1922
   381
{
slouken@1922
   382
    int value = 1;
slouken@1922
   383
slouken@1922
   384
    while (value < input) {
slouken@1922
   385
        value <<= 1;
slouken@1922
   386
    }
slouken@1922
   387
    return value;
slouken@1922
   388
}
slouken@1922
   389
slouken@1918
   390
static int
slouken@1918
   391
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   392
{
slouken@1918
   393
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   394
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1918
   395
    GL_TextureData *data;
slouken@1920
   396
    GLint internalFormat;
slouken@1920
   397
    GLenum format, type;
slouken@1922
   398
    int texture_w, texture_h;
slouken@1924
   399
    GLenum result;
slouken@1918
   400
slouken@1920
   401
    switch (texture->format) {
slouken@1965
   402
    case SDL_PIXELFORMAT_INDEX1LSB:
slouken@1965
   403
    case SDL_PIXELFORMAT_INDEX1MSB:
slouken@1920
   404
        internalFormat = GL_RGB;
slouken@1920
   405
        format = GL_COLOR_INDEX;
slouken@1920
   406
        type = GL_BITMAP;
slouken@1920
   407
        break;
slouken@1965
   408
    case SDL_PIXELFORMAT_INDEX8:
slouken@1974
   409
        if (!renderdata->GL_EXT_paletted_texture_supported) {
slouken@1974
   410
            SDL_SetError("Unsupported texture format");
slouken@1974
   411
            return -1;
slouken@1974
   412
        }
slouken@1974
   413
        internalFormat = GL_COLOR_INDEX8_EXT;
slouken@1920
   414
        format = GL_COLOR_INDEX;
slouken@1920
   415
        type = GL_UNSIGNED_BYTE;
slouken@1920
   416
        break;
slouken@1965
   417
    case SDL_PIXELFORMAT_RGB332:
slouken@1920
   418
        internalFormat = GL_R3_G3_B2;
slouken@1920
   419
        format = GL_RGB;
slouken@1920
   420
        type = GL_UNSIGNED_BYTE_3_3_2;
slouken@1920
   421
        break;
slouken@1965
   422
    case SDL_PIXELFORMAT_RGB444:
slouken@1920
   423
        internalFormat = GL_RGB4;
slouken@1920
   424
        format = GL_RGB;
slouken@1920
   425
        type = GL_UNSIGNED_SHORT_4_4_4_4;
slouken@1920
   426
        break;
slouken@1965
   427
    case SDL_PIXELFORMAT_RGB555:
slouken@1920
   428
        internalFormat = GL_RGB5;
slouken@1920
   429
        format = GL_RGB;
slouken@1920
   430
        type = GL_UNSIGNED_SHORT_5_5_5_1;
slouken@1920
   431
        break;
slouken@1965
   432
    case SDL_PIXELFORMAT_ARGB4444:
slouken@1920
   433
        internalFormat = GL_RGBA4;
slouken@1920
   434
        format = GL_BGRA;
slouken@1920
   435
        type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
slouken@1920
   436
        break;
slouken@1965
   437
    case SDL_PIXELFORMAT_ARGB1555:
slouken@1920
   438
        internalFormat = GL_RGB5_A1;
slouken@1920
   439
        format = GL_BGRA;
slouken@1920
   440
        type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
slouken@1920
   441
        break;
slouken@1965
   442
    case SDL_PIXELFORMAT_RGB565:
slouken@1920
   443
        internalFormat = GL_RGB8;
slouken@1920
   444
        format = GL_RGB;
slouken@1920
   445
        type = GL_UNSIGNED_SHORT_5_6_5;
slouken@1920
   446
        break;
slouken@1965
   447
    case SDL_PIXELFORMAT_RGB24:
slouken@1920
   448
        internalFormat = GL_RGB8;
slouken@1920
   449
        format = GL_RGB;
slouken@1920
   450
        type = GL_UNSIGNED_BYTE;
slouken@1920
   451
        break;
slouken@1965
   452
    case SDL_PIXELFORMAT_RGB888:
slouken@1920
   453
        internalFormat = GL_RGB8;
slouken@1924
   454
        format = GL_BGRA;
slouken@1924
   455
        type = GL_UNSIGNED_BYTE;
slouken@1920
   456
        break;
slouken@1965
   457
    case SDL_PIXELFORMAT_BGR24:
slouken@1920
   458
        internalFormat = GL_RGB8;
slouken@1920
   459
        format = GL_BGR;
slouken@1920
   460
        type = GL_UNSIGNED_BYTE;
slouken@1920
   461
        break;
slouken@1965
   462
    case SDL_PIXELFORMAT_BGR888:
slouken@1920
   463
        internalFormat = GL_RGB8;
slouken@1924
   464
        format = GL_RGBA;
slouken@1924
   465
        type = GL_UNSIGNED_BYTE;
slouken@1920
   466
        break;
slouken@1965
   467
    case SDL_PIXELFORMAT_ARGB8888:
slouken@2230
   468
#ifdef __MACOSX__
slouken@2230
   469
        internalFormat = GL_RGBA;
slouken@2230
   470
        format = GL_BGRA;
slouken@2230
   471
        type = GL_UNSIGNED_INT_8_8_8_8_REV;
slouken@2230
   472
#else
slouken@1920
   473
        internalFormat = GL_RGBA8;
slouken@1920
   474
        format = GL_BGRA;
slouken@1924
   475
        type = GL_UNSIGNED_BYTE;
slouken@2230
   476
#endif
slouken@1920
   477
        break;
slouken@1965
   478
    case SDL_PIXELFORMAT_ABGR8888:
slouken@1920
   479
        internalFormat = GL_RGBA8;
slouken@1920
   480
        format = GL_RGBA;
slouken@1924
   481
        type = GL_UNSIGNED_BYTE;
slouken@1920
   482
        break;
slouken@1965
   483
    case SDL_PIXELFORMAT_ARGB2101010:
slouken@1920
   484
        internalFormat = GL_RGB10_A2;
slouken@1920
   485
        format = GL_BGRA;
slouken@1920
   486
        type = GL_UNSIGNED_INT_2_10_10_10_REV;
slouken@1920
   487
        break;
slouken@1920
   488
    default:
slouken@1920
   489
        SDL_SetError("Unsupported texture format");
slouken@1920
   490
        return -1;
slouken@1920
   491
    }
slouken@1920
   492
slouken@1920
   493
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   494
    if (!data) {
slouken@1918
   495
        SDL_OutOfMemory();
slouken@1918
   496
        return -1;
slouken@1918
   497
    }
slouken@1918
   498
slouken@1974
   499
    if (texture->format == SDL_PIXELFORMAT_INDEX8) {
slouken@1974
   500
        data->palette = (Uint8 *) SDL_malloc(3 * 256 * sizeof(Uint8));
slouken@1974
   501
        if (!data->palette) {
slouken@1974
   502
            SDL_OutOfMemory();
slouken@1974
   503
            SDL_free(data);
slouken@1974
   504
            return -1;
slouken@1974
   505
        }
slouken@1974
   506
        SDL_memset(data->palette, 0xFF, 3 * 256 * sizeof(Uint8));
slouken@1974
   507
    }
slouken@1974
   508
slouken@2222
   509
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@2222
   510
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
slouken@2222
   511
        data->pixels = SDL_malloc(texture->h * data->pitch);
slouken@2222
   512
        if (!data->pixels) {
slouken@2222
   513
            SDL_OutOfMemory();
slouken@2222
   514
            SDL_free(data);
slouken@2222
   515
            return -1;
slouken@2222
   516
        }
slouken@2222
   517
    }
slouken@2222
   518
slouken@1918
   519
    texture->driverdata = data;
slouken@1918
   520
slouken@1927
   521
    renderdata->glGetError();
slouken@1927
   522
    renderdata->glGenTextures(1, &data->texture);
slouken@1926
   523
    if (renderdata->GL_ARB_texture_rectangle_supported) {
slouken@1926
   524
        data->type = GL_TEXTURE_RECTANGLE_ARB;
slouken@1926
   525
        texture_w = texture->w;
slouken@1926
   526
        texture_h = texture->h;
slouken@1926
   527
        data->texw = (GLfloat) texture->w;
slouken@1926
   528
        data->texh = (GLfloat) texture->h;
slouken@1926
   529
    } else {
slouken@1926
   530
        data->type = GL_TEXTURE_2D;
slouken@1926
   531
        texture_w = power_of_2(texture->w);
slouken@1926
   532
        texture_h = power_of_2(texture->h);
slouken@1926
   533
        data->texw = (GLfloat) texture->w / texture_w;
slouken@1926
   534
        data->texh = (GLfloat) texture->h / texture_h;
slouken@1926
   535
    }
slouken@1920
   536
    data->format = format;
slouken@1920
   537
    data->formattype = type;
slouken@1927
   538
    renderdata->glBindTexture(data->type, data->texture);
slouken@2230
   539
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@2230
   540
                                GL_NEAREST);
slouken@2230
   541
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@2230
   542
                                GL_NEAREST);
slouken@2230
   543
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@2230
   544
                                GL_CLAMP_TO_EDGE);
slouken@2230
   545
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@2230
   546
                                GL_CLAMP_TO_EDGE);
slouken@2230
   547
#ifdef __MACOSX__
slouken@2230
   548
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
slouken@2230
   549
#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
slouken@2230
   550
#endif
slouken@2230
   551
#ifndef STORAGE_CACHED_APPLE
slouken@2230
   552
#define STORAGE_CACHED_APPLE                0x85BE
slouken@2230
   553
#endif
slouken@2230
   554
#ifndef STORAGE_SHARED_APPLE
slouken@2230
   555
#define STORAGE_SHARED_APPLE                0x85BF
slouken@2230
   556
#endif
slouken@2230
   557
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@2230
   558
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   559
                                    GL_STORAGE_SHARED_APPLE);
slouken@2230
   560
    } else {
slouken@2230
   561
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   562
                                    GL_STORAGE_CACHED_APPLE);
slouken@2230
   563
    }
slouken@2230
   564
    if (data->pixels && internalFormat == GL_RGBA && format == GL_BGRA
slouken@2230
   565
        && type == GL_UNSIGNED_INT_8_8_8_8_REV && data->pixels) {
slouken@2230
   566
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
slouken@2230
   567
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   568
                                 texture_h, 0, format, type, data->pixels);
slouken@2230
   569
    } else
slouken@2230
   570
#endif
slouken@2230
   571
    {
slouken@2230
   572
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   573
                                 texture_h, 0, format, type, NULL);
slouken@2230
   574
    }
slouken@1927
   575
    result = renderdata->glGetError();
slouken@1924
   576
    if (result != GL_NO_ERROR) {
slouken@1924
   577
        GL_SetError("glTexImage2D()", result);
slouken@1924
   578
        return -1;
slouken@1924
   579
    }
slouken@1918
   580
    return 0;
slouken@1918
   581
}
slouken@1918
   582
slouken@1918
   583
static int
slouken@2222
   584
GL_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@2222
   585
                      void **pixels, int *pitch)
slouken@2222
   586
{
slouken@2222
   587
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@2222
   588
slouken@2222
   589
    *pixels = data->pixels;
slouken@2222
   590
    *pitch = data->pitch;
slouken@2222
   591
    return 0;
slouken@2222
   592
}
slouken@2222
   593
slouken@2222
   594
static int
slouken@1918
   595
GL_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   596
                     const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   597
{
slouken@1918
   598
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   599
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1974
   600
    Uint8 *palette;
slouken@1918
   601
slouken@1974
   602
    if (!data->palette) {
slouken@1974
   603
        SDL_SetError("Texture doesn't have a palette");
slouken@1974
   604
        return -1;
slouken@1974
   605
    }
slouken@1974
   606
    palette = data->palette + firstcolor * 3;
slouken@1974
   607
    while (ncolors--) {
slouken@1974
   608
        *palette++ = colors->r;
slouken@1974
   609
        *palette++ = colors->g;
slouken@1974
   610
        *palette++ = colors->b;
slouken@1974
   611
        ++colors;
slouken@1974
   612
    }
slouken@1974
   613
    renderdata->glBindTexture(data->type, data->texture);
slouken@1974
   614
    renderdata->glColorTableEXT(data->type, GL_RGB8, 256, GL_RGB,
slouken@1974
   615
                                GL_UNSIGNED_BYTE, data->palette);
slouken@1918
   616
    return 0;
slouken@1918
   617
}
slouken@1918
   618
slouken@1918
   619
static int
slouken@1918
   620
GL_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   621
                     SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   622
{
slouken@1974
   623
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   624
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1974
   625
    Uint8 *palette;
slouken@1918
   626
slouken@1974
   627
    if (!data->palette) {
slouken@1974
   628
        SDL_SetError("Texture doesn't have a palette");
slouken@1974
   629
        return -1;
slouken@1974
   630
    }
slouken@1974
   631
    palette = data->palette + firstcolor * 3;
slouken@1974
   632
    while (ncolors--) {
slouken@1974
   633
        colors->r = *palette++;
slouken@1974
   634
        colors->g = *palette++;
slouken@1974
   635
        colors->b = *palette++;
slouken@1974
   636
        colors->unused = SDL_ALPHA_OPAQUE;
slouken@1974
   637
        ++colors;
slouken@1974
   638
    }
slouken@1918
   639
    return 0;
slouken@1918
   640
}
slouken@1918
   641
slouken@1924
   642
static void
slouken@1927
   643
SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture,
slouken@1927
   644
                   int pitch)
slouken@1924
   645
{
slouken@1965
   646
    if (texture->format == SDL_PIXELFORMAT_INDEX1LSB) {
slouken@1927
   647
        renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 1);
slouken@1965
   648
    } else if (texture->format == SDL_PIXELFORMAT_INDEX1MSB) {
slouken@1927
   649
        renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 0);
slouken@1924
   650
    }
slouken@1927
   651
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@1927
   652
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@1927
   653
                              pitch / SDL_BYTESPERPIXEL(texture->format));
slouken@1924
   654
}
slouken@1924
   655
slouken@1918
   656
static int
slouken@1985
   657
GL_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   658
{
slouken@1986
   659
    return 0;
slouken@1985
   660
}
slouken@1985
   661
slouken@1985
   662
static int
slouken@1985
   663
GL_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   664
{
slouken@1986
   665
    return 0;
slouken@1985
   666
}
slouken@1985
   667
slouken@1985
   668
static int
slouken@1985
   669
GL_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   670
{
slouken@1985
   671
    switch (texture->blendMode) {
slouken@1985
   672
    case SDL_TEXTUREBLENDMODE_NONE:
slouken@1985
   673
    case SDL_TEXTUREBLENDMODE_MASK:
slouken@1985
   674
    case SDL_TEXTUREBLENDMODE_BLEND:
slouken@1985
   675
    case SDL_TEXTUREBLENDMODE_ADD:
slouken@1985
   676
    case SDL_TEXTUREBLENDMODE_MOD:
slouken@1985
   677
        return 0;
slouken@1985
   678
    default:
slouken@1985
   679
        SDL_Unsupported();
slouken@1985
   680
        texture->blendMode = SDL_TEXTUREBLENDMODE_NONE;
slouken@1985
   681
        return -1;
slouken@1985
   682
    }
slouken@1985
   683
}
slouken@1985
   684
slouken@1985
   685
static int
slouken@1985
   686
GL_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   687
{
slouken@1985
   688
    switch (texture->scaleMode) {
slouken@1985
   689
    case SDL_TEXTURESCALEMODE_NONE:
slouken@1985
   690
    case SDL_TEXTURESCALEMODE_FAST:
slouken@1985
   691
    case SDL_TEXTURESCALEMODE_SLOW:
slouken@1985
   692
        return 0;
slouken@1985
   693
    case SDL_TEXTURESCALEMODE_BEST:
slouken@1985
   694
        SDL_Unsupported();
slouken@1985
   695
        texture->scaleMode = SDL_TEXTURESCALEMODE_SLOW;
slouken@1985
   696
        return -1;
slouken@1985
   697
    default:
slouken@1985
   698
        SDL_Unsupported();
slouken@1985
   699
        texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
slouken@1985
   700
        return -1;
slouken@1985
   701
    }
slouken@1985
   702
}
slouken@1985
   703
slouken@1985
   704
static int
slouken@1918
   705
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   706
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
   707
{
slouken@1927
   708
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   709
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1924
   710
    GLenum result;
slouken@1918
   711
slouken@1927
   712
    renderdata->glGetError();
slouken@1927
   713
    SetupTextureUpdate(renderdata, texture, pitch);
slouken@1927
   714
    renderdata->glBindTexture(data->type, data->texture);
slouken@1927
   715
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
slouken@1927
   716
                                rect->h, data->format, data->formattype,
slouken@1927
   717
                                pixels);
slouken@1927
   718
    result = renderdata->glGetError();
slouken@1924
   719
    if (result != GL_NO_ERROR) {
slouken@1924
   720
        GL_SetError("glTexSubImage2D()", result);
slouken@1924
   721
        return -1;
slouken@1924
   722
    }
slouken@1918
   723
    return 0;
slouken@1918
   724
}
slouken@1918
   725
slouken@1918
   726
static int
slouken@1918
   727
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   728
               const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1918
   729
               int *pitch)
slouken@1918
   730
{
slouken@1918
   731
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   732
slouken@1920
   733
    if (markDirty) {
slouken@1920
   734
        SDL_AddDirtyRect(&data->dirty, rect);
slouken@1920
   735
    }
slouken@1918
   736
slouken@1920
   737
    *pixels =
slouken@1920
   738
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@1920
   739
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@1920
   740
    *pitch = data->pitch;
slouken@1918
   741
    return 0;
slouken@1918
   742
}
slouken@1918
   743
slouken@1918
   744
static void
slouken@1918
   745
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   746
{
slouken@1918
   747
}
slouken@1918
   748
slouken@1918
   749
static void
slouken@1918
   750
GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
slouken@1918
   751
                const SDL_Rect * rects)
slouken@1918
   752
{
slouken@1918
   753
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   754
    int i;
slouken@1918
   755
slouken@1918
   756
    for (i = 0; i < numrects; ++i) {
slouken@1920
   757
        SDL_AddDirtyRect(&data->dirty, &rects[i]);
slouken@1918
   758
    }
slouken@1918
   759
}
slouken@1918
   760
slouken@1918
   761
static int
slouken@1985
   762
GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a,
slouken@1985
   763
              const SDL_Rect * rect)
slouken@1918
   764
{
slouken@1918
   765
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   766
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1918
   767
slouken@1985
   768
    data->glClearColor((GLclampf) r * inv255f, (GLclampf) g * inv255f,
slouken@1985
   769
                       (GLclampf) b * inv255f, (GLclampf) a * inv255f);
slouken@1927
   770
    data->glViewport(rect->x, window->h - rect->y, rect->w, rect->h);
slouken@1927
   771
    data->glClear(GL_COLOR_BUFFER_BIT);
slouken@1927
   772
    data->glViewport(0, 0, window->w, window->h);
slouken@1918
   773
    return 0;
slouken@1918
   774
}
slouken@1918
   775
slouken@1918
   776
static int
slouken@1918
   777
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   778
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1918
   779
{
slouken@1918
   780
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   781
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@1918
   782
    int minx, miny, maxx, maxy;
slouken@1918
   783
    GLfloat minu, maxu, minv, maxv;
slouken@1918
   784
slouken@1920
   785
    if (texturedata->dirty.count > 0) {
slouken@1920
   786
        SDL_DirtyRect *dirty;
slouken@1920
   787
        void *pixels;
slouken@1920
   788
        int bpp = SDL_BYTESPERPIXEL(texture->format);
slouken@1920
   789
        int pitch = texturedata->pitch;
slouken@1920
   790
slouken@1927
   791
        SetupTextureUpdate(data, texture, pitch);
slouken@1927
   792
        data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1920
   793
        for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
slouken@1920
   794
            SDL_Rect *rect = &dirty->rect;
slouken@1920
   795
            pixels =
slouken@1920
   796
                (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
slouken@1920
   797
                          rect->x * bpp);
slouken@1927
   798
            data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
slouken@1927
   799
                                  rect->w, rect->h, texturedata->format,
slouken@1927
   800
                                  texturedata->formattype, pixels);
slouken@1920
   801
        }
slouken@1920
   802
        SDL_ClearDirtyRects(&texturedata->dirty);
slouken@1920
   803
    }
slouken@1920
   804
slouken@1918
   805
    minx = dstrect->x;
slouken@1918
   806
    miny = dstrect->y;
slouken@1918
   807
    maxx = dstrect->x + dstrect->w;
slouken@1918
   808
    maxy = dstrect->y + dstrect->h;
slouken@1918
   809
slouken@1918
   810
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
   811
    minu *= texturedata->texw;
slouken@1918
   812
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
   813
    maxu *= texturedata->texw;
slouken@1918
   814
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
   815
    minv *= texturedata->texh;
slouken@1918
   816
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
   817
    maxv *= texturedata->texh;
slouken@1918
   818
slouken@1927
   819
    data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1918
   820
slouken@1985
   821
    if (texture->modMode) {
slouken@1985
   822
        data->glColor4f((GLfloat) texture->r * inv255f,
slouken@1985
   823
                        (GLfloat) texture->g * inv255f,
slouken@1985
   824
                        (GLfloat) texture->b * inv255f,
slouken@1985
   825
                        (GLfloat) texture->a * inv255f);
slouken@1985
   826
    } else {
slouken@1985
   827
        data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
slouken@1985
   828
    }
slouken@1985
   829
slouken@1985
   830
    if (texture->blendMode != data->blendMode) {
slouken@1985
   831
        switch (texture->blendMode) {
slouken@1965
   832
        case SDL_TEXTUREBLENDMODE_NONE:
slouken@1927
   833
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
slouken@1927
   834
            data->glDisable(GL_BLEND);
slouken@1927
   835
            break;
slouken@1965
   836
        case SDL_TEXTUREBLENDMODE_MASK:
slouken@1965
   837
        case SDL_TEXTUREBLENDMODE_BLEND:
slouken@1927
   838
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1927
   839
            data->glEnable(GL_BLEND);
slouken@1927
   840
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@1927
   841
            break;
slouken@1965
   842
        case SDL_TEXTUREBLENDMODE_ADD:
slouken@1927
   843
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1927
   844
            data->glEnable(GL_BLEND);
slouken@1927
   845
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@1927
   846
            break;
slouken@1965
   847
        case SDL_TEXTUREBLENDMODE_MOD:
slouken@1927
   848
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1927
   849
            data->glEnable(GL_BLEND);
slouken@1927
   850
            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@1927
   851
            break;
slouken@1927
   852
        }
slouken@1985
   853
        data->blendMode = texture->blendMode;
slouken@1918
   854
    }
slouken@1918
   855
slouken@1985
   856
    if (texture->scaleMode != data->scaleMode) {
slouken@1985
   857
        switch (texture->scaleMode) {
slouken@1965
   858
        case SDL_TEXTURESCALEMODE_NONE:
slouken@1965
   859
        case SDL_TEXTURESCALEMODE_FAST:
slouken@1927
   860
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
slouken@1927
   861
                                  GL_NEAREST);
slouken@1927
   862
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
slouken@1927
   863
                                  GL_NEAREST);
slouken@1927
   864
            break;
slouken@1965
   865
        case SDL_TEXTURESCALEMODE_SLOW:
slouken@1965
   866
        case SDL_TEXTURESCALEMODE_BEST:
slouken@1927
   867
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
slouken@1927
   868
                                  GL_LINEAR);
slouken@1927
   869
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
slouken@1927
   870
                                  GL_LINEAR);
slouken@1927
   871
            break;
slouken@1927
   872
        }
slouken@1985
   873
        data->scaleMode = texture->scaleMode;
slouken@1918
   874
    }
slouken@1918
   875
slouken@1927
   876
    data->glBegin(GL_TRIANGLE_STRIP);
slouken@1927
   877
    data->glTexCoord2f(minu, minv);
slouken@1927
   878
    data->glVertex2i(minx, miny);
slouken@1927
   879
    data->glTexCoord2f(maxu, minv);
slouken@1927
   880
    data->glVertex2i(maxx, miny);
slouken@1927
   881
    data->glTexCoord2f(minu, maxv);
slouken@1927
   882
    data->glVertex2i(minx, maxy);
slouken@1927
   883
    data->glTexCoord2f(maxu, maxv);
slouken@1927
   884
    data->glVertex2i(maxx, maxy);
slouken@1927
   885
    data->glEnd();
slouken@1918
   886
slouken@1918
   887
    return 0;
slouken@1918
   888
}
slouken@1918
   889
slouken@1918
   890
static void
slouken@1918
   891
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
   892
{
slouken@1918
   893
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
   894
}
slouken@1918
   895
slouken@1918
   896
static void
slouken@1918
   897
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   898
{
slouken@1927
   899
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   900
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   901
slouken@1918
   902
    if (!data) {
slouken@1918
   903
        return;
slouken@1918
   904
    }
slouken@1918
   905
    if (data->texture) {
slouken@1927
   906
        renderdata->glDeleteTextures(1, &data->texture);
slouken@1918
   907
    }
slouken@1974
   908
    if (data->palette) {
slouken@1974
   909
        SDL_free(data->palette);
slouken@1974
   910
    }
slouken@1920
   911
    if (data->pixels) {
slouken@1920
   912
        SDL_free(data->pixels);
slouken@1920
   913
    }
slouken@1920
   914
    SDL_FreeDirtyRects(&data->dirty);
slouken@1918
   915
    SDL_free(data);
slouken@1918
   916
    texture->driverdata = NULL;
slouken@1918
   917
}
slouken@1918
   918
slouken@1975
   919
static void
slouken@1918
   920
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
   921
{
slouken@1918
   922
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   923
slouken@1918
   924
    if (data) {
slouken@1920
   925
        if (data->context) {
slouken@1920
   926
            SDL_GL_MakeCurrent(0, NULL);
slouken@1920
   927
            SDL_GL_DeleteContext(data->context);
slouken@1918
   928
        }
slouken@1918
   929
        SDL_free(data);
slouken@1918
   930
    }
slouken@1918
   931
    SDL_free(renderer);
slouken@1918
   932
}
slouken@1918
   933
slouken@1952
   934
#endif /* SDL_VIDEO_RENDER_OGL */
slouken@1918
   935
slouken@1918
   936
/* vi: set ts=4 sw=4 expandtab: */