src/render/opengl/SDL_render_gl.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 12 Feb 2011 00:25:02 -0800
changeset 5264 7ace5f8f432f
parent 5262 b530ef003506
child 5265 48724afcdc6e
permissions -rw-r--r--
Initial pass at shader YV12 support - doesn't quite work yet.
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@5264
   556
        /* Skip to the top of the next texture */
slouken@5264
   557
        const void *top = (const void*)((const Uint8*)pixels + (texture->h-rect->y) * pitch - rect->x);
slouken@5264
   558
slouken@5264
   559
        /* Skip to the correct offset into the next texture */
slouken@5264
   560
        pixels = (const void*)((const Uint8*)top + (rect->y / 2) * pitch + rect->x / 2);
slouken@5264
   561
        if (texture->format == SDL_PIXELFORMAT_YV12) {
slouken@5264
   562
            renderdata->glBindTexture(data->type, data->vtexture);
slouken@5264
   563
        } else {
slouken@5264
   564
            renderdata->glBindTexture(data->type, data->utexture);
slouken@5264
   565
        }
slouken@5264
   566
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@5264
   567
                                    rect->w/2, rect->h/2,
slouken@5264
   568
                                    data->format, data->formattype, pixels);
slouken@5264
   569
slouken@5264
   570
        /* Skip to the top of the next texture */
slouken@5264
   571
        top = (const void*)((const Uint8*)top + (texture->h * pitch)/4);
slouken@5264
   572
slouken@5264
   573
        /* Skip to the correct offset into the next texture */
slouken@5264
   574
        pixels = (const void*)((const Uint8*)top + (rect->y / 2) * pitch + rect->x / 2);
slouken@5264
   575
        if (texture->format == SDL_PIXELFORMAT_YV12) {
slouken@5264
   576
            renderdata->glBindTexture(data->type, data->utexture);
slouken@5264
   577
        } else {
slouken@5264
   578
            renderdata->glBindTexture(data->type, data->vtexture);
slouken@5264
   579
        }
slouken@5264
   580
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@5264
   581
                                    rect->w/2, rect->h/2,
slouken@5264
   582
                                    data->format, data->formattype, pixels);
slouken@5264
   583
    }
slouken@3041
   584
    renderdata->glDisable(data->type);
slouken@1927
   585
    result = renderdata->glGetError();
slouken@1924
   586
    if (result != GL_NO_ERROR) {
slouken@1924
   587
        GL_SetError("glTexSubImage2D()", result);
slouken@1924
   588
        return -1;
slouken@1924
   589
    }
slouken@1918
   590
    return 0;
slouken@1918
   591
}
slouken@1918
   592
slouken@1918
   593
static int
slouken@1918
   594
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   595
               const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1918
   596
{
slouken@1918
   597
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   598
slouken@5227
   599
    data->locked_rect = *rect;
slouken@5227
   600
    *pixels = 
slouken@1920
   601
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@5156
   602
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@1920
   603
    *pitch = data->pitch;
slouken@1918
   604
    return 0;
slouken@1918
   605
}
slouken@1918
   606
slouken@1918
   607
static void
slouken@1918
   608
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   609
{
slouken@5156
   610
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@5227
   611
    const SDL_Rect *rect;
slouken@5227
   612
    void *pixels;
slouken@1918
   613
slouken@5227
   614
    rect = &data->locked_rect;
slouken@5227
   615
    pixels = 
slouken@5227
   616
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@5227
   617
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5227
   618
    GL_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
slouken@1918
   619
}
slouken@1918
   620
slouken@2936
   621
static void
slouken@5224
   622
GL_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@5224
   623
{
slouken@5224
   624
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5224
   625
slouken@5224
   626
    GL_ActivateRenderer(renderer);
slouken@5224
   627
slouken@5224
   628
    if (rect) {
slouken@5224
   629
        int w, h;
slouken@5224
   630
slouken@5224
   631
        SDL_GetWindowSize(renderer->window, &w, &h);
slouken@5224
   632
        data->glScissor(rect->x, (h-(rect->y+rect->h)), rect->w, rect->h);
slouken@5224
   633
        data->glEnable(GL_SCISSOR_TEST);
slouken@5224
   634
    } else {
slouken@5224
   635
        data->glDisable(GL_SCISSOR_TEST);
slouken@5224
   636
    }
slouken@5224
   637
}
slouken@5224
   638
slouken@5224
   639
static void
slouken@5140
   640
GL_SetBlendMode(GL_RenderData * data, int blendMode)
slouken@2936
   641
{
slouken@2936
   642
    if (blendMode != data->blendMode) {
slouken@2936
   643
        switch (blendMode) {
slouken@2936
   644
        case SDL_BLENDMODE_NONE:
slouken@2936
   645
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
slouken@2936
   646
            data->glDisable(GL_BLEND);
slouken@2936
   647
            break;
slouken@2936
   648
        case SDL_BLENDMODE_BLEND:
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_MINUS_SRC_ALPHA);
slouken@2936
   652
            break;
slouken@2936
   653
        case SDL_BLENDMODE_ADD:
slouken@2936
   654
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
   655
            data->glEnable(GL_BLEND);
slouken@2936
   656
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@2936
   657
            break;
slouken@5184
   658
        case SDL_BLENDMODE_MOD:
slouken@5184
   659
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@5184
   660
            data->glEnable(GL_BLEND);
slouken@5184
   661
            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@5184
   662
            break;
slouken@2936
   663
        }
slouken@2936
   664
        data->blendMode = blendMode;
slouken@2936
   665
    }
slouken@2936
   666
}
slouken@2936
   667
slouken@1918
   668
static int
slouken@3596
   669
GL_RenderClear(SDL_Renderer * renderer)
slouken@3596
   670
{
slouken@3596
   671
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3596
   672
slouken@5147
   673
    GL_ActivateRenderer(renderer);
slouken@5147
   674
slouken@3596
   675
    data->glClearColor((GLfloat) renderer->r * inv255f,
slouken@3596
   676
                       (GLfloat) renderer->g * inv255f,
slouken@3596
   677
                       (GLfloat) renderer->b * inv255f,
slouken@3596
   678
                       (GLfloat) renderer->a * inv255f);
slouken@3596
   679
slouken@3596
   680
    data->glClear(GL_COLOR_BUFFER_BIT);
slouken@3596
   681
slouken@3596
   682
    return 0;
slouken@3596
   683
}
slouken@3596
   684
slouken@3596
   685
static int
slouken@3596
   686
GL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   687
                    int count)
slouken@2884
   688
{
slouken@2884
   689
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   690
    int i;
slouken@2884
   691
slouken@5147
   692
    GL_ActivateRenderer(renderer);
slouken@5147
   693
slouken@5140
   694
    GL_SetBlendMode(data, renderer->blendMode);
slouken@5228
   695
    GL_SelectShader(data->shaders, SHADER_SOLID);
slouken@2884
   696
slouken@2884
   697
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2884
   698
                    (GLfloat) renderer->g * inv255f,
slouken@2884
   699
                    (GLfloat) renderer->b * inv255f,
slouken@2884
   700
                    (GLfloat) renderer->a * inv255f);
slouken@2884
   701
slouken@2901
   702
    data->glBegin(GL_POINTS);
slouken@3536
   703
    for (i = 0; i < count; ++i) {
slouken@3536
   704
        data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   705
    }
slouken@2901
   706
    data->glEnd();
slouken@2901
   707
slouken@2901
   708
    return 0;
slouken@2901
   709
}
slouken@2901
   710
slouken@2901
   711
static int
slouken@3596
   712
GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   713
                   int count)
slouken@2901
   714
{
slouken@2901
   715
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   716
    int i;
slouken@2901
   717
slouken@5147
   718
    GL_ActivateRenderer(renderer);
slouken@5147
   719
slouken@5140
   720
    GL_SetBlendMode(data, renderer->blendMode);
slouken@5228
   721
    GL_SelectShader(data->shaders, SHADER_SOLID);
slouken@2901
   722
slouken@2901
   723
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2901
   724
                    (GLfloat) renderer->g * inv255f,
slouken@2901
   725
                    (GLfloat) renderer->b * inv255f,
slouken@2901
   726
                    (GLfloat) renderer->a * inv255f);
slouken@2901
   727
slouken@3536
   728
    if (count > 2 && 
slouken@3536
   729
        points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
slouken@3536
   730
        data->glBegin(GL_LINE_LOOP);
slouken@3536
   731
        /* GL_LINE_LOOP takes care of the final segment */
slouken@3536
   732
        --count;
slouken@3536
   733
        for (i = 0; i < count; ++i) {
slouken@3536
   734
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   735
        }
slouken@3536
   736
        data->glEnd();
slouken@3536
   737
    } else {
slouken@5086
   738
#if defined(__APPLE__) || defined(__WIN32__)
aschiffler@4910
   739
#else
slouken@4905
   740
        int x1, y1, x2, y2;
aschiffler@4910
   741
#endif
slouken@4905
   742
slouken@3536
   743
        data->glBegin(GL_LINE_STRIP);
slouken@3536
   744
        for (i = 0; i < count; ++i) {
slouken@3536
   745
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   746
        }
slouken@3536
   747
        data->glEnd();
slouken@3474
   748
slouken@3536
   749
        /* The line is half open, so we need one more point to complete it.
slouken@3536
   750
         * http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node47.html
slouken@3536
   751
         * If we have to, we can use vertical line and horizontal line textures
slouken@3536
   752
         * for vertical and horizontal lines, and then create custom textures
slouken@3536
   753
         * for diagonal lines and software render those.  It's terrible, but at
slouken@3536
   754
         * least it would be pixel perfect.
slouken@3536
   755
         */
slouken@3536
   756
        data->glBegin(GL_POINTS);
slouken@5086
   757
#if defined(__APPLE__) || defined(__WIN32__)
slouken@3536
   758
        /* Mac OS X and Windows seem to always leave the second point open */
slouken@3536
   759
        data->glVertex2f(0.5f + points[count-1].x, 0.5f + points[count-1].y);
slouken@3474
   760
#else
slouken@3536
   761
        /* Linux seems to leave the right-most or bottom-most point open */
slouken@4905
   762
        x1 = points[0].x;
slouken@4905
   763
        y1 = points[0].y;
slouken@4905
   764
        x2 = points[count-1].x;
slouken@4905
   765
        y2 = points[count-1].y;
slouken@3536
   766
slouken@3536
   767
        if (x1 > x2) {
slouken@3536
   768
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@3536
   769
        } else if (x2 > x1) {
slouken@3536
   770
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@3536
   771
        } else if (y1 > y2) {
slouken@3536
   772
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@3536
   773
        } else if (y2 > y1) {
slouken@3536
   774
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@3536
   775
        }
slouken@3536
   776
#endif
slouken@3536
   777
        data->glEnd();
slouken@3474
   778
    }
slouken@3455
   779
slouken@1918
   780
    return 0;
slouken@1918
   781
}
slouken@1918
   782
slouken@1918
   783
static int
slouken@3596
   784
GL_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count)
slouken@2925
   785
{
slouken@2925
   786
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   787
    int i;
slouken@2925
   788
slouken@5147
   789
    GL_ActivateRenderer(renderer);
slouken@5147
   790
slouken@5140
   791
    GL_SetBlendMode(data, renderer->blendMode);
slouken@5228
   792
    GL_SelectShader(data->shaders, SHADER_SOLID);
slouken@2936
   793
slouken@2925
   794
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2925
   795
                    (GLfloat) renderer->g * inv255f,
slouken@2925
   796
                    (GLfloat) renderer->b * inv255f,
slouken@2925
   797
                    (GLfloat) renderer->a * inv255f);
slouken@2936
   798
slouken@3536
   799
    for (i = 0; i < count; ++i) {
slouken@3536
   800
        const SDL_Rect *rect = rects[i];
slouken@3536
   801
slouken@3536
   802
        data->glRecti(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
slouken@3536
   803
    }
slouken@2925
   804
slouken@2925
   805
    return 0;
slouken@2925
   806
}
slouken@2925
   807
slouken@2925
   808
static int
slouken@1918
   809
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   810
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1918
   811
{
slouken@1918
   812
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   813
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@1918
   814
    int minx, miny, maxx, maxy;
slouken@1918
   815
    GLfloat minu, maxu, minv, maxv;
slouken@1918
   816
slouken@5147
   817
    GL_ActivateRenderer(renderer);
slouken@5147
   818
slouken@1918
   819
    minx = dstrect->x;
slouken@1918
   820
    miny = dstrect->y;
slouken@1918
   821
    maxx = dstrect->x + dstrect->w;
slouken@1918
   822
    maxy = dstrect->y + dstrect->h;
slouken@1918
   823
slouken@1918
   824
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
   825
    minu *= texturedata->texw;
slouken@1918
   826
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
   827
    maxu *= texturedata->texw;
slouken@1918
   828
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
   829
    minv *= texturedata->texh;
slouken@1918
   830
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
   831
    maxv *= texturedata->texh;
slouken@1918
   832
slouken@2884
   833
    data->glEnable(texturedata->type);
slouken@5264
   834
    if (texturedata->yuv) {
slouken@5264
   835
        data->glActiveTextureARB(GL_TEXTURE2_ARB);
slouken@5264
   836
        data->glBindTexture(texturedata->type, texturedata->vtexture);
slouken@5264
   837
        data->glActiveTextureARB(GL_TEXTURE1_ARB);
slouken@5264
   838
        data->glBindTexture(texturedata->type, texturedata->utexture);
slouken@5264
   839
        data->glActiveTextureARB(GL_TEXTURE0_ARB);
slouken@5264
   840
    }
slouken@1927
   841
    data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1918
   842
slouken@1985
   843
    if (texture->modMode) {
slouken@1985
   844
        data->glColor4f((GLfloat) texture->r * inv255f,
slouken@1985
   845
                        (GLfloat) texture->g * inv255f,
slouken@1985
   846
                        (GLfloat) texture->b * inv255f,
slouken@1985
   847
                        (GLfloat) texture->a * inv255f);
slouken@1985
   848
    } else {
slouken@1985
   849
        data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
slouken@1985
   850
    }
slouken@1985
   851
slouken@5140
   852
    GL_SetBlendMode(data, texture->blendMode);
slouken@5264
   853
    if (texturedata->yuv) {
slouken@5264
   854
        GL_SelectShader(data->shaders, SHADER_YV12);
slouken@5264
   855
    } else {
slouken@5264
   856
        GL_SelectShader(data->shaders, SHADER_RGB);
slouken@5264
   857
    }
slouken@1918
   858
slouken@1927
   859
    data->glBegin(GL_TRIANGLE_STRIP);
slouken@1927
   860
    data->glTexCoord2f(minu, minv);
slouken@3472
   861
    data->glVertex2f((GLfloat) minx, (GLfloat) miny);
slouken@1927
   862
    data->glTexCoord2f(maxu, minv);
slouken@3472
   863
    data->glVertex2f((GLfloat) maxx, (GLfloat) miny);
slouken@1927
   864
    data->glTexCoord2f(minu, maxv);
slouken@3472
   865
    data->glVertex2f((GLfloat) minx, (GLfloat) maxy);
slouken@1927
   866
    data->glTexCoord2f(maxu, maxv);
slouken@3472
   867
    data->glVertex2f((GLfloat) maxx, (GLfloat) maxy);
slouken@1927
   868
    data->glEnd();
slouken@1918
   869
slouken@2884
   870
    data->glDisable(texturedata->type);
slouken@2884
   871
slouken@1918
   872
    return 0;
slouken@1918
   873
}
slouken@1918
   874
slouken@3431
   875
static int
slouken@3431
   876
GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
   877
                    Uint32 pixel_format, void * pixels, int pitch)
slouken@3431
   878
{
slouken@3433
   879
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3685
   880
    SDL_Window *window = renderer->window;
slouken@3433
   881
    GLint internalFormat;
slouken@3433
   882
    GLenum format, type;
slouken@3435
   883
    Uint8 *src, *dst, *tmp;
slouken@5154
   884
    int w, h, length, rows;
slouken@3433
   885
slouken@5147
   886
    GL_ActivateRenderer(renderer);
slouken@5147
   887
slouken@3433
   888
    if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
slouken@3435
   889
        /* FIXME: Do a temp copy to a format that is supported */
slouken@3433
   890
        SDL_SetError("Unsupported pixel format");
slouken@3433
   891
        return -1;
slouken@3433
   892
    }
slouken@3433
   893
slouken@5154
   894
    SDL_GetWindowSize(window, &w, &h);
slouken@5154
   895
slouken@3446
   896
    data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
slouken@3446
   897
    data->glPixelStorei(GL_PACK_ROW_LENGTH,
slouken@5156
   898
                        (pitch / SDL_BYTESPERPIXEL(pixel_format)));
slouken@3433
   899
slouken@5154
   900
    data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
slouken@3435
   901
                       format, type, pixels);
slouken@3435
   902
slouken@3435
   903
    /* Flip the rows to be top-down */
slouken@5156
   904
    length = rect->w * SDL_BYTESPERPIXEL(pixel_format);
slouken@3435
   905
    src = (Uint8*)pixels + (rect->h-1)*pitch;
slouken@3435
   906
    dst = (Uint8*)pixels;
slouken@3435
   907
    tmp = SDL_stack_alloc(Uint8, length);
slouken@3435
   908
    rows = rect->h / 2;
slouken@3435
   909
    while (rows--) {
slouken@3435
   910
        SDL_memcpy(tmp, dst, length);
slouken@3435
   911
        SDL_memcpy(dst, src, length);
slouken@3435
   912
        SDL_memcpy(src, tmp, length);
slouken@3447
   913
        dst += pitch;
slouken@3447
   914
        src -= pitch;
slouken@3435
   915
    }
slouken@3435
   916
    SDL_stack_free(tmp);
slouken@3440
   917
slouken@3440
   918
    return 0;
slouken@3431
   919
}
slouken@3431
   920
slouken@1918
   921
static void
slouken@1918
   922
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
   923
{
slouken@5147
   924
    GL_ActivateRenderer(renderer);
slouken@5147
   925
slouken@1918
   926
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
   927
}
slouken@1918
   928
slouken@1918
   929
static void
slouken@1918
   930
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   931
{
slouken@1927
   932
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   933
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   934
slouken@5147
   935
    GL_ActivateRenderer(renderer);
slouken@5147
   936
slouken@1918
   937
    if (!data) {
slouken@1918
   938
        return;
slouken@1918
   939
    }
slouken@1918
   940
    if (data->texture) {
slouken@1927
   941
        renderdata->glDeleteTextures(1, &data->texture);
slouken@1918
   942
    }
slouken@5264
   943
    if (data->yuv) {
slouken@5264
   944
        renderdata->glDeleteTextures(1, &data->utexture);
slouken@5264
   945
        renderdata->glDeleteTextures(1, &data->vtexture);
slouken@5264
   946
    }
slouken@1920
   947
    if (data->pixels) {
slouken@1920
   948
        SDL_free(data->pixels);
slouken@1920
   949
    }
slouken@1918
   950
    SDL_free(data);
slouken@1918
   951
    texture->driverdata = NULL;
slouken@1918
   952
}
slouken@1918
   953
slouken@1975
   954
static void
slouken@1918
   955
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
   956
{
slouken@1918
   957
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   958
slouken@1918
   959
    if (data) {
slouken@1920
   960
        if (data->context) {
bob@2328
   961
            /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
slouken@1920
   962
            SDL_GL_DeleteContext(data->context);
slouken@1918
   963
        }
slouken@1918
   964
        SDL_free(data);
slouken@1918
   965
    }
slouken@1918
   966
    SDL_free(renderer);
slouken@1918
   967
}
slouken@1918
   968
slouken@5226
   969
#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
slouken@1918
   970
slouken@1918
   971
/* vi: set ts=4 sw=4 expandtab: */