src/video/SDL_renderer_gl.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 06 Aug 2006 04:39:13 +0000
changeset 1970 db3ba6c0d0df
parent 1965 a788656ca29a
child 1974 70deaf574153
permissions -rw-r--r--
Allow the render context to do necessary work when the video mode changes.
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@1918
    35
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@1923
    36
static int GL_ActivateRenderer(SDL_Renderer * renderer);
slouken@1970
    37
static int GL_DisplayModeChanged(SDL_Renderer * renderer);
slouken@1918
    38
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    39
static int GL_SetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    40
                                SDL_Texture * texture,
slouken@1918
    41
                                const SDL_Color * colors, int firstcolor,
slouken@1918
    42
                                int ncolors);
slouken@1918
    43
static int GL_GetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    44
                                SDL_Texture * texture, SDL_Color * colors,
slouken@1918
    45
                                int firstcolor, int ncolors);
slouken@1918
    46
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    47
                            const SDL_Rect * rect, const void *pixels,
slouken@1918
    48
                            int pitch);
slouken@1918
    49
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    50
                          const SDL_Rect * rect, int markDirty,
slouken@1918
    51
                          void **pixels, int *pitch);
slouken@1918
    52
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    53
static void GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    54
                            int numrects, const SDL_Rect * rects);
slouken@1918
    55
static int GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1918
    56
                         Uint32 color);
slouken@1918
    57
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    58
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1918
    59
                         int blendMode, int scaleMode);
slouken@1918
    60
static void GL_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    61
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    62
static void GL_DestroyRenderer(SDL_Renderer * renderer);
slouken@1918
    63
slouken@1918
    64
slouken@1918
    65
SDL_RenderDriver GL_RenderDriver = {
slouken@1918
    66
    GL_CreateRenderer,
slouken@1918
    67
    {
slouken@1918
    68
     "opengl",
slouken@1965
    69
     (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_PRESENTVSYNC |
slouken@1965
    70
      SDL_RENDERER_ACCELERATED),
slouken@1965
    71
     (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
slouken@1965
    72
      SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD |
slouken@1965
    73
      SDL_TEXTUREBLENDMODE_MOD),
slouken@1965
    74
     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST |
slouken@1965
    75
      SDL_TEXTURESCALEMODE_SLOW),
slouken@1924
    76
     16,
slouken@1918
    77
     {
slouken@1965
    78
      SDL_PIXELFORMAT_INDEX1LSB,
slouken@1965
    79
      SDL_PIXELFORMAT_INDEX1MSB,
slouken@1965
    80
      SDL_PIXELFORMAT_INDEX8,
slouken@1965
    81
      SDL_PIXELFORMAT_RGB332,
slouken@1965
    82
      SDL_PIXELFORMAT_RGB444,
slouken@1965
    83
      SDL_PIXELFORMAT_RGB555,
slouken@1965
    84
      SDL_PIXELFORMAT_ARGB4444,
slouken@1965
    85
      SDL_PIXELFORMAT_ARGB1555,
slouken@1965
    86
      SDL_PIXELFORMAT_RGB565,
slouken@1965
    87
      SDL_PIXELFORMAT_RGB24,
slouken@1965
    88
      SDL_PIXELFORMAT_BGR24,
slouken@1965
    89
      SDL_PIXELFORMAT_RGB888,
slouken@1965
    90
      SDL_PIXELFORMAT_BGR888,
slouken@1965
    91
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
    92
      SDL_PIXELFORMAT_ABGR8888,
slouken@1965
    93
      SDL_PIXELFORMAT_ARGB2101010},
slouken@1918
    94
     0,
slouken@1918
    95
     0}
slouken@1918
    96
};
slouken@1918
    97
slouken@1918
    98
typedef struct
slouken@1918
    99
{
slouken@1918
   100
    SDL_GLContext context;
slouken@1970
   101
    SDL_bool updateSize;
slouken@1926
   102
    SDL_bool GL_ARB_texture_rectangle_supported;
slouken@1927
   103
    int blendMode;
slouken@1927
   104
    int scaleMode;
slouken@1927
   105
slouken@1927
   106
    /* OpenGL functions */
slouken@1927
   107
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
slouken@1927
   108
#include "SDL_glfuncs.h"
slouken@1927
   109
#undef SDL_PROC
slouken@1918
   110
} GL_RenderData;
slouken@1918
   111
slouken@1918
   112
typedef struct
slouken@1918
   113
{
slouken@1918
   114
    GLuint texture;
slouken@1920
   115
    GLenum type;
slouken@1918
   116
    GLfloat texw;
slouken@1918
   117
    GLfloat texh;
slouken@1920
   118
    GLenum format;
slouken@1920
   119
    GLenum formattype;
slouken@1918
   120
    void *pixels;
slouken@1918
   121
    int pitch;
slouken@1920
   122
    SDL_DirtyRectList dirty;
slouken@1918
   123
} GL_TextureData;
slouken@1918
   124
slouken@1918
   125
slouken@1924
   126
static void
slouken@1924
   127
GL_SetError(const char *prefix, GLenum result)
slouken@1924
   128
{
slouken@1924
   129
    const char *error;
slouken@1924
   130
slouken@1924
   131
    switch (result) {
slouken@1924
   132
    case GL_NO_ERROR:
slouken@1924
   133
        error = "GL_NO_ERROR";
slouken@1924
   134
        break;
slouken@1924
   135
    case GL_INVALID_ENUM:
slouken@1924
   136
        error = "GL_INVALID_ENUM";
slouken@1924
   137
        break;
slouken@1924
   138
    case GL_INVALID_VALUE:
slouken@1924
   139
        error = "GL_INVALID_VALUE";
slouken@1924
   140
        break;
slouken@1924
   141
    case GL_INVALID_OPERATION:
slouken@1924
   142
        error = "GL_INVALID_OPERATION";
slouken@1924
   143
        break;
slouken@1924
   144
    case GL_STACK_OVERFLOW:
slouken@1924
   145
        error = "GL_STACK_OVERFLOW";
slouken@1924
   146
        break;
slouken@1924
   147
    case GL_STACK_UNDERFLOW:
slouken@1924
   148
        error = "GL_STACK_UNDERFLOW";
slouken@1924
   149
        break;
slouken@1924
   150
    case GL_OUT_OF_MEMORY:
slouken@1924
   151
        error = "GL_OUT_OF_MEMORY";
slouken@1924
   152
        break;
slouken@1924
   153
    case GL_TABLE_TOO_LARGE:
slouken@1924
   154
        error = "GL_TABLE_TOO_LARGE";
slouken@1924
   155
        break;
slouken@1924
   156
    default:
slouken@1924
   157
        error = "UNKNOWN";
slouken@1924
   158
        break;
slouken@1924
   159
    }
slouken@1924
   160
    SDL_SetError("%s: %s", prefix, error);
slouken@1924
   161
}
slouken@1924
   162
slouken@1927
   163
static int
slouken@1927
   164
GL_LoadFunctions(GL_RenderData * data)
slouken@1927
   165
{
slouken@1927
   166
#if defined(__QNXNTO__) && (_NTO_VERSION < 630)
slouken@1927
   167
#define __SDL_NOGETPROCADDR__
slouken@1927
   168
#elif defined(__MINT__)
slouken@1927
   169
#define __SDL_NOGETPROCADDR__
slouken@1927
   170
#endif
slouken@1927
   171
#ifdef __SDL_NOGETPROCADDR__
slouken@1927
   172
#define SDL_PROC(ret,func,params) data->func=func;
slouken@1927
   173
#else
slouken@1927
   174
#define SDL_PROC(ret,func,params) \
slouken@1927
   175
    do { \
slouken@1927
   176
        data->func = SDL_GL_GetProcAddress(#func); \
slouken@1927
   177
        if ( ! data->func ) { \
slouken@1927
   178
            SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
slouken@1927
   179
            return -1; \
slouken@1927
   180
        } \
slouken@1927
   181
    } while ( 0 );
slouken@1927
   182
#endif /* __SDL_NOGETPROCADDR__ */
slouken@1927
   183
slouken@1927
   184
#include "SDL_glfuncs.h"
slouken@1927
   185
#undef SDL_PROC
slouken@1927
   186
    return 0;
slouken@1927
   187
}
slouken@1927
   188
slouken@1918
   189
void
slouken@1918
   190
GL_AddRenderDriver(_THIS)
slouken@1918
   191
{
slouken@1920
   192
    if (_this->GL_CreateContext) {
slouken@1918
   193
        SDL_AddRenderDriver(0, &GL_RenderDriver);
slouken@1918
   194
    }
slouken@1918
   195
}
slouken@1918
   196
slouken@1918
   197
SDL_Renderer *
slouken@1918
   198
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1918
   199
{
slouken@1918
   200
    SDL_Renderer *renderer;
slouken@1918
   201
    GL_RenderData *data;
slouken@1952
   202
    GLint value;
slouken@1918
   203
slouken@1918
   204
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@1928
   205
        if (SDL_RecreateWindow(window, window->flags | SDL_WINDOW_OPENGL) < 0) {
slouken@1924
   206
            return NULL;
slouken@1924
   207
        }
slouken@1918
   208
    }
slouken@1918
   209
slouken@1920
   210
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1918
   211
    if (!renderer) {
slouken@1918
   212
        SDL_OutOfMemory();
slouken@1918
   213
        return NULL;
slouken@1918
   214
    }
slouken@1918
   215
slouken@1920
   216
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   217
    if (!data) {
slouken@1918
   218
        GL_DestroyRenderer(renderer);
slouken@1918
   219
        SDL_OutOfMemory();
slouken@1918
   220
        return NULL;
slouken@1918
   221
    }
slouken@1918
   222
slouken@1923
   223
    renderer->ActivateRenderer = GL_ActivateRenderer;
slouken@1970
   224
    renderer->DisplayModeChanged = GL_DisplayModeChanged;
slouken@1918
   225
    renderer->CreateTexture = GL_CreateTexture;
slouken@1918
   226
    renderer->SetTexturePalette = GL_SetTexturePalette;
slouken@1918
   227
    renderer->GetTexturePalette = GL_GetTexturePalette;
slouken@1918
   228
    renderer->UpdateTexture = GL_UpdateTexture;
slouken@1918
   229
    renderer->LockTexture = GL_LockTexture;
slouken@1918
   230
    renderer->UnlockTexture = GL_UnlockTexture;
slouken@1918
   231
    renderer->DirtyTexture = GL_DirtyTexture;
slouken@1918
   232
    renderer->RenderFill = GL_RenderFill;
slouken@1918
   233
    renderer->RenderCopy = GL_RenderCopy;
slouken@1918
   234
    renderer->RenderPresent = GL_RenderPresent;
slouken@1918
   235
    renderer->DestroyTexture = GL_DestroyTexture;
slouken@1918
   236
    renderer->DestroyRenderer = GL_DestroyRenderer;
slouken@1918
   237
    renderer->info = GL_RenderDriver.info;
slouken@1918
   238
    renderer->window = window->id;
slouken@1918
   239
    renderer->driverdata = data;
slouken@1918
   240
slouken@1918
   241
    renderer->info.flags =
slouken@1965
   242
        (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED);
slouken@1918
   243
slouken@1927
   244
    if (GL_LoadFunctions(data) < 0) {
slouken@1927
   245
        GL_DestroyRenderer(renderer);
slouken@1927
   246
        return NULL;
slouken@1927
   247
    }
slouken@1927
   248
slouken@1918
   249
    data->context = SDL_GL_CreateContext(window->id);
slouken@1918
   250
    if (!data->context) {
slouken@1918
   251
        GL_DestroyRenderer(renderer);
slouken@1918
   252
        return NULL;
slouken@1918
   253
    }
slouken@1918
   254
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
slouken@1918
   255
        GL_DestroyRenderer(renderer);
slouken@1918
   256
        return NULL;
slouken@1918
   257
    }
slouken@1918
   258
slouken@1965
   259
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1918
   260
        SDL_GL_SetSwapInterval(1);
slouken@1918
   261
    } else {
slouken@1918
   262
        SDL_GL_SetSwapInterval(0);
slouken@1918
   263
    }
slouken@1918
   264
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@1965
   265
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1918
   266
    }
slouken@1918
   267
slouken@1952
   268
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   269
    renderer->info.max_texture_width = value;
slouken@1952
   270
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   271
    renderer->info.max_texture_height = value;
slouken@1920
   272
slouken@1926
   273
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
slouken@1926
   274
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
slouken@1926
   275
        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
slouken@1926
   276
    }
slouken@1920
   277
slouken@1918
   278
    /* Set up parameters for rendering */
slouken@1927
   279
    data->blendMode = -1;
slouken@1927
   280
    data->scaleMode = -1;
slouken@1927
   281
    data->glDisable(GL_DEPTH_TEST);
slouken@1927
   282
    data->glDisable(GL_CULL_FACE);
slouken@1926
   283
    if (data->GL_ARB_texture_rectangle_supported) {
slouken@1927
   284
        data->glEnable(GL_TEXTURE_RECTANGLE_ARB);
slouken@1926
   285
    } else {
slouken@1927
   286
        data->glEnable(GL_TEXTURE_2D);
slouken@1926
   287
    }
slouken@1970
   288
    data->updateSize = SDL_TRUE;
slouken@1918
   289
slouken@1918
   290
    return renderer;
slouken@1918
   291
}
slouken@1918
   292
slouken@1923
   293
static int
slouken@1923
   294
GL_ActivateRenderer(SDL_Renderer * renderer)
slouken@1923
   295
{
slouken@1923
   296
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1923
   297
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1923
   298
slouken@1970
   299
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
slouken@1970
   300
        return -1;
slouken@1970
   301
    }
slouken@1970
   302
    if (data->updateSize) {
slouken@1970
   303
        data->glMatrixMode(GL_PROJECTION);
slouken@1970
   304
        data->glLoadIdentity();
slouken@1970
   305
        data->glMatrixMode(GL_MODELVIEW);
slouken@1970
   306
        data->glLoadIdentity();
slouken@1970
   307
        data->glViewport(0, 0, window->w, window->h);
slouken@1970
   308
        data->glOrtho(0.0, (GLdouble) window->w, (GLdouble) window->h, 0.0,
slouken@1970
   309
                      0.0, 1.0);
slouken@1970
   310
        data->updateSize = SDL_FALSE;
slouken@1970
   311
    }
slouken@1970
   312
    return 0;
slouken@1970
   313
}
slouken@1970
   314
slouken@1970
   315
static int
slouken@1970
   316
GL_DisplayModeChanged(SDL_Renderer * renderer)
slouken@1970
   317
{
slouken@1970
   318
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1970
   319
slouken@1970
   320
    data->updateSize = SDL_TRUE;
slouken@1970
   321
    return 0;
slouken@1923
   322
}
slouken@1923
   323
slouken@1922
   324
static __inline__ int
slouken@1922
   325
power_of_2(int input)
slouken@1922
   326
{
slouken@1922
   327
    int value = 1;
slouken@1922
   328
slouken@1922
   329
    while (value < input) {
slouken@1922
   330
        value <<= 1;
slouken@1922
   331
    }
slouken@1922
   332
    return value;
slouken@1922
   333
}
slouken@1922
   334
slouken@1918
   335
static int
slouken@1918
   336
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   337
{
slouken@1918
   338
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   339
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1918
   340
    GL_TextureData *data;
slouken@1920
   341
    GLint internalFormat;
slouken@1920
   342
    GLenum format, type;
slouken@1922
   343
    int texture_w, texture_h;
slouken@1924
   344
    GLenum result;
slouken@1918
   345
slouken@1920
   346
    switch (texture->format) {
slouken@1965
   347
    case SDL_PIXELFORMAT_INDEX1LSB:
slouken@1965
   348
    case SDL_PIXELFORMAT_INDEX1MSB:
slouken@1920
   349
        internalFormat = GL_RGB;
slouken@1920
   350
        format = GL_COLOR_INDEX;
slouken@1920
   351
        type = GL_BITMAP;
slouken@1920
   352
        break;
slouken@1965
   353
    case SDL_PIXELFORMAT_INDEX8:
slouken@1920
   354
        internalFormat = GL_RGB;
slouken@1920
   355
        format = GL_COLOR_INDEX;
slouken@1920
   356
        type = GL_UNSIGNED_BYTE;
slouken@1920
   357
        break;
slouken@1965
   358
    case SDL_PIXELFORMAT_RGB332:
slouken@1920
   359
        internalFormat = GL_R3_G3_B2;
slouken@1920
   360
        format = GL_RGB;
slouken@1920
   361
        type = GL_UNSIGNED_BYTE_3_3_2;
slouken@1920
   362
        break;
slouken@1965
   363
    case SDL_PIXELFORMAT_RGB444:
slouken@1920
   364
        internalFormat = GL_RGB4;
slouken@1920
   365
        format = GL_RGB;
slouken@1920
   366
        type = GL_UNSIGNED_SHORT_4_4_4_4;
slouken@1920
   367
        break;
slouken@1965
   368
    case SDL_PIXELFORMAT_RGB555:
slouken@1920
   369
        internalFormat = GL_RGB5;
slouken@1920
   370
        format = GL_RGB;
slouken@1920
   371
        type = GL_UNSIGNED_SHORT_5_5_5_1;
slouken@1920
   372
        break;
slouken@1965
   373
    case SDL_PIXELFORMAT_ARGB4444:
slouken@1920
   374
        internalFormat = GL_RGBA4;
slouken@1920
   375
        format = GL_BGRA;
slouken@1920
   376
        type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
slouken@1920
   377
        break;
slouken@1965
   378
    case SDL_PIXELFORMAT_ARGB1555:
slouken@1920
   379
        internalFormat = GL_RGB5_A1;
slouken@1920
   380
        format = GL_BGRA;
slouken@1920
   381
        type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
slouken@1920
   382
        break;
slouken@1965
   383
    case SDL_PIXELFORMAT_RGB565:
slouken@1920
   384
        internalFormat = GL_RGB8;
slouken@1920
   385
        format = GL_RGB;
slouken@1920
   386
        type = GL_UNSIGNED_SHORT_5_6_5;
slouken@1920
   387
        break;
slouken@1965
   388
    case SDL_PIXELFORMAT_RGB24:
slouken@1920
   389
        internalFormat = GL_RGB8;
slouken@1920
   390
        format = GL_RGB;
slouken@1920
   391
        type = GL_UNSIGNED_BYTE;
slouken@1920
   392
        break;
slouken@1965
   393
    case SDL_PIXELFORMAT_RGB888:
slouken@1920
   394
        internalFormat = GL_RGB8;
slouken@1924
   395
        format = GL_BGRA;
slouken@1924
   396
        type = GL_UNSIGNED_BYTE;
slouken@1920
   397
        break;
slouken@1965
   398
    case SDL_PIXELFORMAT_BGR24:
slouken@1920
   399
        internalFormat = GL_RGB8;
slouken@1920
   400
        format = GL_BGR;
slouken@1920
   401
        type = GL_UNSIGNED_BYTE;
slouken@1920
   402
        break;
slouken@1965
   403
    case SDL_PIXELFORMAT_BGR888:
slouken@1920
   404
        internalFormat = GL_RGB8;
slouken@1924
   405
        format = GL_RGBA;
slouken@1924
   406
        type = GL_UNSIGNED_BYTE;
slouken@1920
   407
        break;
slouken@1965
   408
    case SDL_PIXELFORMAT_ARGB8888:
slouken@1920
   409
        internalFormat = GL_RGBA8;
slouken@1920
   410
        format = GL_BGRA;
slouken@1924
   411
        type = GL_UNSIGNED_BYTE;
slouken@1920
   412
        break;
slouken@1965
   413
    case SDL_PIXELFORMAT_ABGR8888:
slouken@1920
   414
        internalFormat = GL_RGBA8;
slouken@1920
   415
        format = GL_RGBA;
slouken@1924
   416
        type = GL_UNSIGNED_BYTE;
slouken@1920
   417
        break;
slouken@1965
   418
    case SDL_PIXELFORMAT_ARGB2101010:
slouken@1920
   419
        internalFormat = GL_RGB10_A2;
slouken@1920
   420
        format = GL_BGRA;
slouken@1920
   421
        type = GL_UNSIGNED_INT_2_10_10_10_REV;
slouken@1920
   422
        break;
slouken@1920
   423
    default:
slouken@1920
   424
        SDL_SetError("Unsupported texture format");
slouken@1920
   425
        return -1;
slouken@1920
   426
    }
slouken@1920
   427
slouken@1920
   428
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   429
    if (!data) {
slouken@1918
   430
        SDL_OutOfMemory();
slouken@1918
   431
        return -1;
slouken@1918
   432
    }
slouken@1918
   433
slouken@1918
   434
    texture->driverdata = data;
slouken@1918
   435
slouken@1927
   436
    renderdata->glGetError();
slouken@1927
   437
    renderdata->glGenTextures(1, &data->texture);
slouken@1926
   438
    if (renderdata->GL_ARB_texture_rectangle_supported) {
slouken@1926
   439
        data->type = GL_TEXTURE_RECTANGLE_ARB;
slouken@1926
   440
        texture_w = texture->w;
slouken@1926
   441
        texture_h = texture->h;
slouken@1926
   442
        data->texw = (GLfloat) texture->w;
slouken@1926
   443
        data->texh = (GLfloat) texture->h;
slouken@1926
   444
    } else {
slouken@1926
   445
        data->type = GL_TEXTURE_2D;
slouken@1926
   446
        texture_w = power_of_2(texture->w);
slouken@1926
   447
        texture_h = power_of_2(texture->h);
slouken@1926
   448
        data->texw = (GLfloat) texture->w / texture_w;
slouken@1926
   449
        data->texh = (GLfloat) texture->h / texture_h;
slouken@1926
   450
    }
slouken@1920
   451
    data->format = format;
slouken@1920
   452
    data->formattype = type;
slouken@1927
   453
    renderdata->glBindTexture(data->type, data->texture);
slouken@1927
   454
    renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@1927
   455
                             texture_h, 0, format, type, NULL);
slouken@1927
   456
    result = renderdata->glGetError();
slouken@1924
   457
    if (result != GL_NO_ERROR) {
slouken@1924
   458
        GL_SetError("glTexImage2D()", result);
slouken@1924
   459
        return -1;
slouken@1924
   460
    }
slouken@1918
   461
    return 0;
slouken@1918
   462
}
slouken@1918
   463
slouken@1918
   464
static int
slouken@1918
   465
GL_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   466
                     const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   467
{
slouken@1918
   468
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   469
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   470
slouken@1918
   471
    return 0;
slouken@1918
   472
}
slouken@1918
   473
slouken@1918
   474
static int
slouken@1918
   475
GL_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   476
                     SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   477
{
slouken@1918
   478
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   479
slouken@1918
   480
    return 0;
slouken@1918
   481
}
slouken@1918
   482
slouken@1924
   483
static void
slouken@1927
   484
SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture,
slouken@1927
   485
                   int pitch)
slouken@1924
   486
{
slouken@1965
   487
    if (texture->format == SDL_PIXELFORMAT_INDEX1LSB) {
slouken@1927
   488
        renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 1);
slouken@1965
   489
    } else if (texture->format == SDL_PIXELFORMAT_INDEX1MSB) {
slouken@1927
   490
        renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 0);
slouken@1924
   491
    }
slouken@1927
   492
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@1927
   493
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@1927
   494
                              pitch / SDL_BYTESPERPIXEL(texture->format));
slouken@1924
   495
}
slouken@1924
   496
slouken@1918
   497
static int
slouken@1918
   498
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   499
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
   500
{
slouken@1927
   501
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   502
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1924
   503
    GLenum result;
slouken@1918
   504
slouken@1927
   505
    renderdata->glGetError();
slouken@1927
   506
    SetupTextureUpdate(renderdata, texture, pitch);
slouken@1927
   507
    renderdata->glBindTexture(data->type, data->texture);
slouken@1927
   508
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
slouken@1927
   509
                                rect->h, data->format, data->formattype,
slouken@1927
   510
                                pixels);
slouken@1927
   511
    result = renderdata->glGetError();
slouken@1924
   512
    if (result != GL_NO_ERROR) {
slouken@1924
   513
        GL_SetError("glTexSubImage2D()", result);
slouken@1924
   514
        return -1;
slouken@1924
   515
    }
slouken@1918
   516
    return 0;
slouken@1918
   517
}
slouken@1918
   518
slouken@1918
   519
static int
slouken@1918
   520
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   521
               const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1918
   522
               int *pitch)
slouken@1918
   523
{
slouken@1918
   524
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   525
slouken@1920
   526
    if (!data->pixels) {
slouken@1920
   527
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
slouken@1920
   528
        data->pixels = SDL_malloc(texture->h * data->pitch);
slouken@1920
   529
        if (!data->pixels) {
slouken@1920
   530
            SDL_OutOfMemory();
slouken@1920
   531
            return -1;
slouken@1920
   532
        }
slouken@1918
   533
    }
slouken@1918
   534
slouken@1920
   535
    if (markDirty) {
slouken@1920
   536
        SDL_AddDirtyRect(&data->dirty, rect);
slouken@1920
   537
    }
slouken@1918
   538
slouken@1920
   539
    *pixels =
slouken@1920
   540
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@1920
   541
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@1920
   542
    *pitch = data->pitch;
slouken@1918
   543
    return 0;
slouken@1918
   544
}
slouken@1918
   545
slouken@1918
   546
static void
slouken@1918
   547
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   548
{
slouken@1918
   549
}
slouken@1918
   550
slouken@1918
   551
static void
slouken@1918
   552
GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
slouken@1918
   553
                const SDL_Rect * rects)
slouken@1918
   554
{
slouken@1918
   555
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   556
    int i;
slouken@1918
   557
slouken@1918
   558
    for (i = 0; i < numrects; ++i) {
slouken@1920
   559
        SDL_AddDirtyRect(&data->dirty, &rects[i]);
slouken@1918
   560
    }
slouken@1918
   561
}
slouken@1918
   562
slouken@1918
   563
static int
slouken@1918
   564
GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 color)
slouken@1918
   565
{
slouken@1918
   566
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   567
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1918
   568
    GLclampf r, g, b, a;
slouken@1918
   569
slouken@1918
   570
    a = ((GLclampf) ((color >> 24) & 0xFF)) / 255.0f;
slouken@1918
   571
    r = ((GLclampf) ((color >> 16) & 0xFF)) / 255.0f;
slouken@1918
   572
    g = ((GLclampf) ((color >> 8) & 0xFF)) / 255.0f;
slouken@1918
   573
    b = ((GLclampf) (color & 0xFF)) / 255.0f;
slouken@1918
   574
slouken@1927
   575
    data->glClearColor(r, g, b, a);
slouken@1927
   576
    data->glViewport(rect->x, window->h - rect->y, rect->w, rect->h);
slouken@1927
   577
    data->glClear(GL_COLOR_BUFFER_BIT);
slouken@1927
   578
    data->glViewport(0, 0, window->w, window->h);
slouken@1918
   579
    return 0;
slouken@1918
   580
}
slouken@1918
   581
slouken@1918
   582
static int
slouken@1918
   583
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   584
              const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1918
   585
              int blendMode, int scaleMode)
slouken@1918
   586
{
slouken@1918
   587
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   588
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@1918
   589
    int minx, miny, maxx, maxy;
slouken@1918
   590
    GLfloat minu, maxu, minv, maxv;
slouken@1918
   591
slouken@1920
   592
    if (texturedata->dirty.count > 0) {
slouken@1920
   593
        SDL_DirtyRect *dirty;
slouken@1920
   594
        void *pixels;
slouken@1920
   595
        int bpp = SDL_BYTESPERPIXEL(texture->format);
slouken@1920
   596
        int pitch = texturedata->pitch;
slouken@1920
   597
slouken@1927
   598
        SetupTextureUpdate(data, texture, pitch);
slouken@1927
   599
        data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1920
   600
        for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
slouken@1920
   601
            SDL_Rect *rect = &dirty->rect;
slouken@1920
   602
            pixels =
slouken@1920
   603
                (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
slouken@1920
   604
                          rect->x * bpp);
slouken@1927
   605
            data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
slouken@1927
   606
                                  rect->w, rect->h, texturedata->format,
slouken@1927
   607
                                  texturedata->formattype, pixels);
slouken@1920
   608
        }
slouken@1920
   609
        SDL_ClearDirtyRects(&texturedata->dirty);
slouken@1920
   610
    }
slouken@1920
   611
slouken@1918
   612
    minx = dstrect->x;
slouken@1918
   613
    miny = dstrect->y;
slouken@1918
   614
    maxx = dstrect->x + dstrect->w;
slouken@1918
   615
    maxy = dstrect->y + dstrect->h;
slouken@1918
   616
slouken@1918
   617
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
   618
    minu *= texturedata->texw;
slouken@1918
   619
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
   620
    maxu *= texturedata->texw;
slouken@1918
   621
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
   622
    minv *= texturedata->texh;
slouken@1918
   623
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
   624
    maxv *= texturedata->texh;
slouken@1918
   625
slouken@1927
   626
    data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1918
   627
slouken@1927
   628
    if (blendMode != data->blendMode) {
slouken@1927
   629
        switch (blendMode) {
slouken@1965
   630
        case SDL_TEXTUREBLENDMODE_NONE:
slouken@1927
   631
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
slouken@1927
   632
            data->glDisable(GL_BLEND);
slouken@1927
   633
            break;
slouken@1965
   634
        case SDL_TEXTUREBLENDMODE_MASK:
slouken@1965
   635
        case SDL_TEXTUREBLENDMODE_BLEND:
slouken@1927
   636
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1927
   637
            data->glEnable(GL_BLEND);
slouken@1927
   638
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@1927
   639
            break;
slouken@1965
   640
        case SDL_TEXTUREBLENDMODE_ADD:
slouken@1927
   641
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1927
   642
            data->glEnable(GL_BLEND);
slouken@1927
   643
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@1927
   644
            break;
slouken@1965
   645
        case SDL_TEXTUREBLENDMODE_MOD:
slouken@1927
   646
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1927
   647
            data->glEnable(GL_BLEND);
slouken@1927
   648
            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@1927
   649
            break;
slouken@1927
   650
        }
slouken@1927
   651
        data->blendMode = blendMode;
slouken@1918
   652
    }
slouken@1918
   653
slouken@1927
   654
    if (scaleMode != data->scaleMode) {
slouken@1927
   655
        switch (scaleMode) {
slouken@1965
   656
        case SDL_TEXTURESCALEMODE_NONE:
slouken@1965
   657
        case SDL_TEXTURESCALEMODE_FAST:
slouken@1927
   658
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
slouken@1927
   659
                                  GL_NEAREST);
slouken@1927
   660
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
slouken@1927
   661
                                  GL_NEAREST);
slouken@1927
   662
            break;
slouken@1965
   663
        case SDL_TEXTURESCALEMODE_SLOW:
slouken@1965
   664
        case SDL_TEXTURESCALEMODE_BEST:
slouken@1927
   665
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
slouken@1927
   666
                                  GL_LINEAR);
slouken@1927
   667
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
slouken@1927
   668
                                  GL_LINEAR);
slouken@1927
   669
            break;
slouken@1927
   670
        }
slouken@1927
   671
        data->scaleMode = scaleMode;
slouken@1918
   672
    }
slouken@1918
   673
slouken@1927
   674
    data->glBegin(GL_TRIANGLE_STRIP);
slouken@1927
   675
    data->glTexCoord2f(minu, minv);
slouken@1927
   676
    data->glVertex2i(minx, miny);
slouken@1927
   677
    data->glTexCoord2f(maxu, minv);
slouken@1927
   678
    data->glVertex2i(maxx, miny);
slouken@1927
   679
    data->glTexCoord2f(minu, maxv);
slouken@1927
   680
    data->glVertex2i(minx, maxy);
slouken@1927
   681
    data->glTexCoord2f(maxu, maxv);
slouken@1927
   682
    data->glVertex2i(maxx, maxy);
slouken@1927
   683
    data->glEnd();
slouken@1918
   684
slouken@1918
   685
    return 0;
slouken@1918
   686
}
slouken@1918
   687
slouken@1918
   688
static void
slouken@1918
   689
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
   690
{
slouken@1918
   691
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
   692
}
slouken@1918
   693
slouken@1918
   694
static void
slouken@1918
   695
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   696
{
slouken@1927
   697
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   698
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   699
slouken@1918
   700
    if (!data) {
slouken@1918
   701
        return;
slouken@1918
   702
    }
slouken@1918
   703
    if (data->texture) {
slouken@1927
   704
        renderdata->glDeleteTextures(1, &data->texture);
slouken@1918
   705
    }
slouken@1920
   706
    if (data->pixels) {
slouken@1920
   707
        SDL_free(data->pixels);
slouken@1920
   708
    }
slouken@1920
   709
    SDL_FreeDirtyRects(&data->dirty);
slouken@1918
   710
    SDL_free(data);
slouken@1918
   711
    texture->driverdata = NULL;
slouken@1918
   712
}
slouken@1918
   713
slouken@1918
   714
void
slouken@1918
   715
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
   716
{
slouken@1918
   717
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   718
slouken@1918
   719
    if (data) {
slouken@1920
   720
        if (data->context) {
slouken@1920
   721
            SDL_GL_MakeCurrent(0, NULL);
slouken@1920
   722
            SDL_GL_DeleteContext(data->context);
slouken@1918
   723
        }
slouken@1918
   724
        SDL_free(data);
slouken@1918
   725
    }
slouken@1918
   726
    SDL_free(renderer);
slouken@1918
   727
}
slouken@1918
   728
slouken@1952
   729
#endif /* SDL_VIDEO_RENDER_OGL */
slouken@1918
   730
slouken@1918
   731
/* vi: set ts=4 sw=4 expandtab: */