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