src/render/opengl/SDL_render_gl.c
author Alex Szpakowski <slime73@gmail.com>
Thu, 10 Dec 2015 20:25:34 -0400
changeset 9958 687e118144c7
parent 9680 1d13a878b066
child 9977 f9cd179cf50e
permissions -rw-r--r--
SDL_GL_GetAttribute: If a GL context isn't active, only return failure when the specified attribute needs an active GL context to be queried.
slouken@1918
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9619
     3
  Copyright (C) 1997-2015 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@1924
   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@1924
   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@6494
   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@1924
   256
    }
slouken@6494
   257
    return ret;
slouken@1924
   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@6494
   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@5233
   393
    const char *hint;
slouken@1952
   394
    GLint value;
slouken@5154
   395
    Uint32 window_flags;
slime73@9958
   396
    int profile_mask = 0, major = 0, minor = 0;
slouken@8906
   397
    SDL_bool changed_window = SDL_FALSE;
gabomdq@8257
   398
gabomdq@8257
   399
    SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profile_mask);
gabomdq@8257
   400
    SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
gabomdq@8257
   401
    SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
gabomdq@8257
   402
    
slouken@5154
   403
    window_flags = SDL_GetWindowFlags(window);
gabomdq@8257
   404
    if (!(window_flags & SDL_WINDOW_OPENGL) ||
gabomdq@8264
   405
        profile_mask == SDL_GL_CONTEXT_PROFILE_ES || major != RENDERER_CONTEXT_MAJOR || minor != RENDERER_CONTEXT_MINOR) {
slouken@8906
   406
slouken@8906
   407
        changed_window = SDL_TRUE;
gabomdq@8264
   408
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0);
gabomdq@8264
   409
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, RENDERER_CONTEXT_MAJOR);
gabomdq@8264
   410
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, RENDERER_CONTEXT_MINOR);
gabomdq@8264
   411
slouken@5154
   412
        if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
slouken@8906
   413
            goto error;
slouken@1924
   414
        }
slouken@1918
   415
    }
slouken@1918
   416
slouken@1920
   417
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1918
   418
    if (!renderer) {
slouken@1918
   419
        SDL_OutOfMemory();
slouken@8906
   420
        goto error;
slouken@1918
   421
    }
slouken@1918
   422
slouken@1920
   423
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   424
    if (!data) {
slouken@1918
   425
        GL_DestroyRenderer(renderer);
slouken@1918
   426
        SDL_OutOfMemory();
slouken@8906
   427
        goto error;
slouken@1918
   428
    }
slouken@1918
   429
slouken@5147
   430
    renderer->WindowEvent = GL_WindowEvent;
urkle@7746
   431
    renderer->GetOutputSize = GL_GetOutputSize;
slouken@1918
   432
    renderer->CreateTexture = GL_CreateTexture;
slouken@1918
   433
    renderer->UpdateTexture = GL_UpdateTexture;
slouken@7759
   434
    renderer->UpdateTextureYUV = GL_UpdateTextureYUV;
slouken@1918
   435
    renderer->LockTexture = GL_LockTexture;
slouken@1918
   436
    renderer->UnlockTexture = GL_UnlockTexture;
slouken@6247
   437
    renderer->SetRenderTarget = GL_SetRenderTarget;
slouken@5297
   438
    renderer->UpdateViewport = GL_UpdateViewport;
slouken@7141
   439
    renderer->UpdateClipRect = GL_UpdateClipRect;
slouken@3596
   440
    renderer->RenderClear = GL_RenderClear;
slouken@3596
   441
    renderer->RenderDrawPoints = GL_RenderDrawPoints;
slouken@3596
   442
    renderer->RenderDrawLines = GL_RenderDrawLines;
slouken@3596
   443
    renderer->RenderFillRects = GL_RenderFillRects;
slouken@1918
   444
    renderer->RenderCopy = GL_RenderCopy;
gabomdq@6320
   445
    renderer->RenderCopyEx = GL_RenderCopyEx;
slouken@3431
   446
    renderer->RenderReadPixels = GL_RenderReadPixels;
slouken@1918
   447
    renderer->RenderPresent = GL_RenderPresent;
slouken@1918
   448
    renderer->DestroyTexture = GL_DestroyTexture;
slouken@1918
   449
    renderer->DestroyRenderer = GL_DestroyRenderer;
gabomdq@6414
   450
    renderer->GL_BindTexture = GL_BindTexture;
gabomdq@6414
   451
    renderer->GL_UnbindTexture = GL_UnbindTexture;
slouken@1918
   452
    renderer->info = GL_RenderDriver.info;
slouken@8590
   453
    renderer->info.flags = (SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
slouken@1918
   454
    renderer->driverdata = data;
slouken@6171
   455
    renderer->window = window;
slouken@1918
   456
slouken@3685
   457
    data->context = SDL_GL_CreateContext(window);
slouken@1918
   458
    if (!data->context) {
slouken@1918
   459
        GL_DestroyRenderer(renderer);
slouken@8906
   460
        goto error;
slouken@1918
   461
    }
slouken@3685
   462
    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
slouken@1918
   463
        GL_DestroyRenderer(renderer);
slouken@8906
   464
        goto error;
slouken@1918
   465
    }
slouken@5204
   466
slouken@5204
   467
    if (GL_LoadFunctions(data) < 0) {
slouken@5204
   468
        GL_DestroyRenderer(renderer);
slouken@8906
   469
        goto error;
slouken@5204
   470
    }
slouken@5204
   471
slouken@2246
   472
#ifdef __MACOSX__
slouken@2246
   473
    /* Enable multi-threaded rendering */
slouken@2246
   474
    /* Disabled until Ryan finishes his VBO/PBO code...
bob@2295
   475
       CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
bob@2295
   476
     */
slouken@2246
   477
#endif
slouken@2246
   478
slouken@1965
   479
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1918
   480
        SDL_GL_SetSwapInterval(1);
slouken@1918
   481
    } else {
slouken@1918
   482
        SDL_GL_SetSwapInterval(0);
slouken@1918
   483
    }
slouken@1918
   484
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@1965
   485
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1918
   486
    }
slouken@1918
   487
slouken@7194
   488
    /* Check for debug output support */
slouken@7198
   489
    if (SDL_GL_GetAttribute(SDL_GL_CONTEXT_FLAGS, &value) == 0 &&
slouken@7198
   490
        (value & SDL_GL_CONTEXT_DEBUG_FLAG)) {
slouken@7198
   491
        data->debug_enabled = SDL_TRUE;
slouken@7198
   492
    }
slouken@7198
   493
    if (data->debug_enabled && SDL_GL_ExtensionSupported("GL_ARB_debug_output")) {
slouken@7194
   494
        PFNGLDEBUGMESSAGECALLBACKARBPROC glDebugMessageCallbackARBFunc = (PFNGLDEBUGMESSAGECALLBACKARBPROC) SDL_GL_GetProcAddress("glDebugMessageCallbackARB");
slouken@7194
   495
slouken@7194
   496
        data->GL_ARB_debug_output_supported = SDL_TRUE;
slouken@7194
   497
        data->glGetPointerv(GL_DEBUG_CALLBACK_FUNCTION_ARB, (GLvoid **)&data->next_error_callback);
slouken@7194
   498
        data->glGetPointerv(GL_DEBUG_CALLBACK_USER_PARAM_ARB, &data->next_error_userparam);
slouken@7194
   499
        glDebugMessageCallbackARBFunc(GL_HandleDebugMessage, renderer);
slouken@7198
   500
slouken@7198
   501
        /* Make sure our callback is called when errors actually happen */
slouken@7198
   502
        data->glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
slouken@7194
   503
    }
slouken@7194
   504
slouken@9063
   505
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_non_power_of_two")) {
slouken@9063
   506
        data->GL_ARB_texture_non_power_of_two_supported = SDL_TRUE;
slouken@9063
   507
    } else if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle") ||
slouken@9063
   508
               SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
slouken@1926
   509
        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
slouken@9063
   510
    }
slouken@9063
   511
    if (data->GL_ARB_texture_rectangle_supported) {
slouken@6449
   512
        data->glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_ARB, &value);
slouken@6449
   513
        renderer->info.max_texture_width = value;
slouken@6449
   514
        renderer->info.max_texture_height = value;
slouken@6449
   515
    } else {
slouken@6449
   516
        data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@6449
   517
        renderer->info.max_texture_width = value;
slouken@6449
   518
        renderer->info.max_texture_height = value;
slouken@1926
   519
    }
slouken@1920
   520
slouken@5228
   521
    /* Check for multitexture support */
slouken@5228
   522
    if (SDL_GL_ExtensionSupported("GL_ARB_multitexture")) {
slouken@5228
   523
        data->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glActiveTextureARB");
slouken@5228
   524
        if (data->glActiveTextureARB) {
slouken@5228
   525
            data->GL_ARB_multitexture_supported = SDL_TRUE;
slouken@5228
   526
            data->glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &data->num_texture_units);
slouken@5228
   527
        }
slouken@5228
   528
    }
slouken@5228
   529
slouken@5228
   530
    /* Check for shader support */
slouken@5233
   531
    hint = SDL_GetHint(SDL_HINT_RENDER_OPENGL_SHADERS);
slouken@5233
   532
    if (!hint || *hint != '0') {
slouken@5233
   533
        data->shaders = GL_CreateShaderContext();
slouken@5233
   534
    }
slouken@5233
   535
    SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, "OpenGL shaders: %s",
slouken@5233
   536
                data->shaders ? "ENABLED" : "DISABLED");
slouken@5228
   537
slouken@5228
   538
    /* We support YV12 textures using 3 textures and a shader */
slouken@5228
   539
    if (data->shaders && data->num_texture_units >= 3) {
slouken@5228
   540
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_YV12;
slouken@5264
   541
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_IYUV;
slouken@9046
   542
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_NV12;
slouken@9046
   543
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_NV21;
slouken@5228
   544
    }
slouken@7191
   545
slouken@7814
   546
#ifdef __MACOSX__
slouken@7814
   547
    renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_UYVY;
slouken@7814
   548
#endif
slouken@7814
   549
slouken@6232
   550
    if (SDL_GL_ExtensionSupported("GL_EXT_framebuffer_object")) {
slouken@6232
   551
        data->GL_EXT_framebuffer_object_supported = SDL_TRUE;
slouken@6232
   552
        data->glGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC)
slouken@6232
   553
            SDL_GL_GetProcAddress("glGenFramebuffersEXT");
slouken@6232
   554
        data->glDeleteFramebuffersEXT = (PFNGLDELETEFRAMEBUFFERSEXTPROC)
slouken@6232
   555
            SDL_GL_GetProcAddress("glDeleteFramebuffersEXT");
slouken@6232
   556
        data->glFramebufferTexture2DEXT = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)
slouken@6232
   557
            SDL_GL_GetProcAddress("glFramebufferTexture2DEXT");
slouken@6232
   558
        data->glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)
slouken@6232
   559
            SDL_GL_GetProcAddress("glBindFramebufferEXT");
slouken@6246
   560
        data->glCheckFramebufferStatusEXT = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)
slouken@6232
   561
            SDL_GL_GetProcAddress("glCheckFramebufferStatusEXT");
slouken@6246
   562
        renderer->info.flags |= SDL_RENDERER_TARGETTEXTURE;
slouken@6232
   563
    }
slouken@6232
   564
    data->framebuffers = NULL;
slouken@5228
   565
slouken@1918
   566
    /* Set up parameters for rendering */
slouken@5355
   567
    GL_ResetState(renderer);
slouken@1918
   568
slouken@1918
   569
    return renderer;
slouken@8906
   570
slouken@8906
   571
error:
slouken@8906
   572
    if (changed_window) {
slouken@8906
   573
        /* Uh oh, better try to put it back... */
slouken@8906
   574
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile_mask);
slouken@8906
   575
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
slouken@8906
   576
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
slouken@8906
   577
        SDL_RecreateWindow(window, window_flags);
slouken@8906
   578
    }
slouken@8906
   579
    return NULL;
slouken@1918
   580
}
slouken@1918
   581
slouken@5147
   582
static void
slouken@5147
   583
GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1970
   584
{
slouken@6260
   585
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED ||
slouken@6260
   586
        event->event == SDL_WINDOWEVENT_SHOWN ||
slouken@6260
   587
        event->event == SDL_WINDOWEVENT_HIDDEN) {
slouken@5147
   588
        /* Rebind the context to the window area and update matrices */
slouken@5147
   589
        SDL_CurrentContext = NULL;
slouken@5147
   590
    }
slouken@1923
   591
}
slouken@1923
   592
urkle@7746
   593
static int
urkle@7746
   594
GL_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
urkle@7746
   595
{
urkle@7746
   596
    SDL_GL_GetDrawableSize(renderer->window, w, h);
urkle@7746
   597
urkle@7746
   598
    return 0;
urkle@7746
   599
}
urkle@7746
   600
slouken@7194
   601
SDL_FORCE_INLINE int
slouken@1922
   602
power_of_2(int input)
slouken@1922
   603
{
slouken@1922
   604
    int value = 1;
slouken@1922
   605
slouken@1922
   606
    while (value < input) {
slouken@1922
   607
        value <<= 1;
slouken@1922
   608
    }
slouken@1922
   609
    return value;
slouken@1922
   610
}
slouken@1922
   611
slouken@7194
   612
SDL_FORCE_INLINE SDL_bool
slouken@3433
   613
convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
slouken@3433
   614
               GLint* internalFormat, GLenum* format, GLenum* type)
slouken@3433
   615
{
slouken@3433
   616
    switch (pixel_format) {
slouken@3433
   617
    case SDL_PIXELFORMAT_ARGB8888:
slouken@3433
   618
        *internalFormat = GL_RGBA8;
slouken@3433
   619
        *format = GL_BGRA;
slouken@5156
   620
        *type = GL_UNSIGNED_INT_8_8_8_8_REV;
slouken@3433
   621
        break;
slouken@5264
   622
    case SDL_PIXELFORMAT_YV12:
slouken@5264
   623
    case SDL_PIXELFORMAT_IYUV:
slouken@9046
   624
    case SDL_PIXELFORMAT_NV12:
slouken@9046
   625
    case SDL_PIXELFORMAT_NV21:
slouken@5264
   626
        *internalFormat = GL_LUMINANCE;
slouken@5264
   627
        *format = GL_LUMINANCE;
slouken@5264
   628
        *type = GL_UNSIGNED_BYTE;
slouken@5264
   629
        break;
slouken@7814
   630
#ifdef __MACOSX__
slouken@7814
   631
    case SDL_PIXELFORMAT_UYVY:
slouken@9046
   632
        *internalFormat = GL_RGB8;
slouken@9046
   633
        *format = GL_YCBCR_422_APPLE;
slouken@9046
   634
        *type = GL_UNSIGNED_SHORT_8_8_APPLE;
slouken@9046
   635
        break;
slouken@7814
   636
#endif
slouken@3433
   637
    default:
slouken@3433
   638
        return SDL_FALSE;
slouken@3433
   639
    }
slouken@3433
   640
    return SDL_TRUE;
slouken@3433
   641
}
icculus@2835
   642
slouken@5484
   643
static GLenum
slouken@5484
   644
GetScaleQuality(void)
slouken@5484
   645
{
slouken@5484
   646
    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
slouken@5484
   647
slouken@5484
   648
    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
slouken@5484
   649
        return GL_NEAREST;
slouken@5484
   650
    } else {
slouken@5484
   651
        return GL_LINEAR;
slouken@5484
   652
    }
slouken@5484
   653
}
slouken@5484
   654
slouken@1918
   655
static int
slouken@1918
   656
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   657
{
slouken@1918
   658
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   659
    GL_TextureData *data;
slouken@1920
   660
    GLint internalFormat;
slouken@1920
   661
    GLenum format, type;
slouken@1922
   662
    int texture_w, texture_h;
slouken@5503
   663
    GLenum scaleMode;
slouken@1918
   664
slouken@5147
   665
    GL_ActivateRenderer(renderer);
slouken@5147
   666
slouken@3433
   667
    if (!convert_format(renderdata, texture->format, &internalFormat,
slouken@3433
   668
                        &format, &type)) {
icculus@7037
   669
        return SDL_SetError("Texture format %s not supported by OpenGL",
icculus@7037
   670
                            SDL_GetPixelFormatName(texture->format));
slouken@1920
   671
    }
slouken@1920
   672
slouken@1920
   673
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   674
    if (!data) {
icculus@7037
   675
        return SDL_OutOfMemory();
slouken@1918
   676
    }
slouken@1918
   677
slouken@2222
   678
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5264
   679
        size_t size;
slouken@5156
   680
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
slouken@5264
   681
        size = texture->h * data->pitch;
slouken@5264
   682
        if (texture->format == SDL_PIXELFORMAT_YV12 ||
slouken@5264
   683
            texture->format == SDL_PIXELFORMAT_IYUV) {
slouken@5264
   684
            /* Need to add size for the U and V planes */
slouken@5264
   685
            size += (2 * (texture->h * data->pitch) / 4);
slouken@5264
   686
        }
slouken@9046
   687
        if (texture->format == SDL_PIXELFORMAT_NV12 ||
slouken@9046
   688
            texture->format == SDL_PIXELFORMAT_NV21) {
slouken@9046
   689
            /* Need to add size for the U/V plane */
slouken@9046
   690
            size += ((texture->h * data->pitch) / 2);
slouken@9046
   691
        }
slouken@5402
   692
        data->pixels = SDL_calloc(1, size);
slouken@2222
   693
        if (!data->pixels) {
slouken@2222
   694
            SDL_free(data);
icculus@7037
   695
            return SDL_OutOfMemory();
slouken@2222
   696
        }
slouken@2222
   697
    }
slouken@2222
   698
slouken@6232
   699
    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
slouken@6232
   700
        data->fbo = GL_GetFBO(renderdata, texture->w, texture->h);
slouken@6232
   701
    } else {
slouken@6232
   702
        data->fbo = NULL;
slouken@6232
   703
    }
slouken@1918
   704
slouken@6494
   705
    GL_CheckError("", renderer);
slouken@1927
   706
    renderdata->glGenTextures(1, &data->texture);
slouken@9063
   707
    if (GL_CheckError("glGenTextures()", renderer) < 0) {
slouken@9011
   708
        if (data->pixels) {
slouken@9011
   709
            SDL_free(data->pixels);
slouken@9011
   710
        }
slouken@7624
   711
        SDL_free(data);
slouken@7624
   712
        return -1;
slouken@7624
   713
    }
slouken@7779
   714
    texture->driverdata = data;
slouken@7779
   715
slouken@9063
   716
    if (renderdata->GL_ARB_texture_non_power_of_two_supported) {
slouken@9063
   717
        data->type = GL_TEXTURE_2D;
slouken@9063
   718
        texture_w = texture->w;
slouken@9063
   719
        texture_h = texture->h;
slouken@9063
   720
        data->texw = 1.0f;
slouken@9063
   721
        data->texh = 1.0f;
slouken@9063
   722
    } else if (renderdata->GL_ARB_texture_rectangle_supported) {
slouken@1926
   723
        data->type = GL_TEXTURE_RECTANGLE_ARB;
slouken@1926
   724
        texture_w = texture->w;
slouken@1926
   725
        texture_h = texture->h;
icculus@2835
   726
        data->texw = (GLfloat) texture_w;
icculus@2835
   727
        data->texh = (GLfloat) texture_h;
slouken@1926
   728
    } else {
slouken@1926
   729
        data->type = GL_TEXTURE_2D;
slouken@1926
   730
        texture_w = power_of_2(texture->w);
slouken@1926
   731
        texture_h = power_of_2(texture->h);
icculus@2835
   732
        data->texw = (GLfloat) (texture->w) / texture_w;
slouken@1926
   733
        data->texh = (GLfloat) texture->h / texture_h;
slouken@1926
   734
    }
icculus@2835
   735
slouken@1920
   736
    data->format = format;
slouken@1920
   737
    data->formattype = type;
slouken@5503
   738
    scaleMode = GetScaleQuality();
slouken@2884
   739
    renderdata->glEnable(data->type);
slouken@1927
   740
    renderdata->glBindTexture(data->type, data->texture);
slouken@5503
   741
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER, scaleMode);
slouken@5503
   742
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER, scaleMode);
slouken@6207
   743
    /* According to the spec, CLAMP_TO_EDGE is the default for TEXTURE_RECTANGLE
slouken@6207
   744
       and setting it causes an INVALID_ENUM error in the latest NVidia drivers.
slouken@6207
   745
    */
slouken@6207
   746
    if (data->type != GL_TEXTURE_RECTANGLE_ARB) {
slouken@6207
   747
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@6207
   748
                                    GL_CLAMP_TO_EDGE);
slouken@6207
   749
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@6207
   750
                                    GL_CLAMP_TO_EDGE);
slouken@6207
   751
    }
slouken@2840
   752
#ifdef __MACOSX__
slouken@2230
   753
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
slouken@2230
   754
#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
slouken@2230
   755
#endif
slouken@2230
   756
#ifndef STORAGE_CACHED_APPLE
slouken@2230
   757
#define STORAGE_CACHED_APPLE                0x85BE
slouken@2230
   758
#endif
slouken@2230
   759
#ifndef STORAGE_SHARED_APPLE
slouken@2230
   760
#define STORAGE_SHARED_APPLE                0x85BF
slouken@2230
   761
#endif
slouken@2230
   762
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@2230
   763
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   764
                                    GL_STORAGE_SHARED_APPLE);
slouken@2230
   765
    } else {
slouken@2230
   766
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   767
                                    GL_STORAGE_CACHED_APPLE);
slouken@2230
   768
    }
bob@2295
   769
    if (texture->access == SDL_TEXTUREACCESS_STREAMING
slouken@5397
   770
        && texture->format == SDL_PIXELFORMAT_ARGB8888
slouken@5397
   771
        && (texture->w % 8) == 0) {
slouken@2230
   772
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
slouken@5397
   773
        renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@5397
   774
        renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@5397
   775
                          (data->pitch / SDL_BYTESPERPIXEL(texture->format)));
slouken@2230
   776
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   777
                                 texture_h, 0, format, type, data->pixels);
slouken@5397
   778
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
slouken@5156
   779
    }
slouken@5156
   780
    else
slouken@2809
   781
#endif
slouken@2230
   782
    {
slouken@2230
   783
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   784
                                 texture_h, 0, format, type, NULL);
slouken@2230
   785
    }
slouken@3041
   786
    renderdata->glDisable(data->type);
slouken@7194
   787
    if (GL_CheckError("glTexImage2D()", renderer) < 0) {
slouken@1924
   788
        return -1;
slouken@1924
   789
    }
slouken@5264
   790
slouken@5264
   791
    if (texture->format == SDL_PIXELFORMAT_YV12 ||
slouken@5264
   792
        texture->format == SDL_PIXELFORMAT_IYUV) {
slouken@5264
   793
        data->yuv = SDL_TRUE;
slouken@5264
   794
slouken@5264
   795
        renderdata->glGenTextures(1, &data->utexture);
slouken@5264
   796
        renderdata->glGenTextures(1, &data->vtexture);
slouken@5264
   797
        renderdata->glEnable(data->type);
slouken@5264
   798
slouken@5264
   799
        renderdata->glBindTexture(data->type, data->utexture);
slouken@5503
   800
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@5503
   801
                                    scaleMode);
slouken@5503
   802
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@5503
   803
                                    scaleMode);
slouken@5264
   804
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@5264
   805
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   806
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@5264
   807
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   808
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w/2,
slouken@5264
   809
                                 texture_h/2, 0, format, type, NULL);
slouken@5264
   810
slouken@5264
   811
        renderdata->glBindTexture(data->type, data->vtexture);
slouken@5503
   812
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@5503
   813
                                    scaleMode);
slouken@5503
   814
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@5503
   815
                                    scaleMode);
slouken@5264
   816
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@5264
   817
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   818
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@5264
   819
                                    GL_CLAMP_TO_EDGE);
slouken@5264
   820
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w/2,
slouken@5264
   821
                                 texture_h/2, 0, format, type, NULL);
slouken@5264
   822
slouken@5264
   823
        renderdata->glDisable(data->type);
slouken@5264
   824
    }
slouken@6494
   825
slouken@9046
   826
    if (texture->format == SDL_PIXELFORMAT_NV12 ||
slouken@9046
   827
        texture->format == SDL_PIXELFORMAT_NV21) {
slouken@9046
   828
        data->nv12 = SDL_TRUE;
slouken@9046
   829
slouken@9046
   830
        renderdata->glGenTextures(1, &data->utexture);
slouken@9046
   831
        renderdata->glEnable(data->type);
slouken@9046
   832
slouken@9046
   833
        renderdata->glBindTexture(data->type, data->utexture);
slouken@9046
   834
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@9046
   835
                                    scaleMode);
slouken@9046
   836
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@9046
   837
                                    scaleMode);
slouken@9046
   838
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@9046
   839
                                    GL_CLAMP_TO_EDGE);
slouken@9046
   840
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@9046
   841
                                    GL_CLAMP_TO_EDGE);
slouken@9046
   842
        renderdata->glTexImage2D(data->type, 0, GL_LUMINANCE_ALPHA, texture_w/2,
slouken@9046
   843
                                 texture_h/2, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, NULL);
slouken@9046
   844
        renderdata->glDisable(data->type);
slouken@9046
   845
    }
slouken@9046
   846
slouken@7194
   847
    return GL_CheckError("", renderer);
slouken@1918
   848
}
slouken@1918
   849
slouken@1918
   850
static int
slouken@1918
   851
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   852
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
   853
{
slouken@1927
   854
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   855
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
icculus@8650
   856
    const int texturebpp = SDL_BYTESPERPIXEL(texture->format);
icculus@8650
   857
icculus@8650
   858
    SDL_assert(texturebpp != 0);  /* otherwise, division by zero later. */
slouken@1918
   859
slouken@5147
   860
    GL_ActivateRenderer(renderer);
slouken@5147
   861
slouken@5397
   862
    renderdata->glEnable(data->type);
slouken@5397
   863
    renderdata->glBindTexture(data->type, data->texture);
slouken@5227
   864
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
icculus@8650
   865
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, (pitch / texturebpp));
slouken@1927
   866
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
slouken@1927
   867
                                rect->h, data->format, data->formattype,
slouken@1927
   868
                                pixels);
slouken@5264
   869
    if (data->yuv) {
slouken@5265
   870
        renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, (pitch / 2));
slouken@5265
   871
slouken@5264
   872
        /* Skip to the correct offset into the next texture */
slouken@6135
   873
        pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
slouken@5264
   874
        if (texture->format == SDL_PIXELFORMAT_YV12) {
slouken@5264
   875
            renderdata->glBindTexture(data->type, data->vtexture);
slouken@5264
   876
        } else {
slouken@5264
   877
            renderdata->glBindTexture(data->type, data->utexture);
slouken@5264
   878
        }
slouken@5264
   879
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@5264
   880
                                    rect->w/2, rect->h/2,
slouken@5264
   881
                                    data->format, data->formattype, pixels);
slouken@5264
   882
slouken@5264
   883
        /* Skip to the correct offset into the next texture */
slouken@6135
   884
        pixels = (const void*)((const Uint8*)pixels + (rect->h * pitch)/4);
slouken@5264
   885
        if (texture->format == SDL_PIXELFORMAT_YV12) {
slouken@5264
   886
            renderdata->glBindTexture(data->type, data->utexture);
slouken@5264
   887
        } else {
slouken@5264
   888
            renderdata->glBindTexture(data->type, data->vtexture);
slouken@5264
   889
        }
slouken@5264
   890
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@5264
   891
                                    rect->w/2, rect->h/2,
slouken@5264
   892
                                    data->format, data->formattype, pixels);
slouken@5264
   893
    }
slouken@9046
   894
slouken@9046
   895
    if (data->nv12) {
slouken@9046
   896
        renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, (pitch / 2));
slouken@9046
   897
slouken@9046
   898
        /* Skip to the correct offset into the next texture */
slouken@9046
   899
        pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
slouken@9046
   900
        renderdata->glBindTexture(data->type, data->utexture);
slouken@9046
   901
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@9046
   902
                                    rect->w/2, rect->h/2,
slouken@9046
   903
                                    GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, pixels);
slouken@9046
   904
    }
slouken@3041
   905
    renderdata->glDisable(data->type);
slouken@7779
   906
slouken@7194
   907
    return GL_CheckError("glTexSubImage2D()", renderer);
slouken@1918
   908
}
slouken@1918
   909
slouken@1918
   910
static int
slouken@7759
   911
GL_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@7759
   912
                    const SDL_Rect * rect,
slouken@7759
   913
                    const Uint8 *Yplane, int Ypitch,
slouken@7759
   914
                    const Uint8 *Uplane, int Upitch,
slouken@7759
   915
                    const Uint8 *Vplane, int Vpitch)
slouken@7759
   916
{
slouken@7759
   917
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@7759
   918
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@7759
   919
slouken@7759
   920
    GL_ActivateRenderer(renderer);
slouken@7759
   921
slouken@7759
   922
    renderdata->glEnable(data->type);
slouken@7759
   923
    renderdata->glBindTexture(data->type, data->texture);
slouken@7759
   924
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@7759
   925
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Ypitch);
slouken@7759
   926
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
slouken@7759
   927
                                rect->h, data->format, data->formattype,
slouken@7759
   928
                                Yplane);
slouken@7759
   929
slouken@7759
   930
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Upitch);
slouken@7759
   931
    renderdata->glBindTexture(data->type, data->utexture);
slouken@7759
   932
    renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@7759
   933
                                rect->w/2, rect->h/2,
slouken@7759
   934
                                data->format, data->formattype, Uplane);
slouken@7759
   935
slouken@7759
   936
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Vpitch);
slouken@7759
   937
    renderdata->glBindTexture(data->type, data->vtexture);
slouken@7759
   938
    renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
slouken@7759
   939
                                rect->w/2, rect->h/2,
slouken@7759
   940
                                data->format, data->formattype, Vplane);
slouken@7759
   941
    renderdata->glDisable(data->type);
slouken@7779
   942
slouken@7759
   943
    return GL_CheckError("glTexSubImage2D()", renderer);
slouken@7759
   944
}
slouken@7759
   945
slouken@7759
   946
static int
slouken@1918
   947
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   948
               const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1918
   949
{
slouken@1918
   950
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   951
slouken@5227
   952
    data->locked_rect = *rect;
slouken@7191
   953
    *pixels =
slouken@1920
   954
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@5156
   955
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@1920
   956
    *pitch = data->pitch;
slouken@1918
   957
    return 0;
slouken@1918
   958
}
slouken@1918
   959
slouken@1918
   960
static void
slouken@1918
   961
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   962
{
slouken@5156
   963
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@5227
   964
    const SDL_Rect *rect;
slouken@5227
   965
    void *pixels;
slouken@1918
   966
slouken@5227
   967
    rect = &data->locked_rect;
slouken@7191
   968
    pixels =
slouken@5227
   969
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@5227
   970
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5227
   971
    GL_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
slouken@1918
   972
}
slouken@1918
   973
slouken@5297
   974
static int
slouken@6247
   975
GL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@6246
   976
{
slouken@7191
   977
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@6246
   978
    GL_TextureData *texturedata;
slouken@6246
   979
    GLenum status;
slouken@6246
   980
slouken@6246
   981
    GL_ActivateRenderer(renderer);
slouken@7191
   982
slouken@6246
   983
    if (texture == NULL) {
slouken@6246
   984
        data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
slouken@6246
   985
        return 0;
slouken@6246
   986
    }
slouken@6246
   987
slouken@6246
   988
    texturedata = (GL_TextureData *) texture->driverdata;
slouken@6246
   989
    data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, texturedata->fbo->FBO);
slouken@6246
   990
    /* TODO: check if texture pixel format allows this operation */
slouken@6246
   991
    data->glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, texturedata->type, texturedata->texture, 0);
slouken@6246
   992
    /* Check FBO status */
slouken@6246
   993
    status = data->glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
slouken@6246
   994
    if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
icculus@7037
   995
        return SDL_SetError("glFramebufferTexture2DEXT() failed");
slouken@6246
   996
    }
slouken@6246
   997
    return 0;
slouken@6246
   998
}
slouken@6246
   999
slouken@6246
  1000
static int
slouken@5297
  1001
GL_UpdateViewport(SDL_Renderer * renderer)
slouken@5224
  1002
{
slouken@5224
  1003
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5224
  1004
slouken@5297
  1005
    if (SDL_CurrentContext != data->context) {
slouken@5297
  1006
        /* We'll update the viewport after we rebind the context */
slouken@5297
  1007
        return 0;
slouken@5297
  1008
    }
slouken@5224
  1009
slouken@7995
  1010
    if (renderer->target) {
slouken@7995
  1011
        data->glViewport(renderer->viewport.x, renderer->viewport.y,
slouken@7995
  1012
                         renderer->viewport.w, renderer->viewport.h);
slouken@7995
  1013
    } else {
slouken@7995
  1014
        int w, h;
slouken@7995
  1015
slouken@7995
  1016
        SDL_GetRendererOutputSize(renderer, &w, &h);
slouken@7995
  1017
        data->glViewport(renderer->viewport.x, (h - renderer->viewport.y - renderer->viewport.h),
slouken@7995
  1018
                         renderer->viewport.w, renderer->viewport.h);
slouken@7995
  1019
    }
slouken@5224
  1020
slouken@5297
  1021
    data->glMatrixMode(GL_PROJECTION);
slouken@5297
  1022
    data->glLoadIdentity();
slouken@7239
  1023
    if (renderer->viewport.w && renderer->viewport.h) {
slouken@7995
  1024
        if (renderer->target) {
slouken@7239
  1025
            data->glOrtho((GLdouble) 0,
slouken@7239
  1026
                          (GLdouble) renderer->viewport.w,
slouken@7239
  1027
                          (GLdouble) 0,
slouken@7239
  1028
                          (GLdouble) renderer->viewport.h,
slouken@7239
  1029
                           0.0, 1.0);
slouken@7239
  1030
        } else {
slouken@7239
  1031
            data->glOrtho((GLdouble) 0,
slouken@7239
  1032
                          (GLdouble) renderer->viewport.w,
slouken@7239
  1033
                          (GLdouble) renderer->viewport.h,
slouken@7239
  1034
                          (GLdouble) 0,
slouken@7239
  1035
                           0.0, 1.0);
slouken@7239
  1036
        }
slouken@6246
  1037
    }
slouken@7194
  1038
    return GL_CheckError("", renderer);
slouken@5224
  1039
}
slouken@5224
  1040
slouken@7141
  1041
static int
slouken@7141
  1042
GL_UpdateClipRect(SDL_Renderer * renderer)
slouken@7141
  1043
{
slouken@7141
  1044
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@7141
  1045
jorgenpt@8728
  1046
    if (renderer->clipping_enabled) {
jorgenpt@8728
  1047
        const SDL_Rect *rect = &renderer->clip_rect;
slouken@7141
  1048
        data->glEnable(GL_SCISSOR_TEST);
slouken@9680
  1049
        if (renderer->target) {
slouken@9680
  1050
            data->glScissor(renderer->viewport.x + rect->x, renderer->viewport.y + rect->y, rect->w, rect->h);
slouken@9680
  1051
        } else {
slouken@9680
  1052
            int w, h;
slouken@9680
  1053
slouken@9680
  1054
            SDL_GetRendererOutputSize(renderer, &w, &h);
slouken@9680
  1055
            data->glScissor(renderer->viewport.x + rect->x, (h - renderer->viewport.y - renderer->viewport.h) + rect->y, rect->w, rect->h);
slouken@9680
  1056
        }
slouken@7141
  1057
    } else {
slouken@7141
  1058
        data->glDisable(GL_SCISSOR_TEST);
slouken@7141
  1059
    }
slouken@7141
  1060
    return 0;
slouken@7141
  1061
}
slouken@7141
  1062
slouken@5224
  1063
static void
slouken@5355
  1064
GL_SetShader(GL_RenderData * data, GL_Shader shader)
slouken@5355
  1065
{
slouken@5355
  1066
    if (data->shaders && shader != data->current.shader) {
slouken@5355
  1067
        GL_SelectShader(data->shaders, shader);
slouken@5355
  1068
        data->current.shader = shader;
slouken@5355
  1069
    }
slouken@5355
  1070
}
slouken@5355
  1071
slouken@5355
  1072
static void
slouken@5355
  1073
GL_SetColor(GL_RenderData * data, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@5355
  1074
{
slouken@5355
  1075
    Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
slouken@5355
  1076
slouken@5355
  1077
    if (color != data->current.color) {
slouken@5355
  1078
        data->glColor4f((GLfloat) r * inv255f,
slouken@5355
  1079
                        (GLfloat) g * inv255f,
slouken@5355
  1080
                        (GLfloat) b * inv255f,
slouken@5355
  1081
                        (GLfloat) a * inv255f);
slouken@5355
  1082
        data->current.color = color;
slouken@5355
  1083
    }
slouken@5355
  1084
}
slouken@5355
  1085
slouken@5355
  1086
static void
slouken@5140
  1087
GL_SetBlendMode(GL_RenderData * data, int blendMode)
slouken@2936
  1088
{
slouken@5355
  1089
    if (blendMode != data->current.blendMode) {
slouken@2936
  1090
        switch (blendMode) {
slouken@2936
  1091
        case SDL_BLENDMODE_NONE:
slouken@2936
  1092
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
slouken@2936
  1093
            data->glDisable(GL_BLEND);
slouken@2936
  1094
            break;
slouken@2936
  1095
        case SDL_BLENDMODE_BLEND:
slouken@2936
  1096
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
  1097
            data->glEnable(GL_BLEND);
slouken@7502
  1098
            data->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
slouken@2936
  1099
            break;
slouken@2936
  1100
        case SDL_BLENDMODE_ADD:
slouken@2936
  1101
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
  1102
            data->glEnable(GL_BLEND);
slouken@7502
  1103
            data->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
slouken@2936
  1104
            break;
slouken@5184
  1105
        case SDL_BLENDMODE_MOD:
slouken@5184
  1106
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@5184
  1107
            data->glEnable(GL_BLEND);
slouken@7502
  1108
            data->glBlendFuncSeparate(GL_ZERO, GL_SRC_COLOR, GL_ZERO, GL_ONE);
slouken@5184
  1109
            break;
slouken@2936
  1110
        }
slouken@5355
  1111
        data->current.blendMode = blendMode;
slouken@2936
  1112
    }
slouken@2936
  1113
}
slouken@2936
  1114
slouken@5355
  1115
static void
slouken@5355
  1116
GL_SetDrawingState(SDL_Renderer * renderer)
slouken@5355
  1117
{
slouken@5355
  1118
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5355
  1119
slouken@5355
  1120
    GL_ActivateRenderer(renderer);
slouken@5355
  1121
slouken@5381
  1122
    GL_SetColor(data, renderer->r,
slouken@5381
  1123
                      renderer->g,
slouken@5381
  1124
                      renderer->b,
slouken@5381
  1125
                      renderer->a);
slouken@5355
  1126
slouken@5355
  1127
    GL_SetBlendMode(data, renderer->blendMode);
slouken@5355
  1128
slouken@5355
  1129
    GL_SetShader(data, SHADER_SOLID);
slouken@5355
  1130
}
slouken@5355
  1131
slouken@1918
  1132
static int
slouken@3596
  1133
GL_RenderClear(SDL_Renderer * renderer)
slouken@3596
  1134
{
slouken@3596
  1135
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3596
  1136
slouken@5147
  1137
    GL_ActivateRenderer(renderer);
slouken@5147
  1138
slouken@3596
  1139
    data->glClearColor((GLfloat) renderer->r * inv255f,
slouken@3596
  1140
                       (GLfloat) renderer->g * inv255f,
slouken@3596
  1141
                       (GLfloat) renderer->b * inv255f,
slouken@3596
  1142
                       (GLfloat) renderer->a * inv255f);
slouken@3596
  1143
slouken@3596
  1144
    data->glClear(GL_COLOR_BUFFER_BIT);
slouken@3596
  1145
slouken@3596
  1146
    return 0;
slouken@3596
  1147
}
slouken@3596
  1148
slouken@3596
  1149
static int
slouken@6528
  1150
GL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_FPoint * points,
slouken@3596
  1151
                    int count)
slouken@2884
  1152
{
slouken@2884
  1153
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@7908
  1154
    int i;
slouken@2884
  1155
slouken@5355
  1156
    GL_SetDrawingState(renderer);
slouken@2884
  1157
slouken@7908
  1158
    data->glBegin(GL_POINTS);
slouken@7908
  1159
    for (i = 0; i < count; ++i) {
slouken@7908
  1160
        data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@7908
  1161
    }
slouken@7908
  1162
    data->glEnd();
slouken@2901
  1163
slouken@2901
  1164
    return 0;
slouken@2901
  1165
}
slouken@2901
  1166
slouken@2901
  1167
static int
slouken@6528
  1168
GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_FPoint * points,
slouken@3596
  1169
                   int count)
slouken@2901
  1170
{
slouken@2901
  1171
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@7908
  1172
    int i;
slouken@2901
  1173
slouken@5355
  1174
    GL_SetDrawingState(renderer);
slouken@2901
  1175
slouken@7191
  1176
    if (count > 2 &&
slouken@3536
  1177
        points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
slouken@7908
  1178
        data->glBegin(GL_LINE_LOOP);
slouken@3536
  1179
        /* GL_LINE_LOOP takes care of the final segment */
slouken@7908
  1180
        --count;
slouken@7908
  1181
        for (i = 0; i < count; ++i) {
slouken@7908
  1182
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@7908
  1183
        }
slouken@7908
  1184
        data->glEnd();
slouken@3536
  1185
    } else {
slouken@7908
  1186
#if defined(__MACOSX__) || defined(__WIN32__)
slouken@7908
  1187
#else
slouken@7908
  1188
        int x1, y1, x2, y2;
slouken@7908
  1189
#endif
slouken@7908
  1190
slouken@7908
  1191
        data->glBegin(GL_LINE_STRIP);
slouken@7908
  1192
        for (i = 0; i < count; ++i) {
slouken@7908
  1193
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@7908
  1194
        }
slouken@7908
  1195
        data->glEnd();
slouken@7908
  1196
slouken@7908
  1197
        /* The line is half open, so we need one more point to complete it.
slouken@7908
  1198
         * http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node47.html
slouken@7908
  1199
         * If we have to, we can use vertical line and horizontal line textures
slouken@7908
  1200
         * for vertical and horizontal lines, and then create custom textures
slouken@7908
  1201
         * for diagonal lines and software render those.  It's terrible, but at
slouken@7908
  1202
         * least it would be pixel perfect.
slouken@7908
  1203
         */
slouken@7908
  1204
        data->glBegin(GL_POINTS);
slouken@7908
  1205
#if defined(__MACOSX__) || defined(__WIN32__)
slouken@7908
  1206
        /* Mac OS X and Windows seem to always leave the last point open */
slouken@7908
  1207
        data->glVertex2f(0.5f + points[count-1].x, 0.5f + points[count-1].y);
slouken@7908
  1208
#else
slouken@7908
  1209
        /* Linux seems to leave the right-most or bottom-most point open */
slouken@7908
  1210
        x1 = points[0].x;
slouken@7908
  1211
        y1 = points[0].y;
slouken@7908
  1212
        x2 = points[count-1].x;
slouken@7908
  1213
        y2 = points[count-1].y;
slouken@7908
  1214
slouken@7908
  1215
        if (x1 > x2) {
slouken@7908
  1216
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@7908
  1217
        } else if (x2 > x1) {
slouken@7908
  1218
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@7908
  1219
        }
slouken@7908
  1220
        if (y1 > y2) {
slouken@7908
  1221
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@7908
  1222
        } else if (y2 > y1) {
slouken@7908
  1223
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@7908
  1224
        }
slouken@7908
  1225
#endif
slouken@7908
  1226
        data->glEnd();
slouken@7840
  1227
    }
slouken@7194
  1228
    return GL_CheckError("", renderer);
slouken@1918
  1229
}
slouken@1918
  1230
slouken@1918
  1231
static int
slouken@6528
  1232
GL_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects, int count)
slouken@2925
  1233
{
slouken@2925
  1234
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
  1235
    int i;
slouken@2925
  1236
slouken@5355
  1237
    GL_SetDrawingState(renderer);
slouken@2936
  1238
slouken@3536
  1239
    for (i = 0; i < count; ++i) {
slouken@6528
  1240
        const SDL_FRect *rect = &rects[i];
slouken@3536
  1241
slouken@6528
  1242
        data->glRectf(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
slouken@3536
  1243
    }
slouken@7194
  1244
    return GL_CheckError("", renderer);
slouken@2925
  1245
}
slouken@2925
  1246
slouken@2925
  1247
static int
slouken@9046
  1248
GL_SetupCopy(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
  1249
{
slouken@1918
  1250
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1251
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@5147
  1252
slouken@5355
  1253
    data->glEnable(texturedata->type);
slouken@5355
  1254
    if (texturedata->yuv) {
slouken@5355
  1255
        data->glActiveTextureARB(GL_TEXTURE2_ARB);
slouken@5355
  1256
        data->glBindTexture(texturedata->type, texturedata->vtexture);
slouken@5355
  1257
slouken@5355
  1258
        data->glActiveTextureARB(GL_TEXTURE1_ARB);
slouken@5355
  1259
        data->glBindTexture(texturedata->type, texturedata->utexture);
slouken@5355
  1260
slouken@5355
  1261
        data->glActiveTextureARB(GL_TEXTURE0_ARB);
slouken@5355
  1262
    }
slouken@9046
  1263
    if (texturedata->nv12) {
slouken@9046
  1264
        data->glActiveTextureARB(GL_TEXTURE1_ARB);
slouken@9046
  1265
        data->glBindTexture(texturedata->type, texturedata->utexture);
slouken@9046
  1266
slouken@9046
  1267
        data->glActiveTextureARB(GL_TEXTURE0_ARB);
slouken@9046
  1268
    }
slouken@5355
  1269
    data->glBindTexture(texturedata->type, texturedata->texture);
slouken@5355
  1270
slouken@5355
  1271
    if (texture->modMode) {
slouken@5355
  1272
        GL_SetColor(data, texture->r, texture->g, texture->b, texture->a);
slouken@5355
  1273
    } else {
slouken@5355
  1274
        GL_SetColor(data, 255, 255, 255, 255);
slouken@5355
  1275
    }
slouken@5355
  1276
slouken@5355
  1277
    GL_SetBlendMode(data, texture->blendMode);
slouken@5355
  1278
slouken@5355
  1279
    if (texturedata->yuv) {
slouken@9046
  1280
        GL_SetShader(data, SHADER_YUV);
slouken@9046
  1281
    } else if (texturedata->nv12) {
slouken@9046
  1282
        if (texture->format == SDL_PIXELFORMAT_NV12) {
slouken@9046
  1283
            GL_SetShader(data, SHADER_NV12);
slouken@9046
  1284
        } else {
slouken@9046
  1285
            GL_SetShader(data, SHADER_NV21);
slouken@9046
  1286
        }
slouken@5355
  1287
    } else {
slouken@5355
  1288
        GL_SetShader(data, SHADER_RGB);
slouken@5355
  1289
    }
slouken@9046
  1290
    return 0;
slouken@9046
  1291
}
slouken@9046
  1292
slouken@9046
  1293
static int
slouken@9046
  1294
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@9046
  1295
              const SDL_Rect * srcrect, const SDL_FRect * dstrect)
slouken@9046
  1296
{
slouken@9046
  1297
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@9046
  1298
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@9046
  1299
    GLfloat minx, miny, maxx, maxy;
slouken@9046
  1300
    GLfloat minu, maxu, minv, maxv;
slouken@9046
  1301
slouken@9046
  1302
    GL_ActivateRenderer(renderer);
slouken@9046
  1303
slouken@9046
  1304
    if (GL_SetupCopy(renderer, texture) < 0) {
slouken@9046
  1305
        return -1;
slouken@9046
  1306
    }
slouken@5355
  1307
slouken@1918
  1308
    minx = dstrect->x;
slouken@1918
  1309
    miny = dstrect->y;
slouken@1918
  1310
    maxx = dstrect->x + dstrect->w;
slouken@1918
  1311
    maxy = dstrect->y + dstrect->h;
slouken@1918
  1312
slouken@1918
  1313
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
  1314
    minu *= texturedata->texw;
slouken@1918
  1315
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
  1316
    maxu *= texturedata->texw;
slouken@1918
  1317
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
  1318
    minv *= texturedata->texh;
slouken@1918
  1319
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
  1320
    maxv *= texturedata->texh;
slouken@1918
  1321
slouken@1927
  1322
    data->glBegin(GL_TRIANGLE_STRIP);
slouken@1927
  1323
    data->glTexCoord2f(minu, minv);
slouken@6528
  1324
    data->glVertex2f(minx, miny);
slouken@1927
  1325
    data->glTexCoord2f(maxu, minv);
slouken@6528
  1326
    data->glVertex2f(maxx, miny);
slouken@1927
  1327
    data->glTexCoord2f(minu, maxv);
slouken@6528
  1328
    data->glVertex2f(minx, maxy);
slouken@1927
  1329
    data->glTexCoord2f(maxu, maxv);
slouken@6528
  1330
    data->glVertex2f(maxx, maxy);
slouken@1927
  1331
    data->glEnd();
slouken@1918
  1332
slouken@2884
  1333
    data->glDisable(texturedata->type);
slouken@2884
  1334
slouken@7194
  1335
    return GL_CheckError("", renderer);
slouken@1918
  1336
}
slouken@1918
  1337
slouken@3431
  1338
static int
gabomdq@6320
  1339
GL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
  1340
              const SDL_Rect * srcrect, const SDL_FRect * dstrect,
slouken@6528
  1341
              const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
gabomdq@6320
  1342
{
gabomdq@6320
  1343
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
gabomdq@6320
  1344
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
gabomdq@6320
  1345
    GLfloat minx, miny, maxx, maxy;
gabomdq@6320
  1346
    GLfloat centerx, centery;
gabomdq@6320
  1347
    GLfloat minu, maxu, minv, maxv;
slouken@7194
  1348
gabomdq@6320
  1349
    GL_ActivateRenderer(renderer);
gabomdq@6320
  1350
slouken@9046
  1351
    if (GL_SetupCopy(renderer, texture) < 0) {
slouken@9046
  1352
        return -1;
gabomdq@6320
  1353
    }
gabomdq@6320
  1354
slouken@6528
  1355
    centerx = center->x;
slouken@6528
  1356
    centery = center->y;
gabomdq@6320
  1357
slouken@7989
  1358
    if (flip & SDL_FLIP_HORIZONTAL) {
slouken@7989
  1359
        minx =  dstrect->w - centerx;
slouken@7976
  1360
        maxx = -centerx;
slouken@7989
  1361
    }
slouken@7989
  1362
    else {
gabomdq@6320
  1363
        minx = -centerx;
slouken@7989
  1364
        maxx =  dstrect->w - centerx;
slouken@7989
  1365
    }
slouken@7989
  1366
slouken@7989
  1367
    if (flip & SDL_FLIP_VERTICAL) {
slouken@7989
  1368
        miny =  dstrect->h - centery;
slouken@7989
  1369
        maxy = -centery;
slouken@7989
  1370
    }
slouken@7989
  1371
    else {
gabomdq@6320
  1372
        miny = -centery;
slouken@7989
  1373
        maxy =  dstrect->h - centery;
gabomdq@6320
  1374
    }
gabomdq@6320
  1375
gabomdq@6320
  1376
    minu = (GLfloat) srcrect->x / texture->w;
gabomdq@6320
  1377
    minu *= texturedata->texw;
gabomdq@6320
  1378
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
gabomdq@6320
  1379
    maxu *= texturedata->texw;
gabomdq@6320
  1380
    minv = (GLfloat) srcrect->y / texture->h;
gabomdq@6320
  1381
    minv *= texturedata->texh;
gabomdq@6320
  1382
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
gabomdq@6320
  1383
    maxv *= texturedata->texh;
gabomdq@6320
  1384
slouken@7191
  1385
    /* Translate to flip, rotate, translate to position */
gabomdq@6320
  1386
    data->glPushMatrix();
slouken@7191
  1387
    data->glTranslatef((GLfloat)dstrect->x + centerx, (GLfloat)dstrect->y + centery, (GLfloat)0.0);
gabomdq@6320
  1388
    data->glRotated(angle, (GLdouble)0.0, (GLdouble)0.0, (GLdouble)1.0);
slouken@7191
  1389
gabomdq@6320
  1390
    data->glBegin(GL_TRIANGLE_STRIP);
gabomdq@6320
  1391
    data->glTexCoord2f(minu, minv);
gabomdq@6320
  1392
    data->glVertex2f(minx, miny);
gabomdq@6320
  1393
    data->glTexCoord2f(maxu, minv);
gabomdq@6320
  1394
    data->glVertex2f(maxx, miny);
gabomdq@6320
  1395
    data->glTexCoord2f(minu, maxv);
gabomdq@6320
  1396
    data->glVertex2f(minx, maxy);
gabomdq@6320
  1397
    data->glTexCoord2f(maxu, maxv);
gabomdq@6320
  1398
    data->glVertex2f(maxx, maxy);
gabomdq@6320
  1399
    data->glEnd();
gabomdq@6320
  1400
    data->glPopMatrix();
slouken@7191
  1401
gabomdq@6320
  1402
    data->glDisable(texturedata->type);
gabomdq@6320
  1403
slouken@7194
  1404
    return GL_CheckError("", renderer);
gabomdq@6320
  1405
}
gabomdq@6320
  1406
gabomdq@6320
  1407
static int
slouken@3431
  1408
GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
  1409
                    Uint32 pixel_format, void * pixels, int pitch)
slouken@3431
  1410
{
slouken@3433
  1411
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5465
  1412
    Uint32 temp_format = SDL_PIXELFORMAT_ARGB8888;
slouken@5465
  1413
    void *temp_pixels;
slouken@5465
  1414
    int temp_pitch;
slouken@3433
  1415
    GLint internalFormat;
slouken@3433
  1416
    GLenum format, type;
slouken@3435
  1417
    Uint8 *src, *dst, *tmp;
slouken@5154
  1418
    int w, h, length, rows;
slouken@5465
  1419
    int status;
slouken@3433
  1420
slouken@5147
  1421
    GL_ActivateRenderer(renderer);
slouken@5147
  1422
slouken@5465
  1423
    temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
slouken@5465
  1424
    temp_pixels = SDL_malloc(rect->h * temp_pitch);
slouken@5465
  1425
    if (!temp_pixels) {
icculus@7037
  1426
        return SDL_OutOfMemory();
slouken@3433
  1427
    }
slouken@3433
  1428
slouken@5465
  1429
    convert_format(data, temp_format, &internalFormat, &format, &type);
slouken@5465
  1430
slouken@7420
  1431
    SDL_GetRendererOutputSize(renderer, &w, &h);
slouken@5154
  1432
slouken@3446
  1433
    data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
slouken@3446
  1434
    data->glPixelStorei(GL_PACK_ROW_LENGTH,
slouken@5465
  1435
                        (temp_pitch / SDL_BYTESPERPIXEL(temp_format)));
slouken@3433
  1436
slouken@5154
  1437
    data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
slouken@5465
  1438
                       format, type, temp_pixels);
slouken@3435
  1439
slouken@7779
  1440
    if (GL_CheckError("glReadPixels()", renderer) < 0) {
slouken@9115
  1441
        SDL_free(temp_pixels);
slouken@7779
  1442
        return -1;
slouken@7779
  1443
    }
slouken@6494
  1444
slouken@3435
  1445
    /* Flip the rows to be top-down */
slouken@5465
  1446
    length = rect->w * SDL_BYTESPERPIXEL(temp_format);
slouken@5465
  1447
    src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
slouken@5465
  1448
    dst = (Uint8*)temp_pixels;
slouken@3435
  1449
    tmp = SDL_stack_alloc(Uint8, length);
slouken@3435
  1450
    rows = rect->h / 2;
slouken@3435
  1451
    while (rows--) {
slouken@3435
  1452
        SDL_memcpy(tmp, dst, length);
slouken@3435
  1453
        SDL_memcpy(dst, src, length);
slouken@3435
  1454
        SDL_memcpy(src, tmp, length);
slouken@5465
  1455
        dst += temp_pitch;
slouken@5465
  1456
        src -= temp_pitch;
slouken@3435
  1457
    }
slouken@3435
  1458
    SDL_stack_free(tmp);
slouken@3440
  1459
slouken@5465
  1460
    status = SDL_ConvertPixels(rect->w, rect->h,
slouken@5465
  1461
                               temp_format, temp_pixels, temp_pitch,
slouken@5465
  1462
                               pixel_format, pixels, pitch);
slouken@5465
  1463
    SDL_free(temp_pixels);
slouken@5465
  1464
slouken@5465
  1465
    return status;
slouken@3431
  1466
}
slouken@3431
  1467
slouken@1918
  1468
static void
slouken@1918
  1469
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
  1470
{
slouken@5147
  1471
    GL_ActivateRenderer(renderer);
slouken@5147
  1472
slouken@1918
  1473
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
  1474
}
slouken@1918
  1475
slouken@1918
  1476
static void
slouken@1918
  1477
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
  1478
{
slouken@1927
  1479
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1480
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
  1481
slouken@5147
  1482
    GL_ActivateRenderer(renderer);
slouken@5147
  1483
slouken@1918
  1484
    if (!data) {
slouken@1918
  1485
        return;
slouken@1918
  1486
    }
slouken@1918
  1487
    if (data->texture) {
slouken@1927
  1488
        renderdata->glDeleteTextures(1, &data->texture);
slouken@1918
  1489
    }
slouken@5264
  1490
    if (data->yuv) {
slouken@5264
  1491
        renderdata->glDeleteTextures(1, &data->utexture);
slouken@5264
  1492
        renderdata->glDeleteTextures(1, &data->vtexture);
slouken@5264
  1493
    }
slouken@7719
  1494
    SDL_free(data->pixels);
slouken@1918
  1495
    SDL_free(data);
slouken@1918
  1496
    texture->driverdata = NULL;
slouken@1918
  1497
}
slouken@1918
  1498
slouken@1975
  1499
static void
slouken@1918
  1500
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
  1501
{
slouken@1918
  1502
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1503
slouken@1918
  1504
    if (data) {
slouken@7194
  1505
        GL_ClearErrors(renderer);
slouken@7194
  1506
        if (data->GL_ARB_debug_output_supported) {
slouken@7194
  1507
            PFNGLDEBUGMESSAGECALLBACKARBPROC glDebugMessageCallbackARBFunc = (PFNGLDEBUGMESSAGECALLBACKARBPROC) SDL_GL_GetProcAddress("glDebugMessageCallbackARB");
slouken@7194
  1508
slouken@7194
  1509
            /* 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
  1510
            /* For now, just always replace the callback with the original one */
slouken@7194
  1511
            glDebugMessageCallbackARBFunc(data->next_error_callback, data->next_error_userparam);
slouken@7194
  1512
        }
icculus@5566
  1513
        if (data->shaders) {
icculus@5566
  1514
            GL_DestroyShaderContext(data->shaders);
icculus@5566
  1515
        }
slouken@1920
  1516
        if (data->context) {
slouken@6232
  1517
            while (data->framebuffers) {
slouken@6232
  1518
                GL_FBOList *nextnode = data->framebuffers->next;
slouken@6232
  1519
                /* delete the framebuffer object */
slouken@6232
  1520
                data->glDeleteFramebuffersEXT(1, &data->framebuffers->FBO);
slouken@6494
  1521
                GL_CheckError("", renderer);
slouken@6232
  1522
                SDL_free(data->framebuffers);
slouken@6232
  1523
                data->framebuffers = nextnode;
jorgen@7091
  1524
            }
slouken@1920
  1525
            SDL_GL_DeleteContext(data->context);
slouken@1918
  1526
        }
slouken@1918
  1527
        SDL_free(data);
slouken@1918
  1528
    }
slouken@1918
  1529
    SDL_free(renderer);
slouken@1918
  1530
}
slouken@1918
  1531
slouken@7194
  1532
static int
slouken@7194
  1533
GL_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh)
slouken@7194
  1534
{
gabomdq@6414
  1535
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
gabomdq@6414
  1536
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
gabomdq@6414
  1537
    GL_ActivateRenderer(renderer);
gabomdq@6414
  1538
gabomdq@6414
  1539
    data->glEnable(texturedata->type);
gabomdq@6414
  1540
    if (texturedata->yuv) {
gabomdq@6414
  1541
        data->glActiveTextureARB(GL_TEXTURE2_ARB);
gabomdq@6414
  1542
        data->glBindTexture(texturedata->type, texturedata->vtexture);
gabomdq@6414
  1543
gabomdq@6414
  1544
        data->glActiveTextureARB(GL_TEXTURE1_ARB);
gabomdq@6414
  1545
        data->glBindTexture(texturedata->type, texturedata->utexture);
gabomdq@6414
  1546
gabomdq@6414
  1547
        data->glActiveTextureARB(GL_TEXTURE0_ARB);
gabomdq@6414
  1548
    }
gabomdq@6414
  1549
    data->glBindTexture(texturedata->type, texturedata->texture);
gabomdq@6414
  1550
gabomdq@6414
  1551
    if(texw) *texw = (float)texturedata->texw;
gabomdq@6414
  1552
    if(texh) *texh = (float)texturedata->texh;
gabomdq@6414
  1553
gabomdq@6414
  1554
    return 0;
gabomdq@6414
  1555
}
gabomdq@6414
  1556
slouken@7194
  1557
static int
slouken@7194
  1558
GL_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture)
slouken@7194
  1559
{
gabomdq@6414
  1560
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
gabomdq@6414
  1561
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
gabomdq@6414
  1562
    GL_ActivateRenderer(renderer);
gabomdq@6414
  1563
gabomdq@6414
  1564
    if (texturedata->yuv) {
gabomdq@6414
  1565
        data->glActiveTextureARB(GL_TEXTURE2_ARB);
gabomdq@6414
  1566
        data->glDisable(texturedata->type);
gabomdq@6414
  1567
gabomdq@6414
  1568
        data->glActiveTextureARB(GL_TEXTURE1_ARB);
gabomdq@6414
  1569
        data->glDisable(texturedata->type);
gabomdq@6414
  1570
gabomdq@6414
  1571
        data->glActiveTextureARB(GL_TEXTURE0_ARB);
gabomdq@6414
  1572
    }
slouken@7191
  1573
gabomdq@6414
  1574
    data->glDisable(texturedata->type);
gabomdq@6414
  1575
gabomdq@6414
  1576
    return 0;
gabomdq@6414
  1577
}
gabomdq@6414
  1578
slouken@5226
  1579
#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
slouken@1918
  1580
slouken@1918
  1581
/* vi: set ts=4 sw=4 expandtab: */