forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgl.c
2775 lines (2331 loc) · 77.8 KB
/
gl.c
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
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2013 - Hans-Kristian Arntzen
* Copyright (C) 2011-2013 - Daniel De Matteis
* Copyright (C) 2012-2013 - Michael Lelli
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef _MSC_VER
#pragma comment(lib, "opengl32")
#endif
#include "../driver.h"
#include "../performance.h"
#include "scaler/scaler.h"
#include "image.h"
#include "../file.h"
#include <stdint.h>
#include "../libretro.h"
#include <stdio.h>
#include <string.h>
#include "../general.h"
#include <math.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gl_common.h"
#include "gfx_common.h"
#include "gfx_context.h"
#include "../compat/strl.h"
#ifdef HAVE_CG
#include "shader_cg.h"
#endif
#ifdef HAVE_GLSL
#include "shader_glsl.h"
#endif
#include "shader_common.h"
#ifdef ANDROID
#include "../frontend/frontend_android.h"
#endif
// Used for the last pass when rendering to the back buffer.
static const GLfloat vertexes_flipped[] = {
0, 1,
1, 1,
0, 0,
1, 0
};
// Used when rendering to an FBO.
// Texture coords have to be aligned with vertex coordinates.
static const GLfloat vertexes[] = {
0, 0,
1, 0,
0, 1,
1, 1
};
static const GLfloat tex_coords[] = {
0, 0,
1, 0,
0, 1,
1, 1
};
static const GLfloat white_color[] = {
1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1,
};
static inline bool gl_query_extension(gl_t *gl, const char *ext)
{
bool ret = false;
if (gl->core_context)
{
#ifdef GL_NUM_EXTENSIONS
GLint i, exts;
exts = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &exts);
for (i = 0; i < exts; i++)
{
const char *str = (const char*)glGetStringi(GL_EXTENSIONS, i);
if (str && strstr(str, ext))
{
ret = true;
break;
}
}
#endif
}
else
{
const char *str = (const char*)glGetString(GL_EXTENSIONS);
ret = str && strstr(str, ext);
}
RARCH_LOG("Querying GL extension: %s => %s\n",
ext, ret ? "exists" : "doesn't exist");
return ret;
}
#ifdef HAVE_OVERLAY
static void gl_render_overlay(void *data);
static void gl_overlay_vertex_geom(void *data,
unsigned image,
float x, float y, float w, float h);
static void gl_overlay_tex_geom(void *data,
unsigned image,
float x, float y, float w, float h);
#endif
static inline void set_texture_coords(GLfloat *coords, GLfloat xamt, GLfloat yamt)
{
coords[2] = xamt;
coords[6] = xamt;
coords[5] = yamt;
coords[7] = yamt;
}
#if defined(HAVE_EGL) && defined(HAVE_OPENGLES2)
static bool check_eglimage_proc(void)
{
return glEGLImageTargetTexture2DOES != NULL;
}
#endif
#ifdef HAVE_GL_SYNC
static bool check_sync_proc(gl_t *gl)
{
if (!gl_query_extension(gl, "ARB_sync"))
return false;
return glFenceSync && glDeleteSync && glClientWaitSync;
}
#endif
#ifndef HAVE_OPENGLES
static bool init_vao(gl_t *gl)
{
if (!gl->core_context && !gl_query_extension(gl, "ARB_vertex_array_object"))
return false;
bool present = glGenVertexArrays && glBindVertexArray && glDeleteVertexArrays;
if (!present)
return false;
glGenVertexArrays(1, &gl->vao);
return true;
}
#endif
#ifdef HAVE_FBO
#if defined(HAVE_PSGL)
#define glGenFramebuffers glGenFramebuffersOES
#define glBindFramebuffer glBindFramebufferOES
#define glFramebufferTexture2D glFramebufferTexture2DOES
#define glCheckFramebufferStatus glCheckFramebufferStatusOES
#define glDeleteFramebuffers glDeleteFramebuffersOES
#define glGenRenderbuffers glGenRenderbuffersOES
#define glBindRenderbuffer glBindRenderbufferOES
#define glFramebufferRenderbuffer glFramebufferRenderbufferOES
#define glRenderbufferStorage glRenderbufferStorageOES
#define glDeleteRenderbuffers glDeleteRenderbuffersOES
#define GL_FRAMEBUFFER GL_FRAMEBUFFER_OES
#define GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_EXT
#define GL_FRAMEBUFFER_COMPLETE GL_FRAMEBUFFER_COMPLETE_OES
#define check_fbo_proc(gl) (true)
#elif !defined(HAVE_OPENGLES2)
static bool check_fbo_proc(gl_t *gl)
{
if (!gl->core_context && !gl_query_extension(gl, "ARB_framebuffer_object"))
return false;
return glGenFramebuffers && glBindFramebuffer && glFramebufferTexture2D &&
glCheckFramebufferStatus && glDeleteFramebuffers &&
glGenRenderbuffers && glBindRenderbuffer &&
glFramebufferRenderbuffer && glRenderbufferStorage &&
glDeleteRenderbuffers;
}
#else
#define check_fbo_proc(gl) (true)
#endif
#if defined(__APPLE__) || defined(HAVE_PSGL)
#define GL_RGBA32F GL_RGBA32F_ARB
#endif
#endif
////////////////// Shaders
static bool gl_shader_init(void *data)
{
gl_t *gl = (gl_t*)data;
const gl_shader_backend_t *backend = NULL;
const char *shader_path = (g_settings.video.shader_enable && *g_settings.video.shader_path) ?
g_settings.video.shader_path : NULL;
enum rarch_shader_type type = gfx_shader_parse_type(shader_path,
gl->core_context ? RARCH_SHADER_GLSL : DEFAULT_SHADER_TYPE);
if (type == RARCH_SHADER_NONE)
{
RARCH_LOG("[GL]: Not loading any shader.\n");
return true;
}
switch (type)
{
#ifdef HAVE_CG
case RARCH_SHADER_CG:
RARCH_LOG("[GL]: Using Cg shader backend.\n");
backend = &gl_cg_backend;
break;
#endif
#ifdef HAVE_GLSL
case RARCH_SHADER_GLSL:
RARCH_LOG("[GL]: Using GLSL shader backend.\n");
backend = &gl_glsl_backend;
break;
#endif
default:
break;
}
if (!backend)
{
RARCH_ERR("[GL]: Didn't find valid shader backend. Continuing without shaders.\n");
return true;
}
#ifdef HAVE_GLSL
if (gl->core_context && RARCH_SHADER_CG)
{
RARCH_ERR("[GL]: Cg cannot be used with core GL context. Falling back to GLSL.\n");
backend = &gl_glsl_backend;
shader_path = NULL;
}
#endif
gl->shader = backend;
bool ret = gl->shader->init(shader_path);
if (!ret)
{
RARCH_ERR("[GL]: Failed to init shader, falling back to stock.\n");
ret = gl->shader->init(NULL);
}
return ret;
}
static inline void gl_shader_deinit(void *data)
{
gl_t *gl = (gl_t*)data;
if (gl->shader)
gl->shader->deinit();
gl->shader = NULL;
}
#ifndef NO_GL_FF_VERTEX
static void gl_set_coords(const struct gl_coords *coords)
{
glClientActiveTexture(GL_TEXTURE1);
glTexCoordPointer(2, GL_FLOAT, 0, coords->lut_tex_coord);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glClientActiveTexture(GL_TEXTURE0);
glVertexPointer(2, GL_FLOAT, 0, coords->vertex);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_FLOAT, 0, coords->color);
glEnableClientState(GL_COLOR_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, coords->tex_coord);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
static void gl_disable_client_arrays(gl_t *gl)
{
if (gl->core_context)
return;
glClientActiveTexture(GL_TEXTURE1);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glClientActiveTexture(GL_TEXTURE0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
#endif
#ifndef NO_GL_FF_MATRIX
static void gl_set_mvp(const void *data)
{
const math_matrix *mat = (const math_matrix*)data;
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(mat->data);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
#endif
void gl_shader_set_coords(void *data, const struct gl_coords *coords, const math_matrix *mat)
{
gl_t *gl = (gl_t*)data;
bool ret_coords = false;
bool ret_mvp = false;
(void)ret_coords;
(void)ret_mvp;
if (gl->shader)
ret_coords = gl->shader->set_coords(coords);
if (gl->shader)
ret_mvp = gl->shader->set_mvp(mat);
// Fall back to FF-style if needed and possible.
#ifndef NO_GL_FF_VERTEX
if (!ret_coords)
gl_set_coords(coords);
#endif
#ifndef NO_GL_FF_MATRIX
if (!ret_mvp)
gl_set_mvp(mat);
#endif
}
#define gl_shader_num(gl) ((gl->shader) ? gl->shader->num_shaders() : 0)
#define gl_shader_filter_type(gl, index, smooth) ((gl->shader) ? gl->shader->filter_type(index, smooth) : false)
#define gl_shader_wrap_type(gl, index) ((gl->shader) ? gl->shader->wrap_type(index) : RARCH_WRAP_BORDER)
#ifdef IOS
// There is no default frame buffer on IOS.
void apple_bind_game_view_fbo(void);
#define gl_bind_backbuffer() apple_bind_game_view_fbo()
#else
#define gl_bind_backbuffer() glBindFramebuffer(GL_FRAMEBUFFER, 0)
#endif
#ifdef HAVE_FBO
static void gl_shader_scale(void *data, unsigned index, struct gfx_fbo_scale *scale)
{
gl_t *gl = (gl_t*)data;
scale->valid = false;
if (gl->shader)
gl->shader->shader_scale(index, scale);
}
static void gl_compute_fbo_geometry(void *data, unsigned width, unsigned height,
unsigned vp_width, unsigned vp_height)
{
unsigned i, last_width, last_height, last_max_width, last_max_height;
gl_t *gl = (gl_t*)data;
last_width = width;
last_height = height;
last_max_width = gl->tex_w;
last_max_height = gl->tex_h;
GLint max_size = 0;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_size);
bool size_modified = false;
// Calculate viewports for FBOs.
for (i = 0; i < gl->fbo_pass; i++)
{
switch (gl->fbo_scale[i].type_x)
{
case RARCH_SCALE_INPUT:
gl->fbo_rect[i].img_width = last_width * gl->fbo_scale[i].scale_x;
gl->fbo_rect[i].max_img_width = last_max_width * gl->fbo_scale[i].scale_x;
break;
case RARCH_SCALE_ABSOLUTE:
gl->fbo_rect[i].img_width = gl->fbo_rect[i].max_img_width = gl->fbo_scale[i].abs_x;
break;
case RARCH_SCALE_VIEWPORT:
gl->fbo_rect[i].img_width = gl->fbo_rect[i].max_img_width = gl->fbo_scale[i].scale_x * vp_width;
break;
default:
break;
}
switch (gl->fbo_scale[i].type_y)
{
case RARCH_SCALE_INPUT:
gl->fbo_rect[i].img_height = last_height * gl->fbo_scale[i].scale_y;
gl->fbo_rect[i].max_img_height = last_max_height * gl->fbo_scale[i].scale_y;
break;
case RARCH_SCALE_ABSOLUTE:
gl->fbo_rect[i].img_height = gl->fbo_rect[i].max_img_height = gl->fbo_scale[i].abs_y;
break;
case RARCH_SCALE_VIEWPORT:
gl->fbo_rect[i].img_height = gl->fbo_rect[i].max_img_height = gl->fbo_scale[i].scale_y * vp_height;
break;
default:
break;
}
if (gl->fbo_rect[i].img_width > (unsigned)max_size)
{
size_modified = true;
gl->fbo_rect[i].img_width = max_size;
}
if (gl->fbo_rect[i].img_height > (unsigned)max_size)
{
size_modified = true;
gl->fbo_rect[i].img_height = max_size;
}
if (gl->fbo_rect[i].max_img_width > (unsigned)max_size)
{
size_modified = true;
gl->fbo_rect[i].max_img_width = max_size;
}
if (gl->fbo_rect[i].max_img_height > (unsigned)max_size)
{
size_modified = true;
gl->fbo_rect[i].max_img_height = max_size;
}
if (size_modified)
RARCH_WARN("FBO textures exceeded maximum size of GPU (%ux%u). Resizing to fit.\n", max_size, max_size);
last_width = gl->fbo_rect[i].img_width;
last_height = gl->fbo_rect[i].img_height;
last_max_width = gl->fbo_rect[i].max_img_width;
last_max_height = gl->fbo_rect[i].max_img_height;
}
}
#ifdef __CELLOS_LV2__
// TODO: Add float FBO support to PSGL
#define FP_FBO_ENABLE_DEFAULT (false)
#else
#define FP_FBO_ENABLE_DEFAULT (gl->fbo_scale[i].valid && gl->fbo_scale[i].fp_fbo)
#endif
static void gl_create_fbo_textures(void *data)
{
int i;
gl_t *gl = (gl_t*)data;
glGenTextures(gl->fbo_pass, gl->fbo_texture);
GLuint base_filt = g_settings.video.smooth ? GL_LINEAR : GL_NEAREST;
for (i = 0; i < gl->fbo_pass; i++)
{
glBindTexture(GL_TEXTURE_2D, gl->fbo_texture[i]);
GLuint filter_type = base_filt;
bool smooth = false;
if (gl_shader_filter_type(gl, i + 2, &smooth))
filter_type = smooth ? GL_LINEAR : GL_NEAREST;
enum gfx_wrap_type wrap = gl_shader_wrap_type(gl, i + 2);
GLenum wrap_enum = gl_wrap_type_to_enum(wrap);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter_type);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter_type);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_enum);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_enum);
bool fp_fbo = FP_FBO_ENABLE_DEFAULT;
if (fp_fbo)
{
// GLES and GL are inconsistent in which arguments to pass.
#ifdef HAVE_OPENGLES2
bool has_fp_fbo = gl_query_extension(gl, "OES_texture_float_linear");
if (!has_fp_fbo)
RARCH_ERR("OES_texture_float_linear extension not found.\n");
RARCH_LOG("FBO pass #%d is floating-point.\n", i);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
gl->fbo_rect[i].width, gl->fbo_rect[i].height,
0, GL_RGBA, GL_FLOAT, NULL);
#else
bool has_fp_fbo = gl_query_extension(gl, "ARB_texture_float");
if (!has_fp_fbo)
RARCH_ERR("ARB_texture_float extension was not found.\n");
RARCH_LOG("FBO pass #%d is floating-point.\n", i);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F,
gl->fbo_rect[i].width, gl->fbo_rect[i].height,
0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
#endif
}
else
{
#ifdef HAVE_OPENGLES2
glTexImage2D(GL_TEXTURE_2D,
0, GL_RGBA,
gl->fbo_rect[i].width, gl->fbo_rect[i].height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
#else
// Avoid potential performance reductions on particular platforms.
glTexImage2D(GL_TEXTURE_2D,
0, RARCH_GL_INTERNAL_FORMAT32,
gl->fbo_rect[i].width, gl->fbo_rect[i].height, 0,
RARCH_GL_TEXTURE_TYPE32, RARCH_GL_FORMAT32, NULL);
#endif
}
}
glBindTexture(GL_TEXTURE_2D, 0);
}
static bool gl_create_fbo_targets(void *data)
{
int i;
gl_t *gl = (gl_t*)data;
glBindTexture(GL_TEXTURE_2D, 0);
glGenFramebuffers(gl->fbo_pass, gl->fbo);
for (i = 0; i < gl->fbo_pass; i++)
{
glBindFramebuffer(GL_FRAMEBUFFER, gl->fbo[i]);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gl->fbo_texture[i], 0);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
goto error;
}
return true;
error:
glDeleteFramebuffers(gl->fbo_pass, gl->fbo);
RARCH_ERR("Failed to set up frame buffer objects. Multi-pass shading will not work.\n");
return false;
}
void gl_deinit_fbo(void *data)
{
gl_t *gl = (gl_t*)data;
if (gl->fbo_inited)
{
glDeleteTextures(gl->fbo_pass, gl->fbo_texture);
glDeleteFramebuffers(gl->fbo_pass, gl->fbo);
memset(gl->fbo_texture, 0, sizeof(gl->fbo_texture));
memset(gl->fbo, 0, sizeof(gl->fbo));
gl->fbo_inited = false;
gl->fbo_pass = 0;
}
}
void gl_init_fbo(void *data, unsigned width, unsigned height)
{
int i;
gl_t *gl = (gl_t*)data;
if (gl_shader_num(gl) == 0)
return;
struct gfx_fbo_scale scale, scale_last;
gl_shader_scale(gl, 1, &scale);
gl_shader_scale(gl, gl_shader_num(gl), &scale_last);
/* we always want FBO to be at least initialized on startup for consoles */
if (gl_shader_num(gl) == 1 && !scale.valid)
return;
if (!check_fbo_proc(gl))
{
RARCH_ERR("Failed to locate FBO functions. Won't be able to use render-to-texture.\n");
return;
}
gl->fbo_pass = gl_shader_num(gl) - 1;
if (scale_last.valid)
gl->fbo_pass++;
if (!scale.valid)
{
scale.scale_x = 1.0f;
scale.scale_y = 1.0f;
scale.type_x = scale.type_y = RARCH_SCALE_INPUT;
scale.valid = true;
}
gl->fbo_scale[0] = scale;
for (i = 1; i < gl->fbo_pass; i++)
{
gl_shader_scale(gl, i + 1, &gl->fbo_scale[i]);
if (!gl->fbo_scale[i].valid)
{
gl->fbo_scale[i].scale_x = gl->fbo_scale[i].scale_y = 1.0f;
gl->fbo_scale[i].type_x = gl->fbo_scale[i].type_y = RARCH_SCALE_INPUT;
gl->fbo_scale[i].valid = true;
}
}
gl_compute_fbo_geometry(gl, width, height, gl->win_width, gl->win_height);
for (i = 0; i < gl->fbo_pass; i++)
{
gl->fbo_rect[i].width = next_pow2(gl->fbo_rect[i].img_width);
gl->fbo_rect[i].height = next_pow2(gl->fbo_rect[i].img_height);
RARCH_LOG("Creating FBO %d @ %ux%u\n", i, gl->fbo_rect[i].width, gl->fbo_rect[i].height);
}
gl_create_fbo_textures(gl);
if (!gl_create_fbo_targets(gl))
{
glDeleteTextures(gl->fbo_pass, gl->fbo_texture);
RARCH_ERR("Failed to create FBO targets. Will continue without FBO.\n");
return;
}
gl->fbo_inited = true;
}
#ifndef HAVE_RGL
static void gl_deinit_hw_render(gl_t *gl)
{
if (gl->hw_render_fbo_init)
glDeleteFramebuffers(gl->textures, gl->hw_render_fbo);
if (gl->hw_render_depth_init)
glDeleteRenderbuffers(gl->textures, gl->hw_render_depth);
gl->hw_render_fbo_init = false;
}
static bool gl_init_hw_render(gl_t *gl, unsigned width, unsigned height)
{
unsigned i;
RARCH_LOG("[GL]: Initializing HW render (%u x %u).\n", width, height);
GLint max_fbo_size = 0;
GLint max_renderbuffer_size = 0;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_fbo_size);
glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, &max_renderbuffer_size);
RARCH_LOG("[GL]: Max texture size: %d px, renderbuffer size: %u px.\n", max_fbo_size, max_renderbuffer_size);
if (!check_fbo_proc(gl))
return false;
glBindTexture(GL_TEXTURE_2D, 0);
glGenFramebuffers(gl->textures, gl->hw_render_fbo);
bool depth = g_extern.system.hw_render_callback.depth;
bool stencil = g_extern.system.hw_render_callback.stencil;
#ifdef HAVE_OPENGLES2
if (stencil && !gl_query_extension(gl, "OES_packed_depth_stencil"))
return false;
#endif
if (depth)
{
glGenRenderbuffers(gl->textures, gl->hw_render_depth);
gl->hw_render_depth_init = true;
}
for (i = 0; i < gl->textures; i++)
{
glBindFramebuffer(GL_FRAMEBUFFER, gl->hw_render_fbo[i]);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gl->texture[i], 0);
if (depth)
{
if (stencil)
{
#ifdef HAVE_OPENGLES2
// GLES2 is a bit weird, as always. :P
glBindRenderbuffer(GL_RENDERBUFFER, gl->hw_render_depth[i]);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES,
width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
// There's no GL_DEPTH_STENCIL_ATTACHMENT like in desktop GL.
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
GL_RENDERBUFFER, gl->hw_render_depth[i]);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
GL_RENDERBUFFER, gl->hw_render_depth[i]);
#else
// We use ARB FBO extensions, no need to check.
glBindRenderbuffer(GL_RENDERBUFFER, gl->hw_render_depth[i]);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8,
width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
GL_RENDERBUFFER, gl->hw_render_depth[i]);
#endif
}
else
{
glBindRenderbuffer(GL_RENDERBUFFER, gl->hw_render_depth[i]);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16,
width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
GL_RENDERBUFFER, gl->hw_render_depth[i]);
}
}
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
{
RARCH_ERR("[GL]: Failed to create HW render FBO #%u, error: 0x%u.\n", i, (unsigned)status);
return false;
}
}
gl_bind_backbuffer();
gl->hw_render_fbo_init = true;
return true;
}
#endif
#endif
void gl_set_projection(void *data, struct gl_ortho *ortho, bool allow_rotate)
{
gl_t *gl = (gl_t*)data;
// Calculate projection.
matrix_ortho(&gl->mvp_no_rot, ortho->left, ortho->right,
ortho->bottom, ortho->top, ortho->znear, ortho->zfar);
if (allow_rotate)
{
math_matrix rot;
matrix_rotate_z(&rot, M_PI * gl->rotation / 180.0f);
matrix_multiply(&gl->mvp, &rot, &gl->mvp_no_rot);
}
else
gl->mvp = gl->mvp_no_rot;
}
void gl_set_viewport(void *data, unsigned width, unsigned height, bool force_full, bool allow_rotate)
{
gl_t *gl = (gl_t*)data;
int x = 0, y = 0;
struct gl_ortho ortho = {0, 1, 0, 1, -1, 1};
float device_aspect = 0.0f;
if (gl->ctx_driver->translate_aspect)
device_aspect = context_translate_aspect_func(width, height);
else
device_aspect = (float)width / height;
if (g_settings.video.scale_integer && !force_full)
{
gfx_scale_integer(&gl->vp, width, height, g_extern.system.aspect_ratio, gl->keep_aspect);
width = gl->vp.width;
height = gl->vp.height;
}
else if (gl->keep_aspect && !force_full)
{
float desired_aspect = g_extern.system.aspect_ratio;
float delta;
#if defined(HAVE_RGUI) || defined(HAVE_RMENU)
if (g_settings.video.aspect_ratio_idx == ASPECT_RATIO_CUSTOM)
{
const struct rarch_viewport *custom =
&g_extern.console.screen.viewports.custom_vp;
// GL has bottom-left origin viewport.
x = custom->x;
y = gl->win_height - custom->y - custom->height;
width = custom->width;
height = custom->height;
}
else
#endif
{
if (fabsf(device_aspect - desired_aspect) < 0.0001f)
{
// If the aspect ratios of screen and desired aspect ratio are sufficiently equal (floating point stuff),
// assume they are actually equal.
}
else if (device_aspect > desired_aspect)
{
delta = (desired_aspect / device_aspect - 1.0f) / 2.0f + 0.5f;
x = (int)roundf(width * (0.5f - delta));
width = (unsigned)roundf(2.0f * width * delta);
}
else
{
delta = (device_aspect / desired_aspect - 1.0f) / 2.0f + 0.5f;
y = (int)roundf(height * (0.5f - delta));
height = (unsigned)roundf(2.0f * height * delta);
}
}
gl->vp.x = x;
gl->vp.y = y;
gl->vp.width = width;
gl->vp.height = height;
}
else
{
gl->vp.x = gl->vp.y = 0;
gl->vp.width = width;
gl->vp.height = height;
}
#if defined(RARCH_MOBILE)
// In portrait mode, we want viewport to gravitate to top of screen.
if (device_aspect < 1.0f)
gl->vp.y *= 2;
#endif
glViewport(gl->vp.x, gl->vp.y, gl->vp.width, gl->vp.height);
gl_set_projection(gl, &ortho, allow_rotate);
// Set last backbuffer viewport.
if (!force_full)
{
gl->vp_out_width = width;
gl->vp_out_height = height;
}
//RARCH_LOG("Setting viewport @ %ux%u\n", width, height);
}
static void gl_set_rotation(void *data, unsigned rotation)
{
gl_t *gl = (gl_t*)data;
struct gl_ortho ortho = {0, 1, 0, 1, -1, 1};
gl->rotation = 90 * rotation;
gl_set_projection(gl, &ortho, true);
}
#ifdef HAVE_FBO
static inline void gl_start_frame_fbo(void *data)
{
gl_t *gl = (gl_t*)data;
glBindTexture(GL_TEXTURE_2D, gl->texture[gl->tex_index]);
glBindFramebuffer(GL_FRAMEBUFFER, gl->fbo[0]);
gl_set_viewport(gl, gl->fbo_rect[0].img_width, gl->fbo_rect[0].img_height, true, false);
// Need to preserve the "flipped" state when in FBO as well to have
// consistent texture coordinates.
// We will "flip" it in place on last pass.
gl->coords.vertex = vertexes;
}
static void gl_check_fbo_dimensions(void *data)
{
int i;
gl_t *gl = (gl_t*)data;
// Check if we have to recreate our FBO textures.
for (i = 0; i < gl->fbo_pass; i++)
{
// Check proactively since we might suddently get sizes of tex_w width or tex_h height.
if (gl->fbo_rect[i].max_img_width > gl->fbo_rect[i].width ||
gl->fbo_rect[i].max_img_height > gl->fbo_rect[i].height)
{
unsigned img_width = gl->fbo_rect[i].max_img_width;
unsigned img_height = gl->fbo_rect[i].max_img_height;
unsigned max = img_width > img_height ? img_width : img_height;
unsigned pow2_size = next_pow2(max);
gl->fbo_rect[i].width = gl->fbo_rect[i].height = pow2_size;
glBindFramebuffer(GL_FRAMEBUFFER, gl->fbo[i]);
glBindTexture(GL_TEXTURE_2D, gl->fbo_texture[i]);
glTexImage2D(GL_TEXTURE_2D,
0, RARCH_GL_INTERNAL_FORMAT32, gl->fbo_rect[i].width, gl->fbo_rect[i].height,
0, RARCH_GL_TEXTURE_TYPE32,
RARCH_GL_FORMAT32, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gl->fbo_texture[i], 0);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
RARCH_WARN("Failed to reinit FBO texture.\n");
RARCH_LOG("Recreating FBO texture #%d: %ux%u\n", i, gl->fbo_rect[i].width, gl->fbo_rect[i].height);
}
}
}
static void gl_frame_fbo(void *data, const struct gl_tex_info *tex_info)
{
size_t i;
gl_t *gl = (gl_t*)data;
GLfloat fbo_tex_coords[8] = {0.0f};
// Render the rest of our passes.
gl->coords.tex_coord = fbo_tex_coords;
// It's kinda handy ... :)
const struct gl_fbo_rect *prev_rect;
const struct gl_fbo_rect *rect;
struct gl_tex_info *fbo_info;
struct gl_tex_info fbo_tex_info[MAX_SHADERS];
unsigned fbo_tex_info_cnt = 0;
// Calculate viewports, texture coordinates etc, and render all passes from FBOs, to another FBO.
for (i = 1; i < gl->fbo_pass; i++)
{
prev_rect = &gl->fbo_rect[i - 1];
rect = &gl->fbo_rect[i];
fbo_info = &fbo_tex_info[i - 1];
GLfloat xamt = (GLfloat)prev_rect->img_width / prev_rect->width;
GLfloat yamt = (GLfloat)prev_rect->img_height / prev_rect->height;
set_texture_coords(fbo_tex_coords, xamt, yamt);
fbo_info->tex = gl->fbo_texture[i - 1];
fbo_info->input_size[0] = prev_rect->img_width;
fbo_info->input_size[1] = prev_rect->img_height;
fbo_info->tex_size[0] = prev_rect->width;
fbo_info->tex_size[1] = prev_rect->height;
memcpy(fbo_info->coord, fbo_tex_coords, sizeof(fbo_tex_coords));
glBindFramebuffer(GL_FRAMEBUFFER, gl->fbo[i]);
if (gl->shader)
gl->shader->use(i + 1);
glBindTexture(GL_TEXTURE_2D, gl->fbo_texture[i - 1]);
glClear(GL_COLOR_BUFFER_BIT);
// Render to FBO with certain size.
gl_set_viewport(gl, rect->img_width, rect->img_height, true, false);
if (gl->shader)
gl->shader->set_params(prev_rect->img_width, prev_rect->img_height,
prev_rect->width, prev_rect->height,
gl->vp.width, gl->vp.height, g_extern.frame_count,
tex_info, gl->prev_info, fbo_tex_info, fbo_tex_info_cnt);
gl_shader_set_coords(gl, &gl->coords, &gl->mvp);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
fbo_tex_info_cnt++;
}
// Render our last FBO texture directly to screen.
prev_rect = &gl->fbo_rect[gl->fbo_pass - 1];
GLfloat xamt = (GLfloat)prev_rect->img_width / prev_rect->width;
GLfloat yamt = (GLfloat)prev_rect->img_height / prev_rect->height;
set_texture_coords(fbo_tex_coords, xamt, yamt);
// Render our FBO texture to back buffer.
gl_bind_backbuffer();
if (gl->shader)
gl->shader->use(gl->fbo_pass + 1);
glBindTexture(GL_TEXTURE_2D, gl->fbo_texture[gl->fbo_pass - 1]);
glClear(GL_COLOR_BUFFER_BIT);
gl_set_viewport(gl, gl->win_width, gl->win_height, false, true);
if (gl->shader)
gl->shader->set_params(prev_rect->img_width, prev_rect->img_height,
prev_rect->width, prev_rect->height,
gl->vp.width, gl->vp.height, g_extern.frame_count,
tex_info, gl->prev_info, fbo_tex_info, fbo_tex_info_cnt);
gl->coords.vertex = gl->vertex_ptr;
gl_shader_set_coords(gl, &gl->coords, &gl->mvp);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
gl->coords.tex_coord = gl->tex_coords;
}
#endif