Skip to content

Latest commit

 

History

History
2087 lines (1842 loc) · 71 KB

SDL_render_gles2.c

File metadata and controls

2087 lines (1842 loc) · 71 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 3, 2018
Jan 3, 2018
3
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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_internal.h"
#if SDL_VIDEO_RENDER_OGL_ES2 && !SDL_RENDER_DISABLED
Jan 31, 2018
Jan 31, 2018
25
#include "SDL_assert.h"
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
#include "SDL_hints.h"
#include "SDL_opengles2.h"
#include "../SDL_sysrender.h"
#include "../../video/SDL_blit.h"
#include "SDL_shaders_gles2.h"
/* To prevent unnecessary window recreation,
* these should match the defaults selected in SDL_GL_ResetAttributes
*/
#define RENDERER_CONTEXT_MAJOR 2
#define RENDERER_CONTEXT_MINOR 0
/* Used to re-create the window with OpenGL ES capability */
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
/*************************************************************************************************
* Context structures *
*************************************************************************************************/
typedef struct GLES2_FBOList GLES2_FBOList;
struct GLES2_FBOList
{
Uint32 w, h;
GLuint FBO;
GLES2_FBOList *next;
};
typedef struct GLES2_TextureData
{
GLenum texture;
GLenum texture_type;
GLenum pixel_format;
GLenum pixel_type;
void *pixel_data;
int pitch;
/* YUV texture support */
SDL_bool yuv;
SDL_bool nv12;
GLenum texture_v;
GLenum texture_u;
GLES2_FBOList *fbo;
} 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;
GLES2_ShaderCacheEntry *vertex_shader;
GLES2_ShaderCacheEntry *fragment_shader;
GLuint uniform_locations[16];
Sep 28, 2018
Sep 28, 2018
92
Uint32 color;
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
120
121
122
123
GLfloat projection[4][4];
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_ANGLE = 2,
GLES2_ATTRIBUTE_CENTER = 3,
} GLES2_Attribute;
typedef enum
{
GLES2_UNIFORM_PROJECTION,
GLES2_UNIFORM_TEXTURE,
GLES2_UNIFORM_COLOR,
GLES2_UNIFORM_TEXTURE_U,
GLES2_UNIFORM_TEXTURE_V
} GLES2_Uniform;
typedef enum
{
Sep 8, 2018
Sep 8, 2018
124
GLES2_IMAGESOURCE_INVALID,
125
126
127
128
129
130
131
GLES2_IMAGESOURCE_SOLID,
GLES2_IMAGESOURCE_TEXTURE_ABGR,
GLES2_IMAGESOURCE_TEXTURE_ARGB,
GLES2_IMAGESOURCE_TEXTURE_RGB,
GLES2_IMAGESOURCE_TEXTURE_BGR,
GLES2_IMAGESOURCE_TEXTURE_YUV,
GLES2_IMAGESOURCE_TEXTURE_NV12,
Dec 12, 2017
Dec 12, 2017
132
133
GLES2_IMAGESOURCE_TEXTURE_NV21,
GLES2_IMAGESOURCE_TEXTURE_EXTERNAL_OES
134
135
} GLES2_ImageSource;
Sep 28, 2018
Sep 28, 2018
136
typedef struct
Sep 8, 2018
Sep 8, 2018
137
{
Sep 28, 2018
Sep 28, 2018
138
139
140
141
142
143
144
145
146
147
148
149
150
151
SDL_Rect viewport;
SDL_Texture *texture;
SDL_BlendMode blend;
SDL_bool cliprect_enabled;
SDL_Rect cliprect;
SDL_bool texturing;
SDL_bool is_copy_ex;
Uint32 color;
Uint32 clear_color;
GLES2_ProgramCacheEntry *program;
GLfloat projection[4][4];
} GLES2_DrawStateCache;
typedef struct GLES2_RenderData
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
{
SDL_GLContext *context;
SDL_bool debug_enabled;
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
#include "SDL_gles2funcs.h"
#undef SDL_PROC
GLES2_FBOList *framebuffers;
GLuint window_framebuffer;
int shader_format_count;
GLenum *shader_formats;
GLES2_ShaderCache shader_cache;
GLES2_ProgramCache program_cache;
Uint8 clear_r, clear_g, clear_b, clear_a;
Sep 28, 2018
Sep 28, 2018
169
170
GLuint vertex_buffers[8];
GLsizeiptr vertex_buffer_size[8];
Sep 8, 2018
Sep 8, 2018
171
int current_vertex_buffer;
Sep 28, 2018
Sep 28, 2018
172
173
GLES2_DrawStateCache drawstate;
} GLES2_RenderData;
174
175
176
#define GLES2_MAX_CACHED_PROGRAMS 8
Sep 8, 2018
Sep 8, 2018
177
178
static const float inv255f = 1.0f / 255.0f;
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
SDL_FORCE_INLINE const char*
GL_TranslateError (GLenum error)
{
#define GL_ERROR_TRANSLATE(e) case e: return #e;
switch (error) {
GL_ERROR_TRANSLATE(GL_INVALID_ENUM)
GL_ERROR_TRANSLATE(GL_INVALID_VALUE)
GL_ERROR_TRANSLATE(GL_INVALID_OPERATION)
GL_ERROR_TRANSLATE(GL_OUT_OF_MEMORY)
GL_ERROR_TRANSLATE(GL_NO_ERROR)
default:
return "UNKNOWN";
}
#undef GL_ERROR_TRANSLATE
}
SDL_FORCE_INLINE void
GL_ClearErrors(SDL_Renderer *renderer)
{
Sep 28, 2018
Sep 28, 2018
199
GLES2_RenderData *data = (GLES2_RenderData *) renderer->driverdata;
200
201
202
203
204
205
206
207
208
209
210
211
if (!data->debug_enabled) {
return;
}
while (data->glGetError() != GL_NO_ERROR) {
continue;
}
}
SDL_FORCE_INLINE int
GL_CheckAllErrors (const char *prefix, SDL_Renderer *renderer, const char *file, int line, const char *function)
{
Sep 28, 2018
Sep 28, 2018
212
GLES2_RenderData *data = (GLES2_RenderData *) renderer->driverdata;
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
int ret = 0;
if (!data->debug_enabled) {
return 0;
}
/* check gl errors (can return multiple errors) */
for (;;) {
GLenum error = data->glGetError();
if (error != GL_NO_ERROR) {
if (prefix == NULL || prefix[0] == '\0') {
prefix = "generic";
}
SDL_SetError("%s: %s (%d): %s %s (0x%X)", prefix, file, line, function, GL_TranslateError(error), error);
ret = -1;
} else {
break;
}
}
return ret;
}
#if 0
#define GL_CheckError(prefix, renderer)
#else
Jan 31, 2018
Jan 31, 2018
237
#define GL_CheckError(prefix, renderer) GL_CheckAllErrors(prefix, renderer, SDL_FILE, SDL_LINE, SDL_FUNCTION)
238
239
240
241
242
243
244
#endif
/*************************************************************************************************
* Renderer state APIs *
*************************************************************************************************/
Sep 28, 2018
Sep 28, 2018
245
static int GLES2_LoadFunctions(GLES2_RenderData * data)
246
247
248
249
250
251
252
253
254
255
{
#if SDL_VIDEO_DRIVER_UIKIT
#define __SDL_NOGETPROCADDR__
#elif SDL_VIDEO_DRIVER_ANDROID
#define __SDL_NOGETPROCADDR__
#elif SDL_VIDEO_DRIVER_PANDORA
#define __SDL_NOGETPROCADDR__
#endif
#if defined __SDL_NOGETPROCADDR__
Feb 12, 2018
Feb 12, 2018
256
#define SDL_PROC(ret,func,params) data->func=func;
257
258
259
#else
#define SDL_PROC(ret,func,params) \
do { \
Feb 12, 2018
Feb 12, 2018
260
data->func = SDL_GL_GetProcAddress(#func); \
261
if ( ! data->func ) { \
Mar 26, 2017
Mar 26, 2017
262
return SDL_SetError("Couldn't load GLES2 function %s: %s", #func, SDL_GetError()); \
263
264
265
266
267
268
269
270
271
} \
} while ( 0 );
#endif /* __SDL_NOGETPROCADDR__ */
#include "SDL_gles2funcs.h"
#undef SDL_PROC
return 0;
}
Nov 14, 2016
Nov 14, 2016
272
static GLES2_FBOList *
Sep 28, 2018
Sep 28, 2018
273
GLES2_GetFBO(GLES2_RenderData *data, Uint32 w, Uint32 h)
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
{
GLES2_FBOList *result = data->framebuffers;
while ((result) && ((result->w != w) || (result->h != h)) ) {
result = result->next;
}
if (result == NULL) {
result = SDL_malloc(sizeof(GLES2_FBOList));
result->w = w;
result->h = h;
data->glGenFramebuffers(1, &result->FBO);
result->next = data->framebuffers;
data->framebuffers = result;
}
return result;
}
static int
GLES2_ActivateRenderer(SDL_Renderer * renderer)
{
Sep 28, 2018
Sep 28, 2018
293
GLES2_RenderData *data = (GLES2_RenderData *)renderer->driverdata;
Sep 28, 2018
Sep 28, 2018
295
if (SDL_GL_GetCurrentContext() != data->context) {
296
/* Null out the current program to ensure we set it again */
Sep 28, 2018
Sep 28, 2018
297
data->drawstate.program = NULL;
298
299
300
301
302
303
304
305
306
307
308
309
310
311
if (SDL_GL_MakeCurrent(renderer->window, data->context) < 0) {
return -1;
}
}
GL_ClearErrors(renderer);
return 0;
}
static void
GLES2_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
{
Sep 28, 2018
Sep 28, 2018
312
GLES2_RenderData *data = (GLES2_RenderData *)renderer->driverdata;
313
314
315
316
317
318
319
320
321
322
323
324
325
326
if (event->event == SDL_WINDOWEVENT_MINIMIZED) {
/* According to Apple documentation, we need to finish drawing NOW! */
data->glFinish();
}
}
static int
GLES2_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
{
SDL_GL_GetDrawableSize(renderer->window, w, h);
return 0;
}
Aug 14, 2017
Aug 14, 2017
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
static GLenum GetBlendFunc(SDL_BlendFactor factor)
{
switch (factor) {
case SDL_BLENDFACTOR_ZERO:
return GL_ZERO;
case SDL_BLENDFACTOR_ONE:
return GL_ONE;
case SDL_BLENDFACTOR_SRC_COLOR:
return GL_SRC_COLOR;
case SDL_BLENDFACTOR_ONE_MINUS_SRC_COLOR:
return GL_ONE_MINUS_SRC_COLOR;
case SDL_BLENDFACTOR_SRC_ALPHA:
return GL_SRC_ALPHA;
case SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA:
return GL_ONE_MINUS_SRC_ALPHA;
case SDL_BLENDFACTOR_DST_COLOR:
return GL_DST_COLOR;
case SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR:
return GL_ONE_MINUS_DST_COLOR;
case SDL_BLENDFACTOR_DST_ALPHA:
return GL_DST_ALPHA;
case SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA:
return GL_ONE_MINUS_DST_ALPHA;
default:
return GL_INVALID_ENUM;
}
}
static GLenum GetBlendEquation(SDL_BlendOperation operation)
{
switch (operation) {
case SDL_BLENDOPERATION_ADD:
return GL_FUNC_ADD;
case SDL_BLENDOPERATION_SUBTRACT:
return GL_FUNC_SUBTRACT;
case SDL_BLENDOPERATION_REV_SUBTRACT:
return GL_FUNC_REVERSE_SUBTRACT;
default:
return GL_INVALID_ENUM;
}
}
static SDL_bool
GLES2_SupportsBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
{
SDL_BlendFactor srcColorFactor = SDL_GetBlendModeSrcColorFactor(blendMode);
SDL_BlendFactor srcAlphaFactor = SDL_GetBlendModeSrcAlphaFactor(blendMode);
SDL_BlendOperation colorOperation = SDL_GetBlendModeColorOperation(blendMode);
SDL_BlendFactor dstColorFactor = SDL_GetBlendModeDstColorFactor(blendMode);
SDL_BlendFactor dstAlphaFactor = SDL_GetBlendModeDstAlphaFactor(blendMode);
SDL_BlendOperation alphaOperation = SDL_GetBlendModeAlphaOperation(blendMode);
if (GetBlendFunc(srcColorFactor) == GL_INVALID_ENUM ||
GetBlendFunc(srcAlphaFactor) == GL_INVALID_ENUM ||
GetBlendEquation(colorOperation) == GL_INVALID_ENUM ||
GetBlendFunc(dstColorFactor) == GL_INVALID_ENUM ||
GetBlendFunc(dstAlphaFactor) == GL_INVALID_ENUM ||
GetBlendEquation(alphaOperation) == GL_INVALID_ENUM) {
return SDL_FALSE;
}
return SDL_TRUE;
}
Sep 8, 2018
Sep 8, 2018
390
Sep 28, 2018
Sep 28, 2018
391
392
static void
GLES2_EvictShader(GLES2_RenderData *data, GLES2_ShaderCacheEntry *entry)
Sep 28, 2018
Sep 28, 2018
394
395
396
/* Unlink the shader from the cache */
if (entry->next) {
entry->next->prev = entry->prev;
Sep 8, 2018
Sep 8, 2018
397
}
Sep 28, 2018
Sep 28, 2018
398
399
if (entry->prev) {
entry->prev->next = entry->next;
Sep 28, 2018
Sep 28, 2018
401
402
if (data->shader_cache.head == entry) {
data->shader_cache.head = entry->next;
Sep 8, 2018
Sep 8, 2018
403
}
Sep 28, 2018
Sep 28, 2018
404
--data->shader_cache.count;
Sep 8, 2018
Sep 8, 2018
405
Sep 28, 2018
Sep 28, 2018
406
407
408
409
/* Deallocate the shader */
data->glDeleteShader(entry->id);
SDL_free(entry);
}
Sep 8, 2018
Sep 8, 2018
410
Sep 28, 2018
Sep 28, 2018
411
412
413
414
415
416
417
static GLES2_ProgramCacheEntry *
GLES2_CacheProgram(GLES2_RenderData *data, GLES2_ShaderCacheEntry *vertex,
GLES2_ShaderCacheEntry *fragment)
{
GLES2_ProgramCacheEntry *entry;
GLES2_ShaderCacheEntry *shaderEntry;
GLint linkSuccessful;
Sep 8, 2018
Sep 8, 2018
418
Sep 28, 2018
Sep 28, 2018
419
420
421
422
423
424
425
426
427
428
429
430
/* Check if we've already cached this program */
entry = data->program_cache.head;
while (entry) {
if (entry->vertex_shader == vertex && entry->fragment_shader == fragment) {
break;
}
entry = entry->next;
}
if (entry) {
if (data->program_cache.head != entry) {
if (entry->next) {
entry->next->prev = entry->prev;
Sep 8, 2018
Sep 8, 2018
431
}
Sep 28, 2018
Sep 28, 2018
432
433
if (entry->prev) {
entry->prev->next = entry->next;
Sep 8, 2018
Sep 8, 2018
434
}
Sep 28, 2018
Sep 28, 2018
435
436
437
438
entry->prev = NULL;
entry->next = data->program_cache.head;
data->program_cache.head->prev = entry;
data->program_cache.head = entry;
Sep 8, 2018
Sep 8, 2018
439
}
Sep 28, 2018
Sep 28, 2018
440
441
442
443
444
445
446
447
448
449
450
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;
Sep 9, 2018
Sep 9, 2018
451
Sep 28, 2018
Sep 28, 2018
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/* Create the program and link it */
entry->id = data->glCreateProgram();
data->glAttachShader(entry->id, vertex->id);
data->glAttachShader(entry->id, fragment->id);
data->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_POSITION, "a_position");
data->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_TEXCOORD, "a_texCoord");
data->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_ANGLE, "a_angle");
data->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_CENTER, "a_center");
data->glLinkProgram(entry->id);
data->glGetProgramiv(entry->id, GL_LINK_STATUS, &linkSuccessful);
if (!linkSuccessful) {
data->glDeleteProgram(entry->id);
SDL_free(entry);
SDL_SetError("Failed to link shader program");
return NULL;
Sep 8, 2018
Sep 8, 2018
468
Sep 28, 2018
Sep 28, 2018
469
470
471
472
473
474
475
476
477
478
479
/* Predetermine locations of uniform variables */
entry->uniform_locations[GLES2_UNIFORM_PROJECTION] =
data->glGetUniformLocation(entry->id, "u_projection");
entry->uniform_locations[GLES2_UNIFORM_TEXTURE_V] =
data->glGetUniformLocation(entry->id, "u_texture_v");
entry->uniform_locations[GLES2_UNIFORM_TEXTURE_U] =
data->glGetUniformLocation(entry->id, "u_texture_u");
entry->uniform_locations[GLES2_UNIFORM_TEXTURE] =
data->glGetUniformLocation(entry->id, "u_texture");
entry->uniform_locations[GLES2_UNIFORM_COLOR] =
data->glGetUniformLocation(entry->id, "u_color");
Sep 8, 2018
Sep 8, 2018
480
Sep 28, 2018
Sep 28, 2018
481
entry->color = 0;
Sep 28, 2018
Sep 28, 2018
483
484
485
data->glUseProgram(entry->id);
if (entry->uniform_locations[GLES2_UNIFORM_TEXTURE_V] != -1) {
data->glUniform1i(entry->uniform_locations[GLES2_UNIFORM_TEXTURE_V], 2); /* always texture unit 2. */
Sep 28, 2018
Sep 28, 2018
487
488
489
490
491
492
493
494
495
496
497
if (entry->uniform_locations[GLES2_UNIFORM_TEXTURE_U] != -1) {
data->glUniform1i(entry->uniform_locations[GLES2_UNIFORM_TEXTURE_U], 1); /* always texture unit 1. */
}
if (entry->uniform_locations[GLES2_UNIFORM_TEXTURE] != -1) {
data->glUniform1i(entry->uniform_locations[GLES2_UNIFORM_TEXTURE], 0); /* always texture unit 0. */
}
if (entry->uniform_locations[GLES2_UNIFORM_PROJECTION] != -1) {
data->glUniformMatrix4fv(entry->uniform_locations[GLES2_UNIFORM_PROJECTION], 1, GL_FALSE, (GLfloat *)entry->projection);
}
if (entry->uniform_locations[GLES2_UNIFORM_COLOR] != -1) {
data->glUniform4f(entry->uniform_locations[GLES2_UNIFORM_COLOR], 0.0f, 0.0f, 0.0f, 0.0f);
Sep 9, 2018
Sep 9, 2018
498
}
Sep 28, 2018
Sep 28, 2018
500
501
502
503
504
505
/* Cache the linked program */
if (data->program_cache.head) {
entry->next = data->program_cache.head;
data->program_cache.head->prev = entry;
} else {
data->program_cache.tail = entry;
Sep 9, 2018
Sep 9, 2018
506
}
Sep 28, 2018
Sep 28, 2018
507
508
data->program_cache.head = entry;
++data->program_cache.count;
Sep 28, 2018
Sep 28, 2018
510
511
512
/* Increment the refcount of the shaders we're using */
++vertex->references;
++fragment->references;
Sep 28, 2018
Sep 28, 2018
514
515
516
517
518
/* Evict the last entry from the cache if we exceed the limit */
if (data->program_cache.count > GLES2_MAX_CACHED_PROGRAMS) {
shaderEntry = data->program_cache.tail->vertex_shader;
if (--shaderEntry->references <= 0) {
GLES2_EvictShader(data, shaderEntry);
Sep 9, 2018
Sep 9, 2018
519
}
Sep 28, 2018
Sep 28, 2018
520
521
522
shaderEntry = data->program_cache.tail->fragment_shader;
if (--shaderEntry->references <= 0) {
GLES2_EvictShader(data, shaderEntry);
Sep 9, 2018
Sep 9, 2018
523
}
Sep 28, 2018
Sep 28, 2018
524
525
526
527
528
529
530
531
data->glDeleteProgram(data->program_cache.tail->id);
data->program_cache.tail = data->program_cache.tail->prev;
SDL_free(data->program_cache.tail->next);
data->program_cache.tail->next = NULL;
--data->program_cache.count;
}
return entry;
}
Sep 9, 2018
Sep 9, 2018
532
Sep 28, 2018
Sep 28, 2018
533
534
535
536
537
538
539
540
static GLES2_ShaderCacheEntry *
GLES2_CacheShader(GLES2_RenderData *data, GLES2_ShaderType type)
{
const GLES2_Shader *shader;
const GLES2_ShaderInstance *instance = NULL;
GLES2_ShaderCacheEntry *entry = NULL;
GLint compileSuccessful = GL_FALSE;
int i, j;
Sep 9, 2018
Sep 9, 2018
541
Sep 28, 2018
Sep 28, 2018
542
543
544
545
546
547
548
549
550
551
552
553
/* Find the corresponding shader */
shader = GLES2_GetShader(type);
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 < data->shader_format_count && !instance; ++j) {
if (!shader->instances[i]) {
continue;
Sep 28, 2018
Sep 28, 2018
555
556
if (shader->instances[i]->format != data->shader_formats[j]) {
continue;
Sep 28, 2018
Sep 28, 2018
558
instance = shader->instances[i];
Sep 28, 2018
Sep 28, 2018
560
561
562
563
564
}
if (!instance) {
SDL_SetError("The specified shader cannot be loaded on the current platform");
return NULL;
}
Sep 8, 2018
Sep 8, 2018
565
Sep 28, 2018
Sep 28, 2018
566
567
568
569
570
/* Check if we've already cached this shader */
entry = data->shader_cache.head;
while (entry) {
if (entry->instance == instance) {
break;
Sep 28, 2018
Sep 28, 2018
572
573
574
575
576
entry = entry->next;
}
if (entry) {
return entry;
}
Sep 8, 2018
Sep 8, 2018
577
Sep 28, 2018
Sep 28, 2018
578
579
580
581
582
/* Create a shader cache entry */
entry = (GLES2_ShaderCacheEntry *)SDL_calloc(1, sizeof(GLES2_ShaderCacheEntry));
if (!entry) {
SDL_OutOfMemory();
return NULL;
Sep 28, 2018
Sep 28, 2018
584
585
entry->type = type;
entry->instance = instance;
Sep 28, 2018
Sep 28, 2018
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/* Compile or load the selected shader instance */
entry->id = data->glCreateShader(instance->type);
if (instance->format == (GLenum)-1) {
data->glShaderSource(entry->id, 1, (const char **)(char *)&instance->data, NULL);
data->glCompileShader(entry->id);
data->glGetShaderiv(entry->id, GL_COMPILE_STATUS, &compileSuccessful);
} else {
data->glShaderBinary(1, &entry->id, instance->format, instance->data, instance->length);
compileSuccessful = GL_TRUE;
}
if (!compileSuccessful) {
char *info = NULL;
int length = 0;
data->glGetShaderiv(entry->id, GL_INFO_LOG_LENGTH, &length);
if (length > 0) {
info = SDL_stack_alloc(char, length);
if (info) {
data->glGetShaderInfoLog(entry->id, length, &length, info);
}
}
if (info) {
SDL_SetError("Failed to load the shader: %s", info);
SDL_stack_free(info);
} else {
SDL_SetError("Failed to load the shader");
}
data->glDeleteShader(entry->id);
SDL_free(entry);
return NULL;
}
Sep 28, 2018
Sep 28, 2018
619
620
621
622
623
624
625
626
627
/* Link the shader entry in at the front of the cache */
if (data->shader_cache.head) {
entry->next = data->shader_cache.head;
data->shader_cache.head->prev = entry;
}
data->shader_cache.head = entry;
++data->shader_cache.count;
return entry;
}
Sep 28, 2018
Sep 28, 2018
630
GLES2_SelectProgram(GLES2_RenderData *data, GLES2_ImageSource source, int w, int h)
Sep 28, 2018
Sep 28, 2018
632
633
634
635
GLES2_ShaderCacheEntry *vertex = NULL;
GLES2_ShaderCacheEntry *fragment = NULL;
GLES2_ShaderType vtype, ftype;
GLES2_ProgramCacheEntry *program;
Sep 28, 2018
Sep 28, 2018
637
638
639
640
641
/* 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;
Sep 28, 2018
Sep 28, 2018
643
644
case GLES2_IMAGESOURCE_TEXTURE_ABGR:
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_ABGR_SRC;
Sep 28, 2018
Sep 28, 2018
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
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;
break;
case GLES2_IMAGESOURCE_TEXTURE_YUV:
switch (SDL_GetYUVConversionModeForResolution(w, h)) {
case SDL_YUV_CONVERSION_JPEG:
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_YUV_JPEG_SRC;
break;
case SDL_YUV_CONVERSION_BT601:
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_YUV_BT601_SRC;
break;
case SDL_YUV_CONVERSION_BT709:
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_YUV_BT709_SRC;
break;
default:
SDL_SetError("Unsupported YUV conversion mode: %d\n", SDL_GetYUVConversionModeForResolution(w, h));
goto fault;
}
break;
case GLES2_IMAGESOURCE_TEXTURE_NV12:
switch (SDL_GetYUVConversionModeForResolution(w, h)) {
case SDL_YUV_CONVERSION_JPEG:
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_NV12_JPEG_SRC;
break;
case SDL_YUV_CONVERSION_BT601:
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_NV12_BT601_SRC;
break;
case SDL_YUV_CONVERSION_BT709:
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_NV12_BT709_SRC;
break;
default:
SDL_SetError("Unsupported YUV conversion mode: %d\n", SDL_GetYUVConversionModeForResolution(w, h));
goto fault;
}
break;
case GLES2_IMAGESOURCE_TEXTURE_NV21:
switch (SDL_GetYUVConversionModeForResolution(w, h)) {
case SDL_YUV_CONVERSION_JPEG:
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_NV21_JPEG_SRC;
break;
case SDL_YUV_CONVERSION_BT601:
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_NV21_BT601_SRC;
break;
case SDL_YUV_CONVERSION_BT709:
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_NV21_BT709_SRC;
break;
default:
SDL_SetError("Unsupported YUV conversion mode: %d\n", SDL_GetYUVConversionModeForResolution(w, h));
goto fault;
}
break;
case GLES2_IMAGESOURCE_TEXTURE_EXTERNAL_OES:
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_EXTERNAL_OES_SRC;
Dec 12, 2017
Dec 12, 2017
705
break;
Sep 28, 2018
Sep 28, 2018
707
goto fault;
Sep 28, 2018
Sep 28, 2018
710
711
712
713
714
715
716
717
/* Load the requested shaders */
vertex = GLES2_CacheShader(data, vtype);
if (!vertex) {
goto fault;
}
fragment = GLES2_CacheShader(data, ftype);
if (!fragment) {
goto fault;
Dec 12, 2017
Dec 12, 2017
718
719
}
Sep 28, 2018
Sep 28, 2018
720
721
722
723
724
/* Check if we need to change programs at all */
if (data->drawstate.program &&
data->drawstate.program->vertex_shader == vertex &&
data->drawstate.program->fragment_shader == fragment) {
return 0;
Sep 28, 2018
Sep 28, 2018
727
728
729
730
/* Generate a matching program */
program = GLES2_CacheProgram(data, vertex, fragment);
if (!program) {
goto fault;
Sep 28, 2018
Sep 28, 2018
733
734
/* Select that program in OpenGL */
data->glUseProgram(program->id);
Sep 28, 2018
Sep 28, 2018
736
737
/* Set the current program */
data->drawstate.program = program;
Sep 28, 2018
Sep 28, 2018
739
740
741
742
743
/* Clean up and return */
return 0;
fault:
if (vertex && vertex->references <= 0) {
GLES2_EvictShader(data, vertex);
Sep 28, 2018
Sep 28, 2018
745
746
if (fragment && fragment->references <= 0) {
GLES2_EvictShader(data, fragment);
Sep 28, 2018
Sep 28, 2018
748
749
750
data->drawstate.program = NULL;
return -1;
}
Sep 28, 2018
Sep 28, 2018
752
753
754
755
756
757
758
759
760
761
762
763
764
static int
GLES2_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
{
return 0; /* nothing to do in this backend. */
}
static int
GLES2_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
{
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, count * 2 * sizeof (GLfloat), 0, &cmd->data.draw.first);
size_t i;
if (!verts) {
765
766
767
return -1;
}
Sep 28, 2018
Sep 28, 2018
768
769
770
771
cmd->data.draw.count = count;
for (i = 0; i < count; i++) {
*(verts++) = 0.5f + points[i].x;
*(verts++) = 0.5f + points[i].y;
Sep 28, 2018
Sep 28, 2018
774
return 0;
Sep 28, 2018
Sep 28, 2018
778
GLES2_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects, int count)
Sep 28, 2018
Sep 28, 2018
780
781
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, count * 8 * sizeof (GLfloat), 0, &cmd->data.draw.first);
size_t i;
Sep 28, 2018
Sep 28, 2018
783
784
if (!verts) {
return -1;
Jun 24, 2018
Jun 24, 2018
785
786
}
Sep 28, 2018
Sep 28, 2018
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
cmd->data.draw.count = count;
for (i = 0; i < count; i++) {
const SDL_FRect *rect = &rects[i];
const GLfloat minx = rect->x;
const GLfloat maxx = rect->x + rect->w;
const GLfloat miny = rect->y;
const GLfloat maxy = rect->y + rect->h;
*(verts++) = minx;
*(verts++) = miny;
*(verts++) = maxx;
*(verts++) = miny;
*(verts++) = minx;
*(verts++) = maxy;
*(verts++) = maxx;
*(verts++) = maxy;
Sep 28, 2018
Sep 28, 2018
805
806
807
808
809
810
811
812
813
814
815
816
817
return 0;
}
static int
GLES2_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_FRect * dstrect)
{
GLfloat minx, miny, maxx, maxy;
GLfloat minu, maxu, minv, maxv;
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, 16 * sizeof (GLfloat), 0, &cmd->data.draw.first);
if (!verts) {
return -1;
Sep 28, 2018
Sep 28, 2018
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
cmd->data.draw.count = 1;
minx = dstrect->x;
miny = dstrect->y;
maxx = dstrect->x + dstrect->w;
maxy = dstrect->y + dstrect->h;
minu = (GLfloat) srcrect->x / texture->w;
maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
minv = (GLfloat) srcrect->y / texture->h;
maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
*(verts++) = minx;
*(verts++) = miny;
*(verts++) = maxx;
*(verts++) = miny;
*(verts++) = minx;
*(verts++) = maxy;
*(verts++) = maxx;
*(verts++) = maxy;
*(verts++) = minu;
*(verts++) = minv;
*(verts++) = maxu;
*(verts++) = minv;
*(verts++) = minu;
*(verts++) = maxv;
*(verts++) = maxu;
*(verts++) = maxv;
850
851
852
853
return 0;
}
static int
Sep 28, 2018
Sep 28, 2018
854
855
856
GLES2_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
const SDL_Rect * srcquad, const SDL_FRect * dstrect,
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
Sep 28, 2018
Sep 28, 2018
858
859
860
861
862
863
864
865
866
867
868
869
870
/* render expects cos value - 1 (see GLES2_VertexSrc_Default_) */
const float radian_angle = (float)(M_PI * (360.0 - angle) / 180.0);
const GLfloat s = (GLfloat) SDL_sin(radian_angle);
const GLfloat c = (GLfloat) SDL_cos(radian_angle) - 1.0f;
const GLfloat centerx = center->x + dstrect->x;
const GLfloat centery = center->y + dstrect->y;
GLfloat minx, miny, maxx, maxy;
GLfloat minu, maxu, minv, maxv;
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, 32 * sizeof (GLfloat), 0, &cmd->data.draw.first);
if (!verts) {
return -1;
}
Sep 28, 2018
Sep 28, 2018
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
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
if (flip & SDL_FLIP_HORIZONTAL) {
minx = dstrect->x + dstrect->w;
maxx = dstrect->x;
}
else {
minx = dstrect->x;
maxx = dstrect->x + dstrect->w;
}
if (flip & SDL_FLIP_VERTICAL) {
miny = dstrect->y + dstrect->h;
maxy = dstrect->y;
}
else {
miny = dstrect->y;
maxy = dstrect->y + dstrect->h;
}
minu = (GLfloat) (srcquad->x / texture->w);
maxu = (GLfloat) ((srcquad->x + srcquad->w) / texture->w);
minv = (GLfloat) (srcquad->y / texture->h);
maxv = (GLfloat) ((srcquad->y + srcquad->h) / texture->h);
cmd->data.draw.count = 1;
*(verts++) = minx;
*(verts++) = miny;
*(verts++) = maxx;
*(verts++) = miny;
*(verts++) = minx;
*(verts++) = maxy;
*(verts++) = maxx;
*(verts++) = maxy;
*(verts++) = minu;
*(verts++) = minv;
*(verts++) = maxu;
*(verts++) = minv;
*(verts++) = minu;
*(verts++) = maxv;
*(verts++) = maxu;
*(verts++) = maxv;
*(verts++) = s;
*(verts++) = c;
*(verts++) = s;
*(verts++) = c;
*(verts++) = s;
*(verts++) = c;
*(verts++) = s;
*(verts++) = c;
*(verts++) = centerx;
*(verts++) = centery;
*(verts++) = centerx;
*(verts++) = centery;
*(verts++) = centerx;
*(verts++) = centery;
*(verts++) = centerx;
*(verts++) = centery;
return 0;
}
Sep 28, 2018
Sep 28, 2018
936
937
938
939
940
941
942
static int
SetDrawState(GLES2_RenderData *data, const SDL_RenderCommand *cmd, const GLES2_ImageSource imgsrc)
{
const SDL_bool was_copy_ex = data->drawstate.is_copy_ex;
const SDL_bool is_copy_ex = (cmd->command == SDL_RENDERCMD_COPY_EX);
SDL_Texture *texture = cmd->data.draw.texture;
GLES2_ProgramCacheEntry *program;
Sep 8, 2018
Sep 8, 2018
943
Sep 28, 2018
Sep 28, 2018
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
SDL_assert((texture != NULL) == (imgsrc != GLES2_IMAGESOURCE_SOLID));
if (texture != data->drawstate.texture) {
if ((texture != NULL) != data->drawstate.texturing) {
if (texture == NULL) {
data->glDisableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_TEXCOORD);
data->drawstate.texturing = SDL_FALSE;
} else {
data->glEnableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_TEXCOORD);
data->glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) cmd->data.draw.first + (sizeof (GLfloat) * 8));
data->drawstate.texturing = SDL_TRUE;
}
}
if (texture) {
GLES2_TextureData *tdata = (GLES2_TextureData *) texture->driverdata;
if (tdata->yuv) {
data->glActiveTexture(GL_TEXTURE2);
data->glBindTexture(tdata->texture_type, tdata->texture_v);
data->glActiveTexture(GL_TEXTURE1);
data->glBindTexture(tdata->texture_type, tdata->texture_u);
data->glActiveTexture(GL_TEXTURE0);
} else if (tdata->nv12) {
data->glActiveTexture(GL_TEXTURE1);
data->glBindTexture(tdata->texture_type, tdata->texture_u);
data->glActiveTexture(GL_TEXTURE0);
}
data->glBindTexture(tdata->texture_type, tdata->texture);
}
data->drawstate.texture = texture;
Sep 28, 2018
Sep 28, 2018
980
981
982
if (GLES2_SelectProgram(data, imgsrc, texture ? texture->w : 0, texture ? texture->h : 0) < 0) {
return -1;
}
Sep 28, 2018
Sep 28, 2018
984
985
986
987
988
989
program = data->drawstate.program;
if (program->uniform_locations[GLES2_UNIFORM_PROJECTION] != -1) {
if (SDL_memcmp(program->projection, data->drawstate.projection, sizeof (data->drawstate.projection)) != 0) {
data->glUniformMatrix4fv(program->uniform_locations[GLES2_UNIFORM_PROJECTION], 1, GL_FALSE, (GLfloat *)data->drawstate.projection);
SDL_memcpy(program->projection, data->drawstate.projection, sizeof (data->drawstate.projection));
Sep 28, 2018
Sep 28, 2018
991
}
Oct 6, 2017
Oct 6, 2017
992
Sep 28, 2018
Sep 28, 2018
993
994
995
996
997
998
999
1000
if (program->uniform_locations[GLES2_UNIFORM_COLOR] != -1) {
if (data->drawstate.color != program->color) {
const Uint8 r = (data->drawstate.color >> 16) & 0xFF;
const Uint8 g = (data->drawstate.color >> 8) & 0xFF;
const Uint8 b = (data->drawstate.color >> 0) & 0xFF;
const Uint8 a = (data->drawstate.color >> 24) & 0xFF;
data->glUniform4f(program->uniform_locations[GLES2_UNIFORM_COLOR], r * inv255f, g * inv255f, b * inv255f, a * inv255f);
program->color = data->drawstate.color;