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