src/render/opengl/SDL_renderer_gl.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 02 Feb 2011 14:34:54 -0800
changeset 5154 fb424691cfc7
parent 5150 src/video/SDL_renderer_gl.c@1435f8a6425c
child 5156 307ccc9c135e
permissions -rw-r--r--
Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
slouken@1918
     1
/*
slouken@1918
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@1918
     4
slouken@1918
     5
    This library is free software; you can redistribute it and/or
slouken@1918
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1918
     7
    License as published by the Free Software Foundation; either
slouken@1918
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1918
     9
slouken@1918
    10
    This library is distributed in the hope that it will be useful,
slouken@1918
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1918
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1918
    13
    Lesser General Public License for more details.
slouken@1918
    14
slouken@1918
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1918
    16
    License along with this library; if not, write to the Free Software
slouken@1918
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1918
    18
slouken@1918
    19
    Sam Lantinga
slouken@1918
    20
    slouken@libsdl.org
slouken@1918
    21
*/
slouken@1918
    22
#include "SDL_config.h"
slouken@1918
    23
slouken@1952
    24
#if SDL_VIDEO_RENDER_OGL
slouken@1920
    25
slouken@1920
    26
#include "SDL_opengl.h"
slouken@5154
    27
#include "../SDL_sysrender.h"
slouken@1918
    28
slouken@2246
    29
#ifdef __MACOSX__
slouken@2246
    30
#include <OpenGL/OpenGL.h>
slouken@2246
    31
#endif
slouken@2246
    32
slouken@2778
    33
slouken@1918
    34
/* OpenGL renderer implementation */
slouken@1918
    35
slouken@2230
    36
/* Details on optimizing the texture path on Mac OS X:
slouken@2230
    37
   http://developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/chapter_10_section_2.html
slouken@2230
    38
*/
slouken@2230
    39
icculus@2835
    40
/* !!! FIXME: this should go in a higher level than the GL renderer. */
icculus@2835
    41
static __inline__ int
icculus@2835
    42
bytes_per_pixel(const Uint32 format)
icculus@2835
    43
{
slouken@2839
    44
    if (!SDL_ISPIXELFORMAT_FOURCC(format)) {
slouken@2839
    45
        return SDL_BYTESPERPIXEL(format);
slouken@2839
    46
    }
slouken@2839
    47
slouken@2839
    48
    /* FOURCC format */
icculus@2835
    49
    switch (format) {
slouken@2884
    50
    case SDL_PIXELFORMAT_YV12:
slouken@2884
    51
    case SDL_PIXELFORMAT_IYUV:
slouken@2884
    52
    case SDL_PIXELFORMAT_YUY2:
slouken@2884
    53
    case SDL_PIXELFORMAT_UYVY:
slouken@2884
    54
    case SDL_PIXELFORMAT_YVYU:
slouken@2884
    55
        return 2;
slouken@2884
    56
    default:
slouken@2884
    57
        return 1;               /* shouldn't ever hit this. */
icculus@2835
    58
    }
icculus@2835
    59
}
icculus@2835
    60
slouken@5154
    61
/* Used to re-create the window with OpenGL capability */
slouken@5154
    62
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
icculus@2835
    63
slouken@1985
    64
static const float inv255f = 1.0f / 255.0f;
slouken@1985
    65
slouken@1918
    66
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@5147
    67
static void GL_WindowEvent(SDL_Renderer * renderer,
slouken@5147
    68
                           const SDL_WindowEvent *event);
slouken@1918
    69
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@2222
    70
static int GL_QueryTexturePixels(SDL_Renderer * renderer,
slouken@2222
    71
                                 SDL_Texture * texture, void **pixels,
slouken@2222
    72
                                 int *pitch);
slouken@1918
    73
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    74
                            const SDL_Rect * rect, const void *pixels,
slouken@1918
    75
                            int pitch);
slouken@1918
    76
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    77
                          const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1985
    78
                          int *pitch);
slouken@1918
    79
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    80
static void GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    81
                            int numrects, const SDL_Rect * rects);
slouken@3596
    82
static int GL_RenderClear(SDL_Renderer * renderer);
slouken@3596
    83
static int GL_RenderDrawPoints(SDL_Renderer * renderer,
slouken@3596
    84
                               const SDL_Point * points, int count);
slouken@3596
    85
static int GL_RenderDrawLines(SDL_Renderer * renderer,
slouken@3596
    86
                              const SDL_Point * points, int count);
slouken@3596
    87
static int GL_RenderFillRects(SDL_Renderer * renderer,
slouken@3596
    88
                              const SDL_Rect ** rects, int count);
slouken@1918
    89
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    90
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@3431
    91
static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
    92
                               Uint32 pixel_format, void * pixels, int pitch);
slouken@3431
    93
static int GL_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
    94
                                Uint32 pixel_format, const void * pixels, int pitch);
slouken@1918
    95
static void GL_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    96
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    97
static void GL_DestroyRenderer(SDL_Renderer * renderer);
slouken@1918
    98
slouken@1918
    99
slouken@1918
   100
SDL_RenderDriver GL_RenderDriver = {
slouken@1918
   101
    GL_CreateRenderer,
slouken@1918
   102
    {
slouken@1918
   103
     "opengl",
slouken@5142
   104
     (SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
slouken@5149
   105
     13,
slouken@1918
   106
     {
slouken@1965
   107
      SDL_PIXELFORMAT_RGB332,
slouken@1965
   108
      SDL_PIXELFORMAT_RGB444,
slouken@1965
   109
      SDL_PIXELFORMAT_RGB555,
slouken@1965
   110
      SDL_PIXELFORMAT_ARGB4444,
slouken@1965
   111
      SDL_PIXELFORMAT_ARGB1555,
slouken@1965
   112
      SDL_PIXELFORMAT_RGB565,
slouken@1965
   113
      SDL_PIXELFORMAT_RGB24,
slouken@1965
   114
      SDL_PIXELFORMAT_BGR24,
slouken@1965
   115
      SDL_PIXELFORMAT_RGB888,
slouken@1965
   116
      SDL_PIXELFORMAT_BGR888,
slouken@1965
   117
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
   118
      SDL_PIXELFORMAT_ABGR8888,
slouken@2813
   119
      SDL_PIXELFORMAT_ARGB2101010},
slouken@1918
   120
     0,
slouken@1918
   121
     0}
slouken@1918
   122
};
slouken@1918
   123
slouken@1918
   124
typedef struct
slouken@1918
   125
{
slouken@1918
   126
    SDL_GLContext context;
slouken@2833
   127
    SDL_bool updateSize;
slouken@2233
   128
    SDL_bool GL_ARB_texture_rectangle_supported;
slouken@1974
   129
    SDL_bool GL_EXT_paletted_texture_supported;
slouken@2845
   130
    SDL_bool GL_APPLE_ycbcr_422_supported;
slouken@2845
   131
    SDL_bool GL_MESA_ycbcr_texture_supported;
icculus@2835
   132
    SDL_bool GL_ARB_fragment_program_supported;
slouken@1927
   133
    int blendMode;
slouken@1927
   134
slouken@1927
   135
    /* OpenGL functions */
slouken@1927
   136
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
slouken@5154
   137
#include "../../video/SDL_glfuncs.h"
slouken@1927
   138
#undef SDL_PROC
slouken@1974
   139
slouken@2233
   140
    void (*glTextureRangeAPPLE) (GLenum target, GLsizei length,
slouken@2233
   141
                                 const GLvoid * pointer);
icculus@2835
   142
icculus@2835
   143
    PFNGLGETPROGRAMIVARBPROC glGetProgramivARB;
icculus@2835
   144
    PFNGLGETPROGRAMSTRINGARBPROC glGetProgramStringARB;
icculus@2835
   145
    PFNGLPROGRAMLOCALPARAMETER4FVARBPROC glProgramLocalParameter4fvARB;
icculus@2835
   146
    PFNGLDELETEPROGRAMSARBPROC glDeleteProgramsARB;
icculus@2835
   147
    PFNGLGENPROGRAMSARBPROC glGenProgramsARB;
icculus@2835
   148
    PFNGLBINDPROGRAMARBPROC glBindProgramARB;
icculus@2835
   149
    PFNGLPROGRAMSTRINGARBPROC glProgramStringARB;
icculus@2835
   150
icculus@2835
   151
    /* (optional) fragment programs */
icculus@2835
   152
    GLuint fragment_program_UYVY;
slouken@1918
   153
} GL_RenderData;
slouken@1918
   154
slouken@1918
   155
typedef struct
slouken@1918
   156
{
slouken@1918
   157
    GLuint texture;
icculus@2835
   158
    GLuint shader;
slouken@1920
   159
    GLenum type;
slouken@1918
   160
    GLfloat texw;
slouken@1918
   161
    GLfloat texh;
slouken@1920
   162
    GLenum format;
slouken@1920
   163
    GLenum formattype;
slouken@1974
   164
    Uint8 *palette;
slouken@1918
   165
    void *pixels;
slouken@1918
   166
    int pitch;
slouken@1920
   167
    SDL_DirtyRectList dirty;
slouken@2884
   168
    int HACK_RYAN_FIXME;
slouken@1918
   169
} GL_TextureData;
slouken@1918
   170
slouken@1918
   171
slouken@1924
   172
static void
slouken@1924
   173
GL_SetError(const char *prefix, GLenum result)
slouken@1924
   174
{
slouken@1924
   175
    const char *error;
slouken@1924
   176
slouken@1924
   177
    switch (result) {
slouken@1924
   178
    case GL_NO_ERROR:
slouken@1924
   179
        error = "GL_NO_ERROR";
slouken@1924
   180
        break;
slouken@1924
   181
    case GL_INVALID_ENUM:
slouken@1924
   182
        error = "GL_INVALID_ENUM";
slouken@1924
   183
        break;
slouken@1924
   184
    case GL_INVALID_VALUE:
slouken@1924
   185
        error = "GL_INVALID_VALUE";
slouken@1924
   186
        break;
slouken@1924
   187
    case GL_INVALID_OPERATION:
slouken@1924
   188
        error = "GL_INVALID_OPERATION";
slouken@1924
   189
        break;
slouken@1924
   190
    case GL_STACK_OVERFLOW:
slouken@1924
   191
        error = "GL_STACK_OVERFLOW";
slouken@1924
   192
        break;
slouken@1924
   193
    case GL_STACK_UNDERFLOW:
slouken@1924
   194
        error = "GL_STACK_UNDERFLOW";
slouken@1924
   195
        break;
slouken@1924
   196
    case GL_OUT_OF_MEMORY:
slouken@1924
   197
        error = "GL_OUT_OF_MEMORY";
slouken@1924
   198
        break;
slouken@1924
   199
    case GL_TABLE_TOO_LARGE:
slouken@1924
   200
        error = "GL_TABLE_TOO_LARGE";
slouken@1924
   201
        break;
slouken@1924
   202
    default:
slouken@1924
   203
        error = "UNKNOWN";
slouken@1924
   204
        break;
slouken@1924
   205
    }
slouken@1924
   206
    SDL_SetError("%s: %s", prefix, error);
slouken@1924
   207
}
slouken@1924
   208
slouken@1927
   209
static int
slouken@1927
   210
GL_LoadFunctions(GL_RenderData * data)
slouken@1927
   211
{
slouken@1927
   212
#ifdef __SDL_NOGETPROCADDR__
slouken@1927
   213
#define SDL_PROC(ret,func,params) data->func=func;
slouken@1927
   214
#else
slouken@1927
   215
#define SDL_PROC(ret,func,params) \
slouken@1927
   216
    do { \
slouken@1927
   217
        data->func = SDL_GL_GetProcAddress(#func); \
slouken@1927
   218
        if ( ! data->func ) { \
slouken@1927
   219
            SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
slouken@1927
   220
            return -1; \
slouken@1927
   221
        } \
slouken@1927
   222
    } while ( 0 );
slouken@1927
   223
#endif /* __SDL_NOGETPROCADDR__ */
slouken@1927
   224
slouken@5154
   225
#include "../../video/SDL_glfuncs.h"
slouken@1927
   226
#undef SDL_PROC
slouken@1927
   227
    return 0;
slouken@1927
   228
}
slouken@1927
   229
slouken@1918
   230
SDL_Renderer *
slouken@1918
   231
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1918
   232
{
slouken@1918
   233
    SDL_Renderer *renderer;
slouken@1918
   234
    GL_RenderData *data;
slouken@1952
   235
    GLint value;
slouken@5154
   236
    Uint32 window_flags;
slouken@1918
   237
slouken@5154
   238
    window_flags = SDL_GetWindowFlags(window);
slouken@5154
   239
    if (!(window_flags & SDL_WINDOW_OPENGL)) {
slouken@5154
   240
        if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
slouken@1924
   241
            return NULL;
slouken@1924
   242
        }
slouken@1918
   243
    }
slouken@1918
   244
slouken@1920
   245
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1918
   246
    if (!renderer) {
slouken@1918
   247
        SDL_OutOfMemory();
slouken@1918
   248
        return NULL;
slouken@1918
   249
    }
slouken@1918
   250
slouken@1920
   251
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   252
    if (!data) {
slouken@1918
   253
        GL_DestroyRenderer(renderer);
slouken@1918
   254
        SDL_OutOfMemory();
slouken@1918
   255
        return NULL;
slouken@1918
   256
    }
slouken@1918
   257
slouken@5147
   258
    renderer->WindowEvent = GL_WindowEvent;
slouken@1918
   259
    renderer->CreateTexture = GL_CreateTexture;
slouken@2222
   260
    renderer->QueryTexturePixels = GL_QueryTexturePixels;
slouken@1918
   261
    renderer->UpdateTexture = GL_UpdateTexture;
slouken@1918
   262
    renderer->LockTexture = GL_LockTexture;
slouken@1918
   263
    renderer->UnlockTexture = GL_UnlockTexture;
slouken@1918
   264
    renderer->DirtyTexture = GL_DirtyTexture;
slouken@3596
   265
    renderer->RenderClear = GL_RenderClear;
slouken@3596
   266
    renderer->RenderDrawPoints = GL_RenderDrawPoints;
slouken@3596
   267
    renderer->RenderDrawLines = GL_RenderDrawLines;
slouken@3596
   268
    renderer->RenderFillRects = GL_RenderFillRects;
slouken@1918
   269
    renderer->RenderCopy = GL_RenderCopy;
slouken@3431
   270
    renderer->RenderReadPixels = GL_RenderReadPixels;
slouken@3431
   271
    renderer->RenderWritePixels = GL_RenderWritePixels;
slouken@1918
   272
    renderer->RenderPresent = GL_RenderPresent;
slouken@1918
   273
    renderer->DestroyTexture = GL_DestroyTexture;
slouken@1918
   274
    renderer->DestroyRenderer = GL_DestroyRenderer;
slouken@1918
   275
    renderer->info = GL_RenderDriver.info;
slouken@3685
   276
    renderer->window = window;
slouken@1918
   277
    renderer->driverdata = data;
slouken@1918
   278
slouken@5142
   279
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@1918
   280
slouken@1927
   281
    if (GL_LoadFunctions(data) < 0) {
slouken@1927
   282
        GL_DestroyRenderer(renderer);
slouken@1927
   283
        return NULL;
slouken@1927
   284
    }
slouken@1927
   285
slouken@3685
   286
    data->context = SDL_GL_CreateContext(window);
slouken@1918
   287
    if (!data->context) {
slouken@1918
   288
        GL_DestroyRenderer(renderer);
slouken@1918
   289
        return NULL;
slouken@1918
   290
    }
slouken@3685
   291
    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
slouken@1918
   292
        GL_DestroyRenderer(renderer);
slouken@1918
   293
        return NULL;
slouken@1918
   294
    }
slouken@2246
   295
#ifdef __MACOSX__
slouken@2246
   296
    /* Enable multi-threaded rendering */
slouken@2246
   297
    /* Disabled until Ryan finishes his VBO/PBO code...
bob@2295
   298
       CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
bob@2295
   299
     */
slouken@2246
   300
#endif
slouken@2246
   301
slouken@1965
   302
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1918
   303
        SDL_GL_SetSwapInterval(1);
slouken@1918
   304
    } else {
slouken@1918
   305
        SDL_GL_SetSwapInterval(0);
slouken@1918
   306
    }
slouken@1918
   307
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@1965
   308
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1918
   309
    }
slouken@1918
   310
slouken@1952
   311
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   312
    renderer->info.max_texture_width = value;
slouken@1952
   313
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   314
    renderer->info.max_texture_height = value;
slouken@1920
   315
slouken@1926
   316
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
slouken@1926
   317
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
slouken@1926
   318
        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
slouken@1926
   319
    }
slouken@2845
   320
    if (SDL_GL_ExtensionSupported("GL_APPLE_ycbcr_422")) {
slouken@2845
   321
        data->GL_APPLE_ycbcr_422_supported = SDL_TRUE;
slouken@2845
   322
    }
slouken@2845
   323
    if (SDL_GL_ExtensionSupported("GL_MESA_ycbcr_texture")) {
slouken@2845
   324
        data->GL_MESA_ycbcr_texture_supported = SDL_TRUE;
slouken@2845
   325
    }
slouken@2233
   326
    if (SDL_GL_ExtensionSupported("GL_APPLE_texture_range")) {
slouken@2233
   327
        data->glTextureRangeAPPLE =
slouken@2233
   328
            (void (*)(GLenum, GLsizei, const GLvoid *))
slouken@2233
   329
            SDL_GL_GetProcAddress("glTextureRangeAPPLE");
slouken@2233
   330
    }
slouken@1920
   331
icculus@2835
   332
    /* we might use fragment programs for YUV data, etc. */
icculus@2835
   333
    if (SDL_GL_ExtensionSupported("GL_ARB_fragment_program")) {
icculus@2835
   334
        /* !!! FIXME: this doesn't check for errors. */
icculus@2835
   335
        /* !!! FIXME: this should really reuse the glfuncs.h stuff. */
icculus@2835
   336
        data->glGetProgramivARB = (PFNGLGETPROGRAMIVARBPROC)
icculus@2835
   337
            SDL_GL_GetProcAddress("glGetProgramivARB");
icculus@2835
   338
        data->glGetProgramStringARB = (PFNGLGETPROGRAMSTRINGARBPROC)
icculus@2835
   339
            SDL_GL_GetProcAddress("glGetProgramStringARB");
icculus@2835
   340
        data->glProgramLocalParameter4fvARB =
icculus@2835
   341
            (PFNGLPROGRAMLOCALPARAMETER4FVARBPROC)
icculus@2835
   342
            SDL_GL_GetProcAddress("glProgramLocalParameter4fvARB");
icculus@2835
   343
        data->glDeleteProgramsARB = (PFNGLDELETEPROGRAMSARBPROC)
icculus@2835
   344
            SDL_GL_GetProcAddress("glDeleteProgramsARB");
icculus@2835
   345
        data->glGenProgramsARB = (PFNGLGENPROGRAMSARBPROC)
icculus@2835
   346
            SDL_GL_GetProcAddress("glGenProgramsARB");
icculus@2835
   347
        data->glBindProgramARB = (PFNGLBINDPROGRAMARBPROC)
icculus@2835
   348
            SDL_GL_GetProcAddress("glBindProgramARB");
icculus@2835
   349
        data->glProgramStringARB = (PFNGLPROGRAMSTRINGARBPROC)
icculus@2835
   350
            SDL_GL_GetProcAddress("glProgramStringARB");
icculus@2835
   351
        data->GL_ARB_fragment_program_supported = SDL_TRUE;
icculus@2835
   352
    }
icculus@2835
   353
slouken@1918
   354
    /* Set up parameters for rendering */
slouken@1927
   355
    data->blendMode = -1;
slouken@1927
   356
    data->glDisable(GL_DEPTH_TEST);
slouken@1927
   357
    data->glDisable(GL_CULL_FACE);
slouken@3262
   358
    /* This ended up causing video discrepancies between OpenGL and Direct3D */
slouken@3262
   359
    /*data->glEnable(GL_LINE_SMOOTH);*/
slouken@1926
   360
    if (data->GL_ARB_texture_rectangle_supported) {
slouken@1927
   361
        data->glEnable(GL_TEXTURE_RECTANGLE_ARB);
slouken@1926
   362
    } else {
slouken@1927
   363
        data->glEnable(GL_TEXTURE_2D);
slouken@1926
   364
    }
slouken@2833
   365
    data->updateSize = SDL_TRUE;
slouken@1918
   366
slouken@1918
   367
    return renderer;
slouken@1918
   368
}
slouken@1918
   369
slouken@5147
   370
static SDL_GLContext SDL_CurrentContext = NULL;
slouken@5147
   371
slouken@1923
   372
static int
slouken@1923
   373
GL_ActivateRenderer(SDL_Renderer * renderer)
slouken@1923
   374
{
slouken@1923
   375
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3685
   376
    SDL_Window *window = renderer->window;
slouken@1923
   377
slouken@5147
   378
    if (SDL_CurrentContext != data->context) {
slouken@5147
   379
        if (SDL_GL_MakeCurrent(window, data->context) < 0) {
slouken@5147
   380
            return -1;
slouken@5147
   381
        }
slouken@5147
   382
        SDL_CurrentContext = data->context;
slouken@1970
   383
    }
slouken@2833
   384
    if (data->updateSize) {
slouken@5154
   385
        int w, h;
slouken@5154
   386
slouken@5154
   387
        SDL_GetWindowSize(window, &w, &h);
slouken@2836
   388
        data->glMatrixMode(GL_PROJECTION);
slouken@2836
   389
        data->glLoadIdentity();
slouken@2836
   390
        data->glMatrixMode(GL_MODELVIEW);
slouken@2836
   391
        data->glLoadIdentity();
slouken@5154
   392
        data->glViewport(0, 0, w, h);
slouken@5154
   393
        data->glOrtho(0.0, (GLdouble) w, (GLdouble) h, 0.0, 0.0, 1.0);
slouken@2833
   394
        data->updateSize = SDL_FALSE;
slouken@2833
   395
    }
slouken@1970
   396
    return 0;
slouken@1970
   397
}
slouken@1970
   398
slouken@5147
   399
static void
slouken@5147
   400
GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1970
   401
{
slouken@1970
   402
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1970
   403
slouken@5147
   404
    if (event->event == SDL_WINDOWEVENT_RESIZED) {
slouken@5147
   405
        /* Rebind the context to the window area and update matrices */
slouken@5147
   406
        SDL_CurrentContext = NULL;
slouken@5147
   407
        data->updateSize = SDL_TRUE;
slouken@5147
   408
    }
slouken@1923
   409
}
slouken@1923
   410
slouken@1922
   411
static __inline__ int
slouken@1922
   412
power_of_2(int input)
slouken@1922
   413
{
slouken@1922
   414
    int value = 1;
slouken@1922
   415
slouken@1922
   416
    while (value < input) {
slouken@1922
   417
        value <<= 1;
slouken@1922
   418
    }
slouken@1922
   419
    return value;
slouken@1922
   420
}
slouken@1922
   421
icculus@2835
   422
slouken@2858
   423
//#define DEBUG_PROGRAM_COMPILE 1
icculus@2835
   424
slouken@3468
   425
static void
slouken@3468
   426
set_shader_error(GL_RenderData * data, const char *prefix)
slouken@3468
   427
{
slouken@3468
   428
    GLint pos = 0;
slouken@3468
   429
    const GLubyte *errstr;
slouken@3468
   430
    data->glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
slouken@3468
   431
    errstr = data->glGetString(GL_PROGRAM_ERROR_STRING_ARB);
slouken@3479
   432
    SDL_SetError("%s: shader compile error at position %d: %s",
slouken@3468
   433
           prefix, (int) pos, (const char *) errstr);
slouken@3468
   434
}
slouken@3468
   435
icculus@2835
   436
static GLuint
slouken@2884
   437
compile_shader(GL_RenderData * data, GLenum shader_type, const char *_code)
icculus@2835
   438
{
icculus@2847
   439
    const int have_texture_rects = data->GL_ARB_texture_rectangle_supported;
icculus@2847
   440
    const char *replacement = have_texture_rects ? "RECT" : "2D";
slouken@2918
   441
    const size_t replacementlen = SDL_strlen(replacement);
icculus@2847
   442
    const char *token = "%TEXTURETARGET%";
slouken@2918
   443
    const size_t tokenlen = SDL_strlen(token);
icculus@2847
   444
    char *code = NULL;
icculus@2847
   445
    char *ptr = NULL;
icculus@2847
   446
    GLuint program = 0;
icculus@2847
   447
icculus@2847
   448
    /*
icculus@2847
   449
     * The TEX instruction needs a different target depending on what we use.
icculus@2847
   450
     *  To handle this, we use "%TEXTURETARGET%" and replace the string before
icculus@2847
   451
     *  compiling the shader.
icculus@2847
   452
     */
icculus@2847
   453
    code = SDL_strdup(_code);
icculus@2847
   454
    if (code == NULL)
icculus@2847
   455
        return 0;
icculus@2847
   456
slouken@2884
   457
    for (ptr = SDL_strstr(code, token); ptr; ptr = SDL_strstr(ptr + 1, token)) {
slouken@2918
   458
        SDL_memcpy(ptr, replacement, replacementlen);
slouken@2918
   459
        SDL_memmove(ptr + replacementlen, ptr + tokenlen,
slouken@2918
   460
                    SDL_strlen(ptr + tokenlen) + 1);
icculus@2847
   461
    }
icculus@2847
   462
icculus@2835
   463
#if DEBUG_PROGRAM_COMPILE
icculus@2847
   464
    printf("compiling shader:\n%s\n\n", code);
icculus@2835
   465
#endif
icculus@2835
   466
slouken@2884
   467
    data->glGetError();         /* flush any existing error state. */
icculus@2835
   468
    data->glGenProgramsARB(1, &program);
icculus@2835
   469
    data->glBindProgramARB(shader_type, program);
icculus@2835
   470
    data->glProgramStringARB(shader_type, GL_PROGRAM_FORMAT_ASCII_ARB,
slouken@3253
   471
                             (GLsizei)SDL_strlen(code), code);
icculus@2847
   472
icculus@2847
   473
    SDL_free(code);
icculus@2835
   474
slouken@2884
   475
    if (data->glGetError() == GL_INVALID_OPERATION) {
icculus@2835
   476
#if DEBUG_PROGRAM_COMPILE
icculus@2835
   477
        GLint pos = 0;
icculus@2835
   478
        const GLubyte *errstr;
icculus@2835
   479
        data->glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
icculus@2835
   480
        errstr = data->glGetString(GL_PROGRAM_ERROR_STRING_ARB);
icculus@2835
   481
        printf("program compile error at position %d: %s\n\n",
slouken@2884
   482
               (int) pos, (const char *) errstr);
icculus@2835
   483
#endif
icculus@2835
   484
        data->glBindProgramARB(shader_type, 0);
icculus@2835
   485
        data->glDeleteProgramsARB(1, &program);
icculus@2835
   486
        return 0;
icculus@2848
   487
    }
icculus@2835
   488
icculus@2835
   489
    return program;
icculus@2835
   490
}
icculus@2835
   491
icculus@2848
   492
icculus@2848
   493
/*
icculus@2848
   494
 * Fragment program that renders from UYVY textures.
icculus@2848
   495
 * The UYVY to RGB equasion is:
icculus@2848
   496
 *   R = 1.164(Y-16) + 1.596(Cr-128)
icculus@2848
   497
 *   G = 1.164(Y-16) - 0.813(Cr-128) - 0.391(Cb-128)
icculus@2848
   498
 *   B = 1.164(Y-16) + 2.018(Cb-128)
icculus@2848
   499
 * Byte layout is Cb, Y1, Cr, Y2, stored in the R, G, B, A channels.
icculus@2848
   500
 * 4 bytes == 2 pixels: Y1/Cb/Cr, Y2/Cb/Cr
icculus@2848
   501
 *
icculus@2848
   502
 * !!! FIXME: this ignores blendmodes, etc.
icculus@2848
   503
 * !!! FIXME: this could be more efficient...use a dot product for green, etc.
icculus@2848
   504
 */
slouken@2884
   505
static const char *fragment_program_UYVY_source_code = "!!ARBfp1.0\n"
icculus@2848
   506
    /* outputs... */
icculus@2835
   507
    "OUTPUT outcolor = result.color;\n"
icculus@2848
   508
    /* scratch registers... */
slouken@2884
   509
    "TEMP uyvy;\n" "TEMP luminance;\n" "TEMP work;\n"
icculus@2848
   510
    /* Halve the coordinates to grab the correct 32 bits for the fragment. */
icculus@2835
   511
    "MUL work, fragment.texcoord, { 0.5, 1.0, 1.0, 1.0 };\n"
icculus@2848
   512
    /* Sample the YUV texture. Cb, Y1, Cr, Y2, are stored in x, y, z, w. */
icculus@2847
   513
    "TEX uyvy, work, texture[0], %TEXTURETARGET%;\n"
icculus@2848
   514
    /* Do subtractions (128/255, 16/255, 128/255, 16/255) */
slouken@2846
   515
    "SUB uyvy, uyvy, { 0.501960784313726, 0.06274509803922, 0.501960784313726, 0.06274509803922 };\n"
icculus@2848
   516
    /* Choose the luminance component by texcoord. */
icculus@2848
   517
    /* !!! FIXME: laziness wins out for now... just average Y1 and Y2. */
icculus@2835
   518
    "ADD luminance, uyvy.yyyy, uyvy.wwww;\n"
icculus@2835
   519
    "MUL luminance, luminance, { 0.5, 0.5, 0.5, 0.5 };\n"
icculus@2848
   520
    /* Multiply luminance by its magic value. */
icculus@2835
   521
    "MUL luminance, luminance, { 1.164, 1.164, 1.164, 1.164 };\n"
icculus@2848
   522
    /* uyvy.xyzw becomes Cr/Cr/Cb/Cb, with multiplications. */
icculus@2835
   523
    "MUL uyvy, uyvy.zzxx, { 1.596, -0.813, 2.018, -0.391 };\n"
icculus@2848
   524
    /* Add luminance to Cr and Cb, store to RGB channels. */
icculus@2835
   525
    "ADD work.rgb, luminance, uyvy;\n"
icculus@2848
   526
    /* Do final addition for Green channel.  (!!! FIXME: this should be a DPH?) */
icculus@2835
   527
    "ADD work.g, work.g, uyvy.w;\n"
icculus@2848
   528
    /* Make sure alpha channel is fully opaque.  (!!! FIXME: blend modes!) */
icculus@2835
   529
    "MOV work.a, { 1.0 };\n"
icculus@2848
   530
    /* Store out the final fragment color... */
icculus@2835
   531
    "MOV outcolor, work;\n"
icculus@2848
   532
    /* ...and we're done! */
icculus@2835
   533
    "END\n";
icculus@2835
   534
slouken@3433
   535
static __inline__ SDL_bool
slouken@3433
   536
convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
slouken@3433
   537
               GLint* internalFormat, GLenum* format, GLenum* type)
slouken@3433
   538
{
slouken@3433
   539
    switch (pixel_format) {
slouken@3433
   540
    case SDL_PIXELFORMAT_RGB332:
slouken@3433
   541
        *internalFormat = GL_R3_G3_B2;
slouken@3433
   542
        *format = GL_RGB;
slouken@3433
   543
        *type = GL_UNSIGNED_BYTE_3_3_2;
slouken@3433
   544
        break;
slouken@3433
   545
    case SDL_PIXELFORMAT_RGB444:
slouken@3433
   546
        *internalFormat = GL_RGB4;
slouken@3433
   547
        *format = GL_RGB;
slouken@3433
   548
        *type = GL_UNSIGNED_SHORT_4_4_4_4;
slouken@3433
   549
        break;
slouken@3433
   550
    case SDL_PIXELFORMAT_RGB555:
slouken@3433
   551
        *internalFormat = GL_RGB5;
slouken@3433
   552
        *format = GL_RGB;
slouken@3433
   553
        *type = GL_UNSIGNED_SHORT_5_5_5_1;
slouken@3433
   554
        break;
slouken@3433
   555
    case SDL_PIXELFORMAT_ARGB4444:
slouken@3433
   556
        *internalFormat = GL_RGBA4;
slouken@3433
   557
        *format = GL_BGRA;
slouken@3433
   558
        *type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
slouken@3433
   559
        break;
slouken@3433
   560
    case SDL_PIXELFORMAT_ARGB1555:
slouken@3433
   561
        *internalFormat = GL_RGB5_A1;
slouken@3433
   562
        *format = GL_BGRA;
slouken@3433
   563
        *type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
slouken@3433
   564
        break;
slouken@3433
   565
    case SDL_PIXELFORMAT_RGB565:
slouken@3433
   566
        *internalFormat = GL_RGB8;
slouken@3433
   567
        *format = GL_RGB;
slouken@3433
   568
        *type = GL_UNSIGNED_SHORT_5_6_5;
slouken@3433
   569
        break;
slouken@3433
   570
    case SDL_PIXELFORMAT_RGB24:
slouken@3433
   571
        *internalFormat = GL_RGB8;
slouken@3433
   572
        *format = GL_RGB;
slouken@3433
   573
        *type = GL_UNSIGNED_BYTE;
slouken@3433
   574
        break;
slouken@3433
   575
    case SDL_PIXELFORMAT_RGB888:
slouken@3433
   576
        *internalFormat = GL_RGB8;
slouken@3433
   577
        *format = GL_BGRA;
slouken@3433
   578
        *type = GL_UNSIGNED_BYTE;
slouken@3433
   579
        break;
slouken@3433
   580
    case SDL_PIXELFORMAT_BGR24:
slouken@3433
   581
        *internalFormat = GL_RGB8;
slouken@3433
   582
        *format = GL_BGR;
slouken@3433
   583
        *type = GL_UNSIGNED_BYTE;
slouken@3433
   584
        break;
slouken@3433
   585
    case SDL_PIXELFORMAT_BGR888:
slouken@3433
   586
        *internalFormat = GL_RGB8;
slouken@3433
   587
        *format = GL_RGBA;
slouken@3433
   588
        *type = GL_UNSIGNED_BYTE;
slouken@3433
   589
        break;
slouken@3433
   590
    case SDL_PIXELFORMAT_ARGB8888:
slouken@3433
   591
#ifdef __MACOSX__
slouken@3433
   592
        *internalFormat = GL_RGBA;
slouken@3433
   593
        *format = GL_BGRA;
slouken@3433
   594
        *type = GL_UNSIGNED_INT_8_8_8_8_REV;
slouken@3433
   595
#else
slouken@3433
   596
        *internalFormat = GL_RGBA8;
slouken@3433
   597
        *format = GL_BGRA;
slouken@3433
   598
        *type = GL_UNSIGNED_BYTE;
slouken@3433
   599
#endif
slouken@3433
   600
        break;
slouken@3433
   601
    case SDL_PIXELFORMAT_ABGR8888:
slouken@3433
   602
        *internalFormat = GL_RGBA8;
slouken@3433
   603
        *format = GL_RGBA;
slouken@3433
   604
        *type = GL_UNSIGNED_BYTE;
slouken@3433
   605
        break;
slouken@3433
   606
    case SDL_PIXELFORMAT_ARGB2101010:
slouken@3433
   607
        *internalFormat = GL_RGB10_A2;
slouken@3433
   608
        *format = GL_BGRA;
slouken@3433
   609
        *type = GL_UNSIGNED_INT_2_10_10_10_REV;
slouken@3433
   610
        break;
slouken@3433
   611
    case SDL_PIXELFORMAT_UYVY:
slouken@3433
   612
        if (renderdata->GL_APPLE_ycbcr_422_supported) {
slouken@3433
   613
            *internalFormat = GL_RGB;
slouken@3433
   614
            *format = GL_YCBCR_422_APPLE;
slouken@3433
   615
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@3433
   616
            *type = GL_UNSIGNED_SHORT_8_8_APPLE;
slouken@3433
   617
#else
slouken@3433
   618
            *type = GL_UNSIGNED_SHORT_8_8_REV_APPLE;
slouken@3433
   619
#endif
slouken@3433
   620
        } else if (renderdata->GL_MESA_ycbcr_texture_supported) {
slouken@3494
   621
            *internalFormat = GL_YCBCR_MESA;
slouken@3433
   622
            *format = GL_YCBCR_MESA;
slouken@3433
   623
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@3433
   624
            *type = GL_UNSIGNED_SHORT_8_8_MESA;
slouken@3433
   625
#else
slouken@3433
   626
            *type = GL_UNSIGNED_SHORT_8_8_REV_MESA;
slouken@3433
   627
#endif
slouken@3433
   628
        } else if (renderdata->GL_ARB_fragment_program_supported) {
slouken@3433
   629
            *internalFormat = GL_RGBA;
slouken@3433
   630
            *format = GL_RGBA;
slouken@3433
   631
            *type = GL_UNSIGNED_BYTE;
slouken@3433
   632
        } else {
slouken@3433
   633
            return SDL_FALSE;
slouken@3433
   634
        }
slouken@3433
   635
        break;
slouken@3433
   636
    case SDL_PIXELFORMAT_YUY2:
slouken@3433
   637
        if (renderdata->GL_APPLE_ycbcr_422_supported) {
slouken@3433
   638
            *internalFormat = GL_RGB;
slouken@3433
   639
            *format = GL_YCBCR_422_APPLE;
slouken@3433
   640
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@3433
   641
            *type = GL_UNSIGNED_SHORT_8_8_REV_APPLE;
slouken@3433
   642
#else
slouken@3433
   643
            *type = GL_UNSIGNED_SHORT_8_8_APPLE;
slouken@3433
   644
#endif
slouken@3433
   645
        } else if (renderdata->GL_MESA_ycbcr_texture_supported) {
slouken@3494
   646
            *internalFormat = GL_YCBCR_MESA;
slouken@3433
   647
            *format = GL_YCBCR_MESA;
slouken@3433
   648
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@3433
   649
            *type = GL_UNSIGNED_SHORT_8_8_REV_MESA;
slouken@3433
   650
#else
slouken@3433
   651
            *type = GL_UNSIGNED_SHORT_8_8_MESA;
slouken@3433
   652
#endif
slouken@3433
   653
        } else {
slouken@3433
   654
            return SDL_FALSE;
slouken@3433
   655
        }
slouken@3433
   656
        break;
slouken@3433
   657
    default:
slouken@3433
   658
        return SDL_FALSE;
slouken@3433
   659
    }
slouken@3433
   660
    return SDL_TRUE;
slouken@3433
   661
}
icculus@2835
   662
slouken@1918
   663
static int
slouken@1918
   664
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   665
{
slouken@1918
   666
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   667
    GL_TextureData *data;
slouken@1920
   668
    GLint internalFormat;
slouken@1920
   669
    GLenum format, type;
slouken@1922
   670
    int texture_w, texture_h;
icculus@2835
   671
    GLuint shader = 0;
slouken@1924
   672
    GLenum result;
slouken@1918
   673
slouken@5147
   674
    GL_ActivateRenderer(renderer);
slouken@5147
   675
slouken@3433
   676
    if (!convert_format(renderdata, texture->format, &internalFormat,
slouken@3433
   677
                        &format, &type)) {
slouken@4990
   678
        SDL_SetError("Texture format %s not supported by OpenGL",
slouken@4990
   679
                     SDL_GetPixelFormatName(texture->format));
slouken@1920
   680
        return -1;
slouken@1920
   681
    }
slouken@3433
   682
    if (texture->format == SDL_PIXELFORMAT_UYVY &&
slouken@3433
   683
        !renderdata->GL_APPLE_ycbcr_422_supported &&
slouken@3433
   684
        !renderdata->GL_MESA_ycbcr_texture_supported &&
slouken@3433
   685
        renderdata->GL_ARB_fragment_program_supported) {
slouken@3433
   686
        if (renderdata->fragment_program_UYVY == 0) {
slouken@3433
   687
            renderdata->fragment_program_UYVY =
slouken@3433
   688
                compile_shader(renderdata, GL_FRAGMENT_PROGRAM_ARB,
slouken@3433
   689
                               fragment_program_UYVY_source_code);
slouken@3433
   690
            if (renderdata->fragment_program_UYVY == 0) {
slouken@3468
   691
                set_shader_error(renderdata, "UYVY");
slouken@3433
   692
                return -1;
slouken@3433
   693
            }
slouken@3433
   694
        }
slouken@3433
   695
        shader = renderdata->fragment_program_UYVY;
slouken@3433
   696
    }
slouken@1920
   697
slouken@1920
   698
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   699
    if (!data) {
slouken@1918
   700
        SDL_OutOfMemory();
slouken@1918
   701
        return -1;
slouken@1918
   702
    }
slouken@1918
   703
icculus@2835
   704
    data->shader = shader;
icculus@2835
   705
slouken@2222
   706
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
icculus@2835
   707
        data->pitch = texture->w * bytes_per_pixel(texture->format);
slouken@2222
   708
        data->pixels = SDL_malloc(texture->h * data->pitch);
slouken@2222
   709
        if (!data->pixels) {
slouken@2222
   710
            SDL_OutOfMemory();
slouken@2222
   711
            SDL_free(data);
slouken@2222
   712
            return -1;
slouken@2222
   713
        }
slouken@2222
   714
    }
slouken@2222
   715
slouken@1918
   716
    texture->driverdata = data;
slouken@1918
   717
slouken@1927
   718
    renderdata->glGetError();
slouken@1927
   719
    renderdata->glGenTextures(1, &data->texture);
slouken@1926
   720
    if (renderdata->GL_ARB_texture_rectangle_supported) {
slouken@1926
   721
        data->type = GL_TEXTURE_RECTANGLE_ARB;
slouken@1926
   722
        texture_w = texture->w;
slouken@1926
   723
        texture_h = texture->h;
icculus@2835
   724
        data->texw = (GLfloat) texture_w;
icculus@2835
   725
        data->texh = (GLfloat) texture_h;
slouken@1926
   726
    } else {
slouken@1926
   727
        data->type = GL_TEXTURE_2D;
slouken@1926
   728
        texture_w = power_of_2(texture->w);
slouken@1926
   729
        texture_h = power_of_2(texture->h);
icculus@2835
   730
        data->texw = (GLfloat) (texture->w) / texture_w;
slouken@1926
   731
        data->texh = (GLfloat) texture->h / texture_h;
slouken@1926
   732
    }
icculus@2835
   733
slouken@2839
   734
    /* YUV formats use RGBA but are really two bytes per pixel */
slouken@2839
   735
    if (internalFormat == GL_RGBA && bytes_per_pixel(texture->format) < 4) {
slouken@3707
   736
        texture_w /= 2;
slouken@3707
   737
        if (data->type == GL_TEXTURE_2D) {
slouken@3707
   738
            data->texw *= 2.0f;
slouken@3707
   739
        }
slouken@2884
   740
        data->HACK_RYAN_FIXME = 2;
slouken@2843
   741
    } else {
slouken@2884
   742
        data->HACK_RYAN_FIXME = 1;
slouken@2839
   743
    }
slouken@2839
   744
slouken@1920
   745
    data->format = format;
slouken@1920
   746
    data->formattype = type;
slouken@2884
   747
    renderdata->glEnable(data->type);
slouken@1927
   748
    renderdata->glBindTexture(data->type, data->texture);
slouken@2230
   749
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@5138
   750
                                GL_LINEAR);
slouken@2230
   751
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@5138
   752
                                GL_LINEAR);
slouken@2230
   753
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@2230
   754
                                GL_CLAMP_TO_EDGE);
slouken@2230
   755
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@2230
   756
                                GL_CLAMP_TO_EDGE);
slouken@2840
   757
#ifdef __MACOSX__
slouken@2230
   758
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
slouken@2230
   759
#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
slouken@2230
   760
#endif
slouken@2230
   761
#ifndef STORAGE_CACHED_APPLE
slouken@2230
   762
#define STORAGE_CACHED_APPLE                0x85BE
slouken@2230
   763
#endif
slouken@2230
   764
#ifndef STORAGE_SHARED_APPLE
slouken@2230
   765
#define STORAGE_SHARED_APPLE                0x85BF
slouken@2230
   766
#endif
slouken@2230
   767
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@2230
   768
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   769
                                    GL_STORAGE_SHARED_APPLE);
slouken@2230
   770
    } else {
slouken@2230
   771
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   772
                                    GL_STORAGE_CACHED_APPLE);
slouken@2230
   773
    }
slouken@2809
   774
/* This causes a crash in testoverlay for some reason.  Apple bug? */
slouken@2809
   775
#if 0
bob@2295
   776
    if (texture->access == SDL_TEXTUREACCESS_STREAMING
bob@2295
   777
        && texture->format == SDL_PIXELFORMAT_ARGB8888) {
slouken@2237
   778
        /*
bob@2295
   779
           if (renderdata->glTextureRangeAPPLE) {
bob@2295
   780
           renderdata->glTextureRangeAPPLE(data->type,
bob@2295
   781
           texture->h * data->pitch,
bob@2295
   782
           data->pixels);
bob@2295
   783
           }
bob@2295
   784
         */
slouken@2230
   785
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
slouken@2230
   786
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   787
                                 texture_h, 0, format, type, data->pixels);
slouken@2230
   788
    } else
slouken@2230
   789
#endif
slouken@2809
   790
#endif
slouken@2230
   791
    {
slouken@2230
   792
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   793
                                 texture_h, 0, format, type, NULL);
slouken@2230
   794
    }
slouken@3041
   795
    renderdata->glDisable(data->type);
slouken@1927
   796
    result = renderdata->glGetError();
slouken@1924
   797
    if (result != GL_NO_ERROR) {
slouken@1924
   798
        GL_SetError("glTexImage2D()", result);
slouken@1924
   799
        return -1;
slouken@1924
   800
    }
slouken@1918
   801
    return 0;
slouken@1918
   802
}
slouken@1918
   803
slouken@1918
   804
static int
slouken@2222
   805
GL_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@2222
   806
                      void **pixels, int *pitch)
slouken@2222
   807
{
slouken@2222
   808
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@2222
   809
slouken@2222
   810
    *pixels = data->pixels;
slouken@2222
   811
    *pitch = data->pitch;
slouken@2222
   812
    return 0;
slouken@2222
   813
}
slouken@2222
   814
slouken@1924
   815
static void
slouken@1927
   816
SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture,
slouken@1927
   817
                   int pitch)
slouken@1924
   818
{
slouken@1927
   819
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@2808
   820
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@2884
   821
                              (pitch / bytes_per_pixel(texture->format)) /
slouken@3013
   822
                              ((GL_TextureData *) texture->driverdata)->
slouken@3013
   823
                              HACK_RYAN_FIXME);
slouken@1924
   824
}
slouken@1924
   825
slouken@1918
   826
static int
slouken@1918
   827
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   828
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
   829
{
slouken@1927
   830
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   831
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1924
   832
    GLenum result;
slouken@1918
   833
slouken@5147
   834
    GL_ActivateRenderer(renderer);
slouken@5147
   835
slouken@1927
   836
    renderdata->glGetError();
slouken@1927
   837
    SetupTextureUpdate(renderdata, texture, pitch);
slouken@2884
   838
    renderdata->glEnable(data->type);
slouken@1927
   839
    renderdata->glBindTexture(data->type, data->texture);
slouken@1927
   840
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
slouken@1927
   841
                                rect->h, data->format, data->formattype,
slouken@1927
   842
                                pixels);
slouken@3041
   843
    renderdata->glDisable(data->type);
slouken@1927
   844
    result = renderdata->glGetError();
slouken@1924
   845
    if (result != GL_NO_ERROR) {
slouken@1924
   846
        GL_SetError("glTexSubImage2D()", result);
slouken@1924
   847
        return -1;
slouken@1924
   848
    }
slouken@1918
   849
    return 0;
slouken@1918
   850
}
slouken@1918
   851
slouken@1918
   852
static int
slouken@1918
   853
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   854
               const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1918
   855
               int *pitch)
slouken@1918
   856
{
slouken@1918
   857
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   858
slouken@1920
   859
    if (markDirty) {
slouken@1920
   860
        SDL_AddDirtyRect(&data->dirty, rect);
slouken@1920
   861
    }
slouken@1918
   862
slouken@1920
   863
    *pixels =
slouken@1920
   864
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
icculus@2835
   865
                  rect->x * bytes_per_pixel(texture->format));
slouken@1920
   866
    *pitch = data->pitch;
slouken@1918
   867
    return 0;
slouken@1918
   868
}
slouken@1918
   869
slouken@1918
   870
static void
slouken@1918
   871
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   872
{
slouken@1918
   873
}
slouken@1918
   874
slouken@1918
   875
static void
slouken@1918
   876
GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
slouken@1918
   877
                const SDL_Rect * rects)
slouken@1918
   878
{
slouken@1918
   879
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   880
    int i;
slouken@1918
   881
slouken@1918
   882
    for (i = 0; i < numrects; ++i) {
slouken@1920
   883
        SDL_AddDirtyRect(&data->dirty, &rects[i]);
slouken@1918
   884
    }
slouken@1918
   885
}
slouken@1918
   886
slouken@2936
   887
static void
slouken@5140
   888
GL_SetBlendMode(GL_RenderData * data, int blendMode)
slouken@2936
   889
{
slouken@2936
   890
    if (blendMode != data->blendMode) {
slouken@2936
   891
        switch (blendMode) {
slouken@2936
   892
        case SDL_BLENDMODE_NONE:
slouken@2936
   893
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
slouken@2936
   894
            data->glDisable(GL_BLEND);
slouken@2936
   895
            break;
slouken@2936
   896
        case SDL_BLENDMODE_BLEND:
slouken@2936
   897
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
   898
            data->glEnable(GL_BLEND);
slouken@2936
   899
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@2936
   900
            break;
slouken@2936
   901
        case SDL_BLENDMODE_ADD:
slouken@2936
   902
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
   903
            data->glEnable(GL_BLEND);
slouken@2936
   904
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@2936
   905
            break;
slouken@2936
   906
        }
slouken@2936
   907
        data->blendMode = blendMode;
slouken@2936
   908
    }
slouken@2936
   909
}
slouken@2936
   910
slouken@1918
   911
static int
slouken@3596
   912
GL_RenderClear(SDL_Renderer * renderer)
slouken@3596
   913
{
slouken@3596
   914
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3596
   915
slouken@5147
   916
    GL_ActivateRenderer(renderer);
slouken@5147
   917
slouken@3596
   918
    data->glClearColor((GLfloat) renderer->r * inv255f,
slouken@3596
   919
                       (GLfloat) renderer->g * inv255f,
slouken@3596
   920
                       (GLfloat) renderer->b * inv255f,
slouken@3596
   921
                       (GLfloat) renderer->a * inv255f);
slouken@3596
   922
slouken@3596
   923
    data->glClear(GL_COLOR_BUFFER_BIT);
slouken@3596
   924
slouken@3596
   925
    return 0;
slouken@3596
   926
}
slouken@3596
   927
slouken@3596
   928
static int
slouken@3596
   929
GL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   930
                    int count)
slouken@2884
   931
{
slouken@2884
   932
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   933
    int i;
slouken@2884
   934
slouken@5147
   935
    GL_ActivateRenderer(renderer);
slouken@5147
   936
slouken@5140
   937
    GL_SetBlendMode(data, renderer->blendMode);
slouken@2884
   938
slouken@2884
   939
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2884
   940
                    (GLfloat) renderer->g * inv255f,
slouken@2884
   941
                    (GLfloat) renderer->b * inv255f,
slouken@2884
   942
                    (GLfloat) renderer->a * inv255f);
slouken@2884
   943
slouken@2901
   944
    data->glBegin(GL_POINTS);
slouken@3536
   945
    for (i = 0; i < count; ++i) {
slouken@3536
   946
        data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   947
    }
slouken@2901
   948
    data->glEnd();
slouken@2901
   949
slouken@2901
   950
    return 0;
slouken@2901
   951
}
slouken@2901
   952
slouken@2901
   953
static int
slouken@3596
   954
GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   955
                   int count)
slouken@2901
   956
{
slouken@2901
   957
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   958
    int i;
slouken@2901
   959
slouken@5147
   960
    GL_ActivateRenderer(renderer);
slouken@5147
   961
slouken@5140
   962
    GL_SetBlendMode(data, renderer->blendMode);
slouken@2901
   963
slouken@2901
   964
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2901
   965
                    (GLfloat) renderer->g * inv255f,
slouken@2901
   966
                    (GLfloat) renderer->b * inv255f,
slouken@2901
   967
                    (GLfloat) renderer->a * inv255f);
slouken@2901
   968
slouken@3536
   969
    if (count > 2 && 
slouken@3536
   970
        points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
slouken@3536
   971
        data->glBegin(GL_LINE_LOOP);
slouken@3536
   972
        /* GL_LINE_LOOP takes care of the final segment */
slouken@3536
   973
        --count;
slouken@3536
   974
        for (i = 0; i < count; ++i) {
slouken@3536
   975
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   976
        }
slouken@3536
   977
        data->glEnd();
slouken@3536
   978
    } else {
slouken@5086
   979
#if defined(__APPLE__) || defined(__WIN32__)
aschiffler@4910
   980
#else
slouken@4905
   981
        int x1, y1, x2, y2;
aschiffler@4910
   982
#endif
slouken@4905
   983
slouken@3536
   984
        data->glBegin(GL_LINE_STRIP);
slouken@3536
   985
        for (i = 0; i < count; ++i) {
slouken@3536
   986
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   987
        }
slouken@3536
   988
        data->glEnd();
slouken@3474
   989
slouken@3536
   990
        /* The line is half open, so we need one more point to complete it.
slouken@3536
   991
         * http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node47.html
slouken@3536
   992
         * If we have to, we can use vertical line and horizontal line textures
slouken@3536
   993
         * for vertical and horizontal lines, and then create custom textures
slouken@3536
   994
         * for diagonal lines and software render those.  It's terrible, but at
slouken@3536
   995
         * least it would be pixel perfect.
slouken@3536
   996
         */
slouken@3536
   997
        data->glBegin(GL_POINTS);
slouken@5086
   998
#if defined(__APPLE__) || defined(__WIN32__)
slouken@3536
   999
        /* Mac OS X and Windows seem to always leave the second point open */
slouken@3536
  1000
        data->glVertex2f(0.5f + points[count-1].x, 0.5f + points[count-1].y);
slouken@3474
  1001
#else
slouken@3536
  1002
        /* Linux seems to leave the right-most or bottom-most point open */
slouken@4905
  1003
        x1 = points[0].x;
slouken@4905
  1004
        y1 = points[0].y;
slouken@4905
  1005
        x2 = points[count-1].x;
slouken@4905
  1006
        y2 = points[count-1].y;
slouken@3536
  1007
slouken@3536
  1008
        if (x1 > x2) {
slouken@3536
  1009
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@3536
  1010
        } else if (x2 > x1) {
slouken@3536
  1011
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@3536
  1012
        } else if (y1 > y2) {
slouken@3536
  1013
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@3536
  1014
        } else if (y2 > y1) {
slouken@3536
  1015
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@3536
  1016
        }
slouken@3536
  1017
#endif
slouken@3536
  1018
        data->glEnd();
slouken@3474
  1019
    }
slouken@3455
  1020
slouken@1918
  1021
    return 0;
slouken@1918
  1022
}
slouken@1918
  1023
slouken@1918
  1024
static int
slouken@3596
  1025
GL_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count)
slouken@2925
  1026
{
slouken@2925
  1027
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
  1028
    int i;
slouken@2925
  1029
slouken@5147
  1030
    GL_ActivateRenderer(renderer);
slouken@5147
  1031
slouken@5140
  1032
    GL_SetBlendMode(data, renderer->blendMode);
slouken@2936
  1033
slouken@2925
  1034
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2925
  1035
                    (GLfloat) renderer->g * inv255f,
slouken@2925
  1036
                    (GLfloat) renderer->b * inv255f,
slouken@2925
  1037
                    (GLfloat) renderer->a * inv255f);
slouken@2936
  1038
slouken@3536
  1039
    for (i = 0; i < count; ++i) {
slouken@3536
  1040
        const SDL_Rect *rect = rects[i];
slouken@3536
  1041
slouken@3536
  1042
        data->glRecti(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
slouken@3536
  1043
    }
slouken@2925
  1044
slouken@2925
  1045
    return 0;
slouken@2925
  1046
}
slouken@2925
  1047
slouken@2925
  1048
static int
slouken@1918
  1049
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
  1050
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1918
  1051
{
slouken@1918
  1052
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1053
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@1918
  1054
    int minx, miny, maxx, maxy;
slouken@1918
  1055
    GLfloat minu, maxu, minv, maxv;
slouken@1918
  1056
slouken@5147
  1057
    GL_ActivateRenderer(renderer);
slouken@5147
  1058
slouken@2275
  1059
    if (texturedata->dirty.list) {
slouken@1920
  1060
        SDL_DirtyRect *dirty;
slouken@1920
  1061
        void *pixels;
icculus@2835
  1062
        int bpp = bytes_per_pixel(texture->format);
slouken@1920
  1063
        int pitch = texturedata->pitch;
slouken@1920
  1064
slouken@1927
  1065
        SetupTextureUpdate(data, texture, pitch);
slouken@2884
  1066
        data->glEnable(texturedata->type);
slouken@1927
  1067
        data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1920
  1068
        for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
slouken@1920
  1069
            SDL_Rect *rect = &dirty->rect;
slouken@1920
  1070
            pixels =
slouken@1920
  1071
                (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
slouken@1920
  1072
                          rect->x * bpp);
slouken@1927
  1073
            data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
slouken@2884
  1074
                                  rect->w / texturedata->HACK_RYAN_FIXME,
slouken@2884
  1075
                                  rect->h, texturedata->format,
slouken@1927
  1076
                                  texturedata->formattype, pixels);
slouken@1920
  1077
        }
slouken@1920
  1078
        SDL_ClearDirtyRects(&texturedata->dirty);
slouken@1920
  1079
    }
slouken@1920
  1080
slouken@1918
  1081
    minx = dstrect->x;
slouken@1918
  1082
    miny = dstrect->y;
slouken@1918
  1083
    maxx = dstrect->x + dstrect->w;
slouken@1918
  1084
    maxy = dstrect->y + dstrect->h;
slouken@1918
  1085
slouken@1918
  1086
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
  1087
    minu *= texturedata->texw;
slouken@1918
  1088
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
  1089
    maxu *= texturedata->texw;
slouken@1918
  1090
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
  1091
    minv *= texturedata->texh;
slouken@1918
  1092
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
  1093
    maxv *= texturedata->texh;
slouken@1918
  1094
slouken@2884
  1095
    data->glEnable(texturedata->type);
slouken@1927
  1096
    data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1918
  1097
slouken@1985
  1098
    if (texture->modMode) {
slouken@1985
  1099
        data->glColor4f((GLfloat) texture->r * inv255f,
slouken@1985
  1100
                        (GLfloat) texture->g * inv255f,
slouken@1985
  1101
                        (GLfloat) texture->b * inv255f,
slouken@1985
  1102
                        (GLfloat) texture->a * inv255f);
slouken@1985
  1103
    } else {
slouken@1985
  1104
        data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
slouken@1985
  1105
    }
slouken@1985
  1106
slouken@5140
  1107
    GL_SetBlendMode(data, texture->blendMode);
slouken@1918
  1108
slouken@5140
  1109
    /* Set up the shader for the copy, if any */
slouken@5140
  1110
    if (texturedata->shader) {
icculus@2835
  1111
        data->glEnable(GL_FRAGMENT_PROGRAM_ARB);
slouken@5140
  1112
        data->glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, texturedata->shader);
icculus@2835
  1113
    }
icculus@2835
  1114
slouken@1927
  1115
    data->glBegin(GL_TRIANGLE_STRIP);
slouken@1927
  1116
    data->glTexCoord2f(minu, minv);
slouken@3472
  1117
    data->glVertex2f((GLfloat) minx, (GLfloat) miny);
slouken@1927
  1118
    data->glTexCoord2f(maxu, minv);
slouken@3472
  1119
    data->glVertex2f((GLfloat) maxx, (GLfloat) miny);
slouken@1927
  1120
    data->glTexCoord2f(minu, maxv);
slouken@3472
  1121
    data->glVertex2f((GLfloat) minx, (GLfloat) maxy);
slouken@1927
  1122
    data->glTexCoord2f(maxu, maxv);
slouken@3472
  1123
    data->glVertex2f((GLfloat) maxx, (GLfloat) maxy);
slouken@1927
  1124
    data->glEnd();
slouken@1918
  1125
slouken@5140
  1126
    if (texturedata->shader) {
icculus@2835
  1127
        data->glDisable(GL_FRAGMENT_PROGRAM_ARB);
icculus@2835
  1128
    }
slouken@2884
  1129
slouken@2884
  1130
    data->glDisable(texturedata->type);
slouken@2884
  1131
slouken@1918
  1132
    return 0;
slouken@1918
  1133
}
slouken@1918
  1134
slouken@3431
  1135
static int
slouken@3431
  1136
GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
  1137
                    Uint32 pixel_format, void * pixels, int pitch)
slouken@3431
  1138
{
slouken@3433
  1139
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3685
  1140
    SDL_Window *window = renderer->window;
slouken@3433
  1141
    GLint internalFormat;
slouken@3433
  1142
    GLenum format, type;
slouken@3435
  1143
    Uint8 *src, *dst, *tmp;
slouken@5154
  1144
    int w, h, length, rows;
slouken@3433
  1145
slouken@5147
  1146
    GL_ActivateRenderer(renderer);
slouken@5147
  1147
slouken@3433
  1148
    if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
slouken@3435
  1149
        /* FIXME: Do a temp copy to a format that is supported */
slouken@3433
  1150
        SDL_SetError("Unsupported pixel format");
slouken@3433
  1151
        return -1;
slouken@3433
  1152
    }
slouken@3433
  1153
slouken@5154
  1154
    SDL_GetWindowSize(window, &w, &h);
slouken@5154
  1155
slouken@3446
  1156
    data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
slouken@3446
  1157
    data->glPixelStorei(GL_PACK_ROW_LENGTH,
slouken@3446
  1158
                        (pitch / bytes_per_pixel(pixel_format)));
slouken@3433
  1159
slouken@5154
  1160
    data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
slouken@3435
  1161
                       format, type, pixels);
slouken@3435
  1162
slouken@3435
  1163
    /* Flip the rows to be top-down */
slouken@3435
  1164
    length = rect->w * bytes_per_pixel(pixel_format);
slouken@3435
  1165
    src = (Uint8*)pixels + (rect->h-1)*pitch;
slouken@3435
  1166
    dst = (Uint8*)pixels;
slouken@3435
  1167
    tmp = SDL_stack_alloc(Uint8, length);
slouken@3435
  1168
    rows = rect->h / 2;
slouken@3435
  1169
    while (rows--) {
slouken@3435
  1170
        SDL_memcpy(tmp, dst, length);
slouken@3435
  1171
        SDL_memcpy(dst, src, length);
slouken@3435
  1172
        SDL_memcpy(src, tmp, length);
slouken@3447
  1173
        dst += pitch;
slouken@3447
  1174
        src -= pitch;
slouken@3435
  1175
    }
slouken@3435
  1176
    SDL_stack_free(tmp);
slouken@3440
  1177
slouken@3440
  1178
    return 0;
slouken@3431
  1179
}
slouken@3431
  1180
slouken@3431
  1181
static int
slouken@3431
  1182
GL_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
  1183
                     Uint32 pixel_format, const void * pixels, int pitch)
slouken@3431
  1184
{
slouken@3435
  1185
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3685
  1186
    SDL_Window *window = renderer->window;
slouken@3435
  1187
    GLint internalFormat;
slouken@3435
  1188
    GLenum format, type;
slouken@3607
  1189
    Uint8 *src, *dst, *tmp;
slouken@5154
  1190
    int w, h, length, rows;
slouken@3435
  1191
slouken@5147
  1192
    GL_ActivateRenderer(renderer);
slouken@5147
  1193
slouken@3435
  1194
    if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
slouken@3435
  1195
        /* FIXME: Do a temp copy to a format that is supported */
slouken@3435
  1196
        SDL_SetError("Unsupported pixel format");
slouken@3435
  1197
        return -1;
slouken@3435
  1198
    }
slouken@3435
  1199
slouken@5154
  1200
    SDL_GetWindowSize(window, &w, &h);
slouken@5154
  1201
slouken@3435
  1202
    data->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@3435
  1203
    data->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@3435
  1204
                        (pitch / bytes_per_pixel(pixel_format)));
slouken@3435
  1205
slouken@3607
  1206
    /* Flip the rows to be bottom-up */
slouken@3607
  1207
    length = rect->h * rect->w * pitch;
slouken@3607
  1208
    tmp = SDL_stack_alloc(Uint8, length);
slouken@3607
  1209
    src = (Uint8*)pixels + (rect->h-1)*pitch;
slouken@3607
  1210
    dst = (Uint8*)tmp;
slouken@3607
  1211
    rows = rect->h;
slouken@3607
  1212
    while (rows--) {
slouken@3607
  1213
        SDL_memcpy(dst, src, pitch);
slouken@3607
  1214
        dst += pitch;
slouken@3607
  1215
        src -= pitch;
slouken@3607
  1216
    }
slouken@3607
  1217
slouken@5154
  1218
    data->glRasterPos2i(rect->x, (h-rect->y));
slouken@3607
  1219
    data->glDrawPixels(rect->w, rect->h, format, type, tmp);
slouken@3607
  1220
    SDL_stack_free(tmp);
slouken@3440
  1221
slouken@3440
  1222
    return 0;
slouken@3431
  1223
}
slouken@3431
  1224
slouken@1918
  1225
static void
slouken@1918
  1226
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
  1227
{
slouken@5147
  1228
    GL_ActivateRenderer(renderer);
slouken@5147
  1229
slouken@1918
  1230
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
  1231
}
slouken@1918
  1232
slouken@1918
  1233
static void
slouken@1918
  1234
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
  1235
{
slouken@1927
  1236
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1237
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
  1238
slouken@5147
  1239
    GL_ActivateRenderer(renderer);
slouken@5147
  1240
slouken@1918
  1241
    if (!data) {
slouken@1918
  1242
        return;
slouken@1918
  1243
    }
slouken@1918
  1244
    if (data->texture) {
slouken@1927
  1245
        renderdata->glDeleteTextures(1, &data->texture);
slouken@1918
  1246
    }
slouken@1974
  1247
    if (data->palette) {
slouken@1974
  1248
        SDL_free(data->palette);
slouken@1974
  1249
    }
slouken@1920
  1250
    if (data->pixels) {
slouken@1920
  1251
        SDL_free(data->pixels);
slouken@1920
  1252
    }
slouken@1920
  1253
    SDL_FreeDirtyRects(&data->dirty);
slouken@1918
  1254
    SDL_free(data);
slouken@1918
  1255
    texture->driverdata = NULL;
slouken@1918
  1256
}
slouken@1918
  1257
slouken@1975
  1258
static void
slouken@1918
  1259
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
  1260
{
slouken@1918
  1261
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1262
slouken@1918
  1263
    if (data) {
slouken@1920
  1264
        if (data->context) {
icculus@2835
  1265
            if (data->GL_ARB_fragment_program_supported) {
icculus@2835
  1266
                data->glDisable(GL_FRAGMENT_PROGRAM_ARB);
icculus@2835
  1267
                data->glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0);
slouken@3468
  1268
                if (data->fragment_program_UYVY &&
slouken@3468
  1269
                    data->fragment_program_UYVY != ~0) {
slouken@2884
  1270
                    data->glDeleteProgramsARB(1,
slouken@2884
  1271
                                              &data->fragment_program_UYVY);
icculus@2835
  1272
                }
icculus@2835
  1273
            }
icculus@2835
  1274
bob@2328
  1275
            /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
slouken@1920
  1276
            SDL_GL_DeleteContext(data->context);
slouken@1918
  1277
        }
slouken@1918
  1278
        SDL_free(data);
slouken@1918
  1279
    }
slouken@1918
  1280
    SDL_free(renderer);
slouken@1918
  1281
}
slouken@1918
  1282
slouken@1952
  1283
#endif /* SDL_VIDEO_RENDER_OGL */
slouken@1918
  1284
slouken@1918
  1285
/* vi: set ts=4 sw=4 expandtab: */