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

Latest commit

 

History

History
1288 lines (1137 loc) · 40.8 KB

SDL_renderer_gl.c

File metadata and controls

1288 lines (1137 loc) · 40.8 KB
 
Jul 19, 2006
Jul 19, 2006
1
2
/*
SDL - Simple DirectMedia Layer
Jan 24, 2010
Jan 24, 2010
3
Copyright (C) 1997-2010 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);
Feb 2, 2011
Feb 2, 2011
69
70
static void GL_WindowEvent(SDL_Renderer * renderer,
const SDL_WindowEvent *event);
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
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
79
80
const SDL_Rect * rect, int markDirty, void **pixels,
int *pitch);
Jul 19, 2006
Jul 19, 2006
81
82
83
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 23, 2009
Dec 23, 2009
84
85
86
87
88
89
90
static int GL_RenderClear(SDL_Renderer * renderer);
static int GL_RenderDrawPoints(SDL_Renderer * renderer,
const SDL_Point * points, int count);
static int GL_RenderDrawLines(SDL_Renderer * renderer,
const SDL_Point * points, int count);
static int GL_RenderFillRects(SDL_Renderer * renderer,
const SDL_Rect ** rects, int count);
Jul 19, 2006
Jul 19, 2006
91
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
Aug 28, 2006
Aug 28, 2006
92
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
Nov 15, 2009
Nov 15, 2009
93
static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Nov 16, 2009
Nov 16, 2009
94
Uint32 pixel_format, void * pixels, int pitch);
Nov 15, 2009
Nov 15, 2009
95
static int GL_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Nov 16, 2009
Nov 16, 2009
96
Uint32 pixel_format, const void * pixels, int pitch);
Jul 19, 2006
Jul 19, 2006
97
98
99
100
101
102
103
104
105
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",
Feb 1, 2011
Feb 1, 2011
106
(SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
Feb 2, 2011
Feb 2, 2011
107
13,
Jul 19, 2006
Jul 19, 2006
108
{
Aug 5, 2006
Aug 5, 2006
109
110
111
112
113
114
115
116
117
118
119
120
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
121
SDL_PIXELFORMAT_ARGB2101010},
Jul 19, 2006
Jul 19, 2006
122
123
124
125
126
127
128
0,
0}
};
typedef struct
{
SDL_GLContext context;
Dec 5, 2008
Dec 5, 2008
129
SDL_bool updateSize;
Jul 22, 2006
Jul 22, 2006
130
SDL_bool GL_ARB_texture_rectangle_supported;
Aug 12, 2007
Aug 12, 2007
131
SDL_bool GL_EXT_paletted_texture_supported;
Dec 7, 2008
Dec 7, 2008
132
133
SDL_bool GL_APPLE_ycbcr_422_supported;
SDL_bool GL_MESA_ycbcr_texture_supported;
Dec 6, 2008
Dec 6, 2008
134
SDL_bool GL_ARB_fragment_program_supported;
Jul 22, 2006
Jul 22, 2006
135
136
137
138
139
140
int blendMode;
/* 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
141
Aug 12, 2007
Aug 12, 2007
142
143
void (*glTextureRangeAPPLE) (GLenum target, GLsizei length,
const GLvoid * pointer);
Dec 6, 2008
Dec 6, 2008
144
145
146
147
148
149
150
151
152
153
154
PFNGLGETPROGRAMIVARBPROC glGetProgramivARB;
PFNGLGETPROGRAMSTRINGARBPROC glGetProgramStringARB;
PFNGLPROGRAMLOCALPARAMETER4FVARBPROC glProgramLocalParameter4fvARB;
PFNGLDELETEPROGRAMSARBPROC glDeleteProgramsARB;
PFNGLGENPROGRAMSARBPROC glGenProgramsARB;
PFNGLBINDPROGRAMARBPROC glBindProgramARB;
PFNGLPROGRAMSTRINGARBPROC glProgramStringARB;
/* (optional) fragment programs */
GLuint fragment_program_UYVY;
Jul 19, 2006
Jul 19, 2006
155
156
157
158
159
} GL_RenderData;
typedef struct
{
GLuint texture;
Dec 6, 2008
Dec 6, 2008
160
GLuint shader;
Jul 22, 2006
Jul 22, 2006
161
GLenum type;
Jul 19, 2006
Jul 19, 2006
162
163
GLfloat texw;
GLfloat texh;
Jul 22, 2006
Jul 22, 2006
164
165
GLenum format;
GLenum formattype;
Aug 6, 2006
Aug 6, 2006
166
Uint8 *palette;
Jul 19, 2006
Jul 19, 2006
167
168
void *pixels;
int pitch;
Jul 22, 2006
Jul 22, 2006
169
SDL_DirtyRectList dirty;
Dec 20, 2008
Dec 20, 2008
170
int HACK_RYAN_FIXME;
Jul 19, 2006
Jul 19, 2006
171
172
173
} GL_TextureData;
Jul 22, 2006
Jul 22, 2006
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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
235
236
237
238
239
SDL_Renderer *
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
{
SDL_Renderer *renderer;
GL_RenderData *data;
Jul 28, 2006
Jul 28, 2006
240
GLint value;
Jul 19, 2006
Jul 19, 2006
241
Aug 6, 2006
Aug 6, 2006
242
243
244
245
246
247
/* 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
248
if (!(window->flags & SDL_WINDOW_OPENGL)) {
Jul 23, 2006
Jul 23, 2006
249
if (SDL_RecreateWindow(window, window->flags | SDL_WINDOW_OPENGL) < 0) {
Jul 22, 2006
Jul 22, 2006
250
251
return NULL;
}
Jul 19, 2006
Jul 19, 2006
252
253
}
Jul 22, 2006
Jul 22, 2006
254
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
Jul 19, 2006
Jul 19, 2006
255
256
257
258
259
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
Jul 22, 2006
Jul 22, 2006
260
data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
Jul 19, 2006
Jul 19, 2006
261
262
263
264
265
266
if (!data) {
GL_DestroyRenderer(renderer);
SDL_OutOfMemory();
return NULL;
}
Feb 2, 2011
Feb 2, 2011
267
renderer->WindowEvent = GL_WindowEvent;
Jul 19, 2006
Jul 19, 2006
268
renderer->CreateTexture = GL_CreateTexture;
Aug 11, 2007
Aug 11, 2007
269
renderer->QueryTexturePixels = GL_QueryTexturePixels;
Jul 19, 2006
Jul 19, 2006
270
271
272
273
renderer->UpdateTexture = GL_UpdateTexture;
renderer->LockTexture = GL_LockTexture;
renderer->UnlockTexture = GL_UnlockTexture;
renderer->DirtyTexture = GL_DirtyTexture;
Dec 23, 2009
Dec 23, 2009
274
275
276
277
renderer->RenderClear = GL_RenderClear;
renderer->RenderDrawPoints = GL_RenderDrawPoints;
renderer->RenderDrawLines = GL_RenderDrawLines;
renderer->RenderFillRects = GL_RenderFillRects;
Jul 19, 2006
Jul 19, 2006
278
renderer->RenderCopy = GL_RenderCopy;
Nov 15, 2009
Nov 15, 2009
279
280
renderer->RenderReadPixels = GL_RenderReadPixels;
renderer->RenderWritePixels = GL_RenderWritePixels;
Jul 19, 2006
Jul 19, 2006
281
282
283
284
renderer->RenderPresent = GL_RenderPresent;
renderer->DestroyTexture = GL_DestroyTexture;
renderer->DestroyRenderer = GL_DestroyRenderer;
renderer->info = GL_RenderDriver.info;
Jan 21, 2010
Jan 21, 2010
285
renderer->window = window;
Jul 19, 2006
Jul 19, 2006
286
287
renderer->driverdata = data;
Feb 1, 2011
Feb 1, 2011
288
renderer->info.flags = SDL_RENDERER_ACCELERATED;
Jul 19, 2006
Jul 19, 2006
289
Jul 22, 2006
Jul 22, 2006
290
291
292
293
294
if (GL_LoadFunctions(data) < 0) {
GL_DestroyRenderer(renderer);
return NULL;
}
Jan 21, 2010
Jan 21, 2010
295
data->context = SDL_GL_CreateContext(window);
Jul 19, 2006
Jul 19, 2006
296
297
298
299
if (!data->context) {
GL_DestroyRenderer(renderer);
return NULL;
}
Jan 21, 2010
Jan 21, 2010
300
if (SDL_GL_MakeCurrent(window, data->context) < 0) {
Jul 19, 2006
Jul 19, 2006
301
302
303
GL_DestroyRenderer(renderer);
return NULL;
}
Aug 15, 2007
Aug 15, 2007
304
305
306
#ifdef __MACOSX__
/* Enable multi-threaded rendering */
/* Disabled until Ryan finishes his VBO/PBO code...
Jan 8, 2008
Jan 8, 2008
307
308
CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
*/
Aug 15, 2007
Aug 15, 2007
309
310
#endif
Aug 5, 2006
Aug 5, 2006
311
if (flags & SDL_RENDERER_PRESENTVSYNC) {
Jul 19, 2006
Jul 19, 2006
312
313
314
315
316
SDL_GL_SetSwapInterval(1);
} else {
SDL_GL_SetSwapInterval(0);
}
if (SDL_GL_GetSwapInterval() > 0) {
Aug 5, 2006
Aug 5, 2006
317
renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
Jul 19, 2006
Jul 19, 2006
318
319
}
Jul 28, 2006
Jul 28, 2006
320
321
322
323
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
324
Jul 22, 2006
Jul 22, 2006
325
326
327
328
if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
|| SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
}
Dec 7, 2008
Dec 7, 2008
329
330
331
332
333
334
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
335
336
337
338
339
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
340
Dec 6, 2008
Dec 6, 2008
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* 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
363
/* Set up parameters for rendering */
Jul 22, 2006
Jul 22, 2006
364
365
366
data->blendMode = -1;
data->glDisable(GL_DEPTH_TEST);
data->glDisable(GL_CULL_FACE);
Sep 19, 2009
Sep 19, 2009
367
368
/* This ended up causing video discrepancies between OpenGL and Direct3D */
/*data->glEnable(GL_LINE_SMOOTH);*/
Jul 22, 2006
Jul 22, 2006
369
if (data->GL_ARB_texture_rectangle_supported) {
Jul 22, 2006
Jul 22, 2006
370
data->glEnable(GL_TEXTURE_RECTANGLE_ARB);
Jul 22, 2006
Jul 22, 2006
371
} else {
Jul 22, 2006
Jul 22, 2006
372
data->glEnable(GL_TEXTURE_2D);
Jul 22, 2006
Jul 22, 2006
373
}
Dec 5, 2008
Dec 5, 2008
374
data->updateSize = SDL_TRUE;
Jul 19, 2006
Jul 19, 2006
375
376
377
378
return renderer;
}
Feb 2, 2011
Feb 2, 2011
379
380
static SDL_GLContext SDL_CurrentContext = NULL;
Jul 22, 2006
Jul 22, 2006
381
382
383
384
static int
GL_ActivateRenderer(SDL_Renderer * renderer)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
Jan 21, 2010
Jan 21, 2010
385
SDL_Window *window = renderer->window;
Jul 22, 2006
Jul 22, 2006
386
Feb 2, 2011
Feb 2, 2011
387
388
389
390
391
if (SDL_CurrentContext != data->context) {
if (SDL_GL_MakeCurrent(window, data->context) < 0) {
return -1;
}
SDL_CurrentContext = data->context;
Aug 6, 2006
Aug 6, 2006
392
}
Dec 5, 2008
Dec 5, 2008
393
if (data->updateSize) {
Dec 6, 2008
Dec 6, 2008
394
395
396
397
398
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
399
400
data->glOrtho(0.0, (GLdouble) window->w,
(GLdouble) window->h, 0.0, 0.0, 1.0);
Dec 5, 2008
Dec 5, 2008
401
402
data->updateSize = SDL_FALSE;
}
Aug 6, 2006
Aug 6, 2006
403
404
405
return 0;
}
Feb 2, 2011
Feb 2, 2011
406
407
static void
GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
Aug 6, 2006
Aug 6, 2006
408
409
410
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
Feb 2, 2011
Feb 2, 2011
411
412
413
414
415
if (event->event == SDL_WINDOWEVENT_RESIZED) {
/* Rebind the context to the window area and update matrices */
SDL_CurrentContext = NULL;
data->updateSize = SDL_TRUE;
}
Jul 22, 2006
Jul 22, 2006
416
417
}
Jul 22, 2006
Jul 22, 2006
418
419
420
421
422
423
424
425
426
427
428
static __inline__ int
power_of_2(int input)
{
int value = 1;
while (value < input) {
value <<= 1;
}
return value;
}
Dec 6, 2008
Dec 6, 2008
429
Dec 8, 2008
Dec 8, 2008
430
//#define DEBUG_PROGRAM_COMPILE 1
Dec 6, 2008
Dec 6, 2008
431
Nov 21, 2009
Nov 21, 2009
432
433
434
435
436
437
438
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
439
SDL_SetError("%s: shader compile error at position %d: %s",
Nov 21, 2009
Nov 21, 2009
440
441
442
prefix, (int) pos, (const char *) errstr);
}
Dec 6, 2008
Dec 6, 2008
443
static GLuint
Dec 20, 2008
Dec 20, 2008
444
compile_shader(GL_RenderData * data, GLenum shader_type, const char *_code)
Dec 6, 2008
Dec 6, 2008
445
{
Dec 7, 2008
Dec 7, 2008
446
447
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
448
const size_t replacementlen = SDL_strlen(replacement);
Dec 7, 2008
Dec 7, 2008
449
const char *token = "%TEXTURETARGET%";
Dec 25, 2008
Dec 25, 2008
450
const size_t tokenlen = SDL_strlen(token);
Dec 7, 2008
Dec 7, 2008
451
452
453
454
455
456
457
458
459
460
461
462
463
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
464
for (ptr = SDL_strstr(code, token); ptr; ptr = SDL_strstr(ptr + 1, token)) {
Dec 25, 2008
Dec 25, 2008
465
466
467
SDL_memcpy(ptr, replacement, replacementlen);
SDL_memmove(ptr + replacementlen, ptr + tokenlen,
SDL_strlen(ptr + tokenlen) + 1);
Dec 7, 2008
Dec 7, 2008
468
469
}
Dec 6, 2008
Dec 6, 2008
470
#if DEBUG_PROGRAM_COMPILE
Dec 7, 2008
Dec 7, 2008
471
printf("compiling shader:\n%s\n\n", code);
Dec 6, 2008
Dec 6, 2008
472
473
#endif
Dec 20, 2008
Dec 20, 2008
474
data->glGetError(); /* flush any existing error state. */
Dec 6, 2008
Dec 6, 2008
475
476
477
data->glGenProgramsARB(1, &program);
data->glBindProgramARB(shader_type, program);
data->glProgramStringARB(shader_type, GL_PROGRAM_FORMAT_ASCII_ARB,
Sep 5, 2009
Sep 5, 2009
478
(GLsizei)SDL_strlen(code), code);
Dec 7, 2008
Dec 7, 2008
479
480
SDL_free(code);
Dec 6, 2008
Dec 6, 2008
481
Dec 20, 2008
Dec 20, 2008
482
if (data->glGetError() == GL_INVALID_OPERATION) {
Dec 6, 2008
Dec 6, 2008
483
484
485
486
487
488
#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
489
(int) pos, (const char *) errstr);
Dec 6, 2008
Dec 6, 2008
490
491
492
493
#endif
data->glBindProgramARB(shader_type, 0);
data->glDeleteProgramsARB(1, &program);
return 0;
Dec 7, 2008
Dec 7, 2008
494
}
Dec 6, 2008
Dec 6, 2008
495
496
497
498
return program;
}
Dec 7, 2008
Dec 7, 2008
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
* 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
512
static const char *fragment_program_UYVY_source_code = "!!ARBfp1.0\n"
Dec 7, 2008
Dec 7, 2008
513
/* outputs... */
Dec 6, 2008
Dec 6, 2008
514
"OUTPUT outcolor = result.color;\n"
Dec 7, 2008
Dec 7, 2008
515
/* scratch registers... */
Dec 20, 2008
Dec 20, 2008
516
"TEMP uyvy;\n" "TEMP luminance;\n" "TEMP work;\n"
Dec 7, 2008
Dec 7, 2008
517
/* Halve the coordinates to grab the correct 32 bits for the fragment. */
Dec 6, 2008
Dec 6, 2008
518
"MUL work, fragment.texcoord, { 0.5, 1.0, 1.0, 1.0 };\n"
Dec 7, 2008
Dec 7, 2008
519
/* Sample the YUV texture. Cb, Y1, Cr, Y2, are stored in x, y, z, w. */
Dec 7, 2008
Dec 7, 2008
520
"TEX uyvy, work, texture[0], %TEXTURETARGET%;\n"
Dec 7, 2008
Dec 7, 2008
521
/* Do subtractions (128/255, 16/255, 128/255, 16/255) */
Dec 7, 2008
Dec 7, 2008
522
"SUB uyvy, uyvy, { 0.501960784313726, 0.06274509803922, 0.501960784313726, 0.06274509803922 };\n"
Dec 7, 2008
Dec 7, 2008
523
524
/* Choose the luminance component by texcoord. */
/* !!! FIXME: laziness wins out for now... just average Y1 and Y2. */
Dec 6, 2008
Dec 6, 2008
525
526
"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
527
/* Multiply luminance by its magic value. */
Dec 6, 2008
Dec 6, 2008
528
"MUL luminance, luminance, { 1.164, 1.164, 1.164, 1.164 };\n"
Dec 7, 2008
Dec 7, 2008
529
/* uyvy.xyzw becomes Cr/Cr/Cb/Cb, with multiplications. */
Dec 6, 2008
Dec 6, 2008
530
"MUL uyvy, uyvy.zzxx, { 1.596, -0.813, 2.018, -0.391 };\n"
Dec 7, 2008
Dec 7, 2008
531
/* Add luminance to Cr and Cb, store to RGB channels. */
Dec 6, 2008
Dec 6, 2008
532
"ADD work.rgb, luminance, uyvy;\n"
Dec 7, 2008
Dec 7, 2008
533
/* Do final addition for Green channel. (!!! FIXME: this should be a DPH?) */
Dec 6, 2008
Dec 6, 2008
534
"ADD work.g, work.g, uyvy.w;\n"
Dec 7, 2008
Dec 7, 2008
535
/* Make sure alpha channel is fully opaque. (!!! FIXME: blend modes!) */
Dec 6, 2008
Dec 6, 2008
536
"MOV work.a, { 1.0 };\n"
Dec 7, 2008
Dec 7, 2008
537
/* Store out the final fragment color... */
Dec 6, 2008
Dec 6, 2008
538
"MOV outcolor, work;\n"
Dec 7, 2008
Dec 7, 2008
539
/* ...and we're done! */
Dec 6, 2008
Dec 6, 2008
540
541
"END\n";
Nov 15, 2009
Nov 15, 2009
542
543
544
static __inline__ SDL_bool
convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
GLint* internalFormat, GLenum* format, GLenum* type)
Jul 19, 2006
Jul 19, 2006
545
{
Nov 15, 2009
Nov 15, 2009
546
switch (pixel_format) {
Aug 5, 2006
Aug 5, 2006
547
case SDL_PIXELFORMAT_RGB332:
Nov 15, 2009
Nov 15, 2009
548
549
550
*internalFormat = GL_R3_G3_B2;
*format = GL_RGB;
*type = GL_UNSIGNED_BYTE_3_3_2;
Jul 22, 2006
Jul 22, 2006
551
break;
Aug 5, 2006
Aug 5, 2006
552
case SDL_PIXELFORMAT_RGB444:
Nov 15, 2009
Nov 15, 2009
553
554
555
*internalFormat = GL_RGB4;
*format = GL_RGB;
*type = GL_UNSIGNED_SHORT_4_4_4_4;
Jul 22, 2006
Jul 22, 2006
556
break;
Aug 5, 2006
Aug 5, 2006
557
case SDL_PIXELFORMAT_RGB555:
Nov 15, 2009
Nov 15, 2009
558
559
560
*internalFormat = GL_RGB5;
*format = GL_RGB;
*type = GL_UNSIGNED_SHORT_5_5_5_1;
Jul 22, 2006
Jul 22, 2006
561
break;
Aug 5, 2006
Aug 5, 2006
562
case SDL_PIXELFORMAT_ARGB4444:
Nov 15, 2009
Nov 15, 2009
563
564
565
*internalFormat = GL_RGBA4;
*format = GL_BGRA;
*type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
Jul 22, 2006
Jul 22, 2006
566
break;
Aug 5, 2006
Aug 5, 2006
567
case SDL_PIXELFORMAT_ARGB1555:
Nov 15, 2009
Nov 15, 2009
568
569
570
*internalFormat = GL_RGB5_A1;
*format = GL_BGRA;
*type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
Jul 22, 2006
Jul 22, 2006
571
break;
Aug 5, 2006
Aug 5, 2006
572
case SDL_PIXELFORMAT_RGB565:
Nov 15, 2009
Nov 15, 2009
573
574
575
*internalFormat = GL_RGB8;
*format = GL_RGB;
*type = GL_UNSIGNED_SHORT_5_6_5;
Jul 22, 2006
Jul 22, 2006
576
break;
Aug 5, 2006
Aug 5, 2006
577
case SDL_PIXELFORMAT_RGB24:
Nov 15, 2009
Nov 15, 2009
578
579
580
*internalFormat = GL_RGB8;
*format = GL_RGB;
*type = GL_UNSIGNED_BYTE;
Jul 22, 2006
Jul 22, 2006
581
break;
Aug 5, 2006
Aug 5, 2006
582
case SDL_PIXELFORMAT_RGB888:
Nov 15, 2009
Nov 15, 2009
583
584
585
*internalFormat = GL_RGB8;
*format = GL_BGRA;
*type = GL_UNSIGNED_BYTE;
Jul 22, 2006
Jul 22, 2006
586
break;
Aug 5, 2006
Aug 5, 2006
587
case SDL_PIXELFORMAT_BGR24:
Nov 15, 2009
Nov 15, 2009
588
589
590
*internalFormat = GL_RGB8;
*format = GL_BGR;
*type = GL_UNSIGNED_BYTE;
Jul 22, 2006
Jul 22, 2006
591
break;
Aug 5, 2006
Aug 5, 2006
592
case SDL_PIXELFORMAT_BGR888:
Nov 15, 2009
Nov 15, 2009
593
594
595
*internalFormat = GL_RGB8;
*format = GL_RGBA;
*type = GL_UNSIGNED_BYTE;
Jul 22, 2006
Jul 22, 2006
596
break;
Aug 5, 2006
Aug 5, 2006
597
case SDL_PIXELFORMAT_ARGB8888:
Aug 12, 2007
Aug 12, 2007
598
#ifdef __MACOSX__
Nov 15, 2009
Nov 15, 2009
599
600
601
*internalFormat = GL_RGBA;
*format = GL_BGRA;
*type = GL_UNSIGNED_INT_8_8_8_8_REV;
Aug 12, 2007
Aug 12, 2007
602
#else
Nov 15, 2009
Nov 15, 2009
603
604
605
*internalFormat = GL_RGBA8;
*format = GL_BGRA;
*type = GL_UNSIGNED_BYTE;
Aug 12, 2007
Aug 12, 2007
606
#endif
Jul 22, 2006
Jul 22, 2006
607
break;
Aug 5, 2006
Aug 5, 2006
608
case SDL_PIXELFORMAT_ABGR8888:
Nov 15, 2009
Nov 15, 2009
609
610
611
*internalFormat = GL_RGBA8;
*format = GL_RGBA;
*type = GL_UNSIGNED_BYTE;
Jul 22, 2006
Jul 22, 2006
612
break;
Aug 5, 2006
Aug 5, 2006
613
case SDL_PIXELFORMAT_ARGB2101010:
Nov 15, 2009
Nov 15, 2009
614
615
616
*internalFormat = GL_RGB10_A2;
*format = GL_BGRA;
*type = GL_UNSIGNED_INT_2_10_10_10_REV;
Jul 22, 2006
Jul 22, 2006
617
break;
Dec 6, 2008
Dec 6, 2008
618
case SDL_PIXELFORMAT_UYVY:
Dec 7, 2008
Dec 7, 2008
619
if (renderdata->GL_APPLE_ycbcr_422_supported) {
Nov 15, 2009
Nov 15, 2009
620
621
*internalFormat = GL_RGB;
*format = GL_YCBCR_422_APPLE;
Dec 7, 2008
Dec 7, 2008
622
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
Nov 15, 2009
Nov 15, 2009
623
*type = GL_UNSIGNED_SHORT_8_8_APPLE;
Dec 7, 2008
Dec 7, 2008
624
#else
Nov 15, 2009
Nov 15, 2009
625
*type = GL_UNSIGNED_SHORT_8_8_REV_APPLE;
Dec 7, 2008
Dec 7, 2008
626
627
#endif
} else if (renderdata->GL_MESA_ycbcr_texture_supported) {
Nov 26, 2009
Nov 26, 2009
628
*internalFormat = GL_YCBCR_MESA;
Nov 15, 2009
Nov 15, 2009
629
*format = GL_YCBCR_MESA;
Dec 7, 2008
Dec 7, 2008
630
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
Nov 15, 2009
Nov 15, 2009
631
*type = GL_UNSIGNED_SHORT_8_8_MESA;
Dec 7, 2008
Dec 7, 2008
632
#else
Nov 15, 2009
Nov 15, 2009
633
*type = GL_UNSIGNED_SHORT_8_8_REV_MESA;
Dec 7, 2008
Dec 7, 2008
634
635
#endif
} else if (renderdata->GL_ARB_fragment_program_supported) {
Nov 15, 2009
Nov 15, 2009
636
637
638
*internalFormat = GL_RGBA;
*format = GL_RGBA;
*type = GL_UNSIGNED_BYTE;
Dec 6, 2008
Dec 6, 2008
639
} else {
Nov 15, 2009
Nov 15, 2009
640
return SDL_FALSE;
Dec 6, 2008
Dec 6, 2008
641
642
}
break;
Dec 7, 2008
Dec 7, 2008
643
644
case SDL_PIXELFORMAT_YUY2:
if (renderdata->GL_APPLE_ycbcr_422_supported) {
Nov 15, 2009
Nov 15, 2009
645
646
*internalFormat = GL_RGB;
*format = GL_YCBCR_422_APPLE;
Dec 7, 2008
Dec 7, 2008
647
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
Nov 15, 2009
Nov 15, 2009
648
*type = GL_UNSIGNED_SHORT_8_8_REV_APPLE;
Dec 7, 2008
Dec 7, 2008
649
#else
Nov 15, 2009
Nov 15, 2009
650
*type = GL_UNSIGNED_SHORT_8_8_APPLE;
Dec 7, 2008
Dec 7, 2008
651
652
#endif
} else if (renderdata->GL_MESA_ycbcr_texture_supported) {
Nov 26, 2009
Nov 26, 2009
653
*internalFormat = GL_YCBCR_MESA;
Nov 15, 2009
Nov 15, 2009
654
*format = GL_YCBCR_MESA;
Dec 7, 2008
Dec 7, 2008
655
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
Nov 15, 2009
Nov 15, 2009
656
*type = GL_UNSIGNED_SHORT_8_8_REV_MESA;
Dec 7, 2008
Dec 7, 2008
657
#else
Nov 15, 2009
Nov 15, 2009
658
*type = GL_UNSIGNED_SHORT_8_8_MESA;
Dec 6, 2008
Dec 6, 2008
659
#endif
Dec 7, 2008
Dec 7, 2008
660
} else {
Nov 15, 2009
Nov 15, 2009
661
return SDL_FALSE;
Dec 7, 2008
Dec 7, 2008
662
663
}
break;
Jul 22, 2006
Jul 22, 2006
664
default:
Nov 15, 2009
Nov 15, 2009
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
return SDL_FALSE;
}
return SDL_TRUE;
}
static int
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
GL_TextureData *data;
GLint internalFormat;
GLenum format, type;
int texture_w, texture_h;
GLuint shader = 0;
GLenum result;
Feb 2, 2011
Feb 2, 2011
681
682
GL_ActivateRenderer(renderer);
Nov 15, 2009
Nov 15, 2009
683
684
if (!convert_format(renderdata, texture->format, &internalFormat,
&format, &type)) {
Jan 13, 2011
Jan 13, 2011
685
686
SDL_SetError("Texture format %s not supported by OpenGL",
SDL_GetPixelFormatName(texture->format));
Jul 22, 2006
Jul 22, 2006
687
688
return -1;
}
Nov 15, 2009
Nov 15, 2009
689
690
691
692
693
694
695
696
697
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
698
set_shader_error(renderdata, "UYVY");
Nov 15, 2009
Nov 15, 2009
699
700
701
702
703
return -1;
}
}
shader = renderdata->fragment_program_UYVY;
}
Jul 19, 2006
Jul 19, 2006
704
Jul 22, 2006
Jul 22, 2006
705
data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
Jul 19, 2006
Jul 19, 2006
706
707
708
709
710
if (!data) {
SDL_OutOfMemory();
return -1;
}
Dec 6, 2008
Dec 6, 2008
711
712
data->shader = shader;
Aug 11, 2007
Aug 11, 2007
713
if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
Dec 6, 2008
Dec 6, 2008
714
data->pitch = texture->w * bytes_per_pixel(texture->format);
Aug 11, 2007
Aug 11, 2007
715
716
717
718
719
720
721
722
data->pixels = SDL_malloc(texture->h * data->pitch);
if (!data->pixels) {
SDL_OutOfMemory();
SDL_free(data);
return -1;
}
}
Jul 19, 2006
Jul 19, 2006
723
724
texture->driverdata = data;
Jul 22, 2006
Jul 22, 2006
725
726
renderdata->glGetError();
renderdata->glGenTextures(1, &data->texture);
Jul 22, 2006
Jul 22, 2006
727
728
729
730
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
731
732
data->texw = (GLfloat) texture_w;
data->texh = (GLfloat) texture_h;
Jul 22, 2006
Jul 22, 2006
733
734
735
736
} 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
737
data->texw = (GLfloat) (texture->w) / texture_w;
Jul 22, 2006
Jul 22, 2006
738
739
data->texh = (GLfloat) texture->h / texture_h;
}
Dec 6, 2008
Dec 6, 2008
740
Dec 6, 2008
Dec 6, 2008
741
742
/* YUV formats use RGBA but are really two bytes per pixel */
if (internalFormat == GL_RGBA && bytes_per_pixel(texture->format) < 4) {
Feb 17, 2010
Feb 17, 2010
743
744
745
746
texture_w /= 2;
if (data->type == GL_TEXTURE_2D) {
data->texw *= 2.0f;
}
Dec 20, 2008
Dec 20, 2008
747
data->HACK_RYAN_FIXME = 2;
Dec 6, 2008
Dec 6, 2008
748
} else {
Dec 20, 2008
Dec 20, 2008
749
data->HACK_RYAN_FIXME = 1;
Dec 6, 2008
Dec 6, 2008
750
751
}
Jul 22, 2006
Jul 22, 2006
752
753
data->format = format;
data->formattype = type;
Dec 20, 2008
Dec 20, 2008
754
renderdata->glEnable(data->type);
Jul 22, 2006
Jul 22, 2006
755
renderdata->glBindTexture(data->type, data->texture);
Aug 12, 2007
Aug 12, 2007
756
renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
Feb 1, 2011
Feb 1, 2011
757
GL_LINEAR);
Aug 12, 2007
Aug 12, 2007
758
renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
Feb 1, 2011
Feb 1, 2011
759
GL_LINEAR);
Aug 12, 2007
Aug 12, 2007
760
761
762
763
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
764
#ifdef __MACOSX__
Aug 12, 2007
Aug 12, 2007
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
#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
781
782
/* This causes a crash in testoverlay for some reason. Apple bug? */
#if 0
Jan 8, 2008
Jan 8, 2008
783
784
if (texture->access == SDL_TEXTUREACCESS_STREAMING
&& texture->format == SDL_PIXELFORMAT_ARGB8888) {
Aug 13, 2007
Aug 13, 2007
785
/*
Jan 8, 2008
Jan 8, 2008
786
787
788
789
790
791
if (renderdata->glTextureRangeAPPLE) {
renderdata->glTextureRangeAPPLE(data->type,
texture->h * data->pitch,
data->pixels);
}
*/
Aug 12, 2007
Aug 12, 2007
792
793
794
795
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
796
#endif
Aug 12, 2007
Aug 12, 2007
797
798
799
800
801
#endif
{
renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
texture_h, 0, format, type, NULL);
}
Jan 14, 2009
Jan 14, 2009
802
renderdata->glDisable(data->type);
Jul 22, 2006
Jul 22, 2006
803
result = renderdata->glGetError();
Jul 22, 2006
Jul 22, 2006
804
805
806
807
if (result != GL_NO_ERROR) {
GL_SetError("glTexImage2D()", result);
return -1;
}
Jul 19, 2006
Jul 19, 2006
808
809
810
return 0;
}
Aug 11, 2007
Aug 11, 2007
811
812
813
814
815
816
817
818
819
820
821
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 22, 2006
Jul 22, 2006
822
static void
Jul 22, 2006
Jul 22, 2006
823
824
SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture,
int pitch)
Jul 22, 2006
Jul 22, 2006
825
{
Jul 22, 2006
Jul 22, 2006
826
renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Nov 29, 2008
Nov 29, 2008
827
renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
Dec 20, 2008
Dec 20, 2008
828
(pitch / bytes_per_pixel(texture->format)) /
Jan 10, 2009
Jan 10, 2009
829
830
((GL_TextureData *) texture->driverdata)->
HACK_RYAN_FIXME);
Jul 22, 2006
Jul 22, 2006
831
832
}
Jul 19, 2006
Jul 19, 2006
833
834
835
836
static int
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
{
Jul 22, 2006
Jul 22, 2006
837
GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
Jul 19, 2006
Jul 19, 2006
838
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
Jul 22, 2006
Jul 22, 2006
839
GLenum result;
Jul 19, 2006
Jul 19, 2006
840
Feb 2, 2011
Feb 2, 2011
841
842
GL_ActivateRenderer(renderer);
Jul 22, 2006
Jul 22, 2006
843
844
renderdata->glGetError();
SetupTextureUpdate(renderdata, texture, pitch);
Dec 20, 2008
Dec 20, 2008
845
renderdata->glEnable(data->type);
Jul 22, 2006
Jul 22, 2006
846
847
848
849
renderdata->glBindTexture(data->type, data->texture);
renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
rect->h, data->format, data->formattype,
pixels);
Jan 14, 2009
Jan 14, 2009
850
renderdata->glDisable(data->type);
Jul 22, 2006
Jul 22, 2006
851
result = renderdata->glGetError();
Jul 22, 2006
Jul 22, 2006
852
853
854
855
if (result != GL_NO_ERROR) {
GL_SetError("glTexSubImage2D()", result);
return -1;
}
Jul 19, 2006
Jul 19, 2006
856
857
858
859
860
861
862
863
864
865
return 0;
}
static int
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, int markDirty, void **pixels,
int *pitch)
{
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
Jul 22, 2006
Jul 22, 2006
866
867
if (markDirty) {
SDL_AddDirtyRect(&data->dirty, rect);
Jul 19, 2006
Jul 19, 2006
868
}
Jul 22, 2006
Jul 22, 2006
869
870
871
*pixels =
(void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
Dec 6, 2008
Dec 6, 2008
872
rect->x * bytes_per_pixel(texture->format));
Jul 22, 2006
Jul 22, 2006
873
*pitch = data->pitch;
Jul 19, 2006
Jul 19, 2006
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
return 0;
}
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)
{
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
int i;
for (i = 0; i < numrects; ++i) {
Jul 22, 2006
Jul 22, 2006
890
SDL_AddDirtyRect(&data->dirty, &rects[i]);
Jul 19, 2006
Jul 19, 2006
891
892
893
}
}
Dec 31, 2008
Dec 31, 2008
894
static void
Feb 1, 2011
Feb 1, 2011
895
GL_SetBlendMode(GL_RenderData * data, int blendMode)
Dec 31, 2008
Dec 31, 2008
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
{
if (blendMode != data->blendMode) {
switch (blendMode) {
case SDL_BLENDMODE_NONE:
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
data->glDisable(GL_BLEND);
break;
case SDL_BLENDMODE_BLEND:
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
data->glEnable(GL_BLEND);
data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
break;
case SDL_BLENDMODE_ADD:
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
data->glEnable(GL_BLEND);
data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
break;
}
data->blendMode = blendMode;
}
}
Dec 20, 2008
Dec 20, 2008
918
static int
Dec 23, 2009
Dec 23, 2009
919
920
921
922
GL_RenderClear(SDL_Renderer * renderer)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
Feb 2, 2011
Feb 2, 2011
923
924
GL_ActivateRenderer(renderer);
Dec 23, 2009
Dec 23, 2009
925
926
927
928
929
930
931
932
933
934
935
936
937
data->glClearColor((GLfloat) renderer->r * inv255f,
(GLfloat) renderer->g * inv255f,
(GLfloat) renderer->b * inv255f,
(GLfloat) renderer->a * inv255f);
data->glClear(GL_COLOR_BUFFER_BIT);
return 0;
}
static int
GL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
int count)
Jul 19, 2006
Jul 19, 2006
938
939
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
Dec 9, 2009
Dec 9, 2009
940
int i;
Dec 30, 2008
Dec 30, 2008
941
Feb 2, 2011
Feb 2, 2011
942
943
GL_ActivateRenderer(renderer);
Feb 1, 2011
Feb 1, 2011
944
GL_SetBlendMode(data, renderer->blendMode);
Jul 19, 2006
Jul 19, 2006
945
Dec 20, 2008
Dec 20, 2008
946
947
948
949
data->glColor4f((GLfloat) renderer->r * inv255f,
(GLfloat) renderer->g * inv255f,
(GLfloat) renderer->b * inv255f,
(GLfloat) renderer->a * inv255f);
Dec 30, 2008
Dec 30, 2008
950
951
data->glBegin(GL_POINTS);
Dec 9, 2009
Dec 9, 2009
952
953
954
for (i = 0; i < count; ++i) {
data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
}
Dec 30, 2008
Dec 30, 2008
955
data->glEnd();
Dec 20, 2008
Dec 20, 2008
956
957
958
959
return 0;
}
Dec 21, 2008
Dec 21, 2008
960
static int
Dec 23, 2009
Dec 23, 2009
961
962
GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
int count)
Dec 21, 2008
Dec 21, 2008
963
964
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
Dec 9, 2009
Dec 9, 2009
965
int i;
Dec 21, 2008
Dec 21, 2008
966
Feb 2, 2011
Feb 2, 2011
967
968
GL_ActivateRenderer(renderer);
Feb 1, 2011
Feb 1, 2011
969
GL_SetBlendMode(data, renderer->blendMode);
Dec 21, 2008
Dec 21, 2008
970
971
972
973
974
975
data->glColor4f((GLfloat) renderer->r * inv255f,
(GLfloat) renderer->g * inv255f,
(GLfloat) renderer->b * inv255f,
(GLfloat) renderer->a * inv255f);
Dec 9, 2009
Dec 9, 2009
976
977
978
979
980
981
982
983
984
985
if (count > 2 &&
points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
data->glBegin(GL_LINE_LOOP);
/* GL_LINE_LOOP takes care of the final segment */
--count;
for (i = 0; i < count; ++i) {
data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
}
data->glEnd();
} else {
Jan 24, 2011
Jan 24, 2011
986
#if defined(__APPLE__) || defined(__WIN32__)
Nov 20, 2010
Nov 20, 2010
987
#else
Oct 17, 2010
Oct 17, 2010
988
int x1, y1, x2, y2;
Nov 20, 2010
Nov 20, 2010
989
#endif
Oct 17, 2010
Oct 17, 2010
990
Dec 9, 2009
Dec 9, 2009
991
992
993
994
995
996
997
998
999
1000
data->glBegin(GL_LINE_STRIP);
for (i = 0; i < count; ++i) {
data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
}
data->glEnd();
/* The line is half open, so we need one more point to complete it.
* http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node47.html
* If we have to, we can use vertical line and horizontal line textures
* for vertical and horizontal lines, and then create custom textures