configure_graphics.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 <utility>
  10. #include <vector>
  11. #include <QBoxLayout>
  12. #include <QCheckBox>
  13. #include <QColorDialog>
  14. #include <QComboBox>
  15. #include <QIcon>
  16. #include <QLabel>
  17. #include <QLineEdit>
  18. #include <QPixmap>
  19. #include <QPushButton>
  20. #include <QSlider>
  21. #include <QStringLiteral>
  22. #include <QtCore/qobjectdefs.h>
  23. #include <qabstractbutton.h>
  24. #include <qboxlayout.h>
  25. #include <qcoreevent.h>
  26. #include <qglobal.h>
  27. #include <qgridlayout.h>
  28. #include <vulkan/vulkan_core.h>
  29. #include "common/common_types.h"
  30. #include "common/dynamic_library.h"
  31. #include "common/logging/log.h"
  32. #include "common/settings.h"
  33. #include "core/core.h"
  34. #include "ui_configure_graphics.h"
  35. #include "yuzu/configuration/configuration_shared.h"
  36. #include "yuzu/configuration/configure_graphics.h"
  37. #include "yuzu/configuration/shared_widget.h"
  38. #include "yuzu/qt_common.h"
  39. #include "yuzu/uisettings.h"
  40. #include "yuzu/vk_device_info.h"
  41. static const std::vector<VkPresentModeKHR> default_present_modes{VK_PRESENT_MODE_IMMEDIATE_KHR,
  42. VK_PRESENT_MODE_FIFO_KHR};
  43. // Converts a setting to a present mode (or vice versa)
  44. static constexpr VkPresentModeKHR VSyncSettingToMode(Settings::VSyncMode mode) {
  45. switch (mode) {
  46. case Settings::VSyncMode::Immediate:
  47. return VK_PRESENT_MODE_IMMEDIATE_KHR;
  48. case Settings::VSyncMode::Mailbox:
  49. return VK_PRESENT_MODE_MAILBOX_KHR;
  50. case Settings::VSyncMode::FIFO:
  51. return VK_PRESENT_MODE_FIFO_KHR;
  52. case Settings::VSyncMode::FIFORelaxed:
  53. return VK_PRESENT_MODE_FIFO_RELAXED_KHR;
  54. default:
  55. return VK_PRESENT_MODE_FIFO_KHR;
  56. }
  57. }
  58. static constexpr Settings::VSyncMode PresentModeToSetting(VkPresentModeKHR mode) {
  59. switch (mode) {
  60. case VK_PRESENT_MODE_IMMEDIATE_KHR:
  61. return Settings::VSyncMode::Immediate;
  62. case VK_PRESENT_MODE_MAILBOX_KHR:
  63. return Settings::VSyncMode::Mailbox;
  64. case VK_PRESENT_MODE_FIFO_KHR:
  65. return Settings::VSyncMode::FIFO;
  66. case VK_PRESENT_MODE_FIFO_RELAXED_KHR:
  67. return Settings::VSyncMode::FIFORelaxed;
  68. default:
  69. return Settings::VSyncMode::FIFO;
  70. }
  71. }
  72. ConfigureGraphics::ConfigureGraphics(
  73. const Core::System& system_, std::vector<VkDeviceInfo::Record>& records_,
  74. const std::function<void()>& expose_compute_option_,
  75. std::shared_ptr<std::forward_list<ConfigurationShared::Tab*>> group,
  76. const ConfigurationShared::TranslationMap& translations_, QWidget* parent)
  77. : ConfigurationShared::Tab(group, parent), ui{std::make_unique<Ui::ConfigureGraphics>()},
  78. records{records_}, expose_compute_option{expose_compute_option_}, system{system_},
  79. translations{translations_} {
  80. vulkan_device = Settings::values.vulkan_device.GetValue();
  81. RetrieveVulkanDevices();
  82. ui->setupUi(this);
  83. SetConfiguration();
  84. for (const auto& device : vulkan_devices) {
  85. vulkan_device_combobox->addItem(device);
  86. }
  87. UpdateBackgroundColorButton(QColor::fromRgb(Settings::values.bg_red.GetValue(),
  88. Settings::values.bg_green.GetValue(),
  89. Settings::values.bg_blue.GetValue()));
  90. UpdateAPILayout();
  91. PopulateVSyncModeSelection(); //< must happen after UpdateAPILayout
  92. // VSync setting needs to be determined after populating the VSync combobox
  93. if (Settings::IsConfiguringGlobal()) {
  94. const auto vsync_mode_setting = Settings::values.vsync_mode.GetValue();
  95. const auto vsync_mode = VSyncSettingToMode(vsync_mode_setting);
  96. int index{};
  97. for (const auto mode : vsync_mode_combobox_enum_map) {
  98. if (mode == vsync_mode) {
  99. break;
  100. }
  101. index++;
  102. }
  103. if (static_cast<unsigned long>(index) < vsync_mode_combobox_enum_map.size()) {
  104. vsync_mode_combobox->setCurrentIndex(index);
  105. }
  106. }
  107. connect(api_combobox, qOverload<int>(&QComboBox::activated), this, [this] {
  108. UpdateAPILayout();
  109. PopulateVSyncModeSelection();
  110. });
  111. connect(vulkan_device_combobox, qOverload<int>(&QComboBox::activated), this,
  112. [this](int device) {
  113. UpdateDeviceSelection(device);
  114. PopulateVSyncModeSelection();
  115. });
  116. connect(shader_backend_combobox, qOverload<int>(&QComboBox::activated), this,
  117. [this](int backend) { UpdateShaderBackendSelection(backend); });
  118. // connect(ui->bg_button, &QPushButton::clicked, this, [this] {
  119. // const QColor new_bg_color = QColorDialog::getColor(bg_color);
  120. // if (!new_bg_color.isValid()) {
  121. // return;
  122. // }
  123. // UpdateBackgroundColorButton(new_bg_color);
  124. // });
  125. // ui->bg_label->setVisible(Settings::IsConfiguringGlobal());
  126. // ui->bg_combobox->setVisible(!Settings::IsConfiguringGlobal());
  127. api_combobox->setEnabled(!UISettings::values.has_broken_vulkan && api_combobox->isEnabled());
  128. ui->api_widget->setEnabled(
  129. (!UISettings::values.has_broken_vulkan || Settings::IsConfiguringGlobal()) &&
  130. ui->api_widget->isEnabled());
  131. }
  132. void ConfigureGraphics::PopulateVSyncModeSelection() {
  133. if (!Settings::IsConfiguringGlobal()) {
  134. return;
  135. }
  136. const Settings::RendererBackend backend{GetCurrentGraphicsBackend()};
  137. if (backend == Settings::RendererBackend::Null) {
  138. vsync_mode_combobox->setEnabled(false);
  139. return;
  140. }
  141. vsync_mode_combobox->setEnabled(true);
  142. const int current_index = //< current selected vsync mode from combobox
  143. vsync_mode_combobox->currentIndex();
  144. const auto current_mode = //< current selected vsync mode as a VkPresentModeKHR
  145. current_index == -1 ? VSyncSettingToMode(Settings::values.vsync_mode.GetValue())
  146. : vsync_mode_combobox_enum_map[current_index];
  147. int index{};
  148. const int device{vulkan_device_combobox->currentIndex()}; //< current selected Vulkan device
  149. const auto& present_modes = //< relevant vector of present modes for the selected device or API
  150. backend == Settings::RendererBackend::Vulkan ? device_present_modes[device]
  151. : default_present_modes;
  152. vsync_mode_combobox->clear();
  153. vsync_mode_combobox_enum_map.clear();
  154. vsync_mode_combobox_enum_map.reserve(present_modes.size());
  155. for (const auto present_mode : present_modes) {
  156. const auto mode_name = TranslateVSyncMode(present_mode, backend);
  157. if (mode_name.isEmpty()) {
  158. continue;
  159. }
  160. vsync_mode_combobox->insertItem(index, mode_name);
  161. vsync_mode_combobox_enum_map.push_back(present_mode);
  162. if (present_mode == current_mode) {
  163. vsync_mode_combobox->setCurrentIndex(index);
  164. }
  165. index++;
  166. }
  167. }
  168. void ConfigureGraphics::UpdateDeviceSelection(int device) {
  169. if (device == -1) {
  170. return;
  171. }
  172. if (GetCurrentGraphicsBackend() == Settings::RendererBackend::Vulkan) {
  173. vulkan_device = device;
  174. }
  175. }
  176. void ConfigureGraphics::UpdateShaderBackendSelection(int backend) {
  177. if (backend == -1) {
  178. return;
  179. }
  180. if (GetCurrentGraphicsBackend() == Settings::RendererBackend::OpenGL) {
  181. shader_backend = static_cast<Settings::ShaderBackend>(backend);
  182. }
  183. }
  184. ConfigureGraphics::~ConfigureGraphics() = default;
  185. void ConfigureGraphics::SetConfiguration() {
  186. const bool runtime_lock = !system.IsPoweredOn();
  187. QLayout* api_layout = ui->api_widget->layout();
  188. QWidget* api_grid_widget = new QWidget(this);
  189. QVBoxLayout* api_grid_layout = new QVBoxLayout(api_grid_widget);
  190. api_grid_layout->setContentsMargins(0, 0, 0, 0);
  191. api_layout->addWidget(api_grid_widget);
  192. QLayout& graphics_layout = *ui->graphics_widget->layout();
  193. std::map<bool, std::map<std::string, QWidget*>> hold_graphics;
  194. std::forward_list<QWidget*> hold_api;
  195. for (const auto setting : Settings::values.linkage.by_category[Settings::Category::Renderer]) {
  196. const auto& setting_label = setting->GetLabel();
  197. ConfigurationShared::Widget* widget = [&]() {
  198. if (setting->Id() == Settings::values.vulkan_device.Id() ||
  199. setting->Id() == Settings::values.shader_backend.Id() ||
  200. setting->Id() == Settings::values.vsync_mode.Id()) {
  201. return new ConfigurationShared::Widget(
  202. setting, translations, this, runtime_lock, apply_funcs,
  203. ConfigurationShared::RequestType::ComboBox, false);
  204. } else if (setting->Id() == Settings::values.fsr_sharpening_slider.Id()) {
  205. return new ConfigurationShared::Widget(
  206. setting, translations, this, runtime_lock, apply_funcs,
  207. ConfigurationShared::RequestType::ReverseSlider, true, 0.5f);
  208. } else if (setting->Id() == Settings::values.use_speed_limit.Id()) {
  209. return new ConfigurationShared::Widget(
  210. setting, translations, this, runtime_lock, apply_funcs,
  211. ConfigurationShared::RequestType::LineEdit, true, 1.0f,
  212. Settings::values.speed_limit.ToString());
  213. } else {
  214. return new ConfigurationShared::Widget(setting, translations, this, runtime_lock,
  215. apply_funcs);
  216. }
  217. }();
  218. if (!widget->Valid()) {
  219. delete widget;
  220. continue;
  221. }
  222. if (setting->Id() == Settings::values.renderer_backend.Id()) {
  223. api_grid_layout->addWidget(widget);
  224. api_combobox = widget->combobox;
  225. api_restore_global_button = widget->restore_button;
  226. if (!Settings::IsConfiguringGlobal()) {
  227. QObject::connect(api_restore_global_button, &QAbstractButton::clicked,
  228. [=](bool) { UpdateAPILayout(); });
  229. // Detach API's restore button and place it where we want
  230. widget->layout()->removeWidget(api_restore_global_button);
  231. api_layout->addWidget(api_restore_global_button);
  232. }
  233. } else if (setting->Id() == Settings::values.vulkan_device.Id()) {
  234. hold_api.push_front(widget);
  235. vulkan_device_combobox = widget->combobox;
  236. vulkan_device_widget = widget;
  237. } else if (setting->Id() == Settings::values.shader_backend.Id()) {
  238. hold_api.push_front(widget);
  239. shader_backend_combobox = widget->combobox;
  240. shader_backend_widget = widget;
  241. } else if (setting->Id() == Settings::values.use_speed_limit.Id()) {
  242. apply_funcs.push_front([setting, widget](bool powered_on) {
  243. if (!setting->RuntimeModfiable() && powered_on) {
  244. return;
  245. }
  246. u16 value = QVariant(widget->line_edit->text()).value<u16>();
  247. auto& speed_limit = Settings::values.speed_limit;
  248. if (Settings::IsConfiguringGlobal()) {
  249. speed_limit.SetValue(value);
  250. } else {
  251. bool using_global = !widget->restore_button->isVisible();
  252. speed_limit.SetGlobal(using_global);
  253. if (!using_global) {
  254. speed_limit.SetValue(value);
  255. }
  256. }
  257. });
  258. hold_graphics[setting->IsEnum()][setting_label] = widget;
  259. } else if (setting->Id() == Settings::values.vsync_mode.Id()) {
  260. vsync_mode_combobox = widget->combobox;
  261. hold_graphics[setting->IsEnum()][setting_label] = widget;
  262. } else {
  263. hold_graphics[setting->IsEnum()][setting_label] = widget;
  264. }
  265. }
  266. for (const auto& [_, settings] : hold_graphics) {
  267. for (const auto& [label, widget] : settings) {
  268. graphics_layout.addWidget(widget);
  269. }
  270. }
  271. for (auto widget : hold_api) {
  272. api_grid_layout->addWidget(widget);
  273. }
  274. }
  275. const QString ConfigureGraphics::TranslateVSyncMode(VkPresentModeKHR mode,
  276. Settings::RendererBackend backend) const {
  277. switch (mode) {
  278. case VK_PRESENT_MODE_IMMEDIATE_KHR:
  279. return backend == Settings::RendererBackend::OpenGL
  280. ? tr("Off")
  281. : QStringLiteral("Immediate (%1)").arg(tr("VSync Off"));
  282. case VK_PRESENT_MODE_MAILBOX_KHR:
  283. return QStringLiteral("Mailbox (%1)").arg(tr("Recommended"));
  284. case VK_PRESENT_MODE_FIFO_KHR:
  285. return backend == Settings::RendererBackend::OpenGL
  286. ? tr("On")
  287. : QStringLiteral("FIFO (%1)").arg(tr("VSync On"));
  288. case VK_PRESENT_MODE_FIFO_RELAXED_KHR:
  289. return QStringLiteral("FIFO Relaxed");
  290. default:
  291. return {};
  292. break;
  293. }
  294. }
  295. void ConfigureGraphics::ApplyConfiguration() {
  296. const bool powered_on = system.IsPoweredOn();
  297. for (const auto& func : apply_funcs) {
  298. func(powered_on);
  299. }
  300. if (Settings::IsConfiguringGlobal()) {
  301. const auto mode = vsync_mode_combobox_enum_map[vsync_mode_combobox->currentIndex()];
  302. const auto vsync_mode = PresentModeToSetting(mode);
  303. Settings::values.vsync_mode.SetValue(vsync_mode);
  304. }
  305. Settings::values.shader_backend.SetGlobal(true);
  306. Settings::values.vulkan_device.SetGlobal(true);
  307. if (!Settings::IsConfiguringGlobal() && api_restore_global_button->isEnabled()) {
  308. auto backend = static_cast<Settings::RendererBackend>(api_combobox->currentIndex());
  309. switch (backend) {
  310. case Settings::RendererBackend::OpenGL:
  311. Settings::values.shader_backend.SetGlobal(false);
  312. Settings::values.shader_backend.SetValue(
  313. static_cast<Settings::ShaderBackend>(shader_backend_combobox->currentIndex()));
  314. break;
  315. case Settings::RendererBackend::Vulkan:
  316. Settings::values.vulkan_device.SetGlobal(false);
  317. Settings::values.vulkan_device.SetValue(vulkan_device_combobox->currentIndex());
  318. break;
  319. case Settings::RendererBackend::Null:
  320. break;
  321. }
  322. }
  323. }
  324. void ConfigureGraphics::changeEvent(QEvent* event) {
  325. if (event->type() == QEvent::LanguageChange) {
  326. RetranslateUI();
  327. }
  328. QWidget::changeEvent(event);
  329. }
  330. void ConfigureGraphics::RetranslateUI() {
  331. ui->retranslateUi(this);
  332. }
  333. void ConfigureGraphics::UpdateBackgroundColorButton(QColor color) {
  334. bg_color = color;
  335. // QPixmap pixmap(ui->bg_button->size());
  336. // pixmap.fill(bg_color);
  337. // const QIcon color_icon(pixmap);
  338. // ui->bg_button->setIcon(color_icon);
  339. }
  340. void ConfigureGraphics::UpdateAPILayout() {
  341. bool runtime_lock = !system.IsPoweredOn();
  342. if (!Settings::IsConfiguringGlobal() && !api_restore_global_button->isEnabled()) {
  343. vulkan_device = Settings::values.vulkan_device.GetValue(true);
  344. shader_backend = Settings::values.shader_backend.GetValue(true);
  345. vulkan_device_widget->setEnabled(false);
  346. shader_backend_widget->setEnabled(false);
  347. } else {
  348. vulkan_device = Settings::values.vulkan_device.GetValue();
  349. shader_backend = Settings::values.shader_backend.GetValue();
  350. vulkan_device_widget->setEnabled(runtime_lock);
  351. shader_backend_widget->setEnabled(runtime_lock);
  352. }
  353. switch (GetCurrentGraphicsBackend()) {
  354. case Settings::RendererBackend::OpenGL:
  355. shader_backend_combobox->setCurrentIndex(static_cast<u32>(shader_backend));
  356. vulkan_device_widget->setVisible(false);
  357. shader_backend_widget->setVisible(true);
  358. break;
  359. case Settings::RendererBackend::Vulkan:
  360. if (static_cast<int>(vulkan_device) < vulkan_device_combobox->count()) {
  361. vulkan_device_combobox->setCurrentIndex(vulkan_device);
  362. }
  363. vulkan_device_widget->setVisible(true);
  364. shader_backend_widget->setVisible(false);
  365. break;
  366. case Settings::RendererBackend::Null:
  367. vulkan_device_widget->setVisible(false);
  368. shader_backend_widget->setVisible(false);
  369. break;
  370. }
  371. }
  372. void ConfigureGraphics::RetrieveVulkanDevices() {
  373. vulkan_devices.clear();
  374. vulkan_devices.reserve(records.size());
  375. device_present_modes.clear();
  376. device_present_modes.reserve(records.size());
  377. for (const auto& record : records) {
  378. vulkan_devices.push_back(QString::fromStdString(record.name));
  379. device_present_modes.push_back(record.vsync_support);
  380. if (record.has_broken_compute) {
  381. expose_compute_option();
  382. }
  383. }
  384. }
  385. Settings::RendererBackend ConfigureGraphics::GetCurrentGraphicsBackend() const {
  386. if (!Settings::IsConfiguringGlobal() && !api_restore_global_button->isEnabled()) {
  387. return Settings::values.renderer_backend.GetValue(true);
  388. }
  389. return static_cast<Settings::RendererBackend>(api_combobox->currentIndex());
  390. }