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