src/render/opengl/SDL_render_gl.c
author Sam Lantinga
Sun, 19 May 2013 22:28:10 -0700
changeset 7194 987fb567bba9
parent 7191 75360622e65f
child 7196 fe0848afab9f
permissions -rw-r--r--
Fixed bug 1837 - Use error extension instead of glGetError()

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