-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathgropengl.cpp
More file actions
1679 lines (1324 loc) · 47.7 KB
/
Copy pathgropengl.cpp
File metadata and controls
1679 lines (1324 loc) · 47.7 KB
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
#if defined(_WIN32)
#include <windows.h>
#include <windowsx.h>
#include <direct.h>
#endif
#if !defined __APPLE_CC__ && defined SCP_UNIX
#include<glad/glad_glx.h>
//Required because X defines none and always, which is used later
#undef None
#undef Always
#endif
#include "gropengl.h"
#include "ShaderProgram.h"
#include "gropenglbmpman.h"
#include "gropengldeferred.h"
#include "gropengldraw.h"
#include "gropenglopenxr.h"
#include "gropenglpostprocessing.h"
#include "gropenglquery.h"
#include "gropenglshader.h"
#include "gropenglstate.h"
#include "gropenglsync.h"
#include "gropengltexture.h"
#include "gropengltnl.h"
#include "bmpman/bmpman.h"
#include "cfile/cfile.h"
#include "cmdline/cmdline.h"
#include "ddsutils/ddsutils.h"
#include "debugconsole/console.h"
#include "graphics/2d.h"
#include "graphics/matrix.h"
#include "libs/renderdoc/renderdoc.h"
#include "lighting/lighting.h"
#include "math/floating.h"
#include "model/model.h"
#include "options/Option.h"
#include "osapi/osapi.h"
#include "osapi/osregistry.h"
#include "pngutils/pngutils.h"
#ifdef USE_OPENGL_ES
#include "es_compatibility.h"
#endif
#include <glad/glad.h>
// minimum GL / GLES version we can reliably support is 3.2
static const int MIN_REQUIRED_GL_VERSION = 32;
// minimum GLSL version we can reliably support is 110
static const int MIN_REQUIRED_GLSL_VERSION = 150;
int GL_version = 0;
int GLSL_version = 0;
bool GL_initted = 0;
//0==no fog
//1==linear
//2==fog coord EXT
//3==NV Radial
int OGL_fogmode = 0;
int Use_PBOs = 0;
static ubyte *GL_saved_screen = NULL;
static int GL_saved_screen_id = -1;
static GLuint GL_screen_pbo = 0;
float GL_alpha_threshold = 0.0f;
extern const char *Osreg_title;
extern SCP_string Window_title;
extern GLfloat GL_anisotropy;
static GLenum GL_read_format = GL_BGRA;
GLuint GL_vao = 0;
SCP_string GL_implementation_id;
SCP_vector<GLint> GL_binary_formats;
static std::unique_ptr<os::OpenGLContext> GL_context = nullptr;
static std::unique_ptr<os::GraphicsOperations> graphic_operations = nullptr;
static os::Viewport* current_viewport = nullptr;
void gr_opengl_clear()
{
float red = gr_screen.current_clear_color.red / 255.0f;
float green = gr_screen.current_clear_color.green / 255.0f;
float blue = gr_screen.current_clear_color.blue / 255.0f;
float alpha = gr_screen.current_clear_color.alpha / 255.0f;
if ( High_dynamic_range ) {
const float SRGB_GAMMA = 2.2f;
red = pow(red, SRGB_GAMMA);
green = pow(green, SRGB_GAMMA);
blue = pow(blue, SRGB_GAMMA);
}
// Make sure that all channels are enabled before doing this.
GL_state.ColorMask(true, true, true, true);
glClearColor(red, green, blue, alpha);
glClear ( GL_COLOR_BUFFER_BIT );
}
void gr_opengl_flip()
{
if (!GL_initted)
return;
if (Cmdline_window_res) {
GL_state.PopFramebufferState();
glViewport(0, 0, Cmdline_window_res->first, Cmdline_window_res->second);
opengl_shader_set_current(gr_opengl_maybe_create_shader(SDR_TYPE_GAMMA_BLIT, 0));
GL_state.Texture.Enable(0, GL_TEXTURE_2D, Back_texture);
Current_shader->program->Uniforms.setTextureUniform("tex", 0);
GL_state.SetAlphaBlendMode(gr_alpha_blend::ALPHA_BLEND_NONE);
GL_state.SetZbufferType(ZBUFFER_TYPE_NONE);
opengl_set_generic_uniform_data<graphics::generic_data::gamma_blit_data>(
[](graphics::generic_data::gamma_blit_data* data) {
data->gamma = Cmdline_no_set_gamma ? 1.f : Gr_gamma;
});
opengl_draw_full_screen_textured(0.0f, 0.0f, 1.0f, 1.0f);
}
if (Cmdline_gl_finish)
glFinish();
Assertion(GL_state.ValidForFlip(), "OpenGL state is invalid!");
current_viewport->swapBuffers();
opengl_tcache_frame();
#ifndef NDEBUG
int ic = opengl_check_for_errors();
if (ic) {
mprintf(("!!DEBUG!! OpenGL Errors this frame: %i\n", ic));
}
#endif
}
void gr_opengl_setup_frame() {
if (!GL_initted)
return;
if (Cmdline_window_res) {
GL_state.PushFramebufferState();
GL_state.BindFrameBuffer(Back_framebuffer);
glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
}
}
void gr_opengl_set_clip(int x, int y, int w, int h, int resize_mode)
{
// check for sanity of parameters
if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0;
}
int to_resize = (resize_mode != GR_RESIZE_NONE && resize_mode != GR_RESIZE_REPLACE && (gr_screen.custom_size || (gr_screen.rendering_to_texture != -1)));
int max_w = ((to_resize) ? gr_screen.max_w_unscaled : gr_screen.max_w);
int max_h = ((to_resize) ? gr_screen.max_h_unscaled : gr_screen.max_h);
if ((gr_screen.rendering_to_texture != -1) && to_resize) {
gr_unsize_screen_pos(&max_w, &max_h);
}
if (resize_mode != GR_RESIZE_REPLACE) {
if (x >= max_w) {
x = max_w - 1;
}
if (y >= max_h) {
y = max_h - 1;
}
if (x + w > max_w) {
w = max_w - x;
}
if (y + h > max_h) {
h = max_h - y;
}
if (w > max_w) {
w = max_w;
}
if (h > max_h) {
h = max_h;
}
}
gr_screen.offset_x_unscaled = x;
gr_screen.offset_y_unscaled = y;
gr_screen.clip_left_unscaled = 0;
gr_screen.clip_right_unscaled = w-1;
gr_screen.clip_top_unscaled = 0;
gr_screen.clip_bottom_unscaled = h-1;
gr_screen.clip_width_unscaled = w;
gr_screen.clip_height_unscaled = h;
if (to_resize) {
gr_resize_screen_pos(&x, &y, &w, &h, resize_mode);
} else {
gr_unsize_screen_pos( &gr_screen.offset_x_unscaled, &gr_screen.offset_y_unscaled );
gr_unsize_screen_pos( &gr_screen.clip_right_unscaled, &gr_screen.clip_bottom_unscaled );
gr_unsize_screen_pos( &gr_screen.clip_width_unscaled, &gr_screen.clip_height_unscaled );
}
gr_screen.offset_x = x;
gr_screen.offset_y = y;
gr_screen.clip_left = 0;
gr_screen.clip_right = w-1;
gr_screen.clip_top = 0;
gr_screen.clip_bottom = h-1;
gr_screen.clip_width = w;
gr_screen.clip_height = h;
gr_screen.clip_aspect = i2fl(w) / i2fl(h);
gr_screen.clip_center_x = (gr_screen.clip_left + gr_screen.clip_right) * 0.5f;
gr_screen.clip_center_y = (gr_screen.clip_top + gr_screen.clip_bottom) * 0.5f;
// just return early if we aren't actually going to need the scissor test
if ( (x == 0) && (y == 0) && (w == max_w) && (h == max_h) ) {
GL_state.ScissorTest(GL_FALSE);
return;
}
GL_state.ScissorTest(GL_TRUE);
if(GL_rendering_to_texture) {
glScissor(x, y, w, h);
} else {
glScissor(x, gr_screen.max_h-y-h, w, h);
}
}
void gr_opengl_reset_clip()
{
gr_screen.offset_x = gr_screen.offset_x_unscaled = 0;
gr_screen.offset_y = gr_screen.offset_y_unscaled = 0;
gr_screen.clip_left = gr_screen.clip_left_unscaled = 0;
gr_screen.clip_top = gr_screen.clip_top_unscaled = 0;
gr_screen.clip_right = gr_screen.clip_right_unscaled = gr_screen.max_w - 1;
gr_screen.clip_bottom = gr_screen.clip_bottom_unscaled = gr_screen.max_h - 1;
gr_screen.clip_width = gr_screen.clip_width_unscaled = gr_screen.max_w;
gr_screen.clip_height = gr_screen.clip_height_unscaled = gr_screen.max_h;
if (gr_screen.custom_size) {
gr_unsize_screen_pos( &gr_screen.clip_right_unscaled, &gr_screen.clip_bottom_unscaled );
gr_unsize_screen_pos( &gr_screen.clip_width_unscaled, &gr_screen.clip_height_unscaled );
}
gr_screen.clip_aspect = i2fl(gr_screen.clip_width) / i2fl(gr_screen.clip_height);
gr_screen.clip_center_x = (gr_screen.clip_left + gr_screen.clip_right) * 0.5f;
gr_screen.clip_center_y = (gr_screen.clip_top + gr_screen.clip_bottom) * 0.5f;
GL_state.ScissorTest(GL_FALSE);
}
void gr_opengl_print_screen(const char *filename)
{
char tmp[MAX_PATH_LEN];
GLubyte *pixels = NULL;
GLuint pbo = 0;
// save to a "screenshots" directory and tack on the filename
snprintf(tmp, MAX_PATH_LEN, "screenshots/%s.png", filename);
_mkdir(os_get_config_path("screenshots").c_str());
GL_state.PushFramebufferState();
GL_state.BindFrameBuffer(Cmdline_window_res ? Back_framebuffer : 0, GL_FRAMEBUFFER);
//Reading from the front buffer here seems to no longer work correctly; that just reads back all zeros
glReadBuffer(Cmdline_window_res ? GL_COLOR_ATTACHMENT0 : GL_FRONT);
// Clamp float values to [0,1] when reading from the GL_RGBA16F back framebuffer.
// The default GL_FIXED_ONLY only clamps fixed-point FBOs, leaving float FBO reads
// with out-of-range values (HDR > 1.0) producing undefined behavior when converted
// to integer types, which manifests as rainbow artifacts on bright areas.
glClampColor(GL_CLAMP_READ_COLOR, GL_TRUE);
// now for the data
if (Use_PBOs) {
Assert( !pbo );
glGenBuffers(1, &pbo);
if ( !pbo ) {
glClampColor(GL_CLAMP_READ_COLOR, GL_FIXED_ONLY);
return;
}
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo);
glBufferData(GL_PIXEL_PACK_BUFFER, (gr_screen.max_w * gr_screen.max_h * 4), NULL, GL_STATIC_READ);
glReadPixels(0, 0, gr_screen.max_w, gr_screen.max_h, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
// map the image data so that we can save it to file
pixels = (GLubyte*) glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
} else {
pixels = (GLubyte*) vm_malloc(gr_screen.max_w * gr_screen.max_h * 4, memory::quiet_alloc);
if (pixels == NULL) {
glClampColor(GL_CLAMP_READ_COLOR, GL_FIXED_ONLY);
return;
}
glReadPixels(0, 0, gr_screen.max_w, gr_screen.max_h, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, pixels);
glFlush();
}
glClampColor(GL_CLAMP_READ_COLOR, GL_FIXED_ONLY);
// Force alpha to fully opaque so screenshots are not saved with transparency.
// The framebuffer alpha can be < 255 due to blending operations affecting the alpha
// channel as a side effect, even though the rendered image itself is opaque.
int num_pixels = gr_screen.max_w * gr_screen.max_h;
for (int i = 0; i < num_pixels; i++) {
pixels[i * 4 + 3] = 255;
}
if (!png_write_bitmap(os_get_config_path(tmp).c_str(), gr_screen.max_w, gr_screen.max_h, true, pixels)) {
ReleaseWarning(LOCATION, "Failed to write screenshot to \"%s\".", os_get_config_path(tmp).c_str());
}
if (pbo) {
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
pixels = NULL;
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
glDeleteBuffers(1, &pbo);
}
GL_state.PopFramebufferState();
if (pixels != NULL) {
vm_free(pixels);
}
}
SCP_string gr_opengl_blob_screen()
{
GLubyte* pixels = nullptr;
GLuint pbo = 0;
GL_state.PushFramebufferState();
GLuint source_fbo = 0;
GLuint render_target = opengl_get_rtt_framebuffer();
if (render_target != 0) {
source_fbo = render_target;
}
else if (Cmdline_window_res) {
source_fbo = Back_framebuffer;
}
GL_state.BindFrameBuffer(source_fbo, GL_FRAMEBUFFER);
//Reading from the front buffer here seems to no longer work correctly; that just reads back all zeros
glReadBuffer(source_fbo != 0 ? GL_COLOR_ATTACHMENT0 : GL_FRONT);
// now for the data
if (Use_PBOs) {
Assert(!pbo);
glGenBuffers(1, &pbo);
if (!pbo) {
return "";
}
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo);
glBufferData(GL_PIXEL_PACK_BUFFER, (gr_screen.max_w * gr_screen.max_h * 4), NULL, GL_STATIC_READ);
glReadPixels(0, 0, gr_screen.max_w, gr_screen.max_h, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
// map the image data so that we can save it to file
pixels = (GLubyte*)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
}
else {
pixels = (GLubyte*)vm_malloc(gr_screen.max_w * gr_screen.max_h * 4, memory::quiet_alloc);
if (pixels == nullptr) {
return "";
}
glReadPixels(0, 0, gr_screen.max_w, gr_screen.max_h, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, pixels);
glFlush();
}
SCP_string result = png_b64_bitmap(gr_screen.max_w, gr_screen.max_h, true, pixels);
if (pbo) {
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
pixels = NULL;
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
glDeleteBuffers(1, &pbo);
}
GL_state.PopFramebufferState();
if (pixels != nullptr) {
vm_free(pixels);
}
return "data:image/png;base64," + result;
}
void gr_opengl_dump_envmap(const char* filename)
{
char tmp[MAX_PATH_LEN];
GLubyte* pixels = nullptr;
_mkdir(os_get_config_path("envmaps").c_str());
auto width = 512; // These are the hardcoded envmap dimensions
auto height = 512; // in future envmap resolution should be dynamic, or at least extracted from envmap_render_target
auto sphere_width = 4 * width;
auto sphere_height = 2 * height;
auto env_tex = bm_get_gr_info<tcache_slot_opengl>(ENVMAP);
glBindTexture(env_tex->texture_target, env_tex->texture_id);
// Save the previous render target so we can reset it once we are done here
auto previous_target = gr_screen.rendering_to_texture;
// New render target
int sm_flags = (BMP_FLAG_RENDER_TARGET_STATIC);
int spheremap_render_target = bm_make_render_target(sphere_width, sphere_height, sm_flags);
bm_set_render_target(spheremap_render_target);
// Set up cubemap to spherical shader and draw
int env_shader = gr_opengl_maybe_create_shader(SDR_TYPE_ENVMAP_SPHERE_WARP, 0);
opengl_shader_set_current(env_shader);
GL_state.Texture.Enable(0, GL_TEXTURE_CUBE_MAP, env_tex->texture_id);
Current_shader->program->Uniforms.setTextureUniform("envmap", 0);
gr_clear();
opengl_draw_full_screen_textured(0.0f, 0.0f, Scene_texture_u_scale, Scene_texture_v_scale);
bm_set_render_target(previous_target);
// load texture info and write to file
auto sphere_tex = bm_get_gr_info<tcache_slot_opengl>(spheremap_render_target);
glBindTexture(sphere_tex->texture_target, sphere_tex->texture_id);
pixels = (GLubyte*)vm_malloc(sphere_width * sphere_height * 4, memory::quiet_alloc);
glGetTexImage(sphere_tex->texture_target, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
snprintf(tmp, MAX_PATH_LEN, "envmaps/%s.png", filename);
if (!png_write_bitmap(os_get_config_path(tmp).c_str(), 4 * width, 2 * height, true, pixels)) {
ReleaseWarning(LOCATION, "Failed to write envmap to \"%s\".", os_get_config_path(tmp).c_str());
}
// cleanup
if (!bm_release(spheremap_render_target, 1)) {
Warning(LOCATION, "Unable to release environment map render target.");
}
if (pixels != nullptr) {
vm_free(pixels);
}
}
void gr_opengl_shutdown()
{
opengl_tcache_shutdown();
opengl_tnl_shutdown();
opengl_scene_texture_shutdown();
opengl_post_process_shutdown();
opengl_shader_shutdown();
GL_initted = false;
glDeleteVertexArrays(1, &GL_vao);
GL_vao = 0;
graphic_operations->makeOpenGLContextCurrent(nullptr, nullptr);
GL_context = nullptr;
}
void gr_opengl_cleanup(bool closing, int /*minimize*/)
{
if ( !GL_initted ) {
return;
}
if ( !closing && !Fred_running ) {
gr_reset_clip();
gr_clear();
gr_flip();
gr_clear();
gr_flip();
gr_clear();
}
GL_initted = false;
opengl_tcache_flush();
current_viewport = nullptr;
// All windows have to be closed before we destroy the OpenGL context
os::closeAllViewports();
gr_opengl_shutdown();
graphic_operations.reset();
}
int gr_opengl_set_cull(int cull)
{
GLboolean enabled = GL_FALSE;
if (cull) {
enabled = GL_state.CullFace(GL_TRUE);
GL_state.FrontFaceValue(GL_CCW);
GL_state.CullFaceValue(GL_BACK);
} else {
enabled = GL_state.CullFace(GL_FALSE);
}
return (enabled) ? 1 : 0;
}
void gr_opengl_set_clear_color(int r, int g, int b)
{
gr_init_color(&gr_screen.current_clear_color, r, g, b);
}
int gr_opengl_set_color_buffer(int mode)
{
bvec4 enabled;
if ( mode ) {
enabled = GL_state.ColorMask(true, true, true, true);
} else {
enabled = GL_state.ColorMask(false, false, false, false);
}
GL_state.SetAlphaBlendMode(ALPHA_BLEND_ALPHA_BLEND_ALPHA);
// Check if all channels are active
return enabled.x && enabled.y && enabled.z && enabled.w;
}
int gr_opengl_zbuffer_get()
{
if ( !gr_global_zbuffering ) {
return GR_ZBUFF_NONE;
}
return gr_zbuffering_mode;
}
int gr_opengl_zbuffer_set(int mode)
{
int tmp = gr_zbuffering_mode;
gr_zbuffering_mode = mode;
if (gr_zbuffering_mode == GR_ZBUFF_NONE) {
gr_zbuffering = 0;
GL_state.SetZbufferType(ZBUFFER_TYPE_NONE);
} else if ( gr_zbuffering_mode == GR_ZBUFF_READ ) {
gr_zbuffering = 1;
GL_state.SetZbufferType(ZBUFFER_TYPE_READ);
} else {
gr_zbuffering = 1;
GL_state.SetZbufferType(ZBUFFER_TYPE_FULL);
}
return tmp;
}
void gr_opengl_zbuffer_clear(int mode)
{
if (mode) {
gr_zbuffering = 1;
gr_zbuffering_mode = GR_ZBUFF_FULL;
gr_global_zbuffering = 1;
GL_state.SetAlphaBlendMode(ALPHA_BLEND_NONE);
GL_state.SetZbufferType(ZBUFFER_TYPE_FULL);
glClear(GL_DEPTH_BUFFER_BIT);
} else {
gr_zbuffering = 0;
gr_zbuffering_mode = GR_ZBUFF_NONE;
gr_global_zbuffering = 0;
GL_state.DepthTest(GL_FALSE);
}
}
int gr_opengl_stencil_set(int mode)
{
int tmp = gr_stencil_mode;
gr_stencil_mode = mode;
if ( mode == GR_STENCIL_READ ) {
GL_state.StencilTest(1);
GL_state.StencilFunc(GL_NOTEQUAL, 1, 0xFFFF);
GL_state.StencilOpSeparate(GL_FRONT_AND_BACK, GL_KEEP, GL_KEEP, GL_KEEP);
} else if ( mode == GR_STENCIL_WRITE ) {
GL_state.StencilTest(1);
GL_state.StencilFunc(GL_ALWAYS, 1, 0xFFFF);
GL_state.StencilOpSeparate(GL_FRONT_AND_BACK, GL_KEEP, GL_KEEP, GL_REPLACE);
} else {
GL_state.StencilTest(0);
GL_state.StencilFunc(GL_NEVER, 1, 0xFFFF);
GL_state.StencilOpSeparate(GL_FRONT_AND_BACK, GL_KEEP, GL_KEEP, GL_KEEP);
}
return tmp;
}
void gr_opengl_stencil_clear()
{
glClear(GL_STENCIL_BUFFER_BIT);
}
int gr_opengl_alpha_mask_set(int mode, float alpha)
{
if ( mode ) {
GL_alpha_threshold = alpha;
} else {
GL_alpha_threshold = 0.0f;
}
// alpha masking is deprecated
return mode;
}
void gr_opengl_get_region(int /*front*/, int w, int h, ubyte *data)
{
// if (front) {
// glReadBuffer(GL_FRONT);
// } else {
glReadBuffer(GL_BACK);
// }
GL_state.SetAlphaBlendMode(ALPHA_BLEND_NONE);
GL_state.SetZbufferType(ZBUFFER_TYPE_NONE);
if (gr_screen.bits_per_pixel == 16) {
glReadPixels(0, gr_screen.max_h-h, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
} else if (gr_screen.bits_per_pixel == 32) {
glReadPixels(0, gr_screen.max_h-h, w, h, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
}
}
int gr_opengl_save_screen()
{
int i;
ubyte *sptr = NULL, *dptr = NULL;
ubyte *opengl_screen_tmp = NULL;
int width_times_pixel;
gr_opengl_reset_clip();
if (GL_saved_screen || GL_screen_pbo) {
// already have a screen saved so just bail...
return -1;
}
GL_saved_screen = (ubyte*)vm_malloc( gr_screen.max_w * gr_screen.max_h * 4, memory::quiet_alloc);
if (!GL_saved_screen) {
mprintf(( "Couldn't get memory for saved screen!\n" ));
return -1;
}
GLboolean save_state = GL_state.DepthTest(GL_FALSE);
GL_state.PushFramebufferState();
GL_state.BindFrameBuffer(Cmdline_window_res ? Back_framebuffer : 0, GL_FRAMEBUFFER);
//Reading from the front buffer here seems to no longer work correctly; that just reads back all zeros
glReadBuffer(Cmdline_window_res ? GL_COLOR_ATTACHMENT0 : GL_FRONT);
if ( Use_PBOs ) {
GLubyte *pixels = NULL;
glGenBuffers(1, &GL_screen_pbo);
if (!GL_screen_pbo) {
if (GL_saved_screen) {
vm_free(GL_saved_screen);
GL_saved_screen = NULL;
}
return -1;
}
glBindBuffer(GL_PIXEL_PACK_BUFFER, GL_screen_pbo);
glBufferData(GL_PIXEL_PACK_BUFFER, gr_screen.max_w * gr_screen.max_h * 4, NULL, GL_STATIC_READ);
glReadPixels(0, 0, gr_screen.max_w, gr_screen.max_h, GL_read_format, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
pixels = (GLubyte*)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
width_times_pixel = (gr_screen.max_w * 4);
sptr = (ubyte *)pixels;
dptr = (ubyte *)&GL_saved_screen[gr_screen.max_w * gr_screen.max_h * 4];
for (i = 0; i < gr_screen.max_h; i++) {
dptr -= width_times_pixel;
memcpy(dptr, sptr, width_times_pixel);
sptr += width_times_pixel;
}
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
glDeleteBuffers(1, &GL_screen_pbo);
GL_screen_pbo = 0;
GL_saved_screen_id = bm_create(32, gr_screen.max_w, gr_screen.max_h, GL_saved_screen, 0);
} else {
opengl_screen_tmp = (ubyte*)vm_malloc( gr_screen.max_w * gr_screen.max_h * 4, memory::quiet_alloc);
if (!opengl_screen_tmp) {
if (GL_saved_screen) {
vm_free(GL_saved_screen);
GL_saved_screen = NULL;
}
mprintf(( "Couldn't get memory for temporary saved screen!\n" ));
GL_state.DepthTest(save_state);
return -1;
}
glReadPixels(0, 0, gr_screen.max_w, gr_screen.max_h, GL_read_format, GL_UNSIGNED_INT_8_8_8_8_REV, opengl_screen_tmp);
sptr = (ubyte *)&opengl_screen_tmp[gr_screen.max_w * gr_screen.max_h * 4];
dptr = (ubyte *)GL_saved_screen;
width_times_pixel = (gr_screen.max_w * 4);
for (i = 0; i < gr_screen.max_h; i++) {
sptr -= width_times_pixel;
memcpy(dptr, sptr, width_times_pixel);
dptr += width_times_pixel;
}
vm_free(opengl_screen_tmp);
GL_saved_screen_id = bm_create(32, gr_screen.max_w, gr_screen.max_h, GL_saved_screen, 0);
}
GL_state.PopFramebufferState();
GL_state.DepthTest(save_state);
return GL_saved_screen_id;
}
void gr_opengl_restore_screen(int bmp_id)
{
gr_reset_clip();
if ( !GL_saved_screen ) {
gr_clear();
return;
}
Assert( (bmp_id < 0) || (bmp_id == GL_saved_screen_id) );
if (GL_saved_screen_id < 0)
return;
gr_set_bitmap(GL_saved_screen_id);
gr_bitmap(0, 0, GR_RESIZE_NONE); // don't scale here since we already have real screen size
}
void gr_opengl_free_screen(int bmp_id)
{
if (!GL_saved_screen)
return;
vm_free(GL_saved_screen);
GL_saved_screen = NULL;
Assert( (bmp_id < 0) || (bmp_id == GL_saved_screen_id) );
if (GL_saved_screen_id < 0)
return;
bm_release(GL_saved_screen_id);
GL_saved_screen_id = -1;
}
//fill mode, solid/wire frame
void gr_opengl_set_fill_mode(int mode)
{
if (mode == GR_FILL_MODE_SOLID) {
GL_state.SetPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
return;
}
if (mode == GR_FILL_MODE_WIRE) {
GL_state.SetPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
return;
}
// default setting
GL_state.SetPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
void gr_opengl_zbias(int bias)
{
if (bias) {
GL_state.PolygonOffsetFill(GL_TRUE);
if(bias < 0) {
GL_state.SetPolygonOffset(1.0, -i2fl(bias));
}
else {
GL_state.SetPolygonOffset(0.0, -i2fl(bias));
}
} else {
GL_state.PolygonOffsetFill(GL_FALSE);
}
}
void gr_opengl_set_line_width(float width)
{
if (gr_lua_context_active()) {
gr_lua_screen.line_width = width;
} else {
if (width <= 1.0f) {
GL_state.SetLineWidth(width);
}
gr_screen.line_width = width;
}
}
int opengl_check_for_errors(const char *err_at)
{
#ifdef NDEBUG
return 0;
#endif
int num_errors = 0;
auto err = glGetError();
if (err != GL_NO_ERROR) {
if (err_at != NULL) {
nprintf(("OpenGL", "OpenGL Error from %s: %#x\n", err_at, err));
} else {
nprintf(("OpenGL", "OpenGL Error: %#x\n", err));
}
num_errors++;
}
return num_errors;
}
void opengl_set_vsync(bool enable)
{
if (enable) {
// Try to use adaptive vsync when supported
if (!GL_context->setSwapInterval(-1)) {
GL_context->setSwapInterval(1);
}
} else {
GL_context->setSwapInterval(0);
}
}
std::unique_ptr<os::Viewport> gr_opengl_create_viewport(const os::ViewPortProperties& props) {
os::ViewPortProperties attrs = props;
attrs.pixel_format.red_size = Gr_red.bits;
attrs.pixel_format.green_size = Gr_green.bits;
attrs.pixel_format.blue_size = Gr_blue.bits;
attrs.pixel_format.alpha_size = (gr_screen.bits_per_pixel == 32) ? Gr_alpha.bits : 0;
attrs.pixel_format.depth_size = (gr_screen.bits_per_pixel == 32) ? 24 : 16;
attrs.pixel_format.stencil_size = (gr_screen.bits_per_pixel == 32) ? 8 : 1;
attrs.pixel_format.multi_samples = os_config_read_uint(NULL, "OGL_AntiAliasSamples", 0);
attrs.enable_opengl = true;
#ifndef USE_OPENGL_ES
attrs.gl_attributes.profile = os::OpenGLProfile::Core;
#else
attrs.gl_attributes.profile = os::OpenGLProfile::ES;
#endif
return graphic_operations->createViewport(attrs);
}
void gr_opengl_use_viewport(os::Viewport* view) {
graphic_operations->makeOpenGLContextCurrent(view, GL_context.get());
current_viewport = view;
auto size = view->getSize();
gr_screen_resize(size.first, size.second);
}
int opengl_init_display_device()
{
os::ViewPortProperties attrs;
attrs.enable_opengl = true;
attrs.gl_attributes.major_version = MIN_REQUIRED_GL_VERSION / 10;
attrs.gl_attributes.minor_version = MIN_REQUIRED_GL_VERSION % 10;
#ifndef NDEBUG
attrs.gl_attributes.flags.set(os::OpenGLContextFlags::Debug);
#endif
#ifndef USE_OPENGL_ES
attrs.gl_attributes.profile = os::OpenGLProfile::Core;
#else
attrs.gl_attributes.profile = os::OpenGLProfile::ES;
#endif
attrs.display = os_config_read_uint("Video", "Display", 0);
attrs.width = (uint32_t) gr_screen.max_w;
attrs.height = (uint32_t) gr_screen.max_h;
attrs.title = Osreg_title;
if (!Window_title.empty()) {
attrs.title = Window_title;
}
if (Cmdline_enable_vr) {
// Force Windowed mode in VR
} else if (Using_in_game_options) {
switch (Gr_configured_window_state) {
case os::ViewportState::Windowed:
// That's the default
break;
case os::ViewportState::Borderless:
attrs.flags.set(os::ViewPortFlags::Borderless);
break;
case os::ViewportState::Fullscreen:
attrs.flags.set(os::ViewPortFlags::Fullscreen);
break;
}
} else {
if (!Cmdline_window && !Cmdline_fullscreen_window) {
attrs.flags.set(os::ViewPortFlags::Fullscreen);
} else if (Cmdline_fullscreen_window) {
attrs.flags.set(os::ViewPortFlags::Borderless);
}
}
if (Cmdline_capture_mouse)
attrs.flags.set(os::ViewPortFlags::Capture_Mouse);
auto viewport = gr_opengl_create_viewport(attrs);
if (!viewport) {
return 1;
}
const int gl_versions[] = { 45, 44, 43, 42, 41, 40, 33, 32 };
// find the latest and greatest OpenGL context
for (auto ver : gl_versions)
{
auto gl_attrs = attrs.gl_attributes;
gl_attrs.major_version = ver / 10;
gl_attrs.minor_version = ver % 10;
GL_context = graphic_operations->createOpenGLContext(viewport.get(), gl_attrs);
if (GL_context != nullptr)
{
break;
}
}
if (GL_context == nullptr) {
return 1;