src/platform/qt/ShaderSelector.cpp (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "ShaderSelector.h"
7
8#include "ConfigController.h"
9#include "GBAApp.h"
10#include "Display.h"
11#include "VFileDevice.h"
12
13#include <QCheckBox>
14#include <QDoubleSpinBox>
15#include <QFileDialog>
16#include <QFormLayout>
17#include <QGridLayout>
18#include <QSpinBox>
19
20#include <mgba/core/version.h>
21#include <mgba-util/vfs.h>
22#include "platform/video-backend.h"
23
24#if !defined(_WIN32) || defined(USE_EPOXY)
25#include "platform/opengl/gles2.h"
26#endif
27
28using namespace QGBA;
29
30ShaderSelector::ShaderSelector(Display* display, ConfigController* config, QWidget* parent)
31 : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
32 , m_display(display)
33 , m_config(config)
34{
35 m_ui.setupUi(this);
36
37 refreshShaders();
38
39 connect(m_ui.load, &QAbstractButton::clicked, this, &ShaderSelector::selectShader);
40 connect(m_ui.unload, &QAbstractButton::clicked, this, &ShaderSelector::clearShader);
41 connect(m_ui.buttonBox, &QDialogButtonBox::clicked, this, &ShaderSelector::buttonPressed);
42 connect(this, &ShaderSelector::saved, [this]() {
43 m_config->setOption("shader", m_shaderPath);
44 });
45}
46
47ShaderSelector::~ShaderSelector() {
48 clear();
49}
50
51void ShaderSelector::clear() {
52 m_ui.shaderName->setText(tr("No shader active"));
53 m_ui.description->clear();
54 m_ui.author->clear();
55
56 while (QWidget* page = m_ui.passes->widget(0)) {
57 m_ui.passes->removeTab(0);
58 delete page;
59 }
60}
61
62void ShaderSelector::selectShader() {
63 QString path(GBAApp::dataDir());
64 path += QLatin1String("/shaders");
65 QFileDialog dialog(nullptr, tr("Load shader"), path);
66 dialog.setFileMode(QFileDialog::Directory);
67 dialog.exec();
68 QStringList names = dialog.selectedFiles();
69 if (names.count() == 1) {
70 loadShader(names[0]);
71 refreshShaders();
72 }
73}
74
75void ShaderSelector::loadShader(const QString& path) {
76 VDir* shader = VFileDevice::openDir(path);
77 if (!shader) {
78 shader = VFileDevice::openArchive(path);
79 }
80 if (!shader) {
81 return;
82 }
83 m_display->setShaders(shader);
84 shader->close(shader);
85 m_shaderPath = path;
86}
87
88void ShaderSelector::clearShader() {
89 m_display->clearShaders();
90 refreshShaders();
91 m_shaderPath = "";
92}
93
94void ShaderSelector::refreshShaders() {
95 clear();
96 m_shaders = m_display->shaders();
97 if (!m_shaders) {
98 return;
99 }
100 if (m_shaders->name) {
101 m_ui.shaderName->setText(m_shaders->name);
102 } else {
103 m_ui.shaderName->setText(tr("No shader loaded"));
104 }
105 if (m_shaders->description) {
106 m_ui.description->setText(m_shaders->description);
107 } else {
108 m_ui.description->clear();
109 }
110 if (m_shaders->author) {
111 m_ui.author->setText(tr("by %1").arg(m_shaders->author));
112 } else {
113 m_ui.author->clear();
114 }
115
116 disconnect(this, &ShaderSelector::saved, 0, 0);
117 disconnect(this, &ShaderSelector::reset, 0, 0);
118 disconnect(this, &ShaderSelector::resetToDefault, 0, 0);
119
120 connect(this, &ShaderSelector::saved, [this]() {
121 m_config->setOption("shader", m_shaderPath);
122 });
123
124#if !defined(_WIN32) || defined(USE_EPOXY)
125 if (m_shaders->preprocessShader) {
126 m_ui.passes->addTab(makePage(static_cast<mGLES2Shader*>(m_shaders->preprocessShader), "default", 0), tr("Preprocessing"));
127 }
128 mGLES2Shader* shaders = static_cast<mGLES2Shader*>(m_shaders->passes);
129 QFileInfo fi(m_shaderPath);
130 for (size_t p = 0; p < m_shaders->nPasses; ++p) {
131 QWidget* page = makePage(&shaders[p], fi.baseName(), p);
132 if (page) {
133 m_ui.passes->addTab(page, tr("Pass %1").arg(p + 1));
134 }
135 }
136#endif
137}
138
139void ShaderSelector::addUniform(QGridLayout* settings, const QString& section, const QString& name, float* value, float min, float max, int y, int x) {
140 QDoubleSpinBox* f = new QDoubleSpinBox;
141 f->setDecimals(3);
142 if (min < max) {
143 f->setMinimum(min);
144 f->setMaximum(max);
145 }
146 float def = *value;
147 bool ok = false;
148 float v = m_config->getQtOption(name, section).toFloat(&ok);
149 if (ok) {
150 *value = v;
151 }
152 f->setValue(*value);
153 f->setSingleStep(0.001);
154 f->setAccelerated(true);
155 settings->addWidget(f, y, x);
156 connect(f, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), [value](double v) {
157 *value = v;
158 });
159 connect(this, &ShaderSelector::saved, [this, section, name, f]() {
160 m_config->setQtOption(name, f->value(), section);
161 });
162 connect(this, &ShaderSelector::reset, [this, section, name, f]() {
163 bool ok = false;
164 float v = m_config->getQtOption(name, section).toFloat(&ok);
165 if (ok) {
166 f->setValue(v);
167 }
168 });
169 connect(this, &ShaderSelector::resetToDefault, [def, section, name, f]() {
170 f->setValue(def);
171 });
172}
173
174void ShaderSelector::addUniform(QGridLayout* settings, const QString& section, const QString& name, int* value, int min, int max, int y, int x) {
175 QSpinBox* i = new QSpinBox;
176 if (min < max) {
177 i->setMinimum(min);
178 i->setMaximum(max);
179 }
180 int def = *value;
181 bool ok = false;
182 int v = m_config->getQtOption(name, section).toInt(&ok);
183 if (ok) {
184 *value = v;
185 }
186 i->setValue(*value);
187 i->setSingleStep(1);
188 i->setAccelerated(true);
189 settings->addWidget(i, y, x);
190 connect(i, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [value](int v) {
191 *value = v;
192 });
193 connect(this, &ShaderSelector::saved, [this, section, name, i]() {
194 m_config->setQtOption(name, i->value(), section);
195 });
196 connect(this, &ShaderSelector::reset, [this, section, name, i]() {
197 bool ok = false;
198 int v = m_config->getQtOption(name, section).toInt(&ok);
199 if (ok) {
200 i->setValue(v);
201 }
202 });
203 connect(this, &ShaderSelector::resetToDefault, [def, section, name, i]() {
204 i->setValue(def);
205 });
206}
207
208QWidget* ShaderSelector::makePage(mGLES2Shader* shader, const QString& name, int pass) {
209#if !defined(_WIN32) || defined(USE_EPOXY)
210 if (!shader->nUniforms) {
211 return nullptr;
212 }
213 QWidget* page = new QWidget;
214 QFormLayout* layout = new QFormLayout;
215 page->setLayout(layout);
216 for (size_t u = 0 ; u < shader->nUniforms; ++u) {
217 QGridLayout* settings = new QGridLayout;
218 mGLES2Uniform* uniform = &shader->uniforms[u];
219 QString section = QString("shader.%1.%2").arg(name).arg(pass);
220 QString name = QLatin1String(uniform->name);
221 switch (uniform->type) {
222 case GL_FLOAT:
223 addUniform(settings, section, name, &uniform->value.f, uniform->min.f, uniform->max.f, 0, 0);
224 break;
225 case GL_FLOAT_VEC2:
226 addUniform(settings, section, name + "[0]", &uniform->value.fvec2[0], uniform->min.fvec2[0], uniform->max.fvec2[0], 0, 0);
227 addUniform(settings, section, name + "[1]", &uniform->value.fvec2[1], uniform->min.fvec2[1], uniform->max.fvec2[1], 0, 1);
228 break;
229 case GL_FLOAT_VEC3:
230 addUniform(settings, section, name + "[0]", &uniform->value.fvec3[0], uniform->min.fvec3[0], uniform->max.fvec3[0], 0, 0);
231 addUniform(settings, section, name + "[1]", &uniform->value.fvec3[1], uniform->min.fvec3[1], uniform->max.fvec3[1], 0, 1);
232 addUniform(settings, section, name + "[2]", &uniform->value.fvec3[2], uniform->min.fvec3[2], uniform->max.fvec3[2], 0, 2);
233 break;
234 case GL_FLOAT_VEC4:
235 addUniform(settings, section, name + "[0]", &uniform->value.fvec4[0], uniform->min.fvec4[0], uniform->max.fvec4[0], 0, 0);
236 addUniform(settings, section, name + "[1]", &uniform->value.fvec4[1], uniform->min.fvec4[1], uniform->max.fvec4[1], 0, 1);
237 addUniform(settings, section, name + "[2]", &uniform->value.fvec4[2], uniform->min.fvec4[2], uniform->max.fvec4[2], 0, 2);
238 addUniform(settings, section, name + "[3]", &uniform->value.fvec4[3], uniform->min.fvec4[3], uniform->max.fvec4[3], 0, 3);
239 break;
240 case GL_INT:
241 addUniform(settings, section, name, &uniform->value.i, uniform->min.i, uniform->max.i, 0, 0);
242 break;
243 case GL_INT_VEC2:
244 addUniform(settings, section, name + "[0]", &uniform->value.ivec2[0], uniform->min.ivec2[0], uniform->max.ivec2[0], 0, 0);
245 addUniform(settings, section, name + "[1]", &uniform->value.ivec2[1], uniform->min.ivec2[1], uniform->max.ivec2[1], 0, 1);
246 break;
247 case GL_INT_VEC3:
248 addUniform(settings, section, name + "[0]", &uniform->value.ivec3[0], uniform->min.ivec3[0], uniform->max.ivec3[0], 0, 0);
249 addUniform(settings, section, name + "[1]", &uniform->value.ivec3[1], uniform->min.ivec3[1], uniform->max.ivec3[1], 0, 1);
250 addUniform(settings, section, name + "[2]", &uniform->value.ivec3[2], uniform->min.ivec3[2], uniform->max.ivec3[2], 0, 2);
251 break;
252 case GL_INT_VEC4:
253 addUniform(settings, section, name + "[0]", &uniform->value.ivec4[0], uniform->min.ivec4[0], uniform->max.ivec4[0], 0, 0);
254 addUniform(settings, section, name + "[1]", &uniform->value.ivec4[1], uniform->min.ivec4[1], uniform->max.ivec4[1], 0, 1);
255 addUniform(settings, section, name + "[2]", &uniform->value.ivec4[2], uniform->min.ivec4[2], uniform->max.ivec4[2], 0, 2);
256 addUniform(settings, section, name + "[3]", &uniform->value.ivec4[3], uniform->min.ivec4[3], uniform->max.ivec4[3], 0, 3);
257 break;
258 }
259 layout->addRow(shader->uniforms[u].readableName, settings);
260 }
261 return page;
262#else
263 return nullptr;
264#endif
265}
266
267void ShaderSelector::buttonPressed(QAbstractButton* button) {
268 switch (m_ui.buttonBox->standardButton(button)) {
269 case QDialogButtonBox::Reset:
270 emit reset();
271 break;
272 case QDialogButtonBox::Ok:
273 emit saved();
274 close();
275 break;
276 case QDialogButtonBox::RestoreDefaults:
277 emit resetToDefault();
278 break;
279 default:
280 break;
281 }
282}