src/video/SDL_renderer_gl.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 06 Dec 2008 00:56:47 +0000
changeset 2835 f38257b5d936
parent 2833 c2e182a37f5f
child 2836 b128b94ed31e
permissions -rw-r--r--
Initial pixel shader support for YUV textures in the GL renderer.

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