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