mirror of https://github.com/stenzek/duckstation
PostProcessing: Refactor config to use separate sections
parent
8db8baf33f
commit
b217f64bcf
@ -1,219 +0,0 @@
|
|||||||
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
|
|
||||||
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
|
||||||
|
|
||||||
#include "postprocessingchainconfigwidget.h"
|
|
||||||
#include "postprocessingshaderconfigwidget.h"
|
|
||||||
#include "qthost.h"
|
|
||||||
|
|
||||||
#include "util/postprocessing_chain.h"
|
|
||||||
|
|
||||||
#include <QtGui/QCursor>
|
|
||||||
#include <QtWidgets/QMenu>
|
|
||||||
#include <QtWidgets/QMessageBox>
|
|
||||||
|
|
||||||
PostProcessingChainConfigWidget::PostProcessingChainConfigWidget(QWidget* parent) : QWidget(parent)
|
|
||||||
{
|
|
||||||
m_ui.setupUi(this);
|
|
||||||
connectUi();
|
|
||||||
updateButtonStates(std::nullopt);
|
|
||||||
}
|
|
||||||
|
|
||||||
PostProcessingChainConfigWidget::~PostProcessingChainConfigWidget() = default;
|
|
||||||
|
|
||||||
void PostProcessingChainConfigWidget::connectUi()
|
|
||||||
{
|
|
||||||
connect(m_ui.add, &QToolButton::clicked, this, &PostProcessingChainConfigWidget::onAddButtonClicked);
|
|
||||||
connect(m_ui.remove, &QToolButton::clicked, this, &PostProcessingChainConfigWidget::onRemoveButtonClicked);
|
|
||||||
connect(m_ui.clear, &QToolButton::clicked, this, &PostProcessingChainConfigWidget::onClearButtonClicked);
|
|
||||||
connect(m_ui.moveUp, &QToolButton::clicked, this, &PostProcessingChainConfigWidget::onMoveUpButtonClicked);
|
|
||||||
connect(m_ui.moveDown, &QToolButton::clicked, this, &PostProcessingChainConfigWidget::onMoveDownButtonClicked);
|
|
||||||
// connect(m_ui.reload, &QToolButton::clicked, this, &PostProcessingChainConfigWidget::onReloadButtonClicked);
|
|
||||||
connect(m_ui.shaderSettings, &QToolButton::clicked, this,
|
|
||||||
&PostProcessingChainConfigWidget::onShaderConfigButtonClicked);
|
|
||||||
connect(m_ui.shaders, &QListWidget::itemSelectionChanged, this,
|
|
||||||
&PostProcessingChainConfigWidget::onSelectedShaderChanged);
|
|
||||||
|
|
||||||
// m_ui.loadPreset->setEnabled(false);
|
|
||||||
// m_ui.savePreset->setEnabled(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PostProcessingChainConfigWidget::setConfigString(const std::string_view& config_string)
|
|
||||||
{
|
|
||||||
if (!m_chain.CreateFromString(config_string))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
updateList();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingChainConfigWidget::setOptionsButtonVisible(bool visible)
|
|
||||||
{
|
|
||||||
if (visible)
|
|
||||||
{
|
|
||||||
m_ui.shaderSettings->setVisible(true);
|
|
||||||
m_ui.horizontalLayout->addWidget(m_ui.shaderSettings);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_ui.shaderSettings->setVisible(false);
|
|
||||||
m_ui.horizontalLayout->removeWidget(m_ui.shaderSettings);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<u32> PostProcessingChainConfigWidget::getSelectedIndex() const
|
|
||||||
{
|
|
||||||
QList<QListWidgetItem*> selected_items = m_ui.shaders->selectedItems();
|
|
||||||
return selected_items.empty() ? std::nullopt :
|
|
||||||
std::optional<u32>(selected_items.first()->data(Qt::UserRole).toUInt());
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingChainConfigWidget::updateList()
|
|
||||||
{
|
|
||||||
m_ui.shaders->clear();
|
|
||||||
|
|
||||||
for (u32 i = 0; i < m_chain.GetStageCount(); i++)
|
|
||||||
{
|
|
||||||
const PostProcessingShader* shader = m_chain.GetShaderStage(i);
|
|
||||||
|
|
||||||
QListWidgetItem* item = new QListWidgetItem(QString::fromStdString(shader->GetName()), m_ui.shaders);
|
|
||||||
item->setData(Qt::UserRole, QVariant(i));
|
|
||||||
}
|
|
||||||
|
|
||||||
updateButtonStates(std::nullopt);
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingChainConfigWidget::configChanged()
|
|
||||||
{
|
|
||||||
if (m_chain.IsEmpty())
|
|
||||||
chainConfigStringChanged(std::string());
|
|
||||||
else
|
|
||||||
chainConfigStringChanged(m_chain.GetConfigString());
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingChainConfigWidget::updateButtonStates(std::optional<u32> index)
|
|
||||||
{
|
|
||||||
m_ui.remove->setEnabled(index.has_value());
|
|
||||||
m_ui.clear->setEnabled(!m_chain.IsEmpty());
|
|
||||||
// m_ui.reload->setEnabled(!m_chain.IsEmpty());
|
|
||||||
m_ui.shaderSettings->setEnabled(index.has_value() && (index.value() < m_chain.GetStageCount()) &&
|
|
||||||
m_chain.GetShaderStage(index.value())->HasOptions());
|
|
||||||
|
|
||||||
if (index.has_value())
|
|
||||||
{
|
|
||||||
m_ui.moveUp->setEnabled(index.value() > 0);
|
|
||||||
m_ui.moveDown->setEnabled(index.value() < (m_chain.GetStageCount() - 1u));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_ui.moveUp->setEnabled(false);
|
|
||||||
m_ui.moveDown->setEnabled(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingChainConfigWidget::onAddButtonClicked()
|
|
||||||
{
|
|
||||||
QMenu menu;
|
|
||||||
|
|
||||||
const std::vector<std::string> shaders(PostProcessingChain::GetAvailableShaderNames());
|
|
||||||
if (shaders.empty())
|
|
||||||
{
|
|
||||||
menu.addAction(tr("No Shaders Available"))->setEnabled(false);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (const std::string& shader : shaders)
|
|
||||||
{
|
|
||||||
QAction* action = menu.addAction(QString::fromStdString(shader));
|
|
||||||
connect(action, &QAction::triggered, [this, &shader]() {
|
|
||||||
chainAboutToChange();
|
|
||||||
|
|
||||||
if (!m_chain.AddStage(shader))
|
|
||||||
{
|
|
||||||
QMessageBox::critical(this, tr("Error"), tr("Failed to add shader. The log may contain more information."));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
updateList();
|
|
||||||
configChanged();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
menu.exec(QCursor::pos());
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingChainConfigWidget::onRemoveButtonClicked()
|
|
||||||
{
|
|
||||||
QList<QListWidgetItem*> selected_items = m_ui.shaders->selectedItems();
|
|
||||||
if (selected_items.empty())
|
|
||||||
return;
|
|
||||||
|
|
||||||
QListWidgetItem* item = selected_items.first();
|
|
||||||
u32 index = item->data(Qt::UserRole).toUInt();
|
|
||||||
if (index < m_chain.GetStageCount())
|
|
||||||
{
|
|
||||||
chainAboutToChange();
|
|
||||||
m_chain.RemoveStage(index);
|
|
||||||
updateList();
|
|
||||||
configChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingChainConfigWidget::onClearButtonClicked()
|
|
||||||
{
|
|
||||||
if (QMessageBox::question(this, tr("Question"), tr("Are you sure you want to clear all shader stages?"),
|
|
||||||
QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
|
|
||||||
{
|
|
||||||
chainAboutToChange();
|
|
||||||
m_chain.ClearStages();
|
|
||||||
updateList();
|
|
||||||
configChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingChainConfigWidget::onMoveUpButtonClicked()
|
|
||||||
{
|
|
||||||
std::optional<u32> index = getSelectedIndex();
|
|
||||||
if (index.has_value())
|
|
||||||
{
|
|
||||||
chainAboutToChange();
|
|
||||||
m_chain.MoveStageUp(index.value());
|
|
||||||
updateList();
|
|
||||||
configChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingChainConfigWidget::onMoveDownButtonClicked()
|
|
||||||
{
|
|
||||||
std::optional<u32> index = getSelectedIndex();
|
|
||||||
if (index.has_value())
|
|
||||||
{
|
|
||||||
chainAboutToChange();
|
|
||||||
m_chain.MoveStageDown(index.value());
|
|
||||||
updateList();
|
|
||||||
configChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingChainConfigWidget::onShaderConfigButtonClicked()
|
|
||||||
{
|
|
||||||
std::optional<u32> index = getSelectedIndex();
|
|
||||||
if (index.has_value() && index.value() < m_chain.GetStageCount())
|
|
||||||
{
|
|
||||||
PostProcessingShaderConfigDialog shader_config(this, m_chain.GetShaderStage(index.value()));
|
|
||||||
connect(&shader_config, &PostProcessingShaderConfigDialog::configChanged, [this]() { configChanged(); });
|
|
||||||
shader_config.exec();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingChainConfigWidget::onReloadButtonClicked()
|
|
||||||
{
|
|
||||||
g_emu_thread->reloadPostProcessingShaders();
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingChainConfigWidget::onSelectedShaderChanged()
|
|
||||||
{
|
|
||||||
std::optional<u32> index = getSelectedIndex();
|
|
||||||
selectedShaderChanged(index.has_value() ? static_cast<s32>(index.value()) : -1);
|
|
||||||
updateButtonStates(index);
|
|
||||||
}
|
|
||||||
@ -1,58 +0,0 @@
|
|||||||
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
|
|
||||||
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "ui_postprocessingchainconfigwidget.h"
|
|
||||||
|
|
||||||
#include "util/postprocessing_chain.h"
|
|
||||||
|
|
||||||
#include "common/types.h"
|
|
||||||
|
|
||||||
#include <QtWidgets/QWidget>
|
|
||||||
#include <memory>
|
|
||||||
#include <optional>
|
|
||||||
#include <string_view>
|
|
||||||
|
|
||||||
namespace PlatformMisc {
|
|
||||||
class PostProcessingChain;
|
|
||||||
}
|
|
||||||
|
|
||||||
class PostProcessingChainConfigWidget : public QWidget
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
public:
|
|
||||||
PostProcessingChainConfigWidget(QWidget* parent);
|
|
||||||
~PostProcessingChainConfigWidget();
|
|
||||||
|
|
||||||
ALWAYS_INLINE PostProcessingChain& getChain() { return m_chain; }
|
|
||||||
|
|
||||||
bool setConfigString(const std::string_view& config_string);
|
|
||||||
void setOptionsButtonVisible(bool visible);
|
|
||||||
|
|
||||||
Q_SIGNALS:
|
|
||||||
void selectedShaderChanged(qint32 index);
|
|
||||||
void chainAboutToChange();
|
|
||||||
void chainConfigStringChanged(const std::string& new_config_string);
|
|
||||||
|
|
||||||
private Q_SLOTS:
|
|
||||||
void onAddButtonClicked();
|
|
||||||
void onRemoveButtonClicked();
|
|
||||||
void onClearButtonClicked();
|
|
||||||
void onMoveUpButtonClicked();
|
|
||||||
void onMoveDownButtonClicked();
|
|
||||||
void onShaderConfigButtonClicked();
|
|
||||||
void onReloadButtonClicked();
|
|
||||||
void onSelectedShaderChanged();
|
|
||||||
|
|
||||||
private:
|
|
||||||
void connectUi();
|
|
||||||
std::optional<u32> getSelectedIndex() const;
|
|
||||||
void updateList();
|
|
||||||
void configChanged();
|
|
||||||
void updateButtonStates(std::optional<u32> index);
|
|
||||||
|
|
||||||
Ui::PostProcessingChainConfigWidget m_ui;
|
|
||||||
|
|
||||||
PostProcessingChain m_chain;
|
|
||||||
};
|
|
||||||
@ -1,163 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<ui version="4.0">
|
|
||||||
<class>PostProcessingChainConfigWidget</class>
|
|
||||||
<widget class="QWidget" name="PostProcessingChainConfigWidget">
|
|
||||||
<property name="geometry">
|
|
||||||
<rect>
|
|
||||||
<x>0</x>
|
|
||||||
<y>0</y>
|
|
||||||
<width>721</width>
|
|
||||||
<height>210</height>
|
|
||||||
</rect>
|
|
||||||
</property>
|
|
||||||
<property name="sizePolicy">
|
|
||||||
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
|
|
||||||
<horstretch>0</horstretch>
|
|
||||||
<verstretch>0</verstretch>
|
|
||||||
</sizepolicy>
|
|
||||||
</property>
|
|
||||||
<property name="windowTitle">
|
|
||||||
<string>Form</string>
|
|
||||||
</property>
|
|
||||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
|
||||||
<item>
|
|
||||||
<widget class="QListWidget" name="shaders">
|
|
||||||
<property name="sizePolicy">
|
|
||||||
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
|
|
||||||
<horstretch>0</horstretch>
|
|
||||||
<verstretch>0</verstretch>
|
|
||||||
</sizepolicy>
|
|
||||||
</property>
|
|
||||||
<property name="minimumSize">
|
|
||||||
<size>
|
|
||||||
<width>0</width>
|
|
||||||
<height>80</height>
|
|
||||||
</size>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<layout class="QVBoxLayout" name="verticalLayout">
|
|
||||||
<item>
|
|
||||||
<widget class="QToolButton" name="add">
|
|
||||||
<property name="sizePolicy">
|
|
||||||
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
|
|
||||||
<horstretch>0</horstretch>
|
|
||||||
<verstretch>0</verstretch>
|
|
||||||
</sizepolicy>
|
|
||||||
</property>
|
|
||||||
<property name="text">
|
|
||||||
<string>Add</string>
|
|
||||||
</property>
|
|
||||||
<property name="icon">
|
|
||||||
<iconset theme="PostProcessingAdd"/>
|
|
||||||
</property>
|
|
||||||
<property name="toolButtonStyle">
|
|
||||||
<enum>Qt::ToolButtonTextBesideIcon</enum>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<widget class="QToolButton" name="remove">
|
|
||||||
<property name="sizePolicy">
|
|
||||||
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
|
|
||||||
<horstretch>0</horstretch>
|
|
||||||
<verstretch>0</verstretch>
|
|
||||||
</sizepolicy>
|
|
||||||
</property>
|
|
||||||
<property name="text">
|
|
||||||
<string>Remove</string>
|
|
||||||
</property>
|
|
||||||
<property name="icon">
|
|
||||||
<iconset theme="PostProcessingRemove"/>
|
|
||||||
</property>
|
|
||||||
<property name="toolButtonStyle">
|
|
||||||
<enum>Qt::ToolButtonTextBesideIcon</enum>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<widget class="QToolButton" name="clear">
|
|
||||||
<property name="sizePolicy">
|
|
||||||
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
|
|
||||||
<horstretch>0</horstretch>
|
|
||||||
<verstretch>0</verstretch>
|
|
||||||
</sizepolicy>
|
|
||||||
</property>
|
|
||||||
<property name="text">
|
|
||||||
<string>Clear</string>
|
|
||||||
</property>
|
|
||||||
<property name="icon">
|
|
||||||
<iconset theme="Clear"/>
|
|
||||||
</property>
|
|
||||||
<property name="toolButtonStyle">
|
|
||||||
<enum>Qt::ToolButtonTextBesideIcon</enum>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<widget class="QToolButton" name="moveUp">
|
|
||||||
<property name="sizePolicy">
|
|
||||||
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
|
|
||||||
<horstretch>0</horstretch>
|
|
||||||
<verstretch>0</verstretch>
|
|
||||||
</sizepolicy>
|
|
||||||
</property>
|
|
||||||
<property name="text">
|
|
||||||
<string>Move Up</string>
|
|
||||||
</property>
|
|
||||||
<property name="icon">
|
|
||||||
<iconset theme="MoveUp"/>
|
|
||||||
</property>
|
|
||||||
<property name="toolButtonStyle">
|
|
||||||
<enum>Qt::ToolButtonTextBesideIcon</enum>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<widget class="QToolButton" name="moveDown">
|
|
||||||
<property name="sizePolicy">
|
|
||||||
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
|
|
||||||
<horstretch>0</horstretch>
|
|
||||||
<verstretch>0</verstretch>
|
|
||||||
</sizepolicy>
|
|
||||||
</property>
|
|
||||||
<property name="text">
|
|
||||||
<string>Move Down</string>
|
|
||||||
</property>
|
|
||||||
<property name="icon">
|
|
||||||
<iconset theme="MoveDown"/>
|
|
||||||
</property>
|
|
||||||
<property name="toolButtonStyle">
|
|
||||||
<enum>Qt::ToolButtonTextBesideIcon</enum>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<widget class="QToolButton" name="shaderSettings">
|
|
||||||
<property name="sizePolicy">
|
|
||||||
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
|
|
||||||
<horstretch>0</horstretch>
|
|
||||||
<verstretch>0</verstretch>
|
|
||||||
</sizepolicy>
|
|
||||||
</property>
|
|
||||||
<property name="text">
|
|
||||||
<string>Options...</string>
|
|
||||||
</property>
|
|
||||||
<property name="icon">
|
|
||||||
<iconset theme="Options"/>
|
|
||||||
</property>
|
|
||||||
<property name="toolButtonStyle">
|
|
||||||
<enum>Qt::ToolButtonTextBesideIcon</enum>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
</layout>
|
|
||||||
</item>
|
|
||||||
</layout>
|
|
||||||
</widget>
|
|
||||||
<resources>
|
|
||||||
<include location="resources/resources.qrc"/>
|
|
||||||
</resources>
|
|
||||||
<connections/>
|
|
||||||
</ui>
|
|
||||||
@ -1,175 +0,0 @@
|
|||||||
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
|
|
||||||
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
|
||||||
|
|
||||||
#include "postprocessingshaderconfigwidget.h"
|
|
||||||
#include <QtWidgets/QCheckBox>
|
|
||||||
#include <QtWidgets/QDialogButtonBox>
|
|
||||||
#include <QtWidgets/QGridLayout>
|
|
||||||
#include <QtWidgets/QLabel>
|
|
||||||
#include <QtWidgets/QSlider>
|
|
||||||
|
|
||||||
PostProcessingShaderConfigWidget::PostProcessingShaderConfigWidget(QWidget* parent,
|
|
||||||
PostProcessingShader* shader)
|
|
||||||
: QWidget(parent), m_shader(shader)
|
|
||||||
{
|
|
||||||
createUi();
|
|
||||||
}
|
|
||||||
|
|
||||||
PostProcessingShaderConfigWidget::~PostProcessingShaderConfigWidget() = default;
|
|
||||||
|
|
||||||
void PostProcessingShaderConfigWidget::createUi()
|
|
||||||
{
|
|
||||||
m_layout = new QGridLayout(this);
|
|
||||||
u32 row = 0;
|
|
||||||
|
|
||||||
for (PostProcessingShader::Option& option : m_shader->GetOptions())
|
|
||||||
{
|
|
||||||
if (option.type == PostProcessingShader::Option::Type::Bool)
|
|
||||||
{
|
|
||||||
QCheckBox* checkbox = new QCheckBox(QString::fromStdString(option.ui_name), this);
|
|
||||||
checkbox->setChecked(option.value[0].int_value != 0);
|
|
||||||
connect(checkbox, &QCheckBox::stateChanged, [this, &option](int state) {
|
|
||||||
option.value[0].int_value = (state == Qt::Checked) ? 1 : 0;
|
|
||||||
configChanged();
|
|
||||||
});
|
|
||||||
connect(this, &PostProcessingShaderConfigWidget::resettingtoDefaults, [&option, checkbox]() {
|
|
||||||
QSignalBlocker sb(checkbox);
|
|
||||||
checkbox->setChecked(option.default_value[0].int_value != 0);
|
|
||||||
option.value = option.default_value;
|
|
||||||
});
|
|
||||||
m_layout->addWidget(checkbox, row, 0, 1, 3, Qt::AlignLeft);
|
|
||||||
row++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (u32 i = 0; i < option.vector_size; i++)
|
|
||||||
{
|
|
||||||
QString label;
|
|
||||||
if (option.vector_size <= 1)
|
|
||||||
{
|
|
||||||
label = QString::fromStdString(option.ui_name);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
static constexpr std::array<const char*, PostProcessingShader::Option::MAX_VECTOR_COMPONENTS + 1> suffixes = {
|
|
||||||
{QT_TR_NOOP("Red"), QT_TR_NOOP("Green"), QT_TR_NOOP("Blue"), QT_TR_NOOP("Alpha")}};
|
|
||||||
label = tr("%1 (%2)").arg(QString::fromStdString(option.ui_name)).arg(tr(suffixes[i]));
|
|
||||||
}
|
|
||||||
|
|
||||||
m_layout->addWidget(new QLabel(label, this), row, 0, 1, 1, Qt::AlignLeft);
|
|
||||||
|
|
||||||
QSlider* slider = new QSlider(Qt::Horizontal, this);
|
|
||||||
m_layout->addWidget(slider, row, 1, 1, 1, Qt::AlignLeft);
|
|
||||||
|
|
||||||
QLabel* slider_label = new QLabel(this);
|
|
||||||
m_layout->addWidget(slider_label, row, 2, 1, 1, Qt::AlignLeft);
|
|
||||||
|
|
||||||
if (option.type == PostProcessingShader::Option::Type::Int)
|
|
||||||
{
|
|
||||||
slider_label->setText(QString::number(option.value[i].int_value));
|
|
||||||
|
|
||||||
const int range = std::max(option.max_value[i].int_value - option.min_value[i].int_value, 1);
|
|
||||||
const int step_value =
|
|
||||||
(option.step_value[i].int_value != 0) ? option.step_value[i].int_value : ((range + 99) / 100);
|
|
||||||
const int num_steps = range / step_value;
|
|
||||||
slider->setMinimum(0);
|
|
||||||
slider->setMaximum(num_steps);
|
|
||||||
slider->setSingleStep(1);
|
|
||||||
slider->setTickInterval(step_value);
|
|
||||||
slider->setValue((option.value[i].int_value - option.min_value[i].int_value) / step_value);
|
|
||||||
connect(slider, &QSlider::valueChanged, [this, &option, i, slider_label](int value) {
|
|
||||||
const int new_value = std::clamp(option.min_value[i].int_value + (value * option.step_value[i].int_value),
|
|
||||||
option.min_value[i].int_value, option.max_value[i].int_value);
|
|
||||||
option.value[i].int_value = new_value;
|
|
||||||
slider_label->setText(QString::number(new_value));
|
|
||||||
configChanged();
|
|
||||||
});
|
|
||||||
connect(this, &PostProcessingShaderConfigWidget::resettingtoDefaults,
|
|
||||||
[&option, i, slider, slider_label, step_value]() {
|
|
||||||
QSignalBlocker sb(slider);
|
|
||||||
slider->setValue((option.default_value[i].int_value - option.min_value[i].int_value) / step_value);
|
|
||||||
slider_label->setText(QString::number(option.default_value[i].int_value));
|
|
||||||
option.value = option.default_value;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
slider_label->setText(QString::number(option.value[i].float_value));
|
|
||||||
|
|
||||||
const float range = std::max(option.max_value[i].float_value - option.min_value[i].float_value, 1.0f);
|
|
||||||
const float step_value =
|
|
||||||
(option.step_value[i].float_value != 0) ? option.step_value[i].float_value : ((range + 99.0f) / 100.0f);
|
|
||||||
const float num_steps = range / step_value;
|
|
||||||
slider->setMinimum(0);
|
|
||||||
slider->setMaximum(num_steps);
|
|
||||||
slider->setSingleStep(1);
|
|
||||||
slider->setTickInterval(step_value);
|
|
||||||
slider->setValue(
|
|
||||||
static_cast<int>((option.value[i].float_value - option.min_value[i].float_value) / step_value));
|
|
||||||
connect(slider, &QSlider::valueChanged, [this, &option, i, slider_label](int value) {
|
|
||||||
const float new_value = std::clamp(option.min_value[i].float_value +
|
|
||||||
(static_cast<float>(value) * option.step_value[i].float_value),
|
|
||||||
option.min_value[i].float_value, option.max_value[i].float_value);
|
|
||||||
option.value[i].float_value = new_value;
|
|
||||||
slider_label->setText(QString::number(new_value));
|
|
||||||
configChanged();
|
|
||||||
});
|
|
||||||
connect(this, &PostProcessingShaderConfigWidget::resettingtoDefaults,
|
|
||||||
[&option, i, slider, slider_label, step_value]() {
|
|
||||||
QSignalBlocker sb(slider);
|
|
||||||
slider->setValue(static_cast<int>(
|
|
||||||
(option.default_value[i].float_value - option.min_value[i].float_value) / step_value));
|
|
||||||
slider_label->setText(QString::number(option.default_value[i].float_value));
|
|
||||||
option.value = option.default_value;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
row++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QDialogButtonBox* button_box = new QDialogButtonBox(QDialogButtonBox::RestoreDefaults, this);
|
|
||||||
connect(button_box, &QDialogButtonBox::clicked, this, &PostProcessingShaderConfigWidget::onResetToDefaultsClicked);
|
|
||||||
m_layout->addWidget(button_box, row, 0, 1, -1);
|
|
||||||
|
|
||||||
row++;
|
|
||||||
m_layout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding), row, 0, 1, 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingShaderConfigWidget::onResetToDefaultsClicked()
|
|
||||||
{
|
|
||||||
resettingtoDefaults();
|
|
||||||
configChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
PostProcessingShaderConfigDialog::PostProcessingShaderConfigDialog(QWidget* parent,
|
|
||||||
PostProcessingShader* shader)
|
|
||||||
: QDialog(parent)
|
|
||||||
{
|
|
||||||
setWindowTitle(tr("%1 Shader Options").arg(QString::fromStdString(shader->GetName())));
|
|
||||||
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
|
||||||
|
|
||||||
QGridLayout* layout = new QGridLayout(this);
|
|
||||||
m_widget = new PostProcessingShaderConfigWidget(this, shader);
|
|
||||||
layout->addWidget(m_widget);
|
|
||||||
|
|
||||||
connect(m_widget, &PostProcessingShaderConfigWidget::configChanged, this,
|
|
||||||
&PostProcessingShaderConfigDialog::onConfigChanged);
|
|
||||||
|
|
||||||
QDialogButtonBox* button_box = new QDialogButtonBox(QDialogButtonBox::Close, this);
|
|
||||||
connect(button_box, &QDialogButtonBox::rejected, this, &PostProcessingShaderConfigDialog::onCloseClicked);
|
|
||||||
m_widget->getLayout()->addWidget(button_box, m_widget->getLayout()->rowCount() - 1, 2, 1, 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
PostProcessingShaderConfigDialog::~PostProcessingShaderConfigDialog() = default;
|
|
||||||
|
|
||||||
void PostProcessingShaderConfigDialog::onConfigChanged()
|
|
||||||
{
|
|
||||||
configChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingShaderConfigDialog::onCloseClicked()
|
|
||||||
{
|
|
||||||
done(0);
|
|
||||||
}
|
|
||||||
@ -1,55 +0,0 @@
|
|||||||
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
|
|
||||||
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "util/postprocessing_shader.h"
|
|
||||||
|
|
||||||
#include <QtWidgets/QDialog>
|
|
||||||
#include <QtWidgets/QWidget>
|
|
||||||
|
|
||||||
class QGridLayout;
|
|
||||||
|
|
||||||
class PostProcessingShaderConfigWidget : public QWidget
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
public:
|
|
||||||
PostProcessingShaderConfigWidget(QWidget* parent, PostProcessingShader* shader);
|
|
||||||
~PostProcessingShaderConfigWidget();
|
|
||||||
|
|
||||||
QGridLayout* getLayout() { return m_layout; }
|
|
||||||
|
|
||||||
Q_SIGNALS:
|
|
||||||
void configChanged();
|
|
||||||
void resettingtoDefaults();
|
|
||||||
|
|
||||||
private Q_SLOTS:
|
|
||||||
void onResetToDefaultsClicked();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void createUi();
|
|
||||||
|
|
||||||
PostProcessingShader* m_shader;
|
|
||||||
QGridLayout* m_layout;
|
|
||||||
};
|
|
||||||
|
|
||||||
class PostProcessingShaderConfigDialog : public QDialog
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
public:
|
|
||||||
PostProcessingShaderConfigDialog(QWidget* parent, PostProcessingShader* shader);
|
|
||||||
~PostProcessingShaderConfigDialog();
|
|
||||||
|
|
||||||
Q_SIGNALS:
|
|
||||||
void configChanged();
|
|
||||||
|
|
||||||
private Q_SLOTS:
|
|
||||||
void onConfigChanged();
|
|
||||||
void onCloseClicked();
|
|
||||||
|
|
||||||
private:
|
|
||||||
PostProcessingShaderConfigWidget* m_widget;
|
|
||||||
};
|
|
||||||
|
|
||||||
@ -0,0 +1,677 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
|
||||||
|
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
||||||
|
|
||||||
|
#include "postprocessing.h"
|
||||||
|
#include "gpu_device.h"
|
||||||
|
#include "host.h"
|
||||||
|
#include "imgui_manager.h"
|
||||||
|
#include "postprocessing_shader.h"
|
||||||
|
#include "postprocessing_shader_glsl.h"
|
||||||
|
|
||||||
|
// TODO: Remove me
|
||||||
|
#include "core/host.h"
|
||||||
|
#include "core/settings.h"
|
||||||
|
|
||||||
|
#include "IconsFontAwesome5.h"
|
||||||
|
#include "common/assert.h"
|
||||||
|
#include "common/error.h"
|
||||||
|
#include "common/file_system.h"
|
||||||
|
#include "common/log.h"
|
||||||
|
#include "common/path.h"
|
||||||
|
#include "common/string.h"
|
||||||
|
#include "common/string_util.h"
|
||||||
|
#include "common/timer.h"
|
||||||
|
#include "fmt/format.h"
|
||||||
|
|
||||||
|
Log_SetChannel(PostProcessing);
|
||||||
|
|
||||||
|
// TODO: ProgressCallbacks for shader compiling, it can be a bit slow.
|
||||||
|
// TODO: buffer width/height is wrong on resize, need to change it somehow.
|
||||||
|
|
||||||
|
namespace PostProcessing {
|
||||||
|
template<typename T>
|
||||||
|
static u32 ParseVector(const std::string_view& line, ShaderOption::ValueVector* values);
|
||||||
|
|
||||||
|
static TinyString ValueToString(ShaderOption::Type type, u32 vector_size, const ShaderOption::ValueVector& value);
|
||||||
|
|
||||||
|
static TinyString GetStageConfigSection(u32 index);
|
||||||
|
static void CopyStageConfig(SettingsInterface& si, u32 old_index, u32 new_index);
|
||||||
|
static void SwapStageConfig(SettingsInterface& si, u32 lhs_index, u32 rhs_index);
|
||||||
|
static std::unique_ptr<Shader> TryLoadingShader(const std::string& shader_name, bool only_config, Error* error);
|
||||||
|
static void ClearStagesWithError(const Error& error);
|
||||||
|
static SettingsInterface& GetLoadSettingsInterface();
|
||||||
|
static void LoadStages();
|
||||||
|
static void DestroyTextures();
|
||||||
|
|
||||||
|
static std::vector<std::unique_ptr<PostProcessing::Shader>> s_stages;
|
||||||
|
static bool s_enabled = false;
|
||||||
|
|
||||||
|
static GPUTexture::Format s_target_format = GPUTexture::Format::Unknown;
|
||||||
|
static u32 s_target_width = 0;
|
||||||
|
static u32 s_target_height = 0;
|
||||||
|
static Common::Timer s_timer;
|
||||||
|
|
||||||
|
static std::unique_ptr<GPUTexture> s_input_texture;
|
||||||
|
static std::unique_ptr<GPUFramebuffer> s_input_framebuffer;
|
||||||
|
|
||||||
|
static std::unique_ptr<GPUTexture> s_output_texture;
|
||||||
|
static std::unique_ptr<GPUFramebuffer> s_output_framebuffer;
|
||||||
|
|
||||||
|
static std::unordered_map<u64, std::unique_ptr<GPUSampler>> s_samplers;
|
||||||
|
static std::unique_ptr<GPUTexture> s_dummy_texture;
|
||||||
|
} // namespace PostProcessing
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
u32 PostProcessing::ParseVector(const std::string_view& line, ShaderOption::ValueVector* values)
|
||||||
|
{
|
||||||
|
u32 index = 0;
|
||||||
|
size_t start = 0;
|
||||||
|
while (index < PostProcessing::ShaderOption::MAX_VECTOR_COMPONENTS)
|
||||||
|
{
|
||||||
|
while (start < line.size() && std::isspace(line[start]))
|
||||||
|
start++;
|
||||||
|
|
||||||
|
if (start >= line.size())
|
||||||
|
break;
|
||||||
|
|
||||||
|
size_t end = line.find(',', start);
|
||||||
|
if (end == std::string_view::npos)
|
||||||
|
end = line.size();
|
||||||
|
|
||||||
|
const std::string_view component = line.substr(start, end - start);
|
||||||
|
T value = StringUtil::FromChars<T>(component).value_or(static_cast<T>(0));
|
||||||
|
if constexpr (std::is_same_v<T, float>)
|
||||||
|
(*values)[index++].float_value = value;
|
||||||
|
else if constexpr (std::is_same_v<T, s32>)
|
||||||
|
(*values)[index++].int_value = value;
|
||||||
|
|
||||||
|
start = end + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const u32 size = index;
|
||||||
|
|
||||||
|
for (; index < PostProcessing::ShaderOption::MAX_VECTOR_COMPONENTS; index++)
|
||||||
|
{
|
||||||
|
if constexpr (std::is_same_v<T, float>)
|
||||||
|
(*values)[index++].float_value = 0.0f;
|
||||||
|
else if constexpr (std::is_same_v<T, s32>)
|
||||||
|
(*values)[index++].int_value = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 PostProcessing::ShaderOption::ParseFloatVector(const std::string_view& line, ValueVector* values)
|
||||||
|
{
|
||||||
|
return ParseVector<float>(line, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 PostProcessing::ShaderOption::ParseIntVector(const std::string_view& line, ValueVector* values)
|
||||||
|
{
|
||||||
|
return ParseVector<s32>(line, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
TinyString PostProcessing::ValueToString(ShaderOption::Type type, u32 vector_size,
|
||||||
|
const ShaderOption::ValueVector& value)
|
||||||
|
{
|
||||||
|
TinyString ret;
|
||||||
|
|
||||||
|
for (u32 i = 0; i < vector_size; i++)
|
||||||
|
{
|
||||||
|
if (i > 0)
|
||||||
|
ret.AppendCharacter(',');
|
||||||
|
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case ShaderOption::Type::Bool:
|
||||||
|
ret.AppendString((value[i].int_value != 0) ? "true" : "false");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ShaderOption::Type::Int:
|
||||||
|
ret.AppendFmtString("{}", value[i].int_value);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ShaderOption::Type::Float:
|
||||||
|
ret.AppendFmtString("{}", value[i].float_value);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::pair<std::string, std::string>> PostProcessing::GetAvailableShaderNames()
|
||||||
|
{
|
||||||
|
std::vector<std::pair<std::string, std::string>> names;
|
||||||
|
|
||||||
|
FileSystem::FindResultsArray results;
|
||||||
|
FileSystem::FindFiles(Path::Combine(EmuFolders::Resources, "shaders").c_str(), "*.glsl",
|
||||||
|
FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_RECURSIVE | FILESYSTEM_FIND_RELATIVE_PATHS, &results);
|
||||||
|
FileSystem::FindFiles(EmuFolders::Shaders.c_str(), "*.glsl",
|
||||||
|
FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_RECURSIVE | FILESYSTEM_FIND_RELATIVE_PATHS |
|
||||||
|
FILESYSTEM_FIND_KEEP_ARRAY,
|
||||||
|
&results);
|
||||||
|
|
||||||
|
for (FILESYSTEM_FIND_DATA& fd : results)
|
||||||
|
{
|
||||||
|
size_t pos = fd.FileName.rfind('.');
|
||||||
|
if (pos != std::string::npos && pos > 0)
|
||||||
|
fd.FileName.erase(pos);
|
||||||
|
|
||||||
|
// swap any backslashes for forward slashes so the config is cross-platform
|
||||||
|
for (size_t i = 0; i < fd.FileName.size(); i++)
|
||||||
|
{
|
||||||
|
if (fd.FileName[i] == '\\')
|
||||||
|
fd.FileName[i] = '/';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (std::none_of(names.begin(), names.end(), [&fd](const auto& other) { return fd.FileName == other.second; }))
|
||||||
|
{
|
||||||
|
std::string display_name = fmt::format(TRANSLATE_FS("PostProcessing", "{} [GLSL]"), fd.FileName);
|
||||||
|
names.emplace_back(std::move(display_name), std::move(fd.FileName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return names;
|
||||||
|
}
|
||||||
|
|
||||||
|
TinyString PostProcessing::GetStageConfigSection(u32 index)
|
||||||
|
{
|
||||||
|
return TinyString::FromFmt("PostProcessing/Stage{}", index + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostProcessing::CopyStageConfig(SettingsInterface& si, u32 old_index, u32 new_index)
|
||||||
|
{
|
||||||
|
const TinyString old_section = GetStageConfigSection(old_index);
|
||||||
|
const TinyString new_section = GetStageConfigSection(new_index);
|
||||||
|
|
||||||
|
si.ClearSection(new_section);
|
||||||
|
|
||||||
|
for (const auto& [key, value] : si.GetKeyValueList(old_section))
|
||||||
|
si.SetStringValue(new_section, key.c_str(), value.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostProcessing::SwapStageConfig(SettingsInterface& si, u32 lhs_index, u32 rhs_index)
|
||||||
|
{
|
||||||
|
const TinyString lhs_section = GetStageConfigSection(lhs_index);
|
||||||
|
const TinyString rhs_section = GetStageConfigSection(rhs_index);
|
||||||
|
|
||||||
|
const std::vector<std::pair<std::string, std::string>> lhs_kvs = si.GetKeyValueList(lhs_section);
|
||||||
|
si.ClearSection(lhs_section);
|
||||||
|
|
||||||
|
const std::vector<std::pair<std::string, std::string>> rhs_kvs = si.GetKeyValueList(rhs_section);
|
||||||
|
si.ClearSection(rhs_section);
|
||||||
|
|
||||||
|
for (const auto& [key, value] : rhs_kvs)
|
||||||
|
si.SetStringValue(lhs_section, key.c_str(), value.c_str());
|
||||||
|
|
||||||
|
for (const auto& [key, value] : lhs_kvs)
|
||||||
|
si.SetStringValue(rhs_section, key.c_str(), value.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 PostProcessing::Config::GetStageCount(const SettingsInterface& si)
|
||||||
|
{
|
||||||
|
return si.GetUIntValue("PostProcessing", "StageCount", 0u);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string PostProcessing::Config::GetStageShaderName(const SettingsInterface& si, u32 index)
|
||||||
|
{
|
||||||
|
return si.GetStringValue(GetStageConfigSection(index), "ShaderName");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<PostProcessing::ShaderOption> PostProcessing::Config::GetStageOptions(const SettingsInterface& si,
|
||||||
|
u32 index)
|
||||||
|
{
|
||||||
|
std::vector<PostProcessing::ShaderOption> ret;
|
||||||
|
|
||||||
|
const TinyString section = GetStageConfigSection(index);
|
||||||
|
const std::string shader_name = si.GetStringValue(section, "ShaderName");
|
||||||
|
if (shader_name.empty())
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
std::unique_ptr<Shader> shader = TryLoadingShader(shader_name, true, nullptr);
|
||||||
|
if (!shader)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
ret = shader->TakeOptions();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PostProcessing::Config::AddStage(SettingsInterface& si, const std::string& shader_name, Error* error)
|
||||||
|
{
|
||||||
|
std::unique_ptr<Shader> shader = TryLoadingShader(shader_name, true, error);
|
||||||
|
if (!shader)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
const u32 index = GetStageCount(si);
|
||||||
|
si.SetUIntValue("PostProcessing", "StageCount", index + 1);
|
||||||
|
|
||||||
|
const TinyString section = GetStageConfigSection(index);
|
||||||
|
si.SetStringValue(section, "ShaderName", shader->GetName().c_str());
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
// Leave options unset for now.
|
||||||
|
for (const ShaderOption& option : shader->GetOptions())
|
||||||
|
{
|
||||||
|
si.SetStringValue(section, option.name.c_str(),
|
||||||
|
ValueToString(option.type, option.vector_size, option.default_value));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostProcessing::Config::RemoveStage(SettingsInterface& si, u32 index)
|
||||||
|
{
|
||||||
|
const u32 stage_count = GetStageCount(si);
|
||||||
|
if (index >= stage_count)
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (u32 i = index; i < (stage_count - 1); i++)
|
||||||
|
CopyStageConfig(si, i + 1, i);
|
||||||
|
|
||||||
|
si.ClearSection(GetStageConfigSection(stage_count - 1));
|
||||||
|
si.SetUIntValue("PostProcessing", "StageCount", stage_count - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostProcessing::Config::MoveStageUp(SettingsInterface& si, u32 index)
|
||||||
|
{
|
||||||
|
const u32 stage_count = GetStageCount(si);
|
||||||
|
if (index == 0 || index >= stage_count)
|
||||||
|
return;
|
||||||
|
|
||||||
|
SwapStageConfig(si, index, index - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostProcessing::Config::MoveStageDown(SettingsInterface& si, u32 index)
|
||||||
|
{
|
||||||
|
const u32 stage_count = GetStageCount(si);
|
||||||
|
if ((index + 1) >= stage_count)
|
||||||
|
return;
|
||||||
|
|
||||||
|
SwapStageConfig(si, index, index + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostProcessing::Config::SetStageOption(SettingsInterface& si, u32 index, const ShaderOption& option)
|
||||||
|
{
|
||||||
|
const TinyString section = GetStageConfigSection(index);
|
||||||
|
si.SetStringValue(section, option.name.c_str(), ValueToString(option.type, option.vector_size, option.value));
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostProcessing::Config::UnsetStageOption(SettingsInterface& si, u32 index, const ShaderOption& option)
|
||||||
|
{
|
||||||
|
const TinyString section = GetStageConfigSection(index);
|
||||||
|
si.DeleteValue(section, option.name.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostProcessing::Config::ClearStages(SettingsInterface& si)
|
||||||
|
{
|
||||||
|
const u32 count = GetStageCount(si);
|
||||||
|
for (s32 i = static_cast<s32>(count - 1); i >= 0; i--)
|
||||||
|
si.ClearSection(GetStageConfigSection(static_cast<u32>(i)));
|
||||||
|
si.SetUIntValue("PostProcessing", "StageCount", 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PostProcessing::IsActive()
|
||||||
|
{
|
||||||
|
return s_enabled && !s_stages.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PostProcessing::IsEnabled()
|
||||||
|
{
|
||||||
|
return s_enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostProcessing::SetEnabled(bool enabled)
|
||||||
|
{
|
||||||
|
s_enabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<PostProcessing::Shader> PostProcessing::TryLoadingShader(const std::string& shader_name,
|
||||||
|
bool only_config, Error* error)
|
||||||
|
{
|
||||||
|
std::string filename(Path::Combine(EmuFolders::Shaders, fmt::format("{}.glsl", shader_name)));
|
||||||
|
if (FileSystem::FileExists(filename.c_str()))
|
||||||
|
{
|
||||||
|
std::unique_ptr<GLSLShader> shader = std::make_unique<GLSLShader>();
|
||||||
|
if (shader->LoadFromFile(std::string(shader_name), filename.c_str(), error))
|
||||||
|
return shader;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::string> resource_str(
|
||||||
|
Host::ReadResourceFileToString(fmt::format("shaders" FS_OSPATH_SEPARATOR_STR "{}.glsl", shader_name).c_str()));
|
||||||
|
if (resource_str.has_value())
|
||||||
|
{
|
||||||
|
std::unique_ptr<GLSLShader> shader = std::make_unique<GLSLShader>();
|
||||||
|
if (shader->LoadFromString(std::string(shader_name), std::move(resource_str.value()), error))
|
||||||
|
return shader;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log_ErrorPrint(fmt::format("Failed to load shader '{}'", shader_name).c_str());
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostProcessing::ClearStagesWithError(const Error& error)
|
||||||
|
{
|
||||||
|
std::string msg = error.GetDescription();
|
||||||
|
Host::AddIconOSDMessage(
|
||||||
|
"PostProcessLoadFail", ICON_FA_EXCLAMATION_TRIANGLE,
|
||||||
|
fmt::format(TRANSLATE_FS("OSDMessage", "Failed to load post-processing chain: {}"),
|
||||||
|
msg.empty() ? TRANSLATE_SV("PostProcessing", "Unknown Error") : std::string_view(msg)),
|
||||||
|
Host::OSD_ERROR_DURATION);
|
||||||
|
s_stages.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
SettingsInterface& PostProcessing::GetLoadSettingsInterface()
|
||||||
|
{
|
||||||
|
// If PostProcessing/Enable is set in the game settings interface, use that.
|
||||||
|
// Otherwise, use the base settings.
|
||||||
|
|
||||||
|
SettingsInterface* game_si = Host::Internal::GetGameSettingsLayer();
|
||||||
|
if (game_si && game_si->ContainsValue("PostProcessing", "Enabled"))
|
||||||
|
return *game_si;
|
||||||
|
else
|
||||||
|
return *Host::Internal::GetBaseSettingsLayer();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostProcessing::Initialize()
|
||||||
|
{
|
||||||
|
LoadStages();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostProcessing::LoadStages()
|
||||||
|
{
|
||||||
|
auto lock = Host::GetSettingsLock();
|
||||||
|
SettingsInterface& si = GetLoadSettingsInterface();
|
||||||
|
|
||||||
|
s_enabled = si.GetBoolValue("PostProcessing", "Enabled", false);
|
||||||
|
|
||||||
|
const u32 stage_count = Config::GetStageCount(si);
|
||||||
|
if (stage_count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Error error;
|
||||||
|
|
||||||
|
for (u32 i = 0; i < stage_count; i++)
|
||||||
|
{
|
||||||
|
std::string stage_name = Config::GetStageShaderName(si, i);
|
||||||
|
if (stage_name.empty())
|
||||||
|
{
|
||||||
|
error.SetString(fmt::format("No stage name in stage {}.", i + 1));
|
||||||
|
ClearStagesWithError(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
lock.unlock();
|
||||||
|
|
||||||
|
std::unique_ptr<Shader> shader = TryLoadingShader(stage_name, false, &error);
|
||||||
|
if (!shader)
|
||||||
|
{
|
||||||
|
ClearStagesWithError(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
lock.lock();
|
||||||
|
shader->LoadOptions(si, GetStageConfigSection(i));
|
||||||
|
s_stages.push_back(std::move(shader));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stage_count > 0)
|
||||||
|
{
|
||||||
|
s_timer.Reset();
|
||||||
|
Log_DevPrintf("Loaded %u post-processing stages.", stage_count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostProcessing::UpdateSettings()
|
||||||
|
{
|
||||||
|
auto lock = Host::GetSettingsLock();
|
||||||
|
SettingsInterface& si = GetLoadSettingsInterface();
|
||||||
|
|
||||||
|
s_enabled = si.GetBoolValue("PostProcessing", "Enabled", false);
|
||||||
|
|
||||||
|
const u32 stage_count = Config::GetStageCount(si);
|
||||||
|
if (stage_count == 0)
|
||||||
|
{
|
||||||
|
s_stages.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Error error;
|
||||||
|
|
||||||
|
s_stages.resize(stage_count);
|
||||||
|
|
||||||
|
for (u32 i = 0; i < stage_count; i++)
|
||||||
|
{
|
||||||
|
std::string stage_name = Config::GetStageShaderName(si, i);
|
||||||
|
if (stage_name.empty())
|
||||||
|
{
|
||||||
|
error.SetString(fmt::format("No stage name in stage {}.", i + 1));
|
||||||
|
ClearStagesWithError(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!s_stages[i] || stage_name != s_stages[i]->GetName())
|
||||||
|
{
|
||||||
|
if (i < s_stages.size())
|
||||||
|
s_stages[i].reset();
|
||||||
|
|
||||||
|
// Force recompile.
|
||||||
|
s_target_format = GPUTexture::Format::Unknown;
|
||||||
|
|
||||||
|
lock.unlock();
|
||||||
|
|
||||||
|
std::unique_ptr<Shader> shader = TryLoadingShader(stage_name, false, &error);
|
||||||
|
if (!shader)
|
||||||
|
{
|
||||||
|
ClearStagesWithError(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i < s_stages.size())
|
||||||
|
s_stages[i] = std::move(shader);
|
||||||
|
else
|
||||||
|
s_stages.push_back(std::move(shader));
|
||||||
|
|
||||||
|
lock.lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
s_stages[i]->LoadOptions(si, GetStageConfigSection(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stage_count > 0)
|
||||||
|
{
|
||||||
|
s_timer.Reset();
|
||||||
|
Log_DevPrintf("Loaded %u post-processing stages.", stage_count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostProcessing::Toggle()
|
||||||
|
{
|
||||||
|
if (s_stages.empty())
|
||||||
|
{
|
||||||
|
Host::AddIconOSDMessage("PostProcessing", ICON_FA_PAINT_ROLLER,
|
||||||
|
TRANSLATE_STR("OSDMessage", "No post-processing shaders are selected."),
|
||||||
|
Host::OSD_QUICK_DURATION);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool new_enabled = !s_enabled;
|
||||||
|
Host::AddIconOSDMessage("PostProcessing", ICON_FA_PAINT_ROLLER,
|
||||||
|
new_enabled ? TRANSLATE_STR("OSDMessage", "Post-processing is now enabled.") :
|
||||||
|
TRANSLATE_STR("OSDMessage", "Post-processing is now disabled."),
|
||||||
|
Host::OSD_QUICK_DURATION);
|
||||||
|
s_enabled = new_enabled;
|
||||||
|
if (s_enabled)
|
||||||
|
s_timer.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PostProcessing::ReloadShaders()
|
||||||
|
{
|
||||||
|
if (s_stages.empty())
|
||||||
|
{
|
||||||
|
Host::AddIconOSDMessage("PostProcessing", ICON_FA_PAINT_ROLLER,
|
||||||
|
TRANSLATE_STR("OSDMessage", "No post-processing shaders are selected."),
|
||||||
|
Host::OSD_QUICK_DURATION);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
decltype(s_stages)().swap(s_stages);
|
||||||
|
DestroyTextures();
|
||||||
|
LoadStages();
|
||||||
|
|
||||||
|
Host::AddIconOSDMessage("PostProcessing", ICON_FA_PAINT_ROLLER,
|
||||||
|
TRANSLATE_STR("OSDMessage", "Post-processing shaders reloaded."), Host::OSD_QUICK_DURATION);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostProcessing::Shutdown()
|
||||||
|
{
|
||||||
|
s_dummy_texture.reset();
|
||||||
|
s_samplers.clear();
|
||||||
|
s_enabled = false;
|
||||||
|
decltype(s_stages)().swap(s_stages);
|
||||||
|
DestroyTextures();
|
||||||
|
}
|
||||||
|
|
||||||
|
GPUTexture* PostProcessing::GetInputTexture()
|
||||||
|
{
|
||||||
|
return s_input_texture.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
GPUFramebuffer* PostProcessing::GetInputFramebuffer()
|
||||||
|
{
|
||||||
|
return s_input_framebuffer.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
const Common::Timer& PostProcessing::GetTimer()
|
||||||
|
{
|
||||||
|
return s_timer;
|
||||||
|
}
|
||||||
|
|
||||||
|
GPUSampler* PostProcessing::GetSampler(const GPUSampler::Config& config)
|
||||||
|
{
|
||||||
|
auto it = s_samplers.find(config.key);
|
||||||
|
if (it != s_samplers.end())
|
||||||
|
return it->second.get();
|
||||||
|
|
||||||
|
std::unique_ptr<GPUSampler> sampler = g_gpu_device->CreateSampler(config);
|
||||||
|
if (!sampler)
|
||||||
|
Log_ErrorPrint(fmt::format("Failed to create GPU sampler with config={:X}", config.key).c_str());
|
||||||
|
|
||||||
|
it = s_samplers.emplace(config.key, std::move(sampler)).first;
|
||||||
|
return it->second.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
GPUTexture* PostProcessing::GetDummyTexture()
|
||||||
|
{
|
||||||
|
if (s_dummy_texture)
|
||||||
|
return s_dummy_texture.get();
|
||||||
|
|
||||||
|
const u32 zero = 0;
|
||||||
|
s_dummy_texture = g_gpu_device->CreateTexture(1, 1, 1, 1, 1, GPUTexture::Type::Texture, GPUTexture::Format::RGBA8,
|
||||||
|
&zero, sizeof(zero));
|
||||||
|
if (!s_dummy_texture)
|
||||||
|
Log_ErrorPrint("Failed to create dummy texture.");
|
||||||
|
|
||||||
|
return s_dummy_texture.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PostProcessing::CheckTargets(GPUTexture::Format target_format, u32 target_width, u32 target_height)
|
||||||
|
{
|
||||||
|
if (s_target_format == target_format && s_target_width == target_width && s_target_height == target_height)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// In case any allocs fail.
|
||||||
|
DestroyTextures();
|
||||||
|
|
||||||
|
if (!(s_input_texture = g_gpu_device->CreateTexture(target_width, target_height, 1, 1, 1,
|
||||||
|
GPUTexture::Type::RenderTarget, target_format)) ||
|
||||||
|
!(s_input_framebuffer = g_gpu_device->CreateFramebuffer(s_input_texture.get())))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(s_output_texture = g_gpu_device->CreateTexture(target_width, target_height, 1, 1, 1,
|
||||||
|
GPUTexture::Type::RenderTarget, target_format)) ||
|
||||||
|
!(s_output_framebuffer = g_gpu_device->CreateFramebuffer(s_output_texture.get())))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& shader : s_stages)
|
||||||
|
{
|
||||||
|
if (!shader->CompilePipeline(target_format, target_width, target_height) ||
|
||||||
|
!shader->ResizeOutput(target_format, target_width, target_height))
|
||||||
|
{
|
||||||
|
Log_ErrorPrintf("Failed to compile one or more post-processing shaders, disabling.");
|
||||||
|
Host::AddIconOSDMessage(
|
||||||
|
"PostProcessLoadFail", ICON_FA_EXCLAMATION_TRIANGLE,
|
||||||
|
fmt::format("Failed to compile post-processing shader '{}'. Disabling post-processing.", shader->GetName()));
|
||||||
|
s_enabled = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
s_target_format = target_format;
|
||||||
|
s_target_width = target_width;
|
||||||
|
s_target_height = target_height;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostProcessing::DestroyTextures()
|
||||||
|
{
|
||||||
|
s_target_format = GPUTexture::Format::Unknown;
|
||||||
|
s_target_width = 0;
|
||||||
|
s_target_height = 0;
|
||||||
|
|
||||||
|
s_output_framebuffer.reset();
|
||||||
|
s_output_texture.reset();
|
||||||
|
|
||||||
|
s_input_framebuffer.reset();
|
||||||
|
s_input_texture.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PostProcessing::Apply(GPUFramebuffer* final_target, s32 final_left, s32 final_top, s32 final_width,
|
||||||
|
s32 final_height, s32 orig_width, s32 orig_height)
|
||||||
|
{
|
||||||
|
GL_SCOPE("PostProcessing Apply");
|
||||||
|
|
||||||
|
const u32 target_width = final_target ? final_target->GetWidth() : g_gpu_device->GetWindowWidth();
|
||||||
|
const u32 target_height = final_target ? final_target->GetHeight() : g_gpu_device->GetWindowHeight();
|
||||||
|
const GPUTexture::Format target_format =
|
||||||
|
final_target ? final_target->GetRT()->GetFormat() : g_gpu_device->GetWindowFormat();
|
||||||
|
if (!CheckTargets(target_format, target_width, target_height))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
g_gpu_device->SetViewportAndScissor(final_left, final_top, final_width, final_height);
|
||||||
|
|
||||||
|
GPUTexture* input = s_input_texture.get();
|
||||||
|
GPUFramebuffer* input_fb = s_input_framebuffer.get();
|
||||||
|
GPUTexture* output = s_output_texture.get();
|
||||||
|
GPUFramebuffer* output_fb = s_output_framebuffer.get();
|
||||||
|
input->MakeReadyForSampling();
|
||||||
|
|
||||||
|
for (const std::unique_ptr<Shader>& stage : s_stages)
|
||||||
|
{
|
||||||
|
const bool is_final = (stage.get() == s_stages.back().get());
|
||||||
|
|
||||||
|
if (!stage->Apply(input, is_final ? final_target : output_fb, final_left, final_top, final_width, final_height,
|
||||||
|
orig_width, orig_height, s_target_width, s_target_height))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_final)
|
||||||
|
{
|
||||||
|
output->MakeReadyForSampling();
|
||||||
|
std::swap(input, output);
|
||||||
|
std::swap(input_fb, output_fb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@ -0,0 +1,134 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
|
||||||
|
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "gpu_device.h"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <memory>
|
||||||
|
#include <string_view>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace Common
|
||||||
|
{
|
||||||
|
class Timer;
|
||||||
|
}
|
||||||
|
|
||||||
|
class GPUSampler;
|
||||||
|
class GPUFramebuffer;
|
||||||
|
class GPUTexture;
|
||||||
|
|
||||||
|
class Error;
|
||||||
|
class SettingsInterface;
|
||||||
|
|
||||||
|
namespace PostProcessing {
|
||||||
|
struct ShaderOption
|
||||||
|
{
|
||||||
|
enum : u32
|
||||||
|
{
|
||||||
|
MAX_VECTOR_COMPONENTS = 4
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class Type
|
||||||
|
{
|
||||||
|
Invalid,
|
||||||
|
Bool,
|
||||||
|
Int,
|
||||||
|
Float
|
||||||
|
};
|
||||||
|
|
||||||
|
union Value
|
||||||
|
{
|
||||||
|
s32 int_value;
|
||||||
|
float float_value;
|
||||||
|
};
|
||||||
|
static_assert(sizeof(Value) == sizeof(u32));
|
||||||
|
|
||||||
|
using ValueVector = std::array<Value, MAX_VECTOR_COMPONENTS>;
|
||||||
|
static_assert(sizeof(ValueVector) == sizeof(u32) * MAX_VECTOR_COMPONENTS);
|
||||||
|
|
||||||
|
std::string name;
|
||||||
|
std::string ui_name;
|
||||||
|
std::string dependent_option;
|
||||||
|
Type type;
|
||||||
|
u32 vector_size;
|
||||||
|
u32 buffer_size;
|
||||||
|
u32 buffer_offset;
|
||||||
|
ValueVector default_value;
|
||||||
|
ValueVector min_value;
|
||||||
|
ValueVector max_value;
|
||||||
|
ValueVector step_value;
|
||||||
|
ValueVector value;
|
||||||
|
|
||||||
|
static u32 ParseIntVector(const std::string_view& line, ValueVector* values);
|
||||||
|
static u32 ParseFloatVector(const std::string_view& line, ValueVector* values);
|
||||||
|
|
||||||
|
static constexpr ValueVector MakeIntVector(s32 x, s32 y = 0, s32 z = 0, s32 w = 0)
|
||||||
|
{
|
||||||
|
ValueVector ret = {};
|
||||||
|
ret[0].int_value = x;
|
||||||
|
ret[1].int_value = y;
|
||||||
|
ret[2].int_value = z;
|
||||||
|
ret[3].int_value = w;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr ValueVector MakeFloatVector(float x, float y = 0, float z = 0, float w = 0)
|
||||||
|
{
|
||||||
|
ValueVector ret = {};
|
||||||
|
ret[0].float_value = x;
|
||||||
|
ret[1].float_value = y;
|
||||||
|
ret[2].float_value = z;
|
||||||
|
ret[3].float_value = w;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// [display_name, filename]
|
||||||
|
std::vector<std::pair<std::string, std::string>> GetAvailableShaderNames();
|
||||||
|
|
||||||
|
namespace Config {
|
||||||
|
u32 GetStageCount(const SettingsInterface& si);
|
||||||
|
std::string GetStageShaderName(const SettingsInterface& si, u32 index);
|
||||||
|
std::vector<ShaderOption> GetStageOptions(const SettingsInterface& si, u32 index);
|
||||||
|
|
||||||
|
bool AddStage(SettingsInterface& si, const std::string& shader_name, Error* error);
|
||||||
|
void RemoveStage(SettingsInterface& si, u32 index);
|
||||||
|
void MoveStageUp(SettingsInterface& si, u32 index);
|
||||||
|
void MoveStageDown(SettingsInterface& si, u32 index);
|
||||||
|
void SetStageOption(SettingsInterface& si, u32 index, const ShaderOption& option);
|
||||||
|
void UnsetStageOption(SettingsInterface& si, u32 index, const ShaderOption& option);
|
||||||
|
void ClearStages(SettingsInterface& si);
|
||||||
|
} // namespace Config
|
||||||
|
|
||||||
|
bool IsActive();
|
||||||
|
bool IsEnabled();
|
||||||
|
void SetEnabled(bool enabled);
|
||||||
|
|
||||||
|
void Initialize();
|
||||||
|
|
||||||
|
/// Reloads configuration.
|
||||||
|
void UpdateSettings();
|
||||||
|
|
||||||
|
/// Temporarily toggles post-processing on/off.
|
||||||
|
void Toggle();
|
||||||
|
|
||||||
|
/// Reloads post processing shaders with the current configuration.
|
||||||
|
bool ReloadShaders();
|
||||||
|
|
||||||
|
void Shutdown();
|
||||||
|
|
||||||
|
GPUTexture* GetInputTexture();
|
||||||
|
GPUFramebuffer* GetInputFramebuffer();
|
||||||
|
const Common::Timer& GetTimer();
|
||||||
|
|
||||||
|
bool CheckTargets(GPUTexture::Format target_format, u32 target_width, u32 target_height);
|
||||||
|
|
||||||
|
bool Apply(GPUFramebuffer* final_target, s32 final_left, s32 final_top, s32 final_width, s32 final_height,
|
||||||
|
s32 orig_width, s32 orig_height);
|
||||||
|
|
||||||
|
GPUSampler* GetSampler(const GPUSampler::Config& config);
|
||||||
|
GPUTexture* GetDummyTexture();
|
||||||
|
|
||||||
|
}; // namespace PostProcessing
|
||||||
@ -1,279 +0,0 @@
|
|||||||
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
|
|
||||||
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
|
||||||
|
|
||||||
#include "postprocessing_chain.h"
|
|
||||||
#include "gpu_device.h"
|
|
||||||
#include "postprocessing_shader_glsl.h"
|
|
||||||
|
|
||||||
#include "core/host.h"
|
|
||||||
#include "core/settings.h"
|
|
||||||
|
|
||||||
#include "common/assert.h"
|
|
||||||
#include "common/file_system.h"
|
|
||||||
#include "common/log.h"
|
|
||||||
#include "common/path.h"
|
|
||||||
#include "common/string.h"
|
|
||||||
|
|
||||||
#include "fmt/format.h"
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
Log_SetChannel(PostProcessingChain);
|
|
||||||
|
|
||||||
static std::unique_ptr<PostProcessingShader> TryLoadingShader(const std::string_view& shader_name)
|
|
||||||
{
|
|
||||||
std::string filename(Path::Combine(EmuFolders::Shaders, fmt::format("{}.glsl", shader_name)));
|
|
||||||
if (FileSystem::FileExists(filename.c_str()))
|
|
||||||
{
|
|
||||||
std::unique_ptr<PostProcessingShaderGLSL> shader = std::make_unique<PostProcessingShaderGLSL>();
|
|
||||||
if (shader->LoadFromFile(std::string(shader_name), filename.c_str()))
|
|
||||||
return shader;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<std::string> resource_str(
|
|
||||||
Host::ReadResourceFileToString(fmt::format("shaders" FS_OSPATH_SEPARATOR_STR "{}.glsl", shader_name).c_str()));
|
|
||||||
if (resource_str.has_value())
|
|
||||||
{
|
|
||||||
std::unique_ptr<PostProcessingShaderGLSL> shader = std::make_unique<PostProcessingShaderGLSL>();
|
|
||||||
if (shader->LoadFromString(std::string(shader_name), std::move(resource_str.value())))
|
|
||||||
return shader;
|
|
||||||
}
|
|
||||||
|
|
||||||
Log_ErrorPrintf(fmt::format("Failed to load shader '{}'", shader_name).c_str());
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
PostProcessingChain::PostProcessingChain() = default;
|
|
||||||
|
|
||||||
PostProcessingChain::~PostProcessingChain() = default;
|
|
||||||
|
|
||||||
void PostProcessingChain::AddShader(std::unique_ptr<PostProcessingShader> shader)
|
|
||||||
{
|
|
||||||
m_shaders.push_back(std::move(shader));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PostProcessingChain::AddStage(const std::string_view& name)
|
|
||||||
{
|
|
||||||
std::unique_ptr<PostProcessingShader> shader = TryLoadingShader(name);
|
|
||||||
if (!shader)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
m_shaders.push_back(std::move(shader));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string PostProcessingChain::GetConfigString() const
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
bool first = true;
|
|
||||||
|
|
||||||
for (const auto& shader : m_shaders)
|
|
||||||
{
|
|
||||||
if (!first)
|
|
||||||
ss << ':';
|
|
||||||
else
|
|
||||||
first = false;
|
|
||||||
|
|
||||||
ss << shader->GetName();
|
|
||||||
std::string config_string = shader->GetConfigString();
|
|
||||||
if (!config_string.empty())
|
|
||||||
ss << ';' << config_string;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ss.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PostProcessingChain::CreateFromString(const std::string_view& chain_config)
|
|
||||||
{
|
|
||||||
std::vector<std::unique_ptr<PostProcessingShader>> shaders;
|
|
||||||
|
|
||||||
size_t last_sep = 0;
|
|
||||||
while (last_sep < chain_config.size())
|
|
||||||
{
|
|
||||||
size_t next_sep = chain_config.find(':', last_sep);
|
|
||||||
if (next_sep == std::string::npos)
|
|
||||||
next_sep = chain_config.size();
|
|
||||||
|
|
||||||
const std::string_view shader_config = chain_config.substr(last_sep, next_sep - last_sep);
|
|
||||||
size_t first_shader_sep = shader_config.find(';');
|
|
||||||
if (first_shader_sep == std::string::npos)
|
|
||||||
first_shader_sep = shader_config.size();
|
|
||||||
|
|
||||||
const std::string_view shader_name = shader_config.substr(0, first_shader_sep);
|
|
||||||
if (!shader_name.empty())
|
|
||||||
{
|
|
||||||
std::unique_ptr<PostProcessingShader> shader = TryLoadingShader(shader_name);
|
|
||||||
if (!shader)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (first_shader_sep < shader_config.size())
|
|
||||||
shader->SetConfigString(shader_config.substr(first_shader_sep + 1));
|
|
||||||
|
|
||||||
shaders.push_back(std::move(shader));
|
|
||||||
}
|
|
||||||
|
|
||||||
last_sep = next_sep + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (shaders.empty())
|
|
||||||
{
|
|
||||||
Log_ErrorPrintf("Postprocessing chain is empty!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_shaders = std::move(shaders);
|
|
||||||
Log_InfoPrintf("Loaded postprocessing chain of %zu shaders", m_shaders.size());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<std::string> PostProcessingChain::GetAvailableShaderNames()
|
|
||||||
{
|
|
||||||
std::vector<std::string> names;
|
|
||||||
|
|
||||||
FileSystem::FindResultsArray results;
|
|
||||||
FileSystem::FindFiles(Path::Combine(EmuFolders::Resources, "shaders").c_str(), "*.glsl",
|
|
||||||
FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_RECURSIVE | FILESYSTEM_FIND_RELATIVE_PATHS, &results);
|
|
||||||
FileSystem::FindFiles(EmuFolders::Shaders.c_str(), "*.glsl",
|
|
||||||
FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_RECURSIVE | FILESYSTEM_FIND_RELATIVE_PATHS |
|
|
||||||
FILESYSTEM_FIND_KEEP_ARRAY,
|
|
||||||
&results);
|
|
||||||
|
|
||||||
for (FILESYSTEM_FIND_DATA& fd : results)
|
|
||||||
{
|
|
||||||
size_t pos = fd.FileName.rfind('.');
|
|
||||||
if (pos != std::string::npos && pos > 0)
|
|
||||||
fd.FileName.erase(pos);
|
|
||||||
|
|
||||||
// swap any backslashes for forward slashes so the config is cross-platform
|
|
||||||
for (size_t i = 0; i < fd.FileName.size(); i++)
|
|
||||||
{
|
|
||||||
if (fd.FileName[i] == '\\')
|
|
||||||
fd.FileName[i] = '/';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (std::none_of(names.begin(), names.end(), [&fd](const std::string& other) { return fd.FileName == other; }))
|
|
||||||
{
|
|
||||||
names.push_back(std::move(fd.FileName));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return names;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingChain::RemoveStage(u32 index)
|
|
||||||
{
|
|
||||||
Assert(index < m_shaders.size());
|
|
||||||
m_shaders.erase(m_shaders.begin() + index);
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingChain::MoveStageUp(u32 index)
|
|
||||||
{
|
|
||||||
Assert(index < m_shaders.size());
|
|
||||||
if (index == 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
auto shader = std::move(m_shaders[index]);
|
|
||||||
m_shaders.erase(m_shaders.begin() + index);
|
|
||||||
m_shaders.insert(m_shaders.begin() + (index - 1u), std::move(shader));
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingChain::MoveStageDown(u32 index)
|
|
||||||
{
|
|
||||||
Assert(index < m_shaders.size());
|
|
||||||
if (index == (m_shaders.size() - 1u))
|
|
||||||
return;
|
|
||||||
|
|
||||||
auto shader = std::move(m_shaders[index]);
|
|
||||||
m_shaders.erase(m_shaders.begin() + index);
|
|
||||||
m_shaders.insert(m_shaders.begin() + (index + 1u), std::move(shader));
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostProcessingChain::ClearStages()
|
|
||||||
{
|
|
||||||
m_shaders.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PostProcessingChain::CheckTargets(GPUTexture::Format format, u32 target_width, u32 target_height)
|
|
||||||
{
|
|
||||||
if (m_target_format == format && m_target_width == target_width && m_target_height == target_height)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
// In case any allocs fail.
|
|
||||||
m_target_format = GPUTexture::Format::Unknown;
|
|
||||||
m_target_width = 0;
|
|
||||||
m_target_height = 0;
|
|
||||||
m_output_framebuffer.reset();
|
|
||||||
m_output_texture.reset();
|
|
||||||
m_input_framebuffer.reset();
|
|
||||||
m_input_texture.reset();
|
|
||||||
|
|
||||||
if (!(m_input_texture =
|
|
||||||
g_gpu_device->CreateTexture(target_width, target_height, 1, 1, 1, GPUTexture::Type::RenderTarget, format)) ||
|
|
||||||
!(m_input_framebuffer = g_gpu_device->CreateFramebuffer(m_input_texture.get())))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(m_output_texture =
|
|
||||||
g_gpu_device->CreateTexture(target_width, target_height, 1, 1, 1, GPUTexture::Type::RenderTarget, format)) ||
|
|
||||||
!(m_output_framebuffer = g_gpu_device->CreateFramebuffer(m_output_texture.get())))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& shader : m_shaders)
|
|
||||||
{
|
|
||||||
if (!shader->CompilePipeline(format, target_width, target_height) ||
|
|
||||||
!shader->ResizeOutput(format, target_width, target_height))
|
|
||||||
{
|
|
||||||
Log_ErrorPrintf("Failed to compile one or more post-processing shaders, disabling.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m_target_format = format;
|
|
||||||
m_target_width = target_width;
|
|
||||||
m_target_height = target_height;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PostProcessingChain::Apply(GPUFramebuffer* final_target, s32 final_left, s32 final_top, s32 final_width,
|
|
||||||
s32 final_height, s32 orig_width, s32 orig_height)
|
|
||||||
{
|
|
||||||
GL_SCOPE("PostProcessingChain Apply");
|
|
||||||
|
|
||||||
const u32 target_width = final_target ? final_target->GetWidth() : g_gpu_device->GetWindowWidth();
|
|
||||||
const u32 target_height = final_target ? final_target->GetHeight() : g_gpu_device->GetWindowHeight();
|
|
||||||
const GPUTexture::Format target_format =
|
|
||||||
final_target ? final_target->GetRT()->GetFormat() : g_gpu_device->GetWindowFormat();
|
|
||||||
if (!CheckTargets(target_format, target_width, target_height))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
g_gpu_device->SetViewportAndScissor(final_left, final_top, final_width, final_height);
|
|
||||||
|
|
||||||
GPUTexture* input = m_input_texture.get();
|
|
||||||
GPUFramebuffer* input_fb = m_input_framebuffer.get();
|
|
||||||
GPUTexture* output = m_output_texture.get();
|
|
||||||
GPUFramebuffer* output_fb = m_output_framebuffer.get();
|
|
||||||
input->MakeReadyForSampling();
|
|
||||||
|
|
||||||
for (const std::unique_ptr<PostProcessingShader>& stage : m_shaders)
|
|
||||||
{
|
|
||||||
const bool is_final = (stage.get() == m_shaders.back().get());
|
|
||||||
|
|
||||||
if (!stage->Apply(input, is_final ? final_target : output_fb, final_left, final_top, final_width, final_height,
|
|
||||||
orig_width, orig_height, m_target_width, m_target_height))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!is_final)
|
|
||||||
{
|
|
||||||
output->MakeReadyForSampling();
|
|
||||||
std::swap(input, output);
|
|
||||||
std::swap(input_fb, output_fb);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
@ -1,61 +0,0 @@
|
|||||||
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
|
|
||||||
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "gpu_device.h"
|
|
||||||
#include "postprocessing_shader.h"
|
|
||||||
|
|
||||||
#include "common/timer.h"
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <string_view>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
class GPUSampler;
|
|
||||||
class GPUFramebuffer;
|
|
||||||
class GPUTexture;
|
|
||||||
|
|
||||||
class PostProcessingChain
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
PostProcessingChain();
|
|
||||||
~PostProcessingChain();
|
|
||||||
|
|
||||||
static std::vector<std::string> GetAvailableShaderNames();
|
|
||||||
|
|
||||||
ALWAYS_INLINE bool IsEmpty() const { return m_shaders.empty(); }
|
|
||||||
ALWAYS_INLINE u32 GetStageCount() const { return static_cast<u32>(m_shaders.size()); }
|
|
||||||
ALWAYS_INLINE const PostProcessingShader* GetShaderStage(u32 i) const { return m_shaders[i].get(); }
|
|
||||||
ALWAYS_INLINE PostProcessingShader* GetShaderStage(u32 i) { return m_shaders[i].get(); }
|
|
||||||
ALWAYS_INLINE GPUTexture* GetInputTexture() const { return m_input_texture.get(); }
|
|
||||||
ALWAYS_INLINE GPUFramebuffer* GetInputFramebuffer() const { return m_input_framebuffer.get(); }
|
|
||||||
|
|
||||||
void AddShader(std::unique_ptr<PostProcessingShader> shader);
|
|
||||||
bool AddStage(const std::string_view& name);
|
|
||||||
void RemoveStage(u32 index);
|
|
||||||
void MoveStageUp(u32 index);
|
|
||||||
void MoveStageDown(u32 index);
|
|
||||||
void ClearStages();
|
|
||||||
|
|
||||||
std::string GetConfigString() const;
|
|
||||||
|
|
||||||
bool CreateFromString(const std::string_view& chain_config);
|
|
||||||
|
|
||||||
bool CheckTargets(GPUTexture::Format target_format, u32 target_width, u32 target_height);
|
|
||||||
|
|
||||||
bool Apply(GPUFramebuffer* final_target, s32 final_left, s32 final_top, s32 final_width, s32 final_height,
|
|
||||||
s32 orig_width, s32 orig_height);
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<std::unique_ptr<PostProcessingShader>> m_shaders;
|
|
||||||
|
|
||||||
GPUTexture::Format m_target_format = GPUTexture::Format::Unknown;
|
|
||||||
u32 m_target_width = 0;
|
|
||||||
u32 m_target_height = 0;
|
|
||||||
|
|
||||||
std::unique_ptr<GPUTexture> m_input_texture;
|
|
||||||
std::unique_ptr<GPUFramebuffer> m_input_framebuffer;
|
|
||||||
|
|
||||||
std::unique_ptr<GPUTexture> m_output_texture;
|
|
||||||
std::unique_ptr<GPUFramebuffer> m_output_framebuffer;
|
|
||||||
};
|
|
||||||
Loading…
Reference in New Issue