src/render/opengl/SDL_render_gl.c
author Edward Rudd
Fri, 20 Sep 2013 13:43:00 -0400
changeset 7746 6a05d7352575
parent 7719 31b5f9ff36ca
child 7759 869583422e5a
permissions -rw-r--r--
add in High DPI support (aka Retina)

- based on Jørgen's patch with a few bug fixes
slouken@1918
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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);
urkle@7746
    50
static int GL_GetOutputSize(SDL_Renderer * renderer, int *w, int *h);
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@6247
    58
static int GL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@5297
    59
static int GL_UpdateViewport(SDL_Renderer * renderer);
slouken@7141
    60
static int GL_UpdateClipRect(SDL_Renderer * renderer);
slouken@3596
    61
static int GL_RenderClear(SDL_Renderer * renderer);
slouken@3596
    62
static int GL_RenderDrawPoints(SDL_Renderer * renderer,
slouken@6528
    63
                               const SDL_FPoint * points, int count);
slouken@3596
    64
static int GL_RenderDrawLines(SDL_Renderer * renderer,
slouken@6528
    65
                              const SDL_FPoint * points, int count);
slouken@3596
    66
static int GL_RenderFillRects(SDL_Renderer * renderer,
slouken@6528
    67
                              const SDL_FRect * rects, int count);
slouken@1918
    68
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
    69
                         const SDL_Rect * srcrect, const SDL_FRect * dstrect);
gabomdq@6320
    70
static int GL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
    71
                         const SDL_Rect * srcrect, const SDL_FRect * dstrect,
slouken@6528
    72
                         const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip);
slouken@3431
    73
static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
    74
                               Uint32 pixel_format, void * pixels, int pitch);
slouken@1918
    75
static void GL_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    76
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    77
static void GL_DestroyRenderer(SDL_Renderer * renderer);
gabomdq@6414
    78
static int GL_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh);
gabomdq@6414
    79
static int GL_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture);
slouken@1918
    80
slouken@1918
    81
SDL_RenderDriver GL_RenderDriver = {
slouken@1918
    82
    GL_CreateRenderer,
slouken@1918
    83
    {
slouken@1918
    84
     "opengl",
slouken@6237
    85
     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
slouken@5156
    86
     1,
slouken@5156
    87
     {SDL_PIXELFORMAT_ARGB8888},
slouken@1918
    88
     0,
slouken@1918
    89
     0}
slouken@1918
    90
};
slouken@1918
    91
slouken@6232
    92
typedef struct GL_FBOList GL_FBOList;
slouken@6232
    93
slouken@6232
    94
struct GL_FBOList
slouken@6232
    95
{
slouken@6232
    96
    Uint32 w, h;
slouken@6232
    97
    GLuint FBO;
slouken@6232
    98
    GL_FBOList *next;
slouken@6232
    99
};
slouken@6232
   100
slouken@1918
   101
typedef struct
slouken@1918
   102
{
slouken@1918
   103
    SDL_GLContext context;
slouken@7194
   104
slouken@7198
   105
    SDL_bool debug_enabled;
slouken@7194
   106
    SDL_bool GL_ARB_debug_output_supported;
slouken@7194
   107
    int errors;
slouken@7194
   108
    char **error_messages;
slouken@7194
   109
    GLDEBUGPROCARB next_error_callback;
slouken@7194
   110
    GLvoid *next_error_userparam;
slouken@7194
   111
slouken@2233
   112
    SDL_bool GL_ARB_texture_rectangle_supported;
slouken@5355
   113
    struct {
slouken@5355
   114
        GL_Shader shader;
slouken@5355
   115
        Uint32 color;
slouken@5355
   116
        int blendMode;
slouken@5355
   117
    } current;
slouken@7191
   118
slouken@6232
   119
    SDL_bool GL_EXT_framebuffer_object_supported;
slouken@6232
   120
    GL_FBOList *framebuffers;
slouken@1927
   121
slouken@1927
   122
    /* OpenGL functions */
slouken@1927
   123
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
slouken@5204
   124
#include "SDL_glfuncs.h"
slouken@1927
   125
#undef SDL_PROC
slouken@1974
   126
slouken@5228
   127
    /* Multitexture support */
slouken@5228
   128
    SDL_bool GL_ARB_multitexture_supported;
slouken@5228
   129
    PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;
slouken@5351
   130
    GLint num_texture_units;
slouken@7191
   131
slouken@6232
   132
    PFNGLGENFRAMEBUFFERSEXTPROC glGenFramebuffersEXT;
slouken@6232
   133
    PFNGLDELETEFRAMEBUFFERSEXTPROC glDeleteFramebuffersEXT;
slouken@6232
   134
    PFNGLFRAMEBUFFERTEXTURE2DEXTPROC glFramebufferTexture2DEXT;
slouken@6232
   135
    PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT;
slouken@6232
   136
    PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC glCheckFramebufferStatusEXT;
slouken@5228
   137
slouken@5228
   138
    /* Shader support */
slouken@5228
   139
    GL_ShaderContext *shaders;
slouken@5228
   140
slouken@1918
   141
} GL_RenderData;
slouken@1918
   142
slouken@1918
   143
typedef struct
slouken@1918
   144
{
slouken@1918
   145
    GLuint texture;
slouken@1920
   146
    GLenum type;
slouken@1918
   147
    GLfloat texw;
slouken@1918
   148
    GLfloat texh;
slouken@1920
   149
    GLenum format;
slouken@1920
   150
    GLenum formattype;
slouken@1918
   151
    void *pixels;
slouken@1918
   152
    int pitch;
slouken@5227
   153
    SDL_Rect locked_rect;
slouken@5264
   154
slouken@5264
   155
    /* YV12 texture support */
slouken@5264
   156
    SDL_bool yuv;
slouken@5264
   157
    GLuint utexture;
slouken@5264
   158
    GLuint vtexture;
slouken@7191
   159
slouken@6232
   160
    GL_FBOList *fbo;
slouken@1918
   161
} GL_TextureData;
slouken@1918
   162
slouken@7194
   163
SDL_FORCE_INLINE const char*
slouken@6494
   164
GL_TranslateError (GLenum error)
slouken@1924
   165
{
slouken@6494
   166
#define GL_ERROR_TRANSLATE(e) case e: return #e;
slouken@6494
   167
    switch (error) {
slouken@6494
   168
    GL_ERROR_TRANSLATE(GL_INVALID_ENUM)
slouken@6494
   169
    GL_ERROR_TRANSLATE(GL_INVALID_VALUE)
slouken@6494
   170
    GL_ERROR_TRANSLATE(GL_INVALID_OPERATION)
slouken@6494
   171
    GL_ERROR_TRANSLATE(GL_OUT_OF_MEMORY)
slouken@6494
   172
    GL_ERROR_TRANSLATE(GL_NO_ERROR)
slouken@6494
   173
    GL_ERROR_TRANSLATE(GL_STACK_OVERFLOW)
slouken@6494
   174
    GL_ERROR_TRANSLATE(GL_STACK_UNDERFLOW)
slouken@6494
   175
    GL_ERROR_TRANSLATE(GL_TABLE_TOO_LARGE)
slouken@6494
   176
    default:
slouken@6494
   177
        return "UNKNOWN";
slouken@6494
   178
}
slouken@6494
   179
#undef GL_ERROR_TRANSLATE
slouken@6494
   180
}
slouken@1924
   181
slouken@7194
   182
SDL_FORCE_INLINE void
slouken@7194
   183
GL_ClearErrors(SDL_Renderer *renderer)
slouken@7194
   184
{
slouken@7194
   185
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@7194
   186
slouken@7198
   187
    if (!data->debug_enabled)
slouken@7198
   188
    {
slouken@7198
   189
        return;
slouken@7198
   190
    }
slouken@7194
   191
    if (data->GL_ARB_debug_output_supported) {
slouken@7194
   192
        if (data->errors) {
slouken@7194
   193
            int i;
slouken@7194
   194
            for (i = 0; i < data->errors; ++i) {
slouken@7194
   195
                SDL_free(data->error_messages[i]);
slouken@7194
   196
            }
slouken@7194
   197
            SDL_free(data->error_messages);
slouken@7194
   198
slouken@7194
   199
            data->errors = 0;
slouken@7194
   200
            data->error_messages = NULL;
slouken@7194
   201
        }
slouken@7194
   202
    } else {
slouken@7194
   203
        while (data->glGetError() != GL_NO_ERROR) {
slouken@7194
   204
            continue;
slouken@7194
   205
        }
slouken@7194
   206
    }
slouken@7194
   207
}
slouken@7194
   208
slouken@7194
   209
SDL_FORCE_INLINE int
slouken@7194
   210
GL_CheckAllErrors (const char *prefix, SDL_Renderer *renderer, const char *file, int line, const char *function)
slouken@6494
   211
{
slouken@6494
   212
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@6494
   213
    int ret = 0;
slouken@7194
   214
slouken@7198
   215
    if (!data->debug_enabled)
slouken@7198
   216
    {
slouken@7198
   217
        return 0;
slouken@7198
   218
    }
slouken@7194
   219
    if (data->GL_ARB_debug_output_supported) {
slouken@7194
   220
        if (data->errors) {
slouken@7194
   221
            int i;
slouken@7194
   222
            for (i = 0; i < data->errors; ++i) {
slouken@7194
   223
                SDL_SetError("%s: %s (%d): %s %s", prefix, file, line, function, data->error_messages[i]);
slouken@7194
   224
                ret = -1;
slouken@6494
   225
            }
slouken@7194
   226
            GL_ClearErrors(renderer);
slouken@7194
   227
        }
slouken@7194
   228
    } else {
slouken@7194
   229
        /* check gl errors (can return multiple errors) */
slouken@7194
   230
        for (;;) {
slouken@7194
   231
            GLenum error = data->glGetError();
slouken@7194
   232
            if (error != GL_NO_ERROR) {
slouken@7194
   233
                if (prefix == NULL || prefix[0] == '\0') {
slouken@7194
   234
                    prefix = "generic";
slouken@7194
   235
                }
slouken@7194
   236
                SDL_SetError("%s: %s (%d): %s %s (0x%X)", prefix, file, line, function, GL_TranslateError(error), error);
slouken@7194
   237
                ret = -1;
slouken@7194
   238
            } else {
slouken@7194
   239
                break;
slouken@7194
   240
            }
slouken@6494
   241
        }
slouken@1924
   242
    }
slouken@6494
   243
    return ret;
slouken@1924
   244
}
slouken@1924
   245
icculus@6499
   246
#if 0
icculus@6499
   247
#define GL_CheckError(prefix, renderer)
icculus@6499
   248
#elif defined(_MSC_VER)
icculus@6499
   249
#define GL_CheckError(prefix, renderer) GL_CheckAllErrors(prefix, renderer, __FILE__, __LINE__, __FUNCTION__)
icculus@6499
   250
#else
slouken@6494
   251
#define GL_CheckError(prefix, renderer) GL_CheckAllErrors(prefix, renderer, __FILE__, __LINE__, __PRETTY_FUNCTION__)
slouken@6494
   252
#endif
slouken@6494
   253
slouken@1927
   254
static int
slouken@1927
   255
GL_LoadFunctions(GL_RenderData * data)
slouken@1927
   256
{
slouken@1927
   257
#ifdef __SDL_NOGETPROCADDR__
slouken@1927
   258
#define SDL_PROC(ret,func,params) data->func=func;
slouken@1927
   259
#else
slouken@1927
   260
#define SDL_PROC(ret,func,params) \
slouken@1927
   261
    do { \
slouken@1927
   262
        data->func = SDL_GL_GetProcAddress(#func); \
slouken@1927
   263
        if ( ! data->func ) { \
icculus@7037
   264
            return SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
slouken@1927
   265
        } \
slouken@1927
   266
    } while ( 0 );
slouken@1927
   267
#endif /* __SDL_NOGETPROCADDR__ */
slouken@1927
   268
slouken@5204
   269
#include "SDL_glfuncs.h"
slouken@1927
   270
#undef SDL_PROC
slouken@1927
   271
    return 0;
slouken@1927
   272
}
slouken@1927
   273
slouken@5297
   274
static SDL_GLContext SDL_CurrentContext = NULL;
slouken@5297
   275
slouken@5297
   276
static int
slouken@5297
   277
GL_ActivateRenderer(SDL_Renderer * renderer)
slouken@5297
   278
{
slouken@5297
   279
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5297
   280
slouken@5297
   281
    if (SDL_CurrentContext != data->context) {
slouken@5297
   282
        if (SDL_GL_MakeCurrent(renderer->window, data->context) < 0) {
slouken@5297
   283
            return -1;
slouken@5297
   284
        }
slouken@5297
   285
        SDL_CurrentContext = data->context;
slouken@5297
   286
slouken@5297
   287
        GL_UpdateViewport(renderer);
slouken@5297
   288
    }
icculus@7486
   289
icculus@7486
   290
    GL_ClearErrors(renderer);
icculus@7486
   291
slouken@5297
   292
    return 0;
slouken@5297
   293
}
slouken@5297
   294
slouken@5355
   295
/* This is called if we need to invalidate all of the SDL OpenGL state */
slouken@5355
   296
static void
slouken@5355
   297
GL_ResetState(SDL_Renderer *renderer)
slouken@5355
   298
{
slouken@5355
   299
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5355
   300
slouken@5355
   301
    if (SDL_CurrentContext == data->context) {
slouken@5355
   302
        GL_UpdateViewport(renderer);
slouken@5355
   303
    } else {
slouken@5355
   304
        GL_ActivateRenderer(renderer);
slouken@5355
   305
    }
slouken@5355
   306
slouken@5355
   307
    data->current.shader = SHADER_NONE;
slouken@5355
   308
    data->current.color = 0;
slouken@5355
   309
    data->current.blendMode = -1;
slouken@5355
   310
slouken@5355
   311
    data->glDisable(GL_DEPTH_TEST);
slouken@5355
   312
    data->glDisable(GL_CULL_FACE);
slouken@5355
   313
    /* This ended up causing video discrepancies between OpenGL and Direct3D */
gabomdq@7678
   314
    /* data->glEnable(GL_LINE_SMOOTH); */
slouken@5355
   315
slouken@5355
   316
    data->glMatrixMode(GL_MODELVIEW);
slouken@5355
   317
    data->glLoadIdentity();
slouken@6494
   318
slouken@6494
   319
    GL_CheckError("", renderer);
slouken@5355
   320
}
slouken@5355
   321
slouken@7197
   322
static void APIENTRY
slouken@7194
   323
GL_HandleDebugMessage(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const char *message, void *userParam)
slouken@7194
   324
{
slouken@7194
   325
    SDL_Renderer *renderer = (SDL_Renderer *) userParam;
slouken@7194
   326
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@6232
   327
slouken@7194
   328
    if (type == GL_DEBUG_TYPE_ERROR_ARB) {
slouken@7194
   329
        /* Record this error */
slouken@7194
   330
        ++data->errors;
slouken@7194
   331
        data->error_messages = SDL_realloc(data->error_messages, data->errors * sizeof(*data->error_messages));
slouken@7194
   332
        if (data->error_messages) {
slouken@7194
   333
            data->error_messages[data->errors-1] = SDL_strdup(message);
slouken@7194
   334
        }
slouken@7194
   335
    }
slouken@7194
   336
slouken@7194
   337
    /* If there's another error callback, pass it along, otherwise log it */
slouken@7194
   338
    if (data->next_error_callback) {
slouken@7194
   339
        data->next_error_callback(source, type, id, severity, length, message, data->next_error_userparam);
slouken@7194
   340
    } else {
slouken@7194
   341
        if (type == GL_DEBUG_TYPE_ERROR_ARB) {
slouken@7194
   342
            SDL_LogError(SDL_LOG_CATEGORY_RENDER, "%s", message);
slouken@7194
   343
        } else {
slouken@7194
   344
            SDL_LogDebug(SDL_LOG_CATEGORY_RENDER, "%s", message);
slouken@7194
   345
        }
slouken@7194
   346
    }
slouken@7194
   347
}
slouken@7194
   348
slouken@7194
   349
static GL_FBOList *
slouken@6232
   350
GL_GetFBO(GL_RenderData *data, Uint32 w, Uint32 h)
slouken@6232
   351
{
slouken@6232
   352
    GL_FBOList *result = data->framebuffers;
slouken@6232
   353
slouken@6232
   354
    while (result && ((result->w != w) || (result->h != h))) {
slouken@6232
   355
        result = result->next;
slouken@6232
   356
    }
slouken@6232
   357
slouken@6232
   358
    if (!result) {
slouken@6232
   359
        result = SDL_malloc(sizeof(GL_FBOList));
slouken@6232
   360
        if (result) {
slouken@6232
   361
            result->w = w;
slouken@6232
   362
            result->h = h;
slouken@6232
   363
            data->glGenFramebuffersEXT(1, &result->FBO);
slouken@6232
   364
            result->next = data->framebuffers;
slouken@6232
   365
            data->framebuffers = result;
slouken@6232
   366
        }
slouken@6232
   367
    }
slouken@6232
   368
    return result;
slouken@6232
   369
}
slouken@6232
   370
slouken@1918
   371
SDL_Renderer *
slouken@1918
   372
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1918
   373
{
slouken@1918
   374
    SDL_Renderer *renderer;
slouken@1918
   375
    GL_RenderData *data;
slouken@5233
   376
    const char *hint;
slouken@1952
   377
    GLint value;
slouken@5154
   378
    Uint32 window_flags;
slouken@1918
   379
slouken@5154
   380
    window_flags = SDL_GetWindowFlags(window);
slouken@5154
   381
    if (!(window_flags & SDL_WINDOW_OPENGL)) {
slouken@5154
   382
        if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
slouken@6182
   383
            /* Uh oh, better try to put it back... */
slouken@6182
   384
            SDL_RecreateWindow(window, window_flags);
slouken@1924
   385
            return NULL;
slouken@1924
   386
        }
slouken@1918
   387
    }
slouken@1918
   388
slouken@1920
   389
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1918
   390
    if (!renderer) {
slouken@1918
   391
        SDL_OutOfMemory();
slouken@1918
   392
        return NULL;
slouken@1918
   393
    }
slouken@1918
   394
slouken@1920
   395
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   396
    if (!data) {
slouken@1918
   397
        GL_DestroyRenderer(renderer);
slouken@1918
   398
        SDL_OutOfMemory();
slouken@1918
   399
        return NULL;
slouken@1918
   400
    }
slouken@1918
   401
slouken@5147
   402
    renderer->WindowEvent = GL_WindowEvent;
urkle@7746
   403
    renderer->GetOutputSize = GL_GetOutputSize;
slouken@1918
   404
    renderer->CreateTexture = GL_CreateTexture;
slouken@1918
   405
    renderer->UpdateTexture = GL_UpdateTexture;
slouken@1918
   406
    renderer->LockTexture = GL_LockTexture;
slouken@1918
   407
    renderer->UnlockTexture = GL_UnlockTexture;
slouken@6247
   408
    renderer->SetRenderTarget = GL_SetRenderTarget;
slouken@5297
   409
    renderer->UpdateViewport = GL_UpdateViewport;
slouken@7141
   410
    renderer->UpdateClipRect = GL_UpdateClipRect;
slouken@3596
   411
    renderer->RenderClear = GL_RenderClear;
slouken@3596
   412
    renderer->RenderDrawPoints = GL_RenderDrawPoints;
slouken@3596
   413
    renderer->RenderDrawLines = GL_RenderDrawLines;
slouken@3596
   414
    renderer->RenderFillRects = GL_RenderFillRects;
slouken@1918
   415
    renderer->RenderCopy = GL_RenderCopy;
gabomdq@6320
   416
    renderer->RenderCopyEx = GL_RenderCopyEx;
slouken@3431
   417
    renderer->RenderReadPixels = GL_RenderReadPixels;
slouken@1918
   418
    renderer->RenderPresent = GL_RenderPresent;
slouken@1918
   419
    renderer->DestroyTexture = GL_DestroyTexture;
slouken@1918
   420
    renderer->DestroyRenderer = GL_DestroyRenderer;
gabomdq@6414
   421
    renderer->GL_BindTexture = GL_BindTexture;
gabomdq@6414
   422
    renderer->GL_UnbindTexture = GL_UnbindTexture;
slouken@1918
   423
    renderer->info = GL_RenderDriver.info;
slouken@6246
   424
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@1918
   425
    renderer->driverdata = data;
slouken@6171
   426
    renderer->window = window;
slouken@1918
   427
slouken@3685
   428
    data->context = SDL_GL_CreateContext(window);
slouken@1918
   429
    if (!data->context) {
slouken@1918
   430
        GL_DestroyRenderer(renderer);
slouken@1918
   431
        return NULL;
slouken@1918
   432
    }
slouken@3685
   433
    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
slouken@1918
   434
        GL_DestroyRenderer(renderer);
slouken@1918
   435
        return NULL;
slouken@1918
   436
    }
slouken@5204
   437
slouken@5204
   438
    if (GL_LoadFunctions(data) < 0) {
slouken@5204
   439
        GL_DestroyRenderer(renderer);
slouken@5204
   440
        return NULL;
slouken@5204
   441
    }
slouken@5204
   442
slouken@2246
   443
#ifdef __MACOSX__
slouken@2246
   444
    /* Enable multi-threaded rendering */
slouken@2246
   445
    /* Disabled until Ryan finishes his VBO/PBO code...
bob@2295
   446
       CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
bob@2295
   447
     */
slouken@2246
   448
#endif
slouken@2246
   449
slouken@1965
   450
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1918
   451
        SDL_GL_SetSwapInterval(1);
slouken@1918
   452
    } else {
slouken@1918
   453
        SDL_GL_SetSwapInterval(0);
slouken@1918
   454
    }
slouken@1918
   455
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@1965
   456
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1918
   457
    }
slouken@1918
   458
slouken@7194
   459
    /* Check for debug output support */
slouken@7198
   460
    if (SDL_GL_GetAttribute(SDL_GL_CONTEXT_FLAGS, &value) == 0 &&
slouken@7198
   461
        (value & SDL_GL_CONTEXT_DEBUG_FLAG)) {
slouken@7198
   462
        data->debug_enabled = SDL_TRUE;
slouken@7198
   463
    }
slouken@7198
   464
    if (data->debug_enabled && SDL_GL_ExtensionSupported("GL_ARB_debug_output")) {
slouken@7194
   465
        PFNGLDEBUGMESSAGECALLBACKARBPROC glDebugMessageCallbackARBFunc = (PFNGLDEBUGMESSAGECALLBACKARBPROC) SDL_GL_GetProcAddress("glDebugMessageCallbackARB");
slouken@7194
   466
slouken@7194
   467
        data->GL_ARB_debug_output_supported = SDL_TRUE;
slouken@7194
   468
        data->glGetPointerv(GL_DEBUG_CALLBACK_FUNCTION_ARB, (GLvoid **)&data->next_error_callback);
slouken@7194
   469
        data->glGetPointerv(GL_DEBUG_CALLBACK_USER_PARAM_ARB, &data->next_error_userparam);
slouken@7194
   470
        glDebugMessageCallbackARBFunc(GL_HandleDebugMessage, renderer);
slouken@7198
   471
slouken@7198
   472
        /* Make sure our callback is called when errors actually happen */
slouken@7198
   473
        data->glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
slouken@7194
   474
    }
slouken@7194
   475
slouken@1926
   476
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
slouken@1926
   477
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
slouken@1926
   478
        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
slouken@6449
   479
        data->glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_ARB, &value);
slouken@6449
   480
        renderer->info.max_texture_width = value;
slouken@6449
   481
        renderer->info.max_texture_height = value;
slouken@6449
   482
    } else {
slouken@6449
   483
        data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@6449
   484
        renderer->info.max_texture_width = value;
slouken@6449
   485
        renderer->info.max_texture_height = value;
slouken@1926
   486
    }
slouken@1920
   487
slouken@5228
   488
    /* Check for multitexture support */
slouken@5228
   489
    if (SDL_GL_ExtensionSupported("GL_ARB_multitexture")) {
slouken@5228
   490
        data->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glActiveTextureARB");
slouken@5228
   491
        if (data->glActiveTextureARB) {
slouken@5228
   492
            data->GL_ARB_multitexture_supported = SDL_TRUE;
slouken@5228
   493
            data->glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &data->num_texture_units);
slouken@5228
   494
        }
slouken@5228
   495
    }
slouken@5228
   496
slouken@5228
   497
    /* Check for shader support */
slouken@5233
   498
    hint = SDL_GetHint(SDL_HINT_RENDER_OPENGL_SHADERS);
slouken@5233
   499
    if (!hint || *hint != '0') {
slouken@5233
   500
        data->shaders = GL_CreateShaderContext();
slouken@5233
   501
    }
slouken@5233
   502
    SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, "OpenGL shaders: %s",
slouken@5233
   503
                data->shaders ? "ENABLED" : "DISABLED");
slouken@5228
   504
slouken@5228
   505
    /* We support YV12 textures using 3 textures and a shader */
slouken@5228
   506
    if (data->shaders && data->num_texture_units >= 3) {
slouken@5228
   507
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_YV12;
slouken@5264
   508
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_IYUV;
slouken@5228
   509
    }
slouken@7191
   510
slouken@6232
   511
    if (SDL_GL_ExtensionSupported("GL_EXT_framebuffer_object")) {
slouken@6232
   512
        data->GL_EXT_framebuffer_object_supported = SDL_TRUE;
slouken@6232
   513
        data->glGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC)
slouken@6232
   514
            SDL_GL_GetProcAddress("glGenFramebuffersEXT");
slouken@6232
   515
        data->glDeleteFramebuffersEXT = (PFNGLDELETEFRAMEBUFFERSEXTPROC)
slouken@6232
   516
            SDL_GL_GetProcAddress("glDeleteFramebuffersEXT");
slouken@6232
   517
        data->glFramebufferTexture2DEXT = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)
slouken@6232
   518
            SDL_GL_GetProcAddress("glFramebufferTexture2DEXT");
slouken@6232
   519
        data->glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)
slouken@6232
   520
            SDL_GL_GetProcAddress("glBindFramebufferEXT");
slouken@6246
   521
        data->glCheckFramebufferStatusEXT = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)
slouken@6232
   522
            SDL_GL_GetProcAddress("glCheckFramebufferStatusEXT");
slouken@6246
   523
        renderer->info.flags |= SDL_RENDERER_TARGETTEXTURE;
slouken@6232
   524
    }
slouken@6232
   525
    data->framebuffers = NULL;
slouken@5228
   526
slouken@1918
   527
    /* Set up parameters for rendering */
slouken@5355
   528
    GL_ResetState(renderer);
slouken@1918
   529
slouken@1918
   530
    return renderer;
slouken@1918
   531
}
slouken@1918
   532
slouken@5147
   533
static void
slouken@5147
   534
GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1970
   535
{
slouken@6260
   536
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED ||
slouken@6260
   537
        event->event == SDL_WINDOWEVENT_SHOWN ||
slouken@6260
   538
        event->event == SDL_WINDOWEVENT_HIDDEN) {
slouken@5147
   539
        /* Rebind the context to the window area and update matrices */
slouken@5147
   540
        SDL_CurrentContext = NULL;
slouken@5147
   541
    }
slouken@1923
   542
}
slouken@1923
   543
urkle@7746
   544
static int
urkle@7746
   545
GL_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
urkle@7746
   546
{
urkle@7746
   547
    SDL_GL_GetDrawableSize(renderer->window, w, h);
urkle@7746
   548
urkle@7746
   549
    return 0;
urkle@7746
   550
}
urkle@7746
   551
slouken@7194
   552
SDL_FORCE_INLINE int
slouken@1922
   553
power_of_2(int input)
slouken@1922
   554
{
slouken@1922
   555
    int value = 1;
slouken@1922
   556
slouken@1922
   557
    while (value < input) {
slouken@1922
   558
        value <<= 1;
slouken@1922
   559
    }
slouken@1922
   560
    return value;
slouken@1922
   561
}
slouken@1922
   562
slouken@7194
   563
SDL_FORCE_INLINE SDL_bool
slouken@3433
   564
convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
slouken@3433
   565
               GLint* internalFormat, GLenum* format, GLenum* type)
slouken@3433
   566
{
slouken@3433
   567
    switch (pixel_format) {
slouken@3433
   568
    case SDL_PIXELFORMAT_ARGB8888:
slouken@3433
   569
        *internalFormat = GL_RGBA8;
slouken@3433
   570
        *format = GL_BGRA;
slouken@5156
   571
        *type = GL_UNSIGNED_INT_8_8_8_8_REV;
slouken@3433
   572
        break;
slouken@5264
   573
    case SDL_PIXELFORMAT_YV12:
slouken@5264
   574
    case SDL_PIXELFORMAT_IYUV:
slouken@5264
   575
        *internalFormat = GL_LUMINANCE;
slouken@5264
   576
        *format = GL_LUMINANCE;
slouken@5264
   577
        *type = GL_UNSIGNED_BYTE;
slouken@5264
   578
        break;
slouken@3433
   579
    default:
slouken@3433
   580
        return SDL_FALSE;
slouken@3433
   581
    }
slouken@3433
   582
    return SDL_TRUE;
slouken@3433
   583
}
icculus@2835
   584
slouken@5484
   585
static GLenum
slouken@5484
   586
GetScaleQuality(void)
slouken@5484
   587
{
slouken@5484
   588
    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
slouken@5484
   589
slouken@5484
   590
    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
slouken@5484
   591
        return GL_NEAREST;
slouken@5484
   592
    } else {
slouken@5484
   593
        return GL_LINEAR;
slouken@5484
   594
    }
slouken@5484
   595
}
slouken@5484
   596
slouken@1918
   597
static int
slouken@1918
   598
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   599
{
slouken@1918
   600
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   601
    GL_TextureData *data;
slouken@1920
   602
    GLint internalFormat;
slouken@1920
   603
    GLenum format, type;
slouken@1922
   604
    int texture_w, texture_h;
slouken@5503
   605
    GLenum scaleMode;
slouken@1918
   606
slouken@5147
   607
    GL_ActivateRenderer(renderer);
slouken@5147
   608
slouken@3433
   609
    if (!convert_format(renderdata, texture->format, &internalFormat,
slouken@3433
   610
                        &format, &type)) {
icculus@7037
   611
        return SDL_SetError("Texture format %s not supported by OpenGL",
icculus@7037
   612
                            SDL_GetPixelFormatName(texture->format));
slouken@1920
   613
    }
slouken@1920
   614
slouken@1920
   615
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   616
    if (!data) {
icculus@7037
   617
        return SDL_OutOfMemory();
slouken@1918
   618
    }
slouken@1918
   619
slouken@2222
   620
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5264
   621
        size_t size;
slouken@5156
   622
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
slouken@5264
   623
        size = texture->h * data->pitch;
slouken@5264
   624
        if (texture->format == SDL_PIXELFORMAT_YV12 ||
slouken@5264
   625
            texture->format == SDL_PIXELFORMAT_IYUV) {
slouken@5264
   626
            /* Need to add size for the U and V planes */
slouken@5264
   627
            size += (2 * (texture->h * data->pitch) / 4);
slouken@5264
   628
        }
slouken@5402
   629
        data->pixels = SDL_calloc(1, size);
slouken@2222
   630
        if (!data->pixels) {
slouken@2222
   631
            SDL_free(data);
icculus@7037
   632
            return SDL_OutOfMemory();
slouken@2222
   633
        }
slouken@2222
   634
    }
slouken@2222
   635
slouken@1918
   636
    texture->driverdata = data;
slouken@7191
   637
slouken@6232
   638
    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
slouken@6232
   639
        data->fbo = GL_GetFBO(renderdata, texture->w, texture->h);
slouken@6232
   640
    } else {
slouken@6232
   641
        data->fbo = NULL;
slouken@6232
   642
    }
slouken@1918
   643
slouken@6494
   644
    GL_CheckError("", renderer);
slouken@1927
   645
    renderdata->glGenTextures(1, &data->texture);
slouken@7624
   646
    if (GL_CheckError("glGenTexures()", renderer) < 0) {
slouken@7624
   647
        SDL_free(data);
slouken@7624
   648
        return -1;
slouken@7624
   649
    }
slouken@6232
   650
    if ((renderdata->GL_ARB_texture_rectangle_supported)
gabomdq@7678
   651
        /* && texture->access != SDL_TEXTUREACCESS_TARGET */){
slouken@1926
   652
        data->type = GL_TEXTURE_RECTANGLE_ARB;
slouken@1926
   653
        texture_w = texture->w;
slouken@1926
   654
        texture_h = texture->h;
icculus@2835
   655
        data->texw = (GLfloat) texture_w;
icculus@2835
   656
        data->texh = (GLfloat) texture_h;
slouken@1926
   657
    } else {
slouken@1926
   658
        data->type = GL_TEXTURE_2D;
slouken@1926
   659
        texture_w = power_of_2(texture->w);
slouken@1926
   660
        texture_h = power_of_2(texture->h);
icculus@2835
   661
        data->texw = (GLfloat) (texture->w) / texture_w;
slouken@1926
   662
        data->texh = (GLfloat) texture->h / texture_h;
slouken@1926
   663
    }
icculus@2835
   664
slouken@1920
   665
    data->format = format;
slouken@1920
   666
    data->formattype = type;
slouken@5503
   667
    scaleMode = GetScaleQuality();
slouken@2884
   668
    renderdata->glEnable(data->type);
slouken@1927
   669
    renderdata->glBindTexture(data->type, data->texture);
slouken@5503
   670
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER, scaleMode);
slouken@5503
   671
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER, scaleMode);
slouken@6207
   672
    /* According to the spec, CLAMP_TO_EDGE is the default for TEXTURE_RECTANGLE
slouken@6207
   673
       and setting it causes an INVALID_ENUM error in the latest NVidia drivers.
slouken@6207
   674
    */
slouken@6207
   675
    if (data->type != GL_TEXTURE_RECTANGLE_ARB) {
slouken@6207
   676
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@6207
   677
                                    GL_CLAMP_TO_EDGE);
slouken@6207
   678
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@6207
   679
                                    GL_CLAMP_TO_EDGE);
slouken@6207
   680
    }
slouken@2840
   681
#ifdef __MACOSX__
slouken@2230
   682
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
slouken@2230
   683
#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
slouken@2230
   684
#endif
slouken@2230
   685
#ifndef STORAGE_CACHED_APPLE
slouken@2230
   686
#define STORAGE_CACHED_APPLE                0x85BE
slouken@2230
   687
#endif
slouken@2230
   688
#ifndef STORAGE_SHARED_APPLE
slouken@2230
   689
#define STORAGE_SHARED_APPLE                0x85BF
slouken@2230
   690
#endif
slouken@2230
   691
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@2230
   692
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   693
                                    GL_STORAGE_SHARED_APPLE);
slouken@2230
   694
    } else {
slouken@2230
   695
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   696
                                    GL_STORAGE_CACHED_APPLE);
slouken@2230
   697
    }
bob@2295
   698
    if (texture->access == SDL_TEXTUREACCESS_STREAMING
slouken@5397
   699
        && texture->format == SDL_PIXELFORMAT_ARGB8888
slouken@5397
   700
        && (texture->w % 8) == 0) {
slouken@2230
   701
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
slouken@5397
   702
        renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@5397
   703
        renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@5397
   704
                          (data->pitch / SDL_BYTESPERPIXEL(texture->format)));
slouken@2230
   705
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   706
                                 texture_h, 0, format, type, data->pixels);
slouken@5397
   707
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
slouken@5156
   708
    }
slouken@5156
   709
    else
slouken@2809
   710
#endif
slouken@2230
   711
    {
slouken@2230
   712
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   713
                                 texture_h, 0, format, type, NULL);
slouken@2230
   714
    }
slouken@3041
   715
    renderdata->glDisable(data->type);
slouken@7194
   716
    if (GL_CheckError("glTexImage2D()", renderer) < 0) {
slouken@1924
   717
        return -1;
slouken@1924
   718
    }
slouken@5264
   719
slouken@5264
   720
    if (texture->format == SDL_PIXELFORMAT_YV12 ||
slouken@5264
   721
        texture->format == SDL_PIXELFORMAT_IYUV) {
slouken@5264
   722
        data->yuv = SDL_TRUE;
slouken@5264
   723
slouken@5264
   724
        renderdata->glGenTextures(1, &data->utexture);
slouken@5264
   725
        renderdata->glGenTextures(1, &data->vtexture);
slouken@5264
   726
        renderdata->glEnable(data->type);
slouken@5264
   727
slouken@5264
   728
        renderdata->glBindTexture(data->type, data->utexture);
slouken@5503
   729
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@5503
   730
                                    scaleMode);
slouken@5503
   731
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@5503
   732
                                    scaleMode);
slouken@5264
   733
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@5264
   734
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   735
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@5264
   736
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   737
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w/2,
slouken@5264
   738
                                 texture_h/2, 0, format, type, NULL);
slouken@5264
   739
slouken@5264
   740
        renderdata->glBindTexture(data->type, data->vtexture);
slouken@5503
   741
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@5503
   742
                                    scaleMode);
slouken@5503
   743
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@5503
   744
                                    scaleMode);
slouken@5264
   745
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@5264
   746
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   747
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@5264
   748
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   749
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w/2,
slouken@5264
   750
                                 texture_h/2, 0, format, type, NULL);
slouken@5264
   751
slouken@5264
   752
        renderdata->glDisable(data->type);
slouken@5264
   753
    }
slouken@6494
   754
slouken@7194
   755
    return GL_CheckError("", renderer);
slouken@1918
   756
}
slouken@1918
   757
slouken@1918
   758
static int
slouken@1918
   759
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   760
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
   761
{
slouken@1927
   762
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   763
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   764
slouken@5147
   765
    GL_ActivateRenderer(renderer);
slouken@5147
   766
slouken@5397
   767
    renderdata->glEnable(data->type);
slouken@5397
   768
    renderdata->glBindTexture(data->type, data->texture);
slouken@5227
   769
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@5227
   770
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@5227
   771
                              (pitch / SDL_BYTESPERPIXEL(texture->format)));
slouken@1927
   772
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
slouken@1927
   773
                                rect->h, data->format, data->formattype,
slouken@1927
   774
                                pixels);
slouken@5264
   775
    if (data->yuv) {
slouken@5265
   776
        renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, (pitch / 2));
slouken@5265
   777
slouken@5264
   778
        /* Skip to the correct offset into the next texture */
slouken@6135
   779
        pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
slouken@5264
   780
        if (texture->format == SDL_PIXELFORMAT_YV12) {
slouken@5264
   781
            renderdata->glBindTexture(data->type, data->vtexture);
slouken@5264
   782
        } else {
slouken@5264
   783
            renderdata->glBindTexture(data->type, data->utexture);
slouken@5264
   784
        }
slouken@5264
   785
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@5264
   786
                                    rect->w/2, rect->h/2,
slouken@5264
   787
                                    data->format, data->formattype, pixels);
slouken@5264
   788
slouken@5264
   789
        /* Skip to the correct offset into the next texture */
slouken@6135
   790
        pixels = (const void*)((const Uint8*)pixels + (rect->h * pitch)/4);
slouken@5264
   791
        if (texture->format == SDL_PIXELFORMAT_YV12) {
slouken@5264
   792
            renderdata->glBindTexture(data->type, data->utexture);
slouken@5264
   793
        } else {
slouken@5264
   794
            renderdata->glBindTexture(data->type, data->vtexture);
slouken@5264
   795
        }
slouken@5264
   796
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@5264
   797
                                    rect->w/2, rect->h/2,
slouken@5264
   798
                                    data->format, data->formattype, pixels);
slouken@5264
   799
    }
slouken@3041
   800
    renderdata->glDisable(data->type);
slouken@7194
   801
    return GL_CheckError("glTexSubImage2D()", renderer);
slouken@1918
   802
}
slouken@1918
   803
slouken@1918
   804
static int
slouken@1918
   805
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   806
               const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1918
   807
{
slouken@1918
   808
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   809
slouken@5227
   810
    data->locked_rect = *rect;
slouken@7191
   811
    *pixels =
slouken@1920
   812
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@5156
   813
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@1920
   814
    *pitch = data->pitch;
slouken@1918
   815
    return 0;
slouken@1918
   816
}
slouken@1918
   817
slouken@1918
   818
static void
slouken@1918
   819
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   820
{
slouken@5156
   821
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@5227
   822
    const SDL_Rect *rect;
slouken@5227
   823
    void *pixels;
slouken@1918
   824
slouken@5227
   825
    rect = &data->locked_rect;
slouken@7191
   826
    pixels =
slouken@5227
   827
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@5227
   828
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5227
   829
    GL_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
slouken@1918
   830
}
slouken@1918
   831
slouken@5297
   832
static int
slouken@6247
   833
GL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@6246
   834
{
slouken@7191
   835
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@6246
   836
    GL_TextureData *texturedata;
slouken@6246
   837
    GLenum status;
slouken@6246
   838
slouken@6246
   839
    GL_ActivateRenderer(renderer);
slouken@7191
   840
slouken@6246
   841
    if (texture == NULL) {
slouken@6246
   842
        data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
slouken@6246
   843
        return 0;
slouken@6246
   844
    }
slouken@6246
   845
slouken@6246
   846
    texturedata = (GL_TextureData *) texture->driverdata;
slouken@6246
   847
    data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, texturedata->fbo->FBO);
slouken@6246
   848
    /* TODO: check if texture pixel format allows this operation */
slouken@6246
   849
    data->glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, texturedata->type, texturedata->texture, 0);
slouken@6246
   850
    /* Check FBO status */
slouken@6246
   851
    status = data->glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
slouken@6246
   852
    if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
icculus@7037
   853
        return SDL_SetError("glFramebufferTexture2DEXT() failed");
slouken@6246
   854
    }
slouken@6246
   855
    return 0;
slouken@6246
   856
}
slouken@6246
   857
slouken@6246
   858
static int
slouken@5297
   859
GL_UpdateViewport(SDL_Renderer * renderer)
slouken@5224
   860
{
slouken@5224
   861
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5224
   862
slouken@5297
   863
    if (SDL_CurrentContext != data->context) {
slouken@5297
   864
        /* We'll update the viewport after we rebind the context */
slouken@5297
   865
        return 0;
slouken@5297
   866
    }
slouken@5224
   867
slouken@5297
   868
    data->glViewport(renderer->viewport.x, renderer->viewport.y,
slouken@5297
   869
                     renderer->viewport.w, renderer->viewport.h);
slouken@5224
   870
slouken@5297
   871
    data->glMatrixMode(GL_PROJECTION);
slouken@5297
   872
    data->glLoadIdentity();
slouken@7239
   873
    if (renderer->viewport.w && renderer->viewport.h) {
slouken@7239
   874
        if (renderer->target) {
slouken@7239
   875
            data->glOrtho((GLdouble) 0,
slouken@7239
   876
                          (GLdouble) renderer->viewport.w,
slouken@7239
   877
                          (GLdouble) 0,
slouken@7239
   878
                          (GLdouble) renderer->viewport.h,
slouken@7239
   879
                           0.0, 1.0);
slouken@7239
   880
        } else {
slouken@7239
   881
            data->glOrtho((GLdouble) 0,
slouken@7239
   882
                          (GLdouble) renderer->viewport.w,
slouken@7239
   883
                          (GLdouble) renderer->viewport.h,
slouken@7239
   884
                          (GLdouble) 0,
slouken@7239
   885
                           0.0, 1.0);
slouken@7239
   886
        }
slouken@6246
   887
    }
slouken@7194
   888
    return GL_CheckError("", renderer);
slouken@5224
   889
}
slouken@5224
   890
slouken@7141
   891
static int
slouken@7141
   892
GL_UpdateClipRect(SDL_Renderer * renderer)
slouken@7141
   893
{
slouken@7141
   894
    const SDL_Rect *rect = &renderer->clip_rect;
slouken@7141
   895
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@7141
   896
slouken@7141
   897
    if (!SDL_RectEmpty(rect)) {
slouken@7141
   898
        data->glEnable(GL_SCISSOR_TEST);
philipp@7171
   899
        data->glScissor(rect->x, renderer->viewport.h - rect->y - rect->h, rect->w, rect->h);
slouken@7141
   900
    } else {
slouken@7141
   901
        data->glDisable(GL_SCISSOR_TEST);
slouken@7141
   902
    }
slouken@7141
   903
    return 0;
slouken@7141
   904
}
slouken@7141
   905
slouken@5224
   906
static void
slouken@5355
   907
GL_SetShader(GL_RenderData * data, GL_Shader shader)
slouken@5355
   908
{
slouken@5355
   909
    if (data->shaders && shader != data->current.shader) {
slouken@5355
   910
        GL_SelectShader(data->shaders, shader);
slouken@5355
   911
        data->current.shader = shader;
slouken@5355
   912
    }
slouken@5355
   913
}
slouken@5355
   914
slouken@5355
   915
static void
slouken@5355
   916
GL_SetColor(GL_RenderData * data, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@5355
   917
{
slouken@5355
   918
    Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
slouken@5355
   919
slouken@5355
   920
    if (color != data->current.color) {
slouken@5355
   921
        data->glColor4f((GLfloat) r * inv255f,
slouken@5355
   922
                        (GLfloat) g * inv255f,
slouken@5355
   923
                        (GLfloat) b * inv255f,
slouken@5355
   924
                        (GLfloat) a * inv255f);
slouken@5355
   925
        data->current.color = color;
slouken@5355
   926
    }
slouken@5355
   927
}
slouken@5355
   928
slouken@5355
   929
static void
slouken@5140
   930
GL_SetBlendMode(GL_RenderData * data, int blendMode)
slouken@2936
   931
{
slouken@5355
   932
    if (blendMode != data->current.blendMode) {
slouken@2936
   933
        switch (blendMode) {
slouken@2936
   934
        case SDL_BLENDMODE_NONE:
slouken@2936
   935
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
slouken@2936
   936
            data->glDisable(GL_BLEND);
slouken@2936
   937
            break;
slouken@2936
   938
        case SDL_BLENDMODE_BLEND:
slouken@2936
   939
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
   940
            data->glEnable(GL_BLEND);
slouken@7502
   941
            data->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
slouken@2936
   942
            break;
slouken@2936
   943
        case SDL_BLENDMODE_ADD:
slouken@2936
   944
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
   945
            data->glEnable(GL_BLEND);
slouken@7502
   946
            data->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
slouken@2936
   947
            break;
slouken@5184
   948
        case SDL_BLENDMODE_MOD:
slouken@5184
   949
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@5184
   950
            data->glEnable(GL_BLEND);
slouken@7502
   951
            data->glBlendFuncSeparate(GL_ZERO, GL_SRC_COLOR, GL_ZERO, GL_ONE);
slouken@5184
   952
            break;
slouken@2936
   953
        }
slouken@5355
   954
        data->current.blendMode = blendMode;
slouken@2936
   955
    }
slouken@2936
   956
}
slouken@2936
   957
slouken@5355
   958
static void
slouken@5355
   959
GL_SetDrawingState(SDL_Renderer * renderer)
slouken@5355
   960
{
slouken@5355
   961
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5355
   962
slouken@5355
   963
    GL_ActivateRenderer(renderer);
slouken@5355
   964
slouken@5381
   965
    GL_SetColor(data, renderer->r,
slouken@5381
   966
                      renderer->g,
slouken@5381
   967
                      renderer->b,
slouken@5381
   968
                      renderer->a);
slouken@5355
   969
slouken@5355
   970
    GL_SetBlendMode(data, renderer->blendMode);
slouken@5355
   971
slouken@5355
   972
    GL_SetShader(data, SHADER_SOLID);
slouken@5355
   973
}
slouken@5355
   974
slouken@1918
   975
static int
slouken@3596
   976
GL_RenderClear(SDL_Renderer * renderer)
slouken@3596
   977
{
slouken@3596
   978
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3596
   979
slouken@5147
   980
    GL_ActivateRenderer(renderer);
slouken@5147
   981
slouken@3596
   982
    data->glClearColor((GLfloat) renderer->r * inv255f,
slouken@3596
   983
                       (GLfloat) renderer->g * inv255f,
slouken@3596
   984
                       (GLfloat) renderer->b * inv255f,
slouken@3596
   985
                       (GLfloat) renderer->a * inv255f);
slouken@3596
   986
slouken@3596
   987
    data->glClear(GL_COLOR_BUFFER_BIT);
slouken@3596
   988
slouken@3596
   989
    return 0;
slouken@3596
   990
}
slouken@3596
   991
slouken@3596
   992
static int
slouken@6528
   993
GL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_FPoint * points,
slouken@3596
   994
                    int count)
slouken@2884
   995
{
slouken@2884
   996
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   997
    int i;
slouken@2884
   998
slouken@5355
   999
    GL_SetDrawingState(renderer);
slouken@2884
  1000
slouken@2901
  1001
    data->glBegin(GL_POINTS);
slouken@3536
  1002
    for (i = 0; i < count; ++i) {
slouken@3536
  1003
        data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
  1004
    }
slouken@2901
  1005
    data->glEnd();
slouken@2901
  1006
slouken@2901
  1007
    return 0;
slouken@2901
  1008
}
slouken@2901
  1009
slouken@2901
  1010
static int
slouken@6528
  1011
GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_FPoint * points,
slouken@3596
  1012
                   int count)
slouken@2901
  1013
{
slouken@2901
  1014
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
  1015
    int i;
slouken@2901
  1016
slouken@5355
  1017
    GL_SetDrawingState(renderer);
slouken@2901
  1018
slouken@7191
  1019
    if (count > 2 &&
slouken@3536
  1020
        points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
slouken@3536
  1021
        data->glBegin(GL_LINE_LOOP);
slouken@3536
  1022
        /* GL_LINE_LOOP takes care of the final segment */
slouken@3536
  1023
        --count;
slouken@3536
  1024
        for (i = 0; i < count; ++i) {
slouken@3536
  1025
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
  1026
        }
slouken@3536
  1027
        data->glEnd();
slouken@3536
  1028
    } else {
slouken@5086
  1029
#if defined(__APPLE__) || defined(__WIN32__)
aschiffler@4910
  1030
#else
slouken@4905
  1031
        int x1, y1, x2, y2;
aschiffler@4910
  1032
#endif
slouken@4905
  1033
slouken@3536
  1034
        data->glBegin(GL_LINE_STRIP);
slouken@3536
  1035
        for (i = 0; i < count; ++i) {
slouken@3536
  1036
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
  1037
        }
slouken@3536
  1038
        data->glEnd();
slouken@3474
  1039
slouken@3536
  1040
        /* The line is half open, so we need one more point to complete it.
slouken@3536
  1041
         * http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node47.html
slouken@3536
  1042
         * If we have to, we can use vertical line and horizontal line textures
slouken@3536
  1043
         * for vertical and horizontal lines, and then create custom textures
slouken@3536
  1044
         * for diagonal lines and software render those.  It's terrible, but at
slouken@3536
  1045
         * least it would be pixel perfect.
slouken@3536
  1046
         */
slouken@3536
  1047
        data->glBegin(GL_POINTS);
slouken@5086
  1048
#if defined(__APPLE__) || defined(__WIN32__)
slouken@3536
  1049
        /* Mac OS X and Windows seem to always leave the second point open */
slouken@3536
  1050
        data->glVertex2f(0.5f + points[count-1].x, 0.5f + points[count-1].y);
slouken@3474
  1051
#else
slouken@3536
  1052
        /* Linux seems to leave the right-most or bottom-most point open */
slouken@4905
  1053
        x1 = points[0].x;
slouken@4905
  1054
        y1 = points[0].y;
slouken@4905
  1055
        x2 = points[count-1].x;
slouken@4905
  1056
        y2 = points[count-1].y;
slouken@3536
  1057
slouken@3536
  1058
        if (x1 > x2) {
slouken@3536
  1059
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@3536
  1060
        } else if (x2 > x1) {
slouken@3536
  1061
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@3536
  1062
        } else if (y1 > y2) {
slouken@3536
  1063
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@3536
  1064
        } else if (y2 > y1) {
slouken@3536
  1065
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@3536
  1066
        }
slouken@3536
  1067
#endif
slouken@3536
  1068
        data->glEnd();
slouken@3474
  1069
    }
slouken@7194
  1070
    return GL_CheckError("", renderer);
slouken@1918
  1071
}
slouken@1918
  1072
slouken@1918
  1073
static int
slouken@6528
  1074
GL_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects, int count)
slouken@2925
  1075
{
slouken@2925
  1076
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
  1077
    int i;
slouken@2925
  1078
slouken@5355
  1079
    GL_SetDrawingState(renderer);
slouken@2936
  1080
slouken@3536
  1081
    for (i = 0; i < count; ++i) {
slouken@6528
  1082
        const SDL_FRect *rect = &rects[i];
slouken@3536
  1083
slouken@6528
  1084
        data->glRectf(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
slouken@3536
  1085
    }
slouken@7194
  1086
    return GL_CheckError("", renderer);
slouken@2925
  1087
}
slouken@2925
  1088
slouken@2925
  1089
static int
slouken@1918
  1090
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
  1091
              const SDL_Rect * srcrect, const SDL_FRect * dstrect)
slouken@1918
  1092
{
slouken@1918
  1093
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1094
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@6528
  1095
    GLfloat minx, miny, maxx, maxy;
slouken@1918
  1096
    GLfloat minu, maxu, minv, maxv;
slouken@1918
  1097
slouken@5147
  1098
    GL_ActivateRenderer(renderer);
slouken@5147
  1099
slouken@5355
  1100
    data->glEnable(texturedata->type);
slouken@5355
  1101
    if (texturedata->yuv) {
slouken@5355
  1102
        data->glActiveTextureARB(GL_TEXTURE2_ARB);
slouken@5355
  1103
        data->glBindTexture(texturedata->type, texturedata->vtexture);
slouken@5355
  1104
slouken@5355
  1105
        data->glActiveTextureARB(GL_TEXTURE1_ARB);
slouken@5355
  1106
        data->glBindTexture(texturedata->type, texturedata->utexture);
slouken@5355
  1107
slouken@5355
  1108
        data->glActiveTextureARB(GL_TEXTURE0_ARB);
slouken@5355
  1109
    }
slouken@5355
  1110
    data->glBindTexture(texturedata->type, texturedata->texture);
slouken@5355
  1111
slouken@5355
  1112
    if (texture->modMode) {
slouken@5355
  1113
        GL_SetColor(data, texture->r, texture->g, texture->b, texture->a);
slouken@5355
  1114
    } else {
slouken@5355
  1115
        GL_SetColor(data, 255, 255, 255, 255);
slouken@5355
  1116
    }
slouken@5355
  1117
slouken@5355
  1118
    GL_SetBlendMode(data, texture->blendMode);
slouken@5355
  1119
slouken@5355
  1120
    if (texturedata->yuv) {
slouken@5355
  1121
        GL_SetShader(data, SHADER_YV12);
slouken@5355
  1122
    } else {
slouken@5355
  1123
        GL_SetShader(data, SHADER_RGB);
slouken@5355
  1124
    }
slouken@5355
  1125
slouken@1918
  1126
    minx = dstrect->x;
slouken@1918
  1127
    miny = dstrect->y;
slouken@1918
  1128
    maxx = dstrect->x + dstrect->w;
slouken@1918
  1129
    maxy = dstrect->y + dstrect->h;
slouken@1918
  1130
slouken@1918
  1131
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
  1132
    minu *= texturedata->texw;
slouken@1918
  1133
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
  1134
    maxu *= texturedata->texw;
slouken@1918
  1135
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
  1136
    minv *= texturedata->texh;
slouken@1918
  1137
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
  1138
    maxv *= texturedata->texh;
slouken@1918
  1139
slouken@1927
  1140
    data->glBegin(GL_TRIANGLE_STRIP);
slouken@1927
  1141
    data->glTexCoord2f(minu, minv);
slouken@6528
  1142
    data->glVertex2f(minx, miny);
slouken@1927
  1143
    data->glTexCoord2f(maxu, minv);
slouken@6528
  1144
    data->glVertex2f(maxx, miny);
slouken@1927
  1145
    data->glTexCoord2f(minu, maxv);
slouken@6528
  1146
    data->glVertex2f(minx, maxy);
slouken@1927
  1147
    data->glTexCoord2f(maxu, maxv);
slouken@6528
  1148
    data->glVertex2f(maxx, maxy);
slouken@1927
  1149
    data->glEnd();
slouken@1918
  1150
slouken@2884
  1151
    data->glDisable(texturedata->type);
slouken@2884
  1152
slouken@7194
  1153
    return GL_CheckError("", renderer);
slouken@1918
  1154
}
slouken@1918
  1155
slouken@3431
  1156
static int
gabomdq@6320
  1157
GL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
  1158
              const SDL_Rect * srcrect, const SDL_FRect * dstrect,
slouken@6528
  1159
              const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
gabomdq@6320
  1160
{
gabomdq@6320
  1161
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
gabomdq@6320
  1162
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
gabomdq@6320
  1163
    GLfloat minx, miny, maxx, maxy;
gabomdq@6320
  1164
    GLfloat centerx, centery;
gabomdq@6320
  1165
    GLfloat minu, maxu, minv, maxv;
slouken@7194
  1166
gabomdq@6320
  1167
    GL_ActivateRenderer(renderer);
gabomdq@6320
  1168
gabomdq@6320
  1169
    data->glEnable(texturedata->type);
gabomdq@6320
  1170
    if (texturedata->yuv) {
gabomdq@6320
  1171
        data->glActiveTextureARB(GL_TEXTURE2_ARB);
gabomdq@6320
  1172
        data->glBindTexture(texturedata->type, texturedata->vtexture);
gabomdq@6320
  1173
gabomdq@6320
  1174
        data->glActiveTextureARB(GL_TEXTURE1_ARB);
gabomdq@6320
  1175
        data->glBindTexture(texturedata->type, texturedata->utexture);
gabomdq@6320
  1176
gabomdq@6320
  1177
        data->glActiveTextureARB(GL_TEXTURE0_ARB);
gabomdq@6320
  1178
    }
gabomdq@6320
  1179
    data->glBindTexture(texturedata->type, texturedata->texture);
gabomdq@6320
  1180
gabomdq@6320
  1181
    if (texture->modMode) {
gabomdq@6320
  1182
        GL_SetColor(data, texture->r, texture->g, texture->b, texture->a);
gabomdq@6320
  1183
    } else {
gabomdq@6320
  1184
        GL_SetColor(data, 255, 255, 255, 255);
gabomdq@6320
  1185
    }
gabomdq@6320
  1186
gabomdq@6320
  1187
    GL_SetBlendMode(data, texture->blendMode);
gabomdq@6320
  1188
gabomdq@6320
  1189
    if (texturedata->yuv) {
gabomdq@6320
  1190
        GL_SetShader(data, SHADER_YV12);
gabomdq@6320
  1191
    } else {
gabomdq@6320
  1192
        GL_SetShader(data, SHADER_RGB);
gabomdq@6320
  1193
    }
gabomdq@6320
  1194
slouken@6528
  1195
    centerx = center->x;
slouken@6528
  1196
    centery = center->y;
gabomdq@6320
  1197
gabomdq@6320
  1198
    if (flip & SDL_FLIP_HORIZONTAL) {
slouken@6528
  1199
        minx =  dstrect->w - centerx;
gabomdq@6320
  1200
        maxx = -centerx;
gabomdq@6320
  1201
    }
gabomdq@6320
  1202
    else {
gabomdq@6320
  1203
        minx = -centerx;
slouken@6528
  1204
        maxx =  dstrect->w - centerx;
gabomdq@6320
  1205
    }
gabomdq@6320
  1206
gabomdq@6320
  1207
    if (flip & SDL_FLIP_VERTICAL) {
slouken@6528
  1208
        miny =  dstrect->h - centery;
gabomdq@6320
  1209
        maxy = -centery;
gabomdq@6320
  1210
    }
gabomdq@6320
  1211
    else {
gabomdq@6320
  1212
        miny = -centery;
slouken@6528
  1213
        maxy =  dstrect->h - centery;
gabomdq@6320
  1214
    }
gabomdq@6320
  1215
gabomdq@6320
  1216
    minu = (GLfloat) srcrect->x / texture->w;
gabomdq@6320
  1217
    minu *= texturedata->texw;
gabomdq@6320
  1218
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
gabomdq@6320
  1219
    maxu *= texturedata->texw;
gabomdq@6320
  1220
    minv = (GLfloat) srcrect->y / texture->h;
gabomdq@6320
  1221
    minv *= texturedata->texh;
gabomdq@6320
  1222
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
gabomdq@6320
  1223
    maxv *= texturedata->texh;
gabomdq@6320
  1224
slouken@7191
  1225
    /* Translate to flip, rotate, translate to position */
gabomdq@6320
  1226
    data->glPushMatrix();
slouken@7191
  1227
    data->glTranslatef((GLfloat)dstrect->x + centerx, (GLfloat)dstrect->y + centery, (GLfloat)0.0);
gabomdq@6320
  1228
    data->glRotated(angle, (GLdouble)0.0, (GLdouble)0.0, (GLdouble)1.0);
slouken@7191
  1229
gabomdq@6320
  1230
    data->glBegin(GL_TRIANGLE_STRIP);
gabomdq@6320
  1231
    data->glTexCoord2f(minu, minv);
gabomdq@6320
  1232
    data->glVertex2f(minx, miny);
gabomdq@6320
  1233
    data->glTexCoord2f(maxu, minv);
gabomdq@6320
  1234
    data->glVertex2f(maxx, miny);
gabomdq@6320
  1235
    data->glTexCoord2f(minu, maxv);
gabomdq@6320
  1236
    data->glVertex2f(minx, maxy);
gabomdq@6320
  1237
    data->glTexCoord2f(maxu, maxv);
gabomdq@6320
  1238
    data->glVertex2f(maxx, maxy);
gabomdq@6320
  1239
    data->glEnd();
gabomdq@6320
  1240
    data->glPopMatrix();
slouken@7191
  1241
gabomdq@6320
  1242
    data->glDisable(texturedata->type);
gabomdq@6320
  1243
slouken@7194
  1244
    return GL_CheckError("", renderer);
gabomdq@6320
  1245
}
gabomdq@6320
  1246
gabomdq@6320
  1247
static int
slouken@3431
  1248
GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
  1249
                    Uint32 pixel_format, void * pixels, int pitch)
slouken@3431
  1250
{
slouken@3433
  1251
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5465
  1252
    Uint32 temp_format = SDL_PIXELFORMAT_ARGB8888;
slouken@5465
  1253
    void *temp_pixels;
slouken@5465
  1254
    int temp_pitch;
slouken@3433
  1255
    GLint internalFormat;
slouken@3433
  1256
    GLenum format, type;
slouken@3435
  1257
    Uint8 *src, *dst, *tmp;
slouken@5154
  1258
    int w, h, length, rows;
slouken@5465
  1259
    int status;
slouken@3433
  1260
slouken@5147
  1261
    GL_ActivateRenderer(renderer);
slouken@5147
  1262
slouken@5465
  1263
    temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
slouken@5465
  1264
    temp_pixels = SDL_malloc(rect->h * temp_pitch);
slouken@5465
  1265
    if (!temp_pixels) {
icculus@7037
  1266
        return SDL_OutOfMemory();
slouken@3433
  1267
    }
slouken@3433
  1268
slouken@5465
  1269
    convert_format(data, temp_format, &internalFormat, &format, &type);
slouken@5465
  1270
slouken@7420
  1271
    SDL_GetRendererOutputSize(renderer, &w, &h);
slouken@5154
  1272
slouken@3446
  1273
    data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
slouken@3446
  1274
    data->glPixelStorei(GL_PACK_ROW_LENGTH,
slouken@5465
  1275
                        (temp_pitch / SDL_BYTESPERPIXEL(temp_format)));
slouken@3433
  1276
slouken@5154
  1277
    data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
slouken@5465
  1278
                       format, type, temp_pixels);
slouken@3435
  1279
slouken@6494
  1280
    GL_CheckError("", renderer);
slouken@6494
  1281
slouken@3435
  1282
    /* Flip the rows to be top-down */
slouken@5465
  1283
    length = rect->w * SDL_BYTESPERPIXEL(temp_format);
slouken@5465
  1284
    src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
slouken@5465
  1285
    dst = (Uint8*)temp_pixels;
slouken@3435
  1286
    tmp = SDL_stack_alloc(Uint8, length);
slouken@3435
  1287
    rows = rect->h / 2;
slouken@3435
  1288
    while (rows--) {
slouken@3435
  1289
        SDL_memcpy(tmp, dst, length);
slouken@3435
  1290
        SDL_memcpy(dst, src, length);
slouken@3435
  1291
        SDL_memcpy(src, tmp, length);
slouken@5465
  1292
        dst += temp_pitch;
slouken@5465
  1293
        src -= temp_pitch;
slouken@3435
  1294
    }
slouken@3435
  1295
    SDL_stack_free(tmp);
slouken@3440
  1296
slouken@5465
  1297
    status = SDL_ConvertPixels(rect->w, rect->h,
slouken@5465
  1298
                               temp_format, temp_pixels, temp_pitch,
slouken@5465
  1299
                               pixel_format, pixels, pitch);
slouken@5465
  1300
    SDL_free(temp_pixels);
slouken@5465
  1301
slouken@5465
  1302
    return status;
slouken@3431
  1303
}
slouken@3431
  1304
slouken@1918
  1305
static void
slouken@1918
  1306
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
  1307
{
slouken@5147
  1308
    GL_ActivateRenderer(renderer);
slouken@5147
  1309
slouken@1918
  1310
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
  1311
}
slouken@1918
  1312
slouken@1918
  1313
static void
slouken@1918
  1314
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
  1315
{
slouken@1927
  1316
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1317
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
  1318
slouken@5147
  1319
    GL_ActivateRenderer(renderer);
slouken@5147
  1320
slouken@1918
  1321
    if (!data) {
slouken@1918
  1322
        return;
slouken@1918
  1323
    }
slouken@1918
  1324
    if (data->texture) {
slouken@1927
  1325
        renderdata->glDeleteTextures(1, &data->texture);
slouken@1918
  1326
    }
slouken@5264
  1327
    if (data->yuv) {
slouken@5264
  1328
        renderdata->glDeleteTextures(1, &data->utexture);
slouken@5264
  1329
        renderdata->glDeleteTextures(1, &data->vtexture);
slouken@5264
  1330
    }
slouken@7719
  1331
    SDL_free(data->pixels);
slouken@1918
  1332
    SDL_free(data);
slouken@1918
  1333
    texture->driverdata = NULL;
slouken@1918
  1334
}
slouken@1918
  1335
slouken@1975
  1336
static void
slouken@1918
  1337
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
  1338
{
slouken@1918
  1339
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1340
slouken@1918
  1341
    if (data) {
slouken@7194
  1342
        GL_ClearErrors(renderer);
slouken@7194
  1343
        if (data->GL_ARB_debug_output_supported) {
slouken@7194
  1344
            PFNGLDEBUGMESSAGECALLBACKARBPROC glDebugMessageCallbackARBFunc = (PFNGLDEBUGMESSAGECALLBACKARBPROC) SDL_GL_GetProcAddress("glDebugMessageCallbackARB");
slouken@7194
  1345
slouken@7194
  1346
            /* Uh oh, we don't have a safe way of removing ourselves from the callback chain, if it changed after we set our callback. */
slouken@7194
  1347
            /* For now, just always replace the callback with the original one */
slouken@7194
  1348
            glDebugMessageCallbackARBFunc(data->next_error_callback, data->next_error_userparam);
slouken@7194
  1349
        }
icculus@5566
  1350
        if (data->shaders) {
icculus@5566
  1351
            GL_DestroyShaderContext(data->shaders);
icculus@5566
  1352
        }
slouken@1920
  1353
        if (data->context) {
slouken@6232
  1354
            while (data->framebuffers) {
slouken@6232
  1355
                GL_FBOList *nextnode = data->framebuffers->next;
slouken@6232
  1356
                /* delete the framebuffer object */
slouken@6232
  1357
                data->glDeleteFramebuffersEXT(1, &data->framebuffers->FBO);
slouken@6494
  1358
                GL_CheckError("", renderer);
slouken@6232
  1359
                SDL_free(data->framebuffers);
slouken@6232
  1360
                data->framebuffers = nextnode;
jorgen@7091
  1361
            }
slouken@1920
  1362
            SDL_GL_DeleteContext(data->context);
slouken@1918
  1363
        }
slouken@1918
  1364
        SDL_free(data);
slouken@1918
  1365
    }
slouken@1918
  1366
    SDL_free(renderer);
slouken@1918
  1367
}
slouken@1918
  1368
slouken@7194
  1369
static int
slouken@7194
  1370
GL_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh)
slouken@7194
  1371
{
gabomdq@6414
  1372
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
gabomdq@6414
  1373
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
gabomdq@6414
  1374
    GL_ActivateRenderer(renderer);
gabomdq@6414
  1375
gabomdq@6414
  1376
    data->glEnable(texturedata->type);
gabomdq@6414
  1377
    if (texturedata->yuv) {
gabomdq@6414
  1378
        data->glActiveTextureARB(GL_TEXTURE2_ARB);
gabomdq@6414
  1379
        data->glBindTexture(texturedata->type, texturedata->vtexture);
gabomdq@6414
  1380
gabomdq@6414
  1381
        data->glActiveTextureARB(GL_TEXTURE1_ARB);
gabomdq@6414
  1382
        data->glBindTexture(texturedata->type, texturedata->utexture);
gabomdq@6414
  1383
gabomdq@6414
  1384
        data->glActiveTextureARB(GL_TEXTURE0_ARB);
gabomdq@6414
  1385
    }
gabomdq@6414
  1386
    data->glBindTexture(texturedata->type, texturedata->texture);
gabomdq@6414
  1387
gabomdq@6414
  1388
    if(texw) *texw = (float)texturedata->texw;
gabomdq@6414
  1389
    if(texh) *texh = (float)texturedata->texh;
gabomdq@6414
  1390
gabomdq@6414
  1391
    return 0;
gabomdq@6414
  1392
}
gabomdq@6414
  1393
slouken@7194
  1394
static int
slouken@7194
  1395
GL_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture)
slouken@7194
  1396
{
gabomdq@6414
  1397
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
gabomdq@6414
  1398
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
gabomdq@6414
  1399
    GL_ActivateRenderer(renderer);
gabomdq@6414
  1400
gabomdq@6414
  1401
    if (texturedata->yuv) {
gabomdq@6414
  1402
        data->glActiveTextureARB(GL_TEXTURE2_ARB);
gabomdq@6414
  1403
        data->glDisable(texturedata->type);
gabomdq@6414
  1404
gabomdq@6414
  1405
        data->glActiveTextureARB(GL_TEXTURE1_ARB);
gabomdq@6414
  1406
        data->glDisable(texturedata->type);
gabomdq@6414
  1407
gabomdq@6414
  1408
        data->glActiveTextureARB(GL_TEXTURE0_ARB);
gabomdq@6414
  1409
    }
slouken@7191
  1410
gabomdq@6414
  1411
    data->glDisable(texturedata->type);
gabomdq@6414
  1412
gabomdq@6414
  1413
    return 0;
gabomdq@6414
  1414
}
gabomdq@6414
  1415
slouken@5226
  1416
#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
slouken@1918
  1417
slouken@1918
  1418
/* vi: set ts=4 sw=4 expandtab: */