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

Latest commit

 

History

History
executable file
·
1350 lines (1182 loc) · 42.7 KB

SDL_render_gles2.c

File metadata and controls

executable file
·
1350 lines (1182 loc) · 42.7 KB
 
Jun 22, 2011
Jun 22, 2011
1
2
/*
Simple DirectMedia Layer
Dec 31, 2011
Dec 31, 2011
3
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
Jun 22, 2011
Jun 22, 2011
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
#if SDL_VIDEO_RENDER_OGL_ES2 && !SDL_RENDER_DISABLED
#include "SDL_hints.h"
#include "SDL_opengles2.h"
#include "../SDL_sysrender.h"
#include "SDL_shaders_gles2.h"
Jan 8, 2012
Jan 8, 2012
30
31
32
/* Used to re-create the window with OpenGL capability */
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
Jun 22, 2011
Jun 22, 2011
33
34
35
36
37
38
39
40
41
42
43
/*************************************************************************************************
* Bootstrap data *
*************************************************************************************************/
static SDL_Renderer *GLES2_CreateRenderer(SDL_Window *window, Uint32 flags);
SDL_RenderDriver GLES2_RenderDriver = {
GLES2_CreateRenderer,
{
"opengles2",
(SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
Dec 29, 2011
Dec 29, 2011
44
45
46
47
48
4,
{SDL_PIXELFORMAT_ABGR8888,
SDL_PIXELFORMAT_ARGB8888,
SDL_PIXELFORMAT_RGB888,
SDL_PIXELFORMAT_BGR888},
Jun 22, 2011
Jun 22, 2011
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
0,
0
}
};
/*************************************************************************************************
* Context structures *
*************************************************************************************************/
typedef struct GLES2_TextureData
{
GLenum texture;
GLenum texture_type;
GLenum pixel_format;
GLenum pixel_type;
void *pixel_data;
size_t pitch;
} GLES2_TextureData;
typedef struct GLES2_ShaderCacheEntry
{
GLuint id;
GLES2_ShaderType type;
const GLES2_ShaderInstance *instance;
int references;
struct GLES2_ShaderCacheEntry *prev;
struct GLES2_ShaderCacheEntry *next;
} GLES2_ShaderCacheEntry;
typedef struct GLES2_ShaderCache
{
int count;
GLES2_ShaderCacheEntry *head;
} GLES2_ShaderCache;
typedef struct GLES2_ProgramCacheEntry
{
GLuint id;
SDL_BlendMode blend_mode;
GLES2_ShaderCacheEntry *vertex_shader;
GLES2_ShaderCacheEntry *fragment_shader;
GLuint uniform_locations[16];
struct GLES2_ProgramCacheEntry *prev;
struct GLES2_ProgramCacheEntry *next;
} GLES2_ProgramCacheEntry;
typedef struct GLES2_ProgramCache
{
int count;
GLES2_ProgramCacheEntry *head;
GLES2_ProgramCacheEntry *tail;
} GLES2_ProgramCache;
typedef enum
{
GLES2_ATTRIBUTE_POSITION = 0,
GLES2_ATTRIBUTE_TEXCOORD = 1
} GLES2_Attribute;
typedef enum
{
GLES2_UNIFORM_PROJECTION,
GLES2_UNIFORM_TEXTURE,
GLES2_UNIFORM_MODULATION,
GLES2_UNIFORM_COLOR,
GLES2_UNIFORM_COLORTABLE
} GLES2_Uniform;
typedef enum
{
GLES2_IMAGESOURCE_SOLID,
Dec 29, 2011
Dec 29, 2011
120
121
122
123
GLES2_IMAGESOURCE_TEXTURE_ABGR,
GLES2_IMAGESOURCE_TEXTURE_ARGB,
GLES2_IMAGESOURCE_TEXTURE_RGB,
GLES2_IMAGESOURCE_TEXTURE_BGR
Jun 22, 2011
Jun 22, 2011
124
125
126
127
128
129
130
131
132
133
} GLES2_ImageSource;
typedef struct GLES2_DriverContext
{
SDL_GLContext *context;
struct {
int blendMode;
SDL_bool tex_coords;
} current;
Jan 8, 2012
Jan 8, 2012
134
135
136
137
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
#include "SDL_gles2funcs.h"
#undef SDL_PROC
Jun 22, 2011
Jun 22, 2011
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
int shader_format_count;
GLenum *shader_formats;
GLES2_ShaderCache shader_cache;
GLES2_ProgramCache program_cache;
GLES2_ProgramCacheEntry *current_program;
} GLES2_DriverContext;
#define GLES2_MAX_CACHED_PROGRAMS 8
/*************************************************************************************************
* Renderer state APIs *
*************************************************************************************************/
static int GLES2_ActivateRenderer(SDL_Renderer *renderer);
static void GLES2_WindowEvent(SDL_Renderer * renderer,
const SDL_WindowEvent *event);
static int GLES2_UpdateViewport(SDL_Renderer * renderer);
static void GLES2_DestroyRenderer(SDL_Renderer *renderer);
static SDL_GLContext SDL_CurrentContext = NULL;
Jan 8, 2012
Jan 8, 2012
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
static int GLES2_LoadFunctions(GLES2_DriverContext * 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 GLES2 function %s: %s\n", #func, SDL_GetError()); \
return -1; \
} \
} while ( 0 );
#endif /* _SDL_NOGETPROCADDR_ */
#include "SDL_gles2funcs.h"
#undef SDL_PROC
return 0;
}
Jun 22, 2011
Jun 22, 2011
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
static int
GLES2_ActivateRenderer(SDL_Renderer * renderer)
{
GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
if (SDL_CurrentContext != rdata->context) {
/* Null out the current program to ensure we set it again */
rdata->current_program = NULL;
if (SDL_GL_MakeCurrent(renderer->window, rdata->context) < 0) {
return -1;
}
SDL_CurrentContext = rdata->context;
GLES2_UpdateViewport(renderer);
}
return 0;
}
static void
GLES2_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
{
Jan 8, 2012
Jan 8, 2012
201
202
GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
Jun 22, 2011
Jun 22, 2011
203
204
205
206
if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
/* Rebind the context to the window area */
SDL_CurrentContext = NULL;
}
Nov 8, 2011
Nov 8, 2011
207
208
209
if (event->event == SDL_WINDOWEVENT_MINIMIZED) {
/* According to Apple documentation, we need to finish drawing NOW! */
Jan 8, 2012
Jan 8, 2012
210
rdata->glFinish();
Nov 8, 2011
Nov 8, 2011
211
}
Jun 22, 2011
Jun 22, 2011
212
213
214
215
216
217
218
219
220
221
222
223
}
static int
GLES2_UpdateViewport(SDL_Renderer * renderer)
{
GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
if (SDL_CurrentContext != rdata->context) {
/* We'll update the viewport after we rebind the context */
return 0;
}
Jan 8, 2012
Jan 8, 2012
224
rdata->glViewport(renderer->viewport.x, renderer->viewport.y,
Jun 22, 2011
Jun 22, 2011
225
226
227
228
229
230
231
232
233
234
235
236
237
renderer->viewport.w, renderer->viewport.h);
return 0;
}
static void
GLES2_DestroyRenderer(SDL_Renderer *renderer)
{
GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
/* Deallocate everything */
if (rdata) {
GLES2_ActivateRenderer(renderer);
Jan 8, 2012
Jan 8, 2012
238
239
240
241
242
243
{
GLES2_ShaderCacheEntry *entry;
GLES2_ShaderCacheEntry *next;
entry = rdata->shader_cache.head;
while (entry)
{
Jan 8, 2012
Jan 8, 2012
244
rdata->glDeleteShader(entry->id);
Nov 7, 2011
Nov 7, 2011
245
246
247
next = entry->next;
SDL_free(entry);
entry = next;
Jan 8, 2012
Jan 8, 2012
248
249
250
251
252
}
}
{
GLES2_ProgramCacheEntry *entry;
GLES2_ProgramCacheEntry *next;
Nov 7, 2011
Nov 7, 2011
253
254
entry = rdata->program_cache.head;
while (entry) {
Jan 8, 2012
Jan 8, 2012
255
rdata->glDeleteProgram(entry->id);
Nov 7, 2011
Nov 7, 2011
256
257
258
259
next = entry->next;
SDL_free(entry);
entry = next;
}
Jan 8, 2012
Jan 8, 2012
260
}
Jun 22, 2011
Jun 22, 2011
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
if (rdata->context) {
SDL_GL_DeleteContext(rdata->context);
}
if (rdata->shader_formats) {
SDL_free(rdata->shader_formats);
}
SDL_free(rdata);
}
SDL_free(renderer);
}
/*************************************************************************************************
* Texture APIs *
*************************************************************************************************/
static int GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture);
static void GLES2_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture);
static int GLES2_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
void **pixels, int *pitch);
static void GLES2_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture);
static int GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
const void *pixels, int pitch);
static GLenum
GetScaleQuality(void)
{
const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
return GL_NEAREST;
} else {
return GL_LINEAR;
}
}
static int
GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
{
Jan 8, 2012
Jan 8, 2012
299
GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
Jun 22, 2011
Jun 22, 2011
300
301
302
303
304
305
306
307
308
309
310
GLES2_TextureData *tdata;
GLenum format;
GLenum type;
GLenum scaleMode;
GLES2_ActivateRenderer(renderer);
/* Determine the corresponding GLES texture format params */
switch (texture->format)
{
case SDL_PIXELFORMAT_ABGR8888:
Dec 29, 2011
Dec 29, 2011
311
312
313
case SDL_PIXELFORMAT_ARGB8888:
case SDL_PIXELFORMAT_BGR888:
case SDL_PIXELFORMAT_RGB888:
Jun 22, 2011
Jun 22, 2011
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
format = GL_RGBA;
type = GL_UNSIGNED_BYTE;
break;
default:
SDL_SetError("Texture format not supported");
return -1;
}
/* Allocate a texture struct */
tdata = (GLES2_TextureData *)SDL_calloc(1, sizeof(GLES2_TextureData));
if (!tdata)
{
SDL_OutOfMemory();
return -1;
}
tdata->texture = 0;
tdata->texture_type = GL_TEXTURE_2D;
tdata->pixel_format = format;
tdata->pixel_type = type;
scaleMode = GetScaleQuality();
/* Allocate a blob for image data */
if (texture->access == SDL_TEXTUREACCESS_STREAMING)
{
tdata->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
tdata->pixel_data = SDL_calloc(1, tdata->pitch * texture->h);
if (!tdata->pixel_data)
{
SDL_OutOfMemory();
SDL_free(tdata);
return -1;
}
}
/* Allocate the texture */
Jan 8, 2012
Jan 8, 2012
349
350
351
352
353
354
355
356
357
358
rdata->glGetError();
rdata->glGenTextures(1, &tdata->texture);
rdata->glActiveTexture(GL_TEXTURE0);
rdata->glBindTexture(tdata->texture_type, tdata->texture);
rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_MIN_FILTER, scaleMode);
rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_MAG_FILTER, scaleMode);
rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
rdata->glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
rdata->glTexImage2D(tdata->texture_type, 0, format, texture->w, texture->h, 0, format, type, NULL);
if (rdata->glGetError() != GL_NO_ERROR)
Jun 22, 2011
Jun 22, 2011
359
360
{
SDL_SetError("Texture creation failed");
Jan 8, 2012
Jan 8, 2012
361
rdata->glDeleteTextures(1, &tdata->texture);
Jun 22, 2011
Jun 22, 2011
362
363
364
365
366
367
368
369
370
371
SDL_free(tdata);
return -1;
}
texture->driverdata = tdata;
return 0;
}
static void
GLES2_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture)
{
Jan 8, 2012
Jan 8, 2012
372
GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
Jun 22, 2011
Jun 22, 2011
373
374
375
376
377
378
379
GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
GLES2_ActivateRenderer(renderer);
/* Destroy the texture */
if (tdata)
{
Jan 8, 2012
Jan 8, 2012
380
rdata->glDeleteTextures(1, &tdata->texture);
Jun 22, 2011
Jun 22, 2011
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
SDL_free(tdata->pixel_data);
SDL_free(tdata);
texture->driverdata = NULL;
}
}
static int
GLES2_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
void **pixels, int *pitch)
{
GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
/* Retrieve the buffer/pitch for the specified region */
*pixels = (Uint8 *)tdata->pixel_data +
(tdata->pitch * rect->y) +
(rect->x * SDL_BYTESPERPIXEL(texture->format));
*pitch = tdata->pitch;
return 0;
}
static void
GLES2_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture)
{
GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
SDL_Rect rect;
/* We do whole texture updates, at least for now */
rect.x = 0;
rect.y = 0;
rect.w = texture->w;
rect.h = texture->h;
GLES2_UpdateTexture(renderer, texture, &rect, tdata->pixel_data, tdata->pitch);
}
static int
GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
const void *pixels, int pitch)
{
Jan 8, 2012
Jan 8, 2012
420
GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
Jun 22, 2011
Jun 22, 2011
421
422
423
424
425
426
427
GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
Uint8 *blob = NULL;
Uint8 *src;
int srcPitch;
int y;
GLES2_ActivateRenderer(renderer);
Jan 8, 2012
Jan 8, 2012
428
Jun 22, 2011
Jun 22, 2011
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/* Bail out if we're supposed to update an empty rectangle */
if (rect->w <= 0 || rect->h <= 0)
return 0;
/* Reformat the texture data into a tightly packed array */
srcPitch = rect->w * SDL_BYTESPERPIXEL(texture->format);
src = (Uint8 *)pixels;
if (pitch != srcPitch)
{
blob = (Uint8 *)SDL_malloc(srcPitch * rect->h);
if (!blob)
{
SDL_OutOfMemory();
return -1;
}
src = blob;
for (y = 0; y < rect->h; ++y)
{
SDL_memcpy(src, pixels, srcPitch);
src += srcPitch;
pixels = (Uint8 *)pixels + pitch;
}
src = blob;
}
/* Create a texture subimage with the supplied data */
Jan 8, 2012
Jan 8, 2012
455
456
457
458
459
rdata->glGetError();
rdata->glActiveTexture(GL_TEXTURE0);
rdata->glBindTexture(tdata->texture_type, tdata->texture);
rdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
rdata->glTexSubImage2D(tdata->texture_type,
Jun 22, 2011
Jun 22, 2011
460
461
462
463
464
465
466
467
468
469
470
471
0,
rect->x,
rect->y,
rect->w,
rect->h,
tdata->pixel_format,
tdata->pixel_type,
src);
if (blob) {
SDL_free(blob);
}
Jan 8, 2012
Jan 8, 2012
472
if (rdata->glGetError() != GL_NO_ERROR)
Jun 22, 2011
Jun 22, 2011
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
{
SDL_SetError("Failed to update texture");
return -1;
}
return 0;
}
/*************************************************************************************************
* Shader management functions *
*************************************************************************************************/
static GLES2_ShaderCacheEntry *GLES2_CacheShader(SDL_Renderer *renderer, GLES2_ShaderType type,
SDL_BlendMode blendMode);
static void GLES2_EvictShader(SDL_Renderer *renderer, GLES2_ShaderCacheEntry *entry);
static GLES2_ProgramCacheEntry *GLES2_CacheProgram(SDL_Renderer *renderer,
GLES2_ShaderCacheEntry *vertex,
GLES2_ShaderCacheEntry *fragment,
SDL_BlendMode blendMode);
static int GLES2_SelectProgram(SDL_Renderer *renderer, GLES2_ImageSource source,
SDL_BlendMode blendMode);
static int GLES2_SetOrthographicProjection(SDL_Renderer *renderer);
static GLES2_ProgramCacheEntry *
GLES2_CacheProgram(SDL_Renderer *renderer, GLES2_ShaderCacheEntry *vertex,
GLES2_ShaderCacheEntry *fragment, SDL_BlendMode blendMode)
{
GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
GLES2_ProgramCacheEntry *entry;
GLES2_ShaderCacheEntry *shaderEntry;
GLint linkSuccessful;
/* Check if we've already cached this program */
entry = rdata->program_cache.head;
while (entry)
{
if (entry->vertex_shader == vertex && entry->fragment_shader == fragment)
break;
entry = entry->next;
}
if (entry)
{
Nov 8, 2011
Nov 8, 2011
514
if (rdata->program_cache.head != entry)
Jun 22, 2011
Jun 22, 2011
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
{
if (entry->next)
entry->next->prev = entry->prev;
if (entry->prev)
entry->prev->next = entry->next;
entry->prev = NULL;
entry->next = rdata->program_cache.head;
rdata->program_cache.head->prev = entry;
rdata->program_cache.head = entry;
}
return entry;
}
/* Create a program cache entry */
entry = (GLES2_ProgramCacheEntry *)SDL_calloc(1, sizeof(GLES2_ProgramCacheEntry));
if (!entry)
{
SDL_OutOfMemory();
return NULL;
}
entry->vertex_shader = vertex;
entry->fragment_shader = fragment;
entry->blend_mode = blendMode;
/* Create the program and link it */
Jan 8, 2012
Jan 8, 2012
540
541
542
543
544
545
546
547
548
rdata->glGetError();
entry->id = rdata->glCreateProgram();
rdata->glAttachShader(entry->id, vertex->id);
rdata->glAttachShader(entry->id, fragment->id);
rdata->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_POSITION, "a_position");
rdata->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_TEXCOORD, "a_texCoord");
rdata->glLinkProgram(entry->id);
rdata->glGetProgramiv(entry->id, GL_LINK_STATUS, &linkSuccessful);
if (rdata->glGetError() != GL_NO_ERROR || !linkSuccessful)
Jun 22, 2011
Jun 22, 2011
549
550
{
SDL_SetError("Failed to link shader program");
Jan 8, 2012
Jan 8, 2012
551
rdata->glDeleteProgram(entry->id);
Jun 22, 2011
Jun 22, 2011
552
553
554
555
556
557
SDL_free(entry);
return NULL;
}
/* Predetermine locations of uniform variables */
entry->uniform_locations[GLES2_UNIFORM_PROJECTION] =
Jan 8, 2012
Jan 8, 2012
558
rdata->glGetUniformLocation(entry->id, "u_projection");
Jun 22, 2011
Jun 22, 2011
559
entry->uniform_locations[GLES2_UNIFORM_TEXTURE] =
Jan 8, 2012
Jan 8, 2012
560
rdata->glGetUniformLocation(entry->id, "u_texture");
Jun 22, 2011
Jun 22, 2011
561
entry->uniform_locations[GLES2_UNIFORM_MODULATION] =
Jan 8, 2012
Jan 8, 2012
562
rdata->glGetUniformLocation(entry->id, "u_modulation");
Jun 22, 2011
Jun 22, 2011
563
entry->uniform_locations[GLES2_UNIFORM_COLOR] =
Jan 8, 2012
Jan 8, 2012
564
rdata->glGetUniformLocation(entry->id, "u_color");
Jun 22, 2011
Jun 22, 2011
565
entry->uniform_locations[GLES2_UNIFORM_COLORTABLE] =
Jan 8, 2012
Jan 8, 2012
566
rdata->glGetUniformLocation(entry->id, "u_colorTable");
Jun 22, 2011
Jun 22, 2011
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/* Cache the linked program */
if (rdata->program_cache.head)
{
entry->next = rdata->program_cache.head;
rdata->program_cache.head->prev = entry;
}
else
{
rdata->program_cache.tail = entry;
}
rdata->program_cache.head = entry;
++rdata->program_cache.count;
/* Increment the refcount of the shaders we're using */
++vertex->references;
++fragment->references;
/* Evict the last entry from the cache if we exceed the limit */
if (rdata->program_cache.count > GLES2_MAX_CACHED_PROGRAMS)
{
shaderEntry = rdata->program_cache.tail->vertex_shader;
if (--shaderEntry->references <= 0)
GLES2_EvictShader(renderer, shaderEntry);
shaderEntry = rdata->program_cache.tail->fragment_shader;
if (--shaderEntry->references <= 0)
GLES2_EvictShader(renderer, shaderEntry);
Jan 8, 2012
Jan 8, 2012
594
rdata->glDeleteProgram(rdata->program_cache.tail->id);
Jun 22, 2011
Jun 22, 2011
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
rdata->program_cache.tail = rdata->program_cache.tail->prev;
SDL_free(rdata->program_cache.tail->next);
rdata->program_cache.tail->next = NULL;
--rdata->program_cache.count;
}
return entry;
}
static GLES2_ShaderCacheEntry *
GLES2_CacheShader(SDL_Renderer *renderer, GLES2_ShaderType type, SDL_BlendMode blendMode)
{
GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
const GLES2_Shader *shader;
const GLES2_ShaderInstance *instance = NULL;
GLES2_ShaderCacheEntry *entry = NULL;
GLint compileSuccessful = GL_FALSE;
int i, j;
/* Find the corresponding shader */
shader = GLES2_GetShader(type, blendMode);
if (!shader)
{
SDL_SetError("No shader matching the requested characteristics was found");
return NULL;
}
/* Find a matching shader instance that's supported on this hardware */
for (i = 0; i < shader->instance_count && !instance; ++i)
{
for (j = 0; j < rdata->shader_format_count && !instance; ++j)
{
if (!shader->instances)
continue;
if (!shader->instances[i])
continue;
if (shader->instances[i]->format != rdata->shader_formats[j])
continue;
instance = shader->instances[i];
}
}
if (!instance)
{
SDL_SetError("The specified shader cannot be loaded on the current platform");
return NULL;
}
/* Check if we've already cached this shader */
entry = rdata->shader_cache.head;
while (entry)
{
if (entry->instance == instance)
break;
entry = entry->next;
}
if (entry)
return entry;
/* Create a shader cache entry */
entry = (GLES2_ShaderCacheEntry *)SDL_calloc(1, sizeof(GLES2_ShaderCacheEntry));
if (!entry)
{
SDL_OutOfMemory();
return NULL;
}
entry->type = type;
entry->instance = instance;
/* Compile or load the selected shader instance */
Jan 8, 2012
Jan 8, 2012
663
664
rdata->glGetError();
entry->id = rdata->glCreateShader(instance->type);
Jun 22, 2011
Jun 22, 2011
665
666
if (instance->format == (GLenum)-1)
{
Jan 8, 2012
Jan 8, 2012
667
668
669
rdata->glShaderSource(entry->id, 1, (const char **)&instance->data, NULL);
rdata->glCompileShader(entry->id);
rdata->glGetShaderiv(entry->id, GL_COMPILE_STATUS, &compileSuccessful);
Jun 22, 2011
Jun 22, 2011
670
671
672
}
else
{
Jan 8, 2012
Jan 8, 2012
673
rdata->glShaderBinary(1, &entry->id, instance->format, instance->data, instance->length);
Jun 22, 2011
Jun 22, 2011
674
675
compileSuccessful = GL_TRUE;
}
Jan 8, 2012
Jan 8, 2012
676
if (rdata->glGetError() != GL_NO_ERROR || !compileSuccessful)
Jun 22, 2011
Jun 22, 2011
677
678
{
char *info = NULL;
Dec 29, 2011
Dec 29, 2011
679
int length = 0;
Jun 22, 2011
Jun 22, 2011
680
Jan 8, 2012
Jan 8, 2012
681
rdata->glGetShaderiv(entry->id, GL_INFO_LOG_LENGTH, &length);
Jun 22, 2011
Jun 22, 2011
682
683
684
if (length > 0) {
info = SDL_stack_alloc(char, length);
if (info) {
Jan 8, 2012
Jan 8, 2012
685
rdata->glGetShaderInfoLog(entry->id, length, &length, info);
Jun 22, 2011
Jun 22, 2011
686
687
688
689
690
691
692
693
}
}
if (info) {
SDL_SetError("Failed to load the shader: %s", info);
SDL_stack_free(info);
} else {
SDL_SetError("Failed to load the shader");
}
Jan 8, 2012
Jan 8, 2012
694
rdata->glDeleteShader(entry->id);
Jun 22, 2011
Jun 22, 2011
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
SDL_free(entry);
return NULL;
}
/* Link the shader entry in at the front of the cache */
if (rdata->shader_cache.head)
{
entry->next = rdata->shader_cache.head;
rdata->shader_cache.head->prev = entry;
}
rdata->shader_cache.head = entry;
++rdata->shader_cache.count;
return entry;
}
static void
GLES2_EvictShader(SDL_Renderer *renderer, GLES2_ShaderCacheEntry *entry)
{
GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
/* Unlink the shader from the cache */
if (entry->next)
entry->next->prev = entry->prev;
if (entry->prev)
entry->prev->next = entry->next;
if (rdata->shader_cache.head == entry)
rdata->shader_cache.head = entry->next;
--rdata->shader_cache.count;
/* Deallocate the shader */
Jan 8, 2012
Jan 8, 2012
725
rdata->glDeleteShader(entry->id);
Jun 22, 2011
Jun 22, 2011
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
SDL_free(entry);
}
static int
GLES2_SelectProgram(SDL_Renderer *renderer, GLES2_ImageSource source, SDL_BlendMode blendMode)
{
GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
GLES2_ShaderCacheEntry *vertex = NULL;
GLES2_ShaderCacheEntry *fragment = NULL;
GLES2_ShaderType vtype, ftype;
GLES2_ProgramCacheEntry *program;
/* Select an appropriate shader pair for the specified modes */
vtype = GLES2_SHADER_VERTEX_DEFAULT;
switch (source)
{
case GLES2_IMAGESOURCE_SOLID:
ftype = GLES2_SHADER_FRAGMENT_SOLID_SRC;
break;
Dec 29, 2011
Dec 29, 2011
745
746
747
748
749
750
751
752
753
754
755
case GLES2_IMAGESOURCE_TEXTURE_ABGR:
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_ABGR_SRC;
break;
case GLES2_IMAGESOURCE_TEXTURE_ARGB:
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_ARGB_SRC;
break;
case GLES2_IMAGESOURCE_TEXTURE_RGB:
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_RGB_SRC;
break;
case GLES2_IMAGESOURCE_TEXTURE_BGR:
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_BGR_SRC;
Jun 22, 2011
Jun 22, 2011
756
break;
Dec 29, 2011
Dec 29, 2011
757
758
default:
goto fault;
Jun 22, 2011
Jun 22, 2011
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
}
/* Load the requested shaders */
vertex = GLES2_CacheShader(renderer, vtype, blendMode);
if (!vertex)
goto fault;
fragment = GLES2_CacheShader(renderer, ftype, blendMode);
if (!fragment)
goto fault;
/* Check if we need to change programs at all */
if (rdata->current_program &&
rdata->current_program->vertex_shader == vertex &&
rdata->current_program->fragment_shader == fragment)
return 0;
/* Generate a matching program */
program = GLES2_CacheProgram(renderer, vertex, fragment, blendMode);
if (!program)
goto fault;
/* Select that program in OpenGL */
Jan 8, 2012
Jan 8, 2012
781
782
783
rdata->glGetError();
rdata->glUseProgram(program->id);
if (rdata->glGetError() != GL_NO_ERROR)
Jun 22, 2011
Jun 22, 2011
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
{
SDL_SetError("Failed to select program");
goto fault;
}
/* Set the current program */
rdata->current_program = program;
/* Activate an orthographic projection */
if (GLES2_SetOrthographicProjection(renderer) < 0)
goto fault;
/* Clean up and return */
return 0;
fault:
if (vertex && vertex->references <= 0)
GLES2_EvictShader(renderer, vertex);
if (fragment && fragment->references <= 0)
GLES2_EvictShader(renderer, fragment);
rdata->current_program = NULL;
return -1;
}
static int
GLES2_SetOrthographicProjection(SDL_Renderer *renderer)
{
GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
GLfloat projection[4][4];
GLuint locProjection;
/* Prepare an orthographic projection */
projection[0][0] = 2.0f / renderer->viewport.w;
projection[0][1] = 0.0f;
projection[0][2] = 0.0f;
projection[0][3] = 0.0f;
projection[1][0] = 0.0f;
projection[1][1] = -2.0f / renderer->viewport.h;
projection[1][2] = 0.0f;
projection[1][3] = 0.0f;
projection[2][0] = 0.0f;
projection[2][1] = 0.0f;
projection[2][2] = 1.0f;
projection[2][3] = 0.0f;
projection[3][0] = -1.0f;
projection[3][1] = 1.0f;
projection[3][2] = 0.0f;
projection[3][3] = 1.0f;
/* Set the projection matrix */
locProjection = rdata->current_program->uniform_locations[GLES2_UNIFORM_PROJECTION];
Jan 8, 2012
Jan 8, 2012
834
835
836
rdata->glGetError();
rdata->glUniformMatrix4fv(locProjection, 1, GL_FALSE, (GLfloat *)projection);
if (rdata->glGetError() != GL_NO_ERROR)
Jun 22, 2011
Jun 22, 2011
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
{
SDL_SetError("Failed to set orthographic projection");
return -1;
}
return 0;
}
/*************************************************************************************************
* Rendering functions *
*************************************************************************************************/
static const float inv255f = 1.0f / 255.0f;
static int GLES2_RenderClear(SDL_Renderer *renderer);
static int GLES2_RenderDrawPoints(SDL_Renderer *renderer, const SDL_Point *points, int count);
static int GLES2_RenderDrawLines(SDL_Renderer *renderer, const SDL_Point *points, int count);
static int GLES2_RenderFillRects(SDL_Renderer *renderer, const SDL_Rect *rects, int count);
static int GLES2_RenderCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect,
const SDL_Rect *dstrect);
Oct 31, 2011
Oct 31, 2011
856
857
static int GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 pixel_format, void * pixels, int pitch);
Jun 22, 2011
Jun 22, 2011
858
859
860
861
862
863
static void GLES2_RenderPresent(SDL_Renderer *renderer);
static int
GLES2_RenderClear(SDL_Renderer * renderer)
{
Jan 8, 2012
Jan 8, 2012
864
GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
Jun 22, 2011
Jun 22, 2011
865
Jan 8, 2012
Jan 8, 2012
866
867
868
GLES2_ActivateRenderer(renderer);
rdata->glClearColor((GLfloat) renderer->r * inv255f,
Jun 22, 2011
Jun 22, 2011
869
870
871
872
(GLfloat) renderer->g * inv255f,
(GLfloat) renderer->b * inv255f,
(GLfloat) renderer->a * inv255f);
Jan 8, 2012
Jan 8, 2012
873
rdata->glClear(GL_COLOR_BUFFER_BIT);
Jun 22, 2011
Jun 22, 2011
874
875
876
877
878
879
880
881
882
883
884
return 0;
}
static void
GLES2_SetBlendMode(GLES2_DriverContext *rdata, int blendMode)
{
if (blendMode != rdata->current.blendMode) {
switch (blendMode) {
default:
case SDL_BLENDMODE_NONE:
Jan 8, 2012
Jan 8, 2012
885
rdata->glDisable(GL_BLEND);
Jun 22, 2011
Jun 22, 2011
886
887
break;
case SDL_BLENDMODE_BLEND:
Jan 8, 2012
Jan 8, 2012
888
889
rdata->glEnable(GL_BLEND);
rdata->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Jun 22, 2011
Jun 22, 2011
890
891
break;
case SDL_BLENDMODE_ADD:
Jan 8, 2012
Jan 8, 2012
892
893
rdata->glEnable(GL_BLEND);
rdata->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
Jun 22, 2011
Jun 22, 2011
894
895
break;
case SDL_BLENDMODE_MOD:
Jan 8, 2012
Jan 8, 2012
896
897
rdata->glEnable(GL_BLEND);
rdata->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
Jun 22, 2011
Jun 22, 2011
898
899
900
901
902
903
904
905
906
907
908
break;
}
rdata->current.blendMode = blendMode;
}
}
static void
GLES2_SetTexCoords(GLES2_DriverContext * rdata, SDL_bool enabled)
{
if (enabled != rdata->current.tex_coords) {
if (enabled) {
Jan 8, 2012
Jan 8, 2012
909
rdata->glEnableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
Jun 22, 2011
Jun 22, 2011
910
} else {
Jan 8, 2012
Jan 8, 2012
911
rdata->glDisableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
Jun 22, 2011
Jun 22, 2011
912
913
914
915
916
917
918
919
920
921
922
923
}
rdata->current.tex_coords = enabled;
}
}
static int
GLES2_SetDrawingState(SDL_Renderer * renderer)
{
GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
int blendMode = renderer->blendMode;
GLuint locColor;
Jan 8, 2012
Jan 8, 2012
924
rdata->glGetError();
Jun 22, 2011
Jun 22, 2011
925
926
927
928
929
930
931
932
933
934
935
936
937
GLES2_ActivateRenderer(renderer);
GLES2_SetBlendMode(rdata, blendMode);
GLES2_SetTexCoords(rdata, SDL_FALSE);
/* Activate an appropriate shader and set the projection matrix */
if (GLES2_SelectProgram(renderer, GLES2_IMAGESOURCE_SOLID, blendMode) < 0)
return -1;
/* Select the color to draw with */
locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
Jan 8, 2012
Jan 8, 2012
938
rdata->glUniform4f(locColor,
Jun 22, 2011
Jun 22, 2011
939
940
941
942
943
944
945
946
947
948
renderer->r * inv255f,
renderer->g * inv255f,
renderer->b * inv255f,
renderer->a * inv255f);
return 0;
}
static int
GLES2_RenderDrawPoints(SDL_Renderer *renderer, const SDL_Point *points, int count)
{
Jan 8, 2012
Jan 8, 2012
949
GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
Jun 22, 2011
Jun 22, 2011
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
GLfloat *vertices;
int idx;
if (GLES2_SetDrawingState(renderer) < 0) {
return -1;
}
/* Emit the specified vertices as points */
vertices = SDL_stack_alloc(GLfloat, count * 2);
for (idx = 0; idx < count; ++idx)
{
GLfloat x = (GLfloat)points[idx].x + 0.5f;
GLfloat y = (GLfloat)points[idx].y + 0.5f;
vertices[idx * 2] = x;
vertices[(idx * 2) + 1] = y;
}
Jan 8, 2012
Jan 8, 2012
967
968
969
rdata->glGetError();
rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
rdata->glDrawArrays(GL_POINTS, 0, count);
Jun 22, 2011
Jun 22, 2011
970
SDL_stack_free(vertices);
Jan 8, 2012
Jan 8, 2012
971
if (rdata->glGetError() != GL_NO_ERROR)
Jun 22, 2011
Jun 22, 2011
972
973
974
975
976
977
978
979
980
981
{
SDL_SetError("Failed to render lines");
return -1;
}
return 0;
}
static int
GLES2_RenderDrawLines(SDL_Renderer *renderer, const SDL_Point *points, int count)
{
Jan 8, 2012
Jan 8, 2012
982
GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
Jun 22, 2011
Jun 22, 2011
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
GLfloat *vertices;
int idx;
if (GLES2_SetDrawingState(renderer) < 0) {
return -1;
}
/* Emit a line strip including the specified vertices */
vertices = SDL_stack_alloc(GLfloat, count * 2);
for (idx = 0; idx < count; ++idx)
{
GLfloat x = (GLfloat)points[idx].x + 0.5f;
GLfloat y = (GLfloat)points[idx].y + 0.5f;
vertices[idx * 2] = x;
vertices[(idx * 2) + 1] = y;
}
Jan 8, 2012
Jan 8, 2012
1000
rdata->glGetError();