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