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