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