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