src/render/opengl/SDL_render_gl.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Feb 2011 22:26:59 -0800
changeset 5381 79e60256dbf7
parent 5355 cc2215cda170
child 5397 88ff1f463134
permissions -rwxr-xr-x
Fixed compiler warnings
slouken@1918
     1
/*
slouken@1918
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 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@5226
    24
#if SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED
slouken@1920
    25
slouken@5233
    26
#include "SDL_hints.h"
slouken@5233
    27
#include "SDL_log.h"
slouken@1920
    28
#include "SDL_opengl.h"
slouken@5154
    29
#include "../SDL_sysrender.h"
slouken@5228
    30
#include "SDL_shaders_gl.h"
slouken@1918
    31
slouken@2246
    32
#ifdef __MACOSX__
slouken@2246
    33
#include <OpenGL/OpenGL.h>
slouken@2246
    34
#endif
slouken@2246
    35
slouken@2778
    36
slouken@1918
    37
/* OpenGL renderer implementation */
slouken@1918
    38
slouken@2230
    39
/* Details on optimizing the texture path on Mac OS X:
slouken@5203
    40
   http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/opengl_texturedata.html
slouken@2230
    41
*/
slouken@2230
    42
slouken@5154
    43
/* Used to re-create the window with OpenGL capability */
slouken@5154
    44
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
icculus@2835
    45
slouken@1985
    46
static const float inv255f = 1.0f / 255.0f;
slouken@1985
    47
slouken@1918
    48
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@5147
    49
static void GL_WindowEvent(SDL_Renderer * renderer,
slouken@5147
    50
                           const SDL_WindowEvent *event);
slouken@1918
    51
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    52
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    53
                            const SDL_Rect * rect, const void *pixels,
slouken@1918
    54
                            int pitch);
slouken@1918
    55
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
    56
                          const SDL_Rect * rect, void **pixels, int *pitch);
slouken@1918
    57
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@5297
    58
static int GL_UpdateViewport(SDL_Renderer * renderer);
slouken@3596
    59
static int GL_RenderClear(SDL_Renderer * renderer);
slouken@3596
    60
static int GL_RenderDrawPoints(SDL_Renderer * renderer,
slouken@3596
    61
                               const SDL_Point * points, int count);
slouken@3596
    62
static int GL_RenderDrawLines(SDL_Renderer * renderer,
slouken@3596
    63
                              const SDL_Point * points, int count);
slouken@3596
    64
static int GL_RenderFillRects(SDL_Renderer * renderer,
slouken@5297
    65
                              const SDL_Rect * rects, int count);
slouken@1918
    66
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    67
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@3431
    68
static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
    69
                               Uint32 pixel_format, void * pixels, int pitch);
slouken@1918
    70
static void GL_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    71
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    72
static void GL_DestroyRenderer(SDL_Renderer * renderer);
slouken@1918
    73
slouken@1918
    74
slouken@1918
    75
SDL_RenderDriver GL_RenderDriver = {
slouken@1918
    76
    GL_CreateRenderer,
slouken@1918
    77
    {
slouken@1918
    78
     "opengl",
slouken@5203
    79
     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
slouken@5156
    80
     1,
slouken@5156
    81
     {SDL_PIXELFORMAT_ARGB8888},
slouken@1918
    82
     0,
slouken@1918
    83
     0}
slouken@1918
    84
};
slouken@1918
    85
slouken@1918
    86
typedef struct
slouken@1918
    87
{
slouken@1918
    88
    SDL_GLContext context;
slouken@2233
    89
    SDL_bool GL_ARB_texture_rectangle_supported;
slouken@5355
    90
    struct {
slouken@5355
    91
        GL_Shader shader;
slouken@5355
    92
        Uint32 color;
slouken@5355
    93
        int blendMode;
slouken@5355
    94
        GLenum scaleMode;
slouken@5355
    95
    } current;
slouken@1927
    96
slouken@1927
    97
    /* OpenGL functions */
slouken@1927
    98
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
slouken@5204
    99
#include "SDL_glfuncs.h"
slouken@1927
   100
#undef SDL_PROC
slouken@1974
   101
slouken@2233
   102
    void (*glTextureRangeAPPLE) (GLenum target, GLsizei length,
slouken@2233
   103
                                 const GLvoid * pointer);
slouken@5228
   104
slouken@5228
   105
    /* Multitexture support */
slouken@5228
   106
    SDL_bool GL_ARB_multitexture_supported;
slouken@5228
   107
    PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;
slouken@5351
   108
    GLint num_texture_units;
slouken@5228
   109
slouken@5228
   110
    /* Shader support */
slouken@5228
   111
    GL_ShaderContext *shaders;
slouken@5228
   112
slouken@1918
   113
} GL_RenderData;
slouken@1918
   114
slouken@1918
   115
typedef struct
slouken@1918
   116
{
slouken@1918
   117
    GLuint texture;
slouken@1920
   118
    GLenum type;
slouken@1918
   119
    GLfloat texw;
slouken@1918
   120
    GLfloat texh;
slouken@1920
   121
    GLenum format;
slouken@1920
   122
    GLenum formattype;
slouken@1918
   123
    void *pixels;
slouken@1918
   124
    int pitch;
slouken@5355
   125
    int scaleMode;
slouken@5227
   126
    SDL_Rect locked_rect;
slouken@5264
   127
slouken@5264
   128
    /* YV12 texture support */
slouken@5264
   129
    SDL_bool yuv;
slouken@5264
   130
    GLuint utexture;
slouken@5264
   131
    GLuint vtexture;
slouken@1918
   132
} GL_TextureData;
slouken@1918
   133
slouken@1918
   134
slouken@1924
   135
static void
slouken@1924
   136
GL_SetError(const char *prefix, GLenum result)
slouken@1924
   137
{
slouken@1924
   138
    const char *error;
slouken@1924
   139
slouken@1924
   140
    switch (result) {
slouken@1924
   141
    case GL_NO_ERROR:
slouken@1924
   142
        error = "GL_NO_ERROR";
slouken@1924
   143
        break;
slouken@1924
   144
    case GL_INVALID_ENUM:
slouken@1924
   145
        error = "GL_INVALID_ENUM";
slouken@1924
   146
        break;
slouken@1924
   147
    case GL_INVALID_VALUE:
slouken@1924
   148
        error = "GL_INVALID_VALUE";
slouken@1924
   149
        break;
slouken@1924
   150
    case GL_INVALID_OPERATION:
slouken@1924
   151
        error = "GL_INVALID_OPERATION";
slouken@1924
   152
        break;
slouken@1924
   153
    case GL_STACK_OVERFLOW:
slouken@1924
   154
        error = "GL_STACK_OVERFLOW";
slouken@1924
   155
        break;
slouken@1924
   156
    case GL_STACK_UNDERFLOW:
slouken@1924
   157
        error = "GL_STACK_UNDERFLOW";
slouken@1924
   158
        break;
slouken@1924
   159
    case GL_OUT_OF_MEMORY:
slouken@1924
   160
        error = "GL_OUT_OF_MEMORY";
slouken@1924
   161
        break;
slouken@1924
   162
    case GL_TABLE_TOO_LARGE:
slouken@1924
   163
        error = "GL_TABLE_TOO_LARGE";
slouken@1924
   164
        break;
slouken@1924
   165
    default:
slouken@1924
   166
        error = "UNKNOWN";
slouken@1924
   167
        break;
slouken@1924
   168
    }
slouken@1924
   169
    SDL_SetError("%s: %s", prefix, error);
slouken@1924
   170
}
slouken@1924
   171
slouken@1927
   172
static int
slouken@1927
   173
GL_LoadFunctions(GL_RenderData * data)
slouken@1927
   174
{
slouken@1927
   175
#ifdef __SDL_NOGETPROCADDR__
slouken@1927
   176
#define SDL_PROC(ret,func,params) data->func=func;
slouken@1927
   177
#else
slouken@1927
   178
#define SDL_PROC(ret,func,params) \
slouken@1927
   179
    do { \
slouken@1927
   180
        data->func = SDL_GL_GetProcAddress(#func); \
slouken@1927
   181
        if ( ! data->func ) { \
slouken@1927
   182
            SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
slouken@1927
   183
            return -1; \
slouken@1927
   184
        } \
slouken@1927
   185
    } while ( 0 );
slouken@1927
   186
#endif /* __SDL_NOGETPROCADDR__ */
slouken@1927
   187
slouken@5204
   188
#include "SDL_glfuncs.h"
slouken@1927
   189
#undef SDL_PROC
slouken@1927
   190
    return 0;
slouken@1927
   191
}
slouken@1927
   192
slouken@5297
   193
static SDL_GLContext SDL_CurrentContext = NULL;
slouken@5297
   194
slouken@5297
   195
static int
slouken@5297
   196
GL_ActivateRenderer(SDL_Renderer * renderer)
slouken@5297
   197
{
slouken@5297
   198
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5297
   199
slouken@5297
   200
    if (SDL_CurrentContext != data->context) {
slouken@5297
   201
        if (SDL_GL_MakeCurrent(renderer->window, data->context) < 0) {
slouken@5297
   202
            return -1;
slouken@5297
   203
        }
slouken@5297
   204
        SDL_CurrentContext = data->context;
slouken@5297
   205
slouken@5297
   206
        GL_UpdateViewport(renderer);
slouken@5297
   207
    }
slouken@5297
   208
    return 0;
slouken@5297
   209
}
slouken@5297
   210
slouken@5355
   211
/* This is called if we need to invalidate all of the SDL OpenGL state */
slouken@5355
   212
static void
slouken@5355
   213
GL_ResetState(SDL_Renderer *renderer)
slouken@5355
   214
{
slouken@5355
   215
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5355
   216
slouken@5355
   217
    if (SDL_CurrentContext == data->context) {
slouken@5355
   218
        GL_UpdateViewport(renderer);
slouken@5355
   219
    } else {
slouken@5355
   220
        GL_ActivateRenderer(renderer);
slouken@5355
   221
    }
slouken@5355
   222
slouken@5355
   223
    data->current.shader = SHADER_NONE;
slouken@5355
   224
    data->current.color = 0;
slouken@5355
   225
    data->current.blendMode = -1;
slouken@5355
   226
    data->current.scaleMode = 0;
slouken@5355
   227
slouken@5355
   228
    data->glDisable(GL_DEPTH_TEST);
slouken@5355
   229
    data->glDisable(GL_CULL_FACE);
slouken@5355
   230
    /* This ended up causing video discrepancies between OpenGL and Direct3D */
slouken@5355
   231
    /*data->glEnable(GL_LINE_SMOOTH);*/
slouken@5355
   232
slouken@5355
   233
    data->glMatrixMode(GL_MODELVIEW);
slouken@5355
   234
    data->glLoadIdentity();
slouken@5355
   235
}
slouken@5355
   236
slouken@1918
   237
SDL_Renderer *
slouken@1918
   238
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1918
   239
{
slouken@1918
   240
    SDL_Renderer *renderer;
slouken@1918
   241
    GL_RenderData *data;
slouken@5233
   242
    const char *hint;
slouken@1952
   243
    GLint value;
slouken@5154
   244
    Uint32 window_flags;
slouken@1918
   245
slouken@5154
   246
    window_flags = SDL_GetWindowFlags(window);
slouken@5154
   247
    if (!(window_flags & SDL_WINDOW_OPENGL)) {
slouken@5154
   248
        if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
slouken@1924
   249
            return NULL;
slouken@1924
   250
        }
slouken@1918
   251
    }
slouken@1918
   252
slouken@1920
   253
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1918
   254
    if (!renderer) {
slouken@1918
   255
        SDL_OutOfMemory();
slouken@1918
   256
        return NULL;
slouken@1918
   257
    }
slouken@1918
   258
slouken@1920
   259
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   260
    if (!data) {
slouken@1918
   261
        GL_DestroyRenderer(renderer);
slouken@1918
   262
        SDL_OutOfMemory();
slouken@1918
   263
        return NULL;
slouken@1918
   264
    }
slouken@1918
   265
slouken@5147
   266
    renderer->WindowEvent = GL_WindowEvent;
slouken@1918
   267
    renderer->CreateTexture = GL_CreateTexture;
slouken@1918
   268
    renderer->UpdateTexture = GL_UpdateTexture;
slouken@1918
   269
    renderer->LockTexture = GL_LockTexture;
slouken@1918
   270
    renderer->UnlockTexture = GL_UnlockTexture;
slouken@5297
   271
    renderer->UpdateViewport = GL_UpdateViewport;
slouken@3596
   272
    renderer->RenderClear = GL_RenderClear;
slouken@3596
   273
    renderer->RenderDrawPoints = GL_RenderDrawPoints;
slouken@3596
   274
    renderer->RenderDrawLines = GL_RenderDrawLines;
slouken@3596
   275
    renderer->RenderFillRects = GL_RenderFillRects;
slouken@1918
   276
    renderer->RenderCopy = GL_RenderCopy;
slouken@3431
   277
    renderer->RenderReadPixels = GL_RenderReadPixels;
slouken@1918
   278
    renderer->RenderPresent = GL_RenderPresent;
slouken@1918
   279
    renderer->DestroyTexture = GL_DestroyTexture;
slouken@1918
   280
    renderer->DestroyRenderer = GL_DestroyRenderer;
slouken@1918
   281
    renderer->info = GL_RenderDriver.info;
slouken@5297
   282
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@1918
   283
    renderer->driverdata = data;
slouken@1918
   284
slouken@3685
   285
    data->context = SDL_GL_CreateContext(window);
slouken@1918
   286
    if (!data->context) {
slouken@1918
   287
        GL_DestroyRenderer(renderer);
slouken@1918
   288
        return NULL;
slouken@1918
   289
    }
slouken@3685
   290
    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
slouken@1918
   291
        GL_DestroyRenderer(renderer);
slouken@1918
   292
        return NULL;
slouken@1918
   293
    }
slouken@5204
   294
slouken@5204
   295
    if (GL_LoadFunctions(data) < 0) {
slouken@5204
   296
        GL_DestroyRenderer(renderer);
slouken@5204
   297
        return NULL;
slouken@5204
   298
    }
slouken@5204
   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@1952
   316
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   317
    renderer->info.max_texture_width = value;
slouken@1952
   318
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   319
    renderer->info.max_texture_height = value;
slouken@1920
   320
slouken@1926
   321
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
slouken@1926
   322
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
slouken@1926
   323
        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
slouken@1926
   324
    }
slouken@2233
   325
    if (SDL_GL_ExtensionSupported("GL_APPLE_texture_range")) {
slouken@2233
   326
        data->glTextureRangeAPPLE =
slouken@2233
   327
            (void (*)(GLenum, GLsizei, const GLvoid *))
slouken@2233
   328
            SDL_GL_GetProcAddress("glTextureRangeAPPLE");
slouken@2233
   329
    }
slouken@1920
   330
slouken@5228
   331
    /* Check for multitexture support */
slouken@5228
   332
    if (SDL_GL_ExtensionSupported("GL_ARB_multitexture")) {
slouken@5228
   333
        data->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glActiveTextureARB");
slouken@5228
   334
        if (data->glActiveTextureARB) {
slouken@5228
   335
            data->GL_ARB_multitexture_supported = SDL_TRUE;
slouken@5228
   336
            data->glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &data->num_texture_units);
slouken@5228
   337
        }
slouken@5228
   338
    }
slouken@5228
   339
slouken@5228
   340
    /* Check for shader support */
slouken@5233
   341
    hint = SDL_GetHint(SDL_HINT_RENDER_OPENGL_SHADERS);
slouken@5233
   342
    if (!hint || *hint != '0') {
slouken@5233
   343
        data->shaders = GL_CreateShaderContext();
slouken@5233
   344
    }
slouken@5233
   345
    SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, "OpenGL shaders: %s",
slouken@5233
   346
                data->shaders ? "ENABLED" : "DISABLED");
slouken@5228
   347
slouken@5228
   348
    /* We support YV12 textures using 3 textures and a shader */
slouken@5228
   349
    if (data->shaders && data->num_texture_units >= 3) {
slouken@5228
   350
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_YV12;
slouken@5264
   351
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_IYUV;
slouken@5228
   352
    }
slouken@5228
   353
slouken@1918
   354
    /* Set up parameters for rendering */
slouken@5355
   355
    GL_ResetState(renderer);
slouken@1918
   356
slouken@1918
   357
    return renderer;
slouken@1918
   358
}
slouken@1918
   359
slouken@5147
   360
static void
slouken@5147
   361
GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1970
   362
{
slouken@1970
   363
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1970
   364
slouken@5276
   365
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@5147
   366
        /* Rebind the context to the window area and update matrices */
slouken@5147
   367
        SDL_CurrentContext = NULL;
slouken@5147
   368
    }
slouken@1923
   369
}
slouken@1923
   370
slouken@1922
   371
static __inline__ int
slouken@1922
   372
power_of_2(int input)
slouken@1922
   373
{
slouken@1922
   374
    int value = 1;
slouken@1922
   375
slouken@1922
   376
    while (value < input) {
slouken@1922
   377
        value <<= 1;
slouken@1922
   378
    }
slouken@1922
   379
    return value;
slouken@1922
   380
}
slouken@1922
   381
slouken@3433
   382
static __inline__ SDL_bool
slouken@3433
   383
convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
slouken@3433
   384
               GLint* internalFormat, GLenum* format, GLenum* type)
slouken@3433
   385
{
slouken@3433
   386
    switch (pixel_format) {
slouken@3433
   387
    case SDL_PIXELFORMAT_ARGB8888:
slouken@5156
   388
        *internalFormat = GL_RGBA8;
slouken@3433
   389
        *format = GL_BGRA;
slouken@3433
   390
        *type = GL_UNSIGNED_INT_8_8_8_8_REV;
slouken@3433
   391
        break;
slouken@5264
   392
    case SDL_PIXELFORMAT_YV12:
slouken@5264
   393
    case SDL_PIXELFORMAT_IYUV:
slouken@5264
   394
        *internalFormat = GL_LUMINANCE;
slouken@5264
   395
        *format = GL_LUMINANCE;
slouken@5264
   396
        *type = GL_UNSIGNED_BYTE;
slouken@5264
   397
        break;
slouken@3433
   398
    default:
slouken@3433
   399
        return SDL_FALSE;
slouken@3433
   400
    }
slouken@3433
   401
    return SDL_TRUE;
slouken@3433
   402
}
icculus@2835
   403
slouken@1918
   404
static int
slouken@1918
   405
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   406
{
slouken@1918
   407
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   408
    GL_TextureData *data;
slouken@1920
   409
    GLint internalFormat;
slouken@1920
   410
    GLenum format, type;
slouken@1922
   411
    int texture_w, texture_h;
slouken@1924
   412
    GLenum result;
slouken@1918
   413
slouken@5147
   414
    GL_ActivateRenderer(renderer);
slouken@5147
   415
slouken@3433
   416
    if (!convert_format(renderdata, texture->format, &internalFormat,
slouken@3433
   417
                        &format, &type)) {
slouken@4990
   418
        SDL_SetError("Texture format %s not supported by OpenGL",
slouken@4990
   419
                     SDL_GetPixelFormatName(texture->format));
slouken@1920
   420
        return -1;
slouken@1920
   421
    }
slouken@1920
   422
slouken@1920
   423
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   424
    if (!data) {
slouken@1918
   425
        SDL_OutOfMemory();
slouken@1918
   426
        return -1;
slouken@1918
   427
    }
slouken@1918
   428
slouken@2222
   429
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5264
   430
        size_t size;
slouken@5156
   431
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
slouken@5264
   432
        size = texture->h * data->pitch;
slouken@5264
   433
        if (texture->format == SDL_PIXELFORMAT_YV12 ||
slouken@5264
   434
            texture->format == SDL_PIXELFORMAT_IYUV) {
slouken@5264
   435
            /* Need to add size for the U and V planes */
slouken@5264
   436
            size += (2 * (texture->h * data->pitch) / 4);
slouken@5264
   437
        }
slouken@5264
   438
        data->pixels = SDL_malloc(size);
slouken@2222
   439
        if (!data->pixels) {
slouken@2222
   440
            SDL_OutOfMemory();
slouken@2222
   441
            SDL_free(data);
slouken@2222
   442
            return -1;
slouken@2222
   443
        }
slouken@2222
   444
    }
slouken@2222
   445
slouken@1918
   446
    texture->driverdata = data;
slouken@1918
   447
slouken@1927
   448
    renderdata->glGetError();
slouken@1927
   449
    renderdata->glGenTextures(1, &data->texture);
slouken@1926
   450
    if (renderdata->GL_ARB_texture_rectangle_supported) {
slouken@1926
   451
        data->type = GL_TEXTURE_RECTANGLE_ARB;
slouken@1926
   452
        texture_w = texture->w;
slouken@1926
   453
        texture_h = texture->h;
icculus@2835
   454
        data->texw = (GLfloat) texture_w;
icculus@2835
   455
        data->texh = (GLfloat) texture_h;
slouken@1926
   456
    } else {
slouken@1926
   457
        data->type = GL_TEXTURE_2D;
slouken@1926
   458
        texture_w = power_of_2(texture->w);
slouken@1926
   459
        texture_h = power_of_2(texture->h);
icculus@2835
   460
        data->texw = (GLfloat) (texture->w) / texture_w;
slouken@1926
   461
        data->texh = (GLfloat) texture->h / texture_h;
slouken@1926
   462
    }
icculus@2835
   463
slouken@1920
   464
    data->format = format;
slouken@1920
   465
    data->formattype = type;
slouken@5355
   466
    data->scaleMode = GL_LINEAR;
slouken@2884
   467
    renderdata->glEnable(data->type);
slouken@1927
   468
    renderdata->glBindTexture(data->type, data->texture);
slouken@2230
   469
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@2230
   470
                                GL_CLAMP_TO_EDGE);
slouken@2230
   471
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@2230
   472
                                GL_CLAMP_TO_EDGE);
slouken@2840
   473
#ifdef __MACOSX__
slouken@2230
   474
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
slouken@2230
   475
#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
slouken@2230
   476
#endif
slouken@2230
   477
#ifndef STORAGE_CACHED_APPLE
slouken@2230
   478
#define STORAGE_CACHED_APPLE                0x85BE
slouken@2230
   479
#endif
slouken@2230
   480
#ifndef STORAGE_SHARED_APPLE
slouken@2230
   481
#define STORAGE_SHARED_APPLE                0x85BF
slouken@2230
   482
#endif
slouken@2230
   483
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@2230
   484
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   485
                                    GL_STORAGE_SHARED_APPLE);
slouken@2230
   486
    } else {
slouken@2230
   487
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   488
                                    GL_STORAGE_CACHED_APPLE);
slouken@2230
   489
    }
bob@2295
   490
    if (texture->access == SDL_TEXTUREACCESS_STREAMING
bob@2295
   491
        && texture->format == SDL_PIXELFORMAT_ARGB8888) {
slouken@2230
   492
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
slouken@2230
   493
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   494
                                 texture_h, 0, format, type, data->pixels);
slouken@5156
   495
    }
slouken@5156
   496
    else
slouken@2809
   497
#endif
slouken@2230
   498
    {
slouken@2230
   499
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   500
                                 texture_h, 0, format, type, NULL);
slouken@2230
   501
    }
slouken@3041
   502
    renderdata->glDisable(data->type);
slouken@1927
   503
    result = renderdata->glGetError();
slouken@1924
   504
    if (result != GL_NO_ERROR) {
slouken@1924
   505
        GL_SetError("glTexImage2D()", result);
slouken@1924
   506
        return -1;
slouken@1924
   507
    }
slouken@5264
   508
slouken@5264
   509
    if (texture->format == SDL_PIXELFORMAT_YV12 ||
slouken@5264
   510
        texture->format == SDL_PIXELFORMAT_IYUV) {
slouken@5264
   511
        data->yuv = SDL_TRUE;
slouken@5264
   512
slouken@5264
   513
        renderdata->glGenTextures(1, &data->utexture);
slouken@5264
   514
        renderdata->glGenTextures(1, &data->vtexture);
slouken@5264
   515
        renderdata->glEnable(data->type);
slouken@5264
   516
slouken@5264
   517
        renderdata->glBindTexture(data->type, data->utexture);
slouken@5264
   518
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@5264
   519
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   520
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@5264
   521
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   522
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w/2,
slouken@5264
   523
                                 texture_h/2, 0, format, type, NULL);
slouken@5264
   524
slouken@5264
   525
        renderdata->glBindTexture(data->type, data->vtexture);
slouken@5264
   526
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@5264
   527
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   528
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@5264
   529
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   530
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w/2,
slouken@5264
   531
                                 texture_h/2, 0, format, type, NULL);
slouken@5264
   532
slouken@5264
   533
        renderdata->glDisable(data->type);
slouken@5264
   534
    }
slouken@1918
   535
    return 0;
slouken@1918
   536
}
slouken@1918
   537
slouken@1918
   538
static int
slouken@1918
   539
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   540
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
   541
{
slouken@1927
   542
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   543
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1924
   544
    GLenum result;
slouken@1918
   545
slouken@5147
   546
    GL_ActivateRenderer(renderer);
slouken@5147
   547
slouken@1927
   548
    renderdata->glGetError();
slouken@5227
   549
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@5227
   550
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@5227
   551
                              (pitch / SDL_BYTESPERPIXEL(texture->format)));
slouken@2884
   552
    renderdata->glEnable(data->type);
slouken@1927
   553
    renderdata->glBindTexture(data->type, data->texture);
slouken@1927
   554
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
slouken@1927
   555
                                rect->h, data->format, data->formattype,
slouken@1927
   556
                                pixels);
slouken@5264
   557
    if (data->yuv) {
slouken@5265
   558
        const void *top;
slouken@5265
   559
slouken@5265
   560
        renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, (pitch / 2));
slouken@5265
   561
slouken@5264
   562
        /* Skip to the top of the next texture */
slouken@5265
   563
        top = (const void*)((const Uint8*)pixels + (texture->h-rect->y) * pitch - rect->x);
slouken@5264
   564
slouken@5264
   565
        /* Skip to the correct offset into the next texture */
slouken@5264
   566
        pixels = (const void*)((const Uint8*)top + (rect->y / 2) * pitch + rect->x / 2);
slouken@5264
   567
        if (texture->format == SDL_PIXELFORMAT_YV12) {
slouken@5264
   568
            renderdata->glBindTexture(data->type, data->vtexture);
slouken@5264
   569
        } else {
slouken@5264
   570
            renderdata->glBindTexture(data->type, data->utexture);
slouken@5264
   571
        }
slouken@5264
   572
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@5264
   573
                                    rect->w/2, rect->h/2,
slouken@5264
   574
                                    data->format, data->formattype, pixels);
slouken@5264
   575
slouken@5264
   576
        /* Skip to the top of the next texture */
slouken@5264
   577
        top = (const void*)((const Uint8*)top + (texture->h * pitch)/4);
slouken@5264
   578
slouken@5264
   579
        /* Skip to the correct offset into the next texture */
slouken@5264
   580
        pixels = (const void*)((const Uint8*)top + (rect->y / 2) * pitch + rect->x / 2);
slouken@5264
   581
        if (texture->format == SDL_PIXELFORMAT_YV12) {
slouken@5264
   582
            renderdata->glBindTexture(data->type, data->utexture);
slouken@5264
   583
        } else {
slouken@5264
   584
            renderdata->glBindTexture(data->type, data->vtexture);
slouken@5264
   585
        }
slouken@5264
   586
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@5264
   587
                                    rect->w/2, rect->h/2,
slouken@5264
   588
                                    data->format, data->formattype, pixels);
slouken@5264
   589
    }
slouken@3041
   590
    renderdata->glDisable(data->type);
slouken@1927
   591
    result = renderdata->glGetError();
slouken@1924
   592
    if (result != GL_NO_ERROR) {
slouken@1924
   593
        GL_SetError("glTexSubImage2D()", result);
slouken@1924
   594
        return -1;
slouken@1924
   595
    }
slouken@1918
   596
    return 0;
slouken@1918
   597
}
slouken@1918
   598
slouken@1918
   599
static int
slouken@1918
   600
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   601
               const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1918
   602
{
slouken@1918
   603
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   604
slouken@5227
   605
    data->locked_rect = *rect;
slouken@5227
   606
    *pixels = 
slouken@1920
   607
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@5156
   608
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@1920
   609
    *pitch = data->pitch;
slouken@1918
   610
    return 0;
slouken@1918
   611
}
slouken@1918
   612
slouken@1918
   613
static void
slouken@1918
   614
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   615
{
slouken@5156
   616
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@5227
   617
    const SDL_Rect *rect;
slouken@5227
   618
    void *pixels;
slouken@1918
   619
slouken@5227
   620
    rect = &data->locked_rect;
slouken@5227
   621
    pixels = 
slouken@5227
   622
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@5227
   623
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5227
   624
    GL_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
slouken@1918
   625
}
slouken@1918
   626
slouken@5297
   627
static int
slouken@5297
   628
GL_UpdateViewport(SDL_Renderer * renderer)
slouken@5224
   629
{
slouken@5224
   630
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5224
   631
slouken@5297
   632
    if (SDL_CurrentContext != data->context) {
slouken@5297
   633
        /* We'll update the viewport after we rebind the context */
slouken@5297
   634
        return 0;
slouken@5297
   635
    }
slouken@5224
   636
slouken@5297
   637
    data->glViewport(renderer->viewport.x, renderer->viewport.y,
slouken@5297
   638
                     renderer->viewport.w, renderer->viewport.h);
slouken@5224
   639
slouken@5297
   640
    data->glMatrixMode(GL_PROJECTION);
slouken@5297
   641
    data->glLoadIdentity();
slouken@5297
   642
    data->glOrtho((GLdouble) 0,
slouken@5297
   643
                  (GLdouble) renderer->viewport.w,
slouken@5297
   644
                  (GLdouble) renderer->viewport.h,
slouken@5297
   645
                  (GLdouble) 0, 0.0, 1.0);
slouken@5297
   646
    return 0;
slouken@5224
   647
}
slouken@5224
   648
slouken@5224
   649
static void
slouken@5355
   650
GL_SetShader(GL_RenderData * data, GL_Shader shader)
slouken@5355
   651
{
slouken@5355
   652
    if (data->shaders && shader != data->current.shader) {
slouken@5355
   653
        GL_SelectShader(data->shaders, shader);
slouken@5355
   654
        data->current.shader = shader;
slouken@5355
   655
    }
slouken@5355
   656
}
slouken@5355
   657
slouken@5355
   658
static void
slouken@5355
   659
GL_SetColor(GL_RenderData * data, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@5355
   660
{
slouken@5355
   661
    Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
slouken@5355
   662
slouken@5355
   663
    if (color != data->current.color) {
slouken@5355
   664
        data->glColor4f((GLfloat) r * inv255f,
slouken@5355
   665
                        (GLfloat) g * inv255f,
slouken@5355
   666
                        (GLfloat) b * inv255f,
slouken@5355
   667
                        (GLfloat) a * inv255f);
slouken@5355
   668
        data->current.color = color;
slouken@5355
   669
    }
slouken@5355
   670
}
slouken@5355
   671
slouken@5355
   672
static void
slouken@5140
   673
GL_SetBlendMode(GL_RenderData * data, int blendMode)
slouken@2936
   674
{
slouken@5355
   675
    if (blendMode != data->current.blendMode) {
slouken@2936
   676
        switch (blendMode) {
slouken@2936
   677
        case SDL_BLENDMODE_NONE:
slouken@2936
   678
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
slouken@2936
   679
            data->glDisable(GL_BLEND);
slouken@2936
   680
            break;
slouken@2936
   681
        case SDL_BLENDMODE_BLEND:
slouken@2936
   682
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
   683
            data->glEnable(GL_BLEND);
slouken@2936
   684
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@2936
   685
            break;
slouken@2936
   686
        case SDL_BLENDMODE_ADD:
slouken@2936
   687
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
   688
            data->glEnable(GL_BLEND);
slouken@2936
   689
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@2936
   690
            break;
slouken@5184
   691
        case SDL_BLENDMODE_MOD:
slouken@5184
   692
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@5184
   693
            data->glEnable(GL_BLEND);
slouken@5184
   694
            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@5184
   695
            break;
slouken@2936
   696
        }
slouken@5355
   697
        data->current.blendMode = blendMode;
slouken@2936
   698
    }
slouken@2936
   699
}
slouken@2936
   700
slouken@5355
   701
static void
slouken@5355
   702
GL_SetDrawingState(SDL_Renderer * renderer)
slouken@5355
   703
{
slouken@5355
   704
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5355
   705
slouken@5355
   706
    GL_ActivateRenderer(renderer);
slouken@5355
   707
slouken@5381
   708
    GL_SetColor(data, renderer->r,
slouken@5381
   709
                      renderer->g,
slouken@5381
   710
                      renderer->b,
slouken@5381
   711
                      renderer->a);
slouken@5355
   712
slouken@5355
   713
    GL_SetBlendMode(data, renderer->blendMode);
slouken@5355
   714
slouken@5355
   715
    GL_SetShader(data, SHADER_SOLID);
slouken@5355
   716
}
slouken@5355
   717
slouken@1918
   718
static int
slouken@3596
   719
GL_RenderClear(SDL_Renderer * renderer)
slouken@3596
   720
{
slouken@3596
   721
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3596
   722
slouken@5147
   723
    GL_ActivateRenderer(renderer);
slouken@5147
   724
slouken@3596
   725
    data->glClearColor((GLfloat) renderer->r * inv255f,
slouken@3596
   726
                       (GLfloat) renderer->g * inv255f,
slouken@3596
   727
                       (GLfloat) renderer->b * inv255f,
slouken@3596
   728
                       (GLfloat) renderer->a * inv255f);
slouken@3596
   729
slouken@3596
   730
    data->glClear(GL_COLOR_BUFFER_BIT);
slouken@3596
   731
slouken@3596
   732
    return 0;
slouken@3596
   733
}
slouken@3596
   734
slouken@3596
   735
static int
slouken@3596
   736
GL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   737
                    int count)
slouken@2884
   738
{
slouken@2884
   739
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   740
    int i;
slouken@2884
   741
slouken@5355
   742
    GL_SetDrawingState(renderer);
slouken@2884
   743
slouken@2901
   744
    data->glBegin(GL_POINTS);
slouken@3536
   745
    for (i = 0; i < count; ++i) {
slouken@3536
   746
        data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   747
    }
slouken@2901
   748
    data->glEnd();
slouken@2901
   749
slouken@2901
   750
    return 0;
slouken@2901
   751
}
slouken@2901
   752
slouken@2901
   753
static int
slouken@3596
   754
GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   755
                   int count)
slouken@2901
   756
{
slouken@2901
   757
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   758
    int i;
slouken@2901
   759
slouken@5355
   760
    GL_SetDrawingState(renderer);
slouken@2901
   761
slouken@3536
   762
    if (count > 2 && 
slouken@3536
   763
        points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
slouken@3536
   764
        data->glBegin(GL_LINE_LOOP);
slouken@3536
   765
        /* GL_LINE_LOOP takes care of the final segment */
slouken@3536
   766
        --count;
slouken@3536
   767
        for (i = 0; i < count; ++i) {
slouken@3536
   768
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   769
        }
slouken@3536
   770
        data->glEnd();
slouken@3536
   771
    } else {
slouken@5086
   772
#if defined(__APPLE__) || defined(__WIN32__)
aschiffler@4910
   773
#else
slouken@4905
   774
        int x1, y1, x2, y2;
aschiffler@4910
   775
#endif
slouken@4905
   776
slouken@3536
   777
        data->glBegin(GL_LINE_STRIP);
slouken@3536
   778
        for (i = 0; i < count; ++i) {
slouken@3536
   779
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   780
        }
slouken@3536
   781
        data->glEnd();
slouken@3474
   782
slouken@3536
   783
        /* The line is half open, so we need one more point to complete it.
slouken@3536
   784
         * http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node47.html
slouken@3536
   785
         * If we have to, we can use vertical line and horizontal line textures
slouken@3536
   786
         * for vertical and horizontal lines, and then create custom textures
slouken@3536
   787
         * for diagonal lines and software render those.  It's terrible, but at
slouken@3536
   788
         * least it would be pixel perfect.
slouken@3536
   789
         */
slouken@3536
   790
        data->glBegin(GL_POINTS);
slouken@5086
   791
#if defined(__APPLE__) || defined(__WIN32__)
slouken@3536
   792
        /* Mac OS X and Windows seem to always leave the second point open */
slouken@3536
   793
        data->glVertex2f(0.5f + points[count-1].x, 0.5f + points[count-1].y);
slouken@3474
   794
#else
slouken@3536
   795
        /* Linux seems to leave the right-most or bottom-most point open */
slouken@4905
   796
        x1 = points[0].x;
slouken@4905
   797
        y1 = points[0].y;
slouken@4905
   798
        x2 = points[count-1].x;
slouken@4905
   799
        y2 = points[count-1].y;
slouken@3536
   800
slouken@3536
   801
        if (x1 > x2) {
slouken@3536
   802
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@3536
   803
        } else if (x2 > x1) {
slouken@3536
   804
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@3536
   805
        } else if (y1 > y2) {
slouken@3536
   806
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@3536
   807
        } else if (y2 > y1) {
slouken@3536
   808
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@3536
   809
        }
slouken@3536
   810
#endif
slouken@3536
   811
        data->glEnd();
slouken@3474
   812
    }
slouken@3455
   813
slouken@1918
   814
    return 0;
slouken@1918
   815
}
slouken@1918
   816
slouken@1918
   817
static int
slouken@5297
   818
GL_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect * rects, int count)
slouken@2925
   819
{
slouken@2925
   820
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   821
    int i;
slouken@2925
   822
slouken@5355
   823
    GL_SetDrawingState(renderer);
slouken@2936
   824
slouken@3536
   825
    for (i = 0; i < count; ++i) {
slouken@5297
   826
        const SDL_Rect *rect = &rects[i];
slouken@3536
   827
slouken@3536
   828
        data->glRecti(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
slouken@3536
   829
    }
slouken@2925
   830
slouken@2925
   831
    return 0;
slouken@2925
   832
}
slouken@2925
   833
slouken@2925
   834
static int
slouken@1918
   835
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   836
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1918
   837
{
slouken@1918
   838
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   839
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@1918
   840
    int minx, miny, maxx, maxy;
slouken@1918
   841
    GLfloat minu, maxu, minv, maxv;
slouken@1918
   842
slouken@5147
   843
    GL_ActivateRenderer(renderer);
slouken@5147
   844
slouken@5355
   845
    data->glEnable(texturedata->type);
slouken@5355
   846
    if (texturedata->yuv) {
slouken@5355
   847
        data->glActiveTextureARB(GL_TEXTURE2_ARB);
slouken@5355
   848
        data->glBindTexture(texturedata->type, texturedata->vtexture);
slouken@5355
   849
        if (texturedata->scaleMode != data->current.scaleMode) {
slouken@5355
   850
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
slouken@5355
   851
                                  texturedata->scaleMode);
slouken@5355
   852
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
slouken@5355
   853
                                  texturedata->scaleMode);
slouken@5355
   854
        }
slouken@5355
   855
slouken@5355
   856
        data->glActiveTextureARB(GL_TEXTURE1_ARB);
slouken@5355
   857
        data->glBindTexture(texturedata->type, texturedata->utexture);
slouken@5355
   858
        if (texturedata->scaleMode != data->current.scaleMode) {
slouken@5355
   859
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
slouken@5355
   860
                                  texturedata->scaleMode);
slouken@5355
   861
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
slouken@5355
   862
                                  texturedata->scaleMode);
slouken@5355
   863
        }
slouken@5355
   864
slouken@5355
   865
        data->glActiveTextureARB(GL_TEXTURE0_ARB);
slouken@5355
   866
    }
slouken@5355
   867
    data->glBindTexture(texturedata->type, texturedata->texture);
slouken@5355
   868
slouken@5355
   869
    if (texturedata->scaleMode != data->current.scaleMode) {
slouken@5355
   870
        data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
slouken@5355
   871
                              texturedata->scaleMode);
slouken@5355
   872
        data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
slouken@5355
   873
                              texturedata->scaleMode);
slouken@5355
   874
        data->current.scaleMode = texturedata->scaleMode;
slouken@5355
   875
    }
slouken@5355
   876
slouken@5355
   877
    if (texture->modMode) {
slouken@5355
   878
        GL_SetColor(data, texture->r, texture->g, texture->b, texture->a);
slouken@5355
   879
    } else {
slouken@5355
   880
        GL_SetColor(data, 255, 255, 255, 255);
slouken@5355
   881
    }
slouken@5355
   882
slouken@5355
   883
    GL_SetBlendMode(data, texture->blendMode);
slouken@5355
   884
slouken@5355
   885
    if (texturedata->yuv) {
slouken@5355
   886
        GL_SetShader(data, SHADER_YV12);
slouken@5355
   887
    } else {
slouken@5355
   888
        GL_SetShader(data, SHADER_RGB);
slouken@5355
   889
    }
slouken@5355
   890
slouken@1918
   891
    minx = dstrect->x;
slouken@1918
   892
    miny = dstrect->y;
slouken@1918
   893
    maxx = dstrect->x + dstrect->w;
slouken@1918
   894
    maxy = dstrect->y + dstrect->h;
slouken@1918
   895
slouken@1918
   896
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
   897
    minu *= texturedata->texw;
slouken@1918
   898
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
   899
    maxu *= texturedata->texw;
slouken@1918
   900
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
   901
    minv *= texturedata->texh;
slouken@1918
   902
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
   903
    maxv *= texturedata->texh;
slouken@1918
   904
slouken@1927
   905
    data->glBegin(GL_TRIANGLE_STRIP);
slouken@1927
   906
    data->glTexCoord2f(minu, minv);
slouken@3472
   907
    data->glVertex2f((GLfloat) minx, (GLfloat) miny);
slouken@1927
   908
    data->glTexCoord2f(maxu, minv);
slouken@3472
   909
    data->glVertex2f((GLfloat) maxx, (GLfloat) miny);
slouken@1927
   910
    data->glTexCoord2f(minu, maxv);
slouken@3472
   911
    data->glVertex2f((GLfloat) minx, (GLfloat) maxy);
slouken@1927
   912
    data->glTexCoord2f(maxu, maxv);
slouken@3472
   913
    data->glVertex2f((GLfloat) maxx, (GLfloat) maxy);
slouken@1927
   914
    data->glEnd();
slouken@1918
   915
slouken@2884
   916
    data->glDisable(texturedata->type);
slouken@2884
   917
slouken@1918
   918
    return 0;
slouken@1918
   919
}
slouken@1918
   920
slouken@3431
   921
static int
slouken@3431
   922
GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
   923
                    Uint32 pixel_format, void * pixels, int pitch)
slouken@3431
   924
{
slouken@3433
   925
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3685
   926
    SDL_Window *window = renderer->window;
slouken@3433
   927
    GLint internalFormat;
slouken@3433
   928
    GLenum format, type;
slouken@3435
   929
    Uint8 *src, *dst, *tmp;
slouken@5154
   930
    int w, h, length, rows;
slouken@3433
   931
slouken@5147
   932
    GL_ActivateRenderer(renderer);
slouken@5147
   933
slouken@3433
   934
    if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
slouken@3435
   935
        /* FIXME: Do a temp copy to a format that is supported */
slouken@3433
   936
        SDL_SetError("Unsupported pixel format");
slouken@3433
   937
        return -1;
slouken@3433
   938
    }
slouken@3433
   939
slouken@5154
   940
    SDL_GetWindowSize(window, &w, &h);
slouken@5154
   941
slouken@3446
   942
    data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
slouken@3446
   943
    data->glPixelStorei(GL_PACK_ROW_LENGTH,
slouken@5156
   944
                        (pitch / SDL_BYTESPERPIXEL(pixel_format)));
slouken@3433
   945
slouken@5154
   946
    data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
slouken@3435
   947
                       format, type, pixels);
slouken@3435
   948
slouken@3435
   949
    /* Flip the rows to be top-down */
slouken@5156
   950
    length = rect->w * SDL_BYTESPERPIXEL(pixel_format);
slouken@3435
   951
    src = (Uint8*)pixels + (rect->h-1)*pitch;
slouken@3435
   952
    dst = (Uint8*)pixels;
slouken@3435
   953
    tmp = SDL_stack_alloc(Uint8, length);
slouken@3435
   954
    rows = rect->h / 2;
slouken@3435
   955
    while (rows--) {
slouken@3435
   956
        SDL_memcpy(tmp, dst, length);
slouken@3435
   957
        SDL_memcpy(dst, src, length);
slouken@3435
   958
        SDL_memcpy(src, tmp, length);
slouken@3447
   959
        dst += pitch;
slouken@3447
   960
        src -= pitch;
slouken@3435
   961
    }
slouken@3435
   962
    SDL_stack_free(tmp);
slouken@3440
   963
slouken@3440
   964
    return 0;
slouken@3431
   965
}
slouken@3431
   966
slouken@1918
   967
static void
slouken@1918
   968
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
   969
{
slouken@5147
   970
    GL_ActivateRenderer(renderer);
slouken@5147
   971
slouken@1918
   972
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
   973
}
slouken@1918
   974
slouken@1918
   975
static void
slouken@1918
   976
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   977
{
slouken@1927
   978
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   979
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   980
slouken@5147
   981
    GL_ActivateRenderer(renderer);
slouken@5147
   982
slouken@1918
   983
    if (!data) {
slouken@1918
   984
        return;
slouken@1918
   985
    }
slouken@1918
   986
    if (data->texture) {
slouken@1927
   987
        renderdata->glDeleteTextures(1, &data->texture);
slouken@1918
   988
    }
slouken@5264
   989
    if (data->yuv) {
slouken@5264
   990
        renderdata->glDeleteTextures(1, &data->utexture);
slouken@5264
   991
        renderdata->glDeleteTextures(1, &data->vtexture);
slouken@5264
   992
    }
slouken@1920
   993
    if (data->pixels) {
slouken@1920
   994
        SDL_free(data->pixels);
slouken@1920
   995
    }
slouken@1918
   996
    SDL_free(data);
slouken@1918
   997
    texture->driverdata = NULL;
slouken@1918
   998
}
slouken@1918
   999
slouken@1975
  1000
static void
slouken@1918
  1001
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
  1002
{
slouken@1918
  1003
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1004
slouken@1918
  1005
    if (data) {
slouken@1920
  1006
        if (data->context) {
bob@2328
  1007
            /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
slouken@1920
  1008
            SDL_GL_DeleteContext(data->context);
slouken@1918
  1009
        }
slouken@1918
  1010
        SDL_free(data);
slouken@1918
  1011
    }
slouken@1918
  1012
    SDL_free(renderer);
slouken@1918
  1013
}
slouken@1918
  1014
slouken@5226
  1015
#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
slouken@1918
  1016
slouken@1918
  1017
/* vi: set ts=4 sw=4 expandtab: */