src/render/opengl/SDL_render_gl.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 30 Dec 2011 18:18:42 -0500
changeset 6135 b052a5882a47
parent 5981 75caa8a7d559
child 6138 4c64952a58fb
permissions -rw-r--r--
Fixed bug 1315 - Greenish video when video size smaller than texture

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