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

Latest commit

 

History

History
872 lines (745 loc) · 26.5 KB

SDL_render_gl.c

File metadata and controls

872 lines (745 loc) · 26.5 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"
Feb 8, 2011
Feb 8, 2011
24
#if SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED
Jul 22, 2006
Jul 22, 2006
25
26
#include "SDL_opengl.h"
Feb 2, 2011
Feb 2, 2011
27
#include "../SDL_sysrender.h"
Feb 9, 2011
Feb 9, 2011
28
#include "SDL_shaders_gl.h"
Jul 19, 2006
Jul 19, 2006
29
Aug 15, 2007
Aug 15, 2007
30
31
32
33
#ifdef __MACOSX__
#include <OpenGL/OpenGL.h>
#endif
Nov 22, 2008
Nov 22, 2008
34
Jul 19, 2006
Jul 19, 2006
35
36
/* OpenGL renderer implementation */
Aug 12, 2007
Aug 12, 2007
37
/* Details on optimizing the texture path on Mac OS X:
Feb 6, 2011
Feb 6, 2011
38
http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/opengl_texturedata.html
Aug 12, 2007
Aug 12, 2007
39
40
*/
Feb 2, 2011
Feb 2, 2011
41
42
/* Used to re-create the window with OpenGL capability */
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
Dec 6, 2008
Dec 6, 2008
43
Aug 28, 2006
Aug 28, 2006
44
45
static const float inv255f = 1.0f / 255.0f;
Jul 19, 2006
Jul 19, 2006
46
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
Feb 2, 2011
Feb 2, 2011
47
48
static void GL_WindowEvent(SDL_Renderer * renderer,
const SDL_WindowEvent *event);
Jul 19, 2006
Jul 19, 2006
49
50
51
52
53
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
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,
Feb 3, 2011
Feb 3, 2011
54
const SDL_Rect * rect, void **pixels, int *pitch);
Jul 19, 2006
Jul 19, 2006
55
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
Feb 8, 2011
Feb 8, 2011
56
static void GL_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect);
Dec 23, 2009
Dec 23, 2009
57
58
59
60
61
62
63
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
64
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
Aug 28, 2006
Aug 28, 2006
65
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
Nov 15, 2009
Nov 15, 2009
66
static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Nov 16, 2009
Nov 16, 2009
67
Uint32 pixel_format, void * pixels, int pitch);
Jul 19, 2006
Jul 19, 2006
68
69
70
71
72
73
74
75
76
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 6, 2011
Feb 6, 2011
77
(SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
Feb 3, 2011
Feb 3, 2011
78
79
1,
{SDL_PIXELFORMAT_ARGB8888},
Jul 19, 2006
Jul 19, 2006
80
81
82
83
84
85
86
0,
0}
};
typedef struct
{
SDL_GLContext context;
Dec 5, 2008
Dec 5, 2008
87
SDL_bool updateSize;
Jul 22, 2006
Jul 22, 2006
88
SDL_bool GL_ARB_texture_rectangle_supported;
Jul 22, 2006
Jul 22, 2006
89
90
91
92
int blendMode;
/* OpenGL functions */
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
Feb 6, 2011
Feb 6, 2011
93
#include "SDL_glfuncs.h"
Jul 22, 2006
Jul 22, 2006
94
#undef SDL_PROC
Aug 6, 2006
Aug 6, 2006
95
Aug 12, 2007
Aug 12, 2007
96
97
void (*glTextureRangeAPPLE) (GLenum target, GLsizei length,
const GLvoid * pointer);
Feb 9, 2011
Feb 9, 2011
98
99
100
101
102
103
104
105
106
/* Multitexture support */
SDL_bool GL_ARB_multitexture_supported;
PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;
int num_texture_units;
/* Shader support */
GL_ShaderContext *shaders;
Jul 19, 2006
Jul 19, 2006
107
108
109
110
111
} GL_RenderData;
typedef struct
{
GLuint texture;
Jul 22, 2006
Jul 22, 2006
112
GLenum type;
Jul 19, 2006
Jul 19, 2006
113
114
GLfloat texw;
GLfloat texh;
Jul 22, 2006
Jul 22, 2006
115
116
GLenum format;
GLenum formattype;
Jul 19, 2006
Jul 19, 2006
117
118
void *pixels;
int pitch;
Feb 8, 2011
Feb 8, 2011
119
SDL_Rect locked_rect;
Jul 19, 2006
Jul 19, 2006
120
121
122
} GL_TextureData;
Jul 22, 2006
Jul 22, 2006
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
static int
GL_LoadFunctions(GL_RenderData * data)
{
#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__ */
Feb 6, 2011
Feb 6, 2011
176
#include "SDL_glfuncs.h"
Jul 22, 2006
Jul 22, 2006
177
178
179
180
#undef SDL_PROC
return 0;
}
Jul 19, 2006
Jul 19, 2006
181
182
183
184
185
SDL_Renderer *
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
{
SDL_Renderer *renderer;
GL_RenderData *data;
Jul 28, 2006
Jul 28, 2006
186
GLint value;
Feb 2, 2011
Feb 2, 2011
187
Uint32 window_flags;
Jul 19, 2006
Jul 19, 2006
188
Feb 2, 2011
Feb 2, 2011
189
190
191
window_flags = SDL_GetWindowFlags(window);
if (!(window_flags & SDL_WINDOW_OPENGL)) {
if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
Jul 22, 2006
Jul 22, 2006
192
193
return NULL;
}
Jul 19, 2006
Jul 19, 2006
194
195
}
Jul 22, 2006
Jul 22, 2006
196
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
Jul 19, 2006
Jul 19, 2006
197
198
199
200
201
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
Jul 22, 2006
Jul 22, 2006
202
data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
Jul 19, 2006
Jul 19, 2006
203
204
205
206
207
208
if (!data) {
GL_DestroyRenderer(renderer);
SDL_OutOfMemory();
return NULL;
}
Feb 2, 2011
Feb 2, 2011
209
renderer->WindowEvent = GL_WindowEvent;
Jul 19, 2006
Jul 19, 2006
210
211
212
213
renderer->CreateTexture = GL_CreateTexture;
renderer->UpdateTexture = GL_UpdateTexture;
renderer->LockTexture = GL_LockTexture;
renderer->UnlockTexture = GL_UnlockTexture;
Feb 8, 2011
Feb 8, 2011
214
renderer->SetClipRect = GL_SetClipRect;
Dec 23, 2009
Dec 23, 2009
215
216
217
218
renderer->RenderClear = GL_RenderClear;
renderer->RenderDrawPoints = GL_RenderDrawPoints;
renderer->RenderDrawLines = GL_RenderDrawLines;
renderer->RenderFillRects = GL_RenderFillRects;
Jul 19, 2006
Jul 19, 2006
219
renderer->RenderCopy = GL_RenderCopy;
Nov 15, 2009
Nov 15, 2009
220
renderer->RenderReadPixels = GL_RenderReadPixels;
Jul 19, 2006
Jul 19, 2006
221
222
223
224
225
226
renderer->RenderPresent = GL_RenderPresent;
renderer->DestroyTexture = GL_DestroyTexture;
renderer->DestroyRenderer = GL_DestroyRenderer;
renderer->info = GL_RenderDriver.info;
renderer->driverdata = data;
Feb 1, 2011
Feb 1, 2011
227
renderer->info.flags = SDL_RENDERER_ACCELERATED;
Jul 19, 2006
Jul 19, 2006
228
Jan 21, 2010
Jan 21, 2010
229
data->context = SDL_GL_CreateContext(window);
Jul 19, 2006
Jul 19, 2006
230
231
232
233
if (!data->context) {
GL_DestroyRenderer(renderer);
return NULL;
}
Jan 21, 2010
Jan 21, 2010
234
if (SDL_GL_MakeCurrent(window, data->context) < 0) {
Jul 19, 2006
Jul 19, 2006
235
236
237
GL_DestroyRenderer(renderer);
return NULL;
}
Feb 6, 2011
Feb 6, 2011
238
239
240
241
242
243
if (GL_LoadFunctions(data) < 0) {
GL_DestroyRenderer(renderer);
return NULL;
}
Aug 15, 2007
Aug 15, 2007
244
245
246
#ifdef __MACOSX__
/* Enable multi-threaded rendering */
/* Disabled until Ryan finishes his VBO/PBO code...
Jan 8, 2008
Jan 8, 2008
247
248
CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
*/
Aug 15, 2007
Aug 15, 2007
249
250
#endif
Aug 5, 2006
Aug 5, 2006
251
if (flags & SDL_RENDERER_PRESENTVSYNC) {
Jul 19, 2006
Jul 19, 2006
252
253
254
255
256
SDL_GL_SetSwapInterval(1);
} else {
SDL_GL_SetSwapInterval(0);
}
if (SDL_GL_GetSwapInterval() > 0) {
Aug 5, 2006
Aug 5, 2006
257
renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
Jul 19, 2006
Jul 19, 2006
258
259
}
Jul 28, 2006
Jul 28, 2006
260
261
262
263
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
264
Jul 22, 2006
Jul 22, 2006
265
266
267
268
if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
|| SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
}
Aug 12, 2007
Aug 12, 2007
269
270
271
272
273
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
274
Feb 9, 2011
Feb 9, 2011
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* Check for multitexture support */
if (SDL_GL_ExtensionSupported("GL_ARB_multitexture")) {
data->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glActiveTextureARB");
if (data->glActiveTextureARB) {
data->GL_ARB_multitexture_supported = SDL_TRUE;
data->glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &data->num_texture_units);
}
}
/* Check for shader support */
//data->shaders = GL_CreateShaderContext();
#if 0
/* We support YV12 textures using 3 textures and a shader */
if (data->shaders && data->num_texture_units >= 3) {
renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_YV12;
}
#endif
Jul 19, 2006
Jul 19, 2006
294
/* Set up parameters for rendering */
Jul 22, 2006
Jul 22, 2006
295
296
297
data->blendMode = -1;
data->glDisable(GL_DEPTH_TEST);
data->glDisable(GL_CULL_FACE);
Sep 19, 2009
Sep 19, 2009
298
299
/* This ended up causing video discrepancies between OpenGL and Direct3D */
/*data->glEnable(GL_LINE_SMOOTH);*/
Jul 22, 2006
Jul 22, 2006
300
if (data->GL_ARB_texture_rectangle_supported) {
Jul 22, 2006
Jul 22, 2006
301
data->glEnable(GL_TEXTURE_RECTANGLE_ARB);
Jul 22, 2006
Jul 22, 2006
302
} else {
Jul 22, 2006
Jul 22, 2006
303
data->glEnable(GL_TEXTURE_2D);
Jul 22, 2006
Jul 22, 2006
304
}
Dec 5, 2008
Dec 5, 2008
305
data->updateSize = SDL_TRUE;
Jul 19, 2006
Jul 19, 2006
306
307
308
309
return renderer;
}
Feb 2, 2011
Feb 2, 2011
310
311
static SDL_GLContext SDL_CurrentContext = NULL;
Jul 22, 2006
Jul 22, 2006
312
313
314
315
static int
GL_ActivateRenderer(SDL_Renderer * renderer)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
Jan 21, 2010
Jan 21, 2010
316
SDL_Window *window = renderer->window;
Jul 22, 2006
Jul 22, 2006
317
Feb 2, 2011
Feb 2, 2011
318
319
320
321
322
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
323
}
Dec 5, 2008
Dec 5, 2008
324
if (data->updateSize) {
Feb 2, 2011
Feb 2, 2011
325
326
327
int w, h;
SDL_GetWindowSize(window, &w, &h);
Dec 6, 2008
Dec 6, 2008
328
329
330
331
data->glMatrixMode(GL_PROJECTION);
data->glLoadIdentity();
data->glMatrixMode(GL_MODELVIEW);
data->glLoadIdentity();
Feb 2, 2011
Feb 2, 2011
332
333
data->glViewport(0, 0, w, h);
data->glOrtho(0.0, (GLdouble) w, (GLdouble) h, 0.0, 0.0, 1.0);
Dec 5, 2008
Dec 5, 2008
334
335
data->updateSize = SDL_FALSE;
}
Aug 6, 2006
Aug 6, 2006
336
337
338
return 0;
}
Feb 2, 2011
Feb 2, 2011
339
340
static void
GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
Aug 6, 2006
Aug 6, 2006
341
342
343
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
Feb 2, 2011
Feb 2, 2011
344
345
346
347
348
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
349
350
}
Jul 22, 2006
Jul 22, 2006
351
352
353
354
355
356
357
358
359
360
361
static __inline__ int
power_of_2(int input)
{
int value = 1;
while (value < input) {
value <<= 1;
}
return value;
}
Nov 15, 2009
Nov 15, 2009
362
363
364
static __inline__ SDL_bool
convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
GLint* internalFormat, GLenum* format, GLenum* type)
Jul 19, 2006
Jul 19, 2006
365
{
Nov 15, 2009
Nov 15, 2009
366
switch (pixel_format) {
Aug 5, 2006
Aug 5, 2006
367
case SDL_PIXELFORMAT_ARGB8888:
Nov 15, 2009
Nov 15, 2009
368
369
*internalFormat = GL_RGBA8;
*format = GL_BGRA;
Feb 3, 2011
Feb 3, 2011
370
*type = GL_UNSIGNED_INT_8_8_8_8_REV;
Dec 7, 2008
Dec 7, 2008
371
break;
Jul 22, 2006
Jul 22, 2006
372
default:
Nov 15, 2009
Nov 15, 2009
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
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;
GLenum result;
Feb 2, 2011
Feb 2, 2011
388
389
GL_ActivateRenderer(renderer);
Nov 15, 2009
Nov 15, 2009
390
391
if (!convert_format(renderdata, texture->format, &internalFormat,
&format, &type)) {
Jan 13, 2011
Jan 13, 2011
392
393
SDL_SetError("Texture format %s not supported by OpenGL",
SDL_GetPixelFormatName(texture->format));
Jul 22, 2006
Jul 22, 2006
394
395
return -1;
}
Jul 19, 2006
Jul 19, 2006
396
Jul 22, 2006
Jul 22, 2006
397
data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
Jul 19, 2006
Jul 19, 2006
398
399
400
401
402
if (!data) {
SDL_OutOfMemory();
return -1;
}
Aug 11, 2007
Aug 11, 2007
403
if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
Feb 3, 2011
Feb 3, 2011
404
data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
Aug 11, 2007
Aug 11, 2007
405
406
407
408
409
410
411
412
data->pixels = SDL_malloc(texture->h * data->pitch);
if (!data->pixels) {
SDL_OutOfMemory();
SDL_free(data);
return -1;
}
}
Jul 19, 2006
Jul 19, 2006
413
414
texture->driverdata = data;
Jul 22, 2006
Jul 22, 2006
415
416
renderdata->glGetError();
renderdata->glGenTextures(1, &data->texture);
Jul 22, 2006
Jul 22, 2006
417
418
419
420
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
421
422
data->texw = (GLfloat) texture_w;
data->texh = (GLfloat) texture_h;
Jul 22, 2006
Jul 22, 2006
423
424
425
426
} 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
427
data->texw = (GLfloat) (texture->w) / texture_w;
Jul 22, 2006
Jul 22, 2006
428
429
data->texh = (GLfloat) texture->h / texture_h;
}
Dec 6, 2008
Dec 6, 2008
430
Jul 22, 2006
Jul 22, 2006
431
432
data->format = format;
data->formattype = type;
Dec 20, 2008
Dec 20, 2008
433
renderdata->glEnable(data->type);
Jul 22, 2006
Jul 22, 2006
434
renderdata->glBindTexture(data->type, data->texture);
Aug 12, 2007
Aug 12, 2007
435
renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
Feb 1, 2011
Feb 1, 2011
436
GL_LINEAR);
Aug 12, 2007
Aug 12, 2007
437
renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
Feb 1, 2011
Feb 1, 2011
438
GL_LINEAR);
Aug 12, 2007
Aug 12, 2007
439
440
441
442
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
443
#ifdef __MACOSX__
Aug 12, 2007
Aug 12, 2007
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#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);
}
Jan 8, 2008
Jan 8, 2008
460
461
if (texture->access == SDL_TEXTUREACCESS_STREAMING
&& texture->format == SDL_PIXELFORMAT_ARGB8888) {
Aug 12, 2007
Aug 12, 2007
462
463
464
renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
texture_h, 0, format, type, data->pixels);
Feb 3, 2011
Feb 3, 2011
465
466
}
else
Aug 12, 2007
Aug 12, 2007
467
468
469
470
471
#endif
{
renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
texture_h, 0, format, type, NULL);
}
Jan 14, 2009
Jan 14, 2009
472
renderdata->glDisable(data->type);
Jul 22, 2006
Jul 22, 2006
473
result = renderdata->glGetError();
Jul 22, 2006
Jul 22, 2006
474
475
476
477
if (result != GL_NO_ERROR) {
GL_SetError("glTexImage2D()", result);
return -1;
}
Jul 19, 2006
Jul 19, 2006
478
479
480
481
482
483
484
return 0;
}
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
485
GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
Jul 19, 2006
Jul 19, 2006
486
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
Jul 22, 2006
Jul 22, 2006
487
GLenum result;
Jul 19, 2006
Jul 19, 2006
488
Feb 2, 2011
Feb 2, 2011
489
490
GL_ActivateRenderer(renderer);
Jul 22, 2006
Jul 22, 2006
491
renderdata->glGetError();
Feb 8, 2011
Feb 8, 2011
492
493
494
renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
(pitch / SDL_BYTESPERPIXEL(texture->format)));
Dec 20, 2008
Dec 20, 2008
495
renderdata->glEnable(data->type);
Jul 22, 2006
Jul 22, 2006
496
497
498
499
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
500
renderdata->glDisable(data->type);
Jul 22, 2006
Jul 22, 2006
501
result = renderdata->glGetError();
Jul 22, 2006
Jul 22, 2006
502
503
504
505
if (result != GL_NO_ERROR) {
GL_SetError("glTexSubImage2D()", result);
return -1;
}
Jul 19, 2006
Jul 19, 2006
506
507
508
509
510
return 0;
}
static int
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
Feb 3, 2011
Feb 3, 2011
511
const SDL_Rect * rect, void **pixels, int *pitch)
Jul 19, 2006
Jul 19, 2006
512
513
514
{
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
Feb 8, 2011
Feb 8, 2011
515
516
data->locked_rect = *rect;
*pixels =
Jul 22, 2006
Jul 22, 2006
517
(void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
Feb 3, 2011
Feb 3, 2011
518
rect->x * SDL_BYTESPERPIXEL(texture->format));
Jul 22, 2006
Jul 22, 2006
519
*pitch = data->pitch;
Jul 19, 2006
Jul 19, 2006
520
521
522
523
524
525
526
return 0;
}
static void
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
Feb 8, 2011
Feb 8, 2011
527
528
const SDL_Rect *rect;
void *pixels;
Jul 19, 2006
Jul 19, 2006
529
Feb 8, 2011
Feb 8, 2011
530
531
532
533
534
rect = &data->locked_rect;
pixels =
(void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format));
GL_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
Jul 19, 2006
Jul 19, 2006
535
536
}
Feb 8, 2011
Feb 8, 2011
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
static void
GL_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
GL_ActivateRenderer(renderer);
if (rect) {
int w, h;
SDL_GetWindowSize(renderer->window, &w, &h);
data->glScissor(rect->x, (h-(rect->y+rect->h)), rect->w, rect->h);
data->glEnable(GL_SCISSOR_TEST);
} else {
data->glDisable(GL_SCISSOR_TEST);
}
}
Dec 31, 2008
Dec 31, 2008
555
static void
Feb 1, 2011
Feb 1, 2011
556
GL_SetBlendMode(GL_RenderData * data, int blendMode)
Dec 31, 2008
Dec 31, 2008
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
{
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;
Feb 5, 2011
Feb 5, 2011
574
575
576
577
578
case SDL_BLENDMODE_MOD:
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
data->glEnable(GL_BLEND);
data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
break;
Dec 31, 2008
Dec 31, 2008
579
580
581
582
583
}
data->blendMode = blendMode;
}
}
Dec 20, 2008
Dec 20, 2008
584
static int
Dec 23, 2009
Dec 23, 2009
585
586
587
588
GL_RenderClear(SDL_Renderer * renderer)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
Feb 2, 2011
Feb 2, 2011
589
590
GL_ActivateRenderer(renderer);
Dec 23, 2009
Dec 23, 2009
591
592
593
594
595
596
597
598
599
600
601
602
603
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
604
605
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
Dec 9, 2009
Dec 9, 2009
606
int i;
Dec 30, 2008
Dec 30, 2008
607
Feb 2, 2011
Feb 2, 2011
608
609
GL_ActivateRenderer(renderer);
Feb 1, 2011
Feb 1, 2011
610
GL_SetBlendMode(data, renderer->blendMode);
Feb 9, 2011
Feb 9, 2011
611
GL_SelectShader(data->shaders, SHADER_SOLID);
Jul 19, 2006
Jul 19, 2006
612
Dec 20, 2008
Dec 20, 2008
613
614
615
616
data->glColor4f((GLfloat) renderer->r * inv255f,
(GLfloat) renderer->g * inv255f,
(GLfloat) renderer->b * inv255f,
(GLfloat) renderer->a * inv255f);
Dec 30, 2008
Dec 30, 2008
617
618
data->glBegin(GL_POINTS);
Dec 9, 2009
Dec 9, 2009
619
620
621
for (i = 0; i < count; ++i) {
data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
}
Dec 30, 2008
Dec 30, 2008
622
data->glEnd();
Dec 20, 2008
Dec 20, 2008
623
624
625
626
return 0;
}
Dec 21, 2008
Dec 21, 2008
627
static int
Dec 23, 2009
Dec 23, 2009
628
629
GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
int count)
Dec 21, 2008
Dec 21, 2008
630
631
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
Dec 9, 2009
Dec 9, 2009
632
int i;
Dec 21, 2008
Dec 21, 2008
633
Feb 2, 2011
Feb 2, 2011
634
635
GL_ActivateRenderer(renderer);
Feb 1, 2011
Feb 1, 2011
636
GL_SetBlendMode(data, renderer->blendMode);
Feb 9, 2011
Feb 9, 2011
637
GL_SelectShader(data->shaders, SHADER_SOLID);
Dec 21, 2008
Dec 21, 2008
638
639
640
641
642
643
data->glColor4f((GLfloat) renderer->r * inv255f,
(GLfloat) renderer->g * inv255f,
(GLfloat) renderer->b * inv255f,
(GLfloat) renderer->a * inv255f);
Dec 9, 2009
Dec 9, 2009
644
645
646
647
648
649
650
651
652
653
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
654
#if defined(__APPLE__) || defined(__WIN32__)
Nov 20, 2010
Nov 20, 2010
655
#else
Oct 17, 2010
Oct 17, 2010
656
int x1, y1, x2, y2;
Nov 20, 2010
Nov 20, 2010
657
#endif
Oct 17, 2010
Oct 17, 2010
658
Dec 9, 2009
Dec 9, 2009
659
660
661
662
663
664
665
666
667
668
669
670
671
672
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
* for diagonal lines and software render those. It's terrible, but at
* least it would be pixel perfect.
*/
data->glBegin(GL_POINTS);
Jan 24, 2011
Jan 24, 2011
673
#if defined(__APPLE__) || defined(__WIN32__)
Dec 9, 2009
Dec 9, 2009
674
675
/* Mac OS X and Windows seem to always leave the second point open */
data->glVertex2f(0.5f + points[count-1].x, 0.5f + points[count-1].y);
Nov 21, 2009
Nov 21, 2009
676
#else
Dec 9, 2009
Dec 9, 2009
677
/* Linux seems to leave the right-most or bottom-most point open */
Oct 17, 2010
Oct 17, 2010
678
679
680
681
x1 = points[0].x;
y1 = points[0].y;
x2 = points[count-1].x;
y2 = points[count-1].y;
Dec 9, 2009
Dec 9, 2009
682
683
684
685
686
687
688
689
690
691
if (x1 > x2) {
data->glVertex2f(0.5f + x1, 0.5f + y1);
} else if (x2 > x1) {
data->glVertex2f(0.5f + x2, 0.5f + y2);
} else if (y1 > y2) {
data->glVertex2f(0.5f + x1, 0.5f + y1);
} else if (y2 > y1) {
data->glVertex2f(0.5f + x2, 0.5f + y2);
}
Nov 21, 2009
Nov 21, 2009
692
#endif
Dec 9, 2009
Dec 9, 2009
693
694
data->glEnd();
}
Nov 19, 2009
Nov 19, 2009
695
Dec 21, 2008
Dec 21, 2008
696
697
698
return 0;
}
Dec 23, 2009
Dec 23, 2009
699
700
static int
GL_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count)
Dec 20, 2008
Dec 20, 2008
701
702
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
Dec 9, 2009
Dec 9, 2009
703
int i;
Dec 20, 2008
Dec 20, 2008
704
Feb 2, 2011
Feb 2, 2011
705
706
GL_ActivateRenderer(renderer);
Feb 1, 2011
Feb 1, 2011
707
GL_SetBlendMode(data, renderer->blendMode);
Feb 9, 2011
Feb 9, 2011
708
GL_SelectShader(data->shaders, SHADER_SOLID);
Dec 31, 2008
Dec 31, 2008
709
Dec 20, 2008
Dec 20, 2008
710
711
712
713
data->glColor4f((GLfloat) renderer->r * inv255f,
(GLfloat) renderer->g * inv255f,
(GLfloat) renderer->b * inv255f,
(GLfloat) renderer->a * inv255f);
Dec 31, 2008
Dec 31, 2008
714
Dec 9, 2009
Dec 9, 2009
715
716
717
718
719
for (i = 0; i < count; ++i) {
const SDL_Rect *rect = rects[i];
data->glRecti(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
}
Dec 21, 2008
Dec 21, 2008
720
Jul 19, 2006
Jul 19, 2006
721
722
723
724
725
return 0;
}
static int
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
Aug 28, 2006
Aug 28, 2006
726
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
Jul 19, 2006
Jul 19, 2006
727
728
729
730
731
732
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
int minx, miny, maxx, maxy;
GLfloat minu, maxu, minv, maxv;
Feb 2, 2011
Feb 2, 2011
733
734
GL_ActivateRenderer(renderer);
Jul 19, 2006
Jul 19, 2006
735
736
737
738
739
740
minx = dstrect->x;
miny = dstrect->y;
maxx = dstrect->x + dstrect->w;
maxy = dstrect->y + dstrect->h;
minu = (GLfloat) srcrect->x / texture->w;
Jul 22, 2006
Jul 22, 2006
741
minu *= texturedata->texw;
Jul 19, 2006
Jul 19, 2006
742
maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
Jul 22, 2006
Jul 22, 2006
743
maxu *= texturedata->texw;
Jul 19, 2006
Jul 19, 2006
744
minv = (GLfloat) srcrect->y / texture->h;
Jul 22, 2006
Jul 22, 2006
745
minv *= texturedata->texh;
Jul 19, 2006
Jul 19, 2006
746
maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
Jul 22, 2006
Jul 22, 2006
747
maxv *= texturedata->texh;
Jul 19, 2006
Jul 19, 2006
748
Dec 20, 2008
Dec 20, 2008
749
data->glEnable(texturedata->type);
Jul 22, 2006
Jul 22, 2006
750
751
data->glBindTexture(texturedata->type, texturedata->texture);
Aug 28, 2006
Aug 28, 2006
752
753
754
755
756
757
758
759
760
if (texture->modMode) {
data->glColor4f((GLfloat) texture->r * inv255f,
(GLfloat) texture->g * inv255f,
(GLfloat) texture->b * inv255f,
(GLfloat) texture->a * inv255f);
} else {
data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
Feb 1, 2011
Feb 1, 2011
761
GL_SetBlendMode(data, texture->blendMode);
Feb 9, 2011
Feb 9, 2011
762
GL_SelectShader(data->shaders, SHADER_RGB);
Nov 21, 2009
Nov 21, 2009
763
Jul 22, 2006
Jul 22, 2006
764
765
data->glBegin(GL_TRIANGLE_STRIP);
data->glTexCoord2f(minu, minv);
Nov 21, 2009
Nov 21, 2009
766
data->glVertex2f((GLfloat) minx, (GLfloat) miny);
Jul 22, 2006
Jul 22, 2006
767
data->glTexCoord2f(maxu, minv);
Nov 21, 2009
Nov 21, 2009
768
data->glVertex2f((GLfloat) maxx, (GLfloat) miny);
Jul 22, 2006
Jul 22, 2006
769
data->glTexCoord2f(minu, maxv);
Nov 21, 2009
Nov 21, 2009
770
data->glVertex2f((GLfloat) minx, (GLfloat) maxy);
Jul 22, 2006
Jul 22, 2006
771
data->glTexCoord2f(maxu, maxv);
Nov 21, 2009
Nov 21, 2009
772
data->glVertex2f((GLfloat) maxx, (GLfloat) maxy);
Jul 22, 2006
Jul 22, 2006
773
data->glEnd();
Jul 19, 2006
Jul 19, 2006
774
Dec 20, 2008
Dec 20, 2008
775
776
data->glDisable(texturedata->type);
Jul 19, 2006
Jul 19, 2006
777
778
779
return 0;
}
Nov 15, 2009
Nov 15, 2009
780
781
static int
GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Nov 16, 2009
Nov 16, 2009
782
Uint32 pixel_format, void * pixels, int pitch)
Nov 15, 2009
Nov 15, 2009
783
{
Nov 15, 2009
Nov 15, 2009
784
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
Jan 21, 2010
Jan 21, 2010
785
SDL_Window *window = renderer->window;
Nov 15, 2009
Nov 15, 2009
786
787
GLint internalFormat;
GLenum format, type;
Nov 16, 2009
Nov 16, 2009
788
Uint8 *src, *dst, *tmp;
Feb 2, 2011
Feb 2, 2011
789
int w, h, length, rows;
Nov 15, 2009
Nov 15, 2009
790
Feb 2, 2011
Feb 2, 2011
791
792
GL_ActivateRenderer(renderer);
Nov 15, 2009
Nov 15, 2009
793
if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
Nov 16, 2009
Nov 16, 2009
794
/* FIXME: Do a temp copy to a format that is supported */
Nov 15, 2009
Nov 15, 2009
795
796
797
798
SDL_SetError("Unsupported pixel format");
return -1;
}
Feb 2, 2011
Feb 2, 2011
799
800
SDL_GetWindowSize(window, &w, &h);
Nov 18, 2009
Nov 18, 2009
801
802
data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
data->glPixelStorei(GL_PACK_ROW_LENGTH,
Feb 3, 2011
Feb 3, 2011
803
(pitch / SDL_BYTESPERPIXEL(pixel_format)));
Nov 15, 2009
Nov 15, 2009
804
Feb 2, 2011
Feb 2, 2011
805
data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
Nov 16, 2009
Nov 16, 2009
806
807
808
format, type, pixels);
/* Flip the rows to be top-down */
Feb 3, 2011
Feb 3, 2011
809
length = rect->w * SDL_BYTESPERPIXEL(pixel_format);
Nov 16, 2009
Nov 16, 2009
810
811
812
813
814
815
816
817
src = (Uint8*)pixels + (rect->h-1)*pitch;
dst = (Uint8*)pixels;
tmp = SDL_stack_alloc(Uint8, length);
rows = rect->h / 2;
while (rows--) {
SDL_memcpy(tmp, dst, length);
SDL_memcpy(dst, src, length);
SDL_memcpy(src, tmp, length);
Nov 18, 2009
Nov 18, 2009
818
819
dst += pitch;
src -= pitch;
Nov 16, 2009
Nov 16, 2009
820
821
}
SDL_stack_free(tmp);
Nov 17, 2009
Nov 17, 2009
822
823
return 0;
Nov 15, 2009
Nov 15, 2009
824
825
}
Jul 19, 2006
Jul 19, 2006
826
827
828
static void
GL_RenderPresent(SDL_Renderer * renderer)
{
Feb 2, 2011
Feb 2, 2011
829
830
GL_ActivateRenderer(renderer);
Jul 19, 2006
Jul 19, 2006
831
832
833
834
835
836
SDL_GL_SwapWindow(renderer->window);
}
static void
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
Jul 22, 2006
Jul 22, 2006
837
GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
Jul 19, 2006
Jul 19, 2006
838
839
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
Feb 2, 2011
Feb 2, 2011
840
841
GL_ActivateRenderer(renderer);
Jul 19, 2006
Jul 19, 2006
842
843
844
845
if (!data) {
return;
}
if (data->texture) {
Jul 22, 2006
Jul 22, 2006
846
renderdata->glDeleteTextures(1, &data->texture);
Jul 22, 2006
Jul 22, 2006
847
848
849
}
if (data->pixels) {
SDL_free(data->pixels);
Jul 19, 2006
Jul 19, 2006
850
851
852
853
854
}
SDL_free(data);
texture->driverdata = NULL;
}
Aug 7, 2006
Aug 7, 2006
855
static void
Jul 19, 2006
Jul 19, 2006
856
857
858
859
860
GL_DestroyRenderer(SDL_Renderer * renderer)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
if (data) {
Jul 22, 2006
Jul 22, 2006
861
if (data->context) {
Mar 14, 2008
Mar 14, 2008
862
/* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
Jul 22, 2006
Jul 22, 2006
863
SDL_GL_DeleteContext(data->context);
Jul 19, 2006
Jul 19, 2006
864
865
866
867
868
869
}
SDL_free(data);
}
SDL_free(renderer);
}
Feb 8, 2011
Feb 8, 2011
870
#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
Jul 19, 2006
Jul 19, 2006
871
872
/* vi: set ts=4 sw=4 expandtab: */