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