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