ShaderNode: Add compile to binary action
This commit is contained in:
parent
74acf440fc
commit
58e59be267
|
|
@ -90,16 +90,105 @@ void MainWindow::BuildMenu()
|
||||||
QObject::connect(loadShader, &QAction::triggered, this, &MainWindow::OnLoad);
|
QObject::connect(loadShader, &QAction::triggered, this, &MainWindow::OnLoad);
|
||||||
QAction* saveShader = shader->addAction(tr("Save..."));
|
QAction* saveShader = shader->addAction(tr("Save..."));
|
||||||
QObject::connect(saveShader, &QAction::triggered, this, &MainWindow::OnSave);
|
QObject::connect(saveShader, &QAction::triggered, this, &MainWindow::OnSave);
|
||||||
|
QAction* compileShader = shader->addAction(tr("Compile..."));
|
||||||
|
QObject::connect(compileShader, &QAction::triggered, this, &MainWindow::OnCompile);
|
||||||
}
|
}
|
||||||
|
|
||||||
QMenu* compileMenu = menu->addMenu(tr("&Compilation"));
|
QMenu* generateMenu = menu->addMenu(tr("&Generate"));
|
||||||
QAction* compileToGlsl = compileMenu->addAction(tr("GLSL"));
|
QAction* generateGlsl = generateMenu->addAction(tr("GLSL"));
|
||||||
connect(compileToGlsl, &QAction::triggered, [&](bool) { OnCompileToGLSL(); });
|
connect(generateGlsl, &QAction::triggered, [&](bool) { OnGenerateGLSL(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::OnCompileToGLSL()
|
void MainWindow::OnCompile()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
{
|
||||||
|
auto shader = ToShader();
|
||||||
|
|
||||||
|
QString fileName = QFileDialog::getSaveFileName(nullptr, tr("Save shader"), QString(), tr("Shader Files (*.shader)"));
|
||||||
|
if (fileName.isEmpty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!fileName.endsWith("shader", Qt::CaseInsensitive))
|
||||||
|
fileName += ".shader";
|
||||||
|
|
||||||
|
Nz::File file(fileName.toStdString(), Nz::OpenMode_WriteOnly);
|
||||||
|
file.Write(Nz::SerializeShader(shader));
|
||||||
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
|
QMessageBox::critical(this, tr("Compilation failed"), QString("Compilation failed: ") + e.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::OnGenerateGLSL()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Nz::GlslWriter writer;
|
||||||
|
std::string glsl = writer.Generate(ToShader());
|
||||||
|
|
||||||
|
std::cout << glsl << std::endl;
|
||||||
|
|
||||||
|
QTextEdit* output = new QTextEdit;
|
||||||
|
output->setReadOnly(true);
|
||||||
|
output->setText(QString::fromStdString(glsl));
|
||||||
|
output->setAttribute(Qt::WA_DeleteOnClose, true);
|
||||||
|
output->setWindowTitle("GLSL Output");
|
||||||
|
output->show();
|
||||||
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
|
QMessageBox::critical(this, tr("Generation failed"), QString("Generation failed: ") + e.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::OnLoad()
|
||||||
|
{
|
||||||
|
QString fileName = QFileDialog::getOpenFileName(this, tr("Open shader flow"), QString(), tr("Shader Flow Files (*.shaderflow)"));
|
||||||
|
if (fileName.isEmpty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
QFile file(fileName);
|
||||||
|
if (!file.open(QIODevice::ReadOnly))
|
||||||
|
{
|
||||||
|
QMessageBox::critical(this, tr("Failed to open file"), QString("Failed to open shader flow file: ") + file.errorString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject jsonDocument = QJsonDocument::fromJson(file.readAll()).object();
|
||||||
|
if (jsonDocument.isEmpty())
|
||||||
|
{
|
||||||
|
QMessageBox::critical(this, tr("Invalid file"), tr("Invalid shader flow file"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
m_shaderGraph.Load(jsonDocument);
|
||||||
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
|
QMessageBox::critical(this, tr("Invalid file"), tr("Invalid shader flow file: ") + e.what());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::OnSave()
|
||||||
|
{
|
||||||
|
QString fileName = QFileDialog::getSaveFileName(nullptr, tr("Open shader flow"), QString(), tr("Shader Flow Files (*.shaderflow)"));
|
||||||
|
if (fileName.isEmpty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!fileName.endsWith("shaderflow", Qt::CaseInsensitive))
|
||||||
|
fileName += ".shaderflow";
|
||||||
|
|
||||||
|
QFile file(fileName);
|
||||||
|
if (file.open(QIODevice::WriteOnly))
|
||||||
|
file.write(QJsonDocument(m_shaderGraph.Save()).toJson());
|
||||||
|
}
|
||||||
|
|
||||||
|
Nz::ShaderAst MainWindow::ToShader()
|
||||||
{
|
{
|
||||||
Nz::ShaderNodes::StatementPtr shaderAst = m_shaderGraph.ToAst();
|
Nz::ShaderNodes::StatementPtr shaderAst = m_shaderGraph.ToAst();
|
||||||
|
|
||||||
|
|
@ -142,68 +231,5 @@ void MainWindow::OnCompileToGLSL()
|
||||||
|
|
||||||
shader.AddFunction("main", shaderAst);
|
shader.AddFunction("main", shaderAst);
|
||||||
|
|
||||||
Nz::File file("shader.shader", Nz::OpenMode_WriteOnly);
|
return shader;
|
||||||
file.Write(Nz::SerializeShader(shader));
|
|
||||||
|
|
||||||
Nz::GlslWriter writer;
|
|
||||||
Nz::String glsl = writer.Generate(shader);
|
|
||||||
|
|
||||||
std::cout << glsl << std::endl;
|
|
||||||
|
|
||||||
QTextEdit* output = new QTextEdit;
|
|
||||||
output->setReadOnly(true);
|
|
||||||
output->setText(QString::fromUtf8(glsl.GetConstBuffer(), int(glsl.GetSize())));
|
|
||||||
output->setAttribute(Qt::WA_DeleteOnClose, true);
|
|
||||||
output->setWindowTitle("GLSL Output");
|
|
||||||
output->show();
|
|
||||||
}
|
|
||||||
catch (const std::exception& e)
|
|
||||||
{
|
|
||||||
QMessageBox::critical(this, tr("Compilation failed"), QString("Compilation failed: ") + e.what());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::OnLoad()
|
|
||||||
{
|
|
||||||
QString fileName = QFileDialog::getOpenFileName(this, tr("Open shader flow"), QDir::homePath(), tr("Shader Flow Files (*.shaderflow)"));
|
|
||||||
if (fileName.isEmpty())
|
|
||||||
return;
|
|
||||||
|
|
||||||
QFile file(fileName);
|
|
||||||
if (!file.open(QIODevice::ReadOnly))
|
|
||||||
{
|
|
||||||
QMessageBox::critical(this, tr("Failed to open file"), QString("Failed to open shader flow file: ") + file.errorString());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject jsonDocument = QJsonDocument::fromJson(file.readAll()).object();
|
|
||||||
if (jsonDocument.isEmpty())
|
|
||||||
{
|
|
||||||
QMessageBox::critical(this, tr("Invalid file"), tr("Invalid shader flow file"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
m_shaderGraph.Load(jsonDocument);
|
|
||||||
}
|
|
||||||
catch (const std::exception& e)
|
|
||||||
{
|
|
||||||
QMessageBox::critical(this, tr("Invalid file"), tr("Invalid shader flow file: ") + e.what());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::OnSave()
|
|
||||||
{
|
|
||||||
QString fileName = QFileDialog::getSaveFileName(nullptr, tr("Open shader flow"), QDir::homePath(), tr("Shader Flow Files (*.shaderflow)"));
|
|
||||||
if (fileName.isEmpty())
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (!fileName.endsWith("flow", Qt::CaseInsensitive))
|
|
||||||
fileName += ".shaderflow";
|
|
||||||
|
|
||||||
QFile file(fileName);
|
|
||||||
if (file.open(QIODevice::WriteOnly))
|
|
||||||
file.write(QJsonDocument(m_shaderGraph.Save()).toJson());
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,11 @@
|
||||||
|
|
||||||
class NodeEditor;
|
class NodeEditor;
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
class ShaderAst;
|
||||||
|
}
|
||||||
|
|
||||||
class MainWindow : public QMainWindow
|
class MainWindow : public QMainWindow
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
@ -17,9 +22,11 @@ class MainWindow : public QMainWindow
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void BuildMenu();
|
void BuildMenu();
|
||||||
void OnCompileToGLSL();
|
void OnCompile();
|
||||||
|
void OnGenerateGLSL();
|
||||||
void OnLoad();
|
void OnLoad();
|
||||||
void OnSave();
|
void OnSave();
|
||||||
|
Nz::ShaderAst ToShader();
|
||||||
|
|
||||||
NazaraSlot(ShaderGraph, OnSelectedNodeUpdate, m_onSelectedNodeUpdate);
|
NazaraSlot(ShaderGraph, OnSelectedNodeUpdate, m_onSelectedNodeUpdate);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue