src/render/opengles2/SDL_render_gles2.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 17 Apr 2013 11:45:50 -0400
changeset 7075 f4a3c5a68119
parent 7037 3fedf1f25b94
child 7141 e276777b4247
permissions -rw-r--r--
Don't use glDisable(GL_TEXTURE_2D) in GLES2 renderer. Fixes Bugzilla #1799.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
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: 6825
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
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.
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
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:
5260
62d2bc792002 itsnotabigtruck has given me permission to replace the license for his contribution.
Sam Lantinga <slouken@libsdl.org>
parents: 5249
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.
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
#include "SDL_config.h"
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
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_ES2 && !SDL_RENDER_DISABLED
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
5484
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5402
diff changeset
    25
#include "SDL_hints.h"
5204
daa5463466c5 Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
Sam Lantinga <slouken@libsdl.org>
parents: 5202
diff changeset
    26
#include "SDL_opengles2.h"
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
#include "../SDL_sysrender.h"
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
#include "SDL_shaders_gles2.h"
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
6190
519380462400 X11 OpenGL ES minor corrections
Sam Lantinga <slouken@libsdl.org>
parents: 6188
diff changeset
    30
/* Used to re-create the window with OpenGL ES capability */
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
    31
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
    32
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
/*************************************************************************************************
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
 * Bootstrap data                                                                                *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
 *************************************************************************************************/
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
static SDL_Renderer *GLES2_CreateRenderer(SDL_Window *window, Uint32 flags);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
SDL_RenderDriver GLES2_RenderDriver = {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
    GLES2_CreateRenderer,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
    {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
        "opengles2",
6237
240f1bced46b Added a renderer flag to expose whether a renderer supports render to texture.
Sam Lantinga <slouken@libsdl.org>
parents: 6232
diff changeset
    43
        (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
6113
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
    44
        4,
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
    45
        {SDL_PIXELFORMAT_ABGR8888,
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
    46
        SDL_PIXELFORMAT_ARGB8888,
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
    47
        SDL_PIXELFORMAT_RGB888,
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
    48
        SDL_PIXELFORMAT_BGR888},
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
        0,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
        0
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
};
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
/*************************************************************************************************
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
 * Context structures                                                                            *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
 *************************************************************************************************/
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
    58
typedef struct GLES2_FBOList GLES2_FBOList;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
    59
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
    60
struct GLES2_FBOList
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
    61
{
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
    62
   Uint32 w, h;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
    63
   GLuint FBO;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
    64
   GLES2_FBOList *next;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
    65
};
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
    66
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
typedef struct GLES2_TextureData
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
    GLenum texture;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
    GLenum texture_type;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
    GLenum pixel_format;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
    GLenum pixel_type;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
    void *pixel_data;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
    size_t pitch;
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
    75
    GLES2_FBOList *fbo;
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
} GLES2_TextureData;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
typedef struct GLES2_ShaderCacheEntry
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
    GLuint id;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
    GLES2_ShaderType type;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
    const GLES2_ShaderInstance *instance;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
    int references;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
    struct GLES2_ShaderCacheEntry *prev;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
    struct GLES2_ShaderCacheEntry *next;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
} GLES2_ShaderCacheEntry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
typedef struct GLES2_ShaderCache
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
    int count;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    91
    GLES2_ShaderCacheEntry *head;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    92
} GLES2_ShaderCache;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    94
typedef struct GLES2_ProgramCacheEntry
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
    GLuint id;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    97
    SDL_BlendMode blend_mode;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
    GLES2_ShaderCacheEntry *vertex_shader;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
    GLES2_ShaderCacheEntry *fragment_shader;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
    GLuint uniform_locations[16];
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   101
    struct GLES2_ProgramCacheEntry *prev;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   102
    struct GLES2_ProgramCacheEntry *next;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   103
} GLES2_ProgramCacheEntry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   104
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   105
typedef struct GLES2_ProgramCache
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   106
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   107
    int count;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
    GLES2_ProgramCacheEntry *head;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
    GLES2_ProgramCacheEntry *tail;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
} GLES2_ProgramCache;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
typedef enum
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
    GLES2_ATTRIBUTE_POSITION = 0,
6320
6077a1310907 RenderCopyEx,rotation and flipping for all hardware/software backends (#1308)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6271
diff changeset
   115
    GLES2_ATTRIBUTE_TEXCOORD = 1,
6077a1310907 RenderCopyEx,rotation and flipping for all hardware/software backends (#1308)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6271
diff changeset
   116
    GLES2_ATTRIBUTE_ANGLE = 2,
6077a1310907 RenderCopyEx,rotation and flipping for all hardware/software backends (#1308)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6271
diff changeset
   117
    GLES2_ATTRIBUTE_CENTER = 3,
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
} GLES2_Attribute;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   119
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
typedef enum
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   121
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   122
    GLES2_UNIFORM_PROJECTION,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   123
    GLES2_UNIFORM_TEXTURE,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   124
    GLES2_UNIFORM_MODULATION,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   125
    GLES2_UNIFORM_COLOR,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   126
    GLES2_UNIFORM_COLORTABLE
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   127
} GLES2_Uniform;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   128
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   129
typedef enum
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   130
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   131
    GLES2_IMAGESOURCE_SOLID,
6113
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   132
    GLES2_IMAGESOURCE_TEXTURE_ABGR,
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   133
    GLES2_IMAGESOURCE_TEXTURE_ARGB,
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   134
    GLES2_IMAGESOURCE_TEXTURE_RGB,
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   135
    GLES2_IMAGESOURCE_TEXTURE_BGR
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   136
} GLES2_ImageSource;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   137
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   138
typedef struct GLES2_DriverContext
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   139
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   140
    SDL_GLContext *context;
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   141
    struct {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   142
        int blendMode;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   143
        SDL_bool tex_coords;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   144
    } current;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   145
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   146
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   147
#include "SDL_gles2funcs.h"
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   148
#undef SDL_PROC
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   149
    GLES2_FBOList *framebuffers;
6271
d1ba86bc57a5 Fixed magenta texture on iOS with OpenGL ES 2.0
Sam Lantinga <slouken@libsdl.org>
parents: 6269
diff changeset
   150
    GLuint window_framebuffer;
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   151
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   152
    int shader_format_count;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   153
    GLenum *shader_formats;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
    GLES2_ShaderCache shader_cache;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   155
    GLES2_ProgramCache program_cache;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   156
    GLES2_ProgramCacheEntry *current_program;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   157
} GLES2_DriverContext;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   158
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   159
#define GLES2_MAX_CACHED_PROGRAMS 8
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   160
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   161
/*************************************************************************************************
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   162
 * Renderer state APIs                                                                           *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   163
 *************************************************************************************************/
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   164
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5222
diff changeset
   165
static int GLES2_ActivateRenderer(SDL_Renderer *renderer);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
static void GLES2_WindowEvent(SDL_Renderer * renderer,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   167
                              const SDL_WindowEvent *event);
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   168
static int GLES2_UpdateViewport(SDL_Renderer * renderer);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   169
static void GLES2_DestroyRenderer(SDL_Renderer *renderer);
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
   170
static int GLES2_SetOrthographicProjection(SDL_Renderer *renderer);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   172
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
static SDL_GLContext SDL_CurrentContext = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   175
static int GLES2_LoadFunctions(GLES2_DriverContext * data)
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   176
{
6190
519380462400 X11 OpenGL ES minor corrections
Sam Lantinga <slouken@libsdl.org>
parents: 6188
diff changeset
   177
#if SDL_VIDEO_DRIVER_UIKIT
519380462400 X11 OpenGL ES minor corrections
Sam Lantinga <slouken@libsdl.org>
parents: 6188
diff changeset
   178
#define __SDL_NOGETPROCADDR__
519380462400 X11 OpenGL ES minor corrections
Sam Lantinga <slouken@libsdl.org>
parents: 6188
diff changeset
   179
#elif SDL_VIDEO_DRIVER_ANDROID
519380462400 X11 OpenGL ES minor corrections
Sam Lantinga <slouken@libsdl.org>
parents: 6188
diff changeset
   180
#define __SDL_NOGETPROCADDR__
519380462400 X11 OpenGL ES minor corrections
Sam Lantinga <slouken@libsdl.org>
parents: 6188
diff changeset
   181
#elif SDL_VIDEO_DRIVER_PANDORA
519380462400 X11 OpenGL ES minor corrections
Sam Lantinga <slouken@libsdl.org>
parents: 6188
diff changeset
   182
#define __SDL_NOGETPROCADDR__
519380462400 X11 OpenGL ES minor corrections
Sam Lantinga <slouken@libsdl.org>
parents: 6188
diff changeset
   183
#endif
519380462400 X11 OpenGL ES minor corrections
Sam Lantinga <slouken@libsdl.org>
parents: 6188
diff changeset
   184
519380462400 X11 OpenGL ES minor corrections
Sam Lantinga <slouken@libsdl.org>
parents: 6188
diff changeset
   185
#if defined __SDL_NOGETPROCADDR__
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   186
#define SDL_PROC(ret,func,params) data->func=func;
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   187
#else
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   188
#define SDL_PROC(ret,func,params) \
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   189
    do { \
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   190
        data->func = SDL_GL_GetProcAddress(#func); \
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   191
        if ( ! data->func ) { \
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6955
diff changeset
   192
            return SDL_SetError("Couldn't load GLES2 function %s: %s\n", #func, SDL_GetError()); \
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   193
        } \
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   194
    } while ( 0 );
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   195
#endif /* _SDL_NOGETPROCADDR_ */
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   196
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   197
#include "SDL_gles2funcs.h"
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   198
#undef SDL_PROC
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   199
    return 0;
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   200
}
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   201
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   202
GLES2_FBOList *
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   203
GLES2_GetFBO(GLES2_DriverContext *data, Uint32 w, Uint32 h)
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   204
{
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   205
   GLES2_FBOList *result = data->framebuffers;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   206
   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: 6190
diff changeset
   207
   {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   208
       result = result->next;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   209
   }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   210
   if (result == NULL)
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   211
   {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   212
       result = SDL_malloc(sizeof(GLES2_FBOList));
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   213
       result->w = w;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   214
       result->h = h;
6269
07e08cb58696 Added glGenFramebuffers() to the function pointer list
Sam Lantinga <slouken@libsdl.org>
parents: 6268
diff changeset
   215
       data->glGenFramebuffers(1, &result->FBO);
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   216
       result->next = data->framebuffers;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   217
       data->framebuffers = result;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   218
   }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   219
   return result;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   220
}
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   221
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   222
static int
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   223
GLES2_ActivateRenderer(SDL_Renderer * renderer)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   224
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   225
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   226
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   227
    if (SDL_CurrentContext != rdata->context) {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   228
        /* Null out the current program to ensure we set it again */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   229
        rdata->current_program = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   230
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   231
        if (SDL_GL_MakeCurrent(renderer->window, rdata->context) < 0) {
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   232
            return -1;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   233
        }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   234
        SDL_CurrentContext = rdata->context;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   235
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   236
        GLES2_UpdateViewport(renderer);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   237
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   238
    return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   239
}
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   240
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   241
static void
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   242
GLES2_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   243
{
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   244
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   245
6260
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   246
    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
   247
        event->event == SDL_WINDOWEVENT_SHOWN ||
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   248
        event->event == SDL_WINDOWEVENT_HIDDEN) {
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   249
        /* Rebind the context to the window area */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   250
        SDL_CurrentContext = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   251
    }
6060
59d398ac031e Fixed SDL applications being killed immediately after being backgrounded, because they were trying to draw while minimized.
Sam Lantinga <slouken@libsdl.org>
parents: 6052
diff changeset
   252
59d398ac031e Fixed SDL applications being killed immediately after being backgrounded, because they were trying to draw while minimized.
Sam Lantinga <slouken@libsdl.org>
parents: 6052
diff changeset
   253
    if (event->event == SDL_WINDOWEVENT_MINIMIZED) {
59d398ac031e Fixed SDL applications being killed immediately after being backgrounded, because they were trying to draw while minimized.
Sam Lantinga <slouken@libsdl.org>
parents: 6052
diff changeset
   254
        /* According to Apple documentation, we need to finish drawing NOW! */
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   255
        rdata->glFinish();
6060
59d398ac031e Fixed SDL applications being killed immediately after being backgrounded, because they were trying to draw while minimized.
Sam Lantinga <slouken@libsdl.org>
parents: 6052
diff changeset
   256
    }
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   257
}
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   258
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   259
static int
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   260
GLES2_UpdateViewport(SDL_Renderer * renderer)
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5222
diff changeset
   261
{
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   262
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5222
diff changeset
   263
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   264
    if (SDL_CurrentContext != rdata->context) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   265
        /* 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
   266
        return 0;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   267
    }
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5222
diff changeset
   268
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   269
    rdata->glViewport(renderer->viewport.x, renderer->viewport.y,
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   270
               renderer->viewport.w, renderer->viewport.h);
6268
3aa24ec6bc39 Fixed setting the orthographic projection when the viewport changes.
Sam Lantinga <slouken@libsdl.org>
parents: 6260
diff changeset
   271
3aa24ec6bc39 Fixed setting the orthographic projection when the viewport changes.
Sam Lantinga <slouken@libsdl.org>
parents: 6260
diff changeset
   272
    if (rdata->current_program) {
3aa24ec6bc39 Fixed setting the orthographic projection when the viewport changes.
Sam Lantinga <slouken@libsdl.org>
parents: 6260
diff changeset
   273
        GLES2_SetOrthographicProjection(renderer);
3aa24ec6bc39 Fixed setting the orthographic projection when the viewport changes.
Sam Lantinga <slouken@libsdl.org>
parents: 6260
diff changeset
   274
    }
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   275
    return 0;
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5222
diff changeset
   276
}
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5222
diff changeset
   277
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5222
diff changeset
   278
static void
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   279
GLES2_DestroyRenderer(SDL_Renderer *renderer)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   280
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   281
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   282
5209
115fff0641ee Prefer the OpenGL ES 2.0 context when it's available, make it possible to create an OpenGL 2.0 context on iPhoneOS
Sam Lantinga <slouken@libsdl.org>
parents: 5206
diff changeset
   283
    /* Deallocate everything */
115fff0641ee Prefer the OpenGL ES 2.0 context when it's available, make it possible to create an OpenGL 2.0 context on iPhoneOS
Sam Lantinga <slouken@libsdl.org>
parents: 5206
diff changeset
   284
    if (rdata) {
115fff0641ee Prefer the OpenGL ES 2.0 context when it's available, make it possible to create an OpenGL 2.0 context on iPhoneOS
Sam Lantinga <slouken@libsdl.org>
parents: 5206
diff changeset
   285
        GLES2_ActivateRenderer(renderer);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   286
6178
1fc633857667 Fixed tab spacing
Sam Lantinga <slouken@libsdl.org>
parents: 6171
diff changeset
   287
        {
1fc633857667 Fixed tab spacing
Sam Lantinga <slouken@libsdl.org>
parents: 6171
diff changeset
   288
            GLES2_ShaderCacheEntry *entry;
1fc633857667 Fixed tab spacing
Sam Lantinga <slouken@libsdl.org>
parents: 6171
diff changeset
   289
            GLES2_ShaderCacheEntry *next;
1fc633857667 Fixed tab spacing
Sam Lantinga <slouken@libsdl.org>
parents: 6171
diff changeset
   290
            entry = rdata->shader_cache.head;
1fc633857667 Fixed tab spacing
Sam Lantinga <slouken@libsdl.org>
parents: 6171
diff changeset
   291
            while (entry)
1fc633857667 Fixed tab spacing
Sam Lantinga <slouken@libsdl.org>
parents: 6171
diff changeset
   292
            {
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   293
                rdata->glDeleteShader(entry->id);
6052
263ef1203b76 Fixed double-free in the shader cache at shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   294
                next = entry->next;
263ef1203b76 Fixed double-free in the shader cache at shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   295
                SDL_free(entry);
263ef1203b76 Fixed double-free in the shader cache at shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   296
                entry = next;
6178
1fc633857667 Fixed tab spacing
Sam Lantinga <slouken@libsdl.org>
parents: 6171
diff changeset
   297
            }
1fc633857667 Fixed tab spacing
Sam Lantinga <slouken@libsdl.org>
parents: 6171
diff changeset
   298
        }
1fc633857667 Fixed tab spacing
Sam Lantinga <slouken@libsdl.org>
parents: 6171
diff changeset
   299
        {
1fc633857667 Fixed tab spacing
Sam Lantinga <slouken@libsdl.org>
parents: 6171
diff changeset
   300
            GLES2_ProgramCacheEntry *entry;
1fc633857667 Fixed tab spacing
Sam Lantinga <slouken@libsdl.org>
parents: 6171
diff changeset
   301
            GLES2_ProgramCacheEntry *next;
6052
263ef1203b76 Fixed double-free in the shader cache at shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   302
            entry = rdata->program_cache.head;
263ef1203b76 Fixed double-free in the shader cache at shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   303
            while (entry) {
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   304
                rdata->glDeleteProgram(entry->id);
6052
263ef1203b76 Fixed double-free in the shader cache at shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   305
                next = entry->next;
263ef1203b76 Fixed double-free in the shader cache at shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   306
                SDL_free(entry);
263ef1203b76 Fixed double-free in the shader cache at shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   307
                entry = next;
263ef1203b76 Fixed double-free in the shader cache at shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   308
            }
6178
1fc633857667 Fixed tab spacing
Sam Lantinga <slouken@libsdl.org>
parents: 6171
diff changeset
   309
        }
5209
115fff0641ee Prefer the OpenGL ES 2.0 context when it's available, make it possible to create an OpenGL 2.0 context on iPhoneOS
Sam Lantinga <slouken@libsdl.org>
parents: 5206
diff changeset
   310
        if (rdata->context) {
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   311
            while (rdata->framebuffers) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   312
                GLES2_FBOList *nextnode = rdata->framebuffers->next;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   313
                rdata->glDeleteFramebuffers(1, &rdata->framebuffers->FBO);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   314
                SDL_free(rdata->framebuffers);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   315
                rdata->framebuffers = nextnode;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   316
            }
5209
115fff0641ee Prefer the OpenGL ES 2.0 context when it's available, make it possible to create an OpenGL 2.0 context on iPhoneOS
Sam Lantinga <slouken@libsdl.org>
parents: 5206
diff changeset
   317
            SDL_GL_DeleteContext(rdata->context);
115fff0641ee Prefer the OpenGL ES 2.0 context when it's available, make it possible to create an OpenGL 2.0 context on iPhoneOS
Sam Lantinga <slouken@libsdl.org>
parents: 5206
diff changeset
   318
        }
115fff0641ee Prefer the OpenGL ES 2.0 context when it's available, make it possible to create an OpenGL 2.0 context on iPhoneOS
Sam Lantinga <slouken@libsdl.org>
parents: 5206
diff changeset
   319
        if (rdata->shader_formats) {
115fff0641ee Prefer the OpenGL ES 2.0 context when it's available, make it possible to create an OpenGL 2.0 context on iPhoneOS
Sam Lantinga <slouken@libsdl.org>
parents: 5206
diff changeset
   320
            SDL_free(rdata->shader_formats);
115fff0641ee Prefer the OpenGL ES 2.0 context when it's available, make it possible to create an OpenGL 2.0 context on iPhoneOS
Sam Lantinga <slouken@libsdl.org>
parents: 5206
diff changeset
   321
        }
115fff0641ee Prefer the OpenGL ES 2.0 context when it's available, make it possible to create an OpenGL 2.0 context on iPhoneOS
Sam Lantinga <slouken@libsdl.org>
parents: 5206
diff changeset
   322
        SDL_free(rdata);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   323
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   324
    SDL_free(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   325
}
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   326
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   327
/*************************************************************************************************
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   328
 * Texture APIs                                                                                  *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   329
 *************************************************************************************************/
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   330
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   331
static int GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   332
static void GLES2_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   333
static int GLES2_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   334
                             void **pixels, int *pitch);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   335
static void GLES2_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   336
static int GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   337
                               const void *pixels, int pitch);
6247
b6212690f78d Renamed SetTargetTexture() to SetRenderTarget()
Sam Lantinga <slouken@libsdl.org>
parents: 6246
diff changeset
   338
static int GLES2_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   339
5484
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5402
diff changeset
   340
static GLenum
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5402
diff changeset
   341
GetScaleQuality(void)
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5402
diff changeset
   342
{
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5402
diff changeset
   343
    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: 5402
diff changeset
   344
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5402
diff changeset
   345
    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: 5402
diff changeset
   346
        return GL_NEAREST;
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5402
diff changeset
   347
    } else {
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5402
diff changeset
   348
        return GL_LINEAR;
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5402
diff changeset
   349
    }
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5402
diff changeset
   350
}
e20c93bc9122 Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
Sam Lantinga <slouken@libsdl.org>
parents: 5402
diff changeset
   351
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   352
static int
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   353
GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   354
{
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   355
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   356
    GLES2_TextureData *tdata;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   357
    GLenum format;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   358
    GLenum type;
5503
be88d105e91c The scale mode is per texture, not per texture unit.
Sam Lantinga <slouken@libsdl.org>
parents: 5484
diff changeset
   359
    GLenum scaleMode;
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   360
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   361
    GLES2_ActivateRenderer(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   362
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   363
    /* Determine the corresponding GLES texture format params */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   364
    switch (texture->format)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   365
    {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   366
    case SDL_PIXELFORMAT_ABGR8888:
6113
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   367
    case SDL_PIXELFORMAT_ARGB8888:
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   368
    case SDL_PIXELFORMAT_BGR888:
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   369
    case SDL_PIXELFORMAT_RGB888:
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   370
        format = GL_RGBA;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   371
        type = GL_UNSIGNED_BYTE;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   372
        break;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   373
    default:
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6955
diff changeset
   374
        return SDL_SetError("Texture format not supported");
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   375
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   376
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   377
    /* Allocate a texture struct */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   378
    tdata = (GLES2_TextureData *)SDL_calloc(1, sizeof(GLES2_TextureData));
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6955
diff changeset
   379
    if (!tdata) {
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6955
diff changeset
   380
        return SDL_OutOfMemory();
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   381
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   382
    tdata->texture = 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   383
    tdata->texture_type = GL_TEXTURE_2D;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   384
    tdata->pixel_format = format;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   385
    tdata->pixel_type = type;
5503
be88d105e91c The scale mode is per texture, not per texture unit.
Sam Lantinga <slouken@libsdl.org>
parents: 5484
diff changeset
   386
    scaleMode = GetScaleQuality();
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   387
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   388
    /* Allocate a blob for image data */
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6955
diff changeset
   389
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   390
        tdata->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
5402
5d102cd8aef3 Zero streaming textures at the driver level
Sam Lantinga <slouken@libsdl.org>
parents: 5355
diff changeset
   391
        tdata->pixel_data = SDL_calloc(1, tdata->pitch * texture->h);
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6955
diff changeset
   392
        if (!tdata->pixel_data) {
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   393
            SDL_free(tdata);
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6955
diff changeset
   394
            return SDL_OutOfMemory();
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   395
        }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   396
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   397
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   398
    /* Allocate the texture */
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   399
    rdata->glGetError();
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   400
    rdata->glGenTextures(1, &tdata->texture);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   401
    rdata->glActiveTexture(GL_TEXTURE0);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   402
    rdata->glBindTexture(tdata->texture_type, tdata->texture);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   403
    rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_MIN_FILTER, scaleMode);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   404
    rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_MAG_FILTER, scaleMode);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   405
    rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   406
    rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   407
    rdata->glTexImage2D(tdata->texture_type, 0, format, texture->w, texture->h, 0, format, type, NULL);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   408
    if (rdata->glGetError() != GL_NO_ERROR)
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   409
    {
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   410
        rdata->glDeleteTextures(1, &tdata->texture);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   411
        SDL_free(tdata);
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6955
diff changeset
   412
        return SDL_SetError("Texture creation failed");
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   413
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   414
    texture->driverdata = tdata;
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   415
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   416
    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   417
       tdata->fbo = GLES2_GetFBO(renderer->driverdata, texture->w, texture->h);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   418
    } else {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   419
       tdata->fbo = NULL;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   420
    }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   421
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   422
    return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   423
}
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   424
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   425
static void
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   426
GLES2_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   427
{
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   428
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   429
    GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   430
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   431
    GLES2_ActivateRenderer(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   432
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   433
    /* Destroy the texture */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   434
    if (tdata)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   435
    {
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   436
        rdata->glDeleteTextures(1, &tdata->texture);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   437
        SDL_free(tdata->pixel_data);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   438
        SDL_free(tdata);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   439
        texture->driverdata = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   440
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   441
}
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   442
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   443
static int
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   444
GLES2_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   445
                  void **pixels, int *pitch)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   446
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   447
    GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   448
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   449
    /* Retrieve the buffer/pitch for the specified region */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   450
    *pixels = (Uint8 *)tdata->pixel_data +
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   451
              (tdata->pitch * rect->y) +
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   452
              (rect->x * SDL_BYTESPERPIXEL(texture->format));
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   453
    *pitch = tdata->pitch;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   454
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   455
    return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   456
}
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   457
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   458
static void
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   459
GLES2_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   460
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   461
    GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
5227
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   462
    SDL_Rect rect;
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   463
5227
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   464
    /* We do whole texture updates, at least for now */
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   465
    rect.x = 0;
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   466
    rect.y = 0;
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   467
    rect.w = texture->w;
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   468
    rect.h = texture->h;
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   469
    GLES2_UpdateTexture(renderer, texture, &rect, tdata->pixel_data, tdata->pitch);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   470
}
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   471
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   472
static int
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   473
GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   474
                    const void *pixels, int pitch)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   475
{
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   476
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   477
    GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   478
    Uint8 *blob = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   479
    Uint8 *src;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   480
    int srcPitch;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   481
    int y;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   482
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   483
    GLES2_ActivateRenderer(renderer);
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   484
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   485
    /* Bail out if we're supposed to update an empty rectangle */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   486
    if (rect->w <= 0 || rect->h <= 0)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   487
        return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   488
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   489
    /* Reformat the texture data into a tightly packed array */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   490
    srcPitch = rect->w * SDL_BYTESPERPIXEL(texture->format);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   491
    src = (Uint8 *)pixels;
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6955
diff changeset
   492
    if (pitch != srcPitch) {
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   493
        blob = (Uint8 *)SDL_malloc(srcPitch * rect->h);
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6955
diff changeset
   494
        if (!blob) {
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6955
diff changeset
   495
            return SDL_OutOfMemory();
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   496
        }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   497
        src = blob;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   498
        for (y = 0; y < rect->h; ++y)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   499
        {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   500
            SDL_memcpy(src, pixels, srcPitch);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   501
            src += srcPitch;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   502
            pixels = (Uint8 *)pixels + pitch;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   503
        }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   504
        src = blob;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   505
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   506
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   507
    /* Create a texture subimage with the supplied data */
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   508
    rdata->glGetError();
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   509
    rdata->glActiveTexture(GL_TEXTURE0);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   510
    rdata->glBindTexture(tdata->texture_type, tdata->texture);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   511
    rdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   512
    rdata->glTexSubImage2D(tdata->texture_type,
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   513
                    0,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   514
                    rect->x,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   515
                    rect->y,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   516
                    rect->w,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   517
                    rect->h,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   518
                    tdata->pixel_format,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   519
                    tdata->pixel_type,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   520
                    src);
5227
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   521
    if (blob) {
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   522
        SDL_free(blob);
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   523
    }
5214
b0797947dd1c You don't need to copy the pixels into the streaming texture's pixel data. This behavior is defined specifically to optimize the UpdateTexture path.
Sam Lantinga <slouken@libsdl.org>
parents: 5213
diff changeset
   524
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6955
diff changeset
   525
    if (rdata->glGetError() != GL_NO_ERROR) {
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6955
diff changeset
   526
        return SDL_SetError("Failed to update texture");
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   527
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   528
    return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   529
}
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   530
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
static int
6247
b6212690f78d Renamed SetTargetTexture() to SetRenderTarget()
Sam Lantinga <slouken@libsdl.org>
parents: 6246
diff changeset
   532
GLES2_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
   533
{
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
   534
    GLES2_DriverContext *data = (GLES2_DriverContext *) renderer->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
   535
    GLES2_TextureData *texturedata = 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
   536
    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
   537
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
   538
    if (texture == NULL) {
6271
d1ba86bc57a5 Fixed magenta texture on iOS with OpenGL ES 2.0
Sam Lantinga <slouken@libsdl.org>
parents: 6269
diff changeset
   539
        data->glBindFramebuffer(GL_FRAMEBUFFER, data->window_framebuffer);
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
   540
    } else {
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
   541
        texturedata = (GLES2_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
   542
        data->glBindFramebuffer(GL_FRAMEBUFFER, 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
   543
        /* 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
   544
        data->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texturedata->texture_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
   545
        /* 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
   546
        status = data->glCheckFramebufferStatus(GL_FRAMEBUFFER);
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
   547
        if (status != GL_FRAMEBUFFER_COMPLETE) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6955
diff changeset
   548
            return SDL_SetError("glFramebufferTexture2D() 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
   549
        }
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
   550
    }
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
   551
    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
   552
}
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
   553
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   554
/*************************************************************************************************
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   555
 * Shader management functions                                                                   *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   556
 *************************************************************************************************/
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   557
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   558
static GLES2_ShaderCacheEntry *GLES2_CacheShader(SDL_Renderer *renderer, GLES2_ShaderType type,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   559
                                                 SDL_BlendMode blendMode);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   560
static void GLES2_EvictShader(SDL_Renderer *renderer, GLES2_ShaderCacheEntry *entry);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   561
static GLES2_ProgramCacheEntry *GLES2_CacheProgram(SDL_Renderer *renderer,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   562
                                                   GLES2_ShaderCacheEntry *vertex,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   563
                                                   GLES2_ShaderCacheEntry *fragment,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   564
                                                   SDL_BlendMode blendMode);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   565
static int GLES2_SelectProgram(SDL_Renderer *renderer, GLES2_ImageSource source,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   566
                               SDL_BlendMode blendMode);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   567
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   568
static GLES2_ProgramCacheEntry *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   569
GLES2_CacheProgram(SDL_Renderer *renderer, GLES2_ShaderCacheEntry *vertex,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   570
                   GLES2_ShaderCacheEntry *fragment, SDL_BlendMode blendMode)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   571
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   572
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   573
    GLES2_ProgramCacheEntry *entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   574
    GLES2_ShaderCacheEntry *shaderEntry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   575
    GLint linkSuccessful;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   576
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   577
    /* Check if we've already cached this program */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   578
    entry = rdata->program_cache.head;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   579
    while (entry)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   580
    {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   581
        if (entry->vertex_shader == vertex && entry->fragment_shader == fragment)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   582
            break;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   583
        entry = entry->next;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   584
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   585
    if (entry)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   586
    {
6061
4746757951ac Fixed list loop caused when there are two entries in the list and the one being returned is already at the head.
Sam Lantinga <slouken@libsdl.org>
parents: 6060
diff changeset
   587
        if (rdata->program_cache.head != entry)
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   588
        {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   589
            if (entry->next)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   590
                entry->next->prev = entry->prev;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   591
            if (entry->prev)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   592
                entry->prev->next = entry->next;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   593
            entry->prev = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   594
            entry->next = rdata->program_cache.head;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   595
            rdata->program_cache.head->prev = entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   596
            rdata->program_cache.head = entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   597
        }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   598
        return entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   599
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   600
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   601
    /* Create a program cache entry */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   602
    entry = (GLES2_ProgramCacheEntry *)SDL_calloc(1, sizeof(GLES2_ProgramCacheEntry));
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   603
    if (!entry)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   604
    {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   605
        SDL_OutOfMemory();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   606
        return NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   607
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   608
    entry->vertex_shader = vertex;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   609
    entry->fragment_shader = fragment;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   610
    entry->blend_mode = blendMode;
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   611
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   612
    /* Create the program and link it */
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   613
    rdata->glGetError();
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   614
    entry->id = rdata->glCreateProgram();
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   615
    rdata->glAttachShader(entry->id, vertex->id);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   616
    rdata->glAttachShader(entry->id, fragment->id);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   617
    rdata->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_POSITION, "a_position");
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   618
    rdata->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_TEXCOORD, "a_texCoord");
6320
6077a1310907 RenderCopyEx,rotation and flipping for all hardware/software backends (#1308)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6271
diff changeset
   619
    rdata->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_ANGLE, "a_angle");
6077a1310907 RenderCopyEx,rotation and flipping for all hardware/software backends (#1308)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6271
diff changeset
   620
    rdata->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_CENTER, "a_center");
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   621
    rdata->glLinkProgram(entry->id);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   622
    rdata->glGetProgramiv(entry->id, GL_LINK_STATUS, &linkSuccessful);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   623
    if (rdata->glGetError() != GL_NO_ERROR || !linkSuccessful)
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   624
    {
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   625
        rdata->glDeleteProgram(entry->id);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   626
        SDL_free(entry);
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6955
diff changeset
   627
        SDL_SetError("Failed to link shader program");
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   628
        return NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   629
    }
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   630
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   631
    /* Predetermine locations of uniform variables */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   632
    entry->uniform_locations[GLES2_UNIFORM_PROJECTION] =
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   633
        rdata->glGetUniformLocation(entry->id, "u_projection");
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   634
    entry->uniform_locations[GLES2_UNIFORM_TEXTURE] =
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   635
        rdata->glGetUniformLocation(entry->id, "u_texture");
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   636
    entry->uniform_locations[GLES2_UNIFORM_MODULATION] =
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   637
        rdata->glGetUniformLocation(entry->id, "u_modulation");
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   638
    entry->uniform_locations[GLES2_UNIFORM_COLOR] =
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   639
        rdata->glGetUniformLocation(entry->id, "u_color");
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   640
    entry->uniform_locations[GLES2_UNIFORM_COLORTABLE] =
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   641
        rdata->glGetUniformLocation(entry->id, "u_colorTable");
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   642
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   643
    /* Cache the linked program */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   644
    if (rdata->program_cache.head)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   645
    {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   646
        entry->next = rdata->program_cache.head;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   647
        rdata->program_cache.head->prev = entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   648
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   649
    else
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   650
    {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   651
        rdata->program_cache.tail = entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   652
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   653
    rdata->program_cache.head = entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   654
    ++rdata->program_cache.count;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   655
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   656
    /* Increment the refcount of the shaders we're using */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   657
    ++vertex->references;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   658
    ++fragment->references;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   659
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   660
    /* Evict the last entry from the cache if we exceed the limit */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   661
    if (rdata->program_cache.count > GLES2_MAX_CACHED_PROGRAMS)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   662
    {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   663
        shaderEntry = rdata->program_cache.tail->vertex_shader;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   664
        if (--shaderEntry->references <= 0)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   665
            GLES2_EvictShader(renderer, shaderEntry);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   666
        shaderEntry = rdata->program_cache.tail->fragment_shader;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   667
        if (--shaderEntry->references <= 0)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   668
            GLES2_EvictShader(renderer, shaderEntry);
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   669
        rdata->glDeleteProgram(rdata->program_cache.tail->id);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   670
        rdata->program_cache.tail = rdata->program_cache.tail->prev;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   671
        SDL_free(rdata->program_cache.tail->next);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   672
        rdata->program_cache.tail->next = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   673
        --rdata->program_cache.count;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   674
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   675
    return entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   676
}
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   677
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   678
static GLES2_ShaderCacheEntry *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   679
GLES2_CacheShader(SDL_Renderer *renderer, GLES2_ShaderType type, SDL_BlendMode blendMode)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   680
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   681
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   682
    const GLES2_Shader *shader;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   683
    const GLES2_ShaderInstance *instance = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   684
    GLES2_ShaderCacheEntry *entry = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   685
    GLint compileSuccessful = GL_FALSE;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   686
    int i, j;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   687
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   688
    /* Find the corresponding shader */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   689
    shader = GLES2_GetShader(type, blendMode);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   690
    if (!shader)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   691
    {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   692
        SDL_SetError("No shader matching the requested characteristics was found");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   693
        return NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   694
    }
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   695
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   696
    /* Find a matching shader instance that's supported on this hardware */
5206
7d64745f1363 Fixed crash when binary shaders are not available.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   697
    for (i = 0; i < shader->instance_count && !instance; ++i)
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   698
    {
5206
7d64745f1363 Fixed crash when binary shaders are not available.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   699
        for (j = 0; j < rdata->shader_format_count && !instance; ++j)
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   700
        {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   701
            if (!shader->instances)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   702
                continue;
5206
7d64745f1363 Fixed crash when binary shaders are not available.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   703
            if (!shader->instances[i])
7d64745f1363 Fixed crash when binary shaders are not available.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   704
                continue;
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   705
            if (shader->instances[i]->format != rdata->shader_formats[j])
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   706
                continue;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   707
            instance = shader->instances[i];
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   708
        }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   709
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   710
    if (!instance)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   711
    {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   712
        SDL_SetError("The specified shader cannot be loaded on the current platform");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   713
        return NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   714
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   715
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   716
    /* Check if we've already cached this shader */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   717
    entry = rdata->shader_cache.head;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   718
    while (entry)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   719
    {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   720
        if (entry->instance == instance)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   721
            break;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   722
        entry = entry->next;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   723
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   724
    if (entry)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   725
        return entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   726
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   727
    /* Create a shader cache entry */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   728
    entry = (GLES2_ShaderCacheEntry *)SDL_calloc(1, sizeof(GLES2_ShaderCacheEntry));
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   729
    if (!entry)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   730
    {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   731
        SDL_OutOfMemory();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   732
        return NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   733
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   734
    entry->type = type;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   735
    entry->instance = instance;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   736
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   737
    /* Compile or load the selected shader instance */
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   738
    rdata->glGetError();
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   739
    entry->id = rdata->glCreateShader(instance->type);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   740
    if (instance->format == (GLenum)-1)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   741
    {
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   742
        rdata->glShaderSource(entry->id, 1, (const char **)&instance->data, NULL);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   743
        rdata->glCompileShader(entry->id);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   744
        rdata->glGetShaderiv(entry->id, GL_COMPILE_STATUS, &compileSuccessful);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   745
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   746
    else
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   747
    {
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   748
        rdata->glShaderBinary(1, &entry->id, instance->format, instance->data, instance->length);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   749
        compileSuccessful = GL_TRUE;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   750
    }
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   751
    if (rdata->glGetError() != GL_NO_ERROR || !compileSuccessful)
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   752
    {
5212
7b6101f3ea58 Fixed compiling source shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5209
diff changeset
   753
        char *info = NULL;
6113
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   754
        int length = 0;
5212
7b6101f3ea58 Fixed compiling source shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5209
diff changeset
   755
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   756
        rdata->glGetShaderiv(entry->id, GL_INFO_LOG_LENGTH, &length);
5212
7b6101f3ea58 Fixed compiling source shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5209
diff changeset
   757
        if (length > 0) {
7b6101f3ea58 Fixed compiling source shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5209
diff changeset
   758
            info = SDL_stack_alloc(char, length);
7b6101f3ea58 Fixed compiling source shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5209
diff changeset
   759
            if (info) {
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   760
                rdata->glGetShaderInfoLog(entry->id, length, &length, info);
5212
7b6101f3ea58 Fixed compiling source shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5209
diff changeset
   761
            }
7b6101f3ea58 Fixed compiling source shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5209
diff changeset
   762
        }
7b6101f3ea58 Fixed compiling source shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5209
diff changeset
   763
        if (info) {
7b6101f3ea58 Fixed compiling source shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5209
diff changeset
   764
            SDL_SetError("Failed to load the shader: %s", info);
7b6101f3ea58 Fixed compiling source shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5209
diff changeset
   765
            SDL_stack_free(info);
7b6101f3ea58 Fixed compiling source shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5209
diff changeset
   766
        } else {
7b6101f3ea58 Fixed compiling source shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5209
diff changeset
   767
            SDL_SetError("Failed to load the shader");
7b6101f3ea58 Fixed compiling source shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5209
diff changeset
   768
        }
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   769
        rdata->glDeleteShader(entry->id);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   770
        SDL_free(entry);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   771
        return NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   772
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   773
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   774
    /* Link the shader entry in at the front of the cache */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   775
    if (rdata->shader_cache.head)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   776
    {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   777
        entry->next = rdata->shader_cache.head;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   778
        rdata->shader_cache.head->prev = entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   779
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   780
    rdata->shader_cache.head = entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   781
    ++rdata->shader_cache.count;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   782
    return entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   783
}
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   784
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   785
static void
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   786
GLES2_EvictShader(SDL_Renderer *renderer, GLES2_ShaderCacheEntry *entry)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   787
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   788
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   789
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   790
    /* Unlink the shader from the cache */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   791
    if (entry->next)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   792
        entry->next->prev = entry->prev;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   793
    if (entry->prev)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   794
        entry->prev->next = entry->next;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   795
    if (rdata->shader_cache.head == entry)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   796
        rdata->shader_cache.head = entry->next;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   797
    --rdata->shader_cache.count;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   798
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   799
    /* Deallocate the shader */
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   800
    rdata->glDeleteShader(entry->id);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   801
    SDL_free(entry);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   802
}
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   803
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   804
static int
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   805
GLES2_SelectProgram(SDL_Renderer *renderer, GLES2_ImageSource source, SDL_BlendMode blendMode)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   806
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   807
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   808
    GLES2_ShaderCacheEntry *vertex = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   809
    GLES2_ShaderCacheEntry *fragment = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   810
    GLES2_ShaderType vtype, ftype;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   811
    GLES2_ProgramCacheEntry *program;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   812
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   813
    /* Select an appropriate shader pair for the specified modes */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   814
    vtype = GLES2_SHADER_VERTEX_DEFAULT;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   815
    switch (source)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   816
    {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   817
    case GLES2_IMAGESOURCE_SOLID:
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   818
        ftype = GLES2_SHADER_FRAGMENT_SOLID_SRC;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   819
        break;
6113
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   820
    case GLES2_IMAGESOURCE_TEXTURE_ABGR:
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   821
        ftype = GLES2_SHADER_FRAGMENT_TEXTURE_ABGR_SRC;
6341
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
   822
        break;
6113
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   823
    case GLES2_IMAGESOURCE_TEXTURE_ARGB:
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   824
        ftype = GLES2_SHADER_FRAGMENT_TEXTURE_ARGB_SRC;
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   825
        break;
6113
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   826
    case GLES2_IMAGESOURCE_TEXTURE_RGB:
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   827
        ftype = GLES2_SHADER_FRAGMENT_TEXTURE_RGB_SRC;
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   828
        break;
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   829
    case GLES2_IMAGESOURCE_TEXTURE_BGR:
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   830
        ftype = GLES2_SHADER_FRAGMENT_TEXTURE_BGR_SRC;
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   831
        break;
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   832
    default:
64742b8c8eb3 Fixed bug 1335 - Added support for different pixel formats in OpenGL ES 2 renderer
Sam Lantinga <slouken@libsdl.org>
parents: 6076
diff changeset
   833
        goto fault;
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   834
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   835
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   836
    /* Load the requested shaders */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   837
    vertex = GLES2_CacheShader(renderer, vtype, blendMode);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   838
    if (!vertex)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   839
        goto fault;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   840
    fragment = GLES2_CacheShader(renderer, ftype, blendMode);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   841
    if (!fragment)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   842
        goto fault;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   843
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   844
    /* Check if we need to change programs at all */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   845
    if (rdata->current_program &&
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   846
        rdata->current_program->vertex_shader == vertex &&
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   847
        rdata->current_program->fragment_shader == fragment)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   848
        return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   849
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   850
    /* Generate a matching program */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   851
    program = GLES2_CacheProgram(renderer, vertex, fragment, blendMode);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   852
    if (!program)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   853
        goto fault;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   854
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   855
    /* Select that program in OpenGL */
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   856
    rdata->glGetError();
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   857
    rdata->glUseProgram(program->id);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   858
    if (rdata->glGetError() != GL_NO_ERROR)
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   859
    {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   860
        SDL_SetError("Failed to select program");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   861
        goto fault;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   862
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   863
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   864
    /* Set the current program */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   865
    rdata->current_program = program;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   866
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   867
    /* Activate an orthographic projection */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   868
    if (GLES2_SetOrthographicProjection(renderer) < 0)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   869
        goto fault;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   870
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   871
    /* Clean up and return */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   872
    return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   873
fault:
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   874
    if (vertex && vertex->references <= 0)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   875
        GLES2_EvictShader(renderer, vertex);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   876
    if (fragment && fragment->references <= 0)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   877
        GLES2_EvictShader(renderer, fragment);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   878
    rdata->current_program = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   879
    return -1;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   880
}
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   881
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   882
static int
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   883
GLES2_SetOrthographicProjection(SDL_Renderer *renderer)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   884
{
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   885
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   886
    GLfloat projection[4][4];
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   887
    GLuint locProjection;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   888
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   889
    /* Prepare an orthographic projection */
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   890
    projection[0][0] = 2.0f / renderer->viewport.w;
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   891
    projection[0][1] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   892
    projection[0][2] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   893
    projection[0][3] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   894
    projection[1][0] = 0.0f;
6341
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
   895
    if (renderer->target) {
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
   896
        projection[1][1] = 2.0f / renderer->viewport.h;
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
   897
    } else {
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
   898
        projection[1][1] = -2.0f / renderer->viewport.h;
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
   899
    }
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   900
    projection[1][2] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   901
    projection[1][3] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   902
    projection[2][0] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   903
    projection[2][1] = 0.0f;
6341
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
   904
    projection[2][2] = 0.0f;
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   905
    projection[2][3] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   906
    projection[3][0] = -1.0f;
6341
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
   907
    if (renderer->target) {
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
   908
        projection[3][1] = -1.0f;
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
   909
    } else {
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
   910
        projection[3][1] = 1.0f;
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
   911
    }
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   912
    projection[3][2] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   913
    projection[3][3] = 1.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   914
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   915
    /* Set the projection matrix */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   916
    locProjection = rdata->current_program->uniform_locations[GLES2_UNIFORM_PROJECTION];
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   917
    rdata->glGetError();
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   918
    rdata->glUniformMatrix4fv(locProjection, 1, GL_FALSE, (GLfloat *)projection);
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6955
diff changeset
   919
    if (rdata->glGetError() != GL_NO_ERROR) {
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6955
diff changeset
   920
        return SDL_SetError("Failed to set orthographic projection");
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   921
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   922
    return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   923
}
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   924
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   925
/*************************************************************************************************
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   926
 * Rendering functions                                                                           *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   927
 *************************************************************************************************/
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   928
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5222
diff changeset
   929
static const float inv255f = 1.0f / 255.0f;
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5222
diff changeset
   930
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   931
static int GLES2_RenderClear(SDL_Renderer *renderer);
6528
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6414
diff changeset
   932
static int GLES2_RenderDrawPoints(SDL_Renderer *renderer, const SDL_FPoint *points, int count);
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6414
diff changeset
   933
static int GLES2_RenderDrawLines(SDL_Renderer *renderer, const SDL_FPoint *points, int count);
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6414
diff changeset
   934
static int GLES2_RenderFillRects(SDL_Renderer *renderer, const SDL_FRect *rects, int count);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   935
static int GLES2_RenderCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect,
6528
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6414
diff changeset
   936
                            const SDL_FRect *dstrect);
6042
ff2ab1e8d516 Implemented RenderReadPixels for the OpenGL ES 2.0 renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   937
static int GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
ff2ab1e8d516 Implemented RenderReadPixels for the OpenGL ES 2.0 renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   938
                    Uint32 pixel_format, void * pixels, int pitch);
6320
6077a1310907 RenderCopyEx,rotation and flipping for all hardware/software backends (#1308)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6271
diff changeset
   939
static int GLES2_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
6528
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6414
diff changeset
   940
                         const SDL_Rect * srcrect, const SDL_FRect * dstrect,
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6414
diff changeset
   941
                         const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip);
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   942
static void GLES2_RenderPresent(SDL_Renderer *renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   943
5333
5d3a2cd4e8c9 Implemented OpenGL ES RenderClear()
Sam Lantinga <slouken@libsdl.org>
parents: 5332
diff changeset
   944
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   945
static int
5333
5d3a2cd4e8c9 Implemented OpenGL ES RenderClear()
Sam Lantinga <slouken@libsdl.org>
parents: 5332
diff changeset
   946
GLES2_RenderClear(SDL_Renderer * renderer)
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   947
{
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   948
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   949
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   950
    GLES2_ActivateRenderer(renderer);
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents: 6190
diff changeset
   951
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   952
    rdata->glClearColor((GLfloat) renderer->r * inv255f,
5333
5d3a2cd4e8c9 Implemented OpenGL ES RenderClear()
Sam Lantinga <slouken@libsdl.org>
parents: 5332
diff changeset
   953
                 (GLfloat) renderer->g * inv255f,
5d3a2cd4e8c9 Implemented OpenGL ES RenderClear()
Sam Lantinga <slouken@libsdl.org>
parents: 5332
diff changeset
   954
                 (GLfloat) renderer->b * inv255f,
5d3a2cd4e8c9 Implemented OpenGL ES RenderClear()
Sam Lantinga <slouken@libsdl.org>
parents: 5332
diff changeset
   955
                 (GLfloat) renderer->a * inv255f);
5d3a2cd4e8c9 Implemented OpenGL ES RenderClear()
Sam Lantinga <slouken@libsdl.org>
parents: 5332
diff changeset
   956
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   957
    rdata->glClear(GL_COLOR_BUFFER_BIT);
5333
5d3a2cd4e8c9 Implemented OpenGL ES RenderClear()
Sam Lantinga <slouken@libsdl.org>
parents: 5332
diff changeset
   958
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   959
    return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   960
}
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   961
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   962
static void
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   963
GLES2_SetBlendMode(GLES2_DriverContext *rdata, int blendMode)
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   964
{
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   965
    if (blendMode != rdata->current.blendMode) {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   966
        switch (blendMode) {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   967
        default:
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   968
        case SDL_BLENDMODE_NONE:
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   969
            rdata->glDisable(GL_BLEND);
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   970
            break;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   971
        case SDL_BLENDMODE_BLEND:
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   972
            rdata->glEnable(GL_BLEND);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   973
            rdata->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   974
            break;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   975
        case SDL_BLENDMODE_ADD:
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   976
            rdata->glEnable(GL_BLEND);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   977
            rdata->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   978
            break;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   979
        case SDL_BLENDMODE_MOD:
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   980
            rdata->glEnable(GL_BLEND);
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   981
            rdata->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   982
            break;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   983
        }
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   984
        rdata->current.blendMode = blendMode;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   985
    }
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   986
}
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   987
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   988
static void
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   989
GLES2_SetTexCoords(GLES2_DriverContext * rdata, SDL_bool enabled)
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   990
{
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   991
    if (enabled != rdata->current.tex_coords) {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   992
        if (enabled) {
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   993
            rdata->glEnableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   994
        } else {
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
   995
            rdata->glDisableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   996
        }
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   997
        rdata->current.tex_coords = enabled;
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   998
    }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   999
}
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1000
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1001
static int
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1002
GLES2_SetDrawingState(SDL_Renderer * renderer)
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1003
{
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1004
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1005
    int blendMode = renderer->blendMode;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1006
    GLuint locColor;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1007
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
  1008
    rdata->glGetError();
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1009
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1010
    GLES2_ActivateRenderer(renderer);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1011
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1012
    GLES2_SetBlendMode(rdata, blendMode);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1013
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1014
    GLES2_SetTexCoords(rdata, SDL_FALSE);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1015
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1016
    /* Activate an appropriate shader and set the projection matrix */
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1017
    if (GLES2_SelectProgram(renderer, GLES2_IMAGESOURCE_SOLID, blendMode) < 0)
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1018
        return -1;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1019
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1020
    /* Select the color to draw with */
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1021
    locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
6341
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
  1022
    if (renderer->target &&
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
  1023
        (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 ||
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
  1024
         renderer->target->format == SDL_PIXELFORMAT_RGB888)) {
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
  1025
        rdata->glUniform4f(locColor,
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
  1026
                    renderer->b * inv255f,
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
  1027
                    renderer->g * inv255f,
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
  1028
                    renderer->r * inv255f,
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
  1029
                    renderer->a * inv255f);
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
  1030
    } else {
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
  1031
        rdata->glUniform4f(locColor,
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
  1032
                    renderer->r * inv255f,
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
  1033
                    renderer->g * inv255f,
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
  1034
                    renderer->b * inv255f,
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
  1035
                    renderer->a * inv255f);
057bbdb514d5 Fixed orientation and color when rendering to texture
Sam Lantinga <slouken@libsdl.org>
parents: 6320
diff changeset
  1036
    }
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1037
    return 0;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1038
}
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1039
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1040
static int
6528
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6414
diff changeset
  1041
GLES2_RenderDrawPoints(SDL_Renderer *renderer, const SDL_FPoint *points, int count)
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1042
{
6188
e82023802002 Fixed bug 1242 - PATCH: Improve support for OpenGL ES under X11
Sam Lantinga <slouken@libsdl.org>
parents: 6178
diff changeset
  1043
    GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1044
    GLfloat *vertices;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1045
    int idx;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1046
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
  1047
    if (GLES2_SetDrawingState(renderer) < 0) {