configure_graphics.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // SPDX-FileCopyrightText: 2016 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <functional>
  5. #include <iosfwd>
  6. #include <iterator>
  7. #include <string>
  8. #include <tuple>
  9. #include <typeinfo>
  10. #include <utility>
  11. #include <vector>
  12. #include <QBoxLayout>
  13. #include <QCheckBox>
  14. #include <QColorDialog>
  15. #include <QComboBox>
  16. #include <QIcon>
  17. #include <QLabel>
  18. #include <QLineEdit>
  19. #include <QPixmap>
  20. #include <QPushButton>
  21. #include <QSlider>
  22. #include <QStringLiteral>
  23. #include <QtCore/qobjectdefs.h>
  24. #include <qabstractbutton.h>
  25. #include <qboxlayout.h>
  26. #include <qcombobox.h>
  27. #include <qcoreevent.h>
  28. #include <qglobal.h>
  29. #include <qgridlayout.h>
  30. #include <vulkan/vulkan_core.h>
  31. #include "common/common_types.h"
  32. #include "common/dynamic_library.h"
  33. #include "common/logging/log.h"
  34. #include "common/settings.h"
  35. #include "common/settings_enums.h"
  36. #include "core/core.h"
  37. #include "ui_configure_graphics.h"
  38. #include "yuzu/configuration/configuration_shared.h"
  39. #include "yuzu/configuration/configure_graphics.h"
  40. #include "yuzu/configuration/shared_widget.h"
  41. #include "yuzu/qt_common.h"
  42. #include "yuzu/uisettings.h"
  43. #include "yuzu/vk_device_info.h"
  44. static const std::vector<VkPresentModeKHR> default_present_modes{VK_PRESENT_MODE_IMMEDIATE_KHR,
  45. VK_PRESENT_MODE_FIFO_KHR};
  46. // Converts a setting to a present mode (or vice versa)
  47. static constexpr VkPresentModeKHR VSyncSettingToMode(Settings::VSyncMode mode) {
  48. switch (mode) {
  49. case Settings::VSyncMode::Immediate:
  50. return VK_PRESENT_MODE_IMMEDIATE_KHR;
  51. case Settings::VSyncMode::Mailbox:
  52. return VK_PRESENT_MODE_MAILBOX_KHR;
  53. case Settings::VSyncMode::Fifo:
  54. return VK_PRESENT_MODE_FIFO_KHR;
  55. case Settings::VSyncMode::FifoRelaxed:
  56. return VK_PRESENT_MODE_FIFO_RELAXED_KHR;
  57. default:
  58. return VK_PRESENT_MODE_FIFO_KHR;
  59. }
  60. }
  61. static constexpr Settings::VSyncMode PresentModeToSetting(VkPresentModeKHR mode) {
  62. switch (mode) {
  63. case VK_PRESENT_MODE_IMMEDIATE_KHR:
  64. return Settings::VSyncMode::Immediate;
  65. case VK_PRESENT_MODE_MAILBOX_KHR:
  66. return Settings::VSyncMode::Mailbox;
  67. case VK_PRESENT_MODE_FIFO_KHR:
  68. return Settings::VSyncMode::Fifo;
  69. case VK_PRESENT_MODE_FIFO_RELAXED_KHR:
  70. return Settings::VSyncMode::FifoRelaxed;
  71. default:
  72. return Settings::VSyncMode::Fifo;
  73. }
  74. }
  75. ConfigureGraphics::ConfigureGraphics(
  76. const Core::System& system_, std::vector<VkDeviceInfo::Record>& records_,
  77. const std::function<void()>& expose_compute_option_,
  78. const std::function<void(Settings::AspectRatio, Settings::ResolutionSetup)>&
  79. update_aspect_ratio_,
  80. std::shared_ptr<std::vector<ConfigurationShared::Tab*>> group_,
  81. const ConfigurationShared::Builder& builder, QWidget* parent)
  82. : ConfigurationShared::Tab(group_, parent), ui{std::make_unique<Ui::ConfigureGraphics>()},
  83. records{records_}, expose_compute_option{expose_compute_option_},
  84. update_aspect_ratio{update_aspect_ratio_}, system{system_},
  85. combobox_translations{builder.ComboboxTranslations()},
  86. shader_mapping{
  87. combobox_translations.at(Settings::EnumMetadata<Settings::ShaderBackend>::Index())} {
  88. vulkan_device = Settings::values.vulkan_device.GetValue();
  89. RetrieveVulkanDevices();
  90. ui->setupUi(this);
  91. Setup(builder);
  92. for (const auto& device : vulkan_devices) {
  93. vulkan_device_combobox->addItem(device);
  94. }
  95. UpdateBackgroundColorButton(QColor::fromRgb(Settings::values.bg_red.GetValue(),
  96. Settings::values.bg_green.GetValue(),
  97. Settings::values.bg_blue.GetValue()));
  98. UpdateAPILayout();
  99. PopulateVSyncModeSelection(); //< must happen after UpdateAPILayout
  100. // VSync setting needs to be determined after populating the VSync combobox
  101. if (Settings::IsConfiguringGlobal()) {
  102. const auto vsync_mode_setting = Settings::values.vsync_mode.GetValue();
  103. const auto vsync_mode = VSyncSettingToMode(vsync_mode_setting);
  104. int index{};
  105. for (const auto mode : vsync_mode_combobox_enum_map) {
  106. if (mode == vsync_mode) {
  107. break;
  108. }
  109. index++;
  110. }
  111. if (static_cast<unsigned long>(index) < vsync_mode_combobox_enum_map.size()) {
  112. vsync_mode_combobox->setCurrentIndex(index);
  113. }
  114. }
  115. connect(api_combobox, qOverload<int>(&QComboBox::activated), this, [this] {
  116. UpdateAPILayout();
  117. PopulateVSyncModeSelection();
  118. });
  119. connect(vulkan_device_combobox, qOverload<int>(&QComboBox::activated), this,
  120. [this](int device) {
  121. UpdateDeviceSelection(device);
  122. PopulateVSyncModeSelection();
  123. });
  124. connect(shader_backend_combobox, qOverload<int>(&QComboBox::activated), this,
  125. [this](int backend) { UpdateShaderBackendSelection(backend); });
  126. connect(ui->bg_button, &QPushButton::clicked, this, [this] {
  127. const QColor new_bg_color = QColorDialog::getColor(bg_color);
  128. if (!new_bg_color.isValid()) {
  129. return;
  130. }
  131. UpdateBackgroundColorButton(new_bg_color);
  132. });
  133. const auto& update_screenshot_info = [this, &builder]() {
  134. const auto& combobox_enumerations = builder.ComboboxTranslations().at(
  135. Settings::EnumMetadata<Settings::AspectRatio>::Index());
  136. const auto index = aspect_ratio_combobox->currentIndex();
  137. const auto ratio = static_cast<Settings::AspectRatio>(combobox_enumerations[index].first);
  138. const auto& combobox_enumerations_resolution = builder.ComboboxTranslations().at(
  139. Settings::EnumMetadata<Settings::ResolutionSetup>::Index());
  140. const auto res_index = resolution_combobox->currentIndex();
  141. const auto setup = static_cast<Settings::ResolutionSetup>(
  142. combobox_enumerations_resolution[res_index].first);
  143. update_aspect_ratio(ratio, setup);
  144. };
  145. connect(aspect_ratio_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged),
  146. update_screenshot_info);
  147. connect(resolution_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged),
  148. update_screenshot_info);
  149. api_combobox->setEnabled(!UISettings::values.has_broken_vulkan && api_combobox->isEnabled());
  150. ui->api_widget->setEnabled(
  151. (!UISettings::values.has_broken_vulkan || Settings::IsConfiguringGlobal()) &&
  152. ui->api_widget->isEnabled());
  153. if (Settings::IsConfiguringGlobal()) {
  154. ui->bg_widget->setEnabled(Settings::values.bg_red.UsingGlobal());
  155. }
  156. }
  157. void ConfigureGraphics::PopulateVSyncModeSelection() {
  158. if (!Settings::IsConfiguringGlobal()) {
  159. return;
  160. }
  161. const Settings::RendererBackend backend{GetCurrentGraphicsBackend()};
  162. if (backend == Settings::RendererBackend::Null) {
  163. vsync_mode_combobox->setEnabled(false);
  164. return;
  165. }
  166. vsync_mode_combobox->setEnabled(true);
  167. const int current_index = //< current selected vsync mode from combobox
  168. vsync_mode_combobox->currentIndex();
  169. const auto current_mode = //< current selected vsync mode as a VkPresentModeKHR
  170. current_index == -1 ? VSyncSettingToMode(Settings::values.vsync_mode.GetValue())
  171. : vsync_mode_combobox_enum_map[current_index];
  172. int index{};
  173. const int device{vulkan_device_combobox->currentIndex()}; //< current selected Vulkan device
  174. if (device == -1) {
  175. // Invalid device
  176. return;
  177. }
  178. const auto& present_modes = //< relevant vector of present modes for the selected device or API
  179. backend == Settings::RendererBackend::Vulkan ? device_present_modes[device]
  180. : default_present_modes;
  181. vsync_mode_combobox->clear();
  182. vsync_mode_combobox_enum_map.clear();
  183. vsync_mode_combobox_enum_map.reserve(present_modes.size());
  184. for (const auto present_mode : present_modes) {
  185. const auto mode_name = TranslateVSyncMode(present_mode, backend);
  186. if (mode_name.isEmpty()) {
  187. continue;
  188. }
  189. vsync_mode_combobox->insertItem(index, mode_name);
  190. vsync_mode_combobox_enum_map.push_back(present_mode);
  191. if (present_mode == current_mode) {
  192. vsync_mode_combobox->setCurrentIndex(index);
  193. }
  194. index++;
  195. }
  196. }
  197. void ConfigureGraphics::UpdateDeviceSelection(int device) {
  198. if (device == -1) {
  199. return;
  200. }
  201. if (GetCurrentGraphicsBackend() == Settings::RendererBackend::Vulkan) {
  202. vulkan_device = device;
  203. }
  204. }
  205. void ConfigureGraphics::UpdateShaderBackendSelection(int backend) {
  206. if (backend == -1) {
  207. return;
  208. }
  209. if (GetCurrentGraphicsBackend() == Settings::RendererBackend::OpenGL) {
  210. shader_backend = static_cast<Settings::ShaderBackend>(backend);
  211. }
  212. }
  213. ConfigureGraphics::~ConfigureGraphics() = default;
  214. void ConfigureGraphics::SetConfiguration() {}
  215. void ConfigureGraphics::Setup(const ConfigurationShared::Builder& builder) {
  216. QLayout* api_layout = ui->api_widget->layout();
  217. QWidget* api_grid_widget = new QWidget(this);
  218. QVBoxLayout* api_grid_layout = new QVBoxLayout(api_grid_widget);
  219. api_grid_layout->setContentsMargins(0, 0, 0, 0);
  220. api_layout->addWidget(api_grid_widget);
  221. QLayout& graphics_layout = *ui->graphics_widget->layout();
  222. std::map<u32, QWidget*> hold_graphics;
  223. std::vector<QWidget*> hold_api;
  224. for (const auto setting : Settings::values.linkage.by_category[Settings::Category::Renderer]) {
  225. ConfigurationShared::Widget* widget = [&]() {
  226. if (setting->Id() == Settings::values.fsr_sharpening_slider.Id()) {
  227. // FSR needs a reversed slider and a 0.5 multiplier
  228. return builder.BuildWidget(
  229. setting, apply_funcs, ConfigurationShared::RequestType::ReverseSlider, true,
  230. 0.5f, nullptr, tr("%", "FSR sharpening percentage (e.g. 50%)"));
  231. } else {
  232. return builder.BuildWidget(setting, apply_funcs);
  233. }
  234. }();
  235. if (widget == nullptr) {
  236. continue;
  237. }
  238. if (!widget->Valid()) {
  239. widget->deleteLater();
  240. continue;
  241. }
  242. if (setting->Id() == Settings::values.renderer_backend.Id()) {
  243. // Add the renderer combobox now so it's at the top
  244. api_grid_layout->addWidget(widget);
  245. api_combobox = widget->combobox;
  246. api_restore_global_button = widget->restore_button;
  247. if (!Settings::IsConfiguringGlobal()) {
  248. QObject::connect(api_restore_global_button, &QAbstractButton::clicked,
  249. [this](bool) { UpdateAPILayout(); });
  250. // Detach API's restore button and place it where we want
  251. // Lets us put it on the side, and it will automatically scale if there's a
  252. // second combobox (shader_backend, vulkan_device)
  253. widget->layout()->removeWidget(api_restore_global_button);
  254. api_layout->addWidget(api_restore_global_button);
  255. }
  256. } else if (setting->Id() == Settings::values.vulkan_device.Id()) {
  257. // Keep track of vulkan_device's combobox so we can populate it
  258. hold_api.push_back(widget);
  259. vulkan_device_combobox = widget->combobox;
  260. vulkan_device_widget = widget;
  261. } else if (setting->Id() == Settings::values.shader_backend.Id()) {
  262. // Keep track of shader_backend's combobox so we can populate it
  263. hold_api.push_back(widget);
  264. shader_backend_combobox = widget->combobox;
  265. shader_backend_widget = widget;
  266. } else if (setting->Id() == Settings::values.vsync_mode.Id()) {
  267. // Keep track of vsync_mode's combobox so we can populate it
  268. vsync_mode_combobox = widget->combobox;
  269. hold_graphics.emplace(setting->Id(), widget);
  270. } else if (setting->Id() == Settings::values.aspect_ratio.Id()) {
  271. // Keep track of the aspect ratio combobox to update other UI tabs that need it
  272. aspect_ratio_combobox = widget->combobox;
  273. hold_graphics.emplace(setting->Id(), widget);
  274. } else if (setting->Id() == Settings::values.resolution_setup.Id()) {
  275. // Keep track of the resolution combobox to update other UI tabs that need it
  276. resolution_combobox = widget->combobox;
  277. hold_graphics.emplace(setting->Id(), widget);
  278. } else {
  279. hold_graphics.emplace(setting->Id(), widget);
  280. }
  281. }
  282. for (const auto& [id, widget] : hold_graphics) {
  283. graphics_layout.addWidget(widget);
  284. }
  285. for (auto widget : hold_api) {
  286. api_grid_layout->addWidget(widget);
  287. }
  288. // Background color is too specific to build into the new system, so we manage it here
  289. // (3 settings, all collected into a single widget with a QColor to manage on top)
  290. if (Settings::IsConfiguringGlobal()) {
  291. apply_funcs.push_back([this](bool powered_on) {
  292. Settings::values.bg_red.SetValue(static_cast<u8>(bg_color.red()));
  293. Settings::values.bg_green.SetValue(static_cast<u8>(bg_color.green()));
  294. Settings::values.bg_blue.SetValue(static_cast<u8>(bg_color.blue()));
  295. });
  296. } else {
  297. QPushButton* bg_restore_button = ConfigurationShared::Widget::CreateRestoreGlobalButton(
  298. Settings::values.bg_red.UsingGlobal(), ui->bg_widget);
  299. ui->bg_widget->layout()->addWidget(bg_restore_button);
  300. QObject::connect(bg_restore_button, &QAbstractButton::clicked,
  301. [bg_restore_button, this](bool) {
  302. const int r = Settings::values.bg_red.GetValue(true);
  303. const int g = Settings::values.bg_green.GetValue(true);
  304. const int b = Settings::values.bg_blue.GetValue(true);
  305. UpdateBackgroundColorButton(QColor::fromRgb(r, g, b));
  306. bg_restore_button->setVisible(false);
  307. bg_restore_button->setEnabled(false);
  308. });
  309. QObject::connect(ui->bg_button, &QAbstractButton::clicked, [bg_restore_button](bool) {
  310. bg_restore_button->setVisible(true);
  311. bg_restore_button->setEnabled(true);
  312. });
  313. apply_funcs.push_back([bg_restore_button, this](bool powered_on) {
  314. const bool using_global = !bg_restore_button->isEnabled();
  315. Settings::values.bg_red.SetGlobal(using_global);
  316. Settings::values.bg_green.SetGlobal(using_global);
  317. Settings::values.bg_blue.SetGlobal(using_global);
  318. if (!using_global) {
  319. Settings::values.bg_red.SetValue(static_cast<u8>(bg_color.red()));
  320. Settings::values.bg_green.SetValue(static_cast<u8>(bg_color.green()));
  321. Settings::values.bg_blue.SetValue(static_cast<u8>(bg_color.blue()));
  322. }
  323. });
  324. }
  325. }
  326. const QString ConfigureGraphics::TranslateVSyncMode(VkPresentModeKHR mode,
  327. Settings::RendererBackend backend) const {
  328. switch (mode) {
  329. case VK_PRESENT_MODE_IMMEDIATE_KHR:
  330. return backend == Settings::RendererBackend::OpenGL
  331. ? tr("Off")
  332. : QStringLiteral("Immediate (%1)").arg(tr("VSync Off"));
  333. case VK_PRESENT_MODE_MAILBOX_KHR:
  334. return QStringLiteral("Mailbox (%1)").arg(tr("Recommended"));
  335. case VK_PRESENT_MODE_FIFO_KHR:
  336. return backend == Settings::RendererBackend::OpenGL
  337. ? tr("On")
  338. : QStringLiteral("FIFO (%1)").arg(tr("VSync On"));
  339. case VK_PRESENT_MODE_FIFO_RELAXED_KHR:
  340. return QStringLiteral("FIFO Relaxed");
  341. default:
  342. return {};
  343. break;
  344. }
  345. }
  346. int ConfigureGraphics::FindIndex(u32 enumeration, int value) const {
  347. for (u32 i = 0; i < combobox_translations.at(enumeration).size(); i++) {
  348. if (combobox_translations.at(enumeration)[i].first == static_cast<u32>(value)) {
  349. return i;
  350. }
  351. }
  352. return -1;
  353. }
  354. void ConfigureGraphics::ApplyConfiguration() {
  355. const bool powered_on = system.IsPoweredOn();
  356. for (const auto& func : apply_funcs) {
  357. func(powered_on);
  358. }
  359. if (Settings::IsConfiguringGlobal()) {
  360. const auto mode = vsync_mode_combobox_enum_map[vsync_mode_combobox->currentIndex()];
  361. const auto vsync_mode = PresentModeToSetting(mode);
  362. Settings::values.vsync_mode.SetValue(vsync_mode);
  363. }
  364. Settings::values.vulkan_device.SetGlobal(true);
  365. Settings::values.shader_backend.SetGlobal(true);
  366. if (Settings::IsConfiguringGlobal() ||
  367. (!Settings::IsConfiguringGlobal() && api_restore_global_button->isEnabled())) {
  368. auto backend = static_cast<Settings::RendererBackend>(
  369. combobox_translations
  370. .at(Settings::EnumMetadata<
  371. Settings::RendererBackend>::Index())[api_combobox->currentIndex()]
  372. .first);
  373. switch (backend) {
  374. case Settings::RendererBackend::OpenGL:
  375. Settings::values.shader_backend.SetGlobal(Settings::IsConfiguringGlobal());
  376. Settings::values.shader_backend.SetValue(static_cast<Settings::ShaderBackend>(
  377. shader_mapping[shader_backend_combobox->currentIndex()].first));
  378. break;
  379. case Settings::RendererBackend::Vulkan:
  380. Settings::values.vulkan_device.SetGlobal(Settings::IsConfiguringGlobal());
  381. Settings::values.vulkan_device.SetValue(vulkan_device_combobox->currentIndex());
  382. break;
  383. case Settings::RendererBackend::Null:
  384. break;
  385. }
  386. }
  387. }
  388. void ConfigureGraphics::changeEvent(QEvent* event) {
  389. if (event->type() == QEvent::LanguageChange) {
  390. RetranslateUI();
  391. }
  392. QWidget::changeEvent(event);
  393. }
  394. void ConfigureGraphics::RetranslateUI() {
  395. ui->retranslateUi(this);
  396. }
  397. void ConfigureGraphics::UpdateBackgroundColorButton(QColor color) {
  398. bg_color = color;
  399. QPixmap pixmap(ui->bg_button->size());
  400. pixmap.fill(bg_color);
  401. const QIcon color_icon(pixmap);
  402. ui->bg_button->setIcon(color_icon);
  403. }
  404. void ConfigureGraphics::UpdateAPILayout() {
  405. bool runtime_lock = !system.IsPoweredOn();
  406. bool need_global = !(Settings::IsConfiguringGlobal() || api_restore_global_button->isEnabled());
  407. vulkan_device = Settings::values.vulkan_device.GetValue(need_global);
  408. shader_backend = Settings::values.shader_backend.GetValue(need_global);
  409. vulkan_device_widget->setEnabled(!need_global && runtime_lock);
  410. shader_backend_widget->setEnabled(!need_global && runtime_lock);
  411. const auto current_backend = GetCurrentGraphicsBackend();
  412. const bool is_opengl = current_backend == Settings::RendererBackend::OpenGL;
  413. const bool is_vulkan = current_backend == Settings::RendererBackend::Vulkan;
  414. vulkan_device_widget->setVisible(is_vulkan);
  415. shader_backend_widget->setVisible(is_opengl);
  416. if (is_opengl) {
  417. shader_backend_combobox->setCurrentIndex(
  418. FindIndex(Settings::EnumMetadata<Settings::ShaderBackend>::Index(),
  419. static_cast<int>(shader_backend)));
  420. } else if (is_vulkan && static_cast<int>(vulkan_device) < vulkan_device_combobox->count()) {
  421. vulkan_device_combobox->setCurrentIndex(vulkan_device);
  422. }
  423. }
  424. void ConfigureGraphics::RetrieveVulkanDevices() {
  425. vulkan_devices.clear();
  426. vulkan_devices.reserve(records.size());
  427. device_present_modes.clear();
  428. device_present_modes.reserve(records.size());
  429. for (const auto& record : records) {
  430. vulkan_devices.push_back(QString::fromStdString(record.name));
  431. device_present_modes.push_back(record.vsync_support);
  432. if (record.has_broken_compute) {
  433. expose_compute_option();
  434. }
  435. }
  436. }
  437. Settings::RendererBackend ConfigureGraphics::GetCurrentGraphicsBackend() const {
  438. if (!Settings::IsConfiguringGlobal() && !api_restore_global_button->isEnabled()) {
  439. return Settings::values.renderer_backend.GetValue(true);
  440. }
  441. return static_cast<Settings::RendererBackend>(
  442. combobox_translations.at(Settings::EnumMetadata<Settings::RendererBackend>::Index())
  443. .at(api_combobox->currentIndex())
  444. .first);
  445. }