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