src/video/SDL_renderer_gl.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 19 Sep 2009 04:43:41 +0000
changeset 3262 e3d33bd599eb
parent 3253 5d7ef5970073
child 3264 8fe0806637df
permissions -rw-r--r--
Stefan Ullinger

Hi,

so here we go: GL_LINE_SMOOTH bad for business! ;)

I checked the code within SDL_renderer_gl.c / GL_CreateRenderer(..)
and have found the reason for blurred lines when using OpenGL.
I have attached 2 images here.
I don't know whether this has to be changed and I don't know if there
is an option to turn it off/on...just wanted to say that with GL_LINE_SMOOTH
enabled there is a difference when rendering with opengl compared to directx.
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@2901
    99
static int GL_RenderPoint(SDL_Renderer * renderer, int x, int y);
slouken@2884
   100
static int GL_RenderLine(SDL_Renderer * renderer, int x1, int y1, int x2,
slouken@2884
   101
                         int y2);
slouken@2884
   102
static int GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect);
slouken@1918
   103
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   104
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@2884
   105
slouken@1918
   106
static void GL_RenderPresent(SDL_Renderer * renderer);
slouken@1918
   107
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
   108
static void GL_DestroyRenderer(SDL_Renderer * renderer);
slouken@1918
   109
slouken@1918
   110
slouken@1918
   111
SDL_RenderDriver GL_RenderDriver = {
slouken@1918
   112
    GL_CreateRenderer,
slouken@1918
   113
    {
slouken@1918
   114
     "opengl",
slouken@1974
   115
     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD |
slouken@1974
   116
      SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
slouken@1985
   117
     (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
slouken@1985
   118
      SDL_TEXTUREMODULATE_ALPHA),
slouken@2884
   119
     (SDL_BLENDMODE_NONE | SDL_BLENDMODE_MASK |
slouken@2884
   120
      SDL_BLENDMODE_BLEND | SDL_BLENDMODE_ADD | SDL_BLENDMODE_MOD),
slouken@1965
   121
     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST |
slouken@1965
   122
      SDL_TEXTURESCALEMODE_SLOW),
slouken@2813
   123
     15,
slouken@1918
   124
     {
slouken@1965
   125
      SDL_PIXELFORMAT_INDEX1LSB,
slouken@1965
   126
      SDL_PIXELFORMAT_INDEX1MSB,
slouken@1965
   127
      SDL_PIXELFORMAT_INDEX8,
slouken@1965
   128
      SDL_PIXELFORMAT_RGB332,
slouken@1965
   129
      SDL_PIXELFORMAT_RGB444,
slouken@1965
   130
      SDL_PIXELFORMAT_RGB555,
slouken@1965
   131
      SDL_PIXELFORMAT_ARGB4444,
slouken@1965
   132
      SDL_PIXELFORMAT_ARGB1555,
slouken@1965
   133
      SDL_PIXELFORMAT_RGB565,
slouken@1965
   134
      SDL_PIXELFORMAT_RGB24,
slouken@1965
   135
      SDL_PIXELFORMAT_BGR24,
slouken@1965
   136
      SDL_PIXELFORMAT_RGB888,
slouken@1965
   137
      SDL_PIXELFORMAT_BGR888,
slouken@1965
   138
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
   139
      SDL_PIXELFORMAT_ABGR8888,
slouken@2813
   140
      SDL_PIXELFORMAT_ARGB2101010},
slouken@1918
   141
     0,
slouken@1918
   142
     0}
slouken@1918
   143
};
slouken@1918
   144
slouken@1918
   145
typedef struct
slouken@1918
   146
{
slouken@1918
   147
    SDL_GLContext context;
slouken@2833
   148
    SDL_bool updateSize;
slouken@2233
   149
    SDL_bool GL_ARB_texture_rectangle_supported;
slouken@1974
   150
    SDL_bool GL_EXT_paletted_texture_supported;
slouken@2845
   151
    SDL_bool GL_APPLE_ycbcr_422_supported;
slouken@2845
   152
    SDL_bool GL_MESA_ycbcr_texture_supported;
icculus@2835
   153
    SDL_bool GL_ARB_fragment_program_supported;
slouken@1927
   154
    int blendMode;
slouken@1927
   155
    int scaleMode;
slouken@1927
   156
slouken@1927
   157
    /* OpenGL functions */
slouken@1927
   158
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
slouken@1927
   159
#include "SDL_glfuncs.h"
slouken@1927
   160
#undef SDL_PROC
slouken@1974
   161
slouken@1974
   162
    PFNGLCOLORTABLEEXTPROC glColorTableEXT;
slouken@2233
   163
    void (*glTextureRangeAPPLE) (GLenum target, GLsizei length,
slouken@2233
   164
                                 const GLvoid * pointer);
icculus@2835
   165
icculus@2835
   166
    PFNGLGETPROGRAMIVARBPROC glGetProgramivARB;
icculus@2835
   167
    PFNGLGETPROGRAMSTRINGARBPROC glGetProgramStringARB;
icculus@2835
   168
    PFNGLPROGRAMLOCALPARAMETER4FVARBPROC glProgramLocalParameter4fvARB;
icculus@2835
   169
    PFNGLDELETEPROGRAMSARBPROC glDeleteProgramsARB;
icculus@2835
   170
    PFNGLGENPROGRAMSARBPROC glGenProgramsARB;
icculus@2835
   171
    PFNGLBINDPROGRAMARBPROC glBindProgramARB;
icculus@2835
   172
    PFNGLPROGRAMSTRINGARBPROC glProgramStringARB;
icculus@2835
   173
icculus@2835
   174
    /* (optional) fragment programs */
icculus@2835
   175
    GLuint fragment_program_UYVY;
slouken@1918
   176
} GL_RenderData;
slouken@1918
   177
slouken@1918
   178
typedef struct
slouken@1918
   179
{
slouken@1918
   180
    GLuint texture;
icculus@2835
   181
    GLuint shader;
slouken@1920
   182
    GLenum type;
slouken@1918
   183
    GLfloat texw;
slouken@1918
   184
    GLfloat texh;
slouken@1920
   185
    GLenum format;
slouken@1920
   186
    GLenum formattype;
slouken@1974
   187
    Uint8 *palette;
slouken@1918
   188
    void *pixels;
slouken@1918
   189
    int pitch;
slouken@1920
   190
    SDL_DirtyRectList dirty;
slouken@2884
   191
    int HACK_RYAN_FIXME;
slouken@1918
   192
} GL_TextureData;
slouken@1918
   193
slouken@1918
   194
slouken@1924
   195
static void
slouken@1924
   196
GL_SetError(const char *prefix, GLenum result)
slouken@1924
   197
{
slouken@1924
   198
    const char *error;
slouken@1924
   199
slouken@1924
   200
    switch (result) {
slouken@1924
   201
    case GL_NO_ERROR:
slouken@1924
   202
        error = "GL_NO_ERROR";
slouken@1924
   203
        break;
slouken@1924
   204
    case GL_INVALID_ENUM:
slouken@1924
   205
        error = "GL_INVALID_ENUM";
slouken@1924
   206
        break;
slouken@1924
   207
    case GL_INVALID_VALUE:
slouken@1924
   208
        error = "GL_INVALID_VALUE";
slouken@1924
   209
        break;
slouken@1924
   210
    case GL_INVALID_OPERATION:
slouken@1924
   211
        error = "GL_INVALID_OPERATION";
slouken@1924
   212
        break;
slouken@1924
   213
    case GL_STACK_OVERFLOW:
slouken@1924
   214
        error = "GL_STACK_OVERFLOW";
slouken@1924
   215
        break;
slouken@1924
   216
    case GL_STACK_UNDERFLOW:
slouken@1924
   217
        error = "GL_STACK_UNDERFLOW";
slouken@1924
   218
        break;
slouken@1924
   219
    case GL_OUT_OF_MEMORY:
slouken@1924
   220
        error = "GL_OUT_OF_MEMORY";
slouken@1924
   221
        break;
slouken@1924
   222
    case GL_TABLE_TOO_LARGE:
slouken@1924
   223
        error = "GL_TABLE_TOO_LARGE";
slouken@1924
   224
        break;
slouken@1924
   225
    default:
slouken@1924
   226
        error = "UNKNOWN";
slouken@1924
   227
        break;
slouken@1924
   228
    }
slouken@1924
   229
    SDL_SetError("%s: %s", prefix, error);
slouken@1924
   230
}
slouken@1924
   231
slouken@1927
   232
static int
slouken@1927
   233
GL_LoadFunctions(GL_RenderData * data)
slouken@1927
   234
{
slouken@1927
   235
#if defined(__QNXNTO__) && (_NTO_VERSION < 630)
slouken@1927
   236
#define __SDL_NOGETPROCADDR__
slouken@1927
   237
#elif defined(__MINT__)
slouken@1927
   238
#define __SDL_NOGETPROCADDR__
slouken@1927
   239
#endif
slouken@1927
   240
#ifdef __SDL_NOGETPROCADDR__
slouken@1927
   241
#define SDL_PROC(ret,func,params) data->func=func;
slouken@1927
   242
#else
slouken@1927
   243
#define SDL_PROC(ret,func,params) \
slouken@1927
   244
    do { \
slouken@1927
   245
        data->func = SDL_GL_GetProcAddress(#func); \
slouken@1927
   246
        if ( ! data->func ) { \
slouken@1927
   247
            SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
slouken@1927
   248
            return -1; \
slouken@1927
   249
        } \
slouken@1927
   250
    } while ( 0 );
slouken@1927
   251
#endif /* __SDL_NOGETPROCADDR__ */
slouken@1927
   252
slouken@1927
   253
#include "SDL_glfuncs.h"
slouken@1927
   254
#undef SDL_PROC
slouken@1927
   255
    return 0;
slouken@1927
   256
}
slouken@1927
   257
slouken@1918
   258
void
slouken@1918
   259
GL_AddRenderDriver(_THIS)
slouken@1918
   260
{
slouken@1920
   261
    if (_this->GL_CreateContext) {
slouken@1918
   262
        SDL_AddRenderDriver(0, &GL_RenderDriver);
slouken@1918
   263
    }
slouken@1918
   264
}
slouken@1918
   265
slouken@1918
   266
SDL_Renderer *
slouken@1918
   267
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1918
   268
{
slouken@1918
   269
    SDL_Renderer *renderer;
slouken@1918
   270
    GL_RenderData *data;
slouken@1952
   271
    GLint value;
slouken@1974
   272
    int doublebuffer;
slouken@1918
   273
slouken@1974
   274
    /* Render directly to the window, unless we're compositing */
slouken@1974
   275
#ifndef __MACOSX__
slouken@1974
   276
    if (flags & SDL_RENDERER_SINGLEBUFFER) {
slouken@1974
   277
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
slouken@1974
   278
    }
slouken@1974
   279
#endif
slouken@1918
   280
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@1928
   281
        if (SDL_RecreateWindow(window, window->flags | SDL_WINDOW_OPENGL) < 0) {
slouken@1924
   282
            return NULL;
slouken@1924
   283
        }
slouken@1918
   284
    }
slouken@1918
   285
slouken@1920
   286
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1918
   287
    if (!renderer) {
slouken@1918
   288
        SDL_OutOfMemory();
slouken@1918
   289
        return NULL;
slouken@1918
   290
    }
slouken@1918
   291
slouken@1920
   292
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   293
    if (!data) {
slouken@1918
   294
        GL_DestroyRenderer(renderer);
slouken@1918
   295
        SDL_OutOfMemory();
slouken@1918
   296
        return NULL;
slouken@1918
   297
    }
slouken@1918
   298
slouken@1923
   299
    renderer->ActivateRenderer = GL_ActivateRenderer;
slouken@1970
   300
    renderer->DisplayModeChanged = GL_DisplayModeChanged;
slouken@1918
   301
    renderer->CreateTexture = GL_CreateTexture;
slouken@2222
   302
    renderer->QueryTexturePixels = GL_QueryTexturePixels;
slouken@1918
   303
    renderer->SetTexturePalette = GL_SetTexturePalette;
slouken@1918
   304
    renderer->GetTexturePalette = GL_GetTexturePalette;
slouken@1985
   305
    renderer->SetTextureColorMod = GL_SetTextureColorMod;
slouken@1985
   306
    renderer->SetTextureAlphaMod = GL_SetTextureAlphaMod;
slouken@1985
   307
    renderer->SetTextureBlendMode = GL_SetTextureBlendMode;
slouken@1985
   308
    renderer->SetTextureScaleMode = GL_SetTextureScaleMode;
slouken@1918
   309
    renderer->UpdateTexture = GL_UpdateTexture;
slouken@1918
   310
    renderer->LockTexture = GL_LockTexture;
slouken@1918
   311
    renderer->UnlockTexture = GL_UnlockTexture;
slouken@1918
   312
    renderer->DirtyTexture = GL_DirtyTexture;
slouken@2901
   313
    renderer->RenderPoint = GL_RenderPoint;
slouken@2884
   314
    renderer->RenderLine = GL_RenderLine;
slouken@1918
   315
    renderer->RenderFill = GL_RenderFill;
slouken@1918
   316
    renderer->RenderCopy = GL_RenderCopy;
slouken@1918
   317
    renderer->RenderPresent = GL_RenderPresent;
slouken@1918
   318
    renderer->DestroyTexture = GL_DestroyTexture;
slouken@1918
   319
    renderer->DestroyRenderer = GL_DestroyRenderer;
slouken@1918
   320
    renderer->info = GL_RenderDriver.info;
slouken@1918
   321
    renderer->window = window->id;
slouken@1918
   322
    renderer->driverdata = data;
slouken@1918
   323
slouken@1918
   324
    renderer->info.flags =
slouken@1965
   325
        (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED);
slouken@1918
   326
slouken@1927
   327
    if (GL_LoadFunctions(data) < 0) {
slouken@1927
   328
        GL_DestroyRenderer(renderer);
slouken@1927
   329
        return NULL;
slouken@1927
   330
    }
slouken@1927
   331
slouken@1918
   332
    data->context = SDL_GL_CreateContext(window->id);
slouken@1918
   333
    if (!data->context) {
slouken@1918
   334
        GL_DestroyRenderer(renderer);
slouken@1918
   335
        return NULL;
slouken@1918
   336
    }
slouken@1918
   337
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
slouken@1918
   338
        GL_DestroyRenderer(renderer);
slouken@1918
   339
        return NULL;
slouken@1918
   340
    }
slouken@2246
   341
#ifdef __MACOSX__
slouken@2246
   342
    /* Enable multi-threaded rendering */
slouken@2246
   343
    /* Disabled until Ryan finishes his VBO/PBO code...
bob@2295
   344
       CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
bob@2295
   345
     */
slouken@2246
   346
#endif
slouken@2246
   347
slouken@1965
   348
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1918
   349
        SDL_GL_SetSwapInterval(1);
slouken@1918
   350
    } else {
slouken@1918
   351
        SDL_GL_SetSwapInterval(0);
slouken@1918
   352
    }
slouken@1918
   353
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@1965
   354
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1918
   355
    }
slouken@1918
   356
slouken@1974
   357
    if (SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer) == 0) {
slouken@1974
   358
        if (!doublebuffer) {
slouken@1974
   359
            renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER;
slouken@1974
   360
        }
slouken@1974
   361
    }
slouken@1974
   362
slouken@1952
   363
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   364
    renderer->info.max_texture_width = value;
slouken@1952
   365
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   366
    renderer->info.max_texture_height = value;
slouken@1920
   367
slouken@1926
   368
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
slouken@1926
   369
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
slouken@1926
   370
        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
slouken@1926
   371
    }
slouken@1974
   372
    if (SDL_GL_ExtensionSupported("GL_EXT_paletted_texture")) {
slouken@1974
   373
        data->GL_EXT_paletted_texture_supported = SDL_TRUE;
slouken@1974
   374
        data->glColorTableEXT =
slouken@1974
   375
            (PFNGLCOLORTABLEEXTPROC) SDL_GL_GetProcAddress("glColorTableEXT");
slouken@1974
   376
    } else {
slouken@1974
   377
        /* Don't advertise support for 8-bit indexed texture format */
slouken@1974
   378
        Uint32 i, j;
slouken@1974
   379
        SDL_RendererInfo *info = &renderer->info;
slouken@1974
   380
        for (i = 0, j = 0; i < info->num_texture_formats; ++i) {
slouken@1974
   381
            if (info->texture_formats[i] != SDL_PIXELFORMAT_INDEX8) {
slouken@1974
   382
                info->texture_formats[j++] = info->texture_formats[i];
slouken@1974
   383
            }
slouken@1974
   384
        }
slouken@1974
   385
        --info->num_texture_formats;
slouken@1974
   386
    }
slouken@2845
   387
    if (SDL_GL_ExtensionSupported("GL_APPLE_ycbcr_422")) {
slouken@2845
   388
        data->GL_APPLE_ycbcr_422_supported = SDL_TRUE;
slouken@2845
   389
    }
slouken@2845
   390
    if (SDL_GL_ExtensionSupported("GL_MESA_ycbcr_texture")) {
slouken@2845
   391
        data->GL_MESA_ycbcr_texture_supported = SDL_TRUE;
slouken@2845
   392
    }
slouken@2233
   393
    if (SDL_GL_ExtensionSupported("GL_APPLE_texture_range")) {
slouken@2233
   394
        data->glTextureRangeAPPLE =
slouken@2233
   395
            (void (*)(GLenum, GLsizei, const GLvoid *))
slouken@2233
   396
            SDL_GL_GetProcAddress("glTextureRangeAPPLE");
slouken@2233
   397
    }
slouken@1920
   398
icculus@2835
   399
    /* we might use fragment programs for YUV data, etc. */
icculus@2835
   400
    if (SDL_GL_ExtensionSupported("GL_ARB_fragment_program")) {
icculus@2835
   401
        /* !!! FIXME: this doesn't check for errors. */
icculus@2835
   402
        /* !!! FIXME: this should really reuse the glfuncs.h stuff. */
icculus@2835
   403
        data->glGetProgramivARB = (PFNGLGETPROGRAMIVARBPROC)
icculus@2835
   404
            SDL_GL_GetProcAddress("glGetProgramivARB");
icculus@2835
   405
        data->glGetProgramStringARB = (PFNGLGETPROGRAMSTRINGARBPROC)
icculus@2835
   406
            SDL_GL_GetProcAddress("glGetProgramStringARB");
icculus@2835
   407
        data->glProgramLocalParameter4fvARB =
icculus@2835
   408
            (PFNGLPROGRAMLOCALPARAMETER4FVARBPROC)
icculus@2835
   409
            SDL_GL_GetProcAddress("glProgramLocalParameter4fvARB");
icculus@2835
   410
        data->glDeleteProgramsARB = (PFNGLDELETEPROGRAMSARBPROC)
icculus@2835
   411
            SDL_GL_GetProcAddress("glDeleteProgramsARB");
icculus@2835
   412
        data->glGenProgramsARB = (PFNGLGENPROGRAMSARBPROC)
icculus@2835
   413
            SDL_GL_GetProcAddress("glGenProgramsARB");
icculus@2835
   414
        data->glBindProgramARB = (PFNGLBINDPROGRAMARBPROC)
icculus@2835
   415
            SDL_GL_GetProcAddress("glBindProgramARB");
icculus@2835
   416
        data->glProgramStringARB = (PFNGLPROGRAMSTRINGARBPROC)
icculus@2835
   417
            SDL_GL_GetProcAddress("glProgramStringARB");
icculus@2835
   418
        data->GL_ARB_fragment_program_supported = SDL_TRUE;
icculus@2835
   419
    }
icculus@2835
   420
slouken@1918
   421
    /* Set up parameters for rendering */
slouken@1927
   422
    data->blendMode = -1;
slouken@1927
   423
    data->scaleMode = -1;
slouken@1927
   424
    data->glDisable(GL_DEPTH_TEST);
slouken@1927
   425
    data->glDisable(GL_CULL_FACE);
slouken@3262
   426
    /* This ended up causing video discrepancies between OpenGL and Direct3D */
slouken@3262
   427
    /*data->glEnable(GL_LINE_SMOOTH);*/
slouken@1926
   428
    if (data->GL_ARB_texture_rectangle_supported) {
slouken@1927
   429
        data->glEnable(GL_TEXTURE_RECTANGLE_ARB);
slouken@1926
   430
    } else {
slouken@1927
   431
        data->glEnable(GL_TEXTURE_2D);
slouken@1926
   432
    }
slouken@2833
   433
    data->updateSize = SDL_TRUE;
slouken@1918
   434
slouken@1918
   435
    return renderer;
slouken@1918
   436
}
slouken@1918
   437
slouken@1923
   438
static int
slouken@1923
   439
GL_ActivateRenderer(SDL_Renderer * renderer)
slouken@1923
   440
{
slouken@1923
   441
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1923
   442
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1923
   443
slouken@1970
   444
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
slouken@1970
   445
        return -1;
slouken@1970
   446
    }
slouken@2833
   447
    if (data->updateSize) {
slouken@2836
   448
        data->glMatrixMode(GL_PROJECTION);
slouken@2836
   449
        data->glLoadIdentity();
slouken@2836
   450
        data->glMatrixMode(GL_MODELVIEW);
slouken@2836
   451
        data->glLoadIdentity();
slouken@2836
   452
        data->glViewport(0, 0, window->w, window->h);
slouken@2836
   453
        data->glOrtho(0.0, (GLdouble) window->w, (GLdouble) window->h, 0.0,
slouken@2836
   454
                      0.0, 1.0);
slouken@2833
   455
        data->updateSize = SDL_FALSE;
slouken@2833
   456
    }
slouken@1970
   457
    return 0;
slouken@1970
   458
}
slouken@1970
   459
slouken@1970
   460
static int
slouken@1970
   461
GL_DisplayModeChanged(SDL_Renderer * renderer)
slouken@1970
   462
{
slouken@1970
   463
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1970
   464
slouken@2836
   465
    /* Rebind the context to the window area and update matrices */
slouken@2836
   466
    data->updateSize = SDL_TRUE;
slouken@2836
   467
    return GL_ActivateRenderer(renderer);
slouken@1923
   468
}
slouken@1923
   469
slouken@1922
   470
static __inline__ int
slouken@1922
   471
power_of_2(int input)
slouken@1922
   472
{
slouken@1922
   473
    int value = 1;
slouken@1922
   474
slouken@1922
   475
    while (value < input) {
slouken@1922
   476
        value <<= 1;
slouken@1922
   477
    }
slouken@1922
   478
    return value;
slouken@1922
   479
}
slouken@1922
   480
icculus@2835
   481
slouken@2858
   482
//#define DEBUG_PROGRAM_COMPILE 1
icculus@2835
   483
icculus@2835
   484
static GLuint
slouken@2884
   485
compile_shader(GL_RenderData * data, GLenum shader_type, const char *_code)
icculus@2835
   486
{
icculus@2847
   487
    const int have_texture_rects = data->GL_ARB_texture_rectangle_supported;
icculus@2847
   488
    const char *replacement = have_texture_rects ? "RECT" : "2D";
slouken@2918
   489
    const size_t replacementlen = SDL_strlen(replacement);
icculus@2847
   490
    const char *token = "%TEXTURETARGET%";
slouken@2918
   491
    const size_t tokenlen = SDL_strlen(token);
icculus@2847
   492
    char *code = NULL;
icculus@2847
   493
    char *ptr = NULL;
icculus@2847
   494
    GLuint program = 0;
icculus@2847
   495
icculus@2847
   496
    /*
icculus@2847
   497
     * The TEX instruction needs a different target depending on what we use.
icculus@2847
   498
     *  To handle this, we use "%TEXTURETARGET%" and replace the string before
icculus@2847
   499
     *  compiling the shader.
icculus@2847
   500
     */
icculus@2847
   501
    code = SDL_strdup(_code);
icculus@2847
   502
    if (code == NULL)
icculus@2847
   503
        return 0;
icculus@2847
   504
slouken@2884
   505
    for (ptr = SDL_strstr(code, token); ptr; ptr = SDL_strstr(ptr + 1, token)) {
slouken@2918
   506
        SDL_memcpy(ptr, replacement, replacementlen);
slouken@2918
   507
        SDL_memmove(ptr + replacementlen, ptr + tokenlen,
slouken@2918
   508
                    SDL_strlen(ptr + tokenlen) + 1);
icculus@2847
   509
    }
icculus@2847
   510
icculus@2835
   511
#if DEBUG_PROGRAM_COMPILE
icculus@2847
   512
    printf("compiling shader:\n%s\n\n", code);
icculus@2835
   513
#endif
icculus@2835
   514
slouken@2884
   515
    data->glGetError();         /* flush any existing error state. */
icculus@2835
   516
    data->glGenProgramsARB(1, &program);
icculus@2835
   517
    data->glBindProgramARB(shader_type, program);
icculus@2835
   518
    data->glProgramStringARB(shader_type, GL_PROGRAM_FORMAT_ASCII_ARB,
slouken@3253
   519
                             (GLsizei)SDL_strlen(code), code);
icculus@2847
   520
icculus@2847
   521
    SDL_free(code);
icculus@2835
   522
slouken@2884
   523
    if (data->glGetError() == GL_INVALID_OPERATION) {
icculus@2835
   524
#if DEBUG_PROGRAM_COMPILE
icculus@2835
   525
        GLint pos = 0;
icculus@2835
   526
        const GLubyte *errstr;
icculus@2835
   527
        data->glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
icculus@2835
   528
        errstr = data->glGetString(GL_PROGRAM_ERROR_STRING_ARB);
icculus@2835
   529
        printf("program compile error at position %d: %s\n\n",
slouken@2884
   530
               (int) pos, (const char *) errstr);
icculus@2835
   531
#endif
icculus@2835
   532
        data->glBindProgramARB(shader_type, 0);
icculus@2835
   533
        data->glDeleteProgramsARB(1, &program);
icculus@2835
   534
        return 0;
icculus@2848
   535
    }
icculus@2835
   536
icculus@2835
   537
    return program;
icculus@2835
   538
}
icculus@2835
   539
icculus@2848
   540
icculus@2848
   541
/*
icculus@2848
   542
 * Fragment program that renders from UYVY textures.
icculus@2848
   543
 * The UYVY to RGB equasion is:
icculus@2848
   544
 *   R = 1.164(Y-16) + 1.596(Cr-128)
icculus@2848
   545
 *   G = 1.164(Y-16) - 0.813(Cr-128) - 0.391(Cb-128)
icculus@2848
   546
 *   B = 1.164(Y-16) + 2.018(Cb-128)
icculus@2848
   547
 * Byte layout is Cb, Y1, Cr, Y2, stored in the R, G, B, A channels.
icculus@2848
   548
 * 4 bytes == 2 pixels: Y1/Cb/Cr, Y2/Cb/Cr
icculus@2848
   549
 *
icculus@2848
   550
 * !!! FIXME: this ignores blendmodes, etc.
icculus@2848
   551
 * !!! FIXME: this could be more efficient...use a dot product for green, etc.
icculus@2848
   552
 */
slouken@2884
   553
static const char *fragment_program_UYVY_source_code = "!!ARBfp1.0\n"
icculus@2848
   554
    /* outputs... */
icculus@2835
   555
    "OUTPUT outcolor = result.color;\n"
icculus@2848
   556
    /* scratch registers... */
slouken@2884
   557
    "TEMP uyvy;\n" "TEMP luminance;\n" "TEMP work;\n"
icculus@2848
   558
    /* Halve the coordinates to grab the correct 32 bits for the fragment. */
icculus@2835
   559
    "MUL work, fragment.texcoord, { 0.5, 1.0, 1.0, 1.0 };\n"
icculus@2848
   560
    /* Sample the YUV texture. Cb, Y1, Cr, Y2, are stored in x, y, z, w. */
icculus@2847
   561
    "TEX uyvy, work, texture[0], %TEXTURETARGET%;\n"
icculus@2848
   562
    /* Do subtractions (128/255, 16/255, 128/255, 16/255) */
slouken@2846
   563
    "SUB uyvy, uyvy, { 0.501960784313726, 0.06274509803922, 0.501960784313726, 0.06274509803922 };\n"
icculus@2848
   564
    /* Choose the luminance component by texcoord. */
icculus@2848
   565
    /* !!! FIXME: laziness wins out for now... just average Y1 and Y2. */
icculus@2835
   566
    "ADD luminance, uyvy.yyyy, uyvy.wwww;\n"
icculus@2835
   567
    "MUL luminance, luminance, { 0.5, 0.5, 0.5, 0.5 };\n"
icculus@2848
   568
    /* Multiply luminance by its magic value. */
icculus@2835
   569
    "MUL luminance, luminance, { 1.164, 1.164, 1.164, 1.164 };\n"
icculus@2848
   570
    /* uyvy.xyzw becomes Cr/Cr/Cb/Cb, with multiplications. */
icculus@2835
   571
    "MUL uyvy, uyvy.zzxx, { 1.596, -0.813, 2.018, -0.391 };\n"
icculus@2848
   572
    /* Add luminance to Cr and Cb, store to RGB channels. */
icculus@2835
   573
    "ADD work.rgb, luminance, uyvy;\n"
icculus@2848
   574
    /* Do final addition for Green channel.  (!!! FIXME: this should be a DPH?) */
icculus@2835
   575
    "ADD work.g, work.g, uyvy.w;\n"
icculus@2848
   576
    /* Make sure alpha channel is fully opaque.  (!!! FIXME: blend modes!) */
icculus@2835
   577
    "MOV work.a, { 1.0 };\n"
icculus@2848
   578
    /* Store out the final fragment color... */
icculus@2835
   579
    "MOV outcolor, work;\n"
icculus@2848
   580
    /* ...and we're done! */
icculus@2835
   581
    "END\n";
icculus@2835
   582
icculus@2835
   583
slouken@1918
   584
static int
slouken@1918
   585
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   586
{
slouken@1918
   587
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   588
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1918
   589
    GL_TextureData *data;
slouken@1920
   590
    GLint internalFormat;
slouken@1920
   591
    GLenum format, type;
slouken@1922
   592
    int texture_w, texture_h;
icculus@2835
   593
    GLuint shader = 0;
slouken@1924
   594
    GLenum result;
slouken@1918
   595
slouken@1920
   596
    switch (texture->format) {
slouken@1965
   597
    case SDL_PIXELFORMAT_INDEX1LSB:
slouken@1965
   598
    case SDL_PIXELFORMAT_INDEX1MSB:
slouken@1920
   599
        internalFormat = GL_RGB;
slouken@1920
   600
        format = GL_COLOR_INDEX;
slouken@1920
   601
        type = GL_BITMAP;
slouken@1920
   602
        break;
slouken@1965
   603
    case SDL_PIXELFORMAT_INDEX8:
slouken@1974
   604
        if (!renderdata->GL_EXT_paletted_texture_supported) {
slouken@1974
   605
            SDL_SetError("Unsupported texture format");
slouken@1974
   606
            return -1;
slouken@1974
   607
        }
slouken@1974
   608
        internalFormat = GL_COLOR_INDEX8_EXT;
slouken@1920
   609
        format = GL_COLOR_INDEX;
slouken@1920
   610
        type = GL_UNSIGNED_BYTE;
slouken@1920
   611
        break;
slouken@1965
   612
    case SDL_PIXELFORMAT_RGB332:
slouken@1920
   613
        internalFormat = GL_R3_G3_B2;
slouken@1920
   614
        format = GL_RGB;
slouken@1920
   615
        type = GL_UNSIGNED_BYTE_3_3_2;
slouken@1920
   616
        break;
slouken@1965
   617
    case SDL_PIXELFORMAT_RGB444:
slouken@1920
   618
        internalFormat = GL_RGB4;
slouken@1920
   619
        format = GL_RGB;
slouken@1920
   620
        type = GL_UNSIGNED_SHORT_4_4_4_4;
slouken@1920
   621
        break;
slouken@1965
   622
    case SDL_PIXELFORMAT_RGB555:
slouken@1920
   623
        internalFormat = GL_RGB5;
slouken@1920
   624
        format = GL_RGB;
slouken@1920
   625
        type = GL_UNSIGNED_SHORT_5_5_5_1;
slouken@1920
   626
        break;
slouken@1965
   627
    case SDL_PIXELFORMAT_ARGB4444:
slouken@1920
   628
        internalFormat = GL_RGBA4;
slouken@1920
   629
        format = GL_BGRA;
slouken@1920
   630
        type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
slouken@1920
   631
        break;
slouken@1965
   632
    case SDL_PIXELFORMAT_ARGB1555:
slouken@1920
   633
        internalFormat = GL_RGB5_A1;
slouken@1920
   634
        format = GL_BGRA;
slouken@1920
   635
        type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
slouken@1920
   636
        break;
slouken@1965
   637
    case SDL_PIXELFORMAT_RGB565:
slouken@1920
   638
        internalFormat = GL_RGB8;
slouken@1920
   639
        format = GL_RGB;
slouken@1920
   640
        type = GL_UNSIGNED_SHORT_5_6_5;
slouken@1920
   641
        break;
slouken@1965
   642
    case SDL_PIXELFORMAT_RGB24:
slouken@1920
   643
        internalFormat = GL_RGB8;
slouken@1920
   644
        format = GL_RGB;
slouken@1920
   645
        type = GL_UNSIGNED_BYTE;
slouken@1920
   646
        break;
slouken@1965
   647
    case SDL_PIXELFORMAT_RGB888:
slouken@1920
   648
        internalFormat = GL_RGB8;
slouken@1924
   649
        format = GL_BGRA;
slouken@1924
   650
        type = GL_UNSIGNED_BYTE;
slouken@1920
   651
        break;
slouken@1965
   652
    case SDL_PIXELFORMAT_BGR24:
slouken@1920
   653
        internalFormat = GL_RGB8;
slouken@1920
   654
        format = GL_BGR;
slouken@1920
   655
        type = GL_UNSIGNED_BYTE;
slouken@1920
   656
        break;
slouken@1965
   657
    case SDL_PIXELFORMAT_BGR888:
slouken@1920
   658
        internalFormat = GL_RGB8;
slouken@1924
   659
        format = GL_RGBA;
slouken@1924
   660
        type = GL_UNSIGNED_BYTE;
slouken@1920
   661
        break;
slouken@1965
   662
    case SDL_PIXELFORMAT_ARGB8888:
slouken@2230
   663
#ifdef __MACOSX__
slouken@2230
   664
        internalFormat = GL_RGBA;
slouken@2230
   665
        format = GL_BGRA;
slouken@2230
   666
        type = GL_UNSIGNED_INT_8_8_8_8_REV;
slouken@2230
   667
#else
slouken@1920
   668
        internalFormat = GL_RGBA8;
slouken@1920
   669
        format = GL_BGRA;
slouken@1924
   670
        type = GL_UNSIGNED_BYTE;
slouken@2230
   671
#endif
slouken@1920
   672
        break;
slouken@1965
   673
    case SDL_PIXELFORMAT_ABGR8888:
slouken@1920
   674
        internalFormat = GL_RGBA8;
slouken@1920
   675
        format = GL_RGBA;
slouken@1924
   676
        type = GL_UNSIGNED_BYTE;
slouken@1920
   677
        break;
slouken@1965
   678
    case SDL_PIXELFORMAT_ARGB2101010:
slouken@1920
   679
        internalFormat = GL_RGB10_A2;
slouken@1920
   680
        format = GL_BGRA;
slouken@1920
   681
        type = GL_UNSIGNED_INT_2_10_10_10_REV;
slouken@1920
   682
        break;
slouken@2845
   683
    case SDL_PIXELFORMAT_UYVY:
slouken@2845
   684
        if (renderdata->GL_APPLE_ycbcr_422_supported) {
slouken@2845
   685
            internalFormat = GL_RGB;
slouken@2845
   686
            format = GL_YCBCR_422_APPLE;
slouken@2845
   687
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@2845
   688
            type = GL_UNSIGNED_SHORT_8_8_APPLE;
slouken@2844
   689
#else
slouken@2845
   690
            type = GL_UNSIGNED_SHORT_8_8_REV_APPLE;
slouken@2845
   691
#endif
slouken@2845
   692
        } else if (renderdata->GL_MESA_ycbcr_texture_supported) {
slouken@2845
   693
            internalFormat = GL_RGB;
slouken@2845
   694
            format = GL_YCBCR_MESA;
slouken@2845
   695
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@2845
   696
            type = GL_UNSIGNED_SHORT_8_8_MESA;
slouken@2845
   697
#else
slouken@2845
   698
            type = GL_UNSIGNED_SHORT_8_8_REV_MESA;
slouken@2845
   699
#endif
slouken@2845
   700
        } else if (renderdata->GL_ARB_fragment_program_supported) {
icculus@2835
   701
            if (renderdata->fragment_program_UYVY == 0) {
icculus@2835
   702
                renderdata->fragment_program_UYVY =
icculus@2835
   703
                    compile_shader(renderdata, GL_FRAGMENT_PROGRAM_ARB,
icculus@2835
   704
                                   fragment_program_UYVY_source_code);
icculus@2835
   705
                if (renderdata->fragment_program_UYVY == 0) {
icculus@2835
   706
                    SDL_SetError("Fragment program compile error");
icculus@2835
   707
                    return -1;
icculus@2835
   708
                }
icculus@2835
   709
            }
icculus@2835
   710
            shader = renderdata->fragment_program_UYVY;
icculus@2835
   711
            internalFormat = GL_RGBA;
icculus@2835
   712
            format = GL_RGBA;
icculus@2835
   713
            type = GL_UNSIGNED_BYTE;
icculus@2835
   714
        } else {
icculus@2835
   715
            SDL_SetError("Unsupported texture format");
icculus@2835
   716
            return -1;
icculus@2835
   717
        }
icculus@2835
   718
        break;
slouken@2845
   719
    case SDL_PIXELFORMAT_YUY2:
slouken@2845
   720
        if (renderdata->GL_APPLE_ycbcr_422_supported) {
slouken@2845
   721
            internalFormat = GL_RGB;
slouken@2845
   722
            format = GL_YCBCR_422_APPLE;
slouken@2845
   723
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@2845
   724
            type = GL_UNSIGNED_SHORT_8_8_REV_APPLE;
slouken@2845
   725
#else
slouken@2845
   726
            type = GL_UNSIGNED_SHORT_8_8_APPLE;
slouken@2844
   727
#endif
slouken@2845
   728
        } else if (renderdata->GL_MESA_ycbcr_texture_supported) {
slouken@2845
   729
            internalFormat = GL_RGB;
slouken@2845
   730
            format = GL_YCBCR_MESA;
slouken@2845
   731
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@2845
   732
            type = GL_UNSIGNED_SHORT_8_8_REV_MESA;
slouken@2845
   733
#else
slouken@2845
   734
            type = GL_UNSIGNED_SHORT_8_8_MESA;
slouken@2845
   735
#endif
slouken@2845
   736
        } else {
slouken@2845
   737
            SDL_SetError("Unsupported texture format");
slouken@2845
   738
            return -1;
slouken@2845
   739
        }
slouken@2845
   740
        break;
slouken@1920
   741
    default:
slouken@1920
   742
        SDL_SetError("Unsupported texture format");
slouken@1920
   743
        return -1;
slouken@1920
   744
    }
slouken@1920
   745
slouken@1920
   746
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   747
    if (!data) {
slouken@1918
   748
        SDL_OutOfMemory();
slouken@1918
   749
        return -1;
slouken@1918
   750
    }
slouken@1918
   751
icculus@2835
   752
    data->shader = shader;
icculus@2835
   753
slouken@1974
   754
    if (texture->format == SDL_PIXELFORMAT_INDEX8) {
slouken@1974
   755
        data->palette = (Uint8 *) SDL_malloc(3 * 256 * sizeof(Uint8));
slouken@1974
   756
        if (!data->palette) {
slouken@1974
   757
            SDL_OutOfMemory();
slouken@1974
   758
            SDL_free(data);
slouken@1974
   759
            return -1;
slouken@1974
   760
        }
slouken@1974
   761
        SDL_memset(data->palette, 0xFF, 3 * 256 * sizeof(Uint8));
slouken@1974
   762
    }
slouken@1974
   763
slouken@2222
   764
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
icculus@2835
   765
        data->pitch = texture->w * bytes_per_pixel(texture->format);
slouken@2222
   766
        data->pixels = SDL_malloc(texture->h * data->pitch);
slouken@2222
   767
        if (!data->pixels) {
slouken@2222
   768
            SDL_OutOfMemory();
slouken@2222
   769
            SDL_free(data);
slouken@2222
   770
            return -1;
slouken@2222
   771
        }
slouken@2222
   772
    }
slouken@2222
   773
slouken@1918
   774
    texture->driverdata = data;
slouken@1918
   775
slouken@1927
   776
    renderdata->glGetError();
slouken@1927
   777
    renderdata->glGenTextures(1, &data->texture);
slouken@1926
   778
    if (renderdata->GL_ARB_texture_rectangle_supported) {
slouken@1926
   779
        data->type = GL_TEXTURE_RECTANGLE_ARB;
slouken@1926
   780
        texture_w = texture->w;
slouken@1926
   781
        texture_h = texture->h;
icculus@2835
   782
        data->texw = (GLfloat) texture_w;
icculus@2835
   783
        data->texh = (GLfloat) texture_h;
slouken@1926
   784
    } else {
slouken@1926
   785
        data->type = GL_TEXTURE_2D;
slouken@1926
   786
        texture_w = power_of_2(texture->w);
slouken@1926
   787
        texture_h = power_of_2(texture->h);
icculus@2835
   788
        data->texw = (GLfloat) (texture->w) / texture_w;
slouken@1926
   789
        data->texh = (GLfloat) texture->h / texture_h;
slouken@1926
   790
    }
icculus@2835
   791
slouken@2839
   792
    /* YUV formats use RGBA but are really two bytes per pixel */
slouken@2839
   793
    if (internalFormat == GL_RGBA && bytes_per_pixel(texture->format) < 4) {
slouken@2884
   794
        data->HACK_RYAN_FIXME = 2;
slouken@2843
   795
    } else {
slouken@2884
   796
        data->HACK_RYAN_FIXME = 1;
slouken@2839
   797
    }
slouken@2843
   798
    texture_w /= data->HACK_RYAN_FIXME;
slouken@2839
   799
slouken@1920
   800
    data->format = format;
slouken@1920
   801
    data->formattype = type;
slouken@2884
   802
    renderdata->glEnable(data->type);
slouken@1927
   803
    renderdata->glBindTexture(data->type, data->texture);
slouken@2230
   804
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@2230
   805
                                GL_NEAREST);
slouken@2230
   806
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@2230
   807
                                GL_NEAREST);
slouken@2230
   808
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@2230
   809
                                GL_CLAMP_TO_EDGE);
slouken@2230
   810
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@2230
   811
                                GL_CLAMP_TO_EDGE);
slouken@2840
   812
#ifdef __MACOSX__
slouken@2230
   813
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
slouken@2230
   814
#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
slouken@2230
   815
#endif
slouken@2230
   816
#ifndef STORAGE_CACHED_APPLE
slouken@2230
   817
#define STORAGE_CACHED_APPLE                0x85BE
slouken@2230
   818
#endif
slouken@2230
   819
#ifndef STORAGE_SHARED_APPLE
slouken@2230
   820
#define STORAGE_SHARED_APPLE                0x85BF
slouken@2230
   821
#endif
slouken@2230
   822
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@2230
   823
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   824
                                    GL_STORAGE_SHARED_APPLE);
slouken@2230
   825
    } else {
slouken@2230
   826
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   827
                                    GL_STORAGE_CACHED_APPLE);
slouken@2230
   828
    }
slouken@2809
   829
/* This causes a crash in testoverlay for some reason.  Apple bug? */
slouken@2809
   830
#if 0
bob@2295
   831
    if (texture->access == SDL_TEXTUREACCESS_STREAMING
bob@2295
   832
        && texture->format == SDL_PIXELFORMAT_ARGB8888) {
slouken@2237
   833
        /*
bob@2295
   834
           if (renderdata->glTextureRangeAPPLE) {
bob@2295
   835
           renderdata->glTextureRangeAPPLE(data->type,
bob@2295
   836
           texture->h * data->pitch,
bob@2295
   837
           data->pixels);
bob@2295
   838
           }
bob@2295
   839
         */
slouken@2230
   840
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
slouken@2230
   841
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   842
                                 texture_h, 0, format, type, data->pixels);
slouken@2230
   843
    } else
slouken@2230
   844
#endif
slouken@2809
   845
#endif
slouken@2230
   846
    {
slouken@2230
   847
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   848
                                 texture_h, 0, format, type, NULL);
slouken@2230
   849
    }
slouken@3041
   850
    renderdata->glDisable(data->type);
slouken@1927
   851
    result = renderdata->glGetError();
slouken@1924
   852
    if (result != GL_NO_ERROR) {
slouken@1924
   853
        GL_SetError("glTexImage2D()", result);
slouken@1924
   854
        return -1;
slouken@1924
   855
    }
slouken@1918
   856
    return 0;
slouken@1918
   857
}
slouken@1918
   858
slouken@1918
   859
static int
slouken@2222
   860
GL_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@2222
   861
                      void **pixels, int *pitch)
slouken@2222
   862
{
slouken@2222
   863
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@2222
   864
slouken@2222
   865
    *pixels = data->pixels;
slouken@2222
   866
    *pitch = data->pitch;
slouken@2222
   867
    return 0;
slouken@2222
   868
}
slouken@2222
   869
slouken@2222
   870
static int
slouken@1918
   871
GL_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   872
                     const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   873
{
slouken@1918
   874
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   875
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1974
   876
    Uint8 *palette;
slouken@1918
   877
slouken@1974
   878
    if (!data->palette) {
slouken@1974
   879
        SDL_SetError("Texture doesn't have a palette");
slouken@1974
   880
        return -1;
slouken@1974
   881
    }
slouken@1974
   882
    palette = data->palette + firstcolor * 3;
slouken@1974
   883
    while (ncolors--) {
slouken@1974
   884
        *palette++ = colors->r;
slouken@1974
   885
        *palette++ = colors->g;
slouken@1974
   886
        *palette++ = colors->b;
slouken@1974
   887
        ++colors;
slouken@1974
   888
    }
slouken@2884
   889
    renderdata->glEnable(data->type);
slouken@1974
   890
    renderdata->glBindTexture(data->type, data->texture);
slouken@1974
   891
    renderdata->glColorTableEXT(data->type, GL_RGB8, 256, GL_RGB,
slouken@1974
   892
                                GL_UNSIGNED_BYTE, data->palette);
slouken@1918
   893
    return 0;
slouken@1918
   894
}
slouken@1918
   895
slouken@1918
   896
static int
slouken@1918
   897
GL_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   898
                     SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   899
{
slouken@1974
   900
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   901
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1974
   902
    Uint8 *palette;
slouken@1918
   903
slouken@1974
   904
    if (!data->palette) {
slouken@1974
   905
        SDL_SetError("Texture doesn't have a palette");
slouken@1974
   906
        return -1;
slouken@1974
   907
    }
slouken@1974
   908
    palette = data->palette + firstcolor * 3;
slouken@1974
   909
    while (ncolors--) {
slouken@1974
   910
        colors->r = *palette++;
slouken@1974
   911
        colors->g = *palette++;
slouken@1974
   912
        colors->b = *palette++;
slouken@1974
   913
        colors->unused = SDL_ALPHA_OPAQUE;
slouken@1974
   914
        ++colors;
slouken@1974
   915
    }
slouken@1918
   916
    return 0;
slouken@1918
   917
}
slouken@1918
   918
slouken@1924
   919
static void
slouken@1927
   920
SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture,
slouken@1927
   921
                   int pitch)
slouken@1924
   922
{
slouken@1965
   923
    if (texture->format == SDL_PIXELFORMAT_INDEX1LSB) {
slouken@1927
   924
        renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 1);
slouken@1965
   925
    } else if (texture->format == SDL_PIXELFORMAT_INDEX1MSB) {
slouken@1927
   926
        renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 0);
slouken@1924
   927
    }
slouken@1927
   928
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@2808
   929
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@2884
   930
                              (pitch / bytes_per_pixel(texture->format)) /
slouken@3013
   931
                              ((GL_TextureData *) texture->driverdata)->
slouken@3013
   932
                              HACK_RYAN_FIXME);
slouken@1924
   933
}
slouken@1924
   934
slouken@1918
   935
static int
slouken@1985
   936
GL_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   937
{
slouken@1986
   938
    return 0;
slouken@1985
   939
}
slouken@1985
   940
slouken@1985
   941
static int
slouken@1985
   942
GL_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   943
{
slouken@1986
   944
    return 0;
slouken@1985
   945
}
slouken@1985
   946
slouken@1985
   947
static int
slouken@1985
   948
GL_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   949
{
slouken@1985
   950
    switch (texture->blendMode) {
slouken@2884
   951
    case SDL_BLENDMODE_NONE:
slouken@2884
   952
    case SDL_BLENDMODE_MASK:
slouken@2884
   953
    case SDL_BLENDMODE_BLEND:
slouken@2884
   954
    case SDL_BLENDMODE_ADD:
slouken@2884
   955
    case SDL_BLENDMODE_MOD:
slouken@1985
   956
        return 0;
slouken@1985
   957
    default:
slouken@1985
   958
        SDL_Unsupported();
slouken@2884
   959
        texture->blendMode = SDL_BLENDMODE_NONE;
slouken@1985
   960
        return -1;
slouken@1985
   961
    }
slouken@1985
   962
}
slouken@1985
   963
slouken@1985
   964
static int
slouken@1985
   965
GL_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   966
{
slouken@1985
   967
    switch (texture->scaleMode) {
slouken@1985
   968
    case SDL_TEXTURESCALEMODE_NONE:
slouken@1985
   969
    case SDL_TEXTURESCALEMODE_FAST:
slouken@1985
   970
    case SDL_TEXTURESCALEMODE_SLOW:
slouken@1985
   971
        return 0;
slouken@1985
   972
    case SDL_TEXTURESCALEMODE_BEST:
slouken@1985
   973
        SDL_Unsupported();
slouken@1985
   974
        texture->scaleMode = SDL_TEXTURESCALEMODE_SLOW;
slouken@1985
   975
        return -1;
slouken@1985
   976
    default:
slouken@1985
   977
        SDL_Unsupported();
slouken@1985
   978
        texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
slouken@1985
   979
        return -1;
slouken@1985
   980
    }
slouken@1985
   981
}
slouken@1985
   982
slouken@1985
   983
static int
slouken@1918
   984
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   985
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
   986
{
slouken@1927
   987
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   988
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1924
   989
    GLenum result;
slouken@1918
   990
slouken@1927
   991
    renderdata->glGetError();
slouken@1927
   992
    SetupTextureUpdate(renderdata, texture, pitch);
slouken@2884
   993
    renderdata->glEnable(data->type);
slouken@1927
   994
    renderdata->glBindTexture(data->type, data->texture);
slouken@1927
   995
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
slouken@1927
   996
                                rect->h, data->format, data->formattype,
slouken@1927
   997
                                pixels);
slouken@3041
   998
    renderdata->glDisable(data->type);
slouken@1927
   999
    result = renderdata->glGetError();
slouken@1924
  1000
    if (result != GL_NO_ERROR) {
slouken@1924
  1001
        GL_SetError("glTexSubImage2D()", result);
slouken@1924
  1002
        return -1;
slouken@1924
  1003
    }
slouken@1918
  1004
    return 0;
slouken@1918
  1005
}
slouken@1918
  1006
slouken@1918
  1007
static int
slouken@1918
  1008
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
  1009
               const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1918
  1010
               int *pitch)
slouken@1918
  1011
{
slouken@1918
  1012
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
  1013
slouken@1920
  1014
    if (markDirty) {
slouken@1920
  1015
        SDL_AddDirtyRect(&data->dirty, rect);
slouken@1920
  1016
    }
slouken@1918
  1017
slouken@1920
  1018
    *pixels =
slouken@1920
  1019
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
icculus@2835
  1020
                  rect->x * bytes_per_pixel(texture->format));
slouken@1920
  1021
    *pitch = data->pitch;
slouken@1918
  1022
    return 0;
slouken@1918
  1023
}
slouken@1918
  1024
slouken@1918
  1025
static void
slouken@1918
  1026
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
  1027
{
slouken@1918
  1028
}
slouken@1918
  1029
slouken@1918
  1030
static void
slouken@1918
  1031
GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
slouken@1918
  1032
                const SDL_Rect * rects)
slouken@1918
  1033
{
slouken@1918
  1034
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
  1035
    int i;
slouken@1918
  1036
slouken@1918
  1037
    for (i = 0; i < numrects; ++i) {
slouken@1920
  1038
        SDL_AddDirtyRect(&data->dirty, &rects[i]);
slouken@1918
  1039
    }
slouken@1918
  1040
}
slouken@1918
  1041
slouken@2936
  1042
static void
slouken@2936
  1043
GL_SetBlendMode(GL_RenderData * data, int blendMode)
slouken@2936
  1044
{
slouken@2936
  1045
    if (blendMode != data->blendMode) {
slouken@2936
  1046
        switch (blendMode) {
slouken@2936
  1047
        case SDL_BLENDMODE_NONE:
slouken@2936
  1048
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
slouken@2936
  1049
            data->glDisable(GL_BLEND);
slouken@2936
  1050
            break;
slouken@2936
  1051
        case SDL_BLENDMODE_MASK:
slouken@2936
  1052
        case SDL_BLENDMODE_BLEND:
slouken@2936
  1053
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
  1054
            data->glEnable(GL_BLEND);
slouken@2936
  1055
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@2936
  1056
            break;
slouken@2936
  1057
        case SDL_BLENDMODE_ADD:
slouken@2936
  1058
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
  1059
            data->glEnable(GL_BLEND);
slouken@2936
  1060
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@2936
  1061
            break;
slouken@2936
  1062
        case SDL_BLENDMODE_MOD:
slouken@2936
  1063
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
  1064
            data->glEnable(GL_BLEND);
slouken@2936
  1065
            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@2936
  1066
            break;
slouken@2936
  1067
        }
slouken@2936
  1068
        data->blendMode = blendMode;
slouken@2936
  1069
    }
slouken@2936
  1070
}
slouken@2936
  1071
slouken@1918
  1072
static int
slouken@2901
  1073
GL_RenderPoint(SDL_Renderer * renderer, int x, int y)
slouken@2884
  1074
{
slouken@2884
  1075
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@2884
  1076
slouken@2936
  1077
    GL_SetBlendMode(data, renderer->blendMode);
slouken@2884
  1078
slouken@2884
  1079
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2884
  1080
                    (GLfloat) renderer->g * inv255f,
slouken@2884
  1081
                    (GLfloat) renderer->b * inv255f,
slouken@2884
  1082
                    (GLfloat) renderer->a * inv255f);
slouken@2884
  1083
slouken@2901
  1084
    data->glBegin(GL_POINTS);
slouken@2901
  1085
    data->glVertex2i(x, y);
slouken@2901
  1086
    data->glEnd();
slouken@2901
  1087
slouken@2901
  1088
    return 0;
slouken@2901
  1089
}
slouken@2901
  1090
slouken@2901
  1091
static int
slouken@2901
  1092
GL_RenderLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
slouken@2901
  1093
{
slouken@2901
  1094
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@2901
  1095
slouken@2936
  1096
    GL_SetBlendMode(data, renderer->blendMode);
slouken@2901
  1097
slouken@2901
  1098
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2901
  1099
                    (GLfloat) renderer->g * inv255f,
slouken@2901
  1100
                    (GLfloat) renderer->b * inv255f,
slouken@2901
  1101
                    (GLfloat) renderer->a * inv255f);
slouken@2901
  1102
slouken@2901
  1103
    data->glBegin(GL_LINES);
slouken@2901
  1104
    data->glVertex2i(x1, y1);
slouken@2901
  1105
    data->glVertex2i(x2, y2);
slouken@2901
  1106
    data->glEnd();
slouken@2901
  1107
slouken@1918
  1108
    return 0;
slouken@1918
  1109
}
slouken@1918
  1110
slouken@1918
  1111
static int
slouken@2925
  1112
GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@2925
  1113
{
slouken@2925
  1114
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@2925
  1115
slouken@2936
  1116
    GL_SetBlendMode(data, renderer->blendMode);
slouken@2936
  1117
slouken@2925
  1118
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2925
  1119
                    (GLfloat) renderer->g * inv255f,
slouken@2925
  1120
                    (GLfloat) renderer->b * inv255f,
slouken@2925
  1121
                    (GLfloat) renderer->a * inv255f);
slouken@2936
  1122
slouken@2925
  1123
    data->glRecti(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
slouken@2925
  1124
slouken@2925
  1125
    return 0;
slouken@2925
  1126
}
slouken@2925
  1127
slouken@2925
  1128
static int
slouken@1918
  1129
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
  1130
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1918
  1131
{
slouken@1918
  1132
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1133
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@1918
  1134
    int minx, miny, maxx, maxy;
slouken@1918
  1135
    GLfloat minu, maxu, minv, maxv;
slouken@1918
  1136
slouken@2275
  1137
    if (texturedata->dirty.list) {
slouken@1920
  1138
        SDL_DirtyRect *dirty;
slouken@1920
  1139
        void *pixels;
icculus@2835
  1140
        int bpp = bytes_per_pixel(texture->format);
slouken@1920
  1141
        int pitch = texturedata->pitch;
slouken@1920
  1142
slouken@1927
  1143
        SetupTextureUpdate(data, texture, pitch);
slouken@2884
  1144
        data->glEnable(texturedata->type);
slouken@1927
  1145
        data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1920
  1146
        for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
slouken@1920
  1147
            SDL_Rect *rect = &dirty->rect;
slouken@1920
  1148
            pixels =
slouken@1920
  1149
                (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
slouken@1920
  1150
                          rect->x * bpp);
slouken@1927
  1151
            data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
slouken@2884
  1152
                                  rect->w / texturedata->HACK_RYAN_FIXME,
slouken@2884
  1153
                                  rect->h, texturedata->format,
slouken@1927
  1154
                                  texturedata->formattype, pixels);
slouken@1920
  1155
        }
slouken@1920
  1156
        SDL_ClearDirtyRects(&texturedata->dirty);
slouken@1920
  1157
    }
slouken@1920
  1158
slouken@1918
  1159
    minx = dstrect->x;
slouken@1918
  1160
    miny = dstrect->y;
slouken@1918
  1161
    maxx = dstrect->x + dstrect->w;
slouken@1918
  1162
    maxy = dstrect->y + dstrect->h;
slouken@1918
  1163
slouken@1918
  1164
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
  1165
    minu *= texturedata->texw;
slouken@1918
  1166
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
  1167
    maxu *= texturedata->texw;
slouken@1918
  1168
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
  1169
    minv *= texturedata->texh;
slouken@1918
  1170
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
  1171
    maxv *= texturedata->texh;
slouken@1918
  1172
slouken@2884
  1173
    data->glEnable(texturedata->type);
slouken@1927
  1174
    data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1918
  1175
slouken@1985
  1176
    if (texture->modMode) {
slouken@1985
  1177
        data->glColor4f((GLfloat) texture->r * inv255f,
slouken@1985
  1178
                        (GLfloat) texture->g * inv255f,
slouken@1985
  1179
                        (GLfloat) texture->b * inv255f,
slouken@1985
  1180
                        (GLfloat) texture->a * inv255f);
slouken@1985
  1181
    } else {
slouken@1985
  1182
        data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
slouken@1985
  1183
    }
slouken@1985
  1184
slouken@2936
  1185
    GL_SetBlendMode(data, texture->blendMode);
slouken@1918
  1186
slouken@1985
  1187
    if (texture->scaleMode != data->scaleMode) {
slouken@1985
  1188
        switch (texture->scaleMode) {
slouken@1965
  1189
        case SDL_TEXTURESCALEMODE_NONE:
slouken@1965
  1190
        case SDL_TEXTURESCALEMODE_FAST:
slouken@1927
  1191
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
slouken@1927
  1192
                                  GL_NEAREST);
slouken@1927
  1193
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
slouken@1927
  1194
                                  GL_NEAREST);
slouken@1927
  1195
            break;
slouken@1965
  1196
        case SDL_TEXTURESCALEMODE_SLOW:
slouken@1965
  1197
        case SDL_TEXTURESCALEMODE_BEST:
slouken@1927
  1198
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
slouken@1927
  1199
                                  GL_LINEAR);
slouken@1927
  1200
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
slouken@1927
  1201
                                  GL_LINEAR);
slouken@1927
  1202
            break;
slouken@1927
  1203
        }
slouken@1985
  1204
        data->scaleMode = texture->scaleMode;
slouken@1918
  1205
    }
slouken@1918
  1206
icculus@2835
  1207
    if (texturedata->shader != 0) {
icculus@2835
  1208
        data->glEnable(GL_FRAGMENT_PROGRAM_ARB);
icculus@2835
  1209
        data->glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, texturedata->shader);
icculus@2835
  1210
    }
icculus@2835
  1211
slouken@1927
  1212
    data->glBegin(GL_TRIANGLE_STRIP);
slouken@1927
  1213
    data->glTexCoord2f(minu, minv);
slouken@1927
  1214
    data->glVertex2i(minx, miny);
slouken@1927
  1215
    data->glTexCoord2f(maxu, minv);
slouken@1927
  1216
    data->glVertex2i(maxx, miny);
slouken@1927
  1217
    data->glTexCoord2f(minu, maxv);
slouken@1927
  1218
    data->glVertex2i(minx, maxy);
slouken@1927
  1219
    data->glTexCoord2f(maxu, maxv);
slouken@1927
  1220
    data->glVertex2i(maxx, maxy);
slouken@1927
  1221
    data->glEnd();
slouken@1918
  1222
icculus@2835
  1223
    if (texturedata->shader != 0) {
icculus@2835
  1224
        data->glDisable(GL_FRAGMENT_PROGRAM_ARB);
icculus@2835
  1225
    }
slouken@2884
  1226
slouken@2884
  1227
    data->glDisable(texturedata->type);
slouken@2884
  1228
slouken@1918
  1229
    return 0;
slouken@1918
  1230
}
slouken@1918
  1231
slouken@1918
  1232
static void
slouken@1918
  1233
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
  1234
{
slouken@1918
  1235
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
  1236
}
slouken@1918
  1237
slouken@1918
  1238
static void
slouken@1918
  1239
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
  1240
{
slouken@1927
  1241
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1242
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
  1243
slouken@1918
  1244
    if (!data) {
slouken@1918
  1245
        return;
slouken@1918
  1246
    }
slouken@1918
  1247
    if (data->texture) {
slouken@1927
  1248
        renderdata->glDeleteTextures(1, &data->texture);
slouken@1918
  1249
    }
slouken@1974
  1250
    if (data->palette) {
slouken@1974
  1251
        SDL_free(data->palette);
slouken@1974
  1252
    }
slouken@1920
  1253
    if (data->pixels) {
slouken@1920
  1254
        SDL_free(data->pixels);
slouken@1920
  1255
    }
slouken@1920
  1256
    SDL_FreeDirtyRects(&data->dirty);
slouken@1918
  1257
    SDL_free(data);
slouken@1918
  1258
    texture->driverdata = NULL;
slouken@1918
  1259
}
slouken@1918
  1260
slouken@1975
  1261
static void
slouken@1918
  1262
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
  1263
{
slouken@1918
  1264
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
  1265
slouken@1918
  1266
    if (data) {
slouken@1920
  1267
        if (data->context) {
icculus@2835
  1268
            if (data->GL_ARB_fragment_program_supported) {
icculus@2835
  1269
                data->glDisable(GL_FRAGMENT_PROGRAM_ARB);
icculus@2835
  1270
                data->glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0);
icculus@2835
  1271
                if (data->fragment_program_UYVY != 0) {
slouken@2884
  1272
                    data->glDeleteProgramsARB(1,
slouken@2884
  1273
                                              &data->fragment_program_UYVY);
icculus@2835
  1274
                }
icculus@2835
  1275
            }
icculus@2835
  1276
bob@2328
  1277
            /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
slouken@1920
  1278
            SDL_GL_DeleteContext(data->context);
slouken@1918
  1279
        }
slouken@1918
  1280
        SDL_free(data);
slouken@1918
  1281
    }
slouken@1918
  1282
    SDL_free(renderer);
slouken@1918
  1283
}
slouken@1918
  1284
slouken@1952
  1285
#endif /* SDL_VIDEO_RENDER_OGL */
slouken@1918
  1286
slouken@1918
  1287
/* vi: set ts=4 sw=4 expandtab: */