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