| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/Graphics/PostProcessingConfigWindow.h" |
| 5 | |
| 6 | #include <cmath> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include <QCheckBox> |
| 10 | #include <QDialog> |
| 11 | #include <QDialogButtonBox> |
| 12 | #include <QGridLayout> |
| 13 | #include <QLabel> |
| 14 | #include <QLineEdit> |
| 15 | #include <QSlider> |
| 16 | #include <QString> |
| 17 | #include <QTabWidget> |
| 18 | #include <QVBoxLayout> |
| 19 | #include <QWidget> |
| 20 | |
| 21 | #include "DolphinQt/Config/Graphics/EnhancementsWidget.h" |
| 22 | |
| 23 | #include "VideoCommon/PostProcessing.h" |
| 24 | #include "VideoCommon/Present.h" |
| 25 | |
| 26 | using ConfigurationOption = VideoCommon::PostProcessingConfiguration::ConfigurationOption; |
| 27 | using OptionType = ConfigurationOption::OptionType; |
| 28 | |
| 29 | PostProcessingConfigWindow::PostProcessingConfigWindow(EnhancementsWidget* parent, |
| 30 | const std::string& shader) |
| 31 | : QDialog(parent), m_shader(shader) |
| 32 | { |
| 33 | if (g_presenter && g_presenter->GetPostProcessor()) |
| 34 | { |
| 35 | m_post_processor = g_presenter->GetPostProcessor()->GetConfig(); |
| 36 | } |
| 37 | else |
| 38 | { |
| 39 | m_post_processor = new VideoCommon::PostProcessingConfiguration(); |
| 40 | m_post_processor->LoadShader(m_shader); |
| 41 | } |
| 42 | |
| 43 | setWindowTitle(tr("Post-Processing Shader Configuration")); |
| 44 | |
| 45 | PopulateGroups(); |
| 46 | Create(); |
| 47 | ConnectWidgets(); |
| 48 | |
| 49 | adjustSize(); |
| 50 | } |
| 51 | |
| 52 | PostProcessingConfigWindow::~PostProcessingConfigWindow() |
| 53 | { |
| 54 | m_post_processor->SaveOptionsConfiguration(); |
| 55 | if (!(g_presenter && g_presenter->GetPostProcessor())) |
| 56 | { |
| 57 | delete m_post_processor; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void PostProcessingConfigWindow::PopulateGroups() |
| 62 | { |
| 63 | const VideoCommon::PostProcessingConfiguration::ConfigMap& config_map = |
| 64 | m_post_processor->GetOptions(); |
| 65 | |
| 66 | auto config_groups = std::vector<std::unique_ptr<ConfigGroup>>(); |
| 67 | for (const auto& it : config_map) |
| 68 | { |
| 69 | auto config_group = std::make_unique<ConfigGroup>(&it.second); |
| 70 | m_config_map[it.first] = config_group.get(); |
| 71 | config_groups.push_back(std::move(config_group)); |
| 72 | } |
| 73 | |
| 74 | for (auto& config_group : config_groups) |
| 75 | { |
| 76 | const std::string& parent_name = config_group->GetParent(); |
| 77 | if (parent_name.empty()) |
| 78 | { |
| 79 | m_config_groups.emplace_back(std::move(config_group)); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | m_config_map[parent_name]->AddSubGroup(std::move(config_group)); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void PostProcessingConfigWindow::Create() |
| 89 | { |
| 90 | m_tabs = new QTabWidget(); |
| 91 | auto* const general = new QWidget(m_tabs); |
| 92 | auto* const general_layout = new QGridLayout(general); |
| 93 | |
| 94 | u32 row = 0; |
| 95 | bool add_general_page = false; |
| 96 | for (auto& it : m_config_groups) |
| 97 | { |
| 98 | if (it->HasSubGroups()) |
| 99 | { |
| 100 | auto* const tab = CreateDependentTab(it); |
| 101 | m_tabs->addTab(tab, QString::fromStdString(it->GetGUIName())); |
| 102 | |
| 103 | if (it->GetConfigurationOption()->m_type == OptionType::Bool) |
| 104 | { |
| 105 | it->EnableSuboptions(it->GetCheckboxValue()); |
| 106 | } |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | if (!add_general_page) |
| 111 | { |
| 112 | add_general_page = true; |
| 113 | } |
| 114 | row = it->AddWidgets(this, general_layout, row); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (add_general_page) |
| 119 | { |
| 120 | m_tabs->insertTab(0, general, tr("General")); |
| 121 | } |
| 122 | |
| 123 | m_buttons = new QDialogButtonBox(QDialogButtonBox::Ok); |
| 124 | |
| 125 | auto* layout = new QVBoxLayout(this); |
| 126 | layout->addWidget(m_tabs); |
| 127 | layout->addWidget(m_buttons); |
| 128 | } |
| 129 | |
| 130 | void PostProcessingConfigWindow::ConnectWidgets() |
| 131 | { |
| 132 | connect(m_buttons, &QDialogButtonBox::accepted, this, &PostProcessingConfigWindow::accept); |
| 133 | } |
| 134 | |
| 135 | QWidget* |
| 136 | PostProcessingConfigWindow::CreateDependentTab(const std::unique_ptr<ConfigGroup>& config_group) |
| 137 | { |
| 138 | auto* const tab = new QWidget(m_tabs); |
| 139 | auto* const layout = new QGridLayout(tab); |
| 140 | |
| 141 | u32 row = config_group->AddWidgets(this, layout, 0); |
| 142 | for (const auto& child : config_group->GetSubGroups()) |
| 143 | { |
| 144 | row = child->AddWidgets(this, layout, row); |
| 145 | } |
| 146 | |
| 147 | return tab; |
| 148 | } |
| 149 | |
| 150 | void PostProcessingConfigWindow::UpdateBool(ConfigGroup* const config_group, const bool state) |
| 151 | { |
| 152 | m_post_processor->SetOptionb(config_group->GetOptionName(), state); |
| 153 | |
| 154 | config_group->EnableSuboptions(state); |
| 155 | } |
| 156 | |
| 157 | void PostProcessingConfigWindow::UpdateInteger(ConfigGroup* const config_group, const int value) |
| 158 | { |
| 159 | const ConfigurationOption& config_option = |
| 160 | m_post_processor->GetOption(config_group->GetOptionName()); |
| 161 | |
| 162 | const size_t vector_size = config_option.m_integer_values.size(); |
| 163 | |
| 164 | for (size_t i = 0; i < vector_size; ++i) |
| 165 | { |
| 166 | const int current_step = config_group->GetSliderValue(i); |
| 167 | const s32 current_value = config_option.m_integer_step_values[i] * current_step + |
| 168 | config_option.m_integer_min_values[i]; |
| 169 | m_post_processor->SetOptioni(config_option.m_option_name, static_cast<int>(i), current_value); |
| 170 | config_group->SetSliderText(i, QString::number(current_value)); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void PostProcessingConfigWindow::UpdateFloat(ConfigGroup* const config_group, const int value) |
| 175 | { |
| 176 | const ConfigurationOption& config_option = |
| 177 | m_post_processor->GetOption(config_group->GetOptionName()); |
| 178 | |
| 179 | const size_t vector_size = config_option.m_float_values.size(); |
| 180 | |
| 181 | for (size_t i = 0; i < vector_size; ++i) |
| 182 | { |
| 183 | const int current_step = config_group->GetSliderValue(static_cast<unsigned int>(i)); |
| 184 | const float current_value = |
| 185 | config_option.m_float_step_values[i] * current_step + config_option.m_float_min_values[i]; |
| 186 | m_post_processor->SetOptionf(config_option.m_option_name, static_cast<int>(i), current_value); |
| 187 | config_group->SetSliderText(i, QString::asprintf("%f", current_value)); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | PostProcessingConfigWindow::ConfigGroup::ConfigGroup(const ConfigurationOption* config_option) |
| 192 | : m_config_option(config_option) |
| 193 | { |
| 194 | } |
| 195 | |
| 196 | const std::string& PostProcessingConfigWindow::ConfigGroup::GetGUIName() const noexcept |
| 197 | { |
| 198 | return m_config_option->m_gui_name; |
| 199 | } |
| 200 | |
| 201 | const std::string& PostProcessingConfigWindow::ConfigGroup::GetParent() const noexcept |
| 202 | { |
| 203 | return m_config_option->m_dependent_option; |
| 204 | } |
| 205 | |
| 206 | const std::string& PostProcessingConfigWindow::ConfigGroup::GetOptionName() const noexcept |
| 207 | { |
| 208 | return m_config_option->m_option_name; |
| 209 | } |
| 210 | |
| 211 | void PostProcessingConfigWindow::ConfigGroup::AddSubGroup(std::unique_ptr<ConfigGroup>&& subgroup) |
| 212 | { |
| 213 | m_subgroups.emplace_back(std::move(subgroup)); |
| 214 | } |
| 215 | |
| 216 | bool PostProcessingConfigWindow::ConfigGroup::HasSubGroups() const noexcept |
| 217 | { |
| 218 | return !m_subgroups.empty(); |
| 219 | } |
| 220 | |
| 221 | const ConfigurationOption* |
| 222 | PostProcessingConfigWindow::ConfigGroup::GetConfigurationOption() const noexcept |
| 223 | { |
| 224 | return m_config_option; |
| 225 | } |
| 226 | |
| 227 | const std::vector<std::unique_ptr<PostProcessingConfigWindow::ConfigGroup>>& |
| 228 | PostProcessingConfigWindow::ConfigGroup::GetSubGroups() const noexcept |
| 229 | { |
| 230 | return m_subgroups; |
| 231 | } |
| 232 | |
| 233 | u32 PostProcessingConfigWindow::ConfigGroup::AddWidgets(PostProcessingConfigWindow* const parent, |
| 234 | QGridLayout* const grid, const u32 row) |
| 235 | { |
| 236 | auto* const name = new QLabel(QString::fromStdString(m_config_option->m_gui_name)); |
| 237 | grid->addWidget(name, row, 0); |
| 238 | |
| 239 | switch (m_config_option->m_type) |
| 240 | { |
| 241 | case OptionType::Bool: |
| 242 | return AddBool(parent, grid, row); |
| 243 | case OptionType::Float: |
| 244 | return AddFloat(parent, grid, row); |
| 245 | case OptionType::Integer: |
| 246 | return AddInteger(parent, grid, row); |
| 247 | default: |
| 248 | // obviously shouldn't get here |
| 249 | std::abort(); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | u32 PostProcessingConfigWindow::ConfigGroup::AddBool(PostProcessingConfigWindow* const parent, |
| 254 | QGridLayout* const grid, const u32 row) |
| 255 | { |
| 256 | m_checkbox = new QCheckBox(); |
| 257 | m_checkbox->setChecked(m_config_option->m_bool_value); |
| 258 | QObject::connect(m_checkbox, &QCheckBox::toggled, |
| 259 | [this, parent](bool checked) { parent->UpdateBool(this, checked); }); |
| 260 | grid->addWidget(m_checkbox, row, 2); |
| 261 | |
| 262 | return row + 1; |
| 263 | } |
| 264 | |
| 265 | u32 PostProcessingConfigWindow::ConfigGroup::AddInteger(PostProcessingConfigWindow* const parent, |
| 266 | QGridLayout* const grid, u32 row) |
| 267 | { |
| 268 | const size_t vector_size = m_config_option->m_integer_values.size(); |
| 269 | |
| 270 | for (size_t i = 0; i < vector_size; ++i) |
| 271 | { |
| 272 | const double range = |
| 273 | m_config_option->m_integer_max_values[i] - m_config_option->m_integer_min_values[i]; |
| 274 | // "How many steps we have is the range divided by the step interval configured. |
| 275 | // This may not be 100% spot on accurate since developers can have odd stepping intervals |
| 276 | // set. |
| 277 | // Round up so if it is outside our range, then set it to the minimum or maximum" |
| 278 | const int steps = |
| 279 | std::ceil(range / static_cast<double>(m_config_option->m_integer_step_values[i])); |
| 280 | const int current_value = std::round( |
| 281 | (m_config_option->m_integer_values[i] - m_config_option->m_integer_min_values[i]) / |
| 282 | static_cast<double>(m_config_option->m_integer_step_values[i])); |
| 283 | |
| 284 | auto* const slider = new QSlider(Qt::Orientation::Horizontal); |
| 285 | slider->setMinimum(0); |
| 286 | slider->setMaximum(steps); |
| 287 | slider->setValue(current_value); |
| 288 | slider->setTickInterval(range / steps); |
| 289 | QObject::connect(slider, &QSlider::valueChanged, |
| 290 | [this, parent](int value) { parent->UpdateInteger(this, value); }); |
| 291 | |
| 292 | auto* const value_box = new QLineEdit(QString::number(m_config_option->m_integer_values[i])); |
| 293 | value_box->setEnabled(false); |
| 294 | |
| 295 | grid->addWidget(slider, row, 1); |
| 296 | grid->addWidget(value_box, row, 2); |
| 297 | |
| 298 | m_sliders.push_back(slider); |
| 299 | m_value_boxes.push_back(value_box); |
| 300 | if (vector_size > 1) |
| 301 | { |
| 302 | row++; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | return row + 1; |
| 307 | } |
| 308 | |
| 309 | u32 PostProcessingConfigWindow::ConfigGroup::AddFloat(PostProcessingConfigWindow* const parent, |
| 310 | QGridLayout* const grid, u32 row) |
| 311 | { |
| 312 | const size_t vector_size = m_config_option->m_float_values.size(); |
| 313 | |
| 314 | for (size_t i = 0; i < vector_size; ++i) |
| 315 | { |
| 316 | const float range = |
| 317 | m_config_option->m_float_max_values[i] - m_config_option->m_float_min_values[i]; |
| 318 | const int steps = std::ceil(range / m_config_option->m_float_step_values[i]); |
| 319 | const int current_value = |
| 320 | std::round((m_config_option->m_float_values[i] - m_config_option->m_float_min_values[i]) / |
| 321 | m_config_option->m_float_step_values[i]); |
| 322 | |
| 323 | auto* const slider = new QSlider(Qt::Orientation::Horizontal); |
| 324 | slider->setMinimum(0); |
| 325 | slider->setMaximum(steps); |
| 326 | slider->setValue(current_value); |
| 327 | slider->setTickInterval(range / steps); |
| 328 | QObject::connect(slider, &QSlider::valueChanged, |
| 329 | [this, parent](int value) { parent->UpdateFloat(this, value); }); |
| 330 | |
| 331 | auto* const value_box = |
| 332 | new QLineEdit(QString::asprintf("%f", m_config_option->m_float_values[i])); |
| 333 | value_box->setEnabled(false); |
| 334 | |
| 335 | grid->addWidget(slider, row, 1); |
| 336 | grid->addWidget(value_box, row, 2); |
| 337 | |
| 338 | m_sliders.push_back(slider); |
| 339 | m_value_boxes.push_back(value_box); |
| 340 | if (vector_size > 1) |
| 341 | { |
| 342 | row++; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | return row + 1; |
| 347 | } |
| 348 | |
| 349 | void PostProcessingConfigWindow::ConfigGroup::EnableSuboptions(const bool state) |
| 350 | { |
| 351 | for (auto& it : m_subgroups) |
| 352 | { |
| 353 | if (it->m_config_option->m_type == OptionType::Bool) |
| 354 | { |
| 355 | it->m_checkbox->setEnabled(state); |
| 356 | } |
| 357 | else |
| 358 | { |
| 359 | for (auto& slider : it->m_sliders) |
| 360 | { |
| 361 | slider->setEnabled(state); |
| 362 | } |
| 363 | } |
| 364 | it->EnableSuboptions(state); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | int PostProcessingConfigWindow::ConfigGroup::GetCheckboxValue() const |
| 369 | { |
| 370 | return m_checkbox->isChecked(); |
| 371 | } |
| 372 | |
| 373 | int PostProcessingConfigWindow::ConfigGroup::GetSliderValue(size_t index) const |
| 374 | { |
| 375 | return m_sliders[index]->value(); |
| 376 | } |
| 377 | |
| 378 | void PostProcessingConfigWindow::ConfigGroup::SetSliderText(size_t index, const QString& text) |
| 379 | { |
| 380 | m_value_boxes[index]->setText(text); |
| 381 | } |