ShaderNode: Extract texture from SampleTexture

Add TextureData and TextureValue node
This commit is contained in:
Lynix 2020-05-28 10:50:38 +02:00
parent 6ff670f13f
commit eabb8a630d
9 changed files with 307 additions and 111 deletions

View File

@ -7,20 +7,6 @@ ShaderNode(graph)
{
m_output = std::make_shared<Vec4Data>();
m_onTextureListUpdateSlot.Connect(GetGraph().OnTextureListUpdate, [&](ShaderGraph*) { OnTextureListUpdate(); });
m_onTexturePreviewUpdateSlot.Connect(GetGraph().OnTexturePreviewUpdate, [&](ShaderGraph*, std::size_t textureIndex)
{
if (m_currentTextureIndex == textureIndex)
UpdatePreview();
});
if (graph.GetTextureCount() > 0)
{
auto& firstInput = graph.GetTexture(0);
m_currentTextureIndex = 0;
m_currentTextureText = firstInput.name;
}
UpdateOutput();
}
@ -28,45 +14,28 @@ unsigned int SampleTexture::nPorts(QtNodes::PortType portType) const
{
switch (portType)
{
case QtNodes::PortType::In: return 1;
case QtNodes::PortType::In: return 2;
case QtNodes::PortType::Out: return 1;
}
return 0;
}
void SampleTexture::OnTextureListUpdate()
{
m_currentTextureIndex.reset();
std::size_t inputIndex = 0;
for (const auto& textureEntry : GetGraph().GetTextures())
{
if (textureEntry.name == m_currentTextureText)
{
m_currentTextureIndex = inputIndex;
break;
}
inputIndex++;
}
}
void SampleTexture::UpdateOutput()
{
QImage& output = m_output->preview;
if (!m_currentTextureIndex || !m_uv)
if (!m_texture || !m_uv)
{
output = QImage(1, 1, QImage::Format_RGBA8888);
output.fill(QColor::fromRgb(0, 0, 0, 0));
return;
}
const auto& textureEntry = GetGraph().GetTexture(*m_currentTextureIndex);
const QImage& texturePreview = m_texture->preview;
int textureWidth = textureEntry.preview.width();
int textureHeight = textureEntry.preview.height();
int textureWidth = texturePreview.width();
int textureHeight = texturePreview.height();
const QImage& uv = m_uv->preview;
@ -77,7 +46,7 @@ void SampleTexture::UpdateOutput()
std::uint8_t* outputPtr = output.bits();
const std::uint8_t* uvPtr = uv.constBits();
const std::uint8_t* texturePtr = textureEntry.preview.constBits();
const std::uint8_t* texturePtr = texturePreview.constBits();
for (int y = 0; y < uvHeight; ++y)
{
for (int x = 0; x < uvWidth; ++x)
@ -104,57 +73,21 @@ void SampleTexture::UpdateOutput()
bool SampleTexture::ComputePreview(QPixmap& pixmap)
{
if (!m_currentTextureIndex || !m_uv)
if (!m_texture || !m_uv)
return false;
pixmap = QPixmap::fromImage(m_output->preview);
return true;
}
void SampleTexture::BuildNodeEdition(QFormLayout* layout)
{
ShaderNode::BuildNodeEdition(layout);
QComboBox* textureSelection = new QComboBox;
connect(textureSelection, qOverload<int>(&QComboBox::currentIndexChanged), [&](int index)
{
if (index >= 0)
m_currentTextureIndex = static_cast<std::size_t>(index);
else
m_currentTextureIndex.reset();
UpdateOutput();
});
for (const auto& textureEntry : GetGraph().GetTextures())
textureSelection->addItem(QString::fromStdString(textureEntry.name));
layout->addRow(tr("Texture"), textureSelection);
}
Nz::ShaderAst::ExpressionPtr SampleTexture::GetExpression(Nz::ShaderAst::ExpressionPtr* expressions, std::size_t count) const
{
if (!m_currentTextureIndex || !m_uv)
if (!m_texture || !m_uv)
throw std::runtime_error("invalid inputs");
assert(count == 1);
assert(count == 2);
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]);
return Nz::ShaderBuilder::Sample2D(expressions[0], expressions[1]);
}
auto SampleTexture::dataType(QtNodes::PortType portType, QtNodes::PortIndex portIndex) const -> QtNodes::NodeDataType
@ -163,8 +96,14 @@ auto SampleTexture::dataType(QtNodes::PortType portType, QtNodes::PortIndex port
{
case QtNodes::PortType::In:
{
assert(portIndex == 0);
return Vec2Data::Type();
switch (portIndex)
{
case 0: return Texture2Data::Type();
case 1: return Vec2Data::Type();
}
assert(false);
throw std::runtime_error("invalid port index");
}
case QtNodes::PortType::Out:
@ -175,7 +114,7 @@ auto SampleTexture::dataType(QtNodes::PortType portType, QtNodes::PortIndex port
default:
assert(false);
throw std::runtime_error("Invalid PortType");
throw std::runtime_error("invalid PortType");
}
}
@ -185,8 +124,14 @@ QString SampleTexture::portCaption(QtNodes::PortType portType, QtNodes::PortInde
{
case QtNodes::PortType::In:
{
assert(portIndex == 0);
return tr("UV");
switch (portIndex)
{
case 0: return tr("Texture");
case 1: return tr("UV");
}
assert(false);
throw std::runtime_error("invalid port index");
}
case QtNodes::PortType::Out:
@ -210,24 +155,45 @@ std::shared_ptr<QtNodes::NodeData> SampleTexture::outData(QtNodes::PortIndex por
{
assert(port == 0);
if (!m_currentTextureIndex)
return nullptr;
return m_output;
}
void SampleTexture::setInData(std::shared_ptr<QtNodes::NodeData> value, int index)
{
assert(index == 0);
if (value)
switch (index)
{
assert(dynamic_cast<Vec2Data*>(value.get()) != nullptr);
case 0:
{
if (value)
{
assert(dynamic_cast<Texture2Data*>(value.get()) != nullptr);
m_uv = std::static_pointer_cast<Vec2Data>(value);
m_texture = std::static_pointer_cast<Texture2Data>(value);
}
else
m_texture.reset();
break;
}
case 1:
{
if (value)
{
assert(dynamic_cast<Vec2Data*>(value.get()) != nullptr);
m_uv = std::static_pointer_cast<Vec2Data>(value);
}
else
m_uv.reset();
break;
}
default:
assert(false);
throw std::runtime_error("Invalid PortType");
}
else
m_uv.reset();
UpdateOutput();
}

View File

@ -8,6 +8,7 @@
#include <QtWidgets/QLabel>
#include <ShaderNode/ShaderGraph.hpp>
#include <ShaderNode/DataModels/ShaderNode.hpp>
#include <ShaderNode/DataTypes/TextureData.hpp>
#include <ShaderNode/DataTypes/VecData.hpp>
class SampleTexture : public ShaderNode
@ -16,8 +17,6 @@ class SampleTexture : public ShaderNode
SampleTexture(ShaderGraph& graph);
~SampleTexture() = default;
void BuildNodeEdition(QFormLayout* layout) override;
Nz::ShaderAst::ExpressionPtr GetExpression(Nz::ShaderAst::ExpressionPtr* /*expressions*/, std::size_t count) const override;
QString caption() const override { return "Sample texture"; }
@ -37,16 +36,11 @@ class SampleTexture : public ShaderNode
protected:
bool ComputePreview(QPixmap& pixmap) override;
void OnTextureListUpdate();
void UpdateOutput();
NazaraSlot(ShaderGraph, OnTextureListUpdate, m_onTextureListUpdateSlot);
NazaraSlot(ShaderGraph, OnTexturePreviewUpdate, m_onTexturePreviewUpdateSlot);
std::optional<std::size_t> m_currentTextureIndex;
std::shared_ptr<Texture2Data> m_texture;
std::shared_ptr<Vec2Data> m_uv;
std::shared_ptr<Vec4Data> m_output;
std::string m_currentTextureText;
};
#include <ShaderNode/DataModels/SampleTexture.inl>

View File

@ -0,0 +1,146 @@
#include <ShaderNode/DataModels/TextureValue.hpp>
#include <ShaderNode/ShaderGraph.hpp>
#include <ShaderNode/DataTypes/TextureData.hpp>
#include <ShaderNode/DataTypes/VecData.hpp>
#include <Nazara/Renderer/ShaderBuilder.hpp>
#include <QtWidgets/QFormLayout>
TextureValue::TextureValue(ShaderGraph& graph) :
ShaderNode(graph)
{
m_onTextureListUpdateSlot.Connect(GetGraph().OnTextureListUpdate, [&](ShaderGraph*) { OnTextureListUpdate(); });
m_onTexturePreviewUpdateSlot.Connect(GetGraph().OnTexturePreviewUpdate, [&](ShaderGraph*, std::size_t textureIndex)
{
if (m_currentTextureIndex == textureIndex)
UpdatePreview();
});
if (graph.GetTextureCount() > 0)
{
auto& firstInput = graph.GetTexture(0);
m_currentTextureIndex = 0;
m_currentTextureText = firstInput.name;
}
EnablePreview(true);
}
unsigned int TextureValue::nPorts(QtNodes::PortType portType) const
{
switch (portType)
{
case QtNodes::PortType::In: return 0;
case QtNodes::PortType::Out: return 1;
}
return 0;
}
void TextureValue::OnTextureListUpdate()
{
m_currentTextureIndex.reset();
std::size_t inputIndex = 0;
for (const auto& textureEntry : GetGraph().GetTextures())
{
if (textureEntry.name == m_currentTextureText)
{
m_currentTextureIndex = inputIndex;
break;
}
inputIndex++;
}
}
bool TextureValue::ComputePreview(QPixmap& pixmap)
{
if (!m_currentTextureIndex)
return false;
const ShaderGraph& graph = GetGraph();
const auto& textureEntry = graph.GetTexture(*m_currentTextureIndex);
pixmap = QPixmap::fromImage(textureEntry.preview);
return true;
}
void TextureValue::BuildNodeEdition(QFormLayout* layout)
{
ShaderNode::BuildNodeEdition(layout);
QComboBox* textureSelection = new QComboBox;
connect(textureSelection, qOverload<int>(&QComboBox::currentIndexChanged), [&](int index)
{
if (index >= 0)
m_currentTextureIndex = static_cast<std::size_t>(index);
else
m_currentTextureIndex.reset();
UpdatePreview();
Q_EMIT dataUpdated(0);
});
for (const auto& textureEntry : GetGraph().GetTextures())
textureSelection->addItem(QString::fromStdString(textureEntry.name));
layout->addRow(tr("Texture"), textureSelection);
}
Nz::ShaderAst::ExpressionPtr TextureValue::GetExpression(Nz::ShaderAst::ExpressionPtr* /*expressions*/, std::size_t count) const
{
if (!m_currentTextureIndex)
throw std::runtime_error("invalid inputs");
assert(count == 0);
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");
}();
return Nz::ShaderBuilder::Uniform(textureEntry.name, expression);
}
auto TextureValue::dataType(QtNodes::PortType portType, QtNodes::PortIndex portIndex) const -> QtNodes::NodeDataType
{
assert(portType == QtNodes::PortType::Out);
assert(portIndex == 0);
return Vec4Data::Type();
}
std::shared_ptr<QtNodes::NodeData> TextureValue::outData(QtNodes::PortIndex port)
{
assert(port == 0);
if (!m_currentTextureIndex)
return nullptr;
const ShaderGraph& graph = GetGraph();
const auto& textureEntry = graph.GetTexture(*m_currentTextureIndex);
std::shared_ptr<TextureData> textureData;
switch (textureEntry.type)
{
case TextureType::Sampler2D:
textureData = std::make_shared<Texture2Data>();
break;
}
assert(textureData);
textureData->preview = textureEntry.preview;
return textureData;
}

View File

@ -0,0 +1,45 @@
#pragma once
#ifndef NAZARA_SHADERNODES_TEXTUREVALUE_HPP
#define NAZARA_SHADERNODES_TEXTUREVALUE_HPP
#include <QtWidgets/QComboBox>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QLabel>
#include <ShaderNode/ShaderGraph.hpp>
#include <ShaderNode/DataModels/ShaderNode.hpp>
#include <array>
class TextureValue : public ShaderNode
{
public:
TextureValue(ShaderGraph& graph);
~TextureValue() = default;
void BuildNodeEdition(QFormLayout* layout) override;
Nz::ShaderAst::ExpressionPtr GetExpression(Nz::ShaderAst::ExpressionPtr* /*expressions*/, std::size_t count) const override;
QString caption() const override { return "Texture"; }
QString name() const override { return "Texture"; }
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:
bool ComputePreview(QPixmap& pixmap) override;
void OnTextureListUpdate();
NazaraSlot(ShaderGraph, OnTextureListUpdate, m_onTextureListUpdateSlot);
NazaraSlot(ShaderGraph, OnTexturePreviewUpdate, m_onTexturePreviewUpdateSlot);
std::optional<std::size_t> m_currentTextureIndex;
std::string m_currentTextureText;
};
#include <ShaderNode/DataModels/TextureValue.inl>
#endif

View File

@ -0,0 +1 @@
#include <ShaderNode/DataModels/TextureValue.hpp>

View File

@ -0,0 +1 @@
#include <ShaderNode/DataTypes/TextureData.hpp>

View File

@ -0,0 +1,32 @@
#pragma once
#ifndef NAZARA_SHADERNODES_TEXTUREDATA_HPP
#define NAZARA_SHADERNODES_TEXTUREDATA_HPP
#include <Nazara/Renderer/ShaderAst.hpp>
#include <nodes/NodeData>
#include <QtGui/QImage>
struct TextureData : public QtNodes::NodeData
{
inline TextureData();
QImage preview;
};
struct Texture2Data : public TextureData
{
QtNodes::NodeDataType type() const override
{
return Type();
}
static QtNodes::NodeDataType Type()
{
return { "tex2d", "Texture2D" };
}
};
#include <ShaderNode/DataTypes/TextureData.inl>
#endif

View File

@ -0,0 +1,7 @@
#include <ShaderNode/DataTypes/TextureData.hpp>
inline TextureData::TextureData() :
preview(64, 64, QImage::Format_RGBA8888)
{
preview.fill(QColor::fromRgb(255, 255, 255, 0));
}

View File

@ -5,6 +5,7 @@
#include <ShaderNode/DataModels/InputValue.hpp>
#include <ShaderNode/DataModels/SampleTexture.hpp>
#include <ShaderNode/DataModels/ShaderNode.hpp>
#include <ShaderNode/DataModels/TextureValue.hpp>
#include <ShaderNode/DataModels/VecBinOp.hpp>
#include <ShaderNode/DataModels/VecValue.hpp>
#include <ShaderNode/Previews/QuadPreview.hpp>
@ -46,22 +47,26 @@ m_flowScene(BuildRegistry())
UpdateTexturePreview(0, QImage(R"(C:\Users\Lynix\Pictures\potatavril.png)"));
auto& node1 = m_flowScene.createNode(std::make_unique<InputValue>(*this));
auto& node1 = m_flowScene.createNode(std::make_unique<TextureValue>(*this));
node1.nodeGraphicsObject().setPos(0, 200);
auto& node2 = m_flowScene.createNode(std::make_unique<SampleTexture>(*this));
node2.nodeGraphicsObject().setPos(200, 200);
auto& node2 = m_flowScene.createNode(std::make_unique<InputValue>(*this));
node2.nodeGraphicsObject().setPos(50, 350);
auto& node3 = m_flowScene.createNode(std::make_unique<Vec4Mul>(*this));
node3.nodeGraphicsObject().setPos(400, 200);
auto& node3 = m_flowScene.createNode(std::make_unique<SampleTexture>(*this));
node3.nodeGraphicsObject().setPos(200, 200);
auto& node4 = m_flowScene.createNode(std::make_unique<FragmentOutput>(*this));
node4.nodeGraphicsObject().setPos(600, 300);
auto& node4 = m_flowScene.createNode(std::make_unique<Vec4Mul>(*this));
node4.nodeGraphicsObject().setPos(400, 200);
m_flowScene.createConnection(node2, 0, node1, 0);
m_flowScene.createConnection(node3, 0, node2, 0);
auto& node5 = m_flowScene.createNode(std::make_unique<FragmentOutput>(*this));
node5.nodeGraphicsObject().setPos(600, 300);
m_flowScene.createConnection(node3, 0, node1, 0);
m_flowScene.createConnection(node3, 1, node2, 0);
m_flowScene.createConnection(node4, 0, node3, 0);
m_flowScene.createConnection(node4, 1, node3, 0);
m_flowScene.createConnection(node5, 0, node4, 0);
}
ShaderGraph::~ShaderGraph()
@ -105,8 +110,6 @@ Nz::ShaderAst::StatementPtr ShaderGraph::ToAst()
std::function<void(QtNodes::Node*)> DetectVariables;
DetectVariables = [&](QtNodes::Node* node)
{
ShaderNode* shaderNode = static_cast<ShaderNode*>(node->nodeDataModel());
auto it = usageCount.find(node->id());
if (it == usageCount.end())
{
@ -226,6 +229,7 @@ std::shared_ptr<QtNodes::DataModelRegistry> ShaderGraph::BuildRegistry()
RegisterShaderNode<FragmentOutput>(*this, registry, "Outputs");
RegisterShaderNode<InputValue>(*this, registry, "Inputs");
RegisterShaderNode<SampleTexture>(*this, registry, "Texture");
RegisterShaderNode<TextureValue>(*this, registry, "Texture");
RegisterShaderNode<Vec2Add>(*this, registry, "Vector operations");
RegisterShaderNode<Vec2Mul>(*this, registry, "Vector operations");
RegisterShaderNode<Vec2Sub>(*this, registry, "Vector operations");