ShaderNode: Add inputs

This commit is contained in:
Lynix
2020-05-22 23:50:46 +02:00
parent 5169e0fe83
commit 206724c911
20 changed files with 620 additions and 12 deletions

View File

@@ -0,0 +1,132 @@
#include <ShaderGraph.hpp>
#include <DataModels/InputValue.hpp>
#include <DataModels/VecValue.hpp>
#include <Nazara/Renderer/ShaderBuilder.hpp>
InputValue::InputValue(ShaderGraph& graph) :
ShaderNode(graph),
m_currentInputIndex(0)
{
m_layout = new QVBoxLayout;
m_inputSelection = new QComboBox;
m_inputSelection->setStyleSheet("background-color: rgba(255,255,255,255)");
connect(m_inputSelection, qOverload<int>(&QComboBox::currentIndexChanged), [&](int index)
{
if (index < 0)
return;
m_currentInputIndex = static_cast<std::size_t>(index);
UpdatePreview();
});
m_layout->addWidget(m_inputSelection);
m_previewLabel = new QLabel;
m_layout->addWidget(m_previewLabel);
m_widget = new QWidget;
m_widget->setStyleSheet("background-color: rgba(0,0,0,0)");
m_widget->setLayout(m_layout);
m_onInputListUpdateSlot.Connect(GetGraph().OnInputListUpdate, [&](ShaderGraph*) { UpdateInputList(); });
m_onInputUpdateSlot.Connect(GetGraph().OnInputUpdate, [&](ShaderGraph*, std::size_t inputIndex)
{
if (m_currentInputIndex == inputIndex)
UpdatePreview();
});
UpdateInputList();
UpdatePreview();
}
QWidget* InputValue::embeddedWidget()
{
return m_widget;
}
unsigned int InputValue::nPorts(QtNodes::PortType portType) const
{
switch (portType)
{
case QtNodes::PortType::In: return 0;
case QtNodes::PortType::Out: return 1;
}
return 0;
}
void InputValue::UpdatePreview()
{
if (m_inputSelection->count() == 0)
return;
Q_EMIT dataUpdated(0);
}
void InputValue::UpdateInputList()
{
QString currentInput = m_inputSelection->currentText();
m_inputSelection->clear();
for (const auto& inputEntry : GetGraph().GetInputs())
m_inputSelection->addItem(QString::fromStdString(inputEntry.name));
m_inputSelection->setCurrentText(currentInput);
}
Nz::ShaderAst::ExpressionPtr InputValue::GetExpression(Nz::ShaderAst::ExpressionPtr* expressions, std::size_t count) const
{
assert(count == 0);
const auto& inputEntry = GetGraph().GetInput(m_currentInputIndex);
Nz::ShaderAst::ExpressionType expression = [&]
{
switch (inputEntry.type)
{
case InputType::Bool: return Nz::ShaderAst::ExpressionType::Boolean;
case InputType::Float1: return Nz::ShaderAst::ExpressionType::Float1;
case InputType::Float2: return Nz::ShaderAst::ExpressionType::Float2;
case InputType::Float3: return Nz::ShaderAst::ExpressionType::Float3;
case InputType::Float4: return Nz::ShaderAst::ExpressionType::Float4;
}
assert(false);
throw std::runtime_error("Unhandled input type");
}();
return Nz::ShaderBuilder::Input(inputEntry.name, expression);
}
auto InputValue::dataType(QtNodes::PortType portType, QtNodes::PortIndex portIndex) const -> QtNodes::NodeDataType
{
assert(portType == QtNodes::PortType::Out);
assert(portIndex == 0);
const auto& inputEntry = GetGraph().GetInput(m_currentInputIndex);
switch (inputEntry.type)
{
//case InputType::Bool: return Nz::ShaderAst::ExpressionType::Boolean;
//case InputType::Float1: return Nz::ShaderAst::ExpressionType::Float1;
case InputType::Float2: return Vec2Data::Type();
//case InputType::Float3: return Nz::ShaderAst::ExpressionType::Float3;
case InputType::Float4: return Vec4Data::Type();
}
assert(false);
throw std::runtime_error("Unhandled input type");
}
std::shared_ptr<QtNodes::NodeData> InputValue::outData(QtNodes::PortIndex port)
{
assert(port == 0);
const auto& inputEntry = GetGraph().GetInput(m_currentInputIndex);
auto vecData = std::make_shared<Vec4Data>();
vecData->preview = QImage();
return vecData;
}

View File

@@ -0,0 +1,47 @@
#pragma once
#ifndef NAZARA_SHADERNODES_INPUTVALUE_HPP
#define NAZARA_SHADERNODES_INPUTVALUE_HPP
#include <QtWidgets/QComboBox>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QLabel>
#include <ShaderGraph.hpp>
#include <DataModels/ShaderNode.hpp>
#include <array>
class InputValue : public ShaderNode
{
public:
InputValue(ShaderGraph& graph);
~InputValue() = default;
Nz::ShaderAst::ExpressionPtr GetExpression(Nz::ShaderAst::ExpressionPtr* /*expressions*/, std::size_t count) const override;
QString caption() const override { return "Input"; }
QString name() const override { return "Input"; }
QWidget* embeddedWidget() override;
unsigned int nPorts(QtNodes::PortType portType) const override;
QtNodes::NodeDataType dataType(QtNodes::PortType portType, QtNodes::PortIndex portIndex) const override;
std::shared_ptr<QtNodes::NodeData> outData(QtNodes::PortIndex port) override;
protected:
void UpdatePreview();
void UpdateInputList();
NazaraSlot(ShaderGraph, OnInputListUpdate, m_onInputListUpdateSlot);
NazaraSlot(ShaderGraph, OnInputUpdate, m_onInputUpdateSlot);
std::size_t m_currentInputIndex;
QComboBox* m_inputSelection;
QLabel* m_previewLabel;
QWidget* m_widget;
QVBoxLayout* m_layout;
};
#include <DataModels/InputValue.inl>
#endif

View File

@@ -0,0 +1,2 @@
#include <DataModels/InputValue.hpp>
#include <array>

View File

@@ -94,7 +94,20 @@ Nz::ShaderAst::ExpressionPtr SampleTexture::GetExpression(Nz::ShaderAst::Express
{
assert(count == 1);
auto sampler = Nz::ShaderBuilder::Uniform("Texture0", Nz::ShaderAst::ExpressionType::Sampler2D);
const auto& textureEntry = GetGraph().GetTexture(m_currentTextureIndex);
Nz::ShaderAst::ExpressionType expression = [&]
{
switch (textureEntry.type)
{
case TextureType::Sampler2D: return Nz::ShaderAst::ExpressionType::Sampler2D;
}
assert(false);
throw std::runtime_error("Unhandled texture type");
}();
auto sampler = Nz::ShaderBuilder::Uniform(textureEntry.name, expression);
return Nz::ShaderBuilder::Sample2D(sampler, expressions[0]);
}