Skip to content

Latest commit

 

History

History
1257 lines (1078 loc) · 39.7 KB

SDL_render_gles.c

File metadata and controls

1257 lines (1078 loc) · 39.7 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 5, 2019
Jan 5, 2019
3
Copyright (C) 1997-2019 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
25
26
27
28
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_ES && !SDL_RENDER_DISABLED
#include "SDL_hints.h"
#include "SDL_opengles.h"
#include "../SDL_sysrender.h"
Jan 12, 2019
Jan 12, 2019
29
30
/* To prevent unnecessary window recreation,
* these should match the defaults selected in SDL_GL_ResetAttributes
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
*/
#define RENDERER_CONTEXT_MAJOR 1
#define RENDERER_CONTEXT_MINOR 1
#if defined(SDL_VIDEO_DRIVER_PANDORA)
/* Empty function stub to get OpenGL ES 1.x support without */
/* OpenGL ES extension GL_OES_draw_texture supported */
GL_API void GL_APIENTRY
glDrawTexiOES(GLint x, GLint y, GLint z, GLint width, GLint height)
{
return;
}
#endif /* SDL_VIDEO_DRIVER_PANDORA */
/* OpenGL ES 1.1 renderer implementation, based on the OpenGL renderer */
/* Used to re-create the window with OpenGL ES capability */
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
static const float inv255f = 1.0f / 255.0f;
typedef struct GLES_FBOList GLES_FBOList;
struct GLES_FBOList
{
Uint32 w, h;
GLuint FBO;
GLES_FBOList *next;
};
Sep 26, 2018
Sep 26, 2018
64
65
66
typedef struct
{
SDL_Rect viewport;
Oct 2, 2018
Oct 2, 2018
67
SDL_bool viewport_dirty;
Sep 26, 2018
Sep 26, 2018
68
SDL_Texture *texture;
Oct 2, 2018
Oct 2, 2018
69
70
71
SDL_Texture *target;
int drawablew;
int drawableh;
Sep 26, 2018
Sep 26, 2018
72
SDL_BlendMode blend;
Oct 2, 2018
Oct 2, 2018
73
SDL_bool cliprect_enabled_dirty;
Sep 26, 2018
Sep 26, 2018
74
SDL_bool cliprect_enabled;
Oct 2, 2018
Oct 2, 2018
75
SDL_bool cliprect_dirty;
Sep 26, 2018
Sep 26, 2018
76
77
78
79
80
81
SDL_Rect cliprect;
SDL_bool texturing;
Uint32 color;
Uint32 clear_color;
} GLES_DrawStateCache;
82
83
84
85
86
87
88
89
90
91
92
93
94
95
typedef struct
{
SDL_GLContext context;
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
#define SDL_PROC_OES SDL_PROC
#include "SDL_glesfuncs.h"
#undef SDL_PROC
#undef SDL_PROC_OES
SDL_bool GL_OES_framebuffer_object_supported;
GLES_FBOList *framebuffers;
GLuint window_framebuffer;
SDL_bool GL_OES_blend_func_separate_supported;
Aug 14, 2017
Aug 14, 2017
96
97
SDL_bool GL_OES_blend_equation_separate_supported;
SDL_bool GL_OES_blend_subtract_supported;
Sep 26, 2018
Sep 26, 2018
98
99
GLES_DrawStateCache drawstate;
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
160
161
162
163
164
} GLES_RenderData;
typedef struct
{
GLuint texture;
GLenum type;
GLfloat texw;
GLfloat texh;
GLenum format;
GLenum formattype;
void *pixels;
int pitch;
GLES_FBOList *fbo;
} GLES_TextureData;
static int
GLES_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;
default:
error = "UNKNOWN";
break;
}
return SDL_SetError("%s: %s", prefix, error);
}
static int GLES_LoadFunctions(GLES_RenderData * data)
{
#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
#ifdef __SDL_NOGETPROCADDR__
#define SDL_PROC(ret,func,params) data->func=func;
#define SDL_PROC_OES(ret,func,params) data->func=func;
#else
#define SDL_PROC(ret,func,params) \
do { \
Feb 12, 2018
Feb 12, 2018
165
data->func = SDL_GL_GetProcAddress(#func); \
166
if ( ! data->func ) { \
Mar 26, 2017
Mar 26, 2017
167
return SDL_SetError("Couldn't load GLES function %s: %s", #func, SDL_GetError()); \
168
169
170
171
} \
} while ( 0 );
#define SDL_PROC_OES(ret,func,params) \
do { \
Feb 12, 2018
Feb 12, 2018
172
data->func = SDL_GL_GetProcAddress(#func); \
Jan 12, 2019
Jan 12, 2019
173
} while ( 0 );
174
175
176
177
178
179
180
181
#endif /* __SDL_NOGETPROCADDR__ */
#include "SDL_glesfuncs.h"
#undef SDL_PROC
#undef SDL_PROC_OES
return 0;
}
Nov 14, 2016
Nov 14, 2016
182
static GLES_FBOList *
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
GLES_GetFBO(GLES_RenderData *data, Uint32 w, Uint32 h)
{
GLES_FBOList *result = data->framebuffers;
while ((result) && ((result->w != w) || (result->h != h)) ) {
result = result->next;
}
if (result == NULL) {
result = SDL_malloc(sizeof(GLES_FBOList));
result->w = w;
result->h = h;
data->glGenFramebuffersOES(1, &result->FBO);
result->next = data->framebuffers;
data->framebuffers = result;
}
return result;
}
static int
GLES_ActivateRenderer(SDL_Renderer * renderer)
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
Sep 25, 2018
Sep 25, 2018
206
if (SDL_GL_GetCurrentContext() != data->context) {
207
208
209
if (SDL_GL_MakeCurrent(renderer->window, data->context) < 0) {
return -1;
}
Aug 14, 2017
Aug 14, 2017
210
}
Sep 25, 2018
Sep 25, 2018
212
return 0;
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
}
static void
GLES_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
if (event->event == SDL_WINDOWEVENT_MINIMIZED) {
/* According to Apple documentation, we need to finish drawing NOW! */
data->glFinish();
}
}
static int
GLES_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
{
SDL_GL_GetDrawableSize(renderer->window, w, h);
return 0;
}
Aug 14, 2017
Aug 14, 2017
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
299
300
301
302
303
304
305
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_OES;
case SDL_BLENDOPERATION_SUBTRACT:
return GL_FUNC_SUBTRACT_OES;
case SDL_BLENDOPERATION_REV_SUBTRACT:
return GL_FUNC_REVERSE_SUBTRACT_OES;
default:
return GL_INVALID_ENUM;
}
}
static SDL_bool
GLES_SupportsBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
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;
}
if ((srcColorFactor != srcAlphaFactor || dstColorFactor != dstAlphaFactor) && !data->GL_OES_blend_func_separate_supported) {
return SDL_FALSE;
}
if (colorOperation != alphaOperation && !data->GL_OES_blend_equation_separate_supported) {
return SDL_FALSE;
}
if (colorOperation != SDL_BLENDOPERATION_ADD && !data->GL_OES_blend_subtract_supported) {
return SDL_FALSE;
}
return SDL_TRUE;
}
306
307
308
309
310
311
312
313
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
349
350
351
352
353
static SDL_INLINE int
power_of_2(int input)
{
int value = 1;
while (value < input) {
value <<= 1;
}
return value;
}
static int
GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
GLES_TextureData *data;
GLint internalFormat;
GLenum format, type;
int texture_w, texture_h;
GLenum scaleMode;
GLenum result;
GLES_ActivateRenderer(renderer);
switch (texture->format) {
case SDL_PIXELFORMAT_ABGR8888:
internalFormat = GL_RGBA;
format = GL_RGBA;
type = GL_UNSIGNED_BYTE;
break;
default:
return SDL_SetError("Texture format not supported");
}
data = (GLES_TextureData *) SDL_calloc(1, sizeof(*data));
if (!data) {
return SDL_OutOfMemory();
}
if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
data->pixels = SDL_calloc(1, texture->h * data->pitch);
if (!data->pixels) {
SDL_free(data);
return SDL_OutOfMemory();
}
}
Jan 12, 2019
Jan 12, 2019
354
355
356
357
358
359
360
361
362
363
if (texture->access == SDL_TEXTUREACCESS_TARGET) {
if (!renderdata->GL_OES_framebuffer_object_supported) {
SDL_free(data);
return SDL_SetError("GL_OES_framebuffer_object not supported");
}
data->fbo = GLES_GetFBO(renderer->driverdata, texture->w, texture->h);
} else {
data->fbo = NULL;
}
Jan 12, 2019
Jan 12, 2019
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
renderdata->glGetError();
renderdata->glEnable(GL_TEXTURE_2D);
renderdata->glGenTextures(1, &data->texture);
result = renderdata->glGetError();
if (result != GL_NO_ERROR) {
SDL_free(data);
return GLES_SetError("glGenTextures()", result);
}
data->type = GL_TEXTURE_2D;
/* no NPOV textures allowed in OpenGL ES (yet) */
texture_w = power_of_2(texture->w);
texture_h = power_of_2(texture->h);
data->texw = (GLfloat) texture->w / texture_w;
data->texh = (GLfloat) texture->h / texture_h;
data->format = format;
data->formattype = type;
May 8, 2018
May 8, 2018
384
scaleMode = (texture->scaleMode == SDL_ScaleModeNearest) ? GL_NEAREST : GL_LINEAR;
385
386
387
388
389
390
391
392
393
renderdata->glBindTexture(data->type, data->texture);
renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER, scaleMode);
renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER, scaleMode);
renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
texture_h, 0, format, type, NULL);
renderdata->glDisable(GL_TEXTURE_2D);
Feb 5, 2019
Feb 5, 2019
394
395
renderdata->drawstate.texture = texture;
renderdata->drawstate.texturing = SDL_FALSE;
396
397
398
399
400
401
result = renderdata->glGetError();
if (result != GL_NO_ERROR) {
SDL_free(data);
return GLES_SetError("glTexImage2D()", result);
}
Jan 12, 2019
Jan 12, 2019
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
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
455
texture->driverdata = data;
return 0;
}
static int
GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
{
GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
Uint8 *blob = NULL;
Uint8 *src;
int srcPitch;
int y;
GLES_ActivateRenderer(renderer);
/* 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) {
return SDL_OutOfMemory();
}
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 */
renderdata->glGetError();
renderdata->glEnable(data->type);
renderdata->glBindTexture(data->type, data->texture);
renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
renderdata->glTexSubImage2D(data->type,
0,
rect->x,
rect->y,
rect->w,
rect->h,
data->format,
data->formattype,
src);
Oct 1, 2016
Oct 1, 2016
456
renderdata->glDisable(data->type);
457
458
SDL_free(blob);
Feb 5, 2019
Feb 5, 2019
459
460
461
renderdata->drawstate.texture = texture;
renderdata->drawstate.texturing = SDL_FALSE;
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
if (renderdata->glGetError() != GL_NO_ERROR) {
return SDL_SetError("Failed to update texture");
}
return 0;
}
static int
GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch)
{
GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
*pixels =
(void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format));
*pitch = data->pitch;
return 0;
}
static void
GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
GLES_TextureData *data = (GLES_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;
GLES_UpdateTexture(renderer, texture, &rect, data->pixels, data->pitch);
}
static int
GLES_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
GLES_TextureData *texturedata = NULL;
GLenum status;
if (!data->GL_OES_framebuffer_object_supported) {
return SDL_SetError("Can't enable render target support in this renderer");
}
if (texture == NULL) {
data->glBindFramebufferOES(GL_FRAMEBUFFER_OES, data->window_framebuffer);
return 0;
}
texturedata = (GLES_TextureData *) texture->driverdata;
data->glBindFramebufferOES(GL_FRAMEBUFFER_OES, texturedata->fbo->FBO);
/* TODO: check if texture pixel format allows this operation */
data->glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, texturedata->type, texturedata->texture, 0);
/* Check FBO status */
status = data->glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
if (status != GL_FRAMEBUFFER_COMPLETE_OES) {
return SDL_SetError("glFramebufferTexture2DOES() failed");
}
return 0;
}
static int
Sep 25, 2018
Sep 25, 2018
525
GLES_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
Sep 25, 2018
Sep 25, 2018
527
return 0; /* nothing to do in this backend. */
Sep 25, 2018
Sep 25, 2018
531
GLES_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
Sep 25, 2018
Sep 25, 2018
533
534
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, count * 2 * sizeof (GLfloat), 0, &cmd->data.draw.first);
size_t i;
Sep 25, 2018
Sep 25, 2018
536
537
if (!verts) {
return -1;
Oct 1, 2016
Oct 1, 2016
538
}
Sep 25, 2018
Sep 25, 2018
540
541
542
543
cmd->data.draw.count = count;
for (i = 0; i < count; i++) {
*(verts++) = 0.5f + points[i].x;
*(verts++) = 0.5f + points[i].y;
Oct 1, 2016
Oct 1, 2016
544
545
}
546
547
548
549
return 0;
}
static int
Sep 25, 2018
Sep 25, 2018
550
GLES_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects, int count)
Sep 25, 2018
Sep 25, 2018
552
553
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, count * 8 * sizeof (GLfloat), 0, &cmd->data.draw.first);
size_t i;
Sep 25, 2018
Sep 25, 2018
555
556
if (!verts) {
return -1;
Sep 25, 2018
Sep 25, 2018
559
cmd->data.draw.count = count;
Sep 25, 2018
Sep 25, 2018
561
for (i = 0; i < count; i++) {
562
const SDL_FRect *rect = &rects[i];
Sep 25, 2018
Sep 25, 2018
563
564
565
566
567
568
569
570
571
572
573
574
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;
575
576
577
578
579
580
}
return 0;
}
static int
Sep 25, 2018
Sep 25, 2018
581
582
GLES_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_FRect * dstrect)
Sep 25, 2018
Sep 25, 2018
584
GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
585
586
GLfloat minx, miny, maxx, maxy;
GLfloat minu, maxu, minv, maxv;
Sep 25, 2018
Sep 25, 2018
587
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, 16 * sizeof (GLfloat), 0, &cmd->data.draw.first);
Sep 25, 2018
Sep 25, 2018
589
590
if (!verts) {
return -1;
Sep 25, 2018
Sep 25, 2018
593
cmd->data.draw.count = 1;
Oct 1, 2016
Oct 1, 2016
595
596
597
598
599
600
601
602
603
604
605
606
607
608
minx = dstrect->x;
miny = dstrect->y;
maxx = dstrect->x + dstrect->w;
maxy = dstrect->y + dstrect->h;
minu = (GLfloat) srcrect->x / texture->w;
minu *= texturedata->texw;
maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
maxu *= texturedata->texw;
minv = (GLfloat) srcrect->y / texture->h;
minv *= texturedata->texh;
maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
maxv *= texturedata->texh;
Sep 25, 2018
Sep 25, 2018
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
*(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;
626
627
628
629
630
return 0;
}
static int
Sep 25, 2018
Sep 25, 2018
631
632
633
GLES_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 25, 2018
Sep 25, 2018
635
GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
636
637
GLfloat minx, miny, maxx, maxy;
GLfloat centerx, centery;
Sep 25, 2018
Sep 25, 2018
638
639
GLfloat minu, maxu, minv, maxv;
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, 19 * sizeof (GLfloat), 0, &cmd->data.draw.first);
Sep 25, 2018
Sep 25, 2018
641
642
if (!verts) {
return -1;
643
644
645
646
647
648
649
650
}
centerx = center->x;
centery = center->y;
if (flip & SDL_FLIP_HORIZONTAL) {
minx = dstrect->w - centerx;
maxx = -centerx;
Sep 25, 2018
Sep 25, 2018
651
652
}
else {
Sep 25, 2018
Sep 25, 2018
654
maxx = dstrect->w - centerx;
655
656
657
}
if (flip & SDL_FLIP_VERTICAL) {
Sep 25, 2018
Sep 25, 2018
658
miny = dstrect->h - centery;
Sep 25, 2018
Sep 25, 2018
660
661
}
else {
Sep 25, 2018
Sep 25, 2018
663
maxy = dstrect->h - centery;
Sep 25, 2018
Sep 25, 2018
666
minu = (GLfloat) srcquad->x / texture->w;
667
minu *= texturedata->texw;
Sep 25, 2018
Sep 25, 2018
668
maxu = (GLfloat) (srcquad->x + srcquad->w) / texture->w;
669
maxu *= texturedata->texw;
Sep 25, 2018
Sep 25, 2018
670
minv = (GLfloat) srcquad->y / texture->h;
671
minv *= texturedata->texh;
Sep 25, 2018
Sep 25, 2018
672
maxv = (GLfloat) (srcquad->y + srcquad->h) / texture->h;
673
674
maxv *= texturedata->texh;
Sep 25, 2018
Sep 25, 2018
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
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++) = (GLfloat) dstrect->x + centerx;
*(verts++) = (GLfloat) dstrect->y + centery;
*(verts++) = (GLfloat) angle;
698
699
700
701
return 0;
}
Sep 25, 2018
Sep 25, 2018
702
static void
Sep 26, 2018
Sep 26, 2018
703
SetDrawState(GLES_RenderData *data, const SDL_RenderCommand *cmd)
Sep 25, 2018
Sep 25, 2018
704
705
{
const SDL_BlendMode blend = cmd->data.draw.blend;
Dec 29, 2018
Dec 29, 2018
706
707
708
709
const Uint8 r = cmd->data.draw.r;
const Uint8 g = cmd->data.draw.g;
const Uint8 b = cmd->data.draw.b;
const Uint8 a = cmd->data.draw.a;
Oct 2, 2018
Oct 2, 2018
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
const Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
if (color != data->drawstate.color) {
const GLfloat fr = ((GLfloat) r) * inv255f;
const GLfloat fg = ((GLfloat) g) * inv255f;
const GLfloat fb = ((GLfloat) b) * inv255f;
const GLfloat fa = ((GLfloat) a) * inv255f;
data->glColor4f(fr, fg, fb, fa);
data->drawstate.color = color;
}
if (data->drawstate.viewport_dirty) {
const SDL_Rect *viewport = &data->drawstate.viewport;
const SDL_bool istarget = (data->drawstate.target != NULL);
data->glMatrixMode(GL_PROJECTION);
data->glLoadIdentity();
data->glViewport(viewport->x,
istarget ? viewport->y : (data->drawstate.drawableh - viewport->y - viewport->h),
viewport->w, viewport->h);
if (viewport->w && viewport->h) {
data->glOrthof((GLfloat) 0, (GLfloat) viewport->w,
Nov 17, 2018
Nov 17, 2018
731
732
(GLfloat) (istarget ? 0 : viewport->h),
(GLfloat) (istarget ? viewport->h : 0),
Oct 2, 2018
Oct 2, 2018
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
0.0, 1.0);
}
data->glMatrixMode(GL_MODELVIEW);
data->drawstate.viewport_dirty = SDL_FALSE;
}
if (data->drawstate.cliprect_enabled_dirty) {
if (data->drawstate.cliprect_enabled) {
data->glEnable(GL_SCISSOR_TEST);
} else {
data->glDisable(GL_SCISSOR_TEST);
}
data->drawstate.cliprect_enabled_dirty = SDL_FALSE;
}
if (data->drawstate.cliprect_enabled && data->drawstate.cliprect_dirty) {
const SDL_Rect *viewport = &data->drawstate.viewport;
const SDL_Rect *rect = &data->drawstate.cliprect;
const SDL_bool istarget = (data->drawstate.target != NULL);
data->glScissor(viewport->x + rect->x,
istarget ? viewport->y + rect->y : data->drawstate.drawableh - viewport->y - rect->y - rect->h,
rect->w, rect->h);
data->drawstate.cliprect_dirty = SDL_FALSE;
}
Sep 25, 2018
Sep 25, 2018
757
Sep 26, 2018
Sep 26, 2018
758
if (blend != data->drawstate.blend) {
Sep 25, 2018
Sep 25, 2018
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
if (blend == SDL_BLENDMODE_NONE) {
data->glDisable(GL_BLEND);
} else {
data->glEnable(GL_BLEND);
if (data->GL_OES_blend_func_separate_supported) {
data->glBlendFuncSeparateOES(GetBlendFunc(SDL_GetBlendModeSrcColorFactor(blend)),
GetBlendFunc(SDL_GetBlendModeDstColorFactor(blend)),
GetBlendFunc(SDL_GetBlendModeSrcAlphaFactor(blend)),
GetBlendFunc(SDL_GetBlendModeDstAlphaFactor(blend)));
} else {
data->glBlendFunc(GetBlendFunc(SDL_GetBlendModeSrcColorFactor(blend)),
GetBlendFunc(SDL_GetBlendModeDstColorFactor(blend)));
}
if (data->GL_OES_blend_equation_separate_supported) {
data->glBlendEquationSeparateOES(GetBlendEquation(SDL_GetBlendModeColorOperation(blend)),
GetBlendEquation(SDL_GetBlendModeAlphaOperation(blend)));
} else if (data->GL_OES_blend_subtract_supported) {
data->glBlendEquationOES(GetBlendEquation(SDL_GetBlendModeColorOperation(blend)));
}
}
Sep 26, 2018
Sep 26, 2018
779
data->drawstate.blend = blend;
Sep 25, 2018
Sep 25, 2018
780
781
}
Sep 26, 2018
Sep 26, 2018
782
if ((cmd->data.draw.texture != NULL) != data->drawstate.texturing) {
Sep 25, 2018
Sep 25, 2018
783
784
785
if (cmd->data.draw.texture == NULL) {
data->glDisable(GL_TEXTURE_2D);
data->glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Sep 26, 2018
Sep 26, 2018
786
data->drawstate.texturing = SDL_FALSE;
Sep 25, 2018
Sep 25, 2018
787
788
789
} else {
data->glEnable(GL_TEXTURE_2D);
data->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Sep 26, 2018
Sep 26, 2018
790
data->drawstate.texturing = SDL_TRUE;
Sep 25, 2018
Sep 25, 2018
791
792
793
794
795
}
}
}
static void
Sep 29, 2018
Sep 29, 2018
796
SetCopyState(GLES_RenderData *data, const SDL_RenderCommand *cmd)
Sep 25, 2018
Sep 25, 2018
797
798
{
SDL_Texture *texture = cmd->data.draw.texture;
Sep 26, 2018
Sep 26, 2018
799
SetDrawState(data, cmd);
Sep 25, 2018
Sep 25, 2018
800
Sep 26, 2018
Sep 26, 2018
801
if (texture != data->drawstate.texture) {
Sep 25, 2018
Sep 25, 2018
802
803
GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
data->glBindTexture(GL_TEXTURE_2D, texturedata->texture);
Sep 26, 2018
Sep 26, 2018
804
data->drawstate.texture = texture;
Sep 25, 2018
Sep 25, 2018
805
806
807
808
809
810
811
812
813
814
815
816
817
}
}
static int
GLES_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
size_t i;
if (GLES_ActivateRenderer(renderer) < 0) {
return -1;
}
Oct 2, 2018
Oct 2, 2018
818
819
820
821
data->drawstate.target = renderer->target;
if (!renderer->target) {
SDL_GL_GetDrawableSize(renderer->window, &data->drawstate.drawablew, &data->drawstate.drawableh);
Sep 25, 2018
Sep 25, 2018
822
823
824
825
826
}
while (cmd) {
switch (cmd->command) {
case SDL_RENDERCMD_SETDRAWCOLOR: {
Oct 2, 2018
Oct 2, 2018
827
break; /* not used in this render backend. */
Sep 25, 2018
Sep 25, 2018
828
829
830
}
case SDL_RENDERCMD_SETVIEWPORT: {
Sep 26, 2018
Sep 26, 2018
831
832
833
SDL_Rect *viewport = &data->drawstate.viewport;
if (SDL_memcmp(viewport, &cmd->data.viewport.rect, sizeof (SDL_Rect)) != 0) {
SDL_memcpy(viewport, &cmd->data.viewport.rect, sizeof (SDL_Rect));
Oct 2, 2018
Oct 2, 2018
834
data->drawstate.viewport_dirty = SDL_TRUE;
Sep 29, 2018
Sep 29, 2018
835
}
Sep 25, 2018
Sep 25, 2018
836
837
838
839
840
break;
}
case SDL_RENDERCMD_SETCLIPRECT: {
const SDL_Rect *rect = &cmd->data.cliprect.rect;
Sep 26, 2018
Sep 26, 2018
841
842
if (data->drawstate.cliprect_enabled != cmd->data.cliprect.enabled) {
data->drawstate.cliprect_enabled = cmd->data.cliprect.enabled;
Oct 2, 2018
Oct 2, 2018
843
844
845
846
847
data->drawstate.cliprect_enabled_dirty = SDL_TRUE;
}
if (SDL_memcmp(&data->drawstate.cliprect, rect, sizeof (SDL_Rect)) != 0) {
SDL_memcpy(&data->drawstate.cliprect, rect, sizeof (SDL_Rect));
data->drawstate.cliprect_dirty = SDL_TRUE;
Sep 25, 2018
Sep 25, 2018
848
849
850
851
852
}
break;
}
case SDL_RENDERCMD_CLEAR: {
Sep 26, 2018
Sep 26, 2018
853
854
855
856
857
858
859
860
861
862
863
864
865
const Uint8 r = cmd->data.color.r;
const Uint8 g = cmd->data.color.g;
const Uint8 b = cmd->data.color.b;
const Uint8 a = cmd->data.color.a;
const Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
if (color != data->drawstate.clear_color) {
const GLfloat fr = ((GLfloat) r) * inv255f;
const GLfloat fg = ((GLfloat) g) * inv255f;
const GLfloat fb = ((GLfloat) b) * inv255f;
const GLfloat fa = ((GLfloat) a) * inv255f;
data->glClearColor(fr, fg, fb, fa);
data->drawstate.clear_color = color;
}
Sep 25, 2018
Sep 25, 2018
866
Sep 29, 2018
Sep 29, 2018
867
if (data->drawstate.cliprect_enabled) {
Sep 25, 2018
Sep 25, 2018
868
data->glDisable(GL_SCISSOR_TEST);
Oct 2, 2018
Oct 2, 2018
869
data->drawstate.cliprect_enabled_dirty = SDL_TRUE;
Sep 25, 2018
Sep 25, 2018
870
871
872
873
874
875
876
877
878
879
}
data->glClear(GL_COLOR_BUFFER_BIT);
break;
}
case SDL_RENDERCMD_DRAW_POINTS: {
const size_t count = cmd->data.draw.count;
const GLfloat *verts = (GLfloat *) (((Uint8 *) vertices) + cmd->data.draw.first);
Sep 26, 2018
Sep 26, 2018
880
SetDrawState(data, cmd);
Sep 25, 2018
Sep 25, 2018
881
data->glVertexPointer(2, GL_FLOAT, 0, verts);
Nov 17, 2018
Nov 17, 2018
882
data->glDrawArrays(GL_POINTS, 0, (GLsizei) count);
Sep 25, 2018
Sep 25, 2018
883
884
885
886
887
break;
}
case SDL_RENDERCMD_DRAW_LINES: {
const GLfloat *verts = (GLfloat *) (((Uint8 *) vertices) + cmd->data.draw.first);
Sep 25, 2018
Sep 25, 2018
888
const size_t count = cmd->data.draw.count;
Sep 26, 2018
Sep 26, 2018
889
SetDrawState(data, cmd);
Sep 25, 2018
Sep 25, 2018
890
data->glVertexPointer(2, GL_FLOAT, 0, verts);
Sep 25, 2018
Sep 25, 2018
891
if (count > 2 && (verts[0] == verts[(count-1)*2]) && (verts[1] == verts[(count*2)-1])) {
Sep 25, 2018
Sep 25, 2018
892
/* GL_LINE_LOOP takes care of the final segment */
Nov 17, 2018
Nov 17, 2018
893
data->glDrawArrays(GL_LINE_LOOP, 0, (GLsizei) (count - 1));
Sep 25, 2018
Sep 25, 2018
894
} else {
Nov 17, 2018
Nov 17, 2018
895
data->glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) count);
Sep 25, 2018
Sep 25, 2018
896
/* We need to close the endpoint of the line */
Nov 17, 2018
Nov 17, 2018
897
data->glDrawArrays(GL_POINTS, (GLsizei) (count - 1), 1);
Sep 25, 2018
Sep 25, 2018
898
899
900
901
902
903
904
}
break;
}
case SDL_RENDERCMD_FILL_RECTS: {
const size_t count = cmd->data.draw.count;
const GLfloat *verts = (GLfloat *) (((Uint8 *) vertices) + cmd->data.draw.first);
Nov 17, 2018
Nov 17, 2018
905
GLsizei offset = 0;
Sep 26, 2018
Sep 26, 2018
906
SetDrawState(data, cmd);
Sep 25, 2018
Sep 25, 2018
907
908
909
910
911
912
913
914
915
data->glVertexPointer(2, GL_FLOAT, 0, verts);
for (i = 0; i < count; ++i, offset += 4) {
data->glDrawArrays(GL_TRIANGLE_STRIP, offset, 4);
}
break;
}
case SDL_RENDERCMD_COPY: {
const GLfloat *verts = (GLfloat *) (((Uint8 *) vertices) + cmd->data.draw.first);
Sep 26, 2018
Sep 26, 2018
916
SetCopyState(data, cmd);
Sep 25, 2018
Sep 25, 2018
917
918
919
920
921
922
923
924
925
926
927
data->glVertexPointer(2, GL_FLOAT, 0, verts);
data->glTexCoordPointer(2, GL_FLOAT, 0, verts + 8);
data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
break;
}
case SDL_RENDERCMD_COPY_EX: {
const GLfloat *verts = (GLfloat *) (((Uint8 *) vertices) + cmd->data.draw.first);
const GLfloat translatex = verts[16];
const GLfloat translatey = verts[17];
const GLfloat angle = verts[18];
Sep 26, 2018
Sep 26, 2018
928
SetCopyState(data, cmd);
Sep 25, 2018
Sep 25, 2018
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
data->glVertexPointer(2, GL_FLOAT, 0, verts);
data->glTexCoordPointer(2, GL_FLOAT, 0, verts + 8);
/* Translate to flip, rotate, translate to position */
data->glPushMatrix();
data->glTranslatef(translatex, translatey, 0.0f);
data->glRotatef(angle, 0.0, 0.0, 1.0);
data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
data->glPopMatrix();
break;
}
case SDL_RENDERCMD_NO_OP:
break;
}
cmd = cmd->next;
}
Sep 25, 2018
Sep 25, 2018
948
return 0;
Sep 25, 2018
Sep 25, 2018
949
950
}
951
952
static int
GLES_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Sep 29, 2018
Sep 29, 2018
953
Uint32 pixel_format, void * pixels, int pitch)
954
955
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
Oct 1, 2016
Oct 1, 2016
956
Uint32 temp_format = renderer->target ? renderer->target->format : SDL_PIXELFORMAT_ABGR8888;
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
void *temp_pixels;
int temp_pitch;
Uint8 *src, *dst, *tmp;
int w, h, length, rows;
int status;
GLES_ActivateRenderer(renderer);
temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
temp_pixels = SDL_malloc(rect->h * temp_pitch);
if (!temp_pixels) {
return SDL_OutOfMemory();
}
SDL_GetRendererOutputSize(renderer, &w, &h);
data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
Oct 1, 2016
Oct 1, 2016
975
976
977
978
979
data->glReadPixels(rect->x, renderer->target ? rect->y : (h-rect->y)-rect->h,
rect->w, rect->h, GL_RGBA, GL_UNSIGNED_BYTE, temp_pixels);
/* Flip the rows to be top-down if necessary */
if (!renderer->target) {
Oct 23, 2018
Oct 23, 2018
980
SDL_bool isstack;
Oct 1, 2016
Oct 1, 2016
981
982
983
length = rect->w * SDL_BYTESPERPIXEL(temp_format);
src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
dst = (Uint8*)temp_pixels;
Oct 23, 2018
Oct 23, 2018
984
tmp = SDL_small_alloc(Uint8, length, &isstack);
Oct 1, 2016
Oct 1, 2016
985
986
987
988
989
990
991
992
rows = rect->h / 2;
while (rows--) {
SDL_memcpy(tmp, dst, length);
SDL_memcpy(dst, src, length);
SDL_memcpy(src, tmp, length);
dst += temp_pitch;
src -= temp_pitch;
}
Oct 23, 2018
Oct 23, 2018
993
SDL_small_free(tmp, isstack);
994
995
996
997
998
999
1000
}
status = SDL_ConvertPixels(rect->w, rect->h,
temp_format, temp_pixels, temp_pitch,
pixel_format, pixels, pitch);
SDL_free(temp_pixels);