src/render/opengl/SDL_render_gl.c
author Cameron Gutman <cameron.gutman@gmail.com>
Wed, 31 Oct 2018 20:17:53 -0700
changeset 12377 84618d571795
parent 11958 d7582d7286aa
child 12381 dc9108cd4340
permissions -rw-r--r--
Fix crash when GL_LoadFunctions()/GLES2_LoadFunctions() fails
https://bugzilla.libsdl.org/show_bug.cgi?id=4350

We can't safely call GL_DestroyRenderer() until GL_LoadFunctions()
succeeds because we will be missing functions that we try to use
when activating the renderer for destruction if we have an GL context.
slouken@1918
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 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
*/
icculus@8093
    21
#include "../../SDL_internal.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"
icculus@8650
    27
#include "SDL_assert.h"
slouken@1920
    28
#include "SDL_opengl.h"
slouken@5154
    29
#include "../SDL_sysrender.h"
slouken@5228
    30
#include "SDL_shaders_gl.h"
slouken@1918
    31
slouken@2246
    32
#ifdef __MACOSX__
slouken@2246
    33
#include <OpenGL/OpenGL.h>
slouken@2246
    34
#endif
slouken@2246
    35
gabomdq@8264
    36
/* To prevent unnecessary window recreation, 
gabomdq@8264
    37
 * these should match the defaults selected in SDL_GL_ResetAttributes 
gabomdq@8264
    38
 */
gabomdq@8264
    39
gabomdq@8257
    40
#define RENDERER_CONTEXT_MAJOR 2
gabomdq@8264
    41
#define RENDERER_CONTEXT_MINOR 1
slouken@2778
    42
slouken@1918
    43
/* OpenGL renderer implementation */
slouken@1918
    44
slouken@2230
    45
/* Details on optimizing the texture path on Mac OS X:
slouken@5203
    46
   http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/opengl_texturedata.html
slouken@2230
    47
*/
slouken@2230
    48
slouken@5154
    49
/* Used to re-create the window with OpenGL capability */
slouken@5154
    50
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
icculus@2835
    51
slouken@1985
    52
static const float inv255f = 1.0f / 255.0f;
slouken@1985
    53
slouken@1918
    54
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@5147
    55
static void GL_WindowEvent(SDL_Renderer * renderer,
slouken@5147
    56
                           const SDL_WindowEvent *event);
urkle@7746
    57
static int GL_GetOutputSize(SDL_Renderer * renderer, int *w, int *h);
slouken@11282
    58
static SDL_bool GL_SupportsBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode);
slouken@1918
    59
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    60
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    61
                            const SDL_Rect * rect, const void *pixels,
slouken@1918
    62
                            int pitch);
slouken@7759
    63
static int GL_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@7759
    64
                               const SDL_Rect * rect,
slouken@7759
    65
                               const Uint8 *Yplane, int Ypitch,
slouken@7759
    66
                               const Uint8 *Uplane, int Upitch,
slouken@7759
    67
                               const Uint8 *Vplane, int Vpitch);
slouken@1918
    68
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
    69
                          const SDL_Rect * rect, void **pixels, int *pitch);
slouken@1918
    70
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@6247
    71
static int GL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@5297
    72
static int GL_UpdateViewport(SDL_Renderer * renderer);
slouken@7141
    73
static int GL_UpdateClipRect(SDL_Renderer * renderer);
slouken@3596
    74
static int GL_RenderClear(SDL_Renderer * renderer);
slouken@3596
    75
static int GL_RenderDrawPoints(SDL_Renderer * renderer,
slouken@6528
    76
                               const SDL_FPoint * points, int count);
slouken@3596
    77
static int GL_RenderDrawLines(SDL_Renderer * renderer,
slouken@6528
    78
                              const SDL_FPoint * points, int count);
slouken@3596
    79
static int GL_RenderFillRects(SDL_Renderer * renderer,
slouken@6528
    80
                              const SDL_FRect * rects, int count);
slouken@1918
    81
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
    82
                         const SDL_Rect * srcrect, const SDL_FRect * dstrect);
gabomdq@6320
    83
static int GL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
    84
                         const SDL_Rect * srcrect, const SDL_FRect * dstrect,
slouken@6528
    85
                         const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip);
slouken@3431
    86
static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
    87
                               Uint32 pixel_format, void * pixels, int pitch);
slouken@1918
    88
static void GL_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    89
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    90
static void GL_DestroyRenderer(SDL_Renderer * renderer);
gabomdq@6414
    91
static int GL_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh);
gabomdq@6414
    92
static int GL_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture);
slouken@1918
    93
slouken@1918
    94
SDL_RenderDriver GL_RenderDriver = {
slouken@1918
    95
    GL_CreateRenderer,
slouken@1918
    96
    {
slouken@1918
    97
     "opengl",
slouken@6237
    98
     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
slouken@5156
    99
     1,
slouken@5156
   100
     {SDL_PIXELFORMAT_ARGB8888},
slouken@1918
   101
     0,
slouken@1918
   102
     0}
slouken@1918
   103
};
slouken@1918
   104
slouken@6232
   105
typedef struct GL_FBOList GL_FBOList;
slouken@6232
   106
slouken@6232
   107
struct GL_FBOList
slouken@6232
   108
{
slouken@6232
   109
    Uint32 w, h;
slouken@6232
   110
    GLuint FBO;
slouken@6232
   111
    GL_FBOList *next;
slouken@6232
   112
};
slouken@6232
   113
slouken@1918
   114
typedef struct
slouken@1918
   115
{
slouken@1918
   116
    SDL_GLContext context;
slouken@7194
   117
slouken@7198
   118
    SDL_bool debug_enabled;
slouken@7194
   119
    SDL_bool GL_ARB_debug_output_supported;
slouken@7194
   120
    int errors;
slouken@7194
   121
    char **error_messages;
slouken@7194
   122
    GLDEBUGPROCARB next_error_callback;
slouken@7194
   123
    GLvoid *next_error_userparam;
slouken@7194
   124
slouken@9063
   125
    SDL_bool GL_ARB_texture_non_power_of_two_supported;
slouken@2233
   126
    SDL_bool GL_ARB_texture_rectangle_supported;
slouken@5355
   127
    struct {
slouken@5355
   128
        GL_Shader shader;
slouken@5355
   129
        Uint32 color;
slouken@11282
   130
        SDL_BlendMode blendMode;
slouken@5355
   131
    } current;
slouken@7191
   132
slouken@6232
   133
    SDL_bool GL_EXT_framebuffer_object_supported;
slouken@6232
   134
    GL_FBOList *framebuffers;
slouken@1927
   135
slouken@1927
   136
    /* OpenGL functions */
slouken@1927
   137
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
slouken@5204
   138
#include "SDL_glfuncs.h"
slouken@1927
   139
#undef SDL_PROC
slouken@1974
   140
slouken@5228
   141
    /* Multitexture support */
slouken@5228
   142
    SDL_bool GL_ARB_multitexture_supported;
slouken@5228
   143
    PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;
slouken@5351
   144
    GLint num_texture_units;
slouken@7191
   145
slouken@6232
   146
    PFNGLGENFRAMEBUFFERSEXTPROC glGenFramebuffersEXT;
slouken@6232
   147
    PFNGLDELETEFRAMEBUFFERSEXTPROC glDeleteFramebuffersEXT;
slouken@6232
   148
    PFNGLFRAMEBUFFERTEXTURE2DEXTPROC glFramebufferTexture2DEXT;
slouken@6232
   149
    PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT;
slouken@6232
   150
    PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC glCheckFramebufferStatusEXT;
slouken@5228
   151
slouken@5228
   152
    /* Shader support */
slouken@5228
   153
    GL_ShaderContext *shaders;
slouken@5228
   154
slouken@1918
   155
} GL_RenderData;
slouken@1918
   156
slouken@1918
   157
typedef struct
slouken@1918
   158
{
slouken@1918
   159
    GLuint texture;
slouken@1920
   160
    GLenum type;
slouken@1918
   161
    GLfloat texw;
slouken@1918
   162
    GLfloat texh;
slouken@1920
   163
    GLenum format;
slouken@1920
   164
    GLenum formattype;
slouken@1918
   165
    void *pixels;
slouken@1918
   166
    int pitch;
slouken@5227
   167
    SDL_Rect locked_rect;
slouken@5264
   168
slouken@9046
   169
    /* YUV texture support */
slouken@5264
   170
    SDL_bool yuv;
slouken@9046
   171
    SDL_bool nv12;
slouken@5264
   172
    GLuint utexture;
slouken@5264
   173
    GLuint vtexture;
slouken@7191
   174
slouken@6232
   175
    GL_FBOList *fbo;
slouken@1918
   176
} GL_TextureData;
slouken@1918
   177
slouken@7194
   178
SDL_FORCE_INLINE const char*
slouken@6494
   179
GL_TranslateError (GLenum error)
slouken@6494
   180
{
slouken@6494
   181
#define GL_ERROR_TRANSLATE(e) case e: return #e;
slouken@6494
   182
    switch (error) {
slouken@6494
   183
    GL_ERROR_TRANSLATE(GL_INVALID_ENUM)
slouken@6494
   184
    GL_ERROR_TRANSLATE(GL_INVALID_VALUE)
slouken@6494
   185
    GL_ERROR_TRANSLATE(GL_INVALID_OPERATION)
slouken@6494
   186
    GL_ERROR_TRANSLATE(GL_OUT_OF_MEMORY)
slouken@6494
   187
    GL_ERROR_TRANSLATE(GL_NO_ERROR)
slouken@6494
   188
    GL_ERROR_TRANSLATE(GL_STACK_OVERFLOW)
slouken@6494
   189
    GL_ERROR_TRANSLATE(GL_STACK_UNDERFLOW)
slouken@6494
   190
    GL_ERROR_TRANSLATE(GL_TABLE_TOO_LARGE)
slouken@6494
   191
    default:
slouken@6494
   192
        return "UNKNOWN";
slouken@6494
   193
}
slouken@6494
   194
#undef GL_ERROR_TRANSLATE
slouken@6494
   195
}
slouken@1918
   196
slouken@7194
   197
SDL_FORCE_INLINE void
slouken@7194
   198
GL_ClearErrors(SDL_Renderer *renderer)
slouken@7194
   199
{
slouken@7194
   200
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@7194
   201
slouken@7198
   202
    if (!data->debug_enabled)
slouken@7198
   203
    {
slouken@7198
   204
        return;
slouken@7198
   205
    }
slouken@7194
   206
    if (data->GL_ARB_debug_output_supported) {
slouken@7194
   207
        if (data->errors) {
slouken@7194
   208
            int i;
slouken@7194
   209
            for (i = 0; i < data->errors; ++i) {
slouken@7194
   210
                SDL_free(data->error_messages[i]);
slouken@7194
   211
            }
slouken@7194
   212
            SDL_free(data->error_messages);
slouken@7194
   213
slouken@7194
   214
            data->errors = 0;
slouken@7194
   215
            data->error_messages = NULL;
slouken@7194
   216
        }
slouken@7194
   217
    } else {
slouken@7194
   218
        while (data->glGetError() != GL_NO_ERROR) {
slouken@7194
   219
            continue;
slouken@7194
   220
        }
slouken@7194
   221
    }
slouken@7194
   222
}
slouken@7194
   223
slouken@7194
   224
SDL_FORCE_INLINE int
slouken@7194
   225
GL_CheckAllErrors (const char *prefix, SDL_Renderer *renderer, const char *file, int line, const char *function)
slouken@1924
   226
{
slouken@6494
   227
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@6494
   228
    int ret = 0;
slouken@7194
   229
slouken@7198
   230
    if (!data->debug_enabled)
slouken@7198
   231
    {
slouken@7198
   232
        return 0;
slouken@7198
   233
    }
slouken@7194
   234
    if (data->GL_ARB_debug_output_supported) {
slouken@7194
   235
        if (data->errors) {
slouken@7194
   236
            int i;
slouken@7194
   237
            for (i = 0; i < data->errors; ++i) {
slouken@7194
   238
                SDL_SetError("%s: %s (%d): %s %s", prefix, file, line, function, data->error_messages[i]);
slouken@7194
   239
                ret = -1;
slouken@6494
   240
            }
slouken@7194
   241
            GL_ClearErrors(renderer);
slouken@7194
   242
        }
slouken@7194
   243
    } else {
slouken@7194
   244
        /* check gl errors (can return multiple errors) */
slouken@7194
   245
        for (;;) {
slouken@7194
   246
            GLenum error = data->glGetError();
slouken@7194
   247
            if (error != GL_NO_ERROR) {
slouken@7194
   248
                if (prefix == NULL || prefix[0] == '\0') {
slouken@7194
   249
                    prefix = "generic";
slouken@7194
   250
                }
slouken@7194
   251
                SDL_SetError("%s: %s (%d): %s %s (0x%X)", prefix, file, line, function, GL_TranslateError(error), error);
slouken@7194
   252
                ret = -1;
slouken@7194
   253
            } else {
slouken@7194
   254
                break;
slouken@7194
   255
            }
slouken@6494
   256
        }
slouken@6494
   257
    }
slouken@6494
   258
    return ret;
slouken@6494
   259
}
slouken@1924
   260
icculus@6499
   261
#if 0
icculus@6499
   262
#define GL_CheckError(prefix, renderer)
icculus@6499
   263
#else
slouken@11839
   264
#define GL_CheckError(prefix, renderer) GL_CheckAllErrors(prefix, renderer, SDL_FILE, SDL_LINE, SDL_FUNCTION)
slouken@6494
   265
#endif
slouken@1924
   266
slouken@1927
   267
static int
slouken@1927
   268
GL_LoadFunctions(GL_RenderData * data)
slouken@1927
   269
{
slouken@1927
   270
#ifdef __SDL_NOGETPROCADDR__
slouken@1927
   271
#define SDL_PROC(ret,func,params) data->func=func;
slouken@1927
   272
#else
slouken@1927
   273
#define SDL_PROC(ret,func,params) \
slouken@1927
   274
    do { \
sezero@11872
   275
        data->func = SDL_GL_GetProcAddress(#func); \
slouken@1927
   276
        if ( ! data->func ) { \
philipp@10945
   277
            return SDL_SetError("Couldn't load GL function %s: %s", #func, SDL_GetError()); \
slouken@1927
   278
        } \
slouken@1927
   279
    } while ( 0 );
slouken@1927
   280
#endif /* __SDL_NOGETPROCADDR__ */
slouken@1927
   281
slouken@5204
   282
#include "SDL_glfuncs.h"
slouken@1927
   283
#undef SDL_PROC
slouken@1927
   284
    return 0;
slouken@1927
   285
}
slouken@1927
   286
slouken@5297
   287
static SDL_GLContext SDL_CurrentContext = NULL;
slouken@5297
   288
slouken@5297
   289
static int
slouken@5297
   290
GL_ActivateRenderer(SDL_Renderer * renderer)
slouken@5297
   291
{
slouken@5297
   292
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5297
   293
slouken@8869
   294
    if (SDL_CurrentContext != data->context ||
slouken@8869
   295
        SDL_GL_GetCurrentContext() != data->context) {
slouken@5297
   296
        if (SDL_GL_MakeCurrent(renderer->window, data->context) < 0) {
slouken@5297
   297
            return -1;
slouken@5297
   298
        }
slouken@5297
   299
        SDL_CurrentContext = data->context;
slouken@5297
   300
slouken@5297
   301
        GL_UpdateViewport(renderer);
slouken@5297
   302
    }
icculus@7486
   303
icculus@7486
   304
    GL_ClearErrors(renderer);
icculus@7486
   305
slouken@5297
   306
    return 0;
slouken@5297
   307
}
slouken@5297
   308
slouken@5355
   309
/* This is called if we need to invalidate all of the SDL OpenGL state */
slouken@5355
   310
static void
slouken@5355
   311
GL_ResetState(SDL_Renderer *renderer)
slouken@5355
   312
{
slouken@5355
   313
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5355
   314
slouken@8869
   315
    if (SDL_GL_GetCurrentContext() == data->context) {
slouken@5355
   316
        GL_UpdateViewport(renderer);
slouken@5355
   317
    } else {
slouken@5355
   318
        GL_ActivateRenderer(renderer);
slouken@5355
   319
    }
slouken@5355
   320
slouken@5355
   321
    data->current.shader = SHADER_NONE;
slouken@11243
   322
    data->current.color = 0xffffffff;
slouken@11282
   323
    data->current.blendMode = SDL_BLENDMODE_INVALID;
slouken@5355
   324
slouken@5355
   325
    data->glDisable(GL_DEPTH_TEST);
slouken@5355
   326
    data->glDisable(GL_CULL_FACE);
slouken@5355
   327
    /* This ended up causing video discrepancies between OpenGL and Direct3D */
gabomdq@7678
   328
    /* data->glEnable(GL_LINE_SMOOTH); */
slouken@5355
   329
slouken@5355
   330
    data->glMatrixMode(GL_MODELVIEW);
slouken@5355
   331
    data->glLoadIdentity();
slouken@6494
   332
slouken@6494
   333
    GL_CheckError("", renderer);
slouken@5355
   334
}
slouken@5355
   335
slouken@7197
   336
static void APIENTRY
jorgen@8811
   337
GL_HandleDebugMessage(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const char *message, const void *userParam)
slouken@7194
   338
{
slouken@7194
   339
    SDL_Renderer *renderer = (SDL_Renderer *) userParam;
slouken@7194
   340
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@6232
   341
slouken@7194
   342
    if (type == GL_DEBUG_TYPE_ERROR_ARB) {
slouken@7194
   343
        /* Record this error */
philipp@9336
   344
        int errors = data->errors + 1;
philipp@9336
   345
        char **error_messages = SDL_realloc(data->error_messages, errors * sizeof(*data->error_messages));
philipp@9334
   346
        if (error_messages) {
philipp@9336
   347
            data->errors = errors;
philipp@9334
   348
            data->error_messages = error_messages;
slouken@7194
   349
            data->error_messages[data->errors-1] = SDL_strdup(message);
slouken@7194
   350
        }
slouken@7194
   351
    }
slouken@7194
   352
slouken@7194
   353
    /* If there's another error callback, pass it along, otherwise log it */
slouken@7194
   354
    if (data->next_error_callback) {
slouken@7194
   355
        data->next_error_callback(source, type, id, severity, length, message, data->next_error_userparam);
slouken@7194
   356
    } else {
slouken@7194
   357
        if (type == GL_DEBUG_TYPE_ERROR_ARB) {
slouken@7194
   358
            SDL_LogError(SDL_LOG_CATEGORY_RENDER, "%s", message);
slouken@7194
   359
        } else {
slouken@7194
   360
            SDL_LogDebug(SDL_LOG_CATEGORY_RENDER, "%s", message);
slouken@7194
   361
        }
slouken@7194
   362
    }
slouken@7194
   363
}
slouken@7194
   364
slouken@7194
   365
static GL_FBOList *
slouken@6232
   366
GL_GetFBO(GL_RenderData *data, Uint32 w, Uint32 h)
slouken@6232
   367
{
slouken@6232
   368
    GL_FBOList *result = data->framebuffers;
slouken@6232
   369
slouken@6232
   370
    while (result && ((result->w != w) || (result->h != h))) {
slouken@6232
   371
        result = result->next;
slouken@6232
   372
    }
slouken@6232
   373
slouken@6232
   374
    if (!result) {
slouken@6232
   375
        result = SDL_malloc(sizeof(GL_FBOList));
slouken@6232
   376
        if (result) {
slouken@6232
   377
            result->w = w;
slouken@6232
   378
            result->h = h;
slouken@6232
   379
            data->glGenFramebuffersEXT(1, &result->FBO);
slouken@6232
   380
            result->next = data->framebuffers;
slouken@6232
   381
            data->framebuffers = result;
slouken@6232
   382
        }
slouken@6232
   383
    }
slouken@6232
   384
    return result;
slouken@6232
   385
}
slouken@6232
   386
slouken@1918
   387
SDL_Renderer *
slouken@1918
   388
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1918
   389
{
slouken@1918
   390
    SDL_Renderer *renderer;
slouken@1918
   391
    GL_RenderData *data;
slouken@1952
   392
    GLint value;
slouken@5154
   393
    Uint32 window_flags;
slime73@9958
   394
    int profile_mask = 0, major = 0, minor = 0;
slouken@8906
   395
    SDL_bool changed_window = SDL_FALSE;
gabomdq@8257
   396
gabomdq@8257
   397
    SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profile_mask);
gabomdq@8257
   398
    SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
gabomdq@8257
   399
    SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
gabomdq@8257
   400
    
slouken@5154
   401
    window_flags = SDL_GetWindowFlags(window);
gabomdq@8257
   402
    if (!(window_flags & SDL_WINDOW_OPENGL) ||
gabomdq@8264
   403
        profile_mask == SDL_GL_CONTEXT_PROFILE_ES || major != RENDERER_CONTEXT_MAJOR || minor != RENDERER_CONTEXT_MINOR) {
slouken@8906
   404
slouken@8906
   405
        changed_window = SDL_TRUE;
gabomdq@8264
   406
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0);
gabomdq@8264
   407
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, RENDERER_CONTEXT_MAJOR);
gabomdq@8264
   408
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, RENDERER_CONTEXT_MINOR);
gabomdq@8264
   409
slouken@5154
   410
        if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
slouken@8906
   411
            goto error;
slouken@1924
   412
        }
slouken@1918
   413
    }
slouken@1918
   414
slouken@1920
   415
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1918
   416
    if (!renderer) {
slouken@1918
   417
        SDL_OutOfMemory();
slouken@8906
   418
        goto error;
slouken@1918
   419
    }
slouken@1918
   420
slouken@1920
   421
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   422
    if (!data) {
cameron@12377
   423
        SDL_free(renderer);
slouken@1918
   424
        SDL_OutOfMemory();
slouken@8906
   425
        goto error;
slouken@1918
   426
    }
slouken@1918
   427
slouken@5147
   428
    renderer->WindowEvent = GL_WindowEvent;
urkle@7746
   429
    renderer->GetOutputSize = GL_GetOutputSize;
slouken@11282
   430
    renderer->SupportsBlendMode = GL_SupportsBlendMode;
slouken@1918
   431
    renderer->CreateTexture = GL_CreateTexture;
slouken@1918
   432
    renderer->UpdateTexture = GL_UpdateTexture;
slouken@7759
   433
    renderer->UpdateTextureYUV = GL_UpdateTextureYUV;
slouken@1918
   434
    renderer->LockTexture = GL_LockTexture;
slouken@1918
   435
    renderer->UnlockTexture = GL_UnlockTexture;
slouken@6247
   436
    renderer->SetRenderTarget = GL_SetRenderTarget;
slouken@5297
   437
    renderer->UpdateViewport = GL_UpdateViewport;
slouken@7141
   438
    renderer->UpdateClipRect = GL_UpdateClipRect;
slouken@3596
   439
    renderer->RenderClear = GL_RenderClear;
slouken@3596
   440
    renderer->RenderDrawPoints = GL_RenderDrawPoints;
slouken@3596
   441
    renderer->RenderDrawLines = GL_RenderDrawLines;
slouken@3596
   442
    renderer->RenderFillRects = GL_RenderFillRects;
slouken@1918
   443
    renderer->RenderCopy = GL_RenderCopy;
gabomdq@6320
   444
    renderer->RenderCopyEx = GL_RenderCopyEx;
slouken@3431
   445
    renderer->RenderReadPixels = GL_RenderReadPixels;
slouken@1918
   446
    renderer->RenderPresent = GL_RenderPresent;
slouken@1918
   447
    renderer->DestroyTexture = GL_DestroyTexture;
slouken@1918
   448
    renderer->DestroyRenderer = GL_DestroyRenderer;
gabomdq@6414
   449
    renderer->GL_BindTexture = GL_BindTexture;
gabomdq@6414
   450
    renderer->GL_UnbindTexture = GL_UnbindTexture;
slouken@1918
   451
    renderer->info = GL_RenderDriver.info;
slouken@10401
   452
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@1918
   453
    renderer->driverdata = data;
slouken@6171
   454
    renderer->window = window;
slouken@1918
   455
slouken@3685
   456
    data->context = SDL_GL_CreateContext(window);
slouken@1918
   457
    if (!data->context) {
cameron@12377
   458
        SDL_free(renderer);
cameron@12377
   459
        SDL_free(data);
slouken@8906
   460
        goto error;
slouken@1918
   461
    }
slouken@3685
   462
    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
cameron@12377
   463
        SDL_GL_DeleteContext(data->context);
cameron@12377
   464
        SDL_free(renderer);
cameron@12377
   465
        SDL_free(data);
slouken@8906
   466
        goto error;
slouken@1918
   467
    }
slouken@5204
   468
slouken@5204
   469
    if (GL_LoadFunctions(data) < 0) {
cameron@12377
   470
        SDL_GL_DeleteContext(data->context);
cameron@12377
   471
        SDL_free(renderer);
cameron@12377
   472
        SDL_free(data);
slouken@8906
   473
        goto error;
slouken@5204
   474
    }
slouken@5204
   475
slouken@2246
   476
#ifdef __MACOSX__
slouken@2246
   477
    /* Enable multi-threaded rendering */
slouken@2246
   478
    /* Disabled until Ryan finishes his VBO/PBO code...
bob@2295
   479
       CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
bob@2295
   480
     */
slouken@2246
   481
#endif
slouken@2246
   482
slouken@1965
   483
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1918
   484
        SDL_GL_SetSwapInterval(1);
slouken@1918
   485
    } else {
slouken@1918
   486
        SDL_GL_SetSwapInterval(0);
slouken@1918
   487
    }
slouken@1918
   488
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@1965
   489
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1918
   490
    }
slouken@1918
   491
slouken@7194
   492
    /* Check for debug output support */
slouken@7198
   493
    if (SDL_GL_GetAttribute(SDL_GL_CONTEXT_FLAGS, &value) == 0 &&
slouken@7198
   494
        (value & SDL_GL_CONTEXT_DEBUG_FLAG)) {
slouken@7198
   495
        data->debug_enabled = SDL_TRUE;
slouken@7198
   496
    }
slouken@7198
   497
    if (data->debug_enabled && SDL_GL_ExtensionSupported("GL_ARB_debug_output")) {
sezero@11872
   498
        PFNGLDEBUGMESSAGECALLBACKARBPROC glDebugMessageCallbackARBFunc = (PFNGLDEBUGMESSAGECALLBACKARBPROC) SDL_GL_GetProcAddress("glDebugMessageCallbackARB");
slouken@7194
   499
slouken@7194
   500
        data->GL_ARB_debug_output_supported = SDL_TRUE;
icculus@10646
   501
        data->glGetPointerv(GL_DEBUG_CALLBACK_FUNCTION_ARB, (GLvoid **)(char *)&data->next_error_callback);
slouken@7194
   502
        data->glGetPointerv(GL_DEBUG_CALLBACK_USER_PARAM_ARB, &data->next_error_userparam);
slouken@7194
   503
        glDebugMessageCallbackARBFunc(GL_HandleDebugMessage, renderer);
slouken@7198
   504
slouken@7198
   505
        /* Make sure our callback is called when errors actually happen */
slouken@7198
   506
        data->glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
slouken@7194
   507
    }
slouken@7194
   508
slouken@9063
   509
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_non_power_of_two")) {
slouken@9063
   510
        data->GL_ARB_texture_non_power_of_two_supported = SDL_TRUE;
slouken@9063
   511
    } else if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle") ||
slouken@9063
   512
               SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
slouken@1926
   513
        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
slouken@9063
   514
    }
slouken@9063
   515
    if (data->GL_ARB_texture_rectangle_supported) {
slouken@6449
   516
        data->glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_ARB, &value);
slouken@6449
   517
        renderer->info.max_texture_width = value;
slouken@6449
   518
        renderer->info.max_texture_height = value;
slouken@6449
   519
    } else {
slouken@6449
   520
        data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@6449
   521
        renderer->info.max_texture_width = value;
slouken@6449
   522
        renderer->info.max_texture_height = value;
slouken@1926
   523
    }
slouken@1920
   524
slouken@5228
   525
    /* Check for multitexture support */
slouken@5228
   526
    if (SDL_GL_ExtensionSupported("GL_ARB_multitexture")) {
sezero@11872
   527
        data->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glActiveTextureARB");
slouken@5228
   528
        if (data->glActiveTextureARB) {
slouken@5228
   529
            data->GL_ARB_multitexture_supported = SDL_TRUE;
slouken@5228
   530
            data->glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &data->num_texture_units);
slouken@5228
   531
        }
slouken@5228
   532
    }
slouken@5228
   533
slouken@5228
   534
    /* Check for shader support */
slouken@10499
   535
    if (SDL_GetHintBoolean(SDL_HINT_RENDER_OPENGL_SHADERS, SDL_TRUE)) {
slouken@5233
   536
        data->shaders = GL_CreateShaderContext();
slouken@5233
   537
    }
slouken@5233
   538
    SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, "OpenGL shaders: %s",
slouken@5233
   539
                data->shaders ? "ENABLED" : "DISABLED");
slouken@5228
   540
slouken@5228
   541
    /* We support YV12 textures using 3 textures and a shader */
slouken@5228
   542
    if (data->shaders && data->num_texture_units >= 3) {
slouken@5228
   543
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_YV12;
slouken@5264
   544
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_IYUV;
slouken@9046
   545
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_NV12;
slouken@9046
   546
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_NV21;
slouken@5228
   547
    }
slouken@7191
   548
slouken@7814
   549
#ifdef __MACOSX__
slouken@7814
   550
    renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_UYVY;
slouken@7814
   551
#endif
slouken@7814
   552
slouken@6232
   553
    if (SDL_GL_ExtensionSupported("GL_EXT_framebuffer_object")) {
slouken@6232
   554
        data->GL_EXT_framebuffer_object_supported = SDL_TRUE;
sezero@11872
   555
        data->glGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC)
sezero@11872
   556
            SDL_GL_GetProcAddress("glGenFramebuffersEXT");
sezero@11872
   557
        data->glDeleteFramebuffersEXT = (PFNGLDELETEFRAMEBUFFERSEXTPROC)
sezero@11872
   558
            SDL_GL_GetProcAddress("glDeleteFramebuffersEXT");
sezero@11872
   559
        data->glFramebufferTexture2DEXT = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)
sezero@11872
   560
            SDL_GL_GetProcAddress("glFramebufferTexture2DEXT");
sezero@11872
   561
        data->glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)
sezero@11872
   562
            SDL_GL_GetProcAddress("glBindFramebufferEXT");
sezero@11872
   563
        data->glCheckFramebufferStatusEXT = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)
sezero@11872
   564
            SDL_GL_GetProcAddress("glCheckFramebufferStatusEXT");
slouken@6246
   565
        renderer->info.flags |= SDL_RENDERER_TARGETTEXTURE;
slouken@6232
   566
    }
slouken@6232
   567
    data->framebuffers = NULL;
slouken@5228
   568
slouken@1918
   569
    /* Set up parameters for rendering */
slouken@5355
   570
    GL_ResetState(renderer);
slouken@1918
   571
slouken@1918
   572
    return renderer;
slouken@8906
   573
slouken@8906
   574
error:
slouken@8906
   575
    if (changed_window) {
slouken@8906
   576
        /* Uh oh, better try to put it back... */
slouken@8906
   577
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile_mask);
slouken@8906
   578
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
slouken@8906
   579
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
slouken@8906
   580
        SDL_RecreateWindow(window, window_flags);
slouken@8906
   581
    }
slouken@8906
   582
    return NULL;
slouken@1918
   583
}
slouken@1918
   584
slouken@5147
   585
static void
slouken@5147
   586
GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1970
   587
{
slouken@6260
   588
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED ||
slouken@6260
   589
        event->event == SDL_WINDOWEVENT_SHOWN ||
slouken@6260
   590
        event->event == SDL_WINDOWEVENT_HIDDEN) {
slouken@5147
   591
        /* Rebind the context to the window area and update matrices */
slouken@5147
   592
        SDL_CurrentContext = NULL;
slouken@5147
   593
    }
slouken@1923
   594
}
slouken@1923
   595
urkle@7746
   596
static int
urkle@7746
   597
GL_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
urkle@7746
   598
{
urkle@7746
   599
    SDL_GL_GetDrawableSize(renderer->window, w, h);
urkle@7746
   600
    return 0;
urkle@7746
   601
}
urkle@7746
   602
slouken@11282
   603
static GLenum GetBlendFunc(SDL_BlendFactor factor)
slouken@11282
   604
{
slouken@11282
   605
    switch (factor) {
slouken@11282
   606
    case SDL_BLENDFACTOR_ZERO:
slouken@11282
   607
        return GL_ZERO;
slouken@11282
   608
    case SDL_BLENDFACTOR_ONE:
slouken@11282
   609
        return GL_ONE;
slouken@11282
   610
    case SDL_BLENDFACTOR_SRC_COLOR:
slouken@11282
   611
        return GL_SRC_COLOR;
slouken@11282
   612
    case SDL_BLENDFACTOR_ONE_MINUS_SRC_COLOR:
slouken@11282
   613
        return GL_ONE_MINUS_SRC_COLOR;
slouken@11282
   614
    case SDL_BLENDFACTOR_SRC_ALPHA:
slouken@11282
   615
        return GL_SRC_ALPHA;
slouken@11282
   616
    case SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA:
slouken@11282
   617
        return GL_ONE_MINUS_SRC_ALPHA;
slouken@11282
   618
    case SDL_BLENDFACTOR_DST_COLOR:
slouken@11282
   619
        return GL_DST_COLOR;
slouken@11282
   620
    case SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR:
slouken@11282
   621
        return GL_ONE_MINUS_DST_COLOR;
slouken@11282
   622
    case SDL_BLENDFACTOR_DST_ALPHA:
slouken@11282
   623
        return GL_DST_ALPHA;
slouken@11282
   624
    case SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA:
slouken@11282
   625
        return GL_ONE_MINUS_DST_ALPHA;
slouken@11282
   626
    default:
slouken@11282
   627
        return GL_INVALID_ENUM;
slouken@11282
   628
    }
slouken@11282
   629
}
slouken@11282
   630
slouken@11282
   631
static GLenum GetBlendEquation(SDL_BlendOperation operation)
slouken@11282
   632
{
slouken@11282
   633
    switch (operation) {
slouken@11282
   634
    case SDL_BLENDOPERATION_ADD:
slouken@11282
   635
        return GL_FUNC_ADD;
slouken@11282
   636
    case SDL_BLENDOPERATION_SUBTRACT:
slouken@11282
   637
        return GL_FUNC_SUBTRACT;
slouken@11282
   638
    case SDL_BLENDOPERATION_REV_SUBTRACT:
slouken@11282
   639
        return GL_FUNC_REVERSE_SUBTRACT;
slouken@11282
   640
    default:
slouken@11282
   641
        return GL_INVALID_ENUM;
slouken@11282
   642
    }
slouken@11282
   643
}
slouken@11282
   644
slouken@11282
   645
static SDL_bool
slouken@11282
   646
GL_SupportsBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
slouken@11282
   647
{
slouken@11282
   648
    SDL_BlendFactor srcColorFactor = SDL_GetBlendModeSrcColorFactor(blendMode);
slouken@11282
   649
    SDL_BlendFactor srcAlphaFactor = SDL_GetBlendModeSrcAlphaFactor(blendMode);
slouken@11282
   650
    SDL_BlendOperation colorOperation = SDL_GetBlendModeColorOperation(blendMode);
slouken@11282
   651
    SDL_BlendFactor dstColorFactor = SDL_GetBlendModeDstColorFactor(blendMode);
slouken@11282
   652
    SDL_BlendFactor dstAlphaFactor = SDL_GetBlendModeDstAlphaFactor(blendMode);
slouken@11282
   653
    SDL_BlendOperation alphaOperation = SDL_GetBlendModeAlphaOperation(blendMode);
slouken@11282
   654
slouken@11282
   655
    if (GetBlendFunc(srcColorFactor) == GL_INVALID_ENUM ||
slouken@11282
   656
        GetBlendFunc(srcAlphaFactor) == GL_INVALID_ENUM ||
slouken@11282
   657
        GetBlendEquation(colorOperation) == GL_INVALID_ENUM ||
slouken@11282
   658
        GetBlendFunc(dstColorFactor) == GL_INVALID_ENUM ||
slouken@11282
   659
        GetBlendFunc(dstAlphaFactor) == GL_INVALID_ENUM ||
slouken@11282
   660
        GetBlendEquation(alphaOperation) == GL_INVALID_ENUM) {
slouken@11282
   661
        return SDL_FALSE;
slouken@11282
   662
    }
slouken@11282
   663
    if (colorOperation != alphaOperation) {
slouken@11282
   664
        return SDL_FALSE;
slouken@11282
   665
    }
slouken@11282
   666
    return SDL_TRUE;
slouken@11282
   667
}
slouken@11282
   668
slouken@7194
   669
SDL_FORCE_INLINE int
slouken@1922
   670
power_of_2(int input)
slouken@1922
   671
{
slouken@1922
   672
    int value = 1;
slouken@1922
   673
slouken@1922
   674
    while (value < input) {
slouken@1922
   675
        value <<= 1;
slouken@1922
   676
    }
slouken@1922
   677
    return value;
slouken@1922
   678
}
slouken@1922
   679
slouken@7194
   680
SDL_FORCE_INLINE SDL_bool
slouken@3433
   681
convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
slouken@3433
   682
               GLint* internalFormat, GLenum* format, GLenum* type)
slouken@3433
   683
{
slouken@3433
   684
    switch (pixel_format) {
slouken@3433
   685
    case SDL_PIXELFORMAT_ARGB8888:
slouken@5156
   686
        *internalFormat = GL_RGBA8;
slouken@3433
   687
        *format = GL_BGRA;
slouken@3433
   688
        *type = GL_UNSIGNED_INT_8_8_8_8_REV;
slouken@3433
   689
        break;
slouken@5264
   690
    case SDL_PIXELFORMAT_YV12:
slouken@5264
   691
    case SDL_PIXELFORMAT_IYUV:
slouken@9046
   692
    case SDL_PIXELFORMAT_NV12:
slouken@9046
   693
    case SDL_PIXELFORMAT_NV21:
slouken@5264
   694
        *internalFormat = GL_LUMINANCE;
slouken@5264
   695
        *format = GL_LUMINANCE;
slouken@5264
   696
        *type = GL_UNSIGNED_BYTE;
slouken@5264
   697
        break;
slouken@7814
   698
#ifdef __MACOSX__
slouken@7814
   699
    case SDL_PIXELFORMAT_UYVY:
slouken@9046
   700
        *internalFormat = GL_RGB8;
slouken@9046
   701
        *format = GL_YCBCR_422_APPLE;
slouken@9046
   702
        *type = GL_UNSIGNED_SHORT_8_8_APPLE;
slouken@9046
   703
        break;
slouken@7814
   704
#endif
slouken@3433
   705
    default:
slouken@3433
   706
        return SDL_FALSE;
slouken@3433
   707
    }
slouken@3433
   708
    return SDL_TRUE;
slouken@3433
   709
}
icculus@2835
   710
slouken@1918
   711
static int
slouken@1918
   712
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   713
{
slouken@1918
   714
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   715
    GL_TextureData *data;
slouken@1920
   716
    GLint internalFormat;
slouken@1920
   717
    GLenum format, type;
slouken@1922
   718
    int texture_w, texture_h;
slouken@5503
   719
    GLenum scaleMode;
slouken@1918
   720
slouken@5147
   721
    GL_ActivateRenderer(renderer);
slouken@5147
   722
slouken@10401
   723
    if (texture->access == SDL_TEXTUREACCESS_TARGET &&
slouken@10401
   724
        !renderdata->GL_EXT_framebuffer_object_supported) {
slouken@10401
   725
        return SDL_SetError("Render targets not supported by OpenGL");
slouken@10401
   726
    }
slouken@10401
   727
slouken@3433
   728
    if (!convert_format(renderdata, texture->format, &internalFormat,
slouken@3433
   729
                        &format, &type)) {
icculus@7037
   730
        return SDL_SetError("Texture format %s not supported by OpenGL",
icculus@7037
   731
                            SDL_GetPixelFormatName(texture->format));
slouken@1920
   732
    }
slouken@1920
   733
slouken@1920
   734
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   735
    if (!data) {
icculus@7037
   736
        return SDL_OutOfMemory();
slouken@1918
   737
    }
slouken@1918
   738
slouken@2222
   739
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5264
   740
        size_t size;
slouken@5156
   741
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
slouken@5264
   742
        size = texture->h * data->pitch;
slouken@5264
   743
        if (texture->format == SDL_PIXELFORMAT_YV12 ||
slouken@5264
   744
            texture->format == SDL_PIXELFORMAT_IYUV) {
slouken@5264
   745
            /* Need to add size for the U and V planes */
slouken@11574
   746
            size += 2 * ((texture->h + 1) / 2) * ((data->pitch + 1) / 2);
slouken@5264
   747
        }
slouken@9046
   748
        if (texture->format == SDL_PIXELFORMAT_NV12 ||
slouken@9046
   749
            texture->format == SDL_PIXELFORMAT_NV21) {
slouken@9046
   750
            /* Need to add size for the U/V plane */
slouken@11574
   751
            size += 2 * ((texture->h + 1) / 2) * ((data->pitch + 1) / 2);
slouken@9046
   752
        }
slouken@5402
   753
        data->pixels = SDL_calloc(1, size);
slouken@2222
   754
        if (!data->pixels) {
slouken@2222
   755
            SDL_free(data);
icculus@7037
   756
            return SDL_OutOfMemory();
slouken@2222
   757
        }
slouken@2222
   758
    }
slouken@2222
   759
slouken@6232
   760
    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
slouken@6232
   761
        data->fbo = GL_GetFBO(renderdata, texture->w, texture->h);
slouken@6232
   762
    } else {
slouken@6232
   763
        data->fbo = NULL;
slouken@6232
   764
    }
slouken@1918
   765
slouken@6494
   766
    GL_CheckError("", renderer);
slouken@1927
   767
    renderdata->glGenTextures(1, &data->texture);
slouken@9063
   768
    if (GL_CheckError("glGenTextures()", renderer) < 0) {
slouken@9011
   769
        if (data->pixels) {
slouken@9011
   770
            SDL_free(data->pixels);
slouken@9011
   771
        }
slouken@7624
   772
        SDL_free(data);
slouken@7624
   773
        return -1;
slouken@7624
   774
    }
slouken@7779
   775
    texture->driverdata = data;
slouken@7779
   776
slouken@9063
   777
    if (renderdata->GL_ARB_texture_non_power_of_two_supported) {
slouken@9063
   778
        data->type = GL_TEXTURE_2D;
slouken@9063
   779
        texture_w = texture->w;
slouken@9063
   780
        texture_h = texture->h;
slouken@9063
   781
        data->texw = 1.0f;
slouken@9063
   782
        data->texh = 1.0f;
slouken@9063
   783
    } else if (renderdata->GL_ARB_texture_rectangle_supported) {
slouken@1926
   784
        data->type = GL_TEXTURE_RECTANGLE_ARB;
slouken@1926
   785
        texture_w = texture->w;
slouken@1926
   786
        texture_h = texture->h;
icculus@2835
   787
        data->texw = (GLfloat) texture_w;
icculus@2835
   788
        data->texh = (GLfloat) texture_h;
slouken@1926
   789
    } else {
slouken@1926
   790
        data->type = GL_TEXTURE_2D;
slouken@1926
   791
        texture_w = power_of_2(texture->w);
slouken@1926
   792
        texture_h = power_of_2(texture->h);
icculus@2835
   793
        data->texw = (GLfloat) (texture->w) / texture_w;
slouken@1926
   794
        data->texh = (GLfloat) texture->h / texture_h;
slouken@1926
   795
    }
icculus@2835
   796
slouken@1920
   797
    data->format = format;
slouken@1920
   798
    data->formattype = type;
slouken@11958
   799
    scaleMode = (texture->scaleMode == SDL_ScaleModeNearest) ? GL_NEAREST : GL_LINEAR;
slouken@2884
   800
    renderdata->glEnable(data->type);
slouken@1927
   801
    renderdata->glBindTexture(data->type, data->texture);
slouken@5503
   802
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER, scaleMode);
slouken@5503
   803
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER, scaleMode);
slouken@6207
   804
    /* According to the spec, CLAMP_TO_EDGE is the default for TEXTURE_RECTANGLE
slouken@6207
   805
       and setting it causes an INVALID_ENUM error in the latest NVidia drivers.
slouken@6207
   806
    */
slouken@6207
   807
    if (data->type != GL_TEXTURE_RECTANGLE_ARB) {
slouken@6207
   808
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@6207
   809
                                    GL_CLAMP_TO_EDGE);
slouken@6207
   810
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@6207
   811
                                    GL_CLAMP_TO_EDGE);
slouken@6207
   812
    }
slouken@2840
   813
#ifdef __MACOSX__
slouken@2230
   814
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
slouken@2230
   815
#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
slouken@2230
   816
#endif
slouken@2230
   817
#ifndef STORAGE_CACHED_APPLE
slouken@2230
   818
#define STORAGE_CACHED_APPLE                0x85BE
slouken@2230
   819
#endif
slouken@2230
   820
#ifndef STORAGE_SHARED_APPLE
slouken@2230
   821
#define STORAGE_SHARED_APPLE                0x85BF
slouken@2230
   822
#endif
slouken@2230
   823
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@2230
   824
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   825
                                    GL_STORAGE_SHARED_APPLE);
slouken@2230
   826
    } else {
slouken@2230
   827
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   828
                                    GL_STORAGE_CACHED_APPLE);
slouken@2230
   829
    }
bob@2295
   830
    if (texture->access == SDL_TEXTUREACCESS_STREAMING
slouken@5397
   831
        && texture->format == SDL_PIXELFORMAT_ARGB8888
slouken@5397
   832
        && (texture->w % 8) == 0) {
slouken@2230
   833
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
slouken@5397
   834
        renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@5397
   835
        renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@5397
   836
                          (data->pitch / SDL_BYTESPERPIXEL(texture->format)));
slouken@2230
   837
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   838
                                 texture_h, 0, format, type, data->pixels);
slouken@5397
   839
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
slouken@5156
   840
    }
slouken@5156
   841
    else
slouken@2809
   842
#endif
slouken@2230
   843
    {
slouken@2230
   844
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   845
                                 texture_h, 0, format, type, NULL);
slouken@2230
   846
    }
slouken@3041
   847
    renderdata->glDisable(data->type);
slouken@7194
   848
    if (GL_CheckError("glTexImage2D()", renderer) < 0) {
slouken@1924
   849
        return -1;
slouken@1924
   850
    }
slouken@5264
   851
slouken@5264
   852
    if (texture->format == SDL_PIXELFORMAT_YV12 ||
slouken@5264
   853
        texture->format == SDL_PIXELFORMAT_IYUV) {
slouken@5264
   854
        data->yuv = SDL_TRUE;
slouken@5264
   855
slouken@5264
   856
        renderdata->glGenTextures(1, &data->utexture);
slouken@5264
   857
        renderdata->glGenTextures(1, &data->vtexture);
slouken@5264
   858
        renderdata->glEnable(data->type);
slouken@5264
   859
slouken@5264
   860
        renderdata->glBindTexture(data->type, data->utexture);
slouken@5503
   861
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@5503
   862
                                    scaleMode);
slouken@5503
   863
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@5503
   864
                                    scaleMode);
slouken@5264
   865
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@5264
   866
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   867
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@5264
   868
                                    GL_CLAMP_TO_EDGE);
slouken@11574
   869
        renderdata->glTexImage2D(data->type, 0, internalFormat, (texture_w+1)/2,
slouken@11574
   870
                                 (texture_h+1)/2, 0, format, type, NULL);
slouken@5264
   871
slouken@5264
   872
        renderdata->glBindTexture(data->type, data->vtexture);
slouken@5503
   873
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@5503
   874
                                    scaleMode);
slouken@5503
   875
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@5503
   876
                                    scaleMode);
slouken@5264
   877
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@5264
   878
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   879
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@5264
   880
                                    GL_CLAMP_TO_EDGE);
slouken@11574
   881
        renderdata->glTexImage2D(data->type, 0, internalFormat, (texture_w+1)/2,
slouken@11574
   882
                                 (texture_h+1)/2, 0, format, type, NULL);
slouken@5264
   883
slouken@5264
   884
        renderdata->glDisable(data->type);
slouken@5264
   885
    }
slouken@6494
   886
slouken@9046
   887
    if (texture->format == SDL_PIXELFORMAT_NV12 ||
slouken@9046
   888
        texture->format == SDL_PIXELFORMAT_NV21) {
slouken@9046
   889
        data->nv12 = SDL_TRUE;
slouken@9046
   890
slouken@9046
   891
        renderdata->glGenTextures(1, &data->utexture);
slouken@9046
   892
        renderdata->glEnable(data->type);
slouken@9046
   893
slouken@9046
   894
        renderdata->glBindTexture(data->type, data->utexture);
slouken@9046
   895
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@9046
   896
                                    scaleMode);
slouken@9046
   897
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@9046
   898
                                    scaleMode);
slouken@9046
   899
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@9046
   900
                                    GL_CLAMP_TO_EDGE);
slouken@9046
   901
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@9046
   902
                                    GL_CLAMP_TO_EDGE);
slouken@11574
   903
        renderdata->glTexImage2D(data->type, 0, GL_LUMINANCE_ALPHA, (texture_w+1)/2,
slouken@11574
   904
                                 (texture_h+1)/2, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, NULL);
slouken@9046
   905
        renderdata->glDisable(data->type);
slouken@9046
   906
    }
slouken@9046
   907
slouken@7194
   908
    return GL_CheckError("", renderer);
slouken@1918
   909
}
slouken@1918
   910
slouken@1918
   911
static int
slouken@1918
   912
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   913
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
   914
{
slouken@1927
   915
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   916
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
icculus@8650
   917
    const int texturebpp = SDL_BYTESPERPIXEL(texture->format);
icculus@8650
   918
icculus@8650
   919
    SDL_assert(texturebpp != 0);  /* otherwise, division by zero later. */
slouken@1918
   920
slouken@5147
   921
    GL_ActivateRenderer(renderer);
slouken@5147
   922
slouken@5397
   923
    renderdata->glEnable(data->type);
slouken@5397
   924
    renderdata->glBindTexture(data->type, data->texture);
slouken@5227
   925
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
icculus@8650
   926
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, (pitch / texturebpp));
slouken@1927
   927
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
slouken@1927
   928
                                rect->h, data->format, data->formattype,
slouken@1927
   929
                                pixels);
slouken@5264
   930
    if (data->yuv) {
slouken@11574
   931
        renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, ((pitch + 1) / 2));
slouken@5265
   932
slouken@5264
   933
        /* Skip to the correct offset into the next texture */
slouken@6135
   934
        pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
slouken@5264
   935
        if (texture->format == SDL_PIXELFORMAT_YV12) {
slouken@5264
   936
            renderdata->glBindTexture(data->type, data->vtexture);
slouken@5264
   937
        } else {
slouken@5264
   938
            renderdata->glBindTexture(data->type, data->utexture);
slouken@5264
   939
        }
slouken@5264
   940
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@11574
   941
                                    (rect->w+1)/2, (rect->h+1)/2,
slouken@5264
   942
                                    data->format, data->formattype, pixels);
slouken@5264
   943
slouken@5264
   944
        /* Skip to the correct offset into the next texture */
slouken@11574
   945
        pixels = (const void*)((const Uint8*)pixels + ((rect->h + 1) / 2) * ((pitch + 1) / 2));
slouken@5264
   946
        if (texture->format == SDL_PIXELFORMAT_YV12) {
slouken@5264
   947
            renderdata->glBindTexture(data->type, data->utexture);
slouken@5264
   948
        } else {
slouken@5264
   949
            renderdata->glBindTexture(data->type, data->vtexture);
slouken@5264
   950
        }
slouken@5264
   951
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@11574
   952
                                    (rect->w+1)/2, (rect->h+1)/2,
slouken@5264
   953
                                    data->format, data->formattype, pixels);
slouken@5264
   954
    }
slouken@9046
   955
slouken@9046
   956
    if (data->nv12) {
slouken@11574
   957
        renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, ((pitch + 1) / 2));
slouken@9046
   958
slouken@9046
   959
        /* Skip to the correct offset into the next texture */
slouken@9046
   960
        pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
slouken@9046
   961
        renderdata->glBindTexture(data->type, data->utexture);
slouken@9046
   962
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@11574
   963
                                    (rect->w + 1)/2, (rect->h + 1)/2,
slouken@9046
   964
                                    GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, pixels);
slouken@9046
   965
    }
slouken@3041
   966
    renderdata->glDisable(data->type);
slouken@7779
   967
slouken@7194
   968
    return GL_CheckError("glTexSubImage2D()", renderer);
slouken@1918
   969
}
slouken@1918
   970
slouken@1918
   971
static int
slouken@7759
   972
GL_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@7759
   973
                    const SDL_Rect * rect,
slouken@7759
   974
                    const Uint8 *Yplane, int Ypitch,
slouken@7759
   975
                    const Uint8 *Uplane, int Upitch,
slouken@7759
   976
                    const Uint8 *Vplane, int Vpitch)
slouken@7759
   977
{
slouken@7759
   978
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@7759
   979
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@7759
   980
slouken@7759
   981
    GL_ActivateRenderer(renderer);
slouken@7759
   982
slouken@7759
   983
    renderdata->glEnable(data->type);
slouken@7759
   984
    renderdata->glBindTexture(data->type, data->texture);
slouken@7759
   985
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@7759
   986
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Ypitch);
slouken@7759
   987
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
slouken@7759
   988
                                rect->h, data->format, data->formattype,
slouken@7759
   989
                                Yplane);
slouken@7759
   990
slouken@7759
   991
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Upitch);
slouken@7759
   992
    renderdata->glBindTexture(data->type, data->utexture);
slouken@7759
   993
    renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@11574
   994
                                (rect->w + 1)/2, (rect->h + 1)/2,
slouken@7759
   995
                                data->format, data->formattype, Uplane);
slouken@7759
   996
slouken@7759
   997
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Vpitch);
slouken@7759
   998
    renderdata->glBindTexture(data->type, data->vtexture);
slouken@7759
   999
    renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@11574
  1000
                                (rect->w + 1)/2, (rect->h + 1)/2,
slouken@7759
  1001
                                data->format, data->formattype, Vplane);
slouken@7759
  1002
    renderdata->glDisable(data->type);
slouken@7779
  1003
slouken@7759
  1004
    return GL_CheckError("glTexSubImage2D()", renderer);
slouken@7759
  1005
}
slouken@7759
  1006
slouken@7759
  1007
static int
slouken@1918
  1008
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
  1009
               const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1918
  1010
{
slouken@1918
  1011
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
  1012
slouken@5227
  1013
    data->locked_rect = *rect;
slouken@7191
  1014
    *pixels =
slouken@1920
  1015
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@5156
  1016
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@1920
  1017
    *pitch = data->pitch;
slouken@1918
  1018
    return 0;
slouken@1918
  1019
}
slouken@1918
  1020
slouken@1918
  1021
static void
slouken@1918
  1022
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
  1023
{
slouken@5156
  1024
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@5227
  1025
    const SDL_Rect *rect;
slouken@5227
  1026
    void *pixels;
slouken@1918
  1027
slouken@5227
  1028
    rect = &data->locked_rect;
slouken@7191
  1029
    pixels =
slouken@5227
  1030
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@5227
  1031
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5227
  1032
    GL_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
slouken@1918
  1033
}
slouken@1918
  1034
slouken@5297
  1035
static int
slouken@6247
  1036
GL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@6246
  1037
{
slouken@7191
  1038
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@6246
  1039
    GL_TextureData *texturedata;
slouken@6246
  1040
    GLenum status;
slouken@6246
  1041
slouken@6246
  1042
    GL_ActivateRenderer(renderer);
slouken@7191
  1043
slouken@10401
  1044
    if (!data->GL_EXT_framebuffer_object_supported) {
slouken@10401
  1045
        return SDL_SetError("Render targets not supported by OpenGL");
slouken@10401
  1046
    }
slouken@10401
  1047
slouken@6246
  1048
    if (texture == NULL) {
slouken@6246
  1049
        data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
slouken@6246
  1050
        return 0;
slouken@6246
  1051
    }
slouken@6246
  1052
slouken@6246
  1053
    texturedata = (GL_TextureData *) texture->driverdata;
slouken@6246
  1054
    data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, texturedata->fbo->FBO);
slouken@6246
  1055
    /* TODO: check if texture pixel format allows this operation */
slouken@6246
  1056
    data->glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, texturedata->type, texturedata->texture, 0);
slouken@6246
  1057
    /* Check FBO status */
slouken@6246
  1058
    status = data->glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
slouken@6246
  1059
    if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
icculus@7037
  1060
        return SDL_SetError("glFramebufferTexture2DEXT() failed");
slouken@6246
  1061
    }
slouken@6246
  1062
    return 0;
slouken@6246
  1063
}
slouken@6246
  1064
slouken@6246
  1065
static int
slouken@5297
  1066
GL_UpdateViewport(SDL_Renderer * renderer)
slouken@5224
  1067
{
slouken@5224
  1068
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5224
  1069
slouken@5297
  1070
    if (SDL_CurrentContext != data->context) {
slouken@5297
  1071
        /* We'll update the viewport after we rebind the context */
slouken@5297
  1072
        return 0;
slouken@5297
  1073
    }
slouken@5224
  1074
slouken@7995
  1075
    if (renderer->target) {
slouken@7995
  1076
        data->glViewport(renderer->viewport.x, renderer->viewport.y,
slouken@7995
  1077
                         renderer->viewport.w, renderer->viewport.h);
slouken@7995
  1078
    } else {
slouken@7995
  1079
        int w, h;
slouken@7995
  1080
slouken@10531
  1081
        SDL_GL_GetDrawableSize(renderer->window, &w, &h);
slouken@7995
  1082
        data->glViewport(renderer->viewport.x, (h - renderer->viewport.y - renderer->viewport.h),
slouken@7995
  1083
                         renderer->viewport.w, renderer->viewport.h);
slouken@7995
  1084
    }
slouken@5224
  1085
slouken@5297
  1086
    data->glMatrixMode(GL_PROJECTION);
slouken@5297
  1087
    data->glLoadIdentity();
slouken@7239
  1088
    if (renderer->viewport.w && renderer->viewport.h) {
slouken@7995
  1089
        if (renderer->target) {
slouken@7239
  1090
            data->glOrtho((GLdouble) 0,
slouken@7239
  1091
                          (GLdouble) renderer->viewport.w,
slouken@7239
  1092
                          (GLdouble) 0,
slouken@7239
  1093
                          (GLdouble) renderer->viewport.h,
slouken@7239
  1094
                           0.0, 1.0);
slouken@7239
  1095
        } else {
slouken@7239
  1096
            data->glOrtho((GLdouble) 0,
slouken@7239
  1097
                          (GLdouble) renderer->viewport.w,
slouken@7239
  1098
                          (GLdouble) renderer->viewport.h,
slouken@7239
  1099
                          (GLdouble) 0,
slouken@7239
  1100
                           0.0, 1.0);
slouken@7239
  1101
        }
slouken@6246
  1102
    }
slouken@10861
  1103
    data->glMatrixMode(GL_MODELVIEW);
slouken@10861
  1104
slouken@7194
  1105
    return GL_CheckError("", renderer);
slouken@5224
  1106
}
slouken@5224
  1107
slouken@7141
  1108
static int
slouken@7141
  1109
GL_UpdateClipRect(SDL_Renderer * renderer)
slouken@7141
  1110
{
slouken@7141
  1111
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@7141
  1112
jorgenpt@8728
  1113
    if (renderer->clipping_enabled) {
jorgenpt@8728
  1114
        const SDL_Rect *rect = &renderer->clip_rect;
slouken@7141
  1115
        data->glEnable(GL_SCISSOR_TEST);
slouken@9680
  1116
        if (renderer->target) {
slouken@9680
  1117
            data->glScissor(renderer->viewport.x + rect->x, renderer->viewport.y + rect->y, rect->w, rect->h);
slouken@9680
  1118
        } else {
slouken@9680
  1119
            int w, h;
slouken@9680
  1120
slouken@10531
  1121
            SDL_GL_GetDrawableSize(renderer->window, &w, &h);
icculus@9977
  1122
            data->glScissor(renderer->viewport.x + rect->x, h - renderer->viewport.y - rect->y - rect->h, rect->w, rect->h);
slouken@9680
  1123
        }
slouken@7141
  1124
    } else {
slouken@7141
  1125
        data->glDisable(GL_SCISSOR_TEST);
slouken@7141
  1126
    }
slouken@7141
  1127
    return 0;
slouken@7141
  1128
}
slouken@7141
  1129
slouken@5224
  1130
static void
slouken@5355
  1131
GL_SetShader(GL_RenderData * data, GL_Shader shader)
slouken@5355
  1132
{
slouken@5355
  1133
    if (data->shaders && shader != data->current.shader) {
slouken@5355
  1134
        GL_SelectShader(data->shaders, shader);
slouken@5355
  1135
        data->current.shader = shader;
slouken@5355
  1136
    }
slouken@5355
  1137
}
slouken@5355
  1138
slouken@5355
  1139
static void
slouken@5355
  1140
GL_SetColor(GL_RenderData * data, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@5355
  1141
{
slouken@5355
  1142
    Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
slouken@5355
  1143
slouken@5355
  1144
    if (color != data->current.color) {
slouken@5355
  1145
        data->glColor4f((GLfloat) r * inv255f,
slouken@5355
  1146
                        (GLfloat) g * inv255f,
slouken@5355
  1147
                        (GLfloat) b * inv255f,
slouken@5355
  1148
                        (GLfloat) a * inv255f);
slouken@5355
  1149
        data->current.color = color;
slouken@5355
  1150
    }
slouken@5355
  1151
}
slouken@5355
  1152
slouken@5355
  1153
static void
slouken@11282
  1154
GL_SetBlendMode(GL_RenderData * data, SDL_BlendMode blendMode)
slouken@2936
  1155
{
slouken@5355
  1156
    if (blendMode != data->current.blendMode) {
slouken@11282
  1157
        if (blendMode == SDL_BLENDMODE_NONE) {
slouken@2936
  1158
            data->glDisable(GL_BLEND);
slouken@11282
  1159
        } else {
slouken@2936
  1160
            data->glEnable(GL_BLEND);
slouken@11282
  1161
            data->glBlendFuncSeparate(GetBlendFunc(SDL_GetBlendModeSrcColorFactor(blendMode)),
slouken@11282
  1162
                                      GetBlendFunc(SDL_GetBlendModeDstColorFactor(blendMode)),
slouken@11282
  1163
                                      GetBlendFunc(SDL_GetBlendModeSrcAlphaFactor(blendMode)),
slouken@11282
  1164
                                      GetBlendFunc(SDL_GetBlendModeDstAlphaFactor(blendMode)));
slouken@11282
  1165
            data->glBlendEquation(GetBlendEquation(SDL_GetBlendModeColorOperation(blendMode)));
slouken@2936
  1166
        }
slouken@5355
  1167
        data->current.blendMode = blendMode;
slouken@2936
  1168
    }
slouken@2936
  1169
}
slouken@2936
  1170
slouken@5355
  1171
static void
slouken@5355
  1172
GL_SetDrawingState(SDL_Renderer * renderer)
slouken@5355
  1173
{
slouken@5355
  1174
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5355
  1175
slouken@5355
  1176
    GL_ActivateRenderer(renderer);
slouken@5355
  1177
slouken@5381
  1178
    GL_SetColor(data, renderer->r,
slouken@5381
  1179
                      renderer->g,
slouken@5381
  1180
                      renderer->b,
slouken@5381
  1181
                      renderer->a);
slouken@5355
  1182
slouken@5355
  1183
    GL_SetBlendMode(data, renderer->blendMode);
slouken@5355
  1184
slouken@5355
  1185
    GL_SetShader(data, SHADER_SOLID);
slouken@5355
  1186
}
slouken@5355
  1187
slouken@1918
  1188
static int
slouken@3596
  1189
GL_RenderClear(SDL_Renderer * renderer)
slouken@3596
  1190
{
slouken@3596
  1191
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3596
  1192
slouken@5147
  1193
    GL_ActivateRenderer(renderer);
slouken@5147
  1194
slouken@3596
  1195
    data->glClearColor((GLfloat) renderer->r * inv255f,
slouken@3596
  1196
                       (GLfloat) renderer->g * inv255f,
slouken@3596
  1197
                       (GLfloat) renderer->b * inv255f,
slouken@3596
  1198
                       (GLfloat) renderer->a * inv255f);
slouken@3596
  1199
slouken@10408
  1200
    if (renderer->clipping_enabled) {
slouken@10408
  1201
        data->glDisable(GL_SCISSOR_TEST);
slouken@10408
  1202
    }
slouken@10408
  1203
slouken@3596
  1204
    data->glClear(GL_COLOR_BUFFER_BIT);
slouken@3596
  1205
slouken@10408
  1206
    if (renderer->clipping_enabled) {
slouken@10408
  1207
        data->glEnable(GL_SCISSOR_TEST);
slouken@10408
  1208
    }
slouken@10408
  1209
slouken@3596
  1210
    return 0;
slouken@3596
  1211
}
slouken@3596
  1212
slouken@3596
  1213
static int
slouken@6528
  1214
GL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_FPoint * points,
slouken@3596
  1215
                    int count)
slouken@2884
  1216
{
slouken@2884
  1217
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@7908
  1218
    int i;
slouken@2884
  1219
slouken@5355
  1220
    GL_SetDrawingState(renderer);
slouken@2884
  1221
slouken@7908
  1222
    data->glBegin(GL_POINTS);
slouken@7908
  1223
    for (i = 0; i < count; ++i) {
slouken@7908
  1224
        data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@7908
  1225
    }
slouken@7908
  1226
    data->glEnd();
slouken@2901
  1227
slouken@2901
  1228
    return 0;
slouken@2901
  1229
}
slouken@2901
  1230
slouken@2901
  1231
static int
slouken@6528
  1232
GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_FPoint * points,
slouken@3596
  1233
                   int count)
slouken@2901
  1234
{
slouken@2901
  1235
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@7908
  1236
    int i;
slouken@2901
  1237
slouken@5355
  1238
    GL_SetDrawingState(renderer);
slouken@2901
  1239
slouken@7191
  1240
    if (count > 2 &&
slouken@3536
  1241
        points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
slouken@7908
  1242
        data->glBegin(GL_LINE_LOOP);
slouken@3536
  1243
        /* GL_LINE_LOOP takes care of the final segment */
slouken@7908
  1244
        --count;
slouken@7908
  1245
        for (i = 0; i < count; ++i) {
slouken@7908
  1246
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@7908
  1247
        }
slouken@7908
  1248
        data->glEnd();
slouken@3536
  1249
    } else {
slouken@7908
  1250
#if defined(__MACOSX__) || defined(__WIN32__)
slouken@7908
  1251
#else
slouken@7908
  1252
        int x1, y1, x2, y2;
slouken@7908
  1253
#endif
slouken@7908
  1254
slouken@7908
  1255
        data->glBegin(GL_LINE_STRIP);
slouken@7908
  1256
        for (i = 0; i < count; ++i) {
slouken@7908
  1257
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@7908
  1258
        }
slouken@7908
  1259
        data->glEnd();
slouken@7908
  1260
slouken@7908
  1261
        /* The line is half open, so we need one more point to complete it.
slouken@7908
  1262
         * http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node47.html
slouken@7908
  1263
         * If we have to, we can use vertical line and horizontal line textures
slouken@7908
  1264
         * for vertical and horizontal lines, and then create custom textures
slouken@7908
  1265
         * for diagonal lines and software render those.  It's terrible, but at
slouken@7908
  1266
         * least it would be pixel perfect.
slouken@7908
  1267
         */
slouken@7908
  1268
        data->glBegin(GL_POINTS);
slouken@7908
  1269
#if defined(__MACOSX__) || defined(__WIN32__)
slouken@7908
  1270
        /* Mac OS X and Windows seem to always leave the last point open */
slouken@7908
  1271
        data->glVertex2f(0.5f + points[count-1].x, 0.5f + points[count-1].y);
slouken@7908
  1272
#else
slouken@7908
  1273
        /* Linux seems to leave the right-most or bottom-most point open */
slouken@7908
  1274
        x1 = points[0].x;
slouken@7908
  1275
        y1 = points[0].y;
slouken@7908
  1276
        x2 = points[count-1].x;
slouken@7908
  1277
        y2 = points[count-1].y;
slouken@7908
  1278
slouken@7908
  1279
        if (x1 > x2) {
slouken@7908
  1280
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@7908
  1281
        } else if (x2 > x1) {
slouken@7908
  1282
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@7908
  1283
        }
slouken@7908
  1284
        if (y1 > y2) {
slouken@7908
  1285
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@7908
  1286
        } else if (y2 > y1) {
slouken@7908
  1287
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@7908
  1288
        }
slouken@7908
  1289
#endif
slouken@7908
  1290
        data->glEnd();
slouken@7840
  1291
    }
slouken@7194
  1292
    return GL_CheckError("", renderer);
slouken@1918
  1293
}
slouken@1918
  1294
slouken@1918
  1295
static int
slouken@6528
  1296
GL_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects, int count)
slouken@2925
  1297
{
slouken@2925
  1298
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
  1299
    int i;
slouken@2925
  1300
slouken@5355
  1301
    GL_SetDrawingState(renderer);
slouken@2936
  1302
slouken@3536
  1303
    for (i = 0; i < count; ++i) {
slouken@6528
  1304
        const SDL_FRect *rect = &rects[i];
slouken@3536
  1305
slouken@6528
  1306
        data->glRectf(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
slouken@3536
  1307
    }
slouken@7194
  1308
    return GL_CheckError("", renderer);
slouken@2925
  1309
}
slouken@2925
  1310
slouken@2925
  1311
static int
slouken@9046
  1312
GL_SetupCopy(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
  1313
{
slouken@1918
  1314
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1315
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@5147
  1316
slouken@5355
  1317
    data->glEnable(texturedata->type);
slouken@5355
  1318
    if (texturedata->yuv) {
slouken@5355
  1319
        data->glActiveTextureARB(GL_TEXTURE2_ARB);
slouken@5355
  1320
        data->glBindTexture(texturedata->type, texturedata->vtexture);
slouken@5355
  1321
slouken@5355
  1322
        data->glActiveTextureARB(GL_TEXTURE1_ARB);
slouken@5355
  1323
        data->glBindTexture(texturedata->type, texturedata->utexture);
slouken@5355
  1324
slouken@5355
  1325
        data->glActiveTextureARB(GL_TEXTURE0_ARB);
slouken@5355
  1326
    }
slouken@9046
  1327
    if (texturedata->nv12) {
slouken@9046
  1328
        data->glActiveTextureARB(GL_TEXTURE1_ARB);
slouken@9046
  1329
        data->glBindTexture(texturedata->type, texturedata->utexture);
slouken@9046
  1330
slouken@9046
  1331
        data->glActiveTextureARB(GL_TEXTURE0_ARB);
slouken@9046
  1332
    }
slouken@5355
  1333
    data->glBindTexture(texturedata->type, texturedata->texture);
slouken@5355
  1334
slouken@5355
  1335
    if (texture->modMode) {
slouken@5355
  1336
        GL_SetColor(data, texture->r, texture->g, texture->b, texture->a);
slouken@5355
  1337
    } else {
slouken@5355
  1338
        GL_SetColor(data, 255, 255, 255, 255);
slouken@5355
  1339
    }
slouken@5355
  1340
slouken@5355
  1341
    GL_SetBlendMode(data, texture->blendMode);
slouken@5355
  1342
slouken@11702
  1343
    if (texturedata->yuv || texturedata->nv12) {
slouken@11702
  1344
        switch (SDL_GetYUVConversionModeForResolution(texture->w, texture->h)) {
slouken@11702
  1345
        case SDL_YUV_CONVERSION_JPEG:
slouken@11702
  1346
            if (texturedata->yuv) {
slouken@11702
  1347
                GL_SetShader(data, SHADER_YUV_JPEG);
slouken@11702
  1348
            } else if (texture->format == SDL_PIXELFORMAT_NV12) {
slouken@11702
  1349
                GL_SetShader(data, SHADER_NV12_JPEG);
slouken@11702
  1350
            } else {
slouken@11702
  1351
                GL_SetShader(data, SHADER_NV21_JPEG);
slouken@11702
  1352
            }
slouken@11702
  1353
            break;
slouken@11702
  1354
        case SDL_YUV_CONVERSION_BT601:
slouken@11702
  1355
            if (texturedata->yuv) {
slouken@11702
  1356
                GL_SetShader(data, SHADER_YUV_BT601);
slouken@11702
  1357
            } else if (texture->format == SDL_PIXELFORMAT_NV12) {
slouken@11702
  1358
                GL_SetShader(data, SHADER_NV12_BT601);
slouken@11702
  1359
            } else {
slouken@11702
  1360
                GL_SetShader(data, SHADER_NV21_BT601);
slouken@11702
  1361
            }
slouken@11702
  1362
            break;
slouken@11702
  1363
        case SDL_YUV_CONVERSION_BT709:
slouken@11702
  1364
            if (texturedata->yuv) {
slouken@11702
  1365
                GL_SetShader(data, SHADER_YUV_BT709);
slouken@11702
  1366
            } else if (texture->format == SDL_PIXELFORMAT_NV12) {
slouken@11702
  1367
                GL_SetShader(data, SHADER_NV12_BT709);
slouken@11702
  1368
            } else {
slouken@11702
  1369
                GL_SetShader(data, SHADER_NV21_BT709);
slouken@11702
  1370
            }
slouken@11702
  1371
            break;
slouken@11702
  1372
        default:
slouken@11702
  1373
            return SDL_SetError("Unsupported YUV conversion mode");
slouken@9046
  1374
        }
slouken@5355
  1375
    } else {
slouken@5355
  1376
        GL_SetShader(data, SHADER_RGB);
slouken@5355
  1377
    }
slouken@9046
  1378
    return 0;
slouken@9046
  1379
}
slouken@9046
  1380
slouken@9046
  1381
static int
slouken@9046
  1382
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@9046
  1383
              const SDL_Rect * srcrect, const SDL_FRect * dstrect)
slouken@9046
  1384
{
slouken@9046
  1385
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@9046
  1386
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@9046
  1387
    GLfloat minx, miny, maxx, maxy;
slouken@9046
  1388
    GLfloat minu, maxu, minv, maxv;
slouken@9046
  1389
slouken@9046
  1390
    GL_ActivateRenderer(renderer);
slouken@9046
  1391
slouken@9046
  1392
    if (GL_SetupCopy(renderer, texture) < 0) {
slouken@9046
  1393
        return -1;
slouken@9046
  1394
    }
slouken@5355
  1395
slouken@1918
  1396
    minx = dstrect->x;
slouken@1918
  1397
    miny = dstrect->y;
slouken@1918
  1398
    maxx = dstrect->x + dstrect->w;
slouken@1918
  1399
    maxy = dstrect->y + dstrect->h;
slouken@1918
  1400
slouken@1918
  1401
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
  1402
    minu *= texturedata->texw;
slouken@1918
  1403
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
  1404
    maxu *= texturedata->texw;
slouken@1918
  1405
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
  1406
    minv *= texturedata->texh;
slouken@1918
  1407
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
  1408
    maxv *= texturedata->texh;
slouken@1918
  1409
slouken@1927
  1410
    data->glBegin(GL_TRIANGLE_STRIP);
slouken@1927
  1411
    data->glTexCoord2f(minu, minv);
slouken@6528
  1412
    data->glVertex2f(minx, miny);
slouken@1927
  1413
    data->glTexCoord2f(maxu, minv);
slouken@6528
  1414
    data->glVertex2f(maxx, miny);
slouken@1927
  1415
    data->glTexCoord2f(minu, maxv);
slouken@6528
  1416
    data->glVertex2f(minx, maxy);
slouken@1927
  1417
    data->glTexCoord2f(maxu, maxv);
slouken@6528
  1418
    data->glVertex2f(maxx, maxy);
slouken@1927
  1419
    data->glEnd();
slouken@1918
  1420
slouken@2884
  1421
    data->glDisable(texturedata->type);
slouken@2884
  1422
slouken@7194
  1423
    return GL_CheckError("", renderer);
slouken@1918
  1424
}
slouken@1918
  1425
slouken@3431
  1426
static int
gabomdq@6320
  1427
GL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
  1428
              const SDL_Rect * srcrect, const SDL_FRect * dstrect,
slouken@6528
  1429
              const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
gabomdq@6320
  1430
{
gabomdq@6320
  1431
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
gabomdq@6320
  1432
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
gabomdq@6320
  1433
    GLfloat minx, miny, maxx, maxy;
gabomdq@6320
  1434
    GLfloat centerx, centery;
gabomdq@6320
  1435
    GLfloat minu, maxu, minv, maxv;
slouken@7194
  1436
gabomdq@6320
  1437
    GL_ActivateRenderer(renderer);
gabomdq@6320
  1438
slouken@9046
  1439
    if (GL_SetupCopy(renderer, texture) < 0) {
slouken@9046
  1440
        return -1;
gabomdq@6320
  1441
    }
gabomdq@6320
  1442
slouken@6528
  1443
    centerx = center->x;
slouken@6528
  1444
    centery = center->y;
gabomdq@6320
  1445
slouken@7989
  1446
    if (flip & SDL_FLIP_HORIZONTAL) {
slouken@7989
  1447
        minx =  dstrect->w - centerx;
slouken@7989
  1448
        maxx = -centerx;
slouken@7989
  1449
    }
slouken@7989
  1450
    else {
slouken@7989
  1451
        minx = -centerx;
slouken@7989
  1452
        maxx =  dstrect->w - centerx;
slouken@7989
  1453
    }
slouken@7989
  1454
slouken@7989
  1455
    if (flip & SDL_FLIP_VERTICAL) {
slouken@7989
  1456
        miny =  dstrect->h - centery;
slouken@7976
  1457
        maxy = -centery;
slouken@7989
  1458
    }
slouken@7989
  1459
    else {
slouken@7976
  1460
        miny = -centery;
slouken@7989
  1461
        maxy =  dstrect->h - centery;
gabomdq@6320
  1462
    }
gabomdq@6320
  1463
gabomdq@6320
  1464
    minu = (GLfloat) srcrect->x / texture->w;
gabomdq@6320
  1465
    minu *= texturedata->texw;
gabomdq@6320
  1466
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
gabomdq@6320
  1467
    maxu *= texturedata->texw;
gabomdq@6320
  1468
    minv = (GLfloat) srcrect->y / texture->h;
gabomdq@6320
  1469
    minv *= texturedata->texh;
gabomdq@6320
  1470
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
gabomdq@6320
  1471
    maxv *= texturedata->texh;
gabomdq@6320
  1472
slouken@7191
  1473
    /* Translate to flip, rotate, translate to position */
gabomdq@6320
  1474
    data->glPushMatrix();
slouken@7191
  1475
    data->glTranslatef((GLfloat)dstrect->x + centerx, (GLfloat)dstrect->y + centery, (GLfloat)0.0);
gabomdq@6320
  1476
    data->glRotated(angle, (GLdouble)0.0, (GLdouble)0.0, (GLdouble)1.0);
slouken@7191
  1477
gabomdq@6320
  1478
    data->glBegin(GL_TRIANGLE_STRIP);
gabomdq@6320
  1479
    data->glTexCoord2f(minu, minv);
gabomdq@6320
  1480
    data->glVertex2f(minx, miny);
gabomdq@6320
  1481
    data->glTexCoord2f(maxu, minv);
gabomdq@6320
  1482
    data->glVertex2f(maxx, miny);
gabomdq@6320
  1483
    data->glTexCoord2f(minu, maxv);
gabomdq@6320
  1484
    data->glVertex2f(minx, maxy);
gabomdq@6320
  1485
    data->glTexCoord2f(maxu, maxv);
gabomdq@6320
  1486
    data->glVertex2f(maxx, maxy);
gabomdq@6320
  1487
    data->glEnd();
gabomdq@6320
  1488
    data->glPopMatrix();
slouken@7191
  1489
gabomdq@6320
  1490
    data->glDisable(texturedata->type);
gabomdq@6320
  1491
slouken@7194
  1492
    return GL_CheckError("", renderer);
gabomdq@6320
  1493
}
gabomdq@6320
  1494
gabomdq@6320
  1495
static int
slouken@3431
  1496
GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
  1497
                    Uint32 pixel_format, void * pixels, int pitch)
slouken@3431
  1498
{
slouken@3433
  1499
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
philipp@10456
  1500
    Uint32 temp_format = renderer->target ? renderer->target->format : SDL_PIXELFORMAT_ARGB8888;
slouken@5465
  1501
    void *temp_pixels;
slouken@5465
  1502
    int temp_pitch;
slouken@3433
  1503
    GLint internalFormat;
slouken@3433
  1504
    GLenum format, type;
slouken@3435
  1505
    Uint8 *src, *dst, *tmp;
slouken@5154
  1506
    int w, h, length, rows;
slouken@5465
  1507
    int status;
slouken@3433
  1508
slouken@5147
  1509
    GL_ActivateRenderer(renderer);
slouken@5147
  1510
icculus@10650
  1511
    if (!convert_format(data, temp_format, &internalFormat, &format, &type)) {
icculus@10650
  1512
        return SDL_SetError("Texture format %s not supported by OpenGL",
icculus@10650
  1513
                            SDL_GetPixelFormatName(temp_format));
icculus@10650
  1514
    }
icculus@10650
  1515
icculus@10650
  1516
    if (!rect->w || !rect->h) {
icculus@10650
  1517
        return 0;  /* nothing to do. */
icculus@10650
  1518
    }
icculus@10650
  1519
slouken@5465
  1520
    temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
slouken@5465
  1521
    temp_pixels = SDL_malloc(rect->h * temp_pitch);
slouken@5465
  1522
    if (!temp_pixels) {
icculus@7037
  1523
        return SDL_OutOfMemory();
slouken@3433
  1524
    }
slouken@3433
  1525
slouken@7420
  1526
    SDL_GetRendererOutputSize(renderer, &w, &h);
slouken@5154
  1527
slouken@3446
  1528
    data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
slouken@3446
  1529
    data->glPixelStorei(GL_PACK_ROW_LENGTH,
slouken@5465
  1530
                        (temp_pitch / SDL_BYTESPERPIXEL(temp_format)));
slouken@3433
  1531
slouken@10405
  1532
    data->glReadPixels(rect->x, renderer->target ? rect->y : (h-rect->y)-rect->h,
slouken@10405
  1533
                       rect->w, rect->h, format, type, temp_pixels);
slouken@3435
  1534
slouken@7779
  1535
    if (GL_CheckError("glReadPixels()", renderer) < 0) {
slouken@9115
  1536
        SDL_free(temp_pixels);
slouken@7779
  1537
        return -1;
slouken@7779
  1538
    }
slouken@6494
  1539
slouken@10405
  1540
    /* Flip the rows to be top-down if necessary */
slouken@10405
  1541
    if (!renderer->target) {
slouken@10405
  1542
        length = rect->w * SDL_BYTESPERPIXEL(temp_format);
slouken@10405
  1543
        src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
slouken@10405
  1544
        dst = (Uint8*)temp_pixels;
slouken@10405
  1545
        tmp = SDL_stack_alloc(Uint8, length);
slouken@10405
  1546
        rows = rect->h / 2;
slouken@10405
  1547
        while (rows--) {
slouken@10405
  1548
            SDL_memcpy(tmp, dst, length);
slouken@10405
  1549
            SDL_memcpy(dst, src, length);
slouken@10405
  1550
            SDL_memcpy(src, tmp, length);
slouken@10405
  1551
            dst += temp_pitch;
slouken@10405
  1552
            src -= temp_pitch;
slouken@10405
  1553
        }
slouken@10405
  1554
        SDL_stack_free(tmp);
slouken@3435
  1555
    }
slouken@3440
  1556
slouken@5465
  1557
    status = SDL_ConvertPixels(rect->w, rect->h,
slouken@5465
  1558
                               temp_format, temp_pixels, temp_pitch,
slouken@5465
  1559
                               pixel_format, pixels, pitch);
slouken@5465
  1560
    SDL_free(temp_pixels);
slouken@5465
  1561
slouken@5465
  1562
    return status;
slouken@3431
  1563
}
slouken@3431
  1564
slouken@1918
  1565
static void
slouken@1918
  1566
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
  1567
{
slouken@5147
  1568
    GL_ActivateRenderer(renderer);
slouken@5147
  1569
slouken@1918
  1570
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
  1571
}
slouken@1918
  1572
slouken@1918
  1573
static void
slouken@1918
  1574
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
  1575
{
slouken@1927
  1576
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1577
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
  1578
slouken@5147
  1579
    GL_ActivateRenderer(renderer);
slouken@5147
  1580
slouken@1918
  1581
    if (!data) {
slouken@1918
  1582
        return;
slouken@1918
  1583
    }
slouken@1918
  1584
    if (data->texture) {
slouken@1927
  1585
        renderdata->glDeleteTextures(1, &data->texture);
slouken@1918
  1586
    }
slouken@5264
  1587
    if (data->yuv) {
slouken@5264
  1588
        renderdata->glDeleteTextures(1, &data->utexture);
slouken@5264
  1589
        renderdata->glDeleteTextures(1, &data->vtexture);
slouken@5264
  1590
    }
slouken@7719
  1591
    SDL_free(data->pixels);
slouken@1918
  1592
    SDL_free(data);
slouken@1918
  1593
    texture->driverdata = NULL;
slouken@1918
  1594
}
slouken@1918
  1595
slouken@1975
  1596
static void
slouken@1918
  1597
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
  1598
{
slouken@1918
  1599
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1600
slouken@1918
  1601
    if (data) {
icculus@11013
  1602
        if (data->context != NULL) {
icculus@11013
  1603
            /* make sure we delete the right resources! */
icculus@11013
  1604
            GL_ActivateRenderer(renderer);
icculus@11013
  1605
        }
icculus@11013
  1606
slouken@7194
  1607
        GL_ClearErrors(renderer);
slouken@7194
  1608
        if (data->GL_ARB_debug_output_supported) {
sezero@11872
  1609
            PFNGLDEBUGMESSAGECALLBACKARBPROC glDebugMessageCallbackARBFunc = (PFNGLDEBUGMESSAGECALLBACKARBPROC) SDL_GL_GetProcAddress("glDebugMessageCallbackARB");
slouken@7194
  1610
slouken@7194
  1611
            /* 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
  1612
            /* For now, just always replace the callback with the original one */
slouken@7194
  1613
            glDebugMessageCallbackARBFunc(data->next_error_callback, data->next_error_userparam);
slouken@7194
  1614
        }
icculus@5566
  1615
        if (data->shaders) {
icculus@5566
  1616
            GL_DestroyShaderContext(data->shaders);
icculus@5566
  1617
        }
slouken@1920
  1618
        if (data->context) {
slouken@6232
  1619
            while (data->framebuffers) {
slouken@6232
  1620
                GL_FBOList *nextnode = data->framebuffers->next;
slouken@6232
  1621
                /* delete the framebuffer object */
slouken@6232
  1622
                data->glDeleteFramebuffersEXT(1, &data->framebuffers->FBO);
slouken@6494
  1623
                GL_CheckError("", renderer);
slouken@6232
  1624
                SDL_free(data->framebuffers);
slouken@6232
  1625
                data->framebuffers = nextnode;
jorgen@7091
  1626
            }
slouken@1920
  1627
            SDL_GL_DeleteContext(data->context);
slouken@1918
  1628
        }
slouken@1918
  1629
        SDL_free(data);
slouken@1918
  1630
    }
slouken@1918
  1631
    SDL_free(renderer);
slouken@1918
  1632
}
slouken@1918
  1633
slouken@7194
  1634
static int
slouken@7194
  1635
GL_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh)
slouken@7194
  1636
{
gabomdq@6414
  1637
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
gabomdq@6414
  1638
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
gabomdq@6414
  1639
    GL_ActivateRenderer(renderer);
gabomdq@6414
  1640
gabomdq@6414
  1641
    data->glEnable(texturedata->type);
gabomdq@6414
  1642
    if (texturedata->yuv) {
gabomdq@6414
  1643
        data->glActiveTextureARB(GL_TEXTURE2_ARB);
gabomdq@6414
  1644
        data->glBindTexture(texturedata->type, texturedata->vtexture);
gabomdq@6414
  1645
gabomdq@6414
  1646
        data->glActiveTextureARB(GL_TEXTURE1_ARB);
gabomdq@6414
  1647
        data->glBindTexture(texturedata->type, texturedata->utexture);
gabomdq@6414
  1648
gabomdq@6414
  1649
        data->glActiveTextureARB(GL_TEXTURE0_ARB);
gabomdq@6414
  1650
    }
gabomdq@6414
  1651
    data->glBindTexture(texturedata->type, texturedata->texture);
gabomdq@6414
  1652
gabomdq@6414
  1653
    if(texw) *texw = (float)texturedata->texw;
gabomdq@6414
  1654
    if(texh) *texh = (float)texturedata->texh;
gabomdq@6414
  1655
gabomdq@6414
  1656
    return 0;
gabomdq@6414
  1657
}
gabomdq@6414
  1658
slouken@7194
  1659
static int
slouken@7194
  1660
GL_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture)
slouken@7194
  1661
{
gabomdq@6414
  1662
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
gabomdq@6414
  1663
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
gabomdq@6414
  1664
    GL_ActivateRenderer(renderer);
gabomdq@6414
  1665
gabomdq@6414
  1666
    if (texturedata->yuv) {
gabomdq@6414
  1667
        data->glActiveTextureARB(GL_TEXTURE2_ARB);
gabomdq@6414
  1668
        data->glDisable(texturedata->type);
gabomdq@6414
  1669
gabomdq@6414
  1670
        data->glActiveTextureARB(GL_TEXTURE1_ARB);
gabomdq@6414
  1671
        data->glDisable(texturedata->type);
gabomdq@6414
  1672
gabomdq@6414
  1673
        data->glActiveTextureARB(GL_TEXTURE0_ARB);
gabomdq@6414
  1674
    }
slouken@7191
  1675
gabomdq@6414
  1676
    data->glDisable(texturedata->type);
gabomdq@6414
  1677
gabomdq@6414
  1678
    return 0;
gabomdq@6414
  1679
}
gabomdq@6414
  1680
slouken@5226
  1681
#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
slouken@1918
  1682
slouken@1918
  1683
/* vi: set ts=4 sw=4 expandtab: */