-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathrenderer_GL_common.inl
7079 lines (5907 loc) · 220 KB
/
renderer_GL_common.inl
1
2
3
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
228
229
230
231
232
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
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
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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
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
935
936
937
938
939
940
941
942
943
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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This is an implementation file to be included after certain #defines have been set.
See a particular renderer's *.c file for specifics. */
#ifdef _MSC_VER
// Disable warning: selection for inlining
#pragma warning(disable: 4514 4711 4710)
// Disable warning: Spectre mitigation
#pragma warning(disable: 5045)
// Disable warning: 'type cast': conversion from 'long' to 'void *' of greater size
#pragma warning (disable: 4312)
#endif
#if !defined(GLAPIENTRY)
#if defined(GL_APIENTRY)
#define GLAPIENTRY GL_APIENTRY
#else
#define GLAPIENTRY
#endif
#endif
#include <stdint.h>
#include <stdlib.h>
#include "SDL_platform.h"
#include "SDL_gpu.h" // For poor, dumb Intellisense
#include <math.h>
#include <string.h>
// Check for C99 support
// We'll use it for intptr_t which is used to suppress warnings about converting an int to a ptr for GL calls.
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
#include <stdint.h>
#else
#define intptr_t long
#endif
#include "stb_image.h"
#include "stb_image_write.h"
#ifndef PI
#define PI 3.1415926f
#endif
#define RAD_PER_DEG 0.017453293f
#define DEG_PER_RAD 57.2957795f
// Visual C does not support static inline
#ifndef static_inline
#ifdef _MSC_VER
#define static_inline static
#else
#define static_inline static inline
#endif
#endif
#if defined ( WIN32 ) && defined(_MSC_VER)
#define __func__ __FUNCTION__
#endif
// Old Visual C did not support C99 (which includes a safe snprintf)
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf c99_snprintf
// From Valentin Milea: http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010
static_inline int c99_vsnprintf(char* str, size_t size, const char* format, va_list ap)
{
int count = -1;
if (size != 0)
count = _vsnprintf_s(str, size, _TRUNCATE, format, ap);
if (count == -1)
count = _vscprintf(format, ap);
return count;
}
static_inline int c99_snprintf(char* str, size_t size, const char* format, ...)
{
int count;
va_list ap;
va_start(ap, format);
count = c99_vsnprintf(str, size, format, ap);
va_end(ap);
return count;
}
#endif
int gpu_strcasecmp(const char* s1, const char* s2);
// Default to buffer reset VBO upload method
#if defined(SDL_GPU_USE_BUFFER_PIPELINE) && !defined(SDL_GPU_USE_BUFFER_RESET) && !defined(SDL_GPU_USE_BUFFER_MAPPING) && !defined(SDL_GPU_USE_BUFFER_UPDATE)
#define SDL_GPU_USE_BUFFER_RESET
#endif
// Forces a flush when vertex limit is reached (roughly 1000 sprites)
#define GPU_BLIT_BUFFER_VERTICES_PER_SPRITE 4
#define GPU_BLIT_BUFFER_INIT_MAX_NUM_VERTICES (GPU_BLIT_BUFFER_VERTICES_PER_SPRITE*1000)
// Near the unsigned short limit (65535)
#define GPU_BLIT_BUFFER_ABSOLUTE_MAX_VERTICES 60000
// Near the unsigned int limit (4294967295)
#define GPU_INDEX_BUFFER_ABSOLUTE_MAX_VERTICES 4000000000u
// x, y, s, t, r, g, b, a
#define GPU_BLIT_BUFFER_FLOATS_PER_VERTEX 8
// bytes per vertex
#define GPU_BLIT_BUFFER_STRIDE (sizeof(float)*GPU_BLIT_BUFFER_FLOATS_PER_VERTEX)
#define GPU_BLIT_BUFFER_VERTEX_OFFSET 0
#define GPU_BLIT_BUFFER_TEX_COORD_OFFSET 2
#define GPU_BLIT_BUFFER_COLOR_OFFSET 4
// SDL 1.2 / SDL 2.0 translation layer
#ifdef SDL_GPU_USE_SDL2
#define GET_ALPHA(sdl_color) ((sdl_color).a)
static_inline SDL_Window* get_window(Uint32 windowID)
{
return SDL_GetWindowFromID(windowID);
}
static_inline Uint32 get_window_id(SDL_Window* window)
{
return SDL_GetWindowID(window);
}
static_inline void get_window_dimensions(SDL_Window* window, int* w, int* h)
{
SDL_GetWindowSize(window, w, h);
}
static_inline void get_drawable_dimensions(SDL_Window* window, int* w, int* h)
{
SDL_GL_GetDrawableSize(window, w, h);
}
static_inline void resize_window(GPU_Target* target, int w, int h)
{
SDL_SetWindowSize(SDL_GetWindowFromID(target->context->windowID), w, h);
}
static_inline GPU_bool get_fullscreen_state(SDL_Window* window)
{
return (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN);
}
static_inline GPU_bool has_colorkey(SDL_Surface* surface)
{
return (SDL_GetColorKey(surface, NULL) == 0);
}
static_inline GPU_bool is_alpha_format(SDL_PixelFormat* format)
{
return SDL_ISPIXELFORMAT_ALPHA(format->format);
}
#else
#define SDL_Window SDL_Surface
#define GET_ALPHA(sdl_color) ((sdl_color).unused)
static_inline SDL_Window* get_window(Uint32 windowID)
{
return (windowID == 1? SDL_GetVideoSurface() : NULL);
}
static_inline Uint32 get_window_id(SDL_Surface* window)
{
return (SDL_GetVideoSurface() == window? 1 : 0);
}
static_inline void get_window_dimensions(SDL_Window* window, int* w, int* h)
{
if(window == NULL)
return;
*w = window->w;
*h = window->h;
}
static_inline void get_drawable_dimensions(SDL_Window* window, int* w, int* h)
{
get_window_dimensions(window, w, h);
}
static_inline void resize_window(GPU_Target* target, int w, int h)
{
SDL_Surface* screen = SDL_GetVideoSurface();
Uint32 flags = screen->flags;
screen = SDL_SetVideoMode(w, h, 0, flags);
// NOTE: There's a bug in SDL 1.2. This is a workaround. Let's resize again:
screen = SDL_SetVideoMode(w, h, 0, flags);
}
static_inline GPU_bool get_fullscreen_state(SDL_Window* window)
{
return (window->flags & SDL_FULLSCREEN);
}
static_inline GPU_bool has_colorkey(SDL_Surface* surface)
{
return (surface->flags & SDL_SRCCOLORKEY);
}
static_inline GPU_bool is_alpha_format(SDL_PixelFormat* format)
{
return (format->BitsPerPixel == 32); // Not great, as it misses many packed formats. Might be the best we can do.
}
#endif
static_inline void get_target_window_dimensions(GPU_Target* target, int* w, int* h)
{
SDL_Window* window;
if(target == NULL || target->context == NULL)
return;
window = get_window(target->context->windowID);
get_window_dimensions(window, w, h);
}
static_inline void get_target_drawable_dimensions(GPU_Target* target, int* w, int* h)
{
SDL_Window* window;
if(target == NULL || target->context == NULL)
return;
window = get_window(target->context->windowID);
get_drawable_dimensions(window, w, h);
}
#ifndef GL_VERTEX_SHADER
#ifndef SDL_GPU_DISABLE_SHADERS
#define SDL_GPU_DISABLE_SHADERS
#endif
#endif
// Workaround for Intel HD glVertexAttrib() bug.
#ifdef SDL_GPU_USE_OPENGL
// FIXME: This should probably exist in context storage, as I expect it to be a problem across contexts.
static GPU_bool apply_Intel_attrib_workaround = GPU_FALSE;
static GPU_bool vendor_is_Intel = GPU_FALSE;
#endif
static SDL_PixelFormat* AllocFormat(GLenum glFormat);
static void FreeFormat(SDL_PixelFormat* format);
static char shader_message[256];
static GPU_bool isExtensionSupported(const char* extension_str)
{
#ifdef SDL_GPU_USE_OPENGL
return glewIsExtensionSupported(extension_str);
#else
// As suggested by Mesa3D.org
char* p = (char*)glGetString(GL_EXTENSIONS);
char* end;
unsigned long extNameLen;
if(p == NULL)
return GPU_FALSE;
extNameLen = strlen(extension_str);
end = p + strlen(p);
while(p < end)
{
unsigned long n = strcspn(p, " ");
if((extNameLen == n) && (strncmp(extension_str, p, n) == 0))
return GPU_TRUE;
p += (n + 1);
}
return GPU_FALSE;
#endif
}
static_inline void fast_upload_texture(const void* pixels, GPU_Rect update_rect, Uint32 format, int alignment, int row_length)
{
glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
#if defined(SDL_GPU_USE_OPENGL) || SDL_GPU_GLES_MAJOR_VERSION > 2
glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length);
#endif
glTexSubImage2D(GL_TEXTURE_2D, 0,
(GLint)update_rect.x, (GLint)update_rect.y, (GLsizei)update_rect.w, (GLsizei)update_rect.h,
format, GL_UNSIGNED_BYTE, pixels);
#if defined(SDL_GPU_USE_OPENGL) || SDL_GPU_GLES_MAJOR_VERSION > 2
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
#endif
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
}
static void row_upload_texture(const unsigned char* pixels, GPU_Rect update_rect, Uint32 format, int alignment, unsigned int pitch, int bytes_per_pixel)
{
unsigned int i;
unsigned int h = (unsigned int)update_rect.h;
(void)bytes_per_pixel;
glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
if(h > 0 && update_rect.w > 0.0f)
{
// Must upload row by row to account for row length
for(i = 0; i < h; ++i)
{
glTexSubImage2D(GL_TEXTURE_2D, 0,
(GLint)update_rect.x, (GLint)(update_rect.y + i), (GLsizei)update_rect.w, 1,
format, GL_UNSIGNED_BYTE, pixels);
pixels += pitch;
}
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
}
static void copy_upload_texture(const unsigned char* pixels, GPU_Rect update_rect, Uint32 format, int alignment, unsigned int pitch, int bytes_per_pixel)
{
unsigned int i;
unsigned int h = (unsigned int)update_rect.h;
unsigned int w = ((unsigned int)update_rect.w)*bytes_per_pixel;
if(h > 0 && w > 0)
{
unsigned int rem = w % alignment;
// If not already aligned, account for padding on each row
if(rem > 0)
w += alignment - rem;
unsigned char *copy = (unsigned char*)SDL_malloc(w*h);
unsigned char *dst = copy;
for(i = 0; i < h; ++i)
{
memcpy(dst, pixels, w);
pixels += pitch;
dst += w;
}
fast_upload_texture(copy, update_rect, format, alignment, (int)update_rect.w);
SDL_free(copy);
}
}
static void (*slow_upload_texture)(const unsigned char* pixels, GPU_Rect update_rect, Uint32 format, int alignment, unsigned int pitch, int bytes_per_pixel) = NULL;
static_inline void upload_texture(const void* pixels, GPU_Rect update_rect, Uint32 format, int alignment, int row_length, unsigned int pitch, int bytes_per_pixel)
{
(void)pitch;
#if defined(SDL_GPU_USE_OPENGL) || SDL_GPU_GLES_MAJOR_VERSION > 2
(void)bytes_per_pixel;
fast_upload_texture(pixels, update_rect, format, alignment, row_length);
#else
if(row_length == update_rect.w)
fast_upload_texture(pixels, update_rect, format, alignment, row_length);
else
slow_upload_texture(pixels, update_rect, format, alignment, pitch, bytes_per_pixel);
#endif
}
static_inline void upload_new_texture(void* pixels, GPU_Rect update_rect, Uint32 format, int alignment, int row_length, int bytes_per_pixel)
{
#if defined(SDL_GPU_USE_OPENGL) || SDL_GPU_GLES_MAJOR_VERSION > 2
(void)bytes_per_pixel;
glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length);
glTexImage2D(GL_TEXTURE_2D, 0, format, (GLsizei)update_rect.w, (GLsizei)update_rect.h, 0,
format, GL_UNSIGNED_BYTE, pixels);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
#else
glTexImage2D(GL_TEXTURE_2D, 0, format, (GLsizei)update_rect.w, (GLsizei)update_rect.h, 0,
format, GL_UNSIGNED_BYTE, NULL);
upload_texture(pixels, update_rect, format, alignment, row_length, row_length*bytes_per_pixel, bytes_per_pixel);
#endif
}
// Define intermediates for FBO functions in case we only have EXT or OES FBO support.
#if defined(SDL_GPU_ASSUME_CORE_FBO)
#define glBindFramebufferPROC glBindFramebuffer
#define glCheckFramebufferStatusPROC glCheckFramebufferStatus
#define glDeleteFramebuffersPROC glDeleteFramebuffers
#define glFramebufferTexture2DPROC glFramebufferTexture2D
#define glGenFramebuffersPROC glGenFramebuffers
#define glGenerateMipmapPROC glGenerateMipmap
#else
static void GLAPIENTRY glBindFramebufferNOOP(GLenum target, GLuint framebuffer)
{
(void)target;
(void)framebuffer;
GPU_LogError("%s: Unsupported operation\n", __func__);
}
static GLenum GLAPIENTRY glCheckFramebufferStatusNOOP(GLenum target)
{
(void)target;
GPU_LogError("%s: Unsupported operation\n", __func__);
return 0;
}
static void GLAPIENTRY glDeleteFramebuffersNOOP(GLsizei n, const GLuint* framebuffers)
{
(void)n;
(void)framebuffers;
GPU_LogError("%s: Unsupported operation\n", __func__);
}
static void GLAPIENTRY glFramebufferTexture2DNOOP(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
{
(void)target;
(void)attachment;
(void)textarget;
(void)texture;
(void)level;
GPU_LogError("%s: Unsupported operation\n", __func__);
}
static void GLAPIENTRY glGenFramebuffersNOOP(GLsizei n, GLuint *ids)
{
(void)n;
(void)ids;
GPU_LogError("%s: Unsupported operation\n", __func__);
}
static void GLAPIENTRY glGenerateMipmapNOOP(GLenum target)
{
(void)target;
GPU_LogError("%s: Unsupported operation\n", __func__);
}
static void (GLAPIENTRY *glBindFramebufferPROC)(GLenum target, GLuint framebuffer) = glBindFramebufferNOOP;
static GLenum (GLAPIENTRY *glCheckFramebufferStatusPROC)(GLenum target) = glCheckFramebufferStatusNOOP;
static void (GLAPIENTRY *glDeleteFramebuffersPROC)(GLsizei n, const GLuint* framebuffers) = glDeleteFramebuffersNOOP;
static void (GLAPIENTRY *glFramebufferTexture2DPROC)(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level) = glFramebufferTexture2DNOOP;
static void (GLAPIENTRY *glGenFramebuffersPROC)(GLsizei n, GLuint *ids) = glGenFramebuffersNOOP;
static void (GLAPIENTRY *glGenerateMipmapPROC)(GLenum target) = glGenerateMipmapNOOP;
#endif
static void init_features(GPU_Renderer* renderer)
{
// Reset supported features
renderer->enabled_features = 0;
// NPOT textures
#ifdef SDL_GPU_USE_OPENGL
#if SDL_GPU_GL_MAJOR_VERSION >= 2
// Core in GL 2+
renderer->enabled_features |= GPU_FEATURE_NON_POWER_OF_TWO;
#else
if(isExtensionSupported("GL_ARB_texture_non_power_of_two"))
renderer->enabled_features |= GPU_FEATURE_NON_POWER_OF_TWO;
else
renderer->enabled_features &= ~GPU_FEATURE_NON_POWER_OF_TWO;
#endif
#elif defined(SDL_GPU_USE_GLES)
#if SDL_GPU_GLES_MAJOR_VERSION >= 3
// Core in GLES 3+
renderer->enabled_features |= GPU_FEATURE_NON_POWER_OF_TWO;
#else
if(isExtensionSupported("GL_OES_texture_npot") || isExtensionSupported("GL_IMG_texture_npot")
|| isExtensionSupported("GL_APPLE_texture_2D_limited_npot") || isExtensionSupported("GL_ARB_texture_non_power_of_two"))
renderer->enabled_features |= GPU_FEATURE_NON_POWER_OF_TWO;
else
renderer->enabled_features &= ~GPU_FEATURE_NON_POWER_OF_TWO;
#if SDL_GPU_GLES_MAJOR_VERSION >= 2
// Assume limited NPOT support for GLES 2+
renderer->enabled_features |= GPU_FEATURE_NON_POWER_OF_TWO;
#endif
#endif
#endif
// FBO support
#ifdef SDL_GPU_ASSUME_CORE_FBO
renderer->enabled_features |= GPU_FEATURE_RENDER_TARGETS;
renderer->enabled_features |= GPU_FEATURE_CORE_FRAMEBUFFER_OBJECTS;
#elif defined(SDL_GPU_USE_OPENGL)
if(isExtensionSupported("GL_ARB_framebuffer_object"))
{
renderer->enabled_features |= GPU_FEATURE_RENDER_TARGETS;
renderer->enabled_features |= GPU_FEATURE_CORE_FRAMEBUFFER_OBJECTS;
glBindFramebufferPROC = glBindFramebuffer;
glCheckFramebufferStatusPROC = glCheckFramebufferStatus;
glDeleteFramebuffersPROC = glDeleteFramebuffers;
glFramebufferTexture2DPROC = glFramebufferTexture2D;
glGenFramebuffersPROC = glGenFramebuffers;
glGenerateMipmapPROC = glGenerateMipmap;
}
else if(isExtensionSupported("GL_EXT_framebuffer_object"))
{
renderer->enabled_features |= GPU_FEATURE_RENDER_TARGETS;
glBindFramebufferPROC = glBindFramebufferEXT;
glCheckFramebufferStatusPROC = glCheckFramebufferStatusEXT;
glDeleteFramebuffersPROC = glDeleteFramebuffersEXT;
glFramebufferTexture2DPROC = glFramebufferTexture2DEXT;
glGenFramebuffersPROC = glGenFramebuffersEXT;
glGenerateMipmapPROC = glGenerateMipmapEXT;
}
else
renderer->enabled_features &= ~GPU_FEATURE_RENDER_TARGETS;
#elif defined(SDL_GPU_USE_GLES)
if(isExtensionSupported("GL_OES_framebuffer_object"))
{
renderer->enabled_features |= GPU_FEATURE_RENDER_TARGETS;
glBindFramebufferPROC = glBindFramebufferOES;
glCheckFramebufferStatusPROC = glCheckFramebufferStatusOES;
glDeleteFramebuffersPROC = glDeleteFramebuffersOES;
glFramebufferTexture2DPROC = glFramebufferTexture2DOES;
glGenFramebuffersPROC = glGenFramebuffersOES;
glGenerateMipmapPROC = glGenerateMipmapOES;
}
else
renderer->enabled_features &= ~GPU_FEATURE_RENDER_TARGETS;
#endif
// Blending
#ifdef SDL_GPU_USE_OPENGL
renderer->enabled_features |= GPU_FEATURE_BLEND_EQUATIONS;
renderer->enabled_features |= GPU_FEATURE_BLEND_FUNC_SEPARATE;
#if SDL_GPU_GL_MAJOR_VERSION >= 2
// Core in GL 2+
renderer->enabled_features |= GPU_FEATURE_BLEND_EQUATIONS_SEPARATE;
#else
if(isExtensionSupported("GL_EXT_blend_equation_separate"))
renderer->enabled_features |= GPU_FEATURE_BLEND_EQUATIONS_SEPARATE;
else
renderer->enabled_features &= ~GPU_FEATURE_BLEND_EQUATIONS_SEPARATE;
#endif
#elif defined(SDL_GPU_USE_GLES)
#if SDL_GPU_GLES_MAJOR_VERSION >= 2
// Core in GLES 2+
renderer->enabled_features |= GPU_FEATURE_BLEND_EQUATIONS;
renderer->enabled_features |= GPU_FEATURE_BLEND_FUNC_SEPARATE;
renderer->enabled_features |= GPU_FEATURE_BLEND_EQUATIONS_SEPARATE;
#else
if(isExtensionSupported("GL_OES_blend_subtract"))
renderer->enabled_features |= GPU_FEATURE_BLEND_EQUATIONS;
else
renderer->enabled_features &= ~GPU_FEATURE_BLEND_EQUATIONS;
if(isExtensionSupported("GL_OES_blend_func_separate"))
renderer->enabled_features |= GPU_FEATURE_BLEND_FUNC_SEPARATE;
else
renderer->enabled_features &= ~GPU_FEATURE_BLEND_FUNC_SEPARATE;
if(isExtensionSupported("GL_OES_blend_equation_separate"))
renderer->enabled_features |= GPU_FEATURE_BLEND_EQUATIONS_SEPARATE;
else
renderer->enabled_features &= ~GPU_FEATURE_BLEND_EQUATIONS_SEPARATE;
#endif
#endif
// Wrap modes
#ifdef SDL_GPU_USE_OPENGL
#if SDL_GPU_GL_MAJOR_VERSION >= 2
renderer->enabled_features |= GPU_FEATURE_WRAP_REPEAT_MIRRORED;
#else
if(isExtensionSupported("GL_ARB_texture_mirrored_repeat"))
renderer->enabled_features |= GPU_FEATURE_WRAP_REPEAT_MIRRORED;
else
renderer->enabled_features &= ~GPU_FEATURE_WRAP_REPEAT_MIRRORED;
#endif
#elif defined(SDL_GPU_USE_GLES)
#if SDL_GPU_GLES_MAJOR_VERSION >= 2
renderer->enabled_features |= GPU_FEATURE_WRAP_REPEAT_MIRRORED;
#else
if(isExtensionSupported("GL_OES_texture_mirrored_repeat"))
renderer->enabled_features |= GPU_FEATURE_WRAP_REPEAT_MIRRORED;
else
renderer->enabled_features &= ~GPU_FEATURE_WRAP_REPEAT_MIRRORED;
#endif
#endif
// GL texture formats
if(isExtensionSupported("GL_EXT_bgr"))
renderer->enabled_features |= GPU_FEATURE_GL_BGR;
if(isExtensionSupported("GL_EXT_bgra"))
renderer->enabled_features |= GPU_FEATURE_GL_BGRA;
if(isExtensionSupported("GL_EXT_abgr"))
renderer->enabled_features |= GPU_FEATURE_GL_ABGR;
// Disable other texture formats for GLES.
// TODO: Add better (static) checking for format support. Some GL versions do not report previously non-core features as extensions.
#ifdef SDL_GPU_USE_GLES
renderer->enabled_features &= ~GPU_FEATURE_GL_BGR;
renderer->enabled_features &= ~GPU_FEATURE_GL_BGRA;
renderer->enabled_features &= ~GPU_FEATURE_GL_ABGR;
#endif
// Shader support
#ifndef SDL_GPU_DISABLE_SHADERS
if(isExtensionSupported("GL_ARB_fragment_shader"))
renderer->enabled_features |= GPU_FEATURE_FRAGMENT_SHADER;
if(isExtensionSupported("GL_ARB_vertex_shader"))
renderer->enabled_features |= GPU_FEATURE_VERTEX_SHADER;
if(isExtensionSupported("GL_ARB_geometry_shader4"))
renderer->enabled_features |= GPU_FEATURE_GEOMETRY_SHADER;
#endif
#ifdef SDL_GPU_ASSUME_SHADERS
renderer->enabled_features |= GPU_FEATURE_BASIC_SHADERS;
#endif
}
static void extBindFramebuffer(GPU_Renderer* renderer, GLuint handle)
{
if(renderer->enabled_features & GPU_FEATURE_RENDER_TARGETS)
glBindFramebufferPROC(GL_FRAMEBUFFER, handle);
}
static_inline GPU_bool isPowerOfTwo(unsigned int x)
{
return ((x != 0) && !(x & (x - 1)));
}
static_inline unsigned int getNearestPowerOf2(unsigned int n)
{
unsigned int x = 1;
while(x < n)
{
x <<= 1;
}
return x;
}
static void bindTexture(GPU_Renderer* renderer, GPU_Image* image)
{
// Bind the texture to which subsequent calls refer
if(image != ((GPU_CONTEXT_DATA*)renderer->current_context_target->context->data)->last_image)
{
GLuint handle = ((GPU_IMAGE_DATA*)image->data)->handle;
renderer->impl->FlushBlitBuffer(renderer);
glBindTexture( GL_TEXTURE_2D, handle );
((GPU_CONTEXT_DATA*)renderer->current_context_target->context->data)->last_image = image;
}
}
static_inline void flushAndBindTexture(GPU_Renderer* renderer, GLuint handle)
{
// Bind the texture to which subsequent calls refer
renderer->impl->FlushBlitBuffer(renderer);
glBindTexture( GL_TEXTURE_2D, handle );
((GPU_CONTEXT_DATA*)renderer->current_context_target->context->data)->last_image = NULL;
}
// Only for window targets, which have their own contexts.
static void makeContextCurrent(GPU_Renderer* renderer, GPU_Target* target)
{
if (target == NULL || target->context == NULL || renderer->current_context_target == target)
return;
renderer->impl->FlushBlitBuffer(renderer);
#ifdef SDL_GPU_USE_SDL2
SDL_GL_MakeCurrent(SDL_GetWindowFromID(target->context->windowID), target->context->context);
#endif
renderer->current_context_target = target;
}
// Binds the target's framebuffer. Returns false if it can't be bound, true if it is bound or already bound.
static GPU_bool SetActiveTarget(GPU_Renderer* renderer, GPU_Target* target)
{
makeContextCurrent(renderer, target);
if (target == NULL || renderer->current_context_target == NULL)
return GPU_FALSE;
if(renderer->enabled_features & GPU_FEATURE_RENDER_TARGETS)
{
// Bind the FBO
if(target != renderer->current_context_target->context->active_target)
{
GLuint handle = ((GPU_TARGET_DATA*)target->data)->handle;
renderer->impl->FlushBlitBuffer(renderer);
extBindFramebuffer(renderer, handle);
renderer->current_context_target->context->active_target = target;
}
}
else
{
// There's only one possible render target, the default framebuffer.
// Note: Could check against the default framebuffer value (((GPU_TARGET_DATA*)target->data)->handle versus result of GL_FRAMEBUFFER_BINDING)...
renderer->current_context_target->context->active_target = target;
}
return GPU_TRUE;
}
static_inline void flushAndBindFramebuffer(GPU_Renderer* renderer, GLuint handle)
{
// Bind the FBO
renderer->impl->FlushBlitBuffer(renderer);
extBindFramebuffer(renderer, handle);
renderer->current_context_target->context->active_target = NULL;
}
static_inline void flushBlitBufferIfCurrentTexture(GPU_Renderer* renderer, GPU_Image* image)
{
if(image == ((GPU_CONTEXT_DATA*)renderer->current_context_target->context->data)->last_image)
{
renderer->impl->FlushBlitBuffer(renderer);
}
}
static_inline void flushAndClearBlitBufferIfCurrentTexture(GPU_Renderer* renderer, GPU_Image* image)
{
if(image == ((GPU_CONTEXT_DATA*)renderer->current_context_target->context->data)->last_image)
{
renderer->impl->FlushBlitBuffer(renderer);
((GPU_CONTEXT_DATA*)renderer->current_context_target->context->data)->last_image = NULL;
}
}
static_inline GPU_bool isCurrentTarget(GPU_Renderer* renderer, GPU_Target* target)
{
return (target == renderer->current_context_target->context->active_target
|| renderer->current_context_target->context->active_target == NULL);
}
static_inline void flushAndClearBlitBufferIfCurrentFramebuffer(GPU_Renderer* renderer, GPU_Target* target)
{
if(target == renderer->current_context_target->context->active_target
|| renderer->current_context_target->context->active_target == NULL)
{
renderer->impl->FlushBlitBuffer(renderer);
renderer->current_context_target->context->active_target = NULL;
}
}
static GPU_bool growBlitBuffer(GPU_CONTEXT_DATA* cdata, unsigned int minimum_vertices_needed)
{
unsigned int new_max_num_vertices;
float* new_buffer;
if(minimum_vertices_needed <= cdata->blit_buffer_max_num_vertices)
return GPU_TRUE;
if(cdata->blit_buffer_max_num_vertices == GPU_BLIT_BUFFER_ABSOLUTE_MAX_VERTICES)
return GPU_FALSE;
// Calculate new size (in vertices)
new_max_num_vertices = ((unsigned int)cdata->blit_buffer_max_num_vertices) * 2;
while(new_max_num_vertices <= minimum_vertices_needed)
new_max_num_vertices *= 2;
if(new_max_num_vertices > GPU_BLIT_BUFFER_ABSOLUTE_MAX_VERTICES)
new_max_num_vertices = GPU_BLIT_BUFFER_ABSOLUTE_MAX_VERTICES;
//GPU_LogError("Growing to %d vertices\n", new_max_num_vertices);
// Resize the blit buffer
new_buffer = (float*)SDL_malloc(new_max_num_vertices * GPU_BLIT_BUFFER_STRIDE);
memcpy(new_buffer, cdata->blit_buffer, cdata->blit_buffer_num_vertices * GPU_BLIT_BUFFER_STRIDE);
SDL_free(cdata->blit_buffer);
cdata->blit_buffer = new_buffer;
cdata->blit_buffer_max_num_vertices = (unsigned short)new_max_num_vertices;
#ifdef SDL_GPU_USE_BUFFER_PIPELINE
// Resize the VBOs
#if !defined(SDL_GPU_NO_VAO)
glBindVertexArray(cdata->blit_VAO);
#endif
glBindBuffer(GL_ARRAY_BUFFER, cdata->blit_VBO[0]);
glBufferData(GL_ARRAY_BUFFER, GPU_BLIT_BUFFER_STRIDE * cdata->blit_buffer_max_num_vertices, NULL, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, cdata->blit_VBO[1]);
glBufferData(GL_ARRAY_BUFFER, GPU_BLIT_BUFFER_STRIDE * cdata->blit_buffer_max_num_vertices, NULL, GL_STREAM_DRAW);
#if !defined(SDL_GPU_NO_VAO)
glBindVertexArray(0);
#endif
#endif
return GPU_TRUE;
}
static GPU_bool growIndexBuffer(GPU_CONTEXT_DATA* cdata, unsigned int minimum_vertices_needed)
{
unsigned int new_max_num_vertices;
unsigned short* new_indices;
if(minimum_vertices_needed <= cdata->index_buffer_max_num_vertices)
return GPU_TRUE;
if(cdata->index_buffer_max_num_vertices == GPU_INDEX_BUFFER_ABSOLUTE_MAX_VERTICES)
return GPU_FALSE;
// Calculate new size (in vertices)
new_max_num_vertices = cdata->index_buffer_max_num_vertices * 2;
while(new_max_num_vertices <= minimum_vertices_needed)
new_max_num_vertices *= 2;
if(new_max_num_vertices > GPU_INDEX_BUFFER_ABSOLUTE_MAX_VERTICES)
new_max_num_vertices = GPU_INDEX_BUFFER_ABSOLUTE_MAX_VERTICES;
//GPU_LogError("Growing to %d indices\n", new_max_num_vertices);
// Resize the index buffer
new_indices = (unsigned short*)SDL_malloc(new_max_num_vertices * sizeof(unsigned short));
memcpy(new_indices, cdata->index_buffer, cdata->index_buffer_num_vertices * sizeof(unsigned short));
SDL_free(cdata->index_buffer);
cdata->index_buffer = new_indices;
cdata->index_buffer_max_num_vertices = new_max_num_vertices;
#ifdef SDL_GPU_USE_BUFFER_PIPELINE
// Resize the IBO
#if !defined(SDL_GPU_NO_VAO)
glBindVertexArray(cdata->blit_VAO);
#endif
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cdata->blit_IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short) * cdata->index_buffer_max_num_vertices, NULL, GL_DYNAMIC_DRAW);
#if !defined(SDL_GPU_NO_VAO)
glBindVertexArray(0);
#endif
#endif
return GPU_TRUE;
}
static void setClipRect(GPU_Renderer* renderer, GPU_Target* target)
{
if(target->use_clip_rect)
{
GPU_Target* context_target = renderer->current_context_target;
glEnable(GL_SCISSOR_TEST);
if(target->context != NULL)
{
float y;
if(renderer->coordinate_mode == 0)
y = context_target->h - (target->clip_rect.y + target->clip_rect.h);
else
y = target->clip_rect.y;
float xFactor = ((float)context_target->context->drawable_w)/context_target->w;
float yFactor = ((float)context_target->context->drawable_h)/context_target->h;
glScissor((GLint)(target->clip_rect.x * xFactor), (GLint)(y * yFactor), (GLsizei)(target->clip_rect.w * xFactor), (GLsizei)(target->clip_rect.h * yFactor));
}
else
glScissor((GLint)target->clip_rect.x, (GLint)target->clip_rect.y, (GLsizei)target->clip_rect.w, (GLsizei)target->clip_rect.h);
}
}
static void unsetClipRect(GPU_Renderer* renderer, GPU_Target* target)
{
(void)renderer;
if(target->use_clip_rect)
glDisable(GL_SCISSOR_TEST);
}
static void changeDepthTest(GPU_Renderer* renderer, GPU_bool enable)
{
GPU_CONTEXT_DATA* cdata = (GPU_CONTEXT_DATA*)renderer->current_context_target->context->data;
if(cdata->last_depth_test == enable)
return;
cdata->last_depth_test = enable;
if(enable)
glEnable(GL_DEPTH_TEST);
else
glDisable(GL_DEPTH_TEST);
//glEnable(GL_ALPHA_TEST);
}
static void changeDepthWrite(GPU_Renderer* renderer, GPU_bool enable)
{
GPU_CONTEXT_DATA* cdata = (GPU_CONTEXT_DATA*)renderer->current_context_target->context->data;
if(cdata->last_depth_write == enable)
return;
cdata->last_depth_write = enable;
glDepthMask(enable);
}
static void changeDepthFunction(GPU_Renderer* renderer, GPU_ComparisonEnum compare_operation)
{
GPU_CONTEXT_DATA* cdata = (GPU_CONTEXT_DATA*)renderer->current_context_target->context->data;
if(cdata->last_depth_function == compare_operation)
return;
cdata->last_depth_function = compare_operation;
glDepthFunc(compare_operation);
}
static void prepareToRenderToTarget(GPU_Renderer* renderer, GPU_Target* target)
{
// Set up the camera
renderer->impl->SetCamera(renderer, target, &target->camera);
changeDepthTest(renderer, target->use_depth_test);
changeDepthWrite(renderer, target->use_depth_write);
changeDepthFunction(renderer, target->depth_function);
}
static void changeColor(GPU_Renderer* renderer, SDL_Color color)
{
#ifdef SDL_GPU_USE_BUFFER_PIPELINE
(void)renderer;
(void)color;
return;
#else
GPU_CONTEXT_DATA* cdata = (GPU_CONTEXT_DATA*)renderer->current_context_target->context->data;
if(cdata->last_color.r != color.r
|| cdata->last_color.g != color.g
|| cdata->last_color.b != color.b
|| GET_ALPHA(cdata->last_color) != GET_ALPHA(color))
{
renderer->impl->FlushBlitBuffer(renderer);
cdata->last_color = color;
glColor4f(color.r/255.01f, color.g/255.01f, color.b/255.01f, GET_ALPHA(color)/255.01f);
}
#endif
}
static void changeBlending(GPU_Renderer* renderer, GPU_bool enable)
{
GPU_CONTEXT_DATA* cdata = (GPU_CONTEXT_DATA*)renderer->current_context_target->context->data;
if(cdata->last_use_blending == enable)
return;
renderer->impl->FlushBlitBuffer(renderer);
if(enable)
glEnable(GL_BLEND);
else
glDisable(GL_BLEND);
cdata->last_use_blending = enable;
}
static void forceChangeBlendMode(GPU_Renderer* renderer, GPU_BlendMode mode)
{
GPU_CONTEXT_DATA* cdata = (GPU_CONTEXT_DATA*)renderer->current_context_target->context->data;
renderer->impl->FlushBlitBuffer(renderer);
cdata->last_blend_mode = mode;
if(mode.source_color == mode.source_alpha && mode.dest_color == mode.dest_alpha)
{
glBlendFunc(mode.source_color, mode.dest_color);
}
else if(renderer->enabled_features & GPU_FEATURE_BLEND_FUNC_SEPARATE)
{
glBlendFuncSeparate(mode.source_color, mode.dest_color, mode.source_alpha, mode.dest_alpha);
}
else
{
GPU_PushErrorCode("(SDL_gpu internal)", GPU_ERROR_BACKEND_ERROR, "Could not set blend function because GPU_FEATURE_BLEND_FUNC_SEPARATE is not supported.");
}
if(renderer->enabled_features & GPU_FEATURE_BLEND_EQUATIONS)
{
if(mode.color_equation == mode.alpha_equation)
glBlendEquation(mode.color_equation);
else if(renderer->enabled_features & GPU_FEATURE_BLEND_EQUATIONS_SEPARATE)
glBlendEquationSeparate(mode.color_equation, mode.alpha_equation);
else
{
GPU_PushErrorCode("(SDL_gpu internal)", GPU_ERROR_BACKEND_ERROR, "Could not set blend equation because GPU_FEATURE_BLEND_EQUATIONS_SEPARATE is not supported.");
}
}
else
{
GPU_PushErrorCode("(SDL_gpu internal)", GPU_ERROR_BACKEND_ERROR, "Could not set blend equation because GPU_FEATURE_BLEND_EQUATIONS is not supported.");
}
}
static void changeBlendMode(GPU_Renderer* renderer, GPU_BlendMode mode)
{
GPU_CONTEXT_DATA* cdata = (GPU_CONTEXT_DATA*)renderer->current_context_target->context->data;
if(cdata->last_blend_mode.source_color == mode.source_color
&& cdata->last_blend_mode.dest_color == mode.dest_color
&& cdata->last_blend_mode.source_alpha == mode.source_alpha
&& cdata->last_blend_mode.dest_alpha == mode.dest_alpha
&& cdata->last_blend_mode.color_equation == mode.color_equation
&& cdata->last_blend_mode.alpha_equation == mode.alpha_equation)
return;
forceChangeBlendMode(renderer, mode);
}