settings.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <algorithm>
  5. #include <array>
  6. #include <map>
  7. #include <optional>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "common/common_types.h"
  12. #include "common/settings_input.h"
  13. namespace Settings {
  14. enum class RendererBackend : u32 {
  15. OpenGL = 0,
  16. Vulkan = 1,
  17. };
  18. enum class ShaderBackend : u32 {
  19. GLSL = 0,
  20. GLASM = 1,
  21. SPIRV = 2,
  22. };
  23. enum class GPUAccuracy : u32 {
  24. Normal = 0,
  25. High = 1,
  26. Extreme = 2,
  27. };
  28. enum class CPUAccuracy : u32 {
  29. Auto = 0,
  30. Accurate = 1,
  31. Unsafe = 2,
  32. Paranoid = 3,
  33. };
  34. enum class FullscreenMode : u32 {
  35. Borderless = 0,
  36. Exclusive = 1,
  37. };
  38. enum class NvdecEmulation : u32 {
  39. Off = 0,
  40. CPU = 1,
  41. GPU = 2,
  42. };
  43. enum class ResolutionSetup : u32 {
  44. Res1_2X = 0,
  45. Res3_4X = 1,
  46. Res1X = 2,
  47. Res2X = 3,
  48. Res3X = 4,
  49. Res4X = 5,
  50. Res5X = 6,
  51. Res6X = 7,
  52. };
  53. enum class ScalingFilter : u32 {
  54. NearestNeighbor = 0,
  55. Bilinear = 1,
  56. Bicubic = 2,
  57. Gaussian = 3,
  58. ScaleForce = 4,
  59. Fsr = 5,
  60. LastFilter = Fsr,
  61. };
  62. enum class AntiAliasing : u32 {
  63. None = 0,
  64. Fxaa = 1,
  65. LastAA = Fxaa,
  66. };
  67. struct ResolutionScalingInfo {
  68. u32 up_scale{1};
  69. u32 down_shift{0};
  70. f32 up_factor{1.0f};
  71. f32 down_factor{1.0f};
  72. bool active{};
  73. bool downscale{};
  74. s32 ScaleUp(s32 value) const {
  75. if (value == 0) {
  76. return 0;
  77. }
  78. return std::max((value * static_cast<s32>(up_scale)) >> static_cast<s32>(down_shift), 1);
  79. }
  80. u32 ScaleUp(u32 value) const {
  81. if (value == 0U) {
  82. return 0U;
  83. }
  84. return std::max((value * up_scale) >> down_shift, 1U);
  85. }
  86. };
  87. /** The Setting class is a simple resource manager. It defines a label and default value alongside
  88. * the actual value of the setting for simpler and less-error prone use with frontend
  89. * configurations. Specifying a default value and label is required. A minimum and maximum range can
  90. * be specified for sanitization.
  91. */
  92. template <typename Type, bool ranged = false>
  93. class Setting {
  94. protected:
  95. Setting() = default;
  96. /**
  97. * Only sets the setting to the given initializer, leaving the other members to their default
  98. * initializers.
  99. *
  100. * @param global_val Initial value of the setting
  101. */
  102. explicit Setting(const Type& val) : value{val} {}
  103. public:
  104. /**
  105. * Sets a default value, label, and setting value.
  106. *
  107. * @param default_val Intial value of the setting, and default value of the setting
  108. * @param name Label for the setting
  109. */
  110. explicit Setting(const Type& default_val, const std::string& name) requires(!ranged)
  111. : value{default_val}, default_value{default_val}, label{name} {}
  112. virtual ~Setting() = default;
  113. /**
  114. * Sets a default value, minimum value, maximum value, and label.
  115. *
  116. * @param default_val Intial value of the setting, and default value of the setting
  117. * @param min_val Sets the minimum allowed value of the setting
  118. * @param max_val Sets the maximum allowed value of the setting
  119. * @param name Label for the setting
  120. */
  121. explicit Setting(const Type& default_val, const Type& min_val, const Type& max_val,
  122. const std::string& name) requires(ranged)
  123. : value{default_val},
  124. default_value{default_val}, maximum{max_val}, minimum{min_val}, label{name} {}
  125. /**
  126. * Returns a reference to the setting's value.
  127. *
  128. * @returns A reference to the setting
  129. */
  130. [[nodiscard]] virtual const Type& GetValue() const {
  131. return value;
  132. }
  133. /**
  134. * Sets the setting to the given value.
  135. *
  136. * @param val The desired value
  137. */
  138. virtual void SetValue(const Type& val) {
  139. Type temp{ranged ? std::clamp(val, minimum, maximum) : val};
  140. std::swap(value, temp);
  141. }
  142. /**
  143. * Returns the value that this setting was created with.
  144. *
  145. * @returns A reference to the default value
  146. */
  147. [[nodiscard]] const Type& GetDefault() const {
  148. return default_value;
  149. }
  150. /**
  151. * Returns the label this setting was created with.
  152. *
  153. * @returns A reference to the label
  154. */
  155. [[nodiscard]] const std::string& GetLabel() const {
  156. return label;
  157. }
  158. /**
  159. * Assigns a value to the setting.
  160. *
  161. * @param val The desired setting value
  162. *
  163. * @returns A reference to the setting
  164. */
  165. virtual const Type& operator=(const Type& val) {
  166. Type temp{ranged ? std::clamp(val, minimum, maximum) : val};
  167. std::swap(value, temp);
  168. return value;
  169. }
  170. /**
  171. * Returns a reference to the setting.
  172. *
  173. * @returns A reference to the setting
  174. */
  175. explicit virtual operator const Type&() const {
  176. return value;
  177. }
  178. protected:
  179. Type value{}; ///< The setting
  180. const Type default_value{}; ///< The default value
  181. const Type maximum{}; ///< Maximum allowed value of the setting
  182. const Type minimum{}; ///< Minimum allowed value of the setting
  183. const std::string label{}; ///< The setting's label
  184. };
  185. /**
  186. * The SwitchableSetting class is a slightly more complex version of the Setting class. This adds a
  187. * custom setting to switch to when a guest application specifically requires it. The effect is that
  188. * other components of the emulator can access the setting's intended value without any need for the
  189. * component to ask whether the custom or global setting is needed at the moment.
  190. *
  191. * By default, the global setting is used.
  192. */
  193. template <typename Type, bool ranged = false>
  194. class SwitchableSetting : virtual public Setting<Type, ranged> {
  195. public:
  196. /**
  197. * Sets a default value, label, and setting value.
  198. *
  199. * @param default_val Intial value of the setting, and default value of the setting
  200. * @param name Label for the setting
  201. */
  202. explicit SwitchableSetting(const Type& default_val, const std::string& name) requires(!ranged)
  203. : Setting<Type>{default_val, name} {}
  204. virtual ~SwitchableSetting() = default;
  205. /**
  206. * Sets a default value, minimum value, maximum value, and label.
  207. *
  208. * @param default_val Intial value of the setting, and default value of the setting
  209. * @param min_val Sets the minimum allowed value of the setting
  210. * @param max_val Sets the maximum allowed value of the setting
  211. * @param name Label for the setting
  212. */
  213. explicit SwitchableSetting(const Type& default_val, const Type& min_val, const Type& max_val,
  214. const std::string& name) requires(ranged)
  215. : Setting<Type, true>{default_val, min_val, max_val, name} {}
  216. /**
  217. * Tells this setting to represent either the global or custom setting when other member
  218. * functions are used.
  219. *
  220. * @param to_global Whether to use the global or custom setting.
  221. */
  222. void SetGlobal(bool to_global) {
  223. use_global = to_global;
  224. }
  225. /**
  226. * Returns whether this setting is using the global setting or not.
  227. *
  228. * @returns The global state
  229. */
  230. [[nodiscard]] bool UsingGlobal() const {
  231. return use_global;
  232. }
  233. /**
  234. * Returns either the global or custom setting depending on the values of this setting's global
  235. * state or if the global value was specifically requested.
  236. *
  237. * @param need_global Request global value regardless of setting's state; defaults to false
  238. *
  239. * @returns The required value of the setting
  240. */
  241. [[nodiscard]] virtual const Type& GetValue() const override {
  242. if (use_global) {
  243. return this->value;
  244. }
  245. return custom;
  246. }
  247. [[nodiscard]] virtual const Type& GetValue(bool need_global) const {
  248. if (use_global || need_global) {
  249. return this->value;
  250. }
  251. return custom;
  252. }
  253. /**
  254. * Sets the current setting value depending on the global state.
  255. *
  256. * @param val The new value
  257. */
  258. void SetValue(const Type& val) override {
  259. Type temp{ranged ? std::clamp(val, this->minimum, this->maximum) : val};
  260. if (use_global) {
  261. std::swap(this->value, temp);
  262. } else {
  263. std::swap(custom, temp);
  264. }
  265. }
  266. /**
  267. * Assigns the current setting value depending on the global state.
  268. *
  269. * @param val The new value
  270. *
  271. * @returns A reference to the current setting value
  272. */
  273. const Type& operator=(const Type& val) override {
  274. Type temp{ranged ? std::clamp(val, this->minimum, this->maximum) : val};
  275. if (use_global) {
  276. std::swap(this->value, temp);
  277. return this->value;
  278. }
  279. std::swap(custom, temp);
  280. return custom;
  281. }
  282. /**
  283. * Returns the current setting value depending on the global state.
  284. *
  285. * @returns A reference to the current setting value
  286. */
  287. virtual explicit operator const Type&() const override {
  288. if (use_global) {
  289. return this->value;
  290. }
  291. return custom;
  292. }
  293. protected:
  294. bool use_global{true}; ///< The setting's global state
  295. Type custom{}; ///< The custom value of the setting
  296. };
  297. /**
  298. * The InputSetting class allows for getting a reference to either the global or custom members.
  299. * This is required as we cannot easily modify the values of user-defined types within containers
  300. * using the SetValue() member function found in the Setting class. The primary purpose of this
  301. * class is to store an array of 10 PlayerInput structs for both the global and custom setting and
  302. * allows for easily accessing and modifying both settings.
  303. */
  304. template <typename Type>
  305. class InputSetting final {
  306. public:
  307. InputSetting() = default;
  308. explicit InputSetting(Type val) : Setting<Type>(val) {}
  309. ~InputSetting() = default;
  310. void SetGlobal(bool to_global) {
  311. use_global = to_global;
  312. }
  313. [[nodiscard]] bool UsingGlobal() const {
  314. return use_global;
  315. }
  316. [[nodiscard]] Type& GetValue(bool need_global = false) {
  317. if (use_global || need_global) {
  318. return global;
  319. }
  320. return custom;
  321. }
  322. private:
  323. bool use_global{true}; ///< The setting's global state
  324. Type global{}; ///< The setting
  325. Type custom{}; ///< The custom setting value
  326. };
  327. struct TouchFromButtonMap {
  328. std::string name;
  329. std::vector<std::string> buttons;
  330. };
  331. struct Values {
  332. // Audio
  333. Setting<std::string> sink_id{"auto", "output_engine"};
  334. Setting<std::string> audio_output_device_id{"auto", "output_device"};
  335. Setting<std::string> audio_input_device_id{"auto", "input_device"};
  336. Setting<bool> audio_muted{false, "audio_muted"};
  337. SwitchableSetting<u8, true> volume{100, 0, 200, "volume"};
  338. Setting<bool> dump_audio_commands{false, "dump_audio_commands"};
  339. // Core
  340. SwitchableSetting<bool> use_multi_core{true, "use_multi_core"};
  341. SwitchableSetting<bool> use_extended_memory_layout{false, "use_extended_memory_layout"};
  342. // Cpu
  343. SwitchableSetting<CPUAccuracy, true> cpu_accuracy{CPUAccuracy::Auto, CPUAccuracy::Auto,
  344. CPUAccuracy::Paranoid, "cpu_accuracy"};
  345. // TODO: remove cpu_accuracy_first_time, migration setting added 8 July 2021
  346. Setting<bool> cpu_accuracy_first_time{true, "cpu_accuracy_first_time"};
  347. Setting<bool> cpu_debug_mode{false, "cpu_debug_mode"};
  348. Setting<bool> cpuopt_page_tables{true, "cpuopt_page_tables"};
  349. Setting<bool> cpuopt_block_linking{true, "cpuopt_block_linking"};
  350. Setting<bool> cpuopt_return_stack_buffer{true, "cpuopt_return_stack_buffer"};
  351. Setting<bool> cpuopt_fast_dispatcher{true, "cpuopt_fast_dispatcher"};
  352. Setting<bool> cpuopt_context_elimination{true, "cpuopt_context_elimination"};
  353. Setting<bool> cpuopt_const_prop{true, "cpuopt_const_prop"};
  354. Setting<bool> cpuopt_misc_ir{true, "cpuopt_misc_ir"};
  355. Setting<bool> cpuopt_reduce_misalign_checks{true, "cpuopt_reduce_misalign_checks"};
  356. Setting<bool> cpuopt_fastmem{true, "cpuopt_fastmem"};
  357. Setting<bool> cpuopt_fastmem_exclusives{true, "cpuopt_fastmem_exclusives"};
  358. Setting<bool> cpuopt_recompile_exclusives{true, "cpuopt_recompile_exclusives"};
  359. SwitchableSetting<bool> cpuopt_unsafe_unfuse_fma{true, "cpuopt_unsafe_unfuse_fma"};
  360. SwitchableSetting<bool> cpuopt_unsafe_reduce_fp_error{true, "cpuopt_unsafe_reduce_fp_error"};
  361. SwitchableSetting<bool> cpuopt_unsafe_ignore_standard_fpcr{
  362. true, "cpuopt_unsafe_ignore_standard_fpcr"};
  363. SwitchableSetting<bool> cpuopt_unsafe_inaccurate_nan{true, "cpuopt_unsafe_inaccurate_nan"};
  364. SwitchableSetting<bool> cpuopt_unsafe_fastmem_check{true, "cpuopt_unsafe_fastmem_check"};
  365. SwitchableSetting<bool> cpuopt_unsafe_ignore_global_monitor{
  366. true, "cpuopt_unsafe_ignore_global_monitor"};
  367. // Renderer
  368. SwitchableSetting<RendererBackend, true> renderer_backend{
  369. RendererBackend::Vulkan, RendererBackend::OpenGL, RendererBackend::Vulkan, "backend"};
  370. Setting<bool> renderer_debug{false, "debug"};
  371. Setting<bool> renderer_shader_feedback{false, "shader_feedback"};
  372. Setting<bool> enable_nsight_aftermath{false, "nsight_aftermath"};
  373. Setting<bool> disable_shader_loop_safety_checks{false, "disable_shader_loop_safety_checks"};
  374. SwitchableSetting<int> vulkan_device{0, "vulkan_device"};
  375. ResolutionScalingInfo resolution_info{};
  376. SwitchableSetting<ResolutionSetup> resolution_setup{ResolutionSetup::Res1X, "resolution_setup"};
  377. SwitchableSetting<ScalingFilter> scaling_filter{ScalingFilter::Bilinear, "scaling_filter"};
  378. SwitchableSetting<AntiAliasing> anti_aliasing{AntiAliasing::None, "anti_aliasing"};
  379. // *nix platforms may have issues with the borderless windowed fullscreen mode.
  380. // Default to exclusive fullscreen on these platforms for now.
  381. SwitchableSetting<FullscreenMode, true> fullscreen_mode{
  382. #ifdef _WIN32
  383. FullscreenMode::Borderless,
  384. #else
  385. FullscreenMode::Exclusive,
  386. #endif
  387. FullscreenMode::Borderless, FullscreenMode::Exclusive, "fullscreen_mode"};
  388. SwitchableSetting<int, true> aspect_ratio{0, 0, 4, "aspect_ratio"};
  389. SwitchableSetting<int, true> max_anisotropy{0, 0, 5, "max_anisotropy"};
  390. SwitchableSetting<bool> use_speed_limit{true, "use_speed_limit"};
  391. SwitchableSetting<u16, true> speed_limit{100, 0, 9999, "speed_limit"};
  392. SwitchableSetting<bool> use_disk_shader_cache{true, "use_disk_shader_cache"};
  393. SwitchableSetting<GPUAccuracy, true> gpu_accuracy{GPUAccuracy::High, GPUAccuracy::Normal,
  394. GPUAccuracy::Extreme, "gpu_accuracy"};
  395. SwitchableSetting<bool> use_asynchronous_gpu_emulation{true, "use_asynchronous_gpu_emulation"};
  396. SwitchableSetting<NvdecEmulation> nvdec_emulation{NvdecEmulation::GPU, "nvdec_emulation"};
  397. SwitchableSetting<bool> accelerate_astc{true, "accelerate_astc"};
  398. SwitchableSetting<bool> use_vsync{true, "use_vsync"};
  399. SwitchableSetting<ShaderBackend, true> shader_backend{ShaderBackend::GLASM, ShaderBackend::GLSL,
  400. ShaderBackend::SPIRV, "shader_backend"};
  401. SwitchableSetting<bool> use_asynchronous_shaders{false, "use_asynchronous_shaders"};
  402. SwitchableSetting<bool> use_fast_gpu_time{true, "use_fast_gpu_time"};
  403. SwitchableSetting<bool> use_pessimistic_flushes{false, "use_pessimistic_flushes"};
  404. SwitchableSetting<u8> bg_red{0, "bg_red"};
  405. SwitchableSetting<u8> bg_green{0, "bg_green"};
  406. SwitchableSetting<u8> bg_blue{0, "bg_blue"};
  407. // System
  408. SwitchableSetting<std::optional<u32>> rng_seed{std::optional<u32>(), "rng_seed"};
  409. // Measured in seconds since epoch
  410. std::optional<s64> custom_rtc;
  411. // Set on game boot, reset on stop. Seconds difference between current time and `custom_rtc`
  412. s64 custom_rtc_differential;
  413. Setting<s32> current_user{0, "current_user"};
  414. SwitchableSetting<s32, true> language_index{1, 0, 17, "language_index"};
  415. SwitchableSetting<s32, true> region_index{1, 0, 6, "region_index"};
  416. SwitchableSetting<s32, true> time_zone_index{0, 0, 45, "time_zone_index"};
  417. SwitchableSetting<s32, true> sound_index{1, 0, 2, "sound_index"};
  418. // Controls
  419. InputSetting<std::array<PlayerInput, 10>> players;
  420. SwitchableSetting<bool> use_docked_mode{true, "use_docked_mode"};
  421. Setting<bool> enable_raw_input{false, "enable_raw_input"};
  422. Setting<bool> controller_navigation{true, "controller_navigation"};
  423. SwitchableSetting<bool> vibration_enabled{true, "vibration_enabled"};
  424. SwitchableSetting<bool> enable_accurate_vibrations{false, "enable_accurate_vibrations"};
  425. SwitchableSetting<bool> motion_enabled{true, "motion_enabled"};
  426. Setting<std::string> udp_input_servers{"127.0.0.1:26760", "udp_input_servers"};
  427. Setting<bool> enable_udp_controller{false, "enable_udp_controller"};
  428. Setting<bool> pause_tas_on_load{true, "pause_tas_on_load"};
  429. Setting<bool> tas_enable{false, "tas_enable"};
  430. Setting<bool> tas_loop{false, "tas_loop"};
  431. Setting<bool> mouse_panning{false, "mouse_panning"};
  432. Setting<u8, true> mouse_panning_sensitivity{10, 1, 100, "mouse_panning_sensitivity"};
  433. Setting<bool> mouse_enabled{false, "mouse_enabled"};
  434. Setting<bool> emulate_analog_keyboard{false, "emulate_analog_keyboard"};
  435. Setting<bool> keyboard_enabled{false, "keyboard_enabled"};
  436. Setting<bool> debug_pad_enabled{false, "debug_pad_enabled"};
  437. ButtonsRaw debug_pad_buttons;
  438. AnalogsRaw debug_pad_analogs;
  439. TouchscreenInput touchscreen;
  440. Setting<std::string> touch_device{"min_x:100,min_y:50,max_x:1800,max_y:850", "touch_device"};
  441. Setting<int> touch_from_button_map_index{0, "touch_from_button_map"};
  442. std::vector<TouchFromButtonMap> touch_from_button_maps;
  443. Setting<bool> enable_ring_controller{true, "enable_ring_controller"};
  444. RingconRaw ringcon_analogs;
  445. Setting<bool> enable_ir_sensor{false, "enable_ir_sensor"};
  446. Setting<std::string> ir_sensor_device{"auto", "ir_sensor_device"};
  447. // Data Storage
  448. Setting<bool> use_virtual_sd{true, "use_virtual_sd"};
  449. Setting<bool> gamecard_inserted{false, "gamecard_inserted"};
  450. Setting<bool> gamecard_current_game{false, "gamecard_current_game"};
  451. Setting<std::string> gamecard_path{std::string(), "gamecard_path"};
  452. // Debugging
  453. bool record_frame_times;
  454. Setting<bool> use_gdbstub{false, "use_gdbstub"};
  455. Setting<u16> gdbstub_port{6543, "gdbstub_port"};
  456. Setting<std::string> program_args{std::string(), "program_args"};
  457. Setting<bool> dump_exefs{false, "dump_exefs"};
  458. Setting<bool> dump_nso{false, "dump_nso"};
  459. Setting<bool> dump_shaders{false, "dump_shaders"};
  460. Setting<bool> dump_macros{false, "dump_macros"};
  461. Setting<bool> enable_fs_access_log{false, "enable_fs_access_log"};
  462. Setting<bool> reporting_services{false, "reporting_services"};
  463. Setting<bool> quest_flag{false, "quest_flag"};
  464. Setting<bool> disable_macro_jit{false, "disable_macro_jit"};
  465. Setting<bool> extended_logging{false, "extended_logging"};
  466. Setting<bool> use_debug_asserts{false, "use_debug_asserts"};
  467. Setting<bool> use_auto_stub{false, "use_auto_stub"};
  468. Setting<bool> enable_all_controllers{false, "enable_all_controllers"};
  469. Setting<bool> create_crash_dumps{false, "create_crash_dumps"};
  470. Setting<bool> perform_vulkan_check{true, "perform_vulkan_check"};
  471. // Miscellaneous
  472. Setting<std::string> log_filter{"*:Info", "log_filter"};
  473. Setting<bool> use_dev_keys{false, "use_dev_keys"};
  474. // Network
  475. Setting<std::string> network_interface{std::string(), "network_interface"};
  476. // WebService
  477. Setting<bool> enable_telemetry{true, "enable_telemetry"};
  478. Setting<std::string> web_api_url{"https://api.yuzu-emu.org", "web_api_url"};
  479. Setting<std::string> yuzu_username{std::string(), "yuzu_username"};
  480. Setting<std::string> yuzu_token{std::string(), "yuzu_token"};
  481. // Add-Ons
  482. std::map<u64, std::vector<std::string>> disabled_addons;
  483. };
  484. extern Values values;
  485. bool IsConfiguringGlobal();
  486. void SetConfiguringGlobal(bool is_global);
  487. bool IsGPULevelExtreme();
  488. bool IsGPULevelHigh();
  489. bool IsFastmemEnabled();
  490. float Volume();
  491. std::string GetTimeZoneString();
  492. void LogSettings();
  493. void UpdateRescalingInfo();
  494. // Restore the global state of all applicable settings in the Values struct
  495. void RestoreGlobalState(bool is_powered_on);
  496. } // namespace Settings