Navigation Menu

Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
1464 lines (1318 loc) · 47.8 KB

SDL_renderer_gl.c

File metadata and controls

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