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