src/render/opengl/SDL_render_gl.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Nov 2013 22:07:35 -0800
changeset 7995 0eed14683885
parent 7992 6558e2b68c03
child 8093 b43765095a6f
permissions -rw-r--r--
Better fix for bug 2207 - SDL_RenderSetViewport behavior is different/incorrect on OpenGL renderer vs DirectX renderer At least, it works better here on my Mac. :)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5503
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5503
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5503
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5503
diff changeset
     7
  arising from the use of this software.
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5503
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5503
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5503
diff changeset
    11
  freely, subject to the following restrictions:
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5503
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5503
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5503
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5503
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5503
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5503
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5503
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
#include "SDL_config.h"
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
5226
710d00cb3a6a Made it possible to disable the rendering subsystem with configure --disable-render
Sam Lantinga <slouken@libsdl.org>
parents: 5224
diff changeset
    23
#if SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
    24
5233
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
    25
#include "SDL_hints.h"
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
    26
#include "SDL_log.h"
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
    27
#include "SDL_opengl.h"
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
    28
#include "../SDL_sysrender.h"
5228
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
    29
#include "SDL_shaders_gl.h"
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
2246
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
    31
#ifdef __MACOSX__
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
    32
#include <OpenGL/OpenGL.h>
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
    33
#endif
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
    34
2778
38dfc890ee6b Preliminary support for YUV textures
Sam Lantinga <slouken@libsdl.org>
parents: 2328
diff changeset
    35
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
/* OpenGL renderer implementation */
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
2230
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
    38
/* Details on optimizing the texture path on Mac OS X:
5203
25ffd4e5255c Minor consistency cleanup and documentation link update.
Sam Lantinga <slouken@libsdl.org>
parents: 5195
diff changeset
    39
   http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/opengl_texturedata.html
2230
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
    40
*/
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
    41
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
    42
/* Used to re-create the window with OpenGL capability */
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
    43
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
2835
f38257b5d936 Initial pixel shader support for YUV textures in the GL renderer.
Ryan C. Gordon <icculus@icculus.org>
parents: 2833
diff changeset
    44
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    45
static const float inv255f = 1.0f / 255.0f;
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    46
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
    48
static void GL_WindowEvent(SDL_Renderer * renderer,
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
    49
                           const SDL_WindowEvent *event);
7746
6a05d7352575 add in High DPI support (aka Retina)
Edward Rudd <urkle@outoforder.cc>
parents: 7719
diff changeset
    50
static int GL_GetOutputSize(SDL_Renderer * renderer, int *w, int *h);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
                            const SDL_Rect * rect, const void *pixels,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
                            int pitch);
7759
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
    55
static int GL_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
    56
                               const SDL_Rect * rect,
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
    57
                               const Uint8 *Yplane, int Ypitch,
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
    58
                               const Uint8 *Uplane, int Upitch,
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
    59
                               const Uint8 *Vplane, int Vpitch);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
    61
                          const SDL_Rect * rect, void **pixels, int *pitch);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
6247
b6212690f78d Renamed SetTargetTexture() to SetRenderTarget()
Sam Lantinga <slouken@libsdl.org>
parents: 6246
diff changeset
    63
static int GL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
    64
static int GL_UpdateViewport(SDL_Renderer * renderer);
7141
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7091
diff changeset
    65
static int GL_UpdateClipRect(SDL_Renderer * renderer);
3596
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
    66
static int GL_RenderClear(SDL_Renderer * renderer);
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
    67
static int GL_RenderDrawPoints(SDL_Renderer * renderer,
6528
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6499
diff changeset
    68
                               const SDL_FPoint * points, int count);
3596
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
    69
static int GL_RenderDrawLines(SDL_Renderer * renderer,
6528
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6499
diff changeset
    70
                              const SDL_FPoint * points, int count);
3596
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
    71
static int GL_RenderFillRects(SDL_Renderer * renderer,
6528
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6499
diff changeset
    72
                              const SDL_FRect * rects, int count);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
6528
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6499
diff changeset
    74
                         const SDL_Rect * srcrect, const SDL_FRect * dstrect);
6320
6077a1310907 RenderCopyEx,rotation and flipping for all hardware/software backends (#1308)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6260
diff changeset
    75
static int GL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
6528
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6499
diff changeset
    76
                         const SDL_Rect * srcrect, const SDL_FRect * dstrect,
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6499
diff changeset
    77
                         const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip);
3431
c8fbb27627d9 Work in progress on OpenGL ReadPixels/WritePixels interface
Sam Lantinga <slouken@libsdl.org>
parents: 3393
diff changeset
    78
static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
3435
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
    79
                               Uint32 pixel_format, void * pixels, int pitch);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
static void GL_RenderPresent(SDL_Renderer * renderer);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
static void GL_DestroyRenderer(SDL_Renderer * renderer);
6414
df50b0d6c1c3 Implements SDL_GL_BindTexture and SDL_GL_UnbindTexture (#1576)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6320
diff changeset
    83
static int GL_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh);
df50b0d6c1c3 Implements SDL_GL_BindTexture and SDL_GL_UnbindTexture (#1576)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6320
diff changeset
    84
static int GL_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
SDL_RenderDriver GL_RenderDriver = {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
    GL_CreateRenderer,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
    {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
     "opengl",
6237
240f1bced46b Added a renderer flag to expose whether a renderer supports render to texture.
Sam Lantinga <slouken@libsdl.org>
parents: 6232
diff changeset
    90
     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
    91
     1,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
    92
     {SDL_PIXELFORMAT_ARGB8888},
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
     0,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    94
     0}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
};
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
    97
typedef struct GL_FBOList GL_FBOList;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
    98
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
    99
struct GL_FBOList
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   100
{
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   101
    Uint32 w, h;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   102
    GLuint FBO;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   103
    GL_FBOList *next;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   104
};
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   105
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   106
typedef struct
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   107
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
    SDL_GLContext context;
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   109
7198
2928be835dbd It turns out that GL_ARB_debug_output is really only useful on debug contexts, so for consistency and performance we'll only check and report errors on debug contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 7197
diff changeset
   110
    SDL_bool debug_enabled;
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   111
    SDL_bool GL_ARB_debug_output_supported;
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   112
    int errors;
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   113
    char **error_messages;
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   114
    GLDEBUGPROCARB next_error_callback;
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   115
    GLvoid *next_error_userparam;
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   116
2233
fb01ee9716bc Test using glTextureRangeAPPLE
Sam Lantinga <slouken@libsdl.org>
parents: 2230
diff changeset
   117
    SDL_bool GL_ARB_texture_rectangle_supported;
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   118
    struct {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   119
        GL_Shader shader;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   120
        Uint32 color;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   121
        int blendMode;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   122
    } current;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7171
diff changeset
   123
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   124
    SDL_bool GL_EXT_framebuffer_object_supported;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   125
    GL_FBOList *framebuffers;
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   126
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   127
    /* OpenGL functions */
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   128
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
5204
daa5463466c5 Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
Sam Lantinga <slouken@libsdl.org>
parents: 5203
diff changeset
   129
#include "SDL_glfuncs.h"
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   130
#undef SDL_PROC
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   131
5228
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   132
    /* Multitexture support */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   133
    SDL_bool GL_ARB_multitexture_supported;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   134
    PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;
5351
176cd91c3907 Fixed warning on Mac OS X 10.4
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
   135
    GLint num_texture_units;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7171
diff changeset
   136
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   137
    PFNGLGENFRAMEBUFFERSEXTPROC glGenFramebuffersEXT;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   138
    PFNGLDELETEFRAMEBUFFERSEXTPROC glDeleteFramebuffersEXT;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   139
    PFNGLFRAMEBUFFERTEXTURE2DEXTPROC glFramebufferTexture2DEXT;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   140
    PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   141
    PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC glCheckFramebufferStatusEXT;
5228
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   142
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   143
    /* Shader support */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   144
    GL_ShaderContext *shaders;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   145
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   146
} GL_RenderData;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   147
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   148
typedef struct
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   149
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   150
    GLuint texture;
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   151
    GLenum type;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   152
    GLfloat texw;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   153
    GLfloat texh;
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   154
    GLenum format;
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   155
    GLenum formattype;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   156
    void *pixels;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   157
    int pitch;
5227
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   158
    SDL_Rect locked_rect;
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   159
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   160
    /* YV12 texture support */
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   161
    SDL_bool yuv;
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   162
    GLuint utexture;
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   163
    GLuint vtexture;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7171
diff changeset
   164
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   165
    GL_FBOList *fbo;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
} GL_TextureData;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   167
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   168
SDL_FORCE_INLINE const char*
6494
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   169
GL_TranslateError (GLenum error)
1924
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   170
{
6494
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   171
#define GL_ERROR_TRANSLATE(e) case e: return #e;
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   172
    switch (error) {
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   173
    GL_ERROR_TRANSLATE(GL_INVALID_ENUM)
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   174
    GL_ERROR_TRANSLATE(GL_INVALID_VALUE)
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   175
    GL_ERROR_TRANSLATE(GL_INVALID_OPERATION)
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   176
    GL_ERROR_TRANSLATE(GL_OUT_OF_MEMORY)
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   177
    GL_ERROR_TRANSLATE(GL_NO_ERROR)
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   178
    GL_ERROR_TRANSLATE(GL_STACK_OVERFLOW)
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   179
    GL_ERROR_TRANSLATE(GL_STACK_UNDERFLOW)
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   180
    GL_ERROR_TRANSLATE(GL_TABLE_TOO_LARGE)
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   181
    default:
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   182
        return "UNKNOWN";
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   183
}
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   184
#undef GL_ERROR_TRANSLATE
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   185
}
1924
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   186
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   187
SDL_FORCE_INLINE void
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   188
GL_ClearErrors(SDL_Renderer *renderer)
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   189
{
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   190
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   191
7198
2928be835dbd It turns out that GL_ARB_debug_output is really only useful on debug contexts, so for consistency and performance we'll only check and report errors on debug contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 7197
diff changeset
   192
    if (!data->debug_enabled)
2928be835dbd It turns out that GL_ARB_debug_output is really only useful on debug contexts, so for consistency and performance we'll only check and report errors on debug contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 7197
diff changeset
   193
    {
2928be835dbd It turns out that GL_ARB_debug_output is really only useful on debug contexts, so for consistency and performance we'll only check and report errors on debug contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 7197
diff changeset
   194
        return;
2928be835dbd It turns out that GL_ARB_debug_output is really only useful on debug contexts, so for consistency and performance we'll only check and report errors on debug contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 7197
diff changeset
   195
    }
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   196
    if (data->GL_ARB_debug_output_supported) {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   197
        if (data->errors) {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   198
            int i;
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   199
            for (i = 0; i < data->errors; ++i) {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   200
                SDL_free(data->error_messages[i]);
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   201
            }
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   202
            SDL_free(data->error_messages);
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   203
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   204
            data->errors = 0;
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   205
            data->error_messages = NULL;
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   206
        }
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   207
    } else {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   208
        while (data->glGetError() != GL_NO_ERROR) {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   209
            continue;
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   210
        }
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   211
    }
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   212
}
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   213
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   214
SDL_FORCE_INLINE int
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   215
GL_CheckAllErrors (const char *prefix, SDL_Renderer *renderer, const char *file, int line, const char *function)
6494
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   216
{
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   217
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   218
    int ret = 0;
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   219
7198
2928be835dbd It turns out that GL_ARB_debug_output is really only useful on debug contexts, so for consistency and performance we'll only check and report errors on debug contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 7197
diff changeset
   220
    if (!data->debug_enabled)
2928be835dbd It turns out that GL_ARB_debug_output is really only useful on debug contexts, so for consistency and performance we'll only check and report errors on debug contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 7197
diff changeset
   221
    {
2928be835dbd It turns out that GL_ARB_debug_output is really only useful on debug contexts, so for consistency and performance we'll only check and report errors on debug contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 7197
diff changeset
   222
        return 0;
2928be835dbd It turns out that GL_ARB_debug_output is really only useful on debug contexts, so for consistency and performance we'll only check and report errors on debug contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 7197
diff changeset
   223
    }
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   224
    if (data->GL_ARB_debug_output_supported) {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   225
        if (data->errors) {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   226
            int i;
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   227
            for (i = 0; i < data->errors; ++i) {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   228
                SDL_SetError("%s: %s (%d): %s %s", prefix, file, line, function, data->error_messages[i]);
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   229
                ret = -1;
6494
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   230
            }
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   231
            GL_ClearErrors(renderer);
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   232
        }
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   233
    } else {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   234
        /* check gl errors (can return multiple errors) */
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   235
        for (;;) {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   236
            GLenum error = data->glGetError();
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   237
            if (error != GL_NO_ERROR) {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   238
                if (prefix == NULL || prefix[0] == '\0') {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   239
                    prefix = "generic";
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   240
                }
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   241
                SDL_SetError("%s: %s (%d): %s %s (0x%X)", prefix, file, line, function, GL_TranslateError(error), error);
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   242
                ret = -1;
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   243
            } else {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   244
                break;
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   245
            }
6494
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   246
        }
1924
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   247
    }
6494
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   248
    return ret;
1924
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   249
}
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   250
6499
a34024340f54 Patched to compile on Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 6494
diff changeset
   251
#if 0
a34024340f54 Patched to compile on Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 6494
diff changeset
   252
#define GL_CheckError(prefix, renderer)
a34024340f54 Patched to compile on Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 6494
diff changeset
   253
#elif defined(_MSC_VER)
a34024340f54 Patched to compile on Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 6494
diff changeset
   254
#define GL_CheckError(prefix, renderer) GL_CheckAllErrors(prefix, renderer, __FILE__, __LINE__, __FUNCTION__)
a34024340f54 Patched to compile on Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 6494
diff changeset
   255
#else
6494
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   256
#define GL_CheckError(prefix, renderer) GL_CheckAllErrors(prefix, renderer, __FILE__, __LINE__, __PRETTY_FUNCTION__)
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   257
#endif
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   258
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   259
static int
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   260
GL_LoadFunctions(GL_RenderData * data)
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   261
{
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   262
#ifdef __SDL_NOGETPROCADDR__
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   263
#define SDL_PROC(ret,func,params) data->func=func;
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   264
#else
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   265
#define SDL_PROC(ret,func,params) \
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   266
    do { \
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   267
        data->func = SDL_GL_GetProcAddress(#func); \
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   268
        if ( ! data->func ) { \
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   269
            return SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   270
        } \
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   271
    } while ( 0 );
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   272
#endif /* __SDL_NOGETPROCADDR__ */
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   273
5204
daa5463466c5 Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
Sam Lantinga <slouken@libsdl.org>
parents: 5203
diff changeset
   274
#include "SDL_glfuncs.h"
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   275
#undef SDL_PROC
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   276
    return 0;
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   277
}
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   278
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   279
static SDL_GLContext SDL_CurrentContext = NULL;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   280
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   281
static int
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   282
GL_ActivateRenderer(SDL_Renderer * renderer)
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   283
{
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   284
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   285
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   286
    if (SDL_CurrentContext != data->context) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   287
        if (SDL_GL_MakeCurrent(renderer->window, data->context) < 0) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   288
            return -1;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   289
        }
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   290
        SDL_CurrentContext = data->context;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   291
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   292
        GL_UpdateViewport(renderer);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   293
    }
7486
c074c0b23916 Don't try to clear errors in GL_ActivateRenderer() before we MakeCurrent.
Ryan C. Gordon <icculus@icculus.org>
parents: 7420
diff changeset
   294
c074c0b23916 Don't try to clear errors in GL_ActivateRenderer() before we MakeCurrent.
Ryan C. Gordon <icculus@icculus.org>
parents: 7420
diff changeset
   295
    GL_ClearErrors(renderer);
c074c0b23916 Don't try to clear errors in GL_ActivateRenderer() before we MakeCurrent.
Ryan C. Gordon <icculus@icculus.org>
parents: 7420
diff changeset
   296
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   297
    return 0;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   298
}
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   299
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   300
/* This is called if we need to invalidate all of the SDL OpenGL state */
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   301
static void
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   302
GL_ResetState(SDL_Renderer *renderer)
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   303
{
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   304
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   305
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   306
    if (SDL_CurrentContext == data->context) {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   307
        GL_UpdateViewport(renderer);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   308
    } else {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   309
        GL_ActivateRenderer(renderer);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   310
    }
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   311
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   312
    data->current.shader = SHADER_NONE;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   313
    data->current.color = 0;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   314
    data->current.blendMode = -1;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   315
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   316
    data->glDisable(GL_DEPTH_TEST);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   317
    data->glDisable(GL_CULL_FACE);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   318
    /* This ended up causing video discrepancies between OpenGL and Direct3D */
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
   319
    /* data->glEnable(GL_LINE_SMOOTH); */
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   320
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   321
    data->glMatrixMode(GL_MODELVIEW);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   322
    data->glLoadIdentity();
6494
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   323
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   324
    GL_CheckError("", renderer);
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   325
}
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   326
7197
126f8f345bfb Fixed declaration of GL_HandleDebugMessage
Sam Lantinga <slouken@libsdl.org>
parents: 7196
diff changeset
   327
static void APIENTRY
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   328
GL_HandleDebugMessage(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const char *message, void *userParam)
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   329
{
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   330
    SDL_Renderer *renderer = (SDL_Renderer *) userParam;
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   331
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   332
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   333
    if (type == GL_DEBUG_TYPE_ERROR_ARB) {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   334
        /* Record this error */
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   335
        ++data->errors;
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   336
        data->error_messages = SDL_realloc(data->error_messages, data->errors * sizeof(*data->error_messages));
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   337
        if (data->error_messages) {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   338
            data->error_messages[data->errors-1] = SDL_strdup(message);
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   339
        }
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   340
    }
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   341
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   342
    /* If there's another error callback, pass it along, otherwise log it */
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   343
    if (data->next_error_callback) {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   344
        data->next_error_callback(source, type, id, severity, length, message, data->next_error_userparam);
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   345
    } else {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   346
        if (type == GL_DEBUG_TYPE_ERROR_ARB) {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   347
            SDL_LogError(SDL_LOG_CATEGORY_RENDER, "%s", message);
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   348
        } else {
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   349
            SDL_LogDebug(SDL_LOG_CATEGORY_RENDER, "%s", message);
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   350
        }
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   351
    }
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   352
}
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   353
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   354
static GL_FBOList *
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   355
GL_GetFBO(GL_RenderData *data, Uint32 w, Uint32 h)
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   356
{
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   357
    GL_FBOList *result = data->framebuffers;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   358
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   359
    while (result && ((result->w != w) || (result->h != h))) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   360
        result = result->next;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   361
    }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   362
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   363
    if (!result) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   364
        result = SDL_malloc(sizeof(GL_FBOList));
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   365
        if (result) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   366
            result->w = w;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   367
            result->h = h;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   368
            data->glGenFramebuffersEXT(1, &result->FBO);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   369
            result->next = data->framebuffers;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   370
            data->framebuffers = result;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   371
        }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   372
    }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   373
    return result;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   374
}
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   375
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   376
SDL_Renderer *
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   377
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   378
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   379
    SDL_Renderer *renderer;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   380
    GL_RenderData *data;
5233
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
   381
    const char *hint;
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents: 1928
diff changeset
   382
    GLint value;
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
   383
    Uint32 window_flags;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   384
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
   385
    window_flags = SDL_GetWindowFlags(window);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
   386
    if (!(window_flags & SDL_WINDOW_OPENGL)) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
   387
        if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
6182
eb6796f3e598 Fixed crash if the rendering system couldn't create an OpenGL window.
Sam Lantinga <slouken@libsdl.org>
parents: 6171
diff changeset
   388
            /* Uh oh, better try to put it back... */
eb6796f3e598 Fixed crash if the rendering system couldn't create an OpenGL window.
Sam Lantinga <slouken@libsdl.org>
parents: 6171
diff changeset
   389
            SDL_RecreateWindow(window, window_flags);
1924
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   390
            return NULL;
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   391
        }
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   392
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   393
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   394
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   395
    if (!renderer) {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   396
        SDL_OutOfMemory();
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   397
        return NULL;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   398
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   399
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   400
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   401
    if (!data) {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   402
        GL_DestroyRenderer(renderer);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   403
        SDL_OutOfMemory();
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   404
        return NULL;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   405
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   406
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   407
    renderer->WindowEvent = GL_WindowEvent;
7746
6a05d7352575 add in High DPI support (aka Retina)
Edward Rudd <urkle@outoforder.cc>
parents: 7719
diff changeset
   408
    renderer->GetOutputSize = GL_GetOutputSize;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   409
    renderer->CreateTexture = GL_CreateTexture;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   410
    renderer->UpdateTexture = GL_UpdateTexture;
7759
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   411
    renderer->UpdateTextureYUV = GL_UpdateTextureYUV;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   412
    renderer->LockTexture = GL_LockTexture;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   413
    renderer->UnlockTexture = GL_UnlockTexture;
6247
b6212690f78d Renamed SetTargetTexture() to SetRenderTarget()
Sam Lantinga <slouken@libsdl.org>
parents: 6246
diff changeset
   414
    renderer->SetRenderTarget = GL_SetRenderTarget;
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   415
    renderer->UpdateViewport = GL_UpdateViewport;
7141
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7091
diff changeset
   416
    renderer->UpdateClipRect = GL_UpdateClipRect;
3596
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   417
    renderer->RenderClear = GL_RenderClear;
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   418
    renderer->RenderDrawPoints = GL_RenderDrawPoints;
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   419
    renderer->RenderDrawLines = GL_RenderDrawLines;
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   420
    renderer->RenderFillRects = GL_RenderFillRects;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   421
    renderer->RenderCopy = GL_RenderCopy;
6320
6077a1310907 RenderCopyEx,rotation and flipping for all hardware/software backends (#1308)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6260
diff changeset
   422
    renderer->RenderCopyEx = GL_RenderCopyEx;
3431
c8fbb27627d9 Work in progress on OpenGL ReadPixels/WritePixels interface
Sam Lantinga <slouken@libsdl.org>
parents: 3393
diff changeset
   423
    renderer->RenderReadPixels = GL_RenderReadPixels;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   424
    renderer->RenderPresent = GL_RenderPresent;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   425
    renderer->DestroyTexture = GL_DestroyTexture;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   426
    renderer->DestroyRenderer = GL_DestroyRenderer;
6414
df50b0d6c1c3 Implements SDL_GL_BindTexture and SDL_GL_UnbindTexture (#1576)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6320
diff changeset
   427
    renderer->GL_BindTexture = GL_BindTexture;
df50b0d6c1c3 Implements SDL_GL_BindTexture and SDL_GL_UnbindTexture (#1576)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6320
diff changeset
   428
    renderer->GL_UnbindTexture = GL_UnbindTexture;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   429
    renderer->info = GL_RenderDriver.info;
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   430
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   431
    renderer->driverdata = data;
6171
7b9126fde949 Fixed bug 1256 - Invalid window warning in GL_CreateRenderer
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   432
    renderer->window = window;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   433
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3607
diff changeset
   434
    data->context = SDL_GL_CreateContext(window);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   435
    if (!data->context) {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   436
        GL_DestroyRenderer(renderer);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   437
        return NULL;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   438
    }
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3607
diff changeset
   439
    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   440
        GL_DestroyRenderer(renderer);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   441
        return NULL;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   442
    }
5204
daa5463466c5 Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
Sam Lantinga <slouken@libsdl.org>
parents: 5203
diff changeset
   443
daa5463466c5 Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
Sam Lantinga <slouken@libsdl.org>
parents: 5203
diff changeset
   444
    if (GL_LoadFunctions(data) < 0) {
daa5463466c5 Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
Sam Lantinga <slouken@libsdl.org>
parents: 5203
diff changeset
   445
        GL_DestroyRenderer(renderer);
daa5463466c5 Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
Sam Lantinga <slouken@libsdl.org>
parents: 5203
diff changeset
   446
        return NULL;
daa5463466c5 Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
Sam Lantinga <slouken@libsdl.org>
parents: 5203
diff changeset
   447
    }
daa5463466c5 Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
Sam Lantinga <slouken@libsdl.org>
parents: 5203
diff changeset
   448
2246
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
   449
#ifdef __MACOSX__
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
   450
    /* Enable multi-threaded rendering */
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
   451
    /* Disabled until Ryan finishes his VBO/PBO code...
2295
dbc6d1893869 Checking in Christian Walther's patch for x11 keyboard input. Minor code tweaks by Bob.
Bob Pendleton <bob@pendleton.com>
parents: 2275
diff changeset
   452
       CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
dbc6d1893869 Checking in Christian Walther's patch for x11 keyboard input. Minor code tweaks by Bob.
Bob Pendleton <bob@pendleton.com>
parents: 2275
diff changeset
   453
     */
2246
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
   454
#endif
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
   455
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   456
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   457
        SDL_GL_SetSwapInterval(1);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   458
    } else {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   459
        SDL_GL_SetSwapInterval(0);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   460
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   461
    if (SDL_GL_GetSwapInterval() > 0) {
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   462
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   463
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   464
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   465
    /* Check for debug output support */
7198
2928be835dbd It turns out that GL_ARB_debug_output is really only useful on debug contexts, so for consistency and performance we'll only check and report errors on debug contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 7197
diff changeset
   466
    if (SDL_GL_GetAttribute(SDL_GL_CONTEXT_FLAGS, &value) == 0 &&
2928be835dbd It turns out that GL_ARB_debug_output is really only useful on debug contexts, so for consistency and performance we'll only check and report errors on debug contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 7197
diff changeset
   467
        (value & SDL_GL_CONTEXT_DEBUG_FLAG)) {
2928be835dbd It turns out that GL_ARB_debug_output is really only useful on debug contexts, so for consistency and performance we'll only check and report errors on debug contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 7197
diff changeset
   468
        data->debug_enabled = SDL_TRUE;
2928be835dbd It turns out that GL_ARB_debug_output is really only useful on debug contexts, so for consistency and performance we'll only check and report errors on debug contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 7197
diff changeset
   469
    }
2928be835dbd It turns out that GL_ARB_debug_output is really only useful on debug contexts, so for consistency and performance we'll only check and report errors on debug contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 7197
diff changeset
   470
    if (data->debug_enabled && SDL_GL_ExtensionSupported("GL_ARB_debug_output")) {
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   471
        PFNGLDEBUGMESSAGECALLBACKARBPROC glDebugMessageCallbackARBFunc = (PFNGLDEBUGMESSAGECALLBACKARBPROC) SDL_GL_GetProcAddress("glDebugMessageCallbackARB");
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   472
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   473
        data->GL_ARB_debug_output_supported = SDL_TRUE;
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   474
        data->glGetPointerv(GL_DEBUG_CALLBACK_FUNCTION_ARB, (GLvoid **)&data->next_error_callback);
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   475
        data->glGetPointerv(GL_DEBUG_CALLBACK_USER_PARAM_ARB, &data->next_error_userparam);
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   476
        glDebugMessageCallbackARBFunc(GL_HandleDebugMessage, renderer);
7198
2928be835dbd It turns out that GL_ARB_debug_output is really only useful on debug contexts, so for consistency and performance we'll only check and report errors on debug contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 7197
diff changeset
   477
2928be835dbd It turns out that GL_ARB_debug_output is really only useful on debug contexts, so for consistency and performance we'll only check and report errors on debug contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 7197
diff changeset
   478
        /* Make sure our callback is called when errors actually happen */
2928be835dbd It turns out that GL_ARB_debug_output is really only useful on debug contexts, so for consistency and performance we'll only check and report errors on debug contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 7197
diff changeset
   479
        data->glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   480
    }
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   481
1926
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   482
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   483
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   484
        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
6449
fa00544b1374 Fixed bug 1591 - Renderer maximum texture size is incorrect when GL_ARB_texture_rectangle is active
Sam Lantinga <slouken@libsdl.org>
parents: 6414
diff changeset
   485
        data->glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_ARB, &value);
fa00544b1374 Fixed bug 1591 - Renderer maximum texture size is incorrect when GL_ARB_texture_rectangle is active
Sam Lantinga <slouken@libsdl.org>
parents: 6414
diff changeset
   486
        renderer->info.max_texture_width = value;
fa00544b1374 Fixed bug 1591 - Renderer maximum texture size is incorrect when GL_ARB_texture_rectangle is active
Sam Lantinga <slouken@libsdl.org>
parents: 6414
diff changeset
   487
        renderer->info.max_texture_height = value;
fa00544b1374 Fixed bug 1591 - Renderer maximum texture size is incorrect when GL_ARB_texture_rectangle is active
Sam Lantinga <slouken@libsdl.org>
parents: 6414
diff changeset
   488
    } else {
fa00544b1374 Fixed bug 1591 - Renderer maximum texture size is incorrect when GL_ARB_texture_rectangle is active
Sam Lantinga <slouken@libsdl.org>
parents: 6414
diff changeset
   489
        data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
fa00544b1374 Fixed bug 1591 - Renderer maximum texture size is incorrect when GL_ARB_texture_rectangle is active
Sam Lantinga <slouken@libsdl.org>
parents: 6414
diff changeset
   490
        renderer->info.max_texture_width = value;
fa00544b1374 Fixed bug 1591 - Renderer maximum texture size is incorrect when GL_ARB_texture_rectangle is active
Sam Lantinga <slouken@libsdl.org>
parents: 6414
diff changeset
   491
        renderer->info.max_texture_height = value;
1926
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   492
    }
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   493
5228
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   494
    /* Check for multitexture support */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   495
    if (SDL_GL_ExtensionSupported("GL_ARB_multitexture")) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   496
        data->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glActiveTextureARB");
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   497
        if (data->glActiveTextureARB) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   498
            data->GL_ARB_multitexture_supported = SDL_TRUE;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   499
            data->glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &data->num_texture_units);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   500
        }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   501
    }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   502
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   503
    /* Check for shader support */
5233
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
   504
    hint = SDL_GetHint(SDL_HINT_RENDER_OPENGL_SHADERS);
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
   505
    if (!hint || *hint != '0') {
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
   506
        data->shaders = GL_CreateShaderContext();
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
   507
    }
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
   508
    SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, "OpenGL shaders: %s",
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
   509
                data->shaders ? "ENABLED" : "DISABLED");
5228
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   510
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   511
    /* We support YV12 textures using 3 textures and a shader */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   512
    if (data->shaders && data->num_texture_units >= 3) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   513
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_YV12;
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   514
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_IYUV;
5228
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   515
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7171
diff changeset
   516
7814
5a057d8d685e Added support for SDL_PIXELFORMAT_UYVY surfaces on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7779
diff changeset
   517
#ifdef __MACOSX__
5a057d8d685e Added support for SDL_PIXELFORMAT_UYVY surfaces on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7779
diff changeset
   518
    renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_UYVY;
5a057d8d685e Added support for SDL_PIXELFORMAT_UYVY surfaces on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7779
diff changeset
   519
#endif
5a057d8d685e Added support for SDL_PIXELFORMAT_UYVY surfaces on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7779
diff changeset
   520
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   521
    if (SDL_GL_ExtensionSupported("GL_EXT_framebuffer_object")) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   522
        data->GL_EXT_framebuffer_object_supported = SDL_TRUE;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   523
        data->glGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC)
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   524
            SDL_GL_GetProcAddress("glGenFramebuffersEXT");
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   525
        data->glDeleteFramebuffersEXT = (PFNGLDELETEFRAMEBUFFERSEXTPROC)
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   526
            SDL_GL_GetProcAddress("glDeleteFramebuffersEXT");
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   527
        data->glFramebufferTexture2DEXT = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   528
            SDL_GL_GetProcAddress("glFramebufferTexture2DEXT");
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   529
        data->glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   530
            SDL_GL_GetProcAddress("glBindFramebufferEXT");
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   531
        data->glCheckFramebufferStatusEXT = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   532
            SDL_GL_GetProcAddress("glCheckFramebufferStatusEXT");
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   533
        renderer->info.flags |= SDL_RENDERER_TARGETTEXTURE;
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   534
    }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   535
    data->framebuffers = NULL;
5228
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   536
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   537
    /* Set up parameters for rendering */
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   538
    GL_ResetState(renderer);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   539
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   540
    return renderer;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   541
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   542
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   543
static void
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   544
GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
1970
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   545
{
6260
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   546
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED ||
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   547
        event->event == SDL_WINDOWEVENT_SHOWN ||
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   548
        event->event == SDL_WINDOWEVENT_HIDDEN) {
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   549
        /* Rebind the context to the window area and update matrices */
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   550
        SDL_CurrentContext = NULL;
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   551
    }
1923
d4572b97b08f Switch OpenGL contexts when switching render contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 1922
diff changeset
   552
}
d4572b97b08f Switch OpenGL contexts when switching render contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 1922
diff changeset
   553
7746
6a05d7352575 add in High DPI support (aka Retina)
Edward Rudd <urkle@outoforder.cc>
parents: 7719
diff changeset
   554
static int
6a05d7352575 add in High DPI support (aka Retina)
Edward Rudd <urkle@outoforder.cc>
parents: 7719
diff changeset
   555
GL_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
6a05d7352575 add in High DPI support (aka Retina)
Edward Rudd <urkle@outoforder.cc>
parents: 7719
diff changeset
   556
{
6a05d7352575 add in High DPI support (aka Retina)
Edward Rudd <urkle@outoforder.cc>
parents: 7719
diff changeset
   557
    SDL_GL_GetDrawableSize(renderer->window, w, h);
6a05d7352575 add in High DPI support (aka Retina)
Edward Rudd <urkle@outoforder.cc>
parents: 7719
diff changeset
   558
6a05d7352575 add in High DPI support (aka Retina)
Edward Rudd <urkle@outoforder.cc>
parents: 7719
diff changeset
   559
    return 0;
6a05d7352575 add in High DPI support (aka Retina)
Edward Rudd <urkle@outoforder.cc>
parents: 7719
diff changeset
   560
}
6a05d7352575 add in High DPI support (aka Retina)
Edward Rudd <urkle@outoforder.cc>
parents: 7719
diff changeset
   561
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   562
SDL_FORCE_INLINE int
1922
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   563
power_of_2(int input)
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   564
{
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   565
    int value = 1;
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   566
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   567
    while (value < input) {
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   568
        value <<= 1;
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   569
    }
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   570
    return value;
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   571
}
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   572
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   573
SDL_FORCE_INLINE SDL_bool
3433
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   574
convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   575
               GLint* internalFormat, GLenum* format, GLenum* type)
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   576
{
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   577
    switch (pixel_format) {
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   578
    case SDL_PIXELFORMAT_ARGB8888:
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   579
        *internalFormat = GL_RGBA8;
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   580
        *format = GL_BGRA;
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   581
        *type = GL_UNSIGNED_INT_8_8_8_8_REV;
3433
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   582
        break;
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   583
    case SDL_PIXELFORMAT_YV12:
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   584
    case SDL_PIXELFORMAT_IYUV:
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   585
        *internalFormat = GL_LUMINANCE;
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   586
        *format = GL_LUMINANCE;
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   587
        *type = GL_UNSIGNED_BYTE;
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   588
        break;
7814
5a057d8d685e Added support for SDL_PIXELFORMAT_UYVY surfaces on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7779
diff changeset
   589
#ifdef __MACOSX__
5a057d8d685e Added support for SDL_PIXELFORMAT_UYVY surfaces on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7779
diff changeset
   590
    case SDL_PIXELFORMAT_UYVY:
5a057d8d685e Added support for SDL_PIXELFORMAT_UYVY surfaces on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7779
diff changeset
   591
		*internalFormat = GL_RGB8;
5a057d8d685e Added support for SDL_PIXELFORMAT_UYVY surfaces on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7779
diff changeset
   592
		*format = GL_YCBCR_422_APPLE;
5a057d8d685e Added support for SDL_PIXELFORMAT_UYVY surfaces on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7779
diff changeset
   593
		*type = GL_UNSIGNED_SHORT_8_8_APPLE;
5a057d8d685e Added support for SDL_PIXELFORMAT_UYVY surfaces on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7779
diff changeset
   594
		break;
5a057d8d685e Added support for SDL_PIXELFORMAT_UYVY surfaces on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7779
diff changeset
   595
#endif
3433
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   596
    default:
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   597
        return SDL_FALSE;
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   598
    }
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   599
    return SDL_TRUE;
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   600
}
2835
f38257b5d936 Initial pixel shader support for YUV textures in the GL renderer.
Ryan C. Gordon <icculus@icculus.org>
parents: 2833
diff changeset
   601
5484
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5465
diff changeset
   602
static GLenum
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5465
diff changeset
   603
GetScaleQuality(void)
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5465
diff changeset
   604
{
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5465
diff changeset
   605
    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5465
diff changeset
   606
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5465
diff changeset
   607
    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5465
diff changeset
   608
        return GL_NEAREST;
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5465
diff changeset
   609
    } else {
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5465
diff changeset
   610
        return GL_LINEAR;
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5465
diff changeset
   611
    }
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5465
diff changeset
   612
}
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5465
diff changeset
   613
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   614
static int
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   615
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   616
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   617
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   618
    GL_TextureData *data;
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   619
    GLint internalFormat;
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   620
    GLenum format, type;
1922
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   621
    int texture_w, texture_h;
5503
be88d105e91c The scale mode is per texture, not per texture unit.
Sam Lantinga <slouken@libsdl.org>
parents: 5484
diff changeset
   622
    GLenum scaleMode;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   623
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   624
    GL_ActivateRenderer(renderer);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   625
3433
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   626
    if (!convert_format(renderdata, texture->format, &internalFormat,
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   627
                        &format, &type)) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   628
        return SDL_SetError("Texture format %s not supported by OpenGL",
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   629
                            SDL_GetPixelFormatName(texture->format));
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   630
    }
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   631
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   632
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   633
    if (!data) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   634
        return SDL_OutOfMemory();
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   635
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   636
2222
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   637
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   638
        size_t size;
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   639
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   640
        size = texture->h * data->pitch;
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   641
        if (texture->format == SDL_PIXELFORMAT_YV12 ||
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   642
            texture->format == SDL_PIXELFORMAT_IYUV) {
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   643
            /* Need to add size for the U and V planes */
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   644
            size += (2 * (texture->h * data->pitch) / 4);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   645
        }
5402
5d102cd8aef3 Zero streaming textures at the driver level
Sam Lantinga <slouken@libsdl.org>
parents: 5397
diff changeset
   646
        data->pixels = SDL_calloc(1, size);
2222
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   647
        if (!data->pixels) {
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   648
            SDL_free(data);
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   649
            return SDL_OutOfMemory();
2222
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   650
        }
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   651
    }
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   652
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   653
    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   654
        data->fbo = GL_GetFBO(renderdata, texture->w, texture->h);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   655
    } else {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   656
        data->fbo = NULL;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   657
    }
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   658
6494
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   659
    GL_CheckError("", renderer);
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   660
    renderdata->glGenTextures(1, &data->texture);
7624
ac9f3b12c42a Check the return value of glGenTextures()
Sam Lantinga <slouken@libsdl.org>
parents: 7502
diff changeset
   661
    if (GL_CheckError("glGenTexures()", renderer) < 0) {
ac9f3b12c42a Check the return value of glGenTextures()
Sam Lantinga <slouken@libsdl.org>
parents: 7502
diff changeset
   662
        SDL_free(data);
ac9f3b12c42a Check the return value of glGenTextures()
Sam Lantinga <slouken@libsdl.org>
parents: 7502
diff changeset
   663
        return -1;
ac9f3b12c42a Check the return value of glGenTextures()
Sam Lantinga <slouken@libsdl.org>
parents: 7502
diff changeset
   664
    }
7779
d46f8afbcb17 Fixed a potential double-free bug if glGenTextures() failed.
Sam Lantinga <slouken@libsdl.org>
parents: 7759
diff changeset
   665
    texture->driverdata = data;
d46f8afbcb17 Fixed a potential double-free bug if glGenTextures() failed.
Sam Lantinga <slouken@libsdl.org>
parents: 7759
diff changeset
   666
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6207
diff changeset
   667
    if ((renderdata->GL_ARB_texture_rectangle_supported)
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
   668
        /* && texture->access != SDL_TEXTUREACCESS_TARGET */){
1926
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   669
        data->type = GL_TEXTURE_RECTANGLE_ARB;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   670
        texture_w = texture->w;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   671
        texture_h = texture->h;
2835
f38257b5d936 Initial pixel shader support for YUV textures in the GL renderer.
Ryan C. Gordon <icculus@icculus.org>
parents: 2833
diff changeset
   672
        data->texw = (GLfloat) texture_w;
f38257b5d936 Initial pixel shader support for YUV textures in the GL renderer.
Ryan C. Gordon <icculus@icculus.org>
parents: 2833
diff changeset
   673
        data->texh = (GLfloat) texture_h;
1926
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   674
    } else {
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   675
        data->type = GL_TEXTURE_2D;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   676
        texture_w = power_of_2(texture->w);
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   677
        texture_h = power_of_2(texture->h);
2835
f38257b5d936 Initial pixel shader support for YUV textures in the GL renderer.
Ryan C. Gordon <icculus@icculus.org>
parents: 2833
diff changeset
   678
        data->texw = (GLfloat) (texture->w) / texture_w;
1926
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   679
        data->texh = (GLfloat) texture->h / texture_h;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   680
    }
2835
f38257b5d936 Initial pixel shader support for YUV textures in the GL renderer.
Ryan C. Gordon <icculus@icculus.org>
parents: 2833
diff changeset
   681
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   682
    data->format = format;
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   683
    data->formattype = type;
5503
be88d105e91c The scale mode is per texture, not per texture unit.
Sam Lantinga <slouken@libsdl.org>
parents: 5484
diff changeset
   684
    scaleMode = GetScaleQuality();
2884
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2858
diff changeset
   685
    renderdata->glEnable(data->type);
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   686
    renderdata->glBindTexture(data->type, data->texture);
5503
be88d105e91c The scale mode is per texture, not per texture unit.
Sam Lantinga <slouken@libsdl.org>
parents: 5484
diff changeset
   687
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER, scaleMode);
be88d105e91c The scale mode is per texture, not per texture unit.
Sam Lantinga <slouken@libsdl.org>
parents: 5484
diff changeset
   688
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER, scaleMode);
6207
15ff38383cb7 Fixed bug 1331 - SDL_CreateTextureFromSurface fails for OpenGL + Win XP 64 NVidia 285.58 with GL_INVALID_ENUM
Sam Lantinga <slouken@libsdl.org>
parents: 6182
diff changeset
   689
    /* According to the spec, CLAMP_TO_EDGE is the default for TEXTURE_RECTANGLE
15ff38383cb7 Fixed bug 1331 - SDL_CreateTextureFromSurface fails for OpenGL + Win XP 64 NVidia 285.58 with GL_INVALID_ENUM
Sam Lantinga <slouken@libsdl.org>
parents: 6182
diff changeset
   690
       and setting it causes an INVALID_ENUM error in the latest NVidia drivers.
15ff38383cb7 Fixed bug 1331 - SDL_CreateTextureFromSurface fails for OpenGL + Win XP 64 NVidia 285.58 with GL_INVALID_ENUM
Sam Lantinga <slouken@libsdl.org>
parents: 6182
diff changeset
   691
    */
15ff38383cb7 Fixed bug 1331 - SDL_CreateTextureFromSurface fails for OpenGL + Win XP 64 NVidia 285.58 with GL_INVALID_ENUM
Sam Lantinga <slouken@libsdl.org>
parents: 6182
diff changeset
   692
    if (data->type != GL_TEXTURE_RECTANGLE_ARB) {
15ff38383cb7 Fixed bug 1331 - SDL_CreateTextureFromSurface fails for OpenGL + Win XP 64 NVidia 285.58 with GL_INVALID_ENUM
Sam Lantinga <slouken@libsdl.org>
parents: 6182
diff changeset
   693
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
15ff38383cb7 Fixed bug 1331 - SDL_CreateTextureFromSurface fails for OpenGL + Win XP 64 NVidia 285.58 with GL_INVALID_ENUM
Sam Lantinga <slouken@libsdl.org>
parents: 6182
diff changeset
   694
                                    GL_CLAMP_TO_EDGE);
15ff38383cb7 Fixed bug 1331 - SDL_CreateTextureFromSurface fails for OpenGL + Win XP 64 NVidia 285.58 with GL_INVALID_ENUM
Sam Lantinga <slouken@libsdl.org>
parents: 6182
diff changeset
   695
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
15ff38383cb7 Fixed bug 1331 - SDL_CreateTextureFromSurface fails for OpenGL + Win XP 64 NVidia 285.58 with GL_INVALID_ENUM
Sam Lantinga <slouken@libsdl.org>
parents: 6182
diff changeset
   696
                                    GL_CLAMP_TO_EDGE);
15ff38383cb7 Fixed bug 1331 - SDL_CreateTextureFromSurface fails for OpenGL + Win XP 64 NVidia 285.58 with GL_INVALID_ENUM
Sam Lantinga <slouken@libsdl.org>
parents: 6182
diff changeset
   697
    }
2840
53ca0d758b0a The previous checkin fixes the crash, so at least we're not overrunning
Sam Lantinga <slouken@libsdl.org>
parents: 2839
diff changeset
   698
#ifdef __MACOSX__
2230
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   699
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   700
#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   701
#endif
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   702
#ifndef STORAGE_CACHED_APPLE
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   703
#define STORAGE_CACHED_APPLE                0x85BE
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   704
#endif
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   705
#ifndef STORAGE_SHARED_APPLE
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   706
#define STORAGE_SHARED_APPLE                0x85BF
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   707
#endif
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   708
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   709
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   710
                                    GL_STORAGE_SHARED_APPLE);
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   711
    } else {
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   712
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   713
                                    GL_STORAGE_CACHED_APPLE);
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   714
    }
2295
dbc6d1893869 Checking in Christian Walther's patch for x11 keyboard input. Minor code tweaks by Bob.
Bob Pendleton <bob@pendleton.com>
parents: 2275
diff changeset
   715
    if (texture->access == SDL_TEXTUREACCESS_STREAMING
5397
88ff1f463134 Hopefully fixed crashes when creating multiple streaming textures of different sizes.
Sam Lantinga <slouken@libsdl.org>
parents: 5381
diff changeset
   716
        && texture->format == SDL_PIXELFORMAT_ARGB8888
88ff1f463134 Hopefully fixed crashes when creating multiple streaming textures of different sizes.
Sam Lantinga <slouken@libsdl.org>
parents: 5381
diff changeset
   717
        && (texture->w % 8) == 0) {
2230
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   718
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
5397
88ff1f463134 Hopefully fixed crashes when creating multiple streaming textures of different sizes.
Sam Lantinga <slouken@libsdl.org>
parents: 5381
diff changeset
   719
        renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
88ff1f463134 Hopefully fixed crashes when creating multiple streaming textures of different sizes.
Sam Lantinga <slouken@libsdl.org>
parents: 5381
diff changeset
   720
        renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
88ff1f463134 Hopefully fixed crashes when creating multiple streaming textures of different sizes.
Sam Lantinga <slouken@libsdl.org>
parents: 5381
diff changeset
   721
                          (data->pitch / SDL_BYTESPERPIXEL(texture->format)));
2230
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   722
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   723
                                 texture_h, 0, format, type, data->pixels);
5397
88ff1f463134 Hopefully fixed crashes when creating multiple streaming textures of different sizes.
Sam Lantinga <slouken@libsdl.org>
parents: 5381
diff changeset
   724
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   725
    }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   726
    else
2809
7e257c3a3bf0 Temporary fix for Mac OS X crash in textoverlay
Sam Lantinga <slouken@libsdl.org>
parents: 2808
diff changeset
   727
#endif
2230
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   728
    {
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   729
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   730
                                 texture_h, 0, format, type, NULL);
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   731
    }
3041
20d65430e63c Fixed OpenGL state issue reported by Dmytro Bogovych
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   732
    renderdata->glDisable(data->type);
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   733
    if (GL_CheckError("glTexImage2D()", renderer) < 0) {
1924
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   734
        return -1;
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   735
    }
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   736
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   737
    if (texture->format == SDL_PIXELFORMAT_YV12 ||
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   738
        texture->format == SDL_PIXELFORMAT_IYUV) {
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   739
        data->yuv = SDL_TRUE;
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   740
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   741
        renderdata->glGenTextures(1, &data->utexture);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   742
        renderdata->glGenTextures(1, &data->vtexture);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   743
        renderdata->glEnable(data->type);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   744
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   745
        renderdata->glBindTexture(data->type, data->utexture);
5503
be88d105e91c The scale mode is per texture, not per texture unit.
Sam Lantinga <slouken@libsdl.org>
parents: 5484
diff changeset
   746
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
be88d105e91c The scale mode is per texture, not per texture unit.
Sam Lantinga <slouken@libsdl.org>
parents: 5484
diff changeset
   747
                                    scaleMode);
be88d105e91c The scale mode is per texture, not per texture unit.
Sam Lantinga <slouken@libsdl.org>
parents: 5484
diff changeset
   748
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
be88d105e91c The scale mode is per texture, not per texture unit.
Sam Lantinga <slouken@libsdl.org>
parents: 5484
diff changeset
   749
                                    scaleMode);
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   750
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   751
                                    GL_CLAMP_TO_EDGE);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   752
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   753
                                    GL_CLAMP_TO_EDGE);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   754
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w/2,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   755
                                 texture_h/2, 0, format, type, NULL);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   756
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   757
        renderdata->glBindTexture(data->type, data->vtexture);
5503
be88d105e91c The scale mode is per texture, not per texture unit.
Sam Lantinga <slouken@libsdl.org>
parents: 5484
diff changeset
   758
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
be88d105e91c The scale mode is per texture, not per texture unit.
Sam Lantinga <slouken@libsdl.org>
parents: 5484
diff changeset
   759
                                    scaleMode);
be88d105e91c The scale mode is per texture, not per texture unit.
Sam Lantinga <slouken@libsdl.org>
parents: 5484
diff changeset
   760
        renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
be88d105e91c The scale mode is per texture, not per texture unit.
Sam Lantinga <slouken@libsdl.org>
parents: 5484
diff changeset
   761
                                    scaleMode);
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   762
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   763
                                    GL_CLAMP_TO_EDGE);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   764
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   765
                                    GL_CLAMP_TO_EDGE);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   766
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w/2,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   767
                                 texture_h/2, 0, format, type, NULL);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   768
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   769
        renderdata->glDisable(data->type);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   770
    }
6494
309599be5c2a Fixed bug 1584 - Improved glError checks in the opengl renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6449
diff changeset
   771
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   772
    return GL_CheckError("", renderer);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   773
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   774
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   775
static int
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   776
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   777
                 const SDL_Rect * rect, const void *pixels, int pitch)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   778
{
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   779
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   780
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   781
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   782
    GL_ActivateRenderer(renderer);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   783
5397
88ff1f463134 Hopefully fixed crashes when creating multiple streaming textures of different sizes.
Sam Lantinga <slouken@libsdl.org>
parents: 5381
diff changeset
   784
    renderdata->glEnable(data->type);
88ff1f463134 Hopefully fixed crashes when creating multiple streaming textures of different sizes.
Sam Lantinga <slouken@libsdl.org>
parents: 5381
diff changeset
   785
    renderdata->glBindTexture(data->type, data->texture);
5227
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   786
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   787
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   788
                              (pitch / SDL_BYTESPERPIXEL(texture->format)));
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   789
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   790
                                rect->h, data->format, data->formattype,
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   791
                                pixels);
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   792
    if (data->yuv) {
5265
48724afcdc6e Ah there, that fixed it. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5264
diff changeset
   793
        renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, (pitch / 2));
48724afcdc6e Ah there, that fixed it. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5264
diff changeset
   794
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   795
        /* Skip to the correct offset into the next texture */
6135
b052a5882a47 Fixed bug 1315 - Greenish video when video size smaller than texture
Sam Lantinga <slouken@libsdl.org>
parents: 5981
diff changeset
   796
        pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   797
        if (texture->format == SDL_PIXELFORMAT_YV12) {
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   798
            renderdata->glBindTexture(data->type, data->vtexture);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   799
        } else {
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   800
            renderdata->glBindTexture(data->type, data->utexture);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   801
        }
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   802
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   803
                                    rect->w/2, rect->h/2,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   804
                                    data->format, data->formattype, pixels);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   805
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   806
        /* Skip to the correct offset into the next texture */
6135
b052a5882a47 Fixed bug 1315 - Greenish video when video size smaller than texture
Sam Lantinga <slouken@libsdl.org>
parents: 5981
diff changeset
   807
        pixels = (const void*)((const Uint8*)pixels + (rect->h * pitch)/4);
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   808
        if (texture->format == SDL_PIXELFORMAT_YV12) {
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   809
            renderdata->glBindTexture(data->type, data->utexture);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   810
        } else {
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   811
            renderdata->glBindTexture(data->type, data->vtexture);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   812
        }
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   813
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   814
                                    rect->w/2, rect->h/2,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   815
                                    data->format, data->formattype, pixels);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   816
    }
3041
20d65430e63c Fixed OpenGL state issue reported by Dmytro Bogovych
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   817
    renderdata->glDisable(data->type);
7779
d46f8afbcb17 Fixed a potential double-free bug if glGenTextures() failed.
Sam Lantinga <slouken@libsdl.org>
parents: 7759
diff changeset
   818
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   819
    return GL_CheckError("glTexSubImage2D()", renderer);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   820
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   821
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   822
static int
7759
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   823
GL_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   824
                    const SDL_Rect * rect,
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   825
                    const Uint8 *Yplane, int Ypitch,
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   826
                    const Uint8 *Uplane, int Upitch,
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   827
                    const Uint8 *Vplane, int Vpitch)
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   828
{
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   829
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   830
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   831
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   832
    GL_ActivateRenderer(renderer);
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   833
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   834
    renderdata->glEnable(data->type);
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   835
    renderdata->glBindTexture(data->type, data->texture);
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   836
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   837
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Ypitch);
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   838
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   839
                                rect->h, data->format, data->formattype,
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   840
                                Yplane);
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   841
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   842
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Upitch);
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   843
    renderdata->glBindTexture(data->type, data->utexture);
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   844
    renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   845
                                rect->w/2, rect->h/2,
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   846
                                data->format, data->formattype, Uplane);
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   847
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   848
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Vpitch);
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   849
    renderdata->glBindTexture(data->type, data->vtexture);
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   850
    renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   851
                                rect->w/2, rect->h/2,
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   852
                                data->format, data->formattype, Vplane);
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   853
    renderdata->glDisable(data->type);
7779
d46f8afbcb17 Fixed a potential double-free bug if glGenTextures() failed.
Sam Lantinga <slouken@libsdl.org>
parents: 7759
diff changeset
   854
7759
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   855
    return GL_CheckError("glTexSubImage2D()", renderer);
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   856
}
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   857
869583422e5a Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7746
diff changeset
   858
static int
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   859
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   860
               const SDL_Rect * rect, void **pixels, int *pitch)
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   861
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   862
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   863
5227
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   864
    data->locked_rect = *rect;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7171
diff changeset
   865
    *pixels =
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   866
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   867
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   868
    *pitch = data->pitch;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   869
    return 0;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   870
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   871
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   872
static void
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   873
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   874
{
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   875
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
5227
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   876
    const SDL_Rect *rect;
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   877
    void *pixels;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   878
5227
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   879
    rect = &data->locked_rect;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7171
diff changeset
   880
    pixels =
5227
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   881
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   882
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   883
    GL_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   884
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   885
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   886
static int
6247
b6212690f78d Renamed SetTargetTexture() to SetRenderTarget()
Sam Lantinga <slouken@libsdl.org>
parents: 6246
diff changeset
   887
GL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   888
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7171
diff changeset
   889
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   890
    GL_TextureData *texturedata;
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   891
    GLenum status;
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   892
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   893
    GL_ActivateRenderer(renderer);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7171
diff changeset
   894
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   895
    if (texture == NULL) {
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   896
        data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   897
        return 0;
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   898
    }
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   899
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   900
    texturedata = (GL_TextureData *) texture->driverdata;
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   901
    data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, texturedata->fbo->FBO);
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   902
    /* TODO: check if texture pixel format allows this operation */
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   903
    data->glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, texturedata->type, texturedata->texture, 0);
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   904
    /* Check FBO status */
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   905
    status = data->glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   906
    if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   907
        return SDL_SetError("glFramebufferTexture2DEXT() failed");
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   908
    }
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   909
    return 0;
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   910
}
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   911
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   912
static int
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   913
GL_UpdateViewport(SDL_Renderer * renderer)
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   914
{
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   915
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   916
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   917
    if (SDL_CurrentContext != data->context) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   918
        /* We'll update the viewport after we rebind the context */
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   919
        return 0;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   920
    }
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   921
7995
0eed14683885 Better fix for bug 2207 - SDL_RenderSetViewport behavior is different/incorrect on OpenGL renderer vs DirectX renderer
Sam Lantinga <slouken@libsdl.org>
parents: 7992
diff changeset
   922
    if (renderer->target) {
0eed14683885 Better fix for bug 2207 - SDL_RenderSetViewport behavior is different/incorrect on OpenGL renderer vs DirectX renderer
Sam Lantinga <slouken@libsdl.org>
parents: 7992
diff changeset
   923
        data->glViewport(renderer->viewport.x, renderer->viewport.y,
0eed14683885 Better fix for bug 2207 - SDL_RenderSetViewport behavior is different/incorrect on OpenGL renderer vs DirectX renderer
Sam Lantinga <slouken@libsdl.org>
parents: 7992
diff changeset
   924
                         renderer->viewport.w, renderer->viewport.h);
0eed14683885 Better fix for bug 2207 - SDL_RenderSetViewport behavior is different/incorrect on OpenGL renderer vs DirectX renderer
Sam Lantinga <slouken@libsdl.org>
parents: 7992
diff changeset
   925
    } else {
0eed14683885 Better fix for bug 2207 - SDL_RenderSetViewport behavior is different/incorrect on OpenGL renderer vs DirectX renderer
Sam Lantinga <slouken@libsdl.org>
parents: 7992
diff changeset
   926
        int w, h;
0eed14683885 Better fix for bug 2207 - SDL_RenderSetViewport behavior is different/incorrect on OpenGL renderer vs DirectX renderer
Sam Lantinga <slouken@libsdl.org>
parents: 7992
diff changeset
   927
0eed14683885 Better fix for bug 2207 - SDL_RenderSetViewport behavior is different/incorrect on OpenGL renderer vs DirectX renderer
Sam Lantinga <slouken@libsdl.org>
parents: 7992
diff changeset
   928
        SDL_GetRendererOutputSize(renderer, &w, &h);
0eed14683885 Better fix for bug 2207 - SDL_RenderSetViewport behavior is different/incorrect on OpenGL renderer vs DirectX renderer
Sam Lantinga <slouken@libsdl.org>
parents: 7992
diff changeset
   929
        data->glViewport(renderer->viewport.x, (h - renderer->viewport.y - renderer->viewport.h),
0eed14683885 Better fix for bug 2207 - SDL_RenderSetViewport behavior is different/incorrect on OpenGL renderer vs DirectX renderer
Sam Lantinga <slouken@libsdl.org>
parents: 7992
diff changeset
   930
                         renderer->viewport.w, renderer->viewport.h);
0eed14683885 Better fix for bug 2207 - SDL_RenderSetViewport behavior is different/incorrect on OpenGL renderer vs DirectX renderer
Sam Lantinga <slouken@libsdl.org>
parents: 7992
diff changeset
   931
    }
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   932
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   933
    data->glMatrixMode(GL_PROJECTION);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   934
    data->glLoadIdentity();
7239
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7198
diff changeset
   935
    if (renderer->viewport.w && renderer->viewport.h) {
7995
0eed14683885 Better fix for bug 2207 - SDL_RenderSetViewport behavior is different/incorrect on OpenGL renderer vs DirectX renderer
Sam Lantinga <slouken@libsdl.org>
parents: 7992
diff changeset
   936
        if (renderer->target) {
7239
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7198
diff changeset
   937
            data->glOrtho((GLdouble) 0,
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7198
diff changeset
   938
                          (GLdouble) renderer->viewport.w,
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7198
diff changeset
   939
                          (GLdouble) 0,
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7198
diff changeset
   940
                          (GLdouble) renderer->viewport.h,
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7198
diff changeset
   941
                           0.0, 1.0);
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7198
diff changeset
   942
        } else {
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7198
diff changeset
   943
            data->glOrtho((GLdouble) 0,
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7198
diff changeset
   944
                          (GLdouble) renderer->viewport.w,
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7198
diff changeset
   945
                          (GLdouble) renderer->viewport.h,
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7198
diff changeset
   946
                          (GLdouble) 0,
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7198
diff changeset
   947
                           0.0, 1.0);
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7198
diff changeset
   948
        }
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   949
    }
7194
987fb567bba9 Fixed bug 1837 - Use error extension instead of glGetError()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   950
    return GL_CheckError("", renderer);
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   951
}
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   952
7141
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7091
diff changeset
   953
static int
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7091
diff changeset
   954
GL_UpdateClipRect(SDL_Renderer * renderer)
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7091
diff changeset
   955
{
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7091
diff changeset
   956
    const SDL_Rect *rect = &renderer->clip_rect;
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7091
diff changeset
   957
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7091
diff changeset
   958
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7091
diff changeset
   959
    if (!SDL_RectEmpty(rect)) {
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7091
diff changeset
   960
        data->glEnable(GL_SCISSOR_TEST);
7171
929700967727 Fixed building on Visual Studio.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7169
diff changeset
   961
        data->glScissor(rect->x, renderer->viewport.h - rect->y - rect->h, rect->w, rect->h);
7141
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7091
diff changeset
   962
    } else {
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7091
diff changeset
   963
        data->glDisable(GL_SCISSOR_TEST);
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7091
diff changeset
   964
    }
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7091
diff changeset
   965
    return 0;
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7091
diff changeset
   966
}
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7091
diff changeset
   967
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   968
static void
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   969
GL_SetShader(GL_RenderData * data, GL_Shader shader)
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   970
{
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   971
    if (data->shaders && shader != data->current.shader) {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   972
        GL_SelectShader(data->shaders, shader);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   973
        data->current.shader = shader;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   974
    }
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   975
}
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   976
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   977
static void
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   978
GL_SetColor(GL_RenderData * data, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   979
{
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   980
    Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   981
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   982
    if (color != data->current.color) {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   983
        data->glColor4f((GLfloat) r * inv255f,
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   984
                        (GLfloat) g * inv255f,
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   985
                        (GLfloat) b * inv255f,
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   986
                        (GLfloat) a * inv255f);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   987
        data->current.color = color;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   988
    }
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   989
}
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   990
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   991
static void
5140
e743b9c3f6d6 Making the API simpler, the blend modes are "none, blend, add" and are supported by all renderers.
Sam Lantinga <slouken@libsdl.org>
parents: 5138
diff changeset
   992
GL_SetBlendMode(GL_RenderData * data, int blendMode)
2936
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   993
{
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   994
    if (blendMode != data->current.blendMode) {
2936
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   995
        switch (blendMode) {
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   996
        case SDL_BLENDMODE_NONE:
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   997
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   998
            data->glDisable(GL_BLEND);
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   999
            break;
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
  1000
        case SDL_BLENDMODE_BLEND:
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
  1001
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
  1002
            data->glEnable(GL_BLEND);
7502
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7486
diff changeset
  1003
            data->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
2936
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
  1004
            break;
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
  1005
        case SDL_BLENDMODE_ADD:
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
  1006
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
  1007
            data->glEnable(GL_BLEND);
7502
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7486
diff changeset
  1008
            data->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
2936
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
  1009
            break;
5184
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5166
diff changeset
  1010
        case SDL_BLENDMODE_MOD:
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5166
diff changeset
  1011
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lant