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