OpenGL Shader configuration UI

This commit is contained in:
Cacodemon345
2025-03-09 23:58:54 +06:00
parent 7a4d5ee75e
commit 7e61d2d586
13 changed files with 349 additions and 16 deletions

View File

@@ -1725,11 +1725,15 @@ load_gl3_shaders(void)
ini_section_t cat = ini_find_section(config, "GL3 Shaders");
char *p;
char temp[512];
int i = 0;
int i = 0, shaders = 0;
memset(temp, 0, sizeof(temp));
memset(gl3_shader_file, 0, sizeof(gl3_shader_file));
for (int i = 0; i < MAX_USER_SHADERS; i++) {
shaders = ini_section_get_int(cat, "shaders", 0);
if (shaders > MAX_USER_SHADERS)
shaders = MAX_USER_SHADERS;
for (int i = 0; i < shaders; i++) {
temp[0] = 0;
snprintf(temp, 512, "shader%d", i);
p = ini_section_get_string(cat, temp, "");
@@ -2670,8 +2674,12 @@ save_gl3_shaders(void)
int shaders = 0, i = 0;
for (i = 0; i < MAX_USER_SHADERS; i++) {
if (gl3_shader_file[i][0] == 0)
if (gl3_shader_file[i][0] == 0) {
temp[0] = 0;
snprintf(temp, 512, "shader%d", i);
ini_section_delete_var(cat, temp);
break;
}
shaders++;
}

View File

@@ -31,6 +31,7 @@ typedef void *ini_section_t;
extern ini_t ini_new(void);
extern ini_t ini_read(const char *fn);
extern void ini_strip_quotes(ini_t ini);
extern void ini_write(ini_t ini, const char *fn);
extern void ini_dump(ini_t ini);
extern void ini_close(ini_t ini);
@@ -91,7 +92,7 @@ extern ini_section_t ini_find_or_create_section(ini_t ini, const char *name);
extern void ini_rename_section(ini_section_t section, const char *name);
extern void ini_delete_section_if_empty(ini_t ini, ini_section_t section);
static inline void *wx_config_load(const char *path) { return (void*) ini_read(path); }
static inline void *wx_config_load(const char *path) { ini_t ini = ini_read(path); if (ini) ini_strip_quotes(ini); return (void*)ini; }
static inline int wx_config_get_string(void *config, const char *name, char *dst, int size, const char *defVal) {
int res = ini_has_entry(ini_find_or_create_section((ini_t)config, ""), name);
char* str = ini_get_string((ini_t)config, "", name, (char*)defVal);

View File

@@ -254,9 +254,8 @@ ini_delete_section_if_empty(ini_t ini, ini_section_t section)
static section_t *
create_section(list_t *head, const char *name)
{
section_t *ns = malloc(sizeof(section_t));
section_t *ns = calloc(1, sizeof(section_t));
memset(ns, 0x00, sizeof(section_t));
memcpy(ns->name, name, strlen(name) + 1);
list_add(&ns->list, head);
@@ -279,9 +278,8 @@ ini_find_or_create_section(ini_t ini, const char *name)
static entry_t *
create_entry(section_t *section, const char *name)
{
entry_t *ne = malloc(sizeof(entry_t));
entry_t *ne = calloc(1, sizeof(entry_t));
memset(ne, 0x00, sizeof(entry_t));
memcpy(ne->name, name, strlen(name) + 1);
list_add(&ne->list, &section->entry_head);
@@ -390,11 +388,8 @@ ini_read(const char *fn)
if (fp == NULL)
return NULL;
head = malloc(sizeof(list_t));
memset(head, 0x00, sizeof(list_t));
sec = malloc(sizeof(section_t));
memset(sec, 0x00, sizeof(section_t));
head = calloc(1, sizeof(list_t));
sec = calloc(1, sizeof(section_t));
list_add(&sec->list, head);
if (bom)
@@ -475,7 +470,7 @@ ini_read(const char *fn)
d = c;
/* Allocate a new variable entry.. */
ne = malloc(sizeof(entry_t));
ne = calloc(1, sizeof(entry_t));
memset(ne, 0x00, sizeof(entry_t));
memcpy(ne->name, ename, 128);
wcsncpy(ne->wdata, &buff[d], sizeof_w(ne->wdata) - 1);
@@ -551,6 +546,43 @@ ini_write(ini_t ini, const char *fn)
(void) fclose(fp);
}
void
ini_strip_quotes(ini_t ini)
{
list_t *list = (list_t *) ini;
section_t *sec;
sec = (section_t *) list->next;
while (sec != NULL) {
entry_t *ent;
ent = (entry_t *) sec->entry_head.next;
while (ent != NULL) {
if (ent->name[0] != '\0') {
int i = 0;
if (ent->wdata[0] == L'\"') {
memmove(ent->wdata, &ent->wdata[1], sizeof(ent->wdata) - sizeof(wchar_t));
}
if (ent->wdata[wcslen(ent->wdata) - 1] == L'\"') {
ent->wdata[wcslen(ent->wdata) - 1] = 0;
}
if (ent->data[0] == '\"') {
memmove(ent->data, &ent->data[1], sizeof(ent->data) - sizeof(char));
}
if (ent->data[strlen(ent->data) - 1] == '\"') {
ent->data[strlen(ent->data) - 1] = 0;
}
}
ent = (entry_t *) ent->list.next;
}
sec = (section_t *) sec->list.next;
}
}
ini_t
ini_new(void)
{

View File

@@ -197,6 +197,7 @@ add_library(ui STATIC
qt_openglshadermanagerdialog.hpp
qt_openglshadermanagerdialog.cpp
qt_openglshadermanagerdialog.ui
qt_openglshaderconfig.hpp qt_openglshaderconfig.cpp qt_openglshaderconfig.ui
)
if(RTMIDI)

View File

@@ -255,6 +255,8 @@ glslp_t *glslp_parse(const char *f) {
safe_strncpy(s, t + j, sublen);
s[511 < sublen ? 511 : sublen] = 0;
if (s[strlen(s) - 1] == ';') s[strlen(s) - 1] = 0;
struct texture *tex = &glslp->textures[glslp->num_textures++];
strcpy(tex->name, s);

View File

@@ -1982,6 +1982,12 @@ MainWindow::changeEvent(QEvent *event)
}
}
void
MainWindow::reloadAllRenderers()
{
reload_renderers = true;
}
void
MainWindow::on_actionRenderer_options_triggered()
{
@@ -2000,6 +2006,16 @@ MainWindow::on_actionRenderer_options_triggered()
if (renderers[i] && renderers[i]->hasOptions())
renderers[i]->reloadOptions();
}
} else if (reload_renderers && ui->stackedWidget->reloadRendererOption()) {
reload_renderers = false;
ui->stackedWidget->switchRenderer(static_cast<RendererStack::Renderer>(vid_api));
if (show_second_monitors) {
for (int i = 1; i < MONITORS_NUM; i++) {
if (renderers[i] && renderers[i]->reloadRendererOption() && renderers[i]->hasOptions()) {
ui->stackedWidget->switchRenderer(static_cast<RendererStack::Renderer>(vid_api));
}
}
}
}
}
}

View File

@@ -32,6 +32,7 @@ public:
QSize getRenderWidgetSize();
void setSendKeyboardInput(bool enabled);
void checkFullscreenHotkey();
void reloadAllRenderers();
std::array<std::unique_ptr<RendererStack>, 8> renderers;
signals:
@@ -173,6 +174,9 @@ private:
bool fs_on_signal = false;
bool fs_off_signal = false;
/* Reload the renderers after closing renderer options dialog. */
bool reload_renderers = false;
friend class SpecifyDimensions;
friend class ProgSettings;
friend class RendererCommon;

View File

@@ -0,0 +1,85 @@
#include "qt_openglshaderconfig.hpp"
#include "ui_qt_openglshaderconfig.h"
#include "qt_mainwindow.hpp"
extern MainWindow* main_window;
extern "C"
{
#include <86box/86box.h>
#include <86box/plat.h>
#include <86box/config.h>
}
OpenGLShaderConfig::OpenGLShaderConfig(QWidget *parent, glslp_t* shader)
: QDialog(parent)
, ui(new Ui::OpenGLShaderConfig)
{
ui->setupUi(this);
currentShader = shader;
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
glslp_read_shader_config(currentShader);
for (int i = 0; i < currentShader->num_parameters; i++) {
auto spinBox = new QDoubleSpinBox;
spinBox->setObjectName(currentShader->parameters[i].id);
spinBox->setRange(currentShader->parameters[i].min, currentShader->parameters[i].max);
spinBox->setValue(currentShader->parameters[i].value);
spinBox->setSingleStep(currentShader->parameters[i].step);
QFormLayout* layout = (QFormLayout*)ui->scrollAreaWidgetContents->layout();
layout->addRow(currentShader->parameters[i].description, spinBox);
}
}
OpenGLShaderConfig::~OpenGLShaderConfig()
{
delete ui;
}
void OpenGLShaderConfig::on_buttonBox_clicked(QAbstractButton *button)
{
if (ui->buttonBox->buttonRole(button) == QDialogButtonBox::ResetRole) {
for (int i = 0; i < currentShader->num_parameters; i++) {
QDoubleSpinBox* box = this->findChild<QDoubleSpinBox*>(QString(currentShader->parameters[i].id));
if (box) {
box->setValue(currentShader->parameters[i].default_value);
}
}
} else if (ui->buttonBox->buttonRole(button) == QDialogButtonBox::ApplyRole) {
startblit();
for (int i = 0; i < currentShader->num_parameters; i++) {
QDoubleSpinBox* box = this->findChild<QDoubleSpinBox*>(QString(currentShader->parameters[i].id));
if (box) {
float val = (float)box->value();
currentShader->parameters[i].value = val;
}
}
glslp_write_shader_config(currentShader);
config_save();
endblit();
main_window->reloadAllRenderers();
}
}
void OpenGLShaderConfig::on_OpenGLShaderConfig_accepted()
{
startblit();
for (int i = 0; i < currentShader->num_parameters; i++) {
QDoubleSpinBox* box = (QDoubleSpinBox*)this->findChild<QDoubleSpinBox*>(QString(currentShader->parameters[i].id));
if (box) {
float val = (float)box->value();
currentShader->parameters[i].value = val;
}
}
glslp_write_shader_config(currentShader);
config_save();
endblit();
main_window->reloadAllRenderers();
}

View File

@@ -0,0 +1,40 @@
#ifndef QT_OPENGLSHADERCONFIG_HPP
#define QT_OPENGLSHADERCONFIG_HPP
#include <QDialog>
#include <QLabel>
#include <QDoubleSpinBox>
#include <QAbstractButton>
#include <map>
#include <string>
extern "C"
{
#include <86box/qt-glslp-parser.h>
}
namespace Ui {
class OpenGLShaderConfig;
}
class OpenGLShaderConfig : public QDialog {
Q_OBJECT
public:
explicit OpenGLShaderConfig(QWidget *parent = nullptr, glslp_t* shader = nullptr);
~OpenGLShaderConfig();
private slots:
void on_buttonBox_clicked(QAbstractButton *button);
void on_OpenGLShaderConfig_accepted();
private:
Ui::OpenGLShaderConfig *ui;
glslp_t* currentShader;
std::map<std::string, double> defaultValues;
};
#endif // QT_OPENGLSHADERCONFIG_HPP

View File

@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>OpenGLShaderConfig</class>
<widget class="QDialog" name="OpenGLShaderConfig">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Shader Configuration</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="sizeConstraint">
<enum>QLayout::SizeConstraint::SetMinAndMaxSize</enum>
</property>
<item>
<widget class="QScrollArea" name="scrollArea">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>380</width>
<height>250</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout_2">
<property name="sizeConstraint">
<enum>QLayout::SizeConstraint::SetMaximumSize</enum>
</property>
<property name="fieldGrowthPolicy">
<enum>QFormLayout::FieldGrowthPolicy::AllNonFixedFieldsGrow</enum>
</property>
</layout>
</widget>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::StandardButton::Apply|QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok|QDialogButtonBox::StandardButton::Reset</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>OpenGLShaderConfig</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>OpenGLShaderConfig</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@@ -1,6 +1,8 @@
#include "qt_openglshadermanagerdialog.hpp"
#include "ui_qt_openglshadermanagerdialog.h"
#include "qt_openglshaderconfig.hpp"
#include <QListWidgetItem>
#include <QFileDialog>
#include <QMessageBox>
@@ -39,10 +41,21 @@ OpenGLShaderManagerDialog::OpenGLShaderManagerDialog(QWidget *parent)
}
if (ui->shaderListWidget->count()) {
ui->shaderListWidget->setCurrentRow(ui->shaderListWidget->count() - 1);
auto current = ui->shaderListWidget->currentItem();
if (current) {
glslp_t* shader = (glslp_t*)current->data(Qt::UserRole + 2).toULongLong();
if (shader->num_parameters > 0)
ui->buttonConfigure->setEnabled(true);
else
ui->buttonConfigure->setEnabled(false);
} else {
ui->buttonConfigure->setEnabled(false);
}
} else {
ui->buttonRemove->setDisabled(true);
ui->buttonMoveUp->setDisabled(true);
ui->buttonMoveDown->setDisabled(true);
ui->buttonConfigure->setDisabled(true);
}
}
@@ -84,9 +97,16 @@ void OpenGLShaderManagerDialog::on_shaderListWidget_currentItemChanged(QListWidg
ui->buttonRemove->setDisabled(true);
ui->buttonMoveUp->setDisabled(true);
ui->buttonMoveDown->setDisabled(true);
ui->buttonConfigure->setDisabled(true);
return;
} else {
ui->buttonRemove->setDisabled(false);
ui->buttonConfigure->setDisabled(true);
if (current) {
glslp_t* shader = (glslp_t*)current->data(Qt::UserRole + 2).toULongLong();
if (shader->num_parameters > 0)
ui->buttonConfigure->setEnabled(true);
}
}
ui->buttonMoveUp->setDisabled(ui->shaderListWidget->currentRow() == 0);
ui->buttonMoveDown->setDisabled(ui->shaderListWidget->currentRow() == (ui->shaderListWidget->count() - 1));
@@ -95,6 +115,22 @@ void OpenGLShaderManagerDialog::on_shaderListWidget_currentItemChanged(QListWidg
void OpenGLShaderManagerDialog::on_shaderListWidget_currentRowChanged(int currentRow)
{
auto current = ui->shaderListWidget->currentItem();
if (current == nullptr) {
ui->buttonRemove->setDisabled(true);
ui->buttonMoveUp->setDisabled(true);
ui->buttonMoveDown->setDisabled(true);
ui->buttonConfigure->setDisabled(true);
return;
} else {
ui->buttonRemove->setDisabled(false);
ui->buttonConfigure->setDisabled(true);
if (current) {
glslp_t* shader = (glslp_t*)current->data(Qt::UserRole + 2).toULongLong();
if (shader->num_parameters > 0)
ui->buttonConfigure->setEnabled(true);
}
}
ui->buttonMoveUp->setDisabled(ui->shaderListWidget->currentRow() == 0);
ui->buttonMoveDown->setDisabled(ui->shaderListWidget->currentRow() == (ui->shaderListWidget->count() - 1));
}
@@ -143,6 +179,8 @@ void OpenGLShaderManagerDialog::on_buttonRemove_clicked()
glslp_free((glslp_t*)item->data(Qt::UserRole + 2).toULongLong());
}
delete item;
on_shaderListWidget_currentRowChanged(ui->shaderListWidget->currentRow());
}
}
@@ -157,3 +195,15 @@ void OpenGLShaderManagerDialog::on_OpenGLShaderManagerDialog_accepted()
endblit();
}
void OpenGLShaderManagerDialog::on_buttonConfigure_clicked()
{
auto item = ui->shaderListWidget->currentItem();
if (item) {
glslp_t* shader = (glslp_t*)item->data(Qt::UserRole + 2).toULongLong();
auto configDialog = new OpenGLShaderConfig(this, shader);
configDialog->exec();
}
}

View File

@@ -33,6 +33,8 @@ private slots:
void on_OpenGLShaderManagerDialog_accepted();
void on_buttonConfigure_clicked();
private:
Ui::OpenGLShaderManagerDialog *ui;
};

View File

@@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
<string>Shader Manager</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="sizeConstraint">
@@ -59,7 +59,7 @@
<item>
<widget class="QPushButton" name="buttonConfigure">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="text">
<string>Configure</string>