Skip to content

Latest commit

 

History

History
1743 lines (1515 loc) · 60.3 KB

SDL_render_gl.c

File metadata and controls

1743 lines (1515 loc) · 60.3 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
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
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 && !SDL_RENDER_DISABLED
#include "SDL_hints.h"
#include "SDL_log.h"
#include "SDL_assert.h"
#include "SDL_opengl.h"
#include "../SDL_sysrender.h"
#include "SDL_shaders_gl.h"
#ifdef __MACOSX__
#include <OpenGL/OpenGL.h>
#endif
/* To prevent unnecessary window recreation,
* these should match the defaults selected in SDL_GL_ResetAttributes
*/
#define RENDERER_CONTEXT_MAJOR 2
#define RENDERER_CONTEXT_MINOR 1
/* OpenGL renderer implementation */
/* Details on optimizing the texture path on Mac OS X:
http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/opengl_texturedata.html
*/
/* Used to re-create the window with OpenGL capability */
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
static const float inv255f = 1.0f / 255.0f;
typedef struct GL_FBOList GL_FBOList;
struct GL_FBOList
{
Uint32 w, h;
GLuint FBO;
GL_FBOList *next;
};
Sep 25, 2018
Sep 25, 2018
63
64
typedef struct
{
Oct 2, 2018
Oct 2, 2018
65
SDL_bool viewport_dirty;
Sep 25, 2018
Sep 25, 2018
66
67
SDL_Rect viewport;
SDL_Texture *texture;
Oct 2, 2018
Oct 2, 2018
68
69
70
SDL_Texture *target;
int drawablew;
int drawableh;
Sep 25, 2018
Sep 25, 2018
71
72
SDL_BlendMode blend;
GL_Shader shader;
Oct 2, 2018
Oct 2, 2018
73
SDL_bool cliprect_enabled_dirty;
Sep 25, 2018
Sep 25, 2018
74
SDL_bool cliprect_enabled;
Oct 2, 2018
Oct 2, 2018
75
SDL_bool cliprect_dirty;
Sep 25, 2018
Sep 25, 2018
76
77
78
79
80
81
SDL_Rect cliprect;
SDL_bool texturing;
Uint32 color;
Uint32 clear_color;
} GL_DrawStateCache;
82
83
84
85
86
87
88
89
90
91
92
typedef struct
{
SDL_GLContext context;
SDL_bool debug_enabled;
SDL_bool GL_ARB_debug_output_supported;
int errors;
char **error_messages;
GLDEBUGPROCARB next_error_callback;
GLvoid *next_error_userparam;
Sep 20, 2018
Sep 20, 2018
93
94
GLenum textype;
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
SDL_bool GL_ARB_texture_non_power_of_two_supported;
SDL_bool GL_ARB_texture_rectangle_supported;
SDL_bool GL_EXT_framebuffer_object_supported;
GL_FBOList *framebuffers;
/* OpenGL functions */
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
#include "SDL_glfuncs.h"
#undef SDL_PROC
/* Multitexture support */
SDL_bool GL_ARB_multitexture_supported;
PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;
GLint num_texture_units;
PFNGLGENFRAMEBUFFERSEXTPROC glGenFramebuffersEXT;
PFNGLDELETEFRAMEBUFFERSEXTPROC glDeleteFramebuffersEXT;
PFNGLFRAMEBUFFERTEXTURE2DEXTPROC glFramebufferTexture2DEXT;
PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT;
PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC glCheckFramebufferStatusEXT;
/* Shader support */
GL_ShaderContext *shaders;
Sep 25, 2018
Sep 25, 2018
119
GL_DrawStateCache drawstate;
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
} GL_RenderData;
typedef struct
{
GLuint texture;
GLfloat texw;
GLfloat texh;
GLenum format;
GLenum formattype;
void *pixels;
int pitch;
SDL_Rect locked_rect;
/* YUV texture support */
SDL_bool yuv;
SDL_bool nv12;
GLuint utexture;
GLuint vtexture;
GL_FBOList *fbo;
} GL_TextureData;
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)
GL_ERROR_TRANSLATE(GL_STACK_OVERFLOW)
GL_ERROR_TRANSLATE(GL_STACK_UNDERFLOW)
GL_ERROR_TRANSLATE(GL_TABLE_TOO_LARGE)
default:
return "UNKNOWN";
}
#undef GL_ERROR_TRANSLATE
}
SDL_FORCE_INLINE void
GL_ClearErrors(SDL_Renderer *renderer)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
if (!data->debug_enabled)
{
return;
}
if (data->GL_ARB_debug_output_supported) {
if (data->errors) {
int i;
for (i = 0; i < data->errors; ++i) {
SDL_free(data->error_messages[i]);
}
SDL_free(data->error_messages);
data->errors = 0;
data->error_messages = NULL;
}
} else {
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)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
int ret = 0;
if (!data->debug_enabled)
{
return 0;
}
if (data->GL_ARB_debug_output_supported) {
if (data->errors) {
int i;
for (i = 0; i < data->errors; ++i) {
SDL_SetError("%s: %s (%d): %s %s", prefix, file, line, function, data->error_messages[i]);
ret = -1;
}
GL_ClearErrors(renderer);
}
} else {
/* 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
228
#define GL_CheckError(prefix, renderer) GL_CheckAllErrors(prefix, renderer, SDL_FILE, SDL_LINE, SDL_FUNCTION)
229
230
231
232
233
234
235
236
237
238
#endif
static int
GL_LoadFunctions(GL_RenderData * data)
{
#ifdef __SDL_NOGETPROCADDR__
#define SDL_PROC(ret,func,params) data->func=func;
#else
#define SDL_PROC(ret,func,params) \
do { \
Feb 12, 2018
Feb 12, 2018
239
data->func = SDL_GL_GetProcAddress(#func); \
240
if ( ! data->func ) { \
Mar 26, 2017
Mar 26, 2017
241
return SDL_SetError("Couldn't load GL function %s: %s", #func, SDL_GetError()); \
242
243
244
245
246
247
248
249
250
251
252
253
254
255
} \
} while ( 0 );
#endif /* __SDL_NOGETPROCADDR__ */
#include "SDL_glfuncs.h"
#undef SDL_PROC
return 0;
}
static int
GL_ActivateRenderer(SDL_Renderer * renderer)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
Sep 20, 2018
Sep 20, 2018
256
if (SDL_GL_GetCurrentContext() != data->context) {
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
if (SDL_GL_MakeCurrent(renderer->window, data->context) < 0) {
return -1;
}
}
GL_ClearErrors(renderer);
return 0;
}
static void APIENTRY
GL_HandleDebugMessage(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const char *message, const void *userParam)
{
SDL_Renderer *renderer = (SDL_Renderer *) userParam;
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
if (type == GL_DEBUG_TYPE_ERROR_ARB) {
/* Record this error */
int errors = data->errors + 1;
char **error_messages = SDL_realloc(data->error_messages, errors * sizeof(*data->error_messages));
if (error_messages) {
data->errors = errors;
data->error_messages = error_messages;
data->error_messages[data->errors-1] = SDL_strdup(message);
}
}
/* If there's another error callback, pass it along, otherwise log it */
if (data->next_error_callback) {
data->next_error_callback(source, type, id, severity, length, message, data->next_error_userparam);
} else {
if (type == GL_DEBUG_TYPE_ERROR_ARB) {
SDL_LogError(SDL_LOG_CATEGORY_RENDER, "%s", message);
} else {
SDL_LogDebug(SDL_LOG_CATEGORY_RENDER, "%s", message);
}
}
}
static GL_FBOList *
GL_GetFBO(GL_RenderData *data, Uint32 w, Uint32 h)
{
GL_FBOList *result = data->framebuffers;
while (result && ((result->w != w) || (result->h != h))) {
result = result->next;
}
if (!result) {
result = SDL_malloc(sizeof(GL_FBOList));
if (result) {
result->w = w;
result->h = h;
data->glGenFramebuffersEXT(1, &result->FBO);
result->next = data->framebuffers;
data->framebuffers = result;
}
}
return result;
}
static int
GL_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
{
SDL_GL_GetDrawableSize(renderer->window, w, h);
return 0;
}
Aug 14, 2017
Aug 14, 2017
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
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
390
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
GL_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;
}
if (colorOperation != alphaOperation) {
return SDL_FALSE;
}
return SDL_TRUE;
}
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
SDL_FORCE_INLINE int
power_of_2(int input)
{
int value = 1;
while (value < input) {
value <<= 1;
}
return value;
}
SDL_FORCE_INLINE SDL_bool
convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
GLint* internalFormat, GLenum* format, GLenum* type)
{
switch (pixel_format) {
case SDL_PIXELFORMAT_ARGB8888:
*internalFormat = GL_RGBA8;
*format = GL_BGRA;
*type = GL_UNSIGNED_INT_8_8_8_8_REV;
break;
case SDL_PIXELFORMAT_YV12:
case SDL_PIXELFORMAT_IYUV:
case SDL_PIXELFORMAT_NV12:
case SDL_PIXELFORMAT_NV21:
*internalFormat = GL_LUMINANCE;
*format = GL_LUMINANCE;
*type = GL_UNSIGNED_BYTE;
break;
#ifdef __MACOSX__
case SDL_PIXELFORMAT_UYVY:
*internalFormat = GL_RGB8;
*format = GL_YCBCR_422_APPLE;
*type = GL_UNSIGNED_SHORT_8_8_APPLE;
break;
#endif
default:
return SDL_FALSE;
}
return SDL_TRUE;
}
static int
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
Sep 20, 2018
Sep 20, 2018
437
const GLenum textype = renderdata->textype;
438
439
440
441
442
443
444
445
GL_TextureData *data;
GLint internalFormat;
GLenum format, type;
int texture_w, texture_h;
GLenum scaleMode;
GL_ActivateRenderer(renderer);
Feb 4, 2019
Feb 4, 2019
446
447
renderdata->drawstate.texture = NULL; /* we trash this state. */
Oct 1, 2016
Oct 1, 2016
448
449
450
451
452
if (texture->access == SDL_TEXTUREACCESS_TARGET &&
!renderdata->GL_EXT_framebuffer_object_supported) {
return SDL_SetError("Render targets not supported by OpenGL");
}
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
if (!convert_format(renderdata, texture->format, &internalFormat,
&format, &type)) {
return SDL_SetError("Texture format %s not supported by OpenGL",
SDL_GetPixelFormatName(texture->format));
}
data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
if (!data) {
return SDL_OutOfMemory();
}
if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
size_t size;
data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
size = texture->h * data->pitch;
if (texture->format == SDL_PIXELFORMAT_YV12 ||
texture->format == SDL_PIXELFORMAT_IYUV) {
/* Need to add size for the U and V planes */
Oct 6, 2017
Oct 6, 2017
471
size += 2 * ((texture->h + 1) / 2) * ((data->pitch + 1) / 2);
472
473
474
475
}
if (texture->format == SDL_PIXELFORMAT_NV12 ||
texture->format == SDL_PIXELFORMAT_NV21) {
/* Need to add size for the U/V plane */
Oct 6, 2017
Oct 6, 2017
476
size += 2 * ((texture->h + 1) / 2) * ((data->pitch + 1) / 2);
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
}
data->pixels = SDL_calloc(1, size);
if (!data->pixels) {
SDL_free(data);
return SDL_OutOfMemory();
}
}
if (texture->access == SDL_TEXTUREACCESS_TARGET) {
data->fbo = GL_GetFBO(renderdata, texture->w, texture->h);
} else {
data->fbo = NULL;
}
GL_CheckError("", renderer);
renderdata->glGenTextures(1, &data->texture);
if (GL_CheckError("glGenTextures()", renderer) < 0) {
if (data->pixels) {
SDL_free(data->pixels);
}
SDL_free(data);
return -1;
}
texture->driverdata = data;
if (renderdata->GL_ARB_texture_non_power_of_two_supported) {
texture_w = texture->w;
texture_h = texture->h;
data->texw = 1.0f;
data->texh = 1.0f;
} else if (renderdata->GL_ARB_texture_rectangle_supported) {
texture_w = texture->w;
texture_h = texture->h;
data->texw = (GLfloat) texture_w;
data->texh = (GLfloat) texture_h;
} else {
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
521
scaleMode = (texture->scaleMode == SDL_ScaleModeNearest) ? GL_NEAREST : GL_LINEAR;
Sep 20, 2018
Sep 20, 2018
522
523
524
525
renderdata->glEnable(textype);
renderdata->glBindTexture(textype, data->texture);
renderdata->glTexParameteri(textype, GL_TEXTURE_MIN_FILTER, scaleMode);
renderdata->glTexParameteri(textype, GL_TEXTURE_MAG_FILTER, scaleMode);
526
527
528
/* According to the spec, CLAMP_TO_EDGE is the default for TEXTURE_RECTANGLE
and setting it causes an INVALID_ENUM error in the latest NVidia drivers.
*/
Sep 20, 2018
Sep 20, 2018
529
530
if (textype != GL_TEXTURE_RECTANGLE_ARB) {
renderdata->glTexParameteri(textype, GL_TEXTURE_WRAP_S,
Sep 20, 2018
Sep 20, 2018
532
renderdata->glTexParameteri(textype, GL_TEXTURE_WRAP_T,
533
534
535
536
537
538
539
540
541
542
543
544
545
GL_CLAMP_TO_EDGE);
}
#ifdef __MACOSX__
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
#define GL_TEXTURE_STORAGE_HINT_APPLE 0x85BC
#endif
#ifndef STORAGE_CACHED_APPLE
#define STORAGE_CACHED_APPLE 0x85BE
#endif
#ifndef STORAGE_SHARED_APPLE
#define STORAGE_SHARED_APPLE 0x85BF
#endif
if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
Sep 20, 2018
Sep 20, 2018
546
renderdata->glTexParameteri(textype, GL_TEXTURE_STORAGE_HINT_APPLE,
547
548
GL_STORAGE_SHARED_APPLE);
} else {
Sep 20, 2018
Sep 20, 2018
549
renderdata->glTexParameteri(textype, GL_TEXTURE_STORAGE_HINT_APPLE,
550
551
552
553
554
555
556
557
558
GL_STORAGE_CACHED_APPLE);
}
if (texture->access == SDL_TEXTUREACCESS_STREAMING
&& texture->format == SDL_PIXELFORMAT_ARGB8888
&& (texture->w % 8) == 0) {
renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
(data->pitch / SDL_BYTESPERPIXEL(texture->format)));
Sep 20, 2018
Sep 20, 2018
559
renderdata->glTexImage2D(textype, 0, internalFormat, texture_w,
560
561
562
563
564
565
texture_h, 0, format, type, data->pixels);
renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
}
else
#endif
{
Sep 20, 2018
Sep 20, 2018
566
renderdata->glTexImage2D(textype, 0, internalFormat, texture_w,
567
568
texture_h, 0, format, type, NULL);
}
Sep 20, 2018
Sep 20, 2018
569
renderdata->glDisable(textype);
570
571
572
573
574
575
576
577
578
579
if (GL_CheckError("glTexImage2D()", renderer) < 0) {
return -1;
}
if (texture->format == SDL_PIXELFORMAT_YV12 ||
texture->format == SDL_PIXELFORMAT_IYUV) {
data->yuv = SDL_TRUE;
renderdata->glGenTextures(1, &data->utexture);
renderdata->glGenTextures(1, &data->vtexture);
Sep 20, 2018
Sep 20, 2018
580
renderdata->glEnable(textype);
Sep 20, 2018
Sep 20, 2018
582
583
renderdata->glBindTexture(textype, data->utexture);
renderdata->glTexParameteri(textype, GL_TEXTURE_MIN_FILTER,
Sep 20, 2018
Sep 20, 2018
585
renderdata->glTexParameteri(textype, GL_TEXTURE_MAG_FILTER,
Sep 20, 2018
Sep 20, 2018
587
renderdata->glTexParameteri(textype, GL_TEXTURE_WRAP_S,
Sep 20, 2018
Sep 20, 2018
589
renderdata->glTexParameteri(textype, GL_TEXTURE_WRAP_T,
Sep 20, 2018
Sep 20, 2018
591
renderdata->glTexImage2D(textype, 0, internalFormat, (texture_w+1)/2,
Oct 6, 2017
Oct 6, 2017
592
(texture_h+1)/2, 0, format, type, NULL);
Sep 20, 2018
Sep 20, 2018
594
595
renderdata->glBindTexture(textype, data->vtexture);
renderdata->glTexParameteri(textype, GL_TEXTURE_MIN_FILTER,
Sep 20, 2018
Sep 20, 2018
597
renderdata->glTexParameteri(textype, GL_TEXTURE_MAG_FILTER,
Sep 20, 2018
Sep 20, 2018
599
renderdata->glTexParameteri(textype, GL_TEXTURE_WRAP_S,
Sep 20, 2018
Sep 20, 2018
601
renderdata->glTexParameteri(textype, GL_TEXTURE_WRAP_T,
Sep 20, 2018
Sep 20, 2018
603
renderdata->glTexImage2D(textype, 0, internalFormat, (texture_w+1)/2,
Oct 6, 2017
Oct 6, 2017
604
(texture_h+1)/2, 0, format, type, NULL);
Sep 20, 2018
Sep 20, 2018
606
renderdata->glDisable(textype);
607
608
609
610
611
612
613
}
if (texture->format == SDL_PIXELFORMAT_NV12 ||
texture->format == SDL_PIXELFORMAT_NV21) {
data->nv12 = SDL_TRUE;
renderdata->glGenTextures(1, &data->utexture);
Sep 20, 2018
Sep 20, 2018
614
renderdata->glEnable(textype);
Sep 20, 2018
Sep 20, 2018
616
617
renderdata->glBindTexture(textype, data->utexture);
renderdata->glTexParameteri(textype, GL_TEXTURE_MIN_FILTER,
Sep 20, 2018
Sep 20, 2018
619
renderdata->glTexParameteri(textype, GL_TEXTURE_MAG_FILTER,
Sep 20, 2018
Sep 20, 2018
621
renderdata->glTexParameteri(textype, GL_TEXTURE_WRAP_S,
Sep 20, 2018
Sep 20, 2018
623
renderdata->glTexParameteri(textype, GL_TEXTURE_WRAP_T,
Sep 20, 2018
Sep 20, 2018
625
renderdata->glTexImage2D(textype, 0, GL_LUMINANCE_ALPHA, (texture_w+1)/2,
Oct 6, 2017
Oct 6, 2017
626
(texture_h+1)/2, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, NULL);
Sep 20, 2018
Sep 20, 2018
627
renderdata->glDisable(textype);
628
629
630
631
632
633
634
635
636
637
}
return GL_CheckError("", renderer);
}
static int
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
{
GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
Sep 20, 2018
Sep 20, 2018
638
const GLenum textype = renderdata->textype;
639
640
641
642
643
644
645
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
const int texturebpp = SDL_BYTESPERPIXEL(texture->format);
SDL_assert(texturebpp != 0); /* otherwise, division by zero later. */
GL_ActivateRenderer(renderer);
Feb 4, 2019
Feb 4, 2019
646
647
renderdata->drawstate.texture = NULL; /* we trash this state. */
Sep 20, 2018
Sep 20, 2018
648
649
renderdata->glEnable(textype);
renderdata->glBindTexture(textype, data->texture);
650
651
renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, (pitch / texturebpp));
Sep 20, 2018
Sep 20, 2018
652
renderdata->glTexSubImage2D(textype, 0, rect->x, rect->y, rect->w,
653
654
655
rect->h, data->format, data->formattype,
pixels);
if (data->yuv) {
Oct 6, 2017
Oct 6, 2017
656
renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, ((pitch + 1) / 2));
657
658
659
660
/* Skip to the correct offset into the next texture */
pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
if (texture->format == SDL_PIXELFORMAT_YV12) {
Sep 20, 2018
Sep 20, 2018
661
renderdata->glBindTexture(textype, data->vtexture);
Sep 20, 2018
Sep 20, 2018
663
renderdata->glBindTexture(textype, data->utexture);
Sep 20, 2018
Sep 20, 2018
665
renderdata->glTexSubImage2D(textype, 0, rect->x/2, rect->y/2,
Oct 6, 2017
Oct 6, 2017
666
(rect->w+1)/2, (rect->h+1)/2,
667
668
669
data->format, data->formattype, pixels);
/* Skip to the correct offset into the next texture */
Oct 6, 2017
Oct 6, 2017
670
pixels = (const void*)((const Uint8*)pixels + ((rect->h + 1) / 2) * ((pitch + 1) / 2));
671
if (texture->format == SDL_PIXELFORMAT_YV12) {
Sep 20, 2018
Sep 20, 2018
672
renderdata->glBindTexture(textype, data->utexture);
Sep 20, 2018
Sep 20, 2018
674
renderdata->glBindTexture(textype, data->vtexture);
Sep 20, 2018
Sep 20, 2018
676
renderdata->glTexSubImage2D(textype, 0, rect->x/2, rect->y/2,
Oct 6, 2017
Oct 6, 2017
677
(rect->w+1)/2, (rect->h+1)/2,
678
679
680
681
data->format, data->formattype, pixels);
}
if (data->nv12) {
Oct 6, 2017
Oct 6, 2017
682
renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, ((pitch + 1) / 2));
683
684
685
/* Skip to the correct offset into the next texture */
pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
Sep 20, 2018
Sep 20, 2018
686
687
renderdata->glBindTexture(textype, data->utexture);
renderdata->glTexSubImage2D(textype, 0, rect->x/2, rect->y/2,
Oct 6, 2017
Oct 6, 2017
688
(rect->w + 1)/2, (rect->h + 1)/2,
689
690
GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, pixels);
}
Sep 20, 2018
Sep 20, 2018
691
renderdata->glDisable(textype);
692
693
694
695
696
697
698
699
700
701
702
703
return GL_CheckError("glTexSubImage2D()", renderer);
}
static int
GL_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect,
const Uint8 *Yplane, int Ypitch,
const Uint8 *Uplane, int Upitch,
const Uint8 *Vplane, int Vpitch)
{
GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
Sep 20, 2018
Sep 20, 2018
704
const GLenum textype = renderdata->textype;
705
706
707
708
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
GL_ActivateRenderer(renderer);
Feb 4, 2019
Feb 4, 2019
709
710
renderdata->drawstate.texture = NULL; /* we trash this state. */
Sep 20, 2018
Sep 20, 2018
711
712
renderdata->glEnable(textype);
renderdata->glBindTexture(textype, data->texture);
713
714
renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Ypitch);
Sep 20, 2018
Sep 20, 2018
715
renderdata->glTexSubImage2D(textype, 0, rect->x, rect->y, rect->w,
716
717
718
719
rect->h, data->format, data->formattype,
Yplane);
renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Upitch);
Sep 20, 2018
Sep 20, 2018
720
721
renderdata->glBindTexture(textype, data->utexture);
renderdata->glTexSubImage2D(textype, 0, rect->x/2, rect->y/2,
Oct 6, 2017
Oct 6, 2017
722
(rect->w + 1)/2, (rect->h + 1)/2,
723
724
725
data->format, data->formattype, Uplane);
renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Vpitch);
Sep 20, 2018
Sep 20, 2018
726
727
renderdata->glBindTexture(textype, data->vtexture);
renderdata->glTexSubImage2D(textype, 0, rect->x/2, rect->y/2,
Oct 6, 2017
Oct 6, 2017
728
(rect->w + 1)/2, (rect->h + 1)/2,
729
data->format, data->formattype, Vplane);
Sep 20, 2018
Sep 20, 2018
730
renderdata->glDisable(textype);
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
return GL_CheckError("glTexSubImage2D()", renderer);
}
static int
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch)
{
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
data->locked_rect = *rect;
*pixels =
(void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format));
*pitch = data->pitch;
return 0;
}
static void
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
const SDL_Rect *rect;
void *pixels;
rect = &data->locked_rect;
pixels =
(void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format));
GL_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
}
static int
GL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
GL_TextureData *texturedata;
GLenum status;
GL_ActivateRenderer(renderer);
Oct 1, 2016
Oct 1, 2016
772
773
774
775
if (!data->GL_EXT_framebuffer_object_supported) {
return SDL_SetError("Render targets not supported by OpenGL");
}
776
777
778
779
780
781
782
783
if (texture == NULL) {
data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
return 0;
}
texturedata = (GL_TextureData *) texture->driverdata;
data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, texturedata->fbo->FBO);
/* TODO: check if texture pixel format allows this operation */
Sep 20, 2018
Sep 20, 2018
784
data->glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, data->textype, texturedata->texture, 0);
785
786
787
788
789
790
791
792
/* Check FBO status */
status = data->glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
return SDL_SetError("glFramebufferTexture2DEXT() failed");
}
return 0;
}
Sep 20, 2018
Sep 20, 2018
793
794
795
/* !!! FIXME: all these Queue* calls set up the vertex buffer the way the immediate mode
!!! FIXME: renderer wants it, but this might want to operate differently if we move to
!!! FIXME: VBOs at some point. */
Sep 24, 2018
Sep 24, 2018
796
797
798
799
800
801
static int
GL_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
{
return 0; /* nothing to do in this backend. */
}
Sep 20, 2018
Sep 20, 2018
803
GL_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
Sep 24, 2018
Sep 24, 2018
805
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, count * 2 * sizeof (GLfloat), 0, &cmd->data.draw.first);
Sep 20, 2018
Sep 20, 2018
806
size_t i;
Sep 20, 2018
Sep 20, 2018
808
809
if (!verts) {
return -1;
Oct 1, 2016
Oct 1, 2016
810
811
}
Sep 20, 2018
Sep 20, 2018
812
813
814
815
cmd->data.draw.count = count;
for (i = 0; i < count; i++) {
*(verts++) = 0.5f + points[i].x;
*(verts++) = 0.5f + points[i].y;
816
817
818
819
820
821
}
return 0;
}
static int
Sep 20, 2018
Sep 20, 2018
822
GL_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects, int count)
Sep 24, 2018
Sep 24, 2018
824
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, count * 4 * sizeof (GLfloat), 0, &cmd->data.draw.first);
Sep 20, 2018
Sep 20, 2018
825
size_t i;
Sep 20, 2018
Sep 20, 2018
827
828
if (!verts) {
return -1;
Sep 20, 2018
Sep 20, 2018
831
832
cmd->data.draw.count = count;
for (i = 0; i < count; i++) {
833
const SDL_FRect *rect = &rects[i];
Sep 20, 2018
Sep 20, 2018
834
835
836
837
*(verts++) = rect->x;
*(verts++) = rect->y;
*(verts++) = rect->x + rect->w;
*(verts++) = rect->y + rect->h;
838
839
840
841
842
843
}
return 0;
}
static int
Sep 20, 2018
Sep 20, 2018
844
845
GL_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_FRect * dstrect)
846
847
848
849
{
GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
GLfloat minx, miny, maxx, maxy;
GLfloat minu, maxu, minv, maxv;
Sep 24, 2018
Sep 24, 2018
850
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, 8 * sizeof (GLfloat), 0, &cmd->data.draw.first);
Sep 20, 2018
Sep 20, 2018
852
if (!verts) {
853
854
855
return -1;
}
Sep 20, 2018
Sep 20, 2018
856
857
cmd->data.draw.count = 1;
858
859
860
861
862
863
864
865
866
867
868
869
870
871
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 20, 2018
Sep 20, 2018
872
873
874
875
876
877
878
879
880
881
cmd->data.draw.count = 1;
*(verts++) = minx;
*(verts++) = miny;
*(verts++) = maxx;
*(verts++) = maxy;
*(verts++) = minu;
*(verts++) = maxu;
*(verts++) = minv;
*(verts++) = maxv;
return 0;
Sep 20, 2018
Sep 20, 2018
885
886
887
GL_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_FRect * dstrect,
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
888
889
890
891
892
{
GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
GLfloat minx, miny, maxx, maxy;
GLfloat centerx, centery;
GLfloat minu, maxu, minv, maxv;
Sep 24, 2018
Sep 24, 2018
893
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, 11 * sizeof (GLfloat), 0, &cmd->data.draw.first);
Sep 20, 2018
Sep 20, 2018
895
if (!verts) {
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
return -1;
}
centerx = center->x;
centery = center->y;
if (flip & SDL_FLIP_HORIZONTAL) {
minx = dstrect->w - centerx;
maxx = -centerx;
}
else {
minx = -centerx;
maxx = dstrect->w - centerx;
}
if (flip & SDL_FLIP_VERTICAL) {
miny = dstrect->h - centery;
maxy = -centery;
}
else {
miny = -centery;
maxy = dstrect->h - centery;
}
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 20, 2018
Sep 20, 2018
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
cmd->data.draw.count = 1;
*(verts++) = minx;
*(verts++) = miny;
*(verts++) = maxx;
*(verts++) = maxy;
*(verts++) = minu;
*(verts++) = maxu;
*(verts++) = minv;
*(verts++) = maxv;
*(verts++) = (GLfloat) dstrect->x + centerx;
*(verts++) = (GLfloat) dstrect->y + centery;
*(verts++) = (GLfloat) angle;
return 0;
}
static void
Sep 25, 2018
Sep 25, 2018
945
SetDrawState(GL_RenderData *data, const SDL_RenderCommand *cmd, const GL_Shader shader)
Sep 20, 2018
Sep 20, 2018
946
947
948
{
const SDL_BlendMode blend = cmd->data.draw.blend;
Oct 2, 2018
Oct 2, 2018
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
978
979
980
981
982
983
984
if (data->drawstate.viewport_dirty) {
const SDL_bool istarget = data->drawstate.target != NULL;
const SDL_Rect *viewport = &data->drawstate.viewport;
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->glOrtho((GLdouble) 0, (GLdouble) viewport->w,
(GLdouble) istarget ? 0 : viewport->h,
(GLdouble) istarget ? viewport->h : 0,
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->glDisable(GL_SCISSOR_TEST);
} else {
data->glEnable(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;
data->glScissor(viewport->x + rect->x,
data->drawstate.target ? 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
985
if (blend != data->drawstate.blend) {
Sep 20, 2018
Sep 20, 2018
986
987
988
989
990
991
992
993
994
995
if (blend == SDL_BLENDMODE_NONE) {
data->glDisable(GL_BLEND);
} else {
data->glEnable(GL_BLEND);
data->glBlendFuncSeparate(GetBlendFunc(SDL_GetBlendModeSrcColorFactor(blend)),
GetBlendFunc(SDL_GetBlendModeDstColorFactor(blend)),
GetBlendFunc(SDL_GetBlendModeSrcAlphaFactor(blend)),
GetBlendFunc(SDL_GetBlendModeDstAlphaFactor(blend)));
data->glBlendEquation(GetBlendEquation(SDL_GetBlendModeColorOperation(blend)));
}
Sep 25, 2018
Sep 25, 2018
996
data->drawstate.blend = blend;
Sep 20, 2018
Sep 20, 2018
997
998
}
Sep 25, 2018
Sep 25, 2018
999
if (data->shaders && (shader != data->drawstate.shader)) {
Sep 20, 2018
Sep 20, 2018
1000
GL_SelectShader(data->shaders, shader);