| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/Graphics/GeneralWidget.h" |
| 5 | |
| 6 | #include <QCheckBox> |
| 7 | #include <QComboBox> |
| 8 | #include <QGridLayout> |
| 9 | #include <QGroupBox> |
| 10 | #include <QHBoxLayout> |
| 11 | #include <QLabel> |
| 12 | #include <QRadioButton> |
| 13 | #include <QSignalBlocker> |
| 14 | #include <QVBoxLayout> |
| 15 | |
| 16 | #include "Core/Config/GraphicsSettings.h" |
| 17 | #include "Core/Config/MainSettings.h" |
| 18 | #include "Core/ConfigManager.h" |
| 19 | #include "Core/Core.h" |
| 20 | #include "Core/System.h" |
| 21 | |
| 22 | #include "DolphinQt/Config/ConfigControls/ConfigBool.h" |
| 23 | #include "DolphinQt/Config/ConfigControls/ConfigChoice.h" |
| 24 | #include "DolphinQt/Config/ConfigControls/ConfigInteger.h" |
| 25 | #include "DolphinQt/Config/ConfigControls/ConfigRadio.h" |
| 26 | #include "DolphinQt/Config/GameConfigWidget.h" |
| 27 | #include "DolphinQt/Config/Graphics/GraphicsPane.h" |
| 28 | #include "DolphinQt/Config/ToolTipControls/ToolTipComboBox.h" |
| 29 | #include "DolphinQt/QtUtils/ModalMessageBox.h" |
| 30 | #include "DolphinQt/Settings.h" |
| 31 | |
| 32 | #include "VideoCommon/VideoBackendBase.h" |
| 33 | #include "VideoCommon/VideoConfig.h" |
| 34 | |
| 35 | GeneralWidget::GeneralWidget(GraphicsPane* gfx_pane) : m_game_layer{gfx_pane->GetConfigLayer()} |
| 36 | { |
| 37 | CreateWidgets(); |
| 38 | ConnectWidgets(); |
| 39 | AddDescriptions(); |
| 40 | |
| 41 | connect(gfx_pane, &GraphicsPane::BackendChanged, this, &GeneralWidget::OnBackendChanged); |
| 42 | connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, [this](Core::State state) { |
| 43 | OnEmulationStateChanged(state != Core::State::Uninitialized); |
| 44 | }); |
| 45 | OnEmulationStateChanged(!Core::IsUninitialized(Core::System::GetInstance())); |
| 46 | } |
| 47 | |
| 48 | void GeneralWidget::CreateWidgets() |
| 49 | { |
| 50 | auto* main_layout = new QVBoxLayout; |
| 51 | |
| 52 | // Basic Section |
| 53 | auto* m_video_box = new QGroupBox(tr("Basic")); |
| 54 | auto* const video_layout = new QGridLayout{m_video_box}; |
| 55 | |
| 56 | std::vector<std::pair<QString, QString>> options; |
| 57 | for (auto& backend : VideoBackendBase::GetAvailableBackends()) |
| 58 | { |
| 59 | options.push_back(std::make_pair(tr(backend->GetDisplayName().c_str()), |
| 60 | QString::fromStdString(backend->GetConfigName()))); |
| 61 | } |
| 62 | m_backend_combo = new ConfigStringChoice(options, Config::MAIN_GFX_BACKEND, m_game_layer); |
| 63 | m_previous_backend = m_backend_combo->currentIndex(); |
| 64 | |
| 65 | m_aspect_combo = new ConfigChoice({tr("Auto"), tr("Force 16:9"), tr("Force 4:3"), |
| 66 | tr("Stretch to Window"), tr("Custom"), tr("Custom (Stretch)")}, |
| 67 | Config::GFX_ASPECT_RATIO, m_game_layer); |
| 68 | m_custom_aspect_label = new QLabel(tr("Custom Aspect Ratio:")); |
| 69 | constexpr int MAX_CUSTOM_ASPECT_RATIO_RESOLUTION = 10000; |
| 70 | m_custom_aspect_width = new ConfigInteger(1, MAX_CUSTOM_ASPECT_RATIO_RESOLUTION, |
| 71 | Config::GFX_CUSTOM_ASPECT_RATIO_WIDTH, m_game_layer); |
| 72 | m_custom_aspect_height = new ConfigInteger(1, MAX_CUSTOM_ASPECT_RATIO_RESOLUTION, |
| 73 | Config::GFX_CUSTOM_ASPECT_RATIO_HEIGHT, m_game_layer); |
| 74 | ToggleCustomAspectRatio(m_aspect_combo->currentIndex()); |
| 75 | |
| 76 | m_adapter_combo = new ToolTipComboBox; |
| 77 | m_enable_vsync = new ConfigBool(tr("V-Sync"), Config::GFX_VSYNC, m_game_layer); |
| 78 | m_enable_fullscreen = |
| 79 | new ConfigBool(tr("Start in Fullscreen"), Config::MAIN_FULLSCREEN, m_game_layer); |
| 80 | |
| 81 | video_layout->addWidget(new QLabel(tr("Backend:")), 0, 0); |
| 82 | video_layout->addWidget(m_backend_combo, 0, 1, 1, -1); |
| 83 | |
| 84 | video_layout->addWidget(new QLabel(tr("Adapter:")), 1, 0); |
| 85 | video_layout->addWidget(m_adapter_combo, 1, 1, 1, -1); |
| 86 | |
| 87 | video_layout->addWidget(new QLabel(tr("Aspect Ratio:")), 2, 0); |
| 88 | video_layout->addWidget(m_aspect_combo, 2, 1, 1, -1); |
| 89 | |
| 90 | video_layout->addWidget(m_custom_aspect_label, 3, 0); |
| 91 | video_layout->addWidget(m_custom_aspect_width, 3, 1); |
| 92 | video_layout->addWidget(m_custom_aspect_height, 3, 2); |
| 93 | |
| 94 | auto* const basic_grid = new QGridLayout; |
| 95 | video_layout->addLayout(basic_grid, video_layout->rowCount(), 0, 1, -1); |
| 96 | basic_grid->addWidget(m_enable_vsync, 0, 0); |
| 97 | basic_grid->addWidget(m_enable_fullscreen, 0, 1); |
| 98 | |
| 99 | auto* const precision_timing = |
| 100 | new ConfigBool(tr("Precision Frame Timing"), Config::MAIN_PRECISION_FRAME_TIMING); |
| 101 | precision_timing->SetDescription( |
| 102 | tr("Uses high resolution timers and \"busy waiting\" for improved frame pacing." |
| 103 | "<br><br>This will marginally increase power usage." |
| 104 | "<br><br><dolphin_emphasis>If unsure, leave this checked.</dolphin_emphasis>")); |
| 105 | basic_grid->addWidget(precision_timing, 1, 0); |
| 106 | |
| 107 | // Other |
| 108 | auto* m_options_box = new QGroupBox(tr("Other")); |
| 109 | auto* m_options_layout = new QGridLayout(); |
| 110 | |
| 111 | m_autoadjust_window_size = new ConfigBool(tr("Auto-Adjust Window Size"), |
| 112 | Config::MAIN_RENDER_WINDOW_AUTOSIZE, m_game_layer); |
| 113 | m_render_main_window = |
| 114 | new ConfigBool(tr("Render to Main Window"), Config::MAIN_RENDER_TO_MAIN, m_game_layer); |
| 115 | |
| 116 | m_options_box->setLayout(m_options_layout); |
| 117 | |
| 118 | m_options_layout->addWidget(m_render_main_window, 0, 0); |
| 119 | m_options_layout->addWidget(m_autoadjust_window_size, 0, 1); |
| 120 | |
| 121 | // Other |
| 122 | auto* shader_compilation_box = new QGroupBox(tr("Shader Compilation")); |
| 123 | auto* shader_compilation_layout = new QGridLayout(); |
| 124 | |
| 125 | const std::array<const char*, 4> modes = {{ |
| 126 | QT_TR_NOOP("Specialized (Default)"), |
| 127 | QT_TR_NOOP("Exclusive Ubershaders"), |
| 128 | QT_TR_NOOP("Hybrid Ubershaders"), |
| 129 | QT_TR_NOOP("Skip Drawing"), |
| 130 | }}; |
| 131 | for (size_t i = 0; i < modes.size(); i++) |
| 132 | { |
| 133 | m_shader_compilation_mode[i] = new ConfigRadioInt( |
| 134 | tr(modes[i]), Config::GFX_SHADER_COMPILATION_MODE, static_cast<int>(i), m_game_layer); |
| 135 | shader_compilation_layout->addWidget(m_shader_compilation_mode[i], static_cast<int>(i / 2), |
| 136 | static_cast<int>(i % 2)); |
| 137 | } |
| 138 | m_wait_for_shaders = new ConfigBool(tr("Compile Shaders Before Starting"), |
| 139 | Config::GFX_WAIT_FOR_SHADERS_BEFORE_STARTING, m_game_layer); |
| 140 | shader_compilation_layout->addWidget(m_wait_for_shaders); |
| 141 | shader_compilation_box->setLayout(shader_compilation_layout); |
| 142 | |
| 143 | main_layout->addWidget(m_video_box); |
| 144 | main_layout->addWidget(m_options_box); |
| 145 | main_layout->addWidget(shader_compilation_box); |
| 146 | main_layout->addStretch(); |
| 147 | |
| 148 | setLayout(main_layout); |
| 149 | } |
| 150 | |
| 151 | void GeneralWidget::ConnectWidgets() |
| 152 | { |
| 153 | // Video Backend |
| 154 | connect(m_backend_combo, &QComboBox::currentIndexChanged, this, &GeneralWidget::BackendWarning); |
| 155 | connect(m_adapter_combo, &QComboBox::currentIndexChanged, this, [&](int index) { |
| 156 | Config::SetBaseOrCurrent(Config::GFX_ADAPTER, index); |
| 157 | emit BackendChanged(QString::fromStdString(Config::Get(Config::MAIN_GFX_BACKEND))); |
| 158 | }); |
| 159 | connect(m_aspect_combo, &QComboBox::currentIndexChanged, this, |
| 160 | &GeneralWidget::ToggleCustomAspectRatio); |
| 161 | } |
| 162 | |
| 163 | void GeneralWidget::ToggleCustomAspectRatio(int index) |
| 164 | { |
| 165 | const bool is_custom_aspect_ratio = (index == static_cast<int>(AspectMode::Custom)) || |
| 166 | (index == static_cast<int>(AspectMode::CustomStretch)); |
| 167 | m_custom_aspect_label->setHidden(!is_custom_aspect_ratio); |
| 168 | m_custom_aspect_width->setHidden(!is_custom_aspect_ratio); |
| 169 | m_custom_aspect_height->setHidden(!is_custom_aspect_ratio); |
| 170 | } |
| 171 | |
| 172 | void GeneralWidget::BackendWarning() |
| 173 | { |
| 174 | if (Config::GetActiveLayerForConfig(Config::MAIN_GFX_BACKEND) == Config::LayerType::Base) |
| 175 | { |
| 176 | const auto& backends = VideoBackendBase::GetAvailableBackends(); |
| 177 | if (backends.empty()) |
| 178 | { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | const int current_idx = m_backend_combo->currentIndex(); |
| 183 | if (current_idx == -1) |
| 184 | { |
| 185 | // Don't attempt to get the current backend if it doesn't match any available backends. |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | auto warningMessage = backends[current_idx]->GetWarningMessage(); |
| 190 | if (warningMessage) |
| 191 | { |
| 192 | ModalMessageBox confirm_sw(this); |
| 193 | |
| 194 | confirm_sw.setIcon(QMessageBox::Warning); |
| 195 | confirm_sw.setStandardButtons(QMessageBox::Yes | QMessageBox::No); |
| 196 | confirm_sw.setWindowTitle(tr("Confirm backend change")); |
| 197 | confirm_sw.setText(tr(warningMessage->c_str())); |
| 198 | |
| 199 | if (confirm_sw.exec() != QMessageBox::Yes) |
| 200 | { |
| 201 | m_backend_combo->setCurrentIndex(m_previous_backend); |
| 202 | return; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | m_previous_backend = m_backend_combo->currentIndex(); |
| 208 | emit BackendChanged(m_backend_combo->currentData().toString()); |
| 209 | } |
| 210 | |
| 211 | void GeneralWidget::OnEmulationStateChanged(bool running) |
| 212 | { |
| 213 | m_backend_combo->setEnabled(!running); |
| 214 | m_render_main_window->setEnabled(!running); |
| 215 | m_enable_fullscreen->setEnabled(!running); |
| 216 | |
| 217 | const bool supports_adapters = !g_backend_info.Adapters.empty(); |
| 218 | m_adapter_combo->setEnabled(!running && supports_adapters); |
| 219 | |
| 220 | std::string current_backend = m_backend_combo->currentData().toString().toStdString(); |
| 221 | if (Config::Get(Config::MAIN_GFX_BACKEND) != current_backend) |
| 222 | { |
| 223 | { |
| 224 | const QSignalBlocker blocker(m_backend_combo); |
| 225 | m_backend_combo->Load(); |
| 226 | } |
| 227 | emit BackendChanged(QString::fromStdString(Config::Get(Config::MAIN_GFX_BACKEND))); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | void GeneralWidget::AddDescriptions() |
| 232 | { |
| 233 | // We need QObject::tr |
| 234 | static const char TR_BACKEND_DESCRIPTION[] = QT_TR_NOOP( |
| 235 | "Selects which graphics API to use internally.<br><br>The software renderer is extremely " |
| 236 | "slow and only useful for debugging, so any of the other backends are " |
| 237 | "recommended. Different games and different GPUs will behave differently on each " |
| 238 | "backend, so for the best emulation experience it is recommended to try each and " |
| 239 | "select the backend that is least problematic.<br><br><dolphin_emphasis>If unsure, " |
| 240 | "select %1.</dolphin_emphasis>"); |
| 241 | static const char TR_FULLSCREEN_DESCRIPTION[] = |
| 242 | QT_TR_NOOP("Uses the entire screen for rendering.<br><br>If disabled, a " |
| 243 | "render window will be created instead.<br><br><dolphin_emphasis>If " |
| 244 | "unsure, leave this unchecked.</dolphin_emphasis>"); |
| 245 | static const char TR_AUTOSIZE_DESCRIPTION[] = |
| 246 | QT_TR_NOOP("Automatically adjusts the window size to the internal resolution.<br><br>" |
| 247 | "<dolphin_emphasis>If unsure, leave this unchecked.</dolphin_emphasis>"); |
| 248 | static const char TR_RENDER_TO_MAINWINDOW_DESCRIPTION[] = |
| 249 | QT_TR_NOOP("Uses the main Dolphin window for rendering rather than " |
| 250 | "a separate render window.<br><br><dolphin_emphasis>If unsure, leave " |
| 251 | "this unchecked.</dolphin_emphasis>"); |
| 252 | static const char TR_ASPECT_RATIO_DESCRIPTION[] = QT_TR_NOOP( |
| 253 | "Selects which aspect ratio to use for displaying the game." |
| 254 | "<br><br>The aspect ratio of the image sent out by the original consoles varied depending on " |
| 255 | "the game and rarely exactly matched 4:3 or 16:9. Some of the image would be cut off by the " |
| 256 | "edges of the TV, or the image wouldn't fill the TV entirely. By default, Dolphin shows the " |
| 257 | "whole image without distorting its proportions, which means it's normal for the image to " |
| 258 | "not entirely fill your display." |
| 259 | "<br><br><b>Auto</b>: Mimics a TV with either a 4:3 or 16:9 aspect ratio, depending on which " |
| 260 | "type of TV the game seems to be targeting." |
| 261 | "<br><br><b>Force 16:9</b>: Mimics a TV with a 16:9 (widescreen) aspect ratio." |
| 262 | "<br><br><b>Force 4:3</b>: Mimics a TV with a 4:3 aspect ratio." |
| 263 | "<br><br><b>Stretch to Window</b>: Stretches the image to the window size. " |
| 264 | "This will usually distort the image's proportions." |
| 265 | "<br><br><b>Custom</b>: Mimics a TV with the specified aspect ratio. " |
| 266 | "This is mostly intended to be used with aspect ratio cheats/mods." |
| 267 | "<br><br><b>Custom (Stretch)</b>: Similar to `Custom`, but stretches the image to the " |
| 268 | "specified aspect ratio. This will usually distort the image's proportions, and should not " |
| 269 | "be used under normal circumstances." |
| 270 | "<br><br><dolphin_emphasis>If unsure, select Auto.</dolphin_emphasis>"); |
| 271 | static const char TR_VSYNC_DESCRIPTION[] = QT_TR_NOOP( |
| 272 | "Waits for vertical blanks in order to prevent tearing.<br><br>Decreases performance " |
| 273 | "if emulation speed is below 100%.<br><br><dolphin_emphasis>If unsure, leave " |
| 274 | "this " |
| 275 | "unchecked.</dolphin_emphasis>"); |
| 276 | static const char TR_SHADER_COMPILE_SPECIALIZED_DESCRIPTION[] = |
| 277 | QT_TR_NOOP("Ubershaders are never used. Stuttering will occur during shader " |
| 278 | "compilation, but GPU demands are low.<br><br>Recommended for low-end hardware. " |
| 279 | "<br><br><dolphin_emphasis>If unsure, select this mode.</dolphin_emphasis>"); |
| 280 | // The "very powerful GPU" mention below is by 2021 PC GPU standards |
| 281 | static const char TR_SHADER_COMPILE_EXCLUSIVE_UBER_DESCRIPTION[] = QT_TR_NOOP( |
| 282 | "Ubershaders will always be used. Provides a near stutter-free experience at the cost of " |
| 283 | "very high GPU performance requirements.<br><br><dolphin_emphasis>Don't use this unless you " |
| 284 | "encountered stuttering with Hybrid Ubershaders and have a very powerful " |
| 285 | "GPU.</dolphin_emphasis>"); |
| 286 | static const char TR_SHADER_COMPILE_HYBRID_UBER_DESCRIPTION[] = QT_TR_NOOP( |
| 287 | "Ubershaders will be used to prevent stuttering during shader compilation, but " |
| 288 | "specialized shaders will be used when they will not cause stuttering.<br><br>In the " |
| 289 | "best case it eliminates shader compilation stuttering while having minimal " |
| 290 | "performance impact, but results depend on video driver behavior."); |
| 291 | static const char TR_SHADER_COMPILE_SKIP_DRAWING_DESCRIPTION[] = QT_TR_NOOP( |
| 292 | "Prevents shader compilation stuttering by not rendering waiting objects. Can work in " |
| 293 | "scenarios where Ubershaders doesn't, at the cost of introducing visual glitches and broken " |
| 294 | "effects.<br><br><dolphin_emphasis>Not recommended, only use if the other " |
| 295 | "options give poor results.</dolphin_emphasis>"); |
| 296 | static const char TR_SHADER_COMPILE_BEFORE_START_DESCRIPTION[] = |
| 297 | QT_TR_NOOP("Waits for all shaders to finish compiling before starting a game. Enabling this " |
| 298 | "option may reduce stuttering or hitching for a short time after the game is " |
| 299 | "started, at the cost of a longer delay before the game starts. For systems with " |
| 300 | "two or fewer cores, it is recommended to enable this option, as a large shader " |
| 301 | "queue may reduce frame rates.<br><br><dolphin_emphasis>Otherwise, if " |
| 302 | "unsure, leave this unchecked.</dolphin_emphasis>"); |
| 303 | |
| 304 | m_backend_combo->SetTitle(tr("Backend")); |
| 305 | m_backend_combo->SetDescription( |
| 306 | tr(TR_BACKEND_DESCRIPTION) |
| 307 | .arg(QString::fromStdString(VideoBackendBase::GetDefaultBackendDisplayName()))); |
| 308 | |
| 309 | m_adapter_combo->SetTitle(tr("Adapter")); |
| 310 | |
| 311 | m_aspect_combo->SetTitle(tr("Aspect Ratio")); |
| 312 | m_aspect_combo->SetDescription(tr(TR_ASPECT_RATIO_DESCRIPTION)); |
| 313 | |
| 314 | m_custom_aspect_width->SetTitle(tr("Custom Aspect Ratio Width")); |
| 315 | m_custom_aspect_height->SetTitle(tr("Custom Aspect Ratio Height")); |
| 316 | |
| 317 | m_enable_vsync->SetDescription(tr(TR_VSYNC_DESCRIPTION)); |
| 318 | |
| 319 | m_enable_fullscreen->SetDescription(tr(TR_FULLSCREEN_DESCRIPTION)); |
| 320 | |
| 321 | m_autoadjust_window_size->SetDescription(tr(TR_AUTOSIZE_DESCRIPTION)); |
| 322 | |
| 323 | m_render_main_window->SetDescription(tr(TR_RENDER_TO_MAINWINDOW_DESCRIPTION)); |
| 324 | |
| 325 | m_shader_compilation_mode[0]->SetDescription(tr(TR_SHADER_COMPILE_SPECIALIZED_DESCRIPTION)); |
| 326 | |
| 327 | m_shader_compilation_mode[1]->SetDescription(tr(TR_SHADER_COMPILE_EXCLUSIVE_UBER_DESCRIPTION)); |
| 328 | |
| 329 | m_shader_compilation_mode[2]->SetDescription(tr(TR_SHADER_COMPILE_HYBRID_UBER_DESCRIPTION)); |
| 330 | |
| 331 | m_shader_compilation_mode[3]->SetDescription(tr(TR_SHADER_COMPILE_SKIP_DRAWING_DESCRIPTION)); |
| 332 | |
| 333 | m_wait_for_shaders->SetDescription(tr(TR_SHADER_COMPILE_BEFORE_START_DESCRIPTION)); |
| 334 | } |
| 335 | |
| 336 | void GeneralWidget::OnBackendChanged(const QString& backend_name) |
| 337 | { |
| 338 | const QSignalBlocker blocker(m_adapter_combo); |
| 339 | |
| 340 | m_adapter_combo->clear(); |
| 341 | |
| 342 | const auto& adapters = g_backend_info.Adapters; |
| 343 | |
| 344 | for (const auto& adapter : adapters) |
| 345 | m_adapter_combo->addItem(QString::fromStdString(adapter)); |
| 346 | |
| 347 | const bool supports_adapters = !adapters.empty(); |
| 348 | |
| 349 | const int adapter_index = Config::Get(Config::GFX_ADAPTER); |
| 350 | if (adapter_index < m_adapter_combo->count()) |
| 351 | m_adapter_combo->setCurrentIndex(adapter_index); |
| 352 | |
| 353 | m_adapter_combo->setEnabled(supports_adapters && |
| 354 | Core::IsUninitialized(Core::System::GetInstance())); |
| 355 | |
| 356 | static constexpr char TR_ADAPTER_AVAILABLE_DESCRIPTION[] = |
| 357 | QT_TR_NOOP("Selects a hardware adapter to use.<br><br>" |
| 358 | "<dolphin_emphasis>If unsure, select the first one.</dolphin_emphasis>"); |
| 359 | static constexpr char TR_ADAPTER_UNAVAILABLE_DESCRIPTION[] = |
| 360 | QT_TR_NOOP("Selects a hardware adapter to use.<br><br>" |
| 361 | "<dolphin_emphasis>%1 doesn't support this feature.</dolphin_emphasis>"); |
| 362 | |
| 363 | m_adapter_combo->SetDescription(supports_adapters ? |
| 364 | tr(TR_ADAPTER_AVAILABLE_DESCRIPTION) : |
| 365 | tr(TR_ADAPTER_UNAVAILABLE_DESCRIPTION) |
| 366 | .arg(tr(g_video_backend->GetDisplayName().c_str()))); |
| 367 | } |