annotate src/render/opengles2/SDL_render_gles2.c @ 5205:1f2b17f42fd0

Removed extra unneeded pixel formats, added support for VSYNC on other platforms besides Zune HD.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 06 Feb 2011 00:48:16 -0800
parents 523409574510
children daa5463466c5
rev   line source
5204
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1 /*
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
3 Copyright (C) 2010 itsnotabigtruck.
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
4
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
5 Permission is hereby granted, free of charge, to any person obtaining a
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
6 copy of this software and associated documentation files (the "Software"),
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
7 to deal in the Software without restriction, including without limitation
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
9 and/or sell copies of the Software, and to permit persons to whom the
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
10 Software is furnished to do so, subject to the following conditions:
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
11
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
12 The above copyright notice and this permission notice shall be included in
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
13 all copies or substantial portions of the Software.
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
14
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
20 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
21 DEALINGS IN THE SOFTWARE.
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
22 */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
23
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
24 #include "SDL_config.h"
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
26 #if SDL_VIDEO_RENDER_OGL_ES2
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
27
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
28 #ifdef __IPHONEOS__
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
29 #include <OpenGLES/ES2/gl.h>
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
30 #include <OpenGLES/ES2/glext.h>
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
31 #else
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
32 #include <GLES2/gl2.h>
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
33 #include <GLES2/gl2ext.h>
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
34 #endif
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
35 #include "../SDL_sysrender.h"
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
36 #include "SDL_shaders_gles2.h"
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
37
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 * Bootstrap data *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
40 *************************************************************************************************/
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 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
43
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
44 SDL_RenderDriver GLES2_RenderDriver = {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
45 GLES2_CreateRenderer,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
46 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
47 "opengles2",
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
48 (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
5205
1f2b17f42fd0 Removed extra unneeded pixel formats, added support for VSYNC on other platforms besides Zune HD.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
49 1,
1f2b17f42fd0 Removed extra unneeded pixel formats, added support for VSYNC on other platforms besides Zune HD.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
50 {SDL_PIXELFORMAT_ABGR8888},
5204
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
51 0,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
52 0
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
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 * Context structures *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
58 *************************************************************************************************/
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
59
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
60 typedef struct GLES2_TextureData
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
61 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
62 GLenum texture;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
63 GLenum texture_type;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
64 GLenum pixel_format;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
65 GLenum pixel_type;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
66 void *pixel_data;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
67 size_t pitch;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
68 } GLES2_TextureData;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
69
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
70 typedef struct GLES2_ShaderCacheEntry
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
71 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
72 GLuint id;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
73 GLES2_ShaderType type;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
74 const GLES2_ShaderInstance *instance;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
75 int references;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
76 struct GLES2_ShaderCacheEntry *prev;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
77 struct GLES2_ShaderCacheEntry *next;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
78 } 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 typedef struct GLES2_ShaderCache
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
81 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
82 int count;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
83 GLES2_ShaderCacheEntry *head;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
84 } GLES2_ShaderCache;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
85
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
86 typedef struct GLES2_ProgramCacheEntry
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 GLuint id;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
89 SDL_BlendMode blend_mode;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
90 GLES2_ShaderCacheEntry *vertex_shader;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
91 GLES2_ShaderCacheEntry *fragment_shader;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
92 GLuint uniform_locations[16];
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
93 struct GLES2_ProgramCacheEntry *prev;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
94 struct GLES2_ProgramCacheEntry *next;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
95 } GLES2_ProgramCacheEntry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
96
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
97 typedef struct GLES2_ProgramCache
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
98 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
99 int count;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
100 GLES2_ProgramCacheEntry *head;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
101 GLES2_ProgramCacheEntry *tail;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
102 } GLES2_ProgramCache;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
103
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
104 typedef enum
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
105 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
106 GLES2_ATTRIBUTE_POSITION = 0,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
107 GLES2_ATTRIBUTE_TEXCOORD = 1
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
108 } GLES2_Attribute;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
109
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
110 typedef enum
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 GLES2_UNIFORM_PROJECTION,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
113 GLES2_UNIFORM_TEXTURE,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
114 GLES2_UNIFORM_MODULATION,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
115 GLES2_UNIFORM_COLOR,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
116 GLES2_UNIFORM_COLORTABLE
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
117 } GLES2_Uniform;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
118
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
119 typedef enum
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
120 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
121 GLES2_IMAGESOURCE_SOLID,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
122 GLES2_IMAGESOURCE_TEXTURE
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
123 } GLES2_ImageSource;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
124
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
125 typedef struct GLES2_DriverContext
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
126 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
127 SDL_GLContext *context;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
128 int shader_format_count;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
129 GLenum *shader_formats;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
130 GLES2_ShaderCache shader_cache;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
131 GLES2_ProgramCache program_cache;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
132 GLES2_ProgramCacheEntry *current_program;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
133 SDL_bool updateSize;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
134 } GLES2_DriverContext;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
135
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
136 #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
137
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
138 /*************************************************************************************************
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
139 * Renderer state APIs *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
140 *************************************************************************************************/
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
141
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
142 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
143 const SDL_WindowEvent *event);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
144 static int GLES2_ActivateRenderer(SDL_Renderer *renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
145 static int GLES2_DisplayModeChanged(SDL_Renderer *renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
146 static void GLES2_DestroyRenderer(SDL_Renderer *renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
147
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
148 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
149
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
150 static int
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
151 GLES2_ActivateRenderer(SDL_Renderer * renderer)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
152 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
153 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
154 SDL_Window *window = renderer->window;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
155
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
156 if (SDL_CurrentContext != rdata->context) {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
157 /* 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
158 rdata->current_program = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
159
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
160 if (SDL_GL_MakeCurrent(window, rdata->context) < 0) {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
161 return -1;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
162 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
163 SDL_CurrentContext = rdata->context;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
164 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
165 if (rdata->updateSize) {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
166 int w, h;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
167
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
168 SDL_GetWindowSize(window, &w, &h);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
169 glViewport(0, 0, w, h);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
170 rdata->updateSize = SDL_FALSE;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
171 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
172 return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
173 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
174
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
175 static void
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
176 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
177 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
178 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
179
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
180 if (event->event == SDL_WINDOWEVENT_RESIZED) {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
181 /* 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
182 SDL_CurrentContext = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
183 rdata->updateSize = SDL_TRUE;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
184 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
185 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
186
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
187 static void
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
188 GLES2_DestroyRenderer(SDL_Renderer *renderer)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
189 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
190 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
191 GLES2_ProgramCacheEntry *entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
192 GLES2_ProgramCacheEntry *next;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
193
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
194 GLES2_ActivateRenderer(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
195
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
196 /* Deallocate everything */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
197 entry = rdata->program_cache.head;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
198 while (entry)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
199 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
200 glDeleteShader(entry->vertex_shader->id);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
201 glDeleteShader(entry->fragment_shader->id);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
202 SDL_free(entry->vertex_shader);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
203 SDL_free(entry->fragment_shader);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
204 glDeleteProgram(entry->id);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
205 next = entry->next;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
206 SDL_free(entry);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
207 entry = next;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
208 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
209 SDL_GL_DeleteContext(rdata->context);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
210 SDL_free(rdata->shader_formats);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
211 SDL_free(renderer->driverdata);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
212 SDL_free(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
213 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
214
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
215 /*************************************************************************************************
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
216 * Texture APIs *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
217 *************************************************************************************************/
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
218
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
219 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
220 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
221 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
222 void **pixels, int *pitch);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
223 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
224 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
225 const void *pixels, int pitch);
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 static int
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
228 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
229 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
230 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
231 GLES2_TextureData *tdata;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
232 GLenum format;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
233 GLenum type;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
234
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
235 GLES2_ActivateRenderer(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
236
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
237 /* 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
238 switch (texture->format)
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 case SDL_PIXELFORMAT_ABGR8888:
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
241 format = GL_RGBA;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
242 type = GL_UNSIGNED_BYTE;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
243 break;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
244 default:
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
245 SDL_SetError("Texture format not supported");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
246 return -1;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
247 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
248
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
249 /* Allocate a texture struct */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
250 tdata = (GLES2_TextureData *)SDL_calloc(1, sizeof(GLES2_TextureData));
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
251 if (!tdata)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
252 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
253 SDL_OutOfMemory();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
254 return -1;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
255 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
256 tdata->texture = 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
257 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
258 tdata->pixel_format = format;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
259 tdata->pixel_type = type;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
260
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
261 /* Allocate a blob for image data */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
262 if (texture->access == SDL_TEXTUREACCESS_STREAMING)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
263 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
264 tdata->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
265 tdata->pixel_data = SDL_malloc(tdata->pitch * texture->h);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
266 if (!tdata->pixel_data)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
267 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
268 SDL_OutOfMemory();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
269 SDL_free(tdata);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
270 return -1;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
271 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
272 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
273
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
274 /* Allocate the texture */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
275 glGetError();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
276 glGenTextures(1, &tdata->texture);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
277 glActiveTexture(GL_TEXTURE0);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
278 glBindTexture(tdata->texture_type, tdata->texture);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
279 glTexParameteri(tdata->texture_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
280 glTexParameteri(tdata->texture_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
281 glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
282 glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
283 glTexImage2D(tdata->texture_type, 0, format, texture->w, texture->h, 0, format, type, NULL);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
284 if (glGetError() != GL_NO_ERROR)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
285 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
286 SDL_SetError("Texture creation failed");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
287 glDeleteTextures(1, &tdata->texture);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
288 SDL_free(tdata);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
289 return -1;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
290 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
291 texture->driverdata = tdata;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
292 return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
293 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
294
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
295 static void
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
296 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
297 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
298 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
299
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
300 GLES2_ActivateRenderer(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
301
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
302 /* Destroy the texture */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
303 if (tdata)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
304 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
305 glDeleteTextures(1, &tdata->texture);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
306 SDL_free(tdata->pixel_data);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
307 SDL_free(tdata);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
308 texture->driverdata = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
309 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
310 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
311
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
312 static int
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
313 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
314 void **pixels, int *pitch)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
315 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
316 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
317
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
318 /* 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
319 *pixels = (Uint8 *)tdata->pixel_data +
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
320 (tdata->pitch * rect->y) +
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
321 (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
322 *pitch = tdata->pitch;
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 return 0;
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 static void
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
328 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
329 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
330 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
331
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
332 GLES2_ActivateRenderer(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
333
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
334 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
335 glActiveTexture(GL_TEXTURE0);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
336 glBindTexture(tdata->texture_type, tdata->texture);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
337 glTexSubImage2D(tdata->texture_type, 0, 0, 0, texture->w, texture->h,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
338 tdata->pixel_format, tdata->pixel_type, tdata->pixel_data);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
339 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
340
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
341 static int
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
342 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
343 const void *pixels, int pitch)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
344 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
345 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
346 Uint8 *blob = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
347 Uint8 *src;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
348 int srcPitch;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
349 Uint8 *dest;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
350 int y;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
351
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
352 GLES2_ActivateRenderer(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
353
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
354 /* 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
355 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
356 return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
357
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
358 /* 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
359 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
360 src = (Uint8 *)pixels;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
361 if (pitch != srcPitch)
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 blob = (Uint8 *)SDL_malloc(srcPitch * rect->h);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
364 if (!blob)
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 SDL_OutOfMemory();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
367 return -1;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
368 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
369 src = blob;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
370 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
371 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
372 SDL_memcpy(src, pixels, srcPitch);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
373 src += srcPitch;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
374 pixels = (Uint8 *)pixels + pitch;
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 src = blob;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
377 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
378
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
379 /* Create a texture subimage with the supplied data */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
380 glGetError();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
381 glActiveTexture(GL_TEXTURE0);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
382 glBindTexture(tdata->texture_type, tdata->texture);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
383 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
384 glTexSubImage2D(tdata->texture_type,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
385 0,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
386 rect->x,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
387 rect->y,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
388 rect->w,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
389 rect->h,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
390 tdata->pixel_format,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
391 tdata->pixel_type,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
392 src);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
393 if (glGetError() != GL_NO_ERROR)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
394 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
395 SDL_SetError("Failed to update texture");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
396 return -1;
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
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
399 /* Update the (streaming) texture buffer, in one pass if possible */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
400 if (tdata->pixel_data)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
401 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
402 dest = (Uint8 *)tdata->pixel_data +
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
403 (tdata->pitch * rect->y) +
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
404 (SDL_BYTESPERPIXEL(texture->format) * rect->x);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
405 if (rect->w == texture->w)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
406 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
407 SDL_memcpy(dest, src, srcPitch * rect->h);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
408 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
409 else
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
410 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
411 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
412 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
413 SDL_memcpy(dest, src, srcPitch);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
414 src += srcPitch;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
415 dest += tdata->pitch;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
416 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
417 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
418 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
419
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
420 /* Clean up and return */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
421 SDL_free(blob);
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 /*************************************************************************************************
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
426 * Shader management functions *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
427 *************************************************************************************************/
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
428
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
429 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
430 SDL_BlendMode blendMode);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
431 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
432 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
433 GLES2_ShaderCacheEntry *vertex,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
434 GLES2_ShaderCacheEntry *fragment,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
435 SDL_BlendMode blendMode);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
436 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
437 SDL_BlendMode blendMode);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
438 static int GLES2_SetOrthographicProjection(SDL_Renderer *renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
439
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
440 static GLES2_ProgramCacheEntry *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
441 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
442 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
443 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
444 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
445 GLES2_ProgramCacheEntry *entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
446 GLES2_ShaderCacheEntry *shaderEntry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
447 GLint linkSuccessful;
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 /* 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
450 entry = rdata->program_cache.head;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
451 while (entry)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
452 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
453 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
454 break;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
455 entry = entry->next;
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 if (entry)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
458 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
459 if (rdata->program_cache.count > 1)
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 if (entry->next)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
462 entry->next->prev = entry->prev;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
463 if (entry->prev)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
464 entry->prev->next = entry->next;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
465 entry->prev = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
466 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
467 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
468 rdata->program_cache.head = entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
469 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
470 return entry;
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
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
473 /* Create a program cache entry */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
474 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
475 if (!entry)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
476 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
477 SDL_OutOfMemory();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
478 return NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
479 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
480 entry->vertex_shader = vertex;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
481 entry->fragment_shader = fragment;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
482 entry->blend_mode = blendMode;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
483
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
484 /* Create the program and link it */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
485 glGetError();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
486 entry->id = glCreateProgram();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
487 glAttachShader(entry->id, vertex->id);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
488 glAttachShader(entry->id, fragment->id);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
489 glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_POSITION, "a_position");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
490 glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_TEXCOORD, "a_texCoord");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
491 glLinkProgram(entry->id);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
492 glGetProgramiv(entry->id, GL_LINK_STATUS, &linkSuccessful);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
493 if (glGetError() != GL_NO_ERROR || !linkSuccessful)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
494 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
495 SDL_SetError("Failed to link shader program");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
496 glDeleteProgram(entry->id);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
497 SDL_free(entry);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
498 return NULL;
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
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
501 /* Predetermine locations of uniform variables */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
502 entry->uniform_locations[GLES2_UNIFORM_PROJECTION] =
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
503 glGetUniformLocation(entry->id, "u_projection");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
504 entry->uniform_locations[GLES2_UNIFORM_TEXTURE] =
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
505 glGetUniformLocation(entry->id, "u_texture");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
506 entry->uniform_locations[GLES2_UNIFORM_MODULATION] =
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
507 glGetUniformLocation(entry->id, "u_modulation");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
508 entry->uniform_locations[GLES2_UNIFORM_COLOR] =
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
509 glGetUniformLocation(entry->id, "u_color");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
510 entry->uniform_locations[GLES2_UNIFORM_COLORTABLE] =
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
511 glGetUniformLocation(entry->id, "u_colorTable");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
512
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
513 /* Cache the linked program */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
514 if (rdata->program_cache.head)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
515 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
516 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
517 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
518 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
519 else
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
520 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
521 rdata->program_cache.tail = entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
522 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
523 rdata->program_cache.head = entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
524 ++rdata->program_cache.count;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
525
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
526 /* 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
527 ++vertex->references;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
528 ++fragment->references;
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 /* 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
531 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
532 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
533 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
534 if (--shaderEntry->references <= 0)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
535 GLES2_EvictShader(renderer, shaderEntry);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
536 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
537 if (--shaderEntry->references <= 0)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
538 GLES2_EvictShader(renderer, shaderEntry);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
539 glDeleteProgram(rdata->program_cache.tail->id);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
540 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
541 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
542 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
543 --rdata->program_cache.count;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
544 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
545 return entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
546 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
547
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
548 static GLES2_ShaderCacheEntry *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
549 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
550 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
551 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
552 const GLES2_Shader *shader;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
553 const GLES2_ShaderInstance *instance = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
554 GLES2_ShaderCacheEntry *entry = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
555 GLint compileSuccessful = GL_FALSE;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
556 int i, j;
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 /* Find the corresponding shader */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
559 shader = GLES2_GetShader(type, blendMode);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
560 if (!shader)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
561 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
562 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
563 return NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
564 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
565
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
566 /* Find a matching shader instance that's supported on this hardware */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
567 for (i = 0; i < shader->instance_count; ++i)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
568 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
569 for (j = 0; j < rdata->shader_format_count; ++j)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
570 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
571 if (!shader->instances)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
572 continue;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
573 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
574 continue;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
575 instance = shader->instances[i];
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
576 break;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
577 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
578 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
579 if (!instance)
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 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
582 return NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
583 }
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 /* 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
586 entry = rdata->shader_cache.head;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
587 while (entry)
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->instance == instance)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
590 break;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
591 entry = entry->next;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
592 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
593 if (entry)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
594 return entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
595
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
596 /* Create a shader cache entry */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
597 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
598 if (!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 SDL_OutOfMemory();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
601 return NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
602 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
603 entry->type = type;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
604 entry->instance = instance;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
605
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
606 /* Compile or load the selected shader instance */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
607 glGetError();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
608 entry->id = glCreateShader(instance->type);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
609 if (instance->format == (GLenum)-1)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
610 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
611 glShaderSource(entry->id, 1, (const char **)&instance->data, &instance->length);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
612 glCompileShader(entry->id);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
613 glGetShaderiv(entry->id, GL_COMPILE_STATUS, &compileSuccessful);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
614 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
615 else
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
616 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
617 glShaderBinary(1, &entry->id, instance->format, instance->data, instance->length);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
618 compileSuccessful = GL_TRUE;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
619 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
620 if (glGetError() != GL_NO_ERROR || !compileSuccessful)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
621 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
622 SDL_SetError("Failed to load the specified shader");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
623 glDeleteShader(entry->id);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
624 SDL_free(entry);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
625 return NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
626 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
627
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
628 /* 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
629 if (rdata->shader_cache.head)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
630 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
631 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
632 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
633 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
634 rdata->shader_cache.head = entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
635 ++rdata->shader_cache.count;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
636 return entry;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
637 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
638
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
639 static void
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
640 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
641 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
642 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
643
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
644 /* 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
645 if (entry->next)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
646 entry->next->prev = entry->prev;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
647 if (entry->prev)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
648 entry->prev->next = entry->next;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
649 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
650 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
651 --rdata->shader_cache.count;
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 /* Deallocate the shader */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
654 glDeleteShader(entry->id);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
655 SDL_free(entry);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
656 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
657
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
658 static int
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
659 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
660 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
661 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
662 GLES2_ShaderCacheEntry *vertex = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
663 GLES2_ShaderCacheEntry *fragment = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
664 GLES2_ShaderType vtype, ftype;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
665 GLES2_ProgramCacheEntry *program;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
666
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
667 /* 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
668 vtype = GLES2_SHADER_VERTEX_DEFAULT;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
669 switch (source)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
670 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
671 case GLES2_IMAGESOURCE_SOLID:
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
672 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
673 break;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
674 case GLES2_IMAGESOURCE_TEXTURE:
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
675 ftype = GLES2_SHADER_FRAGMENT_TEXTURE_SRC;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
676 break;
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
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
679 /* Load the requested shaders */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
680 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
681 if (!vertex)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
682 goto fault;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
683 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
684 if (!fragment)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
685 goto fault;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
686
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
687 /* 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
688 if (rdata->current_program &&
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
689 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
690 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
691 return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
692
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
693 /* Generate a matching program */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
694 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
695 if (!program)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
696 goto fault;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
697
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
698 /* Select that program in OpenGL */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
699 glGetError();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
700 glUseProgram(program->id);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
701 if (glGetError() != GL_NO_ERROR)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
702 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
703 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
704 goto fault;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
705 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
706
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
707 /* Set the current program */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
708 rdata->current_program = program;
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 /* Activate an orthographic projection */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
711 if (GLES2_SetOrthographicProjection(renderer) < 0)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
712 goto fault;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
713
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
714 /* Clean up and return */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
715 return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
716 fault:
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
717 if (vertex && vertex->references <= 0)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
718 GLES2_EvictShader(renderer, vertex);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
719 if (fragment && fragment->references <= 0)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
720 GLES2_EvictShader(renderer, fragment);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
721 rdata->current_program = NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
722 return -1;
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
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
725 static int
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
726 GLES2_SetOrthographicProjection(SDL_Renderer *renderer)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
727 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
728 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
729 SDL_Window *window = renderer->window;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
730 int w, h;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
731 GLfloat projection[4][4];
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
732 GLuint locProjection;
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 /* Get the window width and height */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
735 SDL_GetWindowSize(window, &w, &h);
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 /* Prepare an orthographic projection */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
738 projection[0][0] = 2.0f / w;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
739 projection[0][1] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
740 projection[0][2] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
741 projection[0][3] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
742 projection[1][0] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
743 projection[1][1] = -2.0f / h;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
744 projection[1][2] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
745 projection[1][3] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
746 projection[2][0] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
747 projection[2][1] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
748 projection[2][2] = 1.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
749 projection[2][3] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
750 projection[3][0] = -1.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
751 projection[3][1] = 1.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
752 projection[3][2] = 0.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
753 projection[3][3] = 1.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
754
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
755 /* Set the projection matrix */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
756 locProjection = rdata->current_program->uniform_locations[GLES2_UNIFORM_PROJECTION];
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
757 glGetError();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
758 glUniformMatrix4fv(locProjection, 1, GL_FALSE, (GLfloat *)projection);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
759 if (glGetError() != GL_NO_ERROR)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
760 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
761 SDL_SetError("Failed to set orthographic projection");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
762 return -1;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
763 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
764 return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
765 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
766
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
767 /*************************************************************************************************
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
768 * Rendering functions *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
769 *************************************************************************************************/
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
770
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
771 static int GLES2_RenderClear(SDL_Renderer *renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
772 static int GLES2_RenderDrawPoints(SDL_Renderer *renderer, const SDL_Point *points, int count);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
773 static int GLES2_RenderDrawLines(SDL_Renderer *renderer, const SDL_Point *points, int count);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
774 static int GLES2_RenderDrawRects(SDL_Renderer *renderer, const SDL_Rect **rects, int count);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
775 static int GLES2_RenderFillRects(SDL_Renderer *renderer, const SDL_Rect **rects, int count);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
776 static int GLES2_RenderCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
777 const SDL_Rect *dstrect);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
778 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
779
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
780 static int
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
781 GLES2_RenderClear(SDL_Renderer *renderer)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
782 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
783 float r = (float)renderer->r / 255.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
784 float g = (float)renderer->g / 255.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
785 float b = (float)renderer->b / 255.0f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
786 float a = (float)renderer->a / 255.0f;
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_ActivateRenderer(renderer);
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 /* Clear the backbuffer with the selected color */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
791 glClearColor(r, g, b, a);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
792 glClear(GL_COLOR_BUFFER_BIT);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
793 return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
794 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
795
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
796 static void
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
797 GLES2_SetBlendMode(int blendMode)
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 switch (blendMode)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
800 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
801 case SDL_BLENDMODE_NONE:
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
802 default:
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
803 glDisable(GL_BLEND);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
804 break;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
805 case SDL_BLENDMODE_BLEND:
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
806 glEnable(GL_BLEND);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
807 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
808 break;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
809 case SDL_BLENDMODE_ADD:
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
810 glEnable(GL_BLEND);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
811 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
812 break;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
813 case SDL_BLENDMODE_MOD:
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
814 glEnable(GL_BLEND);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
815 glBlendFunc(GL_ZERO, GL_SRC_COLOR);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
816 break;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
817 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
818 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
819
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
820 static int
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
821 GLES2_RenderDrawPoints(SDL_Renderer *renderer, const SDL_Point *points, int count)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
822 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
823 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
824 GLfloat *vertices;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
825 SDL_BlendMode blendMode;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
826 int alpha;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
827 GLuint locColor;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
828 int idx;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
829
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
830 GLES2_ActivateRenderer(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
831
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
832 blendMode = renderer->blendMode;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
833 alpha = renderer->a;
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 /* Activate an appropriate shader and set the projection matrix */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
836 if (GLES2_SelectProgram(renderer, GLES2_IMAGESOURCE_SOLID, blendMode) < 0)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
837 return -1;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
838
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
839 /* Select the color to draw with */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
840 locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
841 glGetError();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
842 glUniform4f(locColor,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
843 renderer->r / 255.0f,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
844 renderer->g / 255.0f,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
845 renderer->b / 255.0f,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
846 alpha / 255.0f);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
847
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
848 /* Configure the correct blend mode */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
849 GLES2_SetBlendMode(blendMode);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
850
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
851 /* Emit the specified vertices as points */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
852 vertices = SDL_stack_alloc(GLfloat, count * 2);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
853 for (idx = 0; idx < count; ++idx)
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 GLfloat x = (GLfloat)points[idx].x + 0.5f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
856 GLfloat y = (GLfloat)points[idx].y + 0.5f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
857
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
858 vertices[idx * 2] = x;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
859 vertices[(idx * 2) + 1] = y;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
860 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
861 glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
862 glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
863 glDrawArrays(GL_POINTS, 0, count);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
864 glDisableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
865 SDL_stack_free(vertices);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
866 if (glGetError() != GL_NO_ERROR)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
867 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
868 SDL_SetError("Failed to render lines");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
869 return -1;
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 return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
872 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
873
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
874 static int
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
875 GLES2_RenderDrawLines(SDL_Renderer *renderer, const SDL_Point *points, int count)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
876 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
877 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
878 GLfloat *vertices;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
879 SDL_BlendMode blendMode;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
880 int alpha;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
881 GLuint locColor;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
882 int idx;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
883
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
884 GLES2_ActivateRenderer(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
885
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
886 blendMode = renderer->blendMode;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
887 alpha = renderer->a;
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 /* Activate an appropriate shader and set the projection matrix */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
890 if (GLES2_SelectProgram(renderer, GLES2_IMAGESOURCE_SOLID, blendMode) < 0)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
891 return -1;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
892
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
893 /* Select the color to draw with */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
894 locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
895 glGetError();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
896 glUniform4f(locColor,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
897 renderer->r / 255.0f,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
898 renderer->g / 255.0f,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
899 renderer->b / 255.0f,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
900 alpha / 255.0f);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
901
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
902 /* Configure the correct blend mode */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
903 GLES2_SetBlendMode(blendMode);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
904
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
905 /* Emit a line strip including the specified vertices */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
906 vertices = SDL_stack_alloc(GLfloat, count * 2);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
907 for (idx = 0; idx < count; ++idx)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
908 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
909 GLfloat x = (GLfloat)points[idx].x + 0.5f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
910 GLfloat y = (GLfloat)points[idx].y + 0.5f;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
911
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
912 vertices[idx * 2] = x;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
913 vertices[(idx * 2) + 1] = y;
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 glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
916 glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
917 glDrawArrays(GL_LINE_STRIP, 0, count);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
918 glDisableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
919 SDL_stack_free(vertices);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
920 if (glGetError() != GL_NO_ERROR)
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 SDL_SetError("Failed to render lines");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
923 return -1;
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 return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
926 }
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 static int
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
929 GLES2_RenderFillRects(SDL_Renderer *renderer, const SDL_Rect **rects, int count)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
930 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
931 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
932 GLfloat vertices[8];
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
933 SDL_BlendMode blendMode;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
934 int alpha;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
935 GLuint locColor;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
936 int idx;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
937
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
938 GLES2_ActivateRenderer(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
939
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
940 blendMode = renderer->blendMode;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
941 alpha = renderer->a;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
942
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
943 /* Activate an appropriate shader and set the projection matrix */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
944 if (GLES2_SelectProgram(renderer, GLES2_IMAGESOURCE_SOLID, blendMode) < 0)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
945 return -1;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
946
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
947 /* Select the color to draw with */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
948 locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
949 glGetError();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
950 glUniform4f(locColor,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
951 renderer->r / 255.0f,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
952 renderer->g / 255.0f,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
953 renderer->b / 255.0f,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
954 alpha / 255.0f);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
955
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
956 /* Configure the correct blend mode */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
957 GLES2_SetBlendMode(blendMode);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
958
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
959 /* Emit a line loop for each rectangle */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
960 glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
961 for (idx = 0; idx < count; ++idx)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
962 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
963 GLfloat xMin = (GLfloat)rects[idx]->x;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
964 GLfloat xMax = (GLfloat)(rects[idx]->x + rects[idx]->w);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
965 GLfloat yMin = (GLfloat)rects[idx]->y;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
966 GLfloat yMax = (GLfloat)(rects[idx]->y + rects[idx]->h);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
967
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
968 vertices[0] = xMin;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
969 vertices[1] = yMin;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
970 vertices[2] = xMax;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
971 vertices[3] = yMin;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
972 vertices[4] = xMin;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
973 vertices[5] = yMax;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
974 vertices[6] = xMax;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
975 vertices[7] = yMax;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
976 glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
977 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
978 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
979 glDisableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
980 if (glGetError() != GL_NO_ERROR)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
981 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
982 SDL_SetError("Failed to render lines");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
983 return -1;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
984 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
985 return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
986 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
987
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
988 static int
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
989 GLES2_RenderCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
990 const SDL_Rect *dstrect)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
991 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
992 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
993 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
994 GLES2_ImageSource sourceType;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
995 SDL_BlendMode blendMode;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
996 int alpha;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
997 GLfloat vertices[8];
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
998 GLfloat texCoords[8];
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
999 GLuint locTexture;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1000 GLuint locModulation;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1001 GLuint locColorTable;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1002
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1003 GLES2_ActivateRenderer(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1004
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1005 /* Activate an appropriate shader and set the projection matrix */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1006 blendMode = texture->blendMode;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1007 alpha = texture->a;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1008 sourceType = GLES2_IMAGESOURCE_TEXTURE;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1009 if (GLES2_SelectProgram(renderer, sourceType, blendMode) < 0)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1010 return -1;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1011
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1012 /* Select the target texture */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1013 locTexture = rdata->current_program->uniform_locations[GLES2_UNIFORM_TEXTURE];
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1014 glGetError();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1015 glActiveTexture(GL_TEXTURE0);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1016 glBindTexture(tdata->texture_type, tdata->texture);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1017 glUniform1i(locTexture, 0);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1018
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1019 /* Configure texture blending */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1020 GLES2_SetBlendMode(blendMode);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1021
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1022 /* Configure color modulation */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1023 locModulation = rdata->current_program->uniform_locations[GLES2_UNIFORM_MODULATION];
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1024 glUniform4f(locModulation,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1025 texture->r / 255.0f,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1026 texture->g / 255.0f,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1027 texture->b / 255.0f,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1028 alpha / 255.0f);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1029
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1030 /* Emit the textured quad */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1031 glEnableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1032 glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1033 vertices[0] = (GLfloat)dstrect->x;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1034 vertices[1] = (GLfloat)dstrect->y;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1035 vertices[2] = (GLfloat)(dstrect->x + dstrect->w);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1036 vertices[3] = (GLfloat)dstrect->y;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1037 vertices[4] = (GLfloat)dstrect->x;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1038 vertices[5] = (GLfloat)(dstrect->y + dstrect->h);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1039 vertices[6] = (GLfloat)(dstrect->x + dstrect->w);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1040 vertices[7] = (GLfloat)(dstrect->y + dstrect->h);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1041 glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1042 texCoords[0] = srcrect->x / (GLfloat)texture->w;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1043 texCoords[1] = srcrect->y / (GLfloat)texture->h;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1044 texCoords[2] = (srcrect->x + srcrect->w) / (GLfloat)texture->w;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1045 texCoords[3] = srcrect->y / (GLfloat)texture->h;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1046 texCoords[4] = srcrect->x / (GLfloat)texture->w;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1047 texCoords[5] = (srcrect->y + srcrect->h) / (GLfloat)texture->h;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1048 texCoords[6] = (srcrect->x + srcrect->w) / (GLfloat)texture->w;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1049 texCoords[7] = (srcrect->y + srcrect->h) / (GLfloat)texture->h;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1050 glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, texCoords);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1051 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1052 glDisableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1053 glDisableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1054 if (glGetError() != GL_NO_ERROR)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1055 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1056 SDL_SetError("Failed to render texture");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1057 return -1;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1058 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1059 return 0;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1060 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1061
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1062 static void
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1063 GLES2_RenderPresent(SDL_Renderer *renderer)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1064 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1065 GLES2_ActivateRenderer(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1066
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1067 /* Tell the video driver to swap buffers */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1068 SDL_GL_SwapWindow(renderer->window);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1069 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1070
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1071 /*************************************************************************************************
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1072 * Renderer instantiation *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1073 *************************************************************************************************/
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1074
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1075 #define GL_NVIDIA_PLATFORM_BINARY_NV 0x890B
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1076
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1077 static SDL_Renderer *
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1078 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
1079 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1080 SDL_Renderer *renderer;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1081 GLES2_DriverContext *rdata;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1082 GLint nFormats;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1083 #ifndef ZUNE_HD
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1084 GLboolean hasCompiler;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1085 #endif
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1086
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1087 /* Create the renderer struct */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1088 renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(SDL_Renderer));
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1089 rdata = (GLES2_DriverContext *)SDL_calloc(1, sizeof(GLES2_DriverContext));
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1090 if (!renderer)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1091 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1092 SDL_OutOfMemory();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1093 SDL_free(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1094 SDL_free(rdata);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1095 return NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1096 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1097 renderer->info = GLES2_RenderDriver.info;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1098 renderer->window = window;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1099 renderer->driverdata = rdata;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1100
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1101 /* Create the GL context */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1102 rdata->context = SDL_GL_CreateContext(window);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1103 if (!rdata->context)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1104 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1105 SDL_free(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1106 SDL_free(rdata);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1107 return NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1108 }
5205
1f2b17f42fd0 Removed extra unneeded pixel formats, added support for VSYNC on other platforms besides Zune HD.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
1109 if (SDL_GL_MakeCurrent(window, rdata->context) < 0) {
1f2b17f42fd0 Removed extra unneeded pixel formats, added support for VSYNC on other platforms besides Zune HD.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
1110 SDL_free(renderer);
1f2b17f42fd0 Removed extra unneeded pixel formats, added support for VSYNC on other platforms besides Zune HD.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
1111 SDL_free(rdata);
1f2b17f42fd0 Removed extra unneeded pixel formats, added support for VSYNC on other platforms besides Zune HD.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
1112 return NULL;
1f2b17f42fd0 Removed extra unneeded pixel formats, added support for VSYNC on other platforms besides Zune HD.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
1113 }
1f2b17f42fd0 Removed extra unneeded pixel formats, added support for VSYNC on other platforms besides Zune HD.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
1114
1f2b17f42fd0 Removed extra unneeded pixel formats, added support for VSYNC on other platforms besides Zune HD.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
1115 if (flags & SDL_RENDERER_PRESENTVSYNC) {
1f2b17f42fd0 Removed extra unneeded pixel formats, added support for VSYNC on other platforms besides Zune HD.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
1116 SDL_GL_SetSwapInterval(1);
1f2b17f42fd0 Removed extra unneeded pixel formats, added support for VSYNC on other platforms besides Zune HD.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
1117 } else {
1f2b17f42fd0 Removed extra unneeded pixel formats, added support for VSYNC on other platforms besides Zune HD.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
1118 SDL_GL_SetSwapInterval(0);
1f2b17f42fd0 Removed extra unneeded pixel formats, added support for VSYNC on other platforms besides Zune HD.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
1119 }
1f2b17f42fd0 Removed extra unneeded pixel formats, added support for VSYNC on other platforms besides Zune HD.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
1120 if (SDL_GL_GetSwapInterval() > 0) {
1f2b17f42fd0 Removed extra unneeded pixel formats, added support for VSYNC on other platforms besides Zune HD.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
1121 renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
1f2b17f42fd0 Removed extra unneeded pixel formats, added support for VSYNC on other platforms besides Zune HD.
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
1122 }
5204
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1123
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1124 /* Determine supported shader formats */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1125 /* HACK: glGetInteger is broken on the Zune HD's compositor, so we just hardcode this */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1126 glGetError();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1127 #ifdef ZUNE_HD
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1128 nFormats = 1;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1129 #else /* !ZUNE_HD */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1130 glGetIntegerv(GL_NUM_SHADER_BINARY_FORMATS, &nFormats);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1131 glGetBooleanv(GL_SHADER_COMPILER, &hasCompiler);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1132 if (hasCompiler)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1133 ++nFormats;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1134 #endif /* ZUNE_HD */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1135 rdata->shader_formats = (GLenum *)SDL_calloc(nFormats, sizeof(GLenum));
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1136 if (!rdata->shader_formats)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1137 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1138 SDL_OutOfMemory();
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1139 SDL_free(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1140 SDL_free(rdata);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1141 return NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1142 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1143 rdata->shader_format_count = nFormats;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1144 #ifdef ZUNE_HD
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1145 rdata->shader_formats[0] = GL_NVIDIA_PLATFORM_BINARY_NV;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1146 #else /* !ZUNE_HD */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1147 glGetIntegerv(GL_SHADER_BINARY_FORMATS, (GLint *)rdata->shader_formats);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1148 if (glGetError() != GL_NO_ERROR)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1149 {
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1150 SDL_SetError("Failed to query supported shader formats");
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1151 SDL_free(renderer);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1152 SDL_free(rdata->shader_formats);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1153 SDL_free(rdata);
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1154 return NULL;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1155 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1156 if (hasCompiler)
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1157 rdata->shader_formats[nFormats - 1] = (GLenum)-1;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1158 #endif /* ZUNE_HD */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1159
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1160 /* Populate the function pointers for the module */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1161 renderer->WindowEvent = &GLES2_WindowEvent;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1162 renderer->CreateTexture = &GLES2_CreateTexture;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1163 renderer->UpdateTexture = &GLES2_UpdateTexture;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1164 renderer->LockTexture = &GLES2_LockTexture;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1165 renderer->UnlockTexture = &GLES2_UnlockTexture;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1166 renderer->RenderClear = &GLES2_RenderClear;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1167 renderer->RenderDrawPoints = &GLES2_RenderDrawPoints;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1168 renderer->RenderDrawLines = &GLES2_RenderDrawLines;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1169 renderer->RenderFillRects = &GLES2_RenderFillRects;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1170 renderer->RenderCopy = &GLES2_RenderCopy;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1171 renderer->RenderPresent = &GLES2_RenderPresent;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1172 renderer->DestroyTexture = &GLES2_DestroyTexture;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1173 renderer->DestroyRenderer = &GLES2_DestroyRenderer;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1174 return renderer;
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1175 }
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1176
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1177 #endif /* SDL_VIDEO_RENDER_OGL_ES2 */
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1178
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1179 /* vi: set ts=4 sw=4 expandtab: */