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