-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy path2d.cpp
More file actions
3327 lines (2732 loc) · 91.9 KB
/
2d.cpp
File metadata and controls
3327 lines (2732 loc) · 91.9 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
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#ifdef _WIN32
#include <windows.h>
#include <windowsx.h>
#endif
#include "globalincs/alphacolors.h"
#include "globalincs/systemvars.h"
#include "2d.h"
#include "grinternal.h"
#include "grstub.h"
#include "light.h"
#include "material.h"
#include "matrix.h"
#include "cmdline/cmdline.h"
#include "debugconsole/console.h"
#include "executor/global_executors.h"
#include "graphics/openxr.h"
#include "graphics/paths/PathRenderer.h"
#include "graphics/post_processing.h"
#include "graphics/util/GPUMemoryHeap.h"
#include "graphics/util/UniformBuffer.h"
#include "graphics/util/UniformBufferManager.h"
#include "graphics/shadows.h"
#include "io/mouse.h"
#include "libs/jansson.h"
#include "options/Option.h"
#include "osapi/osapi.h"
#include "parse/parselo.h"
#include "popup/popup.h"
#include "render/3d.h"
#include "scripting/hook_api.h"
#include "scripting/scripting.h"
#include "tracing/tracing.h"
#include "utils/boost/hash_combine.h"
#include "utils/string_utils.h"
#include "gamesequence/gamesequence.h"
#ifdef WITH_OPENGL
#include "graphics/opengl/gropengl.h"
#endif
#ifdef WITH_VULKAN
#include "graphics/vulkan/gr_vulkan.h"
#endif
#include <SDL_surface.h>
#include <algorithm>
#include <climits>
#if (SDL_VERSION_ATLEAST(1, 2, 7))
#include "SDL_cpuinfo.h"
#endif
#define GR_CAPABILITY_ENTRY(capability) gr_capability_def{ gr_capability::CAPABILITY_##capability, #capability }
gr_capability_def gr_capabilities[] = {
GR_CAPABILITY_ENTRY(ENVIRONMENT_MAP),
GR_CAPABILITY_ENTRY(NORMAL_MAP),
GR_CAPABILITY_ENTRY(HEIGHT_MAP),
GR_CAPABILITY_ENTRY(SOFT_PARTICLES),
GR_CAPABILITY_ENTRY(DISTORTION),
GR_CAPABILITY_ENTRY(POST_PROCESSING),
GR_CAPABILITY_ENTRY(DEFERRED_LIGHTING),
GR_CAPABILITY_ENTRY(SHADOWS),
GR_CAPABILITY_ENTRY(THICK_OUTLINE),
GR_CAPABILITY_ENTRY(BATCHED_SUBMODELS),
GR_CAPABILITY_ENTRY(TIMESTAMP_QUERY),
GR_CAPABILITY_ENTRY(SEPARATE_BLEND_FUNCTIONS),
GR_CAPABILITY_ENTRY(PERSISTENT_BUFFER_MAPPING),
gr_capability_def {gr_capability::CAPABILITY_BPTC, "BPTC Texture Compression"}, //This one had a different parse string already!
GR_CAPABILITY_ENTRY(LARGE_SHADER),
GR_CAPABILITY_ENTRY(INSTANCED_RENDERING),
};
const size_t gr_capabilities_num = sizeof(gr_capabilities) / sizeof(gr_capabilities[0]);
#undef GR_CAPABILITY_ENTRY
const char* Resolution_prefixes[GR_NUM_RESOLUTIONS] = {"", "2_"};
screen gr_screen;
lua_screen gr_lua_screen;
color_gun Gr_red, Gr_green, Gr_blue, Gr_alpha;
color_gun Gr_t_red, Gr_t_green, Gr_t_blue, Gr_t_alpha;
color_gun Gr_ta_red, Gr_ta_green, Gr_ta_blue, Gr_ta_alpha;
color_gun *Gr_current_red, *Gr_current_green, *Gr_current_blue, *Gr_current_alpha;
static SCP_string Pending_screenshot_filename;
ubyte Gr_original_palette[768]; // The palette
ubyte Gr_current_palette[768];
char Gr_current_palette_name[128] = NOX("none");
// cursor stuff
io::mouse::Cursor* Web_cursor = NULL;
int Gr_inited = 0;
float Gr_gamma = 1.0f;
static SCP_vector<float> gamma_value_enumerator()
{
SCP_vector<float> vals;
// We want to divide the possible values into increments of 0.05
constexpr auto UPPER_LIMIT = (int)(5.0 / 0.05);
for (int i = 2; i <= UPPER_LIMIT; ++i) {
vals.push_back(0.05f * i);
}
return vals;
}
static SCP_string gamma_display(float value)
{
SCP_string out;
sprintf(out, "%.2f", value);
return out;
}
static bool gamma_change_listener(float new_val, bool initial)
{
if (!initial) {
// This is not valid for the initial config load since that happens before the graphics system is initialized
gr_set_gamma(new_val);
} else {
Gr_gamma = new_val;
}
return true;
}
static void parse_gamma_func()
{
float value;
stuff_float(&value);
constexpr float EPSILON = 0.0001f;
if (value < 0.1f - EPSILON || value > 5.0f + EPSILON) {
error_display(0, "%f is not a valid gamma value! (Out of range)", value);
return;
}
float expected_i = value / 0.05f;
int i = fl2i(std::round(expected_i));
if (std::abs(value - (0.05f * i)) < EPSILON && i >= 2 && i <= 100) {
Gr_gamma = value;
return;
}
error_display(0, "%f is not a valid gamma value! (Invalid increment)", value);
}
static auto GammaOption __UNUSED = options::OptionBuilder<float>("Graphics.Gamma",
std::pair<const char*, int>{"Brightness", 1375},
std::pair<const char*, int>{"The brightness value used for the game window", 1738})
.category(std::make_pair("Graphics", 1825))
.default_func([]() { return Gr_gamma; })
.enumerator(gamma_value_enumerator)
.display(gamma_display)
.change_listener(gamma_change_listener)
.flags({options::OptionFlags::RetailBuiltinOption})
.parser(parse_gamma_func)
.finish();
static void parse_lighting_func()
{
constexpr int num_detail_presets = static_cast<int>(DefaultDetailPreset::Num_detail_presets);
int value[num_detail_presets];
stuff_int_list(value, num_detail_presets, ParseLookupType::RAW_INTEGER_TYPE);
for (int i = 0; i < num_detail_presets; i++) {
if (value[i] < 0 || value[i] > MAX_DETAIL_VALUE) {
error_display(0, "%i is an invalid detail level value!", value[i]);
} else {
change_default_detail_level(static_cast<DefaultDetailPreset>(i), DetailSetting::Lighting, value[i]);
}
}
}
const SCP_vector<std::pair<int, std::pair<const char*, int>>> DetailLevelValues = {{ 0, {"Minimum", 1680}},
{ 1, {"Low", 1160}},
{ 2, {"Medium", 1161}},
{ 3, {"High", 1162}},
{ 4, {"Ultra", 1721}}};
const auto LightingOption __UNUSED = options::OptionBuilder<int>("Graphics.Lighting",
std::pair<const char*, int>{"Lighting", 1367},
std::pair<const char*, int>{"Level of detail of the lighting", 1715})
.importance(1)
.category(std::make_pair("Graphics", 1825))
.values(DetailLevelValues)
.default_func([]() { return Detail.lighting; })
.change_listener([](int val, bool initial) {
Detail.lighting = val;
if (!initial) {
gr_recompile_all_shaders(nullptr);
}
return true;
})
.flags({options::OptionFlags::RetailBuiltinOption})
.parser(parse_lighting_func)
.finish();
os::ViewportState Gr_configured_window_state = os::ViewportState::Fullscreen;
static bool mode_change_func(os::ViewportState state, bool initial)
{
Gr_configured_window_state = state;
if (initial) {
return false;
}
auto window = os::getMainViewport();
if (window == nullptr) {
return false;
}
window->setState(state);
return true;
}
static void parse_window_mode_func()
{
SCP_string value;
stuff_string(value, F_NAME);
if (lcase_equal(value, "windowed")) {
Gr_configured_window_state = os::ViewportState::Windowed;
} else if (lcase_equal(value, "borderless")) {
Gr_configured_window_state = os::ViewportState::Borderless;
} else if (lcase_equal(value, "fullscreen")) {
Gr_configured_window_state = os::ViewportState::Fullscreen;
} else {
error_display(0, "%s is an invalide window mode", value.c_str());
}
}
static auto WindowModeOption __UNUSED = options::OptionBuilder<os::ViewportState>("Graphics.WindowMode",
std::pair<const char*, int>{"Window Mode", 1772},
std::pair<const char*, int>{"Controls how the game window is created", 1773})
.category(std::make_pair("Graphics", 1825))
.level(options::ExpertLevel::Beginner)
.values({{os::ViewportState::Fullscreen, {"Fullscreen", 1679}},
{os::ViewportState::Borderless, {"Borderless", 1675}},
{os::ViewportState::Windowed, {"Windowed", 1676}}})
.importance(98)
.default_func([]() { return Gr_configured_window_state; })
.change_listener(mode_change_func)
.parser(parse_window_mode_func)
.finish();
void removeWindowModeOption()
{
options::OptionsManager::instance()->removeOption(WindowModeOption);
}
const std::shared_ptr<scripting::OverridableHook<>> OnFrameHook = scripting::OverridableHook<>::Factory(
"On Frame", "Called every frame as the last action before showing the frame result to the user.", {}, std::nullopt, CHA_ONFRAME);
// z-buffer stuff
int gr_zbuffering = 0;
int gr_zbuffering_mode = 0;
int gr_global_zbuffering = 0;
// stencil buffer stuff
int gr_stencil_mode = 0;
// Default clipping distances
const float Default_min_draw_distance = 1.0f;
// Reduced from 1e10 to 1e6, as beyond that FSO's physics precision is horrendous anyways, and it allows reasonable lighting and particle clipping all the way out until that point, unlike 1e7 or above where the depth precision just is not enough
const float Default_max_draw_distance = 1e6f;
float Min_draw_distance_cockpit = 0.02f;
float Min_draw_distance = Default_min_draw_distance;
float Max_draw_distance = Default_max_draw_distance;
// Pre-computed screen resize vars
static float Gr_full_resize_X = 1.0f, Gr_full_resize_Y = 1.0f;
static float Gr_full_center_resize_X = 1.0f, Gr_full_center_resize_Y = 1.0f;
static float Gr_resize_X = 1.0f, Gr_resize_Y = 1.0f;
static float Gr_menu_offset_X = 0.0f, Gr_menu_offset_Y = 0.0f;
static float Gr_menu_zoomed_offset_X = 0.0f, Gr_menu_zoomed_offset_Y = 0.0f;
float Gr_save_full_resize_X = 1.0f, Gr_save_full_resize_Y = 1.0f;
float Gr_save_full_center_resize_X = 1.0f, Gr_save_full_center_resize_Y = 1.0f;
float Gr_save_resize_X = 1.0f, Gr_save_resize_Y = 1.0f;
float Gr_save_menu_offset_X = 0.0f, Gr_save_menu_offset_Y = 0.0f;
float Gr_save_menu_zoomed_offset_X = 0.0f, Gr_save_menu_zoomed_offset_Y = 0.0f;
bool Save_custom_screen_size;
bool Deferred_lighting = false;
bool High_dynamic_range = false;
static ushort* Gr_original_gamma_ramp = nullptr;
static int videodisplay_deserializer(const json_t* value)
{
int id;
json_error_t err;
if (json_unpack_ex((json_t*)value, &err, 0, "i", &id) != 0) {
throw json_exception(err);
}
return id;
}
static json_t* videodisplay_serializer(int value) { return json_pack("i", value); }
static SCP_vector<int> videodisplay_enumerator()
{
SCP_vector<int> vals;
for (int i = 0; i < SDL_GetNumVideoDisplays(); ++i) {
vals.push_back(i);
}
return vals;
}
static SCP_string videodisplay_display(int id)
{
SCP_string out;
sprintf(out, "(%d) %s", id + 1, SDL_GetDisplayName(id));
return out;
}
static bool videodisplay_change(int display, bool initial)
{
if (initial) {
return false;
}
auto window = os::getSDLMainWindow();
if (window == nullptr) {
return false;
}
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED_DISPLAY(display), SDL_WINDOWPOS_CENTERED_DISPLAY(display));
return true;
}
// Video display cannot support default settings because graphics have not been
// initialized so we can't validate the setting. But also, this should probably
// only ever be a user setting
static auto VideoDisplayOption = options::OptionBuilder<int>("Graphics.Display",
std::pair<const char*, int>{"Primary display", 1741},
std::pair<const char*, int>{"The display used for rendering", 1742})
.category(std::make_pair("Graphics", 1825))
.level(options::ExpertLevel::Beginner)
.deserializer(videodisplay_deserializer)
.serializer(videodisplay_serializer)
.enumerator(videodisplay_enumerator)
.display(videodisplay_display)
.flags({options::OptionFlags::ForceMultiValueSelection})
.default_val(0)
.change_listener(videodisplay_change)
.importance(99)
.finish();
struct ResolutionInfo {
uint32_t width = 0;
uint32_t height = 0;
ResolutionInfo(uint32_t _width, uint32_t _height) : width(_width), height(_height) {}
ResolutionInfo() = default;
friend bool operator==(const ResolutionInfo& lhs, const ResolutionInfo& rhs)
{
return lhs.width == rhs.width && lhs.height == rhs.height;
}
friend bool operator!=(const ResolutionInfo& lhs, const ResolutionInfo& rhs) { return !(rhs == lhs); }
};
static ResolutionInfo resolution_deserializer(const json_t* el)
{
int width;
int height;
json_error_t err;
if (json_unpack_ex((json_t*)el, &err, 0, "{s:i, s:i}", "width", &width, "height", &height) != 0) {
throw json_exception(err);
}
return {(uint32_t)width, (uint32_t)height};
}
static json_t* resolution_serializer(const ResolutionInfo& value)
{
return json_pack("{s:i, s:i}", "width", value.width, "height", value.height);
}
static SCP_vector<ResolutionInfo> resolution_enumerator()
{
SCP_vector<ResolutionInfo> out;
auto display = VideoDisplayOption->getValue();
for (auto i = 0; i < SDL_GetNumDisplayModes(display); ++i) {
SDL_DisplayMode mode;
if (SDL_GetDisplayMode(display, i, &mode) != 0) {
continue;
}
auto res = ResolutionInfo(mode.w, mode.h);
if (std::find(out.begin(), out.end(), res) == out.end()) {
out.emplace_back(res);
}
}
return out;
}
static SCP_vector<ResolutionInfo> resolution_vr_enumerator()
{
SCP_vector<ResolutionInfo> out;
for (int i = 1000; i <= 6000; i += 500) {
out.emplace_back(ResolutionInfo(i, i));
}
return out;
}
static SCP_string resolution_display(const ResolutionInfo& info)
{
SCP_string str;
sprintf(str, "%dx%d", info.width, info.height);
return str;
}
static ResolutionInfo resolution_default()
{
SDL_DisplayMode mode;
if (SDL_GetDesktopDisplayMode(VideoDisplayOption->getValue(), &mode) != 0) {
return {};
}
return {(uint32_t)mode.w, (uint32_t)mode.h};
}
static ResolutionInfo resolution_vr_default()
{
return {(uint32_t)2500, (uint32_t)2500};
}
static bool resolution_change(const ResolutionInfo& /*info*/, bool initial)
{
if (initial) {
return false;
}
return false;
// The following code should change the size of the window properly but FSO currently can't handle that
/*
auto window = os::getSDLMainWindow();
if (window == nullptr) {
return;
}
auto display = VideoDisplayOption->getValue();
if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
SDL_DisplayMode target;
target.w = info.width;
target.h = info.height;
target.format = 0; // don't care
target.refresh_rate = 0; // don't care
target.driverdata = 0; // initialize to 0
SDL_DisplayMode closest;
if (SDL_GetClosestDisplayMode(display, &target, &closest) == nullptr) {
return;
}
SDL_SetWindowDisplayMode(window, &closest);
} else {
SDL_SetWindowSize(window, info.width, info.height);
// Recenter the window
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED_DISPLAY(display), SDL_WINDOWPOS_CENTERED_DISPLAY(display));
}
*/
}
static bool resolution_vr_change(const ResolutionInfo& /*info*/, bool initial)
{
if (initial) {
return false;
}
return false;
}
// Resolution cannot support default settings because graphics have not been
// initialized so we can't validate the setting. But also, this should probably
// only ever be a user setting
static auto ResolutionOption = options::OptionBuilder<ResolutionInfo>("Graphics.Resolution",
std::pair<const char*, int>{"Resolution", 1748},
std::pair<const char*, int>{"The rendering resolution", 1749})
.category(std::make_pair("Graphics", 1825))
.level(options::ExpertLevel::Beginner)
.deserializer(resolution_deserializer)
.serializer(resolution_serializer)
.enumerator(resolution_enumerator)
.display(resolution_display)
.default_func(resolution_default)
.change_listener(resolution_change)
.importance(100)
.finish();
void removeResolutionOption()
{
options::OptionsManager::instance()->removeOption(ResolutionOption);
}
static auto ResolutionVROption = options::OptionBuilder<ResolutionInfo>("Graphics.ResolutionVR",
std::pair<const char*, int>{"VR Resolution", 1878},
std::pair<const char*, int>{"The rendering resolution when in VR mode", 1879})
.category(std::make_pair("Graphics", 1825))
.level(options::ExpertLevel::Beginner)
.deserializer(resolution_deserializer)
.serializer(resolution_serializer)
.enumerator(resolution_vr_enumerator)
.display(resolution_display)
.default_func(resolution_vr_default)
.change_listener(resolution_vr_change)
.importance(101)
.finish();
void removeResolutionVROption()
{
options::OptionsManager::instance()->removeOption(ResolutionVROption);
}
bool Gr_enable_soft_particles = true;
static void parse_soft_particle_func() {
bool value;
stuff_boolean(&value);
Gr_enable_soft_particles = value;
}
static auto SoftParticlesOption __UNUSED = options::OptionBuilder<bool>("Graphics.SoftParticles",
std::pair<const char*, int>{"Soft Particles", 1761},
std::pair<const char*, int>{"Enable or disable soft particle rendering", 1762})
.category(std::make_pair("Graphics", 1825))
.level(options::ExpertLevel::Advanced)
.default_func([]() { return Gr_enable_soft_particles; })
.bind_to_once(&Gr_enable_soft_particles)
.importance(68)
.parser(parse_soft_particle_func)
.finish();
flagset<FramebufferEffects> Gr_framebuffer_effects{};
static void parse_framebuffer_func() {
SCP_string value;
stuff_string(value, F_NAME);
// Convert to lowercase once
SCP_tolower(value);
// Use a map to associate strings with their respective actions
static const std::unordered_map<std::string, std::function<void()>> effectActions = {
{"shockwaves", []() { Gr_framebuffer_effects.set(FramebufferEffects::Shockwaves); }},
{"thrusters", []() { Gr_framebuffer_effects.set(FramebufferEffects::Thrusters); }},
{"all", []() {
Gr_framebuffer_effects.set(FramebufferEffects::Shockwaves);
Gr_framebuffer_effects.set(FramebufferEffects::Thrusters);
}},
{"none", []() { /* No-op */ }}
};
auto it = effectActions.find(value);
if (it != effectActions.end()) {
Gr_framebuffer_effects = flagset<FramebufferEffects>(); // Clear only if valid
it->second(); // Execute the corresponding action
} else {
error_display(0, "%s is not a valid framebuffer effect setting", value.c_str());
}
}
static auto FramebufferEffectsOption __UNUSED = options::OptionBuilder<flagset<FramebufferEffects>>("Graphics.FramebufferEffects",
std::pair<const char*, int>{"Framebuffer effects", 1732},
std::pair<const char*, int>{"Controls which framebuffer effects will be applied to the scene", 1733})
.category(std::make_pair("Graphics", 1825))
.level(options::ExpertLevel::Advanced)
.values({{{}, {"None", 211}},
{{FramebufferEffects::Shockwaves}, {"Shockwaves", 1688}},
{{FramebufferEffects::Thrusters}, {"Thrusters", 1689}},
{{FramebufferEffects::Shockwaves, FramebufferEffects::Thrusters}, {"All", 1690}}})
.default_func([]() { return Gr_framebuffer_effects; } )
.bind_to_once(&Gr_framebuffer_effects)
.importance(77)
.parser(parse_framebuffer_func)
.finish();
AntiAliasMode Gr_aa_mode = AntiAliasMode::None;
AntiAliasMode Gr_aa_mode_last_frame = AntiAliasMode::None;
static void parse_anti_aliasing_func() {
SCP_string value;
stuff_string(value, F_NAME);
SCP_tolower(value);
// Map of valid values to AntiAliasMode
static const std::unordered_map<std::string, AntiAliasMode> aaModeMap = {
{"none", AntiAliasMode::None},
{"fxaa low", AntiAliasMode::FXAA_Low},
{"fxaa medium", AntiAliasMode::FXAA_Medium},
{"fxaa high", AntiAliasMode::FXAA_High},
{"smaa low", AntiAliasMode::SMAA_Low},
{"smaa medium", AntiAliasMode::SMAA_Medium},
{"smaa high", AntiAliasMode::SMAA_High},
{"smaa ultra", AntiAliasMode::SMAA_Ultra},
};
// Look up the value in the map
auto it = aaModeMap.find(value);
if (it != aaModeMap.end()) {
Gr_aa_mode = it->second; // Set the mode
} else {
error_display(0, "%s is not a valid anti aliasing setting", value.c_str());
}
}
static auto AAOption __UNUSED = options::OptionBuilder<AntiAliasMode>("Graphics.AAMode",
std::pair<const char*, int>{"Anti Aliasing", 1752},
std::pair<const char*, int>{"Controls the anti aliasing mode of the engine.", 1753})
.category(std::make_pair("Graphics", 1825))
.level(options::ExpertLevel::Advanced)
.values({{AntiAliasMode::None, {"None", 211}},
{AntiAliasMode::FXAA_Low, {"FXAA Low", 1681}},
{AntiAliasMode::FXAA_Medium, {"FXAA Medium", 1682}},
{AntiAliasMode::FXAA_High, {"FXAA High", 1683}},
{AntiAliasMode::SMAA_Low, {"SMAA Low", 1684}},
{AntiAliasMode::SMAA_Medium, {"SMAA Medium", 1685}},
{AntiAliasMode::SMAA_High, {"SMAA High", 1686}},
{AntiAliasMode::SMAA_Ultra, {"SMAA Ultra", 1687}}})
.default_func([]() { return Gr_aa_mode; } )
.bind_to(&Gr_aa_mode)
.importance(79)
.parser(parse_anti_aliasing_func)
.finish();
extern int Cmdline_msaa_enabled;
static void parse_msaa_func()
{
SCP_string value;
stuff_string(value, F_NAME);
// Convert to lowercase
SCP_string lowercase_value = value;
SCP_tolower(lowercase_value);
// Map valid values to MSAA settings
static const std::unordered_map<std::string, int> msaaMap = {
{"off", 0},
{"4 samples", 4},
{"8 samples", 8},
//{"16 samples", 16},
};
// Look up the value in the map
auto it = msaaMap.find(lowercase_value);
if (it != msaaMap.end()) {
Cmdline_msaa_enabled = it->second; // Set the MSAA level
} else {
error_display(0, "%s is not a valid MSAA setting", value.c_str());
}
}
static auto MSAAOption __UNUSED = options::OptionBuilder<int>("Graphics.MSAASamples",
std::pair<const char*, int>{"Multisample Anti Aliasing", 1758},
std::pair<const char*, int>{"Controls whether multisample anti asliasing is enabled, and with how many samples", 1759})
.category(std::make_pair("Graphics", 1825))
.level(options::ExpertLevel::Advanced)
.values({{0, {"Off", 1693}},
{4, {"4 Samples", 1694}},
{8, {"8 Samples", 1695}}})
.default_func([]() { return Cmdline_msaa_enabled; } )
.bind_to_once(&Cmdline_msaa_enabled)
.importance(78)
.parser(parse_msaa_func)
.finish();
bool gr_is_fxaa_mode(AntiAliasMode mode)
{
return mode == AntiAliasMode::FXAA_Low || mode == AntiAliasMode::FXAA_Medium || mode == AntiAliasMode::FXAA_High;
}
bool gr_is_smaa_mode(AntiAliasMode mode) {
return mode == AntiAliasMode::SMAA_Low || mode == AntiAliasMode::SMAA_Medium || mode == AntiAliasMode::SMAA_High || mode == AntiAliasMode::SMAA_Ultra;
}
static void parse_post_processing_func()
{
bool value;
stuff_boolean(&value);
Gr_post_processing_enabled = value;
}
bool Gr_post_processing_enabled = true;
static auto PostProcessOption __UNUSED = options::OptionBuilder<bool>("Graphics.PostProcessing",
std::pair<const char*, int>{"Post processing", 1726},
std::pair<const char*, int>{"Controls whether post processing is enabled in the engine.", 1727})
.category(std::make_pair("Graphics", 1825))
.level(options::ExpertLevel::Advanced)
.default_func([]() { return Gr_post_processing_enabled; })
.bind_to_once(&Gr_post_processing_enabled)
.importance(69)
.parser(parse_post_processing_func)
.finish();
bool Gr_enable_vsync = true;
static void parse_vsync_func()
{
bool value;
stuff_boolean(&value);
Gr_enable_vsync = value;
}
static auto VSyncOption __UNUSED = options::OptionBuilder<bool>("Graphics.VSync",
std::pair<const char*, int>{"Vertical Sync", 1766},
std::pair<const char*, int>{"Controls how the engine does vertical synchronization", 1767})
.category(std::make_pair("Graphics", 1825))
.level(options::ExpertLevel::Advanced)
.default_func([]() { return Gr_enable_vsync; })
.bind_to_once(&Gr_enable_vsync)
.importance(70)
.parser(parse_vsync_func)
.finish();
void removeVSyncOption()
{
options::OptionsManager::instance()->removeOption(VSyncOption);
}
static std::unique_ptr<graphics::util::UniformBufferManager> UniformBufferManager;
// Forward definitions
static void uniform_buffer_managers_init();
static void uniform_buffer_managers_deinit();
static void uniform_buffer_managers_retire_buffers();
static void gpu_heap_init();
static void gpu_heap_deinit();
void gr_set_screen_scale(int w, int h, int zoom_w, int zoom_h, int max_w, int max_h, int center_w, int center_h,
bool force_stretch)
{
bool do_zoom = zoom_w > 0 && zoom_h > 0 && (zoom_w != w || zoom_h != h);
Gr_full_resize_X = (float)max_w / (float)w;
Gr_full_resize_Y = (float)max_h / (float)h;
Gr_full_center_resize_X = (float)center_w / (float)w;
Gr_full_center_resize_Y = (float)center_h / (float)h;
if (do_zoom) {
float aspect_quotient = ((float)center_w / (float)center_h) / ((float)zoom_w / (float)zoom_h);
Gr_resize_X = (float)center_w / (float)zoom_w / ((aspect_quotient > 1.0f) ? aspect_quotient : 1.0f);
Gr_resize_Y = (float)center_h / (float)zoom_h * ((aspect_quotient < 1.0f) ? aspect_quotient : 1.0f);
Gr_menu_offset_X = ((center_w - w * Gr_resize_X) / 2.0f) + gr_screen.center_offset_x;
Gr_menu_offset_Y = ((center_h - h * Gr_resize_Y) / 2.0f) + gr_screen.center_offset_y;
Gr_menu_zoomed_offset_X = (Gr_menu_offset_X >= 0.0f) ? Gr_menu_offset_X : gr_screen.center_offset_x;
Gr_menu_zoomed_offset_Y = (Gr_menu_offset_Y >= 0.0f) ? Gr_menu_offset_Y : gr_screen.center_offset_y;
if (force_stretch || Cmdline_stretch_menu) {
if (Gr_menu_offset_X > (float)gr_screen.center_offset_x) {
Gr_resize_X = Gr_full_center_resize_X;
Gr_menu_offset_X = Gr_menu_zoomed_offset_X = (float)gr_screen.center_offset_x;
}
if (Gr_menu_offset_Y > (float)gr_screen.center_offset_y) {
Gr_resize_Y = Gr_full_center_resize_Y;
Gr_menu_offset_Y = Gr_menu_zoomed_offset_Y = (float)gr_screen.center_offset_y;
}
}
} else {
if (force_stretch || Cmdline_stretch_menu) {
Gr_resize_X = Gr_full_center_resize_X;
Gr_resize_Y = Gr_full_center_resize_Y;
Gr_menu_offset_X = Gr_menu_zoomed_offset_X = (float)gr_screen.center_offset_x;
Gr_menu_offset_Y = Gr_menu_zoomed_offset_Y = (float)gr_screen.center_offset_y;
} else {
float aspect_quotient = ((float)center_w / (float)center_h) / ((float)w / (float)h);
Gr_resize_X = Gr_full_center_resize_X / ((aspect_quotient > 1.0f) ? aspect_quotient : 1.0f);
Gr_resize_Y = Gr_full_center_resize_Y * ((aspect_quotient < 1.0f) ? aspect_quotient : 1.0f);
Gr_menu_offset_X = Gr_menu_zoomed_offset_X = ((aspect_quotient > 1.0f) ? ((center_w - w * Gr_resize_X) / 2.0f) : 0.0f) + gr_screen.center_offset_x;
Gr_menu_offset_Y = Gr_menu_zoomed_offset_Y = ((aspect_quotient < 1.0f) ? ((center_h - h * Gr_resize_Y) / 2.0f) : 0.0f) + gr_screen.center_offset_y;
}
}
gr_screen.custom_size = (w != max_w || w != center_w || h != max_h || h != center_h);
if (gr_screen.rendering_to_texture == -1) {
gr_screen.max_w_unscaled = w;
gr_screen.max_h_unscaled = h;
if (do_zoom) {
gr_screen.max_w_unscaled_zoomed = gr_screen.max_w_unscaled + fl2i(Gr_menu_offset_X * 2.0f / Gr_resize_X);
gr_screen.max_h_unscaled_zoomed = gr_screen.max_h_unscaled + fl2i(Gr_menu_offset_Y * 2.0f / Gr_resize_Y);
if (gr_screen.max_w_unscaled_zoomed > gr_screen.max_w_unscaled) {
gr_screen.max_w_unscaled_zoomed = gr_screen.max_w_unscaled;
}
if (gr_screen.max_h_unscaled_zoomed > gr_screen.max_h_unscaled) {
gr_screen.max_h_unscaled_zoomed = gr_screen.max_h_unscaled;
}
} else {
gr_screen.max_w_unscaled_zoomed = gr_screen.max_w_unscaled;
gr_screen.max_h_unscaled_zoomed = gr_screen.max_h_unscaled;
}
}
}
void gr_reset_screen_scale()
{
Gr_full_resize_X = Gr_save_full_resize_X;
Gr_full_resize_Y = Gr_save_full_resize_Y;
Gr_full_center_resize_X = Gr_save_full_center_resize_X;
Gr_full_center_resize_Y = Gr_save_full_center_resize_Y;
Gr_resize_X = Gr_save_resize_X;
Gr_resize_Y = Gr_save_resize_Y;
Gr_menu_offset_X = Gr_save_menu_offset_X;
Gr_menu_offset_Y = Gr_save_menu_offset_Y;
Gr_menu_zoomed_offset_X = Gr_save_menu_zoomed_offset_X;
Gr_menu_zoomed_offset_Y = Gr_save_menu_zoomed_offset_Y;
gr_screen.custom_size = Save_custom_screen_size;
if (gr_screen.rendering_to_texture == -1) {
gr_screen.max_w_unscaled = gr_screen.max_w_unscaled_zoomed = (gr_screen.res == GR_1024) ? 1024 : 640;
gr_screen.max_h_unscaled = gr_screen.max_h_unscaled_zoomed = (gr_screen.res == GR_1024) ? 768 : 480;
}
}
/**
* This function is to be called if you wish to scale GR_1024 or GR_640 x and y positions or
* lengths in order to keep the correctly scaled to nonstandard resolutions
*
* @param x X value, can be NULL
* @param y Y value, can be NULL
* @param w width, can be NULL
* @param h height, can be NULL
* @param resize_mode
* @return always true unless error
*/
bool gr_resize_screen_pos(int *x, int *y, int *w, int *h, int resize_mode)
{
if ( resize_mode == GR_RESIZE_NONE || (!gr_screen.custom_size && (gr_screen.rendering_to_texture == -1)) ) {
return false;
}
float xy_tmp = 0.0f;
if ( x ) {
switch (resize_mode) {
case GR_RESIZE_FULL:
xy_tmp = (*x) * Gr_full_resize_X;
break;
case GR_RESIZE_FULL_CENTER:
xy_tmp = (*x) * Gr_full_center_resize_X + (float)gr_screen.center_offset_x;
break;
case GR_RESIZE_MENU:
xy_tmp = (*x) * Gr_resize_X + Gr_menu_offset_X;
break;
case GR_RESIZE_MENU_ZOOMED:
xy_tmp = (*x) * Gr_resize_X + Gr_menu_zoomed_offset_X;
break;
case GR_RESIZE_MENU_NO_OFFSET:
xy_tmp = (*x) * Gr_resize_X;
break;
}
(*x) = fl2ir(xy_tmp);
}
if ( y ) {
switch (resize_mode) {
case GR_RESIZE_FULL:
xy_tmp = (*y) * Gr_full_resize_Y;
break;
case GR_RESIZE_FULL_CENTER:
xy_tmp = (*y) * Gr_full_center_resize_Y + (float)gr_screen.center_offset_y;
break;
case GR_RESIZE_MENU:
xy_tmp = (*y) * Gr_resize_Y + Gr_menu_offset_Y;
break;
case GR_RESIZE_MENU_ZOOMED:
xy_tmp = (*y) * Gr_resize_Y + Gr_menu_zoomed_offset_Y;
break;
case GR_RESIZE_MENU_NO_OFFSET:
xy_tmp = (*y) * Gr_resize_Y;
break;
}
(*y) = fl2ir(xy_tmp);
}
if ( w ) {
switch (resize_mode) {
case GR_RESIZE_FULL:
xy_tmp = (*w) * Gr_full_resize_X;
break;
case GR_RESIZE_FULL_CENTER:
xy_tmp = (*w) * Gr_full_center_resize_X;
break;
case GR_RESIZE_MENU:
case GR_RESIZE_MENU_ZOOMED:
case GR_RESIZE_MENU_NO_OFFSET:
xy_tmp = (*w) * Gr_resize_X;
break;
}
(*w) = fl2ir(xy_tmp);
}
if ( h ) {
switch (resize_mode) {
case GR_RESIZE_FULL:
xy_tmp = (*h) * Gr_full_resize_Y;
break;
case GR_RESIZE_FULL_CENTER:
xy_tmp = (*h) * Gr_full_center_resize_Y;
break;
case GR_RESIZE_MENU:
case GR_RESIZE_MENU_ZOOMED:
case GR_RESIZE_MENU_NO_OFFSET:
xy_tmp = (*h) * Gr_resize_Y;
break;
}
(*h) = fl2ir(xy_tmp);
}
return true;
}
/**
*
* @param x X value, can be NULL
* @param y Y value, can be NULL
* @param w width, can be NULL
* @param h height, can be NULL
* @param resize_mode
* @return always true unless error
*/
bool gr_unsize_screen_pos(int *x, int *y, int *w, int *h, int resize_mode)
{
if ( resize_mode == GR_RESIZE_NONE || resize_mode == GR_RESIZE_REPLACE || (!gr_screen.custom_size && (gr_screen.rendering_to_texture == -1)) ) {
return false;
}
float xy_tmp = 0.0f;
if ( x ) {
switch (resize_mode) {
case GR_RESIZE_FULL:
xy_tmp = (*x) / Gr_full_resize_X;
break;
case GR_RESIZE_FULL_CENTER:
xy_tmp = ((*x) - (float)gr_screen.center_offset_x) / Gr_full_center_resize_X;
break;
case GR_RESIZE_MENU:
xy_tmp = ((*x) - Gr_menu_offset_X) / Gr_resize_X;
break;
case GR_RESIZE_MENU_ZOOMED:
xy_tmp = ((*x) - Gr_menu_zoomed_offset_X) / Gr_resize_X;
break;
case GR_RESIZE_MENU_NO_OFFSET:
xy_tmp = (*x) / Gr_resize_X;
break;
}
(*x) = fl2ir(xy_tmp);
}